fate: store last version in per-slot file
[libav.git] / libavformat / flvenc.c
CommitLineData
d4f5d74a 1/*
7fbde343 2 * FLV muxer
406792e7 3 * Copyright (c) 2003 The FFmpeg Project
d4f5d74a 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"
80b39e1c 23#include "internal.h"
f23496b5 24#include "avc.h"
d4f5d74a 25
068f2a22
MN
26#undef NDEBUG
27#include <assert.h>
28
7caf0cc6 29static const AVCodecTag flv_video_codec_ids[] = {
148c9bdb
AH
30 {CODEC_ID_FLV1, FLV_CODECID_H263 },
31 {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
1b88277b 32 {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
148c9bdb 33 {CODEC_ID_VP6F, FLV_CODECID_VP6 },
09d8c0ae 34 {CODEC_ID_VP6, FLV_CODECID_VP6 },
f23496b5 35 {CODEC_ID_H264, FLV_CODECID_H264 },
148c9bdb
AH
36 {CODEC_ID_NONE, 0}
37};
38
7caf0cc6 39static const AVCodecTag flv_audio_codec_ids[] = {
148c9bdb 40 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
8e9efe43 41 {CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
44de39f9 42 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
148c9bdb
AH
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},
f23496b5 45 {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
b7d1cd02 46 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
046c4001 47 {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
148c9bdb
AH
48 {CODEC_ID_NONE, 0}
49};
50
d4f5d74a 51typedef struct FLVContext {
11a8e425 52 int reserved;
bc5c918e
DB
53 int64_t duration_offset;
54 int64_t filesize_offset;
634b8cfa 55 int64_t duration;
f23496b5 56 int delay; ///< first dts delay for AVC
d4f5d74a
GM
57} FLVContext;
58
37cdf93d 59static int get_audio_flags(AVCodecContext *enc){
dd1c8f3e 60 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
37cdf93d 61
f23496b5
BC
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;
046c4001
JR
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");
67 return -1;
68 }
69 if (enc->channels != 1) {
70 av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
71 return -1;
72 }
73 if (enc->frame_size / 320 > 8) {
68677dd8
JR
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");
046c4001
JR
77 }
78 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
79 } else {
37cdf93d
MN
80 switch (enc->sample_rate) {
81 case 44100:
6cac3a3b 82 flags |= FLV_SAMPLERATE_44100HZ;
37cdf93d
MN
83 break;
84 case 22050:
6cac3a3b 85 flags |= FLV_SAMPLERATE_22050HZ;
37cdf93d
MN
86 break;
87 case 11025:
6cac3a3b 88 flags |= FLV_SAMPLERATE_11025HZ;
37cdf93d
MN
89 break;
90 case 8000: //nellymoser only
91 case 5512: //not mp3
4838727e 92 if(enc->codec_id != CODEC_ID_MP3){
ec627278
MN
93 flags |= FLV_SAMPLERATE_SPECIAL;
94 break;
4838727e 95 }
37cdf93d 96 default:
755bfeab 97 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
37cdf93d
MN
98 return -1;
99 }
f23496b5 100 }
37cdf93d
MN
101
102 if (enc->channels > 1) {
6cac3a3b 103 flags |= FLV_STEREO;
37cdf93d 104 }
115329f1 105
37cdf93d
MN
106 switch(enc->codec_id){
107 case CODEC_ID_MP3:
6cac3a3b 108 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
37cdf93d 109 break;
8e9efe43 110 case CODEC_ID_PCM_U8:
44de39f9 111 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
bb270c08 112 break;
923bd441 113 case CODEC_ID_PCM_S16BE:
44de39f9 114 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
bb270c08 115 break;
923bd441 116 case CODEC_ID_PCM_S16LE:
6cac3a3b 117 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
bb270c08 118 break;
2fde8aae 119 case CODEC_ID_ADPCM_SWF:
e9509536 120 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
bb270c08 121 break;
b7d1cd02 122 case CODEC_ID_NELLYMOSER:
8ddd280d
AW
123 if (enc->sample_rate == 8000) {
124 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
125 } else {
126 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
127 }
b7d1cd02 128 break;
37cdf93d
MN
129 case 0:
130 flags |= enc->codec_tag<<4;
131 break;
132 default:
a254c574 133 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
37cdf93d
MN
134 return -1;
135 }
115329f1 136
37cdf93d
MN
137 return flags;
138}
139
fd0fb306
MN
140static void put_amf_string(ByteIOContext *pb, const char *str)
141{
142 size_t len = strlen(str);
143 put_be16(pb, len);
144 put_buffer(pb, str, len);
145}
146
147static void put_amf_double(ByteIOContext *pb, double d)
148{
6cac3a3b 149 put_byte(pb, AMF_DATA_TYPE_NUMBER);
fd0fb306
MN
150 put_be64(pb, av_dbl2int(d));
151}
152
148c9bdb
AH
153static void put_amf_bool(ByteIOContext *pb, int b) {
154 put_byte(pb, AMF_DATA_TYPE_BOOL);
155 put_byte(pb, !!b);
156}
157
d4f5d74a
GM
158static int flv_write_header(AVFormatContext *s)
159{
899681cd 160 ByteIOContext *pb = s->pb;
d4f5d74a 161 FLVContext *flv = s->priv_data;
5366f15d
BC
162 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
163 int i;
fd0fb306
MN
164 double framerate = 0.0;
165 int metadata_size_pos, data_size;
d4f5d74a 166
11a8e425 167 for(i=0; i<s->nb_streams; i++){
01f4895c 168 AVCodecContext *enc = s->streams[i]->codec;
72415b2a 169 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
fd0fb306
MN
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);
172 } else {
173 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
174 }
5366f15d
BC
175 video_enc = enc;
176 if(enc->codec_tag == 0) {
148c9bdb
AH
177 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
178 return -1;
179 }
fd0fb306 180 } else {
5366f15d 181 audio_enc = enc;
c45388b1
MN
182 if(get_audio_flags(enc)<0)
183 return -1;
fd0fb306 184 }
254629b1 185 av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
c45388b1
MN
186 }
187 put_tag(pb,"FLV");
188 put_byte(pb,1);
5366f15d
BC
189 put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
190 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
c45388b1
MN
191 put_be32(pb,9);
192 put_be32(pb,0);
193
194 for(i=0; i<s->nb_streams; i++){
195 if(s->streams[i]->codec->codec_tag == 5){
11a8e425
MN
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
201 flv->reserved=5;
202 }
203 }
d4f5d74a 204
fd0fb306
MN
205 /* write meta_tag */
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
211
212 /* now data of data_size size */
213
214 /* first event name as a string */
6cac3a3b 215 put_byte(pb, AMF_DATA_TYPE_STRING);
fd0fb306
MN
216 put_amf_string(pb, "onMetaData"); // 12 bytes
217
218 /* mixed array (hash) with size and string/type/data tuples */
6cac3a3b 219 put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
3f3f3ba7 220 put_be32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size
fd0fb306 221
634b8cfa
BC
222 put_amf_string(pb, "duration");
223 flv->duration_offset= url_ftell(pb);
c5e1e982 224 put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
fd0fb306 225
5366f15d 226 if(video_enc){
fd0fb306 227 put_amf_string(pb, "width");
5366f15d 228 put_amf_double(pb, video_enc->width);
fd0fb306
MN
229
230 put_amf_string(pb, "height");
5366f15d 231 put_amf_double(pb, video_enc->height);
fd0fb306
MN
232
233 put_amf_string(pb, "videodatarate");
426a6f34 234 put_amf_double(pb, video_enc->bit_rate / 1024.0);
fd0fb306
MN
235
236 put_amf_string(pb, "framerate");
237 put_amf_double(pb, framerate);
148c9bdb
AH
238
239 put_amf_string(pb, "videocodecid");
5366f15d 240 put_amf_double(pb, video_enc->codec_tag);
fd0fb306
MN
241 }
242
5366f15d 243 if(audio_enc){
426a6f34
SK
244 put_amf_string(pb, "audiodatarate");
245 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
246
fd0fb306 247 put_amf_string(pb, "audiosamplerate");
5366f15d 248 put_amf_double(pb, audio_enc->sample_rate);
148c9bdb
AH
249
250 put_amf_string(pb, "audiosamplesize");
8e9efe43 251 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
148c9bdb
AH
252
253 put_amf_string(pb, "stereo");
5366f15d 254 put_amf_bool(pb, audio_enc->channels == 2);
148c9bdb
AH
255
256 put_amf_string(pb, "audiocodecid");
5366f15d 257 put_amf_double(pb, audio_enc->codec_tag);
fd0fb306
MN
258 }
259
634b8cfa
BC
260 put_amf_string(pb, "filesize");
261 flv->filesize_offset= url_ftell(pb);
262 put_amf_double(pb, 0); // delayed write
fd0fb306
MN
263
264 put_amf_string(pb, "");
6cac3a3b 265 put_byte(pb, AMF_END_OF_OBJECT);
fd0fb306
MN
266
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);
273
f23496b5
BC
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) {
bc5c918e 277 int64_t pos;
72415b2a 278 put_byte(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
f23496b5
BC
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
284 pos = url_ftell(pb);
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);
289 } else {
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);
294 }
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
300 }
301 }
302
d4f5d74a
GM
303 return 0;
304}
305
d4f5d74a
GM
306static int flv_write_trailer(AVFormatContext *s)
307{
14b32253 308 int64_t file_size;
14b32253 309
899681cd 310 ByteIOContext *pb = s->pb;
d4f5d74a
GM
311 FLVContext *flv = s->priv_data;
312
14b32253 313 file_size = url_ftell(pb);
634b8cfa
BC
314
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);
320
d4f5d74a
GM
321 url_fseek(pb, file_size, SEEK_SET);
322 return 0;
323}
324
e928649b 325static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
d4f5d74a 326{
899681cd 327 ByteIOContext *pb = s->pb;
01f4895c 328 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
d4f5d74a 329 FLVContext *flv = s->priv_data;
f23496b5 330 unsigned ts;
e928649b 331 int size= pkt->size;
71e685b0 332 uint8_t *data= NULL;
f683dbdc 333 int flags, flags_size;
d4f5d74a 334
949b1a13 335// av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
115329f1 336
f23496b5
BC
337 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
338 enc->codec_id == CODEC_ID_AAC)
f683dbdc 339 flags_size= 2;
f23496b5
BC
340 else if(enc->codec_id == CODEC_ID_H264)
341 flags_size= 5;
f683dbdc
MN
342 else
343 flags_size= 1;
344
72415b2a 345 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
6cac3a3b 346 put_byte(pb, FLV_TAG_TYPE_VIDEO);
09d8c0ae 347
bb85077f 348 flags = enc->codec_tag;
09d8c0ae
BL
349 if(flags == 0) {
350 av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
351 return -1;
352 }
353
cc947f04 354 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
75293f05 355 } else {
72415b2a 356 assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
37cdf93d 357 flags = get_audio_flags(enc);
115329f1 358
068f2a22 359 assert(size);
068f2a22 360
6cac3a3b 361 put_byte(pb, FLV_TAG_TYPE_AUDIO);
75293f05
MN
362 }
363
7d637efa 364 if (enc->codec_id == CODEC_ID_H264) {
71e685b0
BC
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)
368 return -1;
369 }
6f4eb12d
BC
370 if (!flv->delay && pkt->dts < 0)
371 flv->delay = -pkt->dts;
f23496b5
BC
372 }
373
374 ts = pkt->dts + flv->delay; // add delay to force positive dts
f683dbdc 375 put_be24(pb,size + flags_size);
f23496b5 376 put_be24(pb,ts);
b83a00b8 377 put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
018b6fb4 378 put_be24(pb,flv->reserved);
75293f05 379 put_byte(pb,flags);
09d8c0ae
BL
380 if (enc->codec_id == CODEC_ID_VP6)
381 put_byte(pb,0);
382 if (enc->codec_id == CODEC_ID_VP6F)
383 put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
f23496b5
BC
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
6f4eb12d 388 put_be24(pb,pkt->pts - pkt->dts);
f23496b5 389 }
71e685b0
BC
390
391 put_buffer(pb, data ? data : pkt->data, size);
392
f683dbdc 393 put_be32(pb,size+flags_size+11); // previous tag size
f23496b5 394 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
115329f1 395
d4f5d74a 396 put_flush_packet(pb);
71e685b0
BC
397
398 av_free(data);
399
d4f5d74a
GM
400 return 0;
401}
402
ff70e601 403AVOutputFormat flv_muxer = {
d4f5d74a 404 "flv",
bde15e74 405 NULL_IF_CONFIG_SMALL("FLV format"),
e817a73d 406 "video/x-flv",
d4f5d74a
GM
407 "flv",
408 sizeof(FLVContext),
b250f9c6 409#if CONFIG_LIBMP3LAME
80783dc2 410 CODEC_ID_MP3,
6ebe07fb 411#else // CONFIG_LIBMP3LAME
964ff354 412 CODEC_ID_ADPCM_SWF,
6ebe07fb 413#endif // CONFIG_LIBMP3LAME
d4f5d74a
GM
414 CODEC_ID_FLV1,
415 flv_write_header,
416 flv_write_packet,
417 flv_write_trailer,
c1854592 418 .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
e458dd0b 419 .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
d4f5d74a 420};