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
29 static const AVCodecTag flv_video_codec_ids[] = {
30 {CODEC_ID_FLV1, FLV_CODECID_H263 },
31 {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
32 {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
33 {CODEC_ID_VP6F, FLV_CODECID_VP6 },
34 {CODEC_ID_VP6, FLV_CODECID_VP6 },
35 {CODEC_ID_H264, FLV_CODECID_H264 },
39 static const AVCodecTag flv_audio_codec_ids[] = {
40 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
41 {CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
42 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
43 {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
44 {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
45 {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
46 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
47 {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
51 typedef struct FLVContext {
53 int64_t duration_offset;
54 int64_t filesize_offset;
56 int delay; ///< first dts delay for AVC
59 static int get_audio_flags(AVCodecContext *enc){
60 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
62 if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
63 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
64 else if (enc->codec_id == CODEC_ID_SPEEX) {
65 if (enc->sample_rate != 16000) {
66 av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
69 if (enc->channels != 1) {
70 av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
73 if (enc->frame_size / 320 > 8) {
74 av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
75 "8 frames per packet. Adobe Flash "
76 "Player cannot handle this!\n");
78 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
80 switch (enc->sample_rate) {
82 flags |= FLV_SAMPLERATE_44100HZ;
85 flags |= FLV_SAMPLERATE_22050HZ;
88 flags |= FLV_SAMPLERATE_11025HZ;
90 case 8000: //nellymoser only
92 if(enc->codec_id != CODEC_ID_MP3){
93 flags |= FLV_SAMPLERATE_SPECIAL;
97 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
102 if (enc->channels > 1) {
106 switch(enc->codec_id){
108 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
110 case CODEC_ID_PCM_U8:
111 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
113 case CODEC_ID_PCM_S16BE:
114 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
116 case CODEC_ID_PCM_S16LE:
117 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
119 case CODEC_ID_ADPCM_SWF:
120 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
122 case CODEC_ID_NELLYMOSER:
123 if (enc->sample_rate == 8000) {
124 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
126 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
130 flags |= enc->codec_tag<<4;
133 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
140 static void put_amf_string(ByteIOContext *pb, const char *str)
142 size_t len = strlen(str);
144 put_buffer(pb, str, len);
147 static void put_amf_double(ByteIOContext *pb, double d)
149 put_byte(pb, AMF_DATA_TYPE_NUMBER);
150 put_be64(pb, av_dbl2int(d));
153 static void put_amf_bool(ByteIOContext *pb, int b) {
154 put_byte(pb, AMF_DATA_TYPE_BOOL);
158 static int flv_write_header(AVFormatContext *s)
160 ByteIOContext *pb = s->pb;
161 FLVContext *flv = s->priv_data;
162 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
164 double framerate = 0.0;
165 int metadata_size_pos, data_size;
167 for(i=0; i<s->nb_streams; i++){
168 AVCodecContext *enc = s->streams[i]->codec;
169 if (enc->codec_type == CODEC_TYPE_VIDEO) {
170 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
171 framerate = av_q2d(s->streams[i]->r_frame_rate);
173 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
176 if(enc->codec_tag == 0) {
177 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
182 if(get_audio_flags(enc)<0)
185 av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
189 put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
190 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
194 for(i=0; i<s->nb_streams; i++){
195 if(s->streams[i]->codec->codec_tag == 5){
196 put_byte(pb,8); // message type
197 put_be24(pb,0); // include flags
198 put_be24(pb,0); // time stamp
199 put_be32(pb,0); // reserved
200 put_be32(pb,11); // size
206 put_byte(pb, 18); // tag type META
207 metadata_size_pos= url_ftell(pb);
208 put_be24(pb, 0); // size of data part (sum of all parts below)
209 put_be24(pb, 0); // time stamp
210 put_be32(pb, 0); // reserved
212 /* now data of data_size size */
214 /* first event name as a string */
215 put_byte(pb, AMF_DATA_TYPE_STRING);
216 put_amf_string(pb, "onMetaData"); // 12 bytes
218 /* mixed array (hash) with size and string/type/data tuples */
219 put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
220 put_be32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size
222 put_amf_string(pb, "duration");
223 flv->duration_offset= url_ftell(pb);
224 put_amf_double(pb, 0); // delayed write
227 put_amf_string(pb, "width");
228 put_amf_double(pb, video_enc->width);
230 put_amf_string(pb, "height");
231 put_amf_double(pb, video_enc->height);
233 put_amf_string(pb, "videodatarate");
234 put_amf_double(pb, video_enc->bit_rate / 1024.0);
236 put_amf_string(pb, "framerate");
237 put_amf_double(pb, framerate);
239 put_amf_string(pb, "videocodecid");
240 put_amf_double(pb, video_enc->codec_tag);
244 put_amf_string(pb, "audiodatarate");
245 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
247 put_amf_string(pb, "audiosamplerate");
248 put_amf_double(pb, audio_enc->sample_rate);
250 put_amf_string(pb, "audiosamplesize");
251 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
253 put_amf_string(pb, "stereo");
254 put_amf_bool(pb, audio_enc->channels == 2);
256 put_amf_string(pb, "audiocodecid");
257 put_amf_double(pb, audio_enc->codec_tag);
260 put_amf_string(pb, "filesize");
261 flv->filesize_offset= url_ftell(pb);
262 put_amf_double(pb, 0); // delayed write
264 put_amf_string(pb, "");
265 put_byte(pb, AMF_END_OF_OBJECT);
267 /* write total size of tag */
268 data_size= url_ftell(pb) - metadata_size_pos - 10;
269 url_fseek(pb, metadata_size_pos, SEEK_SET);
270 put_be24(pb, data_size);
271 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
272 put_be32(pb, data_size + 11);
274 for (i = 0; i < s->nb_streams; i++) {
275 AVCodecContext *enc = s->streams[i]->codec;
276 if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
278 put_byte(pb, enc->codec_type == CODEC_TYPE_VIDEO ?
279 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
280 put_be24(pb, 0); // size patched later
281 put_be24(pb, 0); // ts
282 put_byte(pb, 0); // ts ext
283 put_be24(pb, 0); // streamid
285 if (enc->codec_id == CODEC_ID_AAC) {
286 put_byte(pb, get_audio_flags(enc));
287 put_byte(pb, 0); // AAC sequence header
288 put_buffer(pb, enc->extradata, enc->extradata_size);
290 put_byte(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
291 put_byte(pb, 0); // AVC sequence header
292 put_be24(pb, 0); // composition time
293 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
295 data_size = url_ftell(pb) - pos;
296 url_fseek(pb, -data_size - 10, SEEK_CUR);
297 put_be24(pb, data_size);
298 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
299 put_be32(pb, data_size + 11); // previous tag size
306 static int flv_write_trailer(AVFormatContext *s)
310 ByteIOContext *pb = s->pb;
311 FLVContext *flv = s->priv_data;
313 file_size = url_ftell(pb);
315 /* update informations */
316 url_fseek(pb, flv->duration_offset, SEEK_SET);
317 put_amf_double(pb, flv->duration / (double)1000);
318 url_fseek(pb, flv->filesize_offset, SEEK_SET);
319 put_amf_double(pb, file_size);
321 url_fseek(pb, file_size, SEEK_SET);
325 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
327 ByteIOContext *pb = s->pb;
328 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
329 FLVContext *flv = s->priv_data;
333 int flags, flags_size;
335 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
337 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
338 enc->codec_id == CODEC_ID_AAC)
340 else if(enc->codec_id == CODEC_ID_H264)
345 if (enc->codec_type == CODEC_TYPE_VIDEO) {
346 put_byte(pb, FLV_TAG_TYPE_VIDEO);
348 flags = enc->codec_tag;
350 av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
354 flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
356 assert(enc->codec_type == CODEC_TYPE_AUDIO);
357 flags = get_audio_flags(enc);
361 put_byte(pb, FLV_TAG_TYPE_AUDIO);
364 if (enc->codec_id == CODEC_ID_H264) {
365 /* check if extradata looks like mp4 formated */
366 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
367 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
370 if (!flv->delay && pkt->dts < 0)
371 flv->delay = -pkt->dts;
374 ts = pkt->dts + flv->delay; // add delay to force positive dts
375 put_be24(pb,size + flags_size);
377 put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
378 put_be24(pb,flv->reserved);
380 if (enc->codec_id == CODEC_ID_VP6)
382 if (enc->codec_id == CODEC_ID_VP6F)
383 put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
384 else if (enc->codec_id == CODEC_ID_AAC)
385 put_byte(pb,1); // AAC raw
386 else if (enc->codec_id == CODEC_ID_H264) {
387 put_byte(pb,1); // AVC NALU
388 put_be24(pb,pkt->pts - pkt->dts);
391 put_buffer(pb, data ? data : pkt->data, size);
393 put_be32(pb,size+flags_size+11); // previous tag size
394 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
396 put_flush_packet(pb);
403 AVOutputFormat flv_muxer = {
405 NULL_IF_CONFIG_SMALL("FLV format"),
409 #if CONFIG_LIBMP3LAME
411 #else // CONFIG_LIBMP3LAME
413 #endif // CONFIG_LIBMP3LAME
418 .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
419 .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,