]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/flvdec.c
Use AV_METADATA_DONT_STRDUP* / use av_malloced metadata instead of strduped
[ffmpeg] / libavformat / flvdec.c
index 89ece0fa88244934473de07ce85504cc842944ef..c6428b4bbc91f4d03f55a72620608ba074326d72 100644 (file)
@@ -24,6 +24,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/avstring.h"
+#include "libavcodec/bytestream.h"
 #include "libavcodec/mpeg4audio.h"
 #include "avformat.h"
 #include "flv.h"
@@ -37,7 +39,7 @@ static int flv_probe(AVProbeData *p)
     const uint8_t *d;
 
     d = p->buf;
-    if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0) {
+    if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
         return AVPROBE_SCORE_MAX;
     }
     return 0;
@@ -49,7 +51,7 @@ static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_c
         //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_S8 :
-#ifdef WORDS_BIGENDIAN
+#if HAVE_BIGENDIAN
                                 CODEC_ID_PCM_S16BE;
 #else
                                 CODEC_ID_PCM_S16LE;
@@ -80,6 +82,7 @@ static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_co
     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_VP6A  :
             if(flv_codecid == FLV_CODECID_VP6A)
@@ -101,25 +104,25 @@ static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_co
     return 0;
 }
 
-static int amf_get_string(ByteIOContext *ioc, char *buffer, int buffsize) {
-    int length = get_be16(ioc);
-    if(length >= buffsize) {
-        url_fskip(ioc, length);
-        return -1;
-    }
+static int amf_get_string(ByteIOContext *ioc, char **buf)
+{
+    uint16_t len = get_be16(ioc);
 
-    get_buffer(ioc, buffer, length);
+    *buf = av_malloc(len+1);
+    if (!*buf)
+        return AVERROR_NOMEM;
 
-    buffer[length] = '\0';
+    get_buffer(ioc, *buf, len);
+    (*buf)[len] = '\0';
 
-    return length;
+    return len;
 }
 
 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
     AVCodecContext *acodec, *vcodec;
     ByteIOContext *ioc;
     AMFDataType amf_type;
-    char str_val[256];
+    char *str = NULL;
     double num_val;
 
     num_val = 0;
@@ -133,7 +136,7 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vst
         case AMF_DATA_TYPE_BOOL:
             num_val = get_byte(ioc); break;
         case AMF_DATA_TYPE_STRING:
-            if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
+            if(amf_get_string(ioc, &str) < 0)
                 return -1;
             break;
         case AMF_DATA_TYPE_OBJECT: {
@@ -154,10 +157,11 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vst
             break; //these take up no additional space
         case AMF_DATA_TYPE_MIXEDARRAY:
             url_fskip(ioc, 4); //skip 32-bit max array index
-            while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
+            while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, &str) > 0) {
                 //this is the only case in which we would want a nested parse to not skip over the object
-                if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
+                if(amf_parse_object(s, astream, vstream, str, max_pos, depth + 1) < 0)
                     return -1;
+                av_freep(&str);
             }
             if(get_byte(ioc) != AMF_END_OF_OBJECT)
                 return -1;
@@ -184,36 +188,15 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vst
         vcodec = vstream ? vstream->codec : NULL;
 
         if(amf_type == AMF_DATA_TYPE_BOOL) {
-            if(!strcmp(key, "stereo") && acodec) acodec->channels = num_val > 0 ? 2 : 1;
+            av_metadata_set2(&s->metadata, key, av_d2str(num_val), AV_METADATA_DONT_STRDUP_VAL);
         } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
+            av_metadata_set2(&s->metadata, key, av_d2str(num_val), AV_METADATA_DONT_STRDUP_VAL);
             if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
-//            else if(!strcmp(key, "width")  && vcodec && num_val > 0) vcodec->width  = num_val;
-//            else if(!strcmp(key, "height") && vcodec && num_val > 0) vcodec->height = num_val;
             else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
                 vcodec->bit_rate = num_val * 1024.0;
