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