quick patch for adding 3g2 support to ffmpeg (muxer and demuxer). No movie fragment...
[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
796/* TODO: Not sorted out, but not necessary either */
6e6d6dc0 797static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
798{
799 put_be32(pb, 0x15); /* size */
800 put_tag(pb, "iods");
801 put_be32(pb, 0); /* version & flags */
802 put_be16(pb, 0x1007);
803 put_byte(pb, 0);
804 put_be16(pb, 0x4fff);
805 put_be16(pb, 0xfffe);
806 put_be16(pb, 0x01ff);
807 return 0x15;
808}
809
6e6d6dc0 810static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
811{
812 int maxTrackID = 1, maxTrackLen = 0, i;
f578f938 813 int64_t maxTrackLenTemp;
1cb5f7fd
MN
814
815 put_be32(pb, 0x6c); /* size (always 0x6c) */
816 put_tag(pb, "mvhd");
817 put_be32(pb, 0); /* version & flags */
818 put_be32(pb, mov->time); /* creation time */
819 put_be32(pb, mov->time); /* modification time */
820 put_be32(pb, mov->timescale); /* timescale */
821 for (i=0; i<MAX_STREAMS; i++) {
822 if(mov->tracks[i].entry > 0) {
f578f938
TR
823 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
824 if(maxTrackLen < maxTrackLenTemp)
825 maxTrackLen = maxTrackLenTemp;
1cb5f7fd
MN
826 if(maxTrackID < mov->tracks[i].trackID)
827 maxTrackID = mov->tracks[i].trackID;
828 }
829 }
830 put_be32(pb, maxTrackLen); /* duration of longest track */
831
832 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
833 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
834 put_be16(pb, 0); /* reserved */
835 put_be32(pb, 0); /* reserved */
836 put_be32(pb, 0); /* reserved */
837
838 /* Matrix structure */
839 put_be32(pb, 0x00010000); /* reserved */
840 put_be32(pb, 0x0); /* reserved */
841 put_be32(pb, 0x0); /* reserved */
842 put_be32(pb, 0x0); /* reserved */
843 put_be32(pb, 0x00010000); /* reserved */
844 put_be32(pb, 0x0); /* reserved */
845 put_be32(pb, 0x0); /* reserved */
846 put_be32(pb, 0x0); /* reserved */
847 put_be32(pb, 0x40000000); /* reserved */
848
849 put_be32(pb, 0); /* reserved (preview time) */
850 put_be32(pb, 0); /* reserved (preview duration) */
851 put_be32(pb, 0); /* reserved (poster time) */
852 put_be32(pb, 0); /* reserved (selection time) */
853 put_be32(pb, 0); /* reserved (selection duration) */
854 put_be32(pb, 0); /* reserved (current time) */
855 put_be32(pb, maxTrackID+1); /* Next track id */
856 return 0x6c;
857}
858
b6c50eb1
PB
859static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
860 AVFormatContext *s)
861{
862 int pos = url_ftell(pb);
863 put_be32(pb, 0); /* size */
864 put_tag(pb, "hdlr");
865 put_be32(pb, 0);
866 put_be32(pb, 0);
867 put_tag(pb, "mdir");
868 put_tag(pb, "appl");
869 put_be32(pb, 0);
870 put_be32(pb, 0);
871 put_be16(pb, 0);
872 return updateSize(pb, pos);
873}
874
875/* helper function to write a data tag with the specified string as data */
876static int mov_write_string_data_tag(ByteIOContext *pb, MOVContext* mov,
877 AVFormatContext *s, const char *data)
878{
879 int pos = url_ftell(pb);
880 put_be32(pb, 0); /* size */
881 put_tag(pb, "data");
882 put_be32(pb, 1);
883 put_be32(pb, 0);
884 put_buffer(pb, data, strlen(data));
885 return updateSize(pb, pos);
886}
887
888/* iTunes name of the song/movie */
889static int mov_write_nam_tag(ByteIOContext *pb, MOVContext* mov,
890 AVFormatContext *s)
891{
892 int size = 0;
893 if ( s->title[0] ) {
894 int pos = url_ftell(pb);
895 put_be32(pb, 0); /* size */
896 put_tag(pb, "\251nam");
897 mov_write_string_data_tag(pb, mov, s, s->title);
898 size = updateSize(pb, pos);
899 }
900 return size;
901}
902
903/* iTunes name of the artist/performer */
904static int mov_write_ART_tag(ByteIOContext *pb, MOVContext* mov,
905 AVFormatContext *s)
906{
907 int size = 0;
908 if ( s->author[0] ) {
909 int pos = url_ftell(pb);
910 put_be32(pb, 0); /* size */
911 put_tag(pb, "\251ART");
912 // we use the author here as this is the only thing that we have...
913 mov_write_string_data_tag(pb, mov, s, s->author);
914 size = updateSize(pb, pos);
915 }
916 return size;
917}
918
919/* iTunes name of the writer */
920static int mov_write_wrt_tag(ByteIOContext *pb, MOVContext* mov,
921 AVFormatContext *s)
922{
923 int size = 0;
924 if ( s->author[0] ) {
925 int pos = url_ftell(pb);
926 put_be32(pb, 0); /* size */
927 put_tag(pb, "\251wrt");
928 mov_write_string_data_tag(pb, mov, s, s->author);
929 size = updateSize(pb, pos);
930 }
931 return size;
932}
933
934/* iTunes name of the album */
935static int mov_write_alb_tag(ByteIOContext *pb, MOVContext* mov,
936 AVFormatContext *s)
937{
938 int size = 0;
939 if ( s->album[0] ) {
940 int pos = url_ftell(pb);
941 put_be32(pb, 0); /* size */
942 put_tag(pb, "\251alb");
943 mov_write_string_data_tag(pb, mov, s, s->album);
944 size = updateSize(pb, pos);
945 }
946 return size;
947}
948
949/* iTunes year */
950static int mov_write_day_tag(ByteIOContext *pb, MOVContext* mov,
951 AVFormatContext *s)
952{
953 char year[5];
954 int size = 0;
955 if ( s->year ) {
956 int pos = url_ftell(pb);
957 put_be32(pb, 0); /* size */
958 put_tag(pb, "\251day");
959 snprintf(year, 5, "%04d", s->year);
960 mov_write_string_data_tag(pb, mov, s, year);
961 size = updateSize(pb, pos);
962 }
963 return size;
964}
965
966/* iTunes tool used to create the file */
967static int mov_write_too_tag(ByteIOContext *pb, MOVContext* mov,
968 AVFormatContext *s)
969{
970 int pos = url_ftell(pb);
971 put_be32(pb, 0); /* size */
972 put_tag(pb, "\251too");
973 mov_write_string_data_tag(pb, mov, s, LIBAVFORMAT_IDENT);
974 return updateSize(pb, pos);
975}
976
977/* iTunes comment */
978static int mov_write_cmt_tag(ByteIOContext *pb, MOVContext* mov,
979 AVFormatContext *s)
980{
981 int size = 0;
982 if ( s->comment[0] ) {
983 int pos = url_ftell(pb);
984 put_be32(pb, 0); /* size */
985 put_tag(pb, "\251cmt");
986 mov_write_string_data_tag(pb, mov, s, s->comment);
987 size = updateSize(pb, pos);
988 }
989 return size;
990}
991
992/* iTunes custom genre */
993static int mov_write_gen_tag(ByteIOContext *pb, MOVContext* mov,
994 AVFormatContext *s)
995{
996 int size = 0;
997 if ( s->genre[0] ) {
998 int pos = url_ftell(pb);
999 put_be32(pb, 0); /* size */
1000 put_tag(pb, "\251gen");
1001 mov_write_string_data_tag(pb, mov, s, s->genre);
1002 size = updateSize(pb, pos);
1003 }
1004 return size;
1005}
1006
1007/* iTunes track number */
1008static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1009 AVFormatContext *s)
1010{
1011 int size = 0;
1012 if ( s->track ) {
1013 int pos = url_ftell(pb);
1014 put_be32(pb, 0); /* size */
1015 put_tag(pb, "trkn");
1016 {
1017 int pos = url_ftell(pb);
1018 put_be32(pb, 0); /* size */
1019 put_tag(pb, "data");
1020 put_be32(pb, 0); // 8 bytes empty
1021 put_be32(pb, 0);
1022 put_be16(pb, 0); // empty
1023 put_be16(pb, s->track); // track number
1024 put_be16(pb, 0); // total track number
1025 put_be16(pb, 0); // empty
1026 updateSize(pb, pos);
1027 }
1028 size = updateSize(pb, pos);
1029 }
1030 return size;
1031}
1032
1033/* iTunes meta data list */
1034static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1035 AVFormatContext *s)
1036{
1037 int pos = url_ftell(pb);
1038 put_be32(pb, 0); /* size */
1039 put_tag(pb, "ilst");
1040 mov_write_nam_tag(pb, mov, s);
1041 mov_write_ART_tag(pb, mov, s);
1042 mov_write_wrt_tag(pb, mov, s);
1043 mov_write_alb_tag(pb, mov, s);
1044 mov_write_day_tag(pb, mov, s);
1045 mov_write_too_tag(pb, mov, s);
1046 mov_write_cmt_tag(pb, mov, s);
1047 mov_write_gen_tag(pb, mov, s);
1048 mov_write_trkn_tag(pb, mov, s);
1049 return updateSize(pb, pos);
1050}
1051
1052/* iTunes meta data tag */
1053static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1054 AVFormatContext *s)
1055{
1056 int size = 0;
1057
1058 // only save meta tag if required
1059 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1060 s->comment[0] || s->genre[0] || s->track ) {
1061 int pos = url_ftell(pb);
1062 put_be32(pb, 0); /* size */
1063 put_tag(pb, "meta");
1064 put_be32(pb, 0);
1065 mov_write_itunes_hdlr_tag(pb, mov, s);
1066 mov_write_ilst_tag(pb, mov, s);
1067 size = updateSize(pb, pos);
1068 }
1069 return size;
1070}
1071
69dde1ad
GB
1072static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1073 AVFormatContext *s)
1074{
1075 int pos = url_ftell(pb);
1076 int i;
1077
1078 put_be32(pb, 0); /* size */
1079 put_tag(pb, "udta");
1080
b6c50eb1
PB
1081 /* iTunes meta data */
1082 mov_write_meta_tag(pb, mov, s);
1083
69dde1ad
GB
1084 /* Requirements */
1085 for (i=0; i<MAX_STREAMS; i++) {
1086 if(mov->tracks[i].entry <= 0) continue;
1087 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1088 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1089 int pos = url_ftell(pb);
1090 put_be32(pb, 0); /* size */
1091 put_tag(pb, "\251req");
1092 put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
1093 put_be16(pb, 0);
1094 put_buffer(pb, "QuickTime 6.0 or greater",
1095 sizeof("QuickTime 6.0 or greater") - 1);
1096 updateSize(pb, pos);
1097 break;
1098 }
1099 }
1100
1101 /* Encoder */
501866a1 1102 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
69dde1ad
GB
1103 {
1104 int pos = url_ftell(pb);
1105 put_be32(pb, 0); /* size */
1106 put_tag(pb, "\251enc");
1107 put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
1108 put_be16(pb, 0);
1109 put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
1110 updateSize(pb, pos);
1111 }
1112
1113 if( s->title[0] )
1114 {
1115 int pos = url_ftell(pb);
1116 put_be32(pb, 0); /* size */
1117 put_tag(pb, "\251nam");
1118 put_be16(pb, strlen(s->title)); /* string length */
1119 put_be16(pb, 0);
1120 put_buffer(pb, s->title, strlen(s->title));
1121 updateSize(pb, pos);
1122 }
1123
1124 if( s->author[0] )
1125 {
1126 int pos = url_ftell(pb);
1127 put_be32(pb, 0); /* size */
1128 put_tag(pb, /*"\251aut"*/ "\251day" );
1129 put_be16(pb, strlen(s->author)); /* string length */
1130 put_be16(pb, 0);
1131 put_buffer(pb, s->author, strlen(s->author));
1132 updateSize(pb, pos);
1133 }
1134
1135 if( s->comment[0] )
1136 {
1137 int pos = url_ftell(pb);
1138 put_be32(pb, 0); /* size */
1139 put_tag(pb, "\251des");
1140 put_be16(pb, strlen(s->comment)); /* string length */
1141 put_be16(pb, 0);
1142 put_buffer(pb, s->comment, strlen(s->comment));
1143 updateSize(pb, pos);
1144 }
1145
1146 return updateSize(pb, pos);
1147}
1148
1149static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1150 AVFormatContext *s)
1cb5f7fd 1151{
6e6d6dc0 1152 int pos, i;
1cb5f7fd
MN
1153 pos = url_ftell(pb);
1154 put_be32(pb, 0); /* size placeholder*/
1155 put_tag(pb, "moov");
1156 mov->timescale = globalTimescale;
1157
1158 for (i=0; i<MAX_STREAMS; i++) {
e45ccf79
GB
1159 if(mov->tracks[i].entry <= 0) continue;
1160
1161 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
1162 mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
1163 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
1164 }
1165 else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
1166 /* If AMR, track timescale = 8000, AMR_WB = 16000 */
1167 if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
1168 mov->tracks[i].sampleDuration = 160; // Bytes per chunk
1169 mov->tracks[i].timescale = 8000;
1cb5f7fd 1170 }
e45ccf79
GB
1171 else {
1172 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1173 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1cb5f7fd 1174 }
1cb5f7fd 1175 }
e45ccf79
GB
1176
1177 mov->tracks[i].trackDuration =
1178 mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
1179 mov->tracks[i].time = mov->time;
1180 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
1181 }
1182
6e6d6dc0
MN
1183 mov_write_mvhd_tag(pb, mov);
1184 //mov_write_iods_tag(pb, mov);
1cb5f7fd
MN
1185 for (i=0; i<MAX_STREAMS; i++) {
1186 if(mov->tracks[i].entry > 0) {
6e6d6dc0 1187 mov_write_trak_tag(pb, &(mov->tracks[i]));
1cb5f7fd
MN
1188 }
1189 }
1190
69dde1ad
GB
1191 mov_write_udta_tag(pb, mov, s);
1192
6e6d6dc0 1193 return updateSize(pb, pos);
1cb5f7fd
MN
1194}
1195
f578f938 1196int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1cb5f7fd 1197{
f578f938 1198 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
1199 put_be32(pb, 0); /* size placeholder*/
1200 put_tag(pb, "mdat");
1201 return 0;
1202}
1203
1204/* TODO: This needs to be more general */
e45ccf79 1205int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 1206{
69dde1ad
GB
1207 MOVContext *mov = s->priv_data;
1208
1cb5f7fd
MN
1209 put_be32(pb, 0x14 ); /* size */
1210 put_tag(pb, "ftyp");
e45ccf79 1211
69dde1ad 1212 if ( mov->mode == MODE_3GP )
e45ccf79 1213 put_tag(pb, "3gp4");
8536ab89 1214 else if ( mov->mode == MODE_3G2 )
1215 put_tag(pb, "3g2a");
8af18154 1216 else if ( mov->mode == MODE_PSP )
1217 put_tag(pb, "MSNV");
e45ccf79
GB
1218 else
1219 put_tag(pb, "isom");
1220
1cb5f7fd 1221 put_be32(pb, 0x200 );
e45ccf79 1222
69dde1ad 1223 if ( mov->mode == MODE_3GP )
e45ccf79 1224 put_tag(pb, "3gp4");
8536ab89 1225 else if ( mov->mode == MODE_3G2 )
1226 put_tag(pb, "3g2a");
8af18154 1227 else if ( mov->mode == MODE_PSP )
1228 put_tag(pb, "MSNV");
e45ccf79
GB
1229 else
1230 put_tag(pb, "mp41");
1231
1cb5f7fd
MN
1232 return 0x14;
1233}
1234
0c716ab7 1235static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
8af18154 1236{
8af18154 1237 int AudioRate = s->streams[1]->codec.sample_rate;
1238 int FrameRate = ((s->streams[0]->codec.frame_rate) * (0x10000))/ (s->streams[0]->codec.frame_rate_base);
1239
1240 //printf("audiorate = %d\n",AudioRate);
1241 //printf("framerate = %d / %d = 0x%x\n",s->streams[0]->codec.frame_rate,s->streams[0]->codec.frame_rate_base,FrameRate);
1242
1243 put_be32(pb, 0x94 ); /* size */
1244 put_tag(pb, "uuid");
1245 put_tag(pb, "PROF");
1246
1247 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1248 put_be32(pb, 0xbb88695c );
1249 put_be32(pb, 0xfac9c740 );
1250
1251 put_be32(pb, 0x0 ); /* ? */
1252 put_be32(pb, 0x3 ); /* 3 sections ? */
1253
1254 put_be32(pb, 0x14 ); /* size */
1255 put_tag(pb, "FPRF");
1256 put_be32(pb, 0x0 ); /* ? */
1257 put_be32(pb, 0x0 ); /* ? */
1258 put_be32(pb, 0x0 ); /* ? */
1259
1260 put_be32(pb, 0x2c ); /* size */
1261 put_tag(pb, "APRF"); /* audio */
1262 put_be32(pb, 0x0 );
1263 put_be32(pb, 0x2 );
1264 put_tag(pb, "mp4a");
1265 put_be32(pb, 0x20f );
1266 put_be32(pb, 0x0 );
1267 put_be32(pb, 0x40 );
1268 put_be32(pb, 0x40 );
1269 put_be32(pb, AudioRate ); //24000 ... audio rate?
1270 put_be32(pb, 0x2 );
1271
1272 put_be32(pb, 0x34 ); /* size */
1273 put_tag(pb, "VPRF"); /* video */
1274 put_be32(pb, 0x0 );
1275 put_be32(pb, 0x1 );
1276 put_tag(pb, "mp4v");
1277 put_be32(pb, 0x103 );
1278 put_be32(pb, 0x0 );
1279 put_be32(pb, 0xc0 );
1280 put_be32(pb, 0xc0 );
1281 put_be32(pb, FrameRate); // was 0xefc29
1282 put_be32(pb, FrameRate ); // was 0xefc29
1283 put_be16(pb, s->streams[0]->codec.width);
1284 put_be16(pb, s->streams[0]->codec.height);
1285 put_be32(pb, 0x010001 );
8af18154 1286}
1287
1cb5f7fd
MN
1288static int mov_write_header(AVFormatContext *s)
1289{
1290 ByteIOContext *pb = &s->pb;
69dde1ad
GB
1291 MOVContext *mov = s->priv_data;
1292 int i;
1cb5f7fd 1293
ffdd57d4
MN
1294 for(i=0; i<s->nb_streams; i++){
1295 AVCodecContext *c= &s->streams[i]->codec;
1296
1297 if (c->codec_type == CODEC_TYPE_VIDEO){
1298 if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
1299 if(!codec_get_tag(codec_bmp_tags, c->codec_id))
1300 return -1;
1301 else
1302 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
1303 }
1304 }else if(c->codec_type == CODEC_TYPE_AUDIO){
1305 if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
1306 if(!codec_get_tag(codec_wav_tags, c->codec_id))
1307 return -1;
1308 else
1309 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
1310 }
1311 }
1312 }
1313
69dde1ad
GB
1314 /* Default mode == MP4 */
1315 mov->mode = MODE_MP4;
1316
1317 if (s->oformat != NULL) {
1318 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
8536ab89 1319 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
69dde1ad 1320 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
8af18154 1321 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
69dde1ad 1322
8536ab89 1323 if ( mov->mode == MODE_3GP || mov->mode == MODE_3G2 ||
1324 mov->mode == MODE_MP4 || mov->mode == MODE_PSP )
69dde1ad 1325 mov_write_ftyp_tag(pb,s);
0c716ab7
FR
1326 if ( mov->mode == MODE_PSP ) {
1327 if ( s->nb_streams != 2 ) {
1328 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1329 return -1;
1330 }
8af18154 1331 mov_write_uuidprof_tag(pb,s);
0c716ab7 1332 }
69dde1ad
GB
1333 }
1334
1335 for (i=0; i<MAX_STREAMS; i++) {
1336 mov->tracks[i].mode = mov->mode;
f578f938
TR
1337 }
1338
1cb5f7fd
MN
1339 put_flush_packet(pb);
1340
1341 return 0;
1342}
1343
e928649b 1344static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd
MN
1345{
1346 MOVContext *mov = s->priv_data;
1347 ByteIOContext *pb = &s->pb;
e928649b
MN
1348 AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
1349 MOVTrack* trk = &mov->tracks[pkt->stream_index];
1a31840c 1350 int cl, id;
e45ccf79 1351 unsigned int samplesInChunk = 0;
e928649b 1352 int size= pkt->size;
1cb5f7fd 1353
e45ccf79
GB
1354 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1355 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 1356
e45ccf79
GB
1357 if (enc->codec_type == CODEC_TYPE_VIDEO ) {
1358 samplesInChunk = 1;
1359 }
1360 else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
1361 if( enc->codec_id == CODEC_ID_AMR_NB) {
f578f938 1362 /* We must find out how many AMR blocks there are in one packet */
e45ccf79
GB
1363 static uint16_t packed_size[16] =
1364 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1365 int len = 0;
1366
1367 while (len < size && samplesInChunk < 100) {
e928649b 1368 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
e45ccf79 1369 samplesInChunk++;
f578f938 1370 }
1cb5f7fd 1371 }
e45ccf79
GB
1372 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
1373 samplesInChunk = size/enc->channels;
1cb5f7fd 1374 }
9a4d9388
RS
1375 else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
1376 samplesInChunk = size/(2*enc->channels);
1377 }
e45ccf79
GB
1378 else {
1379 samplesInChunk = 1;
1cb5f7fd 1380 }
e45ccf79
GB
1381 }
1382
1383 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
1384 && trk->vosLen == 0) {
21f52609 1385// assert(enc->extradata_size);
e45ccf79
GB
1386
1387 trk->vosLen = enc->extradata_size;
1388 trk->vosData = av_malloc(trk->vosLen);
1389 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1390 }
1391
1392 cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
1393 id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
1394
1395 if (trk->ents_allocated <= trk->entry) {
1396 trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
1397 if (!trk->cluster)
1398 return -1;
1399 trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
1400 if (!trk->cluster[cl])
1401 return -1;
1402 trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
1403 }
1404 if (mov->mdat_written == 0) {
1405 mov_write_mdat_tag(pb, mov);
1406 mov->mdat_written = 1;
21f52609 1407 mov->time = s->timestamp;
e45ccf79
GB
1408 }
1409
69dde1ad 1410 trk->cluster[cl][id].pos = url_ftell(pb);
e45ccf79
GB
1411 trk->cluster[cl][id].samplesInChunk = samplesInChunk;
1412 trk->cluster[cl][id].size = size;
1413 trk->cluster[cl][id].entries = samplesInChunk;
1414 if(enc->codec_type == CODEC_TYPE_VIDEO) {
e928649b
MN
1415 trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1416 if(trk->cluster[cl][id].key_frame)
f578f938 1417 trk->hasKeyframes = 1;
1cb5f7fd 1418 }
e45ccf79
GB
1419 trk->enc = enc;
1420 trk->entry++;
1421 trk->sampleCount += samplesInChunk;
1422 trk->mdat_size += size;
1423
e928649b 1424 put_buffer(pb, pkt->data, size);
1cb5f7fd
MN
1425
1426 put_flush_packet(pb);
1427 return 0;
1428}
1429
1430static int mov_write_trailer(AVFormatContext *s)
1431{
1432 MOVContext *mov = s->priv_data;
1433 ByteIOContext *pb = &s->pb;
1434 int res = 0;
1435 int i, j;
1cb5f7fd 1436
69dde1ad 1437 offset_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1438
1439 /* Write size of mdat tag */
69dde1ad 1440 for (i=0, j=0; i<MAX_STREAMS; i++) {
1cb5f7fd
MN
1441 if(mov->tracks[i].ents_allocated > 0) {
1442 j += mov->tracks[i].mdat_size;
1443 }
1444 }
f578f938 1445 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1cb5f7fd 1446 put_be32(pb, j+8);
69dde1ad 1447 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1448
69dde1ad 1449 mov_write_moov_tag(pb, mov, s);
1cb5f7fd
MN
1450
1451 for (i=0; i<MAX_STREAMS; i++) {
1452 for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
1453 av_free(mov->tracks[i].cluster[j]);
1454 }
1455 av_free(mov->tracks[i].cluster);
ec7d0d2e
GB
1456 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1457
1cb5f7fd
MN
1458 mov->tracks[i].cluster = NULL;
1459 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1460 }
69dde1ad 1461
1cb5f7fd
MN
1462 put_flush_packet(pb);
1463
1464 return res;
1465}
1466
1467static AVOutputFormat mov_oformat = {
1468 "mov",
1469 "mov format",
1470 NULL,
1471 "mov",
1472 sizeof(MOVContext),
69dde1ad 1473 CODEC_ID_AAC,
2187d948 1474 CODEC_ID_MPEG4,
1cb5f7fd
MN
1475 mov_write_header,
1476 mov_write_packet,
1477 mov_write_trailer,
1478};
1479
1480static AVOutputFormat _3gp_oformat = {
1481 "3gp",
1482 "3gp format",
1483 NULL,
1484 "3gp",
1485 sizeof(MOVContext),
1486 CODEC_ID_AMR_NB,
1487 CODEC_ID_H263,
1488 mov_write_header,
1489 mov_write_packet,
1490 mov_write_trailer,
1491};
1492
1493static AVOutputFormat mp4_oformat = {
1494 "mp4",
1495 "mp4 format",
4cb3f3b6
DC
1496 "application/mp4",
1497 "mp4,m4a",
1cb5f7fd
MN
1498 sizeof(MOVContext),
1499 CODEC_ID_AAC,
1500 CODEC_ID_MPEG4,
1501 mov_write_header,
1502 mov_write_packet,
1503 mov_write_trailer,
1504};
1505
8af18154 1506static AVOutputFormat psp_oformat = {
1507 "psp",
1508 "psp mp4 format",
1509 NULL,
1510 "mp4,psp",
1511 sizeof(MOVContext),
1512 CODEC_ID_AAC,
1513 CODEC_ID_MPEG4,
1514 mov_write_header,
1515 mov_write_packet,
1516 mov_write_trailer,
1517};
1518
8536ab89 1519static AVOutputFormat _3g2_oformat = {
1520 "3g2",
1521 "3gp2 format",
1522 NULL,
1523 "3g2",
1524 sizeof(MOVContext),
1525 CODEC_ID_AMR_NB,
1526 CODEC_ID_H263,
1527 mov_write_header,
1528 mov_write_packet,
1529 mov_write_trailer,
1530};
1531
1cb5f7fd
MN
1532int movenc_init(void)
1533{
1534 av_register_output_format(&mov_oformat);
1535 av_register_output_format(&_3gp_oformat);
1536 av_register_output_format(&mp4_oformat);
8af18154 1537 av_register_output_format(&psp_oformat);
8536ab89 1538 av_register_output_format(&_3g2_oformat);
1cb5f7fd
MN
1539 return 0;
1540}