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