don't trigger metadata compatibility code when user app already set metadata
[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>
1cb5f7fd 5 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
1cb5f7fd
MN
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
1cb5f7fd 12 *
b78e7197 13 * FFmpeg is distributed in the hope that it will be useful,
1cb5f7fd
MN
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
b78e7197 19 * License along with FFmpeg; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1cb5f7fd
MN
21 */
22#include "avformat.h"
9d9f4119 23#include "riff.h"
1cb5f7fd 24#include "avio.h"
e40ee6a2 25#include "isom.h"
1bd2d763 26#include "avc.h"
d4a240cb 27#include "libavcodec/bitstream.h"
1cb5f7fd 28
6e6d6dc0
MN
29#undef NDEBUG
30#include <assert.h>
31
1cb5f7fd
MN
32#define MOV_INDEX_CLUSTER_SIZE 16384
33#define globalTimescale 1000
34
2c77994f
MN
35#define MODE_MP4 0x01
36#define MODE_MOV 0x02
37#define MODE_3GP 0x04
38#define MODE_PSP 0x08 // example working PSP command line:
8af18154 39// ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
2c77994f
MN
40#define MODE_3G2 0x10
41#define MODE_IPOD 0x20
69dde1ad 42
1cb5f7fd 43typedef struct MOVIentry {
b29af723
MN
44 unsigned int flags, size;
45 uint64_t pos;
e45ccf79 46 unsigned int samplesInChunk;
f578f938 47 char key_frame;
1cb5f7fd 48 unsigned int entries;
b4712e3c 49 int64_t cts;
aa90239f 50 int64_t dts;
1cb5f7fd
MN
51} MOVIentry;
52
53typedef struct MOVIndex {
69dde1ad 54 int mode;
1cb5f7fd 55 int entry;
1cb5f7fd
MN
56 long timescale;
57 long time;
a3a80ddc 58 int64_t trackDuration;
e45ccf79 59 long sampleCount;
5cb49ca1 60 long sampleSize;
f578f938 61 int hasKeyframes;
b4712e3c 62 int hasBframes;
ab561df9 63 int language;
1cb5f7fd 64 int trackID;
2c2f58c0 65 int tag; ///< stsd fourcc
1cb5f7fd
MN
66 AVCodecContext *enc;
67
68 int vosLen;
6e6d6dc0 69 uint8_t *vosData;
0aec3c5c 70 MOVIentry *cluster;
aa90239f 71 int audio_vbr;
6a460c36 72 int height; ///< active picture (w/o VBI) height for D-10/IMX
1cb5f7fd
MN
73} MOVTrack;
74
8af18154 75typedef struct MOVContext {
69dde1ad 76 int mode;
a3a80ddc 77 int64_t time;
1cb5f7fd 78 int nb_streams;
bc5c918e 79 int64_t mdat_pos;
039627cf 80 uint64_t mdat_size;
1cb5f7fd
MN
81 long timescale;
82 MOVTrack tracks[MAX_STREAMS];
83} MOVContext;
84
90b5b51e 85//FIXME support 64 bit variant with wide placeholders
bc5c918e 86static int64_t updateSize(ByteIOContext *pb, int64_t pos)
1cb5f7fd 87{
bc5c918e 88 int64_t curpos = url_ftell(pb);
1cb5f7fd 89 url_fseek(pb, pos, SEEK_SET);
6e6d6dc0 90 put_be32(pb, curpos - pos); /* rewrite size */
1cb5f7fd 91 url_fseek(pb, curpos, SEEK_SET);
6e6d6dc0
MN
92
93 return curpos - pos;
1cb5f7fd
MN
94}
95
e45ccf79 96/* Chunk offset atom */
0724373d 97static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd
MN
98{
99 int i;
ed06cf6d 100 int mode64 = 0; // use 32 bit size variant if possible
bc5c918e 101 int64_t pos = url_ftell(pb);
f578f938 102 put_be32(pb, 0); /* size */
b29af723
MN
103 if (pos > UINT32_MAX) {
104 mode64 = 1;
105 put_tag(pb, "co64");
106 } else
107 put_tag(pb, "stco");
1cb5f7fd
MN
108 put_be32(pb, 0); /* version & flags */
109 put_be32(pb, track->entry); /* entry count */
110 for (i=0; i<track->entry; i++) {
b29af723 111 if(mode64 == 1)
0aec3c5c 112 put_be64(pb, track->cluster[i].pos);
b29af723 113 else
0aec3c5c 114 put_be32(pb, track->cluster[i].pos);
1cb5f7fd 115 }
f7635edb 116 return updateSize(pb, pos);
1cb5f7fd
MN
117}
118
e45ccf79 119/* Sample size atom */
0724373d 120static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 121{
f578f938 122 int equalChunks = 1;
e45ccf79 123 int i, j, entries = 0, tst = -1, oldtst = -1;
1cb5f7fd 124
bc5c918e 125 int64_t pos = url_ftell(pb);
f578f938 126 put_be32(pb, 0); /* size */
1cb5f7fd
MN
127 put_tag(pb, "stsz");
128 put_be32(pb, 0); /* version & flags */
129
f578f938 130 for (i=0; i<track->entry; i++) {
0aec3c5c 131 tst = track->cluster[i].size/track->cluster[i].entries;
e45ccf79
GB
132 if(oldtst != -1 && tst != oldtst) {
133 equalChunks = 0;
f578f938
TR
134 }
135 oldtst = tst;
0aec3c5c 136 entries += track->cluster[i].entries;
f578f938 137 }
e45ccf79 138 if (equalChunks) {
0aec3c5c 139 int sSize = track->cluster[0].size/track->cluster[0].entries;
115329f1 140 put_be32(pb, sSize); // sample size
e45ccf79 141 put_be32(pb, entries); // sample count
1cb5f7fd 142 }
f578f938 143 else {
115329f1
DB
144 put_be32(pb, 0); // sample size
145 put_be32(pb, entries); // sample count
f578f938 146 for (i=0; i<track->entry; i++) {
91208916 147 for (j=0; j<track->cluster[i].entries; j++) {
0aec3c5c
BC
148 put_be32(pb, track->cluster[i].size /
149 track->cluster[i].entries);
e45ccf79 150 }
1cb5f7fd
MN
151 }
152 }
f7635edb 153 return updateSize(pb, pos);
1cb5f7fd
MN
154}
155
e45ccf79 156/* Sample to chunk atom */
0724373d 157static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 158{
b29af723 159 int index = 0, oldval = -1, i;
bc5c918e 160 int64_t entryPos, curpos;
f578f938 161
bc5c918e 162 int64_t pos = url_ftell(pb);
f578f938 163 put_be32(pb, 0); /* size */
1cb5f7fd 164 put_tag(pb, "stsc");
115329f1 165 put_be32(pb, 0); // version & flags
f578f938 166 entryPos = url_ftell(pb);
115329f1 167 put_be32(pb, track->entry); // entry count
f578f938 168 for (i=0; i<track->entry; i++) {
0aec3c5c 169 if(oldval != track->cluster[i].samplesInChunk)
f578f938 170 {
115329f1 171 put_be32(pb, i+1); // first chunk
0aec3c5c 172 put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
115329f1 173 put_be32(pb, 0x1); // sample description index
0aec3c5c 174 oldval = track->cluster[i].samplesInChunk;
f578f938 175 index++;
1cb5f7fd
MN
176 }
177 }
f578f938
TR
178 curpos = url_ftell(pb);
179 url_fseek(pb, entryPos, SEEK_SET);
115329f1 180 put_be32(pb, index); // rewrite size
f578f938 181 url_fseek(pb, curpos, SEEK_SET);
1cb5f7fd 182
f7635edb 183 return updateSize(pb, pos);
1cb5f7fd
MN
184}
185
e45ccf79 186/* Sync sample atom */
0724373d 187static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 188{
bc5c918e 189 int64_t curpos, entryPos;
b29af723 190 int i, index = 0;
bc5c918e 191 int64_t pos = url_ftell(pb);
115329f1 192 put_be32(pb, 0); // size
1cb5f7fd 193 put_tag(pb, "stss");
115329f1 194 put_be32(pb, 0); // version & flags
f578f938 195 entryPos = url_ftell(pb);
115329f1 196 put_be32(pb, track->entry); // entry count
f578f938 197 for (i=0; i<track->entry; i++) {
0aec3c5c 198 if(track->cluster[i].key_frame == 1) {
f578f938
TR
199 put_be32(pb, i+1);
200 index++;
201 }
202 }
203 curpos = url_ftell(pb);
204 url_fseek(pb, entryPos, SEEK_SET);
115329f1 205 put_be32(pb, index); // rewrite size
f578f938 206 url_fseek(pb, curpos, SEEK_SET);
f7635edb 207 return updateSize(pb, pos);
1cb5f7fd
MN
208}
209
b7d9da10 210static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd
MN
211{
212 put_be32(pb, 0x11); /* size */
b7d9da10
BC
213 if (track->mode == MODE_MOV) put_tag(pb, "samr");
214 else put_tag(pb, "damr");
1cb5f7fd 215 put_tag(pb, "FFMP");
b7d9da10 216 put_byte(pb, 0); /* decoder version */
3a72cbd9 217
875f9287 218 put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
b7d9da10
BC
219 put_byte(pb, 0x00); /* Mode change period (no restriction) */
220 put_byte(pb, 0x01); /* Frames per sample */
3a72cbd9
BC
221 return 0x11;
222}
223
d4a240cb
BC
224static int mov_write_ac3_tag(ByteIOContext *pb, MOVTrack *track)
225{
226 GetBitContext gbc;
227 PutBitContext pbc;
228 uint8_t buf[3];
229 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
230
231 if (track->vosLen < 7)
232 return -1;
233
234 put_be32(pb, 11);
235 put_tag(pb, "dac3");
236
544a0f47 237 init_get_bits(&gbc, track->vosData+4, track->vosLen-4);
d4a240cb
BC
238 fscod = get_bits(&gbc, 2);
239 frmsizecod = get_bits(&gbc, 6);
240 bsid = get_bits(&gbc, 5);
241 bsmod = get_bits(&gbc, 3);
242 acmod = get_bits(&gbc, 3);
243 if (acmod == 2) {
244 skip_bits(&gbc, 2); // dsurmod
245 } else {
246 if ((acmod & 1) && acmod != 1)
247 skip_bits(&gbc, 2); // cmixlev
248 if (acmod & 4)
249 skip_bits(&gbc, 2); // surmixlev
250 }
251 lfeon = get_bits1(&gbc);
252
253 init_put_bits(&pbc, buf, sizeof(buf));
254 put_bits(&pbc, 2, fscod);
255 put_bits(&pbc, 5, bsid);
256 put_bits(&pbc, 3, bsmod);
257 put_bits(&pbc, 3, acmod);
258 put_bits(&pbc, 1, lfeon);
259 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
260 put_bits(&pbc, 5, 0); // reserved
261
262 flush_put_bits(&pbc);
263 put_buffer(pb, buf, sizeof(buf));
264
265 return 11;
266}
267
463e7afd
BC
268/**
269 * This function writes extradata "as is".
270 * Extradata must be formated like a valid atom (with size and tag)
271 */
272static int mov_write_extradata_tag(ByteIOContext *pb, MOVTrack *track)
273{
274 put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
275 return track->enc->extradata_size;
276}
277
fcef991a
BC
278static int mov_write_enda_tag(ByteIOContext *pb)
279{
280 put_be32(pb, 10);
281 put_tag(pb, "enda");
282 put_be16(pb, 1); /* little endian */
283 return 10;
284}
285
039627cf
BC
286static unsigned int descrLength(unsigned int len)
287{
08db8f18
MN
288 int i;
289 for(i=1; len>>(7*i); i++);
290 return len + 1 + i;
039627cf
BC
291}
292
08db8f18 293static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
039627cf 294{
08db8f18
MN
295 int i= descrLength(size) - size - 2;
296 put_byte(pb, tag);
297 for(; i>0; i--)
298 put_byte(pb, (size>>(7*i)) | 0x80);
299 put_byte(pb, size & 0x7F);
039627cf
BC
300}
301
0724373d 302static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack *track) // Basic
039627cf 303{
bc5c918e 304 int64_t pos = url_ftell(pb);
fcef991a 305 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
039627cf 306
287d6cfa 307 put_be32(pb, 0); // size
039627cf 308 put_tag(pb, "esds");
287d6cfa 309 put_be32(pb, 0); // Version
039627cf
BC
310
311 // ES descriptor
312 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
313 descrLength(1));
314 put_be16(pb, track->trackID);
287d6cfa 315 put_byte(pb, 0x00); // flags (= no flags)
039627cf
BC
316
317 // DecoderConfig descriptor
318 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
319
320 // Object type indication
dfce888f
BC
321 if ((track->enc->codec_id == CODEC_ID_MP2 ||
322 track->enc->codec_id == CODEC_ID_MP3) &&
c4e02d70
BC
323 track->enc->sample_rate > 24000)
324 put_byte(pb, 0x6B); // 11172-3
dfce888f
BC
325 else
326 put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
039627cf
BC
327
328 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
329 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
330 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
287d6cfa 331 put_byte(pb, 0x15); // flags (= Audiostream)
039627cf 332 else
287d6cfa 333 put_byte(pb, 0x11); // flags (= Visualstream)
039627cf 334
287d6cfa
BC
335 put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
336 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
039627cf 337
287d6cfa 338 put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
039627cf 339 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
287d6cfa 340 put_be32(pb, 0); // vbr
039627cf 341 else
287d6cfa 342 put_be32(pb, track->enc->rc_max_rate); // avg bitrate
039627cf 343
287d6cfa 344 if (track->vosLen) {
039627cf
BC
345 // DecoderSpecific info descriptor
346 putDescr(pb, 0x05, track->vosLen);
347 put_buffer(pb, track->vosData, track->vosLen);
348 }
349
039627cf
BC
350 // SL descriptor
351 putDescr(pb, 0x06, 1);
352 put_byte(pb, 0x02);
f7635edb 353 return updateSize(pb, pos);
039627cf
BC
354}
355
0724373d 356static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack *track)
69dde1ad 357{
bc5c918e 358 int64_t pos = url_ftell(pb);
69dde1ad
GB
359
360 put_be32(pb, 0); /* size */
361 put_tag(pb, "wave");
362
363 put_be32(pb, 12); /* size */
364 put_tag(pb, "frma");
fcef991a 365 put_le32(pb, track->tag);
69dde1ad 366
fcef991a 367 if (track->enc->codec_id == CODEC_ID_AAC) {
aa1c1c61
BC
368 /* useless atom needed by mplayer, ipod, not needed by quicktime */
369 put_be32(pb, 12); /* size */
370 put_tag(pb, "mp4a");
371 put_be32(pb, 0);
fcef991a
BC
372 mov_write_esds_tag(pb, track);
373 } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
374 track->enc->codec_id == CODEC_ID_PCM_S32LE) {
375 mov_write_enda_tag(pb);
3a72cbd9 376 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
b7d9da10 377 mov_write_amr_tag(pb, track);
d4a240cb
BC
378 } else if (track->enc->codec_id == CODEC_ID_AC3) {
379 mov_write_ac3_tag(pb, track);
463e7afd
BC
380 } else if (track->enc->codec_id == CODEC_ID_ALAC) {
381 mov_write_extradata_tag(pb, track);
fcef991a 382 }
69dde1ad
GB
383
384 put_be32(pb, 8); /* size */
385 put_be32(pb, 0); /* null tag */
386
f7635edb 387 return updateSize(pb, pos);
69dde1ad
GB
388}
389
0724373d 390static int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack *track)
79e42311
BC
391{
392 put_be32(pb, track->vosLen+8);
393 put_tag(pb, "glbl");
394 put_buffer(pb, track->vosData, track->vosLen);
395 return 8+track->vosLen;
396}
397
0724373d 398static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 399{
bc5c918e 400 int64_t pos = url_ftell(pb);
f45b744c
BC
401 int version = track->mode == MODE_MOV &&
402 (track->audio_vbr ||
403 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
404 track->enc->codec_id == CODEC_ID_PCM_S24LE);
115329f1 405
1cb5f7fd 406 put_be32(pb, 0); /* size */
039627cf 407 put_le32(pb, track->tag); // store it byteswapped
1cb5f7fd
MN
408 put_be32(pb, 0); /* Reserved */
409 put_be16(pb, 0); /* Reserved */
410 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
69dde1ad 411
e45ccf79 412 /* SoundDescription */
fcef991a 413 put_be16(pb, version); /* Version */
e45ccf79 414 put_be16(pb, 0); /* Revision level */
1cb5f7fd
MN
415 put_be32(pb, 0); /* Reserved */
416
2260499c
BC
417 if (track->mode == MODE_MOV) {
418 put_be16(pb, track->enc->channels);
10fc840c
BC
419 if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
420 track->enc->codec_id == CODEC_ID_PCM_S8)
421 put_be16(pb, 8); /* bits per sample */
422 else
423 put_be16(pb, 16);
2260499c
BC
424 put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
425 } else { /* reserved for mp4/3gp */
426 put_be16(pb, 2);
427 put_be16(pb, 16);
428 put_be16(pb, 0);
429 }
ef19c7eb 430
9a4d9388 431 put_be16(pb, 0); /* packet size (= 0) */
1cb5f7fd
MN
432 put_be16(pb, track->timescale); /* Time scale */
433 put_be16(pb, 0); /* Reserved */
434
5cb49ca1 435 if(version == 1) { /* SoundDescription V1 extended info */
5cb49ca1 436 put_be32(pb, track->enc->frame_size); /* Samples per packet */
9a39cd35 437 put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
5cb49ca1 438 put_be32(pb, track->sampleSize); /* Bytes per frame */
aa1c1c61 439 put_be32(pb, 2); /* Bytes per sample */
69dde1ad
GB
440 }
441
6dd19fff
BC
442 if(track->mode == MODE_MOV &&
443 (track->enc->codec_id == CODEC_ID_AAC ||
4ed19420 444 track->enc->codec_id == CODEC_ID_AC3 ||
6dd19fff
BC
445 track->enc->codec_id == CODEC_ID_AMR_NB ||
446 track->enc->codec_id == CODEC_ID_PCM_S24LE ||
463e7afd
BC
447 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
448 track->enc->codec_id == CODEC_ID_ALAC))
fcef991a 449 mov_write_wave_tag(pb, track);
d0c0a29b 450 else if(track->tag == MKTAG('m','p','4','a'))
3a72cbd9
BC
451 mov_write_esds_tag(pb, track);
452 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
b7d9da10 453 mov_write_amr_tag(pb, track);
4ed19420
K
454 else if(track->enc->codec_id == CODEC_ID_AC3)
455 mov_write_ac3_tag(pb, track);
efa1fb39 456 else if(track->enc->codec_id == CODEC_ID_ALAC)
709c9f8d 457 mov_write_extradata_tag(pb, track);
79e42311
BC
458 else if(track->vosLen > 0)
459 mov_write_glbl_tag(pb, track);
3a72cbd9 460
f7635edb 461 return updateSize(pb, pos);
1cb5f7fd
MN
462}
463
6e6d6dc0 464static int mov_write_d263_tag(ByteIOContext *pb)
1cb5f7fd
MN
465{
466 put_be32(pb, 0xf); /* size */
467 put_tag(pb, "d263");
468 put_tag(pb, "FFMP");
87494ea0
BC
469 put_byte(pb, 0); /* decoder version */
470 /* FIXME use AVCodecContext level/profile, when encoder will set values */
471 put_byte(pb, 0xa); /* level */
472 put_byte(pb, 0); /* profile */
1cb5f7fd
MN
473 return 0xf;
474}
475
f578f938
TR
476/* TODO: No idea about these values */
477static int mov_write_svq3_tag(ByteIOContext *pb)
1cb5f7fd 478{
f578f938
TR
479 put_be32(pb, 0x15);
480 put_tag(pb, "SMI ");
481 put_tag(pb, "SEQH");
482 put_be32(pb, 0x5);
483 put_be32(pb, 0xe2c0211d);
484 put_be32(pb, 0xc0000000);
115329f1 485 put_byte(pb, 0);
f578f938 486 return 0x15;
1cb5f7fd
MN
487}
488
6e6eebf9
AJ
489static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
490{
bc5c918e 491 int64_t pos = url_ftell(pb);
6e6eebf9
AJ
492
493 put_be32(pb, 0);
494 put_tag(pb, "avcC");
9ab3f71b 495 ff_isom_write_avcc(pb, track->vosData, track->vosLen);
c1b8e6d8
BC
496 return updateSize(pb, pos);
497}
498
fc4cbc16
BC
499/* also used by all avid codecs (dv, imx, meridien) and their variants */
500static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
501{
502 int i;
503 put_be32(pb, 24); /* size */
504 put_tag(pb, "ACLR");
505 put_tag(pb, "ACLR");
506 put_tag(pb, "0001");
507 put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
508 put_be32(pb, 0); /* unknown */
509
510 put_be32(pb, 24); /* size */
511 put_tag(pb, "APRG");
512 put_tag(pb, "APRG");
513 put_tag(pb, "0001");
514 put_be32(pb, 1); /* unknown */
515 put_be32(pb, 0); /* unknown */
516
517 put_be32(pb, 120); /* size */
518 put_tag(pb, "ARES");
519 put_tag(pb, "ARES");
520 put_tag(pb, "0001");
521 put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
522 put_be32(pb, track->enc->width);
523 /* values below are based on samples created with quicktime and avid codecs */
524 if (track->vosData[5] & 2) { // interlaced
525 put_be32(pb, track->enc->height/2);
526 put_be32(pb, 2); /* unknown */
527 put_be32(pb, 0); /* unknown */
528 put_be32(pb, 4); /* unknown */
529 } else {
530 put_be32(pb, track->enc->height);
531 put_be32(pb, 1); /* unknown */
532 put_be32(pb, 0); /* unknown */
44fb8ebb
BC
533 if (track->enc->height == 1080)
534 put_be32(pb, 5); /* unknown */
535 else
536 put_be32(pb, 6); /* unknown */
fc4cbc16
BC
537 }
538 /* padding */
539 for (i = 0; i < 10; i++)
540 put_be64(pb, 0);
541
542 /* extra padding for stsd needed */
543 put_be32(pb, 0);
544 return 0;
545}
546
a23c9c4a
BC
547static const AVCodecTag codec_3gp_tags[] = {
548 { CODEC_ID_H263, MKTAG('s','2','6','3') },
549 { CODEC_ID_H264, MKTAG('a','v','c','1') },
550 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
551 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
552 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
553 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
3d1b0fcd 554 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1993f8d6 555 { CODEC_ID_NONE, 0 },
a23c9c4a
BC
556};
557
ccac8438
BC
558static const AVCodecTag mov_pix_fmt_tags[] = {
559 { PIX_FMT_YUYV422, MKTAG('y','u','v','s') },
fa7343ea 560 { PIX_FMT_UYVY422, MKTAG('2','v','u','y') },
ccac8438
BC
561 { PIX_FMT_BGR555, MKTAG('r','a','w',' ') },
562 { PIX_FMT_RGB24, MKTAG('r','a','w',' ') },
563 { PIX_FMT_BGR32_1, MKTAG('r','a','w',' ') },
564};
565
709c9f8d
BC
566static const AVCodecTag codec_ipod_tags[] = {
567 { CODEC_ID_H264, MKTAG('a','v','c','1') },
568 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
569 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
570 { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
d4a240cb 571 { CODEC_ID_AC3, MKTAG('a','c','-','3') },
3d1b0fcd 572 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1993f8d6 573 { CODEC_ID_NONE, 0 },
709c9f8d
BC
574};
575
a23c9c4a 576static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
8e321619 577{
039627cf 578 int tag = track->enc->codec_tag;
709c9f8d 579 if (track->mode == MODE_MP4 || track->mode == MODE_PSP) {
a23c9c4a
BC
580 if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
581 return 0;
baa0be4c 582 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
d4a240cb 583 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
c3b13251 584 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
f6204886 585 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
a23c9c4a
BC
586 else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
587 else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
709c9f8d
BC
588 } else if (track->mode == MODE_IPOD) {
589 tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
590 if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
591 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
592 "Quicktime/Ipod might not play the file\n");
a04aec6d 593 } else if (track->mode & MODE_3GP) {
a23c9c4a 594 tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
bbe05246 595 } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
ccac8438
BC
596 (tag == MKTAG('d','v','c','p') ||
597 track->enc->codec_id == CODEC_ID_RAWVIDEO))) {
8e321619 598 if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
506e4bb8
BC
599 if (track->enc->height == 480) /* NTSC */
600 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
601 else tag = MKTAG('d','v','c',' ');
602 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
603 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
604 else tag = MKTAG('d','v','p','p');
ccac8438
BC
605 } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
606 tag = codec_get_tag(mov_pix_fmt_tags, track->enc->pix_fmt);
607 if (!tag) // restore tag
608 tag = track->enc->codec_tag;
8e321619 609 } else {
a23c9c4a 610 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
d97f2144 611 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
0b98a7b3 612 if (!tag) { // if no mac fcc found, try with Microsoft tags
d97f2144 613 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
0b98a7b3
BC
614 if (tag)
615 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
616 "the file may be unplayable!\n");
d97f2144 617 }
a23c9c4a 618 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
d97f2144 619 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
0b98a7b3 620 if (!tag) { // if no mac fcc found, try with Microsoft tags
d97f2144
BC
621 int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
622 if (ms_tag) {
623 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
0b98a7b3
BC
624 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
625 "the file may be unplayable!\n");
d97f2144
BC
626 }
627 }
f6204886
DC
628 } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
629 tag = codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
a23c9c4a
BC
630 }
631 }
632 }
8e321619
BC
633 return tag;
634}
635
aa9f4208
BC
636/** Write uuid atom.
637 * Needed to make file play in iPods running newest firmware
638 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
639 */
640static int mov_write_uuid_tag_ipod(ByteIOContext *pb)
641{
642 put_be32(pb, 28);
643 put_tag(pb, "uuid");
644 put_be32(pb, 0x6b6840f2);
645 put_be32(pb, 0x5f244fc5);
646 put_be32(pb, 0xba39a51b);
647 put_be32(pb, 0xcf0323f3);
648 put_be32(pb, 0x0);
649 return 28;
650}
651
f6204886
DC
652static int mov_write_subtitle_tag(ByteIOContext *pb, MOVTrack *track)
653{
654 int64_t pos = url_ftell(pb);
655 put_be32(pb, 0); /* size */
656 put_le32(pb, track->tag); // store it byteswapped
657 put_be32(pb, 0); /* Reserved */
658 put_be16(pb, 0); /* Reserved */
659 put_be16(pb, 1); /* Data-reference index */
660
661 if (track->enc->extradata_size)
662 put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
663
664 return updateSize(pb, pos);
665}
666
0724373d 667static int mov_write_video_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 668{
bc5c918e 669 int64_t pos = url_ftell(pb);
53ffdd14 670 char compressor_name[32];
8cc7a34d 671
f578f938 672 put_be32(pb, 0); /* size */
039627cf 673 put_le32(pb, track->tag); // store it byteswapped
f578f938
TR
674 put_be32(pb, 0); /* Reserved */
675 put_be16(pb, 0); /* Reserved */
676 put_be16(pb, 1); /* Data-reference index */
677
53ffdd14
RG
678 put_be16(pb, 0); /* Codec stream version */
679 put_be16(pb, 0); /* Codec stream revision (=0) */
5997ed78 680 if (track->mode == MODE_MOV) {
915282e5
BC
681 put_tag(pb, "FFMP"); /* Vendor */
682 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
683 put_be32(pb, 0); /* Temporal Quality */
684 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
685 } else {
686 put_be32(pb, 0x200); /* Temporal Quality = normal */
687 put_be32(pb, 0x200); /* Spatial Quality = normal */
688 }
5997ed78
BC
689 } else {
690 put_be32(pb, 0); /* Reserved */
691 put_be32(pb, 0); /* Reserved */
692 put_be32(pb, 0); /* Reserved */
693 }
f578f938 694 put_be16(pb, track->enc->width); /* Video width */
6a460c36 695 put_be16(pb, track->height); /* Video height */
53ffdd14
RG
696 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
697 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
9a4d9388
RS
698 put_be32(pb, 0); /* Data size (= 0) */
699 put_be16(pb, 1); /* Frame count (= 1) */
115329f1 700
53ffdd14 701 memset(compressor_name,0,32);
5997ed78
BC
702 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
703 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
53ffdd14 704 strncpy(compressor_name,track->enc->codec->name,31);
25cf9062 705 put_byte(pb, strlen(compressor_name));
53ffdd14 706 put_buffer(pb, compressor_name, 31);
115329f1 707
dd1c8f3e
LA
708 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
709 put_be16(pb, track->enc->bits_per_coded_sample);
ccac8438
BC
710 else
711 put_be16(pb, 0x18); /* Reserved */
f578f938 712 put_be16(pb, 0xffff); /* Reserved */
d0c0a29b 713 if(track->tag == MKTAG('m','p','4','v'))
f578f938
TR
714 mov_write_esds_tag(pb, track);
715 else if(track->enc->codec_id == CODEC_ID_H263)
716 mov_write_d263_tag(pb);
717 else if(track->enc->codec_id == CODEC_ID_SVQ3)
115329f1 718 mov_write_svq3_tag(pb);
7b0f4c9a
BC
719 else if(track->enc->codec_id == CODEC_ID_DNXHD)
720 mov_write_avid_tag(pb, track);
aa9f4208 721 else if(track->enc->codec_id == CODEC_ID_H264) {
c1b8e6d8 722 mov_write_avcc_tag(pb, track);
aa9f4208
BC
723 if(track->mode == MODE_IPOD)
724 mov_write_uuid_tag_ipod(pb);
b64518e9 725 } else if(track->vosLen > 0)
79e42311 726 mov_write_glbl_tag(pb, track);
f578f938 727
f7635edb 728 return updateSize(pb, pos);
1cb5f7fd
MN
729}
730
0724373d 731static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 732{
bc5c918e 733 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
734 put_be32(pb, 0); /* size */
735 put_tag(pb, "stsd");
736 put_be32(pb, 0); /* version & flags */
737 put_be32(pb, 1); /* entry count */
f578f938
TR
738 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
739 mov_write_video_tag(pb, track);
740 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
741 mov_write_audio_tag(pb, track);
f6204886
DC
742 else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE)
743 mov_write_subtitle_tag(pb, track);
6e6d6dc0 744 return updateSize(pb, pos);
1cb5f7fd
MN
745}
746
0724373d 747static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)
b4712e3c 748{
c3e92a6c 749 MOVStts *ctts_entries;
b4712e3c
BC
750 uint32_t entries = 0;
751 uint32_t atom_size;
752 int i;
753
754 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
755 ctts_entries[0].count = 1;
0aec3c5c 756 ctts_entries[0].duration = track->cluster[0].cts;
b4712e3c 757 for (i=1; i<track->entry; i++) {
0aec3c5c 758 if (track->cluster[i].cts == ctts_entries[entries].duration) {
b4712e3c
BC
759 ctts_entries[entries].count++; /* compress */
760 } else {
761 entries++;
0aec3c5c 762 ctts_entries[entries].duration = track->cluster[i].cts;
b4712e3c
BC
763 ctts_entries[entries].count = 1;
764 }
765 }
766 entries++; /* last one */
767 atom_size = 16 + (entries * 8);
768 put_be32(pb, atom_size); /* size */
769 put_tag(pb, "ctts");
770 put_be32(pb, 0); /* version & flags */
771 put_be32(pb, entries); /* entry count */
772 for (i=0; i<entries; i++) {
773 put_be32(pb, ctts_entries[i].count);
774 put_be32(pb, ctts_entries[i].duration);
775 }
776 av_free(ctts_entries);
777 return atom_size;
778}
779
e45ccf79 780/* Time to sample atom */
0724373d 781static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 782{
c3e92a6c 783 MOVStts *stts_entries;
aa90239f
BC
784 uint32_t entries = -1;
785 uint32_t atom_size;
786 int i;
787
788 if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
789 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
790 stts_entries[0].count = track->sampleCount;
791 stts_entries[0].duration = 1;
792 entries = 1;
793 } else {
794 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
795 for (i=0; i<track->entry; i++) {
796 int64_t duration = i + 1 == track->entry ?
797 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
798 track->cluster[i+1].dts - track->cluster[i].dts;
799 if (i && duration == stts_entries[entries].duration) {
800 stts_entries[entries].count++; /* compress */
801 } else {
802 entries++;
803 stts_entries[entries].duration = duration;
804 stts_entries[entries].count = 1;
805 }
806 }
807 entries++; /* last one */
808 }
809 atom_size = 16 + (entries * 8);
810 put_be32(pb, atom_size); /* size */
1cb5f7fd
MN
811 put_tag(pb, "stts");
812 put_be32(pb, 0); /* version & flags */
aa90239f
BC
813 put_be32(pb, entries); /* entry count */
814 for (i=0; i<entries; i++) {
815 put_be32(pb, stts_entries[i].count);
816 put_be32(pb, stts_entries[i].duration);
817 }
818 av_free(stts_entries);
819 return atom_size;
1cb5f7fd
MN
820}
821
6e6d6dc0 822static int mov_write_dref_tag(ByteIOContext *pb)
1cb5f7fd
MN
823{
824 put_be32(pb, 28); /* size */
825 put_tag(pb, "dref");
826 put_be32(pb, 0); /* version & flags */
827 put_be32(pb, 1); /* entry count */
828
829 put_be32(pb, 0xc); /* size */
830 put_tag(pb, "url ");
831 put_be32(pb, 1); /* version & flags */
832
833 return 28;
834}
835
0724373d 836static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 837{
bc5c918e 838 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
839 put_be32(pb, 0); /* size */
840 put_tag(pb, "stbl");
6e6d6dc0
MN
841 mov_write_stsd_tag(pb, track);
842 mov_write_stts_tag(pb, track);
f578f938 843 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
b371539a 844 track->hasKeyframes && track->hasKeyframes < track->entry)
f578f938 845 mov_write_stss_tag(pb, track);
b4712e3c
BC
846 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
847 track->hasBframes)
848 mov_write_ctts_tag(pb, track);
6e6d6dc0
MN
849 mov_write_stsc_tag(pb, track);
850 mov_write_stsz_tag(pb, track);
851 mov_write_stco_tag(pb, track);
852 return updateSize(pb, pos);
1cb5f7fd
MN
853}
854
6e6d6dc0 855static int mov_write_dinf_tag(ByteIOContext *pb)
1cb5f7fd 856{
bc5c918e 857 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
858 put_be32(pb, 0); /* size */
859 put_tag(pb, "dinf");
6e6d6dc0
MN
860 mov_write_dref_tag(pb);
861 return updateSize(pb, pos);
1cb5f7fd
MN
862}
863
f6204886
DC
864static int mov_write_nmhd_tag(ByteIOContext *pb)
865{
866 put_be32(pb, 12);
867 put_tag(pb, "nmhd");
868 put_be32(pb, 0);
869 return 12;
870}
871
872static int mov_write_gmhd_tag(ByteIOContext *pb)
873{
874 put_be32(pb, 0x20); /* size */
875 put_tag(pb, "gmhd");
876 put_be32(pb, 0x18); /* gmin size */
877 put_tag(pb, "gmin"); /* generic media info */
878 put_be32(pb, 0); /* version & flags */
879 put_be16(pb, 0x40); /* graphics mode = */
880 put_be16(pb, 0x8000); /* opColor (r?) */
881 put_be16(pb, 0x8000); /* opColor (g?) */
882 put_be16(pb, 0x8000); /* opColor (b?) */
883 put_be16(pb, 0); /* balance */
884 put_be16(pb, 0); /* reserved */
885 return 0x20;
886}
887
6e6d6dc0 888static int mov_write_smhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
889{
890 put_be32(pb, 16); /* size */
891 put_tag(pb, "smhd");
892 put_be32(pb, 0); /* version & flags */
893 put_be16(pb, 0); /* reserved (balance, normally = 0) */
894 put_be16(pb, 0); /* reserved */
895 return 16;
896}
897
6e6d6dc0 898static int mov_write_vmhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
899{
900 put_be32(pb, 0x14); /* size (always 0x14) */
901 put_tag(pb, "vmhd");
902 put_be32(pb, 0x01); /* version & flags */
903 put_be64(pb, 0); /* reserved (graphics mode = copy) */
904 return 0x14;
905}
906
0724373d 907static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 908{
985688b8 909 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
bc5c918e 910 int64_t pos = url_ftell(pb);
115329f1 911
9a4d9388 912 if (!track) { /* no media --> data handler */
bb270c08
DB
913 hdlr = "dhlr";
914 hdlr_type = "url ";
915 descr = "DataHandler";
9a4d9388 916 } else {
bb270c08
DB
917 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
918 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
919 hdlr_type = "vide";
920 descr = "VideoHandler";
87609b51 921 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
bb270c08
DB
922 hdlr_type = "soun";
923 descr = "SoundHandler";
87609b51 924 } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
f6204886
DC
925 if (track->mode == MODE_IPOD) hdlr_type = "sbtl";
926 else hdlr_type = "text";
927 descr = "SubtitleHandler";
bb270c08 928 }
9a4d9388 929 }
115329f1 930
f578f938 931 put_be32(pb, 0); /* size */
1cb5f7fd
MN
932 put_tag(pb, "hdlr");
933 put_be32(pb, 0); /* Version & flags */
906b578f 934 put_buffer(pb, hdlr, 4); /* handler */
9a4d9388 935 put_tag(pb, hdlr_type); /* handler type */
f578f938
TR
936 put_be32(pb ,0); /* reserved */
937 put_be32(pb ,0); /* reserved */
938 put_be32(pb ,0); /* reserved */
9a4d9388
RS
939 put_byte(pb, strlen(descr)); /* string counter */
940 put_buffer(pb, descr, strlen(descr)); /* handler description */
941 return updateSize(pb, pos);
942}
943
0724373d 944static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track)
9a4d9388 945{
bc5c918e 946 int64_t pos = url_ftell(pb);
9a4d9388
RS
947 put_be32(pb, 0); /* size */
948 put_tag(pb, "minf");
1cb5f7fd 949 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
9a4d9388 950 mov_write_vmhd_tag(pb);
f6204886 951 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
9a4d9388 952 mov_write_smhd_tag(pb);
f6204886 953 else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
3d1b0fcd
BC
954 if (track->mode == MODE_MOV) mov_write_gmhd_tag(pb);
955 else mov_write_nmhd_tag(pb);
f6204886 956 }
9a4d9388
RS
957 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
958 mov_write_hdlr_tag(pb, NULL);
959 mov_write_dinf_tag(pb);
960 mov_write_stbl_tag(pb, track);
f578f938 961 return updateSize(pb, pos);
1cb5f7fd
MN
962}
963
0724373d 964static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 965{
a3a80ddc
BC
966 int version = track->trackDuration < INT32_MAX ? 0 : 1;
967
968 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
1cb5f7fd 969 put_tag(pb, "mdhd");
a3a80ddc
BC
970 put_byte(pb, version);
971 put_be24(pb, 0); /* flags */
972 if (version == 1) {
973 put_be64(pb, track->time);
974 put_be64(pb, track->time);
975 } else {
976 put_be32(pb, track->time); /* creation time */
977 put_be32(pb, track->time); /* modification time */
978 }
115329f1 979 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
a3a80ddc 980 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
ab561df9 981 put_be16(pb, track->language); /* language */
1cb5f7fd 982 put_be16(pb, 0); /* reserved (quality) */
db8f4a92
MN
983
984 if(version!=0 && track->mode == MODE_MOV){
985 av_log(NULL, AV_LOG_ERROR,
986 "FATAL error, file duration too long for timebase, this file will not be\n"
b1b64c23 987 "playable with quicktime. Choose a different timebase or a different\n"
db8f4a92
MN
988 "container format\n");
989 }
990
1cb5f7fd
MN
991 return 32;
992}
993
0724373d 994static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 995{
bc5c918e 996 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
997 put_be32(pb, 0); /* size */
998 put_tag(pb, "mdia");
6e6d6dc0
MN
999 mov_write_mdhd_tag(pb, track);
1000 mov_write_hdlr_tag(pb, track);
1001 mov_write_minf_tag(pb, track);
1002 return updateSize(pb, pos);
1cb5f7fd
MN
1003}
1004
59729451 1005static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 1006{
a3a80ddc
BC
1007 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
1008 int version = duration < INT32_MAX ? 0 : 1;
1009
1010 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
1cb5f7fd 1011 put_tag(pb, "tkhd");
a3a80ddc
BC
1012 put_byte(pb, version);
1013 put_be24(pb, 0xf); /* flags (track enabled) */
1014 if (version == 1) {
1015 put_be64(pb, track->time);
1016 put_be64(pb, track->time);
1017 } else {
1018 put_be32(pb, track->time); /* creation time */
1019 put_be32(pb, track->time); /* modification time */
1020 }
1cb5f7fd
MN
1021 put_be32(pb, track->trackID); /* track-id */
1022 put_be32(pb, 0); /* reserved */
a3a80ddc 1023 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
1cb5f7fd
MN
1024
1025 put_be32(pb, 0); /* reserved */
1026 put_be32(pb, 0); /* reserved */
1027 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
1028 /* Volume, only for audio */
1029 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
1030 put_be16(pb, 0x0100);
1031 else
1032 put_be16(pb, 0);
1033 put_be16(pb, 0); /* reserved */
1034
1035 /* Matrix structure */
1036 put_be32(pb, 0x00010000); /* reserved */
1037 put_be32(pb, 0x0); /* reserved */
1038 put_be32(pb, 0x0); /* reserved */
1039 put_be32(pb, 0x0); /* reserved */
1040 put_be32(pb, 0x00010000); /* reserved */
1041 put_be32(pb, 0x0); /* reserved */
1042 put_be32(pb, 0x0); /* reserved */
1043 put_be32(pb, 0x0); /* reserved */
1044 put_be32(pb, 0x40000000); /* reserved */
1045
1046 /* Track width and height, for visual only */
f6204886
DC
1047 if(track->enc->codec_type == CODEC_TYPE_VIDEO ||
1048 track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
59729451 1049 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
6a460c36
BC
1050 if(!sample_aspect_ratio || track->height != track->enc->height)
1051 sample_aspect_ratio = 1;
69dde1ad 1052 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
6a460c36 1053 put_be32(pb, track->height*0x10000);
1cb5f7fd
MN
1054 }
1055 else {
1056 put_be32(pb, 0);
1057 put_be32(pb, 0);
1058 }
1059 return 0x5c;
1060}
1061
8af18154 1062// This box seems important for the psp playback ... without it the movie seems to hang
1063static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
1064{
8af18154 1065 put_be32(pb, 0x24); /* size */
1066 put_tag(pb, "edts");
1067 put_be32(pb, 0x1c); /* size */
1068 put_tag(pb, "elst");
1069 put_be32(pb, 0x0);
1070 put_be32(pb, 0x1);
1071
83fa2ef8 1072 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
8af18154 1073
72289140 1074 put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
8af18154 1075 put_be32(pb, 0x00010000);
1076 return 0x24;
1077}
1078
1079// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1080static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
1081{
1082 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1083 put_tag(pb, "uuid");
1084 put_tag(pb, "USMT");
1085 put_be32(pb, 0x21d24fce);
1086 put_be32(pb, 0xbb88695c);
1087 put_be32(pb, 0xfac9c740);
1088 put_be32(pb, 0x1c); // another size here!
1089 put_tag(pb, "MTDT");
1090 put_be32(pb, 0x00010012);
1091 put_be32(pb, 0x0a);
1092 put_be32(pb, 0x55c40000);
1093 put_be32(pb, 0x1);
1094 put_be32(pb, 0x0);
1095 return 0x34;
1096}
1097
59729451 1098static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 1099{
bc5c918e 1100 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
1101 put_be32(pb, 0); /* size */
1102 put_tag(pb, "trak");
59729451 1103 mov_write_tkhd_tag(pb, track, st);
c6e722e5 1104 if (track->mode == MODE_PSP || track->hasBframes)
8af18154 1105 mov_write_edts_tag(pb, track); // PSP Movies require edts box
6e6d6dc0 1106 mov_write_mdia_tag(pb, track);
115329f1 1107 if (track->mode == MODE_PSP)
8af18154 1108 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
6e6d6dc0 1109 return updateSize(pb, pos);
1cb5f7fd
MN
1110}
1111
88730be6 1112#if 0
1cb5f7fd 1113/* TODO: Not sorted out, but not necessary either */
6e6d6dc0 1114static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
1115{
1116 put_be32(pb, 0x15); /* size */
1117 put_tag(pb, "iods");
1118 put_be32(pb, 0); /* version & flags */
1119 put_be16(pb, 0x1007);
1120 put_byte(pb, 0);
1121 put_be16(pb, 0x4fff);
1122 put_be16(pb, 0xfffe);
1123 put_be16(pb, 0x01ff);
1124 return 0x15;
1125}
88730be6 1126#endif
1cb5f7fd 1127
6e6d6dc0 1128static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd 1129{
b29af723
MN
1130 int maxTrackID = 1, i;
1131 int64_t maxTrackLenTemp, maxTrackLen = 0;
a3a80ddc 1132 int version;
1cb5f7fd 1133
42fb4148 1134 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1135 if(mov->tracks[i].entry > 0) {
83fa2ef8 1136 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
f578f938
TR
1137 if(maxTrackLen < maxTrackLenTemp)
1138 maxTrackLen = maxTrackLenTemp;
1cb5f7fd
MN
1139 if(maxTrackID < mov->tracks[i].trackID)
1140 maxTrackID = mov->tracks[i].trackID;
1141 }
1142 }
a3a80ddc
BC
1143
1144 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1145 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1146 put_tag(pb, "mvhd");
1147 put_byte(pb, version);
1148 put_be24(pb, 0); /* flags */
1149 if (version == 1) {
1150 put_be64(pb, mov->time);
1151 put_be64(pb, mov->time);
1152 } else {
1153 put_be32(pb, mov->time); /* creation time */
1154 put_be32(pb, mov->time); /* modification time */
1155 }
1156 put_be32(pb, mov->timescale); /* timescale */
1157 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1cb5f7fd
MN
1158
1159 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1160 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1161 put_be16(pb, 0); /* reserved */
1162 put_be32(pb, 0); /* reserved */
1163 put_be32(pb, 0); /* reserved */
1164
1165 /* Matrix structure */
1166 put_be32(pb, 0x00010000); /* reserved */
1167 put_be32(pb, 0x0); /* reserved */
1168 put_be32(pb, 0x0); /* reserved */
1169 put_be32(pb, 0x0); /* reserved */
1170 put_be32(pb, 0x00010000); /* reserved */
1171 put_be32(pb, 0x0); /* reserved */
1172 put_be32(pb, 0x0); /* reserved */
1173 put_be32(pb, 0x0); /* reserved */
1174 put_be32(pb, 0x40000000); /* reserved */
1175
1176 put_be32(pb, 0); /* reserved (preview time) */
1177 put_be32(pb, 0); /* reserved (preview duration) */
1178 put_be32(pb, 0); /* reserved (poster time) */
1179 put_be32(pb, 0); /* reserved (selection time) */
1180 put_be32(pb, 0); /* reserved (selection duration) */
1181 put_be32(pb, 0); /* reserved (current time) */
1182 put_be32(pb, maxTrackID+1); /* Next track id */
1183 return 0x6c;
1184}
1185
0724373d 1186static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1187 AVFormatContext *s)
1188{
bc5c918e 1189 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1190 put_be32(pb, 0); /* size */
1191 put_tag(pb, "hdlr");
1192 put_be32(pb, 0);
1193 put_be32(pb, 0);
1194 put_tag(pb, "mdir");
1195 put_tag(pb, "appl");
1196 put_be32(pb, 0);
1197 put_be32(pb, 0);
1198 put_be16(pb, 0);
1199 return updateSize(pb, pos);
1200}
1201
1202/* helper function to write a data tag with the specified string as data */
efda3395 1203static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
b6c50eb1 1204{
efda3395 1205 if(long_style){
bc5c918e 1206 int64_t pos = url_ftell(pb);
b6c50eb1 1207 put_be32(pb, 0); /* size */
efda3395
MN
1208 put_tag(pb, "data");
1209 put_be32(pb, 1);
1210 put_be32(pb, 0);
1211 put_buffer(pb, data, strlen(data));
1212 return updateSize(pb, pos);
1213 }else{
1214 put_be16(pb, strlen(data)); /* string length */
1215 put_be16(pb, 0);
1216 put_buffer(pb, data, strlen(data));
1217 return strlen(data) + 4;
b6c50eb1 1218 }
b6c50eb1
PB
1219}
1220
a97c1e93 1221static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
b6c50eb1 1222 int size = 0;
91208916 1223 if (value && value[0]) {
bc5c918e 1224 int64_t pos = url_ftell(pb);
b6c50eb1 1225 put_be32(pb, 0); /* size */
efda3395
MN
1226 put_tag(pb, name);
1227 mov_write_string_data_tag(pb, value, long_style);
1228 size= updateSize(pb, pos);
b6c50eb1
PB
1229 }
1230 return size;
1231}
1232
1233/* iTunes year */
efda3395 1234static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
b6c50eb1 1235{
efda3395
MN
1236 if(year){
1237 char year_str[5];
1238 snprintf(year_str, sizeof(year_str), "%04d", year);
1239 return mov_write_string_tag(pb, "\251day", year_str, long_style);
1240 }else
1241 return 0;
b6c50eb1
PB
1242}
1243
1244/* iTunes track number */
0724373d 1245static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1246 AVFormatContext *s)
1247{
1248 int size = 0;
91208916 1249 if (s->track) {
bc5c918e 1250 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1251 put_be32(pb, 0); /* size */
1252 put_tag(pb, "trkn");
1253 {
bc5c918e 1254 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1255 put_be32(pb, 0); /* size */
1256 put_tag(pb, "data");
1257 put_be32(pb, 0); // 8 bytes empty
1258 put_be32(pb, 0);
1259 put_be16(pb, 0); // empty
1260 put_be16(pb, s->track); // track number
1261 put_be16(pb, 0); // total track number
1262 put_be16(pb, 0); // empty
1263 updateSize(pb, pos);
1264 }
1265 size = updateSize(pb, pos);
1266 }
1267 return size;
1268}
1269
1270/* iTunes meta data list */
0724373d 1271static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1272 AVFormatContext *s)
1273{
bc5c918e 1274 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1275 put_be32(pb, 0); /* size */
1276 put_tag(pb, "ilst");
efda3395
MN
1277 mov_write_string_tag(pb, "\251nam", s->title , 1);
1278 mov_write_string_tag(pb, "\251ART", s->author , 1);
1279 mov_write_string_tag(pb, "\251wrt", s->author , 1);
1280 mov_write_string_tag(pb, "\251alb", s->album , 1);
1281 mov_write_day_tag(pb, s->year ,1);
d1d87dfb 1282 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
efda3395
MN
1283 mov_write_string_tag(pb, "\251cmt", s->comment , 1);
1284 mov_write_string_tag(pb, "\251gen", s->genre , 1);
bed4fc54 1285 mov_write_string_tag(pb, "\251cpy", s->copyright , 1);
b6c50eb1
PB
1286 mov_write_trkn_tag(pb, mov, s);
1287 return updateSize(pb, pos);
1288}
1289
1290/* iTunes meta data tag */
0724373d 1291static int mov_write_meta_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1292 AVFormatContext *s)
1293{
1294 int size = 0;
1295
1296 // only save meta tag if required
91208916 1297 if (s->title[0] || s->author[0] || s->album[0] || s->year ||
d88812a9 1298 s->comment[0] || s->genre[0] || s->track) {
bc5c918e 1299 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1300 put_be32(pb, 0); /* size */
1301 put_tag(pb, "meta");
1302 put_be32(pb, 0);
1303 mov_write_itunes_hdlr_tag(pb, mov, s);
1304 mov_write_ilst_tag(pb, mov, s);
1305 size = updateSize(pb, pos);
1306 }
1307 return size;
1308}
115329f1 1309
07b7b06d
BC
1310static int utf8len(const uint8_t *b)
1311{
1312 int len=0;
1313 int val;
1314 while(*b){
1315 GET_UTF8(val, *b++, return -1;)
1316 len++;
1317 }
1318 return len;
1319}
1320
1321static int ascii_to_wc(ByteIOContext *pb, const uint8_t *b)
1322{
1323 int val;
1324 while(*b){
1325 GET_UTF8(val, *b++, return -1;)
1326 put_be16(pb, val);
1327 }
1328 put_be16(pb, 0x00);
1329 return 0;
1330}
1331
1332static uint16_t language_code(const char *str)
1333{
1334 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1335}
1336
c6e2c6c9
BC
1337static int mov_write_3gp_udta_tag(ByteIOContext *pb, AVFormatContext *s,
1338 const char *tag, const char *str)
1339{
bc5c918e 1340 int64_t pos = url_ftell(pb);
c6e2c6c9
BC
1341 if (!utf8len(str))
1342 return 0;
1343 put_be32(pb, 0); /* size */
1344 put_tag (pb, tag); /* type */
1345 put_be32(pb, 0); /* version + flags */
1346 if (!strcmp(tag, "yrrc"))
1347 put_be16(pb, s->year);
1348 else {
1349 put_be16(pb, language_code("eng")); /* language */
1350 ascii_to_wc(pb, str);
1351 if (!strcmp(tag, "albm") && s->year)
1352 put_byte(pb, s->year);
1353 }
1354 return updateSize(pb, pos);
1355}
1356
0724373d 1357static int mov_write_udta_tag(ByteIOContext *pb, MOVContext *mov,
69dde1ad
GB
1358 AVFormatContext *s)
1359{
b63ba850 1360 int i;
d1d87dfb 1361 int bitexact = 0;
89a0d790 1362
d1d87dfb
BC
1363 for (i = 0; i < s->nb_streams; i++)
1364 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1365 bitexact = 1;
1366 break;
1367 }
1368
1369 if (!bitexact && (s->title[0] || s->author[0] || s->album[0] || s->year ||
1370 s->comment[0] || s->genre[0] || s->track)) {
bc5c918e 1371 int64_t pos = url_ftell(pb);
69dde1ad 1372
cfac9ec7
BF
1373 put_be32(pb, 0); /* size */
1374 put_tag(pb, "udta");
69dde1ad 1375
a04aec6d 1376 if (mov->mode & MODE_3GP) {
c6e2c6c9
BC
1377 mov_write_3gp_udta_tag(pb, s, "titl", s->title);
1378 mov_write_3gp_udta_tag(pb, s, "auth", s->author);
1379 mov_write_3gp_udta_tag(pb, s, "gnre", s->genre);
1380 mov_write_3gp_udta_tag(pb, s, "dscp", s->comment);
1381 mov_write_3gp_udta_tag(pb, s, "albm", s->album);
bed4fc54 1382 mov_write_3gp_udta_tag(pb, s, "cprt", s->copyright);
c6e2c6c9 1383 mov_write_3gp_udta_tag(pb, s, "yrrc", "nil");
ce072b2d
BC
1384 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1385 mov_write_string_tag(pb, "\251nam", s->title , 0);
1386 mov_write_string_tag(pb, "\251aut", s->author , 0);
1387 mov_write_string_tag(pb, "\251alb", s->album , 0);
1388 mov_write_day_tag(pb, s->year, 0);
1389 mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1390 mov_write_string_tag(pb, "\251des", s->comment , 0);
1391 mov_write_string_tag(pb, "\251gen", s->genre , 0);
bed4fc54 1392 mov_write_string_tag(pb, "\251cpy", s->copyright , 0);
c6e2c6c9 1393 } else {
283c9a8e
BC
1394 /* iTunes meta data */
1395 mov_write_meta_tag(pb, mov, s);
c6e2c6c9 1396 }
cfac9ec7 1397 return updateSize(pb, pos);
89a0d790
BF
1398 }
1399
1400 return 0;
69dde1ad
GB
1401}
1402
d32d6def 1403static void mov_write_psp_udta_tag(ByteIOContext *pb,
20e22af8
BC
1404 const char *str, const char *lang, int type)
1405{
1406 int len = utf8len(str)+1;
1407 if(len<=0)
d32d6def 1408 return;
20e22af8
BC
1409 put_be16(pb, len*2+10); /* size */
1410 put_be32(pb, type); /* type */
1411 put_be16(pb, language_code(lang)); /* language */
1412 put_be16(pb, 0x01); /* ? */
1413 ascii_to_wc(pb, str);
20e22af8
BC
1414}
1415
0724373d 1416static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
dcfdb046 1417{
bc5c918e 1418 int64_t pos, pos2;
dcfdb046 1419
dcfdb046
BL
1420 if (s->title[0]) {
1421 pos = url_ftell(pb);
1422 put_be32(pb, 0); /* size placeholder*/
1423 put_tag(pb, "uuid");
1424 put_tag(pb, "USMT");
91208916
BC
1425 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1426 put_be32(pb, 0xbb88695c);
1427 put_be32(pb, 0xfac9c740);
dcfdb046 1428
d32d6def 1429 pos2 = url_ftell(pb);
dcfdb046
BL
1430 put_be32(pb, 0); /* size placeholder*/
1431 put_tag(pb, "MTDT");
7cf0e16f 1432 put_be16(pb, 4);
dcfdb046 1433
7cf0e16f
MN
1434 // ?
1435 put_be16(pb, 0x0C); /* size */
1436 put_be32(pb, 0x0B); /* type */
1437 put_be16(pb, language_code("und")); /* language */
1438 put_be16(pb, 0x0); /* ? */
1439 put_be16(pb, 0x021C); /* data */
7cf0e16f 1440
d32d6def
BC
1441 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1442 mov_write_psp_udta_tag(pb, s->title, "eng", 0x01);
7cf0e16f 1443// 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
1444 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1445
1446 updateSize(pb, pos2);
1447 return updateSize(pb, pos);
dcfdb046
BL
1448 }
1449
d32d6def 1450 return 0;
dcfdb046
BL
1451}
1452
69dde1ad
GB
1453static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1454 AVFormatContext *s)
1cb5f7fd 1455{
b29af723 1456 int i;
bc5c918e 1457 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
1458 put_be32(pb, 0); /* size placeholder*/
1459 put_tag(pb, "moov");
1460 mov->timescale = globalTimescale;
1461
42fb4148 1462 for (i=0; i<mov->nb_streams; i++) {
e45ccf79
GB
1463 if(mov->tracks[i].entry <= 0) continue;
1464
e45ccf79
GB
1465 mov->tracks[i].time = mov->time;
1466 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
1467 }
1468
6e6d6dc0
MN
1469 mov_write_mvhd_tag(pb, mov);
1470 //mov_write_iods_tag(pb, mov);
42fb4148 1471 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1472 if(mov->tracks[i].entry > 0) {
59729451 1473 mov_write_trak_tag(pb, &(mov->tracks[i]), s->streams[i]);
1cb5f7fd
MN
1474 }
1475 }
1476
dcfdb046
BL
1477 if (mov->mode == MODE_PSP)
1478 mov_write_uuidusmt_tag(pb, s);
c6e2c6c9 1479 else
e344c1ea 1480 mov_write_udta_tag(pb, mov, s);
69dde1ad 1481
6e6d6dc0 1482 return updateSize(pb, pos);
1cb5f7fd
MN
1483}
1484
0724373d 1485static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd 1486{
b29af723 1487 put_be32(pb, 8); // placeholder for extended size field (64 bit)
5340a0bc 1488 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
b29af723 1489
115329f1 1490 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
1491 put_be32(pb, 0); /* size placeholder*/
1492 put_tag(pb, "mdat");
1493 return 0;
1494}
1495
1496/* TODO: This needs to be more general */
7b0fb8b7 1497static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 1498{
69dde1ad 1499 MOVContext *mov = s->priv_data;
bc5c918e 1500 int64_t pos = url_ftell(pb);
cd70d17f 1501 int has_h264 = 0, has_video = 0;
40f8675c 1502 int minor = 0x200;
c55cc074 1503 int i;
69dde1ad 1504
83579084
BC
1505 for (i = 0; i < s->nb_streams; i++) {
1506 AVStream *st = s->streams[i];
cd70d17f
BC
1507 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1508 has_video = 1;
83579084
BC
1509 if (st->codec->codec_id == CODEC_ID_H264)
1510 has_h264 = 1;
1511 }
1512
1560b667 1513 put_be32(pb, 0); /* size */
1cb5f7fd 1514 put_tag(pb, "ftyp");
e45ccf79 1515
40f8675c
BC
1516 if (mov->mode == MODE_3GP) {
1517 put_tag(pb, has_h264 ? "3gp6" : "3gp4");
1518 minor = has_h264 ? 0x100 : 0x200;
1519 } else if (mov->mode & MODE_3G2) {
1520 put_tag(pb, has_h264 ? "3g2b" : "3g2a");
1521 minor = has_h264 ? 0x20000 : 0x10000;
1522 }else if (mov->mode == MODE_PSP)
8af18154 1523 put_tag(pb, "MSNV");
c55cc074 1524 else if (mov->mode == MODE_MP4)
e45ccf79 1525 put_tag(pb, "isom");
cd70d17f
BC
1526 else if (mov->mode == MODE_IPOD)
1527 put_tag(pb, has_video ? "M4V ":"M4A ");
1528 else
26f86eaf 1529 put_tag(pb, "qt ");
e45ccf79 1530
40f8675c 1531 put_be32(pb, minor);
e45ccf79 1532
6ba9ed95
BC
1533 if(mov->mode == MODE_MOV)
1534 put_tag(pb, "qt ");
1535 else{
ed6e47c2 1536 put_tag(pb, "isom");
65f52683 1537 put_tag(pb, "iso2");
83579084
BC
1538 if(has_h264)
1539 put_tag(pb, "avc1");
6ba9ed95 1540 }
ed6e47c2 1541
91208916 1542 if (mov->mode == MODE_3GP)
3473ca9a 1543 put_tag(pb, has_h264 ? "3gp6":"3gp4");
a04aec6d 1544 else if (mov->mode & MODE_3G2)
3473ca9a 1545 put_tag(pb, has_h264 ? "3g2b":"3g2a");
91208916 1546 else if (mov->mode == MODE_PSP)
8af18154 1547 put_tag(pb, "MSNV");
c55cc074 1548 else if (mov->mode == MODE_MP4)
e45ccf79 1549 put_tag(pb, "mp41");
1560b667 1550 return updateSize(pb, pos);
1cb5f7fd
MN
1551}
1552
0c716ab7 1553static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
8af18154 1554{
dcfdb046
BL
1555 AVCodecContext *VideoCodec = s->streams[0]->codec;
1556 AVCodecContext *AudioCodec = s->streams[1]->codec;
1557 int AudioRate = AudioCodec->sample_rate;
1558 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
9e282ba3
MN
1559 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1560 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
8af18154 1561
91208916 1562 put_be32(pb, 0x94); /* size */
8af18154 1563 put_tag(pb, "uuid");
1564 put_tag(pb, "PROF");
1565
91208916
BC
1566 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1567 put_be32(pb, 0xbb88695c);
1568 put_be32(pb, 0xfac9c740);
8af18154 1569
91208916
BC
1570 put_be32(pb, 0x0); /* ? */
1571 put_be32(pb, 0x3); /* 3 sections ? */
8af18154 1572
91208916 1573 put_be32(pb, 0x14); /* size */
8af18154 1574 put_tag(pb, "FPRF");
91208916
BC
1575 put_be32(pb, 0x0); /* ? */
1576 put_be32(pb, 0x0); /* ? */
1577 put_be32(pb, 0x0); /* ? */
8af18154 1578
91208916 1579 put_be32(pb, 0x2c); /* size */
8af18154 1580 put_tag(pb, "APRF"); /* audio */
91208916
BC
1581 put_be32(pb, 0x0);
1582 put_be32(pb, 0x2); /* TrackID */
8af18154 1583 put_tag(pb, "mp4a");
91208916
BC
1584 put_be32(pb, 0x20f);
1585 put_be32(pb, 0x0);
9e282ba3
MN
1586 put_be32(pb, audio_kbitrate);
1587 put_be32(pb, audio_kbitrate);
91208916
BC
1588 put_be32(pb, AudioRate);
1589 put_be32(pb, AudioCodec->channels);
8af18154 1590
91208916 1591 put_be32(pb, 0x34); /* size */
8af18154 1592 put_tag(pb, "VPRF"); /* video */
91208916
BC
1593 put_be32(pb, 0x0);
1594 put_be32(pb, 0x1); /* TrackID */
87b041e0
MN
1595 if (VideoCodec->codec_id == CODEC_ID_H264) {
1596 put_tag(pb, "avc1");
91208916
BC
1597 put_be16(pb, 0x014D);
1598 put_be16(pb, 0x0015);
87b041e0
MN
1599 } else {
1600 put_tag(pb, "mp4v");
91208916
BC
1601 put_be16(pb, 0x0000);
1602 put_be16(pb, 0x0103);
87b041e0 1603 }
91208916 1604 put_be32(pb, 0x0);
9e282ba3
MN
1605 put_be32(pb, video_kbitrate);
1606 put_be32(pb, video_kbitrate);
dcfdb046
BL
1607 put_be32(pb, FrameRate);
1608 put_be32(pb, FrameRate);
1609 put_be16(pb, VideoCodec->width);
1610 put_be16(pb, VideoCodec->height);
1611 put_be32(pb, 0x010001); /* ? */
8af18154 1612}
1613
1cb5f7fd
MN
1614static int mov_write_header(AVFormatContext *s)
1615{
899681cd 1616 ByteIOContext *pb = s->pb;
69dde1ad
GB
1617 MOVContext *mov = s->priv_data;
1618 int i;
1cb5f7fd 1619
899681cd 1620 if (url_is_streamed(s->pb)) {
4f60f34a
BC
1621 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1622 return -1;
1623 }
1624
1522767d
BC
1625 /* Default mode == MP4 */
1626 mov->mode = MODE_MP4;
1627
1628 if (s->oformat != NULL) {
1629 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
a04aec6d 1630 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
1522767d
BC
1631 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1632 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
aa9f4208 1633 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
1522767d 1634
26f86eaf 1635 mov_write_ftyp_tag(pb,s);
91208916
BC
1636 if (mov->mode == MODE_PSP) {
1637 if (s->nb_streams != 2) {
1522767d
BC
1638 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1639 return -1;
1640 }
1641 mov_write_uuidprof_tag(pb,s);
1642 }
1643 }
1644
ffdd57d4 1645 for(i=0; i<s->nb_streams; i++){
039627cf
BC
1646 AVStream *st= s->streams[i];
1647 MOVTrack *track= &mov->tracks[i];
1648
1649 track->enc = st->codec;
2c5ad5fd
BC
1650 track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1651 track->mode = mov->mode;
a23c9c4a
BC
1652 track->tag = mov_find_codec_tag(s, track);
1653 if (!track->tag) {
bc4c3a55
BC
1654 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
1655 "codec not currently supported in container\n", i);
a23c9c4a
BC
1656 return -1;
1657 }
039627cf 1658 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
6a460c36
BC
1659 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
1660 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
1661 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
1662 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
1663 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
1664 return -1;
1665 }
1666 track->height = track->tag>>24 == 'n' ? 486 : 576;
1667 } else
1668 track->height = st->codec->height;
ffb512e3 1669 track->timescale = st->codec->time_base.den;
039627cf 1670 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
c8134d71 1671 if (track->mode == MODE_MOV && track->timescale > 100000)
4ad08acc 1672 av_log(s, AV_LOG_WARNING,
911f9b71
BC
1673 "WARNING codec timebase is very high. If duration is too long,\n"
1674 "file may not be playable by quicktime. Specify a shorter timebase\n"
1675 "or choose different container.\n");
039627cf 1676 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
ffb512e3 1677 track->timescale = st->codec->sample_rate;
039627cf 1678 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
cf74dd4a 1679 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
b596102e
BC
1680 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1681 return -1;
4278662d
BC
1682 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1683 track->audio_vbr = 1;
1684 }else{
cf74dd4a 1685 st->codec->frame_size = 1;
4278662d 1686 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
b596102e 1687 }
e00c3de8
BC
1688 if(track->mode != MODE_MOV &&
1689 track->enc->codec_id == CODEC_ID_MP3 && track->enc->sample_rate < 16000){
1690 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
1691 i, track->enc->sample_rate);
1692 return -1;
1693 }
f6204886
DC
1694 }else if(st->codec->codec_type == CODEC_TYPE_SUBTITLE){
1695 track->timescale = st->codec->time_base.den;
1696 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
ffb512e3 1697 }
f578f938
TR
1698 }
1699
18c697ac
BC
1700 mov_write_mdat_tag(pb, mov);
1701 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
42fb4148 1702 mov->nb_streams = s->nb_streams;
18c697ac 1703
1cb5f7fd
MN
1704 put_flush_packet(pb);
1705
1706 return 0;
1707}
1708
e928649b 1709static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd
MN
1710{
1711 MOVContext *mov = s->priv_data;
899681cd 1712 ByteIOContext *pb = s->pb;
039627cf
BC
1713 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1714 AVCodecContext *enc = trk->enc;
e45ccf79 1715 unsigned int samplesInChunk = 0;
e928649b 1716 int size= pkt->size;
1cb5f7fd 1717
899681cd 1718 if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
e45ccf79 1719 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 1720
06fcf56c 1721 if (enc->codec_id == CODEC_ID_AMR_NB) {
5cb49ca1
BC
1722 /* We must find out how many AMR blocks there are in one packet */
1723 static uint16_t packed_size[16] =
1724 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1725 int len = 0;
1726
1727 while (len < size && samplesInChunk < 100) {
1728 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1729 samplesInChunk++;
1cb5f7fd 1730 }
2111e3f9 1731 if(samplesInChunk > 1){
89938d29 1732 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2111e3f9
MN
1733 return -1;
1734 }
5cb49ca1
BC
1735 } else if (trk->sampleSize)
1736 samplesInChunk = size/trk->sampleSize;
1737 else
5616f85d 1738 samplesInChunk = 1;
e45ccf79 1739
5616f85d
BC
1740 /* copy extradata if it exists */
1741 if (trk->vosLen == 0 && enc->extradata_size > 0) {
e45ccf79
GB
1742 trk->vosLen = enc->extradata_size;
1743 trk->vosData = av_malloc(trk->vosLen);
1744 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1745 }
1746
5d2160a0
BC
1747 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1748 /* from x264 or from bytestream h264 */
1749 /* nal reformating needed */
1750 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
1751 } else {
1752 put_buffer(pb, pkt->data, size);
1753 }
1754
0a63a676 1755 if ((enc->codec_id == CODEC_ID_DNXHD ||
d4a240cb 1756 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
db568c07
BC
1757 /* copy frame to create needed atoms */
1758 trk->vosLen = size;
1759 trk->vosData = av_malloc(size);
e8c4df40
BC
1760 if (!trk->vosData)
1761 return AVERROR(ENOMEM);
db568c07 1762 memcpy(trk->vosData, pkt->data, size);
c1b8e6d8
BC
1763 }
1764
0aec3c5c
BC
1765 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1766 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
e45ccf79
GB
1767 if (!trk->cluster)
1768 return -1;
e45ccf79 1769 }
e45ccf79 1770
5d2160a0 1771 trk->cluster[trk->entry].pos = url_ftell(pb) - size;
0aec3c5c
BC
1772 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1773 trk->cluster[trk->entry].size = size;
1774 trk->cluster[trk->entry].entries = samplesInChunk;
aa90239f
BC
1775 trk->cluster[trk->entry].dts = pkt->dts;
1776 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1777
96f69e0d
BC
1778 if (pkt->pts == AV_NOPTS_VALUE) {
1779 av_log(s, AV_LOG_WARNING, "pts has no value\n");
1780 pkt->pts = pkt->dts;
1781 }
c4f078ff
BC
1782 if (pkt->dts != pkt->pts)
1783 trk->hasBframes = 1;
1784 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1785 trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1786 if(trk->cluster[trk->entry].key_frame)
1787 trk->hasKeyframes++;
e45ccf79
GB
1788 trk->entry++;
1789 trk->sampleCount += samplesInChunk;
039627cf 1790 mov->mdat_size += size;
e45ccf79 1791
1cb5f7fd
MN
1792 put_flush_packet(pb);
1793 return 0;
1794}
1795
1796static int mov_write_trailer(AVFormatContext *s)
1797{
1798 MOVContext *mov = s->priv_data;
899681cd 1799 ByteIOContext *pb = s->pb;
1cb5f7fd 1800 int res = 0;
0aec3c5c 1801 int i;
1cb5f7fd 1802
bc5c918e 1803 int64_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1804
1805 /* Write size of mdat tag */
039627cf 1806 if (mov->mdat_size+8 <= UINT32_MAX) {
b29af723 1807 url_fseek(pb, mov->mdat_pos, SEEK_SET);
039627cf 1808 put_be32(pb, mov->mdat_size+8);
b29af723
MN
1809 } else {
1810 /* overwrite 'wide' placeholder atom */
1811 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1812 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1813 put_tag(pb, "mdat");
039627cf 1814 put_be64(pb, mov->mdat_size+16);
b29af723 1815 }
69dde1ad 1816 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1817
69dde1ad 1818 mov_write_moov_tag(pb, mov, s);
1cb5f7fd 1819
42fb4148 1820 for (i=0; i<mov->nb_streams; i++) {
0aec3c5c
BC
1821 av_freep(&mov->tracks[i].cluster);
1822
91208916 1823 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
ec7d0d2e 1824
1cb5f7fd 1825 }
69dde1ad 1826
1cb5f7fd
MN
1827 put_flush_packet(pb);
1828
1829 return res;
1830}
1831
b250f9c6 1832#if CONFIG_MOV_MUXER
ff70e601 1833AVOutputFormat mov_muxer = {
1cb5f7fd 1834 "mov",
bde15e74 1835 NULL_IF_CONFIG_SMALL("MOV format"),
1cb5f7fd
MN
1836 NULL,
1837 "mov",
1838 sizeof(MOVContext),
69dde1ad 1839 CODEC_ID_AAC,
2187d948 1840 CODEC_ID_MPEG4,
1cb5f7fd
MN
1841 mov_write_header,
1842 mov_write_packet,
1843 mov_write_trailer,
25918212 1844 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
c1854592 1845 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
1cb5f7fd 1846};
ff70e601 1847#endif
b250f9c6 1848#if CONFIG_TGP_MUXER
ff70e601 1849AVOutputFormat tgp_muxer = {
1cb5f7fd 1850 "3gp",
bde15e74 1851 NULL_IF_CONFIG_SMALL("3GP format"),
1cb5f7fd
MN
1852 NULL,
1853 "3gp",
1854 sizeof(MOVContext),
1855 CODEC_ID_AMR_NB,
1856 CODEC_ID_H263,
1857 mov_write_header,
1858 mov_write_packet,
1859 mov_write_trailer,
c64d476c 1860 .flags = AVFMT_GLOBALHEADER,
c1854592 1861 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
1cb5f7fd 1862};
ff70e601 1863#endif
b250f9c6 1864#if CONFIG_MP4_MUXER
ff70e601 1865AVOutputFormat mp4_muxer = {
1cb5f7fd 1866 "mp4",
bde15e74 1867 NULL_IF_CONFIG_SMALL("MP4 format"),
4cb3f3b6 1868 "application/mp4",
ccec1b69 1869 "mp4",
1cb5f7fd
MN
1870 sizeof(MOVContext),
1871 CODEC_ID_AAC,
1872 CODEC_ID_MPEG4,
1873 mov_write_header,
1874 mov_write_packet,
1875 mov_write_trailer,
25918212 1876 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
c1854592 1877 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
1cb5f7fd 1878};
ff70e601 1879#endif
b250f9c6 1880#if CONFIG_PSP_MUXER
ff70e601 1881AVOutputFormat psp_muxer = {
8af18154 1882 "psp",
bde15e74 1883 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
8af18154 1884 NULL,
1885 "mp4,psp",
1886 sizeof(MOVContext),
1887 CODEC_ID_AAC,
1888 CODEC_ID_MPEG4,
1889 mov_write_header,
1890 mov_write_packet,
1891 mov_write_trailer,
dcfdb046 1892 .flags = AVFMT_GLOBALHEADER,
c1854592 1893 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
8af18154 1894};
ff70e601 1895#endif
b250f9c6 1896#if CONFIG_TG2_MUXER
ff70e601 1897AVOutputFormat tg2_muxer = {
8536ab89 1898 "3g2",
bde15e74 1899 NULL_IF_CONFIG_SMALL("3GP2 format"),
8536ab89 1900 NULL,
1901 "3g2",
1902 sizeof(MOVContext),
1903 CODEC_ID_AMR_NB,
1904 CODEC_ID_H263,
1905 mov_write_header,
1906 mov_write_packet,
1907 mov_write_trailer,
c64d476c 1908 .flags = AVFMT_GLOBALHEADER,
c1854592 1909 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
8536ab89 1910};
ff70e601 1911#endif
b250f9c6 1912#if CONFIG_IPOD_MUXER
aa9f4208
BC
1913AVOutputFormat ipod_muxer = {
1914 "ipod",
bde15e74 1915 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
aa9f4208 1916 "application/mp4",
ccec1b69 1917 "m4v,m4a",
aa9f4208
BC
1918 sizeof(MOVContext),
1919 CODEC_ID_AAC,
1920 CODEC_ID_H264,
1921 mov_write_header,
1922 mov_write_packet,
1923 mov_write_trailer,
1924 .flags = AVFMT_GLOBALHEADER,
c1854592 1925 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
aa9f4208
BC
1926};
1927#endif