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