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