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