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_readwrite.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 8000: //nellymoser only
95 if(enc->codec_id != CODEC_ID_MP3){
96 flags |= FLV_SAMPLERATE_SPECIAL;
100 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
105 if (enc->channels > 1) {
109 switch(enc->codec_id){
111 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
113 case CODEC_ID_PCM_U8:
114 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
116 case CODEC_ID_PCM_S16BE:
117 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
119 case CODEC_ID_PCM_S16LE:
120 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
122 case CODEC_ID_ADPCM_SWF:
123 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
125 case CODEC_ID_NELLYMOSER:
126 if (enc->sample_rate == 8000) {
127 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
129 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
133 flags |= enc->codec_tag<<4;
136 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
143 static void put_amf_string(AVIOContext *pb, const char *str)
145 size_t len = strlen(str);
147 avio_write(pb, str, len);
150 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
151 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
152 avio_wb24(pb, 5); /* Tag Data Size */
153 avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms*/
154 avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms*/
155 avio_wb24(pb, 0); /* StreamId = 0 */
156 avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
157 avio_w8(pb, 2); /* AVC end of sequence */
158 avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
159 avio_wb32(pb, 16); /* Size of FLV tag */
162 static void put_amf_double(AVIOContext *pb, double d)
164 avio_w8(pb, AMF_DATA_TYPE_NUMBER);
165 avio_wb64(pb, av_dbl2int(d));
168 static void put_amf_bool(AVIOContext *pb, int b) {
169 avio_w8(pb, AMF_DATA_TYPE_BOOL);
173 static int flv_write_header(AVFormatContext *s)
175 AVIOContext *pb = s->pb;
176 FLVContext *flv = s->priv_data;
177 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
178 int i, metadata_count = 0;
179 double framerate = 0.0;
180 int64_t metadata_size_pos, data_size, metadata_count_pos;
181 AVDictionaryEntry *tag = NULL;
183 for(i=0; i<s->nb_streams; i++){
184 AVCodecContext *enc = s->streams[i]->codec;
185 FLVStreamContext *sc;
186 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
187 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
188 framerate = av_q2d(s->streams[i]->r_frame_rate);
190 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
193 if(enc->codec_tag == 0) {
194 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
199 if(get_audio_flags(enc)<0)
202 avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
204 sc = av_mallocz(sizeof(FLVStreamContext));
206 return AVERROR(ENOMEM);
207 s->streams[i]->priv_data = sc;
210 flv->delay = AV_NOPTS_VALUE;
212 avio_write(pb, "FLV", 3);
214 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
215 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
219 for(i=0; i<s->nb_streams; i++){
220 if(s->streams[i]->codec->codec_tag == 5){
221 avio_w8(pb,8); // message type
222 avio_wb24(pb,0); // include flags
223 avio_wb24(pb,0); // time stamp
224 avio_wb32(pb,0); // reserved
225 avio_wb32(pb,11); // size
231 avio_w8(pb, 18); // tag type META
232 metadata_size_pos= avio_tell(pb);
233 avio_wb24(pb, 0); // size of data part (sum of all parts below)
234 avio_wb24(pb, 0); // time stamp
235 avio_wb32(pb, 0); // reserved
237 /* now data of data_size size */
239 /* first event name as a string */
240 avio_w8(pb, AMF_DATA_TYPE_STRING);
241 put_amf_string(pb, "onMetaData"); // 12 bytes
243 /* mixed array (hash) with size and string/type/data tuples */
244 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
245 metadata_count_pos = avio_tell(pb);
246 metadata_count = 5*!!video_enc + 5*!!audio_enc + 2; // +2 for duration and file size
247 avio_wb32(pb, metadata_count);
249 put_amf_string(pb, "duration");
250 flv->duration_offset= avio_tell(pb);
251 put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
254 put_amf_string(pb, "width");
255 put_amf_double(pb, video_enc->width);
257 put_amf_string(pb, "height");
258 put_amf_double(pb, video_enc->height);
260 put_amf_string(pb, "videodatarate");
261 put_amf_double(pb, video_enc->bit_rate / 1024.0);
263 put_amf_string(pb, "framerate");
264 put_amf_double(pb, framerate);
266 put_amf_string(pb, "videocodecid");
267 put_amf_double(pb, video_enc->codec_tag);
271 put_amf_string(pb, "audiodatarate");
272 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
274 put_amf_string(pb, "audiosamplerate");
275 put_amf_double(pb, audio_enc->sample_rate);
277 put_amf_string(pb, "audiosamplesize");
278 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
280 put_amf_string(pb, "stereo");
281 put_amf_bool(pb, audio_enc->channels == 2);
283 put_amf_string(pb, "audiocodecid");
284 put_amf_double(pb, audio_enc->codec_tag);
287 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
288 put_amf_string(pb, tag->key);
289 avio_w8(pb, AMF_DATA_TYPE_STRING);
290 put_amf_string(pb, tag->value);
294 put_amf_string(pb, "filesize");
295 flv->filesize_offset= avio_tell(pb);
296 put_amf_double(pb, 0); // delayed write
298 put_amf_string(pb, "");
299 avio_w8(pb, AMF_END_OF_OBJECT);
301 /* write total size of tag */
302 data_size= avio_tell(pb) - metadata_size_pos - 10;
304 avio_seek(pb, metadata_count_pos, SEEK_SET);
305 avio_wb32(pb, metadata_count);
307 avio_seek(pb, metadata_size_pos, SEEK_SET);
308 avio_wb24(pb, data_size);
309 avio_skip(pb, data_size + 10 - 3);
310 avio_wb32(pb, data_size + 11);
312 for (i = 0; i < s->nb_streams; i++) {
313 AVCodecContext *enc = s->streams[i]->codec;
314 if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
316 avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
317 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
318 avio_wb24(pb, 0); // size patched later
319 avio_wb24(pb, 0); // ts
320 avio_w8(pb, 0); // ts ext
321 avio_wb24(pb, 0); // streamid
323 if (enc->codec_id == CODEC_ID_AAC) {
324 avio_w8(pb, get_audio_flags(enc));
325 avio_w8(pb, 0); // AAC sequence header
326 avio_write(pb, enc->extradata, enc->extradata_size);
328 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
329 avio_w8(pb, 0); // AVC sequence header
330 avio_wb24(pb, 0); // composition time
331 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
333 data_size = avio_tell(pb) - pos;
334 avio_seek(pb, -data_size - 10, SEEK_CUR);
335 avio_wb24(pb, data_size);
336 avio_skip(pb, data_size + 10 - 3);
337 avio_wb32(pb, data_size + 11); // previous tag size
344 static int flv_write_trailer(AVFormatContext *s)
348 AVIOContext *pb = s->pb;
349 FLVContext *flv = s->priv_data;
353 for (i = 0; i < s->nb_streams; i++) {
354 AVCodecContext *enc = s->streams[i]->codec;
355 FLVStreamContext *sc = s->streams[i]->priv_data;
356 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
357 enc->codec_id == CODEC_ID_H264) {
358 put_avc_eos_tag(pb, sc->last_ts);
362 file_size = avio_tell(pb);
364 /* update information */
365 avio_seek(pb, flv->duration_offset, SEEK_SET);
366 put_amf_double(pb, flv->duration / (double)1000);
367 avio_seek(pb, flv->filesize_offset, SEEK_SET);
368 put_amf_double(pb, file_size);
370 avio_seek(pb, file_size, SEEK_SET);
374 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
376 AVIOContext *pb = s->pb;
377 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
378 FLVContext *flv = s->priv_data;
379 FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
383 int flags, flags_size;
385 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
387 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
388 enc->codec_id == CODEC_ID_AAC)
390 else if(enc->codec_id == CODEC_ID_H264)
395 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
396 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
398 flags = enc->codec_tag;
400 av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
404 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
406 assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
407 flags = get_audio_flags(enc);
411 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
414 if (enc->codec_id == CODEC_ID_H264) {
415 /* check if extradata looks like MP4 */
416 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
417 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
421 if (flv->delay == AV_NOPTS_VALUE)
422 flv->delay = -pkt->dts;
423 if (pkt->dts < -flv->delay) {
424 av_log(s, AV_LOG_WARNING, "Packets are not in the proper order with "
426 return AVERROR(EINVAL);
429 ts = pkt->dts + flv->delay; // add delay to force positive dts
431 /* check Speex packet duration */
432 if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
433 av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
434 "8 frames per packet. Adobe Flash "
435 "Player cannot handle this!\n");
438 if (sc->last_ts < ts)
441 avio_wb24(pb,size + flags_size);
443 avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
444 avio_wb24(pb,flv->reserved);
446 if (enc->codec_id == CODEC_ID_VP6)
448 if (enc->codec_id == CODEC_ID_VP6F)
449 avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
450 else if (enc->codec_id == CODEC_ID_AAC)
451 avio_w8(pb,1); // AAC raw
452 else if (enc->codec_id == CODEC_ID_H264) {
453 avio_w8(pb,1); // AVC NALU
454 avio_wb24(pb,pkt->pts - pkt->dts);
457 avio_write(pb, data ? data : pkt->data, size);
459 avio_wb32(pb,size+flags_size+11); // previous tag size
460 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
469 AVOutputFormat ff_flv_muxer = {
471 .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
472 .mime_type = "video/x-flv",
474 .priv_data_size = sizeof(FLVContext),
475 #if CONFIG_LIBMP3LAME
476 .audio_codec = CODEC_ID_MP3,
477 #else // CONFIG_LIBMP3LAME
478 .audio_codec = CODEC_ID_ADPCM_SWF,
479 #endif // CONFIG_LIBMP3LAME
480 .video_codec = CODEC_ID_FLV1,
481 .write_header = flv_write_header,
482 .write_packet = flv_write_packet,
483 .write_trailer = flv_write_trailer,
484 .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
485 .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,