Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master:
APIchanges: fill in revision for AVFrame.age deprecation
avcodec: deprecate AVFrame.age
4xm: remove unneeded check for remaining unused data.
lavf: force threads to 1 in avformat_find_stream_info()
swscale: fix overflows in vertical scaling at top/bottom edges.
lavf: add OpenMG audio muxer.
omadec: split data that will be used in the muxer to a separate file.
lavf: rename oma.c -> omadec.c
tmv decoder: set correct pix_fmt

Conflicts:
Changelog
doc/APIchanges
libavcodec/mpegvideo.c
libavcodec/version.h
libavformat/oma.c
libavformat/version.h
libswscale/swscale.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2011/12/19 10:11:50
Showing 21 changed files
... ...
@@ -137,6 +137,7 @@ easier to use. The changes are:
137 137
 - cellauto source
138 138
 - v410 Quicktime Uncompressed 4:4:4 10-bit encoder and decoder
139 139
 - SBaGen (SBG) binaural beats script demuxer
140
+- OpenMG Audio muxer
140 141
 
141 142
 
142 143
 version 0.8:
... ...
@@ -31,6 +31,9 @@ API changes, most recent first:
31 31
 2011-10-20 - b35e9e1 - lavu 51.22.0
32 32
   Add av_strtok() to avstring.h.
33 33
 
34
+2011-12-18 - 8400b12 - lavc 53.28.1
35
+  Deprecate AVFrame.age. The field is unused.
36
+
34 37
 2011-xx-xx - xxxxxxx - lavf 53.17.0
35 38
   Add avformat_open_input().
36 39
   Deprecate av_close_input_file() and av_close_input_stream().
... ...
@@ -317,7 +317,7 @@ library:
317 317
     @tab Used in Sierra CD-ROM games.
318 318
 @item Smacker                   @tab   @tab X
319 319
     @tab Multimedia format used by many games.
320
-@item Sony OpenMG (OMA)         @tab   @tab X
320
+@item Sony OpenMG (OMA)         @tab X @tab X
321 321
     @tab Audio format used in Sony Sonic Stage and Sony Vegas.
322 322
 @item Sony PlayStation STR      @tab   @tab X
323 323
 @item Sony Wave64 (W64)         @tab   @tab X
... ...
@@ -1583,7 +1583,6 @@ static int input_get_buffer(AVCodecContext *codec, AVFrame *pic)
1583 1583
         pic->linesize[i] = ref->linesize[i];
1584 1584
     }
1585 1585
     pic->opaque = ref;
1586
-    pic->age    = INT_MAX;
1587 1586
     pic->type   = FF_BUFFER_TYPE_USER;
1588 1587
     pic->reordered_opaque = codec->reordered_opaque;
1589 1588
     if(codec->pkt) pic->pkt_pts = codec->pkt->pts;
... ...
@@ -440,15 +440,6 @@ static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length){
440 440
         dst += 8*stride;
441 441
     }
442 442
 
443
-    if(   bitstream_size != (get_bits_count(&f->gb)+31)/32*4
444
-       || (((const char*)f->wordstream - (const char*)buf + 2)&~2) != extra + bitstream_size + wordstream_size
445
-       || (((const char*)f->bytestream - (const char*)buf + 3)&~3) != extra + bitstream_size + wordstream_size + bytestream_size)
446
-        av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
447
-            bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
448
-            -(((const char*)f->bytestream - (const char*)buf + 3)&~3) + (extra + bitstream_size + wordstream_size + bytestream_size),
449
-            -(((const char*)f->wordstream - (const char*)buf + 2)&~2) + (extra + bitstream_size + wordstream_size)
450
-        );
451
-
452 443
     return 0;
453 444
 }
454 445
 
... ...
@@ -1029,13 +1029,12 @@ typedef struct AVFrame {
1029 1029
      */
1030 1030
     int quality;
1031 1031
 
1032
+#if FF_API_AVFRAME_AGE
1032 1033
     /**
1033
-     * buffer age (1->was last buffer and dint change, 2->..., ...).
1034
-     * Set to INT_MAX if the buffer has not been used yet.
1035
-     * - encoding: unused
1036
-     * - decoding: MUST be set by get_buffer() for video.
1034
+     * @deprecated unused
1037 1035
      */
1038
-    int age;
1036
+    attribute_deprecated int age;
1037
+#endif
1039 1038
 
1040 1039
     /**
1041 1040
      * is this picture used as reference
... ...
@@ -30,7 +30,6 @@
30 30
 #include "avcodec.h"
31 31
 
32 32
 typedef struct InternalBuffer {
33
-    int last_pic_num;
34 33
     uint8_t *base[AV_NUM_DATA_POINTERS];
35 34
     uint8_t *data[AV_NUM_DATA_POINTERS];
36 35
     int linesize[AV_NUM_DATA_POINTERS];
... ...
@@ -263,9 +263,9 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
263 263
     else
264 264
         r = avcodec_default_get_buffer(s->avctx, (AVFrame *) pic);
265 265
 
266
-    if (r < 0 || !pic->f.age || !pic->f.type || !pic->f.data[0]) {
267
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n",
268
-               r, pic->f.age, pic->f.type, pic->f.data[0]);
266
+    if (r < 0 || !pic->f.type || !pic->f.data[0]) {
267
+        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %p)\n",
268
+               r, pic->f.type, pic->f.data[0]);
269 269
         av_freep(&pic->f.hwaccel_picture_private);
270 270
         return -1;
271 271
     }
... ...
@@ -369,15 +369,6 @@ int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
369 369
                           1 * sizeof(AVPanScan), fail)
370 370
     }
371 371
 
372
-    /* It might be nicer if the application would keep track of these
373
-     * but it would require an API change. */
374
-    memmove(s->prev_pict_types + 1, s->prev_pict_types,
375
-            PREV_PICT_TYPES_BUFFER_SIZE-1);
376
-    s->prev_pict_types[0] =  s->dropable ? AV_PICTURE_TYPE_B : s->pict_type;
377
-    if (pic->f.age < PREV_PICT_TYPES_BUFFER_SIZE &&
378
-        s->prev_pict_types[pic->f.age] == AV_PICTURE_TYPE_B)
379
-        pic->f.age = INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2
380
-                              // and it is a bit tricky to skip them anyway.
381 372
     pic->owner2 = s;
382 373
 
383 374
     return 0;
... ...
@@ -574,9 +565,6 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst,
574 574
     s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
575 575
     s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
576 576
 
577
-    memcpy(s->prev_pict_types, s1->prev_pict_types,
578
-           PREV_PICT_TYPES_BUFFER_SIZE);
579
-
580 577
     // Error/bug resilience
581 578
     s->next_p_frame_damaged = s1->next_p_frame_damaged;
582 579
     s->workaround_bugs      = s1->workaround_bugs;
... ...
@@ -837,7 +825,6 @@ av_cold int MPV_common_init(MpegEncContext *s)
837 837
         /* init macroblock skip table */
838 838
         FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
839 839
         // Note the + 1 is for  a quicker mpeg4 slice_end detection
840
-        FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail);
841 840
 
