Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Sony OpenMG (OMA) demuxer | ||
3 | * | ||
4 | * Copyright (c) 2008, 2013 Maxim Poliakovski | ||
5 | * 2008 Benjamin Larsson | ||
6 | * 2011 David Goldwich | ||
7 | * | ||
8 | * This file is part of FFmpeg. | ||
9 | * | ||
10 | * FFmpeg is free software; you can redistribute it and/or | ||
11 | * modify it under the terms of the GNU Lesser General Public | ||
12 | * License as published by the Free Software Foundation; either | ||
13 | * version 2.1 of the License, or (at your option) any later version. | ||
14 | * | ||
15 | * FFmpeg is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * Lesser General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU Lesser General Public | ||
21 | * License along with FFmpeg; if not, write to the Free Software | ||
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
23 | */ | ||
24 | |||
25 | /** | ||
26 | * @file | ||
27 | * This is a demuxer for Sony OpenMG Music files | ||
28 | * | ||
29 | * Known file extensions: ".oma", "aa3" | ||
30 | * The format of such files consists of three parts: | ||
31 | * - "ea3" header carrying overall info and metadata. Except for starting with | ||
32 | * "ea" instead of "ID", it's an ID3v2 header. | ||
33 | * - "EA3" header is a Sony-specific header containing information about | ||
34 | * the OpenMG file: codec type (usually ATRAC, can also be MP3 or WMA), | ||
35 | * codec specific info (packet size, sample rate, channels and so on) | ||
36 | * and DRM related info (file encryption, content id). | ||
37 | * - Sound data organized in packets follow the EA3 header | ||
38 | * (can be encrypted using the Sony DRM!). | ||
39 | * | ||
40 | * Supported decoders: ATRAC3, ATRAC3+, MP3, LPCM | ||
41 | */ | ||
42 | |||
43 | #include <inttypes.h> | ||
44 | |||
45 | #include "libavutil/channel_layout.h" | ||
46 | #include "avformat.h" | ||
47 | #include "internal.h" | ||
48 | #include "libavutil/intreadwrite.h" | ||
49 | #include "libavutil/des.h" | ||
50 | #include "libavutil/mathematics.h" | ||
51 | #include "oma.h" | ||
52 | #include "pcm.h" | ||
53 | #include "id3v2.h" | ||
54 | |||
55 | |||
56 | static const uint64_t leaf_table[] = { | ||
57 | 0xd79e8283acea4620, 0x7a9762f445afd0d8, | ||
58 | 0x354d60a60b8c79f1, 0x584e1cde00b07aee, | ||
59 | 0x1573cd93da7df623, 0x47f98d79620dd535 | ||
60 | }; | ||
61 | |||
62 | /** map ATRAC-X channel id to internal channel layout */ | ||
63 | static const AVChannelLayout oma_chid_to_native_layout[7] = { | ||
64 | AV_CHANNEL_LAYOUT_MONO, | ||
65 | AV_CHANNEL_LAYOUT_STEREO, | ||
66 | AV_CHANNEL_LAYOUT_SURROUND, | ||
67 | AV_CHANNEL_LAYOUT_4POINT0, | ||
68 | AV_CHANNEL_LAYOUT_5POINT1_BACK, | ||
69 | AV_CHANNEL_LAYOUT_6POINT1_BACK, | ||
70 | AV_CHANNEL_LAYOUT_7POINT1 | ||
71 | }; | ||
72 | |||
73 | typedef struct OMAContext { | ||
74 | uint64_t content_start; | ||
75 | int encrypted; | ||
76 | uint16_t k_size; | ||
77 | uint16_t e_size; | ||
78 | uint16_t i_size; | ||
79 | uint16_t s_size; | ||
80 | uint32_t rid; | ||
81 | uint8_t r_val[24]; | ||
82 | uint8_t n_val[24]; | ||
83 | uint8_t m_val[8]; | ||
84 | uint8_t s_val[8]; | ||
85 | uint8_t sm_val[8]; | ||
86 | uint8_t e_val[8]; | ||
87 | uint8_t iv[8]; | ||
88 | struct AVDES *av_des; | ||
89 | |||
90 | int (*read_packet)(AVFormatContext *s, AVPacket *pkt); | ||
91 | } OMAContext; | ||
92 | |||
93 | 7 | static int oma_read_close(AVFormatContext *s) | |
94 | { | ||
95 | 7 | OMAContext *oc = s->priv_data; | |
96 | 7 | av_freep(&oc->av_des); | |
97 | 7 | return 0; | |
98 | } | ||
99 | |||
100 | ✗ | static void hex_log(AVFormatContext *s, int level, | |
101 | const char *name, const uint8_t *value, int len) | ||
102 | { | ||
103 | char buf[33]; | ||
104 | ✗ | len = FFMIN(len, 16); | |
105 | ✗ | if (av_log_get_level() < level) | |
106 | ✗ | return; | |
107 | ✗ | ff_data_to_hex(buf, value, len, 1); | |
108 | ✗ | av_log(s, level, "%s: %s\n", name, buf); | |
109 | } | ||
110 | |||
111 | ✗ | static int kset(AVFormatContext *s, const uint8_t *r_val, const uint8_t *n_val, | |
112 | int len) | ||
113 | { | ||
114 | ✗ | OMAContext *oc = s->priv_data; | |
115 | |||
116 | ✗ | if (!r_val && !n_val) | |
117 | ✗ | return -1; | |
118 | |||
119 | ✗ | len = FFMIN(len, 16); | |
120 | |||
121 | /* use first 64 bits in the third round again */ | ||
122 | ✗ | if (r_val) { | |
123 | ✗ | if (r_val != oc->r_val) { | |
124 | ✗ | memset(oc->r_val, 0, 24); | |
125 | ✗ | memcpy(oc->r_val, r_val, len); | |
126 | } | ||
127 | ✗ | memcpy(&oc->r_val[16], r_val, 8); | |
128 | } | ||
129 | ✗ | if (n_val) { | |
130 | ✗ | if (n_val != oc->n_val) { | |
131 | ✗ | memset(oc->n_val, 0, 24); | |
132 | ✗ | memcpy(oc->n_val, n_val, len); | |
133 | } | ||
134 | ✗ | memcpy(&oc->n_val[16], n_val, 8); | |
135 | } | ||
136 | |||
137 | ✗ | return 0; | |
138 | } | ||
139 | |||
140 | #define OMA_RPROBE_M_VAL 48 + 1 | ||
141 | |||
142 | ✗ | static int rprobe(AVFormatContext *s, uint8_t *enc_header, unsigned size, | |
143 | const uint8_t *r_val) | ||
144 | { | ||
145 | ✗ | OMAContext *oc = s->priv_data; | |
146 | unsigned int pos; | ||
147 | struct AVDES *av_des; | ||
148 | |||
149 | ✗ | if (!enc_header || !r_val || | |
150 | ✗ | size < OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size + oc->i_size || | |
151 | size < OMA_RPROBE_M_VAL) | ||
152 | ✗ | return -1; | |
153 | |||
154 | ✗ | av_des = av_des_alloc(); | |
155 | ✗ | if (!av_des) | |
156 | ✗ | return AVERROR(ENOMEM); | |
157 | |||
158 | /* m_val */ | ||
159 | ✗ | av_des_init(av_des, r_val, 192, 1); | |
160 | ✗ | av_des_crypt(av_des, oc->m_val, &enc_header[48], 1, NULL, 1); | |
161 | |||
162 | /* s_val */ | ||
163 | ✗ | av_des_init(av_des, oc->m_val, 64, 0); | |
164 | ✗ | av_des_crypt(av_des, oc->s_val, NULL, 1, NULL, 0); | |
165 | |||
166 | /* sm_val */ | ||
167 | ✗ | pos = OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size; | |
168 | ✗ | av_des_init(av_des, oc->s_val, 64, 0); | |
169 | ✗ | av_des_mac(av_des, oc->sm_val, &enc_header[pos], (oc->i_size >> 3)); | |
170 | |||
171 | ✗ | pos += oc->i_size; | |
172 | |||
173 | ✗ | av_free(av_des); | |
174 | |||
175 | ✗ | return memcmp(&enc_header[pos], oc->sm_val, 8) ? -1 : 0; | |
176 | } | ||
177 | |||
178 | ✗ | static int nprobe(AVFormatContext *s, uint8_t *enc_header, unsigned size, | |
179 | const uint8_t *n_val) | ||
180 | { | ||
181 | ✗ | OMAContext *oc = s->priv_data; | |
182 | uint64_t pos; | ||
183 | uint32_t taglen, datalen; | ||
184 | struct AVDES *av_des; | ||
185 | |||
186 | ✗ | if (!enc_header || !n_val || | |
187 | ✗ | size < OMA_ENC_HEADER_SIZE + oc->k_size + 4) | |
188 | ✗ | return -1; | |
189 | |||
190 | ✗ | pos = OMA_ENC_HEADER_SIZE + oc->k_size; | |
191 | ✗ | if (!memcmp(&enc_header[pos], "EKB ", 4)) | |
192 | ✗ | pos += 32; | |
193 | |||
194 | ✗ | if (size < pos + 44) | |
195 | ✗ | return -1; | |
196 | |||
197 | ✗ | if (AV_RB32(&enc_header[pos]) != oc->rid) | |
198 | ✗ | av_log(s, AV_LOG_DEBUG, "Mismatching RID\n"); | |
199 | |||
200 | ✗ | taglen = AV_RB32(&enc_header[pos + 32]); | |
201 | ✗ | datalen = AV_RB32(&enc_header[pos + 36]) >> 4; | |
202 | |||
203 | ✗ | pos += 44LL + taglen; | |
204 | |||
205 | ✗ | if (pos + (((uint64_t)datalen) << 4) > size) | |
206 | ✗ | return -1; | |
207 | |||
208 | ✗ | av_des = av_des_alloc(); | |
209 | ✗ | if (!av_des) | |
210 | ✗ | return AVERROR(ENOMEM); | |
211 | |||
212 | ✗ | av_des_init(av_des, n_val, 192, 1); | |
213 | ✗ | while (datalen-- > 0) { | |
214 | ✗ | av_des_crypt(av_des, oc->r_val, &enc_header[pos], 2, NULL, 1); | |
215 | ✗ | kset(s, oc->r_val, NULL, 16); | |
216 | ✗ | if (!rprobe(s, enc_header, size, oc->r_val)) { | |
217 | ✗ | av_free(av_des); | |
218 | ✗ | return 0; | |
219 | } | ||
220 | ✗ | pos += 16; | |
221 | } | ||
222 | |||
223 | ✗ | av_free(av_des); | |
224 | ✗ | return -1; | |
225 | } | ||
226 | |||
227 | ✗ | static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header) | |
228 | { | ||
229 | ✗ | OMAContext *oc = s->priv_data; | |
230 | ✗ | ID3v2ExtraMetaGEOB *geob = NULL; | |
231 | uint8_t *gdata; | ||
232 | |||
233 | ✗ | oc->encrypted = 1; | |
234 | ✗ | av_log(s, AV_LOG_INFO, "File is encrypted\n"); | |
235 | |||
236 | /* find GEOB metadata */ | ||
237 | ✗ | for (; em; em = em->next) { | |
238 | ✗ | if (strcmp(em->tag, "GEOB")) | |
239 | ✗ | continue; | |
240 | ✗ | geob = &em->data.geob; | |
241 | ✗ | if (!strcmp(geob->description, "OMG_LSI") || | |
242 | ✗ | !strcmp(geob->description, "OMG_BKLSI")) | |
243 | break; | ||
244 | } | ||
245 | ✗ | if (!em) { | |
246 | ✗ | av_log(s, AV_LOG_ERROR, "No encryption header found\n"); | |
247 | ✗ | return AVERROR_INVALIDDATA; | |
248 | } | ||
249 | |||
250 | ✗ | if (geob->datasize < 64) { | |
251 | ✗ | av_log(s, AV_LOG_ERROR, | |
252 | "Invalid GEOB data size: %"PRIu32"\n", geob->datasize); | ||
253 | ✗ | return AVERROR_INVALIDDATA; | |
254 | } | ||
255 | |||
256 | ✗ | gdata = geob->data; | |
257 | |||
258 | ✗ | if (AV_RB16(gdata) != 1) | |
259 | ✗ | av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n"); | |
260 | |||
261 | ✗ | oc->k_size = AV_RB16(&gdata[2]); | |
262 | ✗ | oc->e_size = AV_RB16(&gdata[4]); | |
263 | ✗ | oc->i_size = AV_RB16(&gdata[6]); | |
264 | ✗ | oc->s_size = AV_RB16(&gdata[8]); | |
265 | |||
266 | ✗ | if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING ", 12)) { | |
267 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid encryption header\n"); | |
268 | ✗ | return AVERROR_INVALIDDATA; | |
269 | } | ||
270 | ✗ | if (OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size + oc->i_size + 8 > geob->datasize || | |
271 | ✗ | OMA_ENC_HEADER_SIZE + 48 > geob->datasize) { | |
272 | ✗ | av_log(s, AV_LOG_ERROR, "Too little GEOB data\n"); | |
273 | ✗ | return AVERROR_INVALIDDATA; | |
274 | } | ||
275 | ✗ | oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]); | |
276 | ✗ | av_log(s, AV_LOG_DEBUG, "RID: %.8"PRIx32"\n", oc->rid); | |
277 | |||
278 | ✗ | memcpy(oc->iv, &header[0x58], 8); | |
279 | ✗ | hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8); | |
280 | |||
281 | ✗ | hex_log(s, AV_LOG_DEBUG, "CBC-MAC", | |
282 | ✗ | &gdata[OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size + oc->i_size], | |
283 | 8); | ||
284 | |||
285 | ✗ | if (s->keylen > 0) { | |
286 | ✗ | kset(s, s->key, s->key, s->keylen); | |
287 | } | ||
288 | ✗ | if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) || | |
289 | ✗ | rprobe(s, gdata, geob->datasize, oc->r_val) < 0 && | |
290 | ✗ | nprobe(s, gdata, geob->datasize, oc->n_val) < 0) { | |
291 | int i; | ||
292 | ✗ | for (i = 0; i < FF_ARRAY_ELEMS(leaf_table); i += 2) { | |
293 | uint8_t buf[16]; | ||
294 | ✗ | AV_WL64(buf, leaf_table[i]); | |
295 | ✗ | AV_WL64(&buf[8], leaf_table[i + 1]); | |
296 | ✗ | kset(s, buf, buf, 16); | |
297 | ✗ | if (!rprobe(s, gdata, geob->datasize, oc->r_val) || | |
298 | ✗ | !nprobe(s, gdata, geob->datasize, oc->n_val)) | |
299 | break; | ||
300 | } | ||
301 | ✗ | if (i >= FF_ARRAY_ELEMS(leaf_table)) { | |
302 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid key\n"); | |
303 | ✗ | return AVERROR_INVALIDDATA; | |
304 | } | ||
305 | } | ||
306 | |||
307 | ✗ | oc->av_des = av_des_alloc(); | |
308 | ✗ | if (!oc->av_des) | |
309 | ✗ | return AVERROR(ENOMEM); | |
310 | |||
311 | /* e_val */ | ||
312 | ✗ | av_des_init(oc->av_des, oc->m_val, 64, 0); | |
313 | ✗ | av_des_crypt(oc->av_des, oc->e_val, | |
314 | ✗ | &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0); | |
315 | ✗ | hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8); | |
316 | |||
317 | /* init e_val */ | ||
318 | ✗ | av_des_init(oc->av_des, oc->e_val, 64, 1); | |
319 | |||
320 | ✗ | return 0; | |
321 | } | ||
322 | |||
323 | 2642 | static int read_packet(AVFormatContext *s, AVPacket *pkt) | |
324 | { | ||
325 | 2642 | OMAContext *oc = s->priv_data; | |
326 | 2642 | AVStream *st = s->streams[0]; | |
327 | 2642 | int packet_size = st->codecpar->block_align; | |
328 | 2642 | int byte_rate = st->codecpar->bit_rate >> 3; | |
329 | 2642 | int64_t pos = avio_tell(s->pb); | |
330 | 2642 | int ret = av_get_packet(s->pb, pkt, packet_size); | |
331 | |||
332 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2636 times.
|
2642 | if (ret < packet_size) |
333 | 6 | pkt->flags |= AV_PKT_FLAG_CORRUPT; | |
334 | |||
335 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2638 times.
|
2642 | if (ret < 0) |
336 | 4 | return ret; | |
337 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2638 times.
|
2638 | if (!ret) |
338 | ✗ | return AVERROR_EOF; | |
339 | |||
340 | 2638 | pkt->stream_index = 0; | |
341 | |||
342 |
2/4✓ Branch 0 taken 2638 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2638 times.
✗ Branch 3 not taken.
|
2638 | if (pos >= oc->content_start && byte_rate > 0) { |
343 | 2638 | pkt->pts = | |
344 | 2638 | pkt->dts = av_rescale(pos - oc->content_start, st->time_base.den, | |
345 | 2638 | byte_rate * (int64_t)st->time_base.num); | |
346 | } | ||
347 | |||
348 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2638 times.
|
2638 | if (oc->encrypted) { |
349 | /* previous unencrypted block saved in IV for | ||
350 | * the next packet (CBC mode) */ | ||
351 | ✗ | if (ret == packet_size) | |
352 | ✗ | av_des_crypt(oc->av_des, pkt->data, pkt->data, | |
353 | ✗ | (packet_size >> 3), oc->iv, 1); | |
354 | else | ||
355 | ✗ | memset(oc->iv, 0, 8); | |
356 | } | ||
357 | |||
358 | 2638 | return ret; | |
359 | } | ||
360 | |||
361 | ✗ | static int aal_read_packet(AVFormatContext *s, AVPacket *pkt) | |
362 | { | ||
363 | ✗ | int64_t pos = avio_tell(s->pb); | |
364 | int ret, pts; | ||
365 | int packet_size; | ||
366 | unsigned tag; | ||
367 | |||
368 | ✗ | if (avio_feof(s->pb)) | |
369 | ✗ | return AVERROR_EOF; | |
370 | |||
371 | ✗ | tag = avio_rb24(s->pb); | |
372 | ✗ | if (tag == 0) | |
373 | ✗ | return AVERROR_EOF; | |
374 | ✗ | else if (tag != MKBETAG(0,'B','L','K')) | |
375 | ✗ | return AVERROR_INVALIDDATA; | |
376 | |||
377 | ✗ | avio_skip(s->pb, 1); | |
378 | ✗ | packet_size = avio_rb16(s->pb); | |
379 | ✗ | avio_skip(s->pb, 2); | |
380 | ✗ | pts = avio_rb32(s->pb); | |
381 | ✗ | avio_skip(s->pb, 12); | |
382 | ✗ | ret = av_get_packet(s->pb, pkt, packet_size); | |
383 | ✗ | if (ret < packet_size) | |
384 | ✗ | pkt->flags |= AV_PKT_FLAG_CORRUPT; | |
385 | |||
386 | ✗ | if (ret < 0) | |
387 | ✗ | return ret; | |
388 | ✗ | if (!ret) | |
389 | ✗ | return AVERROR_EOF; | |
390 | |||
391 | ✗ | pkt->stream_index = 0; | |
392 | ✗ | pkt->pos = pos; | |
393 | ✗ | if (s->streams[0]->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL) { | |
394 | ✗ | pkt->duration = 1024; | |
395 | ✗ | pkt->pts = pts * 1024LL; | |
396 | } else { | ||
397 | ✗ | pkt->duration = 2048; | |
398 | ✗ | pkt->pts = pts * 2048LL; | |
399 | } | ||
400 | |||
401 | ✗ | return ret; | |
402 | } | ||
403 | |||
404 | 7 | static int oma_read_header(AVFormatContext *s) | |
405 | { | ||
406 | int ret, framesize, jsflag, samplerate; | ||
407 | uint32_t codec_params, channel_id; | ||
408 | int16_t eid; | ||
409 | uint8_t buf[EA3_HEADER_SIZE]; | ||
410 | uint8_t *edata; | ||
411 | AVStream *st; | ||
412 | ID3v2ExtraMeta *extra_meta; | ||
413 | 7 | OMAContext *oc = s->priv_data; | |
414 | |||
415 | 7 | ff_id3v2_read(s, ID3v2_EA3_MAGIC, &extra_meta, 0); | |
416 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if ((ret = ff_id3v2_parse_chapters(s, extra_meta)) < 0) { |
417 | ✗ | ff_id3v2_free_extra_meta(&extra_meta); | |
418 | ✗ | return ret; | |
419 | } | ||
420 | |||
421 | 7 | ret = avio_read(s->pb, buf, EA3_HEADER_SIZE); | |
422 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (ret < EA3_HEADER_SIZE) { |
423 | ✗ | ff_id3v2_free_extra_meta(&extra_meta); | |
424 | ✗ | return -1; | |
425 | } | ||
426 | |||
427 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}), 3) || |
428 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) { |
429 | ✗ | ff_id3v2_free_extra_meta(&extra_meta); | |
430 | ✗ | av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n"); | |
431 | ✗ | return AVERROR_INVALIDDATA; | |
432 | } | ||
433 | |||
434 | 7 | oc->content_start = avio_tell(s->pb); | |
435 | |||
436 | /* encrypted file */ | ||
437 | 7 | eid = AV_RB16(&buf[6]); | |
438 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
7 | if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) { |
439 | ✗ | ff_id3v2_free_extra_meta(&extra_meta); | |
440 | ✗ | return -1; | |
441 | } | ||
442 | |||
443 | 7 | ff_id3v2_free_extra_meta(&extra_meta); | |
444 | |||
445 | 7 | codec_params = AV_RB24(&buf[33]); | |
446 | |||
447 | 7 | st = avformat_new_stream(s, NULL); | |
448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!st) |
449 | ✗ | return AVERROR(ENOMEM); | |
450 | |||
451 | 7 | st->start_time = 0; | |
452 | 7 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
453 | 7 | st->codecpar->codec_tag = buf[32]; | |
454 | 14 | st->codecpar->codec_id = ff_codec_get_id(ff_oma_codec_tags, | |
455 | 7 | st->codecpar->codec_tag); | |
456 | |||
457 | 7 | oc->read_packet = read_packet; | |
458 | |||
459 |
2/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
7 | switch (buf[32]) { |
460 | 2 | case OMA_CODECID_ATRAC3: | |
461 | 2 | samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7] * 100; | |
462 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!samplerate) { |
463 | ✗ | av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n"); | |
464 | ✗ | return AVERROR_INVALIDDATA; | |
465 | } | ||
466 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (samplerate != 44100) |
467 | ✗ | avpriv_request_sample(s, "Sample rate %d", samplerate); | |
468 | |||
469 | 2 | framesize = (codec_params & 0x3FF) * 8; | |
470 | |||
471 | /* get stereo coding mode, 1 for joint-stereo */ | ||
472 | 2 | jsflag = (codec_params >> 17) & 1; | |
473 | |||
474 | 2 | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
475 | 2 | st->codecpar->sample_rate = samplerate; | |
476 | 2 | st->codecpar->bit_rate = st->codecpar->sample_rate * framesize / (1024 / 8); | |
477 | |||
478 | /* fake the ATRAC3 extradata | ||
479 | * (wav format, makes stream copy to wav work) */ | ||
480 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = ff_alloc_extradata(st->codecpar, 14)) < 0) |
481 | ✗ | return ret; | |
482 | |||
483 | 2 | edata = st->codecpar->extradata; | |
484 | 2 | AV_WL16(&edata[0], 1); // always 1 | |
485 | 2 | AV_WL32(&edata[2], samplerate); // samples rate | |
486 | 2 | AV_WL16(&edata[6], jsflag); // coding mode | |
487 | 2 | AV_WL16(&edata[8], jsflag); // coding mode | |
488 | 2 | AV_WL16(&edata[10], 1); // always 1 | |
489 | 2 | AV_WL16(&edata[12], 0); // always 0 | |
490 | |||
491 | 2 | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); | |
492 | 2 | break; | |
493 | 5 | case OMA_CODECID_ATRAC3P: | |
494 | 5 | channel_id = (codec_params >> 10) & 7; | |
495 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!channel_id) { |
496 | ✗ | av_log(s, AV_LOG_ERROR, | |
497 | "Invalid ATRAC-X channel id: %"PRIu32"\n", channel_id); | ||
498 | ✗ | return AVERROR_INVALIDDATA; | |
499 | } | ||
500 | 5 | av_channel_layout_copy(&st->codecpar->ch_layout, | |
501 | 5 | &oma_chid_to_native_layout[channel_id - 1]); | |
502 | 5 | framesize = ((codec_params & 0x3FF) * 8) + 8; | |
503 | 5 | samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7] * 100; | |
504 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!samplerate) { |
505 | ✗ | av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n"); | |
506 | ✗ | return AVERROR_INVALIDDATA; | |
507 | } | ||
508 | 5 | st->codecpar->sample_rate = samplerate; | |
509 | 5 | st->codecpar->bit_rate = samplerate * framesize / (2048 / 8); | |
510 | 5 | avpriv_set_pts_info(st, 64, 1, samplerate); | |
511 | 5 | break; | |
512 | ✗ | case OMA_CODECID_MP3: | |
513 | ✗ | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW; | |
514 | ✗ | framesize = 1024; | |
515 | ✗ | break; | |
516 | ✗ | case OMA_CODECID_LPCM: | |
517 | /* PCM 44.1 kHz 16 bit stereo big-endian */ | ||
518 | ✗ | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
519 | ✗ | st->codecpar->sample_rate = 44100; | |
520 | ✗ | framesize = 1024; | |
521 | /* bit rate = sample rate x PCM block align (= 4) x 8 */ | ||
522 | ✗ | st->codecpar->bit_rate = st->codecpar->sample_rate * 32; | |
523 | ✗ | st->codecpar->bits_per_coded_sample = | |
524 | ✗ | av_get_bits_per_sample(st->codecpar->codec_id); | |
525 | ✗ | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); | |
526 | ✗ | break; | |
527 | ✗ | case OMA_CODECID_ATRAC3AL: | |
528 | ✗ | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
529 | ✗ | st->codecpar->sample_rate = 44100; | |
530 | ✗ | avpriv_set_pts_info(st, 64, 1, 44100); | |
531 | ✗ | oc->read_packet = aal_read_packet; | |
532 | ✗ | framesize = 4096; | |
533 | ✗ | break; | |
534 | ✗ | case OMA_CODECID_ATRAC3PAL: | |
535 | ✗ | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
536 | ✗ | st->codecpar->sample_rate = 44100; | |
537 | ✗ | avpriv_set_pts_info(st, 64, 1, 44100); | |
538 | ✗ | oc->read_packet = aal_read_packet; | |
539 | ✗ | framesize = 4096; | |
540 | ✗ | break; | |
541 | ✗ | default: | |
542 | ✗ | av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n", buf[32]); | |
543 | ✗ | return AVERROR(ENOSYS); | |
544 | } | ||
545 | |||
546 | 7 | st->codecpar->block_align = framesize; | |
547 | |||
548 | 7 | return 0; | |
549 | } | ||
550 | |||
551 | 2642 | static int oma_read_packet(AVFormatContext *s, AVPacket *pkt) | |
552 | { | ||
553 | 2642 | OMAContext *oc = s->priv_data; | |
554 | 2642 | return oc->read_packet(s, pkt); | |
555 | } | ||
556 | |||
557 | 6938 | static int oma_read_probe(const AVProbeData *p) | |
558 | { | ||
559 | 6938 | const uint8_t *buf = p->buf; | |
560 | 6938 | unsigned tag_len = 0; | |
561 | |||
562 |
3/4✓ Branch 0 taken 6938 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6932 times.
|
6938 | if (p->buf_size >= ID3v2_HEADER_SIZE && ff_id3v2_match(buf, ID3v2_EA3_MAGIC)) |
563 | 6 | tag_len = ff_id3v2_tag_len(buf); | |
564 | |||
565 | /* This check cannot overflow as tag_len has at most 28 bits */ | ||
566 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6937 times.
|
6938 | if (p->buf_size < tag_len + 5) |
567 | /* EA3 header comes late, might be outside of the probe buffer */ | ||
568 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | return tag_len ? AVPROBE_SCORE_EXTENSION/2 : 0; |
569 | |||
570 | 6937 | buf += tag_len; | |
571 | |||
572 |
4/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6931 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6937 | if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE) |
573 | 6 | return AVPROBE_SCORE_MAX; | |
574 | else | ||
575 | 6931 | return 0; | |
576 | } | ||
577 | |||
578 | ✗ | static int oma_read_seek(struct AVFormatContext *s, | |
579 | int stream_index, int64_t timestamp, int flags) | ||
580 | { | ||
581 | ✗ | OMAContext *oc = s->priv_data; | |
582 | ✗ | AVStream *st = s->streams[0]; | |
583 | int64_t err; | ||
584 | |||
585 | ✗ | if (st->codecpar->codec_id == AV_CODEC_ID_ATRAC3PAL || | |
586 | ✗ | st->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL) | |
587 | ✗ | return -1; | |
588 | |||
589 | ✗ | err = ff_pcm_read_seek(s, stream_index, timestamp, flags); | |
590 | ✗ | if (!oc->encrypted) | |
591 | ✗ | return err; | |
592 | |||
593 | /* readjust IV for CBC */ | ||
594 | ✗ | if (err || avio_tell(s->pb) < oc->content_start) | |
595 | ✗ | goto wipe; | |
596 | ✗ | if ((err = avio_seek(s->pb, -8, SEEK_CUR)) < 0) | |
597 | ✗ | goto wipe; | |
598 | ✗ | if ((err = avio_read(s->pb, oc->iv, 8)) < 8) { | |
599 | ✗ | if (err >= 0) | |
600 | ✗ | err = AVERROR_EOF; | |
601 | ✗ | goto wipe; | |
602 | } | ||
603 | |||
604 | ✗ | return 0; | |
605 | ✗ | wipe: | |
606 | ✗ | memset(oc->iv, 0, 8); | |
607 | ✗ | return err; | |
608 | } | ||
609 | |||
610 | const AVInputFormat ff_oma_demuxer = { | ||
611 | .name = "oma", | ||
612 | .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"), | ||
613 | .priv_data_size = sizeof(OMAContext), | ||
614 | .flags_internal = FF_FMT_INIT_CLEANUP, | ||
615 | .read_probe = oma_read_probe, | ||
616 | .read_header = oma_read_header, | ||
617 | .read_packet = oma_read_packet, | ||
618 | .read_seek = oma_read_seek, | ||
619 | .read_close = oma_read_close, | ||
620 | .flags = AVFMT_GENERIC_INDEX, | ||
621 | .extensions = "oma,omg,aa3", | ||
622 | .codec_tag = ff_oma_codec_tags_list, | ||
623 | }; | ||
624 |