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