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