]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/flvenc.c
swscale: fix signed shift overflows in ff_yuv2rgb_c_init_tables()
[ffmpeg] / libavformat / flvenc.c
index 851ce491b03613a056dc624c38aaaba6dcf420c5..0d1fa7312ad0e3fc1c099cb09e1d4edc22dbbf10 100644 (file)
@@ -57,10 +57,13 @@ typedef struct FLVContext {
     int64_t duration_offset;
     int64_t filesize_offset;
     int64_t duration;
-    int delay; ///< first dts delay for AVC
-    int64_t last_video_ts;
 } FLVContext;
 
+typedef struct FLVStreamContext {
+    int     delay;      ///< first dts delay for each stream (needed for AVC & Speex)
+    int64_t last_ts;    ///< last timestamp for each stream
+} FLVStreamContext;
+
 static int get_audio_flags(AVCodecContext *enc){
     int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
 
@@ -75,11 +78,6 @@ static int get_audio_flags(AVCodecContext *enc){
             av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
             return -1;
         }
-        if (enc->frame_size / 320 > 8) {
-            av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
-                                        "8 frames per packet. Adobe Flash "
-                                        "Player cannot handle this!\n");
-        }
         return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
     } else {
     switch (enc->sample_rate) {
@@ -184,6 +182,7 @@ static int flv_write_header(AVFormatContext *s)
 
     for(i=0; i<s->nb_streams; i++){
         AVCodecContext *enc = s->streams[i]->codec;
+        FLVStreamContext *sc;
         if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
             if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
                 framerate = av_q2d(s->streams[i]->r_frame_rate);
@@ -201,6 +200,12 @@ static int flv_write_header(AVFormatContext *s)
                 return -1;
         }
         av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
+
+        sc = av_mallocz(sizeof(FLVStreamContext));
+        if (!sc)
+            return AVERROR(ENOMEM);
+        s->streams[i]->priv_data = sc;
+        sc->last_ts = -1;
     }
     avio_write(pb, "FLV", 3);
     avio_w8(pb,1);
@@ -220,8 +225,6 @@ static int flv_write_header(AVFormatContext *s)
         }
     }
 
-    flv->last_video_ts = -1;
-
     /* write meta_tag */
     avio_w8(pb, 18);         // tag type META
     metadata_size_pos= avio_tell(pb);
@@ -347,9 +350,10 @@ static int flv_write_trailer(AVFormatContext *s)
     /* Add EOS tag */
     for (i = 0; i < s->nb_streams; i++) {
         AVCodecContext *enc = s->streams[i]->codec;
+        FLVStreamContext *sc = s->streams[i]->priv_data;
         if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
                 enc->codec_id == CODEC_ID_H264) {
-            put_avc_eos_tag(pb, flv->last_video_ts);
+            put_avc_eos_tag(pb, sc->last_ts);
         }
     }
 
@@ -370,6 +374,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
     AVIOContext *pb = s->pb;
     AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
     FLVContext *flv = s->priv_data;
+    FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
     unsigned ts;
     int size= pkt->size;
     uint8_t *data= NULL;
@@ -411,14 +416,21 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
                 return -1;
         }
     }
-    if (!flv->delay && pkt->dts < 0)
-        flv->delay = -pkt->dts;
+    if (!sc->delay && pkt->dts < 0)
+        sc->delay = -pkt->dts;
 
-    ts = pkt->dts + flv->delay; // add delay to force positive dts
-    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
-        if (flv->last_video_ts < ts)
-            flv->last_video_ts = ts;
+    ts = pkt->dts + sc->delay; // add delay to force positive dts
+
+    /* check Speex packet duration */
+    if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
+        av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
+                                  "8 frames per packet. Adobe Flash "
+                                  "Player cannot handle this!\n");
     }
+
+    if (sc->last_ts < ts)
+        sc->last_ts = ts;
+
     avio_wb24(pb,size + flags_size);
     avio_wb24(pb,ts);
     avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
@@ -438,7 +450,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
     avio_write(pb, data ? data : pkt->data, size);
 
     avio_wb32(pb,size+flags_size+11); // previous tag size
-    flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
+    flv->duration = FFMAX(flv->duration, pkt->pts + sc->delay + pkt->duration);
 
     avio_flush(pb);