4xm: refactor fourxm_read_header
[ffmpeg.git] / libavformat / flvdec.c
index 602cb9e..2f68653 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/channel_layout.h"
 #include "libavutil/dict.h"
 #include "libavutil/opt.h"
 #include "libavutil/intfloat.h"
@@ -69,12 +70,11 @@ static int flv_probe(AVProbeData *p)
     return 0;
 }
 
-static AVStream *create_stream(AVFormatContext *s, int tag, int codec_type)
+static AVStream *create_stream(AVFormatContext *s, int codec_type)
 {
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return NULL;
-    st->id = tag;
     st->codec->codec_type = codec_type;
     avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
     return st;
@@ -94,38 +94,34 @@ static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
     switch(flv_codecid) {
         //no distinction between S16 and S8 PCM codec flags
     case FLV_CODECID_PCM:
-        codec_id = bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
+        codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
 #if HAVE_BIGENDIAN
-                            CODEC_ID_PCM_S16BE;
+                            AV_CODEC_ID_PCM_S16BE;
 #else
-                            CODEC_ID_PCM_S16LE;
+                            AV_CODEC_ID_PCM_S16LE;
 #endif
         return codec_id == acodec->codec_id;
     case FLV_CODECID_PCM_LE:
-        codec_id = bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE;
+        codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE;
         return codec_id == acodec->codec_id;
     case FLV_CODECID_AAC:
-        return acodec->codec_id == CODEC_ID_AAC;
+        return acodec->codec_id == AV_CODEC_ID_AAC;
     case FLV_CODECID_ADPCM:
-        return acodec->codec_id == CODEC_ID_ADPCM_SWF;
+        return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
     case FLV_CODECID_SPEEX:
-        return acodec->codec_id == CODEC_ID_SPEEX;
+        return acodec->codec_id == AV_CODEC_ID_SPEEX;
     case FLV_CODECID_MP3:
-        return acodec->codec_id == CODEC_ID_MP3;
+        return acodec->codec_id == AV_CODEC_ID_MP3;
     case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
-        return acodec->sample_rate == 8000 &&
-               acodec->codec_id == CODEC_ID_NELLYMOSER;
     case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
-        return acodec->sample_rate == 16000 &&
-               acodec->codec_id == CODEC_ID_NELLYMOSER;
     case FLV_CODECID_NELLYMOSER:
-        return acodec->codec_id == CODEC_ID_NELLYMOSER;
+        return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
     case FLV_CODECID_PCM_MULAW:
         return acodec->sample_rate == 8000 &&
-               acodec->codec_id == CODEC_ID_PCM_MULAW;
+               acodec->codec_id == AV_CODEC_ID_PCM_MULAW;
     case FLV_CODECID_PCM_ALAW:
         return acodec->sample_rate = 8000 &&
-               acodec->codec_id == CODEC_ID_PCM_ALAW;
+               acodec->codec_id == AV_CODEC_ID_PCM_ALAW;
     default:
         return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
     }
@@ -137,40 +133,40 @@ static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecCo
     switch(flv_codecid) {
         //no distinction between S16 and S8 PCM codec flags
         case FLV_CODECID_PCM:
-            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
+            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
 #if HAVE_BIGENDIAN
-                                CODEC_ID_PCM_S16BE;
+                                AV_CODEC_ID_PCM_S16BE;
 #else
-                                CODEC_ID_PCM_S16LE;
+                                AV_CODEC_ID_PCM_S16LE;
 #endif
             break;
         case FLV_CODECID_PCM_LE:
-            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
-        case FLV_CODECID_AAC  : acodec->codec_id = CODEC_ID_AAC;                                    break;
-        case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF;                              break;
+            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE; break;
+        case FLV_CODECID_AAC  : acodec->codec_id = AV_CODEC_ID_AAC;                                    break;
+        case FLV_CODECID_ADPCM: acodec->codec_id = AV_CODEC_ID_ADPCM_SWF;                              break;
         case FLV_CODECID_SPEEX:
-            acodec->codec_id = CODEC_ID_SPEEX;
+            acodec->codec_id = AV_CODEC_ID_SPEEX;
             acodec->sample_rate = 16000;
             break;
-        case FLV_CODECID_MP3  : acodec->codec_id = CODEC_ID_MP3      ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
+        case FLV_CODECID_MP3  : acodec->codec_id = AV_CODEC_ID_MP3      ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
         case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
             acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
-            acodec->codec_id = CODEC_ID_NELLYMOSER;
+            acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
             break;
         case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
             acodec->sample_rate = 16000;
-            acodec->codec_id = CODEC_ID_NELLYMOSER;
+            acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
             break;
         case FLV_CODECID_NELLYMOSER:
-            acodec->codec_id = CODEC_ID_NELLYMOSER;
+            acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
             break;
         case FLV_CODECID_PCM_MULAW:
             acodec->sample_rate = 8000;
-            acodec->codec_id = CODEC_ID_PCM_MULAW;
+            acodec->codec_id = AV_CODEC_ID_PCM_MULAW;
             break;
         case FLV_CODECID_PCM_ALAW:
             acodec->sample_rate = 8000;
-            acodec->codec_id = CODEC_ID_PCM_ALAW;
+            acodec->codec_id = AV_CODEC_ID_PCM_ALAW;
             break;
         default:
             av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
@@ -187,17 +183,17 @@ static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
 
     switch (flv_codecid) {
         case FLV_CODECID_H263:
-            return vcodec->codec_id == CODEC_ID_FLV1;
+            return vcodec->codec_id == AV_CODEC_ID_FLV1;
         case FLV_CODECID_SCREEN:
-            return vcodec->codec_id == CODEC_ID_FLASHSV;
+            return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
         case FLV_CODECID_SCREEN2:
-            return vcodec->codec_id == CODEC_ID_FLASHSV2;
+            return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
         case FLV_CODECID_VP6:
-            return vcodec->codec_id == CODEC_ID_VP6F;
+            return vcodec->codec_id == AV_CODEC_ID_VP6F;
         case FLV_CODECID_VP6A:
-            return vcodec->codec_id == CODEC_ID_VP6A;
+            return vcodec->codec_id == AV_CODEC_ID_VP6A;
         case FLV_CODECID_H264:
-            return vcodec->codec_id == CODEC_ID_H264;
+            return vcodec->codec_id == AV_CODEC_ID_H264;
         default:
             return vcodec->codec_tag == flv_codecid;
     }
@@ -205,24 +201,30 @@ static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
     return 0;
 }
 
-static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
+static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid, int read) {
     AVCodecContext *vcodec = vstream->codec;
     switch(flv_codecid) {
-        case FLV_CODECID_H263  : vcodec->codec_id = CODEC_ID_FLV1   ; break;
-        case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
-        case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
-        case FLV_CODECID_VP6   : vcodec->codec_id = CODEC_ID_VP6F   ;
+        case FLV_CODECID_H263  : vcodec->codec_id = AV_CODEC_ID_FLV1   ; break;
+        case FLV_CODECID_SCREEN: vcodec->codec_id = AV_CODEC_ID_FLASHSV; break;
+        case FLV_CODECID_SCREEN2: vcodec->codec_id = AV_CODEC_ID_FLASHSV2; break;
+        case FLV_CODECID_VP6   : vcodec->codec_id = AV_CODEC_ID_VP6F   ;
         case FLV_CODECID_VP6A  :
             if(flv_codecid == FLV_CODECID_VP6A)
-                vcodec->codec_id = CODEC_ID_VP6A;
-            if(vcodec->extradata_size != 1) {
-                vcodec->extradata_size = 1;
-                vcodec->extradata = av_malloc(1);
+                vcodec->codec_id = AV_CODEC_ID_VP6A;
+            if (read) {
+                if (vcodec->extradata_size != 1) {
+                    vcodec->extradata = av_malloc(1);
+                    if (vcodec->extradata)
+                        vcodec->extradata_size = 1;
+                }
+                if (vcodec->extradata)
+                    vcodec->extradata[0] = avio_r8(s->pb);
+                else
+                    avio_skip(s->pb, 1);
             }
-            vcodec->extradata[0] = avio_r8(s->pb);
             return 1; // 1 byte body size adjustment for flv_read_packet()
         case FLV_CODECID_H264:
-            vcodec->codec_id = CODEC_ID_H264;
+            vcodec->codec_id = AV_CODEC_ID_H264;
             return 3; // not 4, reading packet type will consume one byte
         default:
             av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
@@ -398,7 +400,7 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vst
         acodec = astream ? astream->codec : NULL;
         vcodec = vstream ? vstream->codec : NULL;
 
-        if (amf_type == AMF_DATA_TYPE_NUMBER) {
+        if (amf_type == AMF_DATA_TYPE_NUMBER || amf_type == AMF_DATA_TYPE_BOOL) {
             if (!strcmp(key, "duration"))
                 s->duration = num_val * AV_TIME_BASE;
             else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
@@ -406,19 +408,27 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vst
             else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
                 acodec->bit_rate = num_val * 1024.0;
             else if (!strcmp(key, "datastream")) {
-                AVStream *st = create_stream(s, 2, AVMEDIA_TYPE_DATA);
+                AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
                 if (!st)
                     return AVERROR(ENOMEM);
-                st->codec->codec_id = CODEC_ID_TEXT;
+                st->codec->codec_id = AV_CODEC_ID_TEXT;
             } else if (flv->trust_metadata) {
                 if (!strcmp(key, "videocodecid") && vcodec) {
-                    flv_set_video_codec(s, vstream, num_val);
+                    flv_set_video_codec(s, vstream, num_val, 0);
                 } else
                 if (!strcmp(key, "audiocodecid") && acodec) {
-                    flv_set_audio_codec(s, astream, acodec, num_val);
+                    int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
+                    flv_set_audio_codec(s, astream, acodec, id);
                 } else
                 if (!strcmp(key, "audiosamplerate") && acodec) {
                     acodec->sample_rate = num_val;
+                } else if (!strcmp(key, "audiosamplesize") && acodec) {
+                    acodec->bits_per_coded_sample = num_val;
+                } else if (!strcmp(key, "stereo") && acodec) {
+                    acodec->channels = num_val + 1;
+                    acodec->channel_layout = acodec->channels == 2 ?
+                                             AV_CH_LAYOUT_STEREO :
+                                             AV_CH_LAYOUT_MONO;
                 } else
                 if (!strcmp(key, "width") && vcodec) {
                     vcodec->width = num_val;
@@ -506,16 +516,14 @@ static int flv_read_header(AVFormatContext *s)
         av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
     }
 
-    if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
-             != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
-        s->ctx_flags |= AVFMTCTX_NOHEADER;
+    s->ctx_flags |= AVFMTCTX_NOHEADER;
 
     if(flags & FLV_HEADER_FLAG_HASVIDEO){
-        if(!create_stream(s, 0, AVMEDIA_TYPE_VIDEO))
+        if(!create_stream(s, AVMEDIA_TYPE_VIDEO))
             return AVERROR(ENOMEM);
     }
     if(flags & FLV_HEADER_FLAG_HASAUDIO){
-        if(!create_stream(s, 1, AVMEDIA_TYPE_AUDIO))
+        if(!create_stream(s, AVMEDIA_TYPE_AUDIO))
             return AVERROR(ENOMEM);
     }
 
@@ -616,10 +624,10 @@ static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
     }
 
     if (i == s->nb_streams) {
-        st = create_stream(s, 2, AVMEDIA_TYPE_DATA);
+        st = create_stream(s, AVMEDIA_TYPE_DATA);
         if (!st)
             goto out;
-        st->codec->codec_id = CODEC_ID_TEXT;
+        st->codec->codec_id = AV_CODEC_ID_TEXT;
     }
 
     pkt->dts  = dts;
@@ -717,9 +725,8 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
     }
     if(i == s->nb_streams){
-        st = create_stream(s, is_audio,
-             is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO);
-        s->ctx_flags &= ~AVFMTCTX_NOHEADER;
+        st = create_stream(s,
+            is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO);
     }
     av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
     if(  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||         is_audio))
@@ -757,6 +764,8 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
         bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
         if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
             st->codec->channels              = channels;
+            st->codec->channel_layout        = channels == 1 ? AV_CH_LAYOUT_MONO :
+                                                               AV_CH_LAYOUT_STEREO;
             st->codec->sample_rate           = sample_rate;
             st->codec->bits_per_coded_sample = bits_per_coded_sample;
         }
@@ -771,14 +780,14 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
             sample_rate = ctx.sample_rate;
         }
     }else{
-        size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
+        size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
     }
 
