Browse code

flvenc: store delay and last_ts per-stream.

Justin Ruggles authored on 2011/10/21 04:08:48
Showing 1 changed files
... ...
@@ -57,10 +57,13 @@ typedef struct FLVContext {
57 57
     int64_t duration_offset;
58 58
     int64_t filesize_offset;
59 59
     int64_t duration;
60
-    int delay; ///< first dts delay for AVC
61
-    int64_t last_ts;
62 60
 } FLVContext;
63 61
 
62
+typedef struct FLVStreamContext {
63
+    int     delay;      ///< first dts delay for each stream (needed for AVC & Speex)
64
+    int64_t last_ts;    ///< last timestamp for each stream
65
+} FLVStreamContext;
66
+
64 67
 static int get_audio_flags(AVCodecContext *enc){
65 68
     int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
66 69
 
... ...
@@ -179,6 +182,7 @@ static int flv_write_header(AVFormatContext *s)
179 179
 
180 180
     for(i=0; i<s->nb_streams; i++){
181 181
         AVCodecContext *enc = s->streams[i]->codec;
182
+        FLVStreamContext *sc;
182 183
         if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
183 184
             if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
184 185
                 framerate = av_q2d(s->streams[i]->r_frame_rate);
... ...
@@ -196,6 +200,12 @@ static int flv_write_header(AVFormatContext *s)
196 196
                 return -1;
197 197
         }
198 198
         av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
199
+
200
+        sc = av_mallocz(sizeof(FLVStreamContext));
201
+        if (!sc)
202
+            return AVERROR(ENOMEM);
203
+        s->streams[i]->priv_data = sc;
204
+        sc->last_ts = -1;
199 205
     }
200 206
     avio_write(pb, "FLV", 3);
201 207
     avio_w8(pb,1);
... ...
@@ -215,8 +225,6 @@ static int flv_write_header(AVFormatContext *s)
215 215
         }
216 216
     }
217 217
 
218
-    flv->last_ts = -1;
219
-
220 218
     /* write meta_tag */
221 219
     avio_w8(pb, 18);         // tag type META
222 220
     metadata_size_pos= avio_tell(pb);
... ...
@@ -342,9 +350,10 @@ static int flv_write_trailer(AVFormatContext *s)
342 342
     /* Add EOS tag */
343 343
     for (i = 0; i < s->nb_streams; i++) {
344 344
         AVCodecContext *enc = s->streams[i]->codec;
345
+        FLVStreamContext *sc = s->streams[i]->priv_data;
345 346
         if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
346 347
                 enc->codec_id == CODEC_ID_H264) {
347
-            put_avc_eos_tag(pb, flv->last_ts);
348
+            put_avc_eos_tag(pb, sc->last_ts);
348 349
         }
349 350
     }
350 351
 
... ...
@@ -365,6 +374,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
365 365
     AVIOContext *pb = s->pb;
366 366
     AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
367 367
     FLVContext *flv = s->priv_data;
368
+    FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
368 369
     unsigned ts;
369 370
     int size= pkt->size;
370 371
     uint8_t *data= NULL;
... ...
@@ -406,20 +416,20 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
406 406
                 return -1;
407 407
         }
408 408
     }
409
-    if (!flv->delay && pkt->dts < 0)
410
-        flv->delay = -pkt->dts;
409
+    if (!sc->delay && pkt->dts < 0)
410
+        sc->delay = -pkt->dts;
411 411
 
412
-    ts = pkt->dts + flv->delay; // add delay to force positive dts
412
+    ts = pkt->dts + sc->delay; // add delay to force positive dts
413 413
 
414 414
     /* check Speex packet duration */
415
-    if (enc->codec_id == CODEC_ID_SPEEX && ts - flv->last_ts > 160) {
415
+    if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
416 416
         av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
417 417
                                   "8 frames per packet. Adobe Flash "
418 418
                                   "Player cannot handle this!\n");
419 419
     }
420 420
 
421
-    if (flv->last_ts < ts)
422
-        flv->last_ts = ts;
421
+    if (sc->last_ts < ts)
422
+        sc->last_ts = ts;
423 423
 
424 424
     avio_wb24(pb,size + flags_size);
425 425
     avio_wb24(pb,ts);
... ...
@@ -440,7 +450,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
440 440
     avio_write(pb, data ? data : pkt->data, size);
441 441
 
442 442
     avio_wb32(pb,size+flags_size+11); // previous tag size
443
-    flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
443
+    flv->duration = FFMAX(flv->duration, pkt->pts + sc->delay + pkt->duration);
444 444
 
445 445
     avio_flush(pb);
446 446