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