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