Ignore the spec, and calculate aspect ratio on width/height.
[libav.git] / libavformat / movenc.c
CommitLineData
1cb5f7fd 1/*
7fbde343 2 * MOV, 3GP, MP4 muxer
1cb5f7fd 3 * Copyright (c) 2003 Thomas Raivio.
69dde1ad 4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
1cb5f7fd 5 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
1cb5f7fd
MN
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
1cb5f7fd 12 *
b78e7197 13 * FFmpeg is distributed in the hope that it will be useful,
1cb5f7fd
MN
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
b78e7197 19 * License along with FFmpeg; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1cb5f7fd
MN
21 */
22#include "avformat.h"
9d9f4119 23#include "riff.h"
1cb5f7fd 24#include "avio.h"
e40ee6a2 25#include "isom.h"
1bd2d763 26#include "avc.h"
1cb5f7fd 27
6e6d6dc0
MN
28#undef NDEBUG
29#include <assert.h>
30
1cb5f7fd
MN
31#define MOV_INDEX_CLUSTER_SIZE 16384
32#define globalTimescale 1000
33
2c77994f
MN
34#define MODE_MP4 0x01
35#define MODE_MOV 0x02
36#define MODE_3GP 0x04
37#define MODE_PSP 0x08 // example working PSP command line:
8af18154 38// ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
2c77994f
MN
39#define MODE_3G2 0x10
40#define MODE_IPOD 0x20
69dde1ad 41
1cb5f7fd 42typedef struct MOVIentry {
b29af723
MN
43 unsigned int flags, size;
44 uint64_t pos;
e45ccf79 45 unsigned int samplesInChunk;
f578f938 46 char key_frame;
1cb5f7fd 47 unsigned int entries;
b4712e3c 48 int64_t cts;
aa90239f 49 int64_t dts;
1cb5f7fd
MN
50} MOVIentry;
51
52typedef struct MOVIndex {
69dde1ad 53 int mode;
1cb5f7fd 54 int entry;
1cb5f7fd
MN
55 long timescale;
56 long time;
a3a80ddc 57 int64_t trackDuration;
e45ccf79 58 long sampleCount;
5cb49ca1 59 long sampleSize;
f578f938 60 int hasKeyframes;
b4712e3c 61 int hasBframes;
ab561df9 62 int language;
1cb5f7fd 63 int trackID;
2c2f58c0 64 int tag; ///< stsd fourcc
1cb5f7fd
MN
65 AVCodecContext *enc;
66
67 int vosLen;
6e6d6dc0 68 uint8_t *vosData;
0aec3c5c 69 MOVIentry *cluster;
aa90239f 70 int audio_vbr;
1cb5f7fd
MN
71} MOVTrack;
72
8af18154 73typedef struct MOVContext {
69dde1ad 74 int mode;
a3a80ddc 75 int64_t time;
1cb5f7fd 76 int nb_streams;
f578f938 77 offset_t mdat_pos;
039627cf 78 uint64_t mdat_size;
1cb5f7fd
MN
79 long timescale;
80 MOVTrack tracks[MAX_STREAMS];
81} MOVContext;
82
90b5b51e 83//FIXME support 64 bit variant with wide placeholders
0724373d 84static offset_t updateSize(ByteIOContext *pb, offset_t pos)
1cb5f7fd 85{
b29af723 86 offset_t curpos = url_ftell(pb);
1cb5f7fd 87 url_fseek(pb, pos, SEEK_SET);
6e6d6dc0 88 put_be32(pb, curpos - pos); /* rewrite size */
1cb5f7fd 89 url_fseek(pb, curpos, SEEK_SET);
6e6d6dc0
MN
90
91 return curpos - pos;
1cb5f7fd
MN
92}
93
e45ccf79 94/* Chunk offset atom */
0724373d 95static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd
MN
96{
97 int i;
ed06cf6d 98 int mode64 = 0; // use 32 bit size variant if possible
b29af723 99 offset_t pos = url_ftell(pb);
f578f938 100 put_be32(pb, 0); /* size */
b29af723
MN
101 if (pos > UINT32_MAX) {
102 mode64 = 1;
103 put_tag(pb, "co64");
104 } else
105 put_tag(pb, "stco");
1cb5f7fd
MN
106 put_be32(pb, 0); /* version & flags */
107 put_be32(pb, track->entry); /* entry count */
108 for (i=0; i<track->entry; i++) {
b29af723 109 if(mode64 == 1)
0aec3c5c 110 put_be64(pb, track->cluster[i].pos);
b29af723 111 else
0aec3c5c 112 put_be32(pb, track->cluster[i].pos);
1cb5f7fd 113 }
f7635edb 114 return updateSize(pb, pos);
1cb5f7fd
MN
115}
116
e45ccf79 117/* Sample size atom */
0724373d 118static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 119{
f578f938 120 int equalChunks = 1;
e45ccf79 121 int i, j, entries = 0, tst = -1, oldtst = -1;
1cb5f7fd 122
b29af723 123 offset_t pos = url_ftell(pb);
f578f938 124 put_be32(pb, 0); /* size */
1cb5f7fd
MN
125 put_tag(pb, "stsz");
126 put_be32(pb, 0); /* version & flags */
127
f578f938 128 for (i=0; i<track->entry; i++) {
0aec3c5c 129 tst = track->cluster[i].size/track->cluster[i].entries;
e45ccf79
GB
130 if(oldtst != -1 && tst != oldtst) {
131 equalChunks = 0;
f578f938
TR
132 }
133 oldtst = tst;
0aec3c5c 134 entries += track->cluster[i].entries;
f578f938 135 }
e45ccf79 136 if (equalChunks) {
0aec3c5c 137 int sSize = track->cluster[0].size/track->cluster[0].entries;
115329f1 138 put_be32(pb, sSize); // sample size
e45ccf79 139 put_be32(pb, entries); // sample count
1cb5f7fd 140 }
f578f938 141 else {
115329f1
DB
142 put_be32(pb, 0); // sample size
143 put_be32(pb, entries); // sample count
f578f938 144 for (i=0; i<track->entry; i++) {
91208916 145 for (j=0; j<track->cluster[i].entries; j++) {
0aec3c5c
BC
146 put_be32(pb, track->cluster[i].size /
147 track->cluster[i].entries);
e45ccf79 148 }
1cb5f7fd
MN
149 }
150 }
f7635edb 151 return updateSize(pb, pos);
1cb5f7fd
MN
152}
153
e45ccf79 154/* Sample to chunk atom */
0724373d 155static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 156{
b29af723
MN
157 int index = 0, oldval = -1, i;
158 offset_t entryPos, curpos;
f578f938 159
b29af723 160 offset_t pos = url_ftell(pb);
f578f938 161 put_be32(pb, 0); /* size */
1cb5f7fd 162 put_tag(pb, "stsc");
115329f1 163 put_be32(pb, 0); // version & flags
f578f938 164 entryPos = url_ftell(pb);
115329f1 165 put_be32(pb, track->entry); // entry count
f578f938 166 for (i=0; i<track->entry; i++) {
0aec3c5c 167 if(oldval != track->cluster[i].samplesInChunk)
f578f938 168 {
115329f1 169 put_be32(pb, i+1); // first chunk
0aec3c5c 170 put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
115329f1 171 put_be32(pb, 0x1); // sample description index
0aec3c5c 172 oldval = track->cluster[i].samplesInChunk;
f578f938 173 index++;
1cb5f7fd
MN
174 }
175 }
f578f938
TR
176 curpos = url_ftell(pb);
177 url_fseek(pb, entryPos, SEEK_SET);
115329f1 178 put_be32(pb, index); // rewrite size
f578f938 179 url_fseek(pb, curpos, SEEK_SET);
1cb5f7fd 180
f7635edb 181 return updateSize(pb, pos);
1cb5f7fd
MN
182}
183
e45ccf79 184/* Sync sample atom */
0724373d 185static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 186{
b29af723
MN
187 offset_t curpos, entryPos;
188 int i, index = 0;
189 offset_t pos = url_ftell(pb);
115329f1 190 put_be32(pb, 0); // size
1cb5f7fd 191 put_tag(pb, "stss");
115329f1 192 put_be32(pb, 0); // version & flags
f578f938 193 entryPos = url_ftell(pb);
115329f1 194 put_be32(pb, track->entry); // entry count
f578f938 195 for (i=0; i<track->entry; i++) {
0aec3c5c 196 if(track->cluster[i].key_frame == 1) {
f578f938
TR
197 put_be32(pb, i+1);
198 index++;
199 }
200 }
201 curpos = url_ftell(pb);
202 url_fseek(pb, entryPos, SEEK_SET);
115329f1 203 put_be32(pb, index); // rewrite size
f578f938 204 url_fseek(pb, curpos, SEEK_SET);
f7635edb 205 return updateSize(pb, pos);
1cb5f7fd
MN
206}
207
b7d9da10 208static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd
MN
209{
210 put_be32(pb, 0x11); /* size */
b7d9da10
BC
211 if (track->mode == MODE_MOV) put_tag(pb, "samr");
212 else put_tag(pb, "damr");
1cb5f7fd 213 put_tag(pb, "FFMP");
b7d9da10 214 put_byte(pb, 0); /* decoder version */
3a72cbd9 215
875f9287 216 put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
b7d9da10
BC
217 put_byte(pb, 0x00); /* Mode change period (no restriction) */
218 put_byte(pb, 0x01); /* Frames per sample */
3a72cbd9
BC
219 return 0x11;
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 */
226static int mov_write_extradata_tag(ByteIOContext *pb, MOVTrack *track)
227{
228 put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
229 return track->enc->extradata_size;
230}
231
fcef991a
BC
232static int mov_write_enda_tag(ByteIOContext *pb)
233{
234 put_be32(pb, 10);
235 put_tag(pb, "enda");
236 put_be16(pb, 1); /* little endian */
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
08db8f18 247static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
039627cf 248{
08db8f18
MN
249 int i= descrLength(size) - size - 2;
250 put_byte(pb, tag);
251 for(; i>0; i--)
252 put_byte(pb, (size>>(7*i)) | 0x80);
253 put_byte(pb, size & 0x7F);
039627cf
BC
254}
255
0724373d 256static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack *track) // Basic
039627cf 257{
039627cf 258 offset_t pos = url_ftell(pb);
fcef991a 259 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
039627cf 260
287d6cfa 261 put_be32(pb, 0); // size
039627cf 262 put_tag(pb, "esds");
287d6cfa 263 put_be32(pb, 0); // Version
039627cf
BC
264
265 // ES descriptor
266 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
267 descrLength(1));
268 put_be16(pb, track->trackID);
287d6cfa 269 put_byte(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) &&
277 track->enc->sample_rate <= 24000 &&
278 track->enc->sample_rate >= 16000)
279 put_byte(pb, 105); // 13818-3
280 else
281 put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
039627cf
BC
282
283 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
284 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
285 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
287d6cfa 286 put_byte(pb, 0x15); // flags (= Audiostream)
039627cf 287 else
287d6cfa 288 put_byte(pb, 0x11); // flags (= Visualstream)
039627cf 289
287d6cfa
BC
290 put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
291 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
039627cf 292
287d6cfa 293 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 294 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
287d6cfa 295 put_be32(pb, 0); // vbr
039627cf 296 else
287d6cfa 297 put_be32(pb, track->enc->rc_max_rate); // avg bitrate
039627cf 298
287d6cfa 299 if (track->vosLen) {
039627cf
BC
300 // DecoderSpecific info descriptor
301 putDescr(pb, 0x05, track->vosLen);
302 put_buffer(pb, track->vosData, track->vosLen);
303 }
304
039627cf
BC
305 // SL descriptor
306 putDescr(pb, 0x06, 1);
307 put_byte(pb, 0x02);
f7635edb 308 return updateSize(pb, pos);
039627cf
BC
309}
310
0724373d 311static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack *track)
69dde1ad 312{
b29af723 313 offset_t pos = url_ftell(pb);
69dde1ad
GB
314
315 put_be32(pb, 0); /* size */
316 put_tag(pb, "wave");
317
318 put_be32(pb, 12); /* size */
319 put_tag(pb, "frma");
fcef991a 320 put_le32(pb, track->tag);
69dde1ad 321
fcef991a 322 if (track->enc->codec_id == CODEC_ID_AAC) {
aa1c1c61
BC
323 /* useless atom needed by mplayer, ipod, not needed by quicktime */
324 put_be32(pb, 12); /* size */
325 put_tag(pb, "mp4a");
326 put_be32(pb, 0);
fcef991a
BC
327 mov_write_esds_tag(pb, track);
328 } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
329 track->enc->codec_id == CODEC_ID_PCM_S32LE) {
330 mov_write_enda_tag(pb);
3a72cbd9 331 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
b7d9da10 332 mov_write_amr_tag(pb, track);
463e7afd
BC
333 } else if (track->enc->codec_id == CODEC_ID_ALAC) {
334 mov_write_extradata_tag(pb, track);
fcef991a 335 }
69dde1ad
GB
336
337 put_be32(pb, 8); /* size */
338 put_be32(pb, 0); /* null tag */
339
f7635edb 340 return updateSize(pb, pos);
69dde1ad
GB
341}
342
0724373d 343static int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack *track)
79e42311
BC
344{
345 put_be32(pb, track->vosLen+8);
346 put_tag(pb, "glbl");
347 put_buffer(pb, track->vosData, track->vosLen);
348 return 8+track->vosLen;
349}
350
0724373d 351static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 352{
b29af723 353 offset_t pos = url_ftell(pb);
f45b744c
BC
354 int version = track->mode == MODE_MOV &&
355 (track->audio_vbr ||
356 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
357 track->enc->codec_id == CODEC_ID_PCM_S24LE);
115329f1 358
1cb5f7fd 359 put_be32(pb, 0); /* size */
039627cf 360 put_le32(pb, track->tag); // store it byteswapped
1cb5f7fd
MN
361 put_be32(pb, 0); /* Reserved */
362 put_be16(pb, 0); /* Reserved */
363 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
69dde1ad 364
e45ccf79 365 /* SoundDescription */
fcef991a 366 put_be16(pb, version); /* Version */
e45ccf79 367 put_be16(pb, 0); /* Revision level */
1cb5f7fd
MN
368 put_be32(pb, 0); /* Reserved */
369
2260499c
BC
370 if (track->mode == MODE_MOV) {
371 put_be16(pb, track->enc->channels);
10fc840c
BC
372 if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
373 track->enc->codec_id == CODEC_ID_PCM_S8)
374 put_be16(pb, 8); /* bits per sample */
375 else
376 put_be16(pb, 16);
2260499c
BC
377 put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
378 } else { /* reserved for mp4/3gp */
379 put_be16(pb, 2);
380 put_be16(pb, 16);
381 put_be16(pb, 0);
382 }
ef19c7eb 383
9a4d9388 384 put_be16(pb, 0); /* packet size (= 0) */
1cb5f7fd
MN
385 put_be16(pb, track->timescale); /* Time scale */
386 put_be16(pb, 0); /* Reserved */
387
5cb49ca1 388 if(version == 1) { /* SoundDescription V1 extended info */
5cb49ca1 389 put_be32(pb, track->enc->frame_size); /* Samples per packet */
9a39cd35 390 put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
5cb49ca1 391 put_be32(pb, track->sampleSize); /* Bytes per frame */
aa1c1c61 392 put_be32(pb, 2); /* Bytes per sample */
69dde1ad
GB
393 }
394
6dd19fff
BC
395 if(track->mode == MODE_MOV &&
396 (track->enc->codec_id == CODEC_ID_AAC ||
397 track->enc->codec_id == CODEC_ID_AMR_NB ||
398 track->enc->codec_id == CODEC_ID_PCM_S24LE ||
463e7afd
BC
399 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
400 track->enc->codec_id == CODEC_ID_ALAC))
fcef991a 401 mov_write_wave_tag(pb, track);
d0c0a29b 402 else if(track->tag == MKTAG('m','p','4','a'))
3a72cbd9
BC
403 mov_write_esds_tag(pb, track);
404 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
b7d9da10 405 mov_write_amr_tag(pb, track);
709c9f8d
BC
406 else if (track->enc->codec_id == CODEC_ID_ALAC)
407 mov_write_extradata_tag(pb, track);
79e42311
BC
408 else if(track->vosLen > 0)
409 mov_write_glbl_tag(pb, track);
3a72cbd9 410
f7635edb 411 return updateSize(pb, pos);
1cb5f7fd
MN
412}
413
6e6d6dc0 414static int mov_write_d263_tag(ByteIOContext *pb)
1cb5f7fd
MN
415{
416 put_be32(pb, 0xf); /* size */
417 put_tag(pb, "d263");
418 put_tag(pb, "FFMP");
87494ea0
BC
419 put_byte(pb, 0); /* decoder version */
420 /* FIXME use AVCodecContext level/profile, when encoder will set values */
421 put_byte(pb, 0xa); /* level */
422 put_byte(pb, 0); /* profile */
1cb5f7fd
MN
423 return 0xf;
424}
425
f578f938
TR
426/* TODO: No idea about these values */
427static int mov_write_svq3_tag(ByteIOContext *pb)
1cb5f7fd 428{
f578f938
TR
429 put_be32(pb, 0x15);
430 put_tag(pb, "SMI ");
431 put_tag(pb, "SEQH");
432 put_be32(pb, 0x5);
433 put_be32(pb, 0xe2c0211d);
434 put_be32(pb, 0xc0000000);
115329f1 435 put_byte(pb, 0);
f578f938 436 return 0x15;
1cb5f7fd
MN
437}
438
6e6eebf9
AJ
439static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
440{
441 offset_t pos = url_ftell(pb);
442
443 put_be32(pb, 0);
444 put_tag(pb, "avcC");
9ab3f71b 445 ff_isom_write_avcc(pb, track->vosData, track->vosLen);
c1b8e6d8
BC
446 return updateSize(pb, pos);
447}
448
fc4cbc16
BC
449/* also used by all avid codecs (dv, imx, meridien) and their variants */
450static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
451{
452 int i;
453 put_be32(pb, 24); /* size */
454 put_tag(pb, "ACLR");
455 put_tag(pb, "ACLR");
456 put_tag(pb, "0001");
457 put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
458 put_be32(pb, 0); /* unknown */
459
460 put_be32(pb, 24); /* size */
461 put_tag(pb, "APRG");
462 put_tag(pb, "APRG");
463 put_tag(pb, "0001");
464 put_be32(pb, 1); /* unknown */
465 put_be32(pb, 0); /* unknown */
466
467 put_be32(pb, 120); /* size */
468 put_tag(pb, "ARES");
469 put_tag(pb, "ARES");
470 put_tag(pb, "0001");
471 put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
472 put_be32(pb, track->enc->width);
473 /* values below are based on samples created with quicktime and avid codecs */
474 if (track->vosData[5] & 2) { // interlaced
475 put_be32(pb, track->enc->height/2);
476 put_be32(pb, 2); /* unknown */
477 put_be32(pb, 0); /* unknown */
478 put_be32(pb, 4); /* unknown */
479 } else {
480 put_be32(pb, track->enc->height);
481 put_be32(pb, 1); /* unknown */
482 put_be32(pb, 0); /* unknown */
44fb8ebb
BC
483 if (track->enc->height == 1080)
484 put_be32(pb, 5); /* unknown */
485 else
486 put_be32(pb, 6); /* unknown */
fc4cbc16
BC
487 }
488 /* padding */
489 for (i = 0; i < 10; i++)
490 put_be64(pb, 0);
491
492 /* extra padding for stsd needed */
493 put_be32(pb, 0);
494 return 0;
495}
496
a23c9c4a
BC
497static const AVCodecTag codec_3gp_tags[] = {
498 { CODEC_ID_H263, MKTAG('s','2','6','3') },
499 { CODEC_ID_H264, MKTAG('a','v','c','1') },
500 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
501 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
502 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
503 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
504};
505
ccac8438
BC
506static const AVCodecTag mov_pix_fmt_tags[] = {
507 { PIX_FMT_YUYV422, MKTAG('y','u','v','s') },
fa7343ea 508 { PIX_FMT_UYVY422, MKTAG('2','v','u','y') },
ccac8438
BC
509 { PIX_FMT_BGR555, MKTAG('r','a','w',' ') },
510 { PIX_FMT_RGB24, MKTAG('r','a','w',' ') },
511 { PIX_FMT_BGR32_1, MKTAG('r','a','w',' ') },
512};
513
709c9f8d
BC
514static const AVCodecTag codec_ipod_tags[] = {
515 { CODEC_ID_H264, MKTAG('a','v','c','1') },
516 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
517 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
518 { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
519};
520
a23c9c4a 521static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
8e321619 522{
039627cf 523 int tag = track->enc->codec_tag;
709c9f8d 524 if (track->mode == MODE_MP4 || track->mode == MODE_PSP) {
a23c9c4a
BC
525 if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
526 return 0;
527 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
528 else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
529 else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
709c9f8d
BC
530 } else if (track->mode == MODE_IPOD) {
531 tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
532 if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
533 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
534 "Quicktime/Ipod might not play the file\n");
a04aec6d 535 } else if (track->mode & MODE_3GP) {
a23c9c4a 536 tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
bbe05246 537 } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
ccac8438
BC
538 (tag == MKTAG('d','v','c','p') ||
539 track->enc->codec_id == CODEC_ID_RAWVIDEO))) {
8e321619 540 if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
506e4bb8
BC
541 if (track->enc->height == 480) /* NTSC */
542 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
543 else tag = MKTAG('d','v','c',' ');
544 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
545 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
546 else tag = MKTAG('d','v','p','p');
ccac8438
BC
547 } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
548 tag = codec_get_tag(mov_pix_fmt_tags, track->enc->pix_fmt);
549 if (!tag) // restore tag
550 tag = track->enc->codec_tag;
8e321619 551 } else {
a23c9c4a 552 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
d97f2144 553 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
0b98a7b3 554 if (!tag) { // if no mac fcc found, try with Microsoft tags
d97f2144 555 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
0b98a7b3
BC
556 if (tag)
557 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
558 "the file may be unplayable!\n");
d97f2144 559 }
a23c9c4a 560 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
d97f2144 561 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
0b98a7b3 562 if (!tag) { // if no mac fcc found, try with Microsoft tags
d97f2144
BC
563 int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
564 if (ms_tag) {
565 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
0b98a7b3
BC
566 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
567 "the file may be unplayable!\n");
d97f2144
BC
568 }
569 }
a23c9c4a
BC
570 }
571 }
572 }
8e321619
BC
573 return tag;
574}
575
aa9f4208
BC
576/** Write uuid atom.
577 * Needed to make file play in iPods running newest firmware
578 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
579 */
580static int mov_write_uuid_tag_ipod(ByteIOContext *pb)
581{
582 put_be32(pb, 28);
583 put_tag(pb, "uuid");
584 put_be32(pb, 0x6b6840f2);
585 put_be32(pb, 0x5f244fc5);
586 put_be32(pb, 0xba39a51b);
587 put_be32(pb, 0xcf0323f3);
588 put_be32(pb, 0x0);
589 return 28;
590}
591
0724373d 592static int mov_write_video_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 593{
b29af723 594 offset_t pos = url_ftell(pb);
53ffdd14 595 char compressor_name[32];
8cc7a34d 596
f578f938 597 put_be32(pb, 0); /* size */
039627cf 598 put_le32(pb, track->tag); // store it byteswapped
f578f938
TR
599 put_be32(pb, 0); /* Reserved */
600 put_be16(pb, 0); /* Reserved */
601 put_be16(pb, 1); /* Data-reference index */
602
53ffdd14
RG
603 put_be16(pb, 0); /* Codec stream version */
604 put_be16(pb, 0); /* Codec stream revision (=0) */
5997ed78 605 if (track->mode == MODE_MOV) {
915282e5
BC
606 put_tag(pb, "FFMP"); /* Vendor */
607 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
608 put_be32(pb, 0); /* Temporal Quality */
609 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
610 } else {
611 put_be32(pb, 0x200); /* Temporal Quality = normal */
612 put_be32(pb, 0x200); /* Spatial Quality = normal */
613 }
5997ed78
BC
614 } else {
615 put_be32(pb, 0); /* Reserved */
616 put_be32(pb, 0); /* Reserved */
617 put_be32(pb, 0); /* Reserved */
618 }
f578f938
TR
619 put_be16(pb, track->enc->width); /* Video width */
620 put_be16(pb, track->enc->height); /* Video height */
53ffdd14
RG
621 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
622 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
9a4d9388
RS
623 put_be32(pb, 0); /* Data size (= 0) */
624 put_be16(pb, 1); /* Frame count (= 1) */
115329f1 625
53ffdd14 626 memset(compressor_name,0,32);
5997ed78
BC
627 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
628 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
53ffdd14 629 strncpy(compressor_name,track->enc->codec->name,31);
25cf9062 630 put_byte(pb, strlen(compressor_name));
53ffdd14 631 put_buffer(pb, compressor_name, 31);
115329f1 632
ccac8438
BC
633 if (track->mode == MODE_MOV && track->enc->bits_per_sample)
634 put_be16(pb, track->enc->bits_per_sample);
635 else
636 put_be16(pb, 0x18); /* Reserved */
f578f938 637 put_be16(pb, 0xffff); /* Reserved */
d0c0a29b 638 if(track->tag == MKTAG('m','p','4','v'))
f578f938
TR
639 mov_write_esds_tag(pb, track);
640 else if(track->enc->codec_id == CODEC_ID_H263)
641 mov_write_d263_tag(pb);
642 else if(track->enc->codec_id == CODEC_ID_SVQ3)
115329f1 643 mov_write_svq3_tag(pb);
7b0f4c9a
BC
644 else if(track->enc->codec_id == CODEC_ID_DNXHD)
645 mov_write_avid_tag(pb, track);
aa9f4208 646 else if(track->enc->codec_id == CODEC_ID_H264) {
c1b8e6d8 647 mov_write_avcc_tag(pb, track);
aa9f4208
BC
648 if(track->mode == MODE_IPOD)
649 mov_write_uuid_tag_ipod(pb);
b64518e9 650 } else if(track->vosLen > 0)
79e42311 651 mov_write_glbl_tag(pb, track);
f578f938 652
f7635edb 653 return updateSize(pb, pos);
1cb5f7fd
MN
654}
655
0724373d 656static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 657{
b29af723 658 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
659 put_be32(pb, 0); /* size */
660 put_tag(pb, "stsd");
661 put_be32(pb, 0); /* version & flags */
662 put_be32(pb, 1); /* entry count */
f578f938
TR
663 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
664 mov_write_video_tag(pb, track);
665 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
666 mov_write_audio_tag(pb, track);
6e6d6dc0 667 return updateSize(pb, pos);
1cb5f7fd
MN
668}
669
0724373d 670static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)
b4712e3c 671{
e23848a4 672 MOV_stts_t *ctts_entries;
b4712e3c
BC
673 uint32_t entries = 0;
674 uint32_t atom_size;
675 int i;
676
677 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
678 ctts_entries[0].count = 1;
0aec3c5c 679 ctts_entries[0].duration = track->cluster[0].cts;
b4712e3c 680 for (i=1; i<track->entry; i++) {
0aec3c5c 681 if (track->cluster[i].cts == ctts_entries[entries].duration) {
b4712e3c
BC
682 ctts_entries[entries].count++; /* compress */
683 } else {
684 entries++;
0aec3c5c 685 ctts_entries[entries].duration = track->cluster[i].cts;
b4712e3c
BC
686 ctts_entries[entries].count = 1;
687 }
688 }
689 entries++; /* last one */
690 atom_size = 16 + (entries * 8);
691 put_be32(pb, atom_size); /* size */
692 put_tag(pb, "ctts");
693 put_be32(pb, 0); /* version & flags */
694 put_be32(pb, entries); /* entry count */
695 for (i=0; i<entries; i++) {
696 put_be32(pb, ctts_entries[i].count);
697 put_be32(pb, ctts_entries[i].duration);
698 }
699 av_free(ctts_entries);
700 return atom_size;
701}
702
e45ccf79 703/* Time to sample atom */
0724373d 704static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 705{
e23848a4 706 MOV_stts_t *stts_entries;
aa90239f
BC
707 uint32_t entries = -1;
708 uint32_t atom_size;
709 int i;
710
711 if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
712 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
713 stts_entries[0].count = track->sampleCount;
714 stts_entries[0].duration = 1;
715 entries = 1;
716 } else {
717 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
718 for (i=0; i<track->entry; i++) {
719 int64_t duration = i + 1 == track->entry ?
720 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
721 track->cluster[i+1].dts - track->cluster[i].dts;
722 if (i && duration == stts_entries[entries].duration) {
723 stts_entries[entries].count++; /* compress */
724 } else {
725 entries++;
726 stts_entries[entries].duration = duration;
727 stts_entries[entries].count = 1;
728 }
729 }
730 entries++; /* last one */
731 }
732 atom_size = 16 + (entries * 8);
733 put_be32(pb, atom_size); /* size */
1cb5f7fd
MN
734 put_tag(pb, "stts");
735 put_be32(pb, 0); /* version & flags */
aa90239f
BC
736 put_be32(pb, entries); /* entry count */
737 for (i=0; i<entries; i++) {
738 put_be32(pb, stts_entries[i].count);
739 put_be32(pb, stts_entries[i].duration);
740 }
741 av_free(stts_entries);
742 return atom_size;
1cb5f7fd
MN
743}
744
6e6d6dc0 745static int mov_write_dref_tag(ByteIOContext *pb)
1cb5f7fd
MN
746{
747 put_be32(pb, 28); /* size */
748 put_tag(pb, "dref");
749 put_be32(pb, 0); /* version & flags */
750 put_be32(pb, 1); /* entry count */
751
752 put_be32(pb, 0xc); /* size */
753 put_tag(pb, "url ");
754 put_be32(pb, 1); /* version & flags */
755
756 return 28;
757}
758
0724373d 759static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 760{
b29af723 761 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
762 put_be32(pb, 0); /* size */
763 put_tag(pb, "stbl");
6e6d6dc0
MN
764 mov_write_stsd_tag(pb, track);
765 mov_write_stts_tag(pb, track);
f578f938 766 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
b371539a 767 track->hasKeyframes && track->hasKeyframes < track->entry)
f578f938 768 mov_write_stss_tag(pb, track);
b4712e3c
BC
769 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
770 track->hasBframes)
771 mov_write_ctts_tag(pb, track);
6e6d6dc0
MN
772 mov_write_stsc_tag(pb, track);
773 mov_write_stsz_tag(pb, track);
774 mov_write_stco_tag(pb, track);
775 return updateSize(pb, pos);
1cb5f7fd
MN
776}
777
6e6d6dc0 778static int mov_write_dinf_tag(ByteIOContext *pb)
1cb5f7fd 779{
b29af723 780 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
781 put_be32(pb, 0); /* size */
782 put_tag(pb, "dinf");
6e6d6dc0
MN
783 mov_write_dref_tag(pb);
784 return updateSize(pb, pos);
1cb5f7fd
MN
785}
786
6e6d6dc0 787static int mov_write_smhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
788{
789 put_be32(pb, 16); /* size */
790 put_tag(pb, "smhd");
791 put_be32(pb, 0); /* version & flags */
792 put_be16(pb, 0); /* reserved (balance, normally = 0) */
793 put_be16(pb, 0); /* reserved */
794 return 16;
795}
796
6e6d6dc0 797static int mov_write_vmhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
798{
799 put_be32(pb, 0x14); /* size (always 0x14) */
800 put_tag(pb, "vmhd");
801 put_be32(pb, 0x01); /* version & flags */
802 put_be64(pb, 0); /* reserved (graphics mode = copy) */
803 return 0x14;
804}
805
0724373d 806static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 807{
a97c1e93 808 const char *descr, *hdlr, *hdlr_type;
b29af723 809 offset_t pos = url_ftell(pb);
115329f1 810
9a4d9388 811 if (!track) { /* no media --> data handler */
bb270c08
DB
812 hdlr = "dhlr";
813 hdlr_type = "url ";
814 descr = "DataHandler";
9a4d9388 815 } else {
bb270c08
DB
816 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
817 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
818 hdlr_type = "vide";
819 descr = "VideoHandler";
820 } else {
821 hdlr_type = "soun";
822 descr = "SoundHandler";
823 }
9a4d9388 824 }
115329f1 825
f578f938 826 put_be32(pb, 0); /* size */
1cb5f7fd
MN
827 put_tag(pb, "hdlr");
828 put_be32(pb, 0); /* Version & flags */
906b578f 829 put_buffer(pb, hdlr, 4); /* handler */
9a4d9388 830 put_tag(pb, hdlr_type); /* handler type */
f578f938
TR
831 put_be32(pb ,0); /* reserved */
832 put_be32(pb ,0); /* reserved */
833 put_be32(pb ,0); /* reserved */
9a4d9388
RS
834 put_byte(pb, strlen(descr)); /* string counter */
835 put_buffer(pb, descr, strlen(descr)); /* handler description */
836 return updateSize(pb, pos);
837}
838
0724373d 839static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track)
9a4d9388 840{
b29af723 841 offset_t pos = url_ftell(pb);
9a4d9388
RS
842 put_be32(pb, 0); /* size */
843 put_tag(pb, "minf");
1cb5f7fd 844 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
9a4d9388 845 mov_write_vmhd_tag(pb);
1cb5f7fd 846 else
9a4d9388
RS
847 mov_write_smhd_tag(pb);
848 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
849 mov_write_hdlr_tag(pb, NULL);
850 mov_write_dinf_tag(pb);
851 mov_write_stbl_tag(pb, track);
f578f938 852 return updateSize(pb, pos);
1cb5f7fd
MN
853}
854
0724373d 855static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 856{
a3a80ddc
BC
857 int version = track->trackDuration < INT32_MAX ? 0 : 1;
858
859 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
1cb5f7fd 860 put_tag(pb, "mdhd");
a3a80ddc
BC
861 put_byte(pb, version);
862 put_be24(pb, 0); /* flags */
863 if (version == 1) {
864 put_be64(pb, track->time);
865 put_be64(pb, track->time);
866 } else {
867 put_be32(pb, track->time); /* creation time */
868 put_be32(pb, track->time); /* modification time */
869 }
115329f1 870 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
a3a80ddc 871 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
ab561df9 872 put_be16(pb, track->language); /* language */
1cb5f7fd 873 put_be16(pb, 0); /* reserved (quality) */
db8f4a92
MN
874
875 if(version!=0 && track->mode == MODE_MOV){
876 av_log(NULL, AV_LOG_ERROR,
877 "FATAL error, file duration too long for timebase, this file will not be\n"
b1b64c23 878 "playable with quicktime. Choose a different timebase or a different\n"
db8f4a92
MN
879 "container format\n");
880 }
881
1cb5f7fd
MN
882 return 32;
883}
884
0724373d 885static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack *track)
1cb5f7fd 886{
b29af723 887 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
888 put_be32(pb, 0); /* size */
889 put_tag(pb, "mdia");
6e6d6dc0
MN
890 mov_write_mdhd_tag(pb, track);
891 mov_write_hdlr_tag(pb, track);
892 mov_write_minf_tag(pb, track);
893 return updateSize(pb, pos);
1cb5f7fd
MN
894}
895
59729451 896static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 897{
a3a80ddc
BC
898 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
899 int version = duration < INT32_MAX ? 0 : 1;
900
901 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
1cb5f7fd 902 put_tag(pb, "tkhd");
a3a80ddc
BC
903 put_byte(pb, version);
904 put_be24(pb, 0xf); /* flags (track enabled) */
905 if (version == 1) {
906 put_be64(pb, track->time);
907 put_be64(pb, track->time);
908 } else {
909 put_be32(pb, track->time); /* creation time */
910 put_be32(pb, track->time); /* modification time */
911 }
1cb5f7fd
MN
912 put_be32(pb, track->trackID); /* track-id */
913 put_be32(pb, 0); /* reserved */
a3a80ddc 914 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
1cb5f7fd
MN
915
916 put_be32(pb, 0); /* reserved */
917 put_be32(pb, 0); /* reserved */
918 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
919 /* Volume, only for audio */
920 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
921 put_be16(pb, 0x0100);
922 else
923 put_be16(pb, 0);
924 put_be16(pb, 0); /* reserved */
925
926 /* Matrix structure */
927 put_be32(pb, 0x00010000); /* reserved */
928 put_be32(pb, 0x0); /* reserved */
929 put_be32(pb, 0x0); /* reserved */
930 put_be32(pb, 0x0); /* reserved */
931 put_be32(pb, 0x00010000); /* reserved */
932 put_be32(pb, 0x0); /* reserved */
933 put_be32(pb, 0x0); /* reserved */
934 put_be32(pb, 0x0); /* reserved */
935 put_be32(pb, 0x40000000); /* reserved */
936
937 /* Track width and height, for visual only */
938 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
59729451 939 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
91208916 940 if(!sample_aspect_ratio) sample_aspect_ratio = 1;
69dde1ad 941 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
f578f938 942 put_be32(pb, track->enc->height*0x10000);
1cb5f7fd
MN
943 }
944 else {
945 put_be32(pb, 0);
946 put_be32(pb, 0);
947 }
948 return 0x5c;
949}
950
8af18154 951// This box seems important for the psp playback ... without it the movie seems to hang
952static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
953{
8af18154 954 put_be32(pb, 0x24); /* size */
955 put_tag(pb, "edts");
956 put_be32(pb, 0x1c); /* size */
957 put_tag(pb, "elst");
958 put_be32(pb, 0x0);
959 put_be32(pb, 0x1);
960
83fa2ef8 961 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
8af18154 962
72289140 963 put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
8af18154 964 put_be32(pb, 0x00010000);
965 return 0x24;
966}
967
968// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
969static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
970{
971 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
972 put_tag(pb, "uuid");
973 put_tag(pb, "USMT");
974 put_be32(pb, 0x21d24fce);
975 put_be32(pb, 0xbb88695c);
976 put_be32(pb, 0xfac9c740);
977 put_be32(pb, 0x1c); // another size here!
978 put_tag(pb, "MTDT");
979 put_be32(pb, 0x00010012);
980 put_be32(pb, 0x0a);
981 put_be32(pb, 0x55c40000);
982 put_be32(pb, 0x1);
983 put_be32(pb, 0x0);
984 return 0x34;
985}
986
59729451 987static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 988{
b29af723 989 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
990 put_be32(pb, 0); /* size */
991 put_tag(pb, "trak");
59729451 992 mov_write_tkhd_tag(pb, track, st);
c6e722e5 993 if (track->mode == MODE_PSP || track->hasBframes)
8af18154 994 mov_write_edts_tag(pb, track); // PSP Movies require edts box
6e6d6dc0 995 mov_write_mdia_tag(pb, track);
115329f1 996 if (track->mode == MODE_PSP)
8af18154 997 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
6e6d6dc0 998 return updateSize(pb, pos);
1cb5f7fd
MN
999}
1000
88730be6 1001#if 0
1cb5f7fd 1002/* TODO: Not sorted out, but not necessary either */
6e6d6dc0 1003static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
1004{
1005 put_be32(pb, 0x15); /* size */
1006 put_tag(pb, "iods");
1007 put_be32(pb, 0); /* version & flags */
1008 put_be16(pb, 0x1007);
1009 put_byte(pb, 0);
1010 put_be16(pb, 0x4fff);
1011 put_be16(pb, 0xfffe);
1012 put_be16(pb, 0x01ff);
1013 return 0x15;
1014}
88730be6 1015#endif
1cb5f7fd 1016
6e6d6dc0 1017static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd 1018{
b29af723
MN
1019 int maxTrackID = 1, i;
1020 int64_t maxTrackLenTemp, maxTrackLen = 0;
a3a80ddc 1021 int version;
1cb5f7fd 1022
42fb4148 1023 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1024 if(mov->tracks[i].entry > 0) {
83fa2ef8 1025 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
f578f938
TR
1026 if(maxTrackLen < maxTrackLenTemp)
1027 maxTrackLen = maxTrackLenTemp;
1cb5f7fd
MN
1028 if(maxTrackID < mov->tracks[i].trackID)
1029 maxTrackID = mov->tracks[i].trackID;
1030 }
1031 }
a3a80ddc
BC
1032
1033 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1034 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1035 put_tag(pb, "mvhd");
1036 put_byte(pb, version);
1037 put_be24(pb, 0); /* flags */
1038 if (version == 1) {
1039 put_be64(pb, mov->time);
1040 put_be64(pb, mov->time);
1041 } else {
1042 put_be32(pb, mov->time); /* creation time */
1043 put_be32(pb, mov->time); /* modification time */
1044 }
1045 put_be32(pb, mov->timescale); /* timescale */
1046 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1cb5f7fd
MN
1047
1048 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1049 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1050 put_be16(pb, 0); /* reserved */
1051 put_be32(pb, 0); /* reserved */
1052 put_be32(pb, 0); /* reserved */
1053
1054 /* Matrix structure */
1055 put_be32(pb, 0x00010000); /* reserved */
1056 put_be32(pb, 0x0); /* reserved */
1057 put_be32(pb, 0x0); /* reserved */
1058 put_be32(pb, 0x0); /* reserved */
1059 put_be32(pb, 0x00010000); /* reserved */
1060 put_be32(pb, 0x0); /* reserved */
1061 put_be32(pb, 0x0); /* reserved */
1062 put_be32(pb, 0x0); /* reserved */
1063 put_be32(pb, 0x40000000); /* reserved */
1064
1065 put_be32(pb, 0); /* reserved (preview time) */
1066 put_be32(pb, 0); /* reserved (preview duration) */
1067 put_be32(pb, 0); /* reserved (poster time) */
1068 put_be32(pb, 0); /* reserved (selection time) */
1069 put_be32(pb, 0); /* reserved (selection duration) */
1070 put_be32(pb, 0); /* reserved (current time) */
1071 put_be32(pb, maxTrackID+1); /* Next track id */
1072 return 0x6c;
1073}
1074
0724373d 1075static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1076 AVFormatContext *s)
1077{
b29af723 1078 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1079 put_be32(pb, 0); /* size */
1080 put_tag(pb, "hdlr");
1081 put_be32(pb, 0);
1082 put_be32(pb, 0);
1083 put_tag(pb, "mdir");
1084 put_tag(pb, "appl");
1085 put_be32(pb, 0);
1086 put_be32(pb, 0);
1087 put_be16(pb, 0);
1088 return updateSize(pb, pos);
1089}
1090
1091/* helper function to write a data tag with the specified string as data */
efda3395 1092static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
b6c50eb1 1093{
efda3395 1094 if(long_style){
b29af723 1095 offset_t pos = url_ftell(pb);
b6c50eb1 1096 put_be32(pb, 0); /* size */
efda3395
MN
1097 put_tag(pb, "data");
1098 put_be32(pb, 1);
1099 put_be32(pb, 0);
1100 put_buffer(pb, data, strlen(data));
1101 return updateSize(pb, pos);
1102 }else{
1103 put_be16(pb, strlen(data)); /* string length */
1104 put_be16(pb, 0);
1105 put_buffer(pb, data, strlen(data));
1106 return strlen(data) + 4;
b6c50eb1 1107 }
b6c50eb1
PB
1108}
1109
a97c1e93 1110static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
b6c50eb1 1111 int size = 0;
91208916 1112 if (value && value[0]) {
b29af723 1113 offset_t pos = url_ftell(pb);
b6c50eb1 1114 put_be32(pb, 0); /* size */
efda3395
MN
1115 put_tag(pb, name);
1116 mov_write_string_data_tag(pb, value, long_style);
1117 size= updateSize(pb, pos);
b6c50eb1
PB
1118 }
1119 return size;
1120}
1121
1122/* iTunes year */
efda3395 1123static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
b6c50eb1 1124{
efda3395
MN
1125 if(year){
1126 char year_str[5];
1127 snprintf(year_str, sizeof(year_str), "%04d", year);
1128 return mov_write_string_tag(pb, "\251day", year_str, long_style);
1129 }else
1130 return 0;
b6c50eb1
PB
1131}
1132
1133/* iTunes track number */
0724373d 1134static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1135 AVFormatContext *s)
1136{
1137 int size = 0;
91208916 1138 if (s->track) {
b29af723 1139 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1140 put_be32(pb, 0); /* size */
1141 put_tag(pb, "trkn");
1142 {
b29af723 1143 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1144 put_be32(pb, 0); /* size */
1145 put_tag(pb, "data");
1146 put_be32(pb, 0); // 8 bytes empty
1147 put_be32(pb, 0);
1148 put_be16(pb, 0); // empty
1149 put_be16(pb, s->track); // track number
1150 put_be16(pb, 0); // total track number
1151 put_be16(pb, 0); // empty
1152 updateSize(pb, pos);
1153 }
1154 size = updateSize(pb, pos);
1155 }
1156 return size;
1157}
1158
1159/* iTunes meta data list */
0724373d 1160static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1161 AVFormatContext *s)
1162{
b29af723 1163 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1164 put_be32(pb, 0); /* size */
1165 put_tag(pb, "ilst");
efda3395
MN
1166 mov_write_string_tag(pb, "\251nam", s->title , 1);
1167 mov_write_string_tag(pb, "\251ART", s->author , 1);
1168 mov_write_string_tag(pb, "\251wrt", s->author , 1);
1169 mov_write_string_tag(pb, "\251alb", s->album , 1);
1170 mov_write_day_tag(pb, s->year ,1);
d1d87dfb 1171 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
efda3395
MN
1172 mov_write_string_tag(pb, "\251cmt", s->comment , 1);
1173 mov_write_string_tag(pb, "\251gen", s->genre , 1);
b6c50eb1
PB
1174 mov_write_trkn_tag(pb, mov, s);
1175 return updateSize(pb, pos);
1176}
1177
1178/* iTunes meta data tag */
0724373d 1179static int mov_write_meta_tag(ByteIOContext *pb, MOVContext *mov,
b6c50eb1
PB
1180 AVFormatContext *s)
1181{
1182 int size = 0;
1183
1184 // only save meta tag if required
91208916 1185 if (s->title[0] || s->author[0] || s->album[0] || s->year ||
d88812a9 1186 s->comment[0] || s->genre[0] || s->track) {
b29af723 1187 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1188 put_be32(pb, 0); /* size */
1189 put_tag(pb, "meta");
1190 put_be32(pb, 0);
1191 mov_write_itunes_hdlr_tag(pb, mov, s);
1192 mov_write_ilst_tag(pb, mov, s);
1193 size = updateSize(pb, pos);
1194 }
1195 return size;
1196}
115329f1 1197
07b7b06d
BC
1198static int utf8len(const uint8_t *b)
1199{
1200 int len=0;
1201 int val;
1202 while(*b){
1203 GET_UTF8(val, *b++, return -1;)
1204 len++;
1205 }
1206 return len;
1207}
1208
1209static int ascii_to_wc(ByteIOContext *pb, const uint8_t *b)
1210{
1211 int val;
1212 while(*b){
1213 GET_UTF8(val, *b++, return -1;)
1214 put_be16(pb, val);
1215 }
1216 put_be16(pb, 0x00);
1217 return 0;
1218}
1219
1220static uint16_t language_code(const char *str)
1221{
1222 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1223}
1224
c6e2c6c9
BC
1225static int mov_write_3gp_udta_tag(ByteIOContext *pb, AVFormatContext *s,
1226 const char *tag, const char *str)
1227{
1228 offset_t pos = url_ftell(pb);
1229 if (!utf8len(str))
1230 return 0;
1231 put_be32(pb, 0); /* size */
1232 put_tag (pb, tag); /* type */
1233 put_be32(pb, 0); /* version + flags */
1234 if (!strcmp(tag, "yrrc"))
1235 put_be16(pb, s->year);
1236 else {
1237 put_be16(pb, language_code("eng")); /* language */
1238 ascii_to_wc(pb, str);
1239 if (!strcmp(tag, "albm") && s->year)
1240 put_byte(pb, s->year);
1241 }
1242 return updateSize(pb, pos);
1243}
1244
0724373d 1245static int mov_write_udta_tag(ByteIOContext *pb, MOVContext *mov,
69dde1ad
GB
1246 AVFormatContext *s)
1247{
b63ba850 1248 int i;
d1d87dfb 1249 int bitexact = 0;
89a0d790 1250
d1d87dfb
BC
1251 for (i = 0; i < s->nb_streams; i++)
1252 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1253 bitexact = 1;
1254 break;
1255 }
1256
1257 if (!bitexact && (s->title[0] || s->author[0] || s->album[0] || s->year ||
1258 s->comment[0] || s->genre[0] || s->track)) {
cfac9ec7 1259 offset_t pos = url_ftell(pb);
69dde1ad 1260
cfac9ec7
BF
1261 put_be32(pb, 0); /* size */
1262 put_tag(pb, "udta");
69dde1ad 1263
a04aec6d 1264 if (mov->mode & MODE_3GP) {
c6e2c6c9
BC
1265 mov_write_3gp_udta_tag(pb, s, "titl", s->title);
1266 mov_write_3gp_udta_tag(pb, s, "auth", s->author);
1267 mov_write_3gp_udta_tag(pb, s, "gnre", s->genre);
1268 mov_write_3gp_udta_tag(pb, s, "dscp", s->comment);
1269 mov_write_3gp_udta_tag(pb, s, "albm", s->album);
1270 mov_write_3gp_udta_tag(pb, s, "yrrc", "nil");
ce072b2d
BC
1271 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1272 mov_write_string_tag(pb, "\251nam", s->title , 0);
1273 mov_write_string_tag(pb, "\251aut", s->author , 0);
1274 mov_write_string_tag(pb, "\251alb", s->album , 0);
1275 mov_write_day_tag(pb, s->year, 0);
1276 mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1277 mov_write_string_tag(pb, "\251des", s->comment , 0);
1278 mov_write_string_tag(pb, "\251gen", s->genre , 0);
c6e2c6c9 1279 } else {
283c9a8e
BC
1280 /* iTunes meta data */
1281 mov_write_meta_tag(pb, mov, s);
c6e2c6c9 1282 }
cfac9ec7 1283 return updateSize(pb, pos);
89a0d790
BF
1284 }
1285
1286 return 0;
69dde1ad
GB
1287}
1288
d32d6def 1289static void mov_write_psp_udta_tag(ByteIOContext *pb,
20e22af8
BC
1290 const char *str, const char *lang, int type)
1291{
1292 int len = utf8len(str)+1;
1293 if(len<=0)
d32d6def 1294 return;
20e22af8
BC
1295 put_be16(pb, len*2+10); /* size */
1296 put_be32(pb, type); /* type */
1297 put_be16(pb, language_code(lang)); /* language */
1298 put_be16(pb, 0x01); /* ? */
1299 ascii_to_wc(pb, str);
20e22af8
BC
1300}
1301
0724373d 1302static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
dcfdb046 1303{
d32d6def 1304 offset_t pos, pos2;
dcfdb046 1305
dcfdb046
BL
1306 if (s->title[0]) {
1307 pos = url_ftell(pb);
1308 put_be32(pb, 0); /* size placeholder*/
1309 put_tag(pb, "uuid");
1310 put_tag(pb, "USMT");
91208916
BC
1311 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1312 put_be32(pb, 0xbb88695c);
1313 put_be32(pb, 0xfac9c740);
dcfdb046 1314
d32d6def 1315 pos2 = url_ftell(pb);
dcfdb046
BL
1316 put_be32(pb, 0); /* size placeholder*/
1317 put_tag(pb, "MTDT");
7cf0e16f 1318 put_be16(pb, 4);
dcfdb046 1319
7cf0e16f
MN
1320 // ?
1321 put_be16(pb, 0x0C); /* size */
1322 put_be32(pb, 0x0B); /* type */
1323 put_be16(pb, language_code("und")); /* language */
1324 put_be16(pb, 0x0); /* ? */
1325 put_be16(pb, 0x021C); /* data */
7cf0e16f 1326
d32d6def
BC
1327 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1328 mov_write_psp_udta_tag(pb, s->title, "eng", 0x01);
7cf0e16f 1329// 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
1330 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1331
1332 updateSize(pb, pos2);
1333 return updateSize(pb, pos);
dcfdb046
BL
1334 }
1335
d32d6def 1336 return 0;
dcfdb046
BL
1337}
1338
69dde1ad
GB
1339static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1340 AVFormatContext *s)
1cb5f7fd 1341{
b29af723
MN
1342 int i;
1343 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
1344 put_be32(pb, 0); /* size placeholder*/
1345 put_tag(pb, "moov");
1346 mov->timescale = globalTimescale;
1347
42fb4148 1348 for (i=0; i<mov->nb_streams; i++) {
e45ccf79
GB
1349 if(mov->tracks[i].entry <= 0) continue;
1350
e45ccf79
GB
1351 mov->tracks[i].time = mov->time;
1352 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
1353 }
1354
6e6d6dc0
MN
1355 mov_write_mvhd_tag(pb, mov);
1356 //mov_write_iods_tag(pb, mov);
42fb4148 1357 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1358 if(mov->tracks[i].entry > 0) {
59729451 1359 mov_write_trak_tag(pb, &(mov->tracks[i]), s->streams[i]);
1cb5f7fd
MN
1360 }
1361 }
1362
dcfdb046
BL
1363 if (mov->mode == MODE_PSP)
1364 mov_write_uuidusmt_tag(pb, s);
c6e2c6c9 1365 else
e344c1ea 1366 mov_write_udta_tag(pb, mov, s);
69dde1ad 1367
6e6d6dc0 1368 return updateSize(pb, pos);
1cb5f7fd
MN
1369}
1370
0724373d 1371static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd 1372{
b29af723 1373 put_be32(pb, 8); // placeholder for extended size field (64 bit)
5340a0bc 1374 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
b29af723 1375
115329f1 1376 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
1377 put_be32(pb, 0); /* size placeholder*/
1378 put_tag(pb, "mdat");
1379 return 0;
1380}
1381
1382/* TODO: This needs to be more general */
7b0fb8b7 1383static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 1384{
69dde1ad 1385 MOVContext *mov = s->priv_data;
1560b667 1386 offset_t pos = url_ftell(pb);
cd70d17f 1387 int has_h264 = 0, has_video = 0;
40f8675c 1388 int minor = 0x200;
c55cc074 1389 int i;
69dde1ad 1390
83579084
BC
1391 for (i = 0; i < s->nb_streams; i++) {
1392 AVStream *st = s->streams[i];
cd70d17f
BC
1393 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1394 has_video = 1;
83579084
BC
1395 if (st->codec->codec_id == CODEC_ID_H264)
1396 has_h264 = 1;
1397 }
1398
1560b667 1399 put_be32(pb, 0); /* size */
1cb5f7fd 1400 put_tag(pb, "ftyp");
e45ccf79 1401
40f8675c
BC
1402 if (mov->mode == MODE_3GP) {
1403 put_tag(pb, has_h264 ? "3gp6" : "3gp4");
1404 minor = has_h264 ? 0x100 : 0x200;
1405 } else if (mov->mode & MODE_3G2) {
1406 put_tag(pb, has_h264 ? "3g2b" : "3g2a");
1407 minor = has_h264 ? 0x20000 : 0x10000;
1408 }else if (mov->mode == MODE_PSP)
8af18154 1409 put_tag(pb, "MSNV");
c55cc074 1410 else if (mov->mode == MODE_MP4)
e45ccf79 1411 put_tag(pb, "isom");
cd70d17f
BC
1412 else if (mov->mode == MODE_IPOD)
1413 put_tag(pb, has_video ? "M4V ":"M4A ");
1414 else
26f86eaf 1415 put_tag(pb, "qt ");
e45ccf79 1416
40f8675c 1417 put_be32(pb, minor);
e45ccf79 1418
6ba9ed95
BC
1419 if(mov->mode == MODE_MOV)
1420 put_tag(pb, "qt ");
1421 else{
ed6e47c2 1422 put_tag(pb, "isom");
65f52683 1423 put_tag(pb, "iso2");
83579084
BC
1424 if(has_h264)
1425 put_tag(pb, "avc1");
6ba9ed95 1426 }
ed6e47c2 1427
91208916 1428 if (mov->mode == MODE_3GP)
3473ca9a 1429 put_tag(pb, has_h264 ? "3gp6":"3gp4");
a04aec6d 1430 else if (mov->mode & MODE_3G2)
3473ca9a 1431 put_tag(pb, has_h264 ? "3g2b":"3g2a");
91208916 1432 else if (mov->mode == MODE_PSP)
8af18154 1433 put_tag(pb, "MSNV");
c55cc074 1434 else if (mov->mode == MODE_MP4)
e45ccf79 1435 put_tag(pb, "mp41");
1560b667 1436 return updateSize(pb, pos);
1cb5f7fd
MN
1437}
1438
0c716ab7 1439static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
8af18154 1440{
dcfdb046
BL
1441 AVCodecContext *VideoCodec = s->streams[0]->codec;
1442 AVCodecContext *AudioCodec = s->streams[1]->codec;
1443 int AudioRate = AudioCodec->sample_rate;
1444 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
9e282ba3
MN
1445 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1446 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
8af18154 1447
91208916 1448 put_be32(pb, 0x94); /* size */
8af18154 1449 put_tag(pb, "uuid");
1450 put_tag(pb, "PROF");
1451
91208916
BC
1452 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1453 put_be32(pb, 0xbb88695c);
1454 put_be32(pb, 0xfac9c740);
8af18154 1455
91208916
BC
1456 put_be32(pb, 0x0); /* ? */
1457 put_be32(pb, 0x3); /* 3 sections ? */
8af18154 1458
91208916 1459 put_be32(pb, 0x14); /* size */
8af18154 1460 put_tag(pb, "FPRF");
91208916
BC
1461 put_be32(pb, 0x0); /* ? */
1462 put_be32(pb, 0x0); /* ? */
1463 put_be32(pb, 0x0); /* ? */
8af18154 1464
91208916 1465 put_be32(pb, 0x2c); /* size */
8af18154 1466 put_tag(pb, "APRF"); /* audio */
91208916
BC
1467 put_be32(pb, 0x0);
1468 put_be32(pb, 0x2); /* TrackID */
8af18154 1469 put_tag(pb, "mp4a");
91208916
BC
1470 put_be32(pb, 0x20f);
1471 put_be32(pb, 0x0);
9e282ba3
MN
1472 put_be32(pb, audio_kbitrate);
1473 put_be32(pb, audio_kbitrate);
91208916
BC
1474 put_be32(pb, AudioRate);
1475 put_be32(pb, AudioCodec->channels);
8af18154 1476
91208916 1477 put_be32(pb, 0x34); /* size */
8af18154 1478 put_tag(pb, "VPRF"); /* video */
91208916
BC
1479 put_be32(pb, 0x0);
1480 put_be32(pb, 0x1); /* TrackID */
87b041e0
MN
1481 if (VideoCodec->codec_id == CODEC_ID_H264) {
1482 put_tag(pb, "avc1");
91208916
BC
1483 put_be16(pb, 0x014D);
1484 put_be16(pb, 0x0015);
87b041e0
MN
1485 } else {
1486 put_tag(pb, "mp4v");
91208916
BC
1487 put_be16(pb, 0x0000);
1488 put_be16(pb, 0x0103);
87b041e0 1489 }
91208916 1490 put_be32(pb, 0x0);
9e282ba3
MN
1491 put_be32(pb, video_kbitrate);
1492 put_be32(pb, video_kbitrate);
dcfdb046
BL
1493 put_be32(pb, FrameRate);
1494 put_be32(pb, FrameRate);
1495 put_be16(pb, VideoCodec->width);
1496 put_be16(pb, VideoCodec->height);
1497 put_be32(pb, 0x010001); /* ? */
8af18154 1498}
1499
1cb5f7fd
MN
1500static int mov_write_header(AVFormatContext *s)
1501{
899681cd 1502 ByteIOContext *pb = s->pb;
69dde1ad
GB
1503 MOVContext *mov = s->priv_data;
1504 int i;
1cb5f7fd 1505
899681cd 1506 if (url_is_streamed(s->pb)) {
4f60f34a
BC
1507 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1508 return -1;
1509 }
1510
1522767d
BC
1511 /* Default mode == MP4 */
1512 mov->mode = MODE_MP4;
1513
1514 if (s->oformat != NULL) {
1515 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
a04aec6d 1516 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
1522767d
BC
1517 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1518 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
aa9f4208 1519 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
1522767d 1520
26f86eaf 1521 mov_write_ftyp_tag(pb,s);
91208916
BC
1522 if (mov->mode == MODE_PSP) {
1523 if (s->nb_streams != 2) {
1522767d
BC
1524 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1525 return -1;
1526 }
1527 mov_write_uuidprof_tag(pb,s);
1528 }
1529 }
1530
ffdd57d4 1531 for(i=0; i<s->nb_streams; i++){
039627cf
BC
1532 AVStream *st= s->streams[i];
1533 MOVTrack *track= &mov->tracks[i];
1534
1535 track->enc = st->codec;
2c5ad5fd
BC
1536 track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1537 track->mode = mov->mode;
a23c9c4a
BC
1538 track->tag = mov_find_codec_tag(s, track);
1539 if (!track->tag) {
bc4c3a55
BC
1540 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
1541 "codec not currently supported in container\n", i);
a23c9c4a
BC
1542 return -1;
1543 }
039627cf 1544 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
ffb512e3 1545 track->timescale = st->codec->time_base.den;
039627cf 1546 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
c8134d71 1547 if (track->mode == MODE_MOV && track->timescale > 100000)
4ad08acc 1548 av_log(s, AV_LOG_WARNING,
911f9b71
BC
1549 "WARNING codec timebase is very high. If duration is too long,\n"
1550 "file may not be playable by quicktime. Specify a shorter timebase\n"
1551 "or choose different container.\n");
039627cf 1552 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
ffb512e3 1553 track->timescale = st->codec->sample_rate;
039627cf 1554 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
4278662d 1555 if(!st->codec->frame_size){
b596102e
BC
1556 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1557 return -1;
4278662d
BC
1558 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1559 track->audio_vbr = 1;
1560 }else{
1561 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
b596102e 1562 }
ffb512e3 1563 }
f578f938
TR
1564 }
1565
18c697ac
BC
1566 mov_write_mdat_tag(pb, mov);
1567 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
42fb4148 1568 mov->nb_streams = s->nb_streams;
18c697ac 1569
1cb5f7fd
MN
1570 put_flush_packet(pb);
1571
1572 return 0;
1573}
1574
e928649b 1575static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd
MN
1576{
1577 MOVContext *mov = s->priv_data;
899681cd 1578 ByteIOContext *pb = s->pb;
039627cf
BC
1579 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1580 AVCodecContext *enc = trk->enc;
e45ccf79 1581 unsigned int samplesInChunk = 0;
e928649b 1582 int size= pkt->size;
1cb5f7fd 1583
899681cd 1584 if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
e45ccf79 1585 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 1586
06fcf56c 1587 if (enc->codec_id == CODEC_ID_AMR_NB) {
5cb49ca1
BC
1588 /* We must find out how many AMR blocks there are in one packet */
1589 static uint16_t packed_size[16] =
1590 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1591 int len = 0;
1592
1593 while (len < size && samplesInChunk < 100) {
1594 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1595 samplesInChunk++;
1cb5f7fd 1596 }
2111e3f9 1597 if(samplesInChunk > 1){
89938d29 1598 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2111e3f9
MN
1599 return -1;
1600 }
5cb49ca1
BC
1601 } else if (trk->sampleSize)
1602 samplesInChunk = size/trk->sampleSize;
1603 else
5616f85d 1604 samplesInChunk = 1;
e45ccf79 1605
5616f85d
BC
1606 /* copy extradata if it exists */
1607 if (trk->vosLen == 0 && enc->extradata_size > 0) {
e45ccf79
GB
1608 trk->vosLen = enc->extradata_size;
1609 trk->vosData = av_malloc(trk->vosLen);
1610 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1611 }
1612
5616f85d
BC
1613 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1614 /* from x264 or from bytestream h264 */
1615 /* nal reformating needed */
9ab3f71b 1616 int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
ed7fa39c
AJ
1617 if (ret < 0)
1618 return ret;
5616f85d
BC
1619 assert(pkt->size);
1620 size = pkt->size;
fc4cbc16 1621 } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
db568c07
BC
1622 /* copy frame to create needed atoms */
1623 trk->vosLen = size;
1624 trk->vosData = av_malloc(size);
e8c4df40
BC
1625 if (!trk->vosData)
1626 return AVERROR(ENOMEM);
db568c07 1627 memcpy(trk->vosData, pkt->data, size);
c1b8e6d8
BC
1628 }
1629
0aec3c5c
BC
1630 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1631 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
e45ccf79
GB
1632 if (!trk->cluster)
1633 return -1;
e45ccf79 1634 }
e45ccf79 1635
0aec3c5c
BC
1636 trk->cluster[trk->entry].pos = url_ftell(pb);
1637 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1638 trk->cluster[trk->entry].size = size;
1639 trk->cluster[trk->entry].entries = samplesInChunk;
aa90239f
BC
1640 trk->cluster[trk->entry].dts = pkt->dts;
1641 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1642
96f69e0d
BC
1643 if (pkt->pts == AV_NOPTS_VALUE) {
1644 av_log(s, AV_LOG_WARNING, "pts has no value\n");
1645 pkt->pts = pkt->dts;
1646 }
c4f078ff
BC
1647 if (pkt->dts != pkt->pts)
1648 trk->hasBframes = 1;
1649 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1650 trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1651 if(trk->cluster[trk->entry].key_frame)
1652 trk->hasKeyframes++;
e45ccf79
GB
1653 trk->entry++;
1654 trk->sampleCount += samplesInChunk;
039627cf 1655 mov->mdat_size += size;
e45ccf79 1656
e928649b 1657 put_buffer(pb, pkt->data, size);
1cb5f7fd
MN
1658
1659 put_flush_packet(pb);
1660 return 0;
1661}
1662
1663static int mov_write_trailer(AVFormatContext *s)
1664{
1665 MOVContext *mov = s->priv_data;
899681cd 1666 ByteIOContext *pb = s->pb;
1cb5f7fd 1667 int res = 0;
0aec3c5c 1668 int i;
1cb5f7fd 1669
69dde1ad 1670 offset_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1671
1672 /* Write size of mdat tag */
039627cf 1673 if (mov->mdat_size+8 <= UINT32_MAX) {
b29af723 1674 url_fseek(pb, mov->mdat_pos, SEEK_SET);
039627cf 1675 put_be32(pb, mov->mdat_size+8);
b29af723
MN
1676 } else {
1677 /* overwrite 'wide' placeholder atom */
1678 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1679 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1680 put_tag(pb, "mdat");
039627cf 1681 put_be64(pb, mov->mdat_size+16);
b29af723 1682 }
69dde1ad 1683 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1684
69dde1ad 1685 mov_write_moov_tag(pb, mov, s);
1cb5f7fd 1686
42fb4148 1687 for (i=0; i<mov->nb_streams; i++) {
0aec3c5c
BC
1688 av_freep(&mov->tracks[i].cluster);
1689
91208916 1690 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
ec7d0d2e 1691
1cb5f7fd 1692 }
69dde1ad 1693
1cb5f7fd
MN
1694 put_flush_packet(pb);
1695
1696 return res;
1697}
1698
ff70e601
MR
1699#ifdef CONFIG_MOV_MUXER
1700AVOutputFormat mov_muxer = {
1cb5f7fd 1701 "mov",
bde15e74 1702 NULL_IF_CONFIG_SMALL("MOV format"),
1cb5f7fd
MN
1703 NULL,
1704 "mov",
1705 sizeof(MOVContext),
69dde1ad 1706 CODEC_ID_AAC,
2187d948 1707 CODEC_ID_MPEG4,
1cb5f7fd
MN
1708 mov_write_header,
1709 mov_write_packet,
1710 mov_write_trailer,
c64d476c 1711 .flags = AVFMT_GLOBALHEADER,
c1854592 1712 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
1cb5f7fd 1713};
ff70e601
MR
1714#endif
1715#ifdef CONFIG_TGP_MUXER
1716AVOutputFormat tgp_muxer = {
1cb5f7fd 1717 "3gp",
bde15e74 1718 NULL_IF_CONFIG_SMALL("3GP format"),
1cb5f7fd
MN
1719 NULL,
1720 "3gp",
1721 sizeof(MOVContext),
1722 CODEC_ID_AMR_NB,
1723 CODEC_ID_H263,
1724 mov_write_header,
1725 mov_write_packet,
1726 mov_write_trailer,
c64d476c 1727 .flags = AVFMT_GLOBALHEADER,
c1854592 1728 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
1cb5f7fd 1729};
ff70e601
MR
1730#endif
1731#ifdef CONFIG_MP4_MUXER
1732AVOutputFormat mp4_muxer = {
1cb5f7fd 1733 "mp4",
bde15e74 1734 NULL_IF_CONFIG_SMALL("MP4 format"),
4cb3f3b6 1735 "application/mp4",
ccec1b69 1736 "mp4",
1cb5f7fd
MN
1737 sizeof(MOVContext),
1738 CODEC_ID_AAC,
1739 CODEC_ID_MPEG4,
1740 mov_write_header,
1741 mov_write_packet,
1742 mov_write_trailer,
c64d476c 1743 .flags = AVFMT_GLOBALHEADER,
c1854592 1744 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
1cb5f7fd 1745};
ff70e601
MR
1746#endif
1747#ifdef CONFIG_PSP_MUXER
1748AVOutputFormat psp_muxer = {
8af18154 1749 "psp",
bde15e74 1750 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
8af18154 1751 NULL,
1752 "mp4,psp",
1753 sizeof(MOVContext),
1754 CODEC_ID_AAC,
1755 CODEC_ID_MPEG4,
1756 mov_write_header,
1757 mov_write_packet,
1758 mov_write_trailer,
dcfdb046 1759 .flags = AVFMT_GLOBALHEADER,
c1854592 1760 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
8af18154 1761};
ff70e601
MR
1762#endif
1763#ifdef CONFIG_TG2_MUXER
1764AVOutputFormat tg2_muxer = {
8536ab89 1765 "3g2",
bde15e74 1766 NULL_IF_CONFIG_SMALL("3GP2 format"),
8536ab89 1767 NULL,
1768 "3g2",
1769 sizeof(MOVContext),
1770 CODEC_ID_AMR_NB,
1771 CODEC_ID_H263,
1772 mov_write_header,
1773 mov_write_packet,
1774 mov_write_trailer,
c64d476c 1775 .flags = AVFMT_GLOBALHEADER,
c1854592 1776 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
8536ab89 1777};
ff70e601 1778#endif
aa9f4208
BC
1779#ifdef CONFIG_IPOD_MUXER
1780AVOutputFormat ipod_muxer = {
1781 "ipod",
bde15e74 1782 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
aa9f4208 1783 "application/mp4",
ccec1b69 1784 "m4v,m4a",
aa9f4208
BC
1785 sizeof(MOVContext),
1786 CODEC_ID_AAC,
1787 CODEC_ID_H264,
1788 mov_write_header,
1789 mov_write_packet,
1790 mov_write_trailer,
1791 .flags = AVFMT_GLOBALHEADER,
c1854592 1792 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
aa9f4208
BC
1793};
1794#endif