842 841
         s->parse_context.state = -1;
843 842
         if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
... ...
@@ -928,7 +915,6 @@ void MPV_common_end(MpegEncContext *s)
928 928
     av_freep(&s->pred_dir_table);
929 929
 
930 930
     av_freep(&s->mbskip_table);
931
-    av_freep(&s->prev_pict_types);
932 931
     av_freep(&s->bitstream_buffer);
933 932
     s->allocated_bitstream_buffer_size = 0;
934 933
 
... ...
@@ -2201,24 +2187,13 @@ void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
2201 2201
         /* skip only during decoding as we might trash the buffers during encoding a bit */
2202 2202
         if(!s->encoding){
2203 2203
             uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2204
-            const int age = s->current_picture.f.age;
2205
-
2206
-            assert(age);
2207 2204
 
2208 2205
             if (s->mb_skipped) {
2209 2206
                 s->mb_skipped= 0;
2210 2207
                 assert(s->pict_type!=AV_PICTURE_TYPE_I);
2211
-
2212
-                (*mbskip_ptr) ++; /* indicate that this time we skipped it */
2213
-                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2214
-
2215
-                /* if previous was skipped too, then nothing to do !  */
2216
-                if (*mbskip_ptr >= age && s->current_picture.f.reference){
2217
-                    return;
2218
-                }
2208
+                *mbskip_ptr = 1;
2219 2209
             } else if(!s->current_picture.f.reference) {
2220
-                (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2221
-                if(*mbskip_ptr >99) *mbskip_ptr= 99;
2210
+                *mbskip_ptr = 1;
2222 2211
             } else{
2223 2212
                 *mbskip_ptr = 0; /* not skipped */
2224 2213
             }
... ...
@@ -311,8 +311,6 @@ typedef struct MpegEncContext {
311 311
     uint8_t *coded_block;          ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
312 312
     int16_t (*ac_val_base)[16];
313 313
     int16_t (*ac_val[3])[16];      ///< used for for mpeg4 AC prediction, all 3 arrays must be continuous
314
-    uint8_t *prev_pict_types;     ///< previous picture types in bitstream order, used for mb skip
315
-#define PREV_PICT_TYPES_BUFFER_SIZE 256
316 314
     int mb_skipped;                ///< MUST BE SET only during DECODING
317 315
     uint8_t *mbskip_table;        /**< used to avoid copy if macroblock skipped (for black regions for example)
318 316
                                    and used for b-frame encoding & decoding (contains skip table of next P Frame) */
... ...
@@ -868,13 +868,6 @@ int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
868 868
 
869 869
     pthread_mutex_unlock(&p->parent->buffer_mutex);
870 870
 
871
-    /*
872
-     * Buffer age is difficult to keep track of between
873
-     * multiple threads, and the optimizations it allows
874
-     * are not worth the effort. It is disabled for now.
875
-     */
876
-    f->age = INT_MAX;
877
-
878 871
     return err;
879 872
 }
880 873
 
... ...
@@ -34,14 +34,6 @@ typedef struct TMVContext {
34 34
     AVFrame pic;
35 35
 } TMVContext;
36 36
 
37
-static av_cold int tmv_decode_init(AVCodecContext *avctx)
38
-{
39
-    TMVContext *tmv = avctx->priv_data;
40
-
41
-    avcodec_get_frame_defaults(&tmv->pic);
42
-    return 0;
43
-}
44
-
45 37
 static int tmv_decode_frame(AVCodecContext *avctx, void *data,
46 38
                             int *data_size, AVPacket *avpkt)
47 39
 {
... ...
@@ -90,6 +82,14 @@ static int tmv_decode_frame(AVCodecContext *avctx, void *data,
90 90
     return avpkt->size;
91 91
 }
92 92
 
93
+static av_cold int tmv_decode_init(AVCodecContext *avctx)
94
+{
95
+    TMVContext *tmv = avctx->priv_data;
96
+    avctx->pix_fmt = PIX_FMT_PAL8;
97
+    avcodec_get_frame_defaults(&tmv->pic);
98
+    return 0;
99
+}
100
+
93 101
 static av_cold int tmv_decode_close(AVCodecContext *avctx)
94 102
 {
95 103
     TMVContext *tmv = avctx->priv_data;
... ...
@@ -373,7 +373,6 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
373 373
     int w= s->width;
374 374
     int h= s->height;
375 375
     InternalBuffer *buf;
376
-    int *picture_number;
377 376
     AVCodecInternal *avci = s->internal;
378 377
 
379 378
     if(pic->data[0]!=NULL) {
... ...
@@ -394,8 +393,6 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
394 394
     }
395 395
 
396 396
     buf = &avci->buffer[avci->buffer_count];
397
-    picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
398
-    (*picture_number)++;
399 397
 
400 398
     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
401 399
         if(s->active_thread_type&FF_THREAD_FRAME) {
... ...
@@ -409,10 +406,7 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
409 409
         }
410 410
     }
411 411
 
412
-    if(buf->base[0]){
413
-        pic->age= *picture_number - buf->last_pic_num;
414
-        buf->last_pic_num= *picture_number;
415
-    }else{
412
+    if (!buf->base[0]) {
416 413
         int h_chroma_shift, v_chroma_shift;
417 414
         int size[4] = {0};
418 415
         int tmpsize;
... ...
@@ -451,7 +445,6 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
451 451
             size[i] = picture.data[i+1] - picture.data[i];
452 452
         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
453 453
 
454
-        buf->last_pic_num= -256*256*256*64;
455 454
         memset(buf->base, 0, sizeof(buf->base));
456 455
         memset(buf->data, 0, sizeof(buf->data));
457 456
 
... ...
@@ -480,7 +473,6 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
480 480
         buf->width  = s->width;
481 481
         buf->height = s->height;
482 482
         buf->pix_fmt= s->pix_fmt;
483
-        pic->age= 256*256*256*64;
484 483
     }
485 484
     pic->type= FF_BUFFER_TYPE_INTERNAL;
486 485
 
... ...
@@ -22,7 +22,7 @@
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24 24
 #define LIBAVCODEC_VERSION_MINOR 46
25
-#define LIBAVCODEC_VERSION_MICRO  0
25
+#define LIBAVCODEC_VERSION_MICRO  1
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -120,5 +120,8 @@
120 120
 #define FF_API_OLD_TIMECODE (LIBAVCODEC_VERSION_MAJOR < 54)
121 121
 #endif
122 122
 
123
+#ifndef FF_API_AVFRAME_AGE
124
+#define FF_API_AVFRAME_AGE (LIBAVCODEC_VERSION_MAJOR < 54)
125
+#endif
123 126
 
124 127
 #endif /* AVCODEC_VERSION_H */
... ...
@@ -194,7 +194,8 @@ OBJS-$(CONFIG_OGG_DEMUXER)               += oggdec.o         \
194 194
                                             vorbiscomment.o
195 195
 OBJS-$(CONFIG_OGG_MUXER)                 += oggenc.o \
196 196
                                             vorbiscomment.o
197
-OBJS-$(CONFIG_OMA_DEMUXER)               += oma.o pcm.o
197
+OBJS-$(CONFIG_OMA_DEMUXER)               += omadec.o pcm.o oma.o
198
+OBJS-$(CONFIG_OMA_MUXER)                 += omaenc.o rawenc.o oma.o
198 199
 OBJS-$(CONFIG_PCM_ALAW_DEMUXER)          += pcmdec.o pcm.o rawdec.o
199 200
 OBJS-$(CONFIG_PCM_ALAW_MUXER)            += pcmenc.o rawenc.o
200 201
 OBJS-$(CONFIG_PCM_F32BE_DEMUXER)         += pcmdec.o pcm.o rawdec.o
... ...
@@ -166,7 +166,7 @@ void av_register_all(void)
166 166
     REGISTER_MUXDEMUX (NUT, nut);
167 167
     REGISTER_DEMUXER  (NUV, nuv);
168 168
     REGISTER_MUXDEMUX (OGG, ogg);
169
-    REGISTER_DEMUXER  (OMA, oma);
169
+    REGISTER_MUXDEMUX (OMA, oma);
170 170
     REGISTER_MUXDEMUX (PCM_ALAW,  pcm_alaw);
171 171
     REGISTER_MUXDEMUX (PCM_MULAW, pcm_mulaw);
172 172
     REGISTER_MUXDEMUX (PCM_F64BE, pcm_f64be);
... ...
@@ -1,9 +1,5 @@
1 1
 /*
2
- * Sony OpenMG (OMA) demuxer
3
- *
4
- * Copyright (c) 2008 Maxim Poliakovski
5
- *               2008 Benjamin Larsson
6
- *               2011 David Goldwich
2
+ * Sony OpenMG (OMA) common data
7 3
  *
8 4
  * This file is part of FFmpeg.
9 5
  *
... ...
@@ -22,446 +18,17 @@
22 22
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 23
  */
24 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
- * CODEC SUPPORT: Only ATRAC3 codec is currently supported!
41
- */
42
-
43
-#include "avformat.h"
44 25
 #include "internal.h"
45
-#include "libavutil/intreadwrite.h"
46
-#include "libavutil/des.h"
47
-#include "pcm.h"
48
-#include "riff.h"
49
-#include "id3v2.h"
26
+#include "oma.h"
27
+#include "libavcodec/avcodec.h"
50 28
 
51
-#define EA3_HEADER_SIZE 96
52
-#define ID3v2_EA3_MAGIC "ea3"
53
-#define OMA_ENC_HEADER_SIZE 16
29
+const uint16_t ff_oma_srate_tab[6] = { 320, 441, 480, 882, 960, 0 };
54 30
 
55
-enum {
56
-    OMA_CODECID_ATRAC3  = 0,
57
-    OMA_CODECID_ATRAC3P = 1,
58
-    OMA_CODECID_MP3     = 3,
59
-    OMA_CODECID_LPCM    = 4,
60
-    OMA_CODECID_WMA     = 5,
61
-};
62
-
63
-static const AVCodecTag codec_oma_tags[] = {
64
-    { CODEC_ID_ATRAC3,      OMA_CODECID_ATRAC3 },
31
+const AVCodecTag ff_oma_codec_tags[] = {
32
+    { CODEC_ID_ATRAC3,      OMA_CODECID_ATRAC3  },
65 33
     { CODEC_ID_ATRAC3P,     OMA_CODECID_ATRAC3P },
66
-    { CODEC_ID_MP3,         OMA_CODECID_MP3 },
67
-    { CODEC_ID_PCM_S16BE,   OMA_CODECID_LPCM },
68
-};
69
-
70
-static const uint64_t leaf_table[] = {
71
-    0xd79e8283acea4620, 0x7a9762f445afd0d8,
72
-    0x354d60a60b8c79f1, 0x584e1cde00b07aee,
73
-    0x1573cd93da7df623, 0x47f98d79620dd535
74
-};
75
-
76
-typedef struct OMAContext {
77
-    uint64_t content_start;
78
-    int encrypted;
79
-    uint16_t k_size;
80
-    uint16_t e_size;
81
-    uint16_t i_size;
82
-    uint16_t s_size;
83
-    uint32_t rid;
84
-    uint8_t r_val[24];
85
-    uint8_t n_val[24];
86
-    uint8_t m_val[8];
87
-    uint8_t s_val[8];
88
-    uint8_t sm_val[8];
89
-    uint8_t e_val[8];
90
-    uint8_t iv[8];
91
-    struct AVDES av_des;
92
-} OMAContext;
93
-
94
-static void hex_log(AVFormatContext *s, int level, const char *name, const uint8_t *value, int len)
95
-{
96
-    char buf[33];
97
-    len = FFMIN(len, 16);
98
-    if (av_log_get_level() < level)
99
-        return;
100
-    ff_data_to_hex(buf, value, len, 1);
101
-    buf[len<<1] = '\0';
102
-    av_log(s, level, "%s: %s\n", name, buf);
103
-}
104
-
105
-static int kset(AVFormatContext *s, const uint8_t *r_val, const uint8_t *n_val, int len)
106
-{
107
-    OMAContext *oc = s->priv_data;
108
-
109
-    if (!r_val && !n_val)
110
-        return -1;
111
-
112
-    len = FFMIN(len, 16);
113
-
114
-    /* use first 64 bits in the third round again */
115
-    if (r_val) {
116
-        if (r_val != oc->r_val) {
117
-            memset(oc->r_val, 0, 24);
118
-            memcpy(oc->r_val, r_val, len);
119
-        }
120
-        memcpy(&oc->r_val[16], r_val, 8);
121
-    }
122
-    if (n_val) {
123
-        if (n_val != oc->n_val) {
124
-            memset(oc->n_val, 0, 24);
125
-            memcpy(oc->n_val, n_val, len);
126
-        }
127
-        memcpy(&oc->n_val[16], n_val, 8);
128
-    }
129
-
130
-    return 0;
131
-}
132
-
133
-static int rprobe(AVFormatContext *s, uint8_t *enc_header, const uint8_t *r_val)
134
-{
135
-    OMAContext *oc = s->priv_data;
136
-    unsigned int pos;
137
-    struct AVDES av_des;
138
-
139
-    if (!enc_header || !r_val)
140
-        return -1;
141
-
142
-    /* m_val */
143
-    av_des_init(&av_des, r_val, 192, 1);
144
-    av_des_crypt(&av_des, oc->m_val, &enc_header[48], 1, NULL, 1);
145
-
146
-    /* s_val */
147
-    av_des_init(&av_des, oc->m_val, 64, 0);
148
-    av_des_crypt(&av_des, oc->s_val, NULL, 1, NULL, 0);
149
-
150
-    /* sm_val */
151
-    pos = OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size;
152
-    av_des_init(&av_des, oc->s_val, 64, 0);
153
-    av_des_mac(&av_des, oc->sm_val, &enc_header[pos], (oc->i_size >> 3));
154
-
155
-    pos += oc->i_size;
156
-
157
-    return memcmp(&enc_header[pos], oc->sm_val, 8) ? -1 : 0;
158
-}
159
-
160
-static int nprobe(AVFormatContext *s, uint8_t *enc_header, int size, const uint8_t *n_val)
161
-{
162
-    OMAContext *oc = s->priv_data;
163
-    uint32_t pos, taglen, datalen;
164
-    struct AVDES av_des;
165
-
166
-    if (!enc_header || !n_val)
167
-        return -1;
168
-
169
-    pos = OMA_ENC_HEADER_SIZE + oc->k_size;
170
-    if (!memcmp(&enc_header[pos], "EKB ", 4))
171
-        pos += 32;
172
-
173
-    if (AV_RB32(&enc_header[pos]) != oc->rid)
174
-        av_log(s, AV_LOG_DEBUG, "Mismatching RID\n");
175
-
176
-    taglen = AV_RB32(&enc_header[pos+32]);
177
-    datalen = AV_RB32(&enc_header[pos+36]) >> 4;
178
-
179
-    if(taglen + (((uint64_t)datalen)<<4) + 44 > size)
180
-        return -1;
181
-
182
-    pos += 44 + taglen;
183
-
184
-    av_des_init(&av_des, n_val, 192, 1);
185
-    while (datalen-- > 0) {
186
-        av_des_crypt(&av_des, oc->r_val, &enc_header[pos], 2, NULL, 1);
187
-        kset(s, oc->r_val, NULL, 16);
188
-        if (!rprobe(s, enc_header, oc->r_val))
189
-            return 0;
190
-        pos += 16;
191
-    }
192
-
193
-    return -1;
194
-}
195
-
196
-static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
197
-{
198
-    OMAContext *oc = s->priv_data;
199
-    ID3v2ExtraMetaGEOB *geob = NULL;
200
-    uint8_t *gdata;
201
-
202
-    oc->encrypted = 1;
203
-    av_log(s, AV_LOG_INFO, "File is encrypted\n");
204
-
205
-    /* find GEOB metadata */
206
-    while (em) {
207
-        if (!strcmp(em->tag, "GEOB") &&
208
-            (geob = em->data) &&
209
-            (!strcmp(geob->description, "OMG_LSI") ||
210
-             !strcmp(geob->description, "OMG_BKLSI"))) {
211
-            break;
212
-        }
213
-        em = em->next;
214
-    }
215
-    if (!em) {
216
-        av_log(s, AV_LOG_ERROR, "No encryption header found\n");
217
-        return -1;
218
-    }
219
-
220
-    if (geob->datasize < 64) {
221
-        av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\n", geob->datasize);
222
-        return -1;
223
-    }
224
-
225
-    gdata = geob->data;
226
-
227
-    if (AV_RB16(gdata) != 1)
228
-        av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n");
229
-
230
-    oc->k_size = AV_RB16(&gdata[2]);
231
-    oc->e_size = AV_RB16(&gdata[4]);
232
-    oc->i_size = AV_RB16(&gdata[6]);
233
-    oc->s_size = AV_RB16(&gdata[8]);
234
-
235
-    if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING     ", 12)) {
236
-        av_log(s, AV_LOG_ERROR, "Invalid encryption header\n");
237
-        return -1;
238
-    }
239
-    oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
240
-    av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid);
241
-
242
-    memcpy(oc->iv, &header[0x58], 8);
243
-    hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);
244
-
245
-    hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);
246
-
247
-    if (s->keylen > 0) {
248
-        kset(s, s->key, s->key, s->keylen);
249
-    }
250
-    if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||
251
-        rprobe(s, gdata, oc->r_val) < 0 &&
252
-        nprobe(s, gdata, geob->datasize, oc->n_val) < 0) {
253
-        int i;
254
-        for (i = 0; i < sizeof(leaf_table); i += 2) {
255
-            uint8_t buf[16];
256
-            AV_WL64(buf, leaf_table[i]);
257
-            AV_WL64(&buf[8], leaf_table[i+1]);
258
-            kset(s, buf, buf, 16);
259
-            if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, geob->datasize, oc->n_val))
260
-                break;
261
-        }
262
-        if (i >= sizeof(leaf_table)) {
263
-            av_log(s, AV_LOG_ERROR, "Invalid key\n");
264
-            return -1;
265
-        }
266
-    }
267
-
268
-    /* e_val */
269
-    av_des_init(&oc->av_des, oc->m_val, 64, 0);
270
-    av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);
271
-    hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);
272
-
273
-    /* init e_val */
274
-    av_des_init(&oc->av_des, oc->e_val, 64, 1);
275
-
276
-    return 0;
277
-}
278
-
279
-static int oma_read_header(AVFormatContext *s,
280
-                           AVFormatParameters *ap)
281
-{
282
-    static const uint16_t srate_tab[6] = {320,441,480,882,960,0};
283
-    int     ret, framesize, jsflag, samplerate;
284
-    uint32_t codec_params;
285
-    int16_t eid;
286
-    uint8_t buf[EA3_HEADER_SIZE];
287
-    uint8_t *edata;
288
-    AVStream *st;
289
-    ID3v2ExtraMeta *extra_meta = NULL;
290
-    OMAContext *oc = s->priv_data;
291
-
292
-    ff_id3v2_read_all(s, ID3v2_EA3_MAGIC, &extra_meta);
293
-    ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
294
-    if (ret < EA3_HEADER_SIZE)
295
-        return -1;
296
-
297
-    if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
298
-        av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
299
-        return -1;
300
-    }
301
-
302
-    oc->content_start = avio_tell(s->pb);
303
-
304
-    /* encrypted file */
305
-    eid = AV_RB16(&buf[6]);
306
-    if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) {
307
-        ff_id3v2_free_extra_meta(&extra_meta);
308
-        return -1;
309
-    }
310
-
311
-    ff_id3v2_free_extra_meta(&extra_meta);
312
-
313
-    codec_params = AV_RB24(&buf[33]);
314
-
315
-    st = avformat_new_stream(s, NULL);
316
-    if (!st)
317
-        return AVERROR(ENOMEM);
318
-
319
-    st->start_time = 0;
320
-    st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
321
-    st->codec->codec_tag   = buf[32];
322
-    st->codec->codec_id    = ff_codec_get_id(codec_oma_tags, st->codec->codec_tag);
323
-
324
-    switch (buf[32]) {
325
-        case OMA_CODECID_ATRAC3:
326
-            samplerate = srate_tab[(codec_params >> 13) & 7]*100;
327
-            if (samplerate != 44100)
328
-                av_log_ask_for_sample(s, "Unsupported sample rate: %d\n",
329
-                                      samplerate);
330
-
331
-            framesize = (codec_params & 0x3FF) * 8;
332
-            jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
333
-            st->codec->channels    = 2;
334
-            st->codec->sample_rate = samplerate;
335
-            st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
336
-
337
-            /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
338
-            st->codec->extradata_size = 14;
339
-            edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
340
-            if (!edata)
341
-                return AVERROR(ENOMEM);
342
-
343
-            st->codec->extradata = edata;
344
-            AV_WL16(&edata[0],  1);             // always 1
345
-            AV_WL32(&edata[2],  samplerate);    // samples rate
346
-            AV_WL16(&edata[6],  jsflag);        // coding mode
347
-            AV_WL16(&edata[8],  jsflag);        // coding mode
348
-            AV_WL16(&edata[10], 1);             // always 1
349
-            // AV_WL16(&edata[12], 0);          // always 0
350
-
351
-            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
352
-            break;
353
-        case OMA_CODECID_ATRAC3P:
354
-            st->codec->channels = (codec_params >> 10) & 7;
355
-            framesize = ((codec_params & 0x3FF) * 8) + 8;
356
-            st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
357
-            st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
358
-            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
359
-            av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
360
-            break;
361
-        case OMA_CODECID_MP3:
362
-            st->need_parsing = AVSTREAM_PARSE_FULL;
363
-            framesize = 1024;
364
-            break;
365
-        case OMA_CODECID_LPCM:
366
-            /* PCM 44.1 kHz 16 bit stereo big-endian */
367
-            st->codec->channels = 2;
368
-            st->codec->sample_rate = 44100;
369
-            framesize = 1024;
370
-            /* bit rate = sample rate x PCM block align (= 4) x 8 */
371
-            st->codec->bit_rate = st->codec->sample_rate * 32;
372
-            st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
373
-            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
374
-            break;
375
-        default:
376
-            av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
377
-            return -1;
378
-    }
379
-
380
-    st->codec->block_align = framesize;
381
-
382
-    return 0;
383
-}
384
-
385
-
386
-static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
387
-{
388
-    OMAContext *oc = s->priv_data;
389
-    int packet_size = s->streams[0]->codec->block_align;
390
-    int ret = av_get_packet(s->pb, pkt, packet_size);
391
-
392
-    if (ret <= 0)
393
-        return AVERROR(EIO);
394
-
395
-    pkt->stream_index = 0;
396
-
397
-    if (oc->encrypted) {
398
-        /* previous unencrypted block saved in IV for the next packet (CBC mode) */
399
-        av_des_crypt(&oc->av_des, pkt->data, pkt->data, (packet_size >> 3), oc->iv, 1);
400
-    }
401
-
402
-    return ret;
403
-}
404
-
405
-static int oma_read_probe(AVProbeData *p)
406
-{
407
-    const uint8_t *buf;
408
-    unsigned tag_len = 0;
409
-
410
-    buf = p->buf;
411
-
412
-    if (p->buf_size < ID3v2_HEADER_SIZE ||
413
-        !ff_id3v2_match(buf, ID3v2_EA3_MAGIC) ||
414
-        buf[3] != 3 || // version must be 3
415
-        buf[4]) // flags byte zero
416
-        return 0;
417
-
418
-    tag_len = ff_id3v2_tag_len(buf);
419
-
420
-    /* This check cannot overflow as tag_len has at most 28 bits */
421
-    if (p->buf_size < tag_len + 5)
422
-        /* EA3 header comes late, might be outside of the probe buffer */
423
-        return AVPROBE_SCORE_MAX / 2;
424
-
425
-    buf += tag_len;
426
-
427
-    if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE)
428
-        return AVPROBE_SCORE_MAX;
429
-    else
430
-        return 0;
431
-}
432
-
433
-static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
434
-{
435
-    OMAContext *oc = s->priv_data;
436
-
437
-    pcm_read_seek(s, stream_index, timestamp, flags);
438
-
439
-    if (oc->encrypted) {
440
-        /* readjust IV for CBC */
441
-        int64_t pos = avio_tell(s->pb);
442
-        if (pos < oc->content_start)
443
-            memset(oc->iv, 0, 8);
444
-        else {
445
-            if (avio_seek(s->pb, -8, SEEK_CUR) < 0 || avio_read(s->pb, oc->iv, 8) < 8) {
446
-                memset(oc->iv, 0, 8);
447
-                return -1;
448
-            }
449
-        }
450
-    }
451
-
452
-    return 0;
453
-}
454
-
455
-AVInputFormat ff_oma_demuxer = {
456
-    .name           = "oma",
457
-    .long_name      = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
458
-    .priv_data_size = sizeof(OMAContext),
459
-    .read_probe     = oma_read_probe,
460
-    .read_header    = oma_read_header,
461
-    .read_packet    = oma_read_packet,
462
-    .read_seek      = oma_read_seek,
463
-    .flags          = AVFMT_GENERIC_INDEX,
464
-    .extensions     = "oma,omg,aa3",
465
-    .codec_tag      = (const AVCodecTag* const []){codec_oma_tags, 0},
34
+    { CODEC_ID_MP3,         OMA_CODECID_MP3     },
35
+    { CODEC_ID_PCM_S16BE,   OMA_CODECID_LPCM    },
36
+    { 0 },
466 37
 };
