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