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