Coverage Report

Created: 2025-08-28 07:12

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