]> git.sesse.net Git - ffmpeg/blob - libavformat/movenc.c
Merge commit '41776ba9c0ebbb71394cefdf7dd1b243e6c852d5'
[ffmpeg] / libavformat / movenc.c
1 /*
2  * MOV, 3GP, MP4 muxer
3  * Copyright (c) 2003 Thomas Raivio
4  * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <stdint.h>
25 #include <inttypes.h>
26
27 #include "movenc.h"
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "riff.h"
31 #include "avio.h"
32 #include "isom.h"
33 #include "avc.h"
34 #include "libavcodec/get_bits.h"
35 #include "libavcodec/put_bits.h"
36 #include "libavcodec/vc1.h"
37 #include "libavcodec/raw.h"
38 #include "internal.h"
39 #include "libavutil/avstring.h"
40 #include "libavutil/intfloat.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/dict.h"
44 #include "libavutil/pixdesc.h"
45 #include "hevc.h"
46 #include "rtpenc.h"
47 #include "mov_chan.h"
48
49 #undef NDEBUG
50 #include <assert.h>
51
52 static const AVOption options[] = {
53     { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
54     { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
55     { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
56     { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
57     { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
58     { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
59     { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
60     { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
61     { "faststart", "Run a second pass to put the index (moov atom) at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
62     { "omit_tfhd_offset", "Omit the base data offset in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_OMIT_TFHD_OFFSET}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
63     FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
64     { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
65     { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
66     { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
67     { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
68     { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
69     { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
70     { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
71     { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
72     { "video_track_timescale", "set timescale of all video tracks", offsetof(MOVMuxContext, video_track_timescale), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
73     { "brand",    "Override major brand", offsetof(MOVMuxContext, major_brand),   AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
74     { NULL },
75 };
76
77 #define MOV_CLASS(flavor)\
78 static const AVClass flavor ## _muxer_class = {\
79     .class_name = #flavor " muxer",\
80     .item_name  = av_default_item_name,\
81     .option     = options,\
82     .version    = LIBAVUTIL_VERSION_INT,\
83 };
84
85 static int get_moov_size(AVFormatContext *s);
86
87 //FIXME support 64 bit variant with wide placeholders
88 static int64_t update_size(AVIOContext *pb, int64_t pos)
89 {
90     int64_t curpos = avio_tell(pb);
91     avio_seek(pb, pos, SEEK_SET);
92     avio_wb32(pb, curpos - pos); /* rewrite size */
93     avio_seek(pb, curpos, SEEK_SET);
94
95     return curpos - pos;
96 }
97
98 static int supports_edts(MOVMuxContext *mov)
99 {
100     // EDTS with fragments is tricky as we don't know the duration when its written
101     return (mov->use_editlist<0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) || mov->use_editlist>0;
102 }
103
104 static int co64_required(const MOVTrack *track)
105 {
106     if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
107         return 1;
108     return 0;
109 }
110
111 /* Chunk offset atom */
112 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
113 {
114     int i;
115     int mode64 = co64_required(track); // use 32 bit size variant if possible
116     int64_t pos = avio_tell(pb);
117     avio_wb32(pb, 0); /* size */
118     if (mode64)
119         ffio_wfourcc(pb, "co64");
120     else
121         ffio_wfourcc(pb, "stco");
122     avio_wb32(pb, 0); /* version & flags */
123     avio_wb32(pb, track->chunkCount); /* entry count */
124     for (i = 0; i < track->entry; i++) {
125         if (!track->cluster[i].chunkNum)
126             continue;
127         if (mode64 == 1)
128             avio_wb64(pb, track->cluster[i].pos + track->data_offset);
129         else
130             avio_wb32(pb, track->cluster[i].pos + track->data_offset);
131     }
132     return update_size(pb, pos);
133 }
134
135 /* Sample size atom */
136 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
137 {
138     int equalChunks = 1;
139     int i, j, entries = 0, tst = -1, oldtst = -1;
140
141     int64_t pos = avio_tell(pb);
142     avio_wb32(pb, 0); /* size */
143     ffio_wfourcc(pb, "stsz");
144     avio_wb32(pb, 0); /* version & flags */
145
146     for (i = 0; i < track->entry; i++) {
147         tst = track->cluster[i].size / track->cluster[i].entries;
148         if (oldtst != -1 && tst != oldtst)
149             equalChunks = 0;
150         oldtst = tst;
151         entries += track->cluster[i].entries;
152     }
153     if (equalChunks && track->entry) {
154         int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
155         sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
156         avio_wb32(pb, sSize); // sample size
157         avio_wb32(pb, entries); // sample count
158     } else {
159         avio_wb32(pb, 0); // sample size
160         avio_wb32(pb, entries); // sample count
161         for (i = 0; i < track->entry; i++) {
162             for (j = 0; j < track->cluster[i].entries; j++) {
163                 avio_wb32(pb, track->cluster[i].size /
164                           track->cluster[i].entries);
165             }
166         }
167     }
168     return update_size(pb, pos);
169 }
170
171 /* Sample to chunk atom */
172 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
173 {
174     int index = 0, oldval = -1, i;
175     int64_t entryPos, curpos;
176
177     int64_t pos = avio_tell(pb);
178     avio_wb32(pb, 0); /* size */
179     ffio_wfourcc(pb, "stsc");
180     avio_wb32(pb, 0); // version & flags
181     entryPos = avio_tell(pb);
182     avio_wb32(pb, track->chunkCount); // entry count
183     for (i = 0; i < track->entry; i++) {
184         if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
185             avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
186             avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
187             avio_wb32(pb, 0x1); // sample description index
188             oldval = track->cluster[i].samples_in_chunk;
189             index++;
190         }
191     }
192     curpos = avio_tell(pb);
193     avio_seek(pb, entryPos, SEEK_SET);
194     avio_wb32(pb, index); // rewrite size
195     avio_seek(pb, curpos, SEEK_SET);
196
197     return update_size(pb, pos);
198 }
199
200 /* Sync sample atom */
201 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
202 {
203     int64_t curpos, entryPos;
204     int i, index = 0;
205     int64_t pos = avio_tell(pb);
206     avio_wb32(pb, 0); // size
207     ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
208     avio_wb32(pb, 0); // version & flags
209     entryPos = avio_tell(pb);
210     avio_wb32(pb, track->entry); // entry count
211     for (i = 0; i < track->entry; i++) {
212         if (track->cluster[i].flags & flag) {
213             avio_wb32(pb, i + 1);
214             index++;
215         }
216     }
217     curpos = avio_tell(pb);
218     avio_seek(pb, entryPos, SEEK_SET);
219     avio_wb32(pb, index); // rewrite size
220     avio_seek(pb, curpos, SEEK_SET);
221     return update_size(pb, pos);
222 }
223
224 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
225 {
226     avio_wb32(pb, 0x11); /* size */
227     if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
228     else                         ffio_wfourcc(pb, "damr");
229     ffio_wfourcc(pb, "FFMP");
230     avio_w8(pb, 0); /* decoder version */
231
232     avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
233     avio_w8(pb, 0x00); /* Mode change period (no restriction) */
234     avio_w8(pb, 0x01); /* Frames per sample */
235     return 0x11;
236 }
237
238 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
239 {
240     GetBitContext gbc;
241     PutBitContext pbc;
242     uint8_t buf[3];
243     int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
244
245     if (track->vos_len < 7)
246         return -1;
247
248     avio_wb32(pb, 11);
249     ffio_wfourcc(pb, "dac3");
250
251     init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
252     fscod      = get_bits(&gbc, 2);
253     frmsizecod = get_bits(&gbc, 6);
254     bsid       = get_bits(&gbc, 5);
255     bsmod      = get_bits(&gbc, 3);
256     acmod      = get_bits(&gbc, 3);
257     if (acmod == 2) {
258         skip_bits(&gbc, 2); // dsurmod
259     } else {
260         if ((acmod & 1) && acmod != 1)
261             skip_bits(&gbc, 2); // cmixlev
262         if (acmod & 4)
263             skip_bits(&gbc, 2); // surmixlev
264     }
265     lfeon = get_bits1(&gbc);
266
267     init_put_bits(&pbc, buf, sizeof(buf));
268     put_bits(&pbc, 2, fscod);
269     put_bits(&pbc, 5, bsid);
270     put_bits(&pbc, 3, bsmod);
271     put_bits(&pbc, 3, acmod);
272     put_bits(&pbc, 1, lfeon);
273     put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
274     put_bits(&pbc, 5, 0); // reserved
275
276     flush_put_bits(&pbc);
277     avio_write(pb, buf, sizeof(buf));
278
279     return 11;
280 }
281
282 /**
283  * This function writes extradata "as is".
284  * Extradata must be formatted like a valid atom (with size and tag).
285  */
286 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
287 {
288     avio_write(pb, track->enc->extradata, track->enc->extradata_size);
289     return track->enc->extradata_size;
290 }
291
292 static int mov_write_enda_tag(AVIOContext *pb)
293 {
294     avio_wb32(pb, 10);
295     ffio_wfourcc(pb, "enda");
296     avio_wb16(pb, 1); /* little endian */
297     return 10;
298 }
299
300 static int mov_write_enda_tag_be(AVIOContext *pb)
301 {
302   avio_wb32(pb, 10);
303   ffio_wfourcc(pb, "enda");
304   avio_wb16(pb, 0); /* big endian */
305   return 10;
306 }
307
308 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
309 {
310     int i = 3;
311     avio_w8(pb, tag);
312     for (; i > 0; i--)
313         avio_w8(pb, (size >> (7 * i)) | 0x80);
314     avio_w8(pb, size & 0x7F);
315 }
316
317 static unsigned compute_avg_bitrate(MOVTrack *track)
318 {
319     uint64_t size = 0;
320     int i;
321     if (!track->track_duration)
322         return 0;
323     for (i = 0; i < track->entry; i++)
324         size += track->cluster[i].size;
325     return size * 8 * track->timescale / track->track_duration;
326 }
327
328 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
329 {
330     int64_t pos = avio_tell(pb);
331     int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
332     unsigned avg_bitrate;
333
334     avio_wb32(pb, 0); // size
335     ffio_wfourcc(pb, "esds");
336     avio_wb32(pb, 0); // Version
337
338     // ES descriptor
339     put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
340     avio_wb16(pb, track->track_id);
341     avio_w8(pb, 0x00); // flags (= no flags)
342
343     // DecoderConfig descriptor
344     put_descr(pb, 0x04, 13 + decoder_specific_info_len);
345
346     // Object type indication
347     if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
348          track->enc->codec_id == AV_CODEC_ID_MP3) &&
349         track->enc->sample_rate > 24000)
350         avio_w8(pb, 0x6B); // 11172-3
351     else
352         avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
353
354     // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
355     // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
356     if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
357         avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
358     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
359         avio_w8(pb, 0x15); // flags (= Audiostream)
360     else
361         avio_w8(pb, 0x11); // flags (= Visualstream)
362
363     avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
364
365     avg_bitrate = compute_avg_bitrate(track);
366     // maxbitrate (FIXME should be max rate in any 1 sec window)
367     avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
368     avio_wb32(pb, avg_bitrate);
369
370     if (track->vos_len) {
371         // DecoderSpecific info descriptor
372         put_descr(pb, 0x05, track->vos_len);
373         avio_write(pb, track->vos_data, track->vos_len);
374     }
375
376     // SL descriptor
377     put_descr(pb, 0x06, 1);
378     avio_w8(pb, 0x02);
379     return update_size(pb, pos);
380 }
381
382 static int mov_pcm_le_gt16(enum AVCodecID codec_id)
383 {
384     return codec_id == AV_CODEC_ID_PCM_S24LE ||
385            codec_id == AV_CODEC_ID_PCM_S32LE ||
386            codec_id == AV_CODEC_ID_PCM_F32LE ||
387            codec_id == AV_CODEC_ID_PCM_F64LE;
388 }
389
390 static int mov_pcm_be_gt16(enum AVCodecID codec_id)
391 {
392     return codec_id == AV_CODEC_ID_PCM_S24BE ||
393            codec_id == AV_CODEC_ID_PCM_S32BE ||
394            codec_id == AV_CODEC_ID_PCM_F32BE ||
395            codec_id == AV_CODEC_ID_PCM_F64BE;
396 }
397
398 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
399 {
400     int ret;
401     int64_t pos = avio_tell(pb);
402     avio_wb32(pb, 0);
403     avio_wl32(pb, track->tag); // store it byteswapped
404     track->enc->codec_tag = av_bswap16(track->tag >> 16);
405     if ((ret = ff_put_wav_header(pb, track->enc, 0)) < 0)
406         return ret;
407     return update_size(pb, pos);
408 }
409
410 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
411 {
412     int ret;
413     int64_t pos = avio_tell(pb);
414     avio_wb32(pb, 0);
415     ffio_wfourcc(pb, "wfex");
416     if ((ret = ff_put_wav_header(pb, track->enc, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX)) < 0)
417         return ret;
418     return update_size(pb, pos);
419 }
420
421 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
422 {
423     uint32_t layout_tag, bitmap;
424     int64_t pos = avio_tell(pb);
425
426     layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
427                                                track->enc->channel_layout,
428                                                &bitmap);
429     if (!layout_tag) {
430         av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
431                "lack of channel information\n");
432         return 0;
433     }
434
435     avio_wb32(pb, 0);           // Size
436     ffio_wfourcc(pb, "chan");   // Type
437     avio_w8(pb, 0);             // Version
438     avio_wb24(pb, 0);           // Flags
439     avio_wb32(pb, layout_tag);  // mChannelLayoutTag
440     avio_wb32(pb, bitmap);      // mChannelBitmap
441     avio_wb32(pb, 0);           // mNumberChannelDescriptions
442
443     return update_size(pb, pos);
444 }
445
446 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
447 {
448     int64_t pos = avio_tell(pb);
449
450     avio_wb32(pb, 0);     /* size */
451     ffio_wfourcc(pb, "wave");
452
453     if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
454     avio_wb32(pb, 12);    /* size */
455     ffio_wfourcc(pb, "frma");
456     avio_wl32(pb, track->tag);
457     }
458
459     if (track->enc->codec_id == AV_CODEC_ID_AAC) {
460         /* useless atom needed by mplayer, ipod, not needed by quicktime */
461         avio_wb32(pb, 12); /* size */
462         ffio_wfourcc(pb, "mp4a");
463         avio_wb32(pb, 0);
464         mov_write_esds_tag(pb, track);
465     } else if (mov_pcm_le_gt16(track->enc->codec_id))  {
466       mov_write_enda_tag(pb);
467     } else if (mov_pcm_be_gt16(track->enc->codec_id))  {
468       mov_write_enda_tag_be(pb);
469     } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
470         mov_write_amr_tag(pb, track);
471     } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
472         mov_write_ac3_tag(pb, track);
473     } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
474                track->enc->codec_id == AV_CODEC_ID_QDM2) {
475         mov_write_extradata_tag(pb, track);
476     } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
477                track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
478         mov_write_ms_tag(pb, track);
479     }
480
481     avio_wb32(pb, 8);     /* size */
482     avio_wb32(pb, 0);     /* null tag */
483
484     return update_size(pb, pos);
485 }
486
487 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
488 {
489     uint8_t *unescaped;
490     const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
491     int unescaped_size, seq_found = 0;
492     int level = 0, interlace = 0;
493     int packet_seq   = track->vc1_info.packet_seq;
494     int packet_entry = track->vc1_info.packet_entry;
495     int slices       = track->vc1_info.slices;
496     PutBitContext pbc;
497
498     if (track->start_dts == AV_NOPTS_VALUE) {
499         /* No packets written yet, vc1_info isn't authoritative yet. */
500         /* Assume inline sequence and entry headers. This will be
501          * overwritten at the end if the file is seekable. */
502         packet_seq = packet_entry = 1;
503     }
504
505     unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
506     if (!unescaped)
507         return AVERROR(ENOMEM);
508     start = find_next_marker(track->vos_data, end);
509     for (next = start; next < end; start = next) {
510         GetBitContext gb;
511         int size;
512         next = find_next_marker(start + 4, end);
513         size = next - start - 4;
514         if (size <= 0)
515             continue;
516         unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
517         init_get_bits(&gb, unescaped, 8 * unescaped_size);
518         if (AV_RB32(start) == VC1_CODE_SEQHDR) {
519             int profile = get_bits(&gb, 2);
520             if (profile != PROFILE_ADVANCED) {
521                 av_free(unescaped);
522                 return AVERROR(ENOSYS);
523             }
524             seq_found = 1;
525             level = get_bits(&gb, 3);
526             /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
527              * width, height */
528             skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
529             skip_bits(&gb, 1); /* broadcast */
530             interlace = get_bits1(&gb);
531             skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
532         }
533     }
534     if (!seq_found) {
535         av_free(unescaped);
536         return AVERROR(ENOSYS);
537     }
538
539     init_put_bits(&pbc, buf, 7);
540     /* VC1DecSpecStruc */
541     put_bits(&pbc, 4, 12); /* profile - advanced */
542     put_bits(&pbc, 3, level);
543     put_bits(&pbc, 1, 0); /* reserved */
544     /* VC1AdvDecSpecStruc */
545     put_bits(&pbc, 3, level);
546     put_bits(&pbc, 1, 0); /* cbr */
547     put_bits(&pbc, 6, 0); /* reserved */
548     put_bits(&pbc, 1, !interlace); /* no interlace */
549     put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
550     put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
551     put_bits(&pbc, 1, !slices); /* no slice code */
552     put_bits(&pbc, 1, 0); /* no bframe */
553     put_bits(&pbc, 1, 0); /* reserved */
554
555     /* framerate */
556     if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
557         put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
558     else
559         put_bits32(&pbc, 0xffffffff);
560
561     flush_put_bits(&pbc);
562
563     av_free(unescaped);
564
565     return 0;
566 }
567
568 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
569 {
570     uint8_t buf[7] = { 0 };
571     int ret;
572
573     if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
574         return ret;
575
576     avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
577     ffio_wfourcc(pb, "dvc1");
578     track->vc1_info.struct_offset = avio_tell(pb);
579     avio_write(pb, buf, sizeof(buf));
580     avio_write(pb, track->vos_data, track->vos_len);
581
582     return 0;
583 }
584
585 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
586 {
587     avio_wb32(pb, track->vos_len + 8);
588     ffio_wfourcc(pb, "glbl");
589     avio_write(pb, track->vos_data, track->vos_len);
590     return 8 + track->vos_len;
591 }
592
593 /**
594  * Compute flags for 'lpcm' tag.
595  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
596  */
597 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
598 {
599     switch (codec_id) {
600     case AV_CODEC_ID_PCM_F32BE:
601     case AV_CODEC_ID_PCM_F64BE:
602         return 11;
603     case AV_CODEC_ID_PCM_F32LE:
604     case AV_CODEC_ID_PCM_F64LE:
605         return 9;
606     case AV_CODEC_ID_PCM_U8:
607         return 10;
608     case AV_CODEC_ID_PCM_S16BE:
609     case AV_CODEC_ID_PCM_S24BE:
610     case AV_CODEC_ID_PCM_S32BE:
611         return 14;
612     case AV_CODEC_ID_PCM_S8:
613     case AV_CODEC_ID_PCM_S16LE:
614     case AV_CODEC_ID_PCM_S24LE:
615     case AV_CODEC_ID_PCM_S32LE:
616         return 12;
617     default:
618         return 0;
619     }
620 }
621
622 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
623 {
624     int64_t next_dts;
625
626     if (cluster_idx >= track->entry)
627         return 0;
628
629     if (cluster_idx + 1 == track->entry)
630         next_dts = track->track_duration + track->start_dts;
631     else
632         next_dts = track->cluster[cluster_idx + 1].dts;
633
634     next_dts -= track->cluster[cluster_idx].dts;
635
636     av_assert0(next_dts >= 0);
637     av_assert0(next_dts <= INT_MAX);
638
639     return next_dts;
640 }
641
642 static int get_samples_per_packet(MOVTrack *track)
643 {
644     int i, first_duration;
645
646 // return track->enc->frame_size;
647
648     /* use 1 for raw PCM */
649     if (!track->audio_vbr)
650         return 1;
651
652     /* check to see if duration is constant for all clusters */
653     if (!track->entry)
654         return 0;
655     first_duration = get_cluster_duration(track, 0);
656     for (i = 1; i < track->entry; i++) {
657         if (get_cluster_duration(track, i) != first_duration)
658             return 0;
659     }
660     return first_duration;
661 }
662
663 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
664 {
665     int64_t pos = avio_tell(pb);
666     int version = 0;
667     uint32_t tag = track->tag;
668
669     if (track->mode == MODE_MOV) {
670         if (track->timescale > UINT16_MAX) {
671             if (mov_get_lpcm_flags(track->enc->codec_id))
672                 tag = AV_RL32("lpcm");
673             version = 2;
674         } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
675                    mov_pcm_be_gt16(track->enc->codec_id) ||
676                    track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
677                    track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
678                    track->enc->codec_id == AV_CODEC_ID_QDM2) {
679             version = 1;
680         }
681     }
682
683     avio_wb32(pb, 0); /* size */
684     avio_wl32(pb, tag); // store it byteswapped
685     avio_wb32(pb, 0); /* Reserved */
686     avio_wb16(pb, 0); /* Reserved */
687     avio_wb16(pb, 1); /* Data-reference index, XXX  == 1 */
688
689     /* SoundDescription */
690     avio_wb16(pb, version); /* Version */
691     avio_wb16(pb, 0); /* Revision level */
692     avio_wb32(pb, 0); /* Reserved */
693
694     if (version == 2) {
695         avio_wb16(pb, 3);
696         avio_wb16(pb, 16);
697         avio_wb16(pb, 0xfffe);
698         avio_wb16(pb, 0);
699         avio_wb32(pb, 0x00010000);
700         avio_wb32(pb, 72);
701         avio_wb64(pb, av_double2int(track->enc->sample_rate));
702         avio_wb32(pb, track->enc->channels);
703         avio_wb32(pb, 0x7F000000);
704         avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
705         avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
706         avio_wb32(pb, track->sample_size);
707         avio_wb32(pb, get_samples_per_packet(track));
708     } else {
709         if (track->mode == MODE_MOV) {
710             avio_wb16(pb, track->enc->channels);
711             if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
712                 track->enc->codec_id == AV_CODEC_ID_PCM_S8)
713                 avio_wb16(pb, 8); /* bits per sample */
714             else
715                 avio_wb16(pb, 16);
716             avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
717         } else { /* reserved for mp4/3gp */
718             avio_wb16(pb, 2);
719             avio_wb16(pb, 16);
720             avio_wb16(pb, 0);
721         }
722
723         avio_wb16(pb, 0); /* packet size (= 0) */
724         avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
725                       track->enc->sample_rate : 0);
726         avio_wb16(pb, 0); /* Reserved */
727     }
728
729     if (version == 1) { /* SoundDescription V1 extended info */
730         if (mov_pcm_le_gt16(track->enc->codec_id) ||
731             mov_pcm_be_gt16(track->enc->codec_id))
732             avio_wb32(pb, 1); /*  must be 1 for  uncompressed formats */
733         else
734             avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
735         avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
736         avio_wb32(pb, track->sample_size); /* Bytes per frame */
737         avio_wb32(pb, 2); /* Bytes per sample */
738     }
739
740     if (track->mode == MODE_MOV &&
741         (track->enc->codec_id == AV_CODEC_ID_AAC           ||
742          track->enc->codec_id == AV_CODEC_ID_AC3           ||
743          track->enc->codec_id == AV_CODEC_ID_AMR_NB        ||
744          track->enc->codec_id == AV_CODEC_ID_ALAC          ||
745          track->enc->codec_id == AV_CODEC_ID_ADPCM_MS      ||
746          track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
747          track->enc->codec_id == AV_CODEC_ID_QDM2          ||
748          (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
749          (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
750         mov_write_wave_tag(pb, track);
751     else if (track->tag == MKTAG('m','p','4','a'))
752         mov_write_esds_tag(pb, track);
753     else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
754         mov_write_amr_tag(pb, track);
755     else if (track->enc->codec_id == AV_CODEC_ID_AC3)
756         mov_write_ac3_tag(pb, track);
757     else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
758         mov_write_extradata_tag(pb, track);
759     else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
760         mov_write_wfex_tag(pb, track);
761     else if (track->vos_len > 0)
762         mov_write_glbl_tag(pb, track);
763
764     if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
765         mov_write_chan_tag(pb, track);
766
767     return update_size(pb, pos);
768 }
769
770 static int mov_write_d263_tag(AVIOContext *pb)
771 {
772     avio_wb32(pb, 0xf); /* size */
773     ffio_wfourcc(pb, "d263");
774     ffio_wfourcc(pb, "FFMP");
775     avio_w8(pb, 0); /* decoder version */
776     /* FIXME use AVCodecContext level/profile, when encoder will set values */
777     avio_w8(pb, 0xa); /* level */
778     avio_w8(pb, 0); /* profile */
779     return 0xf;
780 }
781
782 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
783 {
784     int64_t pos = avio_tell(pb);
785
786     avio_wb32(pb, 0);
787     ffio_wfourcc(pb, "avcC");
788     ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
789     return update_size(pb, pos);
790 }
791
792 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
793 {
794     int64_t pos = avio_tell(pb);
795
796     avio_wb32(pb, 0);
797     ffio_wfourcc(pb, "hvcC");
798     ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
799     return update_size(pb, pos);
800 }
801
802 /* also used by all avid codecs (dv, imx, meridien) and their variants */
803 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
804 {
805     int i;
806     avio_wb32(pb, 24); /* size */
807     ffio_wfourcc(pb, "ACLR");
808     ffio_wfourcc(pb, "ACLR");
809     ffio_wfourcc(pb, "0001");
810     avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
811     avio_wb32(pb, 0); /* unknown */
812
813     avio_wb32(pb, 24); /* size */
814     ffio_wfourcc(pb, "APRG");
815     ffio_wfourcc(pb, "APRG");
816     ffio_wfourcc(pb, "0001");
817     avio_wb32(pb, 1); /* unknown */
818     avio_wb32(pb, 0); /* unknown */
819
820     avio_wb32(pb, 120); /* size */
821     ffio_wfourcc(pb, "ARES");
822     ffio_wfourcc(pb, "ARES");
823     ffio_wfourcc(pb, "0001");
824     avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
825     avio_wb32(pb, track->enc->width);
826     /* values below are based on samples created with quicktime and avid codecs */
827     if (track->vos_data[5] & 2) { // interlaced
828         avio_wb32(pb, track->enc->height / 2);
829         avio_wb32(pb, 2); /* unknown */
830         avio_wb32(pb, 0); /* unknown */
831         avio_wb32(pb, 4); /* unknown */
832     } else {
833         avio_wb32(pb, track->enc->height);
834         avio_wb32(pb, 1); /* unknown */
835         avio_wb32(pb, 0); /* unknown */
836         if (track->enc->height == 1080)
837             avio_wb32(pb, 5); /* unknown */
838         else
839             avio_wb32(pb, 6); /* unknown */
840     }
841     /* padding */
842     for (i = 0; i < 10; i++)
843         avio_wb64(pb, 0);
844
845     /* extra padding for stsd needed */
846     avio_wb32(pb, 0);
847     return 0;
848 }
849
850 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
851 {
852     int tag = track->enc->codec_tag;
853
854     if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
855         return 0;
856
857     if      (track->enc->codec_id == AV_CODEC_ID_H264)      tag = MKTAG('a','v','c','1');
858     else if (track->enc->codec_id == AV_CODEC_ID_HEVC)      tag = MKTAG('h','e','v','1');
859     else if (track->enc->codec_id == AV_CODEC_ID_AC3)       tag = MKTAG('a','c','-','3');
860     else if (track->enc->codec_id == AV_CODEC_ID_DIRAC)     tag = MKTAG('d','r','a','c');
861     else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT)  tag = MKTAG('t','x','3','g');
862     else if (track->enc->codec_id == AV_CODEC_ID_VC1)       tag = MKTAG('v','c','-','1');
863     else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)  tag = MKTAG('m','p','4','v');
864     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)  tag = MKTAG('m','p','4','a');
865     else if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)  tag = MKTAG('m','p','4','s');
866
867     return tag;
868 }
869
870 static const AVCodecTag codec_ipod_tags[] = {
871     { AV_CODEC_ID_H264,     MKTAG('a','v','c','1') },
872     { AV_CODEC_ID_MPEG4,    MKTAG('m','p','4','v') },
873     { AV_CODEC_ID_AAC,      MKTAG('m','p','4','a') },
874     { AV_CODEC_ID_ALAC,     MKTAG('a','l','a','c') },
875     { AV_CODEC_ID_AC3,      MKTAG('a','c','-','3') },
876     { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
877     { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
878     { AV_CODEC_ID_NONE, 0 },
879 };
880
881 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
882 {
883     int tag = track->enc->codec_tag;
884
885     // keep original tag for subs, ipod supports both formats
886     if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
887           (tag == MKTAG('t', 'x', '3', 'g') ||
888            tag == MKTAG('t', 'e', 'x', 't'))))
889         tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
890
891     if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
892         av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
893                "Quicktime/Ipod might not play the file\n");
894
895     return tag;
896 }
897
898 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
899 {
900     int tag;
901
902     if (track->enc->width == 720) { /* SD */
903         if (track->enc->height == 480) { /* NTSC */
904             if  (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
905             else                                            tag = MKTAG('d','v','c',' ');
906        }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
907         else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
908         else                                                tag = MKTAG('d','v','p','p');
909     } else if (track->enc->height == 720) { /* HD 720 line */
910         if  (track->enc->time_base.den == 50)               tag = MKTAG('d','v','h','q');
911         else                                                tag = MKTAG('d','v','h','p');
912     } else if (track->enc->height == 1080) { /* HD 1080 line */
913         if  (track->enc->time_base.den == 25)               tag = MKTAG('d','v','h','5');
914         else                                                tag = MKTAG('d','v','h','6');
915     } else {
916         av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
917         return 0;
918     }
919
920     return tag;
921 }
922
923 static AVRational find_fps(AVFormatContext *s, AVStream *st)
924 {
925     AVRational rate = {st->codec->time_base.den, st->codec->time_base.num};
926     /* if the codec time base makes no sense, try to fallback on stream frame rate */
927     if (av_timecode_check_frame_rate(rate) < 0) {
928         av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
929                rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
930         rate = st->avg_frame_rate;
931     }
932
933     return rate;
934 }
935
936 static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
937 {
938     int tag = track->enc->codec_tag;
939     int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
940     AVStream *st = track->st;
941     int rate = av_q2d(find_fps(s, st));
942
943     if (!tag)
944         tag = MKTAG('m', '2', 'v', '1'); //fallback tag
945
946     if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) {
947         if (track->enc->width == 1280 && track->enc->height == 720) {
948             if (!interlaced) {
949                 if      (rate == 24) tag = MKTAG('x','d','v','4');
950                 else if (rate == 25) tag = MKTAG('x','d','v','5');
951                 else if (rate == 30) tag = MKTAG('x','d','v','1');
952                 else if (rate == 50) tag = MKTAG('x','d','v','a');
953                 else if (rate == 60) tag = MKTAG('x','d','v','9');
954             }
955         } else if (track->enc->width == 1440 && track->enc->height == 1080) {
956             if (!interlaced) {
957                 if      (rate == 24) tag = MKTAG('x','d','v','6');
958                 else if (rate == 25) tag = MKTAG('x','d','v','7');
959                 else if (rate == 30) tag = MKTAG('x','d','v','8');
960             } else {
961                 if      (rate == 25) tag = MKTAG('x','d','v','3');
962                 else if (rate == 30) tag = MKTAG('x','d','v','2');
963             }
964         } else if (track->enc->width == 1920 && track->enc->height == 1080) {
965             if (!interlaced) {
966                 if      (rate == 24) tag = MKTAG('x','d','v','d');
967                 else if (rate == 25) tag = MKTAG('x','d','v','e');
968                 else if (rate == 30) tag = MKTAG('x','d','v','f');
969             } else {
970                 if      (rate == 25) tag = MKTAG('x','d','v','c');
971                 else if (rate == 30) tag = MKTAG('x','d','v','b');
972             }
973         }
974     } else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
975         if (track->enc->width == 1280 && track->enc->height == 720) {
976             if (!interlaced) {
977                 if      (rate == 24) tag = MKTAG('x','d','5','4');
978                 else if (rate == 25) tag = MKTAG('x','d','5','5');
979                 else if (rate == 30) tag = MKTAG('x','d','5','1');
980                 else if (rate == 50) tag = MKTAG('x','d','5','a');
981                 else if (rate == 60) tag = MKTAG('x','d','5','9');
982             }
983         } else if (track->enc->width == 1920 && track->enc->height == 1080) {
984             if (!interlaced) {
985                 if      (rate == 24) tag = MKTAG('x','d','5','d');
986                 else if (rate == 25) tag = MKTAG('x','d','5','e');
987                 else if (rate == 30) tag = MKTAG('x','d','5','f');
988             } else {
989                 if      (rate == 25) tag = MKTAG('x','d','5','c');
990                 else if (rate == 30) tag = MKTAG('x','d','5','b');
991             }
992         }
993     }
994
995     return tag;
996 }
997
998 static const struct {
999     enum AVPixelFormat pix_fmt;
1000     uint32_t tag;
1001     unsigned bps;
1002 } mov_pix_fmt_tags[] = {
1003     { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'),  0 },
1004     { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'),  0 },
1005     { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'),  0 },
1006     { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
1007     { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
1008     { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
1009     { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
1010     { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
1011     { AV_PIX_FMT_RGB24,   MKTAG('r','a','w',' '), 24 },
1012     { AV_PIX_FMT_BGR24,   MKTAG('2','4','B','G'), 24 },
1013     { AV_PIX_FMT_ARGB,    MKTAG('r','a','w',' '), 32 },
1014     { AV_PIX_FMT_BGRA,    MKTAG('B','G','R','A'), 32 },
1015     { AV_PIX_FMT_RGBA,    MKTAG('R','G','B','A'), 32 },
1016     { AV_PIX_FMT_ABGR,    MKTAG('A','B','G','R'), 32 },
1017     { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1018 };
1019
1020 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
1021 {
1022     int tag = track->enc->codec_tag;
1023     int i;
1024     enum AVPixelFormat pix_fmt;
1025
1026     for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1027         if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
1028             tag = mov_pix_fmt_tags[i].tag;
1029             track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
1030             if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
1031                 break;
1032         }
1033     }
1034
1035     pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_mov,
1036                                   track->enc->bits_per_coded_sample);
1037     if (tag == MKTAG('r','a','w',' ') &&
1038         track->enc->pix_fmt != pix_fmt &&
1039         track->enc->pix_fmt != AV_PIX_FMT_NONE)
1040         av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
1041                av_get_pix_fmt_name(track->enc->pix_fmt));
1042     return tag;
1043 }
1044
1045 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1046 {
1047     int tag = track->enc->codec_tag;
1048
1049     if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1050                  (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
1051                   track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
1052                   track->enc->codec_id == AV_CODEC_ID_H263 ||
1053                   track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1054                   av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
1055         if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
1056             tag = mov_get_dv_codec_tag(s, track);
1057         else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
1058             tag = mov_get_rawvideo_codec_tag(s, track);
1059         else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1060             tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1061         else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1062             tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
1063             if (!tag) { // if no mac fcc found, try with Microsoft tags
1064                 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
1065                 if (tag)
1066                     av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1067                            "the file may be unplayable!\n");
1068             }
1069         } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1070             tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
1071             if (!tag) { // if no mac fcc found, try with Microsoft tags
1072                 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
1073                 if (ms_tag) {
1074                     tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1075                     av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1076                            "the file may be unplayable!\n");
1077                 }
1078             }
1079         } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1080             tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
1081     }
1082
1083     return tag;
1084 }
1085
1086 static const AVCodecTag codec_3gp_tags[] = {
1087     { AV_CODEC_ID_H263,     MKTAG('s','2','6','3') },
1088     { AV_CODEC_ID_H264,     MKTAG('a','v','c','1') },
1089     { AV_CODEC_ID_MPEG4,    MKTAG('m','p','4','v') },
1090     { AV_CODEC_ID_AAC,      MKTAG('m','p','4','a') },
1091     { AV_CODEC_ID_AMR_NB,   MKTAG('s','a','m','r') },
1092     { AV_CODEC_ID_AMR_WB,   MKTAG('s','a','w','b') },
1093     { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1094     { AV_CODEC_ID_NONE, 0 },
1095 };
1096
1097 static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
1098     { AV_CODEC_ID_MP3,    MKTAG('.','m','p','3') },
1099     { AV_CODEC_ID_AAC,    MKTAG('m','p','4','a') },
1100     { AV_CODEC_ID_H264,   MKTAG('a','v','c','1') },
1101     { AV_CODEC_ID_VP6A,   MKTAG('V','P','6','A') },
1102     { AV_CODEC_ID_VP6F,   MKTAG('V','P','6','F') },
1103     { AV_CODEC_ID_NONE, 0 },
1104 };
1105
1106 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
1107 {
1108     int tag;
1109
1110     if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
1111         tag = mp4_get_codec_tag(s, track);
1112     else if (track->mode == MODE_ISM) {
1113         tag = mp4_get_codec_tag(s, track);
1114         if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
1115             tag = MKTAG('w', 'm', 'a', ' ');
1116     } else if (track->mode == MODE_IPOD)
1117         tag = ipod_get_codec_tag(s, track);
1118     else if (track->mode & MODE_3GP)
1119         tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
1120     else if (track->mode == MODE_F4V)
1121         tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
1122     else
1123         tag = mov_get_codec_tag(s, track);
1124
1125     return tag;
1126 }
1127
1128 /** Write uuid atom.
1129  * Needed to make file play in iPods running newest firmware
1130  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1131  */
1132 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
1133 {
1134     avio_wb32(pb, 28);
1135     ffio_wfourcc(pb, "uuid");
1136     avio_wb32(pb, 0x6b6840f2);
1137     avio_wb32(pb, 0x5f244fc5);
1138     avio_wb32(pb, 0xba39a51b);
1139     avio_wb32(pb, 0xcf0323f3);
1140     avio_wb32(pb, 0x0);
1141     return 28;
1142 }
1143
1144 static const uint16_t fiel_data[] = {
1145     0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1146 };
1147
1148 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
1149 {
1150     unsigned mov_field_order = 0;
1151     if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
1152         mov_field_order = fiel_data[track->enc->field_order];
1153     else
1154         return 0;
1155     avio_wb32(pb, 10);
1156     ffio_wfourcc(pb, "fiel");
1157     avio_wb16(pb, mov_field_order);
1158     return 10;
1159 }
1160
1161 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
1162 {
1163     int64_t pos = avio_tell(pb);
1164     avio_wb32(pb, 0);    /* size */
1165     avio_wl32(pb, track->tag); // store it byteswapped
1166     avio_wb32(pb, 0);    /* Reserved */
1167     avio_wb16(pb, 0);    /* Reserved */
1168     avio_wb16(pb, 1);    /* Data-reference index */
1169
1170     if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1171         mov_write_esds_tag(pb, track);
1172     else if (track->enc->extradata_size)
1173         avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1174
1175     return update_size(pb, pos);
1176 }
1177
1178 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1179 {
1180     AVRational sar;
1181     av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
1182               track->enc->sample_aspect_ratio.den, INT_MAX);
1183
1184     avio_wb32(pb, 16);
1185     ffio_wfourcc(pb, "pasp");
1186     avio_wb32(pb, sar.num);
1187     avio_wb32(pb, sar.den);
1188     return 16;
1189 }
1190
1191 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1192 {
1193     AVDictionaryEntry *encoder;
1194     int xdcam_res =  (track->enc->width == 1280 && track->enc->height == 720)
1195                   || (track->enc->width == 1440 && track->enc->height == 1080)
1196                   || (track->enc->width == 1920 && track->enc->height == 1080);
1197
1198     if (track->mode == MODE_MOV &&
1199         (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
1200         av_strlcpy(compressor_name, encoder->value, 32);
1201     } else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1202         int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
1203         AVStream *st = track->st;
1204         int rate = av_q2d(find_fps(NULL, st));
1205         av_strlcatf(compressor_name, len, "XDCAM");
1206         if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
1207             av_strlcatf(compressor_name, len, " HD422");
1208         } else if(track->enc->width == 1440) {
1209             av_strlcatf(compressor_name, len, " HD");
1210         } else
1211             av_strlcatf(compressor_name, len, " EX");
1212
1213         av_strlcatf(compressor_name, len, " %d%c", track->enc->height, interlaced ? 'i' : 'p');
1214
1215         av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1216     }
1217 }
1218
1219 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
1220 {
1221     int64_t pos = avio_tell(pb);
1222     char compressor_name[32] = { 0 };
1223
1224     avio_wb32(pb, 0); /* size */
1225     avio_wl32(pb, track->tag); // store it byteswapped
1226     avio_wb32(pb, 0); /* Reserved */
1227     avio_wb16(pb, 0); /* Reserved */
1228     avio_wb16(pb, 1); /* Data-reference index */
1229
1230     avio_wb16(pb, 0); /* Codec stream version */
1231     avio_wb16(pb, 0); /* Codec stream revision (=0) */
1232     if (track->mode == MODE_MOV) {
1233         ffio_wfourcc(pb, "FFMP"); /* Vendor */
1234         if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1235             avio_wb32(pb, 0); /* Temporal Quality */
1236             avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1237         } else {
1238             avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1239             avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1240         }
1241     } else {
1242         avio_wb32(pb, 0); /* Reserved */
1243         avio_wb32(pb, 0); /* Reserved */
1244         avio_wb32(pb, 0); /* Reserved */
1245     }
1246     avio_wb16(pb, track->enc->width); /* Video width */
1247     avio_wb16(pb, track->height); /* Video height */
1248     avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1249     avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1250     avio_wb32(pb, 0); /* Data size (= 0) */
1251     avio_wb16(pb, 1); /* Frame count (= 1) */
1252
1253     /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1254     find_compressor(compressor_name, 32, track);
1255     avio_w8(pb, strlen(compressor_name));
1256     avio_write(pb, compressor_name, 31);
1257
1258     if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1259         avio_wb16(pb, track->enc->bits_per_coded_sample);
1260     else
1261         avio_wb16(pb, 0x18); /* Reserved */
1262     avio_wb16(pb, 0xffff); /* Reserved */
1263     if (track->tag == MKTAG('m','p','4','v'))
1264         mov_write_esds_tag(pb, track);
1265     else if (track->enc->codec_id == AV_CODEC_ID_H263)
1266         mov_write_d263_tag(pb);
1267     else if (track->enc->codec_id == AV_CODEC_ID_AVUI ||
1268             track->enc->codec_id == AV_CODEC_ID_SVQ3) {
1269         mov_write_extradata_tag(pb, track);
1270         avio_wb32(pb, 0);
1271     } else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
1272         mov_write_avid_tag(pb, track);
1273     else if (track->enc->codec_id == AV_CODEC_ID_HEVC)
1274         mov_write_hvcc_tag(pb, track);
1275     else if (track->enc->codec_id == AV_CODEC_ID_H264) {
1276         mov_write_avcc_tag(pb, track);
1277         if (track->mode == MODE_IPOD)
1278             mov_write_uuid_tag_ipod(pb);
1279     } else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1280         mov_write_dvc1_tag(pb, track);
1281     else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
1282              track->enc->codec_id == AV_CODEC_ID_VP6A) {
1283         /* Don't write any potential extradata here - the cropping
1284          * is signalled via the normal width/height fields. */
1285     } else if (track->vos_len > 0)
1286         mov_write_glbl_tag(pb, track);
1287
1288     if (track->enc->codec_id != AV_CODEC_ID_H264 &&
1289         track->enc->codec_id != AV_CODEC_ID_MPEG4 &&
1290         track->enc->codec_id != AV_CODEC_ID_DNXHD)
1291         if (track->enc->field_order != AV_FIELD_UNKNOWN)
1292             mov_write_fiel_tag(pb, track);
1293
1294     if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1295         track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1296         mov_write_pasp_tag(pb, track);
1297     }
1298
1299     return update_size(pb, pos);
1300 }
1301
1302 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1303 {
1304     int64_t pos = avio_tell(pb);
1305     avio_wb32(pb, 0); /* size */
1306     ffio_wfourcc(pb, "rtp ");
1307     avio_wb32(pb, 0); /* Reserved */
1308     avio_wb16(pb, 0); /* Reserved */
1309     avio_wb16(pb, 1); /* Data-reference index */
1310
1311     avio_wb16(pb, 1); /* Hint track version */
1312     avio_wb16(pb, 1); /* Highest compatible version */
1313     avio_wb32(pb, track->max_packet_size); /* Max packet size */
1314
1315     avio_wb32(pb, 12); /* size */
1316     ffio_wfourcc(pb, "tims");
1317     avio_wb32(pb, track->timescale);
1318
1319     return update_size(pb, pos);
1320 }
1321
1322 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1323 {
1324     int64_t pos = avio_tell(pb);
1325 #if 1
1326     int frame_duration = av_rescale(track->timescale, track->enc->time_base.num, track->enc->time_base.den);
1327     int nb_frames = 1.0/av_q2d(track->enc->time_base) + 0.5;
1328
1329     if (nb_frames > 255) {
1330         av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
1331         return AVERROR(EINVAL);
1332     }
1333
1334     avio_wb32(pb, 0); /* size */
1335     ffio_wfourcc(pb, "tmcd");               /* Data format */
1336     avio_wb32(pb, 0);                       /* Reserved */
1337     avio_wb32(pb, 1);                       /* Data reference index */
1338     avio_wb32(pb, 0);                       /* Flags */
1339     avio_wb32(pb, track->timecode_flags);   /* Flags (timecode) */
1340     avio_wb32(pb, track->timescale);        /* Timescale */
1341     avio_wb32(pb, frame_duration);          /* Frame duration */
1342     avio_w8(pb, nb_frames);                 /* Number of frames */
1343     avio_wb24(pb, 0);                       /* Reserved */
1344     /* TODO: source reference string */
1345 #else
1346
1347     avio_wb32(pb, 0); /* size */
1348     ffio_wfourcc(pb, "tmcd");               /* Data format */
1349     avio_wb32(pb, 0);                       /* Reserved */
1350     avio_wb32(pb, 1);                       /* Data reference index */
1351     if (track->enc->extradata_size)
1352         avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1353 #endif
1354     return update_size(pb, pos);
1355 }
1356
1357 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1358 {
1359     int64_t pos = avio_tell(pb);
1360     avio_wb32(pb, 0); /* size */
1361     ffio_wfourcc(pb, "stsd");
1362     avio_wb32(pb, 0); /* version & flags */
1363     avio_wb32(pb, 1); /* entry count */
1364     if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1365         mov_write_video_tag(pb, track);
1366     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1367         mov_write_audio_tag(pb, track);
1368     else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1369         mov_write_subtitle_tag(pb, track);
1370     else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1371         mov_write_rtp_tag(pb, track);
1372     else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1373         mov_write_tmcd_tag(pb, track);
1374     return update_size(pb, pos);
1375 }
1376
1377 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1378 {
1379     MOVStts *ctts_entries;
1380     uint32_t entries = 0;
1381     uint32_t atom_size;
1382     int i;
1383
1384     ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1385     ctts_entries[0].count = 1;
1386     ctts_entries[0].duration = track->cluster[0].cts;
1387     for (i = 1; i < track->entry; i++) {
1388         if (track->cluster[i].cts == ctts_entries[entries].duration) {
1389             ctts_entries[entries].count++; /* compress */
1390         } else {
1391             entries++;
1392             ctts_entries[entries].duration = track->cluster[i].cts;
1393             ctts_entries[entries].count = 1;
1394         }
1395     }
1396     entries++; /* last one */
1397     atom_size = 16 + (entries * 8);
1398     avio_wb32(pb, atom_size); /* size */
1399     ffio_wfourcc(pb, "ctts");
1400     avio_wb32(pb, 0); /* version & flags */
1401     avio_wb32(pb, entries); /* entry count */
1402     for (i = 0; i < entries; i++) {
1403         avio_wb32(pb, ctts_entries[i].count);
1404         avio_wb32(pb, ctts_entries[i].duration);
1405     }
1406     av_free(ctts_entries);
1407     return atom_size;
1408 }
1409
1410 /* Time to sample atom */
1411 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1412 {
1413     MOVStts *stts_entries;
1414     uint32_t entries = -1;
1415     uint32_t atom_size;
1416     int i;
1417
1418     if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1419         stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1420         stts_entries[0].count = track->sample_count;
1421         stts_entries[0].duration = 1;
1422         entries = 1;
1423     } else {
1424         stts_entries = track->entry ?
1425                        av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1426                        NULL;
1427         for (i = 0; i < track->entry; i++) {
1428             int duration = get_cluster_duration(track, i);
1429             if (i && duration == stts_entries[entries].duration) {
1430                 stts_entries[entries].count++; /* compress */
1431             } else {
1432                 entries++;
1433                 stts_entries[entries].duration = duration;
1434                 stts_entries[entries].count = 1;
1435             }
1436         }
1437         entries++; /* last one */
1438     }
1439     atom_size = 16 + (entries * 8);
1440     avio_wb32(pb, atom_size); /* size */
1441     ffio_wfourcc(pb, "stts");
1442     avio_wb32(pb, 0); /* version & flags */
1443     avio_wb32(pb, entries); /* entry count */
1444     for (i = 0; i < entries; i++) {
1445         avio_wb32(pb, stts_entries[i].count);
1446         avio_wb32(pb, stts_entries[i].duration);
1447     }
1448     av_free(stts_entries);
1449     return atom_size;
1450 }
1451
1452 static int mov_write_dref_tag(AVIOContext *pb)
1453 {
1454     avio_wb32(pb, 28); /* size */
1455     ffio_wfourcc(pb, "dref");
1456     avio_wb32(pb, 0); /* version & flags */
1457     avio_wb32(pb, 1); /* entry count */
1458
1459     avio_wb32(pb, 0xc); /* size */
1460     //FIXME add the alis and rsrc atom
1461     ffio_wfourcc(pb, "url ");
1462     avio_wb32(pb, 1); /* version & flags */
1463
1464     return 28;
1465 }
1466
1467 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1468 {
1469     int64_t pos = avio_tell(pb);
1470     avio_wb32(pb, 0); /* size */
1471     ffio_wfourcc(pb, "stbl");
1472     mov_write_stsd_tag(pb, track);
1473     mov_write_stts_tag(pb, track);
1474     if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1475          track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1476         track->has_keyframes && track->has_keyframes < track->entry)
1477         mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1478     if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1479         mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1480     if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1481         track->flags & MOV_TRACK_CTTS)
1482         mov_write_ctts_tag(pb, track);
1483     mov_write_stsc_tag(pb, track);
1484     mov_write_stsz_tag(pb, track);
1485     mov_write_stco_tag(pb, track);
1486     return update_size(pb, pos);
1487 }
1488
1489 static int mov_write_dinf_tag(AVIOContext *pb)
1490 {
1491     int64_t pos = avio_tell(pb);
1492     avio_wb32(pb, 0); /* size */
1493     ffio_wfourcc(pb, "dinf");
1494     mov_write_dref_tag(pb);
1495     return update_size(pb, pos);
1496 }
1497
1498 static int mov_write_nmhd_tag(AVIOContext *pb)
1499 {
1500     avio_wb32(pb, 12);
1501     ffio_wfourcc(pb, "nmhd");
1502     avio_wb32(pb, 0);
1503     return 12;
1504 }
1505
1506 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
1507 {
1508     int64_t pos = avio_tell(pb);
1509     const char *font = "Lucida Grande";
1510     avio_wb32(pb, 0);                   /* size */
1511     ffio_wfourcc(pb, "tcmi");           /* timecode media information atom */
1512     avio_wb32(pb, 0);                   /* version & flags */
1513     avio_wb16(pb, 0);                   /* text font */
1514     avio_wb16(pb, 0);                   /* text face */
1515     avio_wb16(pb, 12);                  /* text size */
1516     avio_wb16(pb, 0);                   /* (unknown, not in the QT specs...) */
1517     avio_wb16(pb, 0x0000);              /* text color (red) */
1518     avio_wb16(pb, 0x0000);              /* text color (green) */
1519     avio_wb16(pb, 0x0000);              /* text color (blue) */
1520     avio_wb16(pb, 0xffff);              /* background color (red) */
1521     avio_wb16(pb, 0xffff);              /* background color (green) */
1522     avio_wb16(pb, 0xffff);              /* background color (blue) */
1523     avio_w8(pb, strlen(font));          /* font len (part of the pascal string) */
1524     avio_write(pb, font, strlen(font)); /* font name */
1525     return update_size(pb, pos);
1526 }
1527
1528 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
1529 {
1530     int64_t pos = avio_tell(pb);
1531     avio_wb32(pb, 0);      /* size */
1532     ffio_wfourcc(pb, "gmhd");
1533     avio_wb32(pb, 0x18);   /* gmin size */
1534     ffio_wfourcc(pb, "gmin");/* generic media info */
1535     avio_wb32(pb, 0);      /* version & flags */
1536     avio_wb16(pb, 0x40);   /* graphics mode = */
1537     avio_wb16(pb, 0x8000); /* opColor (r?) */
1538     avio_wb16(pb, 0x8000); /* opColor (g?) */
1539     avio_wb16(pb, 0x8000); /* opColor (b?) */
1540     avio_wb16(pb, 0);      /* balance */
1541     avio_wb16(pb, 0);      /* reserved */
1542
1543     /*
1544      * This special text atom is required for
1545      * Apple Quicktime chapters. The contents
1546      * don't appear to be documented, so the
1547      * bytes are copied verbatim.
1548      */
1549     if (track->tag != MKTAG('c','6','0','8')) {
1550     avio_wb32(pb, 0x2C);   /* size */
1551     ffio_wfourcc(pb, "text");
1552     avio_wb16(pb, 0x01);
1553     avio_wb32(pb, 0x00);
1554     avio_wb32(pb, 0x00);
1555     avio_wb32(pb, 0x00);
1556     avio_wb32(pb, 0x01);
1557     avio_wb32(pb, 0x00);
1558     avio_wb32(pb, 0x00);
1559     avio_wb32(pb, 0x00);
1560     avio_wb32(pb, 0x00004000);
1561     avio_wb16(pb, 0x0000);
1562     }
1563
1564     if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1565         int64_t tmcd_pos = avio_tell(pb);
1566         avio_wb32(pb, 0); /* size */
1567         ffio_wfourcc(pb, "tmcd");
1568         mov_write_tcmi_tag(pb, track);
1569         update_size(pb, tmcd_pos);
1570     }
1571     return update_size(pb, pos);
1572 }
1573
1574 static int mov_write_smhd_tag(AVIOContext *pb)
1575 {
1576     avio_wb32(pb, 16); /* size */
1577     ffio_wfourcc(pb, "smhd");
1578     avio_wb32(pb, 0); /* version & flags */
1579     avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1580     avio_wb16(pb, 0); /* reserved */
1581     return 16;
1582 }
1583
1584 static int mov_write_vmhd_tag(AVIOContext *pb)
1585 {
1586     avio_wb32(pb, 0x14); /* size (always 0x14) */
1587     ffio_wfourcc(pb, "vmhd");
1588     avio_wb32(pb, 0x01); /* version & flags */
1589     avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1590     return 0x14;
1591 }
1592
1593 static int is_clcp_track(MOVTrack *track)
1594 {
1595     return track->tag == MKTAG('c','7','0','8') ||
1596            track->tag == MKTAG('c','6','0','8');
1597 }
1598
1599 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1600 {
1601     const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1602     int64_t pos = avio_tell(pb);
1603
1604     hdlr      = "dhlr";
1605     hdlr_type = "url ";
1606     descr     = "DataHandler";
1607
1608     if (track) {
1609         hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1610         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1611             hdlr_type = "vide";
1612             descr     = "VideoHandler";
1613         } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1614             hdlr_type = "soun";
1615             descr     = "SoundHandler";
1616         } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1617             if (is_clcp_track(track)) {
1618                 hdlr_type = "clcp";
1619                 descr = "ClosedCaptionHandler";
1620             } else {
1621                 if (track->tag == MKTAG('t','x','3','g')) {
1622                     hdlr_type = "sbtl";
1623                 } else if (track->tag == MKTAG('m','p','4','s')) {
1624                     hdlr_type = "subp";
1625                 } else {
1626                     hdlr_type = "text";
1627                 }
1628             descr = "SubtitleHandler";
1629             }
1630         } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1631             hdlr_type = "hint";
1632             descr     = "HintHandler";
1633         } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1634             hdlr_type = "tmcd";
1635             descr = "TimeCodeHandler";
1636         } else {
1637             char tag_buf[32];
1638             av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1639                                     track->enc->codec_tag);
1640
1641             av_log(track->enc, AV_LOG_WARNING,
1642                    "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1643                    tag_buf, track->enc->codec_tag);
1644         }
1645     }
1646
1647     avio_wb32(pb, 0); /* size */
1648     ffio_wfourcc(pb, "hdlr");
1649     avio_wb32(pb, 0); /* Version & flags */
1650     avio_write(pb, hdlr, 4); /* handler */
1651     ffio_wfourcc(pb, hdlr_type); /* handler type */
1652     avio_wb32(pb, 0); /* reserved */
1653     avio_wb32(pb, 0); /* reserved */
1654     avio_wb32(pb, 0); /* reserved */
1655     if (!track || track->mode == MODE_MOV)
1656         avio_w8(pb, strlen(descr)); /* pascal string */
1657     avio_write(pb, descr, strlen(descr)); /* handler description */
1658     if (track && track->mode != MODE_MOV)
1659         avio_w8(pb, 0); /* c string */
1660     return update_size(pb, pos);
1661 }
1662
1663 static int mov_write_hmhd_tag(AVIOContext *pb)
1664 {
1665     /* This atom must be present, but leaving the values at zero
1666      * seems harmless. */
1667     avio_wb32(pb, 28); /* size */
1668     ffio_wfourcc(pb, "hmhd");
1669     avio_wb32(pb, 0); /* version, flags */
1670     avio_wb16(pb, 0); /* maxPDUsize */
1671     avio_wb16(pb, 0); /* avgPDUsize */
1672     avio_wb32(pb, 0); /* maxbitrate */
1673     avio_wb32(pb, 0); /* avgbitrate */
1674     avio_wb32(pb, 0); /* reserved */
1675     return 28;
1676 }
1677
1678 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1679 {
1680     int64_t pos = avio_tell(pb);
1681     avio_wb32(pb, 0); /* size */
1682     ffio_wfourcc(pb, "minf");
1683     if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1684         mov_write_vmhd_tag(pb);
1685     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1686         mov_write_smhd_tag(pb);
1687     else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1688         if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
1689             mov_write_gmhd_tag(pb, track);
1690         } else {
1691             mov_write_nmhd_tag(pb);
1692         }
1693     } else if (track->tag == MKTAG('r','t','p',' ')) {
1694         mov_write_hmhd_tag(pb);
1695     } else if (track->tag == MKTAG('t','m','c','d')) {
1696         mov_write_gmhd_tag(pb, track);
1697     }
1698     if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1699         mov_write_hdlr_tag(pb, NULL);
1700     mov_write_dinf_tag(pb);
1701     mov_write_stbl_tag(pb, track);
1702     return update_size(pb, pos);
1703 }
1704
1705 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1706 {
1707     int version = track->track_duration < INT32_MAX ? 0 : 1;
1708
1709     if (track->mode == MODE_ISM)
1710         version = 1;
1711
1712     (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1713     ffio_wfourcc(pb, "mdhd");
1714     avio_w8(pb, version);
1715     avio_wb24(pb, 0); /* flags */
1716     if (version == 1) {
1717         avio_wb64(pb, track->time);
1718         avio_wb64(pb, track->time);
1719     } else {
1720         avio_wb32(pb, track->time); /* creation time */
1721         avio_wb32(pb, track->time); /* modification time */
1722     }
1723     avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1724     if (!track->entry)
1725         (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1726     else
1727         (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1728     avio_wb16(pb, track->language); /* language */
1729     avio_wb16(pb, 0); /* reserved (quality) */
1730
1731     if (version != 0 && track->mode == MODE_MOV) {
1732         av_log(NULL, AV_LOG_ERROR,
1733                "FATAL error, file duration too long for timebase, this file will not be\n"
1734                "playable with quicktime. Choose a different timebase or a different\n"
1735                "container format\n");
1736     }
1737
1738     return 32;
1739 }
1740
1741 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1742 {
1743     int64_t pos = avio_tell(pb);
1744     avio_wb32(pb, 0); /* size */
1745     ffio_wfourcc(pb, "mdia");
1746     mov_write_mdhd_tag(pb, track);
1747     mov_write_hdlr_tag(pb, track);
1748     mov_write_minf_tag(pb, track);
1749     return update_size(pb, pos);
1750 }
1751
1752 /* transformation matrix
1753      |a  b  u|
1754      |c  d  v|
1755      |tx ty w| */
1756 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
1757                          int16_t d, int16_t tx, int16_t ty)
1758 {
1759     avio_wb32(pb, a << 16);  /* 16.16 format */
1760     avio_wb32(pb, b << 16);  /* 16.16 format */
1761     avio_wb32(pb, 0);        /* u in 2.30 format */
1762     avio_wb32(pb, c << 16);  /* 16.16 format */
1763     avio_wb32(pb, d << 16);  /* 16.16 format */
1764     avio_wb32(pb, 0);        /* v in 2.30 format */
1765     avio_wb32(pb, tx << 16); /* 16.16 format */
1766     avio_wb32(pb, ty << 16); /* 16.16 format */
1767     avio_wb32(pb, 1 << 30);  /* w in 2.30 format */
1768 }
1769
1770 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1771 {
1772     int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1773                                       track->timescale, AV_ROUND_UP);
1774     int version = duration < INT32_MAX ? 0 : 1;
1775     int rotation = 0;
1776
1777     if (track->mode == MODE_ISM)
1778         version = 1;
1779
1780     (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1781     ffio_wfourcc(pb, "tkhd");
1782     avio_w8(pb, version);
1783     avio_wb24(pb, (track->flags & MOV_TRACK_ENABLED) ?
1784                   MOV_TKHD_FLAG_ENABLED | MOV_TKHD_FLAG_IN_MOVIE :
1785                   MOV_TKHD_FLAG_IN_MOVIE);
1786     if (version == 1) {
1787         avio_wb64(pb, track->time);
1788         avio_wb64(pb, track->time);
1789     } else {
1790         avio_wb32(pb, track->time); /* creation time */
1791         avio_wb32(pb, track->time); /* modification time */
1792     }
1793     avio_wb32(pb, track->track_id); /* track-id */
1794     avio_wb32(pb, 0); /* reserved */
1795     if (!track->entry)
1796         (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1797     else
1798         (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1799
1800     avio_wb32(pb, 0); /* reserved */
1801     avio_wb32(pb, 0); /* reserved */
1802     avio_wb16(pb, 0); /* layer */
1803     avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1804     /* Volume, only for audio */
1805     if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1806         avio_wb16(pb, 0x0100);
1807     else
1808         avio_wb16(pb, 0);
1809     avio_wb16(pb, 0); /* reserved */
1810
1811     /* Matrix structure */
1812     if (st && st->metadata) {
1813         AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
1814         rotation = (rot && rot->value) ? atoi(rot->value) : 0;
1815     }
1816     if (rotation == 90) {
1817         write_matrix(pb,  0,  1, -1,  0, track->enc->height, 0);
1818     } else if (rotation == 180) {
1819         write_matrix(pb, -1,  0,  0, -1, track->enc->width, track->enc->height);
1820     } else if (rotation == 270) {
1821         write_matrix(pb,  0, -1,  1,  0, 0, track->enc->width);
1822     } else {
1823         write_matrix(pb,  1,  0,  0,  1, 0, 0);
1824     }
1825     /* Track width and height, for visual only */
1826     if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1827                track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1828         if (track->mode == MODE_MOV) {
1829             avio_wb32(pb, track->enc->width << 16);
1830             avio_wb32(pb, track->height << 16);
1831         } else {
1832             double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1833             if (!sample_aspect_ratio || track->height != track->enc->height)
1834                 sample_aspect_ratio = 1;
1835             avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1836             avio_wb32(pb, track->height * 0x10000);
1837         }
1838     } else {
1839         avio_wb32(pb, 0);
1840         avio_wb32(pb, 0);
1841     }
1842     return 0x5c;
1843 }
1844
1845 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1846 {
1847     int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1848                                track->enc->sample_aspect_ratio.den);
1849
1850     int64_t pos = avio_tell(pb);
1851
1852     avio_wb32(pb, 0); /* size */
1853     ffio_wfourcc(pb, "tapt");
1854
1855     avio_wb32(pb, 20);
1856     ffio_wfourcc(pb, "clef");
1857     avio_wb32(pb, 0);
1858     avio_wb32(pb, width << 16);
1859     avio_wb32(pb, track->enc->height << 16);
1860
1861     avio_wb32(pb, 20);
1862     ffio_wfourcc(pb, "prof");
1863     avio_wb32(pb, 0);
1864     avio_wb32(pb, width << 16);
1865     avio_wb32(pb, track->enc->height << 16);
1866
1867     avio_wb32(pb, 20);
1868     ffio_wfourcc(pb, "enof");
1869     avio_wb32(pb, 0);
1870     avio_wb32(pb, track->enc->width << 16);
1871     avio_wb32(pb, track->enc->height << 16);
1872
1873     return update_size(pb, pos);
1874 }
1875
1876 // This box seems important for the psp playback ... without it the movie seems to hang
1877 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1878 {
1879     int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1880                                       track->timescale, AV_ROUND_UP);
1881     int version = duration < INT32_MAX ? 0 : 1;
1882     int entry_size, entry_count, size;
1883     int64_t delay, start_ct = track->cluster[0].cts;
1884     delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1885                            track->timescale, AV_ROUND_DOWN);
1886     version |= delay < INT32_MAX ? 0 : 1;
1887
1888     entry_size = (version == 1) ? 20 : 12;
1889     entry_count = 1 + (delay > 0);
1890     size = 24 + entry_count * entry_size;
1891
1892     /* write the atom data */
1893     avio_wb32(pb, size);
1894     ffio_wfourcc(pb, "edts");
1895     avio_wb32(pb, size - 8);
1896     ffio_wfourcc(pb, "elst");
1897     avio_w8(pb, version);
1898     avio_wb24(pb, 0); /* flags */
1899
1900     avio_wb32(pb, entry_count);
1901     if (delay > 0) { /* add an empty edit to delay presentation */
1902         if (version == 1) {
1903             avio_wb64(pb, delay);
1904             avio_wb64(pb, -1);
1905         } else {
1906             avio_wb32(pb, delay);
1907             avio_wb32(pb, -1);
1908         }
1909         avio_wb32(pb, 0x00010000);
1910     } else {
1911         av_assert0(av_rescale_rnd(track->cluster[0].dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
1912         start_ct  = -FFMIN(track->cluster[0].dts, 0); //FFMIN needed due to rounding
1913         duration += delay;
1914     }
1915
1916     /* duration */
1917     if (version == 1) {
1918         avio_wb64(pb, duration);
1919         avio_wb64(pb, start_ct);
1920     } else {
1921         avio_wb32(pb, duration);
1922         avio_wb32(pb, start_ct);
1923     }
1924     avio_wb32(pb, 0x00010000);
1925     return size;
1926 }
1927
1928 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1929 {
1930     avio_wb32(pb, 20);   // size
1931     ffio_wfourcc(pb, "tref");
1932     avio_wb32(pb, 12);   // size (subatom)
1933     avio_wl32(pb, track->tref_tag);
1934     avio_wb32(pb, track->tref_id);
1935     return 20;
1936 }
1937
1938 // goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
1939 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1940 {
1941     avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1942     ffio_wfourcc(pb, "uuid");
1943     ffio_wfourcc(pb, "USMT");
1944     avio_wb32(pb, 0x21d24fce);
1945     avio_wb32(pb, 0xbb88695c);
1946     avio_wb32(pb, 0xfac9c740);
1947     avio_wb32(pb, 0x1c);     // another size here!
1948     ffio_wfourcc(pb, "MTDT");
1949     avio_wb32(pb, 0x00010012);
1950     avio_wb32(pb, 0x0a);
1951     avio_wb32(pb, 0x55c40000);
1952     avio_wb32(pb, 0x1);
1953     avio_wb32(pb, 0x0);
1954     return 0x34;
1955 }
1956
1957 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1958 {
1959     AVFormatContext *ctx = track->rtp_ctx;
1960     char buf[1000] = "";
1961     int len;
1962
1963     ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1964                        NULL, NULL, 0, 0, ctx);
1965     av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1966     len = strlen(buf);
1967
1968     avio_wb32(pb, len + 24);
1969     ffio_wfourcc(pb, "udta");
1970     avio_wb32(pb, len + 16);
1971     ffio_wfourcc(pb, "hnti");
1972     avio_wb32(pb, len + 8);
1973     ffio_wfourcc(pb, "sdp ");
1974     avio_write(pb, buf, len);
1975     return len + 24;
1976 }
1977
1978 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1979                               MOVTrack *track, AVStream *st)
1980 {
1981     int64_t pos = avio_tell(pb);
1982     avio_wb32(pb, 0); /* size */
1983     ffio_wfourcc(pb, "trak");
1984     mov_write_tkhd_tag(pb, track, st);
1985     if (supports_edts(mov))
1986         mov_write_edts_tag(pb, track);  // PSP Movies and several other cases require edts box
1987     if (track->tref_tag)
1988         mov_write_tref_tag(pb, track);
1989     mov_write_mdia_tag(pb, track);
1990     if (track->mode == MODE_PSP)
1991         mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
1992     if (track->tag == MKTAG('r','t','p',' '))
1993         mov_write_udta_sdp(pb, track);
1994     if (track->mode == MODE_MOV) {
1995         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1996             double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1997             if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
1998                 mov_write_tapt_tag(pb, track);
1999             }
2000         }
2001         if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
2002             mov_write_tapt_tag(pb, track);
2003         }
2004     }
2005     return update_size(pb, pos);
2006 }
2007
2008 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
2009 {
2010     int i, has_audio = 0, has_video = 0;
2011     int64_t pos = avio_tell(pb);
2012     int audio_profile = mov->iods_audio_profile;
2013     int video_profile = mov->iods_video_profile;
2014     for (i = 0; i < mov->nb_streams; i++) {
2015         if (mov->tracks[i].entry > 0) {
2016             has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
2017             has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
2018         }
2019     }
2020     if (audio_profile < 0)
2021         audio_profile = 0xFF - has_audio;
2022     if (video_profile < 0)
2023         video_profile = 0xFF - has_video;
2024     avio_wb32(pb, 0x0); /* size */
2025     ffio_wfourcc(pb, "iods");
2026     avio_wb32(pb, 0);    /* version & flags */
2027     put_descr(pb, 0x10, 7);
2028     avio_wb16(pb, 0x004f);
2029     avio_w8(pb, 0xff);
2030     avio_w8(pb, 0xff);
2031     avio_w8(pb, audio_profile);
2032     avio_w8(pb, video_profile);
2033     avio_w8(pb, 0xff);
2034     return update_size(pb, pos);
2035 }
2036
2037 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
2038 {
2039     avio_wb32(pb, 0x20); /* size */
2040     ffio_wfourcc(pb, "trex");
2041     avio_wb32(pb, 0);   /* version & flags */
2042     avio_wb32(pb, track->track_id); /* track ID */
2043     avio_wb32(pb, 1);   /* default sample description index */
2044     avio_wb32(pb, 0);   /* default sample duration */
2045     avio_wb32(pb, 0);   /* default sample size */
2046     avio_wb32(pb, 0);   /* default sample flags */
2047     return 0;
2048 }
2049
2050 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
2051 {
2052     int64_t pos = avio_tell(pb);
2053     int i;
2054     avio_wb32(pb, 0x0); /* size */
2055     ffio_wfourcc(pb, "mvex");
2056     for (i = 0; i < mov->nb_streams; i++)
2057         mov_write_trex_tag(pb, &mov->tracks[i]);
2058     return update_size(pb, pos);
2059 }
2060
2061 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2062 {
2063     int max_track_id = 1, i;
2064     int64_t max_track_len_temp, max_track_len = 0;
2065     int version;
2066
2067     for (i = 0; i < mov->nb_streams; i++) {
2068         if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
2069             max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
2070                                                 MOV_TIMESCALE,
2071                                                 mov->tracks[i].timescale,
2072                                                 AV_ROUND_UP);
2073             if (max_track_len < max_track_len_temp)
2074                 max_track_len = max_track_len_temp;
2075             if (max_track_id < mov->tracks[i].track_id)
2076                 max_track_id = mov->tracks[i].track_id;
2077         }
2078     }
2079
2080     version = max_track_len < UINT32_MAX ? 0 : 1;
2081     (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
2082     ffio_wfourcc(pb, "mvhd");
2083     avio_w8(pb, version);
2084     avio_wb24(pb, 0); /* flags */
2085     if (version == 1) {
2086         avio_wb64(pb, mov->time);
2087         avio_wb64(pb, mov->time);
2088     } else {
2089         avio_wb32(pb, mov->time); /* creation time */
2090         avio_wb32(pb, mov->time); /* modification time */
2091     }
2092     avio_wb32(pb, MOV_TIMESCALE);
2093     (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
2094
2095     avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
2096     avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
2097     avio_wb16(pb, 0); /* reserved */
2098     avio_wb32(pb, 0); /* reserved */
2099     avio_wb32(pb, 0); /* reserved */
2100
2101     /* Matrix structure */
2102     write_matrix(pb, 1, 0, 0, 1, 0, 0);
2103
2104     avio_wb32(pb, 0); /* reserved (preview time) */
2105     avio_wb32(pb, 0); /* reserved (preview duration) */
2106     avio_wb32(pb, 0); /* reserved (poster time) */
2107     avio_wb32(pb, 0); /* reserved (selection time) */
2108     avio_wb32(pb, 0); /* reserved (selection duration) */
2109     avio_wb32(pb, 0); /* reserved (current time) */
2110     avio_wb32(pb, max_track_id + 1); /* Next track id */
2111     return 0x6c;
2112 }
2113
2114 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
2115                                      AVFormatContext *s)
2116 {
2117     avio_wb32(pb, 33); /* size */
2118     ffio_wfourcc(pb, "hdlr");
2119     avio_wb32(pb, 0);
2120     avio_wb32(pb, 0);
2121     ffio_wfourcc(pb, "mdir");
2122     ffio_wfourcc(pb, "appl");
2123     avio_wb32(pb, 0);
2124     avio_wb32(pb, 0);
2125     avio_w8(pb, 0);
2126     return 33;
2127 }
2128
2129 /* helper function to write a data tag with the specified string as data */
2130 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
2131 {
2132     if (long_style) {
2133         int size = 16 + strlen(data);
2134         avio_wb32(pb, size); /* size */
2135         ffio_wfourcc(pb, "data");
2136         avio_wb32(pb, 1);
2137         avio_wb32(pb, 0);
2138         avio_write(pb, data, strlen(data));
2139         return size;
2140     } else {
2141         if (!lang)
2142             lang = ff_mov_iso639_to_lang("und", 1);
2143         avio_wb16(pb, strlen(data)); /* string length */
2144         avio_wb16(pb, lang);
2145         avio_write(pb, data, strlen(data));
2146         return strlen(data) + 4;
2147     }
2148 }
2149
2150 static int mov_write_string_tag(AVIOContext *pb, const char *name,
2151                                 const char *value, int lang, int long_style)
2152 {
2153     int size = 0;
2154     if (value && value[0]) {
2155         int64_t pos = avio_tell(pb);
2156         avio_wb32(pb, 0); /* size */
2157         ffio_wfourcc(pb, name);
2158         mov_write_string_data_tag(pb, value, lang, long_style);
2159         size = update_size(pb, pos);
2160     }
2161     return size;
2162 }
2163
2164 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
2165                                      const char *name, const char *tag,
2166                                      int long_style)
2167 {
2168     int l, lang = 0, len, len2;
2169     AVDictionaryEntry *t, *t2 = NULL;
2170     char tag2[16];
2171
2172     if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2173         return 0;
2174
2175     len = strlen(t->key);
2176     snprintf(tag2, sizeof(tag2), "%s-", tag);
2177     while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
2178         len2 = strlen(t2->key);
2179         if (len2 == len + 4 && !strcmp(t->value, t2->value)
2180             && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
2181             lang = l;
2182             break;
2183         }
2184     }
2185     return mov_write_string_tag(pb, name, t->value, lang, long_style);
2186 }
2187
2188 /* iTunes bpm number */
2189 static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
2190 {
2191     AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
2192     int size = 0, tmpo = t ? atoi(t->value) : 0;
2193     if (tmpo) {
2194         size = 26;
2195         avio_wb32(pb, size);
2196         ffio_wfourcc(pb, "tmpo");
2197         avio_wb32(pb, size-8); /* size */
2198         ffio_wfourcc(pb, "data");
2199         avio_wb32(pb, 0x15);  //type specifier
2200         avio_wb32(pb, 0);
2201         avio_wb16(pb, tmpo);        // data
2202     }
2203     return size;
2204 }
2205
2206 /* iTunes track or disc number */
2207 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
2208                               AVFormatContext *s, int disc)
2209 {
2210     AVDictionaryEntry *t = av_dict_get(s->metadata,
2211                                        disc ? "disc" : "track",
2212                                        NULL, 0);
2213     int size = 0, track = t ? atoi(t->value) : 0;
2214     if (track) {
2215         int tracks = 0;
2216         char *slash = strchr(t->value, '/');
2217         if (slash)
2218             tracks = atoi(slash + 1);
2219         avio_wb32(pb, 32); /* size */
2220         ffio_wfourcc(pb, disc ? "disk" : "trkn");
2221         avio_wb32(pb, 24); /* size */
2222         ffio_wfourcc(pb, "data");
2223         avio_wb32(pb, 0);        // 8 bytes empty
2224         avio_wb32(pb, 0);
2225         avio_wb16(pb, 0);        // empty
2226         avio_wb16(pb, track);    // track / disc number
2227         avio_wb16(pb, tracks);   // total track / disc number
2228         avio_wb16(pb, 0);        // empty
2229         size = 32;
2230     }
2231     return size;
2232 }
2233
2234 static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
2235                                    const char *name, const char *tag,
2236                                    int len)
2237 {
2238     AVDictionaryEntry *t = NULL;
2239     uint8_t num;
2240     int size = 24 + len;
2241
2242     if (len != 1 && len != 4)
2243         return -1;
2244
2245     if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2246         return 0;
2247     num = atoi(t->value);
2248
2249     avio_wb32(pb, size);
2250     ffio_wfourcc(pb, name);
2251     avio_wb32(pb, size - 8);
2252     ffio_wfourcc(pb, "data");
2253     avio_wb32(pb, 0x15);
2254     avio_wb32(pb, 0);
2255     if (len==4) avio_wb32(pb, num);
2256     else        avio_w8 (pb, num);
2257
2258     return size;
2259 }
2260
2261 /* iTunes meta data list */
2262 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
2263                               AVFormatContext *s)
2264 {
2265     int64_t pos = avio_tell(pb);
2266     avio_wb32(pb, 0); /* size */
2267     ffio_wfourcc(pb, "ilst");
2268     mov_write_string_metadata(s, pb, "\251nam", "title"    , 1);
2269     mov_write_string_metadata(s, pb, "\251ART", "artist"   , 1);
2270     mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
2271     mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
2272     mov_write_string_metadata(s, pb, "\251alb", "album"    , 1);
2273     mov_write_string_metadata(s, pb, "\251day", "date"     , 1);
2274     if (!mov->exact &&
2275         !mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
2276         mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
2277     mov_write_string_metadata(s, pb, "\251cmt", "comment"  , 1);
2278     mov_write_string_metadata(s, pb, "\251gen", "genre"    , 1);
2279     mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2280     mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
2281     mov_write_string_metadata(s, pb, "\251lyr", "lyrics"   , 1);
2282     mov_write_string_metadata(s, pb, "desc",    "description",1);
2283     mov_write_string_metadata(s, pb, "ldes",    "synopsis" , 1);
2284     mov_write_string_metadata(s, pb, "tvsh",    "show"     , 1);
2285     mov_write_string_metadata(s, pb, "tven",    "episode_id",1);
2286     mov_write_string_metadata(s, pb, "tvnn",    "network"  , 1);
2287     mov_write_int8_metadata  (s, pb, "tves",    "episode_sort",4);
2288     mov_write_int8_metadata  (s, pb, "tvsn",    "season_number",4);
2289     mov_write_int8_metadata  (s, pb, "stik",    "media_type",1);
2290     mov_write_int8_metadata  (s, pb, "hdvd",    "hd_video",  1);
2291     mov_write_int8_metadata  (s, pb, "pgap",    "gapless_playback",1);
2292     mov_write_int8_metadata  (s, pb, "cpil",    "compilation", 1);
2293     mov_write_trkn_tag(pb, mov, s, 0); // track number
2294     mov_write_trkn_tag(pb, mov, s, 1); // disc number
2295     mov_write_tmpo_tag(pb, s);
2296     return update_size(pb, pos);
2297 }
2298
2299 /* iTunes meta data tag */
2300 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
2301                               AVFormatContext *s)
2302 {
2303     int size = 0;
2304     int64_t pos = avio_tell(pb);
2305     avio_wb32(pb, 0); /* size */
2306     ffio_wfourcc(pb, "meta");
2307     avio_wb32(pb, 0);
2308     mov_write_itunes_hdlr_tag(pb, mov, s);
2309     mov_write_ilst_tag(pb, mov, s);
2310     size = update_size(pb, pos);
2311     return size;
2312 }
2313
2314 static int utf8len(const uint8_t *b)
2315 {
2316     int len = 0;
2317     int val;
2318     while (*b) {
2319         GET_UTF8(val, *b++, return -1;)
2320         len++;
2321     }
2322     return len;
2323 }
2324
2325 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2326 {
2327     int val;
2328     while (*b) {
2329         GET_UTF8(val, *b++, return -1;)
2330         avio_wb16(pb, val);
2331     }
2332     avio_wb16(pb, 0x00);
2333     return 0;
2334 }
2335
2336 static uint16_t language_code(const char *str)
2337 {
2338     return (((str[0] - 0x60) & 0x1F) << 10) +
2339            (((str[1] - 0x60) & 0x1F) <<  5) +
2340            (( str[2] - 0x60) & 0x1F);
2341 }
2342
2343 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
2344                                   const char *tag, const char *str)
2345 {
2346     int64_t pos = avio_tell(pb);
2347     AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
2348     if (!t || !utf8len(t->value))
2349         return 0;
2350     avio_wb32(pb, 0);   /* size */
2351     ffio_wfourcc(pb, tag); /* type */
2352     avio_wb32(pb, 0);   /* version + flags */
2353     if (!strcmp(tag, "yrrc"))
2354         avio_wb16(pb, atoi(t->value));
2355     else {
2356         avio_wb16(pb, language_code("eng")); /* language */
2357         avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
2358         if (!strcmp(tag, "albm") &&
2359             (t = av_dict_get(s->metadata, "track", NULL, 0)))
2360             avio_w8(pb, atoi(t->value));
2361     }
2362     return update_size(pb, pos);
2363 }
2364
2365 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
2366 {
2367     int64_t pos = avio_tell(pb);
2368     int i, nb_chapters = FFMIN(s->nb_chapters, 255);
2369
2370     avio_wb32(pb, 0);            // size
2371     ffio_wfourcc(pb, "chpl");
2372     avio_wb32(pb, 0x01000000);   // version + flags
2373     avio_wb32(pb, 0);            // unknown
2374     avio_w8(pb, nb_chapters);
2375
2376     for (i = 0; i < nb_chapters; i++) {
2377         AVChapter *c = s->chapters[i];
2378         AVDictionaryEntry *t;
2379         avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
2380
2381         if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2382             int len = FFMIN(strlen(t->value), 255);
2383             avio_w8(pb, len);
2384             avio_write(pb, t->value, len);
2385         } else
2386             avio_w8(pb, 0);
2387     }
2388     return update_size(pb, pos);
2389 }
2390
2391 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
2392                               AVFormatContext *s)
2393 {
2394     AVIOContext *pb_buf;
2395     int ret, size;
2396     uint8_t *buf;
2397
2398     ret = avio_open_dyn_buf(&pb_buf);
2399     if (ret < 0)
2400         return ret;
2401
2402     if (mov->mode & MODE_3GP) {
2403         mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2404         mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2405         mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2406         mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2407         mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2408         mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2409         mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2410         mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2411     } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2412         mov_write_string_metadata(s, pb_buf, "\251ART", "artist",      0);
2413         mov_write_string_metadata(s, pb_buf, "\251nam", "title",       0);
2414         mov_write_string_metadata(s, pb_buf, "\251aut", "author",      0);
2415         mov_write_string_metadata(s, pb_buf, "\251alb", "album",       0);
2416         mov_write_string_metadata(s, pb_buf, "\251day", "date",        0);
2417         mov_write_string_metadata(s, pb_buf, "\251swr", "encoder",     0);
2418         // currently ignored by mov.c
2419         mov_write_string_metadata(s, pb_buf, "\251des", "comment",     0);
2420         // add support for libquicktime, this atom is also actually read by mov.c
2421         mov_write_string_metadata(s, pb_buf, "\251cmt", "comment",     0);
2422         mov_write_string_metadata(s, pb_buf, "\251gen", "genre",       0);
2423         mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright",   0);
2424     } else {
2425         /* iTunes meta data */
2426         mov_write_meta_tag(pb_buf, mov, s);
2427     }
2428
2429     if (s->nb_chapters)
2430         mov_write_chpl_tag(pb_buf, s);
2431
2432     if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2433         avio_wb32(pb, size + 8);
2434         ffio_wfourcc(pb, "udta");
2435         avio_write(pb, buf, size);
2436     }
2437     av_free(buf);
2438
2439     return 0;
2440 }
2441
2442 static void mov_write_psp_udta_tag(AVIOContext *pb,
2443                                    const char *str, const char *lang, int type)
2444 {
2445     int len = utf8len(str) + 1;
2446     if (len <= 0)
2447         return;
2448     avio_wb16(pb, len * 2 + 10);        /* size */
2449     avio_wb32(pb, type);                /* type */
2450     avio_wb16(pb, language_code(lang)); /* language */
2451     avio_wb16(pb, 0x01);                /* ? */
2452     ascii_to_wc(pb, str);
2453 }
2454
2455 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
2456 {
2457     MOVMuxContext *mov = s->priv_data;
2458     AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2459     int64_t pos, pos2;
2460
2461     if (title) {
2462         pos = avio_tell(pb);
2463         avio_wb32(pb, 0); /* size placeholder*/
2464         ffio_wfourcc(pb, "uuid");
2465         ffio_wfourcc(pb, "USMT");
2466         avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2467         avio_wb32(pb, 0xbb88695c);
2468         avio_wb32(pb, 0xfac9c740);
2469
2470         pos2 = avio_tell(pb);
2471         avio_wb32(pb, 0); /* size placeholder*/
2472         ffio_wfourcc(pb, "MTDT");
2473         avio_wb16(pb, 4);
2474
2475         // ?
2476         avio_wb16(pb, 0x0C);                 /* size */
2477         avio_wb32(pb, 0x0B);                 /* type */
2478         avio_wb16(pb, language_code("und")); /* language */
2479         avio_wb16(pb, 0x0);                  /* ? */
2480         avio_wb16(pb, 0x021C);               /* data */
2481
2482         if (!mov->exact)
2483             mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT,      "eng", 0x04);
2484         mov_write_psp_udta_tag(pb, title->value,          "eng", 0x01);
2485         mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2486
2487         update_size(pb, pos2);
2488         return update_size(pb, pos);
2489     }
2490
2491     return 0;
2492 }
2493
2494 static void build_chunks(MOVTrack *trk)
2495 {
2496     int i;
2497     MOVIentry *chunk = &trk->cluster[0];
2498     uint64_t chunkSize = chunk->size;
2499     chunk->chunkNum = 1;
2500     if (trk->chunkCount)
2501         return;
2502     trk->chunkCount = 1;
2503     for (i = 1; i<trk->entry; i++){
2504         if (chunk->pos + chunkSize == trk->cluster[i].pos &&
2505             chunkSize + trk->cluster[i].size < (1<<20)){
2506             chunkSize             += trk->cluster[i].size;
2507             chunk->samples_in_chunk += trk->cluster[i].entries;
2508         } else {
2509             trk->cluster[i].chunkNum = chunk->chunkNum+1;
2510             chunk=&trk->cluster[i];
2511             chunkSize = chunk->size;
2512             trk->chunkCount++;
2513         }
2514     }
2515 }
2516
2517 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2518                               AVFormatContext *s)
2519 {
2520     int i;
2521     int64_t pos = avio_tell(pb);
2522     avio_wb32(pb, 0); /* size placeholder*/
2523     ffio_wfourcc(pb, "moov");
2524
2525     for (i = 0; i < mov->nb_streams; i++) {
2526         if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2527             continue;
2528
2529         mov->tracks[i].time     = mov->time;
2530         mov->tracks[i].track_id = i + 1;
2531
2532         if (mov->tracks[i].entry)
2533             build_chunks(&mov->tracks[i]);
2534     }
2535
2536     if (mov->chapter_track)
2537         for (i = 0; i < s->nb_streams; i++) {
2538             mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2539             mov->tracks[i].tref_id  = mov->tracks[mov->chapter_track].track_id;
2540         }
2541     for (i = 0; i < mov->nb_streams; i++) {
2542         if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2543             mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2544             mov->tracks[i].tref_id =
2545                 mov->tracks[mov->tracks[i].src_track].track_id;
2546         }
2547     }
2548     for (i = 0; i < mov->nb_streams; i++) {
2549         if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
2550             int src_trk = mov->tracks[i].src_track;
2551             mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
2552             mov->tracks[src_trk].tref_id  = mov->tracks[i].track_id;
2553             //src_trk may have a different timescale than the tmcd track
2554             mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
2555                                                        mov->tracks[i].timescale,
2556                                                        mov->tracks[src_trk].timescale);
2557         }
2558     }
2559
2560     mov_write_mvhd_tag(pb, mov);
2561     if (mov->mode != MODE_MOV && !mov->iods_skip)
2562         mov_write_iods_tag(pb, mov);
2563     for (i = 0; i < mov->nb_streams; i++) {
2564         if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2565             mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2566         }
2567     }
2568     if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2569         mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2570
2571     if (mov->mode == MODE_PSP)
2572         mov_write_uuidusmt_tag(pb, s);
2573     else
2574         mov_write_udta_tag(pb, mov, s);
2575
2576     return update_size(pb, pos);
2577 }
2578
2579 static void param_write_int(AVIOContext *pb, const char *name, int value)
2580 {
2581     avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2582 }
2583
2584 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2585 {
2586     avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2587 }
2588
2589 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2590 {
2591     char buf[150];
2592     len = FFMIN(sizeof(buf) / 2 - 1, len);
2593     ff_data_to_hex(buf, value, len, 0);
2594     buf[2 * len] = '\0';
2595     avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2596 }
2597
2598 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2599 {
2600     int64_t pos = avio_tell(pb);
2601     int i;
2602     static const uint8_t uuid[] = {
2603         0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2604         0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2605     };
2606
2607     avio_wb32(pb, 0);
2608     ffio_wfourcc(pb, "uuid");
2609     avio_write(pb, uuid, sizeof(uuid));
2610     avio_wb32(pb, 0);
2611
2612     avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2613     avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2614     avio_printf(pb, "<head>\n");
2615     if (!mov->exact)
2616         avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2617                     LIBAVFORMAT_IDENT);
2618     avio_printf(pb, "</head>\n");
2619     avio_printf(pb, "<body>\n");
2620     avio_printf(pb, "<switch>\n");
2621     for (i = 0; i < mov->nb_streams; i++) {
2622         MOVTrack *track = &mov->tracks[i];
2623         const char *type;
2624         /* track->track_id is initialized in write_moov, and thus isn't known
2625          * here yet */
2626         int track_id = i + 1;
2627
2628         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2629             type = "video";
2630         } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2631             type = "audio";
2632         } else {
2633             continue;
2634         }
2635         avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2636                                                        track->enc->bit_rate);
2637         param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2638         param_write_int(pb, "trackID", track_id);
2639         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2640             if (track->enc->codec_id == AV_CODEC_ID_H264) {
2641                 uint8_t *ptr;
2642                 int size = track->enc->extradata_size;
2643                 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2644                                                    &size)) {
2645                     param_write_hex(pb, "CodecPrivateData",
2646                                     ptr ? ptr : track->enc->extradata,
2647                                     size);
2648                     av_free(ptr);
2649                 }
2650                 param_write_string(pb, "FourCC", "H264");
2651             } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2652                 param_write_string(pb, "FourCC", "WVC1");
2653                 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2654                                 track->enc->extradata_size);
2655             }
2656             param_write_int(pb, "MaxWidth", track->enc->width);
2657             param_write_int(pb, "MaxHeight", track->enc->height);
2658             param_write_int(pb, "DisplayWidth", track->enc->width);
2659             param_write_int(pb, "DisplayHeight", track->enc->height);
2660         } else {
2661             if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2662                 param_write_string(pb, "FourCC", "AACL");
2663             } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2664                 param_write_string(pb, "FourCC", "WMAP");
2665             }
2666             param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2667                             track->enc->extradata_size);
2668             param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2669                                                              track->enc->codec_id));
2670             param_write_int(pb, "Channels", track->enc->channels);
2671             param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2672             param_write_int(pb, "BitsPerSample", 16);
2673             param_write_int(pb, "PacketSize", track->enc->block_align ?
2674                                               track->enc->block_align : 4);
2675         }
2676         avio_printf(pb, "</%s>\n", type);
2677     }
2678     avio_printf(pb, "</switch>\n");
2679     avio_printf(pb, "</body>\n");
2680     avio_printf(pb, "</smil>\n");
2681
2682     return update_size(pb, pos);
2683 }
2684
2685 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2686 {
2687     avio_wb32(pb, 16);
2688     ffio_wfourcc(pb, "mfhd");
2689     avio_wb32(pb, 0);
2690     avio_wb32(pb, mov->fragments);
2691     return 0;
2692 }
2693
2694 static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov,
2695                               MOVTrack *track, int64_t moof_offset)
2696 {
2697     int64_t pos = avio_tell(pb);
2698     uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2699                      MOV_TFHD_BASE_DATA_OFFSET;
2700     if (!track->entry) {
2701         flags |= MOV_TFHD_DURATION_IS_EMPTY;
2702     } else {
2703         flags |= MOV_TFHD_DEFAULT_FLAGS;
2704     }
2705     if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET)
2706         flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
2707
2708     /* Don't set a default sample size, the silverlight player refuses
2709      * to play files with that set. Don't set a default sample duration,
2710      * WMP freaks out if it is set. Don't set a base data offset, PIFF
2711      * file format says it MUST NOT be set. */
2712     if (track->mode == MODE_ISM)
2713         flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2714                    MOV_TFHD_BASE_DATA_OFFSET);
2715
2716     avio_wb32(pb, 0); /* size placeholder */
2717     ffio_wfourcc(pb, "tfhd");
2718     avio_w8(pb, 0); /* version */
2719     avio_wb24(pb, flags);
2720
2721     avio_wb32(pb, track->track_id); /* track-id */
2722     if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2723         avio_wb64(pb, moof_offset);
2724     if (flags & MOV_TFHD_DEFAULT_DURATION) {
2725         track->default_duration = get_cluster_duration(track, 0);
2726         avio_wb32(pb, track->default_duration);
2727     }
2728     if (flags & MOV_TFHD_DEFAULT_SIZE) {
2729         track->default_size = track->entry ? track->cluster[0].size : 1;
2730         avio_wb32(pb, track->default_size);
2731     } else
2732         track->default_size = -1;
2733
2734     if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2735         track->default_sample_flags =
2736             track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2737             (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2738             MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2739         avio_wb32(pb, track->default_sample_flags);
2740     }
2741
2742     return update_size(pb, pos);
2743 }
2744
2745 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2746 {
2747     return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2748            (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2749 }
2750
2751 static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov,
2752                               MOVTrack *track, int moof_size)
2753 {
2754     int64_t pos = avio_tell(pb);
2755     uint32_t flags = MOV_TRUN_DATA_OFFSET;
2756     int i;
2757
2758     for (i = 0; i < track->entry; i++) {
2759         if (get_cluster_duration(track, i) != track->default_duration)
2760             flags |= MOV_TRUN_SAMPLE_DURATION;
2761         if (track->cluster[i].size != track->default_size)
2762             flags |= MOV_TRUN_SAMPLE_SIZE;
2763         if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2764             flags |= MOV_TRUN_SAMPLE_FLAGS;
2765     }
2766     if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2767         flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2768     if (track->flags & MOV_TRACK_CTTS)
2769         flags |= MOV_TRUN_SAMPLE_CTS;
2770
2771     avio_wb32(pb, 0); /* size placeholder */
2772     ffio_wfourcc(pb, "trun");
2773     avio_w8(pb, 0); /* version */
2774     avio_wb24(pb, flags);
2775
2776     avio_wb32(pb, track->entry); /* sample count */
2777     if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
2778         !(mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) &&
2779         track->track_id != 1)
2780         avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
2781     else
2782         avio_wb32(pb, moof_size + 8 + track->data_offset +
2783                       track->cluster[0].pos); /* data offset */
2784     if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2785         avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2786
2787     for (i = 0; i < track->entry; i++) {
2788         if (flags & MOV_TRUN_SAMPLE_DURATION)
2789             avio_wb32(pb, get_cluster_duration(track, i));
2790         if (flags & MOV_TRUN_SAMPLE_SIZE)
2791             avio_wb32(pb, track->cluster[i].size);
2792         if (flags & MOV_TRUN_SAMPLE_FLAGS)
2793             avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2794         if (flags & MOV_TRUN_SAMPLE_CTS)
2795             avio_wb32(pb, track->cluster[i].cts);
2796     }
2797
2798     return update_size(pb, pos);
2799 }
2800
2801 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2802 {
2803     int64_t pos = avio_tell(pb);
2804     static const uint8_t uuid[] = {
2805         0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2806         0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2807     };
2808
2809     avio_wb32(pb, 0); /* size placeholder */
2810     ffio_wfourcc(pb, "uuid");
2811     avio_write(pb, uuid, sizeof(uuid));
2812     avio_w8(pb, 1);
2813     avio_wb24(pb, 0);
2814     avio_wb64(pb, track->frag_start);
2815     avio_wb64(pb, track->start_dts + track->track_duration -
2816                   track->cluster[0].dts);
2817
2818     return update_size(pb, pos);
2819 }
2820
2821 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2822                               MOVTrack *track, int entry)
2823 {
2824     int n = track->nb_frag_info - 1 - entry, i;
2825     int size = 8 + 16 + 4 + 1 + 16*n;
2826     static const uint8_t uuid[] = {
2827         0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2828         0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2829     };
2830
2831     if (entry < 0)
2832         return 0;
2833
2834     avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2835     avio_wb32(pb, size);
2836     ffio_wfourcc(pb, "uuid");
2837     avio_write(pb, uuid, sizeof(uuid));
2838     avio_w8(pb, 1);
2839     avio_wb24(pb, 0);
2840     avio_w8(pb, n);
2841     for (i = 0; i < n; i++) {
2842         int index = entry + 1 + i;
2843         avio_wb64(pb, track->frag_info[index].time);
2844         avio_wb64(pb, track->frag_info[index].duration);
2845     }
2846     if (n < mov->ism_lookahead) {
2847         int free_size = 16 * (mov->ism_lookahead - n);
2848         avio_wb32(pb, free_size);
2849         ffio_wfourcc(pb, "free");
2850         ffio_fill(pb, 0, free_size - 8);
2851     }
2852
2853     return 0;
2854 }
2855
2856 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2857                                MOVTrack *track)
2858 {
2859     int64_t pos = avio_tell(pb);
2860     int i;
2861     for (i = 0; i < mov->ism_lookahead; i++) {
2862         /* Update the tfrf tag for the last ism_lookahead fragments,
2863          * nb_frag_info - 1 is the next fragment to be written. */
2864         mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2865     }
2866     avio_seek(pb, pos, SEEK_SET);
2867     return 0;
2868 }
2869
2870 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2871                               MOVTrack *track, int64_t moof_offset,
2872                               int moof_size)
2873 {
2874     int64_t pos = avio_tell(pb);
2875     avio_wb32(pb, 0); /* size placeholder */
2876     ffio_wfourcc(pb, "traf");
2877
2878     mov_write_tfhd_tag(pb, mov, track, moof_offset);
2879     mov_write_trun_tag(pb, mov, track, moof_size);
2880     if (mov->mode == MODE_ISM) {
2881         mov_write_tfxd_tag(pb, track);
2882
2883         if (mov->ism_lookahead) {
2884             int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
2885
2886             track->tfrf_offset = avio_tell(pb);
2887             avio_wb32(pb, 8 + size);
2888             ffio_wfourcc(pb, "free");
2889             for (i = 0; i < size; i++)
2890                 avio_w8(pb, 0);
2891         }
2892     }
2893
2894     return update_size(pb, pos);
2895 }
2896
2897 static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
2898                                        int tracks, int moof_size)
2899 {
2900     int64_t pos = avio_tell(pb);
2901     int i;
2902
2903     avio_wb32(pb, 0); /* size placeholder */
2904     ffio_wfourcc(pb, "moof");
2905
2906     mov_write_mfhd_tag(pb, mov);
2907     for (i = 0; i < mov->nb_streams; i++) {
2908         MOVTrack *track = &mov->tracks[i];
2909         if (tracks >= 0 && i != tracks)
2910             continue;
2911         if (!track->entry)
2912             continue;
2913         mov_write_traf_tag(pb, mov, track, pos, moof_size);
2914     }
2915
2916     return update_size(pb, pos);
2917 }
2918
2919 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2920 {
2921     AVIOContext *avio_buf;
2922     int ret, moof_size;
2923
2924     if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
2925         return ret;
2926     mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
2927     moof_size = ffio_close_null_buf(avio_buf);
2928     return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
2929 }
2930
2931 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2932 {
2933     int64_t pos = avio_tell(pb);
2934     int i;
2935
2936     avio_wb32(pb, 0); /* size placeholder */
2937     ffio_wfourcc(pb, "tfra");
2938     avio_w8(pb, 1); /* version */
2939     avio_wb24(pb, 0);
2940
2941     avio_wb32(pb, track->track_id);
2942     avio_wb32(pb, 0); /* length of traf/trun/sample num */
2943     avio_wb32(pb, track->nb_frag_info);
2944     for (i = 0; i < track->nb_frag_info; i++) {
2945         avio_wb64(pb, track->frag_info[i].time);
2946         avio_wb64(pb, track->frag_info[i].offset);
2947         avio_w8(pb, 1); /* traf number */
2948         avio_w8(pb, 1); /* trun number */
2949         avio_w8(pb, 1); /* sample number */
2950     }
2951
2952     return update_size(pb, pos);
2953 }
2954
2955 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2956 {
2957     int64_t pos = avio_tell(pb);
2958     int i;
2959
2960     avio_wb32(pb, 0); /* size placeholder */
2961     ffio_wfourcc(pb, "mfra");
2962     /* An empty mfra atom is enough to indicate to the publishing point that
2963      * the stream has ended. */
2964     if (mov->flags & FF_MOV_FLAG_ISML)
2965         return update_size(pb, pos);
2966
2967     for (i = 0; i < mov->nb_streams; i++) {
2968         MOVTrack *track = &mov->tracks[i];
2969         if (track->nb_frag_info)
2970             mov_write_tfra_tag(pb, track);
2971     }
2972
2973     avio_wb32(pb, 16);
2974     ffio_wfourcc(pb, "mfro");
2975     avio_wb32(pb, 0); /* version + flags */
2976     avio_wb32(pb, avio_tell(pb) + 4 - pos);
2977
2978     return update_size(pb, pos);
2979 }
2980
2981 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2982 {
2983     avio_wb32(pb, 8);    // placeholder for extended size field (64 bit)
2984     ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2985
2986     mov->mdat_pos = avio_tell(pb);
2987     avio_wb32(pb, 0); /* size placeholder*/
2988     ffio_wfourcc(pb, "mdat");
2989     return 0;
2990 }
2991
2992 /* TODO: This needs to be more general */
2993 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2994 {
2995     MOVMuxContext *mov = s->priv_data;
2996     int64_t pos = avio_tell(pb);
2997     int has_h264 = 0, has_video = 0;
2998     int minor = 0x200;
2999     int i;
3000
3001     for (i = 0; i < s->nb_streams; i++) {
3002         AVStream *st = s->streams[i];
3003         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3004             has_video = 1;
3005         if (st->codec->codec_id == AV_CODEC_ID_H264)
3006             has_h264 = 1;
3007     }
3008
3009     avio_wb32(pb, 0); /* size */
3010     ffio_wfourcc(pb, "ftyp");
3011
3012     if (mov->major_brand && strlen(mov->major_brand) >= 4)
3013         ffio_wfourcc(pb, mov->major_brand);
3014     else if (mov->mode == MODE_3GP) {
3015         ffio_wfourcc(pb, has_h264 ? "3gp6"  : "3gp4");
3016         minor =     has_h264 ?   0x100 :   0x200;
3017     } else if (mov->mode & MODE_3G2) {
3018         ffio_wfourcc(pb, has_h264 ? "3g2b"  : "3g2a");
3019         minor =     has_h264 ? 0x20000 : 0x10000;
3020     } else if (mov->mode == MODE_PSP)
3021         ffio_wfourcc(pb, "MSNV");
3022     else if (mov->mode == MODE_MP4)
3023         ffio_wfourcc(pb, "isom");
3024     else if (mov->mode == MODE_IPOD)
3025         ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
3026     else if (mov->mode == MODE_ISM)
3027         ffio_wfourcc(pb, "isml");
3028     else if (mov->mode == MODE_F4V)
3029         ffio_wfourcc(pb, "f4v ");
3030     else
3031         ffio_wfourcc(pb, "qt  ");
3032
3033     avio_wb32(pb, minor);
3034
3035     if (mov->mode == MODE_MOV)
3036         ffio_wfourcc(pb, "qt  ");
3037     else if (mov->mode == MODE_ISM) {
3038         ffio_wfourcc(pb, "piff");
3039         ffio_wfourcc(pb, "iso2");
3040     } else {
3041         ffio_wfourcc(pb, "isom");
3042         ffio_wfourcc(pb, "iso2");
3043         if (has_h264)
3044             ffio_wfourcc(pb, "avc1");
3045     }
3046
3047     if (mov->mode == MODE_3GP)
3048         ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
3049     else if (mov->mode & MODE_3G2)
3050         ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
3051     else if (mov->mode == MODE_PSP)
3052         ffio_wfourcc(pb, "MSNV");
3053     else if (mov->mode == MODE_MP4)
3054         ffio_wfourcc(pb, "mp41");
3055     return update_size(pb, pos);
3056 }
3057
3058 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
3059 {
3060     AVCodecContext *video_codec = s->streams[0]->codec;
3061     AVCodecContext *audio_codec = s->streams[1]->codec;
3062     int audio_rate = audio_codec->sample_rate;
3063     int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
3064     int audio_kbitrate = audio_codec->bit_rate / 1000;
3065     int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
3066
3067     avio_wb32(pb, 0x94); /* size */
3068     ffio_wfourcc(pb, "uuid");
3069     ffio_wfourcc(pb, "PROF");
3070
3071     avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3072     avio_wb32(pb, 0xbb88695c);
3073     avio_wb32(pb, 0xfac9c740);
3074
3075     avio_wb32(pb, 0x0);  /* ? */
3076     avio_wb32(pb, 0x3);  /* 3 sections ? */
3077
3078     avio_wb32(pb, 0x14); /* size */
3079     ffio_wfourcc(pb, "FPRF");
3080     avio_wb32(pb, 0x0);  /* ? */
3081     avio_wb32(pb, 0x0);  /* ? */
3082     avio_wb32(pb, 0x0);  /* ? */
3083
3084     avio_wb32(pb, 0x2c);  /* size */
3085     ffio_wfourcc(pb, "APRF"); /* audio */
3086     avio_wb32(pb, 0x0);
3087     avio_wb32(pb, 0x2);   /* TrackID */
3088     ffio_wfourcc(pb, "mp4a");
3089     avio_wb32(pb, 0x20f);
3090     avio_wb32(pb, 0x0);
3091     avio_wb32(pb, audio_kbitrate);
3092     avio_wb32(pb, audio_kbitrate);
3093     avio_wb32(pb, audio_rate);
3094     avio_wb32(pb, audio_codec->channels);
3095
3096     avio_wb32(pb, 0x34);  /* size */
3097     ffio_wfourcc(pb, "VPRF");   /* video */
3098     avio_wb32(pb, 0x0);
3099     avio_wb32(pb, 0x1);    /* TrackID */
3100     if (video_codec->codec_id == AV_CODEC_ID_H264) {
3101         ffio_wfourcc(pb, "avc1");
3102         avio_wb16(pb, 0x014D);
3103         avio_wb16(pb, 0x0015);
3104     } else {
3105         ffio_wfourcc(pb, "mp4v");
3106         avio_wb16(pb, 0x0000);
3107         avio_wb16(pb, 0x0103);
3108     }
3109     avio_wb32(pb, 0x0);
3110     avio_wb32(pb, video_kbitrate);
3111     avio_wb32(pb, video_kbitrate);
3112     avio_wb32(pb, frame_rate);
3113     avio_wb32(pb, frame_rate);
3114     avio_wb16(pb, video_codec->width);
3115     avio_wb16(pb, video_codec->height);
3116     avio_wb32(pb, 0x010001); /* ? */
3117 }
3118
3119 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
3120 {
3121     uint32_t c = -1;
3122     int i, closed_gop = 0;
3123
3124     for (i = 0; i < pkt->size - 4; i++) {
3125         c = (c << 8) + pkt->data[i];
3126         if (c == 0x1b8) { // gop
3127             closed_gop = pkt->data[i + 4] >> 6 & 0x01;
3128         } else if (c == 0x100) { // pic
3129             int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
3130             if (!temp_ref || closed_gop) // I picture is not reordered
3131                 *flags = MOV_SYNC_SAMPLE;
3132             else
3133                 *flags = MOV_PARTIAL_SYNC_SAMPLE;
3134             break;
3135         }
3136     }
3137     return 0;
3138 }
3139
3140 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
3141 {
3142     const uint8_t *start, *next, *end = pkt->data + pkt->size;
3143     int seq = 0, entry = 0;
3144     int key = pkt->flags & AV_PKT_FLAG_KEY;
3145     start = find_next_marker(pkt->data, end);
3146     for (next = start; next < end; start = next) {
3147         next = find_next_marker(start + 4, end);
3148         switch (AV_RB32(start)) {
3149         case VC1_CODE_SEQHDR:
3150             seq = 1;
3151             break;
3152         case VC1_CODE_ENTRYPOINT:
3153             entry = 1;
3154             break;
3155         case VC1_CODE_SLICE:
3156             trk->vc1_info.slices = 1;
3157             break;
3158         }
3159     }
3160     if (!trk->entry && !fragment) {
3161         /* First packet in first fragment */
3162         trk->vc1_info.first_packet_seq   = seq;
3163         trk->vc1_info.first_packet_entry = entry;
3164     } else if ((seq && !trk->vc1_info.packet_seq) ||
3165                (entry && !trk->vc1_info.packet_entry)) {
3166         int i;
3167         for (i = 0; i < trk->entry; i++)
3168             trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
3169         trk->has_keyframes = 0;
3170         if (seq)
3171             trk->vc1_info.packet_seq = 1;
3172         if (entry)
3173             trk->vc1_info.packet_entry = 1;
3174         if (!fragment) {
3175             /* First fragment */
3176             if ((!seq   || trk->vc1_info.first_packet_seq) &&
3177                 (!entry || trk->vc1_info.first_packet_entry)) {
3178                 /* First packet had the same headers as this one, readd the
3179                  * sync sample flag. */
3180                 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
3181                 trk->has_keyframes = 1;
3182             }
3183         }
3184     }
3185     if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
3186         key = seq && entry;
3187     else if (trk->vc1_info.packet_seq)
3188         key = seq;
3189     else if (trk->vc1_info.packet_entry)
3190         key = entry;
3191     if (key) {
3192         trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
3193         trk->has_keyframes++;
3194     }
3195 }
3196
3197 static int mov_flush_fragment(AVFormatContext *s)
3198 {
3199     MOVMuxContext *mov = s->priv_data;
3200     int i, first_track = -1;
3201     int64_t mdat_size = 0;
3202
3203     if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
3204         return 0;
3205
3206     if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
3207         int64_t pos = avio_tell(s->pb);
3208         uint8_t *buf;
3209         int buf_size, moov_size;
3210
3211         for (i = 0; i < mov->nb_streams; i++)
3212             if (!mov->tracks[i].entry)
3213                 break;
3214         /* Don't write the initial moov unless all tracks have data */
3215         if (i < mov->nb_streams)
3216             return 0;
3217
3218         moov_size = get_moov_size(s);
3219         for (i = 0; i < mov->nb_streams; i++)
3220             mov->tracks[i].data_offset = pos + moov_size + 8;
3221
3222         mov_write_moov_tag(s->pb, mov, s);
3223
3224         buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
3225         mov->mdat_buf = NULL;
3226         avio_wb32(s->pb, buf_size + 8);
3227         ffio_wfourcc(s->pb, "mdat");
3228         avio_write(s->pb, buf, buf_size);
3229         av_free(buf);
3230
3231         mov->fragments++;
3232         mov->mdat_size = 0;
3233         for (i = 0; i < mov->nb_streams; i++) {
3234             if (mov->tracks[i].entry)
3235                 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
3236                                              mov->tracks[i].track_duration -
3237                                              mov->tracks[i].cluster[0].dts;
3238             mov->tracks[i].entry = 0;
3239         }
3240         avio_flush(s->pb);
3241         return 0;
3242     }
3243
3244     for (i = 0; i < mov->nb_streams; i++) {
3245         MOVTrack *track = &mov->tracks[i];
3246         if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
3247             track->data_offset = 0;
3248         else
3249             track->data_offset = mdat_size;
3250         if (!track->mdat_buf)
3251             continue;
3252         mdat_size += avio_tell(track->mdat_buf);
3253         if (first_track < 0)
3254             first_track = i;
3255     }
3256
3257     if (!mdat_size)
3258         return 0;
3259
3260     for (i = 0; i < mov->nb_streams; i++) {
3261         MOVTrack *track = &mov->tracks[i];
3262         int buf_size, write_moof = 1, moof_tracks = -1;
3263         uint8_t *buf;
3264         int64_t duration = 0;
3265
3266         if (track->entry)
3267             duration = track->start_dts + track->track_duration -
3268                        track->cluster[0].dts;
3269         if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
3270             if (!track->mdat_buf)
3271                 continue;
3272             mdat_size = avio_tell(track->mdat_buf);
3273             moof_tracks = i;
3274         } else {
3275             write_moof = i == first_track;
3276         }
3277
3278         if (write_moof) {
3279             MOVFragmentInfo *info;
3280             avio_flush(s->pb);
3281             track->nb_frag_info++;
3282             if (track->nb_frag_info >= track->frag_info_capacity) {
3283                 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
3284                 if (av_reallocp_array(&track->frag_info,
3285                                       new_capacity,
3286                                       sizeof(*track->frag_info)))
3287                     return AVERROR(ENOMEM);
3288                 track->frag_info_capacity = new_capacity;
3289             }
3290             info = &track->frag_info[track->nb_frag_info - 1];
3291             info->offset   = avio_tell(s->pb);
3292             info->time     = mov->tracks[i].frag_start;
3293             info->duration = duration;
3294             mov_write_tfrf_tags(s->pb, mov, track);
3295
3296             mov_write_moof_tag(s->pb, mov, moof_tracks);
3297             info->tfrf_offset = track->tfrf_offset;
3298             mov->fragments++;
3299
3300             avio_wb32(s->pb, mdat_size + 8);
3301             ffio_wfourcc(s->pb, "mdat");
3302         }
3303
3304         if (track->entry)
3305             track->frag_start += duration;
3306         track->entry = 0;
3307         if (!track->mdat_buf)
3308             continue;
3309         buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
3310         track->mdat_buf = NULL;
3311
3312         avio_write(s->pb, buf, buf_size);
3313         av_free(buf);
3314     }
3315
3316     mov->mdat_size = 0;
3317
3318     avio_flush(s->pb);
3319     return 0;
3320 }
3321
3322 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
3323 {
3324     MOVMuxContext *mov = s->priv_data;
3325     AVIOContext *pb = s->pb;
3326     MOVTrack *trk = &mov->tracks[pkt->stream_index];
3327     AVCodecContext *enc = trk->enc;
3328     unsigned int samples_in_chunk = 0;
3329     int size = pkt->size;
3330     uint8_t *reformatted_data = NULL;
3331
3332     if (trk->entry) {
3333         int64_t duration = pkt->dts - trk->cluster[trk->entry - 1].dts;
3334         if (duration < 0 || duration > INT_MAX) {
3335             av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
3336                 duration, pkt->dts
3337             );
3338
3339             pkt->dts = trk->cluster[trk->entry - 1].dts + 1;
3340             pkt->pts = AV_NOPTS_VALUE;
3341         }
3342         if (pkt->duration < 0) {
3343             av_log(s, AV_LOG_ERROR, "Application provided duration: %d is invalid\n", pkt->duration);
3344             return AVERROR(EINVAL);
3345         }
3346     }
3347     if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3348         int ret;
3349         if (mov->fragments > 0) {
3350             if (!trk->mdat_buf) {
3351                 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
3352                     return ret;
3353             }
3354             pb = trk->mdat_buf;
3355         } else {
3356             if (!mov->mdat_buf) {
3357                 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
3358                     return ret;
3359             }
3360             pb = mov->mdat_buf;
3361         }
3362     }
3363
3364     if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
3365         /* We must find out how many AMR blocks there are in one packet */
3366         static uint16_t packed_size[16] =
3367             {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
3368         int len = 0;
3369
3370         while (len < size && samples_in_chunk < 100) {
3371             len += packed_size[(pkt->data[len] >> 3) & 0x0F];
3372             samples_in_chunk++;
3373         }
3374         if (samples_in_chunk > 1) {
3375             av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
3376             return -1;
3377         }
3378     } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
3379                enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
3380         samples_in_chunk = enc->frame_size;
3381     } else if (trk->sample_size)
3382         samples_in_chunk = size / trk->sample_size;
3383     else
3384         samples_in_chunk = 1;
3385
3386     /* copy extradata if it exists */
3387     if (trk->vos_len == 0 && enc->extradata_size > 0) {
3388         trk->vos_len  = enc->extradata_size;
3389         trk->vos_data = av_malloc(trk->vos_len);
3390         memcpy(trk->vos_data, enc->extradata, trk->vos_len);
3391     }
3392
3393     if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
3394         (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
3395         if (!s->streams[pkt->stream_index]->nb_frames) {
3396             av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
3397                    "use audio bitstream filter 'aac_adtstoasc' to fix it "
3398                    "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
3399             return -1;
3400         }
3401         av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
3402     }
3403     if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
3404         /* from x264 or from bytestream h264 */
3405         /* nal reformating needed */
3406         if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3407             ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
3408                                        &size);
3409             avio_write(pb, reformatted_data, size);
3410         } else {
3411             size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
3412         }
3413     } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
3414                (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
3415         /* extradata is Annex B, assume the bitstream is too and convert it */
3416         if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3417             ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
3418             avio_write(pb, reformatted_data, size);
3419         } else {
3420             size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
3421         }
3422     } else {
3423         avio_write(pb, pkt->data, size);
3424     }
3425
3426     if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
3427          enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
3428         /* copy frame to create needed atoms */
3429         trk->vos_len  = size;
3430         trk->vos_data = av_malloc(size);
3431         if (!trk->vos_data)
3432             return AVERROR(ENOMEM);
3433         memcpy(trk->vos_data, pkt->data, size);
3434     }
3435
3436     if (trk->entry >= trk->cluster_capacity) {
3437         unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
3438         if (av_reallocp_array(&trk->cluster, new_capacity,
3439                               sizeof(*trk->cluster)))
3440             return AVERROR(ENOMEM);
3441         trk->cluster_capacity = new_capacity;
3442     }
3443
3444     trk->cluster[trk->entry].pos              = avio_tell(pb) - size;
3445     trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
3446     trk->cluster[trk->entry].chunkNum         = 0;
3447     trk->cluster[trk->entry].size             = size;
3448     trk->cluster[trk->entry].entries          = samples_in_chunk;
3449     trk->cluster[trk->entry].dts              = pkt->dts;
3450     if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
3451         /* First packet of a new fragment. We already wrote the duration
3452          * of the last packet of the previous fragment based on track_duration,
3453          * which might not exactly match our dts. Therefore adjust the dts
3454          * of this packet to be what the previous packets duration implies. */
3455         trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
3456     }
3457     if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !supports_edts(mov)) {
3458         trk->cluster[trk->entry].dts = trk->start_dts = 0;
3459     }
3460     if (trk->start_dts == AV_NOPTS_VALUE)
3461         trk->start_dts = pkt->dts;
3462     trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
3463     trk->last_sample_is_subtitle_end = 0;
3464
3465     if (pkt->pts == AV_NOPTS_VALUE) {
3466         av_log(s, AV_LOG_WARNING, "pts has no value\n");
3467         pkt->pts = pkt->dts;
3468     }
3469     if (pkt->dts != pkt->pts)
3470         trk->flags |= MOV_TRACK_CTTS;
3471     trk->cluster[trk->entry].cts   = pkt->pts - pkt->dts;
3472     trk->cluster[trk->entry].flags = 0;
3473     if (enc->codec_id == AV_CODEC_ID_VC1) {
3474         mov_parse_vc1_frame(pkt, trk, mov->fragments);
3475     } else if (pkt->flags & AV_PKT_FLAG_KEY) {
3476         if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
3477             trk->entry > 0) { // force sync sample for the first key frame
3478             mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3479             if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3480                 trk->flags |= MOV_TRACK_STPS;
3481         } else {
3482             trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3483         }
3484         if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
3485             trk->has_keyframes++;
3486     }
3487     trk->entry++;
3488     trk->sample_count += samples_in_chunk;
3489     mov->mdat_size    += size;
3490
3491     if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
3492         ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3493                                  reformatted_data, size);
3494     av_free(reformatted_data);
3495     return 0;
3496 }
3497
3498 static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
3499 {
3500         MOVMuxContext *mov = s->priv_data;
3501         MOVTrack *trk = &mov->tracks[pkt->stream_index];
3502         AVCodecContext *enc = trk->enc;
3503         int64_t frag_duration = 0;
3504         int size = pkt->size;
3505
3506         if (!pkt->size)
3507             return 0;             /* Discard 0 sized packets */
3508
3509         if (trk->entry && pkt->stream_index < s->nb_streams)
3510             frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3511                                          s->streams[pkt->stream_index]->time_base,
3512                                          AV_TIME_BASE_Q);
3513         if ((mov->max_fragment_duration &&
3514              frag_duration >= mov->max_fragment_duration) ||
3515              (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3516              (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3517               enc->codec_type == AVMEDIA_TYPE_VIDEO &&
3518               trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3519             if (frag_duration >= mov->min_fragment_duration)
3520                 mov_flush_fragment(s);
3521         }
3522
3523         return ff_mov_write_packet(s, pkt);
3524 }
3525
3526 static int mov_write_subtitle_end_packet(AVFormatContext *s,
3527                                          int stream_index,
3528                                          int64_t dts) {
3529     AVPacket end;
3530     uint8_t data[2] = {0};
3531     int ret;
3532
3533     av_init_packet(&end);
3534     end.size = sizeof(data);
3535     end.data = data;
3536     end.pts = dts;
3537     end.dts = dts;
3538     end.duration = 0;
3539     end.stream_index = stream_index;
3540
3541     ret = mov_write_single_packet(s, &end);
3542     av_free_packet(&end);
3543
3544     return ret;
3545 }
3546
3547 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
3548 {
3549     if (!pkt) {
3550         mov_flush_fragment(s);
3551         return 1;
3552     } else {
3553         int i;
3554         MOVMuxContext *mov = s->priv_data;
3555
3556         if (!pkt->size) return 0; /* Discard 0 sized packets */
3557
3558         /*
3559          * Subtitles require special handling.
3560          *
3561          * 1) For full complaince, every track must have a sample at
3562          * dts == 0, which is rarely true for subtitles. So, as soon
3563          * as we see any packet with dts > 0, write an empty subtitle
3564          * at dts == 0 for any subtitle track with no samples in it.
3565          *
3566          * 2) For each subtitle track, check if the current packet's
3567          * dts is past the duration of the last subtitle sample. If
3568          * so, we now need to write an end sample for that subtitle.
3569          *
3570          * This must be done conditionally to allow for subtitles that
3571          * immediately replace each other, in which case an end sample
3572          * is not needed, and is, in fact, actively harmful.
3573          *
3574          * 3) See mov_write_trailer for how the final end sample is
3575          * handled.
3576          */
3577         for (i = 0; i < mov->nb_streams; i++) {
3578             MOVTrack *trk = &mov->tracks[i];
3579             int ret;
3580
3581             if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
3582                 trk->track_duration < pkt->dts &&
3583                 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
3584                 ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
3585                 if (ret < 0) return ret;
3586                 trk->last_sample_is_subtitle_end = 1;
3587             }
3588         }
3589
3590         return mov_write_single_packet(s, pkt);
3591     }
3592 }
3593
3594 // QuickTime chapters involve an additional text track with the chapter names
3595 // as samples, and a tref pointing from the other tracks to the chapter one.
3596 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
3597 {
3598     AVIOContext *pb;
3599
3600     MOVMuxContext *mov = s->priv_data;
3601     MOVTrack *track = &mov->tracks[tracknum];
3602     AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
3603     int i, len;
3604
3605     track->mode = mov->mode;
3606     track->tag = MKTAG('t','e','x','t');
3607     track->timescale = MOV_TIMESCALE;
3608     track->enc = avcodec_alloc_context3(NULL);
3609     if (!track->enc)
3610         return AVERROR(ENOMEM);
3611     track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
3612 #if 0
3613     // These properties are required to make QT recognize the chapter track
3614     uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
3615     if (ff_alloc_extradata(track->enc, sizeof(chapter_properties)))
3616         return AVERROR(ENOMEM);
3617     memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
3618 #else
3619     if (avio_open_dyn_buf(&pb) >= 0) {
3620         int size;
3621         uint8_t *buf;
3622
3623         /* Stub header (usually for Quicktime chapter track) */
3624         // TextSampleEntry
3625         avio_wb32(pb, 0x01); // displayFlags
3626         avio_w8(pb, 0x00);   // horizontal justification
3627         avio_w8(pb, 0x00);   // vertical justification
3628         avio_w8(pb, 0x00);   // bgColourRed
3629         avio_w8(pb, 0x00);   // bgColourGreen
3630         avio_w8(pb, 0x00);   // bgColourBlue
3631         avio_w8(pb, 0x00);   // bgColourAlpha
3632         // BoxRecord
3633         avio_wb16(pb, 0x00); // defTextBoxTop
3634         avio_wb16(pb, 0x00); // defTextBoxLeft
3635         avio_wb16(pb, 0x00); // defTextBoxBottom
3636         avio_wb16(pb, 0x00); // defTextBoxRight
3637         // StyleRecord
3638         avio_wb16(pb, 0x00); // startChar
3639         avio_wb16(pb, 0x00); // endChar
3640         avio_wb16(pb, 0x01); // fontID
3641         avio_w8(pb, 0x00);   // fontStyleFlags
3642         avio_w8(pb, 0x00);   // fontSize
3643         avio_w8(pb, 0x00);   // fgColourRed
3644         avio_w8(pb, 0x00);   // fgColourGreen
3645         avio_w8(pb, 0x00);   // fgColourBlue
3646         avio_w8(pb, 0x00);   // fgColourAlpha
3647         // FontTableBox
3648         avio_wb32(pb, 0x0D); // box size
3649         ffio_wfourcc(pb, "ftab"); // box atom name
3650         avio_wb16(pb, 0x01); // entry count
3651         // FontRecord
3652         avio_wb16(pb, 0x01); // font ID
3653         avio_w8(pb, 0x00);   // font name length
3654
3655         if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
3656             track->enc->extradata = buf;
3657             track->enc->extradata_size = size;
3658         } else {
3659             av_freep(&buf);
3660         }
3661     }
3662 #endif
3663
3664     for (i = 0; i < s->nb_chapters; i++) {
3665         AVChapter *c = s->chapters[i];
3666         AVDictionaryEntry *t;
3667
3668         int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3669         pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3670         pkt.duration = end - pkt.dts;
3671
3672         if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3673             len      = strlen(t->value);
3674             pkt.size = len + 2;
3675             pkt.data = av_malloc(pkt.size);
3676             if (!pkt.data)
3677                 return AVERROR(ENOMEM);
3678             AV_WB16(pkt.data, len);
3679             memcpy(pkt.data + 2, t->value, len);
3680             ff_mov_write_packet(s, &pkt);
3681             av_freep(&pkt.data);
3682         }
3683     }
3684
3685     return 0;
3686 }
3687
3688 static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
3689 {
3690     int ret;
3691     MOVMuxContext *mov  = s->priv_data;
3692     MOVTrack *track     = &mov->tracks[index];
3693     AVStream *src_st    = s->streams[src_index];
3694     AVTimecode tc;
3695     AVPacket pkt    = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
3696     AVRational rate = find_fps(s, src_st);
3697
3698     /* compute the frame number */
3699     ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
3700     if (ret < 0)
3701         return ret;
3702
3703     /* tmcd track based on video stream */
3704     track->mode      = mov->mode;
3705     track->tag       = MKTAG('t','m','c','d');
3706     track->src_track = src_index;
3707     track->timescale = mov->tracks[src_index].timescale;
3708     if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
3709         track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
3710
3711     /* encode context: tmcd data stream */
3712     track->enc = avcodec_alloc_context3(NULL);
3713     track->enc->codec_type = AVMEDIA_TYPE_DATA;
3714     track->enc->codec_tag  = track->tag;
3715     track->enc->time_base  = av_inv_q(rate);
3716
3717     /* the tmcd track just contains one packet with the frame number */
3718     pkt.data = av_malloc(pkt.size);
3719     AV_WB32(pkt.data, tc.start);
3720     ret = ff_mov_write_packet(s, &pkt);
3721     av_free(pkt.data);
3722     return ret;
3723 }
3724
3725 /*
3726  * st->disposition controls the "enabled" flag in the tkhd tag.
3727  * QuickTime will not play a track if it is not enabled.  So make sure
3728  * that one track of each type (audio, video, subtitle) is enabled.
3729  *
3730  * Subtitles are special.  For audio and video, setting "enabled" also
3731  * makes the track "default" (i.e. it is rendered when played). For
3732  * subtitles, an "enabled" subtitle is not rendered by default, but
3733  * if no subtitle is enabled, the subtitle menu in QuickTime will be
3734  * empty!
3735  */
3736 static void enable_tracks(AVFormatContext *s)
3737 {
3738     MOVMuxContext *mov = s->priv_data;
3739     int i;
3740     uint8_t enabled[AVMEDIA_TYPE_NB];
3741     int first[AVMEDIA_TYPE_NB];
3742
3743     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3744         enabled[i] = 0;
3745         first[i] = -1;
3746     }
3747
3748     for (i = 0; i < s->nb_streams; i++) {
3749         AVStream *st = s->streams[i];
3750
3751         if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
3752             st->codec->codec_type >= AVMEDIA_TYPE_NB)
3753             continue;
3754
3755         if (first[st->codec->codec_type] < 0)
3756             first[st->codec->codec_type] = i;
3757         if (st->disposition & AV_DISPOSITION_DEFAULT) {
3758             mov->tracks[i].flags |= MOV_TRACK_ENABLED;
3759             enabled[st->codec->codec_type] = 1;
3760         }
3761     }
3762
3763     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3764         switch (i) {
3765         case AVMEDIA_TYPE_VIDEO:
3766         case AVMEDIA_TYPE_AUDIO:
3767         case AVMEDIA_TYPE_SUBTITLE:
3768             if (!enabled[i] && first[i] >= 0)
3769                 mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
3770             break;
3771         }
3772     }
3773 }
3774
3775 static void mov_free(AVFormatContext *s)
3776 {
3777     MOVMuxContext *mov = s->priv_data;
3778     int i;
3779
3780     if (mov->chapter_track) {
3781         if (mov->tracks[mov->chapter_track].enc)
3782             av_freep(&mov->tracks[mov->chapter_track].enc->extradata);
3783         av_freep(&mov->tracks[mov->chapter_track].enc);
3784     }
3785
3786     for (i = 0; i < mov->nb_streams; i++) {
3787         if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3788             ff_mov_close_hinting(&mov->tracks[i]);
3789         else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
3790             av_freep(&mov->tracks[i].enc);
3791         av_freep(&mov->tracks[i].cluster);
3792         av_freep(&mov->tracks[i].frag_info);
3793
3794         if (mov->tracks[i].vos_len)
3795             av_freep(&mov->tracks[i].vos_data);
3796     }
3797
3798     av_freep(&mov->tracks);
3799 }
3800
3801 static uint32_t rgb_to_yuv(uint32_t rgb)
3802 {
3803     uint8_t r, g, b;
3804     int y, cb, cr;
3805
3806     r = (rgb >> 16) & 0xFF;
3807     g = (rgb >>  8) & 0xFF;
3808     b = (rgb      ) & 0xFF;
3809
3810     y  = av_clip_uint8( 16. +  0.257 * r + 0.504 * g + 0.098 * b);
3811     cb = av_clip_uint8(128. -  0.148 * r - 0.291 * g + 0.439 * b);
3812     cr = av_clip_uint8(128. +  0.439 * r - 0.368 * g - 0.071 * b);
3813
3814     return (y << 16) | (cr << 8) | cb;
3815 }
3816
3817 static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track,
3818                                                     AVStream *st)
3819 {
3820     int i, width = 720, height = 480;
3821     int have_palette = 0, have_size = 0;
3822     uint32_t palette[16];
3823     char *cur = st->codec->extradata;
3824
3825     while (cur && *cur) {
3826         if (strncmp("palette:", cur, 8) == 0) {
3827             int i, count;
3828             count = sscanf(cur + 8,
3829                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3830                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3831                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3832                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
3833                 &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
3834                 &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
3835                 &palette[ 8], &palette[ 9], &palette[10], &palette[11],
3836                 &palette[12], &palette[13], &palette[14], &palette[15]);
3837
3838             for (i = 0; i < count; i++) {
3839                 palette[i] = rgb_to_yuv(palette[i]);
3840             }
3841             have_palette = 1;
3842         } else if (!strncmp("size:", cur, 5)) {
3843             sscanf(cur + 5, "%dx%d", &width, &height);
3844             have_size = 1;
3845         }
3846         if (have_palette && have_size)
3847             break;
3848         cur += strcspn(cur, "\n\r");
3849         cur += strspn(cur, "\n\r");
3850     }
3851     if (have_palette) {
3852         track->vos_data = av_malloc(16*4);
3853         if (!track->vos_data)
3854             return AVERROR(ENOMEM);
3855         for (i = 0; i < 16; i++) {
3856             AV_WB32(track->vos_data + i * 4, palette[i]);
3857         }
3858         track->vos_len = 16 * 4;
3859     }
3860     st->codec->width = width;
3861     st->codec->height = track->height = height;
3862
3863     return 0;
3864 }
3865
3866 static int mov_write_header(AVFormatContext *s)
3867 {
3868     AVIOContext *pb = s->pb;
3869     MOVMuxContext *mov = s->priv_data;
3870     AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
3871     int i, ret, hint_track = 0, tmcd_track = 0;
3872
3873     /* Default mode == MP4 */
3874     mov->mode = MODE_MP4;
3875
3876     if (s->oformat != NULL) {
3877         if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3878         else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3879         else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3880         else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3881         else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3882         else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3883         else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
3884     }
3885
3886     for (i = 0; i < s->nb_streams; i++)
3887         if (s->flags & AVFMT_FLAG_BITEXACT)
3888             mov->exact = 1;
3889
3890     /* Set the FRAGMENT flag if any of the fragmentation methods are
3891      * enabled. */
3892     if (mov->max_fragment_duration || mov->max_fragment_size ||
3893         mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3894                       FF_MOV_FLAG_FRAG_KEYFRAME |
3895                       FF_MOV_FLAG_FRAG_CUSTOM))
3896         mov->flags |= FF_MOV_FLAG_FRAGMENT;
3897
3898     /* Set other implicit flags immediately */
3899     if (mov->mode == MODE_ISM)
3900         mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
3901                       FF_MOV_FLAG_FRAGMENT;
3902
3903     /* faststart: moov at the beginning of the file, if supported */
3904     if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3905         if ((mov->flags & FF_MOV_FLAG_FRAGMENT) ||
3906             (s->flags & AVFMT_FLAG_CUSTOM_IO)) {
3907             av_log(s, AV_LOG_WARNING, "The faststart flag is incompatible "
3908                    "with fragmentation and custom IO, disabling faststart\n");
3909             mov->flags &= ~FF_MOV_FLAG_FASTSTART;
3910         } else
3911             mov->reserved_moov_size = -1;
3912     }
3913
3914     if (!supports_edts(mov) && s->avoid_negative_ts < 0) {
3915         s->avoid_negative_ts = 2;
3916     }
3917
3918     /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3919      * is enabled, we don't support non-seekable output at all. */
3920     if (!s->pb->seekable &&
3921         (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
3922         av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3923         return AVERROR(EINVAL);
3924     }
3925
3926     mov_write_ftyp_tag(pb,s);
3927     if (mov->mode == MODE_PSP) {
3928         int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
3929         for (i = 0; i < s->nb_streams; i++) {
3930             AVStream *st = s->streams[i];
3931             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3932                 video_streams_nb++;
3933             else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
3934                 audio_streams_nb++;
3935             else
3936                 other_streams_nb++;
3937             }
3938
3939         if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
3940             av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3941             return AVERROR(EINVAL);
3942         }
3943         mov_write_uuidprof_tag(pb, s);
3944     }
3945
3946     mov->nb_streams = s->nb_streams;
3947     if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
3948         mov->chapter_track = mov->nb_streams++;
3949
3950     if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3951         /* Add hint tracks for each audio and video stream */
3952         hint_track = mov->nb_streams;
3953         for (i = 0; i < s->nb_streams; i++) {
3954             AVStream *st = s->streams[i];
3955             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3956                 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3957                 mov->nb_streams++;
3958             }
3959         }
3960     }
3961
3962     if (mov->mode == MODE_MOV) {
3963         tmcd_track = mov->nb_streams;
3964
3965         /* +1 tmcd track for each video stream with a timecode */
3966         for (i = 0; i < s->nb_streams; i++) {
3967             AVStream *st = s->streams[i];
3968             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3969                 (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
3970                 mov->nb_meta_tmcd++;
3971         }
3972
3973         /* check if there is already a tmcd track to remux */
3974         if (mov->nb_meta_tmcd) {
3975             for (i = 0; i < s->nb_streams; i++) {
3976                 AVStream *st = s->streams[i];
3977                 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
3978                     av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
3979                            "so timecode metadata are now ignored\n");
3980                     mov->nb_meta_tmcd = 0;
3981                 }
3982             }
3983         }
3984
3985         mov->nb_streams += mov->nb_meta_tmcd;
3986     }
3987
3988     // Reserve an extra stream for chapters for the case where chapters
3989     // are written in the trailer
3990     mov->tracks = av_mallocz((mov->nb_streams + 1) * sizeof(*mov->tracks));
3991     if (!mov->tracks)
3992         return AVERROR(ENOMEM);
3993
3994     for (i = 0; i < s->nb_streams; i++) {
3995         AVStream *st= s->streams[i];
3996         MOVTrack *track= &mov->tracks[i];
3997         AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3998
3999         track->st  = st;
4000         track->enc = st->codec;
4001         track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
4002         if (track->language < 0)
4003             track->language = 0;
4004         track->mode = mov->mode;
4005         track->tag  = mov_find_codec_tag(s, track);
4006         if (!track->tag) {
4007             av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
4008                    "codec not currently supported in container\n",
4009                    avcodec_get_name(st->codec->codec_id), i);
4010             ret = AVERROR(EINVAL);
4011             goto error;
4012         }
4013         /* If hinting of this track is enabled by a later hint track,
4014          * this is updated. */
4015         track->hint_track = -1;
4016         track->start_dts  = AV_NOPTS_VALUE;
4017         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
4018             if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
4019                 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
4020                 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
4021                 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
4022                     av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
4023                     ret = AVERROR(EINVAL);
4024                     goto error;
4025                 }
4026                 track->height = track->tag >> 24 == 'n' ? 486 : 576;
4027             }
4028             if (mov->video_track_timescale) {
4029                 track->timescale = mov->video_track_timescale;
4030             } else {
4031                 track->timescale = st->codec->time_base.den;
4032                 while(track->timescale < 10000)
4033                     track->timescale *= 2;
4034             }
4035             if (track->mode == MODE_MOV && track->timescale > 100000)
4036                 av_log(s, AV_LOG_WARNING,
4037                        "WARNING codec timebase is very high. If duration is too long,\n"
4038                        "file may not be playable by quicktime. Specify a shorter timebase\n"
4039                        "or choose different container.\n");
4040         } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
4041             track->timescale = st->codec->sample_rate;
4042             if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
4043                 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
4044                 track->audio_vbr = 1;
4045             }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
4046                      st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
4047                      st->codec->codec_id == AV_CODEC_ID_ILBC){
4048                 if (!st->codec->block_align) {
4049                     av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
4050                     ret = AVERROR(EINVAL);
4051                     goto error;
4052                 }
4053                 track->sample_size = st->codec->block_align;
4054             }else if (st->codec->frame_size > 1){ /* assume compressed audio */
4055                 track->audio_vbr = 1;
4056             }else{
4057                 track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
4058             }
4059             if (st->codec->codec_id == AV_CODEC_ID_ILBC ||
4060                 st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_QT) {
4061                 track->audio_vbr = 1;
4062             }
4063             if (track->mode != MODE_MOV &&
4064                 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
4065                 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
4066                        i, track->enc->sample_rate);
4067                 ret = AVERROR(EINVAL);
4068                 goto error;
4069             }
4070         } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4071             track->timescale = st->codec->time_base.den;
4072         } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
4073             track->timescale = st->codec->time_base.den;
4074         } else {
4075             track->timescale = MOV_TIMESCALE;
4076         }
4077         if (!track->height)
4078             track->height = st->codec->height;
4079         /* The ism specific timescale isn't mandatory, but is assumed by
4080          * some tools, such as mp4split. */
4081         if (mov->mode == MODE_ISM)
4082             track->timescale = 10000000;
4083
4084         avpriv_set_pts_info(st, 64, 1, track->timescale);
4085
4086         /* copy extradata if it exists */
4087         if (st->codec->extradata_size) {
4088             if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
4089                 mov_create_dvd_sub_decoder_specific_info(track, st);
4090             else {
4091                 track->vos_len  = st->codec->extradata_size;
4092                 track->vos_data = av_malloc(track->vos_len);
4093                 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
4094             }
4095         }
4096     }
4097
4098     enable_tracks(s);
4099
4100
4101     if (mov->reserved_moov_size){
4102         mov->reserved_moov_pos= avio_tell(pb);
4103         if (mov->reserved_moov_size > 0)
4104             avio_skip(pb, mov->reserved_moov_size);
4105     }
4106
4107     if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
4108         /* If no fragmentation options have been set, set a default. */
4109         if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
4110                             FF_MOV_FLAG_FRAG_CUSTOM)) &&
4111             !mov->max_fragment_duration && !mov->max_fragment_size)
4112             mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME;
4113     } else {
4114         if (mov->flags & FF_MOV_FLAG_FASTSTART)
4115             mov->reserved_moov_pos = avio_tell(pb);
4116         mov_write_mdat_tag(pb, mov);
4117     }
4118
4119     if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
4120         mov->time = ff_iso8601_to_unix_time(t->value);
4121     if (mov->time)
4122         mov->time += 0x7C25B080; // 1970 based -> 1904 based
4123
4124     if (mov->chapter_track)
4125         if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
4126             goto error;
4127
4128     if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
4129         /* Initialize the hint tracks for each audio and video stream */
4130         for (i = 0; i < s->nb_streams; i++) {
4131             AVStream *st = s->streams[i];
4132             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
4133                 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
4134                 if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
4135                     goto error;
4136                 hint_track++;
4137             }
4138         }
4139     }
4140
4141     if (mov->nb_meta_tmcd) {
4142         /* Initialize the tmcd tracks */
4143         for (i = 0; i < s->nb_streams; i++) {
4144             AVStream *st = s->streams[i];
4145             t = global_tcr;
4146
4147             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
4148                 if (!t)
4149                     t = av_dict_get(st->metadata, "timecode", NULL, 0);
4150                 if (!t)
4151                     continue;
4152                 if ((ret = mov_create_timecode_track(s, tmcd_track, i, t->value)) < 0)
4153                     goto error;
4154                 tmcd_track++;
4155             }
4156         }
4157     }
4158
4159     avio_flush(pb);
4160
4161     if (mov->flags & FF_MOV_FLAG_ISML)
4162         mov_write_isml_manifest(pb, mov);
4163
4164     if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4165         mov_write_moov_tag(pb, mov, s);
4166         mov->fragments++;
4167     }
4168
4169     return 0;
4170  error:
4171     mov_free(s);
4172     return ret;
4173 }
4174
4175 static int get_moov_size(AVFormatContext *s)
4176 {
4177     int ret;
4178     AVIOContext *moov_buf;
4179     MOVMuxContext *mov = s->priv_data;
4180
4181     if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
4182         return ret;
4183     mov_write_moov_tag(moov_buf, mov, s);
4184     return ffio_close_null_buf(moov_buf);
4185 }
4186
4187 /*
4188  * This function gets the moov size if moved to the top of the file: the chunk
4189  * offset table can switch between stco (32-bit entries) to co64 (64-bit
4190  * entries) when the moov is moved to the beginning, so the size of the moov
4191  * would change. It also updates the chunk offset tables.
4192  */
4193 static int compute_moov_size(AVFormatContext *s)
4194 {
4195     int i, moov_size, moov_size2;
4196     MOVMuxContext *mov = s->priv_data;
4197
4198     moov_size = get_moov_size(s);
4199     if (moov_size < 0)
4200         return moov_size;
4201
4202     for (i = 0; i < mov->nb_streams; i++)
4203         mov->tracks[i].data_offset += moov_size;
4204
4205     moov_size2 = get_moov_size(s);
4206     if (moov_size2 < 0)
4207         return moov_size2;
4208
4209     /* if the size changed, we just switched from stco to co64 and need to
4210      * update the offsets */
4211     if (moov_size2 != moov_size)
4212         for (i = 0; i < mov->nb_streams; i++)
4213             mov->tracks[i].data_offset += moov_size2 - moov_size;
4214
4215     return moov_size2;
4216 }
4217
4218 static int shift_data(AVFormatContext *s)
4219 {
4220     int ret = 0, moov_size;
4221     MOVMuxContext *mov = s->priv_data;
4222     int64_t pos, pos_end = avio_tell(s->pb);
4223     uint8_t *buf, *read_buf[2];
4224     int read_buf_id = 0;
4225     int read_size[2];
4226     AVIOContext *read_pb;
4227
4228     moov_size = compute_moov_size(s);
4229     if (moov_size < 0)
4230         return moov_size;
4231
4232     buf = av_malloc(moov_size * 2);
4233     if (!buf)
4234         return AVERROR(ENOMEM);
4235     read_buf[0] = buf;
4236     read_buf[1] = buf + moov_size;
4237
4238     /* Shift the data: the AVIO context of the output can only be used for
4239      * writing, so we re-open the same output, but for reading. It also avoids
4240      * a read/seek/write/seek back and forth. */
4241     avio_flush(s->pb);
4242     ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
4243     if (ret < 0) {
4244         av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
4245                "the second pass (faststart)\n", s->filename);
4246         goto end;
4247     }
4248
4249     /* mark the end of the shift to up to the last data we wrote, and get ready
4250      * for writing */
4251     pos_end = avio_tell(s->pb);
4252     avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
4253
4254     /* start reading at where the new moov will be placed */
4255     avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
4256     pos = avio_tell(read_pb);
4257
4258 #define READ_BLOCK do {                                                             \
4259     read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size);  \
4260     read_buf_id ^= 1;                                                               \
4261 } while (0)
4262
4263     /* shift data by chunk of at most moov_size */
4264     READ_BLOCK;
4265     do {
4266         int n;
4267         READ_BLOCK;
4268         n = read_size[read_buf_id];
4269         if (n <= 0)
4270             break;
4271         avio_write(s->pb, read_buf[read_buf_id], n);
4272         pos += n;
4273     } while (pos < pos_end);
4274     avio_close(read_pb);
4275
4276 end:
4277     av_free(buf);
4278     return ret;
4279 }
4280
4281 static int mov_write_trailer(AVFormatContext *s)
4282 {
4283     MOVMuxContext *mov = s->priv_data;
4284     AVIOContext *pb = s->pb;
4285     int res = 0;
4286     int i;
4287     int64_t moov_pos;
4288
4289     /*
4290      * Before actually writing the trailer, make sure that there are no
4291      * dangling subtitles, that need a terminating sample.
4292      */
4293     for (i = 0; i < mov->nb_streams; i++) {
4294         MOVTrack *trk = &mov->tracks[i];
4295         if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
4296             !trk->last_sample_is_subtitle_end) {
4297             mov_write_subtitle_end_packet(s, i, trk->track_duration);
4298             trk->last_sample_is_subtitle_end = 1;
4299         }
4300     }
4301
4302     // If there were no chapters when the header was written, but there
4303     // are chapters now, write them in the trailer.  This only works
4304     // when we are not doing fragments.
4305     if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
4306         if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
4307             mov->chapter_track = mov->nb_streams++;
4308             if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
4309                 goto error;
4310         }
4311     }
4312
4313     if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
4314         moov_pos = avio_tell(pb);
4315
4316         /* Write size of mdat tag */
4317         if (mov->mdat_size + 8 <= UINT32_MAX) {
4318             avio_seek(pb, mov->mdat_pos, SEEK_SET);
4319             avio_wb32(pb, mov->mdat_size + 8);
4320         } else {
4321             /* overwrite 'wide' placeholder atom */
4322             avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
4323             /* special value: real atom size will be 64 bit value after
4324              * tag field */
4325             avio_wb32(pb, 1);
4326             ffio_wfourcc(pb, "mdat");
4327             avio_wb64(pb, mov->mdat_size + 16);
4328         }
4329         avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
4330
4331         if (mov->flags & FF_MOV_FLAG_FASTSTART) {
4332             av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
4333             res = shift_data(s);
4334             if (res == 0) {
4335                 avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
4336                 mov_write_moov_tag(pb, mov, s);
4337             }
4338         } else if (mov->reserved_moov_size > 0) {
4339             int64_t size;
4340             mov_write_moov_tag(pb, mov, s);
4341             size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
4342             if (size < 8){
4343                 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
4344                 return -1;
4345             }
4346             avio_wb32(pb, size);
4347             ffio_wfourcc(pb, "free");
4348             for (i = 0; i < size; i++)
4349                 avio_w8(pb, 0);
4350             avio_seek(pb, moov_pos, SEEK_SET);
4351         } else {
4352             mov_write_moov_tag(pb, mov, s);
4353         }
4354     } else {
4355         mov_flush_fragment(s);
4356         mov_write_mfra_tag(pb, mov);
4357     }
4358
4359     for (i = 0; i < mov->nb_streams; i++) {
4360         if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
4361             mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
4362             int64_t off = avio_tell(pb);
4363             uint8_t buf[7];
4364             if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
4365                 avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
4366                 avio_write(pb, buf, 7);
4367                 avio_seek(pb, off, SEEK_SET);
4368             }
4369         }
4370     }
4371
4372 error:
4373     mov_free(s);
4374
4375     return res;
4376 }
4377
4378 #if CONFIG_MOV_MUXER
4379 MOV_CLASS(mov)
4380 AVOutputFormat ff_mov_muxer = {
4381     .name              = "mov",
4382     .long_name         = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
4383     .extensions        = "mov",
4384     .priv_data_size    = sizeof(MOVMuxContext),
4385     .audio_codec       = AV_CODEC_ID_AAC,
4386     .video_codec       = CONFIG_LIBX264_ENCODER ?
4387                          AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
4388     .write_header      = mov_write_header,
4389     .write_packet      = mov_write_packet,
4390     .write_trailer     = mov_write_trailer,
4391     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4392     .codec_tag         = (const AVCodecTag* const []){
4393         ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
4394     },
4395     .priv_class        = &mov_muxer_class,
4396 };
4397 #endif
4398 #if CONFIG_TGP_MUXER
4399 MOV_CLASS(tgp)
4400 AVOutputFormat ff_tgp_muxer = {
4401     .name              = "3gp",
4402     .long_name         = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
4403     .extensions        = "3gp",
4404     .priv_data_size    = sizeof(MOVMuxContext),
4405     .audio_codec       = AV_CODEC_ID_AMR_NB,
4406     .video_codec       = AV_CODEC_ID_H263,
4407     .write_header      = mov_write_header,
4408     .write_packet      = mov_write_packet,
4409     .write_trailer     = mov_write_trailer,
4410     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4411     .codec_tag         = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
4412     .priv_class        = &tgp_muxer_class,
4413 };
4414 #endif
4415 #if CONFIG_MP4_MUXER
4416 MOV_CLASS(mp4)
4417 AVOutputFormat ff_mp4_muxer = {
4418     .name              = "mp4",
4419     .long_name         = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
4420     .mime_type         = "application/mp4",
4421     .extensions        = "mp4",
4422     .priv_data_size    = sizeof(MOVMuxContext),
4423     .audio_codec       = AV_CODEC_ID_AAC,
4424     .video_codec       = CONFIG_LIBX264_ENCODER ?
4425                          AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
4426     .write_header      = mov_write_header,
4427     .write_packet      = mov_write_packet,
4428     .write_trailer     = mov_write_trailer,
4429     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4430     .codec_tag         = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4431     .priv_class        = &mp4_muxer_class,
4432 };
4433 #endif
4434 #if CONFIG_PSP_MUXER
4435 MOV_CLASS(psp)
4436 AVOutputFormat ff_psp_muxer = {
4437     .name              = "psp",
4438     .long_name         = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
4439     .extensions        = "mp4,psp",
4440     .priv_data_size    = sizeof(MOVMuxContext),
4441     .audio_codec       = AV_CODEC_ID_AAC,
4442     .video_codec       = CONFIG_LIBX264_ENCODER ?
4443                          AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
4444     .write_header      = mov_write_header,
4445     .write_packet      = mov_write_packet,
4446     .write_trailer     = mov_write_trailer,
4447     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4448     .codec_tag         = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4449     .priv_class        = &psp_muxer_class,
4450 };
4451 #endif
4452 #if CONFIG_TG2_MUXER
4453 MOV_CLASS(tg2)
4454 AVOutputFormat ff_tg2_muxer = {
4455     .name              = "3g2",
4456     .long_name         = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
4457     .extensions        = "3g2",
4458     .priv_data_size    = sizeof(MOVMuxContext),
4459     .audio_codec       = AV_CODEC_ID_AMR_NB,
4460     .video_codec       = AV_CODEC_ID_H263,
4461     .write_header      = mov_write_header,
4462     .write_packet      = mov_write_packet,
4463     .write_trailer     = mov_write_trailer,
4464     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4465     .codec_tag         = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
4466     .priv_class        = &tg2_muxer_class,
4467 };
4468 #endif
4469 #if CONFIG_IPOD_MUXER
4470 MOV_CLASS(ipod)
4471 AVOutputFormat ff_ipod_muxer = {
4472     .name              = "ipod",
4473     .long_name         = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
4474     .mime_type         = "application/mp4",
4475     .extensions        = "m4v,m4a",
4476     .priv_data_size    = sizeof(MOVMuxContext),
4477     .audio_codec       = AV_CODEC_ID_AAC,
4478     .video_codec       = AV_CODEC_ID_H264,
4479     .write_header      = mov_write_header,
4480     .write_packet      = mov_write_packet,
4481     .write_trailer     = mov_write_trailer,
4482     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4483     .codec_tag         = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
4484     .priv_class        = &ipod_muxer_class,
4485 };
4486 #endif
4487 #if CONFIG_ISMV_MUXER
4488 MOV_CLASS(ismv)
4489 AVOutputFormat ff_ismv_muxer = {
4490     .name              = "ismv",
4491     .long_name         = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
4492     .mime_type         = "application/mp4",
4493     .extensions        = "ismv,isma",
4494     .priv_data_size    = sizeof(MOVMuxContext),
4495     .audio_codec       = AV_CODEC_ID_AAC,
4496     .video_codec       = AV_CODEC_ID_H264,
4497     .write_header      = mov_write_header,
4498     .write_packet      = mov_write_packet,
4499     .write_trailer     = mov_write_trailer,
4500     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4501     .codec_tag         = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4502     .priv_class        = &ismv_muxer_class,
4503 };
4504 #endif
4505 #if CONFIG_F4V_MUXER
4506 MOV_CLASS(f4v)
4507 AVOutputFormat ff_f4v_muxer = {
4508     .name              = "f4v",
4509     .long_name         = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
4510     .mime_type         = "application/f4v",
4511     .extensions        = "f4v",
4512     .priv_data_size    = sizeof(MOVMuxContext),
4513     .audio_codec       = AV_CODEC_ID_AAC,
4514     .video_codec       = AV_CODEC_ID_H264,
4515     .write_header      = mov_write_header,
4516     .write_packet      = mov_write_packet,
4517     .write_trailer     = mov_write_trailer,
4518     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
4519     .codec_tag         = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
4520     .priv_class        = &f4v_muxer_class,
4521 };
4522 #endif