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