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