]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/flvdec.c
Fix memleak caused by the fact that url_open_buf() allocates a context
[ffmpeg] / libavformat / flvdec.c
index 58861bb2345361b4c87834206e97c0711d294f91..29925c342cba1aa2552cd405657c8efb7cce7cdb 100644 (file)
 #include "avformat.h"
 #include "flv.h"
 
+typedef struct {
+    int wrong_dts; ///< wrong dts due to negative cts
+} FLVContext;
+
 static int flv_probe(AVProbeData *p)
 {
     const uint8_t *d;
@@ -53,7 +57,10 @@ static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_c
             acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_S8 : 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;
-        case FLV_CODECID_SPEEX: acodec->codec_id = CODEC_ID_SPEEX;                                  break;
+        case FLV_CODECID_SPEEX:
+            acodec->codec_id = 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_NELLYMOSER_8KHZ_MONO:
             acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
@@ -106,7 +113,7 @@ static int amf_get_string(ByteIOContext *ioc, char *buffer, int buffsize) {
     return length;
 }
 
-static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, unsigned int max_pos, int depth) {
+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;
@@ -208,7 +215,7 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vst
     return 0;
 }
 
-static int flv_read_metabody(AVFormatContext *s, unsigned int next_pos) {
+static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
     AMFDataType type;
     AVStream *stream, *astream, *vstream;
     ByteIOContext *ioc;
@@ -296,8 +303,10 @@ static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
 
 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    int ret, i, type, size, flags, is_audio, next, pos;
-    unsigned dts;
+    FLVContext *flv = s->priv_data;
+    int ret, i, type, size, flags, is_audio;
+    int64_t next, pos;
+    int64_t dts, pts = AV_NOPTS_VALUE;
     AVStream *st = NULL;
 
  retry:
@@ -370,8 +379,8 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
     // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
     if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){
         int size;
-        const int pos= url_ftell(s->pb);
-        const int fsize= url_fsize(s->pb);
+        const int64_t pos= url_ftell(s->pb);
+        const int64_t fsize= url_fsize(s->pb);
         url_fseek(s->pb, fsize-4, SEEK_SET);
         size= get_be32(s->pb);
         url_fseek(s->pb, fsize-3-size, SEEK_SET);
@@ -382,12 +391,9 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
 
     if(is_audio){
-        if(!st->codec->sample_rate || !st->codec->bits_per_coded_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {
+        if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {
             st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
-            if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8KHZ_MONO)
-                st->codec->sample_rate= 8000;
-            else
-                st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
+            st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
             st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
             flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
         }
@@ -400,9 +406,14 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
         int type = get_byte(s->pb);
         size--;
         if (st->codec->codec_id == CODEC_ID_H264) {
-            // cts offset ignored because it might to be signed
-            // and would cause pts < dts
-            get_be24(s->pb);
+            int32_t cts = (get_be24(s->pb)+0xff800000)^0xff800000; // sign extension
+            pts = dts + cts;
+            if (cts < 0) { // dts are wrong
+                flv->wrong_dts = 1;
+                av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
+            }
+            if (flv->wrong_dts)
+                dts = AV_NOPTS_VALUE;
         }
         if (type == 0) {
             if ((ret = flv_get_extradata(s, st, size)) < 0)
@@ -419,6 +430,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
        packet */
     pkt->size = ret;
     pkt->dts = dts;
+    pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
     pkt->stream_index = st->index;
 
     if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
@@ -430,7 +442,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
 AVInputFormat flv_demuxer = {
     "flv",
     NULL_IF_CONFIG_SMALL("FLV format"),
-    0,
+    sizeof(FLVContext),
     flv_probe,
     flv_read_header,
     flv_read_packet,