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/dict.h"
23 #include "libavutil/intfloat.h"
33 static const AVCodecTag flv_video_codec_ids[] = {
34 { AV_CODEC_ID_FLV1, FLV_CODECID_H263 },
35 { AV_CODEC_ID_FLASHSV, FLV_CODECID_SCREEN },
36 { AV_CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2 },
37 { AV_CODEC_ID_VP6F, FLV_CODECID_VP6 },
38 { AV_CODEC_ID_VP6A, FLV_CODECID_VP6A },
39 { AV_CODEC_ID_H264, FLV_CODECID_H264 },
40 { AV_CODEC_ID_NONE, 0 }
43 static const AVCodecTag flv_audio_codec_ids[] = {
44 { AV_CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET },
45 { AV_CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
46 { AV_CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
47 { AV_CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET },
48 { AV_CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET },
49 { AV_CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET },
50 { AV_CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET },
51 { AV_CODEC_ID_PCM_MULAW, FLV_CODECID_PCM_MULAW >> FLV_AUDIO_CODECID_OFFSET },
52 { AV_CODEC_ID_PCM_ALAW, FLV_CODECID_PCM_ALAW >> FLV_AUDIO_CODECID_OFFSET },
53 { AV_CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET },
54 { AV_CODEC_ID_NONE, 0 }
57 typedef struct FLVContext {
59 int64_t duration_offset;
60 int64_t filesize_offset;
62 int64_t delay; ///< first dts delay (needed for AVC & Speex)
65 typedef struct FLVStreamContext {
66 int64_t last_ts; ///< last timestamp for each stream
69 static int get_audio_flags(AVFormatContext *s, AVCodecContext *enc)
71 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT
72 : FLV_SAMPLESSIZE_8BIT;
74 if (enc->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
75 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ |
76 FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
77 else if (enc->codec_id == AV_CODEC_ID_SPEEX) {
78 if (enc->sample_rate != 16000) {
79 av_log(s, AV_LOG_ERROR,
80 "flv only supports wideband (16kHz) Speex audio\n");
83 if (enc->channels != 1) {
84 av_log(s, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
87 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
89 switch (enc->sample_rate) {
91 flags |= FLV_SAMPLERATE_44100HZ;
94 flags |= FLV_SAMPLERATE_22050HZ;
97 flags |= FLV_SAMPLERATE_11025HZ;
99 case 16000: // nellymoser only
100 case 8000: // nellymoser only
101 case 5512: // not MP3
102 if (enc->codec_id != AV_CODEC_ID_MP3) {
103 flags |= FLV_SAMPLERATE_SPECIAL;
107 av_log(s, AV_LOG_ERROR,
108 "flv does not support that sample rate, "
109 "choose from (44100, 22050, 11025).\n");
114 if (enc->channels > 1)
117 switch (enc->codec_id) {
118 case AV_CODEC_ID_MP3:
119 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
121 case AV_CODEC_ID_PCM_U8:
122 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
124 case AV_CODEC_ID_PCM_S16BE:
125 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
127 case AV_CODEC_ID_PCM_S16LE:
128 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
130 case AV_CODEC_ID_ADPCM_SWF:
131 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
133 case AV_CODEC_ID_NELLYMOSER:
134 if (enc->sample_rate == 8000)
135 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
136 else if (enc->sample_rate == 16000)
137 flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
139 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
141 case AV_CODEC_ID_PCM_MULAW:
142 flags = FLV_CODECID_PCM_MULAW | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
144 case AV_CODEC_ID_PCM_ALAW:
145 flags = FLV_CODECID_PCM_ALAW | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
148 flags |= enc->codec_tag << 4;
151 av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
158 static void put_amf_string(AVIOContext *pb, const char *str)
160 size_t len = strlen(str);
162 avio_write(pb, str, len);
165 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
167 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
168 avio_wb24(pb, 5); /* Tag Data Size */
169 avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms */
170 avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
171 avio_wb24(pb, 0); /* StreamId = 0 */
172 avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
173 avio_w8(pb, 2); /* AVC end of sequence */
174 avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
175 avio_wb32(pb, 16); /* Size of FLV tag */
178 static void put_amf_double(AVIOContext *pb, double d)
180 avio_w8(pb, AMF_DATA_TYPE_NUMBER);
181 avio_wb64(pb, av_double2int(d));
184 static void put_amf_bool(AVIOContext *pb, int b)
186 avio_w8(pb, AMF_DATA_TYPE_BOOL);
190 static int flv_write_header(AVFormatContext *s)
192 AVIOContext *pb = s->pb;
193 FLVContext *flv = s->priv_data;
194 AVCodecContext *audio_enc = NULL, *video_enc = NULL, *data_enc = NULL;
195 int i, metadata_count = 0;
196 double framerate = 0.0;
197 int64_t metadata_size_pos, data_size, metadata_count_pos;
198 AVDictionaryEntry *tag = NULL;
200 for (i = 0; i < s->nb_streams; i++) {
201 AVCodecContext *enc = s->streams[i]->codec;
202 FLVStreamContext *sc;
203 switch (enc->codec_type) {
204 case AVMEDIA_TYPE_VIDEO:
205 if (s->streams[i]->avg_frame_rate.den &&
206 s->streams[i]->avg_frame_rate.num) {
207 framerate = av_q2d(s->streams[i]->avg_frame_rate);
210 av_log(s, AV_LOG_ERROR,
211 "at most one video stream is supported in flv\n");
212 return AVERROR(EINVAL);
215 if (enc->codec_tag == 0) {
216 av_log(s, AV_LOG_ERROR, "video codec not compatible with flv\n");
220 case AVMEDIA_TYPE_AUDIO:
222 av_log(s, AV_LOG_ERROR,
223 "at most one audio stream is supported in flv\n");
224 return AVERROR(EINVAL);
227 if (get_audio_flags(s, enc) < 0)
228 return AVERROR_INVALIDDATA;
230 case AVMEDIA_TYPE_DATA:
231 if (enc->codec_id != AV_CODEC_ID_TEXT) {
232 av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
233 return AVERROR_INVALIDDATA;
238 av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
241 avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
243 sc = av_mallocz(sizeof(FLVStreamContext));
245 return AVERROR(ENOMEM);
246 s->streams[i]->priv_data = sc;
250 flv->delay = AV_NOPTS_VALUE;
252 avio_write(pb, "FLV", 3);
254 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc +
255 FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
259 for (i = 0; i < s->nb_streams; i++)
260 if (s->streams[i]->codec->codec_tag == 5) {
261 avio_w8(pb, 8); // message type
262 avio_wb24(pb, 0); // include flags
263 avio_wb24(pb, 0); // time stamp
264 avio_wb32(pb, 0); // reserved
265 avio_wb32(pb, 11); // size
270 avio_w8(pb, 18); // tag type META
271 metadata_size_pos = avio_tell(pb);
272 avio_wb24(pb, 0); // size of data part (sum of all parts below)
273 avio_wb24(pb, 0); // timestamp
274 avio_wb32(pb, 0); // reserved
276 /* now data of data_size size */
278 /* first event name as a string */
279 avio_w8(pb, AMF_DATA_TYPE_STRING);
280 put_amf_string(pb, "onMetaData"); // 12 bytes
282 /* mixed array (hash) with size and string/type/data tuples */
283 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
284 metadata_count_pos = avio_tell(pb);
285 metadata_count = 4 * !!video_enc +
288 2; // +2 for duration and file size
290 avio_wb32(pb, metadata_count);
292 put_amf_string(pb, "duration");
293 flv->duration_offset= avio_tell(pb);
295 // fill in the guessed duration, it'll be corrected later if incorrect
296 put_amf_double(pb, s->duration / AV_TIME_BASE);
299 put_amf_string(pb, "width");
300 put_amf_double(pb, video_enc->width);
302 put_amf_string(pb, "height");
303 put_amf_double(pb, video_enc->height);
305 put_amf_string(pb, "videodatarate");
306 put_amf_double(pb, video_enc->bit_rate / 1024.0);
308 if (framerate != 0.0) {
309 put_amf_string(pb, "framerate");
310 put_amf_double(pb, framerate);
314 put_amf_string(pb, "videocodecid");
315 put_amf_double(pb, video_enc->codec_tag);
319 put_amf_string(pb, "audiodatarate");
320 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
322 put_amf_string(pb, "audiosamplerate");
323 put_amf_double(pb, audio_enc->sample_rate);
325 put_amf_string(pb, "audiosamplesize");
326 put_amf_double(pb, audio_enc->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
328 put_amf_string(pb, "stereo");
329 put_amf_bool(pb, audio_enc->channels == 2);
331 put_amf_string(pb, "audiocodecid");
332 put_amf_double(pb, audio_enc->codec_tag);
336 put_amf_string(pb, "datastream");
337 put_amf_double(pb, 0.0);
340 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
341 put_amf_string(pb, tag->key);
342 avio_w8(pb, AMF_DATA_TYPE_STRING);
343 put_amf_string(pb, tag->value);
347 put_amf_string(pb, "filesize");
348 flv->filesize_offset = avio_tell(pb);
349 put_amf_double(pb, 0); // delayed write
351 put_amf_string(pb, "");
352 avio_w8(pb, AMF_END_OF_OBJECT);
354 /* write total size of tag */
355 data_size = avio_tell(pb) - metadata_size_pos - 10;
357 avio_seek(pb, metadata_count_pos, SEEK_SET);
358 avio_wb32(pb, metadata_count);
360 avio_seek(pb, metadata_size_pos, SEEK_SET);
361 avio_wb24(pb, data_size);
362 avio_skip(pb, data_size + 10 - 3);
363 avio_wb32(pb, data_size + 11);
365 for (i = 0; i < s->nb_streams; i++) {
366 AVCodecContext *enc = s->streams[i]->codec;
367 if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264) {
369 avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
370 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
371 avio_wb24(pb, 0); // size patched later
372 avio_wb24(pb, 0); // ts
373 avio_w8(pb, 0); // ts ext
374 avio_wb24(pb, 0); // streamid
376 if (enc->codec_id == AV_CODEC_ID_AAC) {
377 avio_w8(pb, get_audio_flags(s, enc));
378 avio_w8(pb, 0); // AAC sequence header
379 avio_write(pb, enc->extradata, enc->extradata_size);
381 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
382 avio_w8(pb, 0); // AVC sequence header
383 avio_wb24(pb, 0); // composition time
384 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
386 data_size = avio_tell(pb) - pos;
387 avio_seek(pb, -data_size - 10, SEEK_CUR);
388 avio_wb24(pb, data_size);
389 avio_skip(pb, data_size + 10 - 3);
390 avio_wb32(pb, data_size + 11); // previous tag size
397 static int flv_write_trailer(AVFormatContext *s)
401 AVIOContext *pb = s->pb;
402 FLVContext *flv = s->priv_data;
406 for (i = 0; i < s->nb_streams; i++) {
407 AVCodecContext *enc = s->streams[i]->codec;
408 FLVStreamContext *sc = s->streams[i]->priv_data;
409 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
410 enc->codec_id == AV_CODEC_ID_H264)
411 put_avc_eos_tag(pb, sc->last_ts);
414 file_size = avio_tell(pb);
416 /* update information */
417 if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0)
418 av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
420 put_amf_double(pb, flv->duration / (double)1000);
421 if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0)
422 av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
424 put_amf_double(pb, file_size);
426 avio_seek(pb, file_size, SEEK_SET);
430 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
432 AVIOContext *pb = s->pb;
433 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
434 FLVContext *flv = s->priv_data;
435 FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
437 int size = pkt->size;
438 uint8_t *data = NULL;
439 int flags = 0, flags_size;
441 if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
442 enc->codec_id == AV_CODEC_ID_AAC)
444 else if (enc->codec_id == AV_CODEC_ID_H264)
449 switch (enc->codec_type) {
450 case AVMEDIA_TYPE_VIDEO:
451 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
453 flags = enc->codec_tag;
455 av_log(s, AV_LOG_ERROR,
456 "video codec %X not compatible with flv\n",
461 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
463 case AVMEDIA_TYPE_AUDIO:
464 flags = get_audio_flags(s, enc);
468 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
470 case AVMEDIA_TYPE_DATA:
471 avio_w8(pb, FLV_TAG_TYPE_META);
474 return AVERROR(EINVAL);
477 if (enc->codec_id == AV_CODEC_ID_H264)
478 /* check if extradata looks like MP4 */
479 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
480 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
483 if (flv->delay == AV_NOPTS_VALUE)
484 flv->delay = -pkt->dts;
486 if (pkt->dts < -flv->delay) {
487 av_log(s, AV_LOG_WARNING,
488 "Packets are not in the proper order with respect to DTS\n");
489 return AVERROR(EINVAL);
492 ts = pkt->dts + flv->delay; // add delay to force positive dts
494 /* check Speex packet duration */
495 if (enc->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
496 av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
497 "8 frames per packet. Adobe Flash "
498 "Player cannot handle this!\n");
500 if (sc->last_ts < ts)
503 avio_wb24(pb, size + flags_size);
505 avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
506 avio_wb24(pb, flv->reserved);
508 if (enc->codec_type == AVMEDIA_TYPE_DATA) {
510 int64_t metadata_size_pos = avio_tell(pb);
511 avio_w8(pb, AMF_DATA_TYPE_STRING);
512 put_amf_string(pb, "onTextData");
513 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
515 put_amf_string(pb, "type");
516 avio_w8(pb, AMF_DATA_TYPE_STRING);
517 put_amf_string(pb, "Text");
518 put_amf_string(pb, "text");
519 avio_w8(pb, AMF_DATA_TYPE_STRING);
520 put_amf_string(pb, pkt->data);
521 put_amf_string(pb, "");
522 avio_w8(pb, AMF_END_OF_OBJECT);
523 /* write total size of tag */
524 data_size = avio_tell(pb) - metadata_size_pos;
525 avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
526 avio_wb24(pb, data_size);
527 avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
528 avio_wb32(pb, data_size + 11);
531 if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {
532 if (enc->extradata_size)
533 avio_w8(pb, enc->extradata[0]);
535 avio_w8(pb, ((FFALIGN(enc->width, 16) - enc->width) << 4) |
536 (FFALIGN(enc->height, 16) - enc->height));
537 } else if (enc->codec_id == AV_CODEC_ID_AAC)
538 avio_w8(pb, 1); // AAC raw
539 else if (enc->codec_id == AV_CODEC_ID_H264) {
540 avio_w8(pb, 1); // AVC NALU
541 avio_wb24(pb, pkt->pts - pkt->dts);
544 avio_write(pb, data ? data : pkt->data, size);
546 avio_wb32(pb, size + flags_size + 11); // previous tag size
547 flv->duration = FFMAX(flv->duration,
548 pkt->pts + flv->delay + pkt->duration);
556 AVOutputFormat ff_flv_muxer = {
558 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
559 .mime_type = "video/x-flv",
561 .priv_data_size = sizeof(FLVContext),
562 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
563 .video_codec = AV_CODEC_ID_FLV1,
564 .write_header = flv_write_header,
565 .write_packet = flv_write_packet,
566 .write_trailer = flv_write_trailer,
567 .codec_tag = (const AVCodecTag* const []) {
568 flv_video_codec_ids, flv_audio_codec_ids, 0
570 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |