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