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