-    if (st->codec->codec_id == CODEC_ID_AAC ||
-        st->codec->codec_id == CODEC_ID_H264) {
+    if (st->codec->codec_id == AV_CODEC_ID_AAC ||
+        st->codec->codec_id == AV_CODEC_ID_H264) {
         int type = avio_r8(s->pb);
         size--;
-        if (st->codec->codec_id == CODEC_ID_H264) {
+        if (st->codec->codec_id == AV_CODEC_ID_H264) {
             int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
             pts = dts + cts;
             if (cts < 0) { // dts are wrong
@@ -797,11 +806,12 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
             }
             if ((ret = flv_get_extradata(s, st, size)) < 0)
                 return ret;
-            if (st->codec->codec_id == CODEC_ID_AAC) {
+            if (st->codec->codec_id == AV_CODEC_ID_AAC) {
                 MPEG4AudioConfig cfg;
                 avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
                                              st->codec->extradata_size * 8, 1);
                 st->codec->channels = cfg.channels;
+                st->codec->channel_layout = 0;
                 if (cfg.ext_sample_rate)
                     st->codec->sample_rate = cfg.ext_sample_rate;
                 else
@@ -867,7 +877,7 @@ static int flv_read_seek(AVFormatContext *s, int stream_index,
 #define OFFSET(x) offsetof(FLVContext, x)
 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
-    { "flv_metadata", "Allocate streams according the onMetaData array",      OFFSET(trust_metadata), AV_OPT_TYPE_INT,    { 0 }, 0, 1, VD},
+    { "flv_metadata", "Allocate streams according the onMetaData array",      OFFSET(trust_metadata), AV_OPT_TYPE_INT,    { .i64 = 0 }, 0, 1, VD},
     { NULL }
 };
 
@@ -880,7 +890,7 @@ static const AVClass class = {
 
 AVInputFormat ff_flv_demuxer = {
     .name           = "flv",
-    .long_name      = NULL_IF_CONFIG_SMALL("FLV format"),
+    .long_name      = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
     .priv_data_size = sizeof(FLVContext),
     .read_probe     = flv_probe,
     .read_header    = flv_read_header,