467 38
 
468 39
new file mode 100644
... ...
@@ -0,0 +1,39 @@
0
+/*
1
+ * Sony OpenMG (OMA) common data
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#ifndef AVFORMAT_OMA_H
21
+#define AVFORMAT_OMA_H
22
+
23
+#define EA3_HEADER_SIZE 96
24
+#define ID3v2_EA3_MAGIC "ea3"
25
+#define OMA_ENC_HEADER_SIZE 16
26
+
27
+enum {
28
+    OMA_CODECID_ATRAC3  = 0,
29
+    OMA_CODECID_ATRAC3P = 1,
30
+    OMA_CODECID_MP3     = 3,
31
+    OMA_CODECID_LPCM    = 4,
32
+    OMA_CODECID_WMA     = 5,
33
+};
34
+
35
+extern const uint16_t ff_oma_srate_tab[6];
36
+
37
+extern const AVCodecTag ff_oma_codec_tags[];
38
+#endif
0 39
new file mode 100644
... ...
@@ -0,0 +1,449 @@
0
+/*
1
+ * Sony OpenMG (OMA) demuxer
2
+ *
3
+ * Copyright (c) 2008 Maxim Poliakovski
4
+ *               2008 Benjamin Larsson
5
+ *               2011 David Goldwich
6
+ *
7
+ * This file is part of FFmpeg.
8
+ *
9
+ * FFmpeg is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * FFmpeg is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with FFmpeg; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+/**
25
+ * @file
26
+ * This is a demuxer for Sony OpenMG Music files
27
+ *
28
+ * Known file extensions: ".oma", "aa3"
29
+ * The format of such files consists of three parts:
30
+ * - "ea3" header carrying overall info and metadata. Except for starting with
31
+ *   "ea" instead of "ID", it's an ID3v2 header.
32
+ * - "EA3" header is a Sony-specific header containing information about
33
+ *   the OpenMG file: codec type (usually ATRAC, can also be MP3 or WMA),
34
+ *   codec specific info (packet size, sample rate, channels and so on)
35
+ *   and DRM related info (file encryption, content id).
36
+ * - Sound data organized in packets follow the EA3 header
37
+ *   (can be encrypted using the Sony DRM!).
38
+ *
39
+ * CODEC SUPPORT: Only ATRAC3 codec is currently supported!
40
+ */
41
+
42
+#include "avformat.h"
43
+#include "internal.h"
44
+#include "libavutil/intreadwrite.h"
45
+#include "libavutil/des.h"
46
+#include "oma.h"
47
+#include "pcm.h"
48
+#include "riff.h"
49
+#include "id3v2.h"
50
+
51
+
52
+static const uint64_t leaf_table[] = {
53
+    0xd79e8283acea4620, 0x7a9762f445afd0d8,
54
+    0x354d60a60b8c79f1, 0x584e1cde00b07aee,
55
+    0x1573cd93da7df623, 0x47f98d79620dd535
56
+};
57
+
58
+typedef struct OMAContext {
59
+    uint64_t content_start;
60
+    int encrypted;
61
+    uint16_t k_size;
62
+    uint16_t e_size;
63
+    uint16_t i_size;
64
+    uint16_t s_size;
65
+    uint32_t rid;
66
+    uint8_t r_val[24];
67
+    uint8_t n_val[24];
68
+    uint8_t m_val[8];
69
+    uint8_t s_val[8];
70
+    uint8_t sm_val[8];
71
+    uint8_t e_val[8];
72
+    uint8_t iv[8];
73
+    struct AVDES av_des;
74
+} OMAContext;
75
+
76
+static void hex_log(AVFormatContext *s, int level, const char *name, const uint8_t *value, int len)
77
+{
78
+    char buf[33];
79
+    len = FFMIN(len, 16);
80
+    if (av_log_get_level() < level)
81
+        return;
82
+    ff_data_to_hex(buf, value, len, 1);
83
+    buf[len<<1] = '\0';
84
+    av_log(s, level, "%s: %s\n", name, buf);
85
+}
86
+
87
+static int kset(AVFormatContext *s, const uint8_t *r_val, const uint8_t *n_val, int len)
88
+{
89
+    OMAContext *oc = s->priv_data;
90
+
91
+    if (!r_val && !n_val)
92
+        return -1;
93
+
94
+    len = FFMIN(len, 16);
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
+
115
+static int rprobe(AVFormatContext *s, uint8_t *enc_header, const uint8_t *r_val)
116
+{
117
+    OMAContext *oc = s->priv_data;
118
+    unsigned int pos;
119
+    struct AVDES av_des;
120
+
121
+    if (!enc_header || !r_val)
122
+        return -1;
123
+
124
+    /* m_val */
125
+    av_des_init(&av_des, r_val, 192, 1);
126
+    av_des_crypt(&av_des, oc->m_val, &enc_header[48], 1, NULL, 1);
127
+
128
+    /* s_val */
129
+    av_des_init(&av_des, oc->m_val, 64, 0);
130
+    av_des_crypt(&av_des, oc->s_val, NULL, 1, NULL, 0);
131
+
132
+    /* sm_val */
133
+    pos = OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size;
134
+    av_des_init(&av_des, oc->s_val, 64, 0);
135
+    av_des_mac(&av_des, oc->sm_val, &enc_header[pos], (oc->i_size >> 3));
136
+
137
+    pos += oc->i_size;
138
+
139
+    return memcmp(&enc_header[pos], oc->sm_val, 8) ? -1 : 0;
140
+}
141
+
142
+static int nprobe(AVFormatContext *s, uint8_t *enc_header, int size, const uint8_t *n_val)
143
+{
144
+    OMAContext *oc = s->priv_data;
145
+    uint32_t pos, taglen, datalen;
146
+    struct AVDES av_des;
147
+
148
+    if (!enc_header || !n_val)
149
+        return -1;
150
+
151
+    pos = OMA_ENC_HEADER_SIZE + oc->k_size;
152
+    if (!memcmp(&enc_header[pos], "EKB ", 4))
153
+        pos += 32;
154
+
155
+    if (AV_RB32(&enc_header[pos]) != oc->rid)
156
+        av_log(s, AV_LOG_DEBUG, "Mismatching RID\n");
157
+
158
+    taglen = AV_RB32(&enc_header[pos+32]);
159
+    datalen = AV_RB32(&enc_header[pos+36]) >> 4;
160
+
161
+    if(taglen + (((uint64_t)datalen)<<4) + 44 > size)
162
+        return -1;
163
+
164
+    pos += 44 + taglen;
165
+
166
+    av_des_init(&av_des, n_val, 192, 1);
167
+    while (datalen-- > 0) {
168
+        av_des_crypt(&av_des, oc->r_val, &enc_header[pos], 2, NULL, 1);
169
+        kset(s, oc->r_val, NULL, 16);
170
+        if (!rprobe(s, enc_header, oc->r_val))
171
+            return 0;
172
+        pos += 16;
173
+    }
174
+
175
+    return -1;
176
+}
177
+
178
+static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
179
+{
180
+    OMAContext *oc = s->priv_data;
181
+    ID3v2ExtraMetaGEOB *geob = NULL;
182
+    uint8_t *gdata;
183
+
184
+    oc->encrypted = 1;
185
+    av_log(s, AV_LOG_INFO, "File is encrypted\n");
186
+
187
+    /* find GEOB metadata */
188
+    while (em) {
189
+        if (!strcmp(em->tag, "GEOB") &&
190
+            (geob = em->data) &&
191
+            (!strcmp(geob->description, "OMG_LSI") ||
192
+             !strcmp(geob->description, "OMG_BKLSI"))) {
193
+            break;
194
+        }
195
+        em = em->next;
196
+    }
197
+    if (!em) {
198
+        av_log(s, AV_LOG_ERROR, "No encryption header found\n");
199
+        return -1;
200
+    }
201
+
202
+    if (geob->datasize < 64) {
203
+        av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\n", geob->datasize);
204
+        return -1;
205
+    }
206
+
207
+    gdata = geob->data;
208
+
209
+    if (AV_RB16(gdata) != 1)
210
+        av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n");
211
+
212
+    oc->k_size = AV_RB16(&gdata[2]);
213
+    oc->e_size = AV_RB16(&gdata[4]);
214
+    oc->i_size = AV_RB16(&gdata[6]);
215
+    oc->s_size = AV_RB16(&gdata[8]);
216
+
217
+    if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING     ", 12)) {
218
+        av_log(s, AV_LOG_ERROR, "Invalid encryption header\n");
219
+        return -1;
220
+    }
221
+    oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
222
+    av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid);
223
+
224
+    memcpy(oc->iv, &header[0x58], 8);
225
+    hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);
226
+
227
+    hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);
228
+
229
+    if (s->keylen > 0) {
230
+        kset(s, s->key, s->key, s->keylen);
231
+    }
232
+    if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||
233
+        rprobe(s, gdata, oc->r_val) < 0 &&
234
+        nprobe(s, gdata, geob->datasize, oc->n_val) < 0) {
235
+        int i;
236
+        for (i = 0; i < sizeof(leaf_table); i += 2) {
237
+            uint8_t buf[16];
238
+            AV_WL64(buf, leaf_table[i]);
239
+            AV_WL64(&buf[8], leaf_table[i+1]);
240
+            kset(s, buf, buf, 16);
241
+            if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, geob->datasize, oc->n_val))
242
+                break;
243
+        }
244
+        if (i >= sizeof(leaf_table)) {
245
+            av_log(s, AV_LOG_ERROR, "Invalid key\n");
246
+            return -1;
247
+        }
248
+    }
249
+
250
+    /* e_val */
251
+    av_des_init(&oc->av_des, oc->m_val, 64, 0);
252
+    av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);
253
+    hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);
254
+
255
+    /* init e_val */
256
+    av_des_init(&oc->av_des, oc->e_val, 64, 1);
257
+
258
+    return 0;
259
+}
260
+
261
+static int oma_read_header(AVFormatContext *s,
262
+                           AVFormatParameters *ap)
263
+{
264
+    int     ret, framesize, jsflag, samplerate;
265
+    uint32_t codec_params;
266
+    int16_t eid;
267
+    uint8_t buf[EA3_HEADER_SIZE];
268
+    uint8_t *edata;
269
+    AVStream *st;
270
+    ID3v2ExtraMeta *extra_meta = NULL;
271
+    OMAContext *oc = s->priv_data;
272
+
273
+    ff_id3v2_read_all(s, ID3v2_EA3_MAGIC, &extra_meta);
274
+    ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
275
+    if (ret < EA3_HEADER_SIZE)
276
+        return -1;
277
+
278
+    if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
279
+        av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
280
+        return -1;
281
+    }
282
+
283
+    oc->content_start = avio_tell(s->pb);
284
+
285
+    /* encrypted file */
286
+    eid = AV_RB16(&buf[6]);
287
+    if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) {
288
+        ff_id3v2_free_extra_meta(&extra_meta);
289
+        return -1;
290
+    }
291
+
292
+    ff_id3v2_free_extra_meta(&extra_meta);
293
+
294
+    codec_params = AV_RB24(&buf[33]);
295
+
296
+    st = avformat_new_stream(s, NULL);
297
+    if (!st)
298
+        return AVERROR(ENOMEM);
299
+
300
+    st->start_time = 0;
301
+    st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
302
+    st->codec->codec_tag   = buf[32];
303
+    st->codec->codec_id    = ff_codec_get_id(ff_oma_codec_tags, st->codec->codec_tag);
304
+
305
+    switch (buf[32]) {
306
+        case OMA_CODECID_ATRAC3:
307
+            samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
308
+            if (samplerate != 44100)
309
+                av_log_ask_for_sample(s, "Unsupported sample rate: %d\n",
310
+                                      samplerate);
311
+
312
+            framesize = (codec_params & 0x3FF) * 8;
313
+            jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
314
+            st->codec->channels    = 2;
315
+            st->codec->sample_rate = samplerate;
316
+            st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
317
+
318
+            /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
319
+            st->codec->extradata_size = 14;
320
+            edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
321
+            if (!edata)
322
+                return AVERROR(ENOMEM);
323
+
324
+            st->codec->extradata = edata;
325
+            AV_WL16(&edata[0],  1);             // always 1
326
+            AV_WL32(&edata[2],  samplerate);    // samples rate
327
+            AV_WL16(&edata[6],  jsflag);        // coding mode
328
+            AV_WL16(&edata[8],  jsflag);        // coding mode
329
+            AV_WL16(&edata[10], 1);             // always 1
330
+            // AV_WL16(&edata[12], 0);          // always 0
331
+
332
+            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
333
+            break;
334
+        case OMA_CODECID_ATRAC3P:
335
+            st->codec->channels = (codec_params >> 10) & 7;
336
+            framesize = ((codec_params & 0x3FF) * 8) + 8;
337
+            st->codec->sample_rate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
338
+            st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
339
+            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
340
+            av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
341
+            break;
342
+        case OMA_CODECID_MP3:
343
+            st->need_parsing = AVSTREAM_PARSE_FULL;
344
+            framesize = 1024;
345
+            break;
346
+        case OMA_CODECID_LPCM:
347
+            /* PCM 44.1 kHz 16 bit stereo big-endian */
348
+            st->codec->channels = 2;
349
+            st->codec->sample_rate = 44100;
350
+            framesize = 1024;
351
+            /* bit rate = sample rate x PCM block align (= 4) x 8 */
352
+            st->codec->bit_rate = st->codec->sample_rate * 32;
353
+            st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
354
+            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
355
+            break;
356
+        default:
357
+            av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
358
+            return -1;
359
+    }
360
+
361
+    st->codec->block_align = framesize;
362
+
363
+    return 0;
364
+}
365
+
366
+
367
+static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
368
+{
369
+    OMAContext *oc = s->priv_data;
370
+    int packet_size = s->streams[0]->codec->block_align;
371
+    int ret = av_get_packet(s->pb, pkt, packet_size);
372
+
373
+    if (ret <= 0)
374
+        return AVERROR(EIO);
375
+
376
+    pkt->stream_index = 0;
377
+
378
+    if (oc->encrypted) {
379
+        /* previous unencrypted block saved in IV for the next packet (CBC mode) */
380
+        av_des_crypt(&oc->av_des, pkt->data, pkt->data, (packet_size >> 3), oc->iv, 1);
381
+    }
382
+
383
+    return ret;
384
+}
385
+
386
+static int oma_read_probe(AVProbeData *p)
387
+{
388
+    const uint8_t *buf;
389
+    unsigned tag_len = 0;
390
+
391
+    buf = p->buf;
392
+
393
+    if (p->buf_size < ID3v2_HEADER_SIZE ||
394
+        !ff_id3v2_match(buf, ID3v2_EA3_MAGIC) ||
395
+        buf[3] != 3 || // version must be 3
396
+        buf[4]) // flags byte zero
397
+        return 0;
398
+
399
+    tag_len = ff_id3v2_tag_len(buf);
400
+
401
+    /* This check cannot overflow as tag_len has at most 28 bits */
402
+    if (p->buf_size < tag_len + 5)
403
+        /* EA3 header comes late, might be outside of the probe buffer */
404
+        return AVPROBE_SCORE_MAX / 2;
405
+
406
+    buf += tag_len;
407
+
408
+    if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE)
409
+        return AVPROBE_SCORE_MAX;
410
+    else
411
+        return 0;
412
+}
413
+
414
+static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
415
+{
416
+    OMAContext *oc = s->priv_data;
417
+
418
+    pcm_read_seek(s, stream_index, timestamp, flags);
419
+
420
+    if (oc->encrypted) {
421
+        /* readjust IV for CBC */
422
+        int64_t pos = avio_tell(s->pb);
423
+        if (pos < oc->content_start)
424
+            memset(oc->iv, 0, 8);
425
+        else {
426
+            if (avio_seek(s->pb, -8, SEEK_CUR) < 0 || avio_read(s->pb, oc->iv, 8) < 8) {
427
+                memset(oc->iv, 0, 8);
428
+                return -1;
429
+            }
430
+        }
431
+    }
432
+
433
+    return 0;
434
+}
435
+
436
+AVInputFormat ff_oma_demuxer = {
437
+    .name           = "oma",
438
+    .long_name      = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
439
+    .priv_data_size = sizeof(OMAContext),
440
+    .read_probe     = oma_read_probe,
441
+    .read_header    = oma_read_header,
442
+    .read_packet    = oma_read_packet,
443
+    .read_seek      = oma_read_seek,
444
+    .flags          = AVFMT_GENERIC_INDEX,
445
+    .extensions     = "oma,omg,aa3",
446
+    .codec_tag      = (const AVCodecTag* const []){ff_oma_codec_tags, 0},
447
+};
448
+
0 449
new file mode 100644
... ...
@@ -0,0 +1,105 @@
0
+/*
1
+ * Sony OpenMG (OMA) muxer
2
+ *
3
+ * Copyright (c) 2011 Michael Karcher
4
+ *
5
+ * This file is part of Libav.
6
+ *
7
+ * Libav is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * Libav is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with Libav; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "avformat.h"
23
+#include "avio_internal.h"
24
+#include "id3v2.h"
25
+#include "internal.h"
26
+#include "oma.h"
27
+#include "rawenc.h"
28
+
29
+static av_cold int oma_write_header(AVFormatContext *s)
30
+{
31
+    int i;
32
+    AVCodecContext *format;
33
+    int srate_index;
34
+    int isjointstereo;
35
+
36
+    format = s->streams[0]->codec;
37
+    /* check for support of the format first */
38
+
39
+    for (srate_index = 0; ; srate_index++) {
40
+        if (ff_oma_srate_tab[srate_index] == 0) {
41
+            av_log(s, AV_LOG_ERROR, "Sample rate %d not supported in OpenMG audio\n",
42
+                   format->sample_rate);
43
+            return AVERROR(EINVAL);
44
+        }
45
+
46
+        if (ff_oma_srate_tab[srate_index] * 100 == format->sample_rate)
47
+            break;
48
+    }
49
+
50
+    /* Metadata; OpenMG does not support ID3v2.4 */
51
+    ff_id3v2_write(s, 3, ID3v2_EA3_MAGIC);
52
+
53
+    ffio_wfourcc(s->pb, "EA3\0");
54
+    avio_w8(s->pb, EA3_HEADER_SIZE >> 7);
55
+    avio_w8(s->pb, EA3_HEADER_SIZE & 0x7F);
56
+    avio_wl16(s->pb, 0xFFFF);       /* not encrypted */
57
+    for (i = 0; i < 6; i++)
58
+        avio_wl32(s->pb, 0);        /* Padding + DRM id */
59
+
60
+    switch(format->codec_tag) {
61
+    case OMA_CODECID_ATRAC3:
62
+        if (format->channels != 2) {
63
+            av_log(s, AV_LOG_ERROR, "ATRAC3 in OMA is only supported with 2 channels");
64
+            return AVERROR(EINVAL);
65
+        }
66
+        if (format->extradata_size == 14) /* WAV format extradata */
67
+            isjointstereo = format->extradata[6] != 0;
68
+        else if(format->extradata_size == 10) /* RM format extradata */
69
+            isjointstereo = format->extradata[8] == 0x12;
70
+        else {
71
+            av_log(s, AV_LOG_ERROR, "ATRAC3: Unsupported extradata size\n");
72
+            return AVERROR(EINVAL);
73
+        }
74
+        avio_wb32(s->pb, (OMA_CODECID_ATRAC3 << 24) |
75
+                         (isjointstereo << 17) |
76
+                         (srate_index << 13) |
77
+                         (format->block_align/8));
78
+        break;
79
+    case OMA_CODECID_ATRAC3P:
80
+        avio_wb32(s->pb, (OMA_CODECID_ATRAC3P << 24) |
81
+                         (srate_index << 13) |
82
+                         (format->channels << 10) |
83
+                         (format->block_align/8 - 1));
84
+        break;
85
+    default:
86
+        av_log(s, AV_LOG_ERROR, "OMA: unsupported codec tag %d for write\n",
87
+               format->codec_tag);
88
+    }
89
+    for (i = 0; i < (EA3_HEADER_SIZE - 36)/4; i++)
90
+        avio_wl32(s->pb, 0);        /* Padding */
91
+
92
+    return 0;
93
+}
94
+
95
+AVOutputFormat ff_oma_muxer = {
96
+    .name              = "oma",
97
+    .long_name         = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
98
+    .mime_type         = "audio/x-oma",
99
+    .extensions        = "oma",
100
+    .audio_codec       = CODEC_ID_ATRAC3,
101
+    .write_header      = oma_write_header,
102
+    .write_packet      = ff_raw_write_packet,
103
+    .codec_tag         = (const AVCodecTag* const []){ff_oma_codec_tags, 0},
104
+};
... ...
@@ -2393,6 +2393,11 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2393 2393
         assert(!st->codec->codec);