-            else if(!strcmp(key, "audiocodecid") && acodec && 0 <= (int)num_val)
-                flv_set_audio_codec(s, astream, (int)num_val << FLV_AUDIO_CODECID_OFFSET);
-            else if(!strcmp(key, "videocodecid") && vcodec && 0 <= (int)num_val)
-                flv_set_video_codec(s, vstream, (int)num_val);
-            else if(!strcmp(key, "audiosamplesize") && acodec && 0 < (int)num_val) {
-                acodec->bits_per_coded_sample = num_val;
-                //we may have to rewrite a previously read codecid because FLV only marks PCM endianness.
-                if(num_val == 8 && (acodec->codec_id == CODEC_ID_PCM_S16BE || acodec->codec_id == CODEC_ID_PCM_S16LE))
-                    acodec->codec_id = CODEC_ID_PCM_S8;
-            }
-            else if(!strcmp(key, "audiosamplerate") && acodec && num_val >= 0) {
-                //some tools, like FLVTool2, write consistently approximate metadata sample rates
-                if (!acodec->sample_rate) {
-                    switch((int)num_val) {
-                        case 44000: acodec->sample_rate = 44100  ; break;
-                        case 22000: acodec->sample_rate = 22050  ; break;
-                        case 11000: acodec->sample_rate = 11025  ; break;
-                        case 5000 : acodec->sample_rate = 5512   ; break;
-                        default   : acodec->sample_rate = num_val;
-                    }
-                }
-            }
-        }
+        } else if (amf_type == AMF_DATA_TYPE_STRING)
+            av_metadata_set2(&s->metadata, key, str,
+                                   AV_METADATA_DONT_STRDUP_VAL);
     }
 
     return 0;
@@ -223,18 +206,20 @@ static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
     AMFDataType type;
     AVStream *stream, *astream, *vstream;
     ByteIOContext *ioc;
-    int i, keylen;
-    char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
+    int i;
+    char *buf = NULL; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
 
     astream = NULL;
     vstream = NULL;
-    keylen = 0;
     ioc = s->pb;
 
     //first object needs to be "onMetaData" string
     type = get_byte(ioc);
-    if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
+    if (type != AMF_DATA_TYPE_STRING ||
+        amf_get_string(ioc, &buf) < 0 || strcmp(buf, "onMetaData")) {
+        av_freep(&buf);
         return -1;
+    }
 
     //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
     for(i = 0; i < s->nb_streams; i++) {
@@ -244,8 +229,9 @@ static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
     }
 
     //parse the second object (we want a mixed array)
-    if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
+    if(amf_parse_object(s, astream, vstream, buf, next_pos, 0) < 0)
         return -1;
+    av_freep(&buf);
 
     return 0;
 }
@@ -345,7 +331,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
         if (type == FLV_TAG_TYPE_META && size > 13+1+4)
             flv_read_metabody(s, next);
         else /* skip packet */
-            av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
+            av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
     skip:
         url_fseek(s->pb, next, SEEK_SET);
         continue;
@@ -388,7 +374,9 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
         size= get_be32(s->pb);
         url_fseek(s->pb, fsize-3-size, SEEK_SET);
         if(size == get_be24(s->pb) + 11){
-            s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000;
+            uint32_t ts = get_be24(s->pb);
+            ts |= get_byte(s->pb) << 24;
+            s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
         }
         url_fseek(s->pb, pos, SEEK_SET);
     }
@@ -427,9 +415,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
                 MPEG4AudioConfig cfg;
                 ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
                                          st->codec->extradata_size);
-                if (cfg.chan_config > 7)
-                    return -1;
-                st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
+                st->codec->channels = cfg.channels;
                 st->codec->sample_rate = cfg.sample_rate;
                 dprintf(s, "mp4a config channels %d sample rate %d\n",
                         st->codec->channels, st->codec->sample_rate);
@@ -439,8 +425,12 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
     }
 
+    /* skip empty data packets */
+    if (!size)
+        return AVERROR(EAGAIN);
+
     ret= av_get_packet(s->pb, pkt, size);
-    if (ret <= 0) {
+    if (ret < 0) {
         return AVERROR(EIO);
     }
     /* note: we need to modify the packet size here to handle the last