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