2394 2394
         codec = avcodec_find_decoder(st->codec->codec_id);
2395 2395
 
2396
+        /* this function doesn't flush the decoders, so force thread count
2397
+         * to 1 to fix behavior when thread count > number of frames in the file */
2398
+        if (options)
2399
+            av_dict_set(&options[i], "threads", 0, 0);
2400
+
2396 2401
         /* Ensure that subtitle_header is properly set. */
2397 2402
         if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
2398 2403
             && codec && !st->codec->codec)
... ...
@@ -2400,15 +2405,8 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2400 2400
 
2401 2401
         //try to just open decoders, in case this is enough to get parameters
2402 2402
         if(!has_codec_parameters(st->codec)){
2403
-            if (codec && !st->codec->codec){
2404
-                AVDictionary *tmp = NULL;
2405
-                if (options){
2406
-                    av_dict_copy(&tmp, options[i], 0);
2407
-                    av_dict_set(&tmp, "threads", 0, 0);
2408
-                }
2409
-                avcodec_open2(st->codec, codec, options ? &tmp : NULL);
2410
-                av_dict_free(&tmp);
2411
-            }
2403
+            if (codec && !st->codec->codec)
2404
+                avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
2412 2405
         }
2413 2406
     }
2414 2407
 
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 53
33
-#define LIBAVFORMAT_VERSION_MINOR 26
33
+#define LIBAVFORMAT_VERSION_MINOR 27
34 34
 #define LIBAVFORMAT_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \