1 /*
2 * Sony OpenMG (OMA) demuxer
3 *
4 * Copyright (c) 2008 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
77 {
78 char buf[33];
81 return;
83 buf[len<<1] = '0円';
84 av_log(s, level,
"%s: %s\n", name, buf);
85 }
86
88 {
90
91 if (!r_val && !n_val)
92 return -1;
93
95
96 /* use first 64 bits in the third round again */
97 if (r_val) {
98 if (r_val != oc->
r_val) {
99 memset(oc->
r_val, 0, 24);
100 memcpy(oc->
r_val, r_val, len);
101 }
102 memcpy(&oc->
r_val[16], r_val, 8);
103 }
104 if (n_val) {
105 if (n_val != oc->
n_val) {
106 memset(oc->
n_val, 0, 24);
107 memcpy(oc->
n_val, n_val, len);
108 }
109 memcpy(&oc->
n_val[16], n_val, 8);
110 }
111
112 return 0;
113 }
114
116 {
118 unsigned int pos;
120
121 if (!enc_header || !r_val)
122 return -1;
123
124 /* m_val */
127
128 /* s_val */
131
132 /* sm_val */
136
138
139 return memcmp(&enc_header[pos], oc->
sm_val, 8) ? -1 : 0;
140 }
141
143 {
145 uint32_t pos, taglen, datalen;
147
148 if (!enc_header || !n_val)
149 return -1;
150
152 if (!memcmp(&enc_header[pos], "EKB ", 4))
153 pos += 32;
154
157
158 taglen =
AV_RB32(&enc_header[pos+32]);
159 datalen =
AV_RB32(&enc_header[pos+36]) >> 4;
160
161 if(pos + (uint64_t)taglen + (((uint64_t)datalen)<<4) + 44 > size)
162 return -1;
163
164 pos += 44 + taglen;
165
167 while (datalen-- > 0) {
171 return 0;
172 pos += 16;
173 }
174
175 return -1;
176 }
177
179 {
183
186
187 /* find GEOB metadata */
188 while (em) {
189 if (!strcmp(em->
tag,
"GEOB") &&
193 break;
194 }
196 }
197 if (!em) {
199 return -1;
200 }
201
204 return -1;
205 }
206
208
211
216
219 return -1;
220 }
222 || OMA_ENC_HEADER_SIZE + 48 > geob->
datasize
223 ) {
226 }
227 oc->
rid =
AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
229
230 memcpy(oc->
iv, &header[0x58], 8);
232
234
237 }
239 rprobe(s, gdata, oc->r_val) < 0 &&
240 nprobe(s, gdata, geob->datasize, oc->n_val) < 0) {
241 int i;
246 kset(s, buf, buf, 16);
247 if (!
rprobe(s, gdata, oc->r_val) || !
nprobe(s, gdata, geob->datasize, oc->n_val))
248 break;
249 }
252 return -1;
253 }
254 }
255
256 /* e_val */
260
261 /* init e_val */
263
264 return 0;
265 }
266
268 {
269 int ret, framesize, jsflag, samplerate;
270 uint32_t codec_params;
271 int16_t eid;
277
281 return -1;
282
285 return -1;
286 }
287
289
290 /* encrypted file */
292 if (eid != -1 && eid != -128 &&
decrypt_init(s, extra_meta, buf) < 0) {
294 return -1;
295 }
296
298
299 codec_params =
AV_RB24(&buf[33]);
300
302 if (!st)
304
309
310 switch (buf[32]) {
313 if (samplerate != 44100)
315 samplerate);
316
317 framesize = (codec_params & 0x3FF) * 8;
318 jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
323
324 /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
327 if (!edata)
329
331 AV_WL16(&edata[0], 1);
// always 1
332 AV_WL32(&edata[2], samplerate);
// samples rate
333 AV_WL16(&edata[6], jsflag);
// coding mode
334 AV_WL16(&edata[8], jsflag);
// coding mode
335 AV_WL16(&edata[10], 1);
// always 1
336 // AV_WL16(&edata[12], 0); // always 0
337
339 break;
342 framesize = ((codec_params & 0x3FF) * 8) + 8;
347 break;
350 framesize = 1024;
351 break;
353 /* PCM 44.1 kHz 16 bit stereo big-endian */
357 framesize = 1024;
358 /* bit rate = sample rate x PCM block align (= 4) x 8 */
362 break;
363 default:
365 return -1;
366 }
367
369
370 return 0;
371 }
372
373
375 {
379
380 if (ret <= 0)
382
384
386 /* previous unencrypted block saved in IV for the next packet (CBC mode) */
388 }
389
390 return ret;
391 }
392
394 {
396 unsigned tag_len = 0;
397
399
402 buf[3] != 3 || // version must be 3
403 buf[4]) // flags byte zero
404 return 0;
405
407
408 /* This check cannot overflow as tag_len has at most 28 bits */
410 /* EA3 header comes late, might be outside of the probe buffer */
412
413 buf += tag_len;
414
417 else
418 return 0;
419 }
420
422 {
424
426
428 /* readjust IV for CBC */
430 if (pos < oc->content_start)
431 memset(oc->
iv, 0, 8);
432 else {
434 memset(oc->
iv, 0, 8);
435 return -1;
436 }
437 }
438 }
439
440 return 0;
441 }
442
452 .extensions = "oma,omg,aa3",
454 };