3 * Copyright (c) 2003 The Libav Project
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intfloat.h"
28 #include "libavutil/dict.h"
33 static const AVCodecTag flv_video_codec_ids[] = {
34 {CODEC_ID_FLV1, FLV_CODECID_H263 },
35 {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
36 {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
37 {CODEC_ID_VP6F, FLV_CODECID_VP6 },
38 {CODEC_ID_VP6, FLV_CODECID_VP6 },
39 {CODEC_ID_H264, FLV_CODECID_H264 },
43 static const AVCodecTag flv_audio_codec_ids[] = {
44 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
45 {CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
46 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
47 {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
48 {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
49 {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
50 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
51 {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
55 typedef struct FLVContext {
57 int64_t duration_offset;
58 int64_t filesize_offset;
60 int64_t delay; ///< first dts delay (needed for AVC & Speex)
63 typedef struct FLVStreamContext {
64 int64_t last_ts; ///< last timestamp for each stream
67 static int get_audio_flags(AVCodecContext *enc){
68 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
70 if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
71 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
72 else if (enc->codec_id == CODEC_ID_SPEEX) {
73 if (enc->sample_rate != 16000) {
74 av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
77 if (enc->channels != 1) {
78 av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
81 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
83 switch (enc->sample_rate) {
85 flags |= FLV_SAMPLERATE_44100HZ;
88 flags |= FLV_SAMPLERATE_22050HZ;
91 flags |= FLV_SAMPLERATE_11025HZ;
93 case 16000: //nellymoser only
94 case 8000: //nellymoser only
96 if(enc->codec_id != CODEC_ID_MP3){
97 flags |= FLV_SAMPLERATE_SPECIAL;
101 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
106 if (enc->channels > 1) {
110 switch(enc->codec_id){
112 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
114 case CODEC_ID_PCM_U8:
115 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
117 case CODEC_ID_PCM_S16BE:
118 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
120 case CODEC_ID_PCM_S16LE:
121 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
123 case CODEC_ID_ADPCM_SWF:
124 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
126 case CODEC_ID_NELLYMOSER:
127 if (enc->sample_rate == 8000) {
128 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
129 } else if (enc->sample_rate == 16000) {
130 flags |= FLV_CODECID_NELLYMOSER_16KHZ_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_double2int(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;
181 int i, metadata_count = 0;
182 double framerate = 0.0;
183 int64_t metadata_size_pos, data_size, metadata_count_pos;
184 AVDictionaryEntry *tag = NULL;
186 for(i=0; i<s->nb_streams; i++){
187 AVCodecContext *enc = s->streams[i]->codec;
188 FLVStreamContext *sc;
189 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
190 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
191 framerate = av_q2d(s->streams[i]->r_frame_rate);
193 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
196 if(enc->codec_tag == 0) {
197 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
202 if(get_audio_flags(enc)<0)
205 avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
207 sc = av_mallocz(sizeof(FLVStreamContext));
209 return AVERROR(ENOMEM);
210 s->streams[i]->priv_data = sc;
213 flv->delay = AV_NOPTS_VALUE;
215 avio_write(pb, "FLV", 3);
217 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
218 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
222 for(i=0; i<s->nb_streams; i++){
223 if(s->streams[i]->codec->codec_tag == 5){
224 avio_w8(pb,8); // message type
225 avio_wb24(pb,0); // include flags
226 avio_wb24(pb,0); // time stamp
227 avio_wb32(pb,0); // reserved
228 avio_wb32(pb,11); // size
234 avio_w8(pb, 18); // tag type META
235 metadata_size_pos= avio_tell(pb);
236 avio_wb24(pb, 0); // size of data part (sum of all parts below)
237 avio_wb24(pb, 0); // time stamp
238 avio_wb32(pb, 0); // reserved
240 /* now data of data_size size */
242 /* first event name as a string */
243 avio_w8(pb, AMF_DATA_TYPE_STRING);
244 put_amf_string(pb, "onMetaData"); // 12 bytes
246 /* mixed array (hash) with size and string/type/data tuples */
247 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
248 metadata_count_pos = avio_tell(pb);
249 metadata_count = 5*!!video_enc + 5*!!audio_enc + 2; // +2 for duration and file size
250 avio_wb32(pb, metadata_count);
252 put_amf_string(pb, "duration");
253 flv->duration_offset= avio_tell(pb);
254 put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
257 put_amf_string(pb, "width");
258 put_amf_double(pb, video_enc->width);
260 put_amf_string(pb, "height");
261 put_amf_double(pb, video_enc->height);
263 put_amf_string(pb, "videodatarate");
264 put_amf_double(pb, video_enc->bit_rate / 1024.0);
266 put_amf_string(pb, "framerate");
267 put_amf_double(pb, framerate);
269 put_amf_string(pb, "videocodecid");
270 put_amf_double(pb, video_enc->codec_tag);
274 put_amf_string(pb, "audiodatarate");
275 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
277 put_amf_string(pb, "audiosamplerate");
278 put_amf_double(pb, audio_enc->sample_rate);
280 put_amf_string(pb, "audiosamplesize");
281 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
283 put_amf_string(pb, "stereo");
284 put_amf_bool(pb, audio_enc->channels == 2);
286 put_amf_string(pb, "audiocodecid");
287 put_amf_double(pb, audio_enc->codec_tag);
290 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
291 put_amf_string(pb, tag->key);
292 avio_w8(pb, AMF_DATA_TYPE_STRING);
293 put_amf_string(pb, tag->value);
297 put_amf_string(pb, "filesize");
298 flv->filesize_offset= avio_tell(pb);
299 put_amf_double(pb, 0); // delayed write
301 put_amf_string(pb, "");
302 avio_w8(pb, AMF_END_OF_OBJECT);
304 /* write total size of tag */
305 data_size= avio_tell(pb) - metadata_size_pos - 10;
307 avio_seek(pb, metadata_count_pos, SEEK_SET);
308 avio_wb32(pb, metadata_count);
310 avio_seek(pb, metadata_size_pos, SEEK_SET);
311 avio_wb24(pb, data_size);
312 avio_skip(pb, data_size + 10 - 3);
313 avio_wb32(pb, data_size + 11);
315 for (i = 0; i < s->nb_streams; i++) {
316 AVCodecContext *enc = s->streams[i]->codec;
317 if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
319 avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
320 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
321 avio_wb24(pb, 0); // size patched later
322 avio_wb24(pb, 0); // ts
323 avio_w8(pb, 0); // ts ext
324 avio_wb24(pb, 0); // streamid
326 if (enc->codec_id == CODEC_ID_AAC) {
327 avio_w8(pb, get_audio_flags(enc));
328 avio_w8(pb, 0); // AAC sequence header
329 avio_write(pb, enc->extradata, enc->extradata_size);
331 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
332 avio_w8(pb, 0); // AVC sequence header
333 avio_wb24(pb, 0); // composition time
334 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
336 data_size = avio_tell(pb) - pos;
337 avio_seek(pb, -data_size - 10, SEEK_CUR);
338 avio_wb24(pb, data_size);
339 avio_skip(pb, data_size + 10 - 3);
340 avio_wb32(pb, data_size + 11); // previous tag size
347 static int flv_write_trailer(AVFormatContext *s)
351 AVIOContext *pb = s->pb;
352 FLVContext *flv = s->priv_data;
356 for (i = 0; i < s->nb_streams; i++) {
357 AVCodecContext *enc = s->streams[i]->codec;
358 FLVStreamContext *sc = s->streams[i]->priv_data;
359 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
360 enc->codec_id == CODEC_ID_H264) {
361 put_avc_eos_tag(pb, sc->last_ts);
365 file_size = avio_tell(pb);
367 /* update information */
368 avio_seek(pb, flv->duration_offset, SEEK_SET);
369 put_amf_double(pb, flv->duration / (double)1000);
370 avio_seek(pb, flv->filesize_offset, SEEK_SET);
371 put_amf_double(pb, file_size);
373 avio_seek(pb, file_size, SEEK_SET);
377 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
379 AVIOContext *pb = s->pb;
380 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
381 FLVContext *flv = s->priv_data;
382 FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
386 int flags, flags_size;
388 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
390 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
391 enc->codec_id == CODEC_ID_AAC)
393 else if(enc->codec_id == CODEC_ID_H264)
398 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
399 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
401 flags = enc->codec_tag;
403 av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
407 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
409 assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
410 flags = get_audio_flags(enc);
414 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
417 if (enc->codec_id == CODEC_ID_H264) {
418 /* check if extradata looks like MP4 */
419 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
420 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
424 if (flv->delay == AV_NOPTS_VALUE)
425 flv->delay = -pkt->dts;
426 if (pkt->dts < -flv->delay) {
427 av_log(s, AV_LOG_WARNING, "Packets are not in the proper order with "
429 return AVERROR(EINVAL);
432 ts = pkt->dts + flv->delay; // add delay to force positive dts
434 /* check Speex packet duration */
435 if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
436 av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
437 "8 frames per packet. Adobe Flash "
438 "Player cannot handle this!\n");
441 if (sc->last_ts < ts)
444 avio_wb24(pb,size + flags_size);
446 avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
447 avio_wb24(pb,flv->reserved);
449 if (enc->codec_id == CODEC_ID_VP6)
451 if (enc->codec_id == CODEC_ID_VP6F)
452 avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
453 else if (enc->codec_id == CODEC_ID_AAC)
454 avio_w8(pb,1); // AAC raw
455 else if (enc->codec_id == CODEC_ID_H264) {
456 avio_w8(pb,1); // AVC NALU
457 avio_wb24(pb,pkt->pts - pkt->dts);
460 avio_write(pb, data ? data : pkt->data, size);
462 avio_wb32(pb,size+flags_size+11); // previous tag size
463 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
472 AVOutputFormat ff_flv_muxer = {
474 .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
475 .mime_type = "video/x-flv",
477 .priv_data_size = sizeof(FLVContext),
478 #if CONFIG_LIBMP3LAME
479 .audio_codec = CODEC_ID_MP3,
480 #else // CONFIG_LIBMP3LAME
481 .audio_codec = CODEC_ID_ADPCM_SWF,
482 #endif // CONFIG_LIBMP3LAME
483 .video_codec = CODEC_ID_FLV1,
484 .write_header = flv_write_header,
485 .write_packet = flv_write_packet,
486 .write_trailer = flv_write_trailer,
487 .codec_tag = (const AVCodecTag* const []){
488 flv_video_codec_ids, flv_audio_codec_ids, 0
490 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,