3 * Copyright (c) 2003 The FFmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/intfloat_readwrite.h"
29 #include "libavutil/dict.h"
34 static const AVCodecTag flv_video_codec_ids[] = {
35 {CODEC_ID_FLV1, FLV_CODECID_H263 },
36 {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
37 {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
38 {CODEC_ID_VP6F, FLV_CODECID_VP6 },
39 {CODEC_ID_VP6, FLV_CODECID_VP6 },
40 {CODEC_ID_H264, FLV_CODECID_H264 },
44 static const AVCodecTag flv_audio_codec_ids[] = {
45 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
46 {CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
47 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
48 {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
49 {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
50 {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
51 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
52 {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
56 typedef struct FLVContext {
58 int64_t duration_offset;
59 int64_t filesize_offset;
61 int delay; ///< first dts delay for AVC
62 int64_t last_video_ts;
65 static int get_audio_flags(AVCodecContext *enc){
66 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
68 if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
69 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
70 else if (enc->codec_id == CODEC_ID_SPEEX) {
71 if (enc->sample_rate != 16000) {
72 av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
75 if (enc->channels != 1) {
76 av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
79 if (enc->frame_size / 320 > 8) {
80 av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
81 "8 frames per packet. Adobe Flash "
82 "Player cannot handle this!\n");
84 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
86 switch (enc->sample_rate) {
88 flags |= FLV_SAMPLERATE_44100HZ;
91 flags |= FLV_SAMPLERATE_22050HZ;
94 flags |= FLV_SAMPLERATE_11025HZ;
96 case 8000: //nellymoser only
98 if(enc->codec_id != CODEC_ID_MP3){
99 flags |= FLV_SAMPLERATE_SPECIAL;
103 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
108 if (enc->channels > 1) {
112 switch(enc->codec_id){
114 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
116 case CODEC_ID_PCM_U8:
117 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
119 case CODEC_ID_PCM_S16BE:
120 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
122 case CODEC_ID_PCM_S16LE:
123 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
125 case CODEC_ID_ADPCM_SWF:
126 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
128 case CODEC_ID_NELLYMOSER:
129 if (enc->sample_rate == 8000) {
130 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
132 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
136 flags |= enc->codec_tag<<4;
139 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
146 static void put_amf_string(AVIOContext *pb, const char *str)
148 size_t len = strlen(str);
150 avio_write(pb, str, len);
153 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
154 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
155 avio_wb24(pb, 5); /* Tag Data Size */
156 avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms*/
157 avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms*/
158 avio_wb24(pb, 0); /* StreamId = 0 */
159 avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
160 avio_w8(pb, 2); /* AVC end of sequence */
161 avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
162 avio_wb32(pb, 16); /* Size of FLV tag */
165 static void put_amf_double(AVIOContext *pb, double d)
167 avio_w8(pb, AMF_DATA_TYPE_NUMBER);
168 avio_wb64(pb, av_dbl2int(d));
171 static void put_amf_bool(AVIOContext *pb, int b) {
172 avio_w8(pb, AMF_DATA_TYPE_BOOL);
176 static int flv_write_header(AVFormatContext *s)
178 AVIOContext *pb = s->pb;
179 FLVContext *flv = s->priv_data;
180 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
182 double framerate = 0.0;
183 int64_t metadata_size_pos, data_size;
184 AVDictionaryEntry *tag = NULL;
186 for(i=0; i<s->nb_streams; i++){
187 AVCodecContext *enc = s->streams[i]->codec;
188 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
189 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
190 framerate = av_q2d(s->streams[i]->r_frame_rate);
192 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
195 if(enc->codec_tag == 0) {
196 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
201 if(get_audio_flags(enc)<0)
204 av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
206 avio_write(pb, "FLV", 3);
208 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
209 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
213 for(i=0; i<s->nb_streams; i++){
214 if(s->streams[i]->codec->codec_tag == 5){
215 avio_w8(pb,8); // message type
216 avio_wb24(pb,0); // include flags
217 avio_wb24(pb,0); // time stamp
218 avio_wb32(pb,0); // reserved
219 avio_wb32(pb,11); // size
224 flv->last_video_ts = -1;
227 avio_w8(pb, 18); // tag type META
228 metadata_size_pos= avio_tell(pb);
229 avio_wb24(pb, 0); // size of data part (sum of all parts below)
230 avio_wb24(pb, 0); // time stamp
231 avio_wb32(pb, 0); // reserved
233 /* now data of data_size size */
235 /* first event name as a string */
236 avio_w8(pb, AMF_DATA_TYPE_STRING);
237 put_amf_string(pb, "onMetaData"); // 12 bytes
239 /* mixed array (hash) with size and string/type/data tuples */
240 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
241 avio_wb32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size
243 put_amf_string(pb, "duration");
244 flv->duration_offset= avio_tell(pb);
245 put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
248 put_amf_string(pb, "width");
249 put_amf_double(pb, video_enc->width);
251 put_amf_string(pb, "height");
252 put_amf_double(pb, video_enc->height);
254 put_amf_string(pb, "videodatarate");
255 put_amf_double(pb, video_enc->bit_rate / 1024.0);
257 put_amf_string(pb, "framerate");
258 put_amf_double(pb, framerate);
260 put_amf_string(pb, "videocodecid");
261 put_amf_double(pb, video_enc->codec_tag);
265 put_amf_string(pb, "audiodatarate");
266 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
268 put_amf_string(pb, "audiosamplerate");
269 put_amf_double(pb, audio_enc->sample_rate);
271 put_amf_string(pb, "audiosamplesize");
272 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
274 put_amf_string(pb, "stereo");
275 put_amf_bool(pb, audio_enc->channels == 2);
277 put_amf_string(pb, "audiocodecid");
278 put_amf_double(pb, audio_enc->codec_tag);
281 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
282 put_amf_string(pb, tag->key);
283 avio_w8(pb, AMF_DATA_TYPE_STRING);
284 put_amf_string(pb, tag->value);
287 put_amf_string(pb, "filesize");
288 flv->filesize_offset= avio_tell(pb);
289 put_amf_double(pb, 0); // delayed write
291 put_amf_string(pb, "");
292 avio_w8(pb, AMF_END_OF_OBJECT);
294 /* write total size of tag */
295 data_size= avio_tell(pb) - metadata_size_pos - 10;
296 avio_seek(pb, metadata_size_pos, SEEK_SET);
297 avio_wb24(pb, data_size);
298 avio_skip(pb, data_size + 10 - 3);
299 avio_wb32(pb, data_size + 11);
301 for (i = 0; i < s->nb_streams; i++) {
302 AVCodecContext *enc = s->streams[i]->codec;
303 if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
305 avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
306 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
307 avio_wb24(pb, 0); // size patched later
308 avio_wb24(pb, 0); // ts
309 avio_w8(pb, 0); // ts ext
310 avio_wb24(pb, 0); // streamid
312 if (enc->codec_id == CODEC_ID_AAC) {
313 avio_w8(pb, get_audio_flags(enc));
314 avio_w8(pb, 0); // AAC sequence header
315 avio_write(pb, enc->extradata, enc->extradata_size);
317 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
318 avio_w8(pb, 0); // AVC sequence header
319 avio_wb24(pb, 0); // composition time
320 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
322 data_size = avio_tell(pb) - pos;
323 avio_seek(pb, -data_size - 10, SEEK_CUR);
324 avio_wb24(pb, data_size);
325 avio_skip(pb, data_size + 10 - 3);
326 avio_wb32(pb, data_size + 11); // previous tag size
333 static int flv_write_trailer(AVFormatContext *s)
337 AVIOContext *pb = s->pb;
338 FLVContext *flv = s->priv_data;
342 for (i = 0; i < s->nb_streams; i++) {
343 AVCodecContext *enc = s->streams[i]->codec;
344 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
345 enc->codec_id == CODEC_ID_H264) {
346 put_avc_eos_tag(pb, flv->last_video_ts);
350 file_size = avio_tell(pb);
352 /* update informations */
353 avio_seek(pb, flv->duration_offset, SEEK_SET);
354 put_amf_double(pb, flv->duration / (double)1000);
355 avio_seek(pb, flv->filesize_offset, SEEK_SET);
356 put_amf_double(pb, file_size);
358 avio_seek(pb, file_size, SEEK_SET);
362 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
364 AVIOContext *pb = s->pb;
365 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
366 FLVContext *flv = s->priv_data;
370 int flags, flags_size;
372 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
374 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
375 enc->codec_id == CODEC_ID_AAC)
377 else if(enc->codec_id == CODEC_ID_H264)
382 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
383 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
385 flags = enc->codec_tag;
387 av_log(enc, AV_LOG_ERROR, "video codec %s not compatible with flv\n", avcodec_get_name(enc->codec_id));
391 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
393 assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
394 flags = get_audio_flags(enc);
398 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
401 if (enc->codec_id == CODEC_ID_H264) {
402 /* check if extradata looks like mp4 formated */
403 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
404 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
407 if (!flv->delay && pkt->dts < 0)
408 flv->delay = -pkt->dts;
409 } else if (enc->codec_id == CODEC_ID_AAC && pkt->size > 2 &&
410 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
411 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
415 ts = pkt->dts + flv->delay; // add delay to force positive dts
416 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
417 if (flv->last_video_ts < ts)
418 flv->last_video_ts = ts;
420 avio_wb24(pb,size + flags_size);
422 avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
423 avio_wb24(pb,flv->reserved);
425 if (enc->codec_id == CODEC_ID_VP6)
427 if (enc->codec_id == CODEC_ID_VP6F)
428 avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
429 else if (enc->codec_id == CODEC_ID_AAC)
430 avio_w8(pb,1); // AAC raw
431 else if (enc->codec_id == CODEC_ID_H264) {
432 avio_w8(pb,1); // AVC NALU
433 avio_wb24(pb,pkt->pts - pkt->dts);
436 avio_write(pb, data ? data : pkt->data, size);
438 avio_wb32(pb,size+flags_size+11); // previous tag size
439 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
448 AVOutputFormat ff_flv_muxer = {
450 .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
451 .mime_type = "video/x-flv",
453 .priv_data_size = sizeof(FLVContext),
454 #if CONFIG_LIBMP3LAME
455 .audio_codec = CODEC_ID_MP3,
456 #else // CONFIG_LIBMP3LAME
457 .audio_codec = CODEC_ID_ADPCM_SWF,
458 #endif // CONFIG_LIBMP3LAME
459 .video_codec = CODEC_ID_FLV1,
460 .write_header = flv_write_header,
461 .write_packet = flv_write_packet,
462 .write_trailer = flv_write_trailer,
463 .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
464 .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,