lavf: add AVFMT_FLAG_BITEXACT.
[libav.git] / libavformat / movenc.c
CommitLineData
1cb5f7fd 1/*
7fbde343 2 * MOV, 3GP, MP4 muxer
406792e7
DB
3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
7c4502c8 5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
1cb5f7fd 6 *
2912e87a 7 * This file is part of Libav.
b78e7197 8 *
2912e87a 9 * Libav is free software; you can redistribute it and/or
1cb5f7fd
MN
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
b78e7197 12 * version 2.1 of the License, or (at your option) any later version.
1cb5f7fd 13 *
2912e87a 14 * Libav is distributed in the hope that it will be useful,
1cb5f7fd
MN
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
2912e87a 20 * License along with Libav; if not, write to the Free Software
5509bffa 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1cb5f7fd 22 */
4909e517 23
8f8bc923 24#include <stdint.h>
cba4e606 25#include <inttypes.h>
8f8bc923 26
f72dad41 27#include "movenc.h"
1cb5f7fd 28#include "avformat.h"
0abdb293 29#include "avio_internal.h"
9d9f4119 30#include "riff.h"
1cb5f7fd 31#include "avio.h"
e40ee6a2 32#include "isom.h"
1bd2d763 33#include "avc.h"
9106a698 34#include "libavcodec/get_bits.h"
b2755007 35#include "libavcodec/put_bits.h"
4ace130e 36#include "libavcodec/vc1.h"
e977af6f
MS
37#include "internal.h"
38#include "libavutil/avstring.h"
3383a53e 39#include "libavutil/intfloat.h"
0ebcdf5c 40#include "libavutil/mathematics.h"
91e3a25e 41#include "libavutil/opt.h"
d2d67e42 42#include "libavutil/dict.h"
20b40a59 43#include "hevc.h"
d16cccac 44#include "rtpenc.h"
1fdf18f4 45#include "mov_chan.h"
1cb5f7fd 46
6e6d6dc0
MN
47#undef NDEBUG
48#include <assert.h>
49
91e3a25e 50static const AVOption options[] = {
d58dd4b5 51 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
124134e4
MS
52 { "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" },
53 { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
54 { "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" },
55 { "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" },
56 { "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" },
57 { "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" },
f8ef91ff 58 { "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" },
e7bf085b 59 { "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" },
5354a904 60 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
e6153f17
MS
61 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
62 { "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},
63 { "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},
64 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
65 { "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},
66 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
67 { "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},
1e9db41e 68 { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
91e3a25e
MS
69 { NULL },
70};
71
a4ea00d0
AK
72#define MOV_CLASS(flavor)\
73static const AVClass flavor ## _muxer_class = {\
74 .class_name = #flavor " muxer",\
75 .item_name = av_default_item_name,\
76 .option = options,\
77 .version = LIBAVUTIL_VERSION_INT,\
91e3a25e
MS
78};
79
90b5b51e 80//FIXME support 64 bit variant with wide placeholders
b7d3dd90 81static int64_t update_size(AVIOContext *pb, int64_t pos)
1cb5f7fd 82{
a2704c97 83 int64_t curpos = avio_tell(pb);
6b4aa5da 84 avio_seek(pb, pos, SEEK_SET);
77eb5504 85 avio_wb32(pb, curpos - pos); /* rewrite size */
6b4aa5da 86 avio_seek(pb, curpos, SEEK_SET);
6e6d6dc0
MN
87
88 return curpos - pos;
1cb5f7fd
MN
89}
90
60198742
CB
91static int co64_required(const MOVTrack *track)
92{
93 if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
94 return 1;
95 return 0;
96}
97
e45ccf79 98/* Chunk offset atom */
ae628ec1 99static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd
MN
100{
101 int i;
60198742 102 int mode64 = co64_required(track); // use 32 bit size variant if possible
a2704c97 103 int64_t pos = avio_tell(pb);
77eb5504 104 avio_wb32(pb, 0); /* size */
60198742 105 if (mode64) {
0abdb293 106 ffio_wfourcc(pb, "co64");
b29af723 107 } else
0abdb293 108 ffio_wfourcc(pb, "stco");
77eb5504
AK
109 avio_wb32(pb, 0); /* version & flags */
110 avio_wb32(pb, track->entry); /* entry count */
21732063
MS
111 for (i = 0; i < track->entry; i++) {
112 if (mode64 == 1)
0c7b8b75 113 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
b29af723 114 else
0c7b8b75 115 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
1cb5f7fd 116 }
b7d3dd90 117 return update_size(pb, pos);
1cb5f7fd
MN
118}
119
e45ccf79 120/* Sample size atom */
ae628ec1 121static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 122{
f578f938 123 int equalChunks = 1;
e45ccf79 124 int i, j, entries = 0, tst = -1, oldtst = -1;
1cb5f7fd 125
a2704c97 126 int64_t pos = avio_tell(pb);
77eb5504 127 avio_wb32(pb, 0); /* size */
0abdb293 128 ffio_wfourcc(pb, "stsz");
77eb5504 129 avio_wb32(pb, 0); /* version & flags */
1cb5f7fd 130
21732063
MS
131 for (i = 0; i < track->entry; i++) {
132 tst = track->cluster[i].size / track->cluster[i].entries;
133 if (oldtst != -1 && tst != oldtst)
e45ccf79 134 equalChunks = 0;
f578f938 135 oldtst = tst;
0aec3c5c 136 entries += track->cluster[i].entries;
f578f938 137 }
83988d58 138 if (equalChunks && track->entry) {
21732063 139 int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
7fe3207e 140 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
77eb5504
AK
141 avio_wb32(pb, sSize); // sample size
142 avio_wb32(pb, entries); // sample count
21732063 143 } else {
77eb5504
AK
144 avio_wb32(pb, 0); // sample size
145 avio_wb32(pb, entries); // sample count
21732063
MS
146 for (i = 0; i < track->entry; i++) {
147 for (j = 0; j < track->cluster[i].entries; j++) {
77eb5504 148 avio_wb32(pb, track->cluster[i].size /
21732063 149 track->cluster[i].entries);
e45ccf79 150 }
1cb5f7fd
MN
151 }
152 }
b7d3dd90 153 return update_size(pb, pos);
1cb5f7fd
MN
154}
155
e45ccf79 156/* Sample to chunk atom */
ae628ec1 157static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 158{
b29af723 159 int index = 0, oldval = -1, i;
bc5c918e 160 int64_t entryPos, curpos;
f578f938 161
a2704c97 162 int64_t pos = avio_tell(pb);
77eb5504 163 avio_wb32(pb, 0); /* size */
0abdb293 164 ffio_wfourcc(pb, "stsc");
77eb5504 165 avio_wb32(pb, 0); // version & flags
a2704c97 166 entryPos = avio_tell(pb);
77eb5504 167 avio_wb32(pb, track->entry); // entry count
21732063
MS
168 for (i = 0; i < track->entry; i++) {
169 if (oldval != track->cluster[i].samples_in_chunk) {
170 avio_wb32(pb, i + 1); // first chunk
b7d3dd90 171 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
77eb5504 172 avio_wb32(pb, 0x1); // sample description index
b7d3dd90 173 oldval = track->cluster[i].samples_in_chunk;
f578f938 174 index++;
1cb5f7fd
MN
175 }
176 }
a2704c97 177 curpos = avio_tell(pb);
6b4aa5da 178 avio_seek(pb, entryPos, SEEK_SET);
77eb5504 179 avio_wb32(pb, index); // rewrite size
6b4aa5da 180 avio_seek(pb, curpos, SEEK_SET);
1cb5f7fd 181
b7d3dd90 182 return update_size(pb, pos);
1cb5f7fd
MN
183}
184
e45ccf79 185/* Sync sample atom */
ae628ec1 186static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
1cb5f7fd 187{
bc5c918e 188 int64_t curpos, entryPos;
b29af723 189 int i, index = 0;
a2704c97 190 int64_t pos = avio_tell(pb);
77eb5504 191 avio_wb32(pb, 0); // size
0abdb293 192 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
77eb5504 193 avio_wb32(pb, 0); // version & flags
a2704c97 194 entryPos = avio_tell(pb);
77eb5504 195 avio_wb32(pb, track->entry); // entry count
21732063 196 for (i = 0; i < track->entry; i++) {
e1316b19 197 if (track->cluster[i].flags & flag) {
21732063 198 avio_wb32(pb, i + 1);
f578f938
TR
199 index++;
200 }
201 }
a2704c97 202 curpos = avio_tell(pb);
6b4aa5da 203 avio_seek(pb, entryPos, SEEK_SET);
77eb5504 204 avio_wb32(pb, index); // rewrite size
6b4aa5da 205 avio_seek(pb, curpos, SEEK_SET);
b7d3dd90 206 return update_size(pb, pos);
1cb5f7fd
MN
207}
208
ae628ec1 209static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 210{
77eb5504 211 avio_wb32(pb, 0x11); /* size */
0abdb293
AK
212 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
213 else ffio_wfourcc(pb, "damr");
214 ffio_wfourcc(pb, "FFMP");
77eb5504 215 avio_w8(pb, 0); /* decoder version */
3a72cbd9 216
77eb5504
AK
217 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
218 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
219 avio_w8(pb, 0x01); /* Frames per sample */
3a72cbd9
BC
220 return 0x11;
221}
222
ae628ec1 223static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
d4a240cb
BC
224{
225 GetBitContext gbc;
226 PutBitContext pbc;
227 uint8_t buf[3];
228 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
229
b7d3dd90 230 if (track->vos_len < 7)
d4a240cb
BC
231 return -1;
232
77eb5504 233 avio_wb32(pb, 11);
0abdb293 234 ffio_wfourcc(pb, "dac3");
d4a240cb 235
b7d3dd90 236 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
d4a240cb
BC
237 fscod = get_bits(&gbc, 2);
238 frmsizecod = get_bits(&gbc, 6);
239 bsid = get_bits(&gbc, 5);
240 bsmod = get_bits(&gbc, 3);
241 acmod = get_bits(&gbc, 3);
242 if (acmod == 2) {
243 skip_bits(&gbc, 2); // dsurmod
244 } else {
245 if ((acmod & 1) && acmod != 1)
246 skip_bits(&gbc, 2); // cmixlev
247 if (acmod & 4)
248 skip_bits(&gbc, 2); // surmixlev
249 }
250 lfeon = get_bits1(&gbc);
251
252 init_put_bits(&pbc, buf, sizeof(buf));
253 put_bits(&pbc, 2, fscod);
254 put_bits(&pbc, 5, bsid);
255 put_bits(&pbc, 3, bsmod);
256 put_bits(&pbc, 3, acmod);
257 put_bits(&pbc, 1, lfeon);
21732063 258 put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
d4a240cb
BC
259 put_bits(&pbc, 5, 0); // reserved
260
261 flush_put_bits(&pbc);
77eb5504 262 avio_write(pb, buf, sizeof(buf));
d4a240cb
BC
263
264 return 11;
265}
266
463e7afd
BC
267/**
268 * This function writes extradata "as is".
da9cea77 269 * Extradata must be formatted like a valid atom (with size and tag).
463e7afd 270 */
ae628ec1 271static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
463e7afd 272{
77eb5504 273 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
463e7afd
BC
274 return track->enc->extradata_size;
275}
276
b7d3dd90 277static void put_descr(AVIOContext *pb, int tag, unsigned int size)
039627cf 278{
7e19a6e8 279 int i = 3;
77eb5504 280 avio_w8(pb, tag);
21732063
MS
281 for (; i > 0; i--)
282 avio_w8(pb, (size >> (7 * i)) | 0x80);
77eb5504 283 avio_w8(pb, size & 0x7F);
039627cf
BC
284}
285
ae628ec1 286static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
039627cf 287{
a2704c97 288 int64_t pos = avio_tell(pb);
b7d3dd90 289 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
039627cf 290
77eb5504 291 avio_wb32(pb, 0); // size
0abdb293 292 ffio_wfourcc(pb, "esds");
77eb5504 293 avio_wb32(pb, 0); // Version
039627cf
BC
294
295 // ES descriptor
b7d3dd90
MS
296 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
297 avio_wb16(pb, track->track_id);
77eb5504 298 avio_w8(pb, 0x00); // flags (= no flags)
039627cf
BC
299
300 // DecoderConfig descriptor
b7d3dd90 301 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
039627cf
BC
302
303 // Object type indication
36ef5369
AK
304 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
305 track->enc->codec_id == AV_CODEC_ID_MP3) &&
c4e02d70 306 track->enc->sample_rate > 24000)
77eb5504 307 avio_w8(pb, 0x6B); // 11172-3
dfce888f 308 else
77eb5504 309 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
039627cf
BC
310
311 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
312 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
6adf3bc4
JS
313 if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
314 avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
315 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 316 avio_w8(pb, 0x15); // flags (= Audiostream)
039627cf 317 else
77eb5504 318 avio_w8(pb, 0x11); // flags (= Visualstream)
039627cf 319
61d36761 320 avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
039627cf 321
77eb5504 322 avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
21732063
MS
323 if (track->enc->rc_max_rate != track->enc->rc_min_rate ||
324 track->enc->rc_min_rate == 0)
77eb5504 325 avio_wb32(pb, 0); // vbr
039627cf 326 else
77eb5504 327 avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
039627cf 328
b7d3dd90 329 if (track->vos_len) {
039627cf 330 // DecoderSpecific info descriptor
b7d3dd90
MS
331 put_descr(pb, 0x05, track->vos_len);
332 avio_write(pb, track->vos_data, track->vos_len);
039627cf
BC
333 }
334
039627cf 335 // SL descriptor
b7d3dd90 336 put_descr(pb, 0x06, 1);
77eb5504 337 avio_w8(pb, 0x02);
b7d3dd90 338 return update_size(pb, pos);
039627cf
BC
339}
340
ae628ec1 341static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
f2589642 342{
a2704c97 343 int64_t pos = avio_tell(pb);
77eb5504
AK
344 avio_wb32(pb, 0);
345 avio_wl32(pb, track->tag); // store it byteswapped
795ed278 346 track->enc->codec_tag = av_bswap16(track->tag >> 16);
f2589642 347 ff_put_wav_header(pb, track->enc);
b7d3dd90 348 return update_size(pb, pos);
f2589642
BC
349}
350
5d561514
MS
351static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
352{
353 int64_t pos = avio_tell(pb);
354 avio_wb32(pb, 0);
355 ffio_wfourcc(pb, "wfex");
356 ff_put_wav_header(pb, track->enc);
357 return update_size(pb, pos);
358}
359
1fdf18f4
JR
360static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
361{
362 uint32_t layout_tag, bitmap;
363 int64_t pos = avio_tell(pb);
364
365 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
366 track->enc->channel_layout,
367 &bitmap);
368 if (!layout_tag) {
369 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
370 "lack of channel information\n");
371 return 0;
372 }
373
374 avio_wb32(pb, 0); // Size
375 ffio_wfourcc(pb, "chan"); // Type
376 avio_w8(pb, 0); // Version
377 avio_wb24(pb, 0); // Flags
378 avio_wb32(pb, layout_tag); // mChannelLayoutTag
379 avio_wb32(pb, bitmap); // mChannelBitmap
380 avio_wb32(pb, 0); // mNumberChannelDescriptions
381
b7d3dd90 382 return update_size(pb, pos);
1fdf18f4
JR
383}
384
ae628ec1 385static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
69dde1ad 386{
a2704c97 387 int64_t pos = avio_tell(pb);
69dde1ad 388
77eb5504 389 avio_wb32(pb, 0); /* size */
0abdb293 390 ffio_wfourcc(pb, "wave");
69dde1ad 391
77eb5504 392 avio_wb32(pb, 12); /* size */
0abdb293 393 ffio_wfourcc(pb, "frma");
77eb5504 394 avio_wl32(pb, track->tag);
69dde1ad 395
36ef5369 396 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
aa1c1c61 397 /* useless atom needed by mplayer, ipod, not needed by quicktime */
77eb5504 398 avio_wb32(pb, 12); /* size */
0abdb293 399 ffio_wfourcc(pb, "mp4a");
77eb5504 400 avio_wb32(pb, 0);
fcef991a 401 mov_write_esds_tag(pb, track);
36ef5369 402 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
b7d9da10 403 mov_write_amr_tag(pb, track);
36ef5369 404 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
d4a240cb 405 mov_write_ac3_tag(pb, track);
36ef5369 406 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC) {
463e7afd 407 mov_write_extradata_tag(pb, track);
36ef5369
AK
408 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
409 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
f2589642 410 mov_write_ms_tag(pb, track);
fcef991a 411 }
69dde1ad 412
77eb5504
AK
413 avio_wb32(pb, 8); /* size */
414 avio_wb32(pb, 0); /* null tag */
69dde1ad 415
b7d3dd90 416 return update_size(pb, pos);
69dde1ad
GB
417}
418
4ace130e
MS
419static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
420{
421 uint8_t *unescaped;
422 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
423 int unescaped_size, seq_found = 0;
424 int level = 0, interlace = 0;
425 int packet_seq = track->vc1_info.packet_seq;
426 int packet_entry = track->vc1_info.packet_entry;
427 int slices = track->vc1_info.slices;
428 PutBitContext pbc;
429
430 if (track->start_dts == AV_NOPTS_VALUE) {
431 /* No packets written yet, vc1_info isn't authoritative yet. */
432 /* Assume inline sequence and entry headers. This will be
433 * overwritten at the end if the file is seekable. */
434 packet_seq = packet_entry = 1;
435 }
436
437 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
438 if (!unescaped)
439 return AVERROR(ENOMEM);
440 start = find_next_marker(track->vos_data, end);
441 for (next = start; next < end; start = next) {
442 GetBitContext gb;
443 int size;
444 next = find_next_marker(start + 4, end);
445 size = next - start - 4;
446 if (size <= 0)
447 continue;
448 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
449 init_get_bits(&gb, unescaped, 8 * unescaped_size);
450 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
451 int profile = get_bits(&gb, 2);
452 if (profile != PROFILE_ADVANCED) {
453 av_free(unescaped);
454 return AVERROR(ENOSYS);
455 }
456 seq_found = 1;
457 level = get_bits(&gb, 3);
458 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
459 * width, height */
460 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
461 skip_bits(&gb, 1); /* broadcast */
462 interlace = get_bits1(&gb);
463 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
464 }
465 }
466 if (!seq_found) {
467 av_free(unescaped);
468 return AVERROR(ENOSYS);
469 }
470
471 init_put_bits(&pbc, buf, 7);
472 /* VC1DecSpecStruc */
473 put_bits(&pbc, 4, 12); /* profile - advanced */
474 put_bits(&pbc, 3, level);
475 put_bits(&pbc, 1, 0); /* reserved */
476 /* VC1AdvDecSpecStruc */
477 put_bits(&pbc, 3, level);
478 put_bits(&pbc, 1, 0); /* cbr */
479 put_bits(&pbc, 6, 0); /* reserved */
480 put_bits(&pbc, 1, !interlace); /* no interlace */
481 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
482 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
483 put_bits(&pbc, 1, !slices); /* no slice code */
484 put_bits(&pbc, 1, 0); /* no bframe */
485 put_bits(&pbc, 1, 0); /* reserved */
486 put_bits32(&pbc, track->enc->time_base.den); /* framerate */
487 flush_put_bits(&pbc);
488
489 av_free(unescaped);
490
491 return 0;
492}
493
494static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
495{
496 uint8_t buf[7] = { 0 };
497 int ret;
498
499 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
500 return ret;
501
502 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
503 ffio_wfourcc(pb, "dvc1");
504 track->vc1_info.struct_offset = avio_tell(pb);
505 avio_write(pb, buf, sizeof(buf));
506 avio_write(pb, track->vos_data, track->vos_len);
507
508 return 0;
509}
510
ae628ec1 511static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
79e42311 512{
b7d3dd90 513 avio_wb32(pb, track->vos_len + 8);
0abdb293 514 ffio_wfourcc(pb, "glbl");
b7d3dd90
MS
515 avio_write(pb, track->vos_data, track->vos_len);
516 return 8 + track->vos_len;
79e42311
BC
517}
518
feaa8d11
BC
519/**
520 * Compute flags for 'lpcm' tag.
521 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
522 */
36ef5369 523static int mov_get_lpcm_flags(enum AVCodecID codec_id)
feaa8d11
BC
524{
525 switch (codec_id) {
36ef5369
AK
526 case AV_CODEC_ID_PCM_F32BE:
527 case AV_CODEC_ID_PCM_F64BE:
feaa8d11 528 return 11;
36ef5369
AK
529 case AV_CODEC_ID_PCM_F32LE:
530 case AV_CODEC_ID_PCM_F64LE:
feaa8d11 531 return 9;
36ef5369 532 case AV_CODEC_ID_PCM_U8:
feaa8d11 533 return 10;
36ef5369
AK
534 case AV_CODEC_ID_PCM_S16BE:
535 case AV_CODEC_ID_PCM_S24BE:
536 case AV_CODEC_ID_PCM_S32BE:
feaa8d11 537 return 14;
36ef5369
AK
538 case AV_CODEC_ID_PCM_S8:
539 case AV_CODEC_ID_PCM_S16LE:
540 case AV_CODEC_ID_PCM_S24LE:
541 case AV_CODEC_ID_PCM_S32LE:
feaa8d11
BC
542 return 12;
543 default:
544 return 0;
545 }
546}
547
681d1726
JR
548static int get_cluster_duration(MOVTrack *track, int cluster_idx)
549{
550 int64_t next_dts;
551
552 if (cluster_idx >= track->entry)
553 return 0;
554
555 if (cluster_idx + 1 == track->entry)
556 next_dts = track->track_duration + track->start_dts;
557 else
558 next_dts = track->cluster[cluster_idx + 1].dts;
559
560 return next_dts - track->cluster[cluster_idx].dts;
561}
562
f234e8a3
JR
563static int get_samples_per_packet(MOVTrack *track)
564{
565 int i, first_duration;
566
567 /* use 1 for raw PCM */
568 if (!track->audio_vbr)
569 return 1;
570
571 /* check to see if duration is constant for all clusters */
572 if (!track->entry)
573 return 0;
574 first_duration = get_cluster_duration(track, 0);
575 for (i = 1; i < track->entry; i++) {
576 if (get_cluster_duration(track, i) != first_duration)
577 return 0;
578 }
579 return first_duration;
580}
581
ae628ec1 582static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 583{
a2704c97 584 int64_t pos = avio_tell(pb);
feaa8d11
BC
585 int version = 0;
586 uint32_t tag = track->tag;
587
588 if (track->mode == MODE_MOV) {
8e8c5131
JR
589 if (mov_get_lpcm_flags(track->enc->codec_id))
590 tag = AV_RL32("lpcm");
591 version = 2;
feaa8d11 592 }
115329f1 593
77eb5504
AK
594 avio_wb32(pb, 0); /* size */
595 avio_wl32(pb, tag); // store it byteswapped
596 avio_wb32(pb, 0); /* Reserved */
597 avio_wb16(pb, 0); /* Reserved */
598 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
69dde1ad 599
e45ccf79 600 /* SoundDescription */
77eb5504
AK
601 avio_wb16(pb, version); /* Version */
602 avio_wb16(pb, 0); /* Revision level */
603 avio_wb32(pb, 0); /* Reserved */
1cb5f7fd 604
feaa8d11 605 if (version == 2) {
77eb5504
AK
606 avio_wb16(pb, 3);
607 avio_wb16(pb, 16);
608 avio_wb16(pb, 0xfffe);
609 avio_wb16(pb, 0);
610 avio_wb32(pb, 0x00010000);
611 avio_wb32(pb, 72);
3b5d4428 612 avio_wb64(pb, av_double2int(track->enc->sample_rate));
77eb5504
AK
613 avio_wb32(pb, track->enc->channels);
614 avio_wb32(pb, 0x7F000000);
615 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
616 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
b7d3dd90 617 avio_wb32(pb, track->sample_size);
f234e8a3 618 avio_wb32(pb, get_samples_per_packet(track));
feaa8d11 619 } else {
8e8c5131
JR
620 /* reserved for mp4/3gp */
621 avio_wb16(pb, 2);
622 avio_wb16(pb, 16);
623 avio_wb16(pb, 0);
ef19c7eb 624
77eb5504 625 avio_wb16(pb, 0); /* packet size (= 0) */
dc878b96
AC
626 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
627 track->enc->sample_rate : 0);
77eb5504 628 avio_wb16(pb, 0); /* Reserved */
feaa8d11 629 }
1cb5f7fd 630
21732063
MS
631 if (track->mode == MODE_MOV &&
632 (track->enc->codec_id == AV_CODEC_ID_AAC ||
633 track->enc->codec_id == AV_CODEC_ID_AC3 ||
634 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
635 track->enc->codec_id == AV_CODEC_ID_ALAC ||
636 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
637 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV))
fcef991a 638 mov_write_wave_tag(pb, track);
21732063 639 else if (track->tag == MKTAG('m','p','4','a'))
3a72cbd9 640 mov_write_esds_tag(pb, track);
21732063 641 else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
b7d9da10 642 mov_write_amr_tag(pb, track);
21732063 643 else if (track->enc->codec_id == AV_CODEC_ID_AC3)
4ed19420 644 mov_write_ac3_tag(pb, track);
21732063 645 else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
709c9f8d 646 mov_write_extradata_tag(pb, track);
36ef5369 647 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
5d561514 648 mov_write_wfex_tag(pb, track);
b7d3dd90 649 else if (track->vos_len > 0)
79e42311 650 mov_write_glbl_tag(pb, track);
3a72cbd9 651
41e9682a
AC
652 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
653 mov_write_chan_tag(pb, track);
654
b7d3dd90 655 return update_size(pb, pos);
1cb5f7fd
MN
656}
657
ae628ec1 658static int mov_write_d263_tag(AVIOContext *pb)
1cb5f7fd 659{
77eb5504 660 avio_wb32(pb, 0xf); /* size */
0abdb293
AK
661 ffio_wfourcc(pb, "d263");
662 ffio_wfourcc(pb, "FFMP");
77eb5504 663 avio_w8(pb, 0); /* decoder version */
87494ea0 664 /* FIXME use AVCodecContext level/profile, when encoder will set values */
77eb5504
AK
665 avio_w8(pb, 0xa); /* level */
666 avio_w8(pb, 0); /* profile */
1cb5f7fd
MN
667 return 0xf;
668}
669
f578f938 670/* TODO: No idea about these values */
ae628ec1 671static int mov_write_svq3_tag(AVIOContext *pb)
1cb5f7fd 672{
77eb5504 673 avio_wb32(pb, 0x15);
0abdb293
AK
674 ffio_wfourcc(pb, "SMI ");
675 ffio_wfourcc(pb, "SEQH");
77eb5504
AK
676 avio_wb32(pb, 0x5);
677 avio_wb32(pb, 0xe2c0211d);
678 avio_wb32(pb, 0xc0000000);
679 avio_w8(pb, 0);
f578f938 680 return 0x15;
1cb5f7fd
MN
681}
682
ae628ec1 683static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
6e6eebf9 684{
a2704c97 685 int64_t pos = avio_tell(pb);
6e6eebf9 686
77eb5504 687 avio_wb32(pb, 0);
0abdb293 688 ffio_wfourcc(pb, "avcC");
b7d3dd90
MS
689 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
690 return update_size(pb, pos);
c1b8e6d8
BC
691}
692
20b40a59
TW
693static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
694{
695 int64_t pos = avio_tell(pb);
696
697 avio_wb32(pb, 0);
698 ffio_wfourcc(pb, "hvcC");
699 ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
700 return update_size(pb, pos);
701}
702
fc4cbc16 703/* also used by all avid codecs (dv, imx, meridien) and their variants */
ae628ec1 704static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
fc4cbc16
BC
705{
706 int i;
77eb5504 707 avio_wb32(pb, 24); /* size */
0abdb293
AK
708 ffio_wfourcc(pb, "ACLR");
709 ffio_wfourcc(pb, "ACLR");
710 ffio_wfourcc(pb, "0001");
304e983d 711 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
77eb5504 712 avio_wb32(pb, 0); /* unknown */
fc4cbc16 713
77eb5504 714 avio_wb32(pb, 24); /* size */
0abdb293
AK
715 ffio_wfourcc(pb, "APRG");
716 ffio_wfourcc(pb, "APRG");
717 ffio_wfourcc(pb, "0001");
77eb5504
AK
718 avio_wb32(pb, 1); /* unknown */
719 avio_wb32(pb, 0); /* unknown */
fc4cbc16 720
77eb5504 721 avio_wb32(pb, 120); /* size */
0abdb293
AK
722 ffio_wfourcc(pb, "ARES");
723 ffio_wfourcc(pb, "ARES");
724 ffio_wfourcc(pb, "0001");
b7d3dd90 725 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
77eb5504 726 avio_wb32(pb, track->enc->width);
fc4cbc16 727 /* values below are based on samples created with quicktime and avid codecs */
b7d3dd90 728 if (track->vos_data[5] & 2) { // interlaced
21732063 729 avio_wb32(pb, track->enc->height / 2);
77eb5504
AK
730 avio_wb32(pb, 2); /* unknown */
731 avio_wb32(pb, 0); /* unknown */
732 avio_wb32(pb, 4); /* unknown */
fc4cbc16 733 } else {
77eb5504
AK
734 avio_wb32(pb, track->enc->height);
735 avio_wb32(pb, 1); /* unknown */
736 avio_wb32(pb, 0); /* unknown */
44fb8ebb 737 if (track->enc->height == 1080)
77eb5504 738 avio_wb32(pb, 5); /* unknown */
44fb8ebb 739 else
77eb5504 740 avio_wb32(pb, 6); /* unknown */
fc4cbc16
BC
741 }
742 /* padding */
743 for (i = 0; i < 10; i++)
77eb5504 744 avio_wb64(pb, 0);
fc4cbc16
BC
745
746 /* extra padding for stsd needed */
77eb5504 747 avio_wb32(pb, 0);
fc4cbc16
BC
748 return 0;
749}
750
6b600285 751static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
8e321619 752{
039627cf 753 int tag = track->enc->codec_tag;
2ab57c04 754
1a40491e 755 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
2ab57c04
BC
756 return 0;
757
36ef5369 758 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
4f3db5d3 759 else if (track->enc->codec_id == AV_CODEC_ID_HEVC) tag = MKTAG('h','e','v','1');
36ef5369
AK
760 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
761 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
762 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
763 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
21732063
MS
764 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
765 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
6adf3bc4 766 else if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE) tag = MKTAG('m','p','4','s');
6b600285
BC
767
768 return tag;
769}
770
3c8d1447 771static const AVCodecTag codec_ipod_tags[] = {
21732063
MS
772 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
773 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
774 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
775 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
776 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
36ef5369
AK
777 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
778 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
779 { AV_CODEC_ID_NONE, 0 },
3c8d1447
BC
780};
781
6b600285
BC
782static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
783{
784 int tag = track->enc->codec_tag;
785
130ba4b1 786 // keep original tag for subs, ipod supports both formats
72415b2a 787 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
21732063
MS
788 (tag == MKTAG('t', 'x', '3', 'g') ||
789 tag == MKTAG('t', 'e', 'x', 't'))))
1a40491e 790 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
0872acc4 791
ade5b91d 792 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
2ab57c04
BC
793 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
794 "Quicktime/Ipod might not play the file\n");
6b600285
BC
795
796 return tag;
797}
798
799static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
800{
801 int tag;
802
06ed4873
BC
803 if (track->enc->width == 720) /* SD */
804 if (track->enc->height == 480) /* NTSC */
716d413c 805 if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
21732063 806 else tag = MKTAG('d','v','c',' ');
716d413c
AK
807 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
808 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
21732063 809 else tag = MKTAG('d','v','p','p');
06ed4873 810 else if (track->enc->height == 720) /* HD 720 line */
21732063
MS
811 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
812 else tag = MKTAG('d','v','h','p');
06ed4873 813 else if (track->enc->height == 1080) /* HD 1080 line */
21732063
MS
814 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
815 else tag = MKTAG('d','v','h','6');
06ed4873
BC
816 else {
817 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
818 return 0;
819 }
6b600285
BC
820
821 return tag;
822}
823
3c8d1447 824static const struct {
716d413c 825 enum AVPixelFormat pix_fmt;
3c8d1447
BC
826 uint32_t tag;
827 unsigned bps;
828} mov_pix_fmt_tags[] = {
716d413c
AK
829 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
830 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
831 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
832 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
833 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
834 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
835 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
836 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
837 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
838 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
839 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
840 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
841 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
842 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
3c8d1447
BC
843};
844
6b600285
BC
845static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
846{
847 int tag = track->enc->codec_tag;
2ab57c04
BC
848 int i;
849
850 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
851 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
852 tag = mov_pix_fmt_tags[i].tag;
853 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
854 break;
855 }
856 }
6b600285
BC
857
858 return tag;
859}
860
861static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
862{
863 int tag = track->enc->codec_tag;
864
865 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
36ef5369
AK
866 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
867 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
868 track->enc->codec_id == AV_CODEC_ID_H263 ||
6b600285 869 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
36ef5369 870 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
6b600285 871 tag = mov_get_dv_codec_tag(s, track);
36ef5369 872 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
6b600285 873 tag = mov_get_rawvideo_codec_tag(s, track);
72415b2a 874 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
167f3b8d 875 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
08680ab8 876 if (!tag) { // if no mac fcc found, try with Microsoft tags
1a40491e 877 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
08680ab8 878 if (tag)
eb97dbb0 879 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
08680ab8
BC
880 "the file may be unplayable!\n");
881 }
72415b2a 882 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
167f3b8d 883 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
08680ab8 884 if (!tag) { // if no mac fcc found, try with Microsoft tags
1a40491e 885 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
08680ab8
BC
886 if (ms_tag) {
887 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
eb97dbb0 888 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
08680ab8 889 "the file may be unplayable!\n");
d97f2144 890 }
08680ab8 891 }
72415b2a 892 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1a40491e 893 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
a23c9c4a 894 }
2ab57c04 895
8e321619
BC
896 return tag;
897}
898
3c8d1447 899static const AVCodecTag codec_3gp_tags[] = {
21732063
MS
900 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
901 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
902 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
903 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
904 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
905 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
36ef5369
AK
906 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
907 { AV_CODEC_ID_NONE, 0 },
3c8d1447
BC
908};
909
e01fba55
CB
910static const AVCodecTag codec_f4v_tags[] = {
911 { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
912 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
913 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
914 { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
915 { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
916 { AV_CODEC_ID_NONE, 0 },
917};
918
6b600285
BC
919static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
920{
73b7437f 921 int tag;
6b600285 922
5d561514
MS
923 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
924 tag = mp4_get_codec_tag(s, track);
925 else if (track->mode == MODE_ISM) {
6b600285 926 tag = mp4_get_codec_tag(s, track);
36ef5369 927 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
5d561514
MS
928 tag = MKTAG('w', 'm', 'a', ' ');
929 } else if (track->mode == MODE_IPOD)
6b600285
BC
930 tag = ipod_get_codec_tag(s, track);
931 else if (track->mode & MODE_3GP)
1a40491e 932 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
e01fba55
CB
933 else if (track->mode == MODE_F4V)
934 tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
6b600285
BC
935 else
936 tag = mov_get_codec_tag(s, track);
937
938 return tag;
939}
940
aa9f4208
BC
941/** Write uuid atom.
942 * Needed to make file play in iPods running newest firmware
943 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
944 */
ae628ec1 945static int mov_write_uuid_tag_ipod(AVIOContext *pb)
aa9f4208 946{
77eb5504 947 avio_wb32(pb, 28);
0abdb293 948 ffio_wfourcc(pb, "uuid");
77eb5504
AK
949 avio_wb32(pb, 0x6b6840f2);
950 avio_wb32(pb, 0x5f244fc5);
951 avio_wb32(pb, 0xba39a51b);
952 avio_wb32(pb, 0xcf0323f3);
953 avio_wb32(pb, 0x0);
aa9f4208
BC
954 return 28;
955}
956
4bf3c8f2
AC
957static const uint16_t fiel_data[] = {
958 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
959};
960
961static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
962{
963 unsigned mov_field_order = 0;
964 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
965 mov_field_order = fiel_data[track->enc->field_order];
966 else
967 return 0;
968 avio_wb32(pb, 10);
969 ffio_wfourcc(pb, "fiel");
970 avio_wb16(pb, mov_field_order);
971 return 10;
972}
973
ae628ec1 974static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
f6204886 975{
a2704c97 976 int64_t pos = avio_tell(pb);
77eb5504
AK
977 avio_wb32(pb, 0); /* size */
978 avio_wl32(pb, track->tag); // store it byteswapped
979 avio_wb32(pb, 0); /* Reserved */
980 avio_wb16(pb, 0); /* Reserved */
981 avio_wb16(pb, 1); /* Data-reference index */
f6204886 982
6adf3bc4
JS
983 if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
984 mov_write_esds_tag(pb, track);
985 else if (track->enc->extradata_size)
77eb5504 986 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
f6204886 987
b7d3dd90 988 return update_size(pb, pos);
f6204886
DC
989}
990
ae628ec1 991static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
b015be21
BC
992{
993 AVRational sar;
994 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
995 track->enc->sample_aspect_ratio.den, INT_MAX);
996
77eb5504 997 avio_wb32(pb, 16);
0abdb293 998 ffio_wfourcc(pb, "pasp");
77eb5504
AK
999 avio_wb32(pb, sar.num);
1000 avio_wb32(pb, sar.den);
b015be21
BC
1001 return 16;
1002}
1003
ae628ec1 1004static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1005{
a2704c97 1006 int64_t pos = avio_tell(pb);
a92be9b8 1007 char compressor_name[32] = { 0 };
8cc7a34d 1008
77eb5504
AK
1009 avio_wb32(pb, 0); /* size */
1010 avio_wl32(pb, track->tag); // store it byteswapped
1011 avio_wb32(pb, 0); /* Reserved */
1012 avio_wb16(pb, 0); /* Reserved */
1013 avio_wb16(pb, 1); /* Data-reference index */
f578f938 1014
77eb5504
AK
1015 avio_wb16(pb, 0); /* Codec stream version */
1016 avio_wb16(pb, 0); /* Codec stream revision (=0) */
5997ed78 1017 if (track->mode == MODE_MOV) {
0abdb293 1018 ffio_wfourcc(pb, "FFMP"); /* Vendor */
21732063 1019 if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
77eb5504
AK
1020 avio_wb32(pb, 0); /* Temporal Quality */
1021 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
915282e5 1022 } else {
77eb5504
AK
1023 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1024 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
915282e5 1025 }
5997ed78 1026 } else {
77eb5504
AK
1027 avio_wb32(pb, 0); /* Reserved */
1028 avio_wb32(pb, 0); /* Reserved */
1029 avio_wb32(pb, 0); /* Reserved */
5997ed78 1030 }
77eb5504
AK
1031 avio_wb16(pb, track->enc->width); /* Video width */
1032 avio_wb16(pb, track->height); /* Video height */
1033 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1034 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1035 avio_wb32(pb, 0); /* Data size (= 0) */
1036 avio_wb16(pb, 1); /* Frame count (= 1) */
115329f1 1037
5997ed78
BC
1038 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1039 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
21732063 1040 av_strlcpy(compressor_name, track->enc->codec->name, 32);
77eb5504
AK
1041 avio_w8(pb, strlen(compressor_name));
1042 avio_write(pb, compressor_name, 31);
115329f1 1043
dd1c8f3e 1044 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
77eb5504 1045 avio_wb16(pb, track->enc->bits_per_coded_sample);
ccac8438 1046 else
77eb5504
AK
1047 avio_wb16(pb, 0x18); /* Reserved */
1048 avio_wb16(pb, 0xffff); /* Reserved */
21732063 1049 if (track->tag == MKTAG('m','p','4','v'))
f578f938 1050 mov_write_esds_tag(pb, track);
21732063 1051 else if (track->enc->codec_id == AV_CODEC_ID_H263)
f578f938 1052 mov_write_d263_tag(pb);
21732063 1053 else if (track->enc->codec_id == AV_CODEC_ID_SVQ3)
115329f1 1054 mov_write_svq3_tag(pb);
21732063 1055 else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
7b0f4c9a 1056 mov_write_avid_tag(pb, track);
20b40a59
TW
1057 else if (track->enc->codec_id == AV_CODEC_ID_HEVC)
1058 mov_write_hvcc_tag(pb, track);
21732063 1059 else if (track->enc->codec_id == AV_CODEC_ID_H264) {
c1b8e6d8 1060 mov_write_avcc_tag(pb, track);
21732063 1061 if (track->mode == MODE_IPOD)
aa9f4208 1062 mov_write_uuid_tag_ipod(pb);
4bf3c8f2
AC
1063 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1064 mov_write_fiel_tag(pb, track);
36ef5369 1065 else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
4ace130e 1066 mov_write_dvc1_tag(pb, track);
e01fba55
CB
1067 else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
1068 track->enc->codec_id == AV_CODEC_ID_VP6A) {
1069 /* Don't write any potential extradata here - the cropping
1070 * is signalled via the normal width/height fields. */
1071 } else if (track->vos_len > 0)
79e42311 1072 mov_write_glbl_tag(pb, track);
f578f938 1073
d184c86c 1074 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
b015be21
BC
1075 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1076 mov_write_pasp_tag(pb, track);
1077 }
1078
b7d3dd90 1079 return update_size(pb, pos);
1cb5f7fd
MN
1080}
1081
ae628ec1 1082static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
e977af6f 1083{
a2704c97 1084 int64_t pos = avio_tell(pb);
77eb5504 1085 avio_wb32(pb, 0); /* size */
0abdb293 1086 ffio_wfourcc(pb, "rtp ");
77eb5504
AK
1087 avio_wb32(pb, 0); /* Reserved */
1088 avio_wb16(pb, 0); /* Reserved */
1089 avio_wb16(pb, 1); /* Data-reference index */
e977af6f 1090
77eb5504
AK
1091 avio_wb16(pb, 1); /* Hint track version */
1092 avio_wb16(pb, 1); /* Highest compatible version */
1093 avio_wb32(pb, track->max_packet_size); /* Max packet size */
e977af6f 1094
77eb5504 1095 avio_wb32(pb, 12); /* size */
0abdb293 1096 ffio_wfourcc(pb, "tims");
77eb5504 1097 avio_wb32(pb, track->timescale);
e977af6f 1098
b7d3dd90 1099 return update_size(pb, pos);
e977af6f
MS
1100}
1101
b89e8759
LB
1102static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1103{
1104 int64_t pos = avio_tell(pb);
1105
1106 avio_wb32(pb, 0); /* size */
1107 ffio_wfourcc(pb, "tmcd"); /* Data format */
1108 avio_wb32(pb, 0); /* Reserved */
1109 avio_wb32(pb, 1); /* Data reference index */
1110 if (track->enc->extradata_size)
1111 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1112 return update_size(pb, pos);
1113}
1114
ae628ec1 1115static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1116{
a2704c97 1117 int64_t pos = avio_tell(pb);
77eb5504 1118 avio_wb32(pb, 0); /* size */
0abdb293 1119 ffio_wfourcc(pb, "stsd");
77eb5504
AK
1120 avio_wb32(pb, 0); /* version & flags */
1121 avio_wb32(pb, 1); /* entry count */
72415b2a 1122 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
f578f938 1123 mov_write_video_tag(pb, track);
72415b2a 1124 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
f578f938 1125 mov_write_audio_tag(pb, track);
72415b2a 1126 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
f6204886 1127 mov_write_subtitle_tag(pb, track);
e977af6f
MS
1128 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1129 mov_write_rtp_tag(pb, track);
b89e8759
LB
1130 else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1131 mov_write_tmcd_tag(pb, track);
b7d3dd90 1132 return update_size(pb, pos);
1cb5f7fd
MN
1133}
1134
ae628ec1 1135static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
b4712e3c 1136{
c3e92a6c 1137 MOVStts *ctts_entries;
b4712e3c
BC
1138 uint32_t entries = 0;
1139 uint32_t atom_size;
1140 int i;
1141
1142 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1143 ctts_entries[0].count = 1;
0aec3c5c 1144 ctts_entries[0].duration = track->cluster[0].cts;
21732063 1145 for (i = 1; i < track->entry; i++) {
0aec3c5c 1146 if (track->cluster[i].cts == ctts_entries[entries].duration) {
b4712e3c
BC
1147 ctts_entries[entries].count++; /* compress */
1148 } else {
1149 entries++;
0aec3c5c 1150 ctts_entries[entries].duration = track->cluster[i].cts;
b4712e3c
BC
1151 ctts_entries[entries].count = 1;
1152 }
1153 }
1154 entries++; /* last one */
1155 atom_size = 16 + (entries * 8);
77eb5504 1156 avio_wb32(pb, atom_size); /* size */
0abdb293 1157 ffio_wfourcc(pb, "ctts");
77eb5504
AK
1158 avio_wb32(pb, 0); /* version & flags */
1159 avio_wb32(pb, entries); /* entry count */
21732063 1160 for (i = 0; i < entries; i++) {
77eb5504
AK
1161 avio_wb32(pb, ctts_entries[i].count);
1162 avio_wb32(pb, ctts_entries[i].duration);
b4712e3c
BC
1163 }
1164 av_free(ctts_entries);
1165 return atom_size;
1166}
1167
e45ccf79 1168/* Time to sample atom */
ae628ec1 1169static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1170{
c3e92a6c 1171 MOVStts *stts_entries;
aa90239f
BC
1172 uint32_t entries = -1;
1173 uint32_t atom_size;
1174 int i;
1175
72415b2a 1176 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
aa90239f 1177 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
b7d3dd90 1178 stts_entries[0].count = track->sample_count;
aa90239f
BC
1179 stts_entries[0].duration = 1;
1180 entries = 1;
1181 } else {
83988d58
MS
1182 stts_entries = track->entry ?
1183 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1184 NULL;
21732063 1185 for (i = 0; i < track->entry; i++) {
681d1726 1186 int duration = get_cluster_duration(track, i);
aa90239f
BC
1187 if (i && duration == stts_entries[entries].duration) {
1188 stts_entries[entries].count++; /* compress */
1189 } else {
1190 entries++;
1191 stts_entries[entries].duration = duration;
1192 stts_entries[entries].count = 1;
1193 }
1194 }
1195 entries++; /* last one */
1196 }
1197 atom_size = 16 + (entries * 8);
77eb5504 1198 avio_wb32(pb, atom_size); /* size */
0abdb293 1199 ffio_wfourcc(pb, "stts");
77eb5504
AK
1200 avio_wb32(pb, 0); /* version & flags */
1201 avio_wb32(pb, entries); /* entry count */
21732063 1202 for (i = 0; i < entries; i++) {
77eb5504
AK
1203 avio_wb32(pb, stts_entries[i].count);
1204 avio_wb32(pb, stts_entries[i].duration);
aa90239f
BC
1205 }
1206 av_free(stts_entries);
1207 return atom_size;
1cb5f7fd
MN
1208}
1209
ae628ec1 1210static int mov_write_dref_tag(AVIOContext *pb)
1cb5f7fd 1211{
77eb5504 1212 avio_wb32(pb, 28); /* size */
0abdb293 1213 ffio_wfourcc(pb, "dref");
77eb5504
AK
1214 avio_wb32(pb, 0); /* version & flags */
1215 avio_wb32(pb, 1); /* entry count */
1cb5f7fd 1216
77eb5504 1217 avio_wb32(pb, 0xc); /* size */
0abdb293 1218 ffio_wfourcc(pb, "url ");
77eb5504 1219 avio_wb32(pb, 1); /* version & flags */
1cb5f7fd
MN
1220
1221 return 28;
1222}
1223
ae628ec1 1224static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1225{
a2704c97 1226 int64_t pos = avio_tell(pb);
77eb5504 1227 avio_wb32(pb, 0); /* size */
0abdb293 1228 ffio_wfourcc(pb, "stbl");
6e6d6dc0
MN
1229 mov_write_stsd_tag(pb, track);
1230 mov_write_stts_tag(pb, track);
e977af6f
MS
1231 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1232 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
b7d3dd90 1233 track->has_keyframes && track->has_keyframes < track->entry)
e1316b19
BC
1234 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1235 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1236 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
72415b2a 1237 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
e1316b19 1238 track->flags & MOV_TRACK_CTTS)
b4712e3c 1239 mov_write_ctts_tag(pb, track);
6e6d6dc0
MN
1240 mov_write_stsc_tag(pb, track);
1241 mov_write_stsz_tag(pb, track);
1242 mov_write_stco_tag(pb, track);
b7d3dd90 1243 return update_size(pb, pos);
1cb5f7fd
MN
1244}
1245
ae628ec1 1246static int mov_write_dinf_tag(AVIOContext *pb)
1cb5f7fd 1247{
a2704c97 1248 int64_t pos = avio_tell(pb);
77eb5504 1249 avio_wb32(pb, 0); /* size */
0abdb293 1250 ffio_wfourcc(pb, "dinf");
6e6d6dc0 1251 mov_write_dref_tag(pb);
b7d3dd90 1252 return update_size(pb, pos);
1cb5f7fd
MN
1253}
1254
ae628ec1 1255static int mov_write_nmhd_tag(AVIOContext *pb)
f6204886 1256{
77eb5504 1257 avio_wb32(pb, 12);
0abdb293 1258 ffio_wfourcc(pb, "nmhd");
77eb5504 1259 avio_wb32(pb, 0);
f6204886
DC
1260 return 12;
1261}
1262
ae628ec1 1263static int mov_write_gmhd_tag(AVIOContext *pb)
f6204886 1264{
77eb5504 1265 avio_wb32(pb, 0x20); /* size */
0abdb293 1266 ffio_wfourcc(pb, "gmhd");
77eb5504 1267 avio_wb32(pb, 0x18); /* gmin size */
0abdb293 1268 ffio_wfourcc(pb, "gmin");/* generic media info */
77eb5504
AK
1269 avio_wb32(pb, 0); /* version & flags */
1270 avio_wb16(pb, 0x40); /* graphics mode = */
1271 avio_wb16(pb, 0x8000); /* opColor (r?) */
1272 avio_wb16(pb, 0x8000); /* opColor (g?) */
1273 avio_wb16(pb, 0x8000); /* opColor (b?) */
1274 avio_wb16(pb, 0); /* balance */
1275 avio_wb16(pb, 0); /* reserved */
f6204886
DC
1276 return 0x20;
1277}
1278
ae628ec1 1279static int mov_write_smhd_tag(AVIOContext *pb)
1cb5f7fd 1280{
77eb5504 1281 avio_wb32(pb, 16); /* size */
0abdb293 1282 ffio_wfourcc(pb, "smhd");
77eb5504
AK
1283 avio_wb32(pb, 0); /* version & flags */
1284 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1285 avio_wb16(pb, 0); /* reserved */
1cb5f7fd
MN
1286 return 16;
1287}
1288
ae628ec1 1289static int mov_write_vmhd_tag(AVIOContext *pb)
1cb5f7fd 1290{
77eb5504 1291 avio_wb32(pb, 0x14); /* size (always 0x14) */
0abdb293 1292 ffio_wfourcc(pb, "vmhd");
77eb5504
AK
1293 avio_wb32(pb, 0x01); /* version & flags */
1294 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1cb5f7fd
MN
1295 return 0x14;
1296}
1297
99e22b78
MG
1298static int is_clcp_track(MOVTrack *track)
1299{
1300 return track->tag == MKTAG('c','7','0','8') ||
1301 track->tag == MKTAG('c','6','0','8');
1302}
1303
ae628ec1 1304static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1305{
985688b8 1306 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
a2704c97 1307 int64_t pos = avio_tell(pb);
115329f1 1308
67400f6b
LB
1309 hdlr = "dhlr";
1310 hdlr_type = "url ";
1311 descr = "DataHandler";
1312
1313 if (track) {
bb270c08 1314 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
72415b2a 1315 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
bb270c08 1316 hdlr_type = "vide";
21732063 1317 descr = "VideoHandler";
72415b2a 1318 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
bb270c08 1319 hdlr_type = "soun";
21732063 1320 descr = "SoundHandler";
72415b2a 1321 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
f94371b1
MN
1322 if (track->tag == MKTAG('t','x','3','g')) {
1323 hdlr_type = "sbtl";
1324 } else if (track->tag == MKTAG('m','p','4','s')) {
1325 hdlr_type = "subp";
99e22b78
MG
1326 } else if (is_clcp_track(track)) {
1327 hdlr_type = "clcp";
f94371b1
MN
1328 } else {
1329 hdlr_type = "text";
1330 }
f6204886 1331 descr = "SubtitleHandler";
e977af6f
MS
1332 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1333 hdlr_type = "hint";
21732063 1334 descr = "HintHandler";
67400f6b
LB
1335 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1336 hdlr_type = "tmcd";
1337 descr = "TimeCodeHandler";
1338 } else {
1339 char tag_buf[32];
1340 av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1341 track->enc->codec_tag);
1342
1343 av_log(track->enc, AV_LOG_WARNING,
1344 "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1345 tag_buf, track->enc->codec_tag);
bb270c08 1346 }
9a4d9388 1347 }
115329f1 1348
77eb5504 1349 avio_wb32(pb, 0); /* size */
0abdb293 1350 ffio_wfourcc(pb, "hdlr");
77eb5504
AK
1351 avio_wb32(pb, 0); /* Version & flags */
1352 avio_write(pb, hdlr, 4); /* handler */
0abdb293 1353 ffio_wfourcc(pb, hdlr_type); /* handler type */
21732063
MS
1354 avio_wb32(pb, 0); /* reserved */
1355 avio_wb32(pb, 0); /* reserved */
1356 avio_wb32(pb, 0); /* reserved */
606d48ce 1357 if (!track || track->mode == MODE_MOV)
77eb5504
AK
1358 avio_w8(pb, strlen(descr)); /* pascal string */
1359 avio_write(pb, descr, strlen(descr)); /* handler description */
606d48ce 1360 if (track && track->mode != MODE_MOV)
77eb5504 1361 avio_w8(pb, 0); /* c string */
b7d3dd90 1362 return update_size(pb, pos);
9a4d9388
RS
1363}
1364
ae628ec1 1365static int mov_write_hmhd_tag(AVIOContext *pb)
e977af6f
MS
1366{
1367 /* This atom must be present, but leaving the values at zero
1368 * seems harmless. */
77eb5504 1369 avio_wb32(pb, 28); /* size */
0abdb293 1370 ffio_wfourcc(pb, "hmhd");
77eb5504
AK
1371 avio_wb32(pb, 0); /* version, flags */
1372 avio_wb16(pb, 0); /* maxPDUsize */
1373 avio_wb16(pb, 0); /* avgPDUsize */
1374 avio_wb32(pb, 0); /* maxbitrate */
1375 avio_wb32(pb, 0); /* avgbitrate */
1376 avio_wb32(pb, 0); /* reserved */
e977af6f
MS
1377 return 28;
1378}
1379
ae628ec1 1380static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
9a4d9388 1381{
a2704c97 1382 int64_t pos = avio_tell(pb);
77eb5504 1383 avio_wb32(pb, 0); /* size */
0abdb293 1384 ffio_wfourcc(pb, "minf");
21732063 1385 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
9a4d9388 1386 mov_write_vmhd_tag(pb);
72415b2a 1387 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
9a4d9388 1388 mov_write_smhd_tag(pb);
72415b2a 1389 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
99e22b78
MG
1390 if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
1391 mov_write_gmhd_tag(pb);
1392 } else {
1393 mov_write_nmhd_tag(pb);
1394 }
e977af6f
MS
1395 } else if (track->tag == MKTAG('r','t','p',' ')) {
1396 mov_write_hmhd_tag(pb);
b89e8759
LB
1397 } else if (track->tag == MKTAG('t','m','c','d')) {
1398 mov_write_gmhd_tag(pb);
f6204886 1399 }
9a4d9388
RS
1400 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1401 mov_write_hdlr_tag(pb, NULL);
1402 mov_write_dinf_tag(pb);
1403 mov_write_stbl_tag(pb, track);
b7d3dd90 1404 return update_size(pb, pos);
1cb5f7fd
MN
1405}
1406
ae628ec1 1407static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1408{
b7d3dd90 1409 int version = track->track_duration < INT32_MAX ? 0 : 1;
a3a80ddc 1410
53026339
MS
1411 if (track->mode == MODE_ISM)
1412 version = 1;
1413
77eb5504 1414 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
0abdb293 1415 ffio_wfourcc(pb, "mdhd");
77eb5504
AK
1416 avio_w8(pb, version);
1417 avio_wb24(pb, 0); /* flags */
a3a80ddc 1418 if (version == 1) {
77eb5504
AK
1419 avio_wb64(pb, track->time);
1420 avio_wb64(pb, track->time);
a3a80ddc 1421 } else {
77eb5504
AK
1422 avio_wb32(pb, track->time); /* creation time */
1423 avio_wb32(pb, track->time); /* modification time */
a3a80ddc 1424 }
77eb5504 1425 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
99a357f4
MS
1426 if (!track->entry)
1427 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1428 else
1429 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
77eb5504
AK
1430 avio_wb16(pb, track->language); /* language */
1431 avio_wb16(pb, 0); /* reserved (quality) */
db8f4a92 1432
21732063 1433 if (version != 0 && track->mode == MODE_MOV) {
db8f4a92 1434 av_log(NULL, AV_LOG_ERROR,
21732063
MS
1435 "FATAL error, file duration too long for timebase, this file will not be\n"
1436 "playable with quicktime. Choose a different timebase or a different\n"
1437 "container format\n");
db8f4a92
MN
1438 }
1439
1cb5f7fd
MN
1440 return 32;
1441}
1442
ae628ec1 1443static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1444{
a2704c97 1445 int64_t pos = avio_tell(pb);
77eb5504 1446 avio_wb32(pb, 0); /* size */
0abdb293 1447 ffio_wfourcc(pb, "mdia");
6e6d6dc0
MN
1448 mov_write_mdhd_tag(pb, track);
1449 mov_write_hdlr_tag(pb, track);
1450 mov_write_minf_tag(pb, track);
b7d3dd90 1451 return update_size(pb, pos);
1cb5f7fd
MN
1452}
1453
ae628ec1 1454static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 1455{
b7d3dd90 1456 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
729ef3ba 1457 track->timescale, AV_ROUND_UP);
a3a80ddc
BC
1458 int version = duration < INT32_MAX ? 0 : 1;
1459
53026339
MS
1460 if (track->mode == MODE_ISM)
1461 version = 1;
1462
77eb5504 1463 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
0abdb293 1464 ffio_wfourcc(pb, "tkhd");
77eb5504 1465 avio_w8(pb, version);
30ce2890
JS
1466 avio_wb24(pb, (track->flags & MOV_TRACK_ENABLED) ?
1467 MOV_TKHD_FLAG_ENABLED | MOV_TKHD_FLAG_IN_MOVIE :
1468 MOV_TKHD_FLAG_IN_MOVIE);
a3a80ddc 1469 if (version == 1) {
77eb5504
AK
1470 avio_wb64(pb, track->time);
1471 avio_wb64(pb, track->time);
a3a80ddc 1472 } else {
77eb5504
AK
1473 avio_wb32(pb, track->time); /* creation time */
1474 avio_wb32(pb, track->time); /* modification time */
a3a80ddc 1475 }
b7d3dd90 1476 avio_wb32(pb, track->track_id); /* track-id */
77eb5504 1477 avio_wb32(pb, 0); /* reserved */
99a357f4
MS
1478 if (!track->entry)
1479 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1480 else
1481 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1cb5f7fd 1482
77eb5504
AK
1483 avio_wb32(pb, 0); /* reserved */
1484 avio_wb32(pb, 0); /* reserved */
7574cacb 1485 avio_wb16(pb, 0); /* layer */
c92a2a4e 1486 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1cb5f7fd 1487 /* Volume, only for audio */
21732063 1488 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 1489 avio_wb16(pb, 0x0100);
1cb5f7fd 1490 else
77eb5504
AK
1491 avio_wb16(pb, 0);
1492 avio_wb16(pb, 0); /* reserved */
1cb5f7fd
MN
1493
1494 /* Matrix structure */
77eb5504
AK
1495 avio_wb32(pb, 0x00010000); /* reserved */
1496 avio_wb32(pb, 0x0); /* reserved */
1497 avio_wb32(pb, 0x0); /* reserved */
1498 avio_wb32(pb, 0x0); /* reserved */
1499 avio_wb32(pb, 0x00010000); /* reserved */
1500 avio_wb32(pb, 0x0); /* reserved */
1501 avio_wb32(pb, 0x0); /* reserved */
1502 avio_wb32(pb, 0x0); /* reserved */
1503 avio_wb32(pb, 0x40000000); /* reserved */
1cb5f7fd
MN
1504
1505 /* Track width and height, for visual only */
21732063
MS
1506 if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1507 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1508 if (track->mode == MODE_MOV) {
ea1afa12 1509 avio_wb32(pb, track->enc->width << 16);
fffdee89 1510 avio_wb32(pb, track->height << 16);
ea1afa12 1511 } else {
77d207cb 1512 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
21732063 1513 if (!sample_aspect_ratio || track->height != track->enc->height)
77d207cb 1514 sample_aspect_ratio = 1;
21732063
MS
1515 avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1516 avio_wb32(pb, track->height * 0x10000);
ea1afa12 1517 }
21732063 1518 } else {
77eb5504
AK
1519 avio_wb32(pb, 0);
1520 avio_wb32(pb, 0);
1cb5f7fd
MN
1521 }
1522 return 0x5c;
1523}
1524
13ff92d1 1525static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
ea1afa12
MV
1526{
1527 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1528 track->enc->sample_aspect_ratio.den);
1529
a2704c97 1530 int64_t pos = avio_tell(pb);
ea1afa12
MV
1531
1532 avio_wb32(pb, 0); /* size */
13ff92d1 1533 ffio_wfourcc(pb, "tapt");
ea1afa12
MV
1534
1535 avio_wb32(pb, 20);
13ff92d1 1536 ffio_wfourcc(pb, "clef");
ea1afa12
MV
1537 avio_wb32(pb, 0);
1538 avio_wb32(pb, width << 16);
1539 avio_wb32(pb, track->enc->height << 16);
802385db
AS
1540
1541 avio_wb32(pb, 20);
1542 ffio_wfourcc(pb, "prof");
1543 avio_wb32(pb, 0);
1544 avio_wb32(pb, width << 16);
1545 avio_wb32(pb, track->enc->height << 16);
ea1afa12
MV
1546
1547 avio_wb32(pb, 20);
13ff92d1 1548 ffio_wfourcc(pb, "enof");
ea1afa12
MV
1549 avio_wb32(pb, 0);
1550 avio_wb32(pb, track->enc->width << 16);
1551 avio_wb32(pb, track->enc->height << 16);
1552
b7d3dd90 1553 return update_size(pb, pos);
373211d8 1554}
ea1afa12 1555
8af18154 1556// This box seems important for the psp playback ... without it the movie seems to hang
ae628ec1 1557static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
8af18154 1558{
b7d3dd90 1559 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
c1691948
GP
1560 track->timescale, AV_ROUND_UP);
1561 int version = duration < INT32_MAX ? 0 : 1;
1562 int entry_size, entry_count, size;
1563 int64_t delay, start_ct = track->cluster[0].cts;
1564 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1565 track->timescale, AV_ROUND_DOWN);
1566 version |= delay < INT32_MAX ? 0 : 1;
1567
1568 entry_size = (version == 1) ? 20 : 12;
1569 entry_count = 1 + (delay > 0);
1570 size = 24 + entry_count * entry_size;
1571
1572 /* write the atom data */
1573 avio_wb32(pb, size);
0abdb293 1574 ffio_wfourcc(pb, "edts");
c1691948 1575 avio_wb32(pb, size - 8);
0abdb293 1576 ffio_wfourcc(pb, "elst");
c1691948
GP
1577 avio_w8(pb, version);
1578 avio_wb24(pb, 0); /* flags */
8af18154 1579
c1691948
GP
1580 avio_wb32(pb, entry_count);
1581 if (delay > 0) { /* add an empty edit to delay presentation */
1582 if (version == 1) {
1583 avio_wb64(pb, delay);
1584 avio_wb64(pb, -1);
1585 } else {
1586 avio_wb32(pb, delay);
1587 avio_wb32(pb, -1);
1588 }
1589 avio_wb32(pb, 0x00010000);
1590 }
8af18154 1591
c1691948
GP
1592 /* duration */
1593 if (version == 1) {
1594 avio_wb64(pb, duration);
1595 avio_wb64(pb, start_ct);
1596 } else {
1597 avio_wb32(pb, duration);
1598 avio_wb32(pb, start_ct);
1599 }
77eb5504 1600 avio_wb32(pb, 0x00010000);
c1691948 1601 return size;
8af18154 1602}
1603
ae628ec1 1604static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
ddb63017 1605{
77eb5504 1606 avio_wb32(pb, 20); // size
0abdb293 1607 ffio_wfourcc(pb, "tref");
77eb5504
AK
1608 avio_wb32(pb, 12); // size (subatom)
1609 avio_wl32(pb, track->tref_tag);
1610 avio_wb32(pb, track->tref_id);
ddb63017
DC
1611 return 20;
1612}
1613
8af18154 1614// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
ae628ec1 1615static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
8af18154 1616{
77eb5504 1617 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
0abdb293
AK
1618 ffio_wfourcc(pb, "uuid");
1619 ffio_wfourcc(pb, "USMT");
77eb5504
AK
1620 avio_wb32(pb, 0x21d24fce);
1621 avio_wb32(pb, 0xbb88695c);
1622 avio_wb32(pb, 0xfac9c740);
1623 avio_wb32(pb, 0x1c); // another size here!
0abdb293 1624 ffio_wfourcc(pb, "MTDT");
77eb5504
AK
1625 avio_wb32(pb, 0x00010012);
1626 avio_wb32(pb, 0x0a);
1627 avio_wb32(pb, 0x55c40000);
1628 avio_wb32(pb, 0x1);
1629 avio_wb32(pb, 0x0);
8af18154 1630 return 0x34;
1631}
1632
8034130e 1633static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
e977af6f 1634{
8034130e 1635 AVFormatContext *ctx = track->rtp_ctx;
e977af6f
MS
1636 char buf[1000] = "";
1637 int len;
1638
8034130e
LB
1639 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1640 NULL, NULL, 0, 0, ctx);
1641 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
e977af6f
MS
1642 len = strlen(buf);
1643
77eb5504 1644 avio_wb32(pb, len + 24);
0abdb293 1645 ffio_wfourcc(pb, "udta");
77eb5504 1646 avio_wb32(pb, len + 16);
0abdb293 1647 ffio_wfourcc(pb, "hnti");
77eb5504 1648 avio_wb32(pb, len + 8);
0abdb293 1649 ffio_wfourcc(pb, "sdp ");
77eb5504 1650 avio_write(pb, buf, len);
e977af6f
MS
1651 return len + 24;
1652}
1653
83988d58
MS
1654static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1655 MOVTrack *track, AVStream *st)
1cb5f7fd 1656{
a2704c97 1657 int64_t pos = avio_tell(pb);
77eb5504 1658 avio_wb32(pb, 0); /* size */
0abdb293 1659 ffio_wfourcc(pb, "trak");
59729451 1660 mov_write_tkhd_tag(pb, track, st);
83988d58 1661 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
99e22b78
MG
1662 (track->entry && track->cluster[0].dts) ||
1663 is_clcp_track(track)) {
83988d58
MS
1664 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1665 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1666 }
ddb63017
DC
1667 if (track->tref_tag)
1668 mov_write_tref_tag(pb, track);
6e6d6dc0 1669 mov_write_mdia_tag(pb, track);
115329f1 1670 if (track->mode == MODE_PSP)
21732063 1671 mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
e977af6f 1672 if (track->tag == MKTAG('r','t','p',' '))
8034130e 1673 mov_write_udta_sdp(pb, track);
99e22b78
MG
1674 if (track->mode == MODE_MOV) {
1675 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1676 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1677 if ((0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)) {
1678 mov_write_tapt_tag(pb, track);
1679 }
1680 }
1681 if (is_clcp_track(track)) {
ea1afa12 1682 mov_write_tapt_tag(pb, track);
99e22b78 1683 }
21732063 1684 }
b7d3dd90 1685 return update_size(pb, pos);
1cb5f7fd
MN
1686}
1687
ae628ec1 1688static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1689{
d3b8bde2
AC
1690 int i, has_audio = 0, has_video = 0;
1691 int64_t pos = avio_tell(pb);
1692 int audio_profile = mov->iods_audio_profile;
1693 int video_profile = mov->iods_video_profile;
1694 for (i = 0; i < mov->nb_streams; i++) {
21732063 1695 if (mov->tracks[i].entry > 0) {
d3b8bde2
AC
1696 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1697 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1698 }
1699 }
1700 if (audio_profile < 0)
1701 audio_profile = 0xFF - has_audio;
1702 if (video_profile < 0)
1703 video_profile = 0xFF - has_video;
1704 avio_wb32(pb, 0x0); /* size */
0abdb293 1705 ffio_wfourcc(pb, "iods");
77eb5504 1706 avio_wb32(pb, 0); /* version & flags */
b7d3dd90 1707 put_descr(pb, 0x10, 7);
d3b8bde2
AC
1708 avio_wb16(pb, 0x004f);
1709 avio_w8(pb, 0xff);
1710 avio_w8(pb, 0xff);
1711 avio_w8(pb, audio_profile);
1712 avio_w8(pb, video_profile);
1713 avio_w8(pb, 0xff);
b7d3dd90 1714 return update_size(pb, pos);
1cb5f7fd
MN
1715}
1716
83988d58
MS
1717static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1718{
1719 avio_wb32(pb, 0x20); /* size */
1720 ffio_wfourcc(pb, "trex");
1721 avio_wb32(pb, 0); /* version & flags */
b7d3dd90 1722 avio_wb32(pb, track->track_id); /* track ID */
83988d58
MS
1723 avio_wb32(pb, 1); /* default sample description index */
1724 avio_wb32(pb, 0); /* default sample duration */
1725 avio_wb32(pb, 0); /* default sample size */
1726 avio_wb32(pb, 0); /* default sample flags */
1727 return 0;
1728}
1729
1730static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1731{
1732 int64_t pos = avio_tell(pb);
1733 int i;
1734 avio_wb32(pb, 0x0); /* size */
1735 ffio_wfourcc(pb, "mvex");
1736 for (i = 0; i < mov->nb_streams; i++)
1737 mov_write_trex_tag(pb, &mov->tracks[i]);
b7d3dd90 1738 return update_size(pb, pos);
83988d58
MS
1739}
1740
ae628ec1 1741static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1742{
b7d3dd90
MS
1743 int max_track_id = 1, i;
1744 int64_t max_track_len_temp, max_track_len = 0;
a3a80ddc 1745 int version;
1cb5f7fd 1746
21732063 1747 for (i = 0; i < mov->nb_streams; i++) {
22de0f83 1748 if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
b7d3dd90
MS
1749 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1750 MOV_TIMESCALE,
1751 mov->tracks[i].timescale,
1752 AV_ROUND_UP);
1753 if (max_track_len < max_track_len_temp)
1754 max_track_len = max_track_len_temp;
1755 if (max_track_id < mov->tracks[i].track_id)
1756 max_track_id = mov->tracks[i].track_id;
1cb5f7fd
MN
1757 }
1758 }
a3a80ddc 1759
b7d3dd90 1760 version = max_track_len < UINT32_MAX ? 0 : 1;
77eb5504 1761 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
0abdb293 1762 ffio_wfourcc(pb, "mvhd");
77eb5504
AK
1763 avio_w8(pb, version);
1764 avio_wb24(pb, 0); /* flags */
a3a80ddc 1765 if (version == 1) {
77eb5504
AK
1766 avio_wb64(pb, mov->time);
1767 avio_wb64(pb, mov->time);
a3a80ddc 1768 } else {
77eb5504
AK
1769 avio_wb32(pb, mov->time); /* creation time */
1770 avio_wb32(pb, mov->time); /* modification time */
a3a80ddc 1771 }
77eb5504 1772 avio_wb32(pb, MOV_TIMESCALE);
b7d3dd90 1773 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1cb5f7fd 1774
77eb5504
AK
1775 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1776 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1777 avio_wb16(pb, 0); /* reserved */
1778 avio_wb32(pb, 0); /* reserved */
1779 avio_wb32(pb, 0); /* reserved */
1cb5f7fd
MN
1780
1781 /* Matrix structure */
77eb5504
AK
1782 avio_wb32(pb, 0x00010000); /* reserved */
1783 avio_wb32(pb, 0x0); /* reserved */
1784 avio_wb32(pb, 0x0); /* reserved */
1785 avio_wb32(pb, 0x0); /* reserved */
1786 avio_wb32(pb, 0x00010000); /* reserved */
1787 avio_wb32(pb, 0x0); /* reserved */
1788 avio_wb32(pb, 0x0); /* reserved */
1789 avio_wb32(pb, 0x0); /* reserved */
1790 avio_wb32(pb, 0x40000000); /* reserved */
1791
1792 avio_wb32(pb, 0); /* reserved (preview time) */
1793 avio_wb32(pb, 0); /* reserved (preview duration) */
1794 avio_wb32(pb, 0); /* reserved (poster time) */
1795 avio_wb32(pb, 0); /* reserved (selection time) */
1796 avio_wb32(pb, 0); /* reserved (selection duration) */
1797 avio_wb32(pb, 0); /* reserved (current time) */
b7d3dd90 1798 avio_wb32(pb, max_track_id + 1); /* Next track id */
1cb5f7fd
MN
1799 return 0x6c;
1800}
1801
ae628ec1 1802static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1803 AVFormatContext *s)
1804{
77eb5504 1805 avio_wb32(pb, 33); /* size */
0abdb293 1806 ffio_wfourcc(pb, "hdlr");
77eb5504
AK
1807 avio_wb32(pb, 0);
1808 avio_wb32(pb, 0);
0abdb293
AK
1809 ffio_wfourcc(pb, "mdir");
1810 ffio_wfourcc(pb, "appl");
77eb5504
AK
1811 avio_wb32(pb, 0);
1812 avio_wb32(pb, 0);
1813 avio_w8(pb, 0);
6b174197 1814 return 33;
b6c50eb1
PB
1815}
1816
1817/* helper function to write a data tag with the specified string as data */
ae628ec1 1818static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
b6c50eb1 1819{
21732063 1820 if (long_style) {
6b174197 1821 int size = 16 + strlen(data);
77eb5504 1822 avio_wb32(pb, size); /* size */
0abdb293 1823 ffio_wfourcc(pb, "data");
77eb5504
AK
1824 avio_wb32(pb, 1);
1825 avio_wb32(pb, 0);
1826 avio_write(pb, data, strlen(data));
6b174197 1827 return size;
21732063 1828 } else {
fe3ab8ad
BC
1829 if (!lang)
1830 lang = ff_mov_iso639_to_lang("und", 1);
77eb5504
AK
1831 avio_wb16(pb, strlen(data)); /* string length */
1832 avio_wb16(pb, lang);
1833 avio_write(pb, data, strlen(data));
efda3395 1834 return strlen(data) + 4;
b6c50eb1 1835 }
b6c50eb1
PB
1836}
1837
21732063
MS
1838static int mov_write_string_tag(AVIOContext *pb, const char *name,
1839 const char *value, int lang, int long_style)
1840{
b6c50eb1 1841 int size = 0;
91208916 1842 if (value && value[0]) {
a2704c97 1843 int64_t pos = avio_tell(pb);
77eb5504 1844 avio_wb32(pb, 0); /* size */
0abdb293 1845 ffio_wfourcc(pb, name);
d9fc9ff3 1846 mov_write_string_data_tag(pb, value, lang, long_style);
b7d3dd90 1847 size = update_size(pb, pos);
b6c50eb1
PB
1848 }
1849 return size;
1850}
1851
ae628ec1 1852static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1ee2d448
AJ
1853 const char *name, const char *tag,
1854 int long_style)
b6c50eb1 1855{
d9fc9ff3 1856 int l, lang = 0, len, len2;
d2d67e42 1857 AVDictionaryEntry *t, *t2 = NULL;
d9fc9ff3 1858 char tag2[16];
1ee2d448 1859
d2d67e42 1860 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
efda3395 1861 return 0;
1ee2d448 1862
d9fc9ff3
AJ
1863 len = strlen(t->key);
1864 snprintf(tag2, sizeof(tag2), "%s-", tag);
d2d67e42 1865 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
d9fc9ff3 1866 len2 = strlen(t2->key);
21732063
MS
1867 if (len2 == len + 4 && !strcmp(t->value, t2->value)
1868 && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
d9fc9ff3
AJ
1869 lang = l;
1870 break;
1871 }
1872 }
1873 return mov_write_string_tag(pb, name, t->value, lang, long_style);
b6c50eb1
PB
1874}
1875
1876/* iTunes track number */
ae628ec1 1877static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1878 AVFormatContext *s)
1879{
d2d67e42 1880 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1ee2d448
AJ
1881 int size = 0, track = t ? atoi(t->value) : 0;
1882 if (track) {
77eb5504 1883 avio_wb32(pb, 32); /* size */
0abdb293 1884 ffio_wfourcc(pb, "trkn");
21732063
MS
1885 avio_wb32(pb, 24); /* size */
1886 ffio_wfourcc(pb, "data");
1887 avio_wb32(pb, 0); // 8 bytes empty
1888 avio_wb32(pb, 0);
1889 avio_wb16(pb, 0); // empty
1890 avio_wb16(pb, track); // track number
1891 avio_wb16(pb, 0); // total track number
1892 avio_wb16(pb, 0); // empty
6b174197 1893 size = 32;
b6c50eb1
PB
1894 }
1895 return size;
1896}
1897
1898/* iTunes meta data list */
ae628ec1 1899static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1900 AVFormatContext *s)
1901{
a2704c97 1902 int64_t pos = avio_tell(pb);
77eb5504 1903 avio_wb32(pb, 0); /* size */
0abdb293 1904 ffio_wfourcc(pb, "ilst");
1ee2d448 1905 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
ea4c4d7f 1906 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
2cf9c7ef 1907 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
620af1a1 1908 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1ee2d448 1909 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
ca76a119 1910 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
565e0c6d
JS
1911 if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
1912 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1ee2d448
AJ
1913 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1914 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1915 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2cf9c7ef
MM
1916 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1917 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
7382902b
DC
1918 mov_write_string_metadata(s, pb, "desc", "description",1);
1919 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1920 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1921 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1922 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
b6c50eb1 1923 mov_write_trkn_tag(pb, mov, s);
b7d3dd90 1924 return update_size(pb, pos);
b6c50eb1
PB
1925}
1926
1927/* iTunes meta data tag */
ae628ec1 1928static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1929 AVFormatContext *s)
1930{
1931 int size = 0;
a2704c97 1932 int64_t pos = avio_tell(pb);
77eb5504 1933 avio_wb32(pb, 0); /* size */
0abdb293 1934 ffio_wfourcc(pb, "meta");
77eb5504 1935 avio_wb32(pb, 0);
b97fb809
AJ
1936 mov_write_itunes_hdlr_tag(pb, mov, s);
1937 mov_write_ilst_tag(pb, mov, s);
b7d3dd90 1938 size = update_size(pb, pos);
b6c50eb1
PB
1939 return size;
1940}
115329f1 1941
07b7b06d
BC
1942static int utf8len(const uint8_t *b)
1943{
21732063 1944 int len = 0;
07b7b06d 1945 int val;
21732063 1946 while (*b) {
07b7b06d
BC
1947 GET_UTF8(val, *b++, return -1;)
1948 len++;
1949 }
1950 return len;
1951}
1952
ae628ec1 1953static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
07b7b06d
BC
1954{
1955 int val;
21732063 1956 while (*b) {
07b7b06d 1957 GET_UTF8(val, *b++, return -1;)
77eb5504 1958 avio_wb16(pb, val);
07b7b06d 1959 }
77eb5504 1960 avio_wb16(pb, 0x00);
07b7b06d
BC
1961 return 0;
1962}
1963
1964static uint16_t language_code(const char *str)
1965{
21732063
MS
1966 return (((str[0] - 0x60) & 0x1F) << 10) +
1967 (((str[1] - 0x60) & 0x1F) << 5) +
1968 (( str[2] - 0x60) & 0x1F);
07b7b06d
BC
1969}
1970
ae628ec1 1971static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
c6e2c6c9
BC
1972 const char *tag, const char *str)
1973{
a2704c97 1974 int64_t pos = avio_tell(pb);
d2d67e42 1975 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1ee2d448 1976 if (!t || !utf8len(t->value))
c6e2c6c9 1977 return 0;
77eb5504 1978 avio_wb32(pb, 0); /* size */
0abdb293 1979 ffio_wfourcc(pb, tag); /* type */
77eb5504 1980 avio_wb32(pb, 0); /* version + flags */
c6e2c6c9 1981 if (!strcmp(tag, "yrrc"))
77eb5504 1982 avio_wb16(pb, atoi(t->value));
c6e2c6c9 1983 else {
77eb5504 1984 avio_wb16(pb, language_code("eng")); /* language */
21732063 1985 avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
1ee2d448 1986 if (!strcmp(tag, "albm") &&
d2d67e42 1987 (t = av_dict_get(s->metadata, "track", NULL, 0)))
77eb5504 1988 avio_w8(pb, atoi(t->value));
c6e2c6c9 1989 }
b7d3dd90 1990 return update_size(pb, pos);
c6e2c6c9
BC
1991}
1992
ae628ec1 1993static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
dc75e4e3 1994{
a2704c97 1995 int64_t pos = avio_tell(pb);
dc75e4e3
DC
1996 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1997
77eb5504 1998 avio_wb32(pb, 0); // size
0abdb293 1999 ffio_wfourcc(pb, "chpl");
77eb5504
AK
2000 avio_wb32(pb, 0x01000000); // version + flags
2001 avio_wb32(pb, 0); // unknown
2002 avio_w8(pb, nb_chapters);
dc75e4e3
DC
2003
2004 for (i = 0; i < nb_chapters; i++) {
2005 AVChapter *c = s->chapters[i];
d2d67e42 2006 AVDictionaryEntry *t;
77eb5504 2007 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
dc75e4e3 2008
d2d67e42 2009 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
dc75e4e3 2010 int len = FFMIN(strlen(t->value), 255);
77eb5504
AK
2011 avio_w8(pb, len);
2012 avio_write(pb, t->value, len);
dc75e4e3 2013 } else
77eb5504 2014 avio_w8(pb, 0);
dc75e4e3 2015 }
b7d3dd90 2016 return update_size(pb, pos);
dc75e4e3
DC
2017}
2018
ae628ec1 2019static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
69dde1ad
GB
2020 AVFormatContext *s)
2021{
ae628ec1 2022 AVIOContext *pb_buf;
1ee2d448
AJ
2023 int i, ret, size;
2024 uint8_t *buf;
89a0d790 2025
d1d87dfb 2026 for (i = 0; i < s->nb_streams; i++)
0c1959b0 2027 if (s->flags & AVFMT_FLAG_BITEXACT) {
1ee2d448 2028 return 0;
d1d87dfb
BC
2029 }
2030
b92c5452 2031 ret = avio_open_dyn_buf(&pb_buf);
21732063 2032 if (ret < 0)
1ee2d448 2033 return ret;
69dde1ad 2034
21732063
MS
2035 if (mov->mode & MODE_3GP) {
2036 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2037 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2038 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2039 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2040 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2041 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2042 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2043 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2044 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2045 mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
2046 mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
2047 mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
2048 mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
2049 mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
2050 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
2051 mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
2052 mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
2053 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
2054 } else {
2055 /* iTunes meta data */
2056 mov_write_meta_tag(pb_buf, mov, s);
2057 }
1ee2d448 2058
21732063
MS
2059 if (s->nb_chapters)
2060 mov_write_chpl_tag(pb_buf, s);
dc75e4e3 2061
6dc7d80d 2062 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
21732063 2063 avio_wb32(pb, size + 8);
0abdb293 2064 ffio_wfourcc(pb, "udta");
77eb5504 2065 avio_write(pb, buf, size);
89a0d790 2066 }
8b6e0aec 2067 av_free(buf);
89a0d790
BF
2068
2069 return 0;
69dde1ad
GB
2070}
2071
ae628ec1 2072static void mov_write_psp_udta_tag(AVIOContext *pb,
21732063 2073 const char *str, const char *lang, int type)
20e22af8 2074{
21732063
MS
2075 int len = utf8len(str) + 1;
2076 if (len <= 0)
d32d6def 2077 return;
21732063 2078 avio_wb16(pb, len * 2 + 10); /* size */
77eb5504
AK
2079 avio_wb32(pb, type); /* type */
2080 avio_wb16(pb, language_code(lang)); /* language */
2081 avio_wb16(pb, 0x01); /* ? */
20e22af8 2082 ascii_to_wc(pb, str);
20e22af8
BC
2083}
2084
ae628ec1 2085static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
dcfdb046 2086{
d2d67e42 2087 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
bc5c918e 2088 int64_t pos, pos2;
dcfdb046 2089
1ee2d448 2090 if (title) {
a2704c97 2091 pos = avio_tell(pb);
77eb5504 2092 avio_wb32(pb, 0); /* size placeholder*/
0abdb293
AK
2093 ffio_wfourcc(pb, "uuid");
2094 ffio_wfourcc(pb, "USMT");
77eb5504
AK
2095 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2096 avio_wb32(pb, 0xbb88695c);
2097 avio_wb32(pb, 0xfac9c740);
dcfdb046 2098
a2704c97 2099 pos2 = avio_tell(pb);
77eb5504 2100 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 2101 ffio_wfourcc(pb, "MTDT");
77eb5504 2102 avio_wb16(pb, 4);
dcfdb046 2103
7cf0e16f 2104 // ?
77eb5504
AK
2105 avio_wb16(pb, 0x0C); /* size */
2106 avio_wb32(pb, 0x0B); /* type */
2107 avio_wb16(pb, language_code("und")); /* language */
2108 avio_wb16(pb, 0x0); /* ? */
2109 avio_wb16(pb, 0x021C); /* data */
7cf0e16f 2110
d32d6def 2111 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1ee2d448 2112 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
d32d6def
BC
2113 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2114
b7d3dd90
MS
2115 update_size(pb, pos2);
2116 return update_size(pb, pos);
dcfdb046
BL
2117 }
2118
d32d6def 2119 return 0;
dcfdb046
BL
2120}
2121
ae628ec1 2122static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
69dde1ad 2123 AVFormatContext *s)
1cb5f7fd 2124{
b29af723 2125 int i;
a2704c97 2126 int64_t pos = avio_tell(pb);
77eb5504 2127 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 2128 ffio_wfourcc(pb, "moov");
1cb5f7fd 2129
21732063 2130 for (i = 0; i < mov->nb_streams; i++) {
83988d58
MS
2131 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2132 continue;
e45ccf79 2133
21732063
MS
2134 mov->tracks[i].time = mov->time;
2135 mov->tracks[i].track_id = i + 1;
1cb5f7fd
MN
2136 }
2137
ddb63017 2138 if (mov->chapter_track)
21732063 2139 for (i = 0; i < s->nb_streams; i++) {
ddb63017 2140 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
21732063 2141 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
ddb63017 2142 }
e977af6f
MS
2143 for (i = 0; i < mov->nb_streams; i++) {
2144 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2145 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2146 mov->tracks[i].tref_id =
b7d3dd90 2147 mov->tracks[mov->tracks[i].src_track].track_id;
e977af6f
MS
2148 }
2149 }
ddb63017 2150
6e6d6dc0 2151 mov_write_mvhd_tag(pb, mov);
d3b8bde2
AC
2152 if (mov->mode != MODE_MOV && !mov->iods_skip)
2153 mov_write_iods_tag(pb, mov);
21732063 2154 for (i = 0; i < mov->nb_streams; i++) {
83988d58
MS
2155 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2156 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1cb5f7fd
MN
2157 }
2158 }
83988d58
MS
2159 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2160 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
1cb5f7fd 2161
dcfdb046
BL
2162 if (mov->mode == MODE_PSP)
2163 mov_write_uuidusmt_tag(pb, s);
c6e2c6c9 2164 else
e344c1ea 2165 mov_write_udta_tag(pb, mov, s);
69dde1ad 2166
b7d3dd90 2167 return update_size(pb, pos);
1cb5f7fd
MN
2168}
2169
f5322104
MS
2170static void param_write_int(AVIOContext *pb, const char *name, int value)
2171{
2172 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2173}
2174
2175static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2176{
2177 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2178}
2179
2180static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2181{
2182 char buf[150];
21732063 2183 len = FFMIN(sizeof(buf) / 2 - 1, len);
f5322104 2184 ff_data_to_hex(buf, value, len, 0);
21732063 2185 buf[2 * len] = '\0';
f5322104
MS
2186 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2187}
2188
f5322104
MS
2189static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2190{
2191 int64_t pos = avio_tell(pb);
2192 int i;
2193 const uint8_t uuid[] = {
2194 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2195 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2196 };
2197
2198 avio_wb32(pb, 0);
2199 ffio_wfourcc(pb, "uuid");
2200 avio_write(pb, uuid, sizeof(uuid));
2201 avio_wb32(pb, 0);
2202
2203 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2204 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2205 avio_printf(pb, "<head>\n");
2206 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2207 LIBAVFORMAT_IDENT);
2208 avio_printf(pb, "</head>\n");
2209 avio_printf(pb, "<body>\n");
2210 avio_printf(pb, "<switch>\n");
2211 for (i = 0; i < mov->nb_streams; i++) {
2212 MOVTrack *track = &mov->tracks[i];
2213 const char *type;
b7d3dd90 2214 /* track->track_id is initialized in write_moov, and thus isn't known
f5322104
MS
2215 * here yet */
2216 int track_id = i + 1;
2217
2218 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2219 type = "video";
2220 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2221 type = "audio";
2222 } else {
2223 continue;
2224 }
2225 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2226 track->enc->bit_rate);
2227 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2228 param_write_int(pb, "trackID", track_id);
2229 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
36ef5369 2230 if (track->enc->codec_id == AV_CODEC_ID_H264) {
d5ed5e7d
MS
2231 uint8_t *ptr;
2232 int size = track->enc->extradata_size;
2233 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2234 &size)) {
2235 param_write_hex(pb, "CodecPrivateData",
2236 ptr ? ptr : track->enc->extradata,
2237 size);
2238 av_free(ptr);
2239 }
f5322104 2240 param_write_string(pb, "FourCC", "H264");
36ef5369 2241 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
f5322104 2242 param_write_string(pb, "FourCC", "WVC1");
68893afe
MS
2243 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2244 track->enc->extradata_size);
f5322104
MS
2245 }
2246 param_write_int(pb, "MaxWidth", track->enc->width);
2247 param_write_int(pb, "MaxHeight", track->enc->height);
2248 param_write_int(pb, "DisplayWidth", track->enc->width);
2249 param_write_int(pb, "DisplayHeight", track->enc->height);
2250 } else {
36ef5369 2251 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
f5322104 2252 param_write_string(pb, "FourCC", "AACL");
36ef5369 2253 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
f5322104
MS
2254 param_write_string(pb, "FourCC", "WMAP");
2255 }
2256 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2257 track->enc->extradata_size);
2258 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
21732063 2259 track->enc->codec_id));
f5322104
MS
2260 param_write_int(pb, "Channels", track->enc->channels);
2261 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2262 param_write_int(pb, "BitsPerSample", 16);
2263 param_write_int(pb, "PacketSize", track->enc->block_align ?
2264 track->enc->block_align : 4);
2265 }
2266 avio_printf(pb, "</%s>\n", type);
2267 }
2268 avio_printf(pb, "</switch>\n");
2269 avio_printf(pb, "</body>\n");
2270 avio_printf(pb, "</smil>\n");
2271
b7d3dd90 2272 return update_size(pb, pos);
f5322104
MS
2273}
2274
83988d58
MS
2275static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2276{
2277 avio_wb32(pb, 16);
2278 ffio_wfourcc(pb, "mfhd");
2279 avio_wb32(pb, 0);
2280 avio_wb32(pb, mov->fragments);
2281 return 0;
2282}
2283
e7bf085b
MS
2284static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov,
2285 MOVTrack *track, int64_t moof_offset)
83988d58
MS
2286{
2287 int64_t pos = avio_tell(pb);
73328f24
MS
2288 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2289 MOV_TFHD_BASE_DATA_OFFSET;
83988d58 2290 if (!track->entry) {
73328f24 2291 flags |= MOV_TFHD_DURATION_IS_EMPTY;
83988d58 2292 } else {
73328f24 2293 flags |= MOV_TFHD_DEFAULT_FLAGS;
83988d58 2294 }
e7bf085b
MS
2295 if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET)
2296 flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
83988d58 2297
51df7b23
MS
2298 /* Don't set a default sample size, the silverlight player refuses
2299 * to play files with that set. Don't set a default sample duration,
40bbbce7
JH
2300 * WMP freaks out if it is set. Don't set a base data offset, PIFF
2301 * file format says it MUST NOT be set. */
4ddd54da 2302 if (track->mode == MODE_ISM)
40bbbce7
JH
2303 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2304 MOV_TFHD_BASE_DATA_OFFSET);
4ddd54da 2305
83988d58
MS
2306 avio_wb32(pb, 0); /* size placeholder */
2307 ffio_wfourcc(pb, "tfhd");
2308 avio_w8(pb, 0); /* version */
2309 avio_wb24(pb, flags);
2310
b7d3dd90 2311 avio_wb32(pb, track->track_id); /* track-id */
73328f24 2312 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
83988d58 2313 avio_wb64(pb, moof_offset);
73328f24 2314 if (flags & MOV_TFHD_DEFAULT_DURATION) {
f3dab5fb 2315 track->default_duration = get_cluster_duration(track, 0);
83988d58
MS
2316 avio_wb32(pb, track->default_duration);
2317 }
73328f24 2318 if (flags & MOV_TFHD_DEFAULT_SIZE) {
83988d58
MS
2319 track->default_size = track->entry ? track->cluster[0].size : 1;
2320 avio_wb32(pb, track->default_size);
2321 } else
2322 track->default_size = -1;
2323
73328f24 2324 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
83988d58
MS
2325 track->default_sample_flags =
2326 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
5be805d3
MS
2327 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2328 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
83988d58
MS
2329 avio_wb32(pb, track->default_sample_flags);
2330 }
2331
b7d3dd90 2332 return update_size(pb, pos);
83988d58
MS
2333}
2334
2335static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2336{
5be805d3
MS
2337 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2338 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
83988d58
MS
2339}
2340
e7bf085b
MS
2341static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov,
2342 MOVTrack *track, int moof_size)
83988d58
MS
2343{
2344 int64_t pos = avio_tell(pb);
3eec23f3 2345 uint32_t flags = MOV_TRUN_DATA_OFFSET;
83988d58
MS
2346 int i;
2347
2348 for (i = 0; i < track->entry; i++) {
681d1726 2349 if (get_cluster_duration(track, i) != track->default_duration)
3eec23f3 2350 flags |= MOV_TRUN_SAMPLE_DURATION;
83988d58 2351 if (track->cluster[i].size != track->default_size)
3eec23f3 2352 flags |= MOV_TRUN_SAMPLE_SIZE;
83988d58 2353 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
3eec23f3 2354 flags |= MOV_TRUN_SAMPLE_FLAGS;
83988d58 2355 }
3eec23f3
MS
2356 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2357 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
83988d58 2358 if (track->flags & MOV_TRACK_CTTS)
3eec23f3 2359 flags |= MOV_TRUN_SAMPLE_CTS;
83988d58
MS
2360
2361 avio_wb32(pb, 0); /* size placeholder */
2362 ffio_wfourcc(pb, "trun");
2363 avio_w8(pb, 0); /* version */
2364 avio_wb24(pb, flags);
2365
2366 avio_wb32(pb, track->entry); /* sample count */
e7bf085b
MS
2367 if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
2368 !(mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) &&
2369 track->track_id != 1)
2370 avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
2371 else
2372 avio_wb32(pb, moof_size + 8 + track->data_offset +
2373 track->cluster[0].pos); /* data offset */
3eec23f3 2374 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
83988d58
MS
2375 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2376
2377 for (i = 0; i < track->entry; i++) {
3eec23f3 2378 if (flags & MOV_TRUN_SAMPLE_DURATION)
681d1726 2379 avio_wb32(pb, get_cluster_duration(track, i));
3eec23f3 2380 if (flags & MOV_TRUN_SAMPLE_SIZE)
83988d58 2381 avio_wb32(pb, track->cluster[i].size);
3eec23f3 2382 if (flags & MOV_TRUN_SAMPLE_FLAGS)
83988d58 2383 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
3eec23f3 2384 if (flags & MOV_TRUN_SAMPLE_CTS)
83988d58
MS
2385 avio_wb32(pb, track->cluster[i].cts);
2386 }
2387
b7d3dd90 2388 return update_size(pb, pos);
83988d58
MS
2389}
2390
4ddd54da
MS
2391static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2392{
2393 int64_t pos = avio_tell(pb);
2394 const uint8_t uuid[] = {
2395 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2396 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2397 };
2398
2399 avio_wb32(pb, 0); /* size placeholder */
2400 ffio_wfourcc(pb, "uuid");
2401 avio_write(pb, uuid, sizeof(uuid));
2402 avio_w8(pb, 1);
2403 avio_wb24(pb, 0);
2404 avio_wb64(pb, track->frag_start);
b7d3dd90 2405 avio_wb64(pb, track->start_dts + track->track_duration -
4ddd54da
MS
2406 track->cluster[0].dts);
2407
b7d3dd90 2408 return update_size(pb, pos);
4ddd54da
MS
2409}
2410
2411static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2412 MOVTrack *track, int entry)
2413{
2414 int n = track->nb_frag_info - 1 - entry, i;
2415 int size = 8 + 16 + 4 + 1 + 16*n;
2416 const uint8_t uuid[] = {
2417 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2418 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2419 };
2420
2421 if (entry < 0)
2422 return 0;
2423
2424 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2425 avio_wb32(pb, size);
2426 ffio_wfourcc(pb, "uuid");
2427 avio_write(pb, uuid, sizeof(uuid));
2428 avio_w8(pb, 1);
2429 avio_wb24(pb, 0);
2430 avio_w8(pb, n);
2431 for (i = 0; i < n; i++) {
2432 int index = entry + 1 + i;
2433 avio_wb64(pb, track->frag_info[index].time);
2434 avio_wb64(pb, track->frag_info[index].duration);
2435 }
2436 if (n < mov->ism_lookahead) {
21732063 2437 int free_size = 16 * (mov->ism_lookahead - n);
4ddd54da
MS
2438 avio_wb32(pb, free_size);
2439 ffio_wfourcc(pb, "free");
2440 for (i = 0; i < free_size - 8; i++)
2441 avio_w8(pb, 0);
2442 }
2443
2444 return 0;
2445}
2446
2447static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2448 MOVTrack *track)
2449{
2450 int64_t pos = avio_tell(pb);
2451 int i;
2452 for (i = 0; i < mov->ism_lookahead; i++) {
2453 /* Update the tfrf tag for the last ism_lookahead fragments,
2454 * nb_frag_info - 1 is the next fragment to be written. */
2455 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2456 }
2457 avio_seek(pb, pos, SEEK_SET);
2458 return 0;
2459}
2460
2461static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
a4adabb4
MS
2462 MOVTrack *track, int64_t moof_offset,
2463 int moof_size)
83988d58
MS
2464{
2465 int64_t pos = avio_tell(pb);
2466 avio_wb32(pb, 0); /* size placeholder */
2467 ffio_wfourcc(pb, "traf");
2468
e7bf085b
MS
2469 mov_write_tfhd_tag(pb, mov, track, moof_offset);
2470 mov_write_trun_tag(pb, mov, track, moof_size);
4ddd54da
MS
2471 if (mov->mode == MODE_ISM) {
2472 mov_write_tfxd_tag(pb, track);
2473
2474 if (mov->ism_lookahead) {
21732063 2475 int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
4ddd54da
MS
2476
2477 track->tfrf_offset = avio_tell(pb);
2478 avio_wb32(pb, 8 + size);
2479 ffio_wfourcc(pb, "free");
2480 for (i = 0; i < size; i++)
2481 avio_w8(pb, 0);
2482 }
2483 }
83988d58 2484
b7d3dd90 2485 return update_size(pb, pos);
83988d58
MS
2486}
2487
a4adabb4
MS
2488static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
2489 int tracks, int moof_size)
83988d58 2490{
a4adabb4
MS
2491 int64_t pos = avio_tell(pb);
2492 int i;
83988d58
MS
2493
2494 avio_wb32(pb, 0); /* size placeholder */
2495 ffio_wfourcc(pb, "moof");
2496
2497 mov_write_mfhd_tag(pb, mov);
2498 for (i = 0; i < mov->nb_streams; i++) {
2499 MOVTrack *track = &mov->tracks[i];
2500 if (tracks >= 0 && i != tracks)
2501 continue;
2502 if (!track->entry)
2503 continue;
a4adabb4 2504 mov_write_traf_tag(pb, mov, track, pos, moof_size);
83988d58
MS
2505 }
2506
b7d3dd90 2507 return update_size(pb, pos);
83988d58
MS
2508}
2509
a4adabb4
MS
2510static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2511{
2512 AVIOContext *avio_buf;
2513 int ret, moof_size;
a4adabb4 2514
72fe16a1 2515 if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
a4adabb4
MS
2516 return ret;
2517 mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
72fe16a1 2518 moof_size = ffio_close_null_buf(avio_buf);
a4adabb4
MS
2519 return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
2520}
2521
83988d58
MS
2522static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2523{
2524 int64_t pos = avio_tell(pb);
2525 int i;
2526
2527 avio_wb32(pb, 0); /* size placeholder */
2528 ffio_wfourcc(pb, "tfra");
2529 avio_w8(pb, 1); /* version */
2530 avio_wb24(pb, 0);
2531
b7d3dd90 2532 avio_wb32(pb, track->track_id);
83988d58
MS
2533 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2534 avio_wb32(pb, track->nb_frag_info);
2535 for (i = 0; i < track->nb_frag_info; i++) {
2536 avio_wb64(pb, track->frag_info[i].time);
2537 avio_wb64(pb, track->frag_info[i].offset);
2538 avio_w8(pb, 1); /* traf number */
2539 avio_w8(pb, 1); /* trun number */
2540 avio_w8(pb, 1); /* sample number */
2541 }
2542
b7d3dd90 2543 return update_size(pb, pos);
83988d58
MS
2544}
2545
2546static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2547{
2548 int64_t pos = avio_tell(pb);
2549 int i;
2550
2551 avio_wb32(pb, 0); /* size placeholder */
2552 ffio_wfourcc(pb, "mfra");
f5322104
MS
2553 /* An empty mfra atom is enough to indicate to the publishing point that
2554 * the stream has ended. */
2555 if (mov->flags & FF_MOV_FLAG_ISML)
b7d3dd90 2556 return update_size(pb, pos);
83988d58
MS
2557
2558 for (i = 0; i < mov->nb_streams; i++) {
2559 MOVTrack *track = &mov->tracks[i];
2560 if (track->nb_frag_info)
2561 mov_write_tfra_tag(pb, track);
2562 }
2563
2564 avio_wb32(pb, 16);
2565 ffio_wfourcc(pb, "mfro");
2566 avio_wb32(pb, 0); /* version + flags */
2567 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2568
b7d3dd90 2569 return update_size(pb, pos);
83988d58
MS
2570}
2571
ae628ec1 2572static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 2573{
77eb5504 2574 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
0abdb293 2575 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
b29af723 2576
a2704c97 2577 mov->mdat_pos = avio_tell(pb);
77eb5504 2578 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 2579 ffio_wfourcc(pb, "mdat");
1cb5f7fd
MN
2580 return 0;
2581}
2582
2583/* TODO: This needs to be more general */
ae628ec1 2584static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
1cb5f7fd 2585{
2d243fb3 2586 MOVMuxContext *mov = s->priv_data;
a2704c97 2587 int64_t pos = avio_tell(pb);
cd70d17f 2588 int has_h264 = 0, has_video = 0;
40f8675c 2589 int minor = 0x200;
c55cc074 2590 int i;
69dde1ad 2591
83579084
BC
2592 for (i = 0; i < s->nb_streams; i++) {
2593 AVStream *st = s->streams[i];
72415b2a 2594 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
cd70d17f 2595 has_video = 1;
36ef5369 2596 if (st->codec->codec_id == AV_CODEC_ID_H264)
83579084
BC
2597 has_h264 = 1;
2598 }
2599
77eb5504 2600 avio_wb32(pb, 0); /* size */
0abdb293 2601 ffio_wfourcc(pb, "ftyp");
e45ccf79 2602
1e9db41e
JS
2603 if (mov->major_brand && strlen(mov->major_brand) >= 4)
2604 ffio_wfourcc(pb, mov->major_brand);
2605 else if (mov->mode == MODE_3GP) {
0abdb293 2606 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
40f8675c
BC
2607 minor = has_h264 ? 0x100 : 0x200;
2608 } else if (mov->mode & MODE_3G2) {
0abdb293 2609 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
40f8675c 2610 minor = has_h264 ? 0x20000 : 0x10000;
21732063 2611 } else if (mov->mode == MODE_PSP)
0abdb293 2612 ffio_wfourcc(pb, "MSNV");
c55cc074 2613 else if (mov->mode == MODE_MP4)
0abdb293 2614 ffio_wfourcc(pb, "isom");
cd70d17f 2615 else if (mov->mode == MODE_IPOD)
0abdb293 2616 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
4ddd54da
MS
2617 else if (mov->mode == MODE_ISM)
2618 ffio_wfourcc(pb, "isml");
e01fba55
CB
2619 else if (mov->mode == MODE_F4V)
2620 ffio_wfourcc(pb, "f4v ");
cd70d17f 2621 else
0abdb293 2622 ffio_wfourcc(pb, "qt ");
e45ccf79 2623
77eb5504 2624 avio_wb32(pb, minor);
e45ccf79 2625
21732063 2626 if (mov->mode == MODE_MOV)
0abdb293 2627 ffio_wfourcc(pb, "qt ");
4ddd54da
MS
2628 else if (mov->mode == MODE_ISM) {
2629 ffio_wfourcc(pb, "piff");
2630 ffio_wfourcc(pb, "iso2");
2631 } else {
0abdb293
AK
2632 ffio_wfourcc(pb, "isom");
2633 ffio_wfourcc(pb, "iso2");
21732063 2634 if (has_h264)
0abdb293 2635 ffio_wfourcc(pb, "avc1");
6ba9ed95 2636 }
ed6e47c2 2637
91208916 2638 if (mov->mode == MODE_3GP)
0abdb293 2639 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
a04aec6d 2640 else if (mov->mode & MODE_3G2)
0abdb293 2641 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
91208916 2642 else if (mov->mode == MODE_PSP)
0abdb293 2643 ffio_wfourcc(pb, "MSNV");
c55cc074 2644 else if (mov->mode == MODE_MP4)
0abdb293 2645 ffio_wfourcc(pb, "mp41");
b7d3dd90 2646 return update_size(pb, pos);
1cb5f7fd
MN
2647}
2648
ae628ec1 2649static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
8af18154 2650{
b7d3dd90
MS
2651 AVCodecContext *video_codec = s->streams[0]->codec;
2652 AVCodecContext *audio_codec = s->streams[1]->codec;
2653 int audio_rate = audio_codec->sample_rate;
21732063 2654 int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
b7d3dd90
MS
2655 int audio_kbitrate = audio_codec->bit_rate / 1000;
2656 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
8af18154 2657
77eb5504 2658 avio_wb32(pb, 0x94); /* size */
0abdb293
AK
2659 ffio_wfourcc(pb, "uuid");
2660 ffio_wfourcc(pb, "PROF");
8af18154 2661
77eb5504
AK
2662 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2663 avio_wb32(pb, 0xbb88695c);
2664 avio_wb32(pb, 0xfac9c740);
8af18154 2665
77eb5504
AK
2666 avio_wb32(pb, 0x0); /* ? */
2667 avio_wb32(pb, 0x3); /* 3 sections ? */
8af18154 2668
77eb5504 2669 avio_wb32(pb, 0x14); /* size */
0abdb293 2670 ffio_wfourcc(pb, "FPRF");
77eb5504
AK
2671 avio_wb32(pb, 0x0); /* ? */
2672 avio_wb32(pb, 0x0); /* ? */
2673 avio_wb32(pb, 0x0); /* ? */
8af18154 2674
77eb5504 2675 avio_wb32(pb, 0x2c); /* size */
21732063 2676 ffio_wfourcc(pb, "APRF"); /* audio */
77eb5504
AK
2677 avio_wb32(pb, 0x0);
2678 avio_wb32(pb, 0x2); /* TrackID */
0abdb293 2679 ffio_wfourcc(pb, "mp4a");
77eb5504
AK
2680 avio_wb32(pb, 0x20f);
2681 avio_wb32(pb, 0x0);
2682 avio_wb32(pb, audio_kbitrate);
2683 avio_wb32(pb, audio_kbitrate);
b7d3dd90
MS
2684 avio_wb32(pb, audio_rate);
2685 avio_wb32(pb, audio_codec->channels);
77eb5504
AK
2686
2687 avio_wb32(pb, 0x34); /* size */
0abdb293 2688 ffio_wfourcc(pb, "VPRF"); /* video */
77eb5504
AK
2689 avio_wb32(pb, 0x0);
2690 avio_wb32(pb, 0x1); /* TrackID */
36ef5369 2691 if (video_codec->codec_id == AV_CODEC_ID_H264) {
0abdb293 2692 ffio_wfourcc(pb, "avc1");
77eb5504
AK
2693 avio_wb16(pb, 0x014D);
2694 avio_wb16(pb, 0x0015);
87b041e0 2695 } else {
0abdb293 2696 ffio_wfourcc(pb, "mp4v");
77eb5504
AK
2697 avio_wb16(pb, 0x0000);
2698 avio_wb16(pb, 0x0103);
87b041e0 2699 }
77eb5504
AK
2700 avio_wb32(pb, 0x0);
2701 avio_wb32(pb, video_kbitrate);
2702 avio_wb32(pb, video_kbitrate);
b7d3dd90
MS
2703 avio_wb32(pb, frame_rate);
2704 avio_wb32(pb, frame_rate);
2705 avio_wb16(pb, video_codec->width);
2706 avio_wb16(pb, video_codec->height);
77eb5504 2707 avio_wb32(pb, 0x010001); /* ? */
8af18154 2708}
2709
e1316b19
BC
2710static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2711{
2712 uint32_t c = -1;
2713 int i, closed_gop = 0;
2714
2715 for (i = 0; i < pkt->size - 4; i++) {
21732063 2716 c = (c << 8) + pkt->data[i];
e1316b19 2717 if (c == 0x1b8) { // gop
21732063 2718 closed_gop = pkt->data[i + 4] >> 6 & 0x01;
e1316b19 2719 } else if (c == 0x100) { // pic
21732063 2720 int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
e1316b19
BC
2721 if (!temp_ref || closed_gop) // I picture is not reordered
2722 *flags = MOV_SYNC_SAMPLE;
2723 else
2724 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2725 break;
2726 }
2727 }
2728 return 0;
2729}
2730
4ace130e
MS
2731static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2732{
2733 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2734 int seq = 0, entry = 0;
2735 int key = pkt->flags & AV_PKT_FLAG_KEY;
2736 start = find_next_marker(pkt->data, end);
2737 for (next = start; next < end; start = next) {
2738 next = find_next_marker(start + 4, end);
2739 switch (AV_RB32(start)) {
2740 case VC1_CODE_SEQHDR:
2741 seq = 1;
2742 break;
2743 case VC1_CODE_ENTRYPOINT:
2744 entry = 1;
2745 break;
2746 case VC1_CODE_SLICE:
2747 trk->vc1_info.slices = 1;
2748 break;
2749 }
2750 }
2751 if (!trk->entry && !fragment) {
2752 /* First packet in first fragment */
2753 trk->vc1_info.first_packet_seq = seq;
2754 trk->vc1_info.first_packet_entry = entry;
2755 } else if ((seq && !trk->vc1_info.packet_seq) ||
2756 (entry && !trk->vc1_info.packet_entry)) {
2757 int i;
2758 for (i = 0; i < trk->entry; i++)
2759 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2760 trk->has_keyframes = 0;
2761 if (seq)
2762 trk->vc1_info.packet_seq = 1;
2763 if (entry)
2764 trk->vc1_info.packet_entry = 1;
2765 if (!fragment) {
2766 /* First fragment */
2767 if ((!seq || trk->vc1_info.first_packet_seq) &&
2768 (!entry || trk->vc1_info.first_packet_entry)) {
2769 /* First packet had the same headers as this one, readd the
2770 * sync sample flag. */
2771 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2772 trk->has_keyframes = 1;
2773 }
2774 }
2775 }
2776 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2777 key = seq && entry;
2778 else if (trk->vc1_info.packet_seq)
2779 key = seq;
2780 else if (trk->vc1_info.packet_entry)
2781 key = entry;
2782 if (key) {
2783 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2784 trk->has_keyframes++;
2785 }
2786}
2787
83988d58
MS
2788static int mov_flush_fragment(AVFormatContext *s)
2789{
2790 MOVMuxContext *mov = s->priv_data;
2791 int i, first_track = -1;
2792 int64_t mdat_size = 0;
2793
2794 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2795 return 0;
2796
2797 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2798 int64_t pos = avio_tell(s->pb);
0c7b8b75
MS
2799 int ret;
2800 AVIOContext *moov_buf;
2801 uint8_t *buf;
2802 int buf_size;
83988d58
MS
2803
2804 for (i = 0; i < mov->nb_streams; i++)
2805 if (!mov->tracks[i].entry)
2806 break;
2807 /* Don't write the initial moov unless all tracks have data */
2808 if (i < mov->nb_streams)
2809 return 0;
0c7b8b75 2810
72fe16a1 2811 if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
0c7b8b75
MS
2812 return ret;
2813 mov_write_moov_tag(moov_buf, mov, s);
72fe16a1 2814 buf_size = ffio_close_null_buf(moov_buf);
0c7b8b75
MS
2815 for (i = 0; i < mov->nb_streams; i++)
2816 mov->tracks[i].data_offset = pos + buf_size + 8;
2817
83988d58 2818 mov_write_moov_tag(s->pb, mov, s);
0c7b8b75
MS
2819
2820 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2821 mov->mdat_buf = NULL;
2822 avio_wb32(s->pb, buf_size + 8);
2823 ffio_wfourcc(s->pb, "mdat");
2824 avio_write(s->pb, buf, buf_size);
2825 av_free(buf);
2826
83988d58
MS
2827 mov->fragments++;
2828 mov->mdat_size = 0;
2829 for (i = 0; i < mov->nb_streams; i++) {
2830 if (mov->tracks[i].entry)
2831 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
b7d3dd90 2832 mov->tracks[i].track_duration -
83988d58
MS
2833 mov->tracks[i].cluster[0].dts;
2834 mov->tracks[i].entry = 0;
2835 }
e771e6dd 2836 avio_flush(s->pb);
83988d58
MS
2837 return 0;
2838 }
2839
2840 for (i = 0; i < mov->nb_streams; i++) {
2841 MOVTrack *track = &mov->tracks[i];
2842 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2843 track->data_offset = 0;
2844 else
2845 track->data_offset = mdat_size;
2846 if (!track->mdat_buf)
2847 continue;
2848 mdat_size += avio_tell(track->mdat_buf);
2849 if (first_track < 0)
2850 first_track = i;
2851 }
2852
2853 if (!mdat_size)
2854 return 0;
2855
2856 for (i = 0; i < mov->nb_streams; i++) {
2857 MOVTrack *track = &mov->tracks[i];
2858 int buf_size, write_moof = 1, moof_tracks = -1;
2859 uint8_t *buf;
2860 int64_t duration = 0;
2861
2862 if (track->entry)
b7d3dd90 2863 duration = track->start_dts + track->track_duration -
83988d58
MS
2864 track->cluster[0].dts;
2865 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2866 if (!track->mdat_buf)
2867 continue;
2868 mdat_size = avio_tell(track->mdat_buf);
2869 moof_tracks = i;
2870 } else {
2871 write_moof = i == first_track;
2872 }
2873
2874 if (write_moof) {
2875 MOVFragmentInfo *info;
e771e6dd 2876 avio_flush(s->pb);
83988d58 2877 track->nb_frag_info++;
7c020e1a
AS
2878 if (track->nb_frag_info >= track->frag_info_capacity) {
2879 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
2880 if (av_reallocp_array(&track->frag_info,
2881 new_capacity,
2882 sizeof(*track->frag_info)))
2883 return AVERROR(ENOMEM);
2884 track->frag_info_capacity = new_capacity;
2885 }
83988d58
MS
2886 info = &track->frag_info[track->nb_frag_info - 1];
2887 info->offset = avio_tell(s->pb);
2888 info->time = mov->tracks[i].frag_start;
4ddd54da
MS
2889 info->duration = duration;
2890 mov_write_tfrf_tags(s->pb, mov, track);
83988d58
MS
2891
2892 mov_write_moof_tag(s->pb, mov, moof_tracks);
4ddd54da 2893 info->tfrf_offset = track->tfrf_offset;
83988d58
MS
2894 mov->fragments++;
2895
2896 avio_wb32(s->pb, mdat_size + 8);
2897 ffio_wfourcc(s->pb, "mdat");
2898 }
2899
2900 if (track->entry)
2901 track->frag_start += duration;
2902 track->entry = 0;
2903 if (!track->mdat_buf)
2904 continue;
2905 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2906 track->mdat_buf = NULL;
2907
2908 avio_write(s->pb, buf, buf_size);
2909 av_free(buf);
2910 }
2911
2912 mov->mdat_size = 0;
2913
e771e6dd 2914 avio_flush(s->pb);
83988d58
MS
2915 return 0;
2916}
2917
ebbede22 2918int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd 2919{
2d243fb3 2920 MOVMuxContext *mov = s->priv_data;
ae628ec1 2921 AVIOContext *pb = s->pb;
039627cf
BC
2922 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2923 AVCodecContext *enc = trk->enc;
b7d3dd90 2924 unsigned int samples_in_chunk = 0;
21732063 2925 int size = pkt->size;
e2484fb6 2926 uint8_t *reformatted_data = NULL;
83988d58 2927
0c7b8b75
MS
2928 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2929 int ret;
2930 if (mov->fragments > 0) {
2931 if (!trk->mdat_buf) {
2932 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2933 return ret;
2934 }
2935 pb = trk->mdat_buf;
2936 } else {
2937 if (!mov->mdat_buf) {
2938 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2939 return ret;
2940 }
2941 pb = mov->mdat_buf;
83988d58 2942 }
83988d58
MS
2943 }
2944
36ef5369 2945 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
5cb49ca1
BC
2946 /* We must find out how many AMR blocks there are in one packet */
2947 static uint16_t packed_size[16] =
ed669c9b 2948 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
5cb49ca1
BC
2949 int len = 0;
2950
b7d3dd90 2951 while (len < size && samples_in_chunk < 100) {
5cb49ca1 2952 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
b7d3dd90 2953 samples_in_chunk++;
1cb5f7fd 2954 }
b7d3dd90 2955 if (samples_in_chunk > 1) {
89938d29 2956 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2111e3f9
MN
2957 return -1;
2958 }
b7d3dd90
MS
2959 } else if (trk->sample_size)
2960 samples_in_chunk = size / trk->sample_size;
5cb49ca1 2961 else
b7d3dd90 2962 samples_in_chunk = 1;
e45ccf79 2963
5616f85d 2964 /* copy extradata if it exists */
b7d3dd90 2965 if (trk->vos_len == 0 && enc->extradata_size > 0) {
21732063 2966 trk->vos_len = enc->extradata_size;
b7d3dd90
MS
2967 trk->vos_data = av_malloc(trk->vos_len);
2968 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
e45ccf79
GB
2969 }
2970
36ef5369 2971 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
5d2160a0
BC
2972 /* from x264 or from bytestream h264 */
2973 /* nal reformating needed */
e2484fb6
MS
2974 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2975 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2976 &size);
2977 avio_write(pb, reformatted_data, size);
2978 } else {
2979 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2980 }
b6c61fb8
TW
2981 } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
2982 (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
2983 /* extradata is Annex B, assume the bitstream is too and convert it */
2984 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2985 ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
2986 avio_write(pb, reformatted_data, size);
2987 } else {
2988 size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
2989 }
5d2160a0 2990 } else {
77eb5504 2991 avio_write(pb, pkt->data, size);
5d2160a0
BC
2992 }
2993
36ef5369
AK
2994 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
2995 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
db568c07 2996 /* copy frame to create needed atoms */
21732063 2997 trk->vos_len = size;
b7d3dd90
MS
2998 trk->vos_data = av_malloc(size);
2999 if (!trk->vos_data)
e8c4df40 3000 return AVERROR(ENOMEM);
b7d3dd90 3001 memcpy(trk->vos_data, pkt->data, size);
c1b8e6d8
BC
3002 }
3003
3b4feac1 3004 if (trk->entry >= trk->cluster_capacity) {
21732063 3005 unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
3b4feac1
AS
3006 if (av_reallocp_array(&trk->cluster, new_capacity,
3007 sizeof(*trk->cluster)))
3008 return AVERROR(ENOMEM);
3009 trk->cluster_capacity = new_capacity;
e45ccf79 3010 }
e45ccf79 3011
21732063 3012 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
b7d3dd90 3013 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
21732063
MS
3014 trk->cluster[trk->entry].size = size;
3015 trk->cluster[trk->entry].entries = samples_in_chunk;
3016 trk->cluster[trk->entry].dts = pkt->dts;
83988d58
MS
3017 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
3018 /* First packet of a new fragment. We already wrote the duration
b7d3dd90 3019 * of the last packet of the previous fragment based on track_duration,
83988d58
MS
3020 * which might not exactly match our dts. Therefore adjust the dts
3021 * of this packet to be what the previous packets duration implies. */
b7d3dd90 3022 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
83988d58 3023 }
6ca38568
MS
3024 if (trk->start_dts == AV_NOPTS_VALUE)
3025 trk->start_dts = pkt->dts;
b7d3dd90 3026 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
aa90239f 3027
96f69e0d
BC
3028 if (pkt->pts == AV_NOPTS_VALUE) {
3029 av_log(s, AV_LOG_WARNING, "pts has no value\n");
3030 pkt->pts = pkt->dts;
3031 }
c4f078ff 3032 if (pkt->dts != pkt->pts)
e1316b19 3033 trk->flags |= MOV_TRACK_CTTS;
21732063 3034 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
e1316b19 3035 trk->cluster[trk->entry].flags = 0;
36ef5369 3036 if (enc->codec_id == AV_CODEC_ID_VC1) {
4ace130e
MS
3037 mov_parse_vc1_frame(pkt, trk, mov->fragments);
3038 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
36ef5369 3039 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
44b11207 3040 trk->entry > 0) { // force sync sample for the first key frame
e1316b19
BC
3041 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3042 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3043 trk->flags |= MOV_TRACK_STPS;
3044 } else {
3045 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3046 }
3047 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
b7d3dd90 3048 trk->has_keyframes++;
ab4752e3 3049 }
e45ccf79 3050 trk->entry++;
b7d3dd90 3051 trk->sample_count += samples_in_chunk;
21732063 3052 mov->mdat_size += size;
e45ccf79 3053
e977af6f 3054 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
e2484fb6
MS
3055 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3056 reformatted_data, size);
3057 av_free(reformatted_data);
1cb5f7fd
MN
3058 return 0;
3059}
3060
ebbede22 3061static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
b613ff5e
MS
3062{
3063