3 * Copyright (c) 2003 The FFmpeg Project.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 typedef struct FLVContext {
30 static int get_audio_flags(AVCodecContext *enc){
31 int flags = (enc->bits_per_sample == 16) ? 0x2 : 0x0;
33 switch (enc->sample_rate) {
43 case 8000: //nellymoser only
48 av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
52 if (enc->channels > 1) {
56 switch(enc->codec_id){
62 case CODEC_ID_PCM_S16BE:
65 case CODEC_ID_PCM_S16LE:
68 case CODEC_ID_ADPCM_SWF:
72 flags |= enc->codec_tag<<4;
75 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
86 #define AMF_MIXED_ARRAY 8
90 static void put_amf_string(ByteIOContext *pb, const char *str)
92 size_t len = strlen(str);
94 put_buffer(pb, str, len);
97 static void put_amf_double(ByteIOContext *pb, double d)
99 put_byte(pb, AMF_DOUBLE);
100 put_be64(pb, av_dbl2int(d));
103 static int flv_write_header(AVFormatContext *s)
105 ByteIOContext *pb = &s->pb;
106 FLVContext *flv = s->priv_data;
107 int i, width, height, samplerate;
108 double framerate = 0.0;
109 int metadata_size_pos, data_size;
116 put_byte(pb,0); // delayed write
120 for(i=0; i<s->nb_streams; i++){
121 AVCodecContext *enc = s->streams[i]->codec;
122 if (enc->codec_type == CODEC_TYPE_VIDEO) {
124 height = enc->height;
125 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
126 framerate = av_q2d(s->streams[i]->r_frame_rate);
128 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
133 samplerate = enc->sample_rate;
135 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
136 if(enc->codec_tag == 5){
137 put_byte(pb,8); // message type
138 put_be24(pb,0); // include flags
139 put_be24(pb,0); // time stamp
140 put_be32(pb,0); // reserved
141 put_be32(pb,11); // size
144 if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
149 put_byte(pb, 18); // tag type META
150 metadata_size_pos= url_ftell(pb);
151 put_be24(pb, 0); // size of data part (sum of all parts below)
152 put_be24(pb, 0); // time stamp
153 put_be32(pb, 0); // reserved
155 /* now data of data_size size */
157 /* first event name as a string */
158 put_byte(pb, AMF_STRING); // 1 byte
159 put_amf_string(pb, "onMetaData"); // 12 bytes
161 /* mixed array (hash) with size and string/type/data tuples */
162 put_byte(pb, AMF_MIXED_ARRAY);
163 put_be32(pb, 4*flv->hasVideo + flv->hasAudio + (!!s->file_size) + (s->duration != AV_NOPTS_VALUE && s->duration));
165 if (s->duration != AV_NOPTS_VALUE && s->duration) {
166 put_amf_string(pb, "duration");
167 put_amf_double(pb, s->duration / (double)AV_TIME_BASE);
171 put_amf_string(pb, "width");
172 put_amf_double(pb, width);
174 put_amf_string(pb, "height");
175 put_amf_double(pb, height);
177 put_amf_string(pb, "videodatarate");
178 put_amf_double(pb, s->bit_rate / 1024.0);
180 put_amf_string(pb, "framerate");
181 put_amf_double(pb, framerate);
185 put_amf_string(pb, "audiosamplerate");
186 put_amf_double(pb, samplerate);
190 put_amf_string(pb, "filesize");
191 put_amf_double(pb, s->file_size);
194 put_amf_string(pb, "");
195 put_byte(pb, 9); // end marker 1 byte
197 /* write total size of tag */
198 data_size= url_ftell(pb) - metadata_size_pos - 10;
199 url_fseek(pb, metadata_size_pos, SEEK_SET);
200 put_be24(pb, data_size);
201 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
202 put_be32(pb, data_size + 11);
207 static int flv_write_trailer(AVFormatContext *s)
212 ByteIOContext *pb = &s->pb;
213 FLVContext *flv = s->priv_data;
215 file_size = url_ftell(pb);
216 flags |= flv->hasAudio ? 4 : 0;
217 flags |= flv->hasVideo ? 1 : 0;
218 url_fseek(pb, 4, SEEK_SET);
220 url_fseek(pb, file_size, SEEK_SET);
224 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
226 ByteIOContext *pb = &s->pb;
227 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
228 FLVContext *flv = s->priv_data;
232 // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
234 if (enc->codec_type == CODEC_TYPE_VIDEO) {
236 flags = 2; // choose h263
237 flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
239 assert(enc->codec_type == CODEC_TYPE_AUDIO);
240 flags = get_audio_flags(enc);
247 put_be24(pb,size+1); // include flags
248 put_be24(pb,pkt->pts);
249 put_be32(pb,flv->reserved);
251 put_buffer(pb, pkt->data, size);
252 put_be32(pb,size+1+11); // previous tag size
254 put_flush_packet(pb);
258 AVOutputFormat flv_muxer = {
264 #ifdef CONFIG_MP3LAME
266 #else // CONFIG_MP3LAME
268 #endif // CONFIG_MP3LAME