]> git.sesse.net Git - ffmpeg/blob - libavformat/movenc.c
avcodec/h264, videotoolbox: do not return invalid frames on failure
[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/ac3_parser.h"
35 #include "libavcodec/get_bits.h"
36 #include "libavcodec/put_bits.h"
37 #include "libavcodec/vc1_common.h"
38 #include "libavcodec/raw.h"
39 #include "internal.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/intfloat.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/libm.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/dict.h"
46 #include "libavutil/pixdesc.h"
47 #include "libavutil/timecode.h"
48 #include "libavutil/color_utils.h"
49 #include "hevc.h"
50 #include "rtpenc.h"
51 #include "mov_chan.h"
52
53 static const AVOption options[] = {
54     { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
55     { "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" },
56     { "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 },
57     { "empty_moov", "Make the initial moov atom empty", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
58     { "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" },
59     { "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" },
60     { "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" },
61     { "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" },
62     { "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" },
63     { "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" },
64     { "disable_chpl", "Disable Nero chapter atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DISABLE_CHPL}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
65     { "default_base_moof", "Set the default-base-is-moof flag in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DEFAULT_BASE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
66     { "dash", "Write DASH compatible fragmented MP4", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DASH}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
67     { "frag_discont", "Signal that the next fragment is discontinuous from earlier ones", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_DISCONT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
68     { "delay_moov", "Delay writing the initial moov until the first fragment is cut, or until the first fragment flush", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DELAY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
69     { "global_sidx", "Write a global sidx index at the start of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_GLOBAL_SIDX}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
70     { "write_colr", "Write colr atom (Experimental, may be renamed or changed, do not use from scripts)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_COLR}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
71     { "write_gama", "Write deprecated gama atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_GAMA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
72     FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
73     { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
74     { "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},
75     { "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},
76     { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
77     { "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},
78     { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
79     { "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},
80     { "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},
81     { "brand",    "Override major brand", offsetof(MOVMuxContext, major_brand),   AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
82     { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
83     { "fragment_index", "Fragment number of the next fragment", offsetof(MOVMuxContext, fragments), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
84     { "mov_gamma", "gamma value for gama atom", offsetof(MOVMuxContext, gamma), AV_OPT_TYPE_FLOAT, {.dbl = 0.0 }, 0.0, 10, AV_OPT_FLAG_ENCODING_PARAM},
85     { "frag_interleave", "Interleave samples within fragments (max number of consecutive samples, lower is tighter interleaving, but with more overhead)", offsetof(MOVMuxContext, frag_interleave), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
86     { NULL },
87 };
88
89 #define MOV_CLASS(flavor)\
90 static const AVClass flavor ## _muxer_class = {\
91     .class_name = #flavor " muxer",\
92     .item_name  = av_default_item_name,\
93     .option     = options,\
94     .version    = LIBAVUTIL_VERSION_INT,\
95 };
96
97 static int get_moov_size(AVFormatContext *s);
98
99 static int utf8len(const uint8_t *b)
100 {
101     int len = 0;
102     int val;
103     while (*b) {
104         GET_UTF8(val, *b++, return -1;)
105         len++;
106     }
107     return len;
108 }
109
110 //FIXME support 64 bit variant with wide placeholders
111 static int64_t update_size(AVIOContext *pb, int64_t pos)
112 {
113     int64_t curpos = avio_tell(pb);
114     avio_seek(pb, pos, SEEK_SET);
115     avio_wb32(pb, curpos - pos); /* rewrite size */
116     avio_seek(pb, curpos, SEEK_SET);
117
118     return curpos - pos;
119 }
120
121 static int co64_required(const MOVTrack *track)
122 {
123     if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
124         return 1;
125     return 0;
126 }
127
128 /* Chunk offset atom */
129 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
130 {
131     int i;
132     int mode64 = co64_required(track); // use 32 bit size variant if possible
133     int64_t pos = avio_tell(pb);
134     avio_wb32(pb, 0); /* size */
135     if (mode64)
136         ffio_wfourcc(pb, "co64");
137     else
138         ffio_wfourcc(pb, "stco");
139     avio_wb32(pb, 0); /* version & flags */
140     avio_wb32(pb, track->chunkCount); /* entry count */
141     for (i = 0; i < track->entry; i++) {
142         if (!track->cluster[i].chunkNum)
143             continue;
144         if (mode64 == 1)
145             avio_wb64(pb, track->cluster[i].pos + track->data_offset);
146         else
147             avio_wb32(pb, track->cluster[i].pos + track->data_offset);
148     }
149     return update_size(pb, pos);
150 }
151
152 /* Sample size atom */
153 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
154 {
155     int equalChunks = 1;
156     int i, j, entries = 0, tst = -1, oldtst = -1;
157
158     int64_t pos = avio_tell(pb);
159     avio_wb32(pb, 0); /* size */
160     ffio_wfourcc(pb, "stsz");
161     avio_wb32(pb, 0); /* version & flags */
162
163     for (i = 0; i < track->entry; i++) {
164         tst = track->cluster[i].size / track->cluster[i].entries;
165         if (oldtst != -1 && tst != oldtst)
166             equalChunks = 0;
167         oldtst = tst;
168         entries += track->cluster[i].entries;
169     }
170     if (equalChunks && track->entry) {
171         int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
172         sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
173         avio_wb32(pb, sSize); // sample size
174         avio_wb32(pb, entries); // sample count
175     } else {
176         avio_wb32(pb, 0); // sample size
177         avio_wb32(pb, entries); // sample count
178         for (i = 0; i < track->entry; i++) {
179             for (j = 0; j < track->cluster[i].entries; j++) {
180                 avio_wb32(pb, track->cluster[i].size /
181                           track->cluster[i].entries);
182             }
183         }
184     }
185     return update_size(pb, pos);
186 }
187
188 /* Sample to chunk atom */
189 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
190 {
191     int index = 0, oldval = -1, i;
192     int64_t entryPos, curpos;
193
194     int64_t pos = avio_tell(pb);
195     avio_wb32(pb, 0); /* size */
196     ffio_wfourcc(pb, "stsc");
197     avio_wb32(pb, 0); // version & flags
198     entryPos = avio_tell(pb);
199     avio_wb32(pb, track->chunkCount); // entry count
200     for (i = 0; i < track->entry; i++) {
201         if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
202             avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
203             avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
204             avio_wb32(pb, 0x1); // sample description index
205             oldval = track->cluster[i].samples_in_chunk;
206             index++;
207         }
208     }
209     curpos = avio_tell(pb);
210     avio_seek(pb, entryPos, SEEK_SET);
211     avio_wb32(pb, index); // rewrite size
212     avio_seek(pb, curpos, SEEK_SET);
213
214     return update_size(pb, pos);
215 }
216
217 /* Sync sample atom */
218 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
219 {
220     int64_t curpos, entryPos;
221     int i, index = 0;
222     int64_t pos = avio_tell(pb);
223     avio_wb32(pb, 0); // size
224     ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
225     avio_wb32(pb, 0); // version & flags
226     entryPos = avio_tell(pb);
227     avio_wb32(pb, track->entry); // entry count
228     for (i = 0; i < track->entry; i++) {
229         if (track->cluster[i].flags & flag) {
230             avio_wb32(pb, i + 1);
231             index++;
232         }
233     }
234     curpos = avio_tell(pb);
235     avio_seek(pb, entryPos, SEEK_SET);
236     avio_wb32(pb, index); // rewrite size
237     avio_seek(pb, curpos, SEEK_SET);
238     return update_size(pb, pos);
239 }
240
241 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
242 {
243     avio_wb32(pb, 0x11); /* size */
244     if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
245     else                         ffio_wfourcc(pb, "damr");
246     ffio_wfourcc(pb, "FFMP");
247     avio_w8(pb, 0); /* decoder version */
248
249     avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
250     avio_w8(pb, 0x00); /* Mode change period (no restriction) */
251     avio_w8(pb, 0x01); /* Frames per sample */
252     return 0x11;
253 }
254
255 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
256 {
257     GetBitContext gbc;
258     PutBitContext pbc;
259     uint8_t buf[3];
260     int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
261
262     if (track->vos_len < 7)
263         return -1;
264
265     avio_wb32(pb, 11);
266     ffio_wfourcc(pb, "dac3");
267
268     init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
269     fscod      = get_bits(&gbc, 2);
270     frmsizecod = get_bits(&gbc, 6);
271     bsid       = get_bits(&gbc, 5);
272     bsmod      = get_bits(&gbc, 3);
273     acmod      = get_bits(&gbc, 3);
274     if (acmod == 2) {
275         skip_bits(&gbc, 2); // dsurmod
276     } else {
277         if ((acmod & 1) && acmod != 1)
278             skip_bits(&gbc, 2); // cmixlev
279         if (acmod & 4)
280             skip_bits(&gbc, 2); // surmixlev
281     }
282     lfeon = get_bits1(&gbc);
283
284     init_put_bits(&pbc, buf, sizeof(buf));
285     put_bits(&pbc, 2, fscod);
286     put_bits(&pbc, 5, bsid);
287     put_bits(&pbc, 3, bsmod);
288     put_bits(&pbc, 3, acmod);
289     put_bits(&pbc, 1, lfeon);
290     put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
291     put_bits(&pbc, 5, 0); // reserved
292
293     flush_put_bits(&pbc);
294     avio_write(pb, buf, sizeof(buf));
295
296     return 11;
297 }
298
299 struct eac3_info {
300     AVPacket pkt;
301     uint8_t ec3_done;
302     uint8_t num_blocks;
303
304     /* Layout of the EC3SpecificBox */
305     /* maximum bitrate */
306     uint16_t data_rate;
307     /* number of independent substreams */
308     uint8_t  num_ind_sub;
309     struct {
310         /* sample rate code (see ff_ac3_sample_rate_tab) 2 bits */
311         uint8_t fscod;
312         /* bit stream identification 5 bits */
313         uint8_t bsid;
314         /* one bit reserved */
315         /* audio service mixing (not supported yet) 1 bit */
316         /* bit stream mode 3 bits */
317         uint8_t bsmod;
318         /* audio coding mode 3 bits */
319         uint8_t acmod;
320         /* sub woofer on 1 bit */
321         uint8_t lfeon;
322         /* 3 bits reserved */
323         /* number of dependent substreams associated with this substream 4 bits */
324         uint8_t num_dep_sub;
325         /* channel locations of the dependent substream(s), if any, 9 bits */
326         uint16_t chan_loc;
327         /* if there is no dependent substream, then one bit reserved instead */
328     } substream[1]; /* TODO: support 8 independent substreams */
329 };
330
331 #if CONFIG_AC3_PARSER
332 static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
333 {
334     GetBitContext gbc;
335     AC3HeaderInfo tmp, *hdr = &tmp;
336     struct eac3_info *info;
337     int num_blocks;
338
339     if (!track->eac3_priv && !(track->eac3_priv = av_mallocz(sizeof(*info))))
340         return AVERROR(ENOMEM);
341     info = track->eac3_priv;
342
343     init_get_bits(&gbc, pkt->data, pkt->size * 8);
344     if (avpriv_ac3_parse_header(&gbc, &hdr) < 0) {
345         /* drop the packets until we see a good one */
346         if (!track->entry) {
347             av_log(mov, AV_LOG_WARNING, "Dropping invalid packet from start of the stream\n");
348             return 0;
349         }
350         return AVERROR_INVALIDDATA;
351     }
352
353     info->data_rate = FFMAX(info->data_rate, hdr->bit_rate / 1000);
354     num_blocks = hdr->num_blocks;
355
356     if (!info->ec3_done) {
357         /* AC-3 substream must be the first one */
358         if (hdr->bitstream_id <= 10 && hdr->substreamid != 0)
359             return AVERROR(EINVAL);
360
361         /* this should always be the case, given that our AC-3 parser
362          * concatenates dependent frames to their independent parent */
363         if (hdr->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
364             /* substream ids must be incremental */
365             if (hdr->substreamid > info->num_ind_sub + 1)
366                 return AVERROR(EINVAL);
367
368             if (hdr->substreamid == info->num_ind_sub + 1) {
369                 //info->num_ind_sub++;
370                 avpriv_request_sample(track->enc, "Multiple independent substreams");
371                 return AVERROR_PATCHWELCOME;
372             } else if (hdr->substreamid < info->num_ind_sub ||
373                        hdr->substreamid == 0 && info->substream[0].bsid) {
374                 info->ec3_done = 1;
375                 goto concatenate;
376             }
377         }
378
379         /* fill the info needed for the "dec3" atom */
380         info->substream[hdr->substreamid].fscod = hdr->sr_code;
381         info->substream[hdr->substreamid].bsid  = hdr->bitstream_id;
382         info->substream[hdr->substreamid].bsmod = hdr->bitstream_mode;
383         info->substream[hdr->substreamid].acmod = hdr->channel_mode;
384         info->substream[hdr->substreamid].lfeon = hdr->lfe_on;
385
386         /* Parse dependent substream(s), if any */
387         if (pkt->size != hdr->frame_size) {
388             int cumul_size = hdr->frame_size;
389             int parent = hdr->substreamid;
390
391             while (cumul_size != pkt->size) {
392                 int i;
393                 init_get_bits(&gbc, pkt->data + cumul_size, (pkt->size - cumul_size) * 8);
394                 if (avpriv_ac3_parse_header(&gbc, &hdr) < 0)
395                     return AVERROR_INVALIDDATA;
396                 if (hdr->frame_type != EAC3_FRAME_TYPE_DEPENDENT)
397                     return AVERROR(EINVAL);
398                 cumul_size += hdr->frame_size;
399                 info->substream[parent].num_dep_sub++;
400
401                 /* header is parsed up to lfeon, but custom channel map may be needed */
402                 /* skip bsid */
403                 skip_bits(&gbc, 5);
404                 /* skip volume control params */
405                 for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
406                     skip_bits(&gbc, 5); // skip dialog normalization
407                     if (get_bits1(&gbc)) {
408                         skip_bits(&gbc, 8); // skip compression gain word
409                     }
410                 }
411                 /* get the dependent stream channel map, if exists */
412                 if (get_bits1(&gbc))
413                     info->substream[parent].chan_loc |= (get_bits(&gbc, 16) >> 5) & 0x1f;
414                 else
415                     info->substream[parent].chan_loc |= hdr->channel_mode;
416             }
417         }
418     }
419
420 concatenate:
421     if (!info->num_blocks && num_blocks == 6)
422         return pkt->size;
423     else if (info->num_blocks + num_blocks > 6)
424         return AVERROR_INVALIDDATA;
425
426     if (!info->num_blocks) {
427         int ret;
428         if ((ret = av_copy_packet(&info->pkt, pkt)) < 0)
429             return ret;
430         info->num_blocks = num_blocks;
431         return 0;
432     } else {
433         int ret;
434         if ((ret = av_grow_packet(&info->pkt, pkt->size)) < 0)
435             return ret;
436         memcpy(info->pkt.data + info->pkt.size - pkt->size, pkt->data, pkt->size);
437         info->num_blocks += num_blocks;
438         info->pkt.duration += pkt->duration;
439         if ((ret = av_copy_packet_side_data(&info->pkt, pkt)) < 0)
440             return ret;
441         if (info->num_blocks != 6)
442             return 0;
443         av_packet_unref(pkt);
444         if ((ret = av_copy_packet(pkt, &info->pkt)) < 0)
445             return ret;
446         av_packet_unref(&info->pkt);
447         info->num_blocks = 0;
448     }
449
450     return pkt->size;
451 }
452 #endif
453
454 static int mov_write_eac3_tag(AVIOContext *pb, MOVTrack *track)
455 {
456     PutBitContext pbc;
457     uint8_t *buf;
458     struct eac3_info *info;
459     int size, i;
460
461     if (!track->eac3_priv)
462         return AVERROR(EINVAL);
463
464     info = track->eac3_priv;
465     size = 2 + 4 * (info->num_ind_sub + 1);
466     buf = av_malloc(size);
467     if (!buf) {
468         size = AVERROR(ENOMEM);
469         goto end;
470     }
471
472     init_put_bits(&pbc, buf, size);
473     put_bits(&pbc, 13, info->data_rate);
474     put_bits(&pbc,  3, info->num_ind_sub);
475     for (i = 0; i <= info->num_ind_sub; i++) {
476         put_bits(&pbc, 2, info->substream[i].fscod);
477         put_bits(&pbc, 5, info->substream[i].bsid);
478         put_bits(&pbc, 1, 0); /* reserved */
479         put_bits(&pbc, 1, 0); /* asvc */
480         put_bits(&pbc, 3, info->substream[i].bsmod);
481         put_bits(&pbc, 3, info->substream[i].acmod);
482         put_bits(&pbc, 1, info->substream[i].lfeon);
483         put_bits(&pbc, 5, 0); /* reserved */
484         put_bits(&pbc, 4, info->substream[i].num_dep_sub);
485         if (!info->substream[i].num_dep_sub) {
486             put_bits(&pbc, 1, 0); /* reserved */
487             size--;
488         } else {
489             put_bits(&pbc, 9, info->substream[i].chan_loc);
490         }
491     }
492     flush_put_bits(&pbc);
493
494     avio_wb32(pb, size + 8);
495     ffio_wfourcc(pb, "dec3");
496     avio_write(pb, buf, size);
497
498     av_free(buf);
499
500 end:
501     av_packet_unref(&info->pkt);
502     av_freep(&track->eac3_priv);
503
504     return size;
505 }
506
507 /**
508  * This function writes extradata "as is".
509  * Extradata must be formatted like a valid atom (with size and tag).
510  */
511 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
512 {
513     avio_write(pb, track->enc->extradata, track->enc->extradata_size);
514     return track->enc->extradata_size;
515 }
516
517 static int mov_write_enda_tag(AVIOContext *pb)
518 {
519     avio_wb32(pb, 10);
520     ffio_wfourcc(pb, "enda");
521     avio_wb16(pb, 1); /* little endian */
522     return 10;
523 }
524
525 static int mov_write_enda_tag_be(AVIOContext *pb)
526 {
527   avio_wb32(pb, 10);
528   ffio_wfourcc(pb, "enda");
529   avio_wb16(pb, 0); /* big endian */
530   return 10;
531 }
532
533 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
534 {
535     int i = 3;
536     avio_w8(pb, tag);
537     for (; i > 0; i--)
538         avio_w8(pb, (size >> (7 * i)) | 0x80);
539     avio_w8(pb, size & 0x7F);
540 }
541
542 static unsigned compute_avg_bitrate(MOVTrack *track)
543 {
544     uint64_t size = 0;
545     int i;
546     if (!track->track_duration)
547         return 0;
548     for (i = 0; i < track->entry; i++)
549         size += track->cluster[i].size;
550     return size * 8 * track->timescale / track->track_duration;
551 }
552
553 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
554 {
555     int64_t pos = avio_tell(pb);
556     int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
557     unsigned avg_bitrate;
558
559     avio_wb32(pb, 0); // size
560     ffio_wfourcc(pb, "esds");
561     avio_wb32(pb, 0); // Version
562
563     // ES descriptor
564     put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
565     avio_wb16(pb, track->track_id);
566     avio_w8(pb, 0x00); // flags (= no flags)
567
568     // DecoderConfig descriptor
569     put_descr(pb, 0x04, 13 + decoder_specific_info_len);
570
571     // Object type indication
572     if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
573          track->enc->codec_id == AV_CODEC_ID_MP3) &&
574         track->enc->sample_rate > 24000)
575         avio_w8(pb, 0x6B); // 11172-3
576     else
577         avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
578
579     // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
580     // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
581     if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
582         avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
583     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
584         avio_w8(pb, 0x15); // flags (= Audiostream)
585     else
586         avio_w8(pb, 0x11); // flags (= Visualstream)
587
588     avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
589
590     avg_bitrate = compute_avg_bitrate(track);
591     // maxbitrate (FIXME should be max rate in any 1 sec window)
592     avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
593     avio_wb32(pb, avg_bitrate);
594
595     if (track->vos_len) {
596         // DecoderSpecific info descriptor
597         put_descr(pb, 0x05, track->vos_len);
598         avio_write(pb, track->vos_data, track->vos_len);
599     }
600
601     // SL descriptor
602     put_descr(pb, 0x06, 1);
603     avio_w8(pb, 0x02);
604     return update_size(pb, pos);
605 }
606
607 static int mov_pcm_le_gt16(enum AVCodecID codec_id)
608 {
609     return codec_id == AV_CODEC_ID_PCM_S24LE ||
610            codec_id == AV_CODEC_ID_PCM_S32LE ||
611            codec_id == AV_CODEC_ID_PCM_F32LE ||
612            codec_id == AV_CODEC_ID_PCM_F64LE;
613 }
614
615 static int mov_pcm_be_gt16(enum AVCodecID codec_id)
616 {
617     return codec_id == AV_CODEC_ID_PCM_S24BE ||
618            codec_id == AV_CODEC_ID_PCM_S32BE ||
619            codec_id == AV_CODEC_ID_PCM_F32BE ||
620            codec_id == AV_CODEC_ID_PCM_F64BE;
621 }
622
623 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
624 {
625     int ret;
626     int64_t pos = avio_tell(pb);
627     avio_wb32(pb, 0);
628     avio_wl32(pb, track->tag); // store it byteswapped
629     track->enc->codec_tag = av_bswap16(track->tag >> 16);
630     if ((ret = ff_put_wav_header(pb, track->enc, 0)) < 0)
631         return ret;
632     return update_size(pb, pos);
633 }
634
635 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
636 {
637     int ret;
638     int64_t pos = avio_tell(pb);
639     avio_wb32(pb, 0);
640     ffio_wfourcc(pb, "wfex");
641     if ((ret = ff_put_wav_header(pb, track->enc, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX)) < 0)
642         return ret;
643     return update_size(pb, pos);
644 }
645
646 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
647 {
648     uint32_t layout_tag, bitmap;
649     int64_t pos = avio_tell(pb);
650
651     layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
652                                                track->enc->channel_layout,
653                                                &bitmap);
654     if (!layout_tag) {
655         av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
656                "lack of channel information\n");
657         return 0;
658     }
659
660     if (track->multichannel_as_mono)
661         return 0;
662
663     avio_wb32(pb, 0);           // Size
664     ffio_wfourcc(pb, "chan");   // Type
665     avio_w8(pb, 0);             // Version
666     avio_wb24(pb, 0);           // Flags
667     avio_wb32(pb, layout_tag);  // mChannelLayoutTag
668     avio_wb32(pb, bitmap);      // mChannelBitmap
669     avio_wb32(pb, 0);           // mNumberChannelDescriptions
670
671     return update_size(pb, pos);
672 }
673
674 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
675 {
676     int64_t pos = avio_tell(pb);
677
678     avio_wb32(pb, 0);     /* size */
679     ffio_wfourcc(pb, "wave");
680
681     if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
682     avio_wb32(pb, 12);    /* size */
683     ffio_wfourcc(pb, "frma");
684     avio_wl32(pb, track->tag);
685     }
686
687     if (track->enc->codec_id == AV_CODEC_ID_AAC) {
688         /* useless atom needed by mplayer, ipod, not needed by quicktime */
689         avio_wb32(pb, 12); /* size */
690         ffio_wfourcc(pb, "mp4a");
691         avio_wb32(pb, 0);
692         mov_write_esds_tag(pb, track);
693     } else if (mov_pcm_le_gt16(track->enc->codec_id))  {
694       mov_write_enda_tag(pb);
695     } else if (mov_pcm_be_gt16(track->enc->codec_id))  {
696       mov_write_enda_tag_be(pb);
697     } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
698         mov_write_amr_tag(pb, track);
699     } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
700         mov_write_ac3_tag(pb, track);
701     } else if (track->enc->codec_id == AV_CODEC_ID_EAC3) {
702         mov_write_eac3_tag(pb, track);
703     } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
704                track->enc->codec_id == AV_CODEC_ID_QDM2) {
705         mov_write_extradata_tag(pb, track);
706     } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
707                track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
708         mov_write_ms_tag(pb, track);
709     }
710
711     avio_wb32(pb, 8);     /* size */
712     avio_wb32(pb, 0);     /* null tag */
713
714     return update_size(pb, pos);
715 }
716
717 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
718 {
719     uint8_t *unescaped;
720     const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
721     int unescaped_size, seq_found = 0;
722     int level = 0, interlace = 0;
723     int packet_seq   = track->vc1_info.packet_seq;
724     int packet_entry = track->vc1_info.packet_entry;
725     int slices       = track->vc1_info.slices;
726     PutBitContext pbc;
727
728     if (track->start_dts == AV_NOPTS_VALUE) {
729         /* No packets written yet, vc1_info isn't authoritative yet. */
730         /* Assume inline sequence and entry headers. */
731         packet_seq = packet_entry = 1;
732         av_log(NULL, AV_LOG_WARNING,
733                "moov atom written before any packets, unable to write correct "
734                "dvc1 atom. Set the delay_moov flag to fix this.\n");
735     }
736
737     unescaped = av_mallocz(track->vos_len + AV_INPUT_BUFFER_PADDING_SIZE);
738     if (!unescaped)
739         return AVERROR(ENOMEM);
740     start = find_next_marker(track->vos_data, end);
741     for (next = start; next < end; start = next) {
742         GetBitContext gb;
743         int size;
744         next = find_next_marker(start + 4, end);
745         size = next - start - 4;
746         if (size <= 0)
747             continue;
748         unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
749         init_get_bits(&gb, unescaped, 8 * unescaped_size);
750         if (AV_RB32(start) == VC1_CODE_SEQHDR) {
751             int profile = get_bits(&gb, 2);
752             if (profile != PROFILE_ADVANCED) {
753                 av_free(unescaped);
754                 return AVERROR(ENOSYS);
755             }
756             seq_found = 1;
757             level = get_bits(&gb, 3);
758             /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
759              * width, height */
760             skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
761             skip_bits(&gb, 1); /* broadcast */
762             interlace = get_bits1(&gb);
763             skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
764         }
765     }
766     if (!seq_found) {
767         av_free(unescaped);
768         return AVERROR(ENOSYS);
769     }
770
771     init_put_bits(&pbc, buf, 7);
772     /* VC1DecSpecStruc */
773     put_bits(&pbc, 4, 12); /* profile - advanced */
774     put_bits(&pbc, 3, level);
775     put_bits(&pbc, 1, 0); /* reserved */
776     /* VC1AdvDecSpecStruc */
777     put_bits(&pbc, 3, level);
778     put_bits(&pbc, 1, 0); /* cbr */
779     put_bits(&pbc, 6, 0); /* reserved */
780     put_bits(&pbc, 1, !interlace); /* no interlace */
781     put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
782     put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
783     put_bits(&pbc, 1, !slices); /* no slice code */
784     put_bits(&pbc, 1, 0); /* no bframe */
785     put_bits(&pbc, 1, 0); /* reserved */
786
787     /* framerate */
788     if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
789         put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
790     else
791         put_bits32(&pbc, 0xffffffff);
792
793     flush_put_bits(&pbc);
794
795     av_free(unescaped);
796
797     return 0;
798 }
799
800 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
801 {
802     uint8_t buf[7] = { 0 };
803     int ret;
804
805     if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
806         return ret;
807
808     avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
809     ffio_wfourcc(pb, "dvc1");
810     avio_write(pb, buf, sizeof(buf));
811     avio_write(pb, track->vos_data, track->vos_len);
812
813     return 0;
814 }
815
816 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
817 {
818     avio_wb32(pb, track->vos_len + 8);
819     ffio_wfourcc(pb, "glbl");
820     avio_write(pb, track->vos_data, track->vos_len);
821     return 8 + track->vos_len;
822 }
823
824 /**
825  * Compute flags for 'lpcm' tag.
826  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
827  */
828 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
829 {
830     switch (codec_id) {
831     case AV_CODEC_ID_PCM_F32BE:
832     case AV_CODEC_ID_PCM_F64BE:
833         return 11;
834     case AV_CODEC_ID_PCM_F32LE:
835     case AV_CODEC_ID_PCM_F64LE:
836         return 9;
837     case AV_CODEC_ID_PCM_U8:
838         return 10;
839     case AV_CODEC_ID_PCM_S16BE:
840     case AV_CODEC_ID_PCM_S24BE:
841     case AV_CODEC_ID_PCM_S32BE:
842         return 14;
843     case AV_CODEC_ID_PCM_S8:
844     case AV_CODEC_ID_PCM_S16LE:
845     case AV_CODEC_ID_PCM_S24LE:
846     case AV_CODEC_ID_PCM_S32LE:
847         return 12;
848     default:
849         return 0;
850     }
851 }
852
853 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
854 {
855     int64_t next_dts;
856
857     if (cluster_idx >= track->entry)
858         return 0;
859
860     if (cluster_idx + 1 == track->entry)
861         next_dts = track->track_duration + track->start_dts;
862     else
863         next_dts = track->cluster[cluster_idx + 1].dts;
864
865     next_dts -= track->cluster[cluster_idx].dts;
866
867     av_assert0(next_dts >= 0);
868     av_assert0(next_dts <= INT_MAX);
869
870     return next_dts;
871 }
872
873 static int get_samples_per_packet(MOVTrack *track)
874 {
875     int i, first_duration;
876
877 // return track->enc->frame_size;
878
879     /* use 1 for raw PCM */
880     if (!track->audio_vbr)
881         return 1;
882
883     /* check to see if duration is constant for all clusters */
884     if (!track->entry)
885         return 0;
886     first_duration = get_cluster_duration(track, 0);
887     for (i = 1; i < track->entry; i++) {
888         if (get_cluster_duration(track, i) != first_duration)
889             return 0;
890     }
891     return first_duration;
892 }
893
894 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
895 {
896     int64_t pos = avio_tell(pb);
897     int version = 0;
898     uint32_t tag = track->tag;
899
900     if (track->mode == MODE_MOV) {
901         if (track->timescale > UINT16_MAX) {
902             if (mov_get_lpcm_flags(track->enc->codec_id))
903                 tag = AV_RL32("lpcm");
904             version = 2;
905         } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
906                    mov_pcm_be_gt16(track->enc->codec_id) ||
907                    track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
908                    track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
909                    track->enc->codec_id == AV_CODEC_ID_QDM2) {
910             version = 1;
911         }
912     }
913
914     avio_wb32(pb, 0); /* size */
915     avio_wl32(pb, tag); // store it byteswapped
916     avio_wb32(pb, 0); /* Reserved */
917     avio_wb16(pb, 0); /* Reserved */
918     avio_wb16(pb, 1); /* Data-reference index, XXX  == 1 */
919
920     /* SoundDescription */
921     avio_wb16(pb, version); /* Version */
922     avio_wb16(pb, 0); /* Revision level */
923     avio_wb32(pb, 0); /* Reserved */
924
925     if (version == 2) {
926         avio_wb16(pb, 3);
927         avio_wb16(pb, 16);
928         avio_wb16(pb, 0xfffe);
929         avio_wb16(pb, 0);
930         avio_wb32(pb, 0x00010000);
931         avio_wb32(pb, 72);
932         avio_wb64(pb, av_double2int(track->enc->sample_rate));
933         avio_wb32(pb, track->enc->channels);
934         avio_wb32(pb, 0x7F000000);
935         avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
936         avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
937         avio_wb32(pb, track->sample_size);
938         avio_wb32(pb, get_samples_per_packet(track));
939     } else {
940         if (track->mode == MODE_MOV) {
941             avio_wb16(pb, track->enc->channels);
942             if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
943                 track->enc->codec_id == AV_CODEC_ID_PCM_S8)
944                 avio_wb16(pb, 8); /* bits per sample */
945             else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_G726)
946                 avio_wb16(pb, track->enc->bits_per_coded_sample);
947             else
948                 avio_wb16(pb, 16);
949             avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
950         } else { /* reserved for mp4/3gp */
951             avio_wb16(pb, 2);
952             avio_wb16(pb, 16);
953             avio_wb16(pb, 0);
954         }
955
956         avio_wb16(pb, 0); /* packet size (= 0) */
957         avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
958                       track->enc->sample_rate : 0);
959         avio_wb16(pb, 0); /* Reserved */
960     }
961
962     if (version == 1) { /* SoundDescription V1 extended info */
963         if (mov_pcm_le_gt16(track->enc->codec_id) ||
964             mov_pcm_be_gt16(track->enc->codec_id))
965             avio_wb32(pb, 1); /*  must be 1 for  uncompressed formats */
966         else
967             avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
968         avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
969         avio_wb32(pb, track->sample_size); /* Bytes per frame */
970         avio_wb32(pb, 2); /* Bytes per sample */
971     }
972
973     if (track->mode == MODE_MOV &&
974         (track->enc->codec_id == AV_CODEC_ID_AAC           ||
975          track->enc->codec_id == AV_CODEC_ID_AC3           ||
976          track->enc->codec_id == AV_CODEC_ID_EAC3          ||
977          track->enc->codec_id == AV_CODEC_ID_AMR_NB        ||
978          track->enc->codec_id == AV_CODEC_ID_ALAC          ||
979          track->enc->codec_id == AV_CODEC_ID_ADPCM_MS      ||
980          track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
981          track->enc->codec_id == AV_CODEC_ID_QDM2          ||
982          (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
983          (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
984         mov_write_wave_tag(pb, track);
985     else if (track->tag == MKTAG('m','p','4','a'))
986         mov_write_esds_tag(pb, track);
987     else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
988         mov_write_amr_tag(pb, track);
989     else if (track->enc->codec_id == AV_CODEC_ID_AC3)
990         mov_write_ac3_tag(pb, track);
991     else if (track->enc->codec_id == AV_CODEC_ID_EAC3)
992         mov_write_eac3_tag(pb, track);
993     else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
994         mov_write_extradata_tag(pb, track);
995     else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
996         mov_write_wfex_tag(pb, track);
997     else if (track->vos_len > 0)
998         mov_write_glbl_tag(pb, track);
999
1000     if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1001         mov_write_chan_tag(pb, track);
1002
1003     return update_size(pb, pos);
1004 }
1005
1006 static int mov_write_d263_tag(AVIOContext *pb)
1007 {
1008     avio_wb32(pb, 0xf); /* size */
1009     ffio_wfourcc(pb, "d263");
1010     ffio_wfourcc(pb, "FFMP");
1011     avio_w8(pb, 0); /* decoder version */
1012     /* FIXME use AVCodecContext level/profile, when encoder will set values */
1013     avio_w8(pb, 0xa); /* level */
1014     avio_w8(pb, 0); /* profile */
1015     return 0xf;
1016 }
1017
1018 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
1019 {
1020     int64_t pos = avio_tell(pb);
1021
1022     avio_wb32(pb, 0);
1023     ffio_wfourcc(pb, "avcC");
1024     ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
1025     return update_size(pb, pos);
1026 }
1027
1028 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
1029 {
1030     int64_t pos = avio_tell(pb);
1031
1032     avio_wb32(pb, 0);
1033     ffio_wfourcc(pb, "hvcC");
1034     ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
1035     return update_size(pb, pos);
1036 }
1037
1038 /* also used by all avid codecs (dv, imx, meridien) and their variants */
1039 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
1040 {
1041     int i;
1042     int interlaced;
1043     int cid;
1044
1045     if (track->vos_data && track->vos_len > 0x29) {
1046         if (track->vos_data[0] == 0x00 &&
1047             track->vos_data[1] == 0x00 &&
1048             track->vos_data[2] == 0x02 &&
1049             track->vos_data[3] == 0x80 &&
1050             (track->vos_data[4] == 0x01 || track->vos_data[4] == 0x02)) {
1051             /* looks like a DNxHD bit stream */
1052             interlaced = (track->vos_data[5] & 2);
1053             cid = AV_RB32(track->vos_data + 0x28);
1054         } else {
1055             av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1056             return 0;
1057         }
1058     } else {
1059         av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1060         return 0;
1061     }
1062
1063     avio_wb32(pb, 24); /* size */
1064     ffio_wfourcc(pb, "ACLR");
1065     ffio_wfourcc(pb, "ACLR");
1066     ffio_wfourcc(pb, "0001");
1067     if (track->enc->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1068         track->enc->color_range == AVCOL_RANGE_UNSPECIFIED) {
1069         avio_wb32(pb, 1); /* Corresponds to 709 in official encoder */
1070     } else { /* Full range (0-255) */
1071         avio_wb32(pb, 2); /* Corresponds to RGB in official encoder */
1072     }
1073     avio_wb32(pb, 0); /* unknown */
1074
1075     avio_wb32(pb, 24); /* size */
1076     ffio_wfourcc(pb, "APRG");
1077     ffio_wfourcc(pb, "APRG");
1078     ffio_wfourcc(pb, "0001");
1079     avio_wb32(pb, 1); /* unknown */
1080     avio_wb32(pb, 0); /* unknown */
1081
1082     avio_wb32(pb, 120); /* size */
1083     ffio_wfourcc(pb, "ARES");
1084     ffio_wfourcc(pb, "ARES");
1085     ffio_wfourcc(pb, "0001");
1086     avio_wb32(pb, cid); /* dnxhd cid, some id ? */
1087     avio_wb32(pb, track->enc->width);
1088     /* values below are based on samples created with quicktime and avid codecs */
1089     if (interlaced) {
1090         avio_wb32(pb, track->enc->height / 2);
1091         avio_wb32(pb, 2); /* unknown */
1092         avio_wb32(pb, 0); /* unknown */
1093         avio_wb32(pb, 4); /* unknown */
1094     } else {
1095         avio_wb32(pb, track->enc->height);
1096         avio_wb32(pb, 1); /* unknown */
1097         avio_wb32(pb, 0); /* unknown */
1098         if (track->enc->height == 1080)
1099             avio_wb32(pb, 5); /* unknown */
1100         else
1101             avio_wb32(pb, 6); /* unknown */
1102     }
1103     /* padding */
1104     for (i = 0; i < 10; i++)
1105         avio_wb64(pb, 0);
1106
1107     return 0;
1108 }
1109
1110 static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
1111 {
1112     avio_wb32(pb, 12);
1113     ffio_wfourcc(pb, "DpxE");
1114     if (track->enc->extradata_size >= 12 &&
1115         !memcmp(&track->enc->extradata[4], "DpxE", 4)) {
1116         avio_wb32(pb, track->enc->extradata[11]);
1117     } else {
1118         avio_wb32(pb, 1);
1119     }
1120     return 0;
1121 }
1122
1123 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1124 {
1125     int tag = track->enc->codec_tag;
1126
1127     if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
1128         return 0;
1129
1130     if      (track->enc->codec_id == AV_CODEC_ID_H264)      tag = MKTAG('a','v','c','1');
1131     else if (track->enc->codec_id == AV_CODEC_ID_HEVC)      tag = MKTAG('h','e','v','1');
1132     else if (track->enc->codec_id == AV_CODEC_ID_AC3)       tag = MKTAG('a','c','-','3');
1133     else if (track->enc->codec_id == AV_CODEC_ID_EAC3)      tag = MKTAG('e','c','-','3');
1134     else if (track->enc->codec_id == AV_CODEC_ID_DIRAC)     tag = MKTAG('d','r','a','c');
1135     else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT)  tag = MKTAG('t','x','3','g');
1136     else if (track->enc->codec_id == AV_CODEC_ID_VC1)       tag = MKTAG('v','c','-','1');
1137     else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)  tag = MKTAG('m','p','4','v');
1138     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)  tag = MKTAG('m','p','4','a');
1139     else if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)  tag = MKTAG('m','p','4','s');
1140
1141     return tag;
1142 }
1143
1144 static const AVCodecTag codec_ipod_tags[] = {
1145     { AV_CODEC_ID_H264,     MKTAG('a','v','c','1') },
1146     { AV_CODEC_ID_MPEG4,    MKTAG('m','p','4','v') },
1147     { AV_CODEC_ID_AAC,      MKTAG('m','p','4','a') },
1148     { AV_CODEC_ID_ALAC,     MKTAG('a','l','a','c') },
1149     { AV_CODEC_ID_AC3,      MKTAG('a','c','-','3') },
1150     { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1151     { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
1152     { AV_CODEC_ID_NONE, 0 },
1153 };
1154
1155 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1156 {
1157     int tag = track->enc->codec_tag;
1158
1159     // keep original tag for subs, ipod supports both formats
1160     if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1161           (tag == MKTAG('t', 'x', '3', 'g') ||
1162            tag == MKTAG('t', 'e', 'x', 't'))))
1163         tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
1164
1165     if (!av_match_ext(s->filename, "m4a") &&
1166         !av_match_ext(s->filename, "m4b") &&
1167         !av_match_ext(s->filename, "m4v"))
1168         av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a, .m4v nor  .m4b "
1169                "Quicktime/Ipod might not play the file\n");
1170
1171     return tag;
1172 }
1173
1174 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
1175 {
1176     int tag;
1177
1178     if (track->enc->width == 720) { /* SD */
1179         if (track->enc->height == 480) { /* NTSC */
1180             if  (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1181             else                                            tag = MKTAG('d','v','c',' ');
1182        }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1183         else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1184         else                                                tag = MKTAG('d','v','p','p');
1185     } else if (track->enc->height == 720) { /* HD 720 line */
1186         if  (track->st->time_base.den == 50)                tag = MKTAG('d','v','h','q');
1187         else                                                tag = MKTAG('d','v','h','p');
1188     } else if (track->enc->height == 1080) { /* HD 1080 line */
1189         if  (track->st->time_base.den == 25)                tag = MKTAG('d','v','h','5');
1190         else                                                tag = MKTAG('d','v','h','6');
1191     } else {
1192         av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1193         return 0;
1194     }
1195
1196     return tag;
1197 }
1198
1199 static AVRational find_fps(AVFormatContext *s, AVStream *st)
1200 {
1201     AVRational rate = {st->codec->time_base.den, st->codec->time_base.num};
1202     /* if the codec time base makes no sense, try to fallback on stream frame rate */
1203     if (av_timecode_check_frame_rate(rate) < 0) {
1204         av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
1205                rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
1206         rate = st->avg_frame_rate;
1207     }
1208
1209     return rate;
1210 }
1211
1212 static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
1213 {
1214     int tag = track->enc->codec_tag;
1215     int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
1216     AVStream *st = track->st;
1217     int rate = av_q2d(find_fps(s, st));
1218
1219     if (!tag)
1220         tag = MKTAG('m', '2', 'v', '1'); //fallback tag
1221
1222     if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) {
1223         if (track->enc->width == 1280 && track->enc->height == 720) {
1224             if (!interlaced) {
1225                 if      (rate == 24) tag = MKTAG('x','d','v','4');
1226                 else if (rate == 25) tag = MKTAG('x','d','v','5');
1227                 else if (rate == 30) tag = MKTAG('x','d','v','1');
1228                 else if (rate == 50) tag = MKTAG('x','d','v','a');
1229                 else if (rate == 60) tag = MKTAG('x','d','v','9');
1230             }
1231         } else if (track->enc->width == 1440 && track->enc->height == 1080) {
1232             if (!interlaced) {
1233                 if      (rate == 24) tag = MKTAG('x','d','v','6');
1234                 else if (rate == 25) tag = MKTAG('x','d','v','7');
1235                 else if (rate == 30) tag = MKTAG('x','d','v','8');
1236             } else {
1237                 if      (rate == 25) tag = MKTAG('x','d','v','3');
1238                 else if (rate == 30) tag = MKTAG('x','d','v','2');
1239             }
1240         } else if (track->enc->width == 1920 && track->enc->height == 1080) {
1241             if (!interlaced) {
1242                 if      (rate == 24) tag = MKTAG('x','d','v','d');
1243                 else if (rate == 25) tag = MKTAG('x','d','v','e');
1244                 else if (rate == 30) tag = MKTAG('x','d','v','f');
1245             } else {
1246                 if      (rate == 25) tag = MKTAG('x','d','v','c');
1247                 else if (rate == 30) tag = MKTAG('x','d','v','b');
1248             }
1249         }
1250     } else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
1251         if (track->enc->width == 1280 && track->enc->height == 720) {
1252             if (!interlaced) {
1253                 if      (rate == 24) tag = MKTAG('x','d','5','4');
1254                 else if (rate == 25) tag = MKTAG('x','d','5','5');
1255                 else if (rate == 30) tag = MKTAG('x','d','5','1');
1256                 else if (rate == 50) tag = MKTAG('x','d','5','a');
1257                 else if (rate == 60) tag = MKTAG('x','d','5','9');
1258             }
1259         } else if (track->enc->width == 1920 && track->enc->height == 1080) {
1260             if (!interlaced) {
1261                 if      (rate == 24) tag = MKTAG('x','d','5','d');
1262                 else if (rate == 25) tag = MKTAG('x','d','5','e');
1263                 else if (rate == 30) tag = MKTAG('x','d','5','f');
1264             } else {
1265                 if      (rate == 25) tag = MKTAG('x','d','5','c');
1266                 else if (rate == 30) tag = MKTAG('x','d','5','b');
1267             }
1268         }
1269     }
1270
1271     return tag;
1272 }
1273
1274 static int mov_get_h264_codec_tag(AVFormatContext *s, MOVTrack *track)
1275 {
1276     int tag = track->enc->codec_tag;
1277     int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
1278     AVStream *st = track->st;
1279     int rate = av_q2d(find_fps(s, st));
1280
1281     if (!tag)
1282         tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
1283
1284     if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P10) {
1285         if (track->enc->width == 960 && track->enc->height == 720) {
1286             if (!interlaced) {
1287                 if      (rate == 24) tag = MKTAG('a','i','5','p');
1288                 else if (rate == 25) tag = MKTAG('a','i','5','q');
1289                 else if (rate == 30) tag = MKTAG('a','i','5','p');
1290                 else if (rate == 50) tag = MKTAG('a','i','5','q');
1291                 else if (rate == 60) tag = MKTAG('a','i','5','p');
1292             }
1293         } else if (track->enc->width == 1440 && track->enc->height == 1080) {
1294             if (!interlaced) {
1295                 if      (rate == 24) tag = MKTAG('a','i','5','3');
1296                 else if (rate == 25) tag = MKTAG('a','i','5','2');
1297                 else if (rate == 30) tag = MKTAG('a','i','5','3');
1298             } else {
1299                 if      (rate == 50) tag = MKTAG('a','i','5','5');
1300                 else if (rate == 60) tag = MKTAG('a','i','5','6');
1301             }
1302         }
1303     } else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P10) {
1304         if (track->enc->width == 1280 && track->enc->height == 720) {
1305             if (!interlaced) {
1306                 if      (rate == 24) tag = MKTAG('a','i','1','p');
1307                 else if (rate == 25) tag = MKTAG('a','i','1','q');
1308                 else if (rate == 30) tag = MKTAG('a','i','1','p');
1309                 else if (rate == 50) tag = MKTAG('a','i','1','q');
1310                 else if (rate == 60) tag = MKTAG('a','i','1','p');
1311             }
1312         } else if (track->enc->width == 1920 && track->enc->height == 1080) {
1313             if (!interlaced) {
1314                 if      (rate == 24) tag = MKTAG('a','i','1','3');
1315                 else if (rate == 25) tag = MKTAG('a','i','1','2');
1316                 else if (rate == 30) tag = MKTAG('a','i','1','3');
1317             } else {
1318                 if      (rate == 25) tag = MKTAG('a','i','1','5');
1319                 else if (rate == 50) tag = MKTAG('a','i','1','5');
1320                 else if (rate == 60) tag = MKTAG('a','i','1','6');
1321             }
1322         } else if (   track->enc->width == 4096 && track->enc->height == 2160
1323                    || track->enc->width == 3840 && track->enc->height == 2160
1324                    || track->enc->width == 2048 && track->enc->height == 1080) {
1325             tag = MKTAG('a','i','v','x');
1326         }
1327     }
1328
1329     return tag;
1330 }
1331
1332 static const struct {
1333     enum AVPixelFormat pix_fmt;
1334     uint32_t tag;
1335     unsigned bps;
1336 } mov_pix_fmt_tags[] = {
1337     { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'),  0 },
1338     { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'),  0 },
1339     { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'),  0 },
1340     { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
1341     { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
1342     { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
1343     { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
1344     { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
1345     { AV_PIX_FMT_RGB24,   MKTAG('r','a','w',' '), 24 },
1346     { AV_PIX_FMT_BGR24,   MKTAG('2','4','B','G'), 24 },
1347     { AV_PIX_FMT_ARGB,    MKTAG('r','a','w',' '), 32 },
1348     { AV_PIX_FMT_BGRA,    MKTAG('B','G','R','A'), 32 },
1349     { AV_PIX_FMT_RGBA,    MKTAG('R','G','B','A'), 32 },
1350     { AV_PIX_FMT_ABGR,    MKTAG('A','B','G','R'), 32 },
1351     { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1352 };
1353
1354 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
1355 {
1356     int tag = track->enc->codec_tag;
1357     int i;
1358     enum AVPixelFormat pix_fmt;
1359
1360     for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1361         if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
1362             tag = mov_pix_fmt_tags[i].tag;
1363             track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
1364             if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
1365                 break;
1366         }
1367     }
1368
1369     pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_mov,
1370                                   track->enc->bits_per_coded_sample);
1371     if (tag == MKTAG('r','a','w',' ') &&
1372         track->enc->pix_fmt != pix_fmt &&
1373         track->enc->pix_fmt != AV_PIX_FMT_NONE)
1374         av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
1375                av_get_pix_fmt_name(track->enc->pix_fmt));
1376     return tag;
1377 }
1378
1379 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1380 {
1381     int tag = track->enc->codec_tag;
1382
1383     if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1384                  (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
1385                   track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
1386                   track->enc->codec_id == AV_CODEC_ID_H263 ||
1387                   track->enc->codec_id == AV_CODEC_ID_H264 ||
1388                   track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1389                   av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
1390         if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
1391             tag = mov_get_dv_codec_tag(s, track);
1392         else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
1393             tag = mov_get_rawvideo_codec_tag(s, track);
1394         else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1395             tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1396         else if (track->enc->codec_id == AV_CODEC_ID_H264)
1397             tag = mov_get_h264_codec_tag(s, track);
1398         else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1399             tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
1400             if (!tag) { // if no mac fcc found, try with Microsoft tags
1401                 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
1402                 if (tag)
1403                     av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1404                            "the file may be unplayable!\n");
1405             }
1406         } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1407             tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
1408             if (!tag) { // if no mac fcc found, try with Microsoft tags
1409                 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
1410                 if (ms_tag) {
1411                     tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1412                     av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1413                            "the file may be unplayable!\n");
1414                 }
1415             }
1416         } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1417             tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
1418     }
1419
1420     return tag;
1421 }
1422
1423 static const AVCodecTag codec_3gp_tags[] = {
1424     { AV_CODEC_ID_H263,     MKTAG('s','2','6','3') },
1425     { AV_CODEC_ID_H264,     MKTAG('a','v','c','1') },
1426     { AV_CODEC_ID_MPEG4,    MKTAG('m','p','4','v') },
1427     { AV_CODEC_ID_AAC,      MKTAG('m','p','4','a') },
1428     { AV_CODEC_ID_AMR_NB,   MKTAG('s','a','m','r') },
1429     { AV_CODEC_ID_AMR_WB,   MKTAG('s','a','w','b') },
1430     { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1431     { AV_CODEC_ID_NONE, 0 },
1432 };
1433
1434 static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
1435     { AV_CODEC_ID_MP3,    MKTAG('.','m','p','3') },
1436     { AV_CODEC_ID_AAC,    MKTAG('m','p','4','a') },
1437     { AV_CODEC_ID_H264,   MKTAG('a','v','c','1') },
1438     { AV_CODEC_ID_VP6A,   MKTAG('V','P','6','A') },
1439     { AV_CODEC_ID_VP6F,   MKTAG('V','P','6','F') },
1440     { AV_CODEC_ID_NONE, 0 },
1441 };
1442
1443 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
1444 {
1445     int tag;
1446
1447     if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
1448         tag = mp4_get_codec_tag(s, track);
1449     else if (track->mode == MODE_ISM) {
1450         tag = mp4_get_codec_tag(s, track);
1451         if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
1452             tag = MKTAG('w', 'm', 'a', ' ');
1453     } else if (track->mode == MODE_IPOD)
1454         tag = ipod_get_codec_tag(s, track);
1455     else if (track->mode & MODE_3GP)
1456         tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
1457     else if (track->mode == MODE_F4V)
1458         tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
1459     else
1460         tag = mov_get_codec_tag(s, track);
1461
1462     return tag;
1463 }
1464
1465 /** Write uuid atom.
1466  * Needed to make file play in iPods running newest firmware
1467  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1468  */
1469 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
1470 {
1471     avio_wb32(pb, 28);
1472     ffio_wfourcc(pb, "uuid");
1473     avio_wb32(pb, 0x6b6840f2);
1474     avio_wb32(pb, 0x5f244fc5);
1475     avio_wb32(pb, 0xba39a51b);
1476     avio_wb32(pb, 0xcf0323f3);
1477     avio_wb32(pb, 0x0);
1478     return 28;
1479 }
1480
1481 static const uint16_t fiel_data[] = {
1482     0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1483 };
1484
1485 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
1486 {
1487     unsigned mov_field_order = 0;
1488     if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
1489         mov_field_order = fiel_data[track->enc->field_order];
1490     else
1491         return 0;
1492     avio_wb32(pb, 10);
1493     ffio_wfourcc(pb, "fiel");
1494     avio_wb16(pb, mov_field_order);
1495     return 10;
1496 }
1497
1498 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
1499 {
1500     int64_t pos = avio_tell(pb);
1501     avio_wb32(pb, 0);    /* size */
1502     avio_wl32(pb, track->tag); // store it byteswapped
1503     avio_wb32(pb, 0);    /* Reserved */
1504     avio_wb16(pb, 0);    /* Reserved */
1505     avio_wb16(pb, 1);    /* Data-reference index */
1506
1507     if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1508         mov_write_esds_tag(pb, track);
1509     else if (track->enc->extradata_size)
1510         avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1511
1512     return update_size(pb, pos);
1513 }
1514
1515 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1516 {
1517     AVRational sar;
1518     av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
1519               track->enc->sample_aspect_ratio.den, INT_MAX);
1520
1521     avio_wb32(pb, 16);
1522     ffio_wfourcc(pb, "pasp");
1523     avio_wb32(pb, sar.num);
1524     avio_wb32(pb, sar.den);
1525     return 16;
1526 }
1527
1528 static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
1529 {
1530     uint32_t gama = 0;
1531     if (gamma <= 0.0)
1532     {
1533         gamma = avpriv_get_gamma_from_trc(track->enc->color_trc);
1534     }
1535     av_log(pb, AV_LOG_DEBUG, "gamma value %g\n", gamma);
1536
1537     if (gamma > 1e-6) {
1538         gama = (uint32_t)lrint((double)(1<<16) * gamma);
1539         av_log(pb, AV_LOG_DEBUG, "writing gama value %d\n", gama);
1540
1541         av_assert0(track->mode == MODE_MOV);
1542         avio_wb32(pb, 12);
1543         ffio_wfourcc(pb, "gama");
1544         avio_wb32(pb, gama);
1545         return 12;
1546     }
1547     else {
1548         av_log(pb, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
1549     }
1550     return 0;
1551 }
1552
1553 static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
1554 {
1555     // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
1556     // Ref (MP4): ISO/IEC 14496-12:2012
1557
1558     if (track->enc->color_primaries == AVCOL_PRI_UNSPECIFIED &&
1559         track->enc->color_trc == AVCOL_TRC_UNSPECIFIED &&
1560         track->enc->colorspace == AVCOL_SPC_UNSPECIFIED) {
1561         if ((track->enc->width >= 1920 && track->enc->height >= 1080)
1562           || (track->enc->width == 1280 && track->enc->height == 720)) {
1563             av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt709\n");
1564             track->enc->color_primaries = AVCOL_PRI_BT709;
1565         } else if (track->enc->width == 720 && track->height == 576) {
1566             av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt470bg\n");
1567             track->enc->color_primaries = AVCOL_PRI_BT470BG;
1568         } else if (track->enc->width == 720 &&
1569                    (track->height == 486 || track->height == 480)) {
1570             av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming smpte170\n");
1571             track->enc->color_primaries = AVCOL_PRI_SMPTE170M;
1572         } else {
1573             av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, unable to assume anything\n");
1574         }
1575         switch (track->enc->color_primaries) {
1576         case AVCOL_PRI_BT709:
1577             track->enc->color_trc = AVCOL_TRC_BT709;
1578             track->enc->colorspace = AVCOL_SPC_BT709;
1579             break;
1580         case AVCOL_PRI_SMPTE170M:
1581         case AVCOL_PRI_BT470BG:
1582             track->enc->color_trc = AVCOL_TRC_BT709;
1583             track->enc->colorspace = AVCOL_SPC_SMPTE170M;
1584             break;
1585         }
1586     }
1587
1588     /* We should only ever be called by MOV or MP4. */
1589     av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4);
1590
1591     avio_wb32(pb, 18 + (track->mode == MODE_MP4));
1592     ffio_wfourcc(pb, "colr");
1593     if (track->mode == MODE_MP4)
1594         ffio_wfourcc(pb, "nclx");
1595     else
1596         ffio_wfourcc(pb, "nclc");
1597     switch (track->enc->color_primaries) {
1598     case AVCOL_PRI_BT709:     avio_wb16(pb, 1); break;
1599     case AVCOL_PRI_SMPTE170M:
1600     case AVCOL_PRI_SMPTE240M: avio_wb16(pb, 6); break;
1601     case AVCOL_PRI_BT470BG:   avio_wb16(pb, 5); break;
1602     default:                  avio_wb16(pb, 2);
1603     }
1604     switch (track->enc->color_trc) {
1605     case AVCOL_TRC_BT709:     avio_wb16(pb, 1); break;
1606     case AVCOL_TRC_SMPTE170M: avio_wb16(pb, 1); break; // remapped
1607     case AVCOL_TRC_SMPTE240M: avio_wb16(pb, 7); break;
1608     default:                  avio_wb16(pb, 2);
1609     }
1610     switch (track->enc->colorspace) {
1611     case AVCOL_SPC_BT709:     avio_wb16(pb, 1); break;
1612     case AVCOL_SPC_BT470BG:
1613     case AVCOL_SPC_SMPTE170M: avio_wb16(pb, 6); break;
1614     case AVCOL_SPC_SMPTE240M: avio_wb16(pb, 7); break;
1615     default:                  avio_wb16(pb, 2);
1616     }
1617
1618     if (track->mode == MODE_MP4) {
1619         int full_range = track->enc->color_range == AVCOL_RANGE_JPEG;
1620         avio_w8(pb, full_range << 7);
1621         return 19;
1622     } else {
1623         return 18;
1624     }
1625 }
1626
1627 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1628 {
1629     AVDictionaryEntry *encoder;
1630     int xdcam_res =  (track->enc->width == 1280 && track->enc->height == 720)
1631                   || (track->enc->width == 1440 && track->enc->height == 1080)
1632                   || (track->enc->width == 1920 && track->enc->height == 1080);
1633
1634     if (track->mode == MODE_MOV &&
1635         (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
1636         av_strlcpy(compressor_name, encoder->value, 32);
1637     } else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1638         int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
1639         AVStream *st = track->st;
1640         int rate = av_q2d(find_fps(NULL, st));
1641         av_strlcatf(compressor_name, len, "XDCAM");
1642         if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
1643             av_strlcatf(compressor_name, len, " HD422");
1644         } else if(track->enc->width == 1440) {
1645             av_strlcatf(compressor_name, len, " HD");
1646         } else
1647             av_strlcatf(compressor_name, len, " EX");
1648
1649         av_strlcatf(compressor_name, len, " %d%c", track->enc->height, interlaced ? 'i' : 'p');
1650
1651         av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1652     }
1653 }
1654
1655 static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
1656 {
1657     int64_t pos = avio_tell(pb);
1658     char compressor_name[32] = { 0 };
1659     int avid = 0;
1660
1661     avio_wb32(pb, 0); /* size */
1662     avio_wl32(pb, track->tag); // store it byteswapped
1663     avio_wb32(pb, 0); /* Reserved */
1664     avio_wb16(pb, 0); /* Reserved */
1665     avio_wb16(pb, 1); /* Data-reference index */
1666
1667     avio_wb16(pb, 0); /* Codec stream version */
1668     avio_wb16(pb, 0); /* Codec stream revision (=0) */
1669     if (track->mode == MODE_MOV) {
1670         ffio_wfourcc(pb, "FFMP"); /* Vendor */
1671         if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1672             avio_wb32(pb, 0); /* Temporal Quality */
1673             avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1674         } else {
1675             avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1676             avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1677         }
1678     } else {
1679         avio_wb32(pb, 0); /* Reserved */
1680         avio_wb32(pb, 0); /* Reserved */
1681         avio_wb32(pb, 0); /* Reserved */
1682     }
1683     avio_wb16(pb, track->enc->width); /* Video width */
1684     avio_wb16(pb, track->height); /* Video height */
1685     avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1686     avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1687     avio_wb32(pb, 0); /* Data size (= 0) */
1688     avio_wb16(pb, 1); /* Frame count (= 1) */
1689
1690     /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1691     find_compressor(compressor_name, 32, track);
1692     avio_w8(pb, strlen(compressor_name));
1693     avio_write(pb, compressor_name, 31);
1694
1695     if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1696         avio_wb16(pb, track->enc->bits_per_coded_sample);
1697     else
1698         avio_wb16(pb, 0x18); /* Reserved */
1699     avio_wb16(pb, 0xffff); /* Reserved */
1700     if (track->tag == MKTAG('m','p','4','v'))
1701         mov_write_esds_tag(pb, track);
1702     else if (track->enc->codec_id == AV_CODEC_ID_H263)
1703         mov_write_d263_tag(pb);
1704     else if (track->enc->codec_id == AV_CODEC_ID_AVUI ||
1705             track->enc->codec_id == AV_CODEC_ID_SVQ3) {
1706         mov_write_extradata_tag(pb, track);
1707         avio_wb32(pb, 0);
1708     } else if (track->enc->codec_id == AV_CODEC_ID_DNXHD) {
1709         mov_write_avid_tag(pb, track);
1710         avid = 1;
1711     } else if (track->enc->codec_id == AV_CODEC_ID_HEVC)
1712         mov_write_hvcc_tag(pb, track);
1713     else if (track->enc->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
1714         mov_write_avcc_tag(pb, track);
1715         if (track->mode == MODE_IPOD)
1716             mov_write_uuid_tag_ipod(pb);
1717     } else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1718         mov_write_dvc1_tag(pb, track);
1719     else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
1720              track->enc->codec_id == AV_CODEC_ID_VP6A) {
1721         /* Don't write any potential extradata here - the cropping
1722          * is signalled via the normal width/height fields. */
1723     } else if (track->enc->codec_id == AV_CODEC_ID_R10K) {
1724         if (track->enc->codec_tag == MKTAG('R','1','0','k'))
1725             mov_write_dpxe_tag(pb, track);
1726     } else if (track->vos_len > 0)
1727         mov_write_glbl_tag(pb, track);
1728
1729     if (track->enc->codec_id != AV_CODEC_ID_H264 &&
1730         track->enc->codec_id != AV_CODEC_ID_MPEG4 &&
1731         track->enc->codec_id != AV_CODEC_ID_DNXHD)
1732         if (track->enc->field_order != AV_FIELD_UNKNOWN)
1733             mov_write_fiel_tag(pb, track);
1734
1735     if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
1736         if (track->mode == MODE_MOV)
1737             mov_write_gama_tag(pb, track, mov->gamma);
1738         else
1739             av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
1740     }
1741     if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
1742         if (track->mode == MODE_MOV || track->mode == MODE_MP4)
1743             mov_write_colr_tag(pb, track);
1744         else
1745             av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4.\n");
1746     }
1747
1748     if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1749         track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1750         mov_write_pasp_tag(pb, track);
1751     }
1752
1753     /* extra padding for avid stsd */
1754     /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
1755     if (avid)
1756         avio_wb32(pb, 0);
1757
1758     return update_size(pb, pos);
1759 }
1760
1761 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1762 {
1763     int64_t pos = avio_tell(pb);
1764     avio_wb32(pb, 0); /* size */
1765     ffio_wfourcc(pb, "rtp ");
1766     avio_wb32(pb, 0); /* Reserved */
1767     avio_wb16(pb, 0); /* Reserved */
1768     avio_wb16(pb, 1); /* Data-reference index */
1769
1770     avio_wb16(pb, 1); /* Hint track version */
1771     avio_wb16(pb, 1); /* Highest compatible version */
1772     avio_wb32(pb, track->max_packet_size); /* Max packet size */
1773
1774     avio_wb32(pb, 12); /* size */
1775     ffio_wfourcc(pb, "tims");
1776     avio_wb32(pb, track->timescale);
1777
1778     return update_size(pb, pos);
1779 }
1780
1781 static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
1782 {
1783     uint64_t str_size =strlen(reel_name);
1784     int64_t pos = avio_tell(pb);
1785
1786     if (str_size >= UINT16_MAX){
1787         av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
1788         avio_wb16(pb, 0);
1789         return AVERROR(EINVAL);
1790     }
1791
1792     avio_wb32(pb, 0);                              /* size */
1793     ffio_wfourcc(pb, "name");                      /* Data format */
1794     avio_wb16(pb, str_size);                       /* string size */
1795     avio_wb16(pb, track->language);                /* langcode */
1796     avio_write(pb, reel_name, str_size);           /* reel name */
1797     return update_size(pb,pos);
1798 }
1799
1800 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1801 {
1802     int64_t pos = avio_tell(pb);
1803 #if 1
1804     int frame_duration = av_rescale(track->timescale, track->enc->time_base.num, track->enc->time_base.den);
1805     int nb_frames = ROUNDED_DIV(track->enc->time_base.den, track->enc->time_base.num);
1806     AVDictionaryEntry *t = NULL;
1807
1808     if (nb_frames > 255) {
1809         av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
1810         return AVERROR(EINVAL);
1811     }
1812
1813     avio_wb32(pb, 0); /* size */
1814     ffio_wfourcc(pb, "tmcd");               /* Data format */
1815     avio_wb32(pb, 0);                       /* Reserved */
1816     avio_wb32(pb, 1);                       /* Data reference index */
1817     avio_wb32(pb, 0);                       /* Flags */
1818     avio_wb32(pb, track->timecode_flags);   /* Flags (timecode) */
1819     avio_wb32(pb, track->timescale);        /* Timescale */
1820     avio_wb32(pb, frame_duration);          /* Frame duration */
1821     avio_w8(pb, nb_frames);                 /* Number of frames */
1822     avio_w8(pb, 0);                         /* Reserved */
1823
1824     if (track->st)
1825         t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
1826
1827     if (t && utf8len(t->value))
1828         mov_write_source_reference_tag(pb, track, t->value);
1829     else
1830         avio_wb16(pb, 0); /* zero size */
1831 #else
1832
1833     avio_wb32(pb, 0); /* size */
1834     ffio_wfourcc(pb, "tmcd");               /* Data format */
1835     avio_wb32(pb, 0);                       /* Reserved */
1836     avio_wb32(pb, 1);                       /* Data reference index */
1837     if (track->enc->extradata_size)
1838         avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1839 #endif
1840     return update_size(pb, pos);
1841 }
1842
1843 static int mov_write_stsd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
1844 {
1845     int64_t pos = avio_tell(pb);
1846     avio_wb32(pb, 0); /* size */
1847     ffio_wfourcc(pb, "stsd");
1848     avio_wb32(pb, 0); /* version & flags */
1849     avio_wb32(pb, 1); /* entry count */
1850     if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1851         mov_write_video_tag(pb, mov,  track);
1852     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1853         mov_write_audio_tag(pb, track);
1854     else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1855         mov_write_subtitle_tag(pb, track);
1856     else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1857         mov_write_rtp_tag(pb, track);
1858     else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1859         mov_write_tmcd_tag(pb, track);
1860     return update_size(pb, pos);
1861 }
1862
1863 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1864 {
1865     MOVStts *ctts_entries;
1866     uint32_t entries = 0;
1867     uint32_t atom_size;
1868     int i;
1869
1870     ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
1871     if (!ctts_entries)
1872         return AVERROR(ENOMEM);
1873     ctts_entries[0].count = 1;
1874     ctts_entries[0].duration = track->cluster[0].cts;
1875     for (i = 1; i < track->entry; i++) {
1876         if (track->cluster[i].cts == ctts_entries[entries].duration) {
1877             ctts_entries[entries].count++; /* compress */
1878         } else {
1879             entries++;
1880             ctts_entries[entries].duration = track->cluster[i].cts;
1881             ctts_entries[entries].count = 1;
1882         }
1883     }
1884     entries++; /* last one */
1885     atom_size = 16 + (entries * 8);
1886     avio_wb32(pb, atom_size); /* size */
1887     ffio_wfourcc(pb, "ctts");
1888     avio_wb32(pb, 0); /* version & flags */
1889     avio_wb32(pb, entries); /* entry count */
1890     for (i = 0; i < entries; i++) {
1891         avio_wb32(pb, ctts_entries[i].count);
1892         avio_wb32(pb, ctts_entries[i].duration);
1893     }
1894     av_free(ctts_entries);
1895     return atom_size;
1896 }
1897
1898 /* Time to sample atom */
1899 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1900 {
1901     MOVStts *stts_entries = NULL;
1902     uint32_t entries = -1;
1903     uint32_t atom_size;
1904     int i;
1905
1906     if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1907         stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1908         if (!stts_entries)
1909             return AVERROR(ENOMEM);
1910         stts_entries[0].count = track->sample_count;
1911         stts_entries[0].duration = 1;
1912         entries = 1;
1913     } else {
1914         if (track->entry) {
1915             stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
1916             if (!stts_entries)
1917                 return AVERROR(ENOMEM);
1918         }
1919         for (i = 0; i < track->entry; i++) {
1920             int duration = get_cluster_duration(track, i);
1921             if (i && duration == stts_entries[entries].duration) {
1922                 stts_entries[entries].count++; /* compress */
1923             } else {
1924                 entries++;
1925                 stts_entries[entries].duration = duration;
1926                 stts_entries[entries].count = 1;
1927             }
1928         }
1929         entries++; /* last one */
1930     }
1931     atom_size = 16 + (entries * 8);
1932     avio_wb32(pb, atom_size); /* size */
1933     ffio_wfourcc(pb, "stts");
1934     avio_wb32(pb, 0); /* version & flags */
1935     avio_wb32(pb, entries); /* entry count */
1936     for (i = 0; i < entries; i++) {
1937         avio_wb32(pb, stts_entries[i].count);
1938         avio_wb32(pb, stts_entries[i].duration);
1939     }
1940     av_free(stts_entries);
1941     return atom_size;
1942 }
1943
1944 static int mov_write_dref_tag(AVIOContext *pb)
1945 {
1946     avio_wb32(pb, 28); /* size */
1947     ffio_wfourcc(pb, "dref");
1948     avio_wb32(pb, 0); /* version & flags */
1949     avio_wb32(pb, 1); /* entry count */
1950
1951     avio_wb32(pb, 0xc); /* size */
1952     //FIXME add the alis and rsrc atom
1953     ffio_wfourcc(pb, "url ");
1954     avio_wb32(pb, 1); /* version & flags */
1955
1956     return 28;
1957 }
1958
1959 static int mov_write_stbl_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
1960 {
1961     int64_t pos = avio_tell(pb);
1962     int ret;
1963
1964     avio_wb32(pb, 0); /* size */
1965     ffio_wfourcc(pb, "stbl");
1966     mov_write_stsd_tag(pb, mov, track);
1967     mov_write_stts_tag(pb, track);
1968     if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1969          track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1970         track->has_keyframes && track->has_keyframes < track->entry)
1971         mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1972     if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1973         mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1974     if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1975         track->flags & MOV_TRACK_CTTS && track->entry) {
1976
1977         if ((ret = mov_write_ctts_tag(pb, track)) < 0)
1978             return ret;
1979     }
1980     mov_write_stsc_tag(pb, track);
1981     mov_write_stsz_tag(pb, track);
1982     mov_write_stco_tag(pb, track);
1983     return update_size(pb, pos);
1984 }
1985
1986 static int mov_write_dinf_tag(AVIOContext *pb)
1987 {
1988     int64_t pos = avio_tell(pb);
1989     avio_wb32(pb, 0); /* size */
1990     ffio_wfourcc(pb, "dinf");
1991     mov_write_dref_tag(pb);
1992     return update_size(pb, pos);
1993 }
1994
1995 static int mov_write_nmhd_tag(AVIOContext *pb)
1996 {
1997     avio_wb32(pb, 12);
1998     ffio_wfourcc(pb, "nmhd");
1999     avio_wb32(pb, 0);
2000     return 12;
2001 }
2002
2003 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
2004 {
2005     int64_t pos = avio_tell(pb);
2006     const char *font = "Lucida Grande";
2007     avio_wb32(pb, 0);                   /* size */
2008     ffio_wfourcc(pb, "tcmi");           /* timecode media information atom */
2009     avio_wb32(pb, 0);                   /* version & flags */
2010     avio_wb16(pb, 0);                   /* text font */
2011     avio_wb16(pb, 0);                   /* text face */
2012     avio_wb16(pb, 12);                  /* text size */
2013     avio_wb16(pb, 0);                   /* (unknown, not in the QT specs...) */
2014     avio_wb16(pb, 0x0000);              /* text color (red) */
2015     avio_wb16(pb, 0x0000);              /* text color (green) */
2016     avio_wb16(pb, 0x0000);              /* text color (blue) */
2017     avio_wb16(pb, 0xffff);              /* background color (red) */
2018     avio_wb16(pb, 0xffff);              /* background color (green) */
2019     avio_wb16(pb, 0xffff);              /* background color (blue) */
2020     avio_w8(pb, strlen(font));          /* font len (part of the pascal string) */
2021     avio_write(pb, font, strlen(font)); /* font name */
2022     return update_size(pb, pos);
2023 }
2024
2025 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
2026 {
2027     int64_t pos = avio_tell(pb);
2028     avio_wb32(pb, 0);      /* size */
2029     ffio_wfourcc(pb, "gmhd");
2030     avio_wb32(pb, 0x18);   /* gmin size */
2031     ffio_wfourcc(pb, "gmin");/* generic media info */
2032     avio_wb32(pb, 0);      /* version & flags */
2033     avio_wb16(pb, 0x40);   /* graphics mode = */
2034     avio_wb16(pb, 0x8000); /* opColor (r?) */
2035     avio_wb16(pb, 0x8000); /* opColor (g?) */
2036     avio_wb16(pb, 0x8000); /* opColor (b?) */
2037     avio_wb16(pb, 0);      /* balance */
2038     avio_wb16(pb, 0);      /* reserved */
2039
2040     /*
2041      * This special text atom is required for
2042      * Apple Quicktime chapters. The contents
2043      * don't appear to be documented, so the
2044      * bytes are copied verbatim.
2045      */
2046     if (track->tag != MKTAG('c','6','0','8')) {
2047     avio_wb32(pb, 0x2C);   /* size */
2048     ffio_wfourcc(pb, "text");
2049     avio_wb16(pb, 0x01);
2050     avio_wb32(pb, 0x00);
2051     avio_wb32(pb, 0x00);
2052     avio_wb32(pb, 0x00);
2053     avio_wb32(pb, 0x01);
2054     avio_wb32(pb, 0x00);
2055     avio_wb32(pb, 0x00);
2056     avio_wb32(pb, 0x00);
2057     avio_wb32(pb, 0x00004000);
2058     avio_wb16(pb, 0x0000);
2059     }
2060
2061     if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
2062         int64_t tmcd_pos = avio_tell(pb);
2063         avio_wb32(pb, 0); /* size */
2064         ffio_wfourcc(pb, "tmcd");
2065         mov_write_tcmi_tag(pb, track);
2066         update_size(pb, tmcd_pos);
2067     }
2068     return update_size(pb, pos);
2069 }
2070
2071 static int mov_write_smhd_tag(AVIOContext *pb)
2072 {
2073     avio_wb32(pb, 16); /* size */
2074     ffio_wfourcc(pb, "smhd");
2075     avio_wb32(pb, 0); /* version & flags */
2076     avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
2077     avio_wb16(pb, 0); /* reserved */
2078     return 16;
2079 }
2080
2081 static int mov_write_vmhd_tag(AVIOContext *pb)
2082 {
2083     avio_wb32(pb, 0x14); /* size (always 0x14) */
2084     ffio_wfourcc(pb, "vmhd");
2085     avio_wb32(pb, 0x01); /* version & flags */
2086     avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
2087     return 0x14;
2088 }
2089
2090 static int is_clcp_track(MOVTrack *track)
2091 {
2092     return track->tag == MKTAG('c','7','0','8') ||
2093            track->tag == MKTAG('c','6','0','8');
2094 }
2095
2096 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
2097 {
2098     const char *hdlr, *descr = NULL, *hdlr_type = NULL;
2099     int64_t pos = avio_tell(pb);
2100
2101     hdlr      = "dhlr";
2102     hdlr_type = "url ";
2103     descr     = "DataHandler";
2104
2105     if (track) {
2106         hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
2107         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2108             hdlr_type = "vide";
2109             descr     = "VideoHandler";
2110         } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2111             hdlr_type = "soun";
2112             descr     = "SoundHandler";
2113         } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2114             if (is_clcp_track(track)) {
2115                 hdlr_type = "clcp";
2116                 descr = "ClosedCaptionHandler";
2117             } else {
2118                 if (track->tag == MKTAG('t','x','3','g')) {
2119                     hdlr_type = "sbtl";
2120                 } else if (track->tag == MKTAG('m','p','4','s')) {
2121                     hdlr_type = "subp";
2122                 } else {
2123                     hdlr_type = "text";
2124                 }
2125             descr = "SubtitleHandler";
2126             }
2127         } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
2128             hdlr_type = "hint";
2129             descr     = "HintHandler";
2130         } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
2131             hdlr_type = "tmcd";
2132             descr = "TimeCodeHandler";
2133         } else {
2134             char tag_buf[32];
2135             av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
2136                                     track->enc->codec_tag);
2137
2138             av_log(track->enc, AV_LOG_WARNING,
2139                    "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
2140                    tag_buf, track->enc->codec_tag);
2141         }
2142         if (track->st) {
2143             // hdlr.name is used by some players to identify the content title
2144             // of the track. So if an alternate handler description is
2145             // specified, use it.
2146             AVDictionaryEntry *t;
2147             t = av_dict_get(track->st->metadata, "handler", NULL, 0);
2148             if (t && utf8len(t->value))
2149                 descr = t->value;
2150         }
2151     }
2152
2153     avio_wb32(pb, 0); /* size */
2154     ffio_wfourcc(pb, "hdlr");
2155     avio_wb32(pb, 0); /* Version & flags */
2156     avio_write(pb, hdlr, 4); /* handler */
2157     ffio_wfourcc(pb, hdlr_type); /* handler type */
2158     avio_wb32(pb, 0); /* reserved */
2159     avio_wb32(pb, 0); /* reserved */
2160     avio_wb32(pb, 0); /* reserved */
2161     if (!track || track->mode == MODE_MOV)
2162         avio_w8(pb, strlen(descr)); /* pascal string */
2163     avio_write(pb, descr, strlen(descr)); /* handler description */
2164     if (track && track->mode != MODE_MOV)
2165         avio_w8(pb, 0); /* c string */
2166     return update_size(pb, pos);
2167 }
2168
2169 static int mov_write_hmhd_tag(AVIOContext *pb)
2170 {
2171     /* This atom must be present, but leaving the values at zero
2172      * seems harmless. */
2173     avio_wb32(pb, 28); /* size */
2174     ffio_wfourcc(pb, "hmhd");
2175     avio_wb32(pb, 0); /* version, flags */
2176     avio_wb16(pb, 0); /* maxPDUsize */
2177     avio_wb16(pb, 0); /* avgPDUsize */
2178     avio_wb32(pb, 0); /* maxbitrate */
2179     avio_wb32(pb, 0); /* avgbitrate */
2180     avio_wb32(pb, 0); /* reserved */
2181     return 28;
2182 }
2183
2184 static int mov_write_minf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
2185 {
2186     int64_t pos = avio_tell(pb);
2187     int ret;
2188
2189     avio_wb32(pb, 0); /* size */
2190     ffio_wfourcc(pb, "minf");
2191     if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
2192         mov_write_vmhd_tag(pb);
2193     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
2194         mov_write_smhd_tag(pb);
2195     else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2196         if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
2197             mov_write_gmhd_tag(pb, track);
2198         } else {
2199             mov_write_nmhd_tag(pb);
2200         }
2201     } else if (track->tag == MKTAG('r','t','p',' ')) {
2202         mov_write_hmhd_tag(pb);
2203     } else if (track->tag == MKTAG('t','m','c','d')) {
2204         mov_write_gmhd_tag(pb, track);
2205     }
2206     if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
2207         mov_write_hdlr_tag(pb, NULL);
2208     mov_write_dinf_tag(pb);
2209     if ((ret = mov_write_stbl_tag(pb, mov, track)) < 0)
2210         return ret;
2211     return update_size(pb, pos);
2212 }
2213
2214 static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov,
2215                               MOVTrack *track)
2216 {
2217     int version = track->track_duration < INT32_MAX ? 0 : 1;
2218
2219     if (track->mode == MODE_ISM)
2220         version = 1;
2221
2222     (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
2223     ffio_wfourcc(pb, "mdhd");
2224     avio_w8(pb, version);
2225     avio_wb24(pb, 0); /* flags */
2226     if (version == 1) {
2227         avio_wb64(pb, track->time);
2228         avio_wb64(pb, track->time);
2229     } else {
2230         avio_wb32(pb, track->time); /* creation time */
2231         avio_wb32(pb, track->time); /* modification time */
2232     }
2233     avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
2234     if (!track->entry && mov->mode == MODE_ISM)
2235         (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2236     else if (!track->entry)
2237         (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2238     else
2239         (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
2240     avio_wb16(pb, track->language); /* language */
2241     avio_wb16(pb, 0); /* reserved (quality) */
2242
2243     if (version != 0 && track->mode == MODE_MOV) {
2244         av_log(NULL, AV_LOG_ERROR,
2245                "FATAL error, file duration too long for timebase, this file will not be\n"
2246                "playable with quicktime. Choose a different timebase or a different\n"
2247                "container format\n");
2248     }
2249
2250     return 32;
2251 }
2252
2253 static int mov_write_mdia_tag(AVIOContext *pb, MOVMuxContext *mov,
2254                               MOVTrack *track)
2255 {
2256     int64_t pos = avio_tell(pb);
2257     int ret;
2258
2259     avio_wb32(pb, 0); /* size */
2260     ffio_wfourcc(pb, "mdia");
2261     mov_write_mdhd_tag(pb, mov, track);
2262     mov_write_hdlr_tag(pb, track);
2263     if ((ret = mov_write_minf_tag(pb, mov, track)) < 0)
2264         return ret;
2265     return update_size(pb, pos);
2266 }
2267
2268 /* transformation matrix
2269      |a  b  u|
2270      |c  d  v|
2271      |tx ty w| */
2272 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
2273                          int16_t d, int16_t tx, int16_t ty)
2274 {
2275     avio_wb32(pb, a << 16);  /* 16.16 format */
2276     avio_wb32(pb, b << 16);  /* 16.16 format */
2277     avio_wb32(pb, 0);        /* u in 2.30 format */
2278     avio_wb32(pb, c << 16);  /* 16.16 format */
2279     avio_wb32(pb, d << 16);  /* 16.16 format */
2280     avio_wb32(pb, 0);        /* v in 2.30 format */
2281     avio_wb32(pb, tx << 16); /* 16.16 format */
2282     avio_wb32(pb, ty << 16); /* 16.16 format */
2283     avio_wb32(pb, 1 << 30);  /* w in 2.30 format */
2284 }
2285
2286 static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov,
2287                               MOVTrack *track, AVStream *st)
2288 {
2289     int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
2290                                       track->timescale, AV_ROUND_UP);
2291     int version = duration < INT32_MAX ? 0 : 1;
2292     int flags   = MOV_TKHD_FLAG_IN_MOVIE;
2293     int rotation = 0;
2294     int group   = 0;
2295
2296     uint32_t *display_matrix = NULL;
2297     int      display_matrix_size, i;
2298
2299     if (st) {
2300         if (mov->per_stream_grouping)
2301             group = st->index;
2302         else
2303             group = st->codec->codec_type;
2304
2305         display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
2306                                                             &display_matrix_size);
2307         if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))
2308             display_matrix = NULL;
2309     }
2310
2311     if (track->flags & MOV_TRACK_ENABLED)
2312         flags |= MOV_TKHD_FLAG_ENABLED;
2313
2314     if (track->mode == MODE_ISM)
2315         version = 1;
2316
2317     (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
2318     ffio_wfourcc(pb, "tkhd");
2319     avio_w8(pb, version);
2320     avio_wb24(pb, flags);
2321     if (version == 1) {
2322         avio_wb64(pb, track->time);
2323         avio_wb64(pb, track->time);
2324     } else {
2325         avio_wb32(pb, track->time); /* creation time */
2326         avio_wb32(pb, track->time); /* modification time */
2327     }
2328     avio_wb32(pb, track->track_id); /* track-id */
2329     avio_wb32(pb, 0); /* reserved */
2330     if (!track->entry && mov->mode == MODE_ISM)
2331         (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2332     else if (!track->entry)
2333         (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2334     else
2335         (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
2336
2337     avio_wb32(pb, 0); /* reserved */
2338     avio_wb32(pb, 0); /* reserved */
2339     avio_wb16(pb, 0); /* layer */
2340     avio_wb16(pb, group); /* alternate group) */
2341     /* Volume, only for audio */
2342     if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
2343         avio_wb16(pb, 0x0100);
2344     else
2345         avio_wb16(pb, 0);
2346     avio_wb16(pb, 0); /* reserved */
2347
2348     /* Matrix structure */
2349     if (st && st->metadata) {
2350         AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
2351         rotation = (rot && rot->value) ? atoi(rot->value) : 0;
2352     }
2353     if (display_matrix) {
2354         for (i = 0; i < 9; i++)
2355             avio_wb32(pb, display_matrix[i]);
2356     } else if (rotation == 90) {
2357         write_matrix(pb,  0,  1, -1,  0, track->enc->height, 0);
2358     } else if (rotation == 180) {
2359         write_matrix(pb, -1,  0,  0, -1, track->enc->width, track->enc->height);
2360     } else if (rotation == 270) {
2361         write_matrix(pb,  0, -1,  1,  0, 0, track->enc->width);
2362     } else {
2363         write_matrix(pb,  1,  0,  0,  1, 0, 0);
2364     }
2365     /* Track width and height, for visual only */
2366     if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
2367                track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
2368         if (track->mode == MODE_MOV) {
2369             avio_wb32(pb, track->enc->width << 16);
2370             avio_wb32(pb, track->height << 16);
2371         } else {
2372             int64_t track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
2373                                                   track->enc->width * 0x10000LL,
2374                                                   st->sample_aspect_ratio.den);
2375             if (!track_width_1616 ||
2376                 track->height != track->enc->height ||
2377                 track_width_1616 > UINT32_MAX)
2378                 track_width_1616 = track->enc->width * 0x10000U;
2379             avio_wb32(pb, track_width_1616);
2380             avio_wb32(pb, track->height * 0x10000U);
2381         }
2382     } else {
2383         avio_wb32(pb, 0);
2384         avio_wb32(pb, 0);
2385     }
2386     return 0x5c;
2387 }
2388
2389 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
2390 {
2391     int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
2392                                track->enc->sample_aspect_ratio.den);
2393
2394     int64_t pos = avio_tell(pb);
2395
2396     avio_wb32(pb, 0); /* size */
2397     ffio_wfourcc(pb, "tapt");
2398
2399     avio_wb32(pb, 20);
2400     ffio_wfourcc(pb, "clef");
2401     avio_wb32(pb, 0);
2402     avio_wb32(pb, width << 16);
2403     avio_wb32(pb, track->enc->height << 16);
2404
2405     avio_wb32(pb, 20);
2406     ffio_wfourcc(pb, "prof");
2407     avio_wb32(pb, 0);
2408     avio_wb32(pb, width << 16);
2409     avio_wb32(pb, track->enc->height << 16);
2410
2411     avio_wb32(pb, 20);
2412     ffio_wfourcc(pb, "enof");
2413     avio_wb32(pb, 0);
2414     avio_wb32(pb, track->enc->width << 16);
2415     avio_wb32(pb, track->enc->height << 16);
2416
2417     return update_size(pb, pos);
2418 }
2419
2420 // This box seems important for the psp playback ... without it the movie seems to hang
2421 static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov,
2422                               MOVTrack *track)
2423 {
2424     int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
2425                                       track->timescale, AV_ROUND_UP);
2426     int version = duration < INT32_MAX ? 0 : 1;
2427     int entry_size, entry_count, size;
2428     int64_t delay, start_ct = track->start_cts;
2429     int64_t start_dts = track->start_dts;
2430
2431     if (track->entry) {
2432         if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
2433
2434             av_log(mov->fc, AV_LOG_DEBUG,
2435                    "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
2436                    track->cluster[0].dts, track->cluster[0].cts,
2437                    start_dts, start_ct, track->track_id);
2438             start_dts = track->cluster[0].dts;
2439             start_ct  = track->cluster[0].cts;
2440         }
2441     }
2442
2443     delay = av_rescale_rnd(start_dts + start_ct, MOV_TIMESCALE,
2444                            track->timescale, AV_ROUND_DOWN);
2445     version |= delay < INT32_MAX ? 0 : 1;
2446
2447     entry_size = (version == 1) ? 20 : 12;
2448     entry_count = 1 + (delay > 0);
2449     size = 24 + entry_count * entry_size;
2450
2451     /* write the atom data */
2452     avio_wb32(pb, size);
2453     ffio_wfourcc(pb, "edts");
2454     avio_wb32(pb, size - 8);
2455     ffio_wfourcc(pb, "elst");
2456     avio_w8(pb, version);
2457     avio_wb24(pb, 0); /* flags */
2458
2459     avio_wb32(pb, entry_count);
2460     if (delay > 0) { /* add an empty edit to delay presentation */
2461         /* In the positive delay case, the delay includes the cts
2462          * offset, and the second edit list entry below trims out
2463          * the same amount from the actual content. This makes sure
2464          * that the offsetted last sample is included in the edit
2465          * list duration as well. */
2466         if (version == 1) {
2467             avio_wb64(pb, delay);
2468             avio_wb64(pb, -1);
2469         } else {
2470             avio_wb32(pb, delay);
2471             avio_wb32(pb, -1);
2472         }
2473         avio_wb32(pb, 0x00010000);
2474     } else {
2475         /* Avoid accidentally ending up with start_ct = -1 which has got a
2476          * special meaning. Normally start_ct should end up positive or zero
2477          * here, but use FFMIN in case dts is a a small positive integer
2478          * rounded to 0 when represented in MOV_TIMESCALE units. */
2479         av_assert0(av_rescale_rnd(start_dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
2480         start_ct  = -FFMIN(start_dts, 0);
2481         /* Note, this delay is calculated from the pts of the first sample,
2482          * ensuring that we don't reduce the duration for cases with
2483          * dts<0 pts=0. */
2484         duration += delay;
2485     }
2486
2487     /* For fragmented files, we don't know the full length yet. Setting
2488      * duration to 0 allows us to only specify the offset, including
2489      * the rest of the content (from all future fragments) without specifying
2490      * an explicit duration. */
2491     if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2492         duration = 0;
2493
2494     /* duration */
2495     if (version == 1) {
2496         avio_wb64(pb, duration);
2497         avio_wb64(pb, start_ct);
2498     } else {
2499         avio_wb32(pb, duration);
2500         avio_wb32(pb, start_ct);
2501     }
2502     avio_wb32(pb, 0x00010000);
2503     return size;
2504 }
2505
2506 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
2507 {
2508     avio_wb32(pb, 20);   // size
2509     ffio_wfourcc(pb, "tref");
2510     avio_wb32(pb, 12);   // size (subatom)
2511     avio_wl32(pb, track->tref_tag);
2512     avio_wb32(pb, track->tref_id);
2513     return 20;
2514 }
2515
2516 // goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
2517 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
2518 {
2519     avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
2520     ffio_wfourcc(pb, "uuid");
2521     ffio_wfourcc(pb, "USMT");
2522     avio_wb32(pb, 0x21d24fce);
2523     avio_wb32(pb, 0xbb88695c);
2524     avio_wb32(pb, 0xfac9c740);
2525     avio_wb32(pb, 0x1c);     // another size here!
2526     ffio_wfourcc(pb, "MTDT");
2527     avio_wb32(pb, 0x00010012);
2528     avio_wb32(pb, 0x0a);
2529     avio_wb32(pb, 0x55c40000);
2530     avio_wb32(pb, 0x1);
2531     avio_wb32(pb, 0x0);
2532     return 0x34;
2533 }
2534
2535 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
2536 {
2537     AVFormatContext *ctx = track->rtp_ctx;
2538     char buf[1000] = "";
2539     int len;
2540
2541     ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
2542                        NULL, NULL, 0, 0, ctx);
2543     av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
2544     len = strlen(buf);
2545
2546     avio_wb32(pb, len + 24);
2547     ffio_wfourcc(pb, "udta");
2548     avio_wb32(pb, len + 16);
2549     ffio_wfourcc(pb, "hnti");
2550     avio_wb32(pb, len + 8);
2551     ffio_wfourcc(pb, "sdp ");
2552     avio_write(pb, buf, len);
2553     return len + 24;
2554 }
2555
2556 static int mov_write_track_metadata(AVIOContext *pb, AVStream *st,
2557                                     const char *tag, const char *str)
2558 {
2559     int64_t pos = avio_tell(pb);
2560     AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
2561     if (!t || !utf8len(t->value))
2562         return 0;
2563
2564     avio_wb32(pb, 0);   /* size */
2565     ffio_wfourcc(pb, tag); /* type */
2566     avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
2567     return update_size(pb, pos);
2568 }
2569
2570 static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
2571                                     AVStream *st)
2572 {
2573     AVIOContext *pb_buf;
2574     int ret, size;
2575     uint8_t *buf;
2576
2577     if (!st)
2578         return 0;
2579
2580     ret = avio_open_dyn_buf(&pb_buf);
2581     if (ret < 0)
2582         return ret;
2583
2584     if (mov->mode & MODE_MP4)
2585         mov_write_track_metadata(pb_buf, st, "name", "title");
2586
2587     if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2588         avio_wb32(pb, size + 8);
2589         ffio_wfourcc(pb, "udta");
2590         avio_write(pb, buf, size);
2591     }
2592     av_free(buf);
2593
2594     return 0;
2595 }
2596
2597 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
2598                               MOVTrack *track, AVStream *st)
2599 {
2600     int64_t pos = avio_tell(pb);
2601     int entry_backup = track->entry;
2602     int chunk_backup = track->chunkCount;
2603     int ret;
2604
2605     /* If we want to have an empty moov, but some samples already have been
2606      * buffered (delay_moov), pretend that no samples have been written yet. */
2607     if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
2608         track->chunkCount = track->entry = 0;
2609
2610     avio_wb32(pb, 0); /* size */
2611     ffio_wfourcc(pb, "trak");
2612     mov_write_tkhd_tag(pb, mov, track, st);
2613
2614     av_assert2(mov->use_editlist >= 0);
2615
2616     if (track->start_dts != AV_NOPTS_VALUE) {
2617         if (mov->use_editlist)
2618             mov_write_edts_tag(pb, mov, track);  // PSP Movies and several other cases require edts box
2619         else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
2620             av_log(mov->fc, AV_LOG_WARNING,
2621                    "Not writing any edit list even though one would have been required\n");
2622     }
2623
2624     if (track->tref_tag)
2625         mov_write_tref_tag(pb, track);
2626
2627     if ((ret = mov_write_mdia_tag(pb, mov, track)) < 0)
2628         return ret;
2629     if (track->mode == MODE_PSP)
2630         mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
2631     if (track->tag == MKTAG('r','t','p',' '))
2632         mov_write_udta_sdp(pb, track);
2633     if (track->mode == MODE_MOV) {
2634         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2635             double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
2636             if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
2637                 mov_write_tapt_tag(pb, track);
2638             }
2639         }
2640         if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
2641             mov_write_tapt_tag(pb, track);
2642         }
2643     }
2644     mov_write_track_udta_tag(pb, mov, st);
2645     track->entry = entry_backup;
2646     track->chunkCount = chunk_backup;
2647     return update_size(pb, pos);
2648 }
2649
2650 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
2651 {
2652     int i, has_audio = 0, has_video = 0;
2653     int64_t pos = avio_tell(pb);
2654     int audio_profile = mov->iods_audio_profile;
2655     int video_profile = mov->iods_video_profile;
2656     for (i = 0; i < mov->nb_streams; i++) {
2657         if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
2658             has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
2659             has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
2660         }
2661     }
2662     if (audio_profile < 0)
2663         audio_profile = 0xFF - has_audio;
2664     if (video_profile < 0)
2665         video_profile = 0xFF - has_video;
2666     avio_wb32(pb, 0x0); /* size */
2667     ffio_wfourcc(pb, "iods");
2668     avio_wb32(pb, 0);    /* version & flags */
2669     put_descr(pb, 0x10, 7);
2670     avio_wb16(pb, 0x004f);
2671     avio_w8(pb, 0xff);
2672     avio_w8(pb, 0xff);
2673     avio_w8(pb, audio_profile);
2674     avio_w8(pb, video_profile);
2675     avio_w8(pb, 0xff);
2676     return update_size(pb, pos);
2677 }
2678
2679 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
2680 {
2681     avio_wb32(pb, 0x20); /* size */
2682     ffio_wfourcc(pb, "trex");
2683     avio_wb32(pb, 0);   /* version & flags */
2684     avio_wb32(pb, track->track_id); /* track ID */
2685     avio_wb32(pb, 1);   /* default sample description index */
2686     avio_wb32(pb, 0);   /* default sample duration */
2687     avio_wb32(pb, 0);   /* default sample size */
2688     avio_wb32(pb, 0);   /* default sample flags */
2689     return 0;
2690 }
2691
2692 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
2693 {
2694     int64_t pos = avio_tell(pb);
2695     int i;
2696     avio_wb32(pb, 0x0); /* size */
2697     ffio_wfourcc(pb, "mvex");
2698     for (i = 0; i < mov->nb_streams; i++)
2699         mov_write_trex_tag(pb, &mov->tracks[i]);
2700     return update_size(pb, pos);
2701 }
2702
2703 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2704 {
2705     int max_track_id = 1, i;
2706     int64_t max_track_len = 0;
2707     int version;
2708
2709     for (i = 0; i < mov->nb_streams; i++) {
2710         if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
2711             int64_t max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
2712                                                 MOV_TIMESCALE,
2713                                                 mov->tracks[i].timescale,
2714                                                 AV_ROUND_UP);
2715             if (max_track_len < max_track_len_temp)
2716                 max_track_len = max_track_len_temp;
2717             if (max_track_id < mov->tracks[i].track_id)
2718                 max_track_id = mov->tracks[i].track_id;
2719         }
2720     }
2721     /* If using delay_moov, make sure the output is the same as if no
2722      * samples had been written yet. */
2723     if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
2724         max_track_len = 0;
2725         max_track_id  = 1;
2726     }
2727
2728     version = max_track_len < UINT32_MAX ? 0 : 1;
2729     avio_wb32(pb, version == 1 ? 120 : 108); /* size */
2730
2731     ffio_wfourcc(pb, "mvhd");
2732     avio_w8(pb, version);
2733     avio_wb24(pb, 0); /* flags */
2734     if (version == 1) {
2735         avio_wb64(pb, mov->time);
2736         avio_wb64(pb, mov->time);
2737     } else {
2738         avio_wb32(pb, mov->time); /* creation time */
2739         avio_wb32(pb, mov->time); /* modification time */
2740     }
2741     avio_wb32(pb, MOV_TIMESCALE);
2742     (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
2743
2744     avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
2745     avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
2746     avio_wb16(pb, 0); /* reserved */
2747     avio_wb32(pb, 0); /* reserved */
2748     avio_wb32(pb, 0); /* reserved */
2749
2750     /* Matrix structure */
2751     write_matrix(pb, 1, 0, 0, 1, 0, 0);
2752
2753     avio_wb32(pb, 0); /* reserved (preview time) */
2754     avio_wb32(pb, 0); /* reserved (preview duration) */
2755     avio_wb32(pb, 0); /* reserved (poster time) */
2756     avio_wb32(pb, 0); /* reserved (selection time) */
2757     avio_wb32(pb, 0); /* reserved (selection duration) */
2758     avio_wb32(pb, 0); /* reserved (current time) */
2759     avio_wb32(pb, max_track_id + 1); /* Next track id */
2760     return 0x6c;
2761 }
2762
2763 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
2764                                      AVFormatContext *s)
2765 {
2766     avio_wb32(pb, 33); /* size */
2767     ffio_wfourcc(pb, "hdlr");
2768     avio_wb32(pb, 0);
2769     avio_wb32(pb, 0);
2770     ffio_wfourcc(pb, "mdir");
2771     ffio_wfourcc(pb, "appl");
2772     avio_wb32(pb, 0);
2773     avio_wb32(pb, 0);
2774     avio_w8(pb, 0);
2775     return 33;
2776 }
2777
2778 /* helper function to write a data tag with the specified string as data */
2779 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
2780 {
2781     if (long_style) {
2782         int size = 16 + strlen(data);
2783         avio_wb32(pb, size); /* size */
2784         ffio_wfourcc(pb, "data");
2785         avio_wb32(pb, 1);
2786         avio_wb32(pb, 0);
2787         avio_write(pb, data, strlen(data));
2788         return size;
2789     } else {
2790         if (!lang)
2791             lang = ff_mov_iso639_to_lang("und", 1);
2792         avio_wb16(pb, strlen(data)); /* string length */
2793         avio_wb16(pb, lang);
2794         avio_write(pb, data, strlen(data));
2795         return strlen(data) + 4;
2796     }
2797 }
2798
2799 static int mov_write_string_tag(AVIOContext *pb, const char *name,
2800                                 const char *value, int lang, int long_style)
2801 {
2802     int size = 0;
2803     if (value && value[0]) {
2804         int64_t pos = avio_tell(pb);
2805         avio_wb32(pb, 0); /* size */
2806         ffio_wfourcc(pb, name);
2807         mov_write_string_data_tag(pb, value, lang, long_style);
2808         size = update_size(pb, pos);
2809     }
2810     return size;
2811 }
2812
2813 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
2814                                      const char *name, const char *tag,
2815                                      int long_style)
2816 {
2817     int l, lang = 0, len, len2;
2818     AVDictionaryEntry *t, *t2 = NULL;
2819     char tag2[16];
2820
2821     if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2822         return 0;
2823
2824     len = strlen(t->key);
2825     snprintf(tag2, sizeof(tag2), "%s-", tag);
2826     while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
2827         len2 = strlen(t2->key);
2828         if (len2 == len + 4 && !strcmp(t->value, t2->value)
2829             && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
2830             lang = l;
2831             break;
2832         }
2833     }
2834     return mov_write_string_tag(pb, name, t->value, lang, long_style);
2835 }
2836
2837 /* iTunes bpm number */
2838 static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
2839 {
2840     AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
2841     int size = 0, tmpo = t ? atoi(t->value) : 0;
2842     if (tmpo) {
2843         size = 26;
2844         avio_wb32(pb, size);
2845         ffio_wfourcc(pb, "tmpo");
2846         avio_wb32(pb, size-8); /* size */
2847         ffio_wfourcc(pb, "data");
2848         avio_wb32(pb, 0x15);  //type specifier
2849         avio_wb32(pb, 0);
2850         avio_wb16(pb, tmpo);        // data
2851     }
2852     return size;
2853 }
2854
2855 /* iTunes track or disc number */
2856 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
2857                               AVFormatContext *s, int disc)
2858 {
2859     AVDictionaryEntry *t = av_dict_get(s->metadata,
2860                                        disc ? "disc" : "track",
2861                                        NULL, 0);
2862     int size = 0, track = t ? atoi(t->value) : 0;
2863     if (track) {
2864         int tracks = 0;
2865         char *slash = strchr(t->value, '/');
2866         if (slash)
2867             tracks = atoi(slash + 1);
2868         avio_wb32(pb, 32); /* size */
2869         ffio_wfourcc(pb, disc ? "disk" : "trkn");
2870         avio_wb32(pb, 24); /* size */
2871         ffio_wfourcc(pb, "data");
2872         avio_wb32(pb, 0);        // 8 bytes empty
2873         avio_wb32(pb, 0);
2874         avio_wb16(pb, 0);        // empty
2875         avio_wb16(pb, track);    // track / disc number
2876         avio_wb16(pb, tracks);   // total track / disc number
2877         avio_wb16(pb, 0);        // empty
2878         size = 32;
2879     }
2880     return size;
2881 }
2882
2883 static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
2884                                    const char *name, const char *tag,
2885                                    int len)
2886 {
2887     AVDictionaryEntry *t = NULL;
2888     uint8_t num;
2889     int size = 24 + len;
2890
2891     if (len != 1 && len != 4)
2892         return -1;
2893
2894     if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2895         return 0;
2896     num = atoi(t->value);
2897
2898     avio_wb32(pb, size);
2899     ffio_wfourcc(pb, name);
2900     avio_wb32(pb, size - 8);
2901     ffio_wfourcc(pb, "data");
2902     avio_wb32(pb, 0x15);
2903     avio_wb32(pb, 0);
2904     if (len==4) avio_wb32(pb, num);
2905     else        avio_w8 (pb, num);
2906
2907     return size;
2908 }
2909
2910 /* iTunes meta data list */
2911 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
2912                               AVFormatContext *s)
2913 {
2914     int64_t pos = avio_tell(pb);
2915     avio_wb32(pb, 0); /* size */
2916     ffio_wfourcc(pb, "ilst");
2917     mov_write_string_metadata(s, pb, "\251nam", "title"    , 1);
2918     mov_write_string_metadata(s, pb, "\251ART", "artist"   , 1);
2919     mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
2920     mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
2921     mov_write_string_metadata(s, pb, "\251alb", "album"    , 1);
2922     mov_write_string_metadata(s, pb, "\251day", "date"     , 1);
2923     if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
2924         if (!(s->flags & AVFMT_FLAG_BITEXACT))
2925             mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
2926     }
2927     mov_write_string_metadata(s, pb, "\251cmt", "comment"  , 1);
2928     mov_write_string_metadata(s, pb, "\251gen", "genre"    , 1);
2929     mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2930     mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
2931     mov_write_string_metadata(s, pb, "\251lyr", "lyrics"   , 1);
2932     mov_write_string_metadata(s, pb, "desc",    "description",1);
2933     mov_write_string_metadata(s, pb, "ldes",    "synopsis" , 1);
2934     mov_write_string_metadata(s, pb, "tvsh",    "show"     , 1);
2935     mov_write_string_metadata(s, pb, "tven",    "episode_id",1);
2936     mov_write_string_metadata(s, pb, "tvnn",    "network"  , 1);
2937     mov_write_int8_metadata  (s, pb, "tves",    "episode_sort",4);
2938     mov_write_int8_metadata  (s, pb, "tvsn",    "season_number",4);
2939     mov_write_int8_metadata  (s, pb, "stik",    "media_type",1);
2940     mov_write_int8_metadata  (s, pb, "hdvd",    "hd_video",  1);
2941     mov_write_int8_metadata  (s, pb, "pgap",    "gapless_playback",1);
2942     mov_write_int8_metadata  (s, pb, "cpil",    "compilation", 1);
2943     mov_write_trkn_tag(pb, mov, s, 0); // track number
2944     mov_write_trkn_tag(pb, mov, s, 1); // disc number
2945     mov_write_tmpo_tag(pb, s);
2946     return update_size(pb, pos);
2947 }
2948
2949 /* iTunes meta data tag */
2950 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
2951                               AVFormatContext *s)
2952 {
2953     int size = 0;
2954     int64_t pos = avio_tell(pb);
2955     avio_wb32(pb, 0); /* size */
2956     ffio_wfourcc(pb, "meta");
2957     avio_wb32(pb, 0);
2958     mov_write_itunes_hdlr_tag(pb, mov, s);
2959     mov_write_ilst_tag(pb, mov, s);
2960     size = update_size(pb, pos);
2961     return size;
2962 }
2963
2964 static int mov_write_raw_metadata_tag(AVFormatContext *s, AVIOContext *pb,
2965                                       const char *name, const char *key)
2966 {
2967     int len;
2968     AVDictionaryEntry *t;
2969
2970     if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
2971         return 0;
2972
2973     len = strlen(t->value);
2974     if (len > 0) {
2975         int size = len + 8;
2976         avio_wb32(pb, size);
2977         ffio_wfourcc(pb, name);
2978         avio_write(pb, t->value, len);
2979         return size;
2980     }
2981     return 0;
2982 }
2983
2984 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2985 {
2986     int val;
2987     while (*b) {
2988         GET_UTF8(val, *b++, return -1;)
2989         avio_wb16(pb, val);
2990     }
2991     avio_wb16(pb, 0x00);
2992     return 0;
2993 }
2994
2995 static uint16_t language_code(const char *str)
2996 {
2997     return (((str[0] - 0x60) & 0x1F) << 10) +
2998            (((str[1] - 0x60) & 0x1F) <<  5) +
2999            (( str[2] - 0x60) & 0x1F);
3000 }
3001
3002 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
3003                                   const char *tag, const char *str)
3004 {
3005     int64_t pos = avio_tell(pb);
3006     AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
3007     if (!t || !utf8len(t->value))
3008         return 0;
3009     avio_wb32(pb, 0);   /* size */
3010     ffio_wfourcc(pb, tag); /* type */
3011     avio_wb32(pb, 0);   /* version + flags */
3012     if (!strcmp(tag, "yrrc"))
3013         avio_wb16(pb, atoi(t->value));
3014     else {
3015         avio_wb16(pb, language_code("eng")); /* language */
3016         avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
3017         if (!strcmp(tag, "albm") &&
3018             (t = av_dict_get(s->metadata, "track", NULL, 0)))
3019             avio_w8(pb, atoi(t->value));
3020     }
3021     return update_size(pb, pos);
3022 }
3023
3024 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
3025 {
3026     int64_t pos = avio_tell(pb);
3027     int i, nb_chapters = FFMIN(s->nb_chapters, 255);
3028
3029     avio_wb32(pb, 0);            // size
3030     ffio_wfourcc(pb, "chpl");
3031     avio_wb32(pb, 0x01000000);   // version + flags
3032     avio_wb32(pb, 0);            // unknown
3033     avio_w8(pb, nb_chapters);
3034
3035     for (i = 0; i < nb_chapters; i++) {
3036         AVChapter *c = s->chapters[i];
3037         AVDictionaryEntry *t;
3038         avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
3039
3040         if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3041             int len = FFMIN(strlen(t->value), 255);
3042             avio_w8(pb, len);
3043             avio_write(pb, t->value, len);
3044         } else
3045             avio_w8(pb, 0);
3046     }
3047     return update_size(pb, pos);
3048 }
3049
3050 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
3051                               AVFormatContext *s)
3052 {
3053     AVIOContext *pb_buf;
3054     int ret, size;
3055     uint8_t *buf;
3056
3057     ret = avio_open_dyn_buf(&pb_buf);
3058     if (ret < 0)
3059         return ret;
3060
3061     if (mov->mode & MODE_3GP) {
3062         mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
3063         mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
3064         mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
3065         mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
3066         mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
3067         mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
3068         mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
3069         mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
3070     } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
3071         mov_write_string_metadata(s, pb_buf, "\251ART", "artist",      0);
3072         mov_write_string_metadata(s, pb_buf, "\251nam", "title",       0);
3073         mov_write_string_metadata(s, pb_buf, "\251aut", "author",      0);
3074         mov_write_string_metadata(s, pb_buf, "\251alb", "album",       0);
3075         mov_write_string_metadata(s, pb_buf, "\251day", "date",        0);
3076         mov_write_string_metadata(s, pb_buf, "\251swr", "encoder",     0);
3077         // currently ignored by mov.c
3078         mov_write_string_metadata(s, pb_buf, "\251des", "comment",     0);
3079         // add support for libquicktime, this atom is also actually read by mov.c
3080         mov_write_string_metadata(s, pb_buf, "\251cmt", "comment",     0);
3081         mov_write_string_metadata(s, pb_buf, "\251gen", "genre",       0);
3082         mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright",   0);
3083         mov_write_string_metadata(s, pb_buf, "\251mak", "make",        0);
3084         mov_write_string_metadata(s, pb_buf, "\251mod", "model",       0);
3085         mov_write_string_metadata(s, pb_buf, "\251xyz", "location",    0);
3086         mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
3087     } else {
3088         /* iTunes meta data */
3089         mov_write_meta_tag(pb_buf, mov, s);
3090     }
3091
3092     if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
3093         mov_write_chpl_tag(pb_buf, s);
3094
3095     if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3096         avio_wb32(pb, size + 8);
3097         ffio_wfourcc(pb, "udta");
3098         avio_write(pb, buf, size);
3099     }
3100     av_free(buf);
3101
3102     return 0;
3103 }
3104
3105 static void mov_write_psp_udta_tag(AVIOContext *pb,
3106                                    const char *str, const char *lang, int type)
3107 {
3108     int len = utf8len(str) + 1;
3109     if (len <= 0)
3110         return;
3111     avio_wb16(pb, len * 2 + 10);        /* size */
3112     avio_wb32(pb, type);                /* type */
3113     avio_wb16(pb, language_code(lang)); /* language */
3114     avio_wb16(pb, 0x01);                /* ? */
3115     ascii_to_wc(pb, str);
3116 }
3117
3118 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
3119 {
3120     AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
3121     int64_t pos, pos2;
3122
3123     if (title) {
3124         pos = avio_tell(pb);
3125         avio_wb32(pb, 0); /* size placeholder*/
3126         ffio_wfourcc(pb, "uuid");
3127         ffio_wfourcc(pb, "USMT");
3128         avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3129         avio_wb32(pb, 0xbb88695c);
3130         avio_wb32(pb, 0xfac9c740);
3131
3132         pos2 = avio_tell(pb);
3133         avio_wb32(pb, 0); /* size placeholder*/
3134         ffio_wfourcc(pb, "MTDT");
3135         avio_wb16(pb, 4);
3136
3137         // ?
3138         avio_wb16(pb, 0x0C);                 /* size */
3139         avio_wb32(pb, 0x0B);                 /* type */
3140         avio_wb16(pb, language_code("und")); /* language */
3141         avio_wb16(pb, 0x0);                  /* ? */
3142         avio_wb16(pb, 0x021C);               /* data */
3143
3144         if (!(s->flags & AVFMT_FLAG_BITEXACT))
3145             mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT,      "eng", 0x04);
3146         mov_write_psp_udta_tag(pb, title->value,          "eng", 0x01);
3147         mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
3148
3149         update_size(pb, pos2);
3150         return update_size(pb, pos);
3151     }
3152
3153     return 0;
3154 }
3155
3156 static void build_chunks(MOVTrack *trk)
3157 {
3158     int i;
3159     MOVIentry *chunk = &trk->cluster[0];
3160     uint64_t chunkSize = chunk->size;
3161     chunk->chunkNum = 1;
3162     if (trk->chunkCount)
3163         return;
3164     trk->chunkCount = 1;
3165     for (i = 1; i<trk->entry; i++){
3166         if (chunk->pos + chunkSize == trk->cluster[i].pos &&
3167             chunkSize + trk->cluster[i].size < (1<<20)){
3168             chunkSize             += trk->cluster[i].size;
3169             chunk->samples_in_chunk += trk->cluster[i].entries;
3170         } else {
3171             trk->cluster[i].chunkNum = chunk->chunkNum+1;
3172             chunk=&trk->cluster[i];
3173             chunkSize = chunk->size;
3174             trk->chunkCount++;
3175         }
3176     }
3177 }
3178
3179 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
3180                               AVFormatContext *s)
3181 {
3182     int i;
3183     int64_t pos = avio_tell(pb);
3184     avio_wb32(pb, 0); /* size placeholder*/
3185     ffio_wfourcc(pb, "moov");
3186
3187     for (i = 0; i < mov->nb_streams; i++) {
3188         if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3189             continue;
3190
3191         mov->tracks[i].time     = mov->time;
3192         mov->tracks[i].track_id = i + 1;
3193
3194         if (mov->tracks[i].entry)
3195             build_chunks(&mov->tracks[i]);
3196     }
3197
3198     if (mov->chapter_track)
3199         for (i = 0; i < s->nb_streams; i++) {
3200             mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
3201             mov->tracks[i].tref_id  = mov->tracks[mov->chapter_track].track_id;
3202         }
3203     for (i = 0; i < mov->nb_streams; i++) {
3204         if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
3205             mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
3206             mov->tracks[i].tref_id =
3207                 mov->tracks[mov->tracks[i].src_track].track_id;
3208         }
3209     }
3210     for (i = 0; i < mov->nb_streams; i++) {
3211         if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
3212             int src_trk = mov->tracks[i].src_track;
3213             mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
3214             mov->tracks[src_trk].tref_id  = mov->tracks[i].track_id;
3215             //src_trk may have a different timescale than the tmcd track
3216             mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
3217                                                        mov->tracks[i].timescale,
3218                                                        mov->tracks[src_trk].timescale);
3219         }
3220     }
3221
3222     mov_write_mvhd_tag(pb, mov);
3223     if (mov->mode != MODE_MOV && !mov->iods_skip)
3224         mov_write_iods_tag(pb, mov);
3225     for (i = 0; i < mov->nb_streams; i++) {
3226         if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
3227             int ret = mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
3228             if (ret < 0)
3229                 return ret;
3230         }
3231     }
3232     if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3233         mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
3234
3235     if (mov->mode == MODE_PSP)
3236         mov_write_uuidusmt_tag(pb, s);
3237     else
3238         mov_write_udta_tag(pb, mov, s);
3239
3240     return update_size(pb, pos);
3241 }
3242
3243 static void param_write_int(AVIOContext *pb, const char *name, int value)
3244 {
3245     avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
3246 }
3247
3248 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
3249 {
3250     avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
3251 }
3252
3253 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
3254 {
3255     char buf[150];
3256     len = FFMIN(sizeof(buf) / 2 - 1, len);
3257     ff_data_to_hex(buf, value, len, 0);
3258     buf[2 * len] = '\0';
3259     avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
3260 }
3261
3262 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
3263 {
3264     int64_t pos = avio_tell(pb);
3265     int i;
3266     static const uint8_t uuid[] = {
3267         0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3268         0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3269     };
3270
3271     avio_wb32(pb, 0);
3272     ffio_wfourcc(pb, "uuid");
3273     avio_write(pb, uuid, sizeof(uuid));
3274     avio_wb32(pb, 0);
3275
3276     avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
3277     avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
3278     avio_printf(pb, "<head>\n");
3279     if (!(mov->fc->flags & AVFMT_FLAG_BITEXACT))
3280         avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
3281                     LIBAVFORMAT_IDENT);
3282     avio_printf(pb, "</head>\n");
3283     avio_printf(pb, "<body>\n");
3284     avio_printf(pb, "<switch>\n");
3285     for (i = 0; i < mov->nb_streams; i++) {
3286         MOVTrack *track = &mov->tracks[i];
3287         const char *type;
3288         /* track->track_id is initialized in write_moov, and thus isn't known
3289          * here yet */
3290         int track_id = i + 1;
3291
3292         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
3293             type = "video";
3294         } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
3295             type = "audio";
3296         } else {
3297             continue;
3298         }
3299         avio_printf(pb, "<%s systemBitrate=\"%"PRId64"\">\n", type,
3300                     (int64_t)track->enc->bit_rate);
3301         param_write_int(pb, "systemBitrate", track->enc->bit_rate);
3302         param_write_int(pb, "trackID", track_id);
3303         if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
3304             if (track->enc->codec_id == AV_CODEC_ID_H264) {
3305                 uint8_t *ptr;
3306                 int size = track->enc->extradata_size;
3307                 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
3308                                                    &size)) {
3309                     param_write_hex(pb, "CodecPrivateData",
3310                                     ptr ? ptr : track->enc->extradata,
3311                                     size);
3312                     av_free(ptr);
3313                 }
3314                 param_write_string(pb, "FourCC", "H264");
3315             } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
3316                 param_write_string(pb, "FourCC", "WVC1");
3317                 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
3318                                 track->enc->extradata_size);
3319             }
3320             param_write_int(pb, "MaxWidth", track->enc->width);
3321             param_write_int(pb, "MaxHeight", track->enc->height);
3322             param_write_int(pb, "DisplayWidth", track->enc->width);
3323             param_write_int(pb, "DisplayHeight", track->enc->height);
3324         } else {
3325             if (track->enc->codec_id == AV_CODEC_ID_AAC) {
3326                 param_write_string(pb, "FourCC", "AACL");
3327             } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
3328                 param_write_string(pb, "FourCC", "WMAP");
3329             }
3330             param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
3331                             track->enc->extradata_size);
3332             param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
3333                                                              track->enc->codec_id));
3334             param_write_int(pb, "Channels", track->enc->channels);
3335             param_write_int(pb, "SamplingRate", track->enc->sample_rate);
3336             param_write_int(pb, "BitsPerSample", 16);
3337             param_write_int(pb, "PacketSize", track->enc->block_align ?
3338                                               track->enc->block_align : 4);
3339         }
3340         avio_printf(pb, "</%s>\n", type);
3341     }
3342     avio_printf(pb, "</switch>\n");
3343     avio_printf(pb, "</body>\n");
3344     avio_printf(pb, "</smil>\n");
3345
3346     return update_size(pb, pos);
3347 }
3348
3349 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
3350 {
3351     avio_wb32(pb, 16);
3352     ffio_wfourcc(pb, "mfhd");
3353     avio_wb32(pb, 0);
3354     avio_wb32(pb, mov->fragments);
3355     return 0;
3356 }
3357
3358 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
3359 {
3360     return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
3361            (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
3362 }
3363
3364 static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov,
3365                               MOVTrack *track, int64_t moof_offset)
3366 {
3367     int64_t pos = avio_tell(pb);
3368     uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
3369                      MOV_TFHD_BASE_DATA_OFFSET;
3370     if (!track->entry) {
3371         flags |= MOV_TFHD_DURATION_IS_EMPTY;
3372     } else {
3373         flags |= MOV_TFHD_DEFAULT_FLAGS;
3374     }
3375     if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET)
3376         flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
3377     if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
3378         flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
3379         flags |= MOV_TFHD_DEFAULT_BASE_IS_MOOF;
3380     }
3381
3382     /* Don't set a default sample size, the silverlight player refuses
3383      * to play files with that set. Don't set a default sample duration,
3384      * WMP freaks out if it is set. Don't set a base data offset, PIFF
3385      * file format says it MUST NOT be set. */
3386     if (track->mode == MODE_ISM)
3387         flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
3388                    MOV_TFHD_BASE_DATA_OFFSET);
3389
3390     avio_wb32(pb, 0); /* size placeholder */
3391     ffio_wfourcc(pb, "tfhd");
3392     avio_w8(pb, 0); /* version */
3393     avio_wb24(pb, flags);
3394
3395     avio_wb32(pb, track->track_id); /* track-id */
3396     if (flags & MOV_TFHD_BASE_DATA_OFFSET)
3397         avio_wb64(pb, moof_offset);
3398     if (flags & MOV_TFHD_DEFAULT_DURATION) {
3399         track->default_duration = get_cluster_duration(track, 0);
3400         avio_wb32(pb, track->default_duration);
3401     }
3402     if (flags & MOV_TFHD_DEFAULT_SIZE) {
3403         track->default_size = track->entry ? track->cluster[0].size : 1;
3404         avio_wb32(pb, track->default_size);
3405     } else
3406         track->default_size = -1;
3407
3408     if (flags & MOV_TFHD_DEFAULT_FLAGS) {
3409         /* Set the default flags based on the second sample, if available.
3410          * If the first sample is different, that can be signaled via a separate field. */
3411         if (track->entry > 1)
3412             track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
3413         else
3414             track->default_sample_flags =
3415                 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
3416                 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
3417                 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
3418         avio_wb32(pb, track->default_sample_flags);
3419     }
3420
3421     return update_size(pb, pos);
3422 }
3423
3424 static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov,
3425                               MOVTrack *track, int moof_size,
3426                               int first, int end)
3427 {
3428     int64_t pos = avio_tell(pb);
3429     uint32_t flags = MOV_TRUN_DATA_OFFSET;
3430     int i;
3431
3432     for (i = first; i < end; i++) {
3433         if (get_cluster_duration(track, i) != track->default_duration)
3434             flags |= MOV_TRUN_SAMPLE_DURATION;
3435         if (track->cluster[i].size != track->default_size)
3436             flags |= MOV_TRUN_SAMPLE_SIZE;
3437         if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
3438             flags |= MOV_TRUN_SAMPLE_FLAGS;
3439     }
3440     if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
3441          get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
3442         flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
3443     if (track->flags & MOV_TRACK_CTTS)
3444         flags |= MOV_TRUN_SAMPLE_CTS;
3445
3446     avio_wb32(pb, 0); /* size placeholder */
3447     ffio_wfourcc(pb, "trun");
3448     avio_w8(pb, 0); /* version */
3449     avio_wb24(pb, flags);
3450
3451     avio_wb32(pb, end - first); /* sample count */
3452     if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
3453         !(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) &&
3454         !mov->first_trun)
3455         avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
3456     else
3457         avio_wb32(pb, moof_size + 8 + track->data_offset +
3458                       track->cluster[first].pos); /* data offset */
3459     if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
3460         avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
3461
3462     for (i = first; i < end; i++) {
3463         if (flags & MOV_TRUN_SAMPLE_DURATION)
3464             avio_wb32(pb, get_cluster_duration(track, i));
3465         if (flags & MOV_TRUN_SAMPLE_SIZE)
3466             avio_wb32(pb, track->cluster[i].size);
3467         if (flags & MOV_TRUN_SAMPLE_FLAGS)
3468             avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
3469         if (flags & MOV_TRUN_SAMPLE_CTS)
3470             avio_wb32(pb, track->cluster[i].cts);
3471     }
3472
3473     mov->first_trun = 0;
3474     return update_size(pb, pos);
3475 }
3476
3477 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
3478 {
3479     int64_t pos = avio_tell(pb);
3480     static const uint8_t uuid[] = {
3481         0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
3482         0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
3483     };
3484
3485     avio_wb32(pb, 0); /* size placeholder */
3486     ffio_wfourcc(pb, "uuid");
3487     avio_write(pb, uuid, sizeof(uuid));
3488     avio_w8(pb, 1);
3489     avio_wb24(pb, 0);
3490     avio_wb64(pb, track->start_dts + track->frag_start +
3491                   track->cluster[0].cts);
3492     avio_wb64(pb, track->end_pts -
3493                   (track->cluster[0].dts + track->cluster[0].cts));
3494
3495     return update_size(pb, pos);
3496 }
3497
3498 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
3499                               MOVTrack *track, int entry)
3500 {
3501     int n = track->nb_frag_info - 1 - entry, i;
3502     int size = 8 + 16 + 4 + 1 + 16*n;
3503     static const uint8_t uuid[] = {
3504         0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
3505         0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
3506     };
3507
3508     if (entry < 0)
3509         return 0;
3510
3511     avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
3512     avio_wb32(pb, size);
3513     ffio_wfourcc(pb, "uuid");
3514     avio_write(pb, uuid, sizeof(uuid));
3515     avio_w8(pb, 1);
3516     avio_wb24(pb, 0);
3517     avio_w8(pb, n);
3518     for (i = 0; i < n; i++) {
3519         int index = entry + 1 + i;
3520         avio_wb64(pb, track->frag_info[index].time);
3521         avio_wb64(pb, track->frag_info[index].duration);
3522     }
3523     if (n < mov->ism_lookahead) {
3524         int free_size = 16 * (mov->ism_lookahead - n);
3525         avio_wb32(pb, free_size);
3526         ffio_wfourcc(pb, "free");
3527         ffio_fill(pb, 0, free_size - 8);
3528     }
3529
3530     return 0;
3531 }
3532
3533 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
3534                                MOVTrack *track)
3535 {
3536     int64_t pos = avio_tell(pb);
3537     int i;
3538     for (i = 0; i < mov->ism_lookahead; i++) {
3539         /* Update the tfrf tag for the last ism_lookahead fragments,
3540          * nb_frag_info - 1 is the next fragment to be written. */
3541         mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
3542     }
3543     avio_seek(pb, pos, SEEK_SET);
3544     return 0;
3545 }
3546
3547 static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
3548                                 int size)
3549 {
3550     int i;
3551     for (i = 0; i < mov->nb_streams; i++) {
3552         MOVTrack *track = &mov->tracks[i];
3553         MOVFragmentInfo *info;
3554         if ((tracks >= 0 && i != tracks) || !track->entry)
3555             continue;
3556         track->nb_frag_info++;
3557         if (track->nb_frag_info >= track->frag_info_capacity) {
3558             unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
3559             if (av_reallocp_array(&track->frag_info,
3560                                   new_capacity,
3561                                   sizeof(*track->frag_info)))
3562                 return AVERROR(ENOMEM);
3563             track->frag_info_capacity = new_capacity;
3564         }
3565         info = &track->frag_info[track->nb_frag_info - 1];
3566         info->offset   = avio_tell(pb);
3567         info->size     = size;
3568         // Try to recreate the original pts for the first packet
3569         // from the fields we have stored
3570         info->time     = track->start_dts + track->frag_start +
3571                          track->cluster[0].cts;
3572         info->duration = track->end_pts -
3573                          (track->cluster[0].dts + track->cluster[0].cts);
3574         // If the pts is less than zero, we will have trimmed
3575         // away parts of the media track using an edit list,
3576         // and the corresponding start presentation time is zero.
3577         if (info->time < 0) {
3578             info->duration += info->time;
3579             info->time = 0;
3580         }
3581         info->tfrf_offset = 0;
3582         mov_write_tfrf_tags(pb, mov, track);
3583     }
3584     return 0;
3585 }
3586
3587 static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
3588 {
3589     int64_t pos = avio_tell(pb);
3590
3591     avio_wb32(pb, 0); /* size */
3592     ffio_wfourcc(pb, "tfdt");
3593     avio_w8(pb, 1); /* version */
3594     avio_wb24(pb, 0);
3595     avio_wb64(pb, track->frag_start);
3596     return update_size(pb, pos);
3597 }
3598
3599 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
3600                               MOVTrack *track, int64_t moof_offset,
3601                               int moof_size)
3602 {
3603     int64_t pos = avio_tell(pb);
3604     int i, start = 0;
3605     avio_wb32(pb, 0); /* size placeholder */
3606     ffio_wfourcc(pb, "traf");
3607
3608     mov_write_tfhd_tag(pb, mov, track, moof_offset);
3609     if (mov->mode != MODE_ISM)
3610         mov_write_tfdt_tag(pb, track);
3611     for (i = 1; i < track->entry; i++) {
3612         if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
3613             mov_write_trun_tag(pb, mov, track, moof_size, start, i);
3614             start = i;
3615         }
3616     }
3617     mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
3618     if (mov->mode == MODE_ISM) {
3619         mov_write_tfxd_tag(pb, track);
3620
3621         if (mov->ism_lookahead) {
3622             int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
3623
3624             if (track->nb_frag_info > 0) {
3625                 MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
3626                 if (!info->tfrf_offset)
3627                     info->tfrf_offset = avio_tell(pb);
3628             }
3629             avio_wb32(pb, 8 + size);
3630             ffio_wfourcc(pb, "free");
3631             for (i = 0; i < size; i++)
3632                 avio_w8(pb, 0);
3633         }
3634     }
3635
3636     return update_size(pb, pos);
3637 }
3638
3639 static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
3640                                        int tracks, int moof_size)
3641 {
3642     int64_t pos = avio_tell(pb);
3643     int i;
3644
3645     avio_wb32(pb, 0); /* size placeholder */
3646     ffio_wfourcc(pb, "moof");
3647     mov->first_trun = 1;
3648
3649     mov_write_mfhd_tag(pb, mov);
3650     for (i = 0; i < mov->nb_streams; i++) {
3651         MOVTrack *track = &mov->tracks[i];
3652         if (tracks >= 0 && i != tracks)
3653             continue;
3654         if (!track->entry)
3655             continue;
3656         mov_write_traf_tag(pb, mov, track, pos, moof_size);
3657     }
3658
3659     return update_size(pb, pos);
3660 }
3661
3662 static int mov_write_sidx_tag(AVIOContext *pb,
3663                               MOVTrack *track, int ref_size, int total_sidx_size)
3664 {
3665     int64_t pos = avio_tell(pb), offset_pos, end_pos;
3666     int64_t presentation_time, duration, offset;
3667     int starts_with_SAP, i, entries;
3668
3669     if (track->entry) {
3670         entries = 1;
3671         presentation_time = track->start_dts + track->frag_start +
3672                             track->cluster[0].cts;
3673         duration = track->end_pts -
3674                    (track->cluster[0].dts + track->cluster[0].cts);
3675         starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
3676
3677         // pts<0 should be cut away using edts
3678         if (presentation_time < 0) {
3679             duration += presentation_time;
3680             presentation_time = 0;
3681         }
3682     } else {
3683         entries = track->nb_frag_info;
3684         if (entries <= 0)
3685             return 0;
3686         presentation_time = track->frag_info[0].time;
3687     }
3688
3689     avio_wb32(pb, 0); /* size */
3690     ffio_wfourcc(pb, "sidx");
3691     avio_w8(pb, 1); /* version */
3692     avio_wb24(pb, 0);
3693     avio_wb32(pb, track->track_id); /* reference_ID */
3694     avio_wb32(pb, track->timescale); /* timescale */
3695     avio_wb64(pb, presentation_time); /* earliest_presentation_time */
3696     offset_pos = avio_tell(pb);
3697     avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
3698     avio_wb16(pb, 0); /* reserved */
3699
3700     avio_wb16(pb, entries); /* reference_count */
3701     for (i = 0; i < entries; i++) {
3702         if (!track->entry) {
3703             if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
3704                av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
3705             }
3706             duration = track->frag_info[i].duration;
3707             ref_size = track->frag_info[i].size;
3708             starts_with_SAP = 1;
3709         }
3710         avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
3711         avio_wb32(pb, duration); /* subsegment_duration */
3712         avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
3713     }
3714
3715     end_pos = avio_tell(pb);
3716     offset = pos + total_sidx_size - end_pos;
3717     avio_seek(pb, offset_pos, SEEK_SET);
3718     avio_wb64(pb, offset);
3719     avio_seek(pb, end_pos, SEEK_SET);
3720     return update_size(pb, pos);
3721 }
3722
3723 static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov,
3724                                int tracks, int ref_size)
3725 {
3726     int i, round, ret;
3727     AVIOContext *avio_buf;
3728     int total_size = 0;
3729     for (round = 0; round < 2; round++) {
3730         // First run one round to calculate the total size of all
3731         // sidx atoms.
3732         // This would be much simpler if we'd only write one sidx
3733         // atom, for the first track in the moof.
3734         if (round == 0) {
3735             if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
3736                 return ret;
3737         } else {
3738             avio_buf = pb;
3739         }
3740         for (i = 0; i < mov->nb_streams; i++) {
3741             MOVTrack *track = &mov->tracks[i];
3742             if (tracks >= 0 && i != tracks)
3743                 continue;
3744             // When writing a sidx for the full file, entry is 0, but
3745             // we want to include all tracks. ref_size is 0 in this case,
3746             // since we read it from frag_info instead.
3747             if (!track->entry && ref_size > 0)
3748                 continue;
3749             total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
3750                                              total_size);
3751         }
3752         if (round == 0)
3753             total_size = ffio_close_null_buf(avio_buf);
3754     }
3755     return 0;
3756 }
3757
3758 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
3759                               int64_t mdat_size)
3760 {
3761     AVIOContext *avio_buf;
3762     int ret, moof_size;
3763
3764     if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
3765         return ret;
3766     mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
3767     moof_size = ffio_close_null_buf(avio_buf);
3768
3769     if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX))
3770         mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
3771
3772     if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
3773         return ret;
3774
3775     return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
3776 }
3777
3778 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
3779 {
3780     int64_t pos = avio_tell(pb);
3781     int i;
3782
3783     avio_wb32(pb, 0); /* size placeholder */
3784     ffio_wfourcc(pb, "tfra");
3785     avio_w8(pb, 1); /* version */
3786     avio_wb24(pb, 0);
3787
3788     avio_wb32(pb, track->track_id);
3789     avio_wb32(pb, 0); /* length of traf/trun/sample num */
3790     avio_wb32(pb, track->nb_frag_info);
3791     for (i = 0; i < track->nb_frag_info; i++) {
3792         avio_wb64(pb, track->frag_info[i].time);
3793         avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
3794         avio_w8(pb, 1); /* traf number */
3795         avio_w8(pb, 1); /* trun number */
3796         avio_w8(pb, 1); /* sample number */
3797     }
3798
3799     return update_size(pb, pos);
3800 }
3801
3802 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
3803 {
3804     int64_t pos = avio_tell(pb);
3805     int i;
3806
3807     avio_wb32(pb, 0); /* size placeholder */
3808     ffio_wfourcc(pb, "mfra");
3809     /* An empty mfra atom is enough to indicate to the publishing point that
3810      * the stream has ended. */
3811     if (mov->flags & FF_MOV_FLAG_ISML)
3812         return update_size(pb, pos);
3813
3814     for (i = 0; i < mov->nb_streams; i++) {
3815         MOVTrack *track = &mov->tracks[i];
3816         if (track->nb_frag_info)
3817             mov_write_tfra_tag(pb, track);
3818     }
3819
3820     avio_wb32(pb, 16);
3821     ffio_wfourcc(pb, "mfro");
3822     avio_wb32(pb, 0); /* version + flags */
3823     avio_wb32(pb, avio_tell(pb) + 4 - pos);
3824
3825     return update_size(pb, pos);
3826 }
3827
3828 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
3829 {
3830     avio_wb32(pb, 8);    // placeholder for extended size field (64 bit)
3831     ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
3832
3833     mov->mdat_pos = avio_tell(pb);
3834     avio_wb32(pb, 0); /* size placeholder*/
3835     ffio_wfourcc(pb, "mdat");
3836     return 0;
3837 }
3838
3839 /* TODO: This needs to be more general */
3840 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
3841 {
3842     MOVMuxContext *mov = s->priv_data;
3843     int64_t pos = avio_tell(pb);
3844     int has_h264 = 0, has_video = 0;
3845     int minor = 0x200;
3846     int i;
3847
3848     for (i = 0; i < s->nb_streams; i++) {
3849         AVStream *st = s->streams[i];
3850         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3851             has_video = 1;
3852         if (st->codec->codec_id == AV_CODEC_ID_H264)
3853             has_h264 = 1;
3854     }
3855
3856     avio_wb32(pb, 0); /* size */
3857     ffio_wfourcc(pb, "ftyp");
3858
3859     if (mov->major_brand && strlen(mov->major_brand) >= 4)
3860         ffio_wfourcc(pb, mov->major_brand);
3861     else if (mov->mode == MODE_3GP) {
3862         ffio_wfourcc(pb, has_h264 ? "3gp6"  : "3gp4");
3863         minor =     has_h264 ?   0x100 :   0x200;
3864     } else if (mov->mode & MODE_3G2) {
3865         ffio_wfourcc(pb, has_h264 ? "3g2b"  : "3g2a");
3866         minor =     has_h264 ? 0x20000 : 0x10000;
3867     } else if (mov->mode == MODE_PSP)
3868         ffio_wfourcc(pb, "MSNV");
3869     else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
3870         ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
3871     else if (mov->mode == MODE_MP4)
3872         ffio_wfourcc(pb, "isom");
3873     else if (mov->mode == MODE_IPOD)
3874         ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
3875     else if (mov->mode == MODE_ISM)
3876         ffio_wfourcc(pb, "isml");
3877     else if (mov->mode == MODE_F4V)
3878         ffio_wfourcc(pb, "f4v ");
3879     else
3880         ffio_wfourcc(pb, "qt  ");
3881
3882     avio_wb32(pb, minor);
3883
3884     if (mov->mode == MODE_MOV)
3885         ffio_wfourcc(pb, "qt  ");
3886     else if (mov->mode == MODE_ISM) {
3887         ffio_wfourcc(pb, "piff");
3888     } else if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
3889         ffio_wfourcc(pb, "isom");
3890         ffio_wfourcc(pb, "iso2");
3891         if (has_h264)
3892             ffio_wfourcc(pb, "avc1");
3893     }
3894
3895     // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
3896     // brand. This is compatible with users that don't understand tfdt.
3897     if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->mode != MODE_ISM)
3898         ffio_wfourcc(pb, "iso6");
3899
3900     if (mov->mode == MODE_3GP)
3901         ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
3902     else if (mov->mode & MODE_3G2)
3903         ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
3904     else if (mov->mode == MODE_PSP)
3905         ffio_wfourcc(pb, "MSNV");
3906     else if (mov->mode == MODE_MP4)
3907         ffio_wfourcc(pb, "mp41");
3908
3909     if (mov->flags & FF_MOV_FLAG_DASH && mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
3910         ffio_wfourcc(pb, "dash");
3911
3912     return update_size(pb, pos);
3913 }
3914
3915 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
3916 {
3917     AVStream       *video_st    = s->streams[0];
3918     AVCodecContext *video_codec = s->streams[0]->codec;
3919     AVCodecContext *audio_codec = s->streams[1]->codec;
3920     int audio_rate = audio_codec->sample_rate;
3921     // TODO: should be avg_frame_rate
3922     int frame_rate = ((video_st->time_base.den) * (0x10000)) / (video_st->time_base.num);
3923     int audio_kbitrate = audio_codec->bit_rate / 1000;
3924     int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
3925
3926     avio_wb32(pb, 0x94); /* size */
3927     ffio_wfourcc(pb, "uuid");
3928     ffio_wfourcc(pb, "PROF");
3929
3930     avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3931     avio_wb32(pb, 0xbb88695c);
3932     avio_wb32(pb, 0xfac9c740);
3933
3934     avio_wb32(pb, 0x0);  /* ? */
3935     avio_wb32(pb, 0x3);  /* 3 sections ? */
3936
3937     avio_wb32(pb, 0x14); /* size */
3938     ffio_wfourcc(pb, "FPRF");
3939     avio_wb32(pb, 0x0);  /* ? */
3940     avio_wb32(pb, 0x0);  /* ? */
3941     avio_wb32(pb, 0x0);  /* ? */
3942
3943     avio_wb32(pb, 0x2c);  /* size */
3944     ffio_wfourcc(pb, "APRF"); /* audio */
3945     avio_wb32(pb, 0x0);
3946     avio_wb32(pb, 0x2);   /* TrackID */
3947     ffio_wfourcc(pb, "mp4a");
3948     avio_wb32(pb, 0x20f);
3949     avio_wb32(pb, 0x0);
3950     avio_wb32(pb, audio_kbitrate);
3951     avio_wb32(pb, audio_kbitrate);
3952     avio_wb32(pb, audio_rate);
3953     avio_wb32(pb, audio_codec->channels);
3954
3955     avio_wb32(pb, 0x34);  /* size */
3956     ffio_wfourcc(pb, "VPRF");   /* video */
3957     avio_wb32(pb, 0x0);
3958     avio_wb32(pb, 0x1);    /* TrackID */
3959     if (video_codec->codec_id == AV_CODEC_ID_H264) {
3960         ffio_wfourcc(pb, "avc1");
3961         avio_wb16(pb, 0x014D);
3962         avio_wb16(pb, 0x0015);
3963     } else {
3964         ffio_wfourcc(pb, "mp4v");
3965         avio_wb16(pb, 0x0000);
3966         avio_wb16(pb, 0x0103);
3967     }
3968     avio_wb32(pb, 0x0);
3969     avio_wb32(pb, video_kbitrate);
3970     avio_wb32(pb, video_kbitrate);
3971     avio_wb32(pb, frame_rate);
3972     avio_wb32(pb, frame_rate);
3973     avio_wb16(pb, video_codec->width);
3974     avio_wb16(pb, video_codec->height);
3975     avio_wb32(pb, 0x010001); /* ? */
3976 }
3977
3978 static int mov_write_identification(AVIOContext *pb, AVFormatContext *s)
3979 {
3980     MOVMuxContext *mov = s->priv_data;
3981     int i;
3982
3983     mov_write_ftyp_tag(pb,s);
3984     if (mov->mode == MODE_PSP) {
3985         int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
3986         for (i = 0; i < s->nb_streams; i++) {
3987             AVStream *st = s->streams[i];
3988             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3989                 video_streams_nb++;
3990             else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
3991                 audio_streams_nb++;
3992             else
3993                 other_streams_nb++;
3994             }
3995
3996         if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
3997             av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3998             return AVERROR(EINVAL);
3999         }
4000         mov_write_uuidprof_tag(pb, s);
4001     }
4002     return 0;
4003 }
4004
4005 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
4006 {
4007     uint32_t c = -1;
4008     int i, closed_gop = 0;
4009
4010     for (i = 0; i < pkt->size - 4; i++) {
4011         c = (c << 8) + pkt->data[i];
4012         if (c == 0x1b8) { // gop
4013             closed_gop = pkt->data[i + 4] >> 6 & 0x01;
4014         } else if (c == 0x100) { // pic
4015             int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
4016             if (!temp_ref || closed_gop) // I picture is not reordered
4017                 *flags = MOV_SYNC_SAMPLE;
4018             else
4019                 *flags = MOV_PARTIAL_SYNC_SAMPLE;
4020             break;
4021         }
4022     }
4023     return 0;
4024 }
4025
4026 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
4027 {
4028     const uint8_t *start, *next, *end = pkt->data + pkt->size;
4029     int seq = 0, entry = 0;
4030     int key = pkt->flags & AV_PKT_FLAG_KEY;
4031     start = find_next_marker(pkt->data, end);
4032     for (next = start; next < end; start = next) {
4033         next = find_next_marker(start + 4, end);
4034         switch (AV_RB32(start)) {
4035         case VC1_CODE_SEQHDR:
4036             seq = 1;
4037             break;
4038         case VC1_CODE_ENTRYPOINT:
4039             entry = 1;
4040             break;
4041         case VC1_CODE_SLICE:
4042             trk->vc1_info.slices = 1;
4043             break;
4044         }
4045     }
4046     if (!trk->entry && trk->vc1_info.first_packet_seen)
4047         trk->vc1_info.first_frag_written = 1;
4048     if (!trk->entry && !trk->vc1_info.first_frag_written) {
4049         /* First packet in first fragment */
4050         trk->vc1_info.first_packet_seq   = seq;
4051         trk->vc1_info.first_packet_entry = entry;
4052         trk->vc1_info.first_packet_seen  = 1;
4053     } else if ((seq && !trk->vc1_info.packet_seq) ||
4054                (entry && !trk->vc1_info.packet_entry)) {
4055         int i;
4056         for (i = 0; i < trk->entry; i++)
4057             trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
4058         trk->has_keyframes = 0;
4059         if (seq)
4060             trk->vc1_info.packet_seq = 1;
4061         if (entry)
4062             trk->vc1_info.packet_entry = 1;
4063         if (!trk->vc1_info.first_frag_written) {
4064             /* First fragment */
4065             if ((!seq   || trk->vc1_info.first_packet_seq) &&
4066                 (!entry || trk->vc1_info.first_packet_entry)) {
4067                 /* First packet had the same headers as this one, readd the
4068                  * sync sample flag. */
4069                 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
4070                 trk->has_keyframes = 1;
4071             }
4072         }
4073     }
4074     if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
4075         key = seq && entry;
4076     else if (trk->vc1_info.packet_seq)
4077         key = seq;
4078     else if (trk->vc1_info.packet_entry)
4079         key = entry;
4080     if (key) {
4081         trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
4082         trk->has_keyframes++;
4083     }
4084 }
4085
4086 static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
4087 {
4088     MOVMuxContext *mov = s->priv_data;
4089     int ret, buf_size;
4090     uint8_t *buf;
4091     int i, offset;
4092
4093     if (!track->mdat_buf)
4094         return 0;
4095     if (!mov->mdat_buf) {
4096         if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
4097             return ret;
4098     }
4099     buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
4100     track->mdat_buf = NULL;
4101
4102     offset = avio_tell(mov->mdat_buf);
4103     avio_write(mov->mdat_buf, buf, buf_size);
4104     av_free(buf);
4105
4106     for (i = track->entries_flushed; i < track->entry; i++)
4107         track->cluster[i].pos += offset;
4108     track->entries_flushed = track->entry;
4109     return 0;
4110 }
4111
4112 static int mov_flush_fragment(AVFormatContext *s, int force)
4113 {
4114     MOVMuxContext *mov = s->priv_data;
4115     int i, first_track = -1;
4116     int64_t mdat_size = 0;
4117     int ret;
4118
4119     if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
4120         return 0;
4121
4122     for (i = 0; i < mov->nb_streams; i++) {
4123         MOVTrack *track = &mov->tracks[i];
4124         if (track->entry <= 1)
4125             continue;
4126         // Sample durations are calculated as the diff of dts values,
4127         // but for the last sample in a fragment, we don't know the dts
4128         // of the first sample in the next fragment, so we have to rely
4129         // on what was set as duration in the AVPacket. Not all callers
4130         // set this though, so we might want to replace it with an
4131         // estimate if it currently is zero.
4132         if (get_cluster_duration(track, track->entry - 1) != 0)
4133             continue;
4134         // Use the duration (i.e. dts diff) of the second last sample for
4135         // the last one. This is a wild guess (and fatal if it turns out
4136         // to be too long), but probably the best we can do - having a zero
4137         // duration is bad as well.
4138         track->track_duration += get_cluster_duration(track, track->entry - 2);
4139         track->end_pts        += get_cluster_duration(track, track->entry - 2);
4140         if (!mov->missing_duration_warned) {
4141             av_log(s, AV_LOG_WARNING,
4142                    "Estimating the duration of the last packet in a "
4143                    "fragment, consider setting the duration field in "
4144                    "AVPacket instead.\n");
4145             mov->missing_duration_warned = 1;
4146         }
4147     }
4148
4149     if (!mov->moov_written) {
4150         int64_t pos = avio_tell(s->pb);
4151         uint8_t *buf;
4152         int buf_size, moov_size;
4153
4154         for (i = 0; i < mov->nb_streams; i++)
4155             if (!mov->tracks[i].entry)
4156                 break;
4157         /* Don't write the initial moov unless all tracks have data */
4158         if (i < mov->nb_streams && !force)
4159             return 0;
4160
4161         moov_size = get_moov_size(s);
4162         for (i = 0; i < mov->nb_streams; i++)
4163             mov->tracks[i].data_offset = pos + moov_size + 8;
4164
4165         if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
4166             mov_write_identification(s->pb, s);
4167         if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
4168             return ret;
4169
4170         if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
4171             if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
4172                 mov->reserved_header_pos = avio_tell(s->pb);
4173             avio_flush(s->pb);
4174             mov->moov_written = 1;
4175             return 0;
4176         }
4177
4178         buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
4179         mov->mdat_buf = NULL;
4180         avio_wb32(s->pb, buf_size + 8);
4181         ffio_wfourcc(s->pb, "mdat");
4182         avio_write(s->pb, buf, buf_size);
4183         av_free(buf);
4184
4185         if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
4186             mov->reserved_header_pos = avio_tell(s->pb);
4187
4188         mov->moov_written = 1;
4189         mov->mdat_size = 0;
4190         for (i = 0; i < mov->nb_streams; i++) {
4191             if (mov->tracks[i].entry)
4192                 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
4193                                              mov->tracks[i].track_duration -
4194                                              mov->tracks[i].cluster[0].dts;
4195             mov->tracks[i].entry = 0;
4196         }
4197         avio_flush(s->pb);
4198         return 0;
4199     }
4200
4201     if (mov->frag_interleave) {
4202         for (i = 0; i < mov->nb_streams; i++) {
4203             MOVTrack *track = &mov->tracks[i];
4204             int ret;
4205             if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
4206                 return ret;
4207         }
4208
4209         if (!mov->mdat_buf)
4210             return 0;
4211         mdat_size = avio_tell(mov->mdat_buf);
4212     }
4213
4214     for (i = 0; i < mov->nb_streams; i++) {
4215         MOVTrack *track = &mov->tracks[i];
4216         if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF || mov->frag_interleave)
4217             track->data_offset = 0;
4218         else
4219             track->data_offset = mdat_size;
4220         if (!track->entry)
4221             continue;
4222         if (track->mdat_buf)
4223             mdat_size += avio_tell(track->mdat_buf);
4224         if (first_track < 0)
4225             first_track = i;
4226     }
4227
4228     if (!mdat_size)
4229         return 0;
4230
4231     for (i = 0; i < mov->nb_streams; i++) {
4232         MOVTrack *track = &mov->tracks[i];
4233         int buf_size, write_moof = 1, moof_tracks = -1;
4234         uint8_t *buf;
4235         int64_t duration = 0;
4236
4237         if (track->entry)
4238             duration = track->start_dts + track->track_duration -
4239                        track->cluster[0].dts;
4240         if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
4241             if (!track->mdat_buf)
4242                 continue;
4243             mdat_size = avio_tell(track->mdat_buf);
4244             moof_tracks = i;
4245         } else {
4246             write_moof = i == first_track;
4247         }
4248
4249         if (write_moof) {
4250             avio_flush(s->pb);
4251
4252             mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
4253             mov->fragments++;
4254
4255             avio_wb32(s->pb, mdat_size + 8);
4256             ffio_wfourcc(s->pb, "mdat");
4257         }
4258
4259         if (track->entry)
4260             track->frag_start += duration;
4261         track->entry = 0;
4262         track->entries_flushed = 0;
4263         if (!mov->frag_interleave) {
4264             if (!track->mdat_buf)
4265                 continue;
4266             buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
4267             track->mdat_buf = NULL;
4268         } else {
4269             if (!mov->mdat_buf)
4270                 continue;
4271             buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
4272             mov->mdat_buf = NULL;
4273         }
4274
4275         avio_write(s->pb, buf, buf_size);
4276         av_free(buf);
4277     }
4278
4279     mov->mdat_size = 0;
4280
4281     avio_flush(s->pb);
4282     return 0;
4283 }
4284
4285 static int mov_auto_flush_fragment(AVFormatContext *s, int force)
4286 {
4287     MOVMuxContext *mov = s->priv_data;
4288     int had_moov = mov->moov_written;
4289     int ret = mov_flush_fragment(s, force);
4290     if (ret < 0)
4291         return ret;
4292     // If using delay_moov, the first flush only wrote the moov,
4293     // not the actual moof+mdat pair, thus flush once again.
4294     if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
4295         ret = mov_flush_fragment(s, force);
4296     return ret;
4297 }
4298
4299 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
4300 {
4301     MOVMuxContext *mov = s->priv_data;
4302     AVIOContext *pb = s->pb;
4303     MOVTrack *trk = &mov->tracks[pkt->stream_index];
4304     AVCodecContext *enc = trk->enc;
4305     unsigned int samples_in_chunk = 0;
4306     int size = pkt->size, ret = 0;
4307     uint8_t *reformatted_data = NULL;
4308
4309     if (trk->entry) {
4310         int64_t duration = pkt->dts - trk->cluster[trk->entry - 1].dts;
4311         if (duration < 0 || duration > INT_MAX) {
4312             av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
4313                 duration, pkt->dts
4314             );
4315
4316             pkt->dts = trk->cluster[trk->entry - 1].dts + 1;
4317             pkt->pts = AV_NOPTS_VALUE;
4318         }
4319         if (pkt->duration < 0) {
4320             av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" is invalid\n", pkt->duration);
4321             return AVERROR(EINVAL);
4322         }
4323     }
4324     if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
4325         int ret;
4326         if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4327             if (mov->frag_interleave && mov->fragments > 0) {
4328                 if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
4329                     if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
4330                         return ret;
4331                 }
4332             }
4333
4334             if (!trk->mdat_buf) {
4335                 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
4336                     return ret;
4337             }
4338             pb = trk->mdat_buf;
4339         } else {
4340             if (!mov->mdat_buf) {
4341                 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
4342                     return ret;
4343             }
4344             pb = mov->mdat_buf;
4345         }
4346     }
4347
4348     if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
4349         /* We must find out how many AMR blocks there are in one packet */
4350         static const uint16_t packed_size[16] =
4351             {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
4352         int len = 0;
4353
4354         while (len < size && samples_in_chunk < 100) {
4355             len += packed_size[(pkt->data[len] >> 3) & 0x0F];
4356             samples_in_chunk++;
4357         }
4358         if (samples_in_chunk > 1) {
4359             av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
4360             return -1;
4361         }
4362     } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
4363                enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
4364         samples_in_chunk = enc->frame_size;
4365     } else if (trk->sample_size)
4366         samples_in_chunk = size / trk->sample_size;
4367     else
4368         samples_in_chunk = 1;
4369
4370     /* copy extradata if it exists */
4371     if (trk->vos_len == 0 && enc->extradata_size > 0 &&
4372         !TAG_IS_AVCI(trk->tag) &&
4373         (enc->codec_id != AV_CODEC_ID_DNXHD)) {
4374         trk->vos_len  = enc->extradata_size;
4375         trk->vos_data = av_malloc(trk->vos_len);
4376         if (!trk->vos_data) {
4377             ret = AVERROR(ENOMEM);
4378             goto err;
4379         }
4380         memcpy(trk->vos_data, enc->extradata, trk->vos_len);
4381     }
4382
4383     if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
4384         (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
4385         if (!s->streams[pkt->stream_index]->nb_frames) {
4386             av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
4387                    "use the audio bitstream filter 'aac_adtstoasc' to fix it "
4388                    "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
4389             return -1;
4390         }
4391         av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
4392     }
4393     if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1 && !TAG_IS_AVCI(trk->tag)) {
4394         /* from x264 or from bytestream h264 */
4395         /* nal reformating needed */
4396         if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
4397             ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
4398                                        &size);
4399             avio_write(pb, reformatted_data, size);
4400         } else {
4401             size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
4402         }
4403     } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
4404                (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
4405         /* extradata is Annex B, assume the bitstream is too and convert it */
4406         if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
4407             ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
4408             avio_write(pb, reformatted_data, size);
4409         } else {
4410             size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
4411         }
4412 #if CONFIG_AC3_PARSER
4413     } else if (enc->codec_id == AV_CODEC_ID_EAC3) {
4414         size = handle_eac3(mov, pkt, trk);
4415         if (size < 0)
4416             return size;
4417         else if (!size)
4418             goto end;
4419         avio_write(pb, pkt->data, size);
4420 #endif
4421     } else {
4422         avio_write(pb, pkt->data, size);
4423     }
4424
4425     if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
4426          enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
4427         /* copy frame to create needed atoms */
4428         trk->vos_len  = size;
4429         trk->vos_data = av_malloc(size);
4430         if (!trk->vos_data) {
4431             ret = AVERROR(ENOMEM);
4432             goto err;
4433         }
4434         memcpy(trk->vos_data, pkt->data, size);
4435     }
4436
4437     if (trk->entry >= trk->cluster_capacity) {
4438         unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
4439         if (av_reallocp_array(&trk->cluster, new_capacity,
4440                               sizeof(*trk->cluster))) {
4441             ret = AVERROR(ENOMEM);
4442             goto err;
4443         }
4444         trk->cluster_capacity = new_capacity;
4445     }
4446
4447     trk->cluster[trk->entry].pos              = avio_tell(pb) - size;
4448     trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
4449     trk->cluster[trk->entry].chunkNum         = 0;
4450     trk->cluster[trk->entry].size             = size;
4451     trk->cluster[trk->entry].entries          = samples_in_chunk;
4452     trk->cluster[trk->entry].dts              = pkt->dts;
4453     if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
4454         if (!trk->frag_discont) {
4455             /* First packet of a new fragment. We already wrote the duration
4456              * of the last packet of the previous fragment based on track_duration,
4457              * which might not exactly match our dts. Therefore adjust the dts
4458              * of this packet to be what the previous packets duration implies. */
4459             trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
4460             /* We also may have written the pts and the corresponding duration
4461              * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
4462              * the next fragment. This means the cts of the first sample must
4463              * be the same in all fragments. */
4464             if ((mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)) ||
4465                 mov->mode == MODE_ISM)
4466                 pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
4467         } else {
4468             /* New fragment, but discontinuous from previous fragments.
4469              * Pretend the duration sum of the earlier fragments is
4470              * pkt->dts - trk->start_dts. */
4471             trk->frag_start = pkt->dts - trk->start_dts;
4472             trk->end_pts = AV_NOPTS_VALUE;
4473             trk->frag_discont = 0;
4474         }
4475     }
4476
4477     if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
4478         s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
4479         /* Not using edit lists and shifting the first track to start from zero.
4480          * If the other streams start from a later timestamp, we won't be able
4481          * to signal the difference in starting time without an edit list.
4482          * Thus move the timestamp for this first sample to 0, increasing
4483          * its duration instead. */
4484         trk->cluster[trk->entry].dts = trk->start_dts = 0;
4485     }
4486     if (trk->start_dts == AV_NOPTS_VALUE) {
4487         trk->start_dts = pkt->dts;
4488         if (trk->frag_discont) {
4489             if (mov->use_editlist) {
4490                 /* Pretend the whole stream started at pts=0, with earlier fragments
4491                  * already written. If the stream started at pts=0, the duration sum
4492                  * of earlier fragments would have been pkt->pts. */
4493                 trk->frag_start = pkt->pts;
4494                 trk->start_dts  = pkt->dts - pkt->pts;
4495             } else {
4496                 /* Pretend the whole stream started at dts=0, with earlier fragments
4497                  * already written, with a duration summing up to pkt->dts. */
4498                 trk->frag_start = pkt->dts;
4499                 trk->start_dts  = 0;
4500             }
4501             trk->frag_discont = 0;
4502         } else if (pkt->dts && mov->moov_written)
4503             av_log(s, AV_LOG_WARNING,
4504                    "Track %d starts with a nonzero dts %"PRId64", while the moov "
4505                    "already has been written. Set the delay_moov flag to handle "
4506                    "this case.\n",
4507                    pkt->stream_index, pkt->dts);
4508     }
4509     trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
4510     trk->last_sample_is_subtitle_end = 0;
4511
4512     if (pkt->pts == AV_NOPTS_VALUE) {
4513         av_log(s, AV_LOG_WARNING, "pts has no value\n");
4514         pkt->pts = pkt->dts;
4515     }
4516     if (pkt->dts != pkt->pts)
4517         trk->flags |= MOV_TRACK_CTTS;
4518     trk->cluster[trk->entry].cts   = pkt->pts - pkt->dts;
4519     trk->cluster[trk->entry].flags = 0;
4520     if (trk->start_cts == AV_NOPTS_VALUE)
4521         trk->start_cts = pkt->pts - pkt->dts;
4522     if (trk->end_pts == AV_NOPTS_VALUE)
4523         trk->end_pts = trk->cluster[trk->entry].dts +
4524                        trk->cluster[trk->entry].cts + pkt->duration;
4525     else
4526         trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
4527                                            trk->cluster[trk->entry].cts +
4528                                            pkt->duration);
4529
4530     if (enc->codec_id == AV_CODEC_ID_VC1) {
4531         mov_parse_vc1_frame(pkt, trk);
4532     } else if (pkt->flags & AV_PKT_FLAG_KEY) {
4533         if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
4534             trk->entry > 0) { // force sync sample for the first key frame
4535             mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
4536             if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
4537                 trk->flags |= MOV_TRACK_STPS;
4538         } else {
4539             trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
4540         }
4541         if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
4542             trk->has_keyframes++;
4543     }
4544     trk->entry++;
4545     trk->sample_count += samples_in_chunk;
4546     mov->mdat_size    += size;
4547
4548     if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
4549         ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
4550                                  reformatted_data, size);
4551
4552 end:
4553 err:
4554
4555     av_free(reformatted_data);
4556     return ret;
4557 }
4558
4559 static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
4560 {
4561         MOVMuxContext *mov = s->priv_data;
4562         MOVTrack *trk = &mov->tracks[pkt->stream_index];
4563         AVCodecContext *enc = trk->enc;
4564         int64_t frag_duration = 0;
4565         int size = pkt->size;
4566
4567         if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
4568             int i;
4569             for (i = 0; i < s->nb_streams; i++)
4570                 mov->tracks[i].frag_discont = 1;
4571             mov->flags &= ~FF_MOV_FLAG_FRAG_DISCONT;
4572         }
4573
4574         if (!pkt->size) {
4575             if (trk->start_dts == AV_NOPTS_VALUE && trk->frag_discont) {
4576                 trk->start_dts = pkt->dts;
4577                 if (pkt->pts != AV_NOPTS_VALUE)
4578                     trk->start_cts = pkt->pts - pkt->dts;
4579                 else
4580                     trk->start_cts = 0;
4581             }
4582
4583             return 0;             /* Discard 0 sized packets */
4584         }
4585
4586         if (trk->entry && pkt->stream_index < s->nb_streams)
4587             frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
4588                                          s->streams[pkt->stream_index]->time_base,
4589                                          AV_TIME_BASE_Q);
4590         if ((mov->max_fragment_duration &&
4591              frag_duration >= mov->max_fragment_duration) ||
4592              (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
4593              (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
4594               enc->codec_type == AVMEDIA_TYPE_VIDEO &&
4595               trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
4596             if (frag_duration >= mov->min_fragment_duration) {
4597                 // Set the duration of this track to line up with the next
4598                 // sample in this track. This avoids relying on AVPacket
4599                 // duration, but only helps for this particular track, not
4600                 // for the other ones that are flushed at the same time.
4601                 trk->track_duration = pkt->dts - trk->start_dts;
4602                 trk->end_pts = pkt->pts;
4603                 mov_auto_flush_fragment(s, 0);
4604             }
4605         }
4606
4607         return ff_mov_write_packet(s, pkt);
4608 }
4609
4610 static int mov_write_subtitle_end_packet(AVFormatContext *s,
4611                                          int stream_index,
4612                                          int64_t dts) {
4613     AVPacket end;
4614     uint8_t data[2] = {0};
4615     int ret;
4616
4617     av_init_packet(&end);
4618     end.size = sizeof(data);
4619     end.data = data;
4620     end.pts = dts;
4621     end.dts = dts;
4622     end.duration = 0;
4623     end.stream_index = stream_index;
4624
4625     ret = mov_write_single_packet(s, &end);
4626     av_packet_unref(&end);
4627
4628     return ret;
4629 }
4630
4631 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
4632 {
4633     if (!pkt) {
4634         mov_flush_fragment(s, 1);
4635         return 1;
4636     } else {
4637         int i;
4638         MOVMuxContext *mov = s->priv_data;
4639
4640         if (!pkt->size)
4641             return mov_write_single_packet(s, pkt); /* Passthrough. */
4642
4643         /*
4644          * Subtitles require special handling.
4645          *
4646          * 1) For full complaince, every track must have a sample at
4647          * dts == 0, which is rarely true for subtitles. So, as soon
4648          * as we see any packet with dts > 0, write an empty subtitle
4649          * at dts == 0 for any subtitle track with no samples in it.
4650          *
4651          * 2) For each subtitle track, check if the current packet's
4652          * dts is past the duration of the last subtitle sample. If
4653          * so, we now need to write an end sample for that subtitle.
4654          *
4655          * This must be done conditionally to allow for subtitles that
4656          * immediately replace each other, in which case an end sample
4657          * is not needed, and is, in fact, actively harmful.
4658          *
4659          * 3) See mov_write_trailer for how the final end sample is
4660          * handled.
4661          */
4662         for (i = 0; i < mov->nb_streams; i++) {
4663             MOVTrack *trk = &mov->tracks[i];
4664             int ret;
4665
4666             if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
4667                 trk->track_duration < pkt->dts &&
4668                 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
4669                 ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
4670                 if (ret < 0) return ret;
4671                 trk->last_sample_is_subtitle_end = 1;
4672             }
4673         }
4674
4675         return mov_write_single_packet(s, pkt);
4676     }
4677 }
4678
4679 // QuickTime chapters involve an additional text track with the chapter names
4680 // as samples, and a tref pointing from the other tracks to the chapter one.
4681 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
4682 {
4683     AVIOContext *pb;
4684
4685     MOVMuxContext *mov = s->priv_data;
4686     MOVTrack *track = &mov->tracks[tracknum];
4687     AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
4688     int i, len;
4689
4690     track->mode = mov->mode;
4691     track->tag = MKTAG('t','e','x','t');
4692     track->timescale = MOV_TIMESCALE;
4693     track->enc = avcodec_alloc_context3(NULL);
4694     if (!track->enc)
4695         return AVERROR(ENOMEM);
4696     track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
4697 #if 0
4698     // These properties are required to make QT recognize the chapter track
4699     uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
4700     if (ff_alloc_extradata(track->enc, sizeof(chapter_properties)))
4701         return AVERROR(ENOMEM);
4702     memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
4703 #else
4704     if (avio_open_dyn_buf(&pb) >= 0) {
4705         int size;
4706         uint8_t *buf;
4707
4708         /* Stub header (usually for Quicktime chapter track) */
4709         // TextSampleEntry
4710         avio_wb32(pb, 0x01); // displayFlags
4711         avio_w8(pb, 0x00);   // horizontal justification
4712         avio_w8(pb, 0x00);   // vertical justification
4713         avio_w8(pb, 0x00);   // bgColourRed
4714         avio_w8(pb, 0x00);   // bgColourGreen
4715         avio_w8(pb, 0x00);   // bgColourBlue
4716         avio_w8(pb, 0x00);   // bgColourAlpha
4717         // BoxRecord
4718         avio_wb16(pb, 0x00); // defTextBoxTop
4719         avio_wb16(pb, 0x00); // defTextBoxLeft
4720         avio_wb16(pb, 0x00); // defTextBoxBottom
4721         avio_wb16(pb, 0x00); // defTextBoxRight
4722         // StyleRecord
4723         avio_wb16(pb, 0x00); // startChar
4724         avio_wb16(pb, 0x00); // endChar
4725         avio_wb16(pb, 0x01); // fontID
4726         avio_w8(pb, 0x00);   // fontStyleFlags
4727         avio_w8(pb, 0x00);   // fontSize
4728         avio_w8(pb, 0x00);   // fgColourRed
4729         avio_w8(pb, 0x00);   // fgColourGreen
4730         avio_w8(pb, 0x00);   // fgColourBlue
4731         avio_w8(pb, 0x00);   // fgColourAlpha
4732         // FontTableBox
4733         avio_wb32(pb, 0x0D); // box size
4734         ffio_wfourcc(pb, "ftab"); // box atom name
4735         avio_wb16(pb, 0x01); // entry count
4736         // FontRecord
4737         avio_wb16(pb, 0x01); // font ID
4738         avio_w8(pb, 0x00);   // font name length
4739
4740         if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
4741             track->enc->extradata = buf;
4742             track->enc->extradata_size = size;
4743         } else {
4744             av_freep(&buf);
4745         }
4746     }
4747 #endif
4748
4749     for (i = 0; i < s->nb_chapters; i++) {
4750         AVChapter *c = s->chapters[i];
4751         AVDictionaryEntry *t;
4752
4753         int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
4754         pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
4755         pkt.duration = end - pkt.dts;
4756
4757         if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
4758             const char encd[12] = {
4759                 0x00, 0x00, 0x00, 0x0C,
4760                 'e',  'n',  'c',  'd',
4761                 0x00, 0x00, 0x01, 0x00 };
4762             len      = strlen(t->value);
4763             pkt.size = len + 2 + 12;
4764             pkt.data = av_malloc(pkt.size);
4765             if (!pkt.data)
4766                 return AVERROR(ENOMEM);
4767             AV_WB16(pkt.data, len);
4768             memcpy(pkt.data + 2, t->value, len);
4769             memcpy(pkt.data + len + 2, encd, sizeof(encd));
4770             ff_mov_write_packet(s, &pkt);
4771             av_freep(&pkt.data);
4772         }
4773     }
4774
4775     return 0;
4776 }
4777
4778 static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
4779 {
4780     int ret;
4781     MOVMuxContext *mov  = s->priv_data;
4782     MOVTrack *track     = &mov->tracks[index];
4783     AVStream *src_st    = s->streams[src_index];
4784     AVTimecode tc;
4785     AVPacket pkt    = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
4786     AVRational rate = find_fps(s, src_st);
4787
4788     /* compute the frame number */
4789     ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
4790     if (ret < 0)
4791         return ret;
4792
4793     /* tmcd track based on video stream */
4794     track->mode      = mov->mode;
4795     track->tag       = MKTAG('t','m','c','d');
4796     track->src_track = src_index;
4797     track->timescale = mov->tracks[src_index].timescale;
4798     if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
4799         track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
4800
4801     /* set st to src_st for metadata access*/
4802     track->st = src_st;
4803
4804     /* encode context: tmcd data stream */
4805     track->enc = avcodec_alloc_context3(NULL);
4806     if (!track->enc)
4807         return AVERROR(ENOMEM);
4808     track->enc->codec_type = AVMEDIA_TYPE_DATA;
4809     track->enc->codec_tag  = track->tag;
4810     track->enc->time_base  = av_inv_q(rate);
4811
4812     /* the tmcd track just contains one packet with the frame number */
4813     pkt.data = av_malloc(pkt.size);
4814     if (!pkt.data)
4815         return AVERROR(ENOMEM);
4816     AV_WB32(pkt.data, tc.start);
4817     ret = ff_mov_write_packet(s, &pkt);
4818     av_free(pkt.data);
4819     return ret;
4820 }
4821
4822 /*
4823  * st->disposition controls the "enabled" flag in the tkhd tag.
4824  * QuickTime will not play a track if it is not enabled.  So make sure
4825  * that one track of each type (audio, video, subtitle) is enabled.
4826  *
4827  * Subtitles are special.  For audio and video, setting "enabled" also
4828  * makes the track "default" (i.e. it is rendered when played). For
4829  * subtitles, an "enabled" subtitle is not rendered by default, but
4830  * if no subtitle is enabled, the subtitle menu in QuickTime will be
4831  * empty!
4832  */
4833 static void enable_tracks(AVFormatContext *s)
4834 {
4835     MOVMuxContext *mov = s->priv_data;
4836     int i;
4837     int enabled[AVMEDIA_TYPE_NB];
4838     int first[AVMEDIA_TYPE_NB];
4839
4840     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
4841         enabled[i] = 0;
4842         first[i] = -1;
4843     }
4844
4845     for (i = 0; i < s->nb_streams; i++) {
4846         AVStream *st = s->streams[i];
4847
4848         if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
4849             st->codec->codec_type >= AVMEDIA_TYPE_NB)
4850             continue;
4851
4852         if (first[st->codec->codec_type] < 0)
4853             first[st->codec->codec_type] = i;
4854         if (st->disposition & AV_DISPOSITION_DEFAULT) {
4855             mov->tracks[i].flags |= MOV_TRACK_ENABLED;
4856             enabled[st->codec->codec_type]++;
4857         }
4858     }
4859
4860     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
4861         switch (i) {
4862         case AVMEDIA_TYPE_VIDEO:
4863         case AVMEDIA_TYPE_AUDIO:
4864         case AVMEDIA_TYPE_SUBTITLE:
4865             if (enabled[i] > 1)
4866                 mov->per_stream_grouping = 1;
4867             if (!enabled[i] && first[i] >= 0)
4868                 mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
4869             break;
4870         }
4871     }
4872 }
4873
4874 static void mov_free(AVFormatContext *s)
4875 {
4876     MOVMuxContext *mov = s->priv_data;
4877     int i;
4878
4879     if (mov->chapter_track) {
4880         if (mov->tracks[mov->chapter_track].enc)
4881             av_freep(&mov->tracks[mov->chapter_track].enc->extradata);
4882         av_freep(&mov->tracks[mov->chapter_track].enc);
4883     }
4884
4885     for (i = 0; i < mov->nb_streams; i++) {
4886         if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
4887             ff_mov_close_hinting(&mov->tracks[i]);
4888         else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
4889             av_freep(&mov->tracks[i].enc);
4890         av_freep(&mov->tracks[i].cluster);
4891         av_freep(&mov->tracks[i].frag_info);
4892
4893         if (mov->tracks[i].vos_len)
4894             av_freep(&mov->tracks[i].vos_data);
4895     }
4896
4897     av_freep(&mov->tracks);
4898 }
4899
4900 static uint32_t rgb_to_yuv(uint32_t rgb)
4901 {
4902     uint8_t r, g, b;
4903     int y, cb, cr;
4904
4905     r = (rgb >> 16) & 0xFF;
4906     g = (rgb >>  8) & 0xFF;
4907     b = (rgb      ) & 0xFF;
4908
4909     y  = av_clip_uint8(( 16000 +  257 * r + 504 * g +  98 * b)/1000);
4910     cb = av_clip_uint8((128000 -  148 * r - 291 * g + 439 * b)/1000);
4911     cr = av_clip_uint8((128000 +  439 * r - 368 * g -  71 * b)/1000);
4912
4913     return (y << 16) | (cr << 8) | cb;
4914 }
4915
4916 static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track,
4917                                                     AVStream *st)
4918 {
4919     int i, width = 720, height = 480;
4920     int have_palette = 0, have_size = 0;
4921     uint32_t palette[16];
4922     char *cur = st->codec->extradata;
4923
4924     while (cur && *cur) {
4925         if (strncmp("palette:", cur, 8) == 0) {
4926             int i, count;
4927             count = sscanf(cur + 8,
4928                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
4929                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
4930                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
4931                 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
4932                 &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
4933                 &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
4934                 &palette[ 8], &palette[ 9], &palette[10], &palette[11],
4935                 &palette[12], &palette[13], &palette[14], &palette[15]);
4936
4937             for (i = 0; i < count; i++) {
4938                 palette[i] = rgb_to_yuv(palette[i]);
4939             }
4940             have_palette = 1;
4941         } else if (!strncmp("size:", cur, 5)) {
4942             sscanf(cur + 5, "%dx%d", &width, &height);
4943             have_size = 1;
4944         }
4945         if (have_palette && have_size)
4946             break;
4947         cur += strcspn(cur, "\n\r");
4948         cur += strspn(cur, "\n\r");
4949     }
4950     if (have_palette) {
4951         track->vos_data = av_malloc(16*4);
4952         if (!track->vos_data)
4953             return AVERROR(ENOMEM);
4954         for (i = 0; i < 16; i++) {
4955             AV_WB32(track->vos_data + i * 4, palette[i]);
4956         }
4957         track->vos_len = 16 * 4;
4958     }
4959     st->codec->width = width;
4960     st->codec->height = track->height = height;
4961
4962     return 0;
4963 }
4964
4965 static int mov_write_header(AVFormatContext *s)
4966 {
4967     AVIOContext *pb = s->pb;
4968     MOVMuxContext *mov = s->priv_data;
4969     AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
4970     int i, ret, hint_track = 0, tmcd_track = 0;
4971
4972     mov->fc = s;
4973
4974     /* Default mode == MP4 */
4975     mov->mode = MODE_MP4;
4976
4977     if (s->oformat) {
4978         if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
4979         else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
4980         else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
4981         else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
4982         else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
4983         else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
4984         else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
4985     }
4986
4987     if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
4988         mov->flags |= FF_MOV_FLAG_EMPTY_MOOV;
4989
4990     /* Set the FRAGMENT flag if any of the fragmentation methods are
4991      * enabled. */
4992     if (mov->max_fragment_duration || mov->max_fragment_size ||
4993         mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
4994                       FF_MOV_FLAG_FRAG_KEYFRAME |
4995                       FF_MOV_FLAG_FRAG_CUSTOM))
4996         mov->flags |= FF_MOV_FLAG_FRAGMENT;
4997
4998     /* Set other implicit flags immediately */
4999     if (mov->mode == MODE_ISM)
5000         mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
5001                       FF_MOV_FLAG_FRAGMENT;
5002     if (mov->flags & FF_MOV_FLAG_DASH)
5003         mov->flags |= FF_MOV_FLAG_FRAGMENT | FF_MOV_FLAG_EMPTY_MOOV |
5004                       FF_MOV_FLAG_DEFAULT_BASE_MOOF;
5005
5006     if (mov->flags & FF_MOV_FLAG_FASTSTART) {
5007         mov->reserved_moov_size = -1;
5008     }
5009
5010     if (mov->use_editlist < 0) {
5011         mov->use_editlist = 1;
5012         if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
5013             !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
5014             // If we can avoid needing an edit list by shifting the
5015             // tracks, prefer that over (trying to) write edit lists
5016             // in fragmented output.
5017             if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO ||
5018                 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)
5019                 mov->use_editlist = 0;
5020         }
5021     }
5022     if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
5023         !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
5024         av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
5025
5026     if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO)
5027         s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO;
5028
5029     /* Clear the omit_tfhd_offset flag if default_base_moof is set;
5030      * if the latter is set that's enough and omit_tfhd_offset doesn't
5031      * add anything extra on top of that. */
5032     if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
5033         mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
5034         mov->flags &= ~FF_MOV_FLAG_OMIT_TFHD_OFFSET;
5035
5036     if (mov->frag_interleave &&
5037         mov->flags & (FF_MOV_FLAG_OMIT_TFHD_OFFSET | FF_MOV_FLAG_SEPARATE_MOOF)) {
5038         av_log(s, AV_LOG_ERROR,
5039                "Sample interleaving in fragments is mutually exclusive with "
5040                "omit_tfhd_offset and separate_moof\n");
5041         return AVERROR(EINVAL);
5042     }
5043
5044     /* Non-seekable output is ok if using fragmentation. If ism_lookahead
5045      * is enabled, we don't support non-seekable output at all. */
5046     if (!s->pb->seekable &&
5047         (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
5048         av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
5049         return AVERROR(EINVAL);
5050     }
5051
5052     if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
5053         if ((ret = mov_write_identification(pb, s)) < 0)
5054             return ret;
5055     }
5056
5057     mov->nb_streams = s->nb_streams;
5058     if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
5059         mov->chapter_track = mov->nb_streams++;
5060
5061     if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
5062         /* Add hint tracks for each audio and video stream */
5063         hint_track = mov->nb_streams;
5064         for (i = 0; i < s->nb_streams; i++) {
5065             AVStream *st = s->streams[i];
5066             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
5067                 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
5068                 mov->nb_streams++;
5069             }
5070         }
5071     }
5072
5073     if (mov->mode == MODE_MOV) {
5074         tmcd_track = mov->nb_streams;
5075
5076         /* +1 tmcd track for each video stream with a timecode */
5077         for (i = 0; i < s->nb_streams; i++) {
5078             AVStream *st = s->streams[i];
5079             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
5080                 (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
5081                 mov->nb_meta_tmcd++;
5082         }
5083
5084         /* check if there is already a tmcd track to remux */
5085         if (mov->nb_meta_tmcd) {
5086             for (i = 0; i < s->nb_streams; i++) {
5087                 AVStream *st = s->streams[i];
5088                 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
5089                     av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
5090                            "so timecode metadata are now ignored\n");
5091                     mov->nb_meta_tmcd = 0;
5092                 }
5093             }
5094         }
5095
5096         mov->nb_streams += mov->nb_meta_tmcd;
5097     }
5098
5099     // Reserve an extra stream for chapters for the case where chapters
5100     // are written in the trailer
5101     mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks));
5102     if (!mov->tracks)
5103         return AVERROR(ENOMEM);
5104
5105     for (i = 0; i < s->nb_streams; i++) {
5106         AVStream *st= s->streams[i];
5107         MOVTrack *track= &mov->tracks[i];
5108         AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
5109
5110         track->st  = st;
5111         track->enc = st->codec;
5112         track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
5113         if (track->language < 0)
5114             track->language = 0;
5115         track->mode = mov->mode;
5116         track->tag  = mov_find_codec_tag(s, track);
5117         if (!track->tag) {
5118             av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
5119                    "codec not currently supported in container\n",
5120                    avcodec_get_name(st->codec->codec_id), i);
5121             ret = AVERROR(EINVAL);
5122             goto error;
5123         }
5124         /* If hinting of this track is enabled by a later hint track,
5125          * this is updated. */
5126         track->hint_track = -1;
5127         track->start_dts  = AV_NOPTS_VALUE;
5128         track->start_cts  = AV_NOPTS_VALUE;
5129         track->end_pts    = AV_NOPTS_VALUE;
5130         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
5131             if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
5132                 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
5133                 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
5134                 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
5135                     av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
5136                     ret = AVERROR(EINVAL);
5137                     goto error;
5138                 }
5139                 track->height = track->tag >> 24 == 'n' ? 486 : 576;
5140             }
5141             if (mov->video_track_timescale) {
5142                 track->timescale = mov->video_track_timescale;
5143             } else {
5144                 track->timescale = st->time_base.den;
5145                 while(track->timescale < 10000)
5146                     track->timescale *= 2;
5147             }
5148             if (st->codec->width > 65535 || st->codec->height > 65535) {
5149                 av_log(s, AV_LOG_ERROR, "Resolution %dx%d too large for mov/mp4\n", st->codec->width, st->codec->height);
5150                 ret = AVERROR(EINVAL);
5151                 goto error;
5152             }
5153             if (track->mode == MODE_MOV && track->timescale > 100000)
5154                 av_log(s, AV_LOG_WARNING,
5155                        "WARNING codec timebase is very high. If duration is too long,\n"
5156                        "file may not be playable by quicktime. Specify a shorter timebase\n"
5157                        "or choose different container.\n");
5158         } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
5159             track->timescale = st->codec->sample_rate;
5160             if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
5161                 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
5162                 track->audio_vbr = 1;
5163             }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
5164                      st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
5165                      st->codec->codec_id == AV_CODEC_ID_ILBC){
5166                 if (!st->codec->block_align) {
5167                     av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
5168                     ret = AVERROR(EINVAL);
5169                     goto error;
5170                 }
5171                 track->sample_size = st->codec->block_align;
5172             }else if (st->codec->frame_size > 1){ /* assume compressed audio */
5173                 track->audio_vbr = 1;
5174             }else{
5175                 track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
5176             }
5177             if (st->codec->codec_id == AV_CODEC_ID_ILBC ||
5178                 st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_QT) {
5179                 track->audio_vbr = 1;
5180             }
5181             if (track->mode != MODE_MOV &&
5182                 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
5183                 if (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
5184                     av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not standard, to mux anyway set strict to -1\n",
5185                         i, track->enc->sample_rate);
5186                     ret = AVERROR(EINVAL);
5187                     goto error;
5188                 } else {
5189                     av_log(s, AV_LOG_WARNING, "track %d: muxing mp3 at %dhz is not standard in MP4\n",
5190                            i, track->enc->sample_rate);
5191                 }
5192             }
5193         } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
5194             track->timescale = st->time_base.den;
5195         } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
5196             track->timescale = st->time_base.den;
5197         } else {
5198             track->timescale = MOV_TIMESCALE;
5199         }
5200         if (!track->height)
5201             track->height = st->codec->height;
5202         /* The ism specific timescale isn't mandatory, but is assumed by
5203          * some tools, such as mp4split. */
5204         if (mov->mode == MODE_ISM)
5205             track->timescale = 10000000;
5206
5207         avpriv_set_pts_info(st, 64, 1, track->timescale);
5208
5209         /* copy extradata if it exists */
5210         if (st->codec->extradata_size) {
5211             if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
5212                 mov_create_dvd_sub_decoder_specific_info(track, st);
5213             else if (!TAG_IS_AVCI(track->tag) && st->codec->codec_id != AV_CODEC_ID_DNXHD) {
5214                 track->vos_len  = st->codec->extradata_size;
5215                 track->vos_data = av_malloc(track->vos_len);
5216                 if (!track->vos_data) {
5217                     ret = AVERROR(ENOMEM);
5218                     goto error;
5219                 }
5220                 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
5221             }
5222         }
5223     }
5224
5225     for (i = 0; i < s->nb_streams; i++) {
5226         int j;
5227         AVStream *st= s->streams[i];
5228         MOVTrack *track= &mov->tracks[i];
5229
5230         if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
5231             track->enc->channel_layout != AV_CH_LAYOUT_MONO)
5232             continue;
5233
5234         for (j = 0; j < s->nb_streams; j++) {
5235             AVStream *stj= s->streams[j];
5236             MOVTrack *trackj= &mov->tracks[j];
5237             if (j == i)
5238                 continue;
5239
5240             if (stj->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
5241                 trackj->enc->channel_layout != AV_CH_LAYOUT_MONO ||
5242                 trackj->language != track->language ||
5243                 trackj->tag != track->tag
5244             )
5245                 continue;
5246             track->multichannel_as_mono++;
5247         }
5248     }
5249
5250     enable_tracks(s);
5251
5252
5253     if (mov->reserved_moov_size){
5254         mov->reserved_header_pos = avio_tell(pb);
5255         if (mov->reserved_moov_size > 0)
5256             avio_skip(pb, mov->reserved_moov_size);
5257     }
5258
5259     if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
5260         /* If no fragmentation options have been set, set a default. */
5261         if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
5262                             FF_MOV_FLAG_FRAG_CUSTOM)) &&
5263             !mov->max_fragment_duration && !mov->max_fragment_size)
5264             mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME;
5265     } else {
5266         if (mov->flags & FF_MOV_FLAG_FASTSTART)
5267             mov->reserved_header_pos = avio_tell(pb);
5268         mov_write_mdat_tag(pb, mov);
5269     }
5270
5271     if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
5272         mov->time = ff_iso8601_to_unix_time(t->value);
5273         if (mov->time < 0) {
5274             av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", t->value);
5275             mov->time = 0;
5276         }
5277     }
5278     if (mov->time)
5279         mov->time += 0x7C25B080; // 1970 based -> 1904 based
5280
5281     if (mov->chapter_track)
5282         if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
5283             goto error;
5284
5285     if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
5286         /* Initialize the hint tracks for each audio and video stream */
5287         for (i = 0; i < s->nb_streams; i++) {
5288             AVStream *st = s->streams[i];
5289             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
5290                 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
5291                 if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
5292                     goto error;
5293                 hint_track++;
5294             }
5295         }
5296     }
5297
5298     if (mov->nb_meta_tmcd) {
5299         /* Initialize the tmcd tracks */
5300         for (i = 0; i < s->nb_streams; i++) {
5301             AVStream *st = s->streams[i];
5302             t = global_tcr;
5303
5304             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
5305                 if (!t)
5306                     t = av_dict_get(st->metadata, "timecode", NULL, 0);
5307                 if (!t)
5308                     continue;
5309                 if ((ret = mov_create_timecode_track(s, tmcd_track, i, t->value)) < 0)
5310                     goto error;
5311                 tmcd_track++;
5312             }
5313         }
5314     }
5315
5316     avio_flush(pb);
5317
5318     if (mov->flags & FF_MOV_FLAG_ISML)
5319         mov_write_isml_manifest(pb, mov);
5320
5321     if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
5322         !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
5323         if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
5324             return ret;
5325         avio_flush(pb);
5326         mov->moov_written = 1;
5327         if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
5328             mov->reserved_header_pos = avio_tell(pb);
5329     }
5330
5331     return 0;
5332  error:
5333     mov_free(s);
5334     return ret;
5335 }
5336
5337 static int get_moov_size(AVFormatContext *s)
5338 {
5339     int ret;
5340     AVIOContext *moov_buf;
5341     MOVMuxContext *mov = s->priv_data;
5342
5343     if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
5344         return ret;
5345     if ((ret = mov_write_moov_tag(moov_buf, mov, s)) < 0)
5346         return ret;
5347     return ffio_close_null_buf(moov_buf);
5348 }
5349
5350 static int get_sidx_size(AVFormatContext *s)
5351 {
5352     int ret;
5353     AVIOContext *buf;
5354     MOVMuxContext *mov = s->priv_data;
5355
5356     if ((ret = ffio_open_null_buf(&buf)) < 0)
5357         return ret;
5358     mov_write_sidx_tags(buf, mov, -1, 0);
5359     return ffio_close_null_buf(buf);
5360 }
5361
5362 /*
5363  * This function gets the moov size if moved to the top of the file: the chunk
5364  * offset table can switch between stco (32-bit entries) to co64 (64-bit
5365  * entries) when the moov is moved to the beginning, so the size of the moov
5366  * would change. It also updates the chunk offset tables.
5367  */
5368 static int compute_moov_size(AVFormatContext *s)
5369 {
5370     int i, moov_size, moov_size2;
5371     MOVMuxContext *mov = s->priv_data;
5372
5373     moov_size = get_moov_size(s);
5374     if (moov_size < 0)
5375         return moov_size;
5376
5377     for (i = 0; i < mov->nb_streams; i++)
5378         mov->tracks[i].data_offset += moov_size;
5379
5380     moov_size2 = get_moov_size(s);
5381     if (moov_size2 < 0)
5382         return moov_size2;
5383
5384     /* if the size changed, we just switched from stco to co64 and need to
5385      * update the offsets */
5386     if (moov_size2 != moov_size)
5387         for (i = 0; i < mov->nb_streams; i++)
5388             mov->tracks[i].data_offset += moov_size2 - moov_size;
5389
5390     return moov_size2;
5391 }
5392
5393 static int compute_sidx_size(AVFormatContext *s)
5394 {
5395     int i, sidx_size;
5396     MOVMuxContext *mov = s->priv_data;
5397
5398     sidx_size = get_sidx_size(s);
5399     if (sidx_size < 0)
5400         return sidx_size;
5401
5402     for (i = 0; i < mov->nb_streams; i++)
5403         mov->tracks[i].data_offset += sidx_size;
5404
5405     return sidx_size;
5406 }
5407
5408 static int shift_data(AVFormatContext *s)
5409 {
5410     int ret = 0, moov_size;
5411     MOVMuxContext *mov = s->priv_data;
5412     int64_t pos, pos_end = avio_tell(s->pb);
5413     uint8_t *buf, *read_buf[2];
5414     int read_buf_id = 0;
5415     int read_size[2];
5416     AVIOContext *read_pb;
5417
5418     if (mov->flags & FF_MOV_FLAG_FRAGMENT)
5419         moov_size = compute_sidx_size(s);
5420     else
5421         moov_size = compute_moov_size(s);
5422     if (moov_size < 0)
5423         return moov_size;
5424
5425     buf = av_malloc(moov_size * 2);
5426     if (!buf)
5427         return AVERROR(ENOMEM);
5428     read_buf[0] = buf;
5429     read_buf[1] = buf + moov_size;
5430
5431     /* Shift the data: the AVIO context of the output can only be used for
5432      * writing, so we re-open the same output, but for reading. It also avoids
5433      * a read/seek/write/seek back and forth. */
5434     avio_flush(s->pb);
5435     ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
5436     if (ret < 0) {
5437         av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
5438                "the second pass (faststart)\n", s->filename);
5439         goto end;
5440     }
5441
5442     /* mark the end of the shift to up to the last data we wrote, and get ready
5443      * for writing */
5444     pos_end = avio_tell(s->pb);
5445     avio_seek(s->pb, mov->reserved_header_pos + moov_size, SEEK_SET);
5446
5447     /* start reading at where the new moov will be placed */
5448     avio_seek(read_pb, mov->reserved_header_pos, SEEK_SET);
5449     pos = avio_tell(read_pb);
5450
5451 #define READ_BLOCK do {                                                             \
5452     read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size);  \
5453     read_buf_id ^= 1;                                                               \
5454 } while (0)
5455
5456     /* shift data by chunk of at most moov_size */
5457     READ_BLOCK;
5458     do {
5459         int n;
5460         READ_BLOCK;
5461         n = read_size[read_buf_id];
5462         if (n <= 0)
5463             break;
5464         avio_write(s->pb, read_buf[read_buf_id], n);
5465         pos += n;
5466     } while (pos < pos_end);
5467     avio_close(read_pb);
5468
5469 end:
5470     av_free(buf);
5471     return ret;
5472 }
5473
5474 static int mov_write_trailer(AVFormatContext *s)
5475 {
5476     MOVMuxContext *mov = s->priv_data;
5477     AVIOContext *pb = s->pb;
5478     int res = 0;
5479     int i;
5480     int64_t moov_pos;
5481
5482     /*
5483      * Before actually writing the trailer, make sure that there are no
5484      * dangling subtitles, that need a terminating sample.
5485      */
5486     for (i = 0; i < mov->nb_streams; i++) {
5487         MOVTrack *trk = &mov->tracks[i];
5488         if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
5489             !trk->last_sample_is_subtitle_end) {
5490             mov_write_subtitle_end_packet(s, i, trk->track_duration);
5491             trk->last_sample_is_subtitle_end = 1;
5492         }
5493     }
5494
5495     // If there were no chapters when the header was written, but there
5496     // are chapters now, write them in the trailer.  This only works
5497     // when we are not doing fragments.
5498     if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
5499         if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
5500             mov->chapter_track = mov->nb_streams++;
5501             if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
5502                 goto error;
5503         }
5504     }
5505
5506     if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
5507         moov_pos = avio_tell(pb);
5508
5509         /* Write size of mdat tag */
5510         if (mov->mdat_size + 8 <= UINT32_MAX) {
5511             avio_seek(pb, mov->mdat_pos, SEEK_SET);
5512             avio_wb32(pb, mov->mdat_size + 8);
5513         } else {
5514             /* overwrite 'wide' placeholder atom */
5515             avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
5516             /* special value: real atom size will be 64 bit value after
5517              * tag field */
5518             avio_wb32(pb, 1);
5519             ffio_wfourcc(pb, "mdat");
5520             avio_wb64(pb, mov->mdat_size + 16);
5521         }
5522         avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_header_pos : moov_pos, SEEK_SET);
5523
5524         if (mov->flags & FF_MOV_FLAG_FASTSTART) {
5525             av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
5526             res = shift_data(s);
5527             if (res == 0) {
5528                 avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
5529                 if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
5530                     goto error;
5531             }
5532         } else if (mov->reserved_moov_size > 0) {
5533             int64_t size;
5534             if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
5535                 goto error;
5536             size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_header_pos);
5537             if (size < 8){
5538                 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
5539                 res = AVERROR(EINVAL);
5540                 goto error;
5541             }
5542             avio_wb32(pb, size);
5543             ffio_wfourcc(pb, "free");
5544             ffio_fill(pb, 0, size - 8);
5545             avio_seek(pb, moov_pos, SEEK_SET);
5546         } else {
5547             if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
5548                 goto error;
5549         }
5550         res = 0;
5551     } else {
5552         mov_auto_flush_fragment(s, 1);
5553         for (i = 0; i < mov->nb_streams; i++)
5554            mov->tracks[i].data_offset = 0;
5555         if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) {
5556             av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
5557             res = shift_data(s);
5558             if (res == 0) {
5559                 int64_t end = avio_tell(pb);
5560                 avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
5561                 mov_write_sidx_tags(pb, mov, -1, 0);
5562                 avio_seek(pb, end, SEEK_SET);
5563                 mov_write_mfra_tag(pb, mov);
5564             }
5565         } else {
5566             mov_write_mfra_tag(pb, mov);
5567         }
5568     }
5569
5570 error:
5571     mov_free(s);
5572
5573     return res;
5574 }
5575
5576 #if CONFIG_MOV_MUXER
5577 MOV_CLASS(mov)
5578 AVOutputFormat ff_mov_muxer = {
5579     .name              = "mov",
5580     .long_name         = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
5581     .extensions        = "mov",
5582     .priv_data_size    = sizeof(MOVMuxContext),
5583     .audio_codec       = AV_CODEC_ID_AAC,
5584     .video_codec       = CONFIG_LIBX264_ENCODER ?
5585                          AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
5586     .write_header      = mov_write_header,
5587     .write_packet      = mov_write_packet,
5588     .write_trailer     = mov_write_trailer,
5589     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
5590     .codec_tag         = (const AVCodecTag* const []){
5591         ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
5592     },
5593     .priv_class        = &mov_muxer_class,
5594 };
5595 #endif
5596 #if CONFIG_TGP_MUXER
5597 MOV_CLASS(tgp)
5598 AVOutputFormat ff_tgp_muxer = {
5599     .name              = "3gp",
5600     .long_name         = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
5601     .extensions        = "3gp",
5602     .priv_data_size    = sizeof(MOVMuxContext),
5603     .audio_codec       = AV_CODEC_ID_AMR_NB,
5604     .video_codec       = AV_CODEC_ID_H263,
5605     .write_header      = mov_write_header,
5606     .write_packet      = mov_write_packet,
5607     .write_trailer     = mov_write_trailer,
5608     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
5609     .codec_tag         = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
5610     .priv_class        = &tgp_muxer_class,
5611 };
5612 #endif
5613 #if CONFIG_MP4_MUXER
5614 MOV_CLASS(mp4)
5615 AVOutputFormat ff_mp4_muxer = {
5616     .name              = "mp4",
5617     .long_name         = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
5618     .mime_type         = "video/mp4",
5619     .extensions        = "mp4",
5620     .priv_data_size    = sizeof(MOVMuxContext),
5621     .audio_codec       = AV_CODEC_ID_AAC,
5622     .video_codec       = CONFIG_LIBX264_ENCODER ?
5623                          AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
5624     .write_header      = mov_write_header,
5625     .write_packet      = mov_write_packet,
5626     .write_trailer     = mov_write_trailer,
5627     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
5628     .codec_tag         = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
5629     .priv_class        = &mp4_muxer_class,
5630 };
5631 #endif
5632 #if CONFIG_PSP_MUXER
5633 MOV_CLASS(psp)
5634 AVOutputFormat ff_psp_muxer = {
5635     .name              = "psp",
5636     .long_name         = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
5637     .extensions        = "mp4,psp",
5638     .priv_data_size    = sizeof(MOVMuxContext),
5639     .audio_codec       = AV_CODEC_ID_AAC,
5640     .video_codec       = CONFIG_LIBX264_ENCODER ?
5641                          AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
5642     .write_header      = mov_write_header,
5643     .write_packet      = mov_write_packet,
5644     .write_trailer     = mov_write_trailer,
5645     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
5646     .codec_tag         = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
5647     .priv_class        = &psp_muxer_class,
5648 };
5649 #endif
5650 #if CONFIG_TG2_MUXER
5651 MOV_CLASS(tg2)
5652 AVOutputFormat ff_tg2_muxer = {
5653     .name              = "3g2",
5654     .long_name         = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
5655     .extensions        = "3g2",
5656     .priv_data_size    = sizeof(MOVMuxContext),
5657     .audio_codec       = AV_CODEC_ID_AMR_NB,
5658     .video_codec       = AV_CODEC_ID_H263,
5659     .write_header      = mov_write_header,
5660     .write_packet      = mov_write_packet,
5661     .write_trailer     = mov_write_trailer,
5662     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
5663     .codec_tag         = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
5664     .priv_class        = &tg2_muxer_class,
5665 };
5666 #endif
5667 #if CONFIG_IPOD_MUXER
5668 MOV_CLASS(ipod)
5669 AVOutputFormat ff_ipod_muxer = {
5670     .name              = "ipod",
5671     .long_name         = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
5672     .mime_type         = "video/mp4",
5673     .extensions        = "m4v,m4a",
5674     .priv_data_size    = sizeof(MOVMuxContext),
5675     .audio_codec       = AV_CODEC_ID_AAC,
5676     .video_codec       = AV_CODEC_ID_H264,
5677     .write_header      = mov_write_header,
5678     .write_packet      = mov_write_packet,
5679     .write_trailer     = mov_write_trailer,
5680     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
5681     .codec_tag         = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
5682     .priv_class        = &ipod_muxer_class,
5683 };
5684 #endif
5685 #if CONFIG_ISMV_MUXER
5686 MOV_CLASS(ismv)
5687 AVOutputFormat ff_ismv_muxer = {
5688     .name              = "ismv",
5689     .long_name         = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
5690     .mime_type         = "video/mp4",
5691     .extensions        = "ismv,isma",
5692     .priv_data_size    = sizeof(MOVMuxContext),
5693     .audio_codec       = AV_CODEC_ID_AAC,
5694     .video_codec       = AV_CODEC_ID_H264,
5695     .write_header      = mov_write_header,
5696     .write_packet      = mov_write_packet,
5697     .write_trailer     = mov_write_trailer,
5698     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
5699     .codec_tag         = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
5700     .priv_class        = &ismv_muxer_class,
5701 };
5702 #endif
5703 #if CONFIG_F4V_MUXER
5704 MOV_CLASS(f4v)
5705 AVOutputFormat ff_f4v_muxer = {
5706     .name              = "f4v",
5707     .long_name         = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
5708     .mime_type         = "application/f4v",
5709     .extensions        = "f4v",
5710     .priv_data_size    = sizeof(MOVMuxContext),
5711     .audio_codec       = AV_CODEC_ID_AAC,
5712     .video_codec       = AV_CODEC_ID_H264,
5713     .write_header      = mov_write_header,
5714     .write_packet      = mov_write_packet,
5715     .write_trailer     = mov_write_trailer,
5716     .flags             = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
5717     .codec_tag         = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
5718     .priv_class        = &f4v_muxer_class,
5719 };
5720 #endif