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