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
34 typedef struct MOVIentry {
35 unsigned int flags, pos, size;
36 unsigned int samplesInChunk;
41 typedef struct MOVIndex {
67 MOVTrack tracks[MAX_STREAMS];
70 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
72 //FIXME supprt 64bit varaint with wide placeholders
73 static int updateSize (ByteIOContext *pb, int pos)
75 long curpos = url_ftell(pb);
76 url_fseek(pb, pos, SEEK_SET);
77 put_be32(pb, curpos - pos); /* rewrite size */
78 url_fseek(pb, curpos, SEEK_SET);
83 /* Chunk offset atom */
84 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
87 int pos = url_ftell(pb);
88 put_be32(pb, 0); /* size */
90 put_be32(pb, 0); /* version & flags */
91 put_be32(pb, track->entry); /* entry count */
92 for (i=0; i<track->entry; i++) {
93 int cl = i / MOV_INDEX_CLUSTER_SIZE;
94 int id = i % MOV_INDEX_CLUSTER_SIZE;
95 put_be32(pb, track->cluster[cl][id].pos);
97 return updateSize (pb, pos);
100 /* Sample size atom */
101 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
104 int i, j, entries = 0, tst = -1, oldtst = -1;
106 int pos = url_ftell(pb);
107 put_be32(pb, 0); /* size */
109 put_be32(pb, 0); /* version & flags */
111 for (i=0; i<track->entry; i++) {
112 int cl = i / MOV_INDEX_CLUSTER_SIZE;
113 int id = i % MOV_INDEX_CLUSTER_SIZE;
114 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
115 if(oldtst != -1 && tst != oldtst) {
119 entries += track->cluster[cl][id].entries;
122 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
123 put_be32(pb, sSize); // sample size
124 put_be32(pb, entries); // sample count
127 put_be32(pb, 0); // sample size
128 put_be32(pb, entries); // sample count
129 for (i=0; i<track->entry; i++) {
130 int cl = i / MOV_INDEX_CLUSTER_SIZE;
131 int id = i % MOV_INDEX_CLUSTER_SIZE;
132 for ( j=0; j<track->cluster[cl][id].entries; j++) {
133 put_be32(pb, track->cluster[cl][id].size /
134 track->cluster[cl][id].entries);
138 return updateSize (pb, pos);
141 /* Sample to chunk atom */
142 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
144 int index = 0, oldval = -1, i, entryPos, curpos;
146 int pos = url_ftell(pb);
147 put_be32(pb, 0); /* size */
149 put_be32(pb, 0); // version & flags
150 entryPos = url_ftell(pb);
151 put_be32(pb, track->entry); // entry count
152 for (i=0; i<track->entry; i++) {
153 int cl = i / MOV_INDEX_CLUSTER_SIZE;
154 int id = i % MOV_INDEX_CLUSTER_SIZE;
155 if(oldval != track->cluster[cl][id].samplesInChunk)
157 put_be32(pb, i+1); // first chunk
158 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
159 put_be32(pb, 0x1); // sample description index
160 oldval = track->cluster[cl][id].samplesInChunk;
164 curpos = url_ftell(pb);
165 url_fseek(pb, entryPos, SEEK_SET);
166 put_be32(pb, index); // rewrite size
167 url_fseek(pb, curpos, SEEK_SET);
169 return updateSize (pb, pos);
172 /* Sync sample atom */
173 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
176 int i, index = 0, entryPos;
177 int pos = url_ftell(pb);
178 put_be32(pb, 0); // size
180 put_be32(pb, 0); // version & flags
181 entryPos = url_ftell(pb);
182 put_be32(pb, track->entry); // entry count
183 for (i=0; i<track->entry; i++) {
184 int cl = i / MOV_INDEX_CLUSTER_SIZE;
185 int id = i % MOV_INDEX_CLUSTER_SIZE;
186 if(track->cluster[cl][id].key_frame == 1) {
191 curpos = url_ftell(pb);
192 url_fseek(pb, entryPos, SEEK_SET);
193 put_be32(pb, index); // rewrite size
194 url_fseek(pb, curpos, SEEK_SET);
195 return updateSize (pb, pos);
198 static int mov_write_damr_tag(ByteIOContext *pb)
200 put_be32(pb, 0x11); /* size */
205 put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
206 put_be16(pb, 0xa); /* Mode change period (no restriction) */
207 //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
208 //put_be16(pb, 1); /* Mode change period (no restriction) */
212 static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
214 int pos = url_ftell(pb);
216 put_be32(pb, 0); /* size */
219 put_be32(pb, 12); /* size */
223 put_be32(pb, 12); /* size */
227 mov_write_esds_tag(pb, track);
229 put_be32(pb, 12); /* size */
233 put_be32(pb, 8); /* size */
234 put_be32(pb, 0); /* null tag */
236 return updateSize (pb, pos);
239 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
241 int pos = url_ftell(pb);
242 put_be32(pb, 0); /* size */
244 if(track->enc->codec_id == CODEC_ID_PCM_MULAW)
246 else if(track->enc->codec_id == CODEC_ID_PCM_ALAW)
248 else if(track->enc->codec_id == CODEC_ID_ADPCM_IMA_QT)
250 else if(track->enc->codec_id == CODEC_ID_MACE3)
252 else if(track->enc->codec_id == CODEC_ID_MACE6)
254 else if(track->enc->codec_id == CODEC_ID_AAC)
256 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
261 put_be32(pb, 0); /* Reserved */
262 put_be16(pb, 0); /* Reserved */
263 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
265 /* SoundDescription */
266 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
267 put_be16(pb, 1); /* Version 1 */
269 put_be16(pb, 0); /* Version 0 */
270 put_be16(pb, 0); /* Revision level */
271 put_be32(pb, 0); /* Reserved */
273 put_be16(pb, track->enc->channels); /* Number of channels */
274 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
275 to be a good way to get number of bits of audio */
276 put_be16(pb, 0x10); /* Reserved */
277 put_be16(pb, 0xfffe); /* compression ID (= 0) */
278 put_be16(pb, 0xac); /* packet size (= 0) */
279 put_be16(pb, track->timescale); /* Time scale */
280 put_be16(pb, 0); /* Reserved */
282 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
284 /* SoundDescription V1 extended info */
285 put_be32(pb, track->enc->frame_size); /* Samples per packet */
286 put_be32(pb, 1536); /* Bytes per packet */
287 put_be32(pb, 2); /* Bytes per frame */
288 put_be32(pb, 2); /* Bytes per sample */
291 if(track->enc->codec_id == CODEC_ID_AAC) {
292 if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
293 else mov_write_esds_tag(pb, track);
295 if(track->enc->codec_id == CODEC_ID_AMR_NB)
296 mov_write_damr_tag(pb);
297 return updateSize (pb, pos);
300 static int mov_write_d263_tag(ByteIOContext *pb)
302 put_be32(pb, 0xf); /* size */
310 /* TODO: No idea about these values */
311 static int mov_write_svq3_tag(ByteIOContext *pb)
317 put_be32(pb, 0xe2c0211d);
318 put_be32(pb, 0xc0000000);
323 static unsigned int descrLength(unsigned int len)
325 if (len < 0x00000080)
327 else if (len < 0x00004000)
329 else if(len < 0x00200000)
335 static void putDescr(ByteIOContext *pb, int tag, int size)
341 vals[3] = (uint8_t)(len & 0x7f);
343 vals[2] = (uint8_t)((len & 0x7f) | 0x80);
345 vals[1] = (uint8_t)((len & 0x7f) | 0x80);
347 vals[0] = (uint8_t)((len & 0x7f) | 0x80);
349 put_byte(pb, tag); // DescriptorTag
351 if (size < 0x00000080)
353 put_byte(pb, vals[3]);
355 else if (size < 0x00004000)
357 put_byte(pb, vals[2]);
358 put_byte(pb, vals[3]);
360 else if (size < 0x00200000)
362 put_byte(pb, vals[1]);
363 put_byte(pb, vals[2]);
364 put_byte(pb, vals[3]);
366 else if (size < 0x10000000)
368 put_byte(pb, vals[0]);
369 put_byte(pb, vals[1]);
370 put_byte(pb, vals[2]);
371 put_byte(pb, vals[3]);
375 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
377 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
378 int pos = url_ftell(pb);
380 put_be32(pb, 0); // size
382 put_be32(pb, 0); // Version
385 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
387 put_be16(pb, 0x0001); // ID (= 1)
388 put_byte(pb, 0x00); // flags (= no flags)
390 // DecoderConfig descriptor
391 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
393 if(track->enc->codec_id == CODEC_ID_AAC)
394 put_byte(pb, 0x40); // Object type indication
395 else if(track->enc->codec_id == CODEC_ID_MPEG4)
396 put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
398 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
399 put_byte(pb, 0x15); // flags (= Audiostream)
401 put_byte(pb, 0x11); // flags (= Visualstream)
403 put_byte(pb, 0x0); // Buffersize DB (24 bits)
404 put_be16(pb, 0x0dd2); // Buffersize DB
406 // TODO: find real values for these
407 put_be32(pb, 0x0002e918); // maxbitrate
408 put_be32(pb, 0x00017e6b); // avg bitrate
412 // DecoderSpecific info descriptor
413 putDescr(pb, 0x05, track->vosLen);
414 put_buffer(pb, track->vosData, track->vosLen);
418 putDescr(pb, 0x06, 1);
420 return updateSize (pb, pos);
423 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
425 int pos = url_ftell(pb);
426 put_be32(pb, 0); /* size */
427 if(track->enc->codec_id == CODEC_ID_SVQ1)
429 else if(track->enc->codec_id == CODEC_ID_SVQ3)
431 else if(track->enc->codec_id == CODEC_ID_MPEG4)
433 else if(track->enc->codec_id == CODEC_ID_H263)
436 put_tag(pb, " "); /* Unknown tag */
438 put_be32(pb, 0); /* Reserved */
439 put_be16(pb, 0); /* Reserved */
440 put_be16(pb, 1); /* Data-reference index */
442 put_be32(pb, 0); /* Reserved (= 02000c) */
443 put_be32(pb, 0); /* Reserved ("SVis")*/
444 put_be32(pb, 0); /* Reserved */
445 put_be32(pb, 0); /* Reserved (400)*/
446 put_be16(pb, track->enc->width); /* Video width */
447 put_be16(pb, track->enc->height); /* Video height */
448 put_be32(pb, 0x00480000); /* Reserved */
449 put_be32(pb, 0x00480000); /* Reserved */
450 put_be32(pb, 0); /* Reserved */
451 put_be32(pb, 0); /* Reserved */
452 put_be32(pb, 0); /* Reserved */
453 put_be32(pb, 0); /* Reserved */
454 put_be32(pb, 0); /* Reserved */
455 put_be32(pb, 0); /* Reserved */
457 put_be16(pb, 0); /* Reserved */
458 put_be32(pb, 0); /* Reserved */
459 put_be32(pb, 0); /* Reserved */
460 put_be32(pb, 0); /* Reserved */
461 put_be16(pb, 0x18); /* Reserved */
462 put_be16(pb, 0xffff); /* Reserved */
463 if(track->enc->codec_id == CODEC_ID_MPEG4)
464 mov_write_esds_tag(pb, track);
465 else if(track->enc->codec_id == CODEC_ID_H263)
466 mov_write_d263_tag(pb);
467 else if(track->enc->codec_id == CODEC_ID_SVQ3)
468 mov_write_svq3_tag(pb);
470 return updateSize (pb, pos);
473 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
475 int pos = url_ftell(pb);
476 put_be32(pb, 0); /* size */
478 put_be32(pb, 0); /* version & flags */
479 put_be32(pb, 1); /* entry count */
480 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
481 mov_write_video_tag(pb, track);
482 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
483 mov_write_audio_tag(pb, track);
484 return updateSize(pb, pos);
487 /* TODO?: Currently all samples/frames seem to have same duration */
488 /* Time to sample atom */
489 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
491 put_be32(pb, 0x18); /* size */
493 put_be32(pb, 0); /* version & flags */
494 put_be32(pb, 1); /* entry count */
496 put_be32(pb, track->sampleCount); /* sample count */
497 put_be32(pb, track->sampleDuration); /* sample duration */
501 static int mov_write_dref_tag(ByteIOContext *pb)
503 put_be32(pb, 28); /* size */
505 put_be32(pb, 0); /* version & flags */
506 put_be32(pb, 1); /* entry count */
508 put_be32(pb, 0xc); /* size */
510 put_be32(pb, 1); /* version & flags */
515 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
517 int pos = url_ftell(pb);
518 put_be32(pb, 0); /* size */
520 mov_write_stsd_tag(pb, track);
521 mov_write_stts_tag(pb, track);
522 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
524 mov_write_stss_tag(pb, track);
525 mov_write_stsc_tag(pb, track);
526 mov_write_stsz_tag(pb, track);
527 mov_write_stco_tag(pb, track);
528 return updateSize(pb, pos);
531 static int mov_write_dinf_tag(ByteIOContext *pb)
533 int pos = url_ftell(pb);
534 put_be32(pb, 0); /* size */
536 mov_write_dref_tag(pb);
537 return updateSize(pb, pos);
540 static int mov_write_smhd_tag(ByteIOContext *pb)
542 put_be32(pb, 16); /* size */
544 put_be32(pb, 0); /* version & flags */
545 put_be16(pb, 0); /* reserved (balance, normally = 0) */
546 put_be16(pb, 0); /* reserved */
550 static int mov_write_vmhd_tag(ByteIOContext *pb)
552 put_be32(pb, 0x14); /* size (always 0x14) */
554 put_be32(pb, 0x01); /* version & flags */
555 put_be64(pb, 0); /* reserved (graphics mode = copy) */
559 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
561 int pos = url_ftell(pb);
562 put_be32(pb, 0); /* size */
564 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
565 mov_write_vmhd_tag(pb);
567 mov_write_smhd_tag(pb);
568 mov_write_dinf_tag(pb);
569 mov_write_stbl_tag(pb, track);
570 return updateSize(pb, pos);
573 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
576 int pos = url_ftell(pb);
577 put_be32(pb, 0); /* size */
579 put_be32(pb, 0); /* Version & flags */
580 put_be32(pb, 0); /* reserved */
581 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
582 put_tag(pb, "vide"); /* handler type */
584 put_tag(pb, "soun"); /* handler type */
585 put_be32(pb ,0); /* reserved */
586 put_be32(pb ,0); /* reserved */
587 put_be32(pb ,0); /* reserved */
588 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
589 str = "VideoHandler";
591 str = "SoundHandler";
592 put_byte(pb, strlen(str)); /* string counter */
593 put_buffer(pb, str, strlen(str));
594 return updateSize(pb, pos);
597 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
599 put_be32(pb, 32); /* size */
601 put_be32(pb, 0); /* Version & flags */
602 put_be32(pb, track->time); /* creation time */
603 put_be32(pb, track->time); /* modification time */
604 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
605 put_be32(pb, track->trackDuration); /* duration */
606 put_be16(pb, 0); /* language, 0 = english */
607 put_be16(pb, 0); /* reserved (quality) */
611 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
613 int pos = url_ftell(pb);
614 put_be32(pb, 0); /* size */
616 mov_write_mdhd_tag(pb, track);
617 mov_write_hdlr_tag(pb, track);
618 mov_write_minf_tag(pb, track);
619 return updateSize(pb, pos);
622 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
624 int64_t maxTrackLenTemp;
625 put_be32(pb, 0x5c); /* size (always 0x5c) */
627 put_be32(pb, 0xf); /* version & flags (track enabled) */
628 put_be32(pb, track->time); /* creation time */
629 put_be32(pb, track->time); /* modification time */
630 put_be32(pb, track->trackID); /* track-id */
631 put_be32(pb, 0); /* reserved */
632 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
633 put_be32(pb, (long)maxTrackLenTemp); /* duration */
635 put_be32(pb, 0); /* reserved */
636 put_be32(pb, 0); /* reserved */
637 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
638 /* Volume, only for audio */
639 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
640 put_be16(pb, 0x0100);
643 put_be16(pb, 0); /* reserved */
645 /* Matrix structure */
646 put_be32(pb, 0x00010000); /* reserved */
647 put_be32(pb, 0x0); /* reserved */
648 put_be32(pb, 0x0); /* reserved */
649 put_be32(pb, 0x0); /* reserved */
650 put_be32(pb, 0x00010000); /* reserved */
651 put_be32(pb, 0x0); /* reserved */
652 put_be32(pb, 0x0); /* reserved */
653 put_be32(pb, 0x0); /* reserved */
654 put_be32(pb, 0x40000000); /* reserved */
656 /* Track width and height, for visual only */
657 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
658 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
659 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
660 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
661 put_be32(pb, track->enc->height*0x10000);
670 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
672 int pos = url_ftell(pb);
673 put_be32(pb, 0); /* size */
675 mov_write_tkhd_tag(pb, track);
676 mov_write_mdia_tag(pb, track);
677 return updateSize(pb, pos);
680 /* TODO: Not sorted out, but not necessary either */
681 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
683 put_be32(pb, 0x15); /* size */
685 put_be32(pb, 0); /* version & flags */
686 put_be16(pb, 0x1007);
688 put_be16(pb, 0x4fff);
689 put_be16(pb, 0xfffe);
690 put_be16(pb, 0x01ff);
694 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
696 int maxTrackID = 1, maxTrackLen = 0, i;
697 int64_t maxTrackLenTemp;
699 put_be32(pb, 0x6c); /* size (always 0x6c) */
701 put_be32(pb, 0); /* version & flags */
702 put_be32(pb, mov->time); /* creation time */
703 put_be32(pb, mov->time); /* modification time */
704 put_be32(pb, mov->timescale); /* timescale */
705 for (i=0; i<MAX_STREAMS; i++) {
706 if(mov->tracks[i].entry > 0) {
707 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
708 if(maxTrackLen < maxTrackLenTemp)
709 maxTrackLen = maxTrackLenTemp;
710 if(maxTrackID < mov->tracks[i].trackID)
711 maxTrackID = mov->tracks[i].trackID;
714 put_be32(pb, maxTrackLen); /* duration of longest track */
716 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
717 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
718 put_be16(pb, 0); /* reserved */
719 put_be32(pb, 0); /* reserved */
720 put_be32(pb, 0); /* reserved */
722 /* Matrix structure */
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, 0x00010000); /* reserved */
728 put_be32(pb, 0x0); /* reserved */
729 put_be32(pb, 0x0); /* reserved */
730 put_be32(pb, 0x0); /* reserved */
731 put_be32(pb, 0x40000000); /* reserved */
733 put_be32(pb, 0); /* reserved (preview time) */
734 put_be32(pb, 0); /* reserved (preview duration) */
735 put_be32(pb, 0); /* reserved (poster time) */
736 put_be32(pb, 0); /* reserved (selection time) */
737 put_be32(pb, 0); /* reserved (selection duration) */
738 put_be32(pb, 0); /* reserved (current time) */
739 put_be32(pb, maxTrackID+1); /* Next track id */
743 static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
746 int pos = url_ftell(pb);
749 put_be32(pb, 0); /* size */
753 for (i=0; i<MAX_STREAMS; i++) {
754 if(mov->tracks[i].entry <= 0) continue;
755 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
756 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
757 int pos = url_ftell(pb);
758 put_be32(pb, 0); /* size */
759 put_tag(pb, "\251req");
760 put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
762 put_buffer(pb, "QuickTime 6.0 or greater",
763 sizeof("QuickTime 6.0 or greater") - 1);
771 int pos = url_ftell(pb);
772 put_be32(pb, 0); /* size */
773 put_tag(pb, "\251enc");
774 put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
776 put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
782 int pos = url_ftell(pb);
783 put_be32(pb, 0); /* size */
784 put_tag(pb, "\251nam");
785 put_be16(pb, strlen(s->title)); /* string length */
787 put_buffer(pb, s->title, strlen(s->title));
793 int pos = url_ftell(pb);
794 put_be32(pb, 0); /* size */
795 put_tag(pb, /*"\251aut"*/ "\251day" );
796 put_be16(pb, strlen(s->author)); /* string length */
798 put_buffer(pb, s->author, strlen(s->author));
804 int pos = url_ftell(pb);
805 put_be32(pb, 0); /* size */
806 put_tag(pb, "\251des");
807 put_be16(pb, strlen(s->comment)); /* string length */
809 put_buffer(pb, s->comment, strlen(s->comment));
813 return updateSize(pb, pos);
816 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
821 put_be32(pb, 0); /* size placeholder*/
823 mov->timescale = globalTimescale;
825 for (i=0; i<MAX_STREAMS; i++) {
826 if(mov->tracks[i].entry <= 0) continue;
828 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
829 mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
830 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
832 else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
833 /* If AMR, track timescale = 8000, AMR_WB = 16000 */
834 if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
835 mov->tracks[i].sampleDuration = 160; // Bytes per chunk
836 mov->tracks[i].timescale = 8000;
839 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
840 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
844 mov->tracks[i].trackDuration =
845 mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
846 mov->tracks[i].time = mov->time;
847 mov->tracks[i].trackID = i+1;
850 mov_write_mvhd_tag(pb, mov);
851 //mov_write_iods_tag(pb, mov);
852 for (i=0; i<MAX_STREAMS; i++) {
853 if(mov->tracks[i].entry > 0) {
854 mov_write_trak_tag(pb, &(mov->tracks[i]));
858 mov_write_udta_tag(pb, mov, s);
860 return updateSize(pb, pos);
863 int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
865 mov->mdat_pos = url_ftell(pb);
866 put_be32(pb, 0); /* size placeholder*/
871 /* TODO: This needs to be more general */
872 int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
874 MOVContext *mov = s->priv_data;
876 put_be32(pb, 0x14 ); /* size */
879 if ( mov->mode == MODE_3GP )
884 put_be32(pb, 0x200 );
886 if ( mov->mode == MODE_3GP )
894 static int mov_write_header(AVFormatContext *s)
896 ByteIOContext *pb = &s->pb;
897 MOVContext *mov = s->priv_data;
900 /* Default mode == MP4 */
901 mov->mode = MODE_MP4;
903 if (s->oformat != NULL) {
904 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
905 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
907 if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 )
908 mov_write_ftyp_tag(pb,s);
911 for (i=0; i<MAX_STREAMS; i++) {
912 mov->tracks[i].mode = mov->mode;
915 put_flush_packet(pb);
920 static int Timestamp() {
921 return 1067949799U+(24107*86400); //its the modification time of this line :)
924 static int mov_write_packet(AVFormatContext *s, int stream_index,
925 const uint8_t *buf, int size, int64_t pts)
927 MOVContext *mov = s->priv_data;
928 ByteIOContext *pb = &s->pb;
929 AVCodecContext *enc = &s->streams[stream_index]->codec;
930 MOVTrack* trk = &mov->tracks[stream_index];
932 unsigned int samplesInChunk = 0;
934 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
935 if (!size) return 0; /* Discard 0 sized packets */
937 if (enc->codec_type == CODEC_TYPE_VIDEO ) {
940 else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
941 if( enc->codec_id == CODEC_ID_AMR_NB) {
942 /* We must find out how many AMR blocks there are in one packet */
943 static uint16_t packed_size[16] =
944 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
947 while (len < size && samplesInChunk < 100) {
948 len += packed_size[(buf[len] >> 3) & 0x0F];
952 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
953 samplesInChunk = size/enc->channels;
960 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
961 && trk->vosLen == 0) {
962 assert(enc->extradata_size);
964 trk->vosLen = enc->extradata_size;
965 trk->vosData = av_malloc(trk->vosLen);
966 memcpy(trk->vosData, enc->extradata, trk->vosLen);
969 cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
970 id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
972 if (trk->ents_allocated <= trk->entry) {
973 trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
976 trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
977 if (!trk->cluster[cl])
979 trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
981 if (mov->mdat_written == 0) {
982 mov_write_mdat_tag(pb, mov);
983 mov->mdat_written = 1;
984 mov->time = Timestamp();
987 trk->cluster[cl][id].pos = url_ftell(pb);
988 trk->cluster[cl][id].samplesInChunk = samplesInChunk;
989 trk->cluster[cl][id].size = size;
990 trk->cluster[cl][id].entries = samplesInChunk;
991 if(enc->codec_type == CODEC_TYPE_VIDEO) {
992 trk->cluster[cl][id].key_frame = enc->coded_frame->key_frame;
993 if(enc->coded_frame->pict_type == FF_I_TYPE)
994 trk->hasKeyframes = 1;
998 trk->sampleCount += samplesInChunk;
999 trk->mdat_size += size;
1001 put_buffer(pb, buf, size);
1003 put_flush_packet(pb);
1007 static int mov_write_trailer(AVFormatContext *s)
1009 MOVContext *mov = s->priv_data;
1010 ByteIOContext *pb = &s->pb;
1014 offset_t moov_pos = url_ftell(pb);
1016 /* Write size of mdat tag */
1017 for (i=0, j=0; i<MAX_STREAMS; i++) {
1018 if(mov->tracks[i].ents_allocated > 0) {
1019 j += mov->tracks[i].mdat_size;
1022 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1024 url_fseek(pb, moov_pos, SEEK_SET);
1026 mov_write_moov_tag(pb, mov, s);
1028 for (i=0; i<MAX_STREAMS; i++) {
1029 for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
1030 av_free(mov->tracks[i].cluster[j]);
1032 av_free(mov->tracks[i].cluster);
1033 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1035 mov->tracks[i].cluster = NULL;
1036 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1039 put_flush_packet(pb);
1044 static AVOutputFormat mov_oformat = {
1057 static AVOutputFormat _3gp_oformat = {
1070 static AVOutputFormat mp4_oformat = {
1083 int movenc_init(void)
1085 av_register_output_format(&mov_oformat);
1086 av_register_output_format(&_3gp_oformat);
1087 av_register_output_format(&mp4_oformat);