* 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
Unified · Split
| @@ -137,6 +137,7 @@ easier to use. The changes are: · 136 unmodified lines | ||
| 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: · 30 unmodified lines | ||
| 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: · 316 unmodified lines | ||
| 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) · 1582 unmodified lines | ||
| 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){ · 439 unmodified lines | ||
| 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 { · 1028 unmodified lines | ||
| 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 @@ · 29 unmodified lines | ||
| 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) · 262 unmodified lines | ||
| 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) · 97 unmodified lines | ||
| 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, · 190 unmodified lines | ||
| 574 | 565 | s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1); |
| 575 | 566 | s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1); |
| 576 | 567 | |
| 577 | − memcpy(s->prev_pict_types, s1->prev_pict_types, | |
| 578 | − PREV_PICT_TYPES_BUFFER_SIZE); | |
| 579 | − | |
| 580 | 568 | // Error/bug resilience |
| 581 | 569 | s->next_p_frame_damaged = s1->next_p_frame_damaged; |
| 582 | 570 | s->workaround_bugs = s1->workaround_bugs; |
| @@ -837,7 +825,6 @@ av_cold int MPV_common_init(MpegEncContext *s) · 254 unmodified lines | ||
| 837 | 825 | /* init macroblock skip table */ |
| 838 | 826 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail); |
| 839 | 827 | // 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 | 828 | |
| 842 | 829 | s->parse_context.state = -1; |
| 843 | 830 | if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || |
| @@ -928,7 +915,6 @@ void MPV_common_end(MpegEncContext *s) · 84 unmodified lines | ||
| 928 | 915 | av_freep(&s->pred_dir_table); |
| 929 | 916 | |
| 930 | 917 | av_freep(&s->mbskip_table); |
| 931 | − av_freep(&s->prev_pict_types); | |
| 932 | 918 | av_freep(&s->bitstream_buffer); |
| 933 | 919 | s->allocated_bitstream_buffer_size = 0; |
| 934 | 920 | |
| @@ -2201,24 +2187,13 @@ void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64], · 1266 unmodified lines | ||
| 2201 | 2187 | /* skip only during decoding as we might trash the buffers during encoding a bit */ |
| 2202 | 2188 | if(!s->encoding){ |
| 2203 | 2189 | uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy]; |
| 2204 | − const int age = s->current_picture.f.age; | |
| 2205 | − | |
| 2206 | − assert(age); | |
| 2207 | 2190 | |
| 2208 | 2191 | if (s->mb_skipped) { |
| 2209 | 2192 | s->mb_skipped= 0; |
| 2210 | 2193 | 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 | − } | |
| 2194 | + *mbskip_ptr = 1; | |
| 2219 | 2195 | } 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; | |
| 2196 | + *mbskip_ptr = 1; | |
| 2222 | 2197 | } else{ |
| 2223 | 2198 | *mbskip_ptr = 0; /* not skipped */ |
| 2224 | 2199 | } |
| @@ -311,8 +311,6 @@ typedef struct MpegEncContext { · 310 unmodified lines | ||
| 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) · 867 unmodified lines | ||
| 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 { · 33 unmodified lines | ||
| 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, · 42 unmodified lines | ||
| 90 | 82 | return avpkt->size; |
| 91 | 83 | } |
| 92 | 84 | |
| 85 | +static av_cold int tmv_decode_init(AVCodecContext *avctx) | |
| 86 | +{ | |
| 87 | + TMVContext *tmv = avctx->priv_data; | |
| 88 | + avctx->pix_fmt = PIX_FMT_PAL8; | |
| 89 | + avcodec_get_frame_defaults(&tmv->pic); | |
| 90 | + return 0; | |
| 91 | +} | |
| 92 | + | |
| 93 | 93 | static av_cold int tmv_decode_close(AVCodecContext *avctx) |
| 94 | 94 | { |
| 95 | 95 | TMVContext *tmv = avctx->priv_data; |
| @@ -373,7 +373,6 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic) · 372 unmodified lines | ||
| 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) · 14 unmodified lines | ||
| 394 | 393 | } |
| 395 | 394 | |
| 396 | 395 | buf = &avci->buffer[avci->buffer_count]; |
| 397 | − picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack | |
| 398 | − (*picture_number)++; | |
| 399 | 396 | |
| 400 | 397 | if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){ |
| 401 | 398 | if(s->active_thread_type&FF_THREAD_FRAME) { |
| @@ -409,10 +406,7 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic) · 7 unmodified lines | ||
| 409 | 406 | } |
| 410 | 407 | } |
| 411 | 408 | |
| 412 | − if(buf->base[0]){ | |
| 413 | − pic->age= *picture_number - buf->last_pic_num; | |
| 414 | − buf->last_pic_num= *picture_number; | |
| 415 | − }else{ | |
| 409 | + if (!buf->base[0]) { | |
| 416 | 410 | int h_chroma_shift, v_chroma_shift; |
| 417 | 411 | int size[4] = {0}; |
| 418 | 412 | int tmpsize; |
| @@ -451,7 +445,6 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic) · 32 unmodified lines | ||
| 451 | 445 | size[i] = picture.data[i+1] - picture.data[i]; |
| 452 | 446 | size[i] = tmpsize - (picture.data[i] - picture.data[0]); |
| 453 | 447 | |
| 454 | − buf->last_pic_num= -256*256*256*64; | |
| 455 | 448 | memset(buf->base, 0, sizeof(buf->base)); |
| 456 | 449 | memset(buf->data, 0, sizeof(buf->data)); |
| 457 | 450 | |
| @@ -480,7 +473,6 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic) · 22 unmodified lines | ||
| 480 | 473 | buf->width = s->width; |
| 481 | 474 | buf->height = s->height; |
| 482 | 475 | buf->pix_fmt= s->pix_fmt; |
| 483 | − pic->age= 256*256*256*64; | |
| 484 | 476 | } |
| 485 | 477 | pic->type= FF_BUFFER_TYPE_INTERNAL; |
| 486 | 478 | |
| @@ -22,7 +22,7 @@ · 21 unmodified lines | ||
| 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 @@ · 91 unmodified lines | ||
| 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 \ · 193 unmodified lines | ||
| 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) · 165 unmodified lines | ||
| 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 @@ · 12 unmodified lines | ||
| 22 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | 19 | */ |
| 24 | 20 | |
| 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 | 21 | #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" | |
| 22 | +#include "oma.h" | |
| 23 | +#include "libavcodec/avcodec.h" | |
| 50 | 24 | |
| 51 | −#define EA3_HEADER_SIZE 96 | |
| 52 | −#define ID3v2_EA3_MAGIC "ea3" | |
| 53 | −#define OMA_ENC_HEADER_SIZE 16 | |
| 25 | +const uint16_t ff_oma_srate_tab[6] = { 320, 441, 480, 882, 960, 0 }; | |
| 54 | 26 | |
| 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 }, | |
| 27 | +const AVCodecTag ff_oma_codec_tags[] = { | |
| 28 | + { CODEC_ID_ATRAC3, OMA_CODECID_ATRAC3 }, | |
| 65 | 29 | { 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}, | |
| 30 | + { CODEC_ID_MP3, OMA_CODECID_MP3 }, | |
| 31 | + { CODEC_ID_PCM_S16BE, OMA_CODECID_LPCM }, | |
| 32 | + { 0 }, | |
| 466 | 33 | }; |
| 467 | 34 | |
| @@ -0,0 +1,39 @@ | ||
| 1 | +/* | |
| 2 | + * Sony OpenMG (OMA) common data | |
| 3 | + * | |
| 4 | + * This file is part of Libav. | |
| 5 | + * | |
| 6 | + * Libav is free software; you can redistribute it and/or | |
| 7 | + * modify it under the terms of the GNU Lesser General Public | |
| 8 | + * License as published by the Free Software Foundation; either | |
| 9 | + * version 2.1 of the License, or (at your option) any later version. | |
| 10 | + * | |
| 11 | + * Libav is distributed in the hope that it will be useful, | |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | + * Lesser General Public License for more details. | |
| 15 | + * | |
| 16 | + * You should have received a copy of the GNU Lesser General Public | |
| 17 | + * License along with Libav; if not, write to the Free Software | |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 19 | + */ | |
| 20 | + | |
| 21 | +#ifndef AVFORMAT_OMA_H | |
| 22 | +#define AVFORMAT_OMA_H | |
| 23 | + | |
| 24 | +#define EA3_HEADER_SIZE 96 | |
| 25 | +#define ID3v2_EA3_MAGIC "ea3" | |
| 26 | +#define OMA_ENC_HEADER_SIZE 16 | |
| 27 | + | |
| 28 | +enum { | |
| 29 | + OMA_CODECID_ATRAC3 = 0, | |
| 30 | + OMA_CODECID_ATRAC3P = 1, | |
| 31 | + OMA_CODECID_MP3 = 3, | |
| 32 | + OMA_CODECID_LPCM = 4, | |
| 33 | + OMA_CODECID_WMA = 5, | |
| 34 | +}; | |
| 35 | + | |
| 36 | +extern const uint16_t ff_oma_srate_tab[6]; | |
| 37 | + | |
| 38 | +extern const AVCodecTag ff_oma_codec_tags[]; | |
| 39 | +#endif | |
| @@ -0,0 +1,449 @@ | ||
| 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 | + * CODEC SUPPORT: Only ATRAC3 codec is currently supported! | |
| 41 | + */ | |
| 42 | + | |
| 43 | +#include "avformat.h" | |
| 44 | +#include "internal.h" | |
| 45 | +#include "libavutil/intreadwrite.h" | |
| 46 | +#include "libavutil/des.h" | |
| 47 | +#include "oma.h" | |
| 48 | +#include "pcm.h" | |
| 49 | +#include "riff.h" | |
| 50 | +#include "id3v2.h" | |
| 51 | + | |
| 52 | + | |
| 53 | +static const uint64_t leaf_table[] = { | |
| 54 | + 0xd79e8283acea4620, 0x7a9762f445afd0d8, | |
| 55 | + 0x354d60a60b8c79f1, 0x584e1cde00b07aee, | |
| 56 | + 0x1573cd93da7df623, 0x47f98d79620dd535 | |
| 57 | +}; | |
| 58 | + | |
| 59 | +typedef struct OMAContext { | |
| 60 | + uint64_t content_start; | |
| 61 | + int encrypted; | |
| 62 | + uint16_t k_size; | |
| 63 | + uint16_t e_size; | |
| 64 | + uint16_t i_size; | |
| 65 | + uint16_t s_size; | |
| 66 | + uint32_t rid; | |
| 67 | + uint8_t r_val[24]; | |
| 68 | + uint8_t n_val[24]; | |
| 69 | + uint8_t m_val[8]; | |
| 70 | + uint8_t s_val[8]; | |
| 71 | + uint8_t sm_val[8]; | |
| 72 | + uint8_t e_val[8]; | |
| 73 | + uint8_t iv[8]; | |
| 74 | + struct AVDES av_des; | |
| 75 | +} OMAContext; | |
| 76 | + | |
| 77 | +static void hex_log(AVFormatContext *s, int level, const char *name, const uint8_t *value, int len) | |
| 78 | +{ | |
| 79 | + char buf[33]; | |
| 80 | + len = FFMIN(len, 16); | |
| 81 | + if (av_log_get_level() < level) | |
| 82 | + return; | |
| 83 | + ff_data_to_hex(buf, value, len, 1); | |
| 84 | + buf[len<<1] = '\0'; | |
| 85 | + av_log(s, level, "%s: %s\n", name, buf); | |
| 86 | +} | |
| 87 | + | |
| 88 | +static int kset(AVFormatContext *s, const uint8_t *r_val, const uint8_t *n_val, int len) | |
| 89 | +{ | |
| 90 | + OMAContext *oc = s->priv_data; | |
| 91 | + | |
| 92 | + if (!r_val && !n_val) | |
| 93 | + return -1; | |
| 94 | + | |
| 95 | + len = FFMIN(len, 16); | |
| 96 | + | |
| 97 | + /* use first 64 bits in the third round again */ | |
| 98 | + if (r_val) { | |
| 99 | + if (r_val != oc->r_val) { | |
| 100 | + memset(oc->r_val, 0, 24); | |
| 101 | + memcpy(oc->r_val, r_val, len); | |
| 102 | + } | |
| 103 | + memcpy(&oc->r_val[16], r_val, 8); | |
| 104 | + } | |
| 105 | + if (n_val) { | |
| 106 | + if (n_val != oc->n_val) { | |
| 107 | + memset(oc->n_val, 0, 24); | |
| 108 | + memcpy(oc->n_val, n_val, len); | |
| 109 | + } | |
| 110 | + memcpy(&oc->n_val[16], n_val, 8); | |
| 111 | + } | |
| 112 | + | |
| 113 | + return 0; | |
| 114 | +} | |
| 115 | + | |
| 116 | +static int rprobe(AVFormatContext *s, uint8_t *enc_header, const uint8_t *r_val) | |
| 117 | +{ | |
| 118 | + OMAContext *oc = s->priv_data; | |
| 119 | + unsigned int pos; | |
| 120 | + struct AVDES av_des; | |
| 121 | + | |
| 122 | + if (!enc_header || !r_val) | |
| 123 | + return -1; | |
| 124 | + | |
| 125 | + /* m_val */ | |
| 126 | + av_des_init(&av_des, r_val, 192, 1); | |
| 127 | + av_des_crypt(&av_des, oc->m_val, &enc_header[48], 1, NULL, 1); | |
| 128 | + | |
| 129 | + /* s_val */ | |
| 130 | + av_des_init(&av_des, oc->m_val, 64, 0); | |
| 131 | + av_des_crypt(&av_des, oc->s_val, NULL, 1, NULL, 0); | |
| 132 | + | |
| 133 | + /* sm_val */ | |
| 134 | + pos = OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size; | |
| 135 | + av_des_init(&av_des, oc->s_val, 64, 0); | |
| 136 | + av_des_mac(&av_des, oc->sm_val, &enc_header[pos], (oc->i_size >> 3)); | |
| 137 | + | |
| 138 | + pos += oc->i_size; | |
| 139 | + | |
| 140 | + return memcmp(&enc_header[pos], oc->sm_val, 8) ? -1 : 0; | |
| 141 | +} | |
| 142 | + | |
| 143 | +static int nprobe(AVFormatContext *s, uint8_t *enc_header, int size, const uint8_t *n_val) | |
| 144 | +{ | |
| 145 | + OMAContext *oc = s->priv_data; | |
| 146 | + uint32_t pos, taglen, datalen; | |
| 147 | + struct AVDES av_des; | |
| 148 | + | |
| 149 | + if (!enc_header || !n_val) | |
| 150 | + return -1; | |
| 151 | + | |
| 152 | + pos = OMA_ENC_HEADER_SIZE + oc->k_size; | |
| 153 | + if (!memcmp(&enc_header[pos], "EKB ", 4)) | |
| 154 | + pos += 32; | |
| 155 | + | |
| 156 | + if (AV_RB32(&enc_header[pos]) != oc->rid) | |
| 157 | + av_log(s, AV_LOG_DEBUG, "Mismatching RID\n"); | |
| 158 | + | |
| 159 | + taglen = AV_RB32(&enc_header[pos+32]); | |
| 160 | + datalen = AV_RB32(&enc_header[pos+36]) >> 4; | |
| 161 | + | |
| 162 | + if(taglen + (((uint64_t)datalen)<<4) + 44 > size) | |
| 163 | + return -1; | |
| 164 | + | |
| 165 | + pos += 44 + taglen; | |
| 166 | + | |
| 167 | + av_des_init(&av_des, n_val, 192, 1); | |
| 168 | + while (datalen-- > 0) { | |
| 169 | + av_des_crypt(&av_des, oc->r_val, &enc_header[pos], 2, NULL, 1); | |
| 170 | + kset(s, oc->r_val, NULL, 16); | |
| 171 | + if (!rprobe(s, enc_header, oc->r_val)) | |
| 172 | + return 0; | |
| 173 | + pos += 16; | |
| 174 | + } | |
| 175 | + | |
| 176 | + return -1; | |
| 177 | +} | |
| 178 | + | |
| 179 | +static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header) | |
| 180 | +{ | |
| 181 | + OMAContext *oc = s->priv_data; | |
| 182 | + ID3v2ExtraMetaGEOB *geob = NULL; | |
| 183 | + uint8_t *gdata; | |
| 184 | + | |
| 185 | + oc->encrypted = 1; | |
| 186 | + av_log(s, AV_LOG_INFO, "File is encrypted\n"); | |
| 187 | + | |
| 188 | + /* find GEOB metadata */ | |
| 189 | + while (em) { | |
| 190 | + if (!strcmp(em->tag, "GEOB") && | |
| 191 | + (geob = em->data) && | |
| 192 | + (!strcmp(geob->description, "OMG_LSI") || | |
| 193 | + !strcmp(geob->description, "OMG_BKLSI"))) { | |
| 194 | + break; | |
| 195 | + } | |
| 196 | + em = em->next; | |
| 197 | + } | |
| 198 | + if (!em) { | |
| 199 | + av_log(s, AV_LOG_ERROR, "No encryption header found\n"); | |
| 200 | + return -1; | |
| 201 | + } | |
| 202 | + | |
| 203 | + if (geob->datasize < 64) { | |
| 204 | + av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\n", geob->datasize); | |
| 205 | + return -1; | |
| 206 | + } | |
| 207 | + | |
| 208 | + gdata = geob->data; | |
| 209 | + | |
| 210 | + if (AV_RB16(gdata) != 1) | |
| 211 | + av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n"); | |
| 212 | + | |
| 213 | + oc->k_size = AV_RB16(&gdata[2]); | |
| 214 | + oc->e_size = AV_RB16(&gdata[4]); | |
| 215 | + oc->i_size = AV_RB16(&gdata[6]); | |
| 216 | + oc->s_size = AV_RB16(&gdata[8]); | |
| 217 | + | |
| 218 | + if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING ", 12)) { | |
| 219 | + av_log(s, AV_LOG_ERROR, "Invalid encryption header\n"); | |
| 220 | + return -1; | |
| 221 | + } | |
| 222 | + oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]); | |
| 223 | + av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid); | |
| 224 | + | |
| 225 | + memcpy(oc->iv, &header[0x58], 8); | |
| 226 | + hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8); | |
| 227 | + | |
| 228 | + hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8); | |
| 229 | + | |
| 230 | + if (s->keylen > 0) { | |
| 231 | + kset(s, s->key, s->key, s->keylen); | |
| 232 | + } | |
| 233 | + if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) || | |
| 234 | + rprobe(s, gdata, oc->r_val) < 0 && | |
| 235 | + nprobe(s, gdata, geob->datasize, oc->n_val) < 0) { | |
| 236 | + int i; | |
| 237 | + for (i = 0; i < sizeof(leaf_table); i += 2) { | |
| 238 | + uint8_t buf[16]; | |
| 239 | + AV_WL64(buf, leaf_table[i]); | |
| 240 | + AV_WL64(&buf[8], leaf_table[i+1]); | |
| 241 | + kset(s, buf, buf, 16); | |
| 242 | + if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, geob->datasize, oc->n_val)) | |
| 243 | + break; | |
| 244 | + } | |
| 245 | + if (i >= sizeof(leaf_table)) { | |
| 246 | + av_log(s, AV_LOG_ERROR, "Invalid key\n"); | |
| 247 | + return -1; | |
| 248 | + } | |
| 249 | + } | |
| 250 | + | |
| 251 | + /* e_val */ | |
| 252 | + av_des_init(&oc->av_des, oc->m_val, 64, 0); | |
| 253 | + av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0); | |
| 254 | + hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8); | |
| 255 | + | |
| 256 | + /* init e_val */ | |
| 257 | + av_des_init(&oc->av_des, oc->e_val, 64, 1); | |
| 258 | + | |
| 259 | + return 0; | |
| 260 | +} | |
| 261 | + | |
| 262 | +static int oma_read_header(AVFormatContext *s, | |
| 263 | + AVFormatParameters *ap) | |
| 264 | +{ | |
| 265 | + int ret, framesize, jsflag, samplerate; | |
| 266 | + uint32_t codec_params; | |
| 267 | + int16_t eid; | |
| 268 | + uint8_t buf[EA3_HEADER_SIZE]; | |
| 269 | + uint8_t *edata; | |
| 270 | + AVStream *st; | |
| 271 | + ID3v2ExtraMeta *extra_meta = NULL; | |
| 272 | + OMAContext *oc = s->priv_data; | |
| 273 | + | |
| 274 | + ff_id3v2_read_all(s, ID3v2_EA3_MAGIC, &extra_meta); | |
| 275 | + ret = avio_read(s->pb, buf, EA3_HEADER_SIZE); | |
| 276 | + if (ret < EA3_HEADER_SIZE) | |
| 277 | + return -1; | |
| 278 | + | |
| 279 | + if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) { | |
| 280 | + av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n"); | |
| 281 | + return -1; | |
| 282 | + } | |
| 283 | + | |
| 284 | + oc->content_start = avio_tell(s->pb); | |
| 285 | + | |
| 286 | + /* encrypted file */ | |
| 287 | + eid = AV_RB16(&buf[6]); | |
| 288 | + if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) { | |
| 289 | + ff_id3v2_free_extra_meta(&extra_meta); | |
| 290 | + return -1; | |
| 291 | + } | |
| 292 | + | |
| 293 | + ff_id3v2_free_extra_meta(&extra_meta); | |
| 294 | + | |
| 295 | + codec_params = AV_RB24(&buf[33]); | |
| 296 | + | |
| 297 | + st = avformat_new_stream(s, NULL); | |
| 298 | + if (!st) | |
| 299 | + return AVERROR(ENOMEM); | |
| 300 | + | |
| 301 | + st->start_time = 0; | |
| 302 | + st->codec->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 303 | + st->codec->codec_tag = buf[32]; | |
| 304 | + st->codec->codec_id = ff_codec_get_id(ff_oma_codec_tags, st->codec->codec_tag); | |
| 305 | + | |
| 306 | + switch (buf[32]) { | |
| 307 | + case OMA_CODECID_ATRAC3: | |
| 308 | + samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100; | |
| 309 | + if (samplerate != 44100) | |
| 310 | + av_log_ask_for_sample(s, "Unsupported sample rate: %d\n", | |
| 311 | + samplerate); | |
| 312 | + | |
| 313 | + framesize = (codec_params & 0x3FF) * 8; | |
| 314 | + jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */ | |
| 315 | + st->codec->channels = 2; | |
| 316 | + st->codec->sample_rate = samplerate; | |
| 317 | + st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024; | |
| 318 | + | |
| 319 | + /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */ | |
| 320 | + st->codec->extradata_size = 14; | |
| 321 | + edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 322 | + if (!edata) | |
| 323 | + return AVERROR(ENOMEM); | |
| 324 | + | |
| 325 | + st->codec->extradata = edata; | |
| 326 | + AV_WL16(&edata[0], 1); // always 1 | |
| 327 | + AV_WL32(&edata[2], samplerate); // samples rate | |
| 328 | + AV_WL16(&edata[6], jsflag); // coding mode | |
| 329 | + AV_WL16(&edata[8], jsflag); // coding mode | |
| 330 | + AV_WL16(&edata[10], 1); // always 1 | |
| 331 | + // AV_WL16(&edata[12], 0); // always 0 | |
| 332 | + | |
| 333 | + avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
| 334 | + break; | |
| 335 | + case OMA_CODECID_ATRAC3P: | |
| 336 | + st->codec->channels = (codec_params >> 10) & 7; | |
| 337 | + framesize = ((codec_params & 0x3FF) * 8) + 8; | |
| 338 | + st->codec->sample_rate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100; | |
| 339 | + st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024; | |
| 340 | + avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
| 341 | + av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n"); | |
| 342 | + break; | |
| 343 | + case OMA_CODECID_MP3: | |
| 344 | + st->need_parsing = AVSTREAM_PARSE_FULL; | |
| 345 | + framesize = 1024; | |
| 346 | + break; | |
| 347 | + case OMA_CODECID_LPCM: | |
| 348 | + /* PCM 44.1 kHz 16 bit stereo big-endian */ | |
| 349 | + st->codec->channels = 2; | |
| 350 | + st->codec->sample_rate = 44100; | |
| 351 | + framesize = 1024; | |
| 352 | + /* bit rate = sample rate x PCM block align (= 4) x 8 */ | |
| 353 | + st->codec->bit_rate = st->codec->sample_rate * 32; | |
| 354 | + st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id); | |
| 355 | + avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
| 356 | + break; | |
| 357 | + default: | |
| 358 | + av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]); | |
| 359 | + return -1; | |
| 360 | + } | |
| 361 | + | |
| 362 | + st->codec->block_align = framesize; | |
| 363 | + | |
| 364 | + return 0; | |
| 365 | +} | |
| 366 | + | |
| 367 | + | |
| 368 | +static int oma_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 369 | +{ | |
| 370 | + OMAContext *oc = s->priv_data; | |
| 371 | + int packet_size = s->streams[0]->codec->block_align; | |
| 372 | + int ret = av_get_packet(s->pb, pkt, packet_size); | |
| 373 | + | |
| 374 | + if (ret <= 0) | |
| 375 | + return AVERROR(EIO); | |
| 376 | + | |
| 377 | + pkt->stream_index = 0; | |
| 378 | + | |
| 379 | + if (oc->encrypted) { | |
| 380 | + /* previous unencrypted block saved in IV for the next packet (CBC mode) */ | |
| 381 | + av_des_crypt(&oc->av_des, pkt->data, pkt->data, (packet_size >> 3), oc->iv, 1); | |
| 382 | + } | |
| 383 | + | |
| 384 | + return ret; | |
| 385 | +} | |
| 386 | + | |
| 387 | +static int oma_read_probe(AVProbeData *p) | |
| 388 | +{ | |
| 389 | + const uint8_t *buf; | |
| 390 | + unsigned tag_len = 0; | |
| 391 | + | |
| 392 | + buf = p->buf; | |
| 393 | + | |
| 394 | + if (p->buf_size < ID3v2_HEADER_SIZE || | |
| 395 | + !ff_id3v2_match(buf, ID3v2_EA3_MAGIC) || | |
| 396 | + buf[3] != 3 || // version must be 3 | |
| 397 | + buf[4]) // flags byte zero | |
| 398 | + return 0; | |
| 399 | + | |
| 400 | + tag_len = ff_id3v2_tag_len(buf); | |
| 401 | + | |
| 402 | + /* This check cannot overflow as tag_len has at most 28 bits */ | |
| 403 | + if (p->buf_size < tag_len + 5) | |
| 404 | + /* EA3 header comes late, might be outside of the probe buffer */ | |
| 405 | + return AVPROBE_SCORE_MAX / 2; | |
| 406 | + | |
| 407 | + buf += tag_len; | |
| 408 | + | |
| 409 | + if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE) | |
| 410 | + return AVPROBE_SCORE_MAX; | |
| 411 | + else | |
| 412 | + return 0; | |
| 413 | +} | |
| 414 | + | |
| 415 | +static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags) | |
| 416 | +{ | |
| 417 | + OMAContext *oc = s->priv_data; | |
| 418 | + | |
| 419 | + pcm_read_seek(s, stream_index, timestamp, flags); | |
| 420 | + | |
| 421 | + if (oc->encrypted) { | |
| 422 | + /* readjust IV for CBC */ | |
| 423 | + int64_t pos = avio_tell(s->pb); | |
| 424 | + if (pos < oc->content_start) | |
| 425 | + memset(oc->iv, 0, 8); | |
| 426 | + else { | |
| 427 | + if (avio_seek(s->pb, -8, SEEK_CUR) < 0 || avio_read(s->pb, oc->iv, 8) < 8) { | |
| 428 | + memset(oc->iv, 0, 8); | |
| 429 | + return -1; | |
| 430 | + } | |
| 431 | + } | |
| 432 | + } | |
| 433 | + | |
| 434 | + return 0; | |
| 435 | +} | |
| 436 | + | |
| 437 | +AVInputFormat ff_oma_demuxer = { | |
| 438 | + .name = "oma", | |
| 439 | + .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"), | |
| 440 | + .priv_data_size = sizeof(OMAContext), | |
| 441 | + .read_probe = oma_read_probe, | |
| 442 | + .read_header = oma_read_header, | |
| 443 | + .read_packet = oma_read_packet, | |
| 444 | + .read_seek = oma_read_seek, | |
| 445 | + .flags = AVFMT_GENERIC_INDEX, | |
| 446 | + .extensions = "oma,omg,aa3", | |
| 447 | + .codec_tag = (const AVCodecTag* const []){ff_oma_codec_tags, 0}, | |
| 448 | +}; | |
| 449 | + | |
| @@ -0,0 +1,105 @@ | ||
| 1 | +/* | |
| 2 | + * Sony OpenMG (OMA) muxer | |
| 3 | + * | |
| 4 | + * Copyright (c) 2011 Michael Karcher | |
| 5 | + * | |
| 6 | + * This file is part of Libav. | |
| 7 | + * | |
| 8 | + * Libav is free software; you can redistribute it and/or | |
| 9 | + * modify it under the terms of the GNU Lesser General Public | |
| 10 | + * License as published by the Free Software Foundation; either | |
| 11 | + * version 2.1 of the License, or (at your option) any later version. | |
| 12 | + * | |
| 13 | + * Libav is distributed in the hope that it will be useful, | |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 16 | + * Lesser General Public License for more details. | |
| 17 | + * | |
| 18 | + * You should have received a copy of the GNU Lesser General Public | |
| 19 | + * License along with Libav; if not, write to the Free Software | |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 21 | + */ | |
| 22 | + | |
| 23 | +#include "avformat.h" | |
| 24 | +#include "avio_internal.h" | |
| 25 | +#include "id3v2.h" | |
| 26 | +#include "internal.h" | |
| 27 | +#include "oma.h" | |
| 28 | +#include "rawenc.h" | |
| 29 | + | |
| 30 | +static av_cold int oma_write_header(AVFormatContext *s) | |
| 31 | +{ | |
| 32 | + int i; | |
| 33 | + AVCodecContext *format; | |
| 34 | + int srate_index; | |
| 35 | + int isjointstereo; | |
| 36 | + | |
| 37 | + format = s->streams[0]->codec; | |
| 38 | + /* check for support of the format first */ | |
| 39 | + | |
| 40 | + for (srate_index = 0; ; srate_index++) { | |
| 41 | + if (ff_oma_srate_tab[srate_index] == 0) { | |
| 42 | + av_log(s, AV_LOG_ERROR, "Sample rate %d not supported in OpenMG audio\n", | |
| 43 | + format->sample_rate); | |
| 44 | + return AVERROR(EINVAL); | |
| 45 | + } | |
| 46 | + | |
| 47 | + if (ff_oma_srate_tab[srate_index] * 100 == format->sample_rate) | |
| 48 | + break; | |
| 49 | + } | |
| 50 | + | |
| 51 | + /* Metadata; OpenMG does not support ID3v2.4 */ | |
| 52 | + ff_id3v2_write(s, 3, ID3v2_EA3_MAGIC); | |
| 53 | + | |
| 54 | + ffio_wfourcc(s->pb, "EA3\0"); | |
| 55 | + avio_w8(s->pb, EA3_HEADER_SIZE >> 7); | |
| 56 | + avio_w8(s->pb, EA3_HEADER_SIZE & 0x7F); | |
| 57 | + avio_wl16(s->pb, 0xFFFF); /* not encrypted */ | |
| 58 | + for (i = 0; i < 6; i++) | |
| 59 | + avio_wl32(s->pb, 0); /* Padding + DRM id */ | |
| 60 | + | |
| 61 | + switch(format->codec_tag) { | |
| 62 | + case OMA_CODECID_ATRAC3: | |
| 63 | + if (format->channels != 2) { | |
| 64 | + av_log(s, AV_LOG_ERROR, "ATRAC3 in OMA is only supported with 2 channels"); | |
| 65 | + return AVERROR(EINVAL); | |
| 66 | + } | |
| 67 | + if (format->extradata_size == 14) /* WAV format extradata */ | |
| 68 | + isjointstereo = format->extradata[6] != 0; | |
| 69 | + else if(format->extradata_size == 10) /* RM format extradata */ | |
| 70 | + isjointstereo = format->extradata[8] == 0x12; | |
| 71 | + else { | |
| 72 | + av_log(s, AV_LOG_ERROR, "ATRAC3: Unsupported extradata size\n"); | |
| 73 | + return AVERROR(EINVAL); | |
| 74 | + } | |
| 75 | + avio_wb32(s->pb, (OMA_CODECID_ATRAC3 << 24) | | |
| 76 | + (isjointstereo << 17) | | |
| 77 | + (srate_index << 13) | | |
| 78 | + (format->block_align/8)); | |
| 79 | + break; | |
| 80 | + case OMA_CODECID_ATRAC3P: | |
| 81 | + avio_wb32(s->pb, (OMA_CODECID_ATRAC3P << 24) | | |
| 82 | + (srate_index << 13) | | |
| 83 | + (format->channels << 10) | | |
| 84 | + (format->block_align/8 - 1)); | |
| 85 | + break; | |
| 86 | + default: | |
| 87 | + av_log(s, AV_LOG_ERROR, "OMA: unsupported codec tag %d for write\n", | |
| 88 | + format->codec_tag); | |
| 89 | + } | |
| 90 | + for (i = 0; i < (EA3_HEADER_SIZE - 36)/4; i++) | |
| 91 | + avio_wl32(s->pb, 0); /* Padding */ | |
| 92 | + | |
| 93 | + return 0; | |
| 94 | +} | |
| 95 | + | |
| 96 | +AVOutputFormat ff_oma_muxer = { | |
| 97 | + .name = "oma", | |
| 98 | + .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"), | |
| 99 | + .mime_type = "audio/x-oma", | |
| 100 | + .extensions = "oma", | |
| 101 | + .audio_codec = CODEC_ID_ATRAC3, | |
| 102 | + .write_header = oma_write_header, | |
| 103 | + .write_packet = ff_raw_write_packet, | |
| 104 | + .codec_tag = (const AVCodecTag* const []){ff_oma_codec_tags, 0}, | |
| 105 | +}; | |
| @@ -2393,6 +2393,11 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) · 2392 unmodified lines | ||
| 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) · 1 unmodified line | ||
| 2400 | 2405 | |
| 2401 | 2406 | //try to just open decoders, in case this is enough to get parameters |
| 2402 | 2407 | 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 | − } | |
| 2408 | + if (codec && !st->codec->codec) | |
| 2409 | + avcodec_open2(st->codec, codec, options ? &options[i] : NULL); | |
| 2412 | 2410 | } |
| 2413 | 2411 | } |
| 2414 | 2412 | |
| @@ -30,7 +30,7 @@ · 29 unmodified lines | ||
| 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, \ |