split mov_find_codec_tag in separate per format functions
[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;
a23c9c4a
BC
586 if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
587 return 0;
baa0be4c 588 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
d4a240cb 589 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
c3b13251 590 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
f6204886 591 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
a23c9c4a
BC
592 else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
593 else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
6b600285
BC
594
595 return tag;
596}
597
598static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
599{
600 int tag = track->enc->codec_tag;
601
7943a90a
BC
602 if (track->enc->codec_type == CODEC_TYPE_SUBTITLE &&
603 (tag == MKTAG('t','x','3','g') ||
604 tag == MKTAG('t','e','x','t')))
605 track->tag = tag; // keep original tag
606 else
d168b3fd 607 tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
709c9f8d
BC
608 if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
609 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
610 "Quicktime/Ipod might not play the file\n");
6b600285
BC
611
612 return tag;
613}
614
615static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
616{
617 int tag;
618
506e4bb8
BC
619 if (track->enc->height == 480) /* NTSC */
620 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
621 else tag = MKTAG('d','v','c',' ');
622 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
623 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
624 else tag = MKTAG('d','v','p','p');
6b600285
BC
625
626 return tag;
627}
628
629static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
630{
631 int tag = track->enc->codec_tag;
2fa6bc78
BC
632 int i;
633 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
634 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
635 tag = mov_pix_fmt_tags[i].tag;
636 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
637 break;
638 }
639 }
ccac8438
BC
640 if (!tag) // restore tag
641 tag = track->enc->codec_tag;
6b600285
BC
642
643 return tag;
644}
645
646static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
647{
648 int tag = track->enc->codec_tag;
649
650 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
651 (tag == MKTAG('d','v','c','p') ||
652 track->enc->codec_id == CODEC_ID_RAWVIDEO ||
653 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
654 if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
655 tag = mov_get_dv_codec_tag(s, track);
656 } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
657 tag = mov_get_rawvideo_codec_tag(s, track);
8e321619 658 } else {
a23c9c4a 659 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
d97f2144 660 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
0b98a7b3 661 if (!tag) { // if no mac fcc found, try with Microsoft tags
d97f2144 662 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
0b98a7b3
BC
663 if (tag)
664 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
665 "the file may be unplayable!\n");
d97f2144 666 }
a23c9c4a 667 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
d97f2144 668 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
0b98a7b3 669 if (!tag) { // if no mac fcc found, try with Microsoft tags
d97f2144
BC
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));
0b98a7b3
BC
673 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
674 "the file may be unplayable!\n");
d97f2144
BC
675 }
676 }
f6204886
DC
677 } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
678 tag = codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
a23c9c4a
BC
679 }
680 }
681 }
8e321619
BC
682 return tag;
683}
684
6b600285
BC
685static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
686{
687 int tag = track->enc->codec_tag;
688
689 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
690 tag = mp4_get_codec_tag(s, track);
691 else if (track->mode == MODE_IPOD)
692 tag = ipod_get_codec_tag(s, track);
693 else if (track->mode & MODE_3GP)
694 tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
695 else
696 tag = mov_get_codec_tag(s, track);
697
698 return tag;
699}
700
aa9f4208
BC
701/** Write uuid atom.
702 * Needed to make file play in iPods running newest firmware
703 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
704 */
705static int mov_write_uuid_tag_ipod(ByteIOContext *pb)
706{
707 put_be32(pb, 28);
708 put_tag(pb, "uuid");
709 put_be32(pb, 0x6b6840f2);
710 put_be32(pb, 0x5f244fc5);
711 put_be32(pb, 0xba39a51b);
712 put_be32(pb, 0xcf0323f3);
713 put_be32(pb, 0x0);
714 return 28;
715}
716
f6204886
DC
717static int mov_write_subtitle_tag(ByteIOContext *pb, MOVTrack *track)
718{
719 int64_t pos = url_ftell(pb);
720 put_be32(pb, 0); /* size */
721 put_le32(pb, track->tag); // store it byteswapped
722 put_be32(pb, 0); /* Reserved */
723 put_be16(pb, 0); /* Reserved */
724 put_be16(pb, 1); /* Data-reference index */
725
726 if (track->enc->extradata_size)
727 put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
728
729 return updateSize(pb, pos);
730}
731
0724373d 732static int mov_write_video_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 733{
bc5c918e 734 int64_t pos = url_ftell(pb);
53ffdd14 735 char compressor_name[32];
8cc7a34d 736
f578f938 737 put_be32(pb, 0); /* size */
039627cf 738 put_le32(pb, track->tag); // store it byteswapped
f578f938
TR
739 put_be32(pb, 0); /* Reserved */
740 put_be16(pb, 0); /* Reserved */
741 put_be16(pb, 1); /* Data-reference index */
742
53ffdd14
RG
743 put_be16(pb, 0); /* Codec stream version */
744 put_be16(pb, 0); /* Codec stream revision (=0) */
5997ed78 745 if (track->mode == MODE_MOV) {
915282e5
BC
746 put_tag(pb, "FFMP"); /* Vendor */
747 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
748 put_be32(pb, 0); /* Temporal Quality */
749 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
750 } else {
751 put_be32(pb, 0x200); /* Temporal Quality = normal */
752 put_be32(pb, 0x200); /* Spatial Quality = normal */
753 }
5997ed78
BC
754 } else {
755 put_be32(pb, 0); /* Reserved */
756 put_be32(pb, 0); /* Reserved */
757 put_be32(pb, 0); /* Reserved */
758 }
f578f938 759 put_be16(pb, track->enc->width); /* Video width */
6a460c36 760 put_be16(pb, track->height); /* Video height */
53ffdd14
RG
761 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
762 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
9a4d9388
RS
763 put_be32(pb, 0); /* Data size (= 0) */
764 put_be16(pb, 1); /* Frame count (= 1) */
115329f1 765
53ffdd14 766 memset(compressor_name,0,32);
5997ed78
BC
767 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
768 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
53ffdd14 769 strncpy(compressor_name,track->enc->codec->name,31);
25cf9062 770 put_byte(pb, strlen(compressor_name));
53ffdd14 771 put_buffer(pb, compressor_name, 31);
115329f1 772
dd1c8f3e
LA
773 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
774 put_be16(pb, track->enc->bits_per_coded_sample);
ccac8438
BC
775 else
776 put_be16(pb, 0x18); /* Reserved */
f578f938 777 put_be16(pb, 0xffff); /* Reserved */
d0c0a29b 778 if(track->tag == MKTAG('m','p','4','v'))
f578f938
TR
779 mov_write_esds_tag(pb, track);
780 else if(track->enc->codec_id == CODEC_ID_H263)
781 mov_write_d263_tag(pb);
782 else if(track->enc->codec_id == CODEC_ID_SVQ3)
115329f1 783 mov_write_svq3_tag(pb);
7b0f4c9a
BC
784 else if(track->enc->codec_id == CODEC_ID_DNXHD)
785 mov_write_avid_tag(pb, track);
aa9f4208 786 else if(track->enc->codec_id == CODEC_ID_H264) {
c1b8e6d8 787 mov_write_avcc_tag(pb, track);
aa9f4208
BC
788 if(track->mode == MODE_IPOD)
789 mov_write_uuid_tag_ipod(pb);
b64518e9 790 } else if(track->vosLen > 0)
79e42311 791 mov_write_glbl_tag(pb, track);
f578f938 792
f7635edb 793 return updateSize(pb, pos);
1cb5f7fd
MN
794}
795
0724373d 796static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 797{
bc5c918e 798 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
799 put_be32(pb, 0); /* size */
800 put_tag(pb, "stsd");
801 put_be32(pb, 0); /* version & flags */
802 put_be32(pb, 1); /* entry count */
f578f938
TR
803 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
804 mov_write_video_tag(pb, track);
805 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
806 mov_write_audio_tag(pb, track);
f6204886
DC
807 else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE)
808 mov_write_subtitle_tag(pb, track);
6e6d6dc0 809 return updateSize(pb, pos);
1cb5f7fd
MN
810}
811
0724373d 812static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)
b4712e3c 813{
c3e92a6c 814 MOVStts *ctts_entries;
b4712e3c
BC
815 uint32_t entries = 0;
816 uint32_t atom_size;
817 int i;
818
819 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
820 ctts_entries[0].count = 1;
0aec3c5c 821 ctts_entries[0].duration = track->cluster[0].cts;
b4712e3c 822 for (i=1; i<track->entry; i++) {
0aec3c5c 823 if (track->cluster[i].cts == ctts_entries[entries].duration) {
b4712e3c
BC
824 ctts_entries[entries].count++; /* compress */
825 } else {
826 entries++;
0aec3c5c 827 ctts_entries[entries].duration = track->cluster[i].cts;
b4712e3c
BC
828 ctts_entries[entries].count = 1;
829 }
830 }
831 entries++; /* last one */
832 atom_size = 16 + (entries * 8);
833 put_be32(pb, atom_size); /* size */
834 put_tag(pb, "ctts");
835 put_be32(pb, 0); /* version & flags */
836 put_be32(pb, entries); /* entry count */
837 for (i=0; i<entries; i++) {
838 put_be32(pb, ctts_entries[i].count);
839 put_be32(pb, ctts_entries[i].duration);
840 }
841 av_free(ctts_entries);
842 return atom_size;
843}
844
e45ccf79 845/* Time to sample atom */
0724373d 846static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 847{
c3e92a6c 848 MOVStts *stts_entries;
aa90239f
BC
849 uint32_t entries = -1;
850 uint32_t atom_size;
851 int i;
852
853 if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
854 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
855 stts_entries[0].count = track->sampleCount;
856 stts_entries[0].duration = 1;
857 entries = 1;
858 } else {
859 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
860 for (i=0; i<track->entry; i++) {
861 int64_t duration = i + 1 == track->entry ?
862 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
863 track->cluster[i+1].dts - track->cluster[i].dts;
864 if (i && duration == stts_entries[entries].duration) {
865 stts_entries[entries].count++; /* compress */
866 } else {
867 entries++;
868 stts_entries[entries].duration = duration;
869 stts_entries[entries].count = 1;
870 }
871 }
872 entries++; /* last one */
873 }
874 atom_size = 16 + (entries * 8);
875 put_be32(pb, atom_size); /* size */
1cb5f7fd
MN
876 put_tag(pb, "stts");
877 put_be32(pb, 0); /* version & flags */
aa90239f
BC
878 put_be32(pb, entries); /* entry count */
879 for (i=0; i<entries; i++) {
880 put_be32(pb, stts_entries[i].count);
881 put_be32(pb, stts_entries[i].duration);
882 }
883 av_free(stts_entries);
884 return atom_size;
1cb5f7fd
MN
885}
886
6e6d6dc0 887static int mov_write_dref_tag(ByteIOContext *pb)
1cb5f7fd
MN
888{
889 put_be32(pb, 28); /* size */
890 put_tag(pb, "dref");
891 put_be32(pb, 0); /* version & flags */
892 put_be32(pb, 1); /* entry count */
893
894 put_be32(pb, 0xc); /* size */
895 put_tag(pb, "url ");
896 put_be32(pb, 1); /* version & flags */
897
898 return 28;
899}
900
0724373d 901static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 902{
bc5c918e 903 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
904 put_be32(pb, 0); /* size */
905 put_tag(pb, "stbl");
6e6d6dc0
MN
906 mov_write_stsd_tag(pb, track);
907 mov_write_stts_tag(pb, track);
f578f938 908 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
b371539a 909 track->hasKeyframes && track->hasKeyframes < track->entry)
f578f938 910 mov_write_stss_tag(pb, track);
b4712e3c
BC
911 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
912 track->hasBframes)
913 mov_write_ctts_tag(pb, track);
6e6d6dc0
MN
914 mov_write_stsc_tag(pb, track);
915 mov_write_stsz_tag(pb, track);
916 mov_write_stco_tag(pb, track);
917 return updateSize(pb, pos);
1cb5f7fd
MN
918}
919
6e6d6dc0 920static int mov_write_dinf_tag(ByteIOContext *pb)
1cb5f7fd 921{
bc5c918e 922 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
923 put_be32(pb, 0); /* size */
924 put_tag(pb, "dinf");
6e6d6dc0
MN
925 mov_write_dref_tag(pb);
926 return updateSize(pb, pos);
1cb5f7fd
MN
927}
928
f6204886
DC
929static int mov_write_nmhd_tag(ByteIOContext *pb)
930{
931 put_be32(pb, 12);
932 put_tag(pb, "nmhd");
933 put_be32(pb, 0);
934 return 12;
935}
936
937static int mov_write_gmhd_tag(ByteIOContext *pb)
938{
939 put_be32(pb, 0x20); /* size */
940 put_tag(pb, "gmhd");
941 put_be32(pb, 0x18); /* gmin size */
942 put_tag(pb, "gmin"); /* generic media info */
943 put_be32(pb, 0); /* version & flags */
944 put_be16(pb, 0x40); /* graphics mode = */
945 put_be16(pb, 0x8000); /* opColor (r?) */
946 put_be16(pb, 0x8000); /* opColor (g?) */
947 put_be16(pb, 0x8000); /* opColor (b?) */
948 put_be16(pb, 0); /* balance */
949 put_be16(pb, 0); /* reserved */
950 return 0x20;
951}
952
6e6d6dc0 953static int mov_write_smhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
954{
955 put_be32(pb, 16); /* size */
956 put_tag(pb, "smhd");
957 put_be32(pb, 0); /* version & flags */
958 put_be16(pb, 0); /* reserved (balance, normally = 0) */
959 put_be16(pb, 0); /* reserved */
960 return 16;
961}
962
6e6d6dc0 963static int mov_write_vmhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
964{
965 put_be32(pb, 0x14); /* size (always 0x14) */
966 put_tag(pb, "vmhd");
967 put_be32(pb, 0x01); /* version & flags */
968 put_be64(pb, 0); /* reserved (graphics mode = copy) */
969 return 0x14;
970}
971
0724373d 972static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 973{
985688b8 974 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
bc5c918e 975 int64_t pos = url_ftell(pb);
115329f1 976
9a4d9388 977 if (!track) { /* no media --> data handler */
bb270c08
DB
978 hdlr = "dhlr";
979 hdlr_type = "url ";
980 descr = "DataHandler";
9a4d9388 981 } else {
bb270c08
DB
982 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
983 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
984 hdlr_type = "vide";
985 descr = "VideoHandler";
87609b51 986 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
bb270c08
DB
987 hdlr_type = "soun";
988 descr = "SoundHandler";
87609b51 989 } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
9f520554 990 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
80357cfc 991 else hdlr_type = "text";
f6204886 992 descr = "SubtitleHandler";
bb270c08 993 }
9a4d9388 994 }
115329f1 995
f578f938 996 put_be32(pb, 0); /* size */
1cb5f7fd
MN
997 put_tag(pb, "hdlr");
998 put_be32(pb, 0); /* Version & flags */
906b578f 999 put_buffer(pb, hdlr, 4); /* handler */
9a4d9388 1000 put_tag(pb, hdlr_type); /* handler type */
f578f938
TR
1001 put_be32(pb ,0); /* reserved */
1002 put_be32(pb ,0); /* reserved */
1003 put_be32(pb ,0); /* reserved */
9a4d9388
RS
1004 put_byte(pb, strlen(descr)); /* string counter */
1005 put_buffer(pb, descr, strlen(descr)); /* handler description */
1006 return updateSize(pb, pos);
1007}
1008
0724373d 1009static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track)
9a4d9388 1010{
bc5c918e 1011 int64_t pos = url_ftell(pb);
9a4d9388
RS
1012 put_be32(pb, 0); /* size */
1013 put_tag(pb, "minf");
1cb5f7fd 1014 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
9a4d9388 1015 mov_write_vmhd_tag(pb);
f6204886 1016 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
9a4d9388 1017 mov_write_smhd_tag(pb);
f6204886 1018 else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
9f520554 1019 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
80357cfc 1020 else mov_write_nmhd_tag(pb);
f6204886 1021 }
9a4d9388
RS
1022 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1023 mov_write_hdlr_tag(pb, NULL);
1024 mov_write_dinf_tag(pb);
1025 mov_write_stbl_tag(pb, track);
f578f938 1026 return updateSize(pb, pos);
1cb5f7fd
MN
1027}
1028
0724373d 1029static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 1030{
a3a80ddc
BC
1031 int version = track->trackDuration < INT32_MAX ? 0 : 1;
1032
1033 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
1cb5f7fd 1034 put_tag(pb, "mdhd");
a3a80ddc
BC
1035 put_byte(pb, version);
1036 put_be24(pb, 0); /* flags */
1037 if (version == 1) {
1038 put_be64(pb, track->time);
1039 put_be64(pb, track->time);
1040 } else {
1041 put_be32(pb, track->time); /* creation time */
1042 put_be32(pb, track->time); /* modification time */
1043 }
115329f1 1044 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
a3a80ddc 1045 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
ab561df9 1046 put_be16(pb, track->language); /* language */
1cb5f7fd 1047 put_be16(pb, 0); /* reserved (quality) */
db8f4a92
MN
1048
1049 if(version!=0 && track->mode == MODE_MOV){
1050 av_log(NULL, AV_LOG_ERROR,
1051 "FATAL error, file duration too long for timebase, this file will not be\n"
b1b64c23 1052 "playable with quicktime. Choose a different timebase or a different\n"
db8f4a92
MN
1053 "container format\n");
1054 }
1055
1cb5f7fd
MN
1056 return 32;
1057}
1058
0724373d 1059static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 1060{
bc5c918e 1061 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
1062 put_be32(pb, 0); /* size */
1063 put_tag(pb, "mdia");
6e6d6dc0
MN
1064 mov_write_mdhd_tag(pb, track);
1065 mov_write_hdlr_tag(pb, track);
1066 mov_write_minf_tag(pb, track);
1067 return updateSize(pb, pos);
1cb5f7fd
MN
1068}
1069
59729451 1070static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 1071{
a3a80ddc
BC
1072 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
1073 int version = duration < INT32_MAX ? 0 : 1;
1074
1075 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
1cb5f7fd 1076 put_tag(pb, "tkhd");
a3a80ddc
BC
1077 put_byte(pb, version);
1078 put_be24(pb, 0xf); /* flags (track enabled) */
1079 if (version == 1) {
1080 put_be64(pb, track->time);
1081 put_be64(pb, track->time);
1082 } else {
1083 put_be32(pb, track->time); /* creation time */
1084 put_be32(pb, track->time); /* modification time */
1085 }
1cb5f7fd
MN
1086 put_be32(pb, track->trackID); /* track-id */
1087 put_be32(pb, 0); /* reserved */
a3a80ddc 1088 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
1cb5f7fd
MN
1089
1090 put_be32(pb, 0); /* reserved */
1091 put_be32(pb, 0); /* reserved */
1092 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
1093 /* Volume, only for audio */
1094 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
1095 put_be16(pb, 0x0100);
1096 else
1097 put_be16(pb, 0);
1098 put_be16(pb, 0); /* reserved */
1099
1100 /* Matrix structure */
1101 put_be32(pb, 0x00010000); /* reserved */
1102 put_be32(pb, 0x0); /* reserved */
1103 put_be32(pb, 0x0); /* reserved */
1104 put_be32(pb, 0x0); /* reserved */
1105 put_be32(pb, 0x00010000); /* reserved */
1106 put_be32(pb, 0x0); /* reserved */
1107 put_be32(pb, 0x0); /* reserved */
1108 put_be32(pb, 0x0); /* reserved */
1109 put_be32(pb, 0x40000000); /* reserved */
1110
1111 /* Track width and height, for visual only */
f6204886
DC
1112 if(track->enc->codec_type == CODEC_TYPE_VIDEO ||
1113 track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
59729451 1114 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
6a460c36
BC
1115 if(!sample_aspect_ratio || track->height != track->enc->height)
1116 sample_aspect_ratio = 1;
69dde1ad 1117 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
6a460c36 1118 put_be32(pb, track->height*0x10000);
1cb5f7fd
MN
1119 }
1120 else {
1121 put_be32(pb, 0);
1122 put_be32(pb, 0);
1123 }
1124 return 0x5c;
1125}
1126
8af18154 1127// This box seems important for the psp playback ... without it the movie seems to hang
1128static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
1129{
8af18154 1130 put_be32(pb, 0x24); /* size */
1131 put_tag(pb, "edts");
1132 put_be32(pb, 0x1c); /* size */
1133 put_tag(pb, "elst");
1134 put_be32(pb, 0x0);
1135 put_be32(pb, 0x1);
1136
83fa2ef8 1137 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
8af18154 1138
72289140 1139 put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
8af18154 1140 put_be32(pb, 0x00010000);
1141 return 0x24;
1142}
1143
1144// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1145static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
1146{
1147 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1148 put_tag(pb, "uuid");
1149 put_tag(pb, "USMT");
1150 put_be32(pb, 0x21d24fce);
1151 put_be32(pb, 0xbb88695c);
1152 put_be32(pb, 0xfac9c740);
1153 put_be32(pb, 0x1c); // another size here!
1154 put_tag(pb, "MTDT");
1155 put_be32(pb, 0x00010012);
1156 put_be32(pb, 0x0a);
1157 put_be32(pb, 0x55c40000);
1158 put_be32(pb, 0x1);
1159 put_be32(pb, 0x0);
1160 return 0x34;
1161}
1162
59729451 1163static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 1164{
bc5c918e 1165 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
1166 put_be32(pb, 0); /* size */
1167 put_tag(pb, "trak");
59729451 1168 mov_write_tkhd_tag(pb, track, st);
c6e722e5 1169 if (track->mode == MODE_PSP || track->hasBframes)
8af18154 1170 mov_write_edts_tag(pb, track); // PSP Movies require edts box
6e6d6dc0 1171 mov_write_mdia_tag(pb, track);
115329f1 1172 if (track->mode == MODE_PSP)
8af18154 1173 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
6e6d6dc0 1174 return updateSize(pb, pos);
1cb5f7fd
MN
1175}
1176
88730be6 1177#if 0
1cb5f7fd 1178/* TODO: Not sorted out, but not necessary either */
2d243fb3 1179static int mov_write_iods_tag(ByteIOContext *pb, MOVMuxContext *mov)
1cb5f7fd
MN
1180{
1181 put_be32(pb, 0x15); /* size */
1182 put_tag(pb, "iods");
1183 put_be32(pb, 0); /* version & flags */
1184 put_be16(pb, 0x1007);
1185 put_byte(pb, 0);
1186 put_be16(pb, 0x4fff);
1187 put_be16(pb, 0xfffe);
1188 put_be16(pb, 0x01ff);
1189 return 0x15;
1190}
88730be6 1191#endif
1cb5f7fd 1192
2d243fb3 1193static int mov_write_mvhd_tag(ByteIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1194{
b29af723
MN
1195 int maxTrackID = 1, i;
1196 int64_t maxTrackLenTemp, maxTrackLen = 0;
a3a80ddc 1197 int version;
1cb5f7fd 1198
42fb4148 1199 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1200 if(mov->tracks[i].entry > 0) {
83fa2ef8 1201 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
f578f938
TR
1202 if(maxTrackLen < maxTrackLenTemp)
1203 maxTrackLen = maxTrackLenTemp;
1cb5f7fd
MN
1204 if(maxTrackID < mov->tracks[i].trackID)
1205 maxTrackID = mov->tracks[i].trackID;
1206 }
1207 }
a3a80ddc
BC
1208
1209 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1210 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1211 put_tag(pb, "mvhd");
1212 put_byte(pb, version);
1213 put_be24(pb, 0); /* flags */
1214 if (version == 1) {
1215 put_be64(pb, mov->time);
1216 put_be64(pb, mov->time);
1217 } else {
1218 put_be32(pb, mov->time); /* creation time */
1219 put_be32(pb, mov->time); /* modification time */
1220 }
1221 put_be32(pb, mov->timescale); /* timescale */
1222 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1cb5f7fd
MN
1223
1224 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1225 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1226 put_be16(pb, 0); /* reserved */
1227 put_be32(pb, 0); /* reserved */
1228 put_be32(pb, 0); /* reserved */
1229
1230 /* Matrix structure */
1231 put_be32(pb, 0x00010000); /* reserved */
1232 put_be32(pb, 0x0); /* reserved */
1233 put_be32(pb, 0x0); /* reserved */
1234 put_be32(pb, 0x0); /* reserved */
1235 put_be32(pb, 0x00010000); /* reserved */
1236 put_be32(pb, 0x0); /* reserved */
1237 put_be32(pb, 0x0); /* reserved */
1238 put_be32(pb, 0x0); /* reserved */
1239 put_be32(pb, 0x40000000); /* reserved */
1240
1241 put_be32(pb, 0); /* reserved (preview time) */
1242 put_be32(pb, 0); /* reserved (preview duration) */
1243 put_be32(pb, 0); /* reserved (poster time) */
1244 put_be32(pb, 0); /* reserved (selection time) */
1245 put_be32(pb, 0); /* reserved (selection duration) */
1246 put_be32(pb, 0); /* reserved (current time) */
1247 put_be32(pb, maxTrackID+1); /* Next track id */
1248 return 0x6c;
1249}
1250
2d243fb3 1251static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1252 AVFormatContext *s)
1253{
bc5c918e 1254 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1255 put_be32(pb, 0); /* size */
1256 put_tag(pb, "hdlr");
1257 put_be32(pb, 0);
1258 put_be32(pb, 0);
1259 put_tag(pb, "mdir");
1260 put_tag(pb, "appl");
1261 put_be32(pb, 0);
1262 put_be32(pb, 0);
1263 put_be16(pb, 0);
1264 return updateSize(pb, pos);
1265}
1266
1267/* helper function to write a data tag with the specified string as data */
d9fc9ff3 1268static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int lang, int long_style)
b6c50eb1 1269{
efda3395 1270 if(long_style){
bc5c918e 1271 int64_t pos = url_ftell(pb);
b6c50eb1 1272 put_be32(pb, 0); /* size */
efda3395
MN
1273 put_tag(pb, "data");
1274 put_be32(pb, 1);
1275 put_be32(pb, 0);
1276 put_buffer(pb, data, strlen(data));
1277 return updateSize(pb, pos);
1278 }else{
1279 put_be16(pb, strlen(data)); /* string length */
d9fc9ff3 1280 put_be16(pb, lang);
efda3395
MN
1281 put_buffer(pb, data, strlen(data));
1282 return strlen(data) + 4;
b6c50eb1 1283 }
b6c50eb1
PB
1284}
1285
d9fc9ff3 1286static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int lang, int long_style){
b6c50eb1 1287 int size = 0;
91208916 1288 if (value && value[0]) {
bc5c918e 1289 int64_t pos = url_ftell(pb);
b6c50eb1 1290 put_be32(pb, 0); /* size */
efda3395 1291 put_tag(pb, name);
d9fc9ff3 1292 mov_write_string_data_tag(pb, value, lang, long_style);
efda3395 1293 size= updateSize(pb, pos);
b6c50eb1
PB
1294 }
1295 return size;
1296}
1297
1ee2d448
AJ
1298static int mov_write_string_metadata(AVFormatContext *s, ByteIOContext *pb,
1299 const char *name, const char *tag,
1300 int long_style)
b6c50eb1 1301{
d9fc9ff3
AJ
1302 int l, lang = 0, len, len2;
1303 AVMetadataTag *t, *t2 = NULL;
1304 char tag2[16];
1ee2d448
AJ
1305
1306 if (!(t = av_metadata_get(s->metadata, tag, NULL, 0)))
efda3395 1307 return 0;
1ee2d448 1308
d9fc9ff3
AJ
1309 len = strlen(t->key);
1310 snprintf(tag2, sizeof(tag2), "%s-", tag);
1311 while ((t2 = av_metadata_get(s->metadata, tag2, t2, AV_METADATA_IGNORE_SUFFIX))) {
1312 len2 = strlen(t2->key);
1313 if (len2 == len+4 && !strcmp(t->value, t2->value)
1314 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 0)) >= 0) {
1315 lang = l;
1316 break;
1317 }
1318 }
1319 return mov_write_string_tag(pb, name, t->value, lang, long_style);
b6c50eb1
PB
1320}
1321
1322/* iTunes track number */
2d243fb3 1323static int mov_write_trkn_tag(ByteIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1324 AVFormatContext *s)
1325{
1ee2d448
AJ
1326 AVMetadataTag *t = av_metadata_get(s->metadata, "track", NULL, 0);
1327 int size = 0, track = t ? atoi(t->value) : 0;
1328 if (track) {
bc5c918e 1329 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1330 put_be32(pb, 0); /* size */
1331 put_tag(pb, "trkn");
1332 {
bc5c918e 1333 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1334 put_be32(pb, 0); /* size */
1335 put_tag(pb, "data");
1336 put_be32(pb, 0); // 8 bytes empty
1337 put_be32(pb, 0);
1338 put_be16(pb, 0); // empty
1ee2d448 1339 put_be16(pb, track); // track number
b6c50eb1
PB
1340 put_be16(pb, 0); // total track number
1341 put_be16(pb, 0); // empty
1342 updateSize(pb, pos);
1343 }
1344 size = updateSize(pb, pos);
1345 }
1346 return size;
1347}
1348
1349/* iTunes meta data list */
2d243fb3 1350static int mov_write_ilst_tag(ByteIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1351 AVFormatContext *s)
1352{
bc5c918e 1353 int64_t pos = url_ftell(pb);
b6c50eb1
PB
1354 put_be32(pb, 0); /* size */
1355 put_tag(pb, "ilst");
1ee2d448
AJ
1356 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1357 mov_write_string_metadata(s, pb, "\251ART", "author" , 1);
1358 mov_write_string_metadata(s, pb, "\251wrt", "author" , 1);
1359 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1360 mov_write_string_metadata(s, pb, "\251day", "year" , 1);
d9fc9ff3 1361 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1ee2d448
AJ
1362 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1363 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1364 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
b6c50eb1
PB
1365 mov_write_trkn_tag(pb, mov, s);
1366 return updateSize(pb, pos);
1367}
1368
1369/* iTunes meta data tag */
2d243fb3 1370static int mov_write_meta_tag(ByteIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1371 AVFormatContext *s)
1372{
1373 int size = 0;
b97fb809
AJ
1374 int64_t pos = url_ftell(pb);
1375 put_be32(pb, 0); /* size */
1376 put_tag(pb, "meta");
1377 put_be32(pb, 0);
1378 mov_write_itunes_hdlr_tag(pb, mov, s);
1379 mov_write_ilst_tag(pb, mov, s);
1380 size = updateSize(pb, pos);
b6c50eb1
PB
1381 return size;
1382}
115329f1 1383
07b7b06d
BC
1384static int utf8len(const uint8_t *b)
1385{
1386 int len=0;
1387 int val;
1388 while(*b){
1389 GET_UTF8(val, *b++, return -1;)
1390 len++;
1391 }
1392 return len;
1393}
1394
1395static int ascii_to_wc(ByteIOContext *pb, const uint8_t *b)
1396{
1397 int val;
1398 while(*b){
1399 GET_UTF8(val, *b++, return -1;)
1400 put_be16(pb, val);
1401 }
1402 put_be16(pb, 0x00);
1403 return 0;
1404}
1405
1406static uint16_t language_code(const char *str)
1407{
1408 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1409}
1410
c6e2c6c9
BC
1411static int mov_write_3gp_udta_tag(ByteIOContext *pb, AVFormatContext *s,
1412 const char *tag, const char *str)
1413{
bc5c918e 1414 int64_t pos = url_ftell(pb);
1ee2d448
AJ
1415 AVMetadataTag *t = av_metadata_get(s->metadata, str, NULL, 0);
1416 if (!t || !utf8len(t->value))
c6e2c6c9
BC
1417 return 0;
1418 put_be32(pb, 0); /* size */
1419 put_tag (pb, tag); /* type */
1420 put_be32(pb, 0); /* version + flags */
1421 if (!strcmp(tag, "yrrc"))
1ee2d448 1422 put_be16(pb, atoi(t->value));
c6e2c6c9
BC
1423 else {
1424 put_be16(pb, language_code("eng")); /* language */
1ee2d448
AJ
1425 ascii_to_wc(pb, t->value);
1426 if (!strcmp(tag, "albm") &&
1427 (t = av_metadata_get(s->metadata, "year", NULL, 0)))
1428 put_byte(pb, atoi(t->value));
c6e2c6c9
BC
1429 }
1430 return updateSize(pb, pos);
1431}
1432
2d243fb3 1433static int mov_write_udta_tag(ByteIOContext *pb, MOVMuxContext *mov,
69dde1ad
GB
1434 AVFormatContext *s)
1435{
1ee2d448
AJ
1436 ByteIOContext *pb_buf;
1437 int i, ret, size;
1438 uint8_t *buf;
89a0d790 1439
d1d87dfb
BC
1440 for (i = 0; i < s->nb_streams; i++)
1441 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1ee2d448 1442 return 0;
d1d87dfb
BC
1443 }
1444
1ee2d448
AJ
1445 ret = url_open_dyn_buf(&pb_buf);
1446 if(ret < 0)
1447 return ret;
69dde1ad 1448
a04aec6d 1449 if (mov->mode & MODE_3GP) {
1ee2d448
AJ
1450 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1451 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1452 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1453 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1454 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1455 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1456 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "year");
ce072b2d 1457 } 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
1458 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1459 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1460 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1461 mov_write_string_metadata(s, pb_buf, "\251day", "year" , 0);
d9fc9ff3 1462 mov_write_string_tag(pb_buf, "\251enc", LIBAVFORMAT_IDENT, 0, 0);
1ee2d448
AJ
1463 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1464 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1465 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
c6e2c6c9 1466 } else {
283c9a8e 1467 /* iTunes meta data */
1ee2d448 1468 mov_write_meta_tag(pb_buf, mov, s);
c6e2c6c9 1469 }
1ee2d448
AJ
1470
1471 if ((size = url_close_dyn_buf(pb_buf, &buf)) > 0) {
1472 put_be32(pb, size+8);
1473 put_tag(pb, "udta");
1474 put_buffer(pb, buf, size);
1475 av_free(buf);
89a0d790
BF
1476 }
1477
1478 return 0;
69dde1ad
GB
1479}
1480
d32d6def 1481static void mov_write_psp_udta_tag(ByteIOContext *pb,
20e22af8
BC
1482 const char *str, const char *lang, int type)
1483{
1484 int len = utf8len(str)+1;
1485 if(len<=0)
d32d6def 1486 return;
20e22af8
BC
1487 put_be16(pb, len*2+10); /* size */
1488 put_be32(pb, type); /* type */
1489 put_be16(pb, language_code(lang)); /* language */
1490 put_be16(pb, 0x01); /* ? */
1491 ascii_to_wc(pb, str);
20e22af8
BC
1492}
1493
0724373d 1494static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
dcfdb046 1495{
1ee2d448 1496 AVMetadataTag *title = av_metadata_get(s->metadata, "title", NULL, 0);
bc5c918e 1497 int64_t pos, pos2;
dcfdb046 1498
1ee2d448 1499 if (title) {
dcfdb046
BL
1500 pos = url_ftell(pb);
1501 put_be32(pb, 0); /* size placeholder*/
1502 put_tag(pb, "uuid");
1503 put_tag(pb, "USMT");
91208916
BC
1504 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1505 put_be32(pb, 0xbb88695c);
1506 put_be32(pb, 0xfac9c740);
dcfdb046 1507
d32d6def 1508 pos2 = url_ftell(pb);
dcfdb046
BL
1509 put_be32(pb, 0); /* size placeholder*/
1510 put_tag(pb, "MTDT");
7cf0e16f 1511 put_be16(pb, 4);
dcfdb046 1512
7cf0e16f
MN
1513 // ?
1514 put_be16(pb, 0x0C); /* size */
1515 put_be32(pb, 0x0B); /* type */
1516 put_be16(pb, language_code("und")); /* language */
1517 put_be16(pb, 0x0); /* ? */
1518 put_be16(pb, 0x021C); /* data */
7cf0e16f 1519
d32d6def 1520 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1ee2d448 1521 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
7cf0e16f 1522// 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
1523 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1524
1525 updateSize(pb, pos2);
1526 return updateSize(pb, pos);
dcfdb046
BL
1527 }
1528
d32d6def 1529 return 0;
dcfdb046
BL
1530}
1531
2d243fb3 1532static int mov_write_moov_tag(ByteIOContext *pb, MOVMuxContext *mov,
69dde1ad 1533 AVFormatContext *s)
1cb5f7fd 1534{
b29af723 1535 int i;
bc5c918e 1536 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
1537 put_be32(pb, 0); /* size placeholder*/
1538 put_tag(pb, "moov");
1539 mov->timescale = globalTimescale;
1540
42fb4148 1541 for (i=0; i<mov->nb_streams; i++) {
e45ccf79
GB
1542 if(mov->tracks[i].entry <= 0) continue;
1543
e45ccf79
GB
1544 mov->tracks[i].time = mov->time;
1545 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
1546 }
1547
6e6d6dc0
MN
1548 mov_write_mvhd_tag(pb, mov);
1549 //mov_write_iods_tag(pb, mov);
42fb4148 1550 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1551 if(mov->tracks[i].entry > 0) {
59729451 1552 mov_write_trak_tag(pb, &(mov->tracks[i]), s->streams[i]);
1cb5f7fd
MN
1553 }
1554 }
1555
dcfdb046
BL
1556 if (mov->mode == MODE_PSP)
1557 mov_write_uuidusmt_tag(pb, s);
c6e2c6c9 1558 else
e344c1ea 1559 mov_write_udta_tag(pb, mov, s);
69dde1ad 1560
6e6d6dc0 1561 return updateSize(pb, pos);
1cb5f7fd
MN
1562}
1563
2d243fb3 1564static int mov_write_mdat_tag(ByteIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1565{
b29af723 1566 put_be32(pb, 8); // placeholder for extended size field (64 bit)
5340a0bc 1567 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
b29af723 1568
115329f1 1569 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
1570 put_be32(pb, 0); /* size placeholder*/
1571 put_tag(pb, "mdat");
1572 return 0;
1573}
1574
1575/* TODO: This needs to be more general */
7b0fb8b7 1576static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 1577{
2d243fb3 1578 MOVMuxContext *mov = s->priv_data;
bc5c918e 1579 int64_t pos = url_ftell(pb);
cd70d17f 1580 int has_h264 = 0, has_video = 0;
40f8675c 1581 int minor = 0x200;
c55cc074 1582 int i;
69dde1ad 1583
83579084
BC
1584 for (i = 0; i < s->nb_streams; i++) {
1585 AVStream *st = s->streams[i];
cd70d17f
BC
1586 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1587 has_video = 1;
83579084
BC
1588 if (st->codec->codec_id == CODEC_ID_H264)
1589 has_h264 = 1;
1590 }
1591
1560b667 1592 put_be32(pb, 0); /* size */
1cb5f7fd 1593 put_tag(pb, "ftyp");
e45ccf79 1594
40f8675c
BC
1595 if (mov->mode == MODE_3GP) {
1596 put_tag(pb, has_h264 ? "3gp6" : "3gp4");
1597 minor = has_h264 ? 0x100 : 0x200;
1598 } else if (mov->mode & MODE_3G2) {
1599 put_tag(pb, has_h264 ? "3g2b" : "3g2a");
1600 minor = has_h264 ? 0x20000 : 0x10000;
1601 }else if (mov->mode == MODE_PSP)
8af18154 1602 put_tag(pb, "MSNV");
c55cc074 1603 else if (mov->mode == MODE_MP4)
e45ccf79 1604 put_tag(pb, "isom");
cd70d17f
BC
1605 else if (mov->mode == MODE_IPOD)
1606 put_tag(pb, has_video ? "M4V ":"M4A ");
1607 else
26f86eaf 1608 put_tag(pb, "qt ");
e45ccf79 1609
40f8675c 1610 put_be32(pb, minor);
e45ccf79 1611
6ba9ed95
BC
1612 if(mov->mode == MODE_MOV)
1613 put_tag(pb, "qt ");
1614 else{
ed6e47c2 1615 put_tag(pb, "isom");
65f52683 1616 put_tag(pb, "iso2");
83579084
BC
1617 if(has_h264)
1618 put_tag(pb, "avc1");
6ba9ed95 1619 }
ed6e47c2 1620
91208916 1621 if (mov->mode == MODE_3GP)
3473ca9a 1622 put_tag(pb, has_h264 ? "3gp6":"3gp4");
a04aec6d 1623 else if (mov->mode & MODE_3G2)
3473ca9a 1624 put_tag(pb, has_h264 ? "3g2b":"3g2a");
91208916 1625 else if (mov->mode == MODE_PSP)
8af18154 1626 put_tag(pb, "MSNV");
c55cc074 1627 else if (mov->mode == MODE_MP4)
e45ccf79 1628 put_tag(pb, "mp41");
1560b667 1629 return updateSize(pb, pos);
1cb5f7fd
MN
1630}
1631
0c716ab7 1632static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
8af18154 1633{
dcfdb046
BL
1634 AVCodecContext *VideoCodec = s->streams[0]->codec;
1635 AVCodecContext *AudioCodec = s->streams[1]->codec;
1636 int AudioRate = AudioCodec->sample_rate;
1637 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
9e282ba3
MN
1638 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1639 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
8af18154 1640
91208916 1641 put_be32(pb, 0x94); /* size */
8af18154 1642 put_tag(pb, "uuid");
1643 put_tag(pb, "PROF");
1644
91208916
BC
1645 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1646 put_be32(pb, 0xbb88695c);
1647 put_be32(pb, 0xfac9c740);
8af18154 1648
91208916
BC
1649 put_be32(pb, 0x0); /* ? */
1650 put_be32(pb, 0x3); /* 3 sections ? */
8af18154 1651
91208916 1652 put_be32(pb, 0x14); /* size */
8af18154 1653 put_tag(pb, "FPRF");
91208916
BC
1654 put_be32(pb, 0x0); /* ? */
1655 put_be32(pb, 0x0); /* ? */
1656 put_be32(pb, 0x0); /* ? */
8af18154 1657
91208916 1658 put_be32(pb, 0x2c); /* size */
8af18154 1659 put_tag(pb, "APRF"); /* audio */
91208916
BC
1660 put_be32(pb, 0x0);
1661 put_be32(pb, 0x2); /* TrackID */
8af18154 1662 put_tag(pb, "mp4a");
91208916
BC
1663 put_be32(pb, 0x20f);
1664 put_be32(pb, 0x0);
9e282ba3
MN
1665 put_be32(pb, audio_kbitrate);
1666 put_be32(pb, audio_kbitrate);
91208916
BC
1667 put_be32(pb, AudioRate);
1668 put_be32(pb, AudioCodec->channels);
8af18154 1669
91208916 1670 put_be32(pb, 0x34); /* size */
8af18154 1671 put_tag(pb, "VPRF"); /* video */
91208916
BC
1672 put_be32(pb, 0x0);
1673 put_be32(pb, 0x1); /* TrackID */
87b041e0
MN
1674 if (VideoCodec->codec_id == CODEC_ID_H264) {
1675 put_tag(pb, "avc1");
91208916
BC
1676 put_be16(pb, 0x014D);
1677 put_be16(pb, 0x0015);
87b041e0
MN
1678 } else {
1679 put_tag(pb, "mp4v");
91208916
BC
1680 put_be16(pb, 0x0000);
1681 put_be16(pb, 0x0103);
87b041e0 1682 }
91208916 1683 put_be32(pb, 0x0);
9e282ba3
MN
1684 put_be32(pb, video_kbitrate);
1685 put_be32(pb, video_kbitrate);
dcfdb046
BL
1686 put_be32(pb, FrameRate);
1687 put_be32(pb, FrameRate);
1688 put_be16(pb, VideoCodec->width);
1689 put_be16(pb, VideoCodec->height);
1690 put_be32(pb, 0x010001); /* ? */
8af18154 1691}
1692
1cb5f7fd
MN
1693static int mov_write_header(AVFormatContext *s)
1694{
899681cd 1695 ByteIOContext *pb = s->pb;
2d243fb3 1696 MOVMuxContext *mov = s->priv_data;
69dde1ad 1697 int i;
1cb5f7fd 1698
899681cd 1699 if (url_is_streamed(s->pb)) {
4f60f34a
BC
1700 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1701 return -1;
1702 }
1703
1522767d
BC
1704 /* Default mode == MP4 */
1705 mov->mode = MODE_MP4;
1706
1707 if (s->oformat != NULL) {
1708 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
a04aec6d 1709 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
1522767d
BC
1710 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1711 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
aa9f4208 1712 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
1522767d 1713
26f86eaf 1714 mov_write_ftyp_tag(pb,s);
91208916
BC
1715 if (mov->mode == MODE_PSP) {
1716 if (s->nb_streams != 2) {
1522767d
BC
1717 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1718 return -1;
1719 }
1720 mov_write_uuidprof_tag(pb,s);
1721 }
1722 }
1723
5c5776e1
BC
1724 mov->tracks = av_mallocz(s->nb_streams*sizeof(*mov->tracks));
1725 if (!mov->tracks)
1726 return AVERROR(ENOMEM);
1727
ffdd57d4 1728 for(i=0; i<s->nb_streams; i++){
039627cf
BC
1729 AVStream *st= s->streams[i];
1730 MOVTrack *track= &mov->tracks[i];
1ee2d448 1731 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
039627cf
BC
1732
1733 track->enc = st->codec;
1ee2d448 1734 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2904fd39
AJ
1735 if (track->language < 0)
1736 track->language = 0;
2c5ad5fd 1737 track->mode = mov->mode;
a23c9c4a
BC
1738 track->tag = mov_find_codec_tag(s, track);
1739 if (!track->tag) {
bc4c3a55
BC
1740 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
1741 "codec not currently supported in container\n", i);
5c5776e1 1742 goto error;
a23c9c4a 1743 }
039627cf 1744 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
6a460c36
BC
1745 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
1746 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
1747 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
1748 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
1749 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
5c5776e1 1750 goto error;
6a460c36
BC
1751 }
1752 track->height = track->tag>>24 == 'n' ? 486 : 576;
a2523ce2 1753 }
ffb512e3 1754 track->timescale = st->codec->time_base.den;
039627cf 1755 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
c8134d71 1756 if (track->mode == MODE_MOV && track->timescale > 100000)
4ad08acc 1757 av_log(s, AV_LOG_WARNING,
911f9b71
BC
1758 "WARNING codec timebase is very high. If duration is too long,\n"
1759 "file may not be playable by quicktime. Specify a shorter timebase\n"
1760 "or choose different container.\n");
039627cf 1761 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
ffb512e3 1762 track->timescale = st->codec->sample_rate;
039627cf 1763 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
cf74dd4a 1764 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
b596102e 1765 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
5c5776e1 1766 goto error;
4278662d
BC
1767 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1768 track->audio_vbr = 1;
1769 }else{
cf74dd4a 1770 st->codec->frame_size = 1;
4278662d 1771 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
b596102e 1772 }
e00c3de8
BC
1773 if(track->mode != MODE_MOV &&
1774 track->enc->codec_id == CODEC_ID_MP3 && track->enc->sample_rate < 16000){
1775 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
1776 i, track->enc->sample_rate);
5c5776e1 1777 goto error;
e00c3de8 1778 }
f6204886
DC
1779 }else if(st->codec->codec_type == CODEC_TYPE_SUBTITLE){
1780 track->timescale = st->codec->time_base.den;
1781 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
ffb512e3 1782 }
a2523ce2
BC
1783 if (!track->height)
1784 track->height = st->codec->height;
f578f938
TR
1785 }
1786
18c697ac
BC
1787 mov_write_mdat_tag(pb, mov);
1788 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
42fb4148 1789 mov->nb_streams = s->nb_streams;
18c697ac 1790
1cb5f7fd
MN
1791 put_flush_packet(pb);
1792
1793 return 0;
5c5776e1
BC
1794 error:
1795 av_freep(&mov->tracks);
1796 return -1;
1cb5f7fd
MN
1797}
1798
e928649b 1799static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd 1800{
2d243fb3 1801 MOVMuxContext *mov = s->priv_data;
899681cd 1802 ByteIOContext *pb = s->pb;
039627cf
BC
1803 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1804 AVCodecContext *enc = trk->enc;
e45ccf79 1805 unsigned int samplesInChunk = 0;
e928649b 1806 int size= pkt->size;
1cb5f7fd 1807
899681cd 1808 if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
e45ccf79 1809 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 1810
06fcf56c 1811 if (enc->codec_id == CODEC_ID_AMR_NB) {
5cb49ca1
BC
1812 /* We must find out how many AMR blocks there are in one packet */
1813 static uint16_t packed_size[16] =
1814 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1815 int len = 0;
1816
1817 while (len < size && samplesInChunk < 100) {
1818 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1819 samplesInChunk++;
1cb5f7fd 1820 }
2111e3f9 1821 if(samplesInChunk > 1){
89938d29 1822 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2111e3f9
MN
1823 return -1;
1824 }
5cb49ca1
BC
1825 } else if (trk->sampleSize)
1826 samplesInChunk = size/trk->sampleSize;
1827 else
5616f85d 1828 samplesInChunk = 1;
e45ccf79 1829
5616f85d
BC
1830 /* copy extradata if it exists */
1831 if (trk->vosLen == 0 && enc->extradata_size > 0) {
e45ccf79
GB
1832 trk->vosLen = enc->extradata_size;
1833 trk->vosData = av_malloc(trk->vosLen);
1834 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1835 }
1836
5d2160a0
BC
1837 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1838 /* from x264 or from bytestream h264 */
1839 /* nal reformating needed */
1840 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
1841 } else {
1842 put_buffer(pb, pkt->data, size);
1843 }
1844
0a63a676 1845 if ((enc->codec_id == CODEC_ID_DNXHD ||
d4a240cb 1846 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
db568c07
BC
1847 /* copy frame to create needed atoms */
1848 trk->vosLen = size;
1849 trk->vosData = av_malloc(size);
e8c4df40
BC
1850 if (!trk->vosData)
1851 return AVERROR(ENOMEM);
db568c07 1852 memcpy(trk->vosData, pkt->data, size);
c1b8e6d8
BC
1853 }
1854
0aec3c5c
BC
1855 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1856 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
e45ccf79
GB
1857 if (!trk->cluster)
1858 return -1;
e45ccf79 1859 }
e45ccf79 1860
5d2160a0 1861 trk->cluster[trk->entry].pos = url_ftell(pb) - size;
0aec3c5c
BC
1862 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1863 trk->cluster[trk->entry].size = size;
1864 trk->cluster[trk->entry].entries = samplesInChunk;
aa90239f
BC
1865 trk->cluster[trk->entry].dts = pkt->dts;
1866 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1867
96f69e0d
BC
1868 if (pkt->pts == AV_NOPTS_VALUE) {
1869 av_log(s, AV_LOG_WARNING, "pts has no value\n");
1870 pkt->pts = pkt->dts;
1871 }
c4f078ff
BC
1872 if (pkt->dts != pkt->pts)
1873 trk->hasBframes = 1;
1874 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1875 trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1876 if(trk->cluster[trk->entry].key_frame)
1877 trk->hasKeyframes++;
e45ccf79
GB
1878 trk->entry++;
1879 trk->sampleCount += samplesInChunk;
039627cf 1880 mov->mdat_size += size;
e45ccf79 1881
1cb5f7fd
MN
1882 put_flush_packet(pb);
1883 return 0;
1884}
1885
1886static int mov_write_trailer(AVFormatContext *s)
1887{
2d243fb3 1888 MOVMuxContext *mov = s->priv_data;
899681cd 1889 ByteIOContext *pb = s->pb;
1cb5f7fd 1890 int res = 0;
0aec3c5c 1891 int i;
1cb5f7fd 1892
bc5c918e 1893 int64_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1894
1895 /* Write size of mdat tag */
039627cf 1896 if (mov->mdat_size+8 <= UINT32_MAX) {
b29af723 1897 url_fseek(pb, mov->mdat_pos, SEEK_SET);
039627cf 1898 put_be32(pb, mov->mdat_size+8);
b29af723
MN
1899 } else {
1900 /* overwrite 'wide' placeholder atom */
1901 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1902 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1903 put_tag(pb, "mdat");
039627cf 1904 put_be64(pb, mov->mdat_size+16);
b29af723 1905 }
69dde1ad 1906 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1907
69dde1ad 1908 mov_write_moov_tag(pb, mov, s);
1cb5f7fd 1909
42fb4148 1910 for (i=0; i<mov->nb_streams; i++) {
0aec3c5c
BC
1911 av_freep(&mov->tracks[i].cluster);
1912
91208916 1913 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
ec7d0d2e 1914
1cb5f7fd 1915 }
69dde1ad 1916
1cb5f7fd
MN
1917 put_flush_packet(pb);
1918
5c5776e1
BC
1919 av_freep(&mov->tracks);
1920
1cb5f7fd
MN
1921 return res;
1922}
1923
b250f9c6 1924#if CONFIG_MOV_MUXER
ff70e601 1925AVOutputFormat mov_muxer = {
1cb5f7fd 1926 "mov",
bde15e74 1927 NULL_IF_CONFIG_SMALL("MOV format"),
1cb5f7fd
MN
1928 NULL,
1929 "mov",
2d243fb3 1930 sizeof(MOVMuxContext),
69dde1ad 1931 CODEC_ID_AAC,
2187d948 1932 CODEC_ID_MPEG4,
1cb5f7fd
MN
1933 mov_write_header,
1934 mov_write_packet,
1935 mov_write_trailer,
25918212 1936 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
c1854592 1937 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
1cb5f7fd 1938};
ff70e601 1939#endif
b250f9c6 1940#if CONFIG_TGP_MUXER
ff70e601 1941AVOutputFormat tgp_muxer = {
1cb5f7fd 1942 "3gp",
bde15e74 1943 NULL_IF_CONFIG_SMALL("3GP format"),
1cb5f7fd
MN
1944 NULL,
1945 "3gp",
2d243fb3 1946 sizeof(MOVMuxContext),
1cb5f7fd
MN
1947 CODEC_ID_AMR_NB,
1948 CODEC_ID_H263,
1949 mov_write_header,
1950 mov_write_packet,
1951 mov_write_trailer,
c64d476c 1952 .flags = AVFMT_GLOBALHEADER,
c1854592 1953 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
1cb5f7fd 1954};
ff70e601 1955#endif
b250f9c6 1956#if CONFIG_MP4_MUXER
ff70e601 1957AVOutputFormat mp4_muxer = {
1cb5f7fd 1958 "mp4",
bde15e74 1959 NULL_IF_CONFIG_SMALL("MP4 format"),
4cb3f3b6 1960 "application/mp4",
ccec1b69 1961 "mp4",
2d243fb3 1962 sizeof(MOVMuxContext),
1cb5f7fd
MN
1963 CODEC_ID_AAC,
1964 CODEC_ID_MPEG4,
1965 mov_write_header,
1966 mov_write_packet,
1967 mov_write_trailer,
25918212 1968 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
c1854592 1969 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
1cb5f7fd 1970};
ff70e601 1971#endif
b250f9c6 1972#if CONFIG_PSP_MUXER
ff70e601 1973AVOutputFormat psp_muxer = {
8af18154 1974 "psp",
bde15e74 1975 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
8af18154 1976 NULL,
1977 "mp4,psp",
2d243fb3 1978 sizeof(MOVMuxContext),
8af18154 1979 CODEC_ID_AAC,
1980 CODEC_ID_MPEG4,
1981 mov_write_header,
1982 mov_write_packet,
1983 mov_write_trailer,
dcfdb046 1984 .flags = AVFMT_GLOBALHEADER,
c1854592 1985 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
8af18154 1986};
ff70e601 1987#endif
b250f9c6 1988#if CONFIG_TG2_MUXER
ff70e601 1989AVOutputFormat tg2_muxer = {
8536ab89 1990 "3g2",
bde15e74 1991 NULL_IF_CONFIG_SMALL("3GP2 format"),
8536ab89 1992 NULL,
1993 "3g2",
2d243fb3 1994 sizeof(MOVMuxContext),
8536ab89 1995 CODEC_ID_AMR_NB,
1996 CODEC_ID_H263,
1997 mov_write_header,
1998 mov_write_packet,
1999 mov_write_trailer,
c64d476c 2000 .flags = AVFMT_GLOBALHEADER,
c1854592 2001 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
8536ab89 2002};
ff70e601 2003#endif
b250f9c6 2004#if CONFIG_IPOD_MUXER
aa9f4208
BC
2005AVOutputFormat ipod_muxer = {
2006 "ipod",
bde15e74 2007 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
aa9f4208 2008 "application/mp4",
ccec1b69 2009 "m4v,m4a",
2d243fb3 2010 sizeof(MOVMuxContext),
aa9f4208
BC
2011 CODEC_ID_AAC,
2012 CODEC_ID_H264,
2013 mov_write_header,
2014 mov_write_packet,
2015 mov_write_trailer,
2016 .flags = AVFMT_GLOBALHEADER,
d1df4da0 2017 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
aa9f4208
BC
2018};
2019#endif