MOV: Fix writing \251wrt metadata (r20453 only fixed the demuxer)
[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);
b6c50eb1
PB
1380 mov_write_trkn_tag(pb, mov, s);
1381 return updateSize(pb, pos);
1382}
1383
1384/* iTunes meta data tag */
2d243fb3 1385static int mov_write_meta_tag(ByteIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1386 AVFormatContext *s)
1387{
1388 int size = 0;
b97fb809
AJ
1389 int64_t pos = url_ftell(pb);
1390 put_be32(pb, 0); /* size */
1391 put_tag(pb, "meta");
1392 put_be32(pb, 0);
1393 mov_write_itunes_hdlr_tag(pb, mov, s);
1394 mov_write_ilst_tag(pb, mov, s);
1395 size = updateSize(pb, pos);
b6c50eb1
PB
1396 return size;
1397}
115329f1 1398
07b7b06d
BC
1399static int utf8len(const uint8_t *b)
1400{
1401 int len=0;
1402 int val;
1403 while(*b){
1404 GET_UTF8(val, *b++, return -1;)
1405 len++;
1406 }
1407 return len;
1408}
1409
1410static int ascii_to_wc(ByteIOContext *pb, const uint8_t *b)
1411{
1412 int val;
1413 while(*b){
1414 GET_UTF8(val, *b++, return -1;)
1415 put_be16(pb, val);
1416 }
1417 put_be16(pb, 0x00);
1418 return 0;
1419}
1420
1421static uint16_t language_code(const char *str)
1422{
1423 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1424}
1425
c6e2c6c9
BC
1426static int mov_write_3gp_udta_tag(ByteIOContext *pb, AVFormatContext *s,
1427 const char *tag, const char *str)
1428{
bc5c918e 1429 int64_t pos = url_ftell(pb);
1ee2d448
AJ
1430 AVMetadataTag *t = av_metadata_get(s->metadata, str, NULL, 0);
1431 if (!t || !utf8len(t->value))
c6e2c6c9
BC
1432 return 0;
1433 put_be32(pb, 0); /* size */
1434 put_tag (pb, tag); /* type */
1435 put_be32(pb, 0); /* version + flags */
1436 if (!strcmp(tag, "yrrc"))
1ee2d448 1437 put_be16(pb, atoi(t->value));
c6e2c6c9
BC
1438 else {
1439 put_be16(pb, language_code("eng")); /* language */
15c315b7 1440 put_buffer(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1ee2d448
AJ
1441 if (!strcmp(tag, "albm") &&
1442 (t = av_metadata_get(s->metadata, "year", NULL, 0)))
1443 put_byte(pb, atoi(t->value));
c6e2c6c9
BC
1444 }
1445 return updateSize(pb, pos);
1446}
1447
2d243fb3 1448static int mov_write_udta_tag(ByteIOContext *pb, MOVMuxContext *mov,
69dde1ad
GB
1449 AVFormatContext *s)
1450{
1ee2d448
AJ
1451 ByteIOContext *pb_buf;
1452 int i, ret, size;
1453 uint8_t *buf;
89a0d790 1454
d1d87dfb
BC
1455 for (i = 0; i < s->nb_streams; i++)
1456 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1ee2d448 1457 return 0;
d1d87dfb
BC
1458 }
1459
1ee2d448
AJ
1460 ret = url_open_dyn_buf(&pb_buf);
1461 if(ret < 0)
1462 return ret;
69dde1ad 1463
a04aec6d 1464 if (mov->mode & MODE_3GP) {
1ee2d448
AJ
1465 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1466 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1467 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1468 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1469 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1470 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1471 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "year");
ce072b2d 1472 } 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
1473 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1474 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1475 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1476 mov_write_string_metadata(s, pb_buf, "\251day", "year" , 0);
d9fc9ff3 1477 mov_write_string_tag(pb_buf, "\251enc", LIBAVFORMAT_IDENT, 0, 0);
1ee2d448
AJ
1478 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1479 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1480 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
c6e2c6c9 1481 } else {
283c9a8e 1482 /* iTunes meta data */
1ee2d448 1483 mov_write_meta_tag(pb_buf, mov, s);
c6e2c6c9 1484 }
1ee2d448
AJ
1485
1486 if ((size = url_close_dyn_buf(pb_buf, &buf)) > 0) {
1487 put_be32(pb, size+8);
1488 put_tag(pb, "udta");
1489 put_buffer(pb, buf, size);
1490 av_free(buf);
89a0d790
BF
1491 }
1492
1493 return 0;
69dde1ad
GB
1494}
1495
d32d6def 1496static void mov_write_psp_udta_tag(ByteIOContext *pb,
20e22af8
BC
1497 const char *str, const char *lang, int type)
1498{
1499 int len = utf8len(str)+1;
1500 if(len<=0)
d32d6def 1501 return;
20e22af8
BC
1502 put_be16(pb, len*2+10); /* size */
1503 put_be32(pb, type); /* type */
1504 put_be16(pb, language_code(lang)); /* language */
1505 put_be16(pb, 0x01); /* ? */
1506 ascii_to_wc(pb, str);
20e22af8
BC
1507}
1508
0724373d 1509static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
dcfdb046 1510{
1ee2d448 1511 AVMetadataTag *title = av_metadata_get(s->metadata, "title", NULL, 0);
bc5c918e 1512 int64_t pos, pos2;
dcfdb046 1513
1ee2d448 1514 if (title) {
dcfdb046
BL
1515 pos = url_ftell(pb);
1516 put_be32(pb, 0); /* size placeholder*/
1517 put_tag(pb, "uuid");
1518 put_tag(pb, "USMT");
91208916
BC
1519 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1520 put_be32(pb, 0xbb88695c);
1521 put_be32(pb, 0xfac9c740);
dcfdb046 1522
d32d6def 1523 pos2 = url_ftell(pb);
dcfdb046
BL
1524 put_be32(pb, 0); /* size placeholder*/
1525 put_tag(pb, "MTDT");
7cf0e16f 1526 put_be16(pb, 4);
dcfdb046 1527
7cf0e16f
MN
1528 // ?
1529 put_be16(pb, 0x0C); /* size */
1530 put_be32(pb, 0x0B); /* type */
1531 put_be16(pb, language_code("und")); /* language */
1532 put_be16(pb, 0x0); /* ? */
1533 put_be16(pb, 0x021C); /* data */
7cf0e16f 1534
d32d6def 1535 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1ee2d448 1536 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
7cf0e16f 1537// 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
1538 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1539
1540 updateSize(pb, pos2);
1541 return updateSize(pb, pos);
dcfdb046
BL
1542 }
1543
d32d6def 1544 return 0;
dcfdb046
BL
1545}
1546
2d243fb3 1547static int mov_write_moov_tag(ByteIOContext *pb, MOVMuxContext *mov,
69dde1ad 1548 AVFormatContext *s)
1cb5f7fd 1549{
b29af723 1550 int i;
bc5c918e 1551 int64_t pos = url_ftell(pb);
1cb5f7fd
MN
1552 put_be32(pb, 0); /* size placeholder*/
1553 put_tag(pb, "moov");
1554 mov->timescale = globalTimescale;
1555
42fb4148 1556 for (i=0; i<mov->nb_streams; i++) {
e45ccf79
GB
1557 if(mov->tracks[i].entry <= 0) continue;
1558
e45ccf79
GB
1559 mov->tracks[i].time = mov->time;
1560 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
1561 }
1562
6e6d6dc0
MN
1563 mov_write_mvhd_tag(pb, mov);
1564 //mov_write_iods_tag(pb, mov);
42fb4148 1565 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1566 if(mov->tracks[i].entry > 0) {
59729451 1567 mov_write_trak_tag(pb, &(mov->tracks[i]), s->streams[i]);
1cb5f7fd
MN
1568 }
1569 }
1570
dcfdb046
BL
1571 if (mov->mode == MODE_PSP)
1572 mov_write_uuidusmt_tag(pb, s);
c6e2c6c9 1573 else
e344c1ea 1574 mov_write_udta_tag(pb, mov, s);
69dde1ad 1575
6e6d6dc0 1576 return updateSize(pb, pos);
1cb5f7fd
MN
1577}
1578
2d243fb3 1579static int mov_write_mdat_tag(ByteIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1580{
b29af723 1581 put_be32(pb, 8); // placeholder for extended size field (64 bit)
5340a0bc 1582 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
b29af723 1583
115329f1 1584 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
1585 put_be32(pb, 0); /* size placeholder*/
1586 put_tag(pb, "mdat");
1587 return 0;
1588}
1589
1590/* TODO: This needs to be more general */
7b0fb8b7 1591static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 1592{
2d243fb3 1593 MOVMuxContext *mov = s->priv_data;
bc5c918e 1594 int64_t pos = url_ftell(pb);
cd70d17f 1595 int has_h264 = 0, has_video = 0;
40f8675c 1596 int minor = 0x200;
c55cc074 1597 int i;
69dde1ad 1598
83579084
BC
1599 for (i = 0; i < s->nb_streams; i++) {
1600 AVStream *st = s->streams[i];
cd70d17f
BC
1601 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1602 has_video = 1;
83579084
BC
1603 if (st->codec->codec_id == CODEC_ID_H264)
1604 has_h264 = 1;
1605 }
1606
1560b667 1607 put_be32(pb, 0); /* size */
1cb5f7fd 1608 put_tag(pb, "ftyp");
e45ccf79 1609
40f8675c
BC
1610 if (mov->mode == MODE_3GP) {
1611 put_tag(pb, has_h264 ? "3gp6" : "3gp4");
1612 minor = has_h264 ? 0x100 : 0x200;
1613 } else if (mov->mode & MODE_3G2) {
1614 put_tag(pb, has_h264 ? "3g2b" : "3g2a");
1615 minor = has_h264 ? 0x20000 : 0x10000;
1616 }else if (mov->mode == MODE_PSP)
8af18154 1617 put_tag(pb, "MSNV");
c55cc074 1618 else if (mov->mode == MODE_MP4)
e45ccf79 1619 put_tag(pb, "isom");
cd70d17f
BC
1620 else if (mov->mode == MODE_IPOD)
1621 put_tag(pb, has_video ? "M4V ":"M4A ");
1622 else
26f86eaf 1623 put_tag(pb, "qt ");
e45ccf79 1624
40f8675c 1625 put_be32(pb, minor);
e45ccf79 1626
6ba9ed95
BC
1627 if(mov->mode == MODE_MOV)
1628 put_tag(pb, "qt ");
1629 else{
ed6e47c2 1630 put_tag(pb, "isom");
65f52683 1631 put_tag(pb, "iso2");
83579084
BC
1632 if(has_h264)
1633 put_tag(pb, "avc1");
6ba9ed95 1634 }
ed6e47c2 1635
91208916 1636 if (mov->mode == MODE_3GP)
3473ca9a 1637 put_tag(pb, has_h264 ? "3gp6":"3gp4");
a04aec6d 1638 else if (mov->mode & MODE_3G2)
3473ca9a 1639 put_tag(pb, has_h264 ? "3g2b":"3g2a");
91208916 1640 else if (mov->mode == MODE_PSP)
8af18154 1641 put_tag(pb, "MSNV");
c55cc074 1642 else if (mov->mode == MODE_MP4)
e45ccf79 1643 put_tag(pb, "mp41");
1560b667 1644 return updateSize(pb, pos);
1cb5f7fd
MN
1645}
1646
0c716ab7 1647static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
8af18154 1648{
dcfdb046
BL
1649 AVCodecContext *VideoCodec = s->streams[0]->codec;
1650 AVCodecContext *AudioCodec = s->streams[1]->codec;
1651 int AudioRate = AudioCodec->sample_rate;
1652 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
9e282ba3
MN
1653 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1654 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
8af18154 1655
91208916 1656 put_be32(pb, 0x94); /* size */
8af18154 1657 put_tag(pb, "uuid");
1658 put_tag(pb, "PROF");
1659
91208916
BC
1660 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1661 put_be32(pb, 0xbb88695c);
1662 put_be32(pb, 0xfac9c740);
8af18154 1663
91208916
BC
1664 put_be32(pb, 0x0); /* ? */
1665 put_be32(pb, 0x3); /* 3 sections ? */
8af18154 1666
91208916 1667 put_be32(pb, 0x14); /* size */
8af18154 1668 put_tag(pb, "FPRF");
91208916
BC
1669 put_be32(pb, 0x0); /* ? */
1670 put_be32(pb, 0x0); /* ? */
1671 put_be32(pb, 0x0); /* ? */
8af18154 1672
91208916 1673 put_be32(pb, 0x2c); /* size */
8af18154 1674 put_tag(pb, "APRF"); /* audio */
91208916
BC
1675 put_be32(pb, 0x0);
1676 put_be32(pb, 0x2); /* TrackID */
8af18154 1677 put_tag(pb, "mp4a");
91208916
BC
1678 put_be32(pb, 0x20f);
1679 put_be32(pb, 0x0);
9e282ba3
MN
1680 put_be32(pb, audio_kbitrate);
1681 put_be32(pb, audio_kbitrate);
91208916
BC
1682 put_be32(pb, AudioRate);
1683 put_be32(pb, AudioCodec->channels);
8af18154 1684
91208916 1685 put_be32(pb, 0x34); /* size */
8af18154 1686 put_tag(pb, "VPRF"); /* video */
91208916
BC
1687 put_be32(pb, 0x0);
1688 put_be32(pb, 0x1); /* TrackID */
87b041e0
MN
1689 if (VideoCodec->codec_id == CODEC_ID_H264) {
1690 put_tag(pb, "avc1");
91208916
BC
1691 put_be16(pb, 0x014D);
1692 put_be16(pb, 0x0015);
87b041e0
MN
1693 } else {
1694 put_tag(pb, "mp4v");
91208916
BC
1695 put_be16(pb, 0x0000);
1696 put_be16(pb, 0x0103);
87b041e0 1697 }
91208916 1698 put_be32(pb, 0x0);
9e282ba3
MN
1699 put_be32(pb, video_kbitrate);
1700 put_be32(pb, video_kbitrate);
dcfdb046
BL
1701 put_be32(pb, FrameRate);
1702 put_be32(pb, FrameRate);
1703 put_be16(pb, VideoCodec->width);
1704 put_be16(pb, VideoCodec->height);
1705 put_be32(pb, 0x010001); /* ? */
8af18154 1706}
1707
1cb5f7fd
MN
1708static int mov_write_header(AVFormatContext *s)
1709{
899681cd 1710 ByteIOContext *pb = s->pb;
2d243fb3 1711 MOVMuxContext *mov = s->priv_data;
69dde1ad 1712 int i;
1cb5f7fd 1713
899681cd 1714 if (url_is_streamed(s->pb)) {
4f60f34a
BC
1715 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1716 return -1;
1717 }
1718
1522767d
BC
1719 /* Default mode == MP4 */
1720 mov->mode = MODE_MP4;
1721
1722 if (s->oformat != NULL) {
1723 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
a04aec6d 1724 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
1522767d
BC
1725 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1726 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
aa9f4208 1727 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
1522767d 1728
26f86eaf 1729 mov_write_ftyp_tag(pb,s);
91208916
BC
1730 if (mov->mode == MODE_PSP) {
1731 if (s->nb_streams != 2) {
1522767d
BC
1732 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1733 return -1;
1734 }
1735 mov_write_uuidprof_tag(pb,s);
1736 }
1737 }
1738
5c5776e1
BC
1739 mov->tracks = av_mallocz(s->nb_streams*sizeof(*mov->tracks));
1740 if (!mov->tracks)
1741 return AVERROR(ENOMEM);
1742
ffdd57d4 1743 for(i=0; i<s->nb_streams; i++){
039627cf
BC
1744 AVStream *st= s->streams[i];
1745 MOVTrack *track= &mov->tracks[i];
1ee2d448 1746 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
039627cf
BC
1747
1748 track->enc = st->codec;
1ee2d448 1749 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2904fd39
AJ
1750 if (track->language < 0)
1751 track->language = 0;
2c5ad5fd 1752 track->mode = mov->mode;
a23c9c4a
BC
1753 track->tag = mov_find_codec_tag(s, track);
1754 if (!track->tag) {
bc4c3a55
BC
1755 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
1756 "codec not currently supported in container\n", i);
5c5776e1 1757 goto error;
a23c9c4a 1758 }
039627cf 1759 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
6a460c36
BC
1760 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
1761 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
1762 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
1763 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
1764 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
5c5776e1 1765 goto error;
6a460c36
BC
1766 }
1767 track->height = track->tag>>24 == 'n' ? 486 : 576;
a2523ce2 1768 }
ffb512e3 1769 track->timescale = st->codec->time_base.den;
c8134d71 1770 if (track->mode == MODE_MOV && track->timescale > 100000)
4ad08acc 1771 av_log(s, AV_LOG_WARNING,
911f9b71
BC
1772 "WARNING codec timebase is very high. If duration is too long,\n"
1773 "file may not be playable by quicktime. Specify a shorter timebase\n"
1774 "or choose different container.\n");
039627cf 1775 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
ffb512e3 1776 track->timescale = st->codec->sample_rate;
cf74dd4a 1777 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
b596102e 1778 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
5c5776e1 1779 goto error;
4278662d
BC
1780 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1781 track->audio_vbr = 1;
1782 }else{
cf74dd4a 1783 st->codec->frame_size = 1;
4278662d 1784 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
b596102e 1785 }
e00c3de8
BC
1786 if(track->mode != MODE_MOV &&
1787 track->enc->codec_id == CODEC_ID_MP3 && track->enc->sample_rate < 16000){
1788 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
1789 i, track->enc->sample_rate);
5c5776e1 1790 goto error;
e00c3de8 1791 }
f6204886
DC
1792 }else if(st->codec->codec_type == CODEC_TYPE_SUBTITLE){
1793 track->timescale = st->codec->time_base.den;
ffb512e3 1794 }
a2523ce2
BC
1795 if (!track->height)
1796 track->height = st->codec->height;
3a86d3f6
BC
1797
1798 av_set_pts_info(st, 64, 1, track->timescale);
f578f938
TR
1799 }
1800
18c697ac
BC
1801 mov_write_mdat_tag(pb, mov);
1802 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
42fb4148 1803 mov->nb_streams = s->nb_streams;
18c697ac 1804
1cb5f7fd
MN
1805 put_flush_packet(pb);
1806
1807 return 0;
5c5776e1
BC
1808 error:
1809 av_freep(&mov->tracks);
1810 return -1;
1cb5f7fd
MN
1811}
1812
e1316b19
BC
1813static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
1814{
1815 uint32_t c = -1;
1816 int i, closed_gop = 0;
1817
1818 for (i = 0; i < pkt->size - 4; i++) {
1819 c = (c<<8) + pkt->data[i];
1820 if (c == 0x1b8) { // gop
1821 closed_gop = pkt->data[i+4]>>6 & 0x01;
1822 } else if (c == 0x100) { // pic
1823 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1824 if (!temp_ref || closed_gop) // I picture is not reordered
1825 *flags = MOV_SYNC_SAMPLE;
1826 else
1827 *flags = MOV_PARTIAL_SYNC_SAMPLE;
1828 break;
1829 }
1830 }
1831 return 0;
1832}
1833
e928649b 1834static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd 1835{
2d243fb3 1836 MOVMuxContext *mov = s->priv_data;
899681cd 1837 ByteIOContext *pb = s->pb;
039627cf
BC
1838 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1839 AVCodecContext *enc = trk->enc;
e45ccf79 1840 unsigned int samplesInChunk = 0;
e928649b 1841 int size= pkt->size;
1cb5f7fd 1842
899681cd 1843 if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
e45ccf79 1844 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 1845
06fcf56c 1846 if (enc->codec_id == CODEC_ID_AMR_NB) {
5cb49ca1
BC
1847 /* We must find out how many AMR blocks there are in one packet */
1848 static uint16_t packed_size[16] =
1849 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1850 int len = 0;
1851
1852 while (len < size && samplesInChunk < 100) {
1853 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1854 samplesInChunk++;
1cb5f7fd 1855 }
2111e3f9 1856 if(samplesInChunk > 1){
89938d29 1857 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2111e3f9
MN
1858 return -1;
1859 }
5cb49ca1
BC
1860 } else if (trk->sampleSize)
1861 samplesInChunk = size/trk->sampleSize;
1862 else
5616f85d 1863 samplesInChunk = 1;
e45ccf79 1864
5616f85d
BC
1865 /* copy extradata if it exists */
1866 if (trk->vosLen == 0 && enc->extradata_size > 0) {
e45ccf79
GB
1867 trk->vosLen = enc->extradata_size;
1868 trk->vosData = av_malloc(trk->vosLen);
1869 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1870 }
1871
5d2160a0
BC
1872 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1873 /* from x264 or from bytestream h264 */
1874 /* nal reformating needed */
1875 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
1876 } else {
1877 put_buffer(pb, pkt->data, size);
1878 }
1879
0a63a676 1880 if ((enc->codec_id == CODEC_ID_DNXHD ||
1fbbedff 1881 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
db568c07
BC
1882 /* copy frame to create needed atoms */
1883 trk->vosLen = size;
1884 trk->vosData = av_malloc(size);
e8c4df40
BC
1885 if (!trk->vosData)
1886 return AVERROR(ENOMEM);
db568c07 1887 memcpy(trk->vosData, pkt->data, size);
c1b8e6d8
BC
1888 }
1889
0aec3c5c
BC
1890 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1891 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
e45ccf79
GB
1892 if (!trk->cluster)
1893 return -1;
e45ccf79 1894 }
e45ccf79 1895
5d2160a0 1896 trk->cluster[trk->entry].pos = url_ftell(pb) - size;
0aec3c5c
BC
1897 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1898 trk->cluster[trk->entry].size = size;
1899 trk->cluster[trk->entry].entries = samplesInChunk;
aa90239f
BC
1900 trk->cluster[trk->entry].dts = pkt->dts;
1901 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1902
96f69e0d
BC
1903 if (pkt->pts == AV_NOPTS_VALUE) {
1904 av_log(s, AV_LOG_WARNING, "pts has no value\n");
1905 pkt->pts = pkt->dts;
1906 }
c4f078ff 1907 if (pkt->dts != pkt->pts)
e1316b19 1908 trk->flags |= MOV_TRACK_CTTS;
c4f078ff 1909 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
e1316b19 1910 trk->cluster[trk->entry].flags = 0;
ab4752e3 1911 if (pkt->flags & PKT_FLAG_KEY) {
e1316b19
BC
1912 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO) {
1913 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
1914 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
1915 trk->flags |= MOV_TRACK_STPS;
1916 } else {
1917 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
1918 }
1919 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
1920 trk->hasKeyframes++;
ab4752e3 1921 }
e45ccf79
GB
1922 trk->entry++;
1923 trk->sampleCount += samplesInChunk;
039627cf 1924 mov->mdat_size += size;
e45ccf79 1925
1cb5f7fd
MN
1926 put_flush_packet(pb);
1927 return 0;
1928}
1929
1930static int mov_write_trailer(AVFormatContext *s)
1931{
2d243fb3 1932 MOVMuxContext *mov = s->priv_data;
899681cd 1933 ByteIOContext *pb = s->pb;
1cb5f7fd 1934 int res = 0;
0aec3c5c 1935 int i;
1cb5f7fd 1936
bc5c918e 1937 int64_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1938
1939 /* Write size of mdat tag */
039627cf 1940 if (mov->mdat_size+8 <= UINT32_MAX) {
b29af723 1941 url_fseek(pb, mov->mdat_pos, SEEK_SET);
039627cf 1942 put_be32(pb, mov->mdat_size+8);
b29af723
MN
1943 } else {
1944 /* overwrite 'wide' placeholder atom */
1945 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1946 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1947 put_tag(pb, "mdat");
039627cf 1948 put_be64(pb, mov->mdat_size+16);
b29af723 1949 }
69dde1ad 1950 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1951
69dde1ad 1952 mov_write_moov_tag(pb, mov, s);
1cb5f7fd 1953
42fb4148 1954 for (i=0; i<mov->nb_streams; i++) {
0aec3c5c
BC
1955 av_freep(&mov->tracks[i].cluster);
1956
91208916 1957 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
ec7d0d2e 1958
1cb5f7fd 1959 }
69dde1ad 1960
1cb5f7fd
MN
1961 put_flush_packet(pb);
1962
5c5776e1
BC
1963 av_freep(&mov->tracks);
1964
1cb5f7fd
MN
1965 return res;
1966}
1967
b250f9c6 1968#if CONFIG_MOV_MUXER
ff70e601 1969AVOutputFormat mov_muxer = {
1cb5f7fd 1970 "mov",
bde15e74 1971 NULL_IF_CONFIG_SMALL("MOV format"),
1cb5f7fd
MN
1972 NULL,
1973 "mov",
2d243fb3 1974 sizeof(MOVMuxContext),
69dde1ad 1975 CODEC_ID_AAC,
2187d948 1976 CODEC_ID_MPEG4,
1cb5f7fd
MN
1977 mov_write_header,
1978 mov_write_packet,
1979 mov_write_trailer,
25918212 1980 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
c1854592 1981 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
1cb5f7fd 1982};
ff70e601 1983#endif
b250f9c6 1984#if CONFIG_TGP_MUXER
ff70e601 1985AVOutputFormat tgp_muxer = {
1cb5f7fd 1986 "3gp",
bde15e74 1987 NULL_IF_CONFIG_SMALL("3GP format"),
1cb5f7fd
MN
1988 NULL,
1989 "3gp",
2d243fb3 1990 sizeof(MOVMuxContext),
1cb5f7fd
MN
1991 CODEC_ID_AMR_NB,
1992 CODEC_ID_H263,
1993 mov_write_header,
1994 mov_write_packet,
1995 mov_write_trailer,
c64d476c 1996 .flags = AVFMT_GLOBALHEADER,
c1854592 1997 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
1cb5f7fd 1998};
ff70e601 1999#endif
b250f9c6 2000#if CONFIG_MP4_MUXER
ff70e601 2001AVOutputFormat mp4_muxer = {
1cb5f7fd 2002 "mp4",
bde15e74 2003 NULL_IF_CONFIG_SMALL("MP4 format"),
4cb3f3b6 2004 "application/mp4",
ccec1b69 2005 "mp4",
2d243fb3 2006 sizeof(MOVMuxContext),
1cb5f7fd
MN
2007 CODEC_ID_AAC,
2008 CODEC_ID_MPEG4,
2009 mov_write_header,
2010 mov_write_packet,
2011 mov_write_trailer,
25918212 2012 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
c1854592 2013 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
1cb5f7fd 2014};
ff70e601 2015#endif
b250f9c6 2016#if CONFIG_PSP_MUXER
ff70e601 2017AVOutputFormat psp_muxer = {
8af18154 2018 "psp",
bde15e74 2019 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
8af18154 2020 NULL,
2021 "mp4,psp",
2d243fb3 2022 sizeof(MOVMuxContext),
8af18154 2023 CODEC_ID_AAC,
2024 CODEC_ID_MPEG4,
2025 mov_write_header,
2026 mov_write_packet,
2027 mov_write_trailer,
dcfdb046 2028 .flags = AVFMT_GLOBALHEADER,
c1854592 2029 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
8af18154 2030};
ff70e601 2031#endif
b250f9c6 2032#if CONFIG_TG2_MUXER
ff70e601 2033AVOutputFormat tg2_muxer = {
8536ab89 2034 "3g2",
bde15e74 2035 NULL_IF_CONFIG_SMALL("3GP2 format"),
8536ab89 2036 NULL,
2037 "3g2",
2d243fb3 2038 sizeof(MOVMuxContext),
8536ab89 2039 CODEC_ID_AMR_NB,
2040 CODEC_ID_H263,
2041 mov_write_header,
2042 mov_write_packet,
2043 mov_write_trailer,
c64d476c 2044 .flags = AVFMT_GLOBALHEADER,
c1854592 2045 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
8536ab89 2046};
ff70e601 2047#endif
b250f9c6 2048#if CONFIG_IPOD_MUXER
aa9f4208
BC
2049AVOutputFormat ipod_muxer = {
2050 "ipod",
bde15e74 2051 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
aa9f4208 2052 "application/mp4",
ccec1b69 2053 "m4v,m4a",
2d243fb3 2054 sizeof(MOVMuxContext),
aa9f4208
BC
2055 CODEC_ID_AAC,
2056 CODEC_ID_H264,
2057 mov_write_header,
2058 mov_write_packet,
2059 mov_write_trailer,
2060 .flags = AVFMT_GLOBALHEADER,
d1df4da0 2061 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
aa9f4208
BC
2062};
2063#endif