2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
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.
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.
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
27 #define MOV_INDEX_CLUSTER_SIZE 16384
28 #define globalTimescale 1000
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
36 typedef struct MOVIentry {
37 unsigned int flags, pos, size;
38 unsigned int samplesInChunk;
43 typedef struct MOVIndex {
62 typedef struct MOVContext {
69 MOVTrack tracks[MAX_STREAMS];
72 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
74 //FIXME supprt 64bit varaint with wide placeholders
75 static int updateSize (ByteIOContext *pb, int pos)
77 long curpos = url_ftell(pb);
78 url_fseek(pb, pos, SEEK_SET);
79 put_be32(pb, curpos - pos); /* rewrite size */
80 url_fseek(pb, curpos, SEEK_SET);
85 /* Chunk offset atom */
86 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
89 int pos = url_ftell(pb);
90 put_be32(pb, 0); /* size */
92 put_be32(pb, 0); /* version & flags */
93 put_be32(pb, track->entry); /* entry count */
94 for (i=0; i<track->entry; i++) {
95 int cl = i / MOV_INDEX_CLUSTER_SIZE;
96 int id = i % MOV_INDEX_CLUSTER_SIZE;
97 put_be32(pb, track->cluster[cl][id].pos);
99 return updateSize (pb, pos);
102 /* Sample size atom */
103 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
106 int i, j, entries = 0, tst = -1, oldtst = -1;
108 int pos = url_ftell(pb);
109 put_be32(pb, 0); /* size */
111 put_be32(pb, 0); /* version & flags */
113 for (i=0; i<track->entry; i++) {
114 int cl = i / MOV_INDEX_CLUSTER_SIZE;
115 int id = i % MOV_INDEX_CLUSTER_SIZE;
116 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
117 if(oldtst != -1 && tst != oldtst) {
121 entries += track->cluster[cl][id].entries;
124 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
125 put_be32(pb, sSize); // sample size
126 put_be32(pb, entries); // sample count
129 put_be32(pb, 0); // sample size
130 put_be32(pb, entries); // sample count
131 for (i=0; i<track->entry; i++) {
132 int cl = i / MOV_INDEX_CLUSTER_SIZE;
133 int id = i % MOV_INDEX_CLUSTER_SIZE;
134 for ( j=0; j<track->cluster[cl][id].entries; j++) {
135 put_be32(pb, track->cluster[cl][id].size /
136 track->cluster[cl][id].entries);
140 return updateSize (pb, pos);
143 /* Sample to chunk atom */
144 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
146 int index = 0, oldval = -1, i, entryPos, curpos;
148 int pos = url_ftell(pb);
149 put_be32(pb, 0); /* size */
151 put_be32(pb, 0); // version & flags
152 entryPos = url_ftell(pb);
153 put_be32(pb, track->entry); // entry count
154 for (i=0; i<track->entry; i++) {
155 int cl = i / MOV_INDEX_CLUSTER_SIZE;
156 int id = i % MOV_INDEX_CLUSTER_SIZE;
157 if(oldval != track->cluster[cl][id].samplesInChunk)
159 put_be32(pb, i+1); // first chunk
160 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
161 put_be32(pb, 0x1); // sample description index
162 oldval = track->cluster[cl][id].samplesInChunk;
166 curpos = url_ftell(pb);
167 url_fseek(pb, entryPos, SEEK_SET);
168 put_be32(pb, index); // rewrite size
169 url_fseek(pb, curpos, SEEK_SET);
171 return updateSize (pb, pos);
174 /* Sync sample atom */
175 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
178 int i, index = 0, entryPos;
179 int pos = url_ftell(pb);
180 put_be32(pb, 0); // size
182 put_be32(pb, 0); // version & flags
183 entryPos = url_ftell(pb);
184 put_be32(pb, track->entry); // entry count
185 for (i=0; i<track->entry; i++) {
186 int cl = i / MOV_INDEX_CLUSTER_SIZE;
187 int id = i % MOV_INDEX_CLUSTER_SIZE;
188 if(track->cluster[cl][id].key_frame == 1) {
193 curpos = url_ftell(pb);
194 url_fseek(pb, entryPos, SEEK_SET);
195 put_be32(pb, index); // rewrite size
196 url_fseek(pb, curpos, SEEK_SET);
197 return updateSize (pb, pos);
200 static int mov_write_damr_tag(ByteIOContext *pb)
202 put_be32(pb, 0x11); /* size */
207 put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
208 put_be16(pb, 0xa); /* Mode change period (no restriction) */
209 //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
210 //put_be16(pb, 1); /* Mode change period (no restriction) */
214 static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
216 int pos = url_ftell(pb);
218 put_be32(pb, 0); /* size */
221 put_be32(pb, 12); /* size */
225 put_be32(pb, 12); /* size */
229 mov_write_esds_tag(pb, track);
231 put_be32(pb, 12); /* size */
235 put_be32(pb, 8); /* size */
236 put_be32(pb, 0); /* null tag */
238 return updateSize (pb, pos);
241 const CodecTag codec_movaudio_tags[] = {
242 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
243 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
244 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
245 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
246 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
247 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
248 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
249 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
250 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
251 { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
255 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
257 int pos = url_ftell(pb);
260 put_be32(pb, 0); /* size */
262 tag = track->enc->codec_tag;
264 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
265 // if no mac fcc found, try with Microsoft tags
268 int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
269 tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
271 put_le32(pb, tag); // store it byteswapped
273 put_be32(pb, 0); /* Reserved */
274 put_be16(pb, 0); /* Reserved */
275 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
277 /* SoundDescription */
278 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
279 put_be16(pb, 1); /* Version 1 */
281 put_be16(pb, 0); /* Version 0 */
282 put_be16(pb, 0); /* Revision level */
283 put_be32(pb, 0); /* Reserved */
285 put_be16(pb, track->enc->channels); /* Number of channels */
286 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
287 to be a good way to get number of bits of audio */
288 put_be16(pb, 0x10); /* Reserved */
290 if(track->enc->codec_id == CODEC_ID_AAC ||
291 track->enc->codec_id == CODEC_ID_MP3)
293 put_be16(pb, 0xfffe); /* compression ID (vbr)*/
297 put_be16(pb, 0); /* compression ID (= 0) */
299 put_be16(pb, 0); /* packet size (= 0) */
300 put_be16(pb, track->timescale); /* Time scale */
301 put_be16(pb, 0); /* Reserved */
303 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
305 /* SoundDescription V1 extended info */
306 put_be32(pb, track->enc->frame_size); /* Samples per packet */
307 put_be32(pb, 1536); /* Bytes per packet */
308 put_be32(pb, 2); /* Bytes per frame */
309 put_be32(pb, 2); /* Bytes per sample */
312 if(track->enc->codec_id == CODEC_ID_AAC) {
313 if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
314 else mov_write_esds_tag(pb, track);
316 if(track->enc->codec_id == CODEC_ID_AMR_NB)
317 mov_write_damr_tag(pb);
318 return updateSize (pb, pos);
321 static int mov_write_d263_tag(ByteIOContext *pb)
323 put_be32(pb, 0xf); /* size */
331 /* TODO: No idea about these values */
332 static int mov_write_svq3_tag(ByteIOContext *pb)
338 put_be32(pb, 0xe2c0211d);
339 put_be32(pb, 0xc0000000);
344 static unsigned int descrLength(unsigned int len)
346 if (len < 0x00000080)
348 else if (len < 0x00004000)
350 else if(len < 0x00200000)
356 static void putDescr(ByteIOContext *pb, int tag, int size)
362 vals[3] = (uint8_t)(len & 0x7f);
364 vals[2] = (uint8_t)((len & 0x7f) | 0x80);
366 vals[1] = (uint8_t)((len & 0x7f) | 0x80);
368 vals[0] = (uint8_t)((len & 0x7f) | 0x80);
370 put_byte(pb, tag); // DescriptorTag
372 if (size < 0x00000080)
374 put_byte(pb, vals[3]);
376 else if (size < 0x00004000)
378 put_byte(pb, vals[2]);
379 put_byte(pb, vals[3]);
381 else if (size < 0x00200000)
383 put_byte(pb, vals[1]);
384 put_byte(pb, vals[2]);
385 put_byte(pb, vals[3]);
387 else if (size < 0x10000000)
389 put_byte(pb, vals[0]);
390 put_byte(pb, vals[1]);
391 put_byte(pb, vals[2]);
392 put_byte(pb, vals[3]);
396 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
398 int decoderSpecificInfoLen;
399 int pos = url_ftell(pb);
400 void *vosDataBackup=track->vosData;
401 int vosLenBackup=track->vosLen;
403 // we should be able to have these passed in, via vosData, then we wouldn't need to attack this routine at all
404 static const char PSPAACData[]={0x13,0x10};
405 static const char PSPMP4Data[]={0x00,0x00,0x01,0xB0,0x03,0x00,0x00,0x01,0xB5,0x09,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x20,0x00,0x84,0x5D,0x4C,0x28,0x50,0x20,0xF0,0xA3,0x1F };
408 if (track->mode == MODE_PSP) // fails on psp if this is not here
410 if (track->enc->codec_id == CODEC_ID_AAC)
413 track->vosData = PSPAACData;
416 if (track->enc->codec_id == CODEC_ID_MPEG4)
419 track->vosData = PSPMP4Data;
423 decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
425 put_be32(pb, 0); // size
427 put_be32(pb, 0); // Version
430 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
432 put_be16(pb, 0x0001); // ID (= 1)
433 put_byte(pb, 0x00); // flags (= no flags)
435 // DecoderConfig descriptor
436 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
438 if(track->enc->codec_id == CODEC_ID_AAC)
439 put_byte(pb, 0x40); // Object type indication
440 else if(track->enc->codec_id == CODEC_ID_MPEG4)
441 put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
443 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
444 put_byte(pb, 0x15); // flags (= Audiostream)
446 put_byte(pb, 0x11); // flags (= Visualstream)
448 put_byte(pb, 0x0); // Buffersize DB (24 bits)
449 put_be16(pb, 0x0dd2); // Buffersize DB
451 // TODO: find real values for these
452 put_be32(pb, track->enc->bit_rate); // maxbitrate
453 put_be32(pb, track->enc->bit_rate); // avg bitrate
457 // DecoderSpecific info descriptor
458 putDescr(pb, 0x05, track->vosLen);
459 put_buffer(pb, track->vosData, track->vosLen);
462 track->vosData = vosDataBackup;
463 track->vosLen = vosLenBackup;
466 putDescr(pb, 0x06, 1);
468 return updateSize (pb, pos);
471 const CodecTag codec_movvideo_tags[] = {
472 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
473 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
474 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
475 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
476 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
480 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
482 int pos = url_ftell(pb);
483 char compressor_name[32];
486 put_be32(pb, 0); /* size */
488 tag = track->enc->codec_tag;
490 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
491 // if no mac fcc found, try with Microsoft tags
493 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
494 put_le32(pb, tag); // store it byteswapped
496 put_be32(pb, 0); /* Reserved */
497 put_be16(pb, 0); /* Reserved */
498 put_be16(pb, 1); /* Data-reference index */
500 put_be16(pb, 0); /* Codec stream version */
501 put_be16(pb, 0); /* Codec stream revision (=0) */
502 put_tag(pb, "FFMP"); /* Vendor */
503 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
504 put_be32(pb, 0); /* Temporal Quality */
505 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
507 put_be32(pb, 0x200); /* Temporal Quality = normal */
508 put_be32(pb, 0x200); /* Spatial Quality = normal */
510 put_be16(pb, track->enc->width); /* Video width */
511 put_be16(pb, track->enc->height); /* Video height */
512 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
513 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
514 put_be32(pb, 0); /* Data size (= 0) */
515 put_be16(pb, 1); /* Frame count (= 1) */
517 memset(compressor_name,0,32);
518 if (track->enc->codec && track->enc->codec->name)
519 strncpy(compressor_name,track->enc->codec->name,31);
520 put_byte(pb, strlen(compressor_name));
521 put_buffer(pb, compressor_name, 31);
523 put_be16(pb, 0x18); /* Reserved */
524 put_be16(pb, 0xffff); /* Reserved */
525 if(track->enc->codec_id == CODEC_ID_MPEG4)
526 mov_write_esds_tag(pb, track);
527 else if(track->enc->codec_id == CODEC_ID_H263)
528 mov_write_d263_tag(pb);
529 else if(track->enc->codec_id == CODEC_ID_SVQ3)
530 mov_write_svq3_tag(pb);
532 return updateSize (pb, pos);
535 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
537 int pos = url_ftell(pb);
538 put_be32(pb, 0); /* size */
540 put_be32(pb, 0); /* version & flags */
541 put_be32(pb, 1); /* entry count */
542 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
543 mov_write_video_tag(pb, track);
544 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
545 mov_write_audio_tag(pb, track);
546 return updateSize(pb, pos);
549 /* TODO?: Currently all samples/frames seem to have same duration */
550 /* Time to sample atom */
551 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
553 put_be32(pb, 0x18); /* size */
555 put_be32(pb, 0); /* version & flags */
556 put_be32(pb, 1); /* entry count */
558 put_be32(pb, track->sampleCount); /* sample count */
559 put_be32(pb, track->sampleDuration); /* sample duration */
563 static int mov_write_dref_tag(ByteIOContext *pb)
565 put_be32(pb, 28); /* size */
567 put_be32(pb, 0); /* version & flags */
568 put_be32(pb, 1); /* entry count */
570 put_be32(pb, 0xc); /* size */
572 put_be32(pb, 1); /* version & flags */
577 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
579 int pos = url_ftell(pb);
580 put_be32(pb, 0); /* size */
582 mov_write_stsd_tag(pb, track);
583 mov_write_stts_tag(pb, track);
584 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
586 mov_write_stss_tag(pb, track);
587 mov_write_stsc_tag(pb, track);
588 mov_write_stsz_tag(pb, track);
589 mov_write_stco_tag(pb, track);
590 return updateSize(pb, pos);
593 static int mov_write_dinf_tag(ByteIOContext *pb)
595 int pos = url_ftell(pb);
596 put_be32(pb, 0); /* size */
598 mov_write_dref_tag(pb);
599 return updateSize(pb, pos);
602 static int mov_write_smhd_tag(ByteIOContext *pb)
604 put_be32(pb, 16); /* size */
606 put_be32(pb, 0); /* version & flags */
607 put_be16(pb, 0); /* reserved (balance, normally = 0) */
608 put_be16(pb, 0); /* reserved */
612 static int mov_write_vmhd_tag(ByteIOContext *pb)
614 put_be32(pb, 0x14); /* size (always 0x14) */
616 put_be32(pb, 0x01); /* version & flags */
617 put_be64(pb, 0); /* reserved (graphics mode = copy) */
621 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
623 char *descr, *hdlr, *hdlr_type;
624 int pos = url_ftell(pb);
626 if (!track) { /* no media --> data handler */
629 descr = "DataHandler";
631 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
632 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
634 descr = "VideoHandler";
637 descr = "SoundHandler";
641 put_be32(pb, 0); /* size */
643 put_be32(pb, 0); /* Version & flags */
644 put_buffer(pb, hdlr, 4); /* handler */
645 put_tag(pb, hdlr_type); /* handler type */
646 put_be32(pb ,0); /* reserved */
647 put_be32(pb ,0); /* reserved */
648 put_be32(pb ,0); /* reserved */
649 put_byte(pb, strlen(descr)); /* string counter */
650 put_buffer(pb, descr, strlen(descr)); /* handler description */
651 return updateSize(pb, pos);
654 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
656 int pos = url_ftell(pb);
657 put_be32(pb, 0); /* size */
659 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
660 mov_write_vmhd_tag(pb);
662 mov_write_smhd_tag(pb);
663 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
664 mov_write_hdlr_tag(pb, NULL);
665 mov_write_dinf_tag(pb);
666 mov_write_stbl_tag(pb, track);
667 return updateSize(pb, pos);
670 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
672 put_be32(pb, 32); /* size */
674 put_be32(pb, 0); /* Version & flags */
675 put_be32(pb, track->time); /* creation time */
676 put_be32(pb, track->time); /* modification time */
677 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
678 put_be32(pb, track->trackDuration); /* duration */
679 put_be16(pb, 0); /* language, 0 = english */
680 put_be16(pb, 0); /* reserved (quality) */
684 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
686 int pos = url_ftell(pb);
687 put_be32(pb, 0); /* size */
689 mov_write_mdhd_tag(pb, track);
690 mov_write_hdlr_tag(pb, track);
691 mov_write_minf_tag(pb, track);
692 return updateSize(pb, pos);
695 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
697 int64_t maxTrackLenTemp;
698 put_be32(pb, 0x5c); /* size (always 0x5c) */
700 put_be32(pb, 0xf); /* version & flags (track enabled) */
701 put_be32(pb, track->time); /* creation time */
702 put_be32(pb, track->time); /* modification time */
703 put_be32(pb, track->trackID); /* track-id */
704 put_be32(pb, 0); /* reserved */
705 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
706 put_be32(pb, (long)maxTrackLenTemp); /* duration */
708 put_be32(pb, 0); /* reserved */
709 put_be32(pb, 0); /* reserved */
710 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
711 /* Volume, only for audio */
712 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
713 put_be16(pb, 0x0100);
716 put_be16(pb, 0); /* reserved */
718 /* Matrix structure */
719 put_be32(pb, 0x00010000); /* reserved */
720 put_be32(pb, 0x0); /* reserved */
721 put_be32(pb, 0x0); /* reserved */
722 put_be32(pb, 0x0); /* reserved */
723 put_be32(pb, 0x00010000); /* reserved */
724 put_be32(pb, 0x0); /* reserved */
725 put_be32(pb, 0x0); /* reserved */
726 put_be32(pb, 0x0); /* reserved */
727 put_be32(pb, 0x40000000); /* reserved */
729 /* Track width and height, for visual only */
730 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
731 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
732 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
733 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
734 put_be32(pb, track->enc->height*0x10000);
743 // This box seems important for the psp playback ... without it the movie seems to hang
744 static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
746 int64_t maxTrackLenTemp;
747 put_be32(pb, 0x24); /* size */
749 put_be32(pb, 0x1c); /* size */
754 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
755 put_be32(pb, (long)maxTrackLenTemp); /* duration ... doesn't seem to effect psp */
758 put_be32(pb, 0x00010000);
762 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
763 static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
765 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
768 put_be32(pb, 0x21d24fce);
769 put_be32(pb, 0xbb88695c);
770 put_be32(pb, 0xfac9c740);
771 put_be32(pb, 0x1c); // another size here!
773 put_be32(pb, 0x00010012);
775 put_be32(pb, 0x55c40000);
781 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
783 int pos = url_ftell(pb);
784 put_be32(pb, 0); /* size */
786 mov_write_tkhd_tag(pb, track);
787 if (track->mode == MODE_PSP)
788 mov_write_edts_tag(pb, track); // PSP Movies require edts box
789 mov_write_mdia_tag(pb, track);
790 if (track->mode == MODE_PSP)
791 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
792 return updateSize(pb, pos);
795 /* TODO: Not sorted out, but not necessary either */
796 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
798 put_be32(pb, 0x15); /* size */
800 put_be32(pb, 0); /* version & flags */
801 put_be16(pb, 0x1007);
803 put_be16(pb, 0x4fff);
804 put_be16(pb, 0xfffe);
805 put_be16(pb, 0x01ff);
809 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
811 int maxTrackID = 1, maxTrackLen = 0, i;
812 int64_t maxTrackLenTemp;
814 put_be32(pb, 0x6c); /* size (always 0x6c) */
816 put_be32(pb, 0); /* version & flags */
817 put_be32(pb, mov->time); /* creation time */
818 put_be32(pb, mov->time); /* modification time */
819 put_be32(pb, mov->timescale); /* timescale */
820 for (i=0; i<MAX_STREAMS; i++) {
821 if(mov->tracks[i].entry > 0) {
822 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
823 if(maxTrackLen < maxTrackLenTemp)
824 maxTrackLen = maxTrackLenTemp;
825 if(maxTrackID < mov->tracks[i].trackID)
826 maxTrackID = mov->tracks[i].trackID;
829 put_be32(pb, maxTrackLen); /* duration of longest track */
831 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
832 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
833 put_be16(pb, 0); /* reserved */
834 put_be32(pb, 0); /* reserved */
835 put_be32(pb, 0); /* reserved */
837 /* Matrix structure */
838 put_be32(pb, 0x00010000); /* reserved */
839 put_be32(pb, 0x0); /* reserved */
840 put_be32(pb, 0x0); /* reserved */
841 put_be32(pb, 0x0); /* reserved */
842 put_be32(pb, 0x00010000); /* reserved */
843 put_be32(pb, 0x0); /* reserved */
844 put_be32(pb, 0x0); /* reserved */
845 put_be32(pb, 0x0); /* reserved */
846 put_be32(pb, 0x40000000); /* reserved */
848 put_be32(pb, 0); /* reserved (preview time) */
849 put_be32(pb, 0); /* reserved (preview duration) */
850 put_be32(pb, 0); /* reserved (poster time) */
851 put_be32(pb, 0); /* reserved (selection time) */
852 put_be32(pb, 0); /* reserved (selection duration) */
853 put_be32(pb, 0); /* reserved (current time) */
854 put_be32(pb, maxTrackID+1); /* Next track id */
858 static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
861 int pos = url_ftell(pb);
862 put_be32(pb, 0); /* size */
871 return updateSize(pb, pos);
874 /* helper function to write a data tag with the specified string as data */
875 static int mov_write_string_data_tag(ByteIOContext *pb, MOVContext* mov,
876 AVFormatContext *s, const char *data)
878 int pos = url_ftell(pb);
879 put_be32(pb, 0); /* size */
883 put_buffer(pb, data, strlen(data));
884 return updateSize(pb, pos);
887 /* iTunes name of the song/movie */
888 static int mov_write_nam_tag(ByteIOContext *pb, MOVContext* mov,
893 int pos = url_ftell(pb);
894 put_be32(pb, 0); /* size */
895 put_tag(pb, "\251nam");
896 mov_write_string_data_tag(pb, mov, s, s->title);
897 size = updateSize(pb, pos);
902 /* iTunes name of the artist/performer */
903 static int mov_write_ART_tag(ByteIOContext *pb, MOVContext* mov,
907 if ( s->author[0] ) {
908 int pos = url_ftell(pb);
909 put_be32(pb, 0); /* size */
910 put_tag(pb, "\251ART");
911 // we use the author here as this is the only thing that we have...
912 mov_write_string_data_tag(pb, mov, s, s->author);
913 size = updateSize(pb, pos);
918 /* iTunes name of the writer */
919 static int mov_write_wrt_tag(ByteIOContext *pb, MOVContext* mov,
923 if ( s->author[0] ) {
924 int pos = url_ftell(pb);
925 put_be32(pb, 0); /* size */
926 put_tag(pb, "\251wrt");
927 mov_write_string_data_tag(pb, mov, s, s->author);
928 size = updateSize(pb, pos);
933 /* iTunes name of the album */
934 static int mov_write_alb_tag(ByteIOContext *pb, MOVContext* mov,
939 int pos = url_ftell(pb);
940 put_be32(pb, 0); /* size */
941 put_tag(pb, "\251alb");
942 mov_write_string_data_tag(pb, mov, s, s->album);
943 size = updateSize(pb, pos);
949 static int mov_write_day_tag(ByteIOContext *pb, MOVContext* mov,
955 int pos = url_ftell(pb);
956 put_be32(pb, 0); /* size */
957 put_tag(pb, "\251day");
958 snprintf(year, 5, "%04d", s->year);
959 mov_write_string_data_tag(pb, mov, s, year);
960 size = updateSize(pb, pos);
965 /* iTunes tool used to create the file */
966 static int mov_write_too_tag(ByteIOContext *pb, MOVContext* mov,
969 int pos = url_ftell(pb);
970 put_be32(pb, 0); /* size */
971 put_tag(pb, "\251too");
972 mov_write_string_data_tag(pb, mov, s, LIBAVFORMAT_IDENT);
973 return updateSize(pb, pos);
977 static int mov_write_cmt_tag(ByteIOContext *pb, MOVContext* mov,
981 if ( s->comment[0] ) {
982 int pos = url_ftell(pb);
983 put_be32(pb, 0); /* size */
984 put_tag(pb, "\251cmt");
985 mov_write_string_data_tag(pb, mov, s, s->comment);
986 size = updateSize(pb, pos);
991 /* iTunes custom genre */
992 static int mov_write_gen_tag(ByteIOContext *pb, MOVContext* mov,
997 int pos = url_ftell(pb);
998 put_be32(pb, 0); /* size */
999 put_tag(pb, "\251gen");
1000 mov_write_string_data_tag(pb, mov, s, s->genre);
1001 size = updateSize(pb, pos);
1006 /* iTunes track number */
1007 static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1012 int pos = url_ftell(pb);
1013 put_be32(pb, 0); /* size */
1014 put_tag(pb, "trkn");
1016 int pos = url_ftell(pb);
1017 put_be32(pb, 0); /* size */
1018 put_tag(pb, "data");
1019 put_be32(pb, 0); // 8 bytes empty
1021 put_be16(pb, 0); // empty
1022 put_be16(pb, s->track); // track number
1023 put_be16(pb, 0); // total track number
1024 put_be16(pb, 0); // empty
1025 updateSize(pb, pos);
1027 size = updateSize(pb, pos);
1032 /* iTunes meta data list */
1033 static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1036 int pos = url_ftell(pb);
1037 put_be32(pb, 0); /* size */
1038 put_tag(pb, "ilst");
1039 mov_write_nam_tag(pb, mov, s);
1040 mov_write_ART_tag(pb, mov, s);
1041 mov_write_wrt_tag(pb, mov, s);
1042 mov_write_alb_tag(pb, mov, s);
1043 mov_write_day_tag(pb, mov, s);
1044 mov_write_too_tag(pb, mov, s);
1045 mov_write_cmt_tag(pb, mov, s);
1046 mov_write_gen_tag(pb, mov, s);
1047 mov_write_trkn_tag(pb, mov, s);
1048 return updateSize(pb, pos);
1051 /* iTunes meta data tag */
1052 static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1057 // only save meta tag if required
1058 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1059 s->comment[0] || s->genre[0] || s->track ) {
1060 int pos = url_ftell(pb);
1061 put_be32(pb, 0); /* size */
1062 put_tag(pb, "meta");
1064 mov_write_itunes_hdlr_tag(pb, mov, s);
1065 mov_write_ilst_tag(pb, mov, s);
1066 size = updateSize(pb, pos);
1071 static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1074 int pos = url_ftell(pb);
1077 put_be32(pb, 0); /* size */
1078 put_tag(pb, "udta");
1080 /* iTunes meta data */
1081 mov_write_meta_tag(pb, mov, s);
1084 for (i=0; i<MAX_STREAMS; i++) {
1085 if(mov->tracks[i].entry <= 0) continue;
1086 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1087 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1088 int pos = url_ftell(pb);
1089 put_be32(pb, 0); /* size */
1090 put_tag(pb, "\251req");
1091 put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
1093 put_buffer(pb, "QuickTime 6.0 or greater",
1094 sizeof("QuickTime 6.0 or greater") - 1);
1095 updateSize(pb, pos);
1101 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1103 int pos = url_ftell(pb);
1104 put_be32(pb, 0); /* size */
1105 put_tag(pb, "\251enc");
1106 put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
1108 put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
1109 updateSize(pb, pos);
1114 int pos = url_ftell(pb);
1115 put_be32(pb, 0); /* size */
1116 put_tag(pb, "\251nam");
1117 put_be16(pb, strlen(s->title)); /* string length */
1119 put_buffer(pb, s->title, strlen(s->title));
1120 updateSize(pb, pos);
1125 int pos = url_ftell(pb);
1126 put_be32(pb, 0); /* size */
1127 put_tag(pb, /*"\251aut"*/ "\251day" );
1128 put_be16(pb, strlen(s->author)); /* string length */
1130 put_buffer(pb, s->author, strlen(s->author));
1131 updateSize(pb, pos);
1136 int pos = url_ftell(pb);
1137 put_be32(pb, 0); /* size */
1138 put_tag(pb, "\251des");
1139 put_be16(pb, strlen(s->comment)); /* string length */
1141 put_buffer(pb, s->comment, strlen(s->comment));
1142 updateSize(pb, pos);
1145 return updateSize(pb, pos);
1148 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1152 pos = url_ftell(pb);
1153 put_be32(pb, 0); /* size placeholder*/
1154 put_tag(pb, "moov");
1155 mov->timescale = globalTimescale;
1157 for (i=0; i<MAX_STREAMS; i++) {
1158 if(mov->tracks[i].entry <= 0) continue;
1160 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
1161 mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
1162 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
1164 else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
1165 /* If AMR, track timescale = 8000, AMR_WB = 16000 */
1166 if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
1167 mov->tracks[i].sampleDuration = 160; // Bytes per chunk
1168 mov->tracks[i].timescale = 8000;
1171 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1172 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1176 mov->tracks[i].trackDuration =
1177 mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
1178 mov->tracks[i].time = mov->time;
1179 mov->tracks[i].trackID = i+1;
1182 mov_write_mvhd_tag(pb, mov);
1183 //mov_write_iods_tag(pb, mov);
1184 for (i=0; i<MAX_STREAMS; i++) {
1185 if(mov->tracks[i].entry > 0) {
1186 mov_write_trak_tag(pb, &(mov->tracks[i]));
1190 mov_write_udta_tag(pb, mov, s);
1192 return updateSize(pb, pos);
1195 int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1197 mov->mdat_pos = url_ftell(pb);
1198 put_be32(pb, 0); /* size placeholder*/
1199 put_tag(pb, "mdat");
1203 /* TODO: This needs to be more general */
1204 int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1206 MOVContext *mov = s->priv_data;
1208 put_be32(pb, 0x14 ); /* size */
1209 put_tag(pb, "ftyp");
1211 if ( mov->mode == MODE_3GP )
1212 put_tag(pb, "3gp4");
1213 else if ( mov->mode == MODE_PSP )
1214 put_tag(pb, "MSNV");
1216 put_tag(pb, "isom");
1218 put_be32(pb, 0x200 );
1220 if ( mov->mode == MODE_3GP )
1221 put_tag(pb, "3gp4");
1222 else if ( mov->mode == MODE_PSP )
1223 put_tag(pb, "MSNV");
1225 put_tag(pb, "mp41");
1230 int mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1232 MOVContext *mov = s->priv_data;
1233 int AudioRate = s->streams[1]->codec.sample_rate;
1234 int FrameRate = ((s->streams[0]->codec.frame_rate) * (0x10000))/ (s->streams[0]->codec.frame_rate_base);
1236 //printf("audiorate = %d\n",AudioRate);
1237 //printf("framerate = %d / %d = 0x%x\n",s->streams[0]->codec.frame_rate,s->streams[0]->codec.frame_rate_base,FrameRate);
1239 put_be32(pb, 0x94 ); /* size */
1240 put_tag(pb, "uuid");
1241 put_tag(pb, "PROF");
1243 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1244 put_be32(pb, 0xbb88695c );
1245 put_be32(pb, 0xfac9c740 );
1247 put_be32(pb, 0x0 ); /* ? */
1248 put_be32(pb, 0x3 ); /* 3 sections ? */
1250 put_be32(pb, 0x14 ); /* size */
1251 put_tag(pb, "FPRF");
1252 put_be32(pb, 0x0 ); /* ? */
1253 put_be32(pb, 0x0 ); /* ? */
1254 put_be32(pb, 0x0 ); /* ? */
1256 put_be32(pb, 0x2c ); /* size */
1257 put_tag(pb, "APRF"); /* audio */
1260 put_tag(pb, "mp4a");
1261 put_be32(pb, 0x20f );
1263 put_be32(pb, 0x40 );
1264 put_be32(pb, 0x40 );
1265 put_be32(pb, AudioRate ); //24000 ... audio rate?
1268 put_be32(pb, 0x34 ); /* size */
1269 put_tag(pb, "VPRF"); /* video */
1272 put_tag(pb, "mp4v");
1273 put_be32(pb, 0x103 );
1275 put_be32(pb, 0xc0 );
1276 put_be32(pb, 0xc0 );
1277 put_be32(pb, FrameRate); // was 0xefc29
1278 put_be32(pb, FrameRate ); // was 0xefc29
1279 put_be16(pb, s->streams[0]->codec.width);
1280 put_be16(pb, s->streams[0]->codec.height);
1281 put_be32(pb, 0x010001 );
1286 static int mov_write_header(AVFormatContext *s)
1288 ByteIOContext *pb = &s->pb;
1289 MOVContext *mov = s->priv_data;
1292 for(i=0; i<s->nb_streams; i++){
1293 AVCodecContext *c= &s->streams[i]->codec;
1295 if (c->codec_type == CODEC_TYPE_VIDEO){
1296 if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
1297 if(!codec_get_tag(codec_bmp_tags, c->codec_id))
1300 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
1302 }else if(c->codec_type == CODEC_TYPE_AUDIO){
1303 if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
1304 if(!codec_get_tag(codec_wav_tags, c->codec_id))
1307 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
1312 /* Default mode == MP4 */
1313 mov->mode = MODE_MP4;
1315 if (s->oformat != NULL) {
1316 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1317 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1318 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1320 if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 || mov->mode == MODE_PSP )
1321 mov_write_ftyp_tag(pb,s);
1322 if ( mov->mode == MODE_PSP )
1323 mov_write_uuidprof_tag(pb,s);
1326 for (i=0; i<MAX_STREAMS; i++) {
1327 mov->tracks[i].mode = mov->mode;
1330 put_flush_packet(pb);
1335 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1337 MOVContext *mov = s->priv_data;
1338 ByteIOContext *pb = &s->pb;
1339 AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
1340 MOVTrack* trk = &mov->tracks[pkt->stream_index];
1342 unsigned int samplesInChunk = 0;
1343 int size= pkt->size;
1345 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1346 if (!size) return 0; /* Discard 0 sized packets */
1348 if (enc->codec_type == CODEC_TYPE_VIDEO ) {
1351 else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
1352 if( enc->codec_id == CODEC_ID_AMR_NB) {
1353 /* We must find out how many AMR blocks there are in one packet */
1354 static uint16_t packed_size[16] =
1355 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1358 while (len < size && samplesInChunk < 100) {
1359 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1363 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
1364 samplesInChunk = size/enc->channels;
1366 else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
1367 samplesInChunk = size/(2*enc->channels);
1374 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
1375 && trk->vosLen == 0) {
1376 // assert(enc->extradata_size);
1378 trk->vosLen = enc->extradata_size;
1379 trk->vosData = av_malloc(trk->vosLen);
1380 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1383 cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
1384 id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
1386 if (trk->ents_allocated <= trk->entry) {
1387 trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
1390 trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
1391 if (!trk->cluster[cl])
1393 trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
1395 if (mov->mdat_written == 0) {
1396 mov_write_mdat_tag(pb, mov);
1397 mov->mdat_written = 1;
1398 mov->time = s->timestamp;
1401 trk->cluster[cl][id].pos = url_ftell(pb);
1402 trk->cluster[cl][id].samplesInChunk = samplesInChunk;
1403 trk->cluster[cl][id].size = size;
1404 trk->cluster[cl][id].entries = samplesInChunk;
1405 if(enc->codec_type == CODEC_TYPE_VIDEO) {
1406 trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1407 if(trk->cluster[cl][id].key_frame)
1408 trk->hasKeyframes = 1;
1412 trk->sampleCount += samplesInChunk;
1413 trk->mdat_size += size;
1415 put_buffer(pb, pkt->data, size);
1417 put_flush_packet(pb);
1421 static int mov_write_trailer(AVFormatContext *s)
1423 MOVContext *mov = s->priv_data;
1424 ByteIOContext *pb = &s->pb;
1428 offset_t moov_pos = url_ftell(pb);
1430 /* Write size of mdat tag */
1431 for (i=0, j=0; i<MAX_STREAMS; i++) {
1432 if(mov->tracks[i].ents_allocated > 0) {
1433 j += mov->tracks[i].mdat_size;
1436 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1438 url_fseek(pb, moov_pos, SEEK_SET);
1440 mov_write_moov_tag(pb, mov, s);
1442 for (i=0; i<MAX_STREAMS; i++) {
1443 for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
1444 av_free(mov->tracks[i].cluster[j]);
1446 av_free(mov->tracks[i].cluster);
1447 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1449 mov->tracks[i].cluster = NULL;
1450 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1453 put_flush_packet(pb);
1458 static AVOutputFormat mov_oformat = {
1471 static AVOutputFormat _3gp_oformat = {
1484 static AVOutputFormat mp4_oformat = {
1497 static AVOutputFormat psp_oformat = {
1510 int movenc_init(void)
1512 av_register_output_format(&mov_oformat);
1513 av_register_output_format(&_3gp_oformat);
1514 av_register_output_format(&mp4_oformat);
1515 av_register_output_format(&psp_oformat);