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