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
27 typedef struct FLVContext {
31 offset_t duration_offset;
32 offset_t filesize_offset;
36 static int get_audio_flags(AVCodecContext *enc){
37 int flags = (enc->bits_per_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
39 switch (enc->sample_rate) {
41 flags |= FLV_SAMPLERATE_44100HZ;
44 flags |= FLV_SAMPLERATE_22050HZ;
47 flags |= FLV_SAMPLERATE_11025HZ;
49 case 8000: //nellymoser only
51 flags |= FLV_SAMPLERATE_SPECIAL;
54 av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
58 if (enc->channels > 1) {
62 switch(enc->codec_id){
64 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
67 flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_8BIT;
69 case CODEC_ID_PCM_S16BE:
70 flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_16BIT;
72 case CODEC_ID_PCM_S16LE:
73 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
75 case CODEC_ID_ADPCM_SWF:
76 flags |= FLV_CODECID_ADPCM;
79 flags |= enc->codec_tag<<4;
82 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
89 static void put_amf_string(ByteIOContext *pb, const char *str)
91 size_t len = strlen(str);
93 put_buffer(pb, str, len);
96 static void put_amf_double(ByteIOContext *pb, double d)
98 put_byte(pb, AMF_DATA_TYPE_NUMBER);
99 put_be64(pb, av_dbl2int(d));
102 static int flv_write_header(AVFormatContext *s)
104 ByteIOContext *pb = &s->pb;
105 FLVContext *flv = s->priv_data;
106 int i, width, height, samplerate;
107 double framerate = 0.0;
108 int metadata_size_pos, data_size;
115 put_byte(pb,0); // delayed write
119 for(i=0; i<s->nb_streams; i++){
120 AVCodecContext *enc = s->streams[i]->codec;
121 if (enc->codec_type == CODEC_TYPE_VIDEO) {
123 height = enc->height;
124 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
125 framerate = av_q2d(s->streams[i]->r_frame_rate);
127 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
132 samplerate = enc->sample_rate;
134 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
135 if(enc->codec_tag == 5){
136 put_byte(pb,8); // message type
137 put_be24(pb,0); // include flags
138 put_be24(pb,0); // time stamp
139 put_be32(pb,0); // reserved
140 put_be32(pb,11); // size
143 if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
148 put_byte(pb, 18); // tag type META
149 metadata_size_pos= url_ftell(pb);
150 put_be24(pb, 0); // size of data part (sum of all parts below)
151 put_be24(pb, 0); // time stamp
152 put_be32(pb, 0); // reserved
154 /* now data of data_size size */
156 /* first event name as a string */
157 put_byte(pb, AMF_DATA_TYPE_STRING);
158 put_amf_string(pb, "onMetaData"); // 12 bytes
160 /* mixed array (hash) with size and string/type/data tuples */
161 put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
162 put_be32(pb, 4*flv->hasVideo + flv->hasAudio + 2); // +2 for duration and file size
164 put_amf_string(pb, "duration");
165 flv->duration_offset= url_ftell(pb);
166 put_amf_double(pb, 0); // delayed write
169 put_amf_string(pb, "width");
170 put_amf_double(pb, width);
172 put_amf_string(pb, "height");
173 put_amf_double(pb, height);
175 put_amf_string(pb, "videodatarate");
176 put_amf_double(pb, s->bit_rate / 1024.0);
178 put_amf_string(pb, "framerate");
179 put_amf_double(pb, framerate);
183 put_amf_string(pb, "audiosamplerate");
184 put_amf_double(pb, samplerate);
187 put_amf_string(pb, "filesize");
188 flv->filesize_offset= url_ftell(pb);
189 put_amf_double(pb, 0); // delayed write
191 put_amf_string(pb, "");
192 put_byte(pb, AMF_END_OF_OBJECT);
194 /* write total size of tag */
195 data_size= url_ftell(pb) - metadata_size_pos - 10;
196 url_fseek(pb, metadata_size_pos, SEEK_SET);
197 put_be24(pb, data_size);
198 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
199 put_be32(pb, data_size + 11);
204 static int flv_write_trailer(AVFormatContext *s)
209 ByteIOContext *pb = &s->pb;
210 FLVContext *flv = s->priv_data;
212 file_size = url_ftell(pb);
213 flags |= flv->hasAudio ? FLV_HEADER_FLAG_HASAUDIO : 0;
214 flags |= flv->hasVideo ? FLV_HEADER_FLAG_HASVIDEO : 0;
215 url_fseek(pb, 4, SEEK_SET);
218 /* update informations */
219 url_fseek(pb, flv->duration_offset, SEEK_SET);
220 put_amf_double(pb, flv->duration / (double)1000);
221 url_fseek(pb, flv->filesize_offset, SEEK_SET);
222 put_amf_double(pb, file_size);
224 url_fseek(pb, file_size, SEEK_SET);
228 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
230 ByteIOContext *pb = &s->pb;
231 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
232 FLVContext *flv = s->priv_data;
236 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
238 if (enc->codec_type == CODEC_TYPE_VIDEO) {
239 put_byte(pb, FLV_TAG_TYPE_VIDEO);
240 flags = FLV_CODECID_H263;
241 flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
243 assert(enc->codec_type == CODEC_TYPE_AUDIO);
244 flags = get_audio_flags(enc);
248 put_byte(pb, FLV_TAG_TYPE_AUDIO);
251 put_be24(pb,size+1); // include flags
252 put_be24(pb,pkt->pts);
253 put_be32(pb,flv->reserved);
255 put_buffer(pb, pkt->data, size);
256 put_be32(pb,size+1+11); // previous tag size
257 flv->duration = pkt->pts + pkt->duration;
259 put_flush_packet(pb);
263 AVOutputFormat flv_muxer = {
269 #ifdef CONFIG_MP3LAME
271 #else // CONFIG_MP3LAME
273 #endif // CONFIG_MP3LAME