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