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