flv: Validate and reject unsupported codecs
[libav.git] / libavformat / flvenc.c
CommitLineData
d4f5d74a 1/*
7fbde343 2 * FLV muxer
2912e87a 3 * Copyright (c) 2003 The Libav Project
d4f5d74a 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav 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 *
2912e87a 12 * Libav 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
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d4f5d74a 20 */
ee8aecd2 21
1cb34ea4 22#include "libavutil/dict.h"
3383a53e 23#include "libavutil/intfloat.h"
1cb34ea4 24#include "avc.h"
d4f5d74a 25#include "avformat.h"
6cac3a3b 26#include "flv.h"
80b39e1c 27#include "internal.h"
16f82508 28#include "metadata.h"
d4f5d74a 29
068f2a22
MN
30#undef NDEBUG
31#include <assert.h>
32
7caf0cc6 33static const AVCodecTag flv_video_codec_ids[] = {
36ef5369
AK
34 { AV_CODEC_ID_FLV1, FLV_CODECID_H263 },
35 { AV_CODEC_ID_FLASHSV, FLV_CODECID_SCREEN },
36 { AV_CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2 },
37 { AV_CODEC_ID_VP6F, FLV_CODECID_VP6 },
be1e1373 38 { AV_CODEC_ID_VP6A, FLV_CODECID_VP6A },
36ef5369
AK
39 { AV_CODEC_ID_H264, FLV_CODECID_H264 },
40 { AV_CODEC_ID_NONE, 0 }
148c9bdb
AH
41};
42
7caf0cc6 43static const AVCodecTag flv_audio_codec_ids[] = {
36ef5369
AK
44 { AV_CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET },
45 { AV_CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
46 { AV_CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
47 { AV_CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET },
48 { AV_CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET },
49 { AV_CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET },
50 { AV_CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET },
51 { AV_CODEC_ID_PCM_MULAW, FLV_CODECID_PCM_MULAW >> FLV_AUDIO_CODECID_OFFSET },
52 { AV_CODEC_ID_PCM_ALAW, FLV_CODECID_PCM_ALAW >> FLV_AUDIO_CODECID_OFFSET },
53 { AV_CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET },
54 { AV_CODEC_ID_NONE, 0 }
148c9bdb
AH
55};
56
d4f5d74a 57typedef struct FLVContext {
1cb34ea4 58 int reserved;
bc5c918e
DB
59 int64_t duration_offset;
60 int64_t filesize_offset;
634b8cfa 61 int64_t duration;
905de119 62 int64_t delay; ///< first dts delay (needed for AVC & Speex)
4d0cd5f5
AS
63
64 AVCodecContext *audio_enc;
65 AVCodecContext *video_enc;
66 double framerate;
67 AVCodecContext *data_enc;
d4f5d74a
GM
68} FLVContext;
69
ef74e397 70typedef struct FLVStreamContext {
ef74e397
JR
71 int64_t last_ts; ///< last timestamp for each stream
72} FLVStreamContext;
73
0a3ad7ff
AK
74static int get_audio_flags(AVFormatContext *s, AVCodecContext *enc)
75{
1cb34ea4
LB
76 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT
77 : FLV_SAMPLESSIZE_8BIT;
37cdf93d 78
36ef5369 79 if (enc->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
1cb34ea4
LB
80 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ |
81 FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
36ef5369 82 else if (enc->codec_id == AV_CODEC_ID_SPEEX) {
046c4001 83 if (enc->sample_rate != 16000) {
1cb34ea4
LB
84 av_log(s, AV_LOG_ERROR,
85 "flv only supports wideband (16kHz) Speex audio\n");
046c4001
JR
86 return -1;
87 }
88 if (enc->channels != 1) {
0a3ad7ff 89 av_log(s, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
046c4001
JR
90 return -1;
91 }
046c4001
JR
92 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
93 } else {
1cb34ea4
LB
94 switch (enc->sample_rate) {
95 case 44100:
6cac3a3b 96 flags |= FLV_SAMPLERATE_44100HZ;
37cdf93d 97 break;
1cb34ea4 98 case 22050:
6cac3a3b 99 flags |= FLV_SAMPLERATE_22050HZ;
37cdf93d 100 break;
1cb34ea4 101 case 11025:
6cac3a3b 102 flags |= FLV_SAMPLERATE_11025HZ;
37cdf93d 103 break;
1cb34ea4
LB
104 case 16000: // nellymoser only
105 case 8000: // nellymoser only
106 case 5512: // not MP3
36ef5369 107 if (enc->codec_id != AV_CODEC_ID_MP3) {
ec627278
MN
108 flags |= FLV_SAMPLERATE_SPECIAL;
109 break;
4838727e 110 }
37cdf93d 111 default:
1cb34ea4
LB
112 av_log(s, AV_LOG_ERROR,
113 "flv does not support that sample rate, "
114 "choose from (44100, 22050, 11025).\n");
37cdf93d 115 return -1;
1cb34ea4 116 }
f23496b5 117 }
37cdf93d 118
1cb34ea4 119 if (enc->channels > 1)
6cac3a3b 120 flags |= FLV_STEREO;
115329f1 121
1cb34ea4 122 switch (enc->codec_id) {
36ef5369 123 case AV_CODEC_ID_MP3:
6cac3a3b 124 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
37cdf93d 125 break;
36ef5369 126 case AV_CODEC_ID_PCM_U8:
44de39f9 127 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
bb270c08 128 break;
36ef5369 129 case AV_CODEC_ID_PCM_S16BE:
44de39f9 130 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
bb270c08 131 break;
36ef5369 132 case AV_CODEC_ID_PCM_S16LE:
6cac3a3b 133 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
bb270c08 134 break;
36ef5369 135 case AV_CODEC_ID_ADPCM_SWF:
1cb34ea4 136 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
bb270c08 137 break;
36ef5369 138 case AV_CODEC_ID_NELLYMOSER:
1cb34ea4
LB
139 if (enc->sample_rate == 8000)
140 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
141 else if (enc->sample_rate == 16000)
bd2ff1a8 142 flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
1cb34ea4
LB
143 else
144 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
b7d1cd02 145 break;
36ef5369 146 case AV_CODEC_ID_PCM_MULAW:
b92c7ee6
DF
147 flags = FLV_CODECID_PCM_MULAW | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
148 break;
36ef5369 149 case AV_CODEC_ID_PCM_ALAW:
b92c7ee6
DF
150 flags = FLV_CODECID_PCM_ALAW | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
151 break;
37cdf93d 152 case 0:
1cb34ea4 153 flags |= enc->codec_tag << 4;
37cdf93d
MN
154 break;
155 default:
0a3ad7ff 156 av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
37cdf93d
MN
157 return -1;
158 }
115329f1 159
37cdf93d
MN
160 return flags;
161}
162
ae628ec1 163static void put_amf_string(AVIOContext *pb, const char *str)
fd0fb306
MN
164{
165 size_t len = strlen(str);
77eb5504
AK
166 avio_wb16(pb, len);
167 avio_write(pb, str, len);
fd0fb306
MN
168}
169
1cb34ea4
LB
170static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
171{
77eb5504 172 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
1cb34ea4
LB
173 avio_wb24(pb, 5); /* Tag Data Size */
174 avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms */
175 avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
176 avio_wb24(pb, 0); /* StreamId = 0 */
177 avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
178 avio_w8(pb, 2); /* AVC end of sequence */
179 avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
180 avio_wb32(pb, 16); /* Size of FLV tag */
df4f1d51
TF
181}
182
ae628ec1 183static void put_amf_double(AVIOContext *pb, double d)
fd0fb306 184{
77eb5504 185 avio_w8(pb, AMF_DATA_TYPE_NUMBER);
3383a53e 186 avio_wb64(pb, av_double2int(d));
fd0fb306
MN
187}
188
1cb34ea4
LB
189static void put_amf_bool(AVIOContext *pb, int b)
190{
77eb5504
AK
191 avio_w8(pb, AMF_DATA_TYPE_BOOL);
192 avio_w8(pb, !!b);
148c9bdb
AH
193}
194
c64f3615 195static void write_metadata(AVFormatContext *s, unsigned int ts)
d4f5d74a 196{
ae628ec1 197 AVIOContext *pb = s->pb;
d4f5d74a 198 FLVContext *flv = s->priv_data;
4b68edd0 199 int metadata_count = 0;
cad0c375 200 int64_t metadata_size_pos, data_size, metadata_count_pos;
d2d67e42 201 AVDictionaryEntry *tag = NULL;
d4f5d74a 202
fd0fb306 203 /* write meta_tag */
1cb34ea4
LB
204 avio_w8(pb, 18); // tag type META
205 metadata_size_pos = avio_tell(pb);
206 avio_wb24(pb, 0); // size of data part (sum of all parts below)
c64f3615 207 avio_wb24(pb, ts); // timestamp
1cb34ea4 208 avio_wb32(pb, 0); // reserved
fd0fb306
MN
209
210 /* now data of data_size size */
211
212 /* first event name as a string */
77eb5504 213 avio_w8(pb, AMF_DATA_TYPE_STRING);
fd0fb306
MN
214 put_amf_string(pb, "onMetaData"); // 12 bytes
215
216 /* mixed array (hash) with size and string/type/data tuples */
77eb5504 217 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
cad0c375 218 metadata_count_pos = avio_tell(pb);
4d0cd5f5
AS
219 metadata_count = 4 * !!flv->video_enc +
220 5 * !!flv->audio_enc +
221 1 * !!flv->data_enc +
21e2dc9f
LB
222 2; // +2 for duration and file size
223
cad0c375 224 avio_wb32(pb, metadata_count);
fd0fb306 225
634b8cfa 226 put_amf_string(pb, "duration");
4d0cd5f5 227 flv->duration_offset = avio_tell(pb);
fd0fb306 228
1cb34ea4
LB
229 // fill in the guessed duration, it'll be corrected later if incorrect
230 put_amf_double(pb, s->duration / AV_TIME_BASE);
231
4d0cd5f5 232 if (flv->video_enc) {
fd0fb306 233 put_amf_string(pb, "width");
4d0cd5f5 234 put_amf_double(pb, flv->video_enc->width);
fd0fb306
MN
235
236 put_amf_string(pb, "height");
4d0cd5f5 237 put_amf_double(pb, flv->video_enc->height);
fd0fb306
MN
238
239 put_amf_string(pb, "videodatarate");
4d0cd5f5 240 put_amf_double(pb, flv->video_enc->bit_rate / 1024.0);
fd0fb306 241
4d0cd5f5 242 if (flv->framerate != 0.0) {
43e7f079 243 put_amf_string(pb, "framerate");
4d0cd5f5 244 put_amf_double(pb, flv->framerate);
74bc9458 245 metadata_count++;
43e7f079 246 }
148c9bdb
AH
247
248 put_amf_string(pb, "videocodecid");
4d0cd5f5 249 put_amf_double(pb, flv->video_enc->codec_tag);
fd0fb306
MN
250 }
251
4d0cd5f5 252 if (flv->audio_enc) {
426a6f34 253 put_amf_string(pb, "audiodatarate");
4d0cd5f5 254 put_amf_double(pb, flv->audio_enc->bit_rate / 1024.0);
426a6f34 255
fd0fb306 256 put_amf_string(pb, "audiosamplerate");
4d0cd5f5 257 put_amf_double(pb, flv->audio_enc->sample_rate);
148c9bdb
AH
258
259 put_amf_string(pb, "audiosamplesize");
4d0cd5f5 260 put_amf_double(pb, flv->audio_enc->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
148c9bdb
AH
261
262 put_amf_string(pb, "stereo");
4d0cd5f5 263 put_amf_bool(pb, flv->audio_enc->channels == 2);
148c9bdb
AH
264
265 put_amf_string(pb, "audiocodecid");
4d0cd5f5 266 put_amf_double(pb, flv->audio_enc->codec_tag);
fd0fb306
MN
267 }
268
4d0cd5f5 269 if (flv->data_enc) {
21e2dc9f
LB
270 put_amf_string(pb, "datastream");
271 put_amf_double(pb, 0.0);
272 }
273
d2d67e42 274 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
16f82508 275 put_amf_string(pb, tag->key);
77eb5504 276 avio_w8(pb, AMF_DATA_TYPE_STRING);
16f82508 277 put_amf_string(pb, tag->value);
cad0c375 278 metadata_count++;
16f82508
TT
279 }
280
634b8cfa 281 put_amf_string(pb, "filesize");
1cb34ea4 282 flv->filesize_offset = avio_tell(pb);
634b8cfa 283 put_amf_double(pb, 0); // delayed write
fd0fb306
MN
284
285 put_amf_string(pb, "");
77eb5504 286 avio_w8(pb, AMF_END_OF_OBJECT);
fd0fb306
MN
287
288 /* write total size of tag */
1cb34ea4 289 data_size = avio_tell(pb) - metadata_size_pos - 10;
cad0c375
MS
290
291 avio_seek(pb, metadata_count_pos, SEEK_SET);
292 avio_wb32(pb, metadata_count);
293
6b4aa5da 294 avio_seek(pb, metadata_size_pos, SEEK_SET);
77eb5504 295 avio_wb24(pb, data_size);
45a8a02a 296 avio_skip(pb, data_size + 10 - 3);
77eb5504 297 avio_wb32(pb, data_size + 11);
4d0cd5f5
AS
298}
299
e767c9e8
LB
300static int unsupported_codec(AVFormatContext *s,
301 const char* type, int codec_id)
302{
303 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
304 av_log(s, AV_LOG_ERROR,
305 "%s codec %s not compatible with flv\n",
306 type,
307 desc ? desc->name : "unknown");
308 return AVERROR(ENOSYS);
309}
310
4d0cd5f5
AS
311static int flv_write_header(AVFormatContext *s)
312{
313 int i;
314 AVIOContext *pb = s->pb;
315 FLVContext *flv = s->priv_data;
316 int64_t data_size;
317
318 for (i = 0; i < s->nb_streams; i++) {
319 AVCodecContext *enc = s->streams[i]->codec;
320 FLVStreamContext *sc;
321 switch (enc->codec_type) {
322 case AVMEDIA_TYPE_VIDEO:
323 if (s->streams[i]->avg_frame_rate.den &&
324 s->streams[i]->avg_frame_rate.num) {
325 flv->framerate = av_q2d(s->streams[i]->avg_frame_rate);
326 }
327 if (flv->video_enc) {
328 av_log(s, AV_LOG_ERROR,
329 "at most one video stream is supported in flv\n");
330 return AVERROR(EINVAL);
331 }
332 flv->video_enc = enc;
e767c9e8
LB
333 if (!ff_codec_get_tag(flv_video_codec_ids, enc->codec_id))
334 return unsupported_codec(s, "Video", enc->codec_id);
4d0cd5f5
AS
335 break;
336 case AVMEDIA_TYPE_AUDIO:
337 if (flv->audio_enc) {
338 av_log(s, AV_LOG_ERROR,
339 "at most one audio stream is supported in flv\n");
340 return AVERROR(EINVAL);
341 }
342 flv->audio_enc = enc;
343 if (get_audio_flags(s, enc) < 0)
e767c9e8 344 return unsupported_codec(s, "Audio", enc->codec_id);
4d0cd5f5
AS
345 break;
346 case AVMEDIA_TYPE_DATA:
e767c9e8
LB
347 if (enc->codec_id != AV_CODEC_ID_TEXT)
348 return unsupported_codec(s, "Data", enc->codec_id);
4d0cd5f5
AS
349 flv->data_enc = enc;
350 break;
351 default:
352 av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
353 return -1;
354 }
355 avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
356
357 sc = av_mallocz(sizeof(FLVStreamContext));
358 if (!sc)
359 return AVERROR(ENOMEM);
360 s->streams[i]->priv_data = sc;
361 sc->last_ts = -1;
362 }
363
364 flv->delay = AV_NOPTS_VALUE;
365
366 avio_write(pb, "FLV", 3);
367 avio_w8(pb, 1);
368 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!flv->audio_enc +
369 FLV_HEADER_FLAG_HASVIDEO * !!flv->video_enc);
370 avio_wb32(pb, 9);
371 avio_wb32(pb, 0);
372
373 for (i = 0; i < s->nb_streams; i++)
374 if (s->streams[i]->codec->codec_tag == 5) {
375 avio_w8(pb, 8); // message type
376 avio_wb24(pb, 0); // include flags
377 avio_wb24(pb, 0); // time stamp
378 avio_wb32(pb, 0); // reserved
379 avio_wb32(pb, 11); // size
380 flv->reserved = 5;
381 }
382
c64f3615 383 write_metadata(s, 0);
fd0fb306 384
f23496b5
BC
385 for (i = 0; i < s->nb_streams; i++) {
386 AVCodecContext *enc = s->streams[i]->codec;
36ef5369 387 if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264) {
bc5c918e 388 int64_t pos;
77eb5504 389 avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
1cb34ea4 390 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
77eb5504
AK
391 avio_wb24(pb, 0); // size patched later
392 avio_wb24(pb, 0); // ts
1cb34ea4 393 avio_w8(pb, 0); // ts ext
77eb5504 394 avio_wb24(pb, 0); // streamid
a2704c97 395 pos = avio_tell(pb);
36ef5369 396 if (enc->codec_id == AV_CODEC_ID_AAC) {
0a3ad7ff 397 avio_w8(pb, get_audio_flags(s, enc));
77eb5504
AK
398 avio_w8(pb, 0); // AAC sequence header
399 avio_write(pb, enc->extradata, enc->extradata_size);
f23496b5 400 } else {
77eb5504
AK
401 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
402 avio_w8(pb, 0); // AVC sequence header
403 avio_wb24(pb, 0); // composition time
f23496b5
BC
404 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
405 }
a2704c97 406 data_size = avio_tell(pb) - pos;
6b4aa5da 407 avio_seek(pb, -data_size - 10, SEEK_CUR);
77eb5504 408 avio_wb24(pb, data_size);
45a8a02a 409 avio_skip(pb, data_size + 10 - 3);
77eb5504 410 avio_wb32(pb, data_size + 11); // previous tag size
f23496b5
BC
411 }
412 }
413
d4f5d74a
GM
414 return 0;
415}
416
d4f5d74a
GM
417static int flv_write_trailer(AVFormatContext *s)
418{
14b32253 419 int64_t file_size;
14b32253 420
ae628ec1 421 AVIOContext *pb = s->pb;
d4f5d74a 422 FLVContext *flv = s->priv_data;
df4f1d51
TF
423 int i;
424
425 /* Add EOS tag */
426 for (i = 0; i < s->nb_streams; i++) {
427 AVCodecContext *enc = s->streams[i]->codec;
ef74e397 428 FLVStreamContext *sc = s->streams[i]->priv_data;
b9f9e59a 429 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
36ef5369 430 enc->codec_id == AV_CODEC_ID_H264)
ef74e397 431 put_avc_eos_tag(pb, sc->last_ts);
df4f1d51 432 }
d4f5d74a 433
a2704c97 434 file_size = avio_tell(pb);
634b8cfa 435
da9cea77 436 /* update information */
1eaff98c
BA
437 if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0)
438 av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
439 else
440 put_amf_double(pb, flv->duration / (double)1000);
441 if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0)
442 av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
443 else
444 put_amf_double(pb, file_size);
634b8cfa 445
6b4aa5da 446 avio_seek(pb, file_size, SEEK_SET);
d4f5d74a
GM
447 return 0;
448}
449
e928649b 450static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
d4f5d74a 451{
1cb34ea4
LB
452 AVIOContext *pb = s->pb;
453 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
454 FLVContext *flv = s->priv_data;
ef74e397 455 FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
f23496b5 456 unsigned ts;
1cb34ea4
LB
457 int size = pkt->size;
458 uint8_t *data = NULL;
6a08955c 459 int flags = 0, flags_size;
d4f5d74a 460
be1e1373 461 if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
36ef5369 462 enc->codec_id == AV_CODEC_ID_AAC)
1cb34ea4 463 flags_size = 2;
36ef5369 464 else if (enc->codec_id == AV_CODEC_ID_H264)
1cb34ea4 465 flags_size = 5;
f683dbdc 466 else
1cb34ea4 467 flags_size = 1;
f683dbdc 468
c64f3615
AS
469 if (flv->delay == AV_NOPTS_VALUE)
470 flv->delay = -pkt->dts;
471
472 if (pkt->dts < -flv->delay) {
473 av_log(s, AV_LOG_WARNING,
474 "Packets are not in the proper order with respect to DTS\n");
475 return AVERROR(EINVAL);
476 }
477
478 ts = pkt->dts + flv->delay; // add delay to force positive dts
479
480 if (s->event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) {
481 write_metadata(s, ts);
482 s->event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
483 }
484
21e2dc9f
LB
485 switch (enc->codec_type) {
486 case AVMEDIA_TYPE_VIDEO:
77eb5504 487 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
09d8c0ae 488
e767c9e8 489 flags = ff_codec_get_tag(flv_video_codec_ids, enc->codec_id);
09d8c0ae 490
cc947f04 491 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
21e2dc9f
LB
492 break;
493 case AVMEDIA_TYPE_AUDIO:
0a3ad7ff 494 flags = get_audio_flags(s, enc);
115329f1 495
068f2a22 496 assert(size);
068f2a22 497
77eb5504 498 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
21e2dc9f 499 break;
21e2dc9f
LB
500 case AVMEDIA_TYPE_DATA:
501 avio_w8(pb, FLV_TAG_TYPE_META);
502 break;
503 default:
504 return AVERROR(EINVAL);
505 }
1cb34ea4 506
36ef5369 507 if (enc->codec_id == AV_CODEC_ID_H264)
da9cea77 508 /* check if extradata looks like MP4 */
1cb34ea4 509 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
71e685b0
BC
510 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
511 return -1;
1cb34ea4 512
4ee247a2 513 /* check Speex packet duration */
36ef5369 514 if (enc->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
4ee247a2
JR
515 av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
516 "8 frames per packet. Adobe Flash "
517 "Player cannot handle this!\n");
4ee247a2 518
ef74e397
JR
519 if (sc->last_ts < ts)
520 sc->last_ts = ts;
4ee247a2 521
1cb34ea4
LB
522 avio_wb24(pb, size + flags_size);
523 avio_wb24(pb, ts);
524 avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
525 avio_wb24(pb, flv->reserved);
21e2dc9f
LB
526
527 if (enc->codec_type == AVMEDIA_TYPE_DATA) {
528 int data_size;
f412b2c9 529 int64_t metadata_size_pos = avio_tell(pb);
21e2dc9f
LB
530 avio_w8(pb, AMF_DATA_TYPE_STRING);
531 put_amf_string(pb, "onTextData");
532 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
533 avio_wb32(pb, 2);
534 put_amf_string(pb, "type");
535 avio_w8(pb, AMF_DATA_TYPE_STRING);
536 put_amf_string(pb, "Text");
537 put_amf_string(pb, "text");
538 avio_w8(pb, AMF_DATA_TYPE_STRING);
539 put_amf_string(pb, pkt->data);
540 put_amf_string(pb, "");
541 avio_w8(pb, AMF_END_OF_OBJECT);
542 /* write total size of tag */
543 data_size = avio_tell(pb) - metadata_size_pos;
544 avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
545 avio_wb24(pb, data_size);
546 avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
547 avio_wb32(pb, data_size + 11);
548 } else {
1cb34ea4 549 avio_w8(pb,flags);
e6ed8668
MS
550 if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {
551 if (enc->extradata_size)
552 avio_w8(pb, enc->extradata[0]);
553 else
554 avio_w8(pb, ((FFALIGN(enc->width, 16) - enc->width) << 4) |
555 (FFALIGN(enc->height, 16) - enc->height));
556 } else if (enc->codec_id == AV_CODEC_ID_AAC)
1cb34ea4 557 avio_w8(pb, 1); // AAC raw
36ef5369 558 else if (enc->codec_id == AV_CODEC_ID_H264) {
1cb34ea4
LB
559 avio_w8(pb, 1); // AVC NALU
560 avio_wb24(pb, pkt->pts - pkt->dts);
561 }
71e685b0 562
1cb34ea4 563 avio_write(pb, data ? data : pkt->data, size);
71e685b0 564
1cb34ea4
LB
565 avio_wb32(pb, size + flags_size + 11); // previous tag size
566 flv->duration = FFMAX(flv->duration,
567 pkt->pts + flv->delay + pkt->duration);
21e2dc9f 568 }
71e685b0
BC
569
570 av_free(data);
571
0e28e9ca 572 return pb->error;
d4f5d74a
GM
573}
574
c6610a21 575AVOutputFormat ff_flv_muxer = {
dfc2c4d9 576 .name = "flv",
0177b7d2 577 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
dfc2c4d9
AK
578 .mime_type = "video/x-flv",
579 .extensions = "flv",
580 .priv_data_size = sizeof(FLVContext),
36ef5369
AK
581 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
582 .video_codec = AV_CODEC_ID_FLV1,
dfc2c4d9
AK
583 .write_header = flv_write_header,
584 .write_packet = flv_write_packet,
585 .write_trailer = flv_write_trailer,
1cb34ea4
LB
586 .codec_tag = (const AVCodecTag* const []) {
587 flv_video_codec_ids, flv_audio_codec_ids, 0
588 },
e9cef897
JZ
589 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
590 AVFMT_TS_NONSTRICT,
d4f5d74a 591};