FFmpeg: libavformat/omadec.c Source File
Go to the documentation of this file. 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
56
57
59 0xd79e8283acea4620, 0x7a9762f445afd0d8,
60 0x354d60a60b8c79f1, 0x584e1cde00b07aee,
61 0x1573cd93da7df623, 0x47f98d79620dd535
62 };
63
64 /** map ATRAC-X channel id to internal channel layout */
73 };
74
91
94
96 {
99 return 0;
100 }
101
104 {
105 char buf[33];
108 return;
111 }
112
115 {
117
118 if (!r_val && !n_val)
119 return -1;
120
122
123 /* use first 64 bits in the third round again */
124 if (r_val) {
125 if (r_val != oc->
r_val) {
126 memset(oc->
r_val, 0, 24);
128 }
129 memcpy(&oc->
r_val[16], r_val, 8);
130 }
131 if (n_val) {
132 if (n_val != oc->
n_val) {
133 memset(oc->
n_val, 0, 24);
135 }
136 memcpy(&oc->
n_val[16], n_val, 8);
137 }
138
139 return 0;
140 }
141
142 #define OMA_RPROBE_M_VAL 48 + 1
143
145 const uint8_t *r_val)
146 {
149 struct AVDES *av_des;
150
151 if (!enc_header || !r_val ||
152 size < OMA_ENC_HEADER_SIZE + oc->k_size + oc->
e_size + oc->
i_size ||
154 return -1;
155
157 if (!av_des)
159
160 /* m_val */
163
164 /* s_val */
167
168 /* sm_val */
172
174
176
177 return memcmp(&enc_header[
pos], oc->
sm_val, 8) ? -1 : 0;
178 }
179
181 const uint8_t *n_val)
182 {
185 uint32_t taglen, datalen;
186 struct AVDES *av_des;
187
188 if (!enc_header || !n_val ||
189 size < OMA_ENC_HEADER_SIZE + oc->k_size + 4)
190 return -1;
191
193 if (!memcmp(&enc_header[
pos],
"EKB ", 4))
195
197 return -1;
198
201
203 datalen =
AV_RB32(&enc_header[
pos + 36]) >> 4;
204
205 pos += 44LL + taglen;
206
207 if (
pos + (((uint64_t)datalen) << 4) >
size)
208 return -1;
209
211 if (!av_des)
213
215 while (datalen-- > 0) {
220 return 0;
221 }
223 }
224
226 return -1;
227 }
228
230 {
233 uint8_t *gdata;
234
237
238 /* find GEOB metadata */
239 for (; em; em = em->
next) {
240 if (strcmp(em->
tag,
"GEOB"))
241 continue;
245 break;
246 }
247 if (!em) {
250 }
251
254 "Invalid GEOB data size: %"PRIu32
"\n", geob->
datasize);
256 }
257
259
262
267
271 }
276 }
279
282
285 8);
286
289 }
290 if (!memcmp(oc->
r_val, (
const uint8_t[8]){0}, 8) ||
295 uint8_t buf[16];
298 kset(
s, buf, buf, 16);
301 break;
302 }
306 }
307 }
308
312
313 /* e_val */
318
319 /* init e_val */
321
322 return 0;
323 }
324
326 {
333
334 if (
ret < packet_size)
336
341
343
348 }
349
351 /* previous unencrypted block saved in IV for
352 * the next packet (CBC mode) */
353 if (
ret == packet_size)
355 (packet_size >> 3), oc->
iv, 1);
356 else
357 memset(oc->
iv, 0, 8);
358 }
359
361 }
362
364 {
367 int packet_size;
369
372
378
385 if (
ret < packet_size)
387
392
398 } else {
401 }
402
404 }
405
407 {
408 int ret, framesize, jsflag, samplerate;
409 uint32_t codec_params, channel_id;
410 int16_t eid;
412 uint8_t *edata;
416
421 }
422
426 return -1;
427 }
428
429 if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}), 3) ||
434 }
435
437
438 /* encrypted file */
440 if (eid != -1 && eid != -128 &&
decrypt_init(
s, extra_meta, buf) < 0) {
442 return -1;
443 }
444
446
447 codec_params =
AV_RB24(&buf[33]);
448
450 if (!st)
452
458
460
461 switch (buf[32]) {
464 if (!samplerate) {
467 }
468 if (samplerate != 44100)
470
471 framesize = (codec_params & 0x3FF) * 8;
472
473 /* get stereo coding mode, 1 for joint-stereo */
474 jsflag = (codec_params >> 17) & 1;
475
479
480 /* fake the ATRAC3 extradata
481 * (wav format, makes stream copy to wav work) */
484
486 AV_WL16(&edata[0], 1);
// always 1
487 AV_WL32(&edata[2], samplerate);
// samples rate
488 AV_WL16(&edata[6], jsflag);
// coding mode
489 AV_WL16(&edata[8], jsflag);
// coding mode
490 AV_WL16(&edata[10], 1);
// always 1
491 AV_WL16(&edata[12], 0);
// always 0
492
494 break;
496 channel_id = (codec_params >> 10) & 7;
497 if (!channel_id) {
499 "Invalid ATRAC-X channel id: %"PRIu32"\n", channel_id);
501 }
504 framesize = ((codec_params & 0x3FF) * 8) + 8;
506 if (!samplerate) {
509 }
513 break;
516 framesize = 1024;
517 break;
519 /* PCM 44.1 kHz 16 bit stereo big-endian */
522 framesize = 1024;
523 /* bit rate = sample rate x PCM block align (= 4) x 8 */
528 break;
534 framesize = 4096;
535 break;
541 framesize = 4096;
542 break;
543 default:
546 }
547
549
550 return 0;
551 }
552
554 {
557 }
558
560 {
561 const uint8_t *buf = p->
buf;
562 unsigned tag_len = 0;
563
566
567 /* This check cannot overflow as tag_len has at most 28 bits */
569 /* EA3 header comes late, might be outside of the probe buffer */
571
572 buf += tag_len;
573
576 else
577 return 0;
578 }
579
582 {
586
589 return -1;
590
593 return err;
594
595 /* readjust IV for CBC */
597 goto wipe;
598 if ((err =
avio_seek(
s->pb, -8, SEEK_CUR)) < 0)
599 goto wipe;
601 if (err >= 0)
603 goto wipe;
604 }
605
606 return 0;
607 wipe:
608 memset(oc->
iv, 0, 8);
609 return err;
610 }
611
616 .p.extensions = "oma,omg,aa3",
625 };
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, av_unused int decrypt)
#define AV_LOG_WARNING
Something somehow does not look correct.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
enum AVMediaType codec_type
General type of the encoded data.
#define AV_CHANNEL_LAYOUT_STEREO
const FFInputFormat ff_oma_demuxer
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
const AVCodecTag *const ff_oma_codec_tags_list[]
void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypts / decrypts using the DES algorithm.
static void hex_log(AVFormatContext *s, int level, const char *name, const uint8_t *value, int len)
const uint16_t ff_oma_srate_tab[8]
#define OMA_ENC_HEADER_SIZE
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
Read an ID3v2 tag, including supported extra metadata.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
int buf_size
Size of buf except extra allocated bytes.
static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
static av_cold int read_close(AVFormatContext *ctx)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
static int rprobe(AVFormatContext *s, uint8_t *enc_header, unsigned size, const uint8_t *r_val)
#define AV_CHANNEL_LAYOUT_SURROUND
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)
Create chapters for all CHAP tags found in the ID3v2 header.
int(* read_packet)(AVFormatContext *s, AVPacket *pkt)
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
unsigned int avio_rb32(AVIOContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
static int nprobe(AVFormatContext *s, uint8_t *enc_header, unsigned size, const uint8_t *n_val)
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_7POINT1
void av_des_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count)
Calculates CBC-MAC using the DES algorithm.
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
enum AVStreamParseType need_parsing
int av_log_get_level(void)
Get the current log level.
const AVCodecTag ff_oma_codec_tags[]
AVCodecParameters * codecpar
Codec parameters associated with this stream.
static int read_header(FFV1Context *f)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
static int read_packet(AVFormatContext *s, AVPacket *pkt)
This structure contains the data a format has to probe a file.
AVChannelLayout ch_layout
Audio only.
#define ID3v2_HEADER_SIZE
int sample_rate
Audio only.
unsigned int avio_rb24(AVIOContext *s)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
An AVChannelLayout holds information about the channel layout of audio data.
#define MKBETAG(a, b, c, d)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static const uint8_t header[24]
static int aal_read_packet(AVFormatContext *s, AVPacket *pkt)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
static int kset(AVFormatContext *s, const uint8_t *r_val, const uint8_t *n_val, int len)
int flags
A combination of AV_PKT_FLAG values.
#define AV_LOG_INFO
Standard information.
AVDES * av_des_alloc(void)
Allocate an AVDES context.
#define AV_CHANNEL_LAYOUT_6POINT1_BACK
static int oma_read_close(AVFormatContext *s)
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static const AVChannelLayout oma_chid_to_native_layout[7]
map ATRAC-X channel id to internal channel layout
int block_align
Audio only.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
static int oma_read_header(AVFormatContext *s)
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
unsigned int avio_rb16(AVIOContext *s)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
static int oma_read_probe(const AVProbeData *p)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
static int read_probe(const AVProbeData *p)
int bits_per_coded_sample
The number of bits per sample in the codedwords.
#define avpriv_request_sample(...)
#define AV_CHANNEL_LAYOUT_MONO
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
This structure stores compressed data.
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
int64_t pos
byte position in stream, -1 if unknown
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define flags(name, subs,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
static const uint64_t leaf_table[]
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Generated on Fri Aug 22 2025 13:59:34 for FFmpeg by
doxygen
1.8.17