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