From: Michael Niedermayer Date: Mon, 17 Jul 2006 21:51:21 +0000 (+0000) Subject: add header info to flv format based on a patch by (Philipp Klaus >ffmpegdevel ad... X-Git-Tag: v0.5~12351 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fd0fb3064d7dcdad03abd4ae5c2d9d1b184b6f2e;p=platform%2Fupstream%2Flibav.git add header info to flv format based on a patch by (Philipp Klaus >ffmpegdevel ad pylonsoft mot ch ) Originally committed as revision 5779 to svn://svn.ffmpeg.org/ffmpeg/trunk --- diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index 7c94587..de1dd08 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -79,11 +79,34 @@ static int get_audio_flags(AVCodecContext *enc){ return flags; } +#define AMF_DOUBLE 0 +#define AMF_BOOLEAN 1 +#define AMF_STRING 2 +#define AMF_OBJECT 3 +#define AMF_MIXED_ARRAY 8 +#define AMF_ARRAY 10 +#define AMF_DATE 11 + +static void put_amf_string(ByteIOContext *pb, const char *str) +{ + size_t len = strlen(str); + put_be16(pb, len); + put_buffer(pb, str, len); +} + +static void put_amf_double(ByteIOContext *pb, double d) +{ + put_byte(pb, AMF_DOUBLE); + put_be64(pb, av_dbl2int(d)); +} + static int flv_write_header(AVFormatContext *s) { ByteIOContext *pb = &s->pb; FLVContext *flv = s->priv_data; - int i; + int i, width, height, samplerate; + double framerate = 0.0; + int metadata_size_pos, data_size; flv->hasAudio = 0; flv->hasVideo = 0; @@ -96,6 +119,19 @@ static int flv_write_header(AVFormatContext *s) for(i=0; inb_streams; i++){ AVCodecContext *enc = s->streams[i]->codec; + if (enc->codec_type == CODEC_TYPE_VIDEO) { + width = enc->width; + height = enc->height; + if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) { + framerate = av_q2d(s->streams[i]->r_frame_rate); + } else { + framerate = 1/av_q2d(s->streams[i]->codec->time_base); + } + flv->hasVideo=1; + } else { + flv->hasAudio=1; + samplerate = enc->sample_rate; + } av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */ if(enc->codec_tag == 5){ put_byte(pb,8); // message type @@ -109,6 +145,62 @@ static int flv_write_header(AVFormatContext *s) return -1; } + /* write meta_tag */ + put_byte(pb, 18); // tag type META + metadata_size_pos= url_ftell(pb); + put_be24(pb, 0); // size of data part (sum of all parts below) + put_be24(pb, 0); // time stamp + put_be32(pb, 0); // reserved + + /* now data of data_size size */ + + /* first event name as a string */ + put_byte(pb, AMF_STRING); // 1 byte + put_amf_string(pb, "onMetaData"); // 12 bytes + + /* mixed array (hash) with size and string/type/data tuples */ + put_byte(pb, AMF_MIXED_ARRAY); + put_be32(pb, 4*flv->hasVideo + flv->hasAudio + (!!s->file_size) + (s->duration != AV_NOPTS_VALUE && s->duration)); + + if (s->duration != AV_NOPTS_VALUE && s->duration) { + put_amf_string(pb, "duration"); + put_amf_double(pb, s->duration / (double)AV_TIME_BASE); + } + + if(flv->hasVideo){ + put_amf_string(pb, "width"); + put_amf_double(pb, width); + + put_amf_string(pb, "height"); + put_amf_double(pb, height); + + put_amf_string(pb, "videodatarate"); + put_amf_double(pb, s->bit_rate / 1024.0); + + put_amf_string(pb, "framerate"); + put_amf_double(pb, framerate); + } + + if(flv->hasAudio){ + put_amf_string(pb, "audiosamplerate"); + put_amf_double(pb, samplerate); + } + + if(s->file_size){ + put_amf_string(pb, "filesize"); + put_amf_double(pb, s->file_size); + } + + put_amf_string(pb, ""); + put_byte(pb, 9); // end marker 1 byte + + /* write total size of tag */ + data_size= url_ftell(pb) - metadata_size_pos - 10; + url_fseek(pb, metadata_size_pos, SEEK_SET); + put_be24(pb, data_size); + url_fseek(pb, data_size + 10 - 3, SEEK_CUR); + put_be32(pb, data_size + 11); + return 0; } @@ -143,7 +235,6 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) put_byte(pb, 9); flags = 2; // choose h263 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator - flv->hasVideo = 1; } else { assert(enc->codec_type == CODEC_TYPE_AUDIO); flags = get_audio_flags(enc); @@ -151,9 +242,6 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) assert(size); put_byte(pb, 8); - - // We got audio! Make sure we set this to the global flags on closure - flv->hasAudio = 1; } put_be24(pb,size+1); // include flags diff --git a/tests/ffmpeg.regression.ref b/tests/ffmpeg.regression.ref index 58a141f..ba24153 100644 --- a/tests/ffmpeg.regression.ref +++ b/tests/ffmpeg.regression.ref @@ -125,8 +125,8 @@ stddev: 20.00 PSNR:22.10 bytes:7602176 1453500 ./data/a-asv2.avi 0b310840a6d3970595983491687669df *./data/out.yuv stddev: 18.82 PSNR:22.63 bytes:7602176 -de42cb5a15bfa8e70cf60edda0b32355 *./data/a-flv.flv -657991 ./data/a-flv.flv +2e1647c88d1b7e56d91058484db602da *./data/a-flv.flv +658104 ./data/a-flv.flv 4d9c04e7017dc68da7891048c46c1c37 *./data/out.yuv stddev: 8.08 PSNR:29.97 bytes:7602176 a7ef4746f27be309138c188e327d3ebe *./data/a-ffv1.avi diff --git a/tests/libav.regression.ref b/tests/libav.regression.ref index 37f48a9..c38c129 100644 --- a/tests/libav.regression.ref +++ b/tests/libav.regression.ref @@ -19,8 +19,8 @@ ffmpeg regression test 0026c4e2528ff8f71b79ef53d38cc04d *./data/b-libav.ffm 389120 ./data/b-libav.ffm ./data/b-libav.ffm CRC=0x688bd1eb -794934a02582f8dfc85d1856514cf37c *./data/b-libav.flv - 339325 ./data/b-libav.flv +eb8ff480628c444be572045ef7208528 *./data/b-libav.flv +339438 ./data/b-libav.flv ./data/b-libav.flv CRC=0x7b9076f8 63e05396e2d52135c4025ce14eb090a9 *./data/b-libav.mov 369893 ./data/b-libav.mov diff --git a/tests/rotozoom.regression.ref b/tests/rotozoom.regression.ref index 606b3ea..350f074 100644 --- a/tests/rotozoom.regression.ref +++ b/tests/rotozoom.regression.ref @@ -125,8 +125,8 @@ f41877971145f58fae85c613ec570d66 *./data/a-asv2.avi 788036 ./data/a-asv2.avi 74a78015b64b2cf8cb9da2e44f508a69 *./data/out.yuv stddev: 10.28 PSNR:27.88 bytes:7602176 -d847c144e7b72027531047ea4c441cbf *./data/a-flv.flv -131771 ./data/a-flv.flv +ae1a6e011fa1ae015cb16a925de4bcfa *./data/a-flv.flv +131884 ./data/a-flv.flv e61cd1e7c5a292d396ae90f4d2fb3db7 *./data/out.yuv stddev: 5.33 PSNR:33.57 bytes:7602176 d0831a8339491fd680b650f05262e5d9 *./data/a-ffv1.avi