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