[FFmpeg-devel] [PATCH] Add AVC EOS tag to H264-encoded FLV files

Thierry Foucu tfoucu
Fri Aug 13 07:27:28 CEST 2010


On Thu, Aug 5, 2010 at 1:33 PM, Baptiste Coudurier <
baptiste.coudurier at gmail.com> wrote:

> On 08/05/2010 09:48 AM, Thierry Foucu wrote:
> >
> > [...]
>
>
>> +static void put_avc_eos_tag(ByteIOContext *pb, unsigned ts) {
>> +    put_byte(pb, FLV_TAG_TYPE_VIDEO);
>> +    put_be24(pb, 5);  /* Tag Data Size */
>> +    put_be24(pb, ts);  /* lower 24 bits of timestamp in ms*/
>> +    put_byte(pb, (ts>>  24)&  0x7F);  /* MSB of ts in ms*/
>> +    put_be24(pb, 0);  /* StreamId = 0 */
>> +    put_byte(pb, 23);  /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
>> +    put_byte(pb, 2);  /* AVC end of sequence */
>> +    put_be24(pb, 0);  /* Always 0 for AVC EOS. */
>> +    put_be32(pb, 16);  /* Size of FLV tag */
>> +}
>> +
>>  static void put_amf_double(ByteIOContext *pb, double d)
>>  {
>>      put_byte(pb, AMF_DATA_TYPE_NUMBER);
>> @@ -309,7 +321,16 @@
>>
>>      ByteIOContext *pb = s->pb;
>>      FLVContext *flv = s->priv_data;
>> +    int i;
>>
>> +    /* Add EOS tag */
>> +    for (i = 0; i<  s->nb_streams; i++) {
>> +        AVCodecContext *enc = s->streams[i]->codec;
>> +        if (enc->codec_type == CODEC_TYPE_VIDEO&&  enc->codec_id ==
>> CODEC_ID_H264) {
>> +            put_avc_eos_tag(pb, flv->duration);
>> +        }
>>
>
> flv->duration is the pts, this is wrong, you must write the dts in the
> timestamp field.
>
>
Ok, what about this one:

Index: libavformat/flvenc.c
===================================================================
--- libavformat/flvenc.c (revision 24791)
+++ libavformat/flvenc.c (working copy)
@@ -54,6 +54,7 @@
     int64_t filesize_offset;
     int64_t duration;
     int delay; ///< first dts delay for AVC
+    int64_t last_video_ts;
 } FLVContext;

 static int get_audio_flags(AVCodecContext *enc){
@@ -144,6 +145,18 @@
     put_buffer(pb, str, len);
 }

+static void put_avc_eos_tag(ByteIOContext *pb, unsigned ts) {
+    put_byte(pb, FLV_TAG_TYPE_VIDEO);
+    put_be24(pb, 5);  /* Tag Data Size */
+    put_be24(pb, ts);  /* lower 24 bits of timestamp in ms*/
+    put_byte(pb, (ts >> 24) & 0x7F);  /* MSB of ts in ms*/
+    put_be24(pb, 0);  /* StreamId = 0 */
+    put_byte(pb, 23);  /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
+    put_byte(pb, 2);  /* AVC end of sequence */
+    put_be24(pb, 0);  /* Always 0 for AVC EOS. */
+    put_be32(pb, 16);  /* Size of FLV tag */
+}
+
 static void put_amf_double(ByteIOContext *pb, double d)
 {
     put_byte(pb, AMF_DATA_TYPE_NUMBER);
@@ -202,6 +215,8 @@
         }
     }

+    flv->last_video_ts = -1;
+
     /* write meta_tag */
     put_byte(pb, 18);         // tag type META
     metadata_size_pos= url_ftell(pb);
@@ -309,7 +324,17 @@

     ByteIOContext *pb = s->pb;
     FLVContext *flv = s->priv_data;
+    int i;

+    /* Add EOS tag */
+    for (i = 0; i < s->nb_streams; i++) {
+        AVCodecContext *enc = s->streams[i]->codec;
+        if (enc->codec_type == CODEC_TYPE_VIDEO &&
+                enc->codec_id == CODEC_ID_H264) {
+            put_avc_eos_tag(pb, flv->last_video_ts);
+        }
+    }
+
     file_size = url_ftell(pb);

     /* update informations */
@@ -372,6 +397,10 @@
     }

     ts = pkt->dts + flv->delay; // add delay to force positive dts
+    if (enc->codec_type == CODEC_TYPE_VIDEO) {
+        if (flv->last_video_ts < ts)
+            flv->last_video_ts = ts;
+    }
     put_be24(pb,size + flags_size);
     put_be24(pb,ts);
     put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_





>
> --
> Baptiste COUDURIER
> Key fingerprint                 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA
> FFmpeg maintainer                                  http://www.ffmpeg.org
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at mplayerhq.hu
> https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-devel
>
-------------- next part --------------
Index: libavformat/flvenc.c
===================================================================
--- libavformat/flvenc.c	(revision 24791)
+++ libavformat/flvenc.c	(working copy)
@@ -54,6 +54,7 @@
     int64_t filesize_offset;
     int64_t duration;
     int delay; ///< first dts delay for AVC
+    int64_t last_video_ts;
 } FLVContext;
 
 static int get_audio_flags(AVCodecContext *enc){
@@ -144,6 +145,18 @@
     put_buffer(pb, str, len);
 }
 
+static void put_avc_eos_tag(ByteIOContext *pb, unsigned ts) {
+    put_byte(pb, FLV_TAG_TYPE_VIDEO);
+    put_be24(pb, 5);  /* Tag Data Size */
+    put_be24(pb, ts);  /* lower 24 bits of timestamp in ms*/
+    put_byte(pb, (ts >> 24) & 0x7F);  /* MSB of ts in ms*/
+    put_be24(pb, 0);  /* StreamId = 0 */
+    put_byte(pb, 23);  /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
+    put_byte(pb, 2);  /* AVC end of sequence */
+    put_be24(pb, 0);  /* Always 0 for AVC EOS. */
+    put_be32(pb, 16);  /* Size of FLV tag */
+}
+
 static void put_amf_double(ByteIOContext *pb, double d)
 {
     put_byte(pb, AMF_DATA_TYPE_NUMBER);
@@ -202,6 +215,8 @@
         }
     }
 
+    flv->last_video_ts = -1;
+
     /* write meta_tag */
     put_byte(pb, 18);         // tag type META
     metadata_size_pos= url_ftell(pb);
@@ -309,7 +324,17 @@
 
     ByteIOContext *pb = s->pb;
     FLVContext *flv = s->priv_data;
+    int i;
 
+    /* Add EOS tag */
+    for (i = 0; i < s->nb_streams; i++) {
+        AVCodecContext *enc = s->streams[i]->codec;
+        if (enc->codec_type == CODEC_TYPE_VIDEO &&
+                enc->codec_id == CODEC_ID_H264) {
+            put_avc_eos_tag(pb, flv->last_video_ts);
+        }
+    }
+
     file_size = url_ftell(pb);
 
     /* update informations */
@@ -372,6 +397,10 @@
     }
 
     ts = pkt->dts + flv->delay; // add delay to force positive dts
+    if (enc->codec_type == CODEC_TYPE_VIDEO) {
+        if (flv->last_video_ts < ts)
+            flv->last_video_ts = ts;
+    }
     put_be24(pb,size + flags_size);
     put_be24(pb,ts);
     put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_



More information about the ffmpeg-devel mailing list