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.h"
29 #include "libavutil/dict.h"
34 static const AVCodecTag flv_video_codec_ids[] = {
35 {CODEC_ID_FLV1, FLV_CODECID_H263 },
36 {CODEC_ID_H263, FLV_CODECID_REALH263},
37 {CODEC_ID_MPEG4, FLV_CODECID_MPEG4 },
38 {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
39 {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
40 {CODEC_ID_VP6F, FLV_CODECID_VP6 },
41 {CODEC_ID_VP6, FLV_CODECID_VP6 },
42 {CODEC_ID_VP6A, FLV_CODECID_VP6A },
43 {CODEC_ID_H264, FLV_CODECID_H264 },
47 static const AVCodecTag flv_audio_codec_ids[] = {
48 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
49 {CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
50 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
51 {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
52 {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
53 {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
54 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
55 {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
59 typedef struct FLVContext {
61 int64_t duration_offset;
62 int64_t filesize_offset;
64 int64_t delay; ///< first dts delay (needed for AVC & Speex)
67 typedef struct FLVStreamContext {
68 int64_t last_ts; ///< last timestamp for each stream
71 static int get_audio_flags(AVFormatContext *s, AVCodecContext *enc)
73 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
75 if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
76 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
77 else if (enc->codec_id == CODEC_ID_SPEEX) {
78 if (enc->sample_rate != 16000) {
79 av_log(s, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
82 if (enc->channels != 1) {
83 av_log(s, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
86 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
88 switch (enc->sample_rate) {
90 flags |= FLV_SAMPLERATE_44100HZ;
93 flags |= FLV_SAMPLERATE_22050HZ;
96 flags |= FLV_SAMPLERATE_11025HZ;
98 case 16000: //nellymoser only
99 case 8000: //nellymoser only
101 if(enc->codec_id != CODEC_ID_MP3){
102 flags |= FLV_SAMPLERATE_SPECIAL;
106 av_log(s, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
111 if (enc->channels > 1) {
115 switch(enc->codec_id){
117 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
119 case CODEC_ID_PCM_U8:
120 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
122 case CODEC_ID_PCM_S16BE:
123 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
125 case CODEC_ID_PCM_S16LE:
126 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
128 case CODEC_ID_ADPCM_SWF:
129 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
131 case CODEC_ID_NELLYMOSER:
132 if (enc->sample_rate == 8000) {
133 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
134 } else if (enc->sample_rate == 16000) {
135 flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
137 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
141 flags |= enc->codec_tag<<4;
144 av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
151 static void put_amf_string(AVIOContext *pb, const char *str)
153 size_t len = strlen(str);
155 avio_write(pb, str, len);
158 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
159 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
160 avio_wb24(pb, 5); /* Tag Data Size */
161 avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms*/
162 avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms*/
163 avio_wb24(pb, 0); /* StreamId = 0 */
164 avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
165 avio_w8(pb, 2); /* AVC end of sequence */
166 avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
167 avio_wb32(pb, 16); /* Size of FLV tag */
170 static void put_amf_double(AVIOContext *pb, double d)
172 avio_w8(pb, AMF_DATA_TYPE_NUMBER);
173 avio_wb64(pb, av_double2int(d));
176 static void put_amf_bool(AVIOContext *pb, int b) {
177 avio_w8(pb, AMF_DATA_TYPE_BOOL);
181 static int flv_write_header(AVFormatContext *s)
183 AVIOContext *pb = s->pb;
184 FLVContext *flv = s->priv_data;
185 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
186 int i, metadata_count = 0;
187 double framerate = 0.0;
188 int64_t metadata_size_pos, data_size, metadata_count_pos;
189 AVDictionaryEntry *tag = NULL;
191 for(i=0; i<s->nb_streams; i++){
192 AVCodecContext *enc = s->streams[i]->codec;
193 FLVStreamContext *sc;
194 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
195 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
196 framerate = av_q2d(s->streams[i]->r_frame_rate);
198 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
201 if(enc->codec_tag == 0) {
202 av_log(s, AV_LOG_ERROR, "video codec not compatible with flv\n");
205 } else if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
207 if (get_audio_flags(s, enc) < 0)
210 avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
212 sc = av_mallocz(sizeof(FLVStreamContext));
214 return AVERROR(ENOMEM);
215 s->streams[i]->priv_data = sc;
218 flv->delay = AV_NOPTS_VALUE;
220 avio_write(pb, "FLV", 3);
222 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
223 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
227 for(i=0; i<s->nb_streams; i++){
228 if(s->streams[i]->codec->codec_tag == 5){
229 avio_w8(pb,8); // message type
230 avio_wb24(pb,0); // include flags
231 avio_wb24(pb,0); // time stamp
232 avio_wb32(pb,0); // reserved
233 avio_wb32(pb,11); // size
239 avio_w8(pb, 18); // tag type META
240 metadata_size_pos= avio_tell(pb);
241 avio_wb24(pb, 0); // size of data part (sum of all parts below)
242 avio_wb24(pb, 0); // time stamp
243 avio_wb32(pb, 0); // reserved
245 /* now data of data_size size */
247 /* first event name as a string */
248 avio_w8(pb, AMF_DATA_TYPE_STRING);
249 put_amf_string(pb, "onMetaData"); // 12 bytes
251 /* mixed array (hash) with size and string/type/data tuples */
252 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
253 metadata_count_pos = avio_tell(pb);
254 metadata_count = 5*!!video_enc + 5*!!audio_enc + 2; // +2 for duration and file size
255 avio_wb32(pb, metadata_count);
257 put_amf_string(pb, "duration");
258 flv->duration_offset= avio_tell(pb);
259 put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
262 put_amf_string(pb, "width");
263 put_amf_double(pb, video_enc->width);
265 put_amf_string(pb, "height");
266 put_amf_double(pb, video_enc->height);
268 put_amf_string(pb, "videodatarate");
269 put_amf_double(pb, video_enc->bit_rate / 1024.0);
271 put_amf_string(pb, "framerate");
272 put_amf_double(pb, framerate);
274 put_amf_string(pb, "videocodecid");
275 put_amf_double(pb, video_enc->codec_tag);
279 put_amf_string(pb, "audiodatarate");
280 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
282 put_amf_string(pb, "audiosamplerate");
283 put_amf_double(pb, audio_enc->sample_rate);
285 put_amf_string(pb, "audiosamplesize");
286 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
288 put_amf_string(pb, "stereo");
289 put_amf_bool(pb, audio_enc->channels == 2);
291 put_amf_string(pb, "audiocodecid");
292 put_amf_double(pb, audio_enc->codec_tag);
295 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
296 if( !strcmp(tag->key, "width")
297 ||!strcmp(tag->key, "height")
298 ||!strcmp(tag->key, "videodatarate")
299 ||!strcmp(tag->key, "framerate")
300 ||!strcmp(tag->key, "videocodecid")
301 ||!strcmp(tag->key, "audiodatarate")
302 ||!strcmp(tag->key, "audiosamplerate")
303 ||!strcmp(tag->key, "audiosamplesize")
304 ||!strcmp(tag->key, "stereo")
305 ||!strcmp(tag->key, "audiocodecid")
306 ||!strcmp(tag->key, "duration")
307 ||!strcmp(tag->key, "onMetaData")
309 av_log(s, AV_LOG_DEBUG, "ignoring metadata for %s\n", tag->key);
312 put_amf_string(pb, tag->key);
313 avio_w8(pb, AMF_DATA_TYPE_STRING);
314 put_amf_string(pb, tag->value);
318 put_amf_string(pb, "filesize");
319 flv->filesize_offset= avio_tell(pb);
320 put_amf_double(pb, 0); // delayed write
322 put_amf_string(pb, "");
323 avio_w8(pb, AMF_END_OF_OBJECT);
325 /* write total size of tag */
326 data_size= avio_tell(pb) - metadata_size_pos - 10;
328 avio_seek(pb, metadata_count_pos, SEEK_SET);
329 avio_wb32(pb, metadata_count);
331 avio_seek(pb, metadata_size_pos, SEEK_SET);
332 avio_wb24(pb, data_size);
333 avio_skip(pb, data_size + 10 - 3);
334 avio_wb32(pb, data_size + 11);
336 for (i = 0; i < s->nb_streams; i++) {
337 AVCodecContext *enc = s->streams[i]->codec;
338 if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264 || enc->codec_id == CODEC_ID_MPEG4) {
340 avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
341 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
342 avio_wb24(pb, 0); // size patched later
343 avio_wb24(pb, 0); // ts
344 avio_w8(pb, 0); // ts ext
345 avio_wb24(pb, 0); // streamid
347 if (enc->codec_id == CODEC_ID_AAC) {
348 avio_w8(pb, get_audio_flags(s, enc));
349 avio_w8(pb, 0); // AAC sequence header
350 avio_write(pb, enc->extradata, enc->extradata_size);
352 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
353 avio_w8(pb, 0); // AVC sequence header
354 avio_wb24(pb, 0); // composition time
355 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
357 data_size = avio_tell(pb) - pos;
358 avio_seek(pb, -data_size - 10, SEEK_CUR);
359 avio_wb24(pb, data_size);
360 avio_skip(pb, data_size + 10 - 3);
361 avio_wb32(pb, data_size + 11); // previous tag size
368 static int flv_write_trailer(AVFormatContext *s)
372 AVIOContext *pb = s->pb;
373 FLVContext *flv = s->priv_data;
377 for (i = 0; i < s->nb_streams; i++) {
378 AVCodecContext *enc = s->streams[i]->codec;
379 FLVStreamContext *sc = s->streams[i]->priv_data;
380 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
381 (enc->codec_id == CODEC_ID_H264 || enc->codec_id == CODEC_ID_MPEG4)) {
382 put_avc_eos_tag(pb, sc->last_ts);
386 file_size = avio_tell(pb);
388 /* update information */
389 avio_seek(pb, flv->duration_offset, SEEK_SET);
390 put_amf_double(pb, flv->duration / (double)1000);
391 avio_seek(pb, flv->filesize_offset, SEEK_SET);
392 put_amf_double(pb, file_size);
394 avio_seek(pb, file_size, SEEK_SET);
398 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
400 AVIOContext *pb = s->pb;
401 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
402 FLVContext *flv = s->priv_data;
403 FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
407 int flags, flags_size;
409 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
411 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
412 enc->codec_id == CODEC_ID_VP6A || enc->codec_id == CODEC_ID_AAC)
414 else if(enc->codec_id == CODEC_ID_H264 || enc->codec_id == CODEC_ID_MPEG4)
419 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
420 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
422 flags = enc->codec_tag;
424 av_log(s, AV_LOG_ERROR, "video codec %s not compatible with flv\n", avcodec_get_name(enc->codec_id));
428 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
429 } else if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
430 flags = get_audio_flags(s, enc);
434 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
436 // In-band flv metadata ("scriptdata")
437 assert(enc->codec_type == AVMEDIA_TYPE_DATA);
438 avio_w8(pb, FLV_TAG_TYPE_META);
443 if (enc->codec_id == CODEC_ID_H264 || enc->codec_id == CODEC_ID_MPEG4) {
444 /* check if extradata looks like mp4 formated */
445 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
446 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
449 } else if (enc->codec_id == CODEC_ID_AAC && pkt->size > 2 &&
450 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
451 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
454 if (flv->delay == AV_NOPTS_VALUE)
455 flv->delay = -pkt->dts;
456 if (pkt->dts < -flv->delay) {
457 av_log(s, AV_LOG_WARNING, "Packets are not in the proper order with "
459 return AVERROR(EINVAL);
462 ts = pkt->dts + flv->delay; // add delay to force positive dts
464 /* check Speex packet duration */
465 if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
466 av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
467 "8 frames per packet. Adobe Flash "
468 "Player cannot handle this!\n");
471 if (sc->last_ts < ts)
474 avio_wb24(pb,size + flags_size);
476 avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
477 avio_wb24(pb,flv->reserved);
482 if (enc->codec_id == CODEC_ID_VP6)
484 if (enc->codec_id == CODEC_ID_VP6F || enc->codec_id == CODEC_ID_VP6A)
485 avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
486 else if (enc->codec_id == CODEC_ID_AAC)
487 avio_w8(pb,1); // AAC raw
488 else if (enc->codec_id == CODEC_ID_H264 || enc->codec_id == CODEC_ID_MPEG4) {
489 avio_w8(pb,1); // AVC NALU
490 avio_wb24(pb,pkt->pts - pkt->dts);
493 avio_write(pb, data ? data : pkt->data, size);
495 avio_wb32(pb,size+flags_size+11); // previous tag size
496 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
505 AVOutputFormat ff_flv_muxer = {
507 .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
508 .mime_type = "video/x-flv",
510 .priv_data_size = sizeof(FLVContext),
511 #if CONFIG_LIBMP3LAME
512 .audio_codec = CODEC_ID_MP3,
513 #else // CONFIG_LIBMP3LAME
514 .audio_codec = CODEC_ID_ADPCM_SWF,
515 #endif // CONFIG_LIBMP3LAME
516 .video_codec = CODEC_ID_FLV1,
517 .write_header = flv_write_header,
518 .write_packet = flv_write_packet,
519 .write_trailer = flv_write_trailer,
520 .codec_tag = (const AVCodecTag* const []){
521 flv_video_codec_ids, flv_audio_codec_ids, 0
523 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |