aac chan config is 0 if bitstream contains program_config_element
[libav.git] / libavformat / flvenc.c
CommitLineData
d4f5d74a 1/*
7fbde343 2 * FLV muxer
d4f5d74a
GM
3 * Copyright (c) 2003 The FFmpeg Project.
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
d4f5d74a
GM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
d4f5d74a 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
d4f5d74a
GM
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d4f5d74a
GM
20 */
21#include "avformat.h"
6cac3a3b 22#include "flv.h"
148c9bdb 23#include "riff.h"
d4f5d74a 24
068f2a22
MN
25#undef NDEBUG
26#include <assert.h>
27
7caf0cc6 28static const AVCodecTag flv_video_codec_ids[] = {
148c9bdb
AH
29 {CODEC_ID_FLV1, FLV_CODECID_H263 },
30 {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
31 {CODEC_ID_VP6F, FLV_CODECID_VP6 },
09d8c0ae 32 {CODEC_ID_VP6, FLV_CODECID_VP6 },
148c9bdb
AH
33 {CODEC_ID_NONE, 0}
34};
35
7caf0cc6 36static const AVCodecTag flv_audio_codec_ids[] = {
148c9bdb 37 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
44de39f9
MN
38 {CODEC_ID_PCM_S8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
39 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
148c9bdb
AH
40 {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
41 {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
b7d1cd02 42 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
148c9bdb
AH
43 {CODEC_ID_NONE, 0}
44};
45
d4f5d74a 46typedef struct FLVContext {
11a8e425 47 int reserved;
634b8cfa
BC
48 offset_t duration_offset;
49 offset_t filesize_offset;
50 int64_t duration;
d4f5d74a
GM
51} FLVContext;
52
37cdf93d 53static int get_audio_flags(AVCodecContext *enc){
6cac3a3b 54 int flags = (enc->bits_per_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
37cdf93d
MN
55
56 switch (enc->sample_rate) {
57 case 44100:
6cac3a3b 58 flags |= FLV_SAMPLERATE_44100HZ;
37cdf93d
MN
59 break;
60 case 22050:
6cac3a3b 61 flags |= FLV_SAMPLERATE_22050HZ;
37cdf93d
MN
62 break;
63 case 11025:
6cac3a3b 64 flags |= FLV_SAMPLERATE_11025HZ;
37cdf93d
MN
65 break;
66 case 8000: //nellymoser only
67 case 5512: //not mp3
4838727e 68 if(enc->codec_id != CODEC_ID_MP3){
ec627278
MN
69 flags |= FLV_SAMPLERATE_SPECIAL;
70 break;
4838727e 71 }
37cdf93d 72 default:
755bfeab 73 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
37cdf93d
MN
74 return -1;
75 }
76
77 if (enc->channels > 1) {
6cac3a3b 78 flags |= FLV_STEREO;
37cdf93d 79 }
115329f1 80
37cdf93d
MN
81 switch(enc->codec_id){
82 case CODEC_ID_MP3:
6cac3a3b 83 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
37cdf93d 84 break;
923bd441 85 case CODEC_ID_PCM_S8:
44de39f9 86 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
bb270c08 87 break;
923bd441 88 case CODEC_ID_PCM_S16BE:
44de39f9 89 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
bb270c08 90 break;
923bd441 91 case CODEC_ID_PCM_S16LE:
6cac3a3b 92 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
bb270c08 93 break;
2fde8aae 94 case CODEC_ID_ADPCM_SWF:
e9509536 95 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
bb270c08 96 break;
b7d1cd02
BW
97 case CODEC_ID_NELLYMOSER:
98 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
99 break;
37cdf93d
MN
100 case 0:
101 flags |= enc->codec_tag<<4;
102 break;
103 default:
a254c574 104 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
37cdf93d
MN
105 return -1;
106 }
115329f1 107
37cdf93d
MN
108 return flags;
109}
110
fd0fb306
MN
111static void put_amf_string(ByteIOContext *pb, const char *str)
112{
113 size_t len = strlen(str);
114 put_be16(pb, len);
115 put_buffer(pb, str, len);
116}
117
118static void put_amf_double(ByteIOContext *pb, double d)
119{
6cac3a3b 120 put_byte(pb, AMF_DATA_TYPE_NUMBER);
fd0fb306
MN
121 put_be64(pb, av_dbl2int(d));
122}
123
148c9bdb
AH
124static void put_amf_bool(ByteIOContext *pb, int b) {
125 put_byte(pb, AMF_DATA_TYPE_BOOL);
126 put_byte(pb, !!b);
127}
128
d4f5d74a
GM
129static int flv_write_header(AVFormatContext *s)
130{
899681cd 131 ByteIOContext *pb = s->pb;
d4f5d74a 132 FLVContext *flv = s->priv_data;
5366f15d
BC
133 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
134 int i;
fd0fb306
MN
135 double framerate = 0.0;
136 int metadata_size_pos, data_size;
d4f5d74a 137
11a8e425 138 for(i=0; i<s->nb_streams; i++){
01f4895c 139 AVCodecContext *enc = s->streams[i]->codec;
fd0fb306 140 if (enc->codec_type == CODEC_TYPE_VIDEO) {
fd0fb306
MN
141 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
142 framerate = av_q2d(s->streams[i]->r_frame_rate);
143 } else {
144 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
145 }
5366f15d
BC
146 video_enc = enc;
147 if(enc->codec_tag == 0) {
148c9bdb
AH
148 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
149 return -1;
150 }
fd0fb306 151 } else {
5366f15d 152 audio_enc = enc;
c45388b1
MN
153 if(get_audio_flags(enc)<0)
154 return -1;
fd0fb306 155 }
254629b1 156 av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
c45388b1
MN
157 }
158 put_tag(pb,"FLV");
159 put_byte(pb,1);
5366f15d
BC
160 put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
161 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
c45388b1
MN
162 put_be32(pb,9);
163 put_be32(pb,0);
164
165 for(i=0; i<s->nb_streams; i++){
166 if(s->streams[i]->codec->codec_tag == 5){
11a8e425
MN
167 put_byte(pb,8); // message type
168 put_be24(pb,0); // include flags
169 put_be24(pb,0); // time stamp
170 put_be32(pb,0); // reserved
171 put_be32(pb,11); // size
172 flv->reserved=5;
173 }
174 }
d4f5d74a 175
fd0fb306
MN
176 /* write meta_tag */
177 put_byte(pb, 18); // tag type META
178 metadata_size_pos= url_ftell(pb);
179 put_be24(pb, 0); // size of data part (sum of all parts below)
180 put_be24(pb, 0); // time stamp
181 put_be32(pb, 0); // reserved
182
183 /* now data of data_size size */
184
185 /* first event name as a string */
6cac3a3b 186 put_byte(pb, AMF_DATA_TYPE_STRING);
fd0fb306
MN
187 put_amf_string(pb, "onMetaData"); // 12 bytes
188
189 /* mixed array (hash) with size and string/type/data tuples */
6cac3a3b 190 put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
5366f15d 191 put_be32(pb, 5*!!video_enc + 4*!!audio_enc + 2); // +2 for duration and file size
fd0fb306 192
634b8cfa
BC
193 put_amf_string(pb, "duration");
194 flv->duration_offset= url_ftell(pb);
195 put_amf_double(pb, 0); // delayed write
fd0fb306 196
5366f15d 197 if(video_enc){
fd0fb306 198 put_amf_string(pb, "width");
5366f15d 199 put_amf_double(pb, video_enc->width);
fd0fb306
MN
200
201 put_amf_string(pb, "height");
5366f15d 202 put_amf_double(pb, video_enc->height);
fd0fb306
MN
203
204 put_amf_string(pb, "videodatarate");
205 put_amf_double(pb, s->bit_rate / 1024.0);
206
207 put_amf_string(pb, "framerate");
208 put_amf_double(pb, framerate);
148c9bdb
AH
209
210 put_amf_string(pb, "videocodecid");
5366f15d 211 put_amf_double(pb, video_enc->codec_tag);
fd0fb306
MN
212 }
213
5366f15d 214 if(audio_enc){
fd0fb306 215 put_amf_string(pb, "audiosamplerate");
5366f15d 216 put_amf_double(pb, audio_enc->sample_rate);
148c9bdb
AH
217
218 put_amf_string(pb, "audiosamplesize");
5366f15d 219 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_S8 ? 8 : 16);
148c9bdb
AH
220
221 put_amf_string(pb, "stereo");
5366f15d 222 put_amf_bool(pb, audio_enc->channels == 2);
148c9bdb
AH
223
224 put_amf_string(pb, "audiocodecid");
5366f15d 225 put_amf_double(pb, audio_enc->codec_tag);
fd0fb306
MN
226 }
227
634b8cfa
BC
228 put_amf_string(pb, "filesize");
229 flv->filesize_offset= url_ftell(pb);
230 put_amf_double(pb, 0); // delayed write
fd0fb306
MN
231
232 put_amf_string(pb, "");
6cac3a3b 233 put_byte(pb, AMF_END_OF_OBJECT);
fd0fb306
MN
234
235 /* write total size of tag */
236 data_size= url_ftell(pb) - metadata_size_pos - 10;
237 url_fseek(pb, metadata_size_pos, SEEK_SET);
238 put_be24(pb, data_size);
239 url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
240 put_be32(pb, data_size + 11);
241
d4f5d74a
GM
242 return 0;
243}
244
d4f5d74a
GM
245static int flv_write_trailer(AVFormatContext *s)
246{
14b32253 247 int64_t file_size;
14b32253 248
899681cd 249 ByteIOContext *pb = s->pb;
d4f5d74a
GM
250 FLVContext *flv = s->priv_data;
251
14b32253 252 file_size = url_ftell(pb);
634b8cfa
BC
253
254 /* update informations */
255 url_fseek(pb, flv->duration_offset, SEEK_SET);
256 put_amf_double(pb, flv->duration / (double)1000);
257 url_fseek(pb, flv->filesize_offset, SEEK_SET);
258 put_amf_double(pb, file_size);
259
d4f5d74a
GM
260 url_fseek(pb, file_size, SEEK_SET);
261 return 0;
262}
263
e928649b 264static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
d4f5d74a 265{
899681cd 266 ByteIOContext *pb = s->pb;
01f4895c 267 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
d4f5d74a 268 FLVContext *flv = s->priv_data;
e928649b 269 int size= pkt->size;
f683dbdc 270 int flags, flags_size;
d4f5d74a 271
949b1a13 272// av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
115329f1 273
f683dbdc
MN
274 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F)
275 flags_size= 2;
276 else
277 flags_size= 1;
278
d4f5d74a 279 if (enc->codec_type == CODEC_TYPE_VIDEO) {
6cac3a3b 280 put_byte(pb, FLV_TAG_TYPE_VIDEO);
09d8c0ae 281
bb85077f 282 flags = enc->codec_tag;
09d8c0ae
BL
283 if(flags == 0) {
284 av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
285 return -1;
286 }
287
6cac3a3b 288 flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
75293f05
MN
289 } else {
290 assert(enc->codec_type == CODEC_TYPE_AUDIO);
37cdf93d 291 flags = get_audio_flags(enc);
115329f1 292
068f2a22 293 assert(size);
068f2a22 294
6cac3a3b 295 put_byte(pb, FLV_TAG_TYPE_AUDIO);
75293f05
MN
296 }
297
f683dbdc 298 put_be24(pb,size + flags_size);
75293f05 299 put_be24(pb,pkt->pts);
018b6fb4
AB
300 put_byte(pb,pkt->pts >> 24);
301 put_be24(pb,flv->reserved);
75293f05 302 put_byte(pb,flags);
09d8c0ae
BL
303 if (enc->codec_id == CODEC_ID_VP6)
304 put_byte(pb,0);
305 if (enc->codec_id == CODEC_ID_VP6F)
306 put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
75293f05 307 put_buffer(pb, pkt->data, size);
f683dbdc 308 put_be32(pb,size+flags_size+11); // previous tag size
a9ee86e7 309 flv->duration = FFMAX(flv->duration, pkt->pts + pkt->duration);
115329f1 310
d4f5d74a
GM
311 put_flush_packet(pb);
312 return 0;
313}
314
ff70e601 315AVOutputFormat flv_muxer = {
d4f5d74a
GM
316 "flv",
317 "flv format",
e817a73d 318 "video/x-flv",
d4f5d74a
GM
319 "flv",
320 sizeof(FLVContext),
6ebe07fb 321#ifdef CONFIG_LIBMP3LAME
80783dc2 322 CODEC_ID_MP3,
6ebe07fb 323#else // CONFIG_LIBMP3LAME
964ff354 324 CODEC_ID_ADPCM_SWF,
6ebe07fb 325#endif // CONFIG_LIBMP3LAME
d4f5d74a
GM
326 CODEC_ID_FLV1,
327 flv_write_header,
328 flv_write_packet,
329 flv_write_trailer,
6c77805f 330 .codec_tag= (const AVCodecTag*[]){flv_video_codec_ids, flv_audio_codec_ids, 0},
d4f5d74a 331};