More correct printf format specifiers
[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
ae628ec1 1298static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1299{
985688b8 1300 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
a2704c97 1301 int64_t pos = avio_tell(pb);
115329f1 1302
67400f6b
LB
1303 hdlr = "dhlr";
1304 hdlr_type = "url ";
1305 descr = "DataHandler";
1306
1307 if (track) {
bb270c08 1308 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
72415b2a 1309 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
bb270c08 1310 hdlr_type = "vide";
21732063 1311 descr = "VideoHandler";
72415b2a 1312 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
bb270c08 1313 hdlr_type = "soun";
21732063 1314 descr = "SoundHandler";
72415b2a 1315 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
f94371b1
MN
1316 if (track->tag == MKTAG('t','x','3','g')) {
1317 hdlr_type = "sbtl";
1318 } else if (track->tag == MKTAG('m','p','4','s')) {
1319 hdlr_type = "subp";
1320 } else {
1321 hdlr_type = "text";
1322 }
f6204886 1323 descr = "SubtitleHandler";
e977af6f
MS
1324 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1325 hdlr_type = "hint";
21732063 1326 descr = "HintHandler";
67400f6b
LB
1327 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1328 hdlr_type = "tmcd";
1329 descr = "TimeCodeHandler";
1330 } else {
1331 char tag_buf[32];
1332 av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1333 track->enc->codec_tag);
1334
1335 av_log(track->enc, AV_LOG_WARNING,
1336 "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1337 tag_buf, track->enc->codec_tag);
bb270c08 1338 }
9a4d9388 1339 }
115329f1 1340
77eb5504 1341 avio_wb32(pb, 0); /* size */
0abdb293 1342 ffio_wfourcc(pb, "hdlr");
77eb5504
AK
1343 avio_wb32(pb, 0); /* Version & flags */
1344 avio_write(pb, hdlr, 4); /* handler */
0abdb293 1345 ffio_wfourcc(pb, hdlr_type); /* handler type */
21732063
MS
1346 avio_wb32(pb, 0); /* reserved */
1347 avio_wb32(pb, 0); /* reserved */
1348 avio_wb32(pb, 0); /* reserved */
606d48ce 1349 if (!track || track->mode == MODE_MOV)
77eb5504
AK
1350 avio_w8(pb, strlen(descr)); /* pascal string */
1351 avio_write(pb, descr, strlen(descr)); /* handler description */
606d48ce 1352 if (track && track->mode != MODE_MOV)
77eb5504 1353 avio_w8(pb, 0); /* c string */
b7d3dd90 1354 return update_size(pb, pos);
9a4d9388
RS
1355}
1356
ae628ec1 1357static int mov_write_hmhd_tag(AVIOContext *pb)
e977af6f
MS
1358{
1359 /* This atom must be present, but leaving the values at zero
1360 * seems harmless. */
77eb5504 1361 avio_wb32(pb, 28); /* size */
0abdb293 1362 ffio_wfourcc(pb, "hmhd");
77eb5504
AK
1363 avio_wb32(pb, 0); /* version, flags */
1364 avio_wb16(pb, 0); /* maxPDUsize */
1365 avio_wb16(pb, 0); /* avgPDUsize */
1366 avio_wb32(pb, 0); /* maxbitrate */
1367 avio_wb32(pb, 0); /* avgbitrate */
1368 avio_wb32(pb, 0); /* reserved */
e977af6f
MS
1369 return 28;
1370}
1371
ae628ec1 1372static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
9a4d9388 1373{
a2704c97 1374 int64_t pos = avio_tell(pb);
77eb5504 1375 avio_wb32(pb, 0); /* size */
0abdb293 1376 ffio_wfourcc(pb, "minf");
21732063 1377 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
9a4d9388 1378 mov_write_vmhd_tag(pb);
72415b2a 1379 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
9a4d9388 1380 mov_write_smhd_tag(pb);
72415b2a 1381 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
9f520554 1382 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
80357cfc 1383 else mov_write_nmhd_tag(pb);
e977af6f
MS
1384 } else if (track->tag == MKTAG('r','t','p',' ')) {
1385 mov_write_hmhd_tag(pb);
b89e8759
LB
1386 } else if (track->tag == MKTAG('t','m','c','d')) {
1387 mov_write_gmhd_tag(pb);
f6204886 1388 }
9a4d9388
RS
1389 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1390 mov_write_hdlr_tag(pb, NULL);
1391 mov_write_dinf_tag(pb);
1392 mov_write_stbl_tag(pb, track);
b7d3dd90 1393 return update_size(pb, pos);
1cb5f7fd
MN
1394}
1395
ae628ec1 1396static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1397{
b7d3dd90 1398 int version = track->track_duration < INT32_MAX ? 0 : 1;
a3a80ddc 1399
53026339
MS
1400 if (track->mode == MODE_ISM)
1401 version = 1;
1402
77eb5504 1403 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
0abdb293 1404 ffio_wfourcc(pb, "mdhd");
77eb5504
AK
1405 avio_w8(pb, version);
1406 avio_wb24(pb, 0); /* flags */
a3a80ddc 1407 if (version == 1) {
77eb5504
AK
1408 avio_wb64(pb, track->time);
1409 avio_wb64(pb, track->time);
a3a80ddc 1410 } else {
77eb5504
AK
1411 avio_wb32(pb, track->time); /* creation time */
1412 avio_wb32(pb, track->time); /* modification time */
a3a80ddc 1413 }
77eb5504 1414 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
99a357f4
MS
1415 if (!track->entry)
1416 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1417 else
1418 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
77eb5504
AK
1419 avio_wb16(pb, track->language); /* language */
1420 avio_wb16(pb, 0); /* reserved (quality) */
db8f4a92 1421
21732063 1422 if (version != 0 && track->mode == MODE_MOV) {
db8f4a92 1423 av_log(NULL, AV_LOG_ERROR,
21732063
MS
1424 "FATAL error, file duration too long for timebase, this file will not be\n"
1425 "playable with quicktime. Choose a different timebase or a different\n"
1426 "container format\n");
db8f4a92
MN
1427 }
1428
1cb5f7fd
MN
1429 return 32;
1430}
1431
ae628ec1 1432static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1433{
a2704c97 1434 int64_t pos = avio_tell(pb);
77eb5504 1435 avio_wb32(pb, 0); /* size */
0abdb293 1436 ffio_wfourcc(pb, "mdia");
6e6d6dc0
MN
1437 mov_write_mdhd_tag(pb, track);
1438 mov_write_hdlr_tag(pb, track);
1439 mov_write_minf_tag(pb, track);
b7d3dd90 1440 return update_size(pb, pos);
1cb5f7fd
MN
1441}
1442
ae628ec1 1443static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 1444{
b7d3dd90 1445 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
729ef3ba 1446 track->timescale, AV_ROUND_UP);
a3a80ddc
BC
1447 int version = duration < INT32_MAX ? 0 : 1;
1448
53026339
MS
1449 if (track->mode == MODE_ISM)
1450 version = 1;
1451
77eb5504 1452 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
0abdb293 1453 ffio_wfourcc(pb, "tkhd");
77eb5504 1454 avio_w8(pb, version);
30ce2890
JS
1455 avio_wb24(pb, (track->flags & MOV_TRACK_ENABLED) ?
1456 MOV_TKHD_FLAG_ENABLED | MOV_TKHD_FLAG_IN_MOVIE :
1457 MOV_TKHD_FLAG_IN_MOVIE);
a3a80ddc 1458 if (version == 1) {
77eb5504
AK
1459 avio_wb64(pb, track->time);
1460 avio_wb64(pb, track->time);
a3a80ddc 1461 } else {
77eb5504
AK
1462 avio_wb32(pb, track->time); /* creation time */
1463 avio_wb32(pb, track->time); /* modification time */
a3a80ddc 1464 }
b7d3dd90 1465 avio_wb32(pb, track->track_id); /* track-id */
77eb5504 1466 avio_wb32(pb, 0); /* reserved */
99a357f4
MS
1467 if (!track->entry)
1468 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1469 else
1470 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1cb5f7fd 1471
77eb5504
AK
1472 avio_wb32(pb, 0); /* reserved */
1473 avio_wb32(pb, 0); /* reserved */
7574cacb 1474 avio_wb16(pb, 0); /* layer */
c92a2a4e 1475 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1cb5f7fd 1476 /* Volume, only for audio */
21732063 1477 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 1478 avio_wb16(pb, 0x0100);
1cb5f7fd 1479 else
77eb5504
AK
1480 avio_wb16(pb, 0);
1481 avio_wb16(pb, 0); /* reserved */
1cb5f7fd
MN
1482
1483 /* Matrix structure */
77eb5504
AK
1484 avio_wb32(pb, 0x00010000); /* reserved */
1485 avio_wb32(pb, 0x0); /* reserved */
1486 avio_wb32(pb, 0x0); /* reserved */
1487 avio_wb32(pb, 0x0); /* reserved */
1488 avio_wb32(pb, 0x00010000); /* reserved */
1489 avio_wb32(pb, 0x0); /* reserved */
1490 avio_wb32(pb, 0x0); /* reserved */
1491 avio_wb32(pb, 0x0); /* reserved */
1492 avio_wb32(pb, 0x40000000); /* reserved */
1cb5f7fd
MN
1493
1494 /* Track width and height, for visual only */
21732063
MS
1495 if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1496 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1497 if (track->mode == MODE_MOV) {
ea1afa12 1498 avio_wb32(pb, track->enc->width << 16);
fffdee89 1499 avio_wb32(pb, track->height << 16);
ea1afa12 1500 } else {
77d207cb 1501 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
21732063 1502 if (!sample_aspect_ratio || track->height != track->enc->height)
77d207cb 1503 sample_aspect_ratio = 1;
21732063
MS
1504 avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1505 avio_wb32(pb, track->height * 0x10000);
ea1afa12 1506 }
21732063 1507 } else {
77eb5504
AK
1508 avio_wb32(pb, 0);
1509 avio_wb32(pb, 0);
1cb5f7fd
MN
1510 }
1511 return 0x5c;
1512}
1513
13ff92d1 1514static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
ea1afa12
MV
1515{
1516 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1517 track->enc->sample_aspect_ratio.den);
1518
a2704c97 1519 int64_t pos = avio_tell(pb);
ea1afa12
MV
1520
1521 avio_wb32(pb, 0); /* size */
13ff92d1 1522 ffio_wfourcc(pb, "tapt");
ea1afa12
MV
1523
1524 avio_wb32(pb, 20);
13ff92d1 1525 ffio_wfourcc(pb, "clef");
ea1afa12
MV
1526 avio_wb32(pb, 0);
1527 avio_wb32(pb, width << 16);
1528 avio_wb32(pb, track->enc->height << 16);
1529
1530 avio_wb32(pb, 20);
13ff92d1 1531 ffio_wfourcc(pb, "enof");
ea1afa12
MV
1532 avio_wb32(pb, 0);
1533 avio_wb32(pb, track->enc->width << 16);
1534 avio_wb32(pb, track->enc->height << 16);
1535
b7d3dd90 1536 return update_size(pb, pos);
373211d8 1537}
ea1afa12 1538
8af18154 1539// This box seems important for the psp playback ... without it the movie seems to hang
ae628ec1 1540static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
8af18154 1541{
b7d3dd90 1542 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
c1691948
GP
1543 track->timescale, AV_ROUND_UP);
1544 int version = duration < INT32_MAX ? 0 : 1;
1545 int entry_size, entry_count, size;
1546 int64_t delay, start_ct = track->cluster[0].cts;
1547 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1548 track->timescale, AV_ROUND_DOWN);
1549 version |= delay < INT32_MAX ? 0 : 1;
1550
1551 entry_size = (version == 1) ? 20 : 12;
1552 entry_count = 1 + (delay > 0);
1553 size = 24 + entry_count * entry_size;
1554
1555 /* write the atom data */
1556 avio_wb32(pb, size);
0abdb293 1557 ffio_wfourcc(pb, "edts");
c1691948 1558 avio_wb32(pb, size - 8);
0abdb293 1559 ffio_wfourcc(pb, "elst");
c1691948
GP
1560 avio_w8(pb, version);
1561 avio_wb24(pb, 0); /* flags */
8af18154 1562
c1691948
GP
1563 avio_wb32(pb, entry_count);
1564 if (delay > 0) { /* add an empty edit to delay presentation */
1565 if (version == 1) {
1566 avio_wb64(pb, delay);
1567 avio_wb64(pb, -1);
1568 } else {
1569 avio_wb32(pb, delay);
1570 avio_wb32(pb, -1);
1571 }
1572 avio_wb32(pb, 0x00010000);
1573 }
8af18154 1574
c1691948
GP
1575 /* duration */
1576 if (version == 1) {
1577 avio_wb64(pb, duration);
1578 avio_wb64(pb, start_ct);
1579 } else {
1580 avio_wb32(pb, duration);
1581 avio_wb32(pb, start_ct);
1582 }
77eb5504 1583 avio_wb32(pb, 0x00010000);
c1691948 1584 return size;
8af18154 1585}
1586
ae628ec1 1587static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
ddb63017 1588{
77eb5504 1589 avio_wb32(pb, 20); // size
0abdb293 1590 ffio_wfourcc(pb, "tref");
77eb5504
AK
1591 avio_wb32(pb, 12); // size (subatom)
1592 avio_wl32(pb, track->tref_tag);
1593 avio_wb32(pb, track->tref_id);
ddb63017
DC
1594 return 20;
1595}
1596
8af18154 1597// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
ae628ec1 1598static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
8af18154 1599{
77eb5504 1600 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
0abdb293
AK
1601 ffio_wfourcc(pb, "uuid");
1602 ffio_wfourcc(pb, "USMT");
77eb5504
AK
1603 avio_wb32(pb, 0x21d24fce);
1604 avio_wb32(pb, 0xbb88695c);
1605 avio_wb32(pb, 0xfac9c740);
1606 avio_wb32(pb, 0x1c); // another size here!
0abdb293 1607 ffio_wfourcc(pb, "MTDT");
77eb5504
AK
1608 avio_wb32(pb, 0x00010012);
1609 avio_wb32(pb, 0x0a);
1610 avio_wb32(pb, 0x55c40000);
1611 avio_wb32(pb, 0x1);
1612 avio_wb32(pb, 0x0);
8af18154 1613 return 0x34;
1614}
1615
8034130e 1616static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
e977af6f 1617{
8034130e 1618 AVFormatContext *ctx = track->rtp_ctx;
e977af6f
MS
1619 char buf[1000] = "";
1620 int len;
1621
8034130e
LB
1622 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1623 NULL, NULL, 0, 0, ctx);
1624 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
e977af6f
MS
1625 len = strlen(buf);
1626
77eb5504 1627 avio_wb32(pb, len + 24);
0abdb293 1628 ffio_wfourcc(pb, "udta");
77eb5504 1629 avio_wb32(pb, len + 16);
0abdb293 1630 ffio_wfourcc(pb, "hnti");
77eb5504 1631 avio_wb32(pb, len + 8);
0abdb293 1632 ffio_wfourcc(pb, "sdp ");
77eb5504 1633 avio_write(pb, buf, len);
e977af6f
MS
1634 return len + 24;
1635}
1636
83988d58
MS
1637static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1638 MOVTrack *track, AVStream *st)
1cb5f7fd 1639{
a2704c97 1640 int64_t pos = avio_tell(pb);
77eb5504 1641 avio_wb32(pb, 0); /* size */
0abdb293 1642 ffio_wfourcc(pb, "trak");
59729451 1643 mov_write_tkhd_tag(pb, track, st);
83988d58
MS
1644 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1645 (track->entry && track->cluster[0].dts)) {
1646 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1647 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1648 }
ddb63017
DC
1649 if (track->tref_tag)
1650 mov_write_tref_tag(pb, track);
6e6d6dc0 1651 mov_write_mdia_tag(pb, track);
115329f1 1652 if (track->mode == MODE_PSP)
21732063 1653 mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
e977af6f 1654 if (track->tag == MKTAG('r','t','p',' '))
8034130e 1655 mov_write_udta_sdp(pb, track);
ea1afa12
MV
1656 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1657 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1658 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1659 mov_write_tapt_tag(pb, track);
21732063 1660 }
b7d3dd90 1661 return update_size(pb, pos);
1cb5f7fd
MN
1662}
1663
ae628ec1 1664static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1665{
d3b8bde2
AC
1666 int i, has_audio = 0, has_video = 0;
1667 int64_t pos = avio_tell(pb);
1668 int audio_profile = mov->iods_audio_profile;
1669 int video_profile = mov->iods_video_profile;
1670 for (i = 0; i < mov->nb_streams; i++) {
21732063 1671 if (mov->tracks[i].entry > 0) {
d3b8bde2
AC
1672 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1673 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1674 }
1675 }
1676 if (audio_profile < 0)
1677 audio_profile = 0xFF - has_audio;
1678 if (video_profile < 0)
1679 video_profile = 0xFF - has_video;
1680 avio_wb32(pb, 0x0); /* size */
0abdb293 1681 ffio_wfourcc(pb, "iods");
77eb5504 1682 avio_wb32(pb, 0); /* version & flags */
b7d3dd90 1683 put_descr(pb, 0x10, 7);
d3b8bde2
AC
1684 avio_wb16(pb, 0x004f);
1685 avio_w8(pb, 0xff);
1686 avio_w8(pb, 0xff);
1687 avio_w8(pb, audio_profile);
1688 avio_w8(pb, video_profile);
1689 avio_w8(pb, 0xff);
b7d3dd90 1690 return update_size(pb, pos);
1cb5f7fd
MN
1691}
1692
83988d58
MS
1693static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1694{
1695 avio_wb32(pb, 0x20); /* size */
1696 ffio_wfourcc(pb, "trex");
1697 avio_wb32(pb, 0); /* version & flags */
b7d3dd90 1698 avio_wb32(pb, track->track_id); /* track ID */
83988d58
MS
1699 avio_wb32(pb, 1); /* default sample description index */
1700 avio_wb32(pb, 0); /* default sample duration */
1701 avio_wb32(pb, 0); /* default sample size */
1702 avio_wb32(pb, 0); /* default sample flags */
1703 return 0;
1704}
1705
1706static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1707{
1708 int64_t pos = avio_tell(pb);
1709 int i;
1710 avio_wb32(pb, 0x0); /* size */
1711 ffio_wfourcc(pb, "mvex");
1712 for (i = 0; i < mov->nb_streams; i++)
1713 mov_write_trex_tag(pb, &mov->tracks[i]);
b7d3dd90 1714 return update_size(pb, pos);
83988d58
MS
1715}
1716
ae628ec1 1717static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1718{
b7d3dd90
MS
1719 int max_track_id = 1, i;
1720 int64_t max_track_len_temp, max_track_len = 0;
a3a80ddc 1721 int version;
1cb5f7fd 1722
21732063 1723 for (i = 0; i < mov->nb_streams; i++) {
22de0f83 1724 if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
b7d3dd90
MS
1725 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1726 MOV_TIMESCALE,
1727 mov->tracks[i].timescale,
1728 AV_ROUND_UP);
1729 if (max_track_len < max_track_len_temp)
1730 max_track_len = max_track_len_temp;
1731 if (max_track_id < mov->tracks[i].track_id)
1732 max_track_id = mov->tracks[i].track_id;
1cb5f7fd
MN
1733 }
1734 }
a3a80ddc 1735
b7d3dd90 1736 version = max_track_len < UINT32_MAX ? 0 : 1;
77eb5504 1737 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
0abdb293 1738 ffio_wfourcc(pb, "mvhd");
77eb5504
AK
1739 avio_w8(pb, version);
1740 avio_wb24(pb, 0); /* flags */
a3a80ddc 1741 if (version == 1) {
77eb5504
AK
1742 avio_wb64(pb, mov->time);
1743 avio_wb64(pb, mov->time);
a3a80ddc 1744 } else {
77eb5504
AK
1745 avio_wb32(pb, mov->time); /* creation time */
1746 avio_wb32(pb, mov->time); /* modification time */
a3a80ddc 1747 }
77eb5504 1748 avio_wb32(pb, MOV_TIMESCALE);
b7d3dd90 1749 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1cb5f7fd 1750
77eb5504
AK
1751 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1752 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1753 avio_wb16(pb, 0); /* reserved */
1754 avio_wb32(pb, 0); /* reserved */
1755 avio_wb32(pb, 0); /* reserved */
1cb5f7fd
MN
1756
1757 /* Matrix structure */
77eb5504
AK
1758 avio_wb32(pb, 0x00010000); /* reserved */
1759 avio_wb32(pb, 0x0); /* reserved */
1760 avio_wb32(pb, 0x0); /* reserved */
1761 avio_wb32(pb, 0x0); /* reserved */
1762 avio_wb32(pb, 0x00010000); /* reserved */
1763 avio_wb32(pb, 0x0); /* reserved */
1764 avio_wb32(pb, 0x0); /* reserved */
1765 avio_wb32(pb, 0x0); /* reserved */
1766 avio_wb32(pb, 0x40000000); /* reserved */
1767
1768 avio_wb32(pb, 0); /* reserved (preview time) */
1769 avio_wb32(pb, 0); /* reserved (preview duration) */
1770 avio_wb32(pb, 0); /* reserved (poster time) */
1771 avio_wb32(pb, 0); /* reserved (selection time) */
1772 avio_wb32(pb, 0); /* reserved (selection duration) */
1773 avio_wb32(pb, 0); /* reserved (current time) */
b7d3dd90 1774 avio_wb32(pb, max_track_id + 1); /* Next track id */
1cb5f7fd
MN
1775 return 0x6c;
1776}
1777
ae628ec1 1778static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1779 AVFormatContext *s)
1780{
77eb5504 1781 avio_wb32(pb, 33); /* size */
0abdb293 1782 ffio_wfourcc(pb, "hdlr");
77eb5504
AK
1783 avio_wb32(pb, 0);
1784 avio_wb32(pb, 0);
0abdb293
AK
1785 ffio_wfourcc(pb, "mdir");
1786 ffio_wfourcc(pb, "appl");
77eb5504
AK
1787 avio_wb32(pb, 0);
1788 avio_wb32(pb, 0);
1789 avio_w8(pb, 0);
6b174197 1790 return 33;
b6c50eb1
PB
1791}
1792
1793/* helper function to write a data tag with the specified string as data */
ae628ec1 1794static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
b6c50eb1 1795{
21732063 1796 if (long_style) {
6b174197 1797 int size = 16 + strlen(data);
77eb5504 1798 avio_wb32(pb, size); /* size */
0abdb293 1799 ffio_wfourcc(pb, "data");
77eb5504
AK
1800 avio_wb32(pb, 1);
1801 avio_wb32(pb, 0);
1802 avio_write(pb, data, strlen(data));
6b174197 1803 return size;
21732063 1804 } else {
fe3ab8ad
BC
1805 if (!lang)
1806 lang = ff_mov_iso639_to_lang("und", 1);
77eb5504
AK
1807 avio_wb16(pb, strlen(data)); /* string length */
1808 avio_wb16(pb, lang);
1809 avio_write(pb, data, strlen(data));
efda3395 1810 return strlen(data) + 4;
b6c50eb1 1811 }
b6c50eb1
PB
1812}
1813
21732063
MS
1814static int mov_write_string_tag(AVIOContext *pb, const char *name,
1815 const char *value, int lang, int long_style)
1816{
b6c50eb1 1817 int size = 0;
91208916 1818 if (value && value[0]) {
a2704c97 1819 int64_t pos = avio_tell(pb);
77eb5504 1820 avio_wb32(pb, 0); /* size */
0abdb293 1821 ffio_wfourcc(pb, name);
d9fc9ff3 1822 mov_write_string_data_tag(pb, value, lang, long_style);
b7d3dd90 1823 size = update_size(pb, pos);
b6c50eb1
PB
1824 }
1825 return size;
1826}
1827
ae628ec1 1828static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1ee2d448
AJ
1829 const char *name, const char *tag,
1830 int long_style)
b6c50eb1 1831{
d9fc9ff3 1832 int l, lang = 0, len, len2;
d2d67e42 1833 AVDictionaryEntry *t, *t2 = NULL;
d9fc9ff3 1834 char tag2[16];
1ee2d448 1835
d2d67e42 1836 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
efda3395 1837 return 0;
1ee2d448 1838
d9fc9ff3
AJ
1839 len = strlen(t->key);
1840 snprintf(tag2, sizeof(tag2), "%s-", tag);
d2d67e42 1841 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
d9fc9ff3 1842 len2 = strlen(t2->key);
21732063
MS
1843 if (len2 == len + 4 && !strcmp(t->value, t2->value)
1844 && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
d9fc9ff3
AJ
1845 lang = l;
1846 break;
1847 }
1848 }
1849 return mov_write_string_tag(pb, name, t->value, lang, long_style);
b6c50eb1
PB
1850}
1851
1852/* iTunes track number */
ae628ec1 1853static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1854 AVFormatContext *s)
1855{
d2d67e42 1856 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1ee2d448
AJ
1857 int size = 0, track = t ? atoi(t->value) : 0;
1858 if (track) {
77eb5504 1859 avio_wb32(pb, 32); /* size */
0abdb293 1860 ffio_wfourcc(pb, "trkn");
21732063
MS
1861 avio_wb32(pb, 24); /* size */
1862 ffio_wfourcc(pb, "data");
1863 avio_wb32(pb, 0); // 8 bytes empty
1864 avio_wb32(pb, 0);
1865 avio_wb16(pb, 0); // empty
1866 avio_wb16(pb, track); // track number
1867 avio_wb16(pb, 0); // total track number
1868 avio_wb16(pb, 0); // empty
6b174197 1869 size = 32;
b6c50eb1
PB
1870 }
1871 return size;
1872}
1873
1874/* iTunes meta data list */
ae628ec1 1875static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1876 AVFormatContext *s)
1877{
a2704c97 1878 int64_t pos = avio_tell(pb);
77eb5504 1879 avio_wb32(pb, 0); /* size */
0abdb293 1880 ffio_wfourcc(pb, "ilst");
1ee2d448 1881 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
ea4c4d7f 1882 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
2cf9c7ef 1883 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
620af1a1 1884 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1ee2d448 1885 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
ca76a119 1886 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
565e0c6d
JS
1887 if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
1888 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1ee2d448
AJ
1889 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1890 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1891 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2cf9c7ef
MM
1892 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1893 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
7382902b
DC
1894 mov_write_string_metadata(s, pb, "desc", "description",1);
1895 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1896 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1897 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1898 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
b6c50eb1 1899 mov_write_trkn_tag(pb, mov, s);
b7d3dd90 1900 return update_size(pb, pos);
b6c50eb1
PB
1901}
1902
1903/* iTunes meta data tag */
ae628ec1 1904static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1905 AVFormatContext *s)
1906{
1907 int size = 0;
a2704c97 1908 int64_t pos = avio_tell(pb);
77eb5504 1909 avio_wb32(pb, 0); /* size */
0abdb293 1910 ffio_wfourcc(pb, "meta");
77eb5504 1911 avio_wb32(pb, 0);
b97fb809
AJ
1912 mov_write_itunes_hdlr_tag(pb, mov, s);
1913 mov_write_ilst_tag(pb, mov, s);
b7d3dd90 1914 size = update_size(pb, pos);
b6c50eb1
PB
1915 return size;
1916}
115329f1 1917
07b7b06d
BC
1918static int utf8len(const uint8_t *b)
1919{
21732063 1920 int len = 0;
07b7b06d 1921 int val;
21732063 1922 while (*b) {
07b7b06d
BC
1923 GET_UTF8(val, *b++, return -1;)
1924 len++;
1925 }
1926 return len;
1927}
1928
ae628ec1 1929static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
07b7b06d
BC
1930{
1931 int val;
21732063 1932 while (*b) {
07b7b06d 1933 GET_UTF8(val, *b++, return -1;)
77eb5504 1934 avio_wb16(pb, val);
07b7b06d 1935 }
77eb5504 1936 avio_wb16(pb, 0x00);
07b7b06d
BC
1937 return 0;
1938}
1939
1940static uint16_t language_code(const char *str)
1941{
21732063
MS
1942 return (((str[0] - 0x60) & 0x1F) << 10) +
1943 (((str[1] - 0x60) & 0x1F) << 5) +
1944 (( str[2] - 0x60) & 0x1F);
07b7b06d
BC
1945}
1946
ae628ec1 1947static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
c6e2c6c9
BC
1948 const char *tag, const char *str)
1949{
a2704c97 1950 int64_t pos = avio_tell(pb);
d2d67e42 1951 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1ee2d448 1952 if (!t || !utf8len(t->value))
c6e2c6c9 1953 return 0;
77eb5504 1954 avio_wb32(pb, 0); /* size */
0abdb293 1955 ffio_wfourcc(pb, tag); /* type */
77eb5504 1956 avio_wb32(pb, 0); /* version + flags */
c6e2c6c9 1957 if (!strcmp(tag, "yrrc"))
77eb5504 1958 avio_wb16(pb, atoi(t->value));
c6e2c6c9 1959 else {
77eb5504 1960 avio_wb16(pb, language_code("eng")); /* language */
21732063 1961 avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
1ee2d448 1962 if (!strcmp(tag, "albm") &&
d2d67e42 1963 (t = av_dict_get(s->metadata, "track", NULL, 0)))
77eb5504 1964 avio_w8(pb, atoi(t->value));
c6e2c6c9 1965 }
b7d3dd90 1966 return update_size(pb, pos);
c6e2c6c9
BC
1967}
1968
ae628ec1 1969static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
dc75e4e3 1970{
a2704c97 1971 int64_t pos = avio_tell(pb);
dc75e4e3
DC
1972 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1973
77eb5504 1974 avio_wb32(pb, 0); // size
0abdb293 1975 ffio_wfourcc(pb, "chpl");
77eb5504
AK
1976 avio_wb32(pb, 0x01000000); // version + flags
1977 avio_wb32(pb, 0); // unknown
1978 avio_w8(pb, nb_chapters);
dc75e4e3
DC
1979
1980 for (i = 0; i < nb_chapters; i++) {
1981 AVChapter *c = s->chapters[i];
d2d67e42 1982 AVDictionaryEntry *t;
77eb5504 1983 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
dc75e4e3 1984
d2d67e42 1985 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
dc75e4e3 1986 int len = FFMIN(strlen(t->value), 255);
77eb5504
AK
1987 avio_w8(pb, len);
1988 avio_write(pb, t->value, len);
dc75e4e3 1989 } else
77eb5504 1990 avio_w8(pb, 0);
dc75e4e3 1991 }
b7d3dd90 1992 return update_size(pb, pos);
dc75e4e3
DC
1993}
1994
ae628ec1 1995static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
69dde1ad
GB
1996 AVFormatContext *s)
1997{
ae628ec1 1998 AVIOContext *pb_buf;
1ee2d448
AJ
1999 int i, ret, size;
2000 uint8_t *buf;
89a0d790 2001
d1d87dfb
BC
2002 for (i = 0; i < s->nb_streams; i++)
2003 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1ee2d448 2004 return 0;
d1d87dfb
BC
2005 }
2006
b92c5452 2007 ret = avio_open_dyn_buf(&pb_buf);
21732063 2008 if (ret < 0)
1ee2d448 2009 return ret;
69dde1ad 2010
21732063
MS
2011 if (mov->mode & MODE_3GP) {
2012 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2013 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2014 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2015 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2016 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2017 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2018 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2019 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2020 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2021 mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
2022 mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
2023 mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
2024 mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
2025 mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
2026 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
2027 mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
2028 mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
2029 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
2030 } else {
2031 /* iTunes meta data */
2032 mov_write_meta_tag(pb_buf, mov, s);
2033 }
1ee2d448 2034
21732063
MS
2035 if (s->nb_chapters)
2036 mov_write_chpl_tag(pb_buf, s);
dc75e4e3 2037
6dc7d80d 2038 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
21732063 2039 avio_wb32(pb, size + 8);
0abdb293 2040 ffio_wfourcc(pb, "udta");
77eb5504 2041 avio_write(pb, buf, size);
89a0d790 2042 }
8b6e0aec 2043 av_free(buf);
89a0d790
BF
2044
2045 return 0;
69dde1ad
GB
2046}
2047
ae628ec1 2048static void mov_write_psp_udta_tag(AVIOContext *pb,
21732063 2049 const char *str, const char *lang, int type)
20e22af8 2050{
21732063
MS
2051 int len = utf8len(str) + 1;
2052 if (len <= 0)
d32d6def 2053 return;
21732063 2054 avio_wb16(pb, len * 2 + 10); /* size */
77eb5504
AK
2055 avio_wb32(pb, type); /* type */
2056 avio_wb16(pb, language_code(lang)); /* language */
2057 avio_wb16(pb, 0x01); /* ? */
20e22af8 2058 ascii_to_wc(pb, str);
20e22af8
BC
2059}
2060
ae628ec1 2061static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
dcfdb046 2062{
d2d67e42 2063 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
bc5c918e 2064 int64_t pos, pos2;
dcfdb046 2065
1ee2d448 2066 if (title) {
a2704c97 2067 pos = avio_tell(pb);
77eb5504 2068 avio_wb32(pb, 0); /* size placeholder*/
0abdb293
AK
2069 ffio_wfourcc(pb, "uuid");
2070 ffio_wfourcc(pb, "USMT");
77eb5504
AK
2071 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2072 avio_wb32(pb, 0xbb88695c);
2073 avio_wb32(pb, 0xfac9c740);
dcfdb046 2074
a2704c97 2075 pos2 = avio_tell(pb);
77eb5504 2076 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 2077 ffio_wfourcc(pb, "MTDT");
77eb5504 2078 avio_wb16(pb, 4);
dcfdb046 2079
7cf0e16f 2080 // ?
77eb5504
AK
2081 avio_wb16(pb, 0x0C); /* size */
2082 avio_wb32(pb, 0x0B); /* type */
2083 avio_wb16(pb, language_code("und")); /* language */
2084 avio_wb16(pb, 0x0); /* ? */
2085 avio_wb16(pb, 0x021C); /* data */
7cf0e16f 2086
d32d6def 2087 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1ee2d448 2088 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
d32d6def
BC
2089 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2090
b7d3dd90
MS
2091 update_size(pb, pos2);
2092 return update_size(pb, pos);
dcfdb046
BL
2093 }
2094
d32d6def 2095 return 0;
dcfdb046
BL
2096}
2097
ae628ec1 2098static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
69dde1ad 2099 AVFormatContext *s)
1cb5f7fd 2100{
b29af723 2101 int i;
a2704c97 2102 int64_t pos = avio_tell(pb);
77eb5504 2103 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 2104 ffio_wfourcc(pb, "moov");
1cb5f7fd 2105
21732063 2106 for (i = 0; i < mov->nb_streams; i++) {
83988d58
MS
2107 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2108 continue;
e45ccf79 2109
21732063
MS
2110 mov->tracks[i].time = mov->time;
2111 mov->tracks[i].track_id = i + 1;
1cb5f7fd
MN
2112 }
2113
ddb63017 2114 if (mov->chapter_track)
21732063 2115 for (i = 0; i < s->nb_streams; i++) {
ddb63017 2116 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
21732063 2117 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
ddb63017 2118 }
e977af6f
MS
2119 for (i = 0; i < mov->nb_streams; i++) {
2120 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2121 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2122 mov->tracks[i].tref_id =
b7d3dd90 2123 mov->tracks[mov->tracks[i].src_track].track_id;
e977af6f
MS
2124 }
2125 }
ddb63017 2126
6e6d6dc0 2127 mov_write_mvhd_tag(pb, mov);
d3b8bde2
AC
2128 if (mov->mode != MODE_MOV && !mov->iods_skip)
2129 mov_write_iods_tag(pb, mov);
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 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1cb5f7fd
MN
2133 }
2134 }
83988d58
MS
2135 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2136 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
1cb5f7fd 2137
dcfdb046
BL
2138 if (mov->mode == MODE_PSP)
2139 mov_write_uuidusmt_tag(pb, s);
c6e2c6c9 2140 else
e344c1ea 2141 mov_write_udta_tag(pb, mov, s);
69dde1ad 2142
b7d3dd90 2143 return update_size(pb, pos);
1cb5f7fd
MN
2144}
2145
f5322104
MS
2146static void param_write_int(AVIOContext *pb, const char *name, int value)
2147{
2148 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2149}
2150
2151static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2152{
2153 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2154}
2155
2156static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2157{
2158 char buf[150];
21732063 2159 len = FFMIN(sizeof(buf) / 2 - 1, len);
f5322104 2160 ff_data_to_hex(buf, value, len, 0);
21732063 2161 buf[2 * len] = '\0';
f5322104
MS
2162 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2163}
2164
f5322104
MS
2165static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2166{
2167 int64_t pos = avio_tell(pb);
2168 int i;
2169 const uint8_t uuid[] = {
2170 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2171 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2172 };
2173
2174 avio_wb32(pb, 0);
2175 ffio_wfourcc(pb, "uuid");
2176 avio_write(pb, uuid, sizeof(uuid));
2177 avio_wb32(pb, 0);
2178
2179 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2180 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2181 avio_printf(pb, "<head>\n");
2182 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2183 LIBAVFORMAT_IDENT);
2184 avio_printf(pb, "</head>\n");
2185 avio_printf(pb, "<body>\n");
2186 avio_printf(pb, "<switch>\n");
2187 for (i = 0; i < mov->nb_streams; i++) {
2188 MOVTrack *track = &mov->tracks[i];
2189 const char *type;
b7d3dd90 2190 /* track->track_id is initialized in write_moov, and thus isn't known
f5322104
MS
2191 * here yet */
2192 int track_id = i + 1;
2193
2194 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2195 type = "video";
2196 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2197 type = "audio";
2198 } else {
2199 continue;
2200 }
2201 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2202 track->enc->bit_rate);
2203 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2204 param_write_int(pb, "trackID", track_id);
2205 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
36ef5369 2206 if (track->enc->codec_id == AV_CODEC_ID_H264) {
d5ed5e7d
MS
2207 uint8_t *ptr;
2208 int size = track->enc->extradata_size;
2209 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2210 &size)) {
2211 param_write_hex(pb, "CodecPrivateData",
2212 ptr ? ptr : track->enc->extradata,
2213 size);
2214 av_free(ptr);
2215 }
f5322104 2216 param_write_string(pb, "FourCC", "H264");
36ef5369 2217 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
f5322104 2218 param_write_string(pb, "FourCC", "WVC1");
68893afe
MS
2219 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2220 track->enc->extradata_size);
f5322104
MS
2221 }
2222 param_write_int(pb, "MaxWidth", track->enc->width);
2223 param_write_int(pb, "MaxHeight", track->enc->height);
2224 param_write_int(pb, "DisplayWidth", track->enc->width);
2225 param_write_int(pb, "DisplayHeight", track->enc->height);
2226 } else {
36ef5369 2227 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
f5322104 2228 param_write_string(pb, "FourCC", "AACL");
36ef5369 2229 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
f5322104
MS
2230 param_write_string(pb, "FourCC", "WMAP");
2231 }
2232 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2233 track->enc->extradata_size);
2234 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
21732063 2235 track->enc->codec_id));
f5322104
MS
2236 param_write_int(pb, "Channels", track->enc->channels);
2237 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2238 param_write_int(pb, "BitsPerSample", 16);
2239 param_write_int(pb, "PacketSize", track->enc->block_align ?
2240 track->enc->block_align : 4);
2241 }
2242 avio_printf(pb, "</%s>\n", type);
2243 }
2244 avio_printf(pb, "</switch>\n");
2245 avio_printf(pb, "</body>\n");
2246 avio_printf(pb, "</smil>\n");
2247
b7d3dd90 2248 return update_size(pb, pos);
f5322104
MS
2249}
2250
83988d58
MS
2251static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2252{
2253 avio_wb32(pb, 16);
2254 ffio_wfourcc(pb, "mfhd");
2255 avio_wb32(pb, 0);
2256 avio_wb32(pb, mov->fragments);
2257 return 0;
2258}
2259
e7bf085b
MS
2260static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov,
2261 MOVTrack *track, int64_t moof_offset)
83988d58
MS
2262{
2263 int64_t pos = avio_tell(pb);
73328f24
MS
2264 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2265 MOV_TFHD_BASE_DATA_OFFSET;
83988d58 2266 if (!track->entry) {
73328f24 2267 flags |= MOV_TFHD_DURATION_IS_EMPTY;
83988d58 2268 } else {
73328f24 2269 flags |= MOV_TFHD_DEFAULT_FLAGS;
83988d58 2270 }
e7bf085b
MS
2271 if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET)
2272 flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
83988d58 2273
51df7b23
MS
2274 /* Don't set a default sample size, the silverlight player refuses
2275 * to play files with that set. Don't set a default sample duration,
40bbbce7
JH
2276 * WMP freaks out if it is set. Don't set a base data offset, PIFF
2277 * file format says it MUST NOT be set. */
4ddd54da 2278 if (track->mode == MODE_ISM)
40bbbce7
JH
2279 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2280 MOV_TFHD_BASE_DATA_OFFSET);
4ddd54da 2281
83988d58
MS
2282 avio_wb32(pb, 0); /* size placeholder */
2283 ffio_wfourcc(pb, "tfhd");
2284 avio_w8(pb, 0); /* version */
2285 avio_wb24(pb, flags);
2286
b7d3dd90 2287 avio_wb32(pb, track->track_id); /* track-id */
73328f24 2288 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
83988d58 2289 avio_wb64(pb, moof_offset);
73328f24 2290 if (flags & MOV_TFHD_DEFAULT_DURATION) {
f3dab5fb 2291 track->default_duration = get_cluster_duration(track, 0);
83988d58
MS
2292 avio_wb32(pb, track->default_duration);
2293 }
73328f24 2294 if (flags & MOV_TFHD_DEFAULT_SIZE) {
83988d58
MS
2295 track->default_size = track->entry ? track->cluster[0].size : 1;
2296 avio_wb32(pb, track->default_size);
2297 } else
2298 track->default_size = -1;
2299
73328f24 2300 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
83988d58
MS
2301 track->default_sample_flags =
2302 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
5be805d3
MS
2303 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2304 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
83988d58
MS
2305 avio_wb32(pb, track->default_sample_flags);
2306 }
2307
b7d3dd90 2308 return update_size(pb, pos);
83988d58
MS
2309}
2310
2311static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2312{
5be805d3
MS
2313 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2314 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
83988d58
MS
2315}
2316
e7bf085b
MS
2317static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov,
2318 MOVTrack *track, int moof_size)
83988d58
MS
2319{
2320 int64_t pos = avio_tell(pb);
3eec23f3 2321 uint32_t flags = MOV_TRUN_DATA_OFFSET;
83988d58
MS
2322 int i;
2323
2324 for (i = 0; i < track->entry; i++) {
681d1726 2325 if (get_cluster_duration(track, i) != track->default_duration)
3eec23f3 2326 flags |= MOV_TRUN_SAMPLE_DURATION;
83988d58 2327 if (track->cluster[i].size != track->default_size)
3eec23f3 2328 flags |= MOV_TRUN_SAMPLE_SIZE;
83988d58 2329 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
3eec23f3 2330 flags |= MOV_TRUN_SAMPLE_FLAGS;
83988d58 2331 }
3eec23f3
MS
2332 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2333 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
83988d58 2334 if (track->flags & MOV_TRACK_CTTS)
3eec23f3 2335 flags |= MOV_TRUN_SAMPLE_CTS;
83988d58
MS
2336
2337 avio_wb32(pb, 0); /* size placeholder */
2338 ffio_wfourcc(pb, "trun");
2339 avio_w8(pb, 0); /* version */
2340 avio_wb24(pb, flags);
2341
2342 avio_wb32(pb, track->entry); /* sample count */
e7bf085b
MS
2343 if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
2344 !(mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) &&
2345 track->track_id != 1)
2346 avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
2347 else
2348 avio_wb32(pb, moof_size + 8 + track->data_offset +
2349 track->cluster[0].pos); /* data offset */
3eec23f3 2350 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
83988d58
MS
2351 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2352
2353 for (i = 0; i < track->entry; i++) {
3eec23f3 2354 if (flags & MOV_TRUN_SAMPLE_DURATION)
681d1726 2355 avio_wb32(pb, get_cluster_duration(track, i));
3eec23f3 2356 if (flags & MOV_TRUN_SAMPLE_SIZE)
83988d58 2357 avio_wb32(pb, track->cluster[i].size);
3eec23f3 2358 if (flags & MOV_TRUN_SAMPLE_FLAGS)
83988d58 2359 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
3eec23f3 2360 if (flags & MOV_TRUN_SAMPLE_CTS)
83988d58
MS
2361 avio_wb32(pb, track->cluster[i].cts);
2362 }
2363
b7d3dd90 2364 return update_size(pb, pos);
83988d58
MS
2365}
2366
4ddd54da
MS
2367static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2368{
2369 int64_t pos = avio_tell(pb);
2370 const uint8_t uuid[] = {
2371 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2372 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2373 };
2374
2375 avio_wb32(pb, 0); /* size placeholder */
2376 ffio_wfourcc(pb, "uuid");
2377 avio_write(pb, uuid, sizeof(uuid));
2378 avio_w8(pb, 1);
2379 avio_wb24(pb, 0);
2380 avio_wb64(pb, track->frag_start);
b7d3dd90 2381 avio_wb64(pb, track->start_dts + track->track_duration -
4ddd54da
MS
2382 track->cluster[0].dts);
2383
b7d3dd90 2384 return update_size(pb, pos);
4ddd54da
MS
2385}
2386
2387static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2388 MOVTrack *track, int entry)
2389{
2390 int n = track->nb_frag_info - 1 - entry, i;
2391 int size = 8 + 16 + 4 + 1 + 16*n;
2392 const uint8_t uuid[] = {
2393 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2394 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2395 };
2396
2397 if (entry < 0)
2398 return 0;
2399
2400 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2401 avio_wb32(pb, size);
2402 ffio_wfourcc(pb, "uuid");
2403 avio_write(pb, uuid, sizeof(uuid));
2404 avio_w8(pb, 1);
2405 avio_wb24(pb, 0);
2406 avio_w8(pb, n);
2407 for (i = 0; i < n; i++) {
2408 int index = entry + 1 + i;
2409 avio_wb64(pb, track->frag_info[index].time);
2410 avio_wb64(pb, track->frag_info[index].duration);
2411 }
2412 if (n < mov->ism_lookahead) {
21732063 2413 int free_size = 16 * (mov->ism_lookahead - n);
4ddd54da
MS
2414 avio_wb32(pb, free_size);
2415 ffio_wfourcc(pb, "free");
2416 for (i = 0; i < free_size - 8; i++)
2417 avio_w8(pb, 0);
2418 }
2419
2420 return 0;
2421}
2422
2423static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2424 MOVTrack *track)
2425{
2426 int64_t pos = avio_tell(pb);
2427 int i;
2428 for (i = 0; i < mov->ism_lookahead; i++) {
2429 /* Update the tfrf tag for the last ism_lookahead fragments,
2430 * nb_frag_info - 1 is the next fragment to be written. */
2431 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2432 }
2433 avio_seek(pb, pos, SEEK_SET);
2434 return 0;
2435}
2436
2437static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
a4adabb4
MS
2438 MOVTrack *track, int64_t moof_offset,
2439 int moof_size)
83988d58
MS
2440{
2441 int64_t pos = avio_tell(pb);
2442 avio_wb32(pb, 0); /* size placeholder */
2443 ffio_wfourcc(pb, "traf");
2444
e7bf085b
MS
2445 mov_write_tfhd_tag(pb, mov, track, moof_offset);
2446 mov_write_trun_tag(pb, mov, track, moof_size);
4ddd54da
MS
2447 if (mov->mode == MODE_ISM) {
2448 mov_write_tfxd_tag(pb, track);
2449
2450 if (mov->ism_lookahead) {
21732063 2451 int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
4ddd54da
MS
2452
2453 track->tfrf_offset = avio_tell(pb);
2454 avio_wb32(pb, 8 + size);
2455 ffio_wfourcc(pb, "free");
2456 for (i = 0; i < size; i++)
2457 avio_w8(pb, 0);
2458 }
2459 }
83988d58 2460
b7d3dd90 2461 return update_size(pb, pos);
83988d58
MS
2462}
2463
a4adabb4
MS
2464static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
2465 int tracks, int moof_size)
83988d58 2466{
a4adabb4
MS
2467 int64_t pos = avio_tell(pb);
2468 int i;
83988d58
MS
2469
2470 avio_wb32(pb, 0); /* size placeholder */
2471 ffio_wfourcc(pb, "moof");
2472
2473 mov_write_mfhd_tag(pb, mov);
2474 for (i = 0; i < mov->nb_streams; i++) {
2475 MOVTrack *track = &mov->tracks[i];
2476 if (tracks >= 0 && i != tracks)
2477 continue;
2478 if (!track->entry)
2479 continue;
a4adabb4 2480 mov_write_traf_tag(pb, mov, track, pos, moof_size);
83988d58
MS
2481 }
2482
b7d3dd90 2483 return update_size(pb, pos);
83988d58
MS
2484}
2485
a4adabb4
MS
2486static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2487{
2488 AVIOContext *avio_buf;
2489 int ret, moof_size;
a4adabb4 2490
72fe16a1 2491 if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
a4adabb4
MS
2492 return ret;
2493 mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
72fe16a1 2494 moof_size = ffio_close_null_buf(avio_buf);
a4adabb4
MS
2495 return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
2496}
2497
83988d58
MS
2498static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2499{
2500 int64_t pos = avio_tell(pb);
2501 int i;
2502
2503 avio_wb32(pb, 0); /* size placeholder */
2504 ffio_wfourcc(pb, "tfra");
2505 avio_w8(pb, 1); /* version */
2506 avio_wb24(pb, 0);
2507
b7d3dd90 2508 avio_wb32(pb, track->track_id);
83988d58
MS
2509 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2510 avio_wb32(pb, track->nb_frag_info);
2511 for (i = 0; i < track->nb_frag_info; i++) {
2512 avio_wb64(pb, track->frag_info[i].time);
2513 avio_wb64(pb, track->frag_info[i].offset);
2514 avio_w8(pb, 1); /* traf number */
2515 avio_w8(pb, 1); /* trun number */
2516 avio_w8(pb, 1); /* sample number */
2517 }
2518
b7d3dd90 2519 return update_size(pb, pos);
83988d58
MS
2520}
2521
2522static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2523{
2524 int64_t pos = avio_tell(pb);
2525 int i;
2526
2527 avio_wb32(pb, 0); /* size placeholder */
2528 ffio_wfourcc(pb, "mfra");
f5322104
MS
2529 /* An empty mfra atom is enough to indicate to the publishing point that
2530 * the stream has ended. */
2531 if (mov->flags & FF_MOV_FLAG_ISML)
b7d3dd90 2532 return update_size(pb, pos);
83988d58
MS
2533
2534 for (i = 0; i < mov->nb_streams; i++) {
2535 MOVTrack *track = &mov->tracks[i];
2536 if (track->nb_frag_info)
2537 mov_write_tfra_tag(pb, track);
2538 }
2539
2540 avio_wb32(pb, 16);
2541 ffio_wfourcc(pb, "mfro");
2542 avio_wb32(pb, 0); /* version + flags */
2543 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2544
b7d3dd90 2545 return update_size(pb, pos);
83988d58
MS
2546}
2547
ae628ec1 2548static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 2549{
77eb5504 2550 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
0abdb293 2551 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
b29af723 2552
a2704c97 2553 mov->mdat_pos = avio_tell(pb);
77eb5504 2554 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 2555 ffio_wfourcc(pb, "mdat");
1cb5f7fd
MN
2556 return 0;
2557}
2558
2559/* TODO: This needs to be more general */
ae628ec1 2560static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
1cb5f7fd 2561{
2d243fb3 2562 MOVMuxContext *mov = s->priv_data;
a2704c97 2563 int64_t pos = avio_tell(pb);
cd70d17f 2564 int has_h264 = 0, has_video = 0;
40f8675c 2565 int minor = 0x200;
c55cc074 2566 int i;
69dde1ad 2567
83579084
BC
2568 for (i = 0; i < s->nb_streams; i++) {
2569 AVStream *st = s->streams[i];
72415b2a 2570 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
cd70d17f 2571 has_video = 1;
36ef5369 2572 if (st->codec->codec_id == AV_CODEC_ID_H264)
83579084
BC
2573 has_h264 = 1;
2574 }
2575
77eb5504 2576 avio_wb32(pb, 0); /* size */
0abdb293 2577 ffio_wfourcc(pb, "ftyp");
e45ccf79 2578
1e9db41e
JS
2579 if (mov->major_brand && strlen(mov->major_brand) >= 4)
2580 ffio_wfourcc(pb, mov->major_brand);
2581 else if (mov->mode == MODE_3GP) {
0abdb293 2582 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
40f8675c
BC
2583 minor = has_h264 ? 0x100 : 0x200;
2584 } else if (mov->mode & MODE_3G2) {
0abdb293 2585 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
40f8675c 2586 minor = has_h264 ? 0x20000 : 0x10000;
21732063 2587 } else if (mov->mode == MODE_PSP)
0abdb293 2588 ffio_wfourcc(pb, "MSNV");
c55cc074 2589 else if (mov->mode == MODE_MP4)
0abdb293 2590 ffio_wfourcc(pb, "isom");
cd70d17f 2591 else if (mov->mode == MODE_IPOD)
0abdb293 2592 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
4ddd54da
MS
2593 else if (mov->mode == MODE_ISM)
2594 ffio_wfourcc(pb, "isml");
e01fba55
CB
2595 else if (mov->mode == MODE_F4V)
2596 ffio_wfourcc(pb, "f4v ");
cd70d17f 2597 else
0abdb293 2598 ffio_wfourcc(pb, "qt ");
e45ccf79 2599
77eb5504 2600 avio_wb32(pb, minor);
e45ccf79 2601
21732063 2602 if (mov->mode == MODE_MOV)
0abdb293 2603 ffio_wfourcc(pb, "qt ");
4ddd54da
MS
2604 else if (mov->mode == MODE_ISM) {
2605 ffio_wfourcc(pb, "piff");
2606 ffio_wfourcc(pb, "iso2");
2607 } else {
0abdb293
AK
2608 ffio_wfourcc(pb, "isom");
2609 ffio_wfourcc(pb, "iso2");
21732063 2610 if (has_h264)
0abdb293 2611 ffio_wfourcc(pb, "avc1");
6ba9ed95 2612 }
ed6e47c2 2613
91208916 2614 if (mov->mode == MODE_3GP)
0abdb293 2615 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
a04aec6d 2616 else if (mov->mode & MODE_3G2)
0abdb293 2617 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
91208916 2618 else if (mov->mode == MODE_PSP)
0abdb293 2619 ffio_wfourcc(pb, "MSNV");
c55cc074 2620 else if (mov->mode == MODE_MP4)
0abdb293 2621 ffio_wfourcc(pb, "mp41");
b7d3dd90 2622 return update_size(pb, pos);
1cb5f7fd
MN
2623}
2624
ae628ec1 2625static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
8af18154 2626{
b7d3dd90
MS
2627 AVCodecContext *video_codec = s->streams[0]->codec;
2628 AVCodecContext *audio_codec = s->streams[1]->codec;
2629 int audio_rate = audio_codec->sample_rate;
21732063 2630 int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
b7d3dd90
MS
2631 int audio_kbitrate = audio_codec->bit_rate / 1000;
2632 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
8af18154 2633
77eb5504 2634 avio_wb32(pb, 0x94); /* size */
0abdb293
AK
2635 ffio_wfourcc(pb, "uuid");
2636 ffio_wfourcc(pb, "PROF");
8af18154 2637
77eb5504
AK
2638 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2639 avio_wb32(pb, 0xbb88695c);
2640 avio_wb32(pb, 0xfac9c740);
8af18154 2641
77eb5504
AK
2642 avio_wb32(pb, 0x0); /* ? */
2643 avio_wb32(pb, 0x3); /* 3 sections ? */
8af18154 2644
77eb5504 2645 avio_wb32(pb, 0x14); /* size */
0abdb293 2646 ffio_wfourcc(pb, "FPRF");
77eb5504
AK
2647 avio_wb32(pb, 0x0); /* ? */
2648 avio_wb32(pb, 0x0); /* ? */
2649 avio_wb32(pb, 0x0); /* ? */
8af18154 2650
77eb5504 2651 avio_wb32(pb, 0x2c); /* size */
21732063 2652 ffio_wfourcc(pb, "APRF"); /* audio */
77eb5504
AK
2653 avio_wb32(pb, 0x0);
2654 avio_wb32(pb, 0x2); /* TrackID */
0abdb293 2655 ffio_wfourcc(pb, "mp4a");
77eb5504
AK
2656 avio_wb32(pb, 0x20f);
2657 avio_wb32(pb, 0x0);
2658 avio_wb32(pb, audio_kbitrate);
2659 avio_wb32(pb, audio_kbitrate);
b7d3dd90
MS
2660 avio_wb32(pb, audio_rate);
2661 avio_wb32(pb, audio_codec->channels);
77eb5504
AK
2662
2663 avio_wb32(pb, 0x34); /* size */
0abdb293 2664 ffio_wfourcc(pb, "VPRF"); /* video */
77eb5504
AK
2665 avio_wb32(pb, 0x0);
2666 avio_wb32(pb, 0x1); /* TrackID */
36ef5369 2667 if (video_codec->codec_id == AV_CODEC_ID_H264) {
0abdb293 2668 ffio_wfourcc(pb, "avc1");
77eb5504
AK
2669 avio_wb16(pb, 0x014D);
2670 avio_wb16(pb, 0x0015);
87b041e0 2671 } else {
0abdb293 2672 ffio_wfourcc(pb, "mp4v");
77eb5504
AK
2673 avio_wb16(pb, 0x0000);
2674 avio_wb16(pb, 0x0103);
87b041e0 2675 }
77eb5504
AK
2676 avio_wb32(pb, 0x0);
2677 avio_wb32(pb, video_kbitrate);
2678 avio_wb32(pb, video_kbitrate);
b7d3dd90
MS
2679 avio_wb32(pb, frame_rate);
2680 avio_wb32(pb, frame_rate);
2681 avio_wb16(pb, video_codec->width);
2682 avio_wb16(pb, video_codec->height);
77eb5504 2683 avio_wb32(pb, 0x010001); /* ? */
8af18154 2684}
2685
e1316b19
BC
2686static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2687{
2688 uint32_t c = -1;
2689 int i, closed_gop = 0;
2690
2691 for (i = 0; i < pkt->size - 4; i++) {
21732063 2692 c = (c << 8) + pkt->data[i];
e1316b19 2693 if (c == 0x1b8) { // gop
21732063 2694 closed_gop = pkt->data[i + 4] >> 6 & 0x01;
e1316b19 2695 } else if (c == 0x100) { // pic
21732063 2696 int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
e1316b19
BC
2697 if (!temp_ref || closed_gop) // I picture is not reordered
2698 *flags = MOV_SYNC_SAMPLE;
2699 else
2700 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2701 break;
2702 }
2703 }
2704 return 0;
2705}
2706
4ace130e
MS
2707static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2708{
2709 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2710 int seq = 0, entry = 0;
2711 int key = pkt->flags & AV_PKT_FLAG_KEY;
2712 start = find_next_marker(pkt->data, end);
2713 for (next = start; next < end; start = next) {
2714 next = find_next_marker(start + 4, end);
2715 switch (AV_RB32(start)) {
2716 case VC1_CODE_SEQHDR:
2717 seq = 1;
2718 break;
2719 case VC1_CODE_ENTRYPOINT:
2720 entry = 1;
2721 break;
2722 case VC1_CODE_SLICE:
2723 trk->vc1_info.slices = 1;
2724 break;
2725 }
2726 }
2727 if (!trk->entry && !fragment) {
2728 /* First packet in first fragment */
2729 trk->vc1_info.first_packet_seq = seq;
2730 trk->vc1_info.first_packet_entry = entry;
2731 } else if ((seq && !trk->vc1_info.packet_seq) ||
2732 (entry && !trk->vc1_info.packet_entry)) {
2733 int i;
2734 for (i = 0; i < trk->entry; i++)
2735 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2736 trk->has_keyframes = 0;
2737 if (seq)
2738 trk->vc1_info.packet_seq = 1;
2739 if (entry)
2740 trk->vc1_info.packet_entry = 1;
2741 if (!fragment) {
2742 /* First fragment */
2743 if ((!seq || trk->vc1_info.first_packet_seq) &&
2744 (!entry || trk->vc1_info.first_packet_entry)) {
2745 /* First packet had the same headers as this one, readd the
2746 * sync sample flag. */
2747 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2748 trk->has_keyframes = 1;
2749 }
2750 }
2751 }
2752 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2753 key = seq && entry;
2754 else if (trk->vc1_info.packet_seq)
2755 key = seq;
2756 else if (trk->vc1_info.packet_entry)
2757 key = entry;
2758 if (key) {
2759 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2760 trk->has_keyframes++;
2761 }
2762}
2763
83988d58
MS
2764static int mov_flush_fragment(AVFormatContext *s)
2765{
2766 MOVMuxContext *mov = s->priv_data;
2767 int i, first_track = -1;
2768 int64_t mdat_size = 0;
2769
2770 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2771 return 0;
2772
2773 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2774 int64_t pos = avio_tell(s->pb);
0c7b8b75
MS
2775 int ret;
2776 AVIOContext *moov_buf;
2777 uint8_t *buf;
2778 int buf_size;
83988d58
MS
2779
2780 for (i = 0; i < mov->nb_streams; i++)
2781 if (!mov->tracks[i].entry)
2782 break;
2783 /* Don't write the initial moov unless all tracks have data */
2784 if (i < mov->nb_streams)
2785 return 0;
0c7b8b75 2786
72fe16a1 2787 if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
0c7b8b75
MS
2788 return ret;
2789 mov_write_moov_tag(moov_buf, mov, s);
72fe16a1 2790 buf_size = ffio_close_null_buf(moov_buf);
0c7b8b75
MS
2791 for (i = 0; i < mov->nb_streams; i++)
2792 mov->tracks[i].data_offset = pos + buf_size + 8;
2793
83988d58 2794 mov_write_moov_tag(s->pb, mov, s);
0c7b8b75
MS
2795
2796 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2797 mov->mdat_buf = NULL;
2798 avio_wb32(s->pb, buf_size + 8);
2799 ffio_wfourcc(s->pb, "mdat");
2800 avio_write(s->pb, buf, buf_size);
2801 av_free(buf);
2802
83988d58
MS
2803 mov->fragments++;
2804 mov->mdat_size = 0;
2805 for (i = 0; i < mov->nb_streams; i++) {
2806 if (mov->tracks[i].entry)
2807 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
b7d3dd90 2808 mov->tracks[i].track_duration -
83988d58
MS
2809 mov->tracks[i].cluster[0].dts;
2810 mov->tracks[i].entry = 0;
2811 }
e771e6dd 2812 avio_flush(s->pb);
83988d58
MS
2813 return 0;
2814 }
2815
2816 for (i = 0; i < mov->nb_streams; i++) {
2817 MOVTrack *track = &mov->tracks[i];
2818 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2819 track->data_offset = 0;
2820 else
2821 track->data_offset = mdat_size;
2822 if (!track->mdat_buf)
2823 continue;
2824 mdat_size += avio_tell(track->mdat_buf);
2825 if (first_track < 0)
2826 first_track = i;
2827 }
2828
2829 if (!mdat_size)
2830 return 0;
2831
2832 for (i = 0; i < mov->nb_streams; i++) {
2833 MOVTrack *track = &mov->tracks[i];
2834 int buf_size, write_moof = 1, moof_tracks = -1;
2835 uint8_t *buf;
2836 int64_t duration = 0;
2837
2838 if (track->entry)
b7d3dd90 2839 duration = track->start_dts + track->track_duration -
83988d58
MS
2840 track->cluster[0].dts;
2841 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2842 if (!track->mdat_buf)
2843 continue;
2844 mdat_size = avio_tell(track->mdat_buf);
2845 moof_tracks = i;
2846 } else {
2847 write_moof = i == first_track;
2848 }
2849
2850 if (write_moof) {
2851 MOVFragmentInfo *info;
e771e6dd 2852 avio_flush(s->pb);
83988d58 2853 track->nb_frag_info++;
7c020e1a
AS
2854 if (track->nb_frag_info >= track->frag_info_capacity) {
2855 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
2856 if (av_reallocp_array(&track->frag_info,
2857 new_capacity,
2858 sizeof(*track->frag_info)))
2859 return AVERROR(ENOMEM);
2860 track->frag_info_capacity = new_capacity;
2861 }
83988d58
MS
2862 info = &track->frag_info[track->nb_frag_info - 1];
2863 info->offset = avio_tell(s->pb);
2864 info->time = mov->tracks[i].frag_start;
4ddd54da
MS
2865 info->duration = duration;
2866 mov_write_tfrf_tags(s->pb, mov, track);
83988d58
MS
2867
2868 mov_write_moof_tag(s->pb, mov, moof_tracks);
4ddd54da 2869 info->tfrf_offset = track->tfrf_offset;
83988d58
MS
2870 mov->fragments++;
2871
2872 avio_wb32(s->pb, mdat_size + 8);
2873 ffio_wfourcc(s->pb, "mdat");
2874 }
2875
2876 if (track->entry)
2877 track->frag_start += duration;
2878 track->entry = 0;
2879 if (!track->mdat_buf)
2880 continue;
2881 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2882 track->mdat_buf = NULL;
2883
2884 avio_write(s->pb, buf, buf_size);
2885 av_free(buf);
2886 }
2887
2888 mov->mdat_size = 0;
2889
e771e6dd 2890 avio_flush(s->pb);
83988d58
MS
2891 return 0;
2892}
2893
ebbede22 2894int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd 2895{
2d243fb3 2896 MOVMuxContext *mov = s->priv_data;
ae628ec1 2897 AVIOContext *pb = s->pb;
039627cf
BC
2898 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2899 AVCodecContext *enc = trk->enc;
b7d3dd90 2900 unsigned int samples_in_chunk = 0;
21732063 2901 int size = pkt->size;
e2484fb6 2902 uint8_t *reformatted_data = NULL;
83988d58 2903
0c7b8b75
MS
2904 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2905 int ret;
2906 if (mov->fragments > 0) {
2907 if (!trk->mdat_buf) {
2908 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2909 return ret;
2910 }
2911 pb = trk->mdat_buf;
2912 } else {
2913 if (!mov->mdat_buf) {
2914 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2915 return ret;
2916 }
2917 pb = mov->mdat_buf;
83988d58 2918 }
83988d58
MS
2919 }
2920
36ef5369 2921 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
5cb49ca1
BC
2922 /* We must find out how many AMR blocks there are in one packet */
2923 static uint16_t packed_size[16] =
ed669c9b 2924 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
5cb49ca1
BC
2925 int len = 0;
2926
b7d3dd90 2927 while (len < size && samples_in_chunk < 100) {
5cb49ca1 2928 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
b7d3dd90 2929 samples_in_chunk++;
1cb5f7fd 2930 }
b7d3dd90 2931 if (samples_in_chunk > 1) {
89938d29 2932 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2111e3f9
MN
2933 return -1;
2934 }
b7d3dd90
MS
2935 } else if (trk->sample_size)
2936 samples_in_chunk = size / trk->sample_size;
5cb49ca1 2937 else
b7d3dd90 2938 samples_in_chunk = 1;
e45ccf79 2939
5616f85d 2940 /* copy extradata if it exists */
b7d3dd90 2941 if (trk->vos_len == 0 && enc->extradata_size > 0) {
21732063 2942 trk->vos_len = enc->extradata_size;
b7d3dd90
MS
2943 trk->vos_data = av_malloc(trk->vos_len);
2944 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
e45ccf79
GB
2945 }
2946
36ef5369 2947 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
5d2160a0
BC
2948 /* from x264 or from bytestream h264 */
2949 /* nal reformating needed */
e2484fb6
MS
2950 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2951 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2952 &size);
2953 avio_write(pb, reformatted_data, size);
2954 } else {
2955 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2956 }
b6c61fb8
TW
2957 } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
2958 (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
2959 /* extradata is Annex B, assume the bitstream is too and convert it */
2960 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2961 ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
2962 avio_write(pb, reformatted_data, size);
2963 } else {
2964 size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
2965 }
5d2160a0 2966 } else {
77eb5504 2967 avio_write(pb, pkt->data, size);
5d2160a0
BC
2968 }
2969
36ef5369
AK
2970 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
2971 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
db568c07 2972 /* copy frame to create needed atoms */
21732063 2973 trk->vos_len = size;
b7d3dd90
MS
2974 trk->vos_data = av_malloc(size);
2975 if (!trk->vos_data)
e8c4df40 2976 return AVERROR(ENOMEM);
b7d3dd90 2977 memcpy(trk->vos_data, pkt->data, size);
c1b8e6d8
BC
2978 }
2979
3b4feac1 2980 if (trk->entry >= trk->cluster_capacity) {
21732063 2981 unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
3b4feac1
AS
2982 if (av_reallocp_array(&trk->cluster, new_capacity,
2983 sizeof(*trk->cluster)))
2984 return AVERROR(ENOMEM);
2985 trk->cluster_capacity = new_capacity;
e45ccf79 2986 }
e45ccf79 2987
21732063 2988 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
b7d3dd90 2989 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
21732063
MS
2990 trk->cluster[trk->entry].size = size;
2991 trk->cluster[trk->entry].entries = samples_in_chunk;
2992 trk->cluster[trk->entry].dts = pkt->dts;
83988d58
MS
2993 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
2994 /* First packet of a new fragment. We already wrote the duration
b7d3dd90 2995 * of the last packet of the previous fragment based on track_duration,
83988d58
MS
2996 * which might not exactly match our dts. Therefore adjust the dts
2997 * of this packet to be what the previous packets duration implies. */
b7d3dd90 2998 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
83988d58 2999 }
6ca38568
MS
3000 if (trk->start_dts == AV_NOPTS_VALUE)
3001 trk->start_dts = pkt->dts;
b7d3dd90 3002 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
aa90239f 3003
96f69e0d
BC
3004 if (pkt->pts == AV_NOPTS_VALUE) {
3005 av_log(s, AV_LOG_WARNING, "pts has no value\n");
3006 pkt->pts = pkt->dts;
3007 }
c4f078ff 3008 if (pkt->dts != pkt->pts)
e1316b19 3009 trk->flags |= MOV_TRACK_CTTS;
21732063 3010 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
e1316b19 3011 trk->cluster[trk->entry].flags = 0;
36ef5369 3012 if (enc->codec_id == AV_CODEC_ID_VC1) {
4ace130e
MS
3013 mov_parse_vc1_frame(pkt, trk, mov->fragments);
3014 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
36ef5369 3015 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
44b11207 3016 trk->entry > 0) { // force sync sample for the first key frame
e1316b19
BC
3017 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3018 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3019 trk->flags |= MOV_TRACK_STPS;
3020 } else {
3021 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3022 }
3023 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
b7d3dd90 3024 trk->has_keyframes++;
ab4752e3 3025 }
e45ccf79 3026 trk->entry++;
b7d3dd90 3027 trk->sample_count += samples_in_chunk;
21732063 3028 mov->mdat_size += size;
e45ccf79 3029
e977af6f 3030 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
e2484fb6
MS
3031 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3032 reformatted_data, size);
3033 av_free(reformatted_data);
1cb5f7fd
MN
3034 return 0;
3035}
3036
ebbede22 3037static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
b613ff5e
MS
3038{
3039 if (!pkt) {
3040 mov_flush_fragment(s);
3041 return 1;
3042 } else {
ebbede22
LB
3043 MOVMuxContext *mov = s->priv_data;
3044 MOVTrack *trk = &mov->tracks[pkt->stream_index];
3045 AVCodecContext *enc = trk->enc;
3046 int64_t frag_duration = 0;
3047 int size = pkt->size;
3048
21732063
MS
3049 if (!pkt->size)
3050 return 0; /* Discard 0 sized packets */
ebbede22
LB
3051
3052 if (trk->entry)
3053 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3054 s->streams[pkt->stream_index]->time_base,
3055 AV_TIME_BASE_Q);
3056 if ((mov->max_fragment_duration &&
3057 frag_duration >= mov->max_fragment_duration) ||
3058 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3059 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3060 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
3061 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3062 if (frag_duration >= mov->min_fragment_duration)
3063 mov_flush_fragment(s);
3064 }
3065