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