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