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