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