don't crash on null codec; by matthieu castet <castet DOT matthieu AT free DOT fr>
[libav.git] / libavformat / movenc.c
CommitLineData
1cb5f7fd
MN
1/*
2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
69dde1ad 4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
1cb5f7fd
MN
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "avformat.h"
f578f938 21#include "avi.h"
1cb5f7fd 22#include "avio.h"
1cb5f7fd 23
6e6d6dc0
MN
24#undef NDEBUG
25#include <assert.h>
26
1cb5f7fd
MN
27#define MOV_INDEX_CLUSTER_SIZE 16384
28#define globalTimescale 1000
29
69dde1ad
GB
30#define MODE_MP4 0
31#define MODE_MOV 1
32#define MODE_3GP 2
8af18154 33#define MODE_PSP 3 // example working PSP command line:
34// ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
69dde1ad 35
1cb5f7fd 36typedef struct MOVIentry {
e45ccf79
GB
37 unsigned int flags, pos, size;
38 unsigned int samplesInChunk;
f578f938 39 char key_frame;
1cb5f7fd
MN
40 unsigned int entries;
41} MOVIentry;
42
43typedef struct MOVIndex {
69dde1ad 44 int mode;
1cb5f7fd 45 int entry;
1cb5f7fd 46 int mdat_size;
1cb5f7fd
MN
47 int ents_allocated;
48 long timescale;
49 long time;
f578f938 50 long trackDuration;
e45ccf79
GB
51 long sampleCount;
52 long sampleDuration;
f578f938 53 int hasKeyframes;
1cb5f7fd
MN
54 int trackID;
55 AVCodecContext *enc;
56
57 int vosLen;
6e6d6dc0 58 uint8_t *vosData;
1cb5f7fd
MN
59 MOVIentry** cluster;
60} MOVTrack;
61
8af18154 62typedef struct MOVContext {
69dde1ad 63 int mode;
1cb5f7fd
MN
64 long time;
65 int nb_streams;
f578f938
TR
66 int mdat_written;
67 offset_t mdat_pos;
1cb5f7fd
MN
68 long timescale;
69 MOVTrack tracks[MAX_STREAMS];
70} MOVContext;
71
e45ccf79
GB
72static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
73
6e6d6dc0
MN
74//FIXME supprt 64bit varaint with wide placeholders
75static int updateSize (ByteIOContext *pb, int pos)
1cb5f7fd
MN
76{
77 long curpos = url_ftell(pb);
78 url_fseek(pb, pos, SEEK_SET);
6e6d6dc0 79 put_be32(pb, curpos - pos); /* rewrite size */
1cb5f7fd 80 url_fseek(pb, curpos, SEEK_SET);
6e6d6dc0
MN
81
82 return curpos - pos;
1cb5f7fd
MN
83}
84
e45ccf79 85/* Chunk offset atom */
6e6d6dc0 86static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
87{
88 int i;
f578f938
TR
89 int pos = url_ftell(pb);
90 put_be32(pb, 0); /* size */
1cb5f7fd
MN
91 put_tag(pb, "stco");
92 put_be32(pb, 0); /* version & flags */
93 put_be32(pb, track->entry); /* entry count */
94 for (i=0; i<track->entry; i++) {
95 int cl = i / MOV_INDEX_CLUSTER_SIZE;
96 int id = i % MOV_INDEX_CLUSTER_SIZE;
97 put_be32(pb, track->cluster[cl][id].pos);
98 }
f578f938 99 return updateSize (pb, pos);
1cb5f7fd
MN
100}
101
e45ccf79 102/* Sample size atom */
6e6d6dc0 103static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 104{
f578f938 105 int equalChunks = 1;
e45ccf79 106 int i, j, entries = 0, tst = -1, oldtst = -1;
1cb5f7fd 107
f578f938
TR
108 int pos = url_ftell(pb);
109 put_be32(pb, 0); /* size */
1cb5f7fd
MN
110 put_tag(pb, "stsz");
111 put_be32(pb, 0); /* version & flags */
112
f578f938
TR
113 for (i=0; i<track->entry; i++) {
114 int cl = i / MOV_INDEX_CLUSTER_SIZE;
115 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79
GB
116 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
117 if(oldtst != -1 && tst != oldtst) {
118 equalChunks = 0;
f578f938
TR
119 }
120 oldtst = tst;
e45ccf79 121 entries += track->cluster[cl][id].entries;
f578f938 122 }
e45ccf79
GB
123 if (equalChunks) {
124 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
f578f938 125 put_be32(pb, sSize); // sample size
e45ccf79 126 put_be32(pb, entries); // sample count
1cb5f7fd 127 }
f578f938
TR
128 else {
129 put_be32(pb, 0); // sample size
e45ccf79 130 put_be32(pb, entries); // sample count
f578f938 131 for (i=0; i<track->entry; i++) {
1cb5f7fd
MN
132 int cl = i / MOV_INDEX_CLUSTER_SIZE;
133 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79
GB
134 for ( j=0; j<track->cluster[cl][id].entries; j++) {
135 put_be32(pb, track->cluster[cl][id].size /
136 track->cluster[cl][id].entries);
137 }
1cb5f7fd
MN
138 }
139 }
f578f938 140 return updateSize (pb, pos);
1cb5f7fd
MN
141}
142
e45ccf79 143/* Sample to chunk atom */
6e6d6dc0 144static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 145{
f578f938
TR
146 int index = 0, oldval = -1, i, entryPos, curpos;
147
148 int pos = url_ftell(pb);
149 put_be32(pb, 0); /* size */
1cb5f7fd
MN
150 put_tag(pb, "stsc");
151 put_be32(pb, 0); // version & flags
f578f938
TR
152 entryPos = url_ftell(pb);
153 put_be32(pb, track->entry); // entry count
154 for (i=0; i<track->entry; i++) {
155 int cl = i / MOV_INDEX_CLUSTER_SIZE;
156 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79 157 if(oldval != track->cluster[cl][id].samplesInChunk)
f578f938 158 {
1cb5f7fd 159 put_be32(pb, i+1); // first chunk
e45ccf79 160 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
1cb5f7fd 161 put_be32(pb, 0x1); // sample description index
e45ccf79 162 oldval = track->cluster[cl][id].samplesInChunk;
f578f938 163 index++;
1cb5f7fd
MN
164 }
165 }
f578f938
TR
166 curpos = url_ftell(pb);
167 url_fseek(pb, entryPos, SEEK_SET);
168 put_be32(pb, index); // rewrite size
169 url_fseek(pb, curpos, SEEK_SET);
1cb5f7fd 170
f578f938 171 return updateSize (pb, pos);
1cb5f7fd
MN
172}
173
e45ccf79 174/* Sync sample atom */
f578f938 175static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 176{
f578f938
TR
177 long curpos;
178 int i, index = 0, entryPos;
179 int pos = url_ftell(pb);
180 put_be32(pb, 0); // size
1cb5f7fd 181 put_tag(pb, "stss");
f578f938
TR
182 put_be32(pb, 0); // version & flags
183 entryPos = url_ftell(pb);
184 put_be32(pb, track->entry); // entry count
185 for (i=0; i<track->entry; i++) {
186 int cl = i / MOV_INDEX_CLUSTER_SIZE;
187 int id = i % MOV_INDEX_CLUSTER_SIZE;
188 if(track->cluster[cl][id].key_frame == 1) {
189 put_be32(pb, i+1);
190 index++;
191 }
192 }
193 curpos = url_ftell(pb);
194 url_fseek(pb, entryPos, SEEK_SET);
195 put_be32(pb, index); // rewrite size
196 url_fseek(pb, curpos, SEEK_SET);
197 return updateSize (pb, pos);
1cb5f7fd
MN
198}
199
6e6d6dc0 200static int mov_write_damr_tag(ByteIOContext *pb)
1cb5f7fd
MN
201{
202 put_be32(pb, 0x11); /* size */
203 put_tag(pb, "damr");
204 put_tag(pb, "FFMP");
205 put_byte(pb, 0);
f578f938
TR
206
207 put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
208 put_be16(pb, 0xa); /* Mode change period (no restriction) */
209 //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
210 //put_be16(pb, 1); /* Mode change period (no restriction) */
1cb5f7fd
MN
211 return 0x11;
212}
213
69dde1ad
GB
214static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
215{
216 int pos = url_ftell(pb);
217
218 put_be32(pb, 0); /* size */
219 put_tag(pb, "wave");
220
221 put_be32(pb, 12); /* size */
222 put_tag(pb, "frma");
223 put_tag(pb, "mp4a");
224
225 put_be32(pb, 12); /* size */
226 put_tag(pb, "mp4a");
227 put_be32(pb, 0);
228
229 mov_write_esds_tag(pb, track);
230
231 put_be32(pb, 12); /* size */
232 put_tag(pb, "srcq");
233 put_be32(pb, 0x40);
234
235 put_be32(pb, 8); /* size */
236 put_be32(pb, 0); /* null tag */
237
238 return updateSize (pb, pos);
239}
240
8cc7a34d
AB
241const CodecTag codec_movaudio_tags[] = {
242 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
243 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
244 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
245 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
246 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
247 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
248 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
249 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
250 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
251 { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
252 { 0, 0 },
253};
254
f578f938 255static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 256{
ffdd57d4
MN
257 int pos = url_ftell(pb);
258 int tag;
8cc7a34d 259
1cb5f7fd 260 put_be32(pb, 0); /* size */
f578f938 261
caacd4de
RG
262 tag = track->enc->codec_tag;
263 if (!tag)
8cc7a34d
AB
264 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
265 // if no mac fcc found, try with Microsoft tags
266 if (!tag)
267 {
268 int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
ffdd57d4 269 tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
8cc7a34d 270 }
ffdd57d4 271 put_le32(pb, tag); // store it byteswapped
f578f938 272
1cb5f7fd
MN
273 put_be32(pb, 0); /* Reserved */
274 put_be16(pb, 0); /* Reserved */
275 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
69dde1ad 276
e45ccf79 277 /* SoundDescription */
69dde1ad
GB
278 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
279 put_be16(pb, 1); /* Version 1 */
280 else
281 put_be16(pb, 0); /* Version 0 */
e45ccf79 282 put_be16(pb, 0); /* Revision level */
1cb5f7fd
MN
283 put_be32(pb, 0); /* Reserved */
284
f578f938
TR
285 put_be16(pb, track->enc->channels); /* Number of channels */
286 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
e45ccf79 287 to be a good way to get number of bits of audio */
1cb5f7fd 288 put_be16(pb, 0x10); /* Reserved */
ef19c7eb
GB
289
290 if(track->enc->codec_id == CODEC_ID_AAC ||
291 track->enc->codec_id == CODEC_ID_MP3)
292 {
293 put_be16(pb, 0xfffe); /* compression ID (vbr)*/
294 }
295 else
296 {
297 put_be16(pb, 0); /* compression ID (= 0) */
298 }
9a4d9388 299 put_be16(pb, 0); /* packet size (= 0) */
1cb5f7fd
MN
300 put_be16(pb, track->timescale); /* Time scale */
301 put_be16(pb, 0); /* Reserved */
302
69dde1ad
GB
303 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
304 {
305 /* SoundDescription V1 extended info */
306 put_be32(pb, track->enc->frame_size); /* Samples per packet */
307 put_be32(pb, 1536); /* Bytes per packet */
308 put_be32(pb, 2); /* Bytes per frame */
309 put_be32(pb, 2); /* Bytes per sample */
310 }
311
312 if(track->enc->codec_id == CODEC_ID_AAC) {
313 if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
314 else mov_write_esds_tag(pb, track);
315 }
f578f938
TR
316 if(track->enc->codec_id == CODEC_ID_AMR_NB)
317 mov_write_damr_tag(pb);
6e6d6dc0 318 return updateSize (pb, pos);
1cb5f7fd
MN
319}
320
6e6d6dc0 321static int mov_write_d263_tag(ByteIOContext *pb)
1cb5f7fd
MN
322{
323 put_be32(pb, 0xf); /* size */
324 put_tag(pb, "d263");
325 put_tag(pb, "FFMP");
326 put_be16(pb, 0x0a);
327 put_byte(pb, 0);
328 return 0xf;
329}
330
f578f938
TR
331/* TODO: No idea about these values */
332static int mov_write_svq3_tag(ByteIOContext *pb)
1cb5f7fd 333{
f578f938
TR
334 put_be32(pb, 0x15);
335 put_tag(pb, "SMI ");
336 put_tag(pb, "SEQH");
337 put_be32(pb, 0x5);
338 put_be32(pb, 0xe2c0211d);
339 put_be32(pb, 0xc0000000);
340 put_byte(pb, 0);
341 return 0x15;
1cb5f7fd
MN
342}
343
e45ccf79
GB
344static unsigned int descrLength(unsigned int len)
345{
346 if (len < 0x00000080)
347 return 2 + len;
348 else if (len < 0x00004000)
349 return 3 + len;
350 else if(len < 0x00200000)
351 return 4 + len;
352 else
353 return 5 + len;
354}
355
356static void putDescr(ByteIOContext *pb, int tag, int size)
1cb5f7fd 357{
e45ccf79
GB
358 uint32_t len;
359 uint8_t vals[4];
360
361 len = size;
362 vals[3] = (uint8_t)(len & 0x7f);
363 len >>= 7;
364 vals[2] = (uint8_t)((len & 0x7f) | 0x80);
365 len >>= 7;
366 vals[1] = (uint8_t)((len & 0x7f) | 0x80);
367 len >>= 7;
368 vals[0] = (uint8_t)((len & 0x7f) | 0x80);
369
370 put_byte(pb, tag); // DescriptorTag
371
372 if (size < 0x00000080)
373 {
374 put_byte(pb, vals[3]);
375 }
376 else if (size < 0x00004000)
377 {
378 put_byte(pb, vals[2]);
379 put_byte(pb, vals[3]);
380 }
381 else if (size < 0x00200000)
382 {
383 put_byte(pb, vals[1]);
384 put_byte(pb, vals[2]);
385 put_byte(pb, vals[3]);
386 }
387 else if (size < 0x10000000)
388 {
389 put_byte(pb, vals[0]);
390 put_byte(pb, vals[1]);
391 put_byte(pb, vals[2]);
392 put_byte(pb, vals[3]);
393 }
1cb5f7fd
MN
394}
395
6e6d6dc0 396static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
1cb5f7fd 397{
46103f6b 398 int decoderSpecificInfoLen;
e45ccf79 399 int pos = url_ftell(pb);
8af18154 400 void *vosDataBackup=track->vosData;
401 int vosLenBackup=track->vosLen;
402
403 // we should be able to have these passed in, via vosData, then we wouldn't need to attack this routine at all
404 static const char PSPAACData[]={0x13,0x10};
405 static const char PSPMP4Data[]={0x00,0x00,0x01,0xB0,0x03,0x00,0x00,0x01,0xB5,0x09,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x20,0x00,0x84,0x5D,0x4C,0x28,0x50,0x20,0xF0,0xA3,0x1F };
406
407
408 if (track->mode == MODE_PSP) // fails on psp if this is not here
409 {
410 if (track->enc->codec_id == CODEC_ID_AAC)
411 {
412 track->vosLen = 2;
413 track->vosData = PSPAACData;
414 }
415
416 if (track->enc->codec_id == CODEC_ID_MPEG4)
417 {
418 track->vosLen = 28;
419 track->vosData = PSPMP4Data;
420 }
421 }
e45ccf79 422
46103f6b
FR
423 decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
424
e45ccf79 425 put_be32(pb, 0); // size
1cb5f7fd 426 put_tag(pb, "esds");
e45ccf79 427 put_be32(pb, 0); // Version
1cb5f7fd 428
e45ccf79
GB
429 // ES descriptor
430 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
431 descrLength(1));
432 put_be16(pb, 0x0001); // ID (= 1)
1cb5f7fd
MN
433 put_byte(pb, 0x00); // flags (= no flags)
434
e45ccf79
GB
435 // DecoderConfig descriptor
436 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
437
438 if(track->enc->codec_id == CODEC_ID_AAC)
439 put_byte(pb, 0x40); // Object type indication
440 else if(track->enc->codec_id == CODEC_ID_MPEG4)
441 put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
442
443 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
444 put_byte(pb, 0x15); // flags (= Audiostream)
445 else
446 put_byte(pb, 0x11); // flags (= Visualstream)
447
1cb5f7fd
MN
448 put_byte(pb, 0x0); // Buffersize DB (24 bits)
449 put_be16(pb, 0x0dd2); // Buffersize DB
450
451 // TODO: find real values for these
8af18154 452 put_be32(pb, track->enc->bit_rate); // maxbitrate
453 put_be32(pb, track->enc->bit_rate); // avg bitrate
1cb5f7fd 454
e45ccf79
GB
455 if (track->vosLen)
456 {
457 // DecoderSpecific info descriptor
458 putDescr(pb, 0x05, track->vosLen);
459 put_buffer(pb, track->vosData, track->vosLen);
460 }
461
8af18154 462 track->vosData = vosDataBackup;
463 track->vosLen = vosLenBackup;
464
e45ccf79 465 // SL descriptor
4bfc029f 466 putDescr(pb, 0x06, 1);
1cb5f7fd 467 put_byte(pb, 0x02);
e45ccf79 468 return updateSize (pb, pos);
1cb5f7fd
MN
469}
470
8cc7a34d
AB
471const CodecTag codec_movvideo_tags[] = {
472 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
473 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
474 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
475 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
476 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
477 { 0, 0 },
478};
479
f578f938 480static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 481{
ffdd57d4 482 int pos = url_ftell(pb);
53ffdd14 483 char compressor_name[32];
ffdd57d4 484 int tag;
8cc7a34d 485
f578f938 486 put_be32(pb, 0); /* size */
8cc7a34d 487
caacd4de
RG
488 tag = track->enc->codec_tag;
489 if (!tag)
8cc7a34d
AB
490 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
491 // if no mac fcc found, try with Microsoft tags
492 if (!tag)
493 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
ffdd57d4 494 put_le32(pb, tag); // store it byteswapped
6e6d6dc0 495
f578f938
TR
496 put_be32(pb, 0); /* Reserved */
497 put_be16(pb, 0); /* Reserved */
498 put_be16(pb, 1); /* Data-reference index */
499
53ffdd14
RG
500 put_be16(pb, 0); /* Codec stream version */
501 put_be16(pb, 0); /* Codec stream revision (=0) */
502 put_tag(pb, "FFMP"); /* Vendor */
503 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
504 put_be32(pb, 0); /* Temporal Quality */
505 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
506 } else {
507 put_be32(pb, 0x200); /* Temporal Quality = normal */
508 put_be32(pb, 0x200); /* Spatial Quality = normal */
509 }
f578f938
TR
510 put_be16(pb, track->enc->width); /* Video width */
511 put_be16(pb, track->enc->height); /* Video height */
53ffdd14
RG
512 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
513 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
9a4d9388
RS
514 put_be32(pb, 0); /* Data size (= 0) */
515 put_be16(pb, 1); /* Frame count (= 1) */
516
53ffdd14 517 memset(compressor_name,0,32);
25cf9062 518 if (track->enc->codec && track->enc->codec->name)
53ffdd14 519 strncpy(compressor_name,track->enc->codec->name,31);
25cf9062 520 put_byte(pb, strlen(compressor_name));
53ffdd14 521 put_buffer(pb, compressor_name, 31);
9a4d9388 522
f578f938
TR
523 put_be16(pb, 0x18); /* Reserved */
524 put_be16(pb, 0xffff); /* Reserved */
525 if(track->enc->codec_id == CODEC_ID_MPEG4)
526 mov_write_esds_tag(pb, track);
527 else if(track->enc->codec_id == CODEC_ID_H263)
528 mov_write_d263_tag(pb);
529 else if(track->enc->codec_id == CODEC_ID_SVQ3)
530 mov_write_svq3_tag(pb);
531
532 return updateSize (pb, pos);
1cb5f7fd
MN
533}
534
6e6d6dc0 535static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 536{
6e6d6dc0 537 int pos = url_ftell(pb);
1cb5f7fd
MN
538 put_be32(pb, 0); /* size */
539 put_tag(pb, "stsd");
540 put_be32(pb, 0); /* version & flags */
541 put_be32(pb, 1); /* entry count */
f578f938
TR
542 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
543 mov_write_video_tag(pb, track);
544 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
545 mov_write_audio_tag(pb, track);
6e6d6dc0 546 return updateSize(pb, pos);
1cb5f7fd
MN
547}
548
f578f938 549/* TODO?: Currently all samples/frames seem to have same duration */
e45ccf79 550/* Time to sample atom */
6e6d6dc0 551static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
552{
553 put_be32(pb, 0x18); /* size */
554 put_tag(pb, "stts");
555 put_be32(pb, 0); /* version & flags */
556 put_be32(pb, 1); /* entry count */
557
e45ccf79
GB
558 put_be32(pb, track->sampleCount); /* sample count */
559 put_be32(pb, track->sampleDuration); /* sample duration */
1cb5f7fd
MN
560 return 0x18;
561}
562
6e6d6dc0 563static int mov_write_dref_tag(ByteIOContext *pb)
1cb5f7fd
MN
564{
565 put_be32(pb, 28); /* size */
566 put_tag(pb, "dref");
567 put_be32(pb, 0); /* version & flags */
568 put_be32(pb, 1); /* entry count */
569
570 put_be32(pb, 0xc); /* size */
571 put_tag(pb, "url ");
572 put_be32(pb, 1); /* version & flags */
573
574 return 28;
575}
576
6e6d6dc0 577static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 578{
6e6d6dc0 579 int pos = url_ftell(pb);
1cb5f7fd
MN
580 put_be32(pb, 0); /* size */
581 put_tag(pb, "stbl");
6e6d6dc0
MN
582 mov_write_stsd_tag(pb, track);
583 mov_write_stts_tag(pb, track);
f578f938
TR
584 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
585 track->hasKeyframes)
586 mov_write_stss_tag(pb, track);
6e6d6dc0
MN
587 mov_write_stsc_tag(pb, track);
588 mov_write_stsz_tag(pb, track);
589 mov_write_stco_tag(pb, track);
590 return updateSize(pb, pos);
1cb5f7fd
MN
591}
592
6e6d6dc0 593static int mov_write_dinf_tag(ByteIOContext *pb)
1cb5f7fd 594{
6e6d6dc0 595 int pos = url_ftell(pb);
1cb5f7fd
MN
596 put_be32(pb, 0); /* size */
597 put_tag(pb, "dinf");
6e6d6dc0
MN
598 mov_write_dref_tag(pb);
599 return updateSize(pb, pos);
1cb5f7fd
MN
600}
601
6e6d6dc0 602static int mov_write_smhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
603{
604 put_be32(pb, 16); /* size */
605 put_tag(pb, "smhd");
606 put_be32(pb, 0); /* version & flags */
607 put_be16(pb, 0); /* reserved (balance, normally = 0) */
608 put_be16(pb, 0); /* reserved */
609 return 16;
610}
611
6e6d6dc0 612static int mov_write_vmhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
613{
614 put_be32(pb, 0x14); /* size (always 0x14) */
615 put_tag(pb, "vmhd");
616 put_be32(pb, 0x01); /* version & flags */
617 put_be64(pb, 0); /* reserved (graphics mode = copy) */
618 return 0x14;
619}
620
6e6d6dc0 621static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 622{
9a4d9388 623 char *descr, *hdlr, *hdlr_type;
f578f938 624 int pos = url_ftell(pb);
9a4d9388
RS
625
626 if (!track) { /* no media --> data handler */
627 hdlr = "dhlr";
628 hdlr_type = "url ";
629 descr = "DataHandler";
630 } else {
631 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
632 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
633 hdlr_type = "vide";
634 descr = "VideoHandler";
635 } else {
636 hdlr_type = "soun";
637 descr = "SoundHandler";
638 }
639 }
640
f578f938 641 put_be32(pb, 0); /* size */
1cb5f7fd
MN
642 put_tag(pb, "hdlr");
643 put_be32(pb, 0); /* Version & flags */
906b578f 644 put_buffer(pb, hdlr, 4); /* handler */
9a4d9388 645 put_tag(pb, hdlr_type); /* handler type */
f578f938
TR
646 put_be32(pb ,0); /* reserved */
647 put_be32(pb ,0); /* reserved */
648 put_be32(pb ,0); /* reserved */
9a4d9388
RS
649 put_byte(pb, strlen(descr)); /* string counter */
650 put_buffer(pb, descr, strlen(descr)); /* handler description */
651 return updateSize(pb, pos);
652}
653
654static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
655{
656 int pos = url_ftell(pb);
657 put_be32(pb, 0); /* size */
658 put_tag(pb, "minf");
1cb5f7fd 659 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
9a4d9388 660 mov_write_vmhd_tag(pb);
1cb5f7fd 661 else
9a4d9388
RS
662 mov_write_smhd_tag(pb);
663 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
664 mov_write_hdlr_tag(pb, NULL);
665 mov_write_dinf_tag(pb);
666 mov_write_stbl_tag(pb, track);
f578f938 667 return updateSize(pb, pos);
1cb5f7fd
MN
668}
669
6e6d6dc0 670static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
671{
672 put_be32(pb, 32); /* size */
673 put_tag(pb, "mdhd");
674 put_be32(pb, 0); /* Version & flags */
675 put_be32(pb, track->time); /* creation time */
676 put_be32(pb, track->time); /* modification time */
e45ccf79
GB
677 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
678 put_be32(pb, track->trackDuration); /* duration */
1cb5f7fd
MN
679 put_be16(pb, 0); /* language, 0 = english */
680 put_be16(pb, 0); /* reserved (quality) */
681 return 32;
682}
683
6e6d6dc0 684static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 685{
6e6d6dc0 686 int pos = url_ftell(pb);
1cb5f7fd
MN
687 put_be32(pb, 0); /* size */
688 put_tag(pb, "mdia");
6e6d6dc0
MN
689 mov_write_mdhd_tag(pb, track);
690 mov_write_hdlr_tag(pb, track);
691 mov_write_minf_tag(pb, track);
692 return updateSize(pb, pos);
1cb5f7fd
MN
693}
694
6e6d6dc0 695static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 696{
f578f938 697 int64_t maxTrackLenTemp;
1cb5f7fd
MN
698 put_be32(pb, 0x5c); /* size (always 0x5c) */
699 put_tag(pb, "tkhd");
f578f938 700 put_be32(pb, 0xf); /* version & flags (track enabled) */
1cb5f7fd
MN
701 put_be32(pb, track->time); /* creation time */
702 put_be32(pb, track->time); /* modification time */
703 put_be32(pb, track->trackID); /* track-id */
704 put_be32(pb, 0); /* reserved */
f578f938
TR
705 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
706 put_be32(pb, (long)maxTrackLenTemp); /* duration */
1cb5f7fd
MN
707
708 put_be32(pb, 0); /* reserved */
709 put_be32(pb, 0); /* reserved */
710 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
711 /* Volume, only for audio */
712 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
713 put_be16(pb, 0x0100);
714 else
715 put_be16(pb, 0);
716 put_be16(pb, 0); /* reserved */
717
718 /* Matrix structure */
719 put_be32(pb, 0x00010000); /* reserved */
720 put_be32(pb, 0x0); /* reserved */
721 put_be32(pb, 0x0); /* reserved */
722 put_be32(pb, 0x0); /* reserved */
723 put_be32(pb, 0x00010000); /* reserved */
724 put_be32(pb, 0x0); /* reserved */
725 put_be32(pb, 0x0); /* reserved */
726 put_be32(pb, 0x0); /* reserved */
727 put_be32(pb, 0x40000000); /* reserved */
728
729 /* Track width and height, for visual only */
730 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
69dde1ad
GB
731 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
732 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
733 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
f578f938 734 put_be32(pb, track->enc->height*0x10000);
1cb5f7fd
MN
735 }
736 else {
737 put_be32(pb, 0);
738 put_be32(pb, 0);
739 }
740 return 0x5c;
741}
742
8af18154 743// This box seems important for the psp playback ... without it the movie seems to hang
744static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
745{
746 int64_t maxTrackLenTemp;
747 put_be32(pb, 0x24); /* size */
748 put_tag(pb, "edts");
749 put_be32(pb, 0x1c); /* size */
750 put_tag(pb, "elst");
751 put_be32(pb, 0x0);
752 put_be32(pb, 0x1);
753
754 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
755 put_be32(pb, (long)maxTrackLenTemp); /* duration ... doesn't seem to effect psp */
756
757 put_be32(pb, 0x0);
758 put_be32(pb, 0x00010000);
759 return 0x24;
760}
761
762// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
763static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
764{
765 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
766 put_tag(pb, "uuid");
767 put_tag(pb, "USMT");
768 put_be32(pb, 0x21d24fce);
769 put_be32(pb, 0xbb88695c);
770 put_be32(pb, 0xfac9c740);
771 put_be32(pb, 0x1c); // another size here!
772 put_tag(pb, "MTDT");
773 put_be32(pb, 0x00010012);
774 put_be32(pb, 0x0a);
775 put_be32(pb, 0x55c40000);
776 put_be32(pb, 0x1);
777 put_be32(pb, 0x0);
778 return 0x34;
779}
780
6e6d6dc0 781static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 782{
6e6d6dc0 783 int pos = url_ftell(pb);
1cb5f7fd
MN
784 put_be32(pb, 0); /* size */
785 put_tag(pb, "trak");
6e6d6dc0 786 mov_write_tkhd_tag(pb, track);
8af18154 787 if (track->mode == MODE_PSP)
788 mov_write_edts_tag(pb, track); // PSP Movies require edts box
6e6d6dc0 789 mov_write_mdia_tag(pb, track);
8af18154 790 if (track->mode == MODE_PSP)
791 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
6e6d6dc0 792 return updateSize(pb, pos);
1cb5f7fd
MN
793}
794
795/* TODO: Not sorted out, but not necessary either */
6e6d6dc0 796static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
797{
798 put_be32(pb, 0x15); /* size */
799 put_tag(pb, "iods");
800 put_be32(pb, 0); /* version & flags */
801 put_be16(pb, 0x1007);
802 put_byte(pb, 0);
803 put_be16(pb, 0x4fff);
804 put_be16(pb, 0xfffe);
805 put_be16(pb, 0x01ff);
806 return 0x15;
807}
808
6e6d6dc0 809static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
810{
811 int maxTrackID = 1, maxTrackLen = 0, i;
f578f938 812 int64_t maxTrackLenTemp;
1cb5f7fd
MN
813
814 put_be32(pb, 0x6c); /* size (always 0x6c) */
815 put_tag(pb, "mvhd");
816 put_be32(pb, 0); /* version & flags */
817 put_be32(pb, mov->time); /* creation time */
818 put_be32(pb, mov->time); /* modification time */
819 put_be32(pb, mov->timescale); /* timescale */
820 for (i=0; i<MAX_STREAMS; i++) {
821 if(mov->tracks[i].entry > 0) {
f578f938
TR
822 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
823 if(maxTrackLen < maxTrackLenTemp)
824 maxTrackLen = maxTrackLenTemp;
1cb5f7fd
MN
825 if(maxTrackID < mov->tracks[i].trackID)
826 maxTrackID = mov->tracks[i].trackID;
827 }
828 }
829 put_be32(pb, maxTrackLen); /* duration of longest track */
830
831 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
832 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
833 put_be16(pb, 0); /* reserved */
834 put_be32(pb, 0); /* reserved */
835 put_be32(pb, 0); /* reserved */
836
837 /* Matrix structure */
838 put_be32(pb, 0x00010000); /* reserved */
839 put_be32(pb, 0x0); /* reserved */
840 put_be32(pb, 0x0); /* reserved */
841 put_be32(pb, 0x0); /* reserved */
842 put_be32(pb, 0x00010000); /* reserved */
843 put_be32(pb, 0x0); /* reserved */
844 put_be32(pb, 0x0); /* reserved */
845 put_be32(pb, 0x0); /* reserved */
846 put_be32(pb, 0x40000000); /* reserved */
847
848 put_be32(pb, 0); /* reserved (preview time) */
849 put_be32(pb, 0); /* reserved (preview duration) */
850 put_be32(pb, 0); /* reserved (poster time) */
851 put_be32(pb, 0); /* reserved (selection time) */
852 put_be32(pb, 0); /* reserved (selection duration) */
853 put_be32(pb, 0); /* reserved (current time) */
854 put_be32(pb, maxTrackID+1); /* Next track id */
855 return 0x6c;
856}
857
b6c50eb1
PB
858static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
859 AVFormatContext *s)
860{
861 int pos = url_ftell(pb);
862 put_be32(pb, 0); /* size */
863 put_tag(pb, "hdlr");
864 put_be32(pb, 0);
865 put_be32(pb, 0);
866 put_tag(pb, "mdir");
867 put_tag(pb, "appl");
868 put_be32(pb, 0);
869 put_be32(pb, 0);
870 put_be16(pb, 0);
871 return updateSize(pb, pos);
872}
873
874/* helper function to write a data tag with the specified string as data */
875static int mov_write_string_data_tag(ByteIOContext *pb, MOVContext* mov,
876 AVFormatContext *s, const char *data)
877{
878 int pos = url_ftell(pb);
879 put_be32(pb, 0); /* size */
880 put_tag(pb, "data");
881 put_be32(pb, 1);
882 put_be32(pb, 0);
883 put_buffer(pb, data, strlen(data));
884 return updateSize(pb, pos);
885}
886
887/* iTunes name of the song/movie */
888static int mov_write_nam_tag(ByteIOContext *pb, MOVContext* mov,
889 AVFormatContext *s)
890{
891 int size = 0;
892 if ( s->title[0] ) {
893 int pos = url_ftell(pb);
894 put_be32(pb, 0); /* size */
895 put_tag(pb, "\251nam");
896 mov_write_string_data_tag(pb, mov, s, s->title);
897 size = updateSize(pb, pos);
898 }
899 return size;
900}
901
902/* iTunes name of the artist/performer */
903static int mov_write_ART_tag(ByteIOContext *pb, MOVContext* mov,
904 AVFormatContext *s)
905{
906 int size = 0;
907 if ( s->author[0] ) {
908 int pos = url_ftell(pb);
909 put_be32(pb, 0); /* size */
910 put_tag(pb, "\251ART");
911 // we use the author here as this is the only thing that we have...
912 mov_write_string_data_tag(pb, mov, s, s->author);
913 size = updateSize(pb, pos);
914 }
915 return size;
916}
917
918/* iTunes name of the writer */
919static int mov_write_wrt_tag(ByteIOContext *pb, MOVContext* mov,
920 AVFormatContext *s)
921{
922 int size = 0;
923 if ( s->author[0] ) {
924 int pos = url_ftell(pb);
925 put_be32(pb, 0); /* size */
926 put_tag(pb, "\251wrt");
927 mov_write_string_data_tag(pb, mov, s, s->author);
928 size = updateSize(pb, pos);
929 }
930 return size;
931}
932
933/* iTunes name of the album */
934static int mov_write_alb_tag(ByteIOContext *pb, MOVContext* mov,
935 AVFormatContext *s)
936{
937 int size = 0;
938 if ( s->album[0] ) {
939 int pos = url_ftell(pb);
940 put_be32(pb, 0); /* size */
941 put_tag(pb, "\251alb");
942 mov_write_string_data_tag(pb, mov, s, s->album);
943 size = updateSize(pb, pos);
944 }
945 return size;
946}
947
948/* iTunes year */
949static int mov_write_day_tag(ByteIOContext *pb, MOVContext* mov,
950 AVFormatContext *s)
951{
952 char year[5];
953 int size = 0;
954 if ( s->year ) {
955 int pos = url_ftell(pb);
956 put_be32(pb, 0); /* size */
957 put_tag(pb, "\251day");
958 snprintf(year, 5, "%04d", s->year);
959 mov_write_string_data_tag(pb, mov, s, year);
960 size = updateSize(pb, pos);
961 }
962 return size;
963}
964
965/* iTunes tool used to create the file */
966static int mov_write_too_tag(ByteIOContext *pb, MOVContext* mov,
967 AVFormatContext *s)
968{
969 int pos = url_ftell(pb);
970 put_be32(pb, 0); /* size */
971 put_tag(pb, "\251too");
972 mov_write_string_data_tag(pb, mov, s, LIBAVFORMAT_IDENT);
973 return updateSize(pb, pos);
974}
975
976/* iTunes comment */
977static int mov_write_cmt_tag(ByteIOContext *pb, MOVContext* mov,
978 AVFormatContext *s)
979{
980 int size = 0;
981 if ( s->comment[0] ) {
982 int pos = url_ftell(pb);
983 put_be32(pb, 0); /* size */
984 put_tag(pb, "\251cmt");
985 mov_write_string_data_tag(pb, mov, s, s->comment);
986 size = updateSize(pb, pos);
987 }
988 return size;
989}
990
991/* iTunes custom genre */
992static int mov_write_gen_tag(ByteIOContext *pb, MOVContext* mov,
993 AVFormatContext *s)
994{
995 int size = 0;
996 if ( s->genre[0] ) {
997 int pos = url_ftell(pb);
998 put_be32(pb, 0); /* size */
999 put_tag(pb, "\251gen");
1000 mov_write_string_data_tag(pb, mov, s, s->genre);
1001 size = updateSize(pb, pos);
1002 }
1003 return size;
1004}
1005
1006/* iTunes track number */
1007static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1008 AVFormatContext *s)
1009{
1010 int size = 0;
1011 if ( s->track ) {
1012 int pos = url_ftell(pb);
1013 put_be32(pb, 0); /* size */
1014 put_tag(pb, "trkn");
1015 {
1016 int pos = url_ftell(pb);
1017 put_be32(pb, 0); /* size */
1018 put_tag(pb, "data");
1019 put_be32(pb, 0); // 8 bytes empty
1020 put_be32(pb, 0);
1021 put_be16(pb, 0); // empty
1022 put_be16(pb, s->track); // track number
1023 put_be16(pb, 0); // total track number
1024 put_be16(pb, 0); // empty
1025 updateSize(pb, pos);
1026 }
1027 size = updateSize(pb, pos);
1028 }
1029 return size;
1030}
1031
1032/* iTunes meta data list */
1033static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1034 AVFormatContext *s)
1035{
1036 int pos = url_ftell(pb);
1037 put_be32(pb, 0); /* size */
1038 put_tag(pb, "ilst");
1039 mov_write_nam_tag(pb, mov, s);
1040 mov_write_ART_tag(pb, mov, s);
1041 mov_write_wrt_tag(pb, mov, s);
1042 mov_write_alb_tag(pb, mov, s);
1043 mov_write_day_tag(pb, mov, s);
1044 mov_write_too_tag(pb, mov, s);
1045 mov_write_cmt_tag(pb, mov, s);
1046 mov_write_gen_tag(pb, mov, s);
1047 mov_write_trkn_tag(pb, mov, s);
1048 return updateSize(pb, pos);
1049}
1050
1051/* iTunes meta data tag */
1052static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1053 AVFormatContext *s)
1054{
1055 int size = 0;
1056
1057 // only save meta tag if required
1058 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1059 s->comment[0] || s->genre[0] || s->track ) {
1060 int pos = url_ftell(pb);
1061 put_be32(pb, 0); /* size */
1062 put_tag(pb, "meta");
1063 put_be32(pb, 0);
1064 mov_write_itunes_hdlr_tag(pb, mov, s);
1065 mov_write_ilst_tag(pb, mov, s);
1066 size = updateSize(pb, pos);
1067 }
1068 return size;
1069}
1070
69dde1ad
GB
1071static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1072 AVFormatContext *s)
1073{
1074 int pos = url_ftell(pb);
1075 int i;
1076
1077 put_be32(pb, 0); /* size */
1078 put_tag(pb, "udta");
1079
b6c50eb1
PB
1080 /* iTunes meta data */
1081 mov_write_meta_tag(pb, mov, s);
1082
69dde1ad
GB
1083 /* Requirements */
1084 for (i=0; i<MAX_STREAMS; i++) {
1085 if(mov->tracks[i].entry <= 0) continue;
1086 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1087 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1088 int pos = url_ftell(pb);
1089 put_be32(pb, 0); /* size */
1090 put_tag(pb, "\251req");
1091 put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
1092 put_be16(pb, 0);
1093 put_buffer(pb, "QuickTime 6.0 or greater",
1094 sizeof("QuickTime 6.0 or greater") - 1);
1095 updateSize(pb, pos);
1096 break;
1097 }
1098 }
1099
1100 /* Encoder */
501866a1 1101 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
69dde1ad
GB
1102 {
1103 int pos = url_ftell(pb);
1104 put_be32(pb, 0); /* size */
1105 put_tag(pb, "\251enc");
1106 put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
1107 put_be16(pb, 0);
1108 put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
1109 updateSize(pb, pos);
1110 }
1111
1112 if( s->title[0] )
1113 {
1114 int pos = url_ftell(pb);
1115 put_be32(pb, 0); /* size */
1116 put_tag(pb, "\251nam");
1117 put_be16(pb, strlen(s->title)); /* string length */
1118 put_be16(pb, 0);
1119 put_buffer(pb, s->title, strlen(s->title));
1120 updateSize(pb, pos);
1121 }
1122
1123 if( s->author[0] )
1124 {
1125 int pos = url_ftell(pb);
1126 put_be32(pb, 0); /* size */
1127 put_tag(pb, /*"\251aut"*/ "\251day" );
1128 put_be16(pb, strlen(s->author)); /* string length */
1129 put_be16(pb, 0);
1130 put_buffer(pb, s->author, strlen(s->author));
1131 updateSize(pb, pos);
1132 }
1133
1134 if( s->comment[0] )
1135 {
1136 int pos = url_ftell(pb);
1137 put_be32(pb, 0); /* size */
1138 put_tag(pb, "\251des");
1139 put_be16(pb, strlen(s->comment)); /* string length */
1140 put_be16(pb, 0);
1141 put_buffer(pb, s->comment, strlen(s->comment));
1142 updateSize(pb, pos);
1143 }
1144
1145 return updateSize(pb, pos);
1146}
1147
1148static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1149 AVFormatContext *s)
1cb5f7fd 1150{
6e6d6dc0 1151 int pos, i;
1cb5f7fd
MN
1152 pos = url_ftell(pb);
1153 put_be32(pb, 0); /* size placeholder*/
1154 put_tag(pb, "moov");
1155 mov->timescale = globalTimescale;
1156
1157 for (i=0; i<MAX_STREAMS; i++) {
e45ccf79
GB
1158 if(mov->tracks[i].entry <= 0) continue;
1159
1160 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
1161 mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
1162 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
1163 }
1164 else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
1165 /* If AMR, track timescale = 8000, AMR_WB = 16000 */
1166 if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
1167 mov->tracks[i].sampleDuration = 160; // Bytes per chunk
1168 mov->tracks[i].timescale = 8000;
1cb5f7fd 1169 }
e45ccf79
GB
1170 else {
1171 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1172 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1cb5f7fd 1173 }
1cb5f7fd 1174 }
e45ccf79
GB
1175
1176 mov->tracks[i].trackDuration =
1177 mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
1178 mov->tracks[i].time = mov->time;
1179 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
1180 }
1181
6e6d6dc0
MN
1182 mov_write_mvhd_tag(pb, mov);
1183 //mov_write_iods_tag(pb, mov);
1cb5f7fd
MN
1184 for (i=0; i<MAX_STREAMS; i++) {
1185 if(mov->tracks[i].entry > 0) {
6e6d6dc0 1186 mov_write_trak_tag(pb, &(mov->tracks[i]));
1cb5f7fd
MN
1187 }
1188 }
1189
69dde1ad
GB
1190 mov_write_udta_tag(pb, mov, s);
1191
6e6d6dc0 1192 return updateSize(pb, pos);
1cb5f7fd
MN
1193}
1194
f578f938 1195int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1cb5f7fd 1196{
f578f938 1197 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
1198 put_be32(pb, 0); /* size placeholder*/
1199 put_tag(pb, "mdat");
1200 return 0;
1201}
1202
1203/* TODO: This needs to be more general */
e45ccf79 1204int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 1205{
69dde1ad
GB
1206 MOVContext *mov = s->priv_data;
1207
1cb5f7fd
MN
1208 put_be32(pb, 0x14 ); /* size */
1209 put_tag(pb, "ftyp");
e45ccf79 1210
69dde1ad 1211 if ( mov->mode == MODE_3GP )
e45ccf79 1212 put_tag(pb, "3gp4");
8af18154 1213 else if ( mov->mode == MODE_PSP )
1214 put_tag(pb, "MSNV");
e45ccf79
GB
1215 else
1216 put_tag(pb, "isom");
1217
1cb5f7fd 1218 put_be32(pb, 0x200 );
e45ccf79 1219
69dde1ad 1220 if ( mov->mode == MODE_3GP )
e45ccf79 1221 put_tag(pb, "3gp4");
8af18154 1222 else if ( mov->mode == MODE_PSP )
1223 put_tag(pb, "MSNV");
e45ccf79
GB
1224 else
1225 put_tag(pb, "mp41");
1226
1cb5f7fd
MN
1227 return 0x14;
1228}
1229
8af18154 1230int mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1231{
1232 MOVContext *mov = s->priv_data;
1233 int AudioRate = s->streams[1]->codec.sample_rate;
1234 int FrameRate = ((s->streams[0]->codec.frame_rate) * (0x10000))/ (s->streams[0]->codec.frame_rate_base);
1235
1236 //printf("audiorate = %d\n",AudioRate);
1237 //printf("framerate = %d / %d = 0x%x\n",s->streams[0]->codec.frame_rate,s->streams[0]->codec.frame_rate_base,FrameRate);
1238
1239 put_be32(pb, 0x94 ); /* size */
1240 put_tag(pb, "uuid");
1241 put_tag(pb, "PROF");
1242
1243 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1244 put_be32(pb, 0xbb88695c );
1245 put_be32(pb, 0xfac9c740 );
1246
1247 put_be32(pb, 0x0 ); /* ? */
1248 put_be32(pb, 0x3 ); /* 3 sections ? */
1249
1250 put_be32(pb, 0x14 ); /* size */
1251 put_tag(pb, "FPRF");
1252 put_be32(pb, 0x0 ); /* ? */
1253 put_be32(pb, 0x0 ); /* ? */
1254 put_be32(pb, 0x0 ); /* ? */
1255
1256 put_be32(pb, 0x2c ); /* size */
1257 put_tag(pb, "APRF"); /* audio */
1258 put_be32(pb, 0x0 );
1259 put_be32(pb, 0x2 );
1260 put_tag(pb, "mp4a");
1261 put_be32(pb, 0x20f );
1262 put_be32(pb, 0x0 );
1263 put_be32(pb, 0x40 );
1264 put_be32(pb, 0x40 );
1265 put_be32(pb, AudioRate ); //24000 ... audio rate?
1266 put_be32(pb, 0x2 );
1267
1268 put_be32(pb, 0x34 ); /* size */
1269 put_tag(pb, "VPRF"); /* video */
1270 put_be32(pb, 0x0 );
1271 put_be32(pb, 0x1 );
1272 put_tag(pb, "mp4v");
1273 put_be32(pb, 0x103 );
1274 put_be32(pb, 0x0 );
1275 put_be32(pb, 0xc0 );
1276 put_be32(pb, 0xc0 );
1277 put_be32(pb, FrameRate); // was 0xefc29
1278 put_be32(pb, FrameRate ); // was 0xefc29
1279 put_be16(pb, s->streams[0]->codec.width);
1280 put_be16(pb, s->streams[0]->codec.height);
1281 put_be32(pb, 0x010001 );
1282
1283 return 0x94;
1284}
1285
1cb5f7fd
MN
1286static int mov_write_header(AVFormatContext *s)
1287{
1288 ByteIOContext *pb = &s->pb;
69dde1ad
GB
1289 MOVContext *mov = s->priv_data;
1290 int i;
1cb5f7fd 1291
ffdd57d4
MN
1292 for(i=0; i<s->nb_streams; i++){
1293 AVCodecContext *c= &s->streams[i]->codec;
1294
1295 if (c->codec_type == CODEC_TYPE_VIDEO){
1296 if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
1297 if(!codec_get_tag(codec_bmp_tags, c->codec_id))
1298 return -1;
1299 else
1300 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
1301 }
1302 }else if(c->codec_type == CODEC_TYPE_AUDIO){
1303 if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
1304 if(!codec_get_tag(codec_wav_tags, c->codec_id))
1305 return -1;
1306 else
1307 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
1308 }
1309 }
1310 }
1311
69dde1ad
GB
1312 /* Default mode == MP4 */
1313 mov->mode = MODE_MP4;
1314
1315 if (s->oformat != NULL) {
1316 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1317 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
8af18154 1318 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
69dde1ad 1319
8af18154 1320 if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 || mov->mode == MODE_PSP )
69dde1ad 1321 mov_write_ftyp_tag(pb,s);
8af18154 1322 if ( mov->mode == MODE_PSP )
1323 mov_write_uuidprof_tag(pb,s);
69dde1ad
GB
1324 }
1325
1326 for (i=0; i<MAX_STREAMS; i++) {
1327 mov->tracks[i].mode = mov->mode;
f578f938
TR
1328 }
1329
1cb5f7fd
MN
1330 put_flush_packet(pb);
1331
1332 return 0;
1333}
1334
e928649b 1335static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd
MN
1336{
1337 MOVContext *mov = s->priv_data;
1338 ByteIOContext *pb = &s->pb;
e928649b
MN
1339 AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
1340 MOVTrack* trk = &mov->tracks[pkt->stream_index];
1a31840c 1341 int cl, id;
e45ccf79 1342 unsigned int samplesInChunk = 0;
e928649b 1343 int size= pkt->size;
1cb5f7fd 1344
e45ccf79
GB
1345 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1346 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 1347
e45ccf79
GB
1348 if (enc->codec_type == CODEC_TYPE_VIDEO ) {
1349 samplesInChunk = 1;
1350 }
1351 else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
1352 if( enc->codec_id == CODEC_ID_AMR_NB) {
f578f938 1353 /* We must find out how many AMR blocks there are in one packet */
e45ccf79
GB
1354 static uint16_t packed_size[16] =
1355 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1356 int len = 0;
1357
1358 while (len < size && samplesInChunk < 100) {
e928649b 1359 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
e45ccf79 1360 samplesInChunk++;
f578f938 1361 }
1cb5f7fd 1362 }
e45ccf79
GB
1363 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
1364 samplesInChunk = size/enc->channels;
1cb5f7fd 1365 }
9a4d9388
RS
1366 else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
1367 samplesInChunk = size/(2*enc->channels);
1368 }
e45ccf79
GB
1369 else {
1370 samplesInChunk = 1;
1cb5f7fd 1371 }
e45ccf79
GB
1372 }
1373
1374 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
1375 && trk->vosLen == 0) {
21f52609 1376// assert(enc->extradata_size);
e45ccf79
GB
1377
1378 trk->vosLen = enc->extradata_size;
1379 trk->vosData = av_malloc(trk->vosLen);
1380 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1381 }
1382
1383 cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
1384 id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
1385
1386 if (trk->ents_allocated <= trk->entry) {
1387 trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
1388 if (!trk->cluster)
1389 return -1;
1390 trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
1391 if (!trk->cluster[cl])
1392 return -1;
1393 trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
1394 }
1395 if (mov->mdat_written == 0) {
1396 mov_write_mdat_tag(pb, mov);
1397 mov->mdat_written = 1;
21f52609 1398 mov->time = s->timestamp;
e45ccf79
GB
1399 }
1400
69dde1ad 1401 trk->cluster[cl][id].pos = url_ftell(pb);
e45ccf79
GB
1402 trk->cluster[cl][id].samplesInChunk = samplesInChunk;
1403 trk->cluster[cl][id].size = size;
1404 trk->cluster[cl][id].entries = samplesInChunk;
1405 if(enc->codec_type == CODEC_TYPE_VIDEO) {
e928649b
MN
1406 trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1407 if(trk->cluster[cl][id].key_frame)
f578f938 1408 trk->hasKeyframes = 1;
1cb5f7fd 1409 }
e45ccf79
GB
1410 trk->enc = enc;
1411 trk->entry++;
1412 trk->sampleCount += samplesInChunk;
1413 trk->mdat_size += size;
1414
e928649b 1415 put_buffer(pb, pkt->data, size);
1cb5f7fd
MN
1416
1417 put_flush_packet(pb);
1418 return 0;
1419}
1420
1421static int mov_write_trailer(AVFormatContext *s)
1422{
1423 MOVContext *mov = s->priv_data;
1424 ByteIOContext *pb = &s->pb;
1425 int res = 0;
1426 int i, j;
1cb5f7fd 1427
69dde1ad 1428 offset_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1429
1430 /* Write size of mdat tag */
69dde1ad 1431 for (i=0, j=0; i<MAX_STREAMS; i++) {
1cb5f7fd
MN
1432 if(mov->tracks[i].ents_allocated > 0) {
1433 j += mov->tracks[i].mdat_size;
1434 }
1435 }
f578f938 1436 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1cb5f7fd 1437 put_be32(pb, j+8);
69dde1ad 1438 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1439
69dde1ad 1440 mov_write_moov_tag(pb, mov, s);
1cb5f7fd
MN
1441
1442 for (i=0; i<MAX_STREAMS; i++) {
1443 for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
1444 av_free(mov->tracks[i].cluster[j]);
1445 }
1446 av_free(mov->tracks[i].cluster);
ec7d0d2e
GB
1447 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1448
1cb5f7fd
MN
1449 mov->tracks[i].cluster = NULL;
1450 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1451 }
69dde1ad 1452
1cb5f7fd
MN
1453 put_flush_packet(pb);
1454
1455 return res;
1456}
1457
1458static AVOutputFormat mov_oformat = {
1459 "mov",
1460 "mov format",
1461 NULL,
1462 "mov",
1463 sizeof(MOVContext),
69dde1ad 1464 CODEC_ID_AAC,
2187d948 1465 CODEC_ID_MPEG4,
1cb5f7fd
MN
1466 mov_write_header,
1467 mov_write_packet,
1468 mov_write_trailer,
1469};
1470
1471static AVOutputFormat _3gp_oformat = {
1472 "3gp",
1473 "3gp format",
1474 NULL,
1475 "3gp",
1476 sizeof(MOVContext),
1477 CODEC_ID_AMR_NB,
1478 CODEC_ID_H263,
1479 mov_write_header,
1480 mov_write_packet,
1481 mov_write_trailer,
1482};
1483
1484static AVOutputFormat mp4_oformat = {
1485 "mp4",
1486 "mp4 format",
4cb3f3b6
DC
1487 "application/mp4",
1488 "mp4,m4a",
1cb5f7fd
MN
1489 sizeof(MOVContext),
1490 CODEC_ID_AAC,
1491 CODEC_ID_MPEG4,
1492 mov_write_header,
1493 mov_write_packet,
1494 mov_write_trailer,
1495};
1496
8af18154 1497static AVOutputFormat psp_oformat = {
1498 "psp",
1499 "psp mp4 format",
1500 NULL,
1501 "mp4,psp",
1502 sizeof(MOVContext),
1503 CODEC_ID_AAC,
1504 CODEC_ID_MPEG4,
1505 mov_write_header,
1506 mov_write_packet,
1507 mov_write_trailer,
1508};
1509
1cb5f7fd
MN
1510int movenc_init(void)
1511{
1512 av_register_output_format(&mov_oformat);
1513 av_register_output_format(&_3gp_oformat);
1514 av_register_output_format(&mp4_oformat);
8af18154 1515 av_register_output_format(&psp_oformat);
1cb5f7fd
MN
1516 return 0;
1517}