comparison flvenc.c @ 6385:ea409874020e libavformat

Add AVC EOS tag to H264-encoded FLV files. Patch by Thierry Foucu, tfoucu gmail
author cehoyos
date Wed, 18 Aug 2010 09:39:21 +0000
parents c7b98381ec2d
children ae4ea19af762
comparison
equal deleted inserted replaced
6384:2d51ca7714c3 6385:ea409874020e
52 int reserved; 52 int reserved;
53 int64_t duration_offset; 53 int64_t duration_offset;
54 int64_t filesize_offset; 54 int64_t filesize_offset;
55 int64_t duration; 55 int64_t duration;
56 int delay; ///< first dts delay for AVC 56 int delay; ///< first dts delay for AVC
57 int64_t last_video_ts;
57 } FLVContext; 58 } FLVContext;
58 59
59 static int get_audio_flags(AVCodecContext *enc){ 60 static int get_audio_flags(AVCodecContext *enc){
60 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT; 61 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
61 62
142 size_t len = strlen(str); 143 size_t len = strlen(str);
143 put_be16(pb, len); 144 put_be16(pb, len);
144 put_buffer(pb, str, len); 145 put_buffer(pb, str, len);
145 } 146 }
146 147
148 static void put_avc_eos_tag(ByteIOContext *pb, unsigned ts) {
149 put_byte(pb, FLV_TAG_TYPE_VIDEO);
150 put_be24(pb, 5); /* Tag Data Size */
151 put_be24(pb, ts); /* lower 24 bits of timestamp in ms*/
152 put_byte(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms*/
153 put_be24(pb, 0); /* StreamId = 0 */
154 put_byte(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
155 put_byte(pb, 2); /* AVC end of sequence */
156 put_be24(pb, 0); /* Always 0 for AVC EOS. */
157 put_be32(pb, 16); /* Size of FLV tag */
158 }
159
147 static void put_amf_double(ByteIOContext *pb, double d) 160 static void put_amf_double(ByteIOContext *pb, double d)
148 { 161 {
149 put_byte(pb, AMF_DATA_TYPE_NUMBER); 162 put_byte(pb, AMF_DATA_TYPE_NUMBER);
150 put_be64(pb, av_dbl2int(d)); 163 put_be64(pb, av_dbl2int(d));
151 } 164 }
199 put_be32(pb,0); // reserved 212 put_be32(pb,0); // reserved
200 put_be32(pb,11); // size 213 put_be32(pb,11); // size
201 flv->reserved=5; 214 flv->reserved=5;
202 } 215 }
203 } 216 }
217
218 flv->last_video_ts = -1;
204 219
205 /* write meta_tag */ 220 /* write meta_tag */
206 put_byte(pb, 18); // tag type META 221 put_byte(pb, 18); // tag type META
207 metadata_size_pos= url_ftell(pb); 222 metadata_size_pos= url_ftell(pb);
208 put_be24(pb, 0); // size of data part (sum of all parts below) 223 put_be24(pb, 0); // size of data part (sum of all parts below)
307 { 322 {
308 int64_t file_size; 323 int64_t file_size;
309 324
310 ByteIOContext *pb = s->pb; 325 ByteIOContext *pb = s->pb;
311 FLVContext *flv = s->priv_data; 326 FLVContext *flv = s->priv_data;
327 int i;
328
329 /* Add EOS tag */
330 for (i = 0; i < s->nb_streams; i++) {
331 AVCodecContext *enc = s->streams[i]->codec;
332 if (enc->codec_type == CODEC_TYPE_VIDEO &&
333 enc->codec_id == CODEC_ID_H264) {
334 put_avc_eos_tag(pb, flv->last_video_ts);
335 }
336 }
312 337
313 file_size = url_ftell(pb); 338 file_size = url_ftell(pb);
314 339
315 /* update informations */ 340 /* update informations */
316 url_fseek(pb, flv->duration_offset, SEEK_SET); 341 url_fseek(pb, flv->duration_offset, SEEK_SET);
370 if (!flv->delay && pkt->dts < 0) 395 if (!flv->delay && pkt->dts < 0)
371 flv->delay = -pkt->dts; 396 flv->delay = -pkt->dts;
372 } 397 }
373 398
374 ts = pkt->dts + flv->delay; // add delay to force positive dts 399 ts = pkt->dts + flv->delay; // add delay to force positive dts
400 if (enc->codec_type == CODEC_TYPE_VIDEO) {
401 if (flv->last_video_ts < ts)
402 flv->last_video_ts = ts;
403 }
375 put_be24(pb,size + flags_size); 404 put_be24(pb,size + flags_size);
376 put_be24(pb,ts); 405 put_be24(pb,ts);
377 put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_ 406 put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
378 put_be24(pb,flv->reserved); 407 put_be24(pb,flv->reserved);
379 put_byte(pb,flags); 408 put_byte(pb,flags);