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 */
41
50
51
53 0xd79e8283acea4620, 0x7a9762f445afd0d8,
54 0x354d60a60b8c79f1, 0x584e1cde00b07aee,
55 0x1573cd93da7df623, 0x47f98d79620dd535
56 };
57
75
78 {
82 return;
84 buf[len << 1] = '0円';
85 av_log(s, level,
"%s: %s\n", name, buf);
86 }
87
90 {
92
93 if (!r_val && !n_val)
94 return -1;
95
97
98 /* use first 64 bits in the third round again */
99 if (r_val) {
100 if (r_val != oc->
r_val) {
101 memset(oc->
r_val, 0, 24);
102 memcpy(oc->
r_val, r_val, len);
103 }
104 memcpy(&oc->
r_val[16], r_val, 8);
105 }
106 if (n_val) {
107 if (n_val != oc->
n_val) {
108 memset(oc->
n_val, 0, 24);
109 memcpy(oc->
n_val, n_val, len);
110 }
111 memcpy(&oc->
n_val[16], n_val, 8);
112 }
113
114 return 0;
115 }
116
117 #define OMA_RPROBE_M_VAL 48 + 1
118
121 {
123 unsigned int pos;
125
126 if (!enc_header || !r_val ||
127 size < OMA_ENC_HEADER_SIZE + oc->k_size + oc->
e_size + oc->
i_size ||
129 return -1;
130
131 /* m_val */
134
135 /* s_val */
138
139 /* sm_val */
143
145
146 return memcmp(&enc_header[pos], oc->
sm_val, 8) ? -1 : 0;
147 }
148
151 {
153 uint64_t pos;
154 uint32_t taglen, datalen;
156
157 if (!enc_header || !n_val ||
158 size < OMA_ENC_HEADER_SIZE + oc->k_size + 4)
159 return -1;
160
162 if (!memcmp(&enc_header[pos], "EKB ", 4))
163 pos += 32;
164
165 if (size < pos + 44)
166 return -1;
167
170
171 taglen =
AV_RB32(&enc_header[pos + 32]);
172 datalen =
AV_RB32(&enc_header[pos + 36]) >> 4;
173
175
176 if (pos + (((uint64_t)datalen) << 4) > size)
177 return -1;
178
180 while (datalen-- > 0) {
184 return 0;
185 pos += 16;
186 }
187
188 return -1;
189 }
190
192 {
196
199
200 /* find GEOB metadata */
201 while (em) {
202 if (!strcmp(em->
tag,
"GEOB") &&
206 break;
207 }
209 }
210 if (!em) {
213 }
214
217 "Invalid GEOB data size: %u\n", geob->
datasize);
219 }
220
222
225
230
234 }
236 || OMA_ENC_HEADER_SIZE + 48 > geob->
datasize
237 ) {
240 }
241 oc->
rid =
AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
243
244 memcpy(oc->
iv, &header[0x58], 8);
246
249 8);
250
253 }
255 rprobe(
s, gdata, geob->datasize, oc->r_val) < 0 &&
256 nprobe(
s, gdata, geob->datasize, oc->n_val) < 0) {
257 int i;
262 kset(
s, buf, buf, 16);
263 if (!
rprobe(
s, gdata, geob->datasize, oc->r_val) ||
264 !
nprobe(
s, gdata, geob->datasize, oc->n_val))
265 break;
266 }
270 }
271 }
272
273 /* e_val */
278
279 /* init e_val */
281
282 return 0;
283 }
284
286 {
287 int ret, framesize, jsflag, samplerate;
288 uint32_t codec_params, channel_id;
289 int16_t eid;
295
299 return -1;
300
301 if (memcmp(buf, ((
const uint8_t[]){
'E',
'A',
'3'}), 3) ||
305 }
306
308
309 /* encrypted file */
311 if (eid != -1 && eid != -128 &&
decrypt_init(s, extra_meta, buf) < 0) {
313 return -1;
314 }
315
317
318 codec_params =
AV_RB24(&buf[33]);
319
321 if (!st)
323
329
330 switch (buf[32]) {
333 if (!samplerate) {
336 }
337 if (samplerate != 44100)
339
340 framesize = (codec_params & 0x3FF) * 8;
341
342 /* get stereo coding mode, 1 for joint-stereo */
343 jsflag = (codec_params >> 17) & 1;
344
349
350 /* fake the ATRAC3 extradata
351 * (wav format, makes stream copy to wav work) */
354
356 AV_WL16(&edata[0], 1);
// always 1
357 AV_WL32(&edata[2], samplerate);
// samples rate
358 AV_WL16(&edata[6], jsflag);
// coding mode
359 AV_WL16(&edata[8], jsflag);
// coding mode
360 AV_WL16(&edata[10], 1);
// always 1
361 // AV_WL16(&edata[12], 0); // always 0
362
364 break;
366 channel_id = (codec_params >> 10) & 7;
367 if (!channel_id) {
369 "Invalid ATRAC-X channel id: %d\n", channel_id);
371 }
374 framesize = ((codec_params & 0x3FF) * 8) + 8;
376 if (!samplerate) {
379 }
384 break;
387 framesize = 1024;
388 break;
390 /* PCM 44.1 kHz 16 bit stereo big-endian */
394 framesize = 1024;
395 /* bit rate = sample rate x PCM block align (= 4) x 8 */
400 break;
401 default:
404 }
405
407
408 return 0;
409 }
410
411
413 {
417
418 if (ret < packet_size)
420
421 if (ret < 0)
423 if (!ret)
425
427
429 /* previous unencrypted block saved in IV for
430 * the next packet (CBC mode) */
431 if (ret == packet_size)
433 (packet_size >> 3), oc->
iv, 1);
434 else
435 memset(oc->
iv, 0, 8);
436 }
437
439 }
440
442 {
444 unsigned tag_len = 0;
445
447
450 buf[3] != 3 || // version must be 3
451 buf[4]) // flags byte zero
452 return 0;
453
455
456 /* This check cannot overflow as tag_len has at most 28 bits */
458 /* EA3 header comes late, might be outside of the probe buffer */
460
461 buf += tag_len;
462
465 else
466 return 0;
467 }
468
470 int stream_index, int64_t timestamp,
int flags)
471 {
474
476 return err;
477
478 /* readjust IV for CBC */
480 goto wipe;
482 goto wipe;
484 if (err >= 0)
486 goto wipe;
487 }
488
489 return 0;
490 wipe:
491 memset(oc->
iv, 0, 8);
492 return err;
493 }
494
504 .extensions = "oma,omg,aa3",
506 };