quick patch for adding 3g2 support to ffmpeg (muxer and demuxer). No movie fragment...
[libav.git] / libavformat / movenc.c
1 /*
2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
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"
21 #include "avi.h"
22 #include "avio.h"
23
24 #undef NDEBUG
25 #include <assert.h>
26
27 #define MOV_INDEX_CLUSTER_SIZE 16384
28 #define globalTimescale 1000
29
30 #define MODE_MP4 0
31 #define MODE_MOV 1
32 #define MODE_3GP 2
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
35 #define MODE_3G2 4
36
37 typedef struct MOVIentry {
38 unsigned int flags, pos, size;
39 unsigned int samplesInChunk;
40 char key_frame;
41 unsigned int entries;
42 } MOVIentry;
43
44 typedef struct MOVIndex {
45 int mode;
46 int entry;
47 int mdat_size;
48 int ents_allocated;
49 long timescale;
50 long time;
51 long trackDuration;
52 long sampleCount;
53 long sampleDuration;
54 int hasKeyframes;
55 int trackID;
56 AVCodecContext *enc;
57
58 int vosLen;
59 uint8_t *vosData;
60 MOVIentry** cluster;
61 } MOVTrack;
62
63 typedef struct MOVContext {
64 int mode;
65 long time;
66 int nb_streams;
67 int mdat_written;
68 offset_t mdat_pos;
69 long timescale;
70 MOVTrack tracks[MAX_STREAMS];
71 } MOVContext;
72
73 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
74
75 //FIXME supprt 64bit varaint with wide placeholders
76 static int updateSize (ByteIOContext *pb, int pos)
77 {
78 long curpos = url_ftell(pb);
79 url_fseek(pb, pos, SEEK_SET);
80 put_be32(pb, curpos - pos); /* rewrite size */
81 url_fseek(pb, curpos, SEEK_SET);
82
83 return curpos - pos;
84 }
85
86 /* Chunk offset atom */
87 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
88 {
89 int i;
90 int pos = url_ftell(pb);
91 put_be32(pb, 0); /* size */
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 }
100 return updateSize (pb, pos);
101 }
102
103 /* Sample size atom */
104 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
105 {
106 int equalChunks = 1;
107 int i, j, entries = 0, tst = -1, oldtst = -1;
108
109 int pos = url_ftell(pb);
110 put_be32(pb, 0); /* size */
111 put_tag(pb, "stsz");
112 put_be32(pb, 0); /* version & flags */
113
114 for (i=0; i<track->entry; i++) {
115 int cl = i / MOV_INDEX_CLUSTER_SIZE;
116 int id = i % MOV_INDEX_CLUSTER_SIZE;
117 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
118 if(oldtst != -1 && tst != oldtst) {
119 equalChunks = 0;
120 }
121 oldtst = tst;
122 entries += track->cluster[cl][id].entries;
123 }
124 if (equalChunks) {
125 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
126 put_be32(pb, sSize); // sample size
127 put_be32(pb, entries); // sample count
128 }
129 else {
130 put_be32(pb, 0); // sample size
131 put_be32(pb, entries); // sample count
132 for (i=0; i<track->entry; i++) {
133 int cl = i / MOV_INDEX_CLUSTER_SIZE;
134 int id = i % MOV_INDEX_CLUSTER_SIZE;
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 }
139 }
140 }
141 return updateSize (pb, pos);
142 }
143
144 /* Sample to chunk atom */
145 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
146 {
147 int index = 0, oldval = -1, i, entryPos, curpos;
148
149 int pos = url_ftell(pb);
150 put_be32(pb, 0); /* size */
151 put_tag(pb, "stsc");
152 put_be32(pb, 0); // version & flags
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;
158 if(oldval != track->cluster[cl][id].samplesInChunk)
159 {
160 put_be32(pb, i+1); // first chunk
161 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
162 put_be32(pb, 0x1); // sample description index
163 oldval = track->cluster[cl][id].samplesInChunk;
164 index++;
165 }
166 }
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);
171
172 return updateSize (pb, pos);
173 }
174
175 /* Sync sample atom */
176 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
177 {
178 long curpos;
179 int i, index = 0, entryPos;
180 int pos = url_ftell(pb);
181 put_be32(pb, 0); // size
182 put_tag(pb, "stss");
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);
199 }
200
201 static int mov_write_damr_tag(ByteIOContext *pb)
202 {
203 put_be32(pb, 0x11); /* size */
204 put_tag(pb, "damr");
205 put_tag(pb, "FFMP");
206 put_byte(pb, 0);
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) */
212 return 0x11;
213 }
214
215 static 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
242 const 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
256 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
257 {
258 int pos = url_ftell(pb);
259 int tag;
260
261 put_be32(pb, 0); /* size */
262
263 tag = track->enc->codec_tag;
264 if (!tag)
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);
270 tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
271 }
272 put_le32(pb, tag); // store it byteswapped
273
274 put_be32(pb, 0); /* Reserved */
275 put_be16(pb, 0); /* Reserved */
276 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
277
278 /* SoundDescription */
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 */
283 put_be16(pb, 0); /* Revision level */
284 put_be32(pb, 0); /* Reserved */
285
286 put_be16(pb, track->enc->channels); /* Number of channels */
287 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
288 to be a good way to get number of bits of audio */
289 put_be16(pb, 0x10); /* Reserved */
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 }
300 put_be16(pb, 0); /* packet size (= 0) */
301 put_be16(pb, track->timescale); /* Time scale */
302 put_be16(pb, 0); /* Reserved */
303
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 }
317 if(track->enc->codec_id == CODEC_ID_AMR_NB)
318 mov_write_damr_tag(pb);
319 return updateSize (pb, pos);
320 }
321
322 static int mov_write_d263_tag(ByteIOContext *pb)
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
332 /* TODO: No idea about these values */
333 static int mov_write_svq3_tag(ByteIOContext *pb)
334 {
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;
343 }
344
345 static 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
357 static void putDescr(ByteIOContext *pb, int tag, int size)
358 {
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 }
395 }
396
397 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
398 {
399 int decoderSpecificInfoLen;
400 int pos = url_ftell(pb);
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 }
423
424 decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
425
426 put_be32(pb, 0); // size
427 put_tag(pb, "esds");
428 put_be32(pb, 0); // Version
429
430 // ES descriptor
431 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
432 descrLength(1));
433 put_be16(pb, 0x0001); // ID (= 1)
434 put_byte(pb, 0x00); // flags (= no flags)
435
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
449 put_byte(pb, 0x0); // Buffersize DB (24 bits)
450 put_be16(pb, 0x0dd2); // Buffersize DB
451
452 // TODO: find real values for these
453 put_be32(pb, track->enc->bit_rate); // maxbitrate
454 put_be32(pb, track->enc->bit_rate); // avg bitrate
455
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
463 track->vosData = vosDataBackup;
464 track->vosLen = vosLenBackup;
465
466 // SL descriptor
467 putDescr(pb, 0x06, 1);
468 put_byte(pb, 0x02);
469 return updateSize (pb, pos);
470 }
471
472 const 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
481 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
482 {
483 int pos = url_ftell(pb);
484 char compressor_name[32];
485 int tag;
486
487 put_be32(pb, 0); /* size */
488
489 tag = track->enc->codec_tag;
490 if (!tag)
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);
495 put_le32(pb, tag); // store it byteswapped
496
497 put_be32(pb, 0); /* Reserved */
498 put_be16(pb, 0); /* Reserved */
499 put_be16(pb, 1); /* Data-reference index */
500
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 }
511 put_be16(pb, track->enc->width); /* Video width */
512 put_be16(pb, track->enc->height); /* Video height */
513 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
514 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
515 put_be32(pb, 0); /* Data size (= 0) */
516 put_be16(pb, 1); /* Frame count (= 1) */
517
518 memset(compressor_name,0,32);
519 if (track->enc->codec && track->enc->codec->name)
520 strncpy(compressor_name,track->enc->codec->name,31);
521 put_byte(pb, strlen(compressor_name));
522 put_buffer(pb, compressor_name, 31);
523
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);
534 }
535
536 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
537 {
538 int pos = url_ftell(pb);
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 */
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);
547 return updateSize(pb, pos);
548 }
549
550 /* TODO: */
551 /* Time to sample atom */
552 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
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
559 put_be32(pb, track->sampleCount); /* sample count */
560 put_be32(pb, track->sampleDuration); /* sample duration */
561 return 0x18;
562 }
563
564 static int mov_write_dref_tag(ByteIOContext *pb)
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
578 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
579 {
580 int pos = url_ftell(pb);
581 put_be32(pb, 0); /* size */
582 put_tag(pb, "stbl");
583 mov_write_stsd_tag(pb, track);
584 mov_write_stts_tag(pb, track);
585 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
586 track->hasKeyframes)
587 mov_write_stss_tag(pb, track);
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);
592 }
593
594 static int mov_write_dinf_tag(ByteIOContext *pb)
595 {
596 int pos = url_ftell(pb);
597 put_be32(pb, 0); /* size */
598 put_tag(pb, "dinf");
599 mov_write_dref_tag(pb);
600 return updateSize(pb, pos);
601 }
602
603 static int mov_write_smhd_tag(ByteIOContext *pb)
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
613 static int mov_write_vmhd_tag(ByteIOContext *pb)
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
622 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
623 {
624 char *descr, *hdlr, *hdlr_type;
625 int pos = url_ftell(pb);
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
642 put_be32(pb, 0); /* size */
643 put_tag(pb, "hdlr");
644 put_be32(pb, 0); /* Version & flags */
645 put_buffer(pb, hdlr, 4); /* handler */
646 put_tag(pb, hdlr_type); /* handler type */
647 put_be32(pb ,0); /* reserved */
648 put_be32(pb ,0); /* reserved */
649 put_be32(pb ,0); /* reserved */
650 put_byte(pb, strlen(descr)); /* string counter */
651 put_buffer(pb, descr, strlen(descr)); /* handler description */
652 return updateSize(pb, pos);
653 }
654
655 static 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");
660 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
661 mov_write_vmhd_tag(pb);
662 else
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);
668 return updateSize(pb, pos);
669 }
670
671 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
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 */
678 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
679 put_be32(pb, track->trackDuration); /* duration */
680 put_be16(pb, 0); /* language, 0 = english */
681 put_be16(pb, 0); /* reserved (quality) */
682 return 32;
683 }
684
685 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
686 {
687 int pos = url_ftell(pb);
688 put_be32(pb, 0); /* size */
689 put_tag(pb, "mdia");
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);
694 }
695
696 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
697 {
698 int64_t maxTrackLenTemp;
699 put_be32(pb, 0x5c); /* size (always 0x5c) */
700 put_tag(pb, "tkhd");
701 put_be32(pb, 0xf); /* version & flags (track enabled) */
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 */
706 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
707 put_be32(pb, (long)maxTrackLenTemp); /* duration */
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) {
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);
735 put_be32(pb, track->enc->height*0x10000);
736 }
737 else {
738 put_be32(pb, 0);
739 put_be32(pb, 0);
740 }
741 return 0x5c;
742 }
743
744 // This box seems important for the psp playback ... without it the movie seems to hang
745 static 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)
764 static 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
782 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
783 {
784 int pos = url_ftell(pb);
785 put_be32(pb, 0); /* size */
786 put_tag(pb, "trak");
787 mov_write_tkhd_tag(pb, track);
788 if (track->mode == MODE_PSP)
789 mov_write_edts_tag(pb, track); // PSP Movies require edts box
790 mov_write_mdia_tag(pb, track);
791 if (track->mode == MODE_PSP)
792 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
793 return updateSize(pb, pos);
794 }
795
796 /* TODO: Not sorted out, but not necessary either */
797 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
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
810 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
811 {
812 int maxTrackID = 1, maxTrackLen = 0, i;
813 int64_t maxTrackLenTemp;
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) {
823 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
824 if(maxTrackLen < maxTrackLenTemp)
825 maxTrackLen = maxTrackLenTemp;
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
859 static 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 */
876 static 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 */
889 static 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 */
904 static 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 */
920 static 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 */
935 static 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 */
950 static 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 */
967 static 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 */
978 static 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 */
993 static 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 */
1008 static 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 */
1034 static 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 */
1053 static 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
1072 static 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
1081 /* iTunes meta data */
1082 mov_write_meta_tag(pb, mov, s);
1083
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 */
1102 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
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
1149 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1150 AVFormatContext *s)
1151 {
1152 int pos, i;
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++) {
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;
1170 }
1171 else {
1172 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1173 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1174 }
1175 }
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;
1181 }
1182
1183 mov_write_mvhd_tag(pb, mov);
1184 //mov_write_iods_tag(pb, mov);
1185 for (i=0; i<MAX_STREAMS; i++) {
1186 if(mov->tracks[i].entry > 0) {
1187 mov_write_trak_tag(pb, &(mov->tracks[i]));
1188 }
1189 }
1190
1191 mov_write_udta_tag(pb, mov, s);
1192
1193 return updateSize(pb, pos);
1194 }
1195
1196 int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1197 {
1198 mov->mdat_pos = url_ftell(pb);
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 */
1205 int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1206 {
1207 MOVContext *mov = s->priv_data;
1208
1209 put_be32(pb, 0x14 ); /* size */
1210 put_tag(pb, "ftyp");
1211
1212 if ( mov->mode == MODE_3GP )
1213 put_tag(pb, "3gp4");
1214 else if ( mov->mode == MODE_3G2 )
1215 put_tag(pb, "3g2a");
1216 else if ( mov->mode == MODE_PSP )
1217 put_tag(pb, "MSNV");
1218 else
1219 put_tag(pb, "isom");
1220
1221 put_be32(pb, 0x200 );
1222
1223 if ( mov->mode == MODE_3GP )
1224 put_tag(pb, "3gp4");
1225 else if ( mov->mode == MODE_3G2 )
1226 put_tag(pb, "3g2a");
1227 else if ( mov->mode == MODE_PSP )
1228 put_tag(pb, "MSNV");
1229 else
1230 put_tag(pb, "mp41");
1231
1232 return 0x14;
1233 }
1234
1235 static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1236 {
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 );
1286 }
1287
1288 static int mov_write_header(AVFormatContext *s)
1289 {
1290 ByteIOContext *pb = &s->pb;
1291 MOVContext *mov = s->priv_data;
1292 int i;
1293
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
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;
1319 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1320 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1321 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1322
1323 if ( mov->mode == MODE_3GP || mov->mode == MODE_3G2 ||
1324 mov->mode == MODE_MP4 || mov->mode == MODE_PSP )
1325 mov_write_ftyp_tag(pb,s);
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 }
1331 mov_write_uuidprof_tag(pb,s);
1332 }
1333 }
1334
1335 for (i=0; i<MAX_STREAMS; i++) {
1336 mov->tracks[i].mode = mov->mode;
1337 }
1338
1339 put_flush_packet(pb);
1340
1341 return 0;
1342 }
1343
1344 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1345 {
1346 MOVContext *mov = s->priv_data;
1347 ByteIOContext *pb = &s->pb;
1348 AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
1349 MOVTrack* trk = &mov->tracks[pkt->stream_index];
1350 int cl, id;
1351 unsigned int samplesInChunk = 0;
1352 int size= pkt->size;
1353
1354 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1355 if (!size) return 0; /* Discard 0 sized packets */
1356
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) {
1362 /* We must find out how many AMR blocks there are in one packet */
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) {
1368 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1369 samplesInChunk++;
1370 }
1371 }
1372 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
1373 samplesInChunk = size/enc->channels;
1374 }
1375 else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
1376 samplesInChunk = size/(2*enc->channels);
1377 }
1378 else {
1379 samplesInChunk = 1;
1380 }
1381 }
1382
1383 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
1384 && trk->vosLen == 0) {
1385 // assert(enc->extradata_size);
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;
1407 mov->time = s->timestamp;
1408 }
1409
1410 trk->cluster[cl][id].pos = url_ftell(pb);
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) {
1415 trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1416 if(trk->cluster[cl][id].key_frame)
1417 trk->hasKeyframes = 1;
1418 }
1419 trk->enc = enc;
1420 trk->entry++;
1421 trk->sampleCount += samplesInChunk;
1422 trk->mdat_size += size;
1423
1424 put_buffer(pb, pkt->data, size);
1425
1426 put_flush_packet(pb);
1427 return 0;
1428 }
1429
1430 static 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;
1436
1437 offset_t moov_pos = url_ftell(pb);
1438
1439 /* Write size of mdat tag */
1440 for (i=0, j=0; i<MAX_STREAMS; i++) {
1441 if(mov->tracks[i].ents_allocated > 0) {
1442 j += mov->tracks[i].mdat_size;
1443 }
1444 }
1445 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1446 put_be32(pb, j+8);
1447 url_fseek(pb, moov_pos, SEEK_SET);
1448
1449 mov_write_moov_tag(pb, mov, s);
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);
1456 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1457
1458 mov->tracks[i].cluster = NULL;
1459 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1460 }
1461
1462 put_flush_packet(pb);
1463
1464 return res;
1465 }
1466
1467 static AVOutputFormat mov_oformat = {
1468 "mov",
1469 "mov format",
1470 NULL,
1471 "mov",
1472 sizeof(MOVContext),
1473 CODEC_ID_AAC,
1474 CODEC_ID_MPEG4,
1475 mov_write_header,
1476 mov_write_packet,
1477 mov_write_trailer,
1478 };
1479
1480 static 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
1493 static AVOutputFormat mp4_oformat = {
1494 "mp4",
1495 "mp4 format",
1496 "application/mp4",
1497 "mp4,m4a",
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
1506 static 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
1519 static 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
1532 int 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);
1537 av_register_output_format(&psp_oformat);
1538 av_register_output_format(&_3g2_oformat);
1539 return 0;
1540 }