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