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_VP6F, FLV_CODECID_VP6 },
33 {CODEC_ID_VP6, FLV_CODECID_VP6 },
34 {CODEC_ID_H264, FLV_CODECID_H264 },
38 static const AVCodecTag flv_audio_codec_ids[] = {
39 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
40 {CODEC_ID_PCM_S8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
41 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
42 {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
43 {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
44 {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
45 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
46 {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
50 typedef struct FLVContext {
52 int64_t duration_offset;
53 int64_t filesize_offset;
55 int delay; ///< first dts delay for AVC
58 static int get_audio_flags(AVCodecContext *enc){
59 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
61 if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
62 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
63 else if (enc->codec_id == CODEC_ID_SPEEX) {
64 if (enc->sample_rate != 16000) {
65 av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
68 if (enc->channels != 1) {
69 av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
72 if (enc->frame_size / 320 > 8) {
73 av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
74 "8 frames per packet. Adobe Flash "
75 "Player cannot handle this!\n");
77 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
79 switch (enc->sample_rate) {
81 flags |= FLV_SAMPLERATE_44100HZ;
84 flags |= FLV_SAMPLERATE_22050HZ;
87 flags |= FLV_SAMPLERATE_11025HZ;
89 case 8000: //nellymoser only
91 if(enc->codec_id != CODEC_ID_MP3){
92 flags |= FLV_SAMPLERATE_SPECIAL;
96 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
101 if (enc->channels > 1) {
105 switch(enc->codec_id){
107 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
109 case CODEC_ID_PCM_S8:
110 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
112 case CODEC_ID_PCM_S16BE:
113 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
115 case CODEC_ID_PCM_S16LE:
116 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
118 case CODEC_ID_ADPCM_SWF:
119 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
121 case CODEC_ID_NELLYMOSER:
122 if (enc->sample_rate == 8000) {
123 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
125 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
129 flags |= enc->codec_tag<<4;
132 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
139 static void put_amf_string(ByteIOContext *pb, const char *str)
141 size_t len = strlen(str);
143 put_buffer(pb, str, len);
146 static void put_amf_double(ByteIOContext *pb, double d)
148 put_byte(pb, AMF_DATA_TYPE_NUMBER);
149 put_be64(pb, av_dbl2int(d));
152 static void put_amf_bool(ByteIOContext *pb, int b) {
153 put_byte(pb, AMF_DATA_TYPE_BOOL);
157 static int flv_write_header(AVFormatContext *s)
159 ByteIOContext *pb = s->pb;
160 FLVContext *flv = s->priv_data;
161 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
163 double framerate = 0.0;
164 int metadata_size_pos, data_size;
166 for(i=0; i<s->nb_streams; i++){
167 AVCodecContext *enc = s->streams[i]->codec;
168 if (enc->codec_type == CODEC_TYPE_VIDEO) {
169 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
170 framerate = av_q2d(s->streams[i]->r_frame_rate);
172 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
175 if(enc->codec_tag == 0) {
176 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
181 if(get_audio_flags(enc)<0)
184 av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
188 put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
189 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
193 for(i=0; i<s->nb_streams; i++){
194 if(s->streams[i]->codec->codec_tag == 5){
195 put_byte(pb,8); // message type
196 put_be24(pb,0); // include flags
197 put_be24(pb,0); // time stamp
198 put_be32(pb,0); // reserved
199 put_be32(pb,11); // size
205 put_byte(pb, 18); // tag type META
206 metadata_size_pos= url_ftell(pb);
207 put_be24(pb, 0); // size of data part (sum of all parts below)
208 put_be24(pb, 0); // time stamp
209 put_be32(pb, 0); // reserved
211 /* now data of data_size size */
213 /* first event name as a string */
214 put_byte(pb, AMF_DATA_TYPE_STRING);
215 put_amf_string(pb, "onMetaData"); // 12 bytes
217 /* mixed array (hash) with size and string/type/data tuples */
218 put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
219 put_be32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size
221 put_amf_string(pb, "duration");
222 flv->duration_offset= url_ftell(pb);
223 put_amf_double(pb, 0); // delayed write
226 put_amf_string(pb, "width");
227 put_amf_double(pb, video_enc->width);
229 put_amf_string(pb, "height");
230 put_amf_double(pb, video_enc->height);
232 put_amf_string(pb, "videodatarate");
233 put_amf_double(pb, video_enc->bit_rate / 1024.0);
235 put_amf_string(pb, "framerate");
236 put_amf_double(pb, framerate);
238 put_amf_string(pb, "videocodecid");
239 put_amf_double(pb, video_enc->codec_tag);
243 put_amf_string(pb, "audiodatarate");
244 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
246 put_amf_string(pb, "audiosamplerate");
247 put_amf_double(pb, audio_enc->sample_rate);
249 put_amf_string(pb, "audiosamplesize");
250 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_S8 ? 8 : 16);
252 put_amf_string(pb, "stereo");
253 put_amf_bool(pb, audio_enc->channels == 2);
255 put_amf_string(pb, "audiocodecid");
256 put_amf_double(pb, audio_enc->codec_tag);
259 put_amf_string(pb, "filesize");
260 flv->filesize_offset= url_ftell(pb);
261 put_amf_double(pb, 0); // delayed write
263 put_amf_string(pb, "");
264 put_byte(pb, AMF_END_OF_OBJECT);
266 /* write total size of tag */
267 data_size= url_ftell(pb) - metadata_size_pos - 10;
268 url_fseek(pb, metadata_size_pos, SEEK_SET);
269 put_be24(pb, data_size);
270 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
271 put_be32(pb, data_size + 11);
273 for (i = 0; i < s->nb_streams; i++) {
274 AVCodecContext *enc = s->streams[i]->codec;
275 if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
277 put_byte(pb, enc->codec_type == CODEC_TYPE_VIDEO ?
278 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
279 put_be24(pb, 0); // size patched later
280 put_be24(pb, 0); // ts
281 put_byte(pb, 0); // ts ext
282 put_be24(pb, 0); // streamid
284 if (enc->codec_id == CODEC_ID_AAC) {
285 put_byte(pb, get_audio_flags(enc));
286 put_byte(pb, 0); // AAC sequence header
287 put_buffer(pb, enc->extradata, enc->extradata_size);
289 put_byte(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
290 put_byte(pb, 0); // AVC sequence header
291 put_be24(pb, 0); // composition time
292 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
294 data_size = url_ftell(pb) - pos;
295 url_fseek(pb, -data_size - 10, SEEK_CUR);
296 put_be24(pb, data_size);
297 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
298 put_be32(pb, data_size + 11); // previous tag size
305 static int flv_write_trailer(AVFormatContext *s)
309 ByteIOContext *pb = s->pb;
310 FLVContext *flv = s->priv_data;
312 file_size = url_ftell(pb);
314 /* update informations */
315 url_fseek(pb, flv->duration_offset, SEEK_SET);
316 put_amf_double(pb, flv->duration / (double)1000);
317 url_fseek(pb, flv->filesize_offset, SEEK_SET);
318 put_amf_double(pb, file_size);
320 url_fseek(pb, file_size, SEEK_SET);
324 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
326 ByteIOContext *pb = s->pb;
327 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
328 FLVContext *flv = s->priv_data;
332 int flags, flags_size;
334 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
336 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
337 enc->codec_id == CODEC_ID_AAC)
339 else if(enc->codec_id == CODEC_ID_H264)
344 if (enc->codec_type == CODEC_TYPE_VIDEO) {
345 put_byte(pb, FLV_TAG_TYPE_VIDEO);
347 flags = enc->codec_tag;
349 av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
353 flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
355 assert(enc->codec_type == CODEC_TYPE_AUDIO);
356 flags = get_audio_flags(enc);
360 put_byte(pb, FLV_TAG_TYPE_AUDIO);
363 if (enc->codec_id == CODEC_ID_H264) {
364 /* check if extradata looks like mp4 formated */
365 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
366 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
369 if (!flv->delay && pkt->dts < 0)
370 flv->delay = -pkt->dts;
373 ts = pkt->dts + flv->delay; // add delay to force positive dts
374 put_be24(pb,size + flags_size);
376 put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
377 put_be24(pb,flv->reserved);
379 if (enc->codec_id == CODEC_ID_VP6)
381 if (enc->codec_id == CODEC_ID_VP6F)
382 put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
383 else if (enc->codec_id == CODEC_ID_AAC)
384 put_byte(pb,1); // AAC raw
385 else if (enc->codec_id == CODEC_ID_H264) {
386 put_byte(pb,1); // AVC NALU
387 put_be24(pb,pkt->pts - pkt->dts);
390 put_buffer(pb, data ? data : pkt->data, size);
392 put_be32(pb,size+flags_size+11); // previous tag size
393 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
395 put_flush_packet(pb);
402 AVOutputFormat flv_muxer = {
404 NULL_IF_CONFIG_SMALL("FLV format"),
408 #if CONFIG_LIBMP3LAME
410 #else // CONFIG_LIBMP3LAME
412 #endif // CONFIG_LIBMP3LAME
417 .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
418 .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,