2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
4 * Enhancements by Gildas Bazin <gbazin@netcourrier.com>
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
30 typedef struct MOVIentry {
31 unsigned int flags, pos, size;
32 unsigned int samplesInChunk;
37 typedef struct MOVIndex {
62 MOVTrack tracks[MAX_STREAMS];
65 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
67 //FIXME supprt 64bit varaint with wide placeholders
68 static int updateSize (ByteIOContext *pb, int pos)
70 long curpos = url_ftell(pb);
71 url_fseek(pb, pos, SEEK_SET);
72 put_be32(pb, curpos - pos); /* rewrite size */
73 url_fseek(pb, curpos, SEEK_SET);
78 /* Chunk offset atom */
79 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
82 int pos = url_ftell(pb);
83 put_be32(pb, 0); /* size */
85 put_be32(pb, 0); /* version & flags */
86 put_be32(pb, track->entry); /* entry count */
87 for (i=0; i<track->entry; i++) {
88 int cl = i / MOV_INDEX_CLUSTER_SIZE;
89 int id = i % MOV_INDEX_CLUSTER_SIZE;
90 put_be32(pb, track->cluster[cl][id].pos);
92 return updateSize (pb, pos);
95 /* Sample size atom */
96 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
99 int i, j, entries = 0, tst = -1, oldtst = -1;
101 int pos = url_ftell(pb);
102 put_be32(pb, 0); /* size */
104 put_be32(pb, 0); /* version & flags */
106 for (i=0; i<track->entry; i++) {
107 int cl = i / MOV_INDEX_CLUSTER_SIZE;
108 int id = i % MOV_INDEX_CLUSTER_SIZE;
109 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
110 if(oldtst != -1 && tst != oldtst) {
114 entries += track->cluster[cl][id].entries;
117 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
118 put_be32(pb, sSize); // sample size
119 put_be32(pb, entries); // sample count
122 put_be32(pb, 0); // sample size
123 put_be32(pb, entries); // sample count
124 for (i=0; i<track->entry; i++) {
125 int cl = i / MOV_INDEX_CLUSTER_SIZE;
126 int id = i % MOV_INDEX_CLUSTER_SIZE;
127 for ( j=0; j<track->cluster[cl][id].entries; j++) {
128 put_be32(pb, track->cluster[cl][id].size /
129 track->cluster[cl][id].entries);
133 return updateSize (pb, pos);
136 /* Sample to chunk atom */
137 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
139 int index = 0, oldval = -1, i, entryPos, curpos;
141 int pos = url_ftell(pb);
142 put_be32(pb, 0); /* size */
144 put_be32(pb, 0); // version & flags
145 entryPos = url_ftell(pb);
146 put_be32(pb, track->entry); // entry count
147 for (i=0; i<track->entry; i++) {
148 int cl = i / MOV_INDEX_CLUSTER_SIZE;
149 int id = i % MOV_INDEX_CLUSTER_SIZE;
150 if(oldval != track->cluster[cl][id].samplesInChunk)
152 put_be32(pb, i+1); // first chunk
153 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
154 put_be32(pb, 0x1); // sample description index
155 oldval = track->cluster[cl][id].samplesInChunk;
159 curpos = url_ftell(pb);
160 url_fseek(pb, entryPos, SEEK_SET);
161 put_be32(pb, index); // rewrite size
162 url_fseek(pb, curpos, SEEK_SET);
164 return updateSize (pb, pos);
167 /* Sync sample atom */
168 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
171 int i, index = 0, entryPos;
172 int pos = url_ftell(pb);
173 put_be32(pb, 0); // size
175 put_be32(pb, 0); // version & flags
176 entryPos = url_ftell(pb);
177 put_be32(pb, track->entry); // entry count
178 for (i=0; i<track->entry; i++) {
179 int cl = i / MOV_INDEX_CLUSTER_SIZE;
180 int id = i % MOV_INDEX_CLUSTER_SIZE;
181 if(track->cluster[cl][id].key_frame == 1) {
186 curpos = url_ftell(pb);
187 url_fseek(pb, entryPos, SEEK_SET);
188 put_be32(pb, index); // rewrite size
189 url_fseek(pb, curpos, SEEK_SET);
190 return updateSize (pb, pos);
193 static int mov_write_damr_tag(ByteIOContext *pb)
195 put_be32(pb, 0x11); /* size */
200 put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
201 put_be16(pb, 0xa); /* Mode change period (no restriction) */
202 //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
203 //put_be16(pb, 1); /* Mode change period (no restriction) */
207 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
209 int pos = url_ftell(pb);
210 put_be32(pb, 0); /* size */
212 if(track->enc->codec_id == CODEC_ID_PCM_MULAW)
214 else if(track->enc->codec_id == CODEC_ID_PCM_ALAW)
216 else if(track->enc->codec_id == CODEC_ID_ADPCM_IMA_QT)
218 else if(track->enc->codec_id == CODEC_ID_MACE3)
220 else if(track->enc->codec_id == CODEC_ID_MACE6)
222 else if(track->enc->codec_id == CODEC_ID_AAC)
224 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
229 put_be32(pb, 0); /* Reserved */
230 put_be16(pb, 0); /* Reserved */
231 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
232 /* SoundDescription */
233 put_be16(pb, 0); /* Version */
234 put_be16(pb, 0); /* Revision level */
235 put_be32(pb, 0); /* Reserved */
237 put_be16(pb, track->enc->channels); /* Number of channels */
238 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
239 to be a good way to get number of bits of audio */
240 put_be16(pb, 0x10); /* Reserved */
241 put_be16(pb, 0); /* compression ID (= 0) */
242 put_be16(pb, 0); /* packet size (= 0) */
243 put_be16(pb, track->timescale); /* Time scale */
244 put_be16(pb, 0); /* Reserved */
246 if(track->enc->codec_id == CODEC_ID_AAC)
247 mov_write_esds_tag(pb, track);
248 if(track->enc->codec_id == CODEC_ID_AMR_NB)
249 mov_write_damr_tag(pb);
250 return updateSize (pb, pos);
253 static int mov_write_d263_tag(ByteIOContext *pb)
255 put_be32(pb, 0xf); /* size */
263 /* TODO: No idea about these values */
264 static int mov_write_svq3_tag(ByteIOContext *pb)
270 put_be32(pb, 0xe2c0211d);
271 put_be32(pb, 0xc0000000);
276 static unsigned int descrLength(unsigned int len)
278 if (len < 0x00000080)
280 else if (len < 0x00004000)
282 else if(len < 0x00200000)
288 static void putDescr(ByteIOContext *pb, int tag, int size)
294 vals[3] = (uint8_t)(len & 0x7f);
296 vals[2] = (uint8_t)((len & 0x7f) | 0x80);
298 vals[1] = (uint8_t)((len & 0x7f) | 0x80);
300 vals[0] = (uint8_t)((len & 0x7f) | 0x80);
302 put_byte(pb, tag); // DescriptorTag
304 if (size < 0x00000080)
306 put_byte(pb, vals[3]);
308 else if (size < 0x00004000)
310 put_byte(pb, vals[2]);
311 put_byte(pb, vals[3]);
313 else if (size < 0x00200000)
315 put_byte(pb, vals[1]);
316 put_byte(pb, vals[2]);
317 put_byte(pb, vals[3]);
319 else if (size < 0x10000000)
321 put_byte(pb, vals[0]);
322 put_byte(pb, vals[1]);
323 put_byte(pb, vals[2]);
324 put_byte(pb, vals[3]);
328 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
330 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
331 int pos = url_ftell(pb);
333 put_be32(pb, 0); // size
335 put_be32(pb, 0); // Version
338 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
340 put_be16(pb, 0x0001); // ID (= 1)
341 put_byte(pb, 0x00); // flags (= no flags)
343 // DecoderConfig descriptor
344 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
346 if(track->enc->codec_id == CODEC_ID_AAC)
347 put_byte(pb, 0x40); // Object type indication
348 else if(track->enc->codec_id == CODEC_ID_MPEG4)
349 put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
351 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
352 put_byte(pb, 0x15); // flags (= Audiostream)
354 put_byte(pb, 0x11); // flags (= Visualstream)
356 put_byte(pb, 0x0); // Buffersize DB (24 bits)
357 put_be16(pb, 0x0dd2); // Buffersize DB
359 // TODO: find real values for these
360 put_be32(pb, 0x0002e918); // maxbitrate
361 put_be32(pb, 0x00017e6b); // avg bitrate
365 // DecoderSpecific info descriptor
366 putDescr(pb, 0x05, track->vosLen);
367 put_buffer(pb, track->vosData, track->vosLen);
371 putDescr(pb, 0x06, descrLength(1));
373 return updateSize (pb, pos);
376 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
378 int pos = url_ftell(pb);
379 put_be32(pb, 0); /* size */
380 if(track->enc->codec_id == CODEC_ID_SVQ1)
382 else if(track->enc->codec_id == CODEC_ID_SVQ3)
384 else if(track->enc->codec_id == CODEC_ID_MPEG4)
386 else if(track->enc->codec_id == CODEC_ID_H263)
389 put_tag(pb, " "); /* Unknown tag */
391 put_be32(pb, 0); /* Reserved */
392 put_be16(pb, 0); /* Reserved */
393 put_be16(pb, 1); /* Data-reference index */
395 put_be32(pb, 0); /* Reserved (= 02000c) */
396 put_be32(pb, 0); /* Reserved ("SVis")*/
397 put_be32(pb, 0); /* Reserved */
398 put_be32(pb, 0); /* Reserved (400)*/
399 put_be16(pb, track->enc->width); /* Video width */
400 put_be16(pb, track->enc->height); /* Video height */
401 put_be32(pb, 0x00480000); /* Reserved */
402 put_be32(pb, 0x00480000); /* Reserved */
403 put_be32(pb, 0); /* Reserved */
404 put_be32(pb, 0); /* Reserved */
405 put_be32(pb, 0); /* Reserved */
406 put_be32(pb, 0); /* Reserved */
407 put_be32(pb, 0); /* Reserved */
408 put_be32(pb, 0); /* Reserved */
410 put_be16(pb, 0); /* Reserved */
411 put_be32(pb, 0); /* Reserved */
412 put_be32(pb, 0); /* Reserved */
413 put_be32(pb, 0); /* Reserved */
414 put_be16(pb, 0x18); /* Reserved */
415 put_be16(pb, 0xffff); /* Reserved */
416 if(track->enc->codec_id == CODEC_ID_MPEG4)
417 mov_write_esds_tag(pb, track);
418 else if(track->enc->codec_id == CODEC_ID_H263)
419 mov_write_d263_tag(pb);
420 else if(track->enc->codec_id == CODEC_ID_SVQ3)
421 mov_write_svq3_tag(pb);
423 return updateSize (pb, pos);
426 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
428 int pos = url_ftell(pb);
429 put_be32(pb, 0); /* size */
431 put_be32(pb, 0); /* version & flags */
432 put_be32(pb, 1); /* entry count */
433 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
434 mov_write_video_tag(pb, track);
435 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
436 mov_write_audio_tag(pb, track);
437 return updateSize(pb, pos);
440 /* TODO?: Currently all samples/frames seem to have same duration */
441 /* Time to sample atom */
442 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
444 put_be32(pb, 0x18); /* size */
446 put_be32(pb, 0); /* version & flags */
447 put_be32(pb, 1); /* entry count */
449 put_be32(pb, track->sampleCount); /* sample count */
450 put_be32(pb, track->sampleDuration); /* sample duration */
454 static int mov_write_dref_tag(ByteIOContext *pb)
456 put_be32(pb, 28); /* size */
458 put_be32(pb, 0); /* version & flags */
459 put_be32(pb, 1); /* entry count */
461 put_be32(pb, 0xc); /* size */
463 put_be32(pb, 1); /* version & flags */
468 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
470 int pos = url_ftell(pb);
471 put_be32(pb, 0); /* size */
473 mov_write_stsd_tag(pb, track);
474 mov_write_stts_tag(pb, track);
475 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
477 mov_write_stss_tag(pb, track);
478 mov_write_stsc_tag(pb, track);
479 mov_write_stsz_tag(pb, track);
480 mov_write_stco_tag(pb, track);
481 return updateSize(pb, pos);
484 static int mov_write_dinf_tag(ByteIOContext *pb)
486 int pos = url_ftell(pb);
487 put_be32(pb, 0); /* size */
489 mov_write_dref_tag(pb);
490 return updateSize(pb, pos);
493 static int mov_write_smhd_tag(ByteIOContext *pb)
495 put_be32(pb, 16); /* size */
497 put_be32(pb, 0); /* version & flags */
498 put_be16(pb, 0); /* reserved (balance, normally = 0) */
499 put_be16(pb, 0); /* reserved */
503 static int mov_write_vmhd_tag(ByteIOContext *pb)
505 put_be32(pb, 0x14); /* size (always 0x14) */
507 put_be32(pb, 0x01); /* version & flags */
508 put_be64(pb, 0); /* reserved (graphics mode = copy) */
512 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
514 int pos = url_ftell(pb);
515 put_be32(pb, 0); /* size */
517 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
518 mov_write_vmhd_tag(pb);
520 mov_write_smhd_tag(pb);
521 mov_write_dinf_tag(pb);
522 mov_write_stbl_tag(pb, track);
523 return updateSize(pb, pos);
526 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
529 int pos = url_ftell(pb);
530 put_be32(pb, 0); /* size */
532 put_be32(pb, 0); /* Version & flags */
533 put_be32(pb, 0); /* reserved */
534 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
535 put_tag(pb, "vide"); /* handler type */
537 put_tag(pb, "soun"); /* handler type */
538 put_be32(pb ,0); /* reserved */
539 put_be32(pb ,0); /* reserved */
540 put_be32(pb ,0); /* reserved */
541 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
542 str = "VideoHandler";
544 str = "SoundHandler";
545 put_byte(pb, strlen(str)); /* string counter */
546 put_buffer(pb, str, strlen(str));
547 return updateSize(pb, pos);
550 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
552 put_be32(pb, 32); /* size */
554 put_be32(pb, 0); /* Version & flags */
555 put_be32(pb, track->time); /* creation time */
556 put_be32(pb, track->time); /* modification time */
557 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
558 put_be32(pb, track->trackDuration); /* duration */
559 put_be16(pb, 0); /* language, 0 = english */
560 put_be16(pb, 0); /* reserved (quality) */
564 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
566 int pos = url_ftell(pb);
567 put_be32(pb, 0); /* size */
569 mov_write_mdhd_tag(pb, track);
570 mov_write_hdlr_tag(pb, track);
571 mov_write_minf_tag(pb, track);
572 return updateSize(pb, pos);
575 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
577 int64_t maxTrackLenTemp;
578 put_be32(pb, 0x5c); /* size (always 0x5c) */
580 put_be32(pb, 0xf); /* version & flags (track enabled) */
581 put_be32(pb, track->time); /* creation time */
582 put_be32(pb, track->time); /* modification time */
583 put_be32(pb, track->trackID); /* track-id */
584 put_be32(pb, 0); /* reserved */
585 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
586 put_be32(pb, (long)maxTrackLenTemp); /* duration */
588 put_be32(pb, 0); /* reserved */
589 put_be32(pb, 0); /* reserved */
590 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
591 /* Volume, only for audio */
592 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
593 put_be16(pb, 0x0100);
596 put_be16(pb, 0); /* reserved */
598 /* Matrix structure */
599 put_be32(pb, 0x00010000); /* reserved */
600 put_be32(pb, 0x0); /* reserved */
601 put_be32(pb, 0x0); /* reserved */
602 put_be32(pb, 0x0); /* reserved */
603 put_be32(pb, 0x00010000); /* reserved */
604 put_be32(pb, 0x0); /* reserved */
605 put_be32(pb, 0x0); /* reserved */
606 put_be32(pb, 0x0); /* reserved */
607 put_be32(pb, 0x40000000); /* reserved */
609 /* Track width and height, for visual only */
610 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
611 put_be32(pb, track->enc->width*0x10000);
612 put_be32(pb, track->enc->height*0x10000);
621 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
623 int pos = url_ftell(pb);
624 put_be32(pb, 0); /* size */
626 mov_write_tkhd_tag(pb, track);
627 mov_write_mdia_tag(pb, track);
628 return updateSize(pb, pos);
631 /* TODO: Not sorted out, but not necessary either */
632 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
634 put_be32(pb, 0x15); /* size */
636 put_be32(pb, 0); /* version & flags */
637 put_be16(pb, 0x1007);
639 put_be16(pb, 0x4fff);
640 put_be16(pb, 0xfffe);
641 put_be16(pb, 0x01ff);
645 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
647 int maxTrackID = 1, maxTrackLen = 0, i;
648 int64_t maxTrackLenTemp;
650 put_be32(pb, 0x6c); /* size (always 0x6c) */
652 put_be32(pb, 0); /* version & flags */
653 put_be32(pb, mov->time); /* creation time */
654 put_be32(pb, mov->time); /* modification time */
655 put_be32(pb, mov->timescale); /* timescale */
656 for (i=0; i<MAX_STREAMS; i++) {
657 if(mov->tracks[i].entry > 0) {
658 maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
659 if(maxTrackLen < maxTrackLenTemp)
660 maxTrackLen = maxTrackLenTemp;
661 if(maxTrackID < mov->tracks[i].trackID)
662 maxTrackID = mov->tracks[i].trackID;
665 put_be32(pb, maxTrackLen); /* duration of longest track */
667 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
668 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
669 put_be16(pb, 0); /* reserved */
670 put_be32(pb, 0); /* reserved */
671 put_be32(pb, 0); /* reserved */
673 /* Matrix structure */
674 put_be32(pb, 0x00010000); /* reserved */
675 put_be32(pb, 0x0); /* reserved */
676 put_be32(pb, 0x0); /* reserved */
677 put_be32(pb, 0x0); /* reserved */
678 put_be32(pb, 0x00010000); /* reserved */
679 put_be32(pb, 0x0); /* reserved */
680 put_be32(pb, 0x0); /* reserved */
681 put_be32(pb, 0x0); /* reserved */
682 put_be32(pb, 0x40000000); /* reserved */
684 put_be32(pb, 0); /* reserved (preview time) */
685 put_be32(pb, 0); /* reserved (preview duration) */
686 put_be32(pb, 0); /* reserved (poster time) */
687 put_be32(pb, 0); /* reserved (selection time) */
688 put_be32(pb, 0); /* reserved (selection duration) */
689 put_be32(pb, 0); /* reserved (current time) */
690 put_be32(pb, maxTrackID+1); /* Next track id */
694 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov)
698 put_be32(pb, 0); /* size placeholder*/
700 mov->timescale = globalTimescale;
702 for (i=0; i<MAX_STREAMS; i++) {
703 if(mov->tracks[i].entry <= 0) continue;
705 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
706 mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
707 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
709 else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
710 /* If AMR, track timescale = 8000, AMR_WB = 16000 */
711 if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
712 mov->tracks[i].sampleDuration = 160; // Bytes per chunk
713 mov->tracks[i].timescale = 8000;
716 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
717 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
721 mov->tracks[i].trackDuration =
722 mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
723 mov->tracks[i].time = mov->time;
724 mov->tracks[i].trackID = i+1;
727 mov_write_mvhd_tag(pb, mov);
728 //mov_write_iods_tag(pb, mov);
729 for (i=0; i<MAX_STREAMS; i++) {
730 if(mov->tracks[i].entry > 0) {
731 mov_write_trak_tag(pb, &(mov->tracks[i]));
735 return updateSize(pb, pos);
738 int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
740 mov->mdat_pos = url_ftell(pb);
741 put_be32(pb, 0); /* size placeholder*/
746 /* TODO: This needs to be more general */
747 int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
749 put_be32(pb, 0x14 ); /* size */
752 if (!strcmp("3gp", s->oformat->name))
757 put_be32(pb, 0x200 );
759 if (!strcmp("3gp", s->oformat->name))
767 static int mov_write_header(AVFormatContext *s)
769 ByteIOContext *pb = &s->pb;
771 if(s->oformat != NULL) {
772 if(!strcmp("3gp", s->oformat->name) || !strcmp("mp4", s->oformat->name))
773 mov_write_ftyp_tag(pb,s);
776 put_flush_packet(pb);
781 static int Timestamp() {
782 return 1067949799U+(24107*86400); //its the modification time of this line :)
785 static int mov_write_packet(AVFormatContext *s, int stream_index,
786 const uint8_t *buf, int size, int64_t pts)
788 MOVContext *mov = s->priv_data;
789 ByteIOContext *pb = &s->pb;
790 AVCodecContext *enc = &s->streams[stream_index]->codec;
791 MOVTrack* trk = &mov->tracks[stream_index];
793 unsigned int samplesInChunk = 0;
795 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
796 if (!size) return 0; /* Discard 0 sized packets */
798 if (enc->codec_type == CODEC_TYPE_VIDEO ) {
801 else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
802 if( enc->codec_id == CODEC_ID_AMR_NB) {
803 /* We must find out how many AMR blocks there are in one packet */
804 static uint16_t packed_size[16] =
805 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
808 while (len < size && samplesInChunk < 100) {
809 len += packed_size[(buf[len] >> 3) & 0x0F];
813 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
814 samplesInChunk = size/enc->channels;
821 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
822 && trk->vosLen == 0) {
823 assert(enc->extradata_size);
825 trk->vosLen = enc->extradata_size;
826 trk->vosData = av_malloc(trk->vosLen);
827 memcpy(trk->vosData, enc->extradata, trk->vosLen);
830 cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
831 id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
833 if (trk->ents_allocated <= trk->entry) {
834 trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
837 trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
838 if (!trk->cluster[cl])
840 trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
842 if (mov->mdat_written == 0) {
843 mov_write_mdat_tag(pb, mov);
844 mov->mdat_written = 1;
845 mov->time = Timestamp();
848 trk->cluster[cl][id].pos = url_ftell(pb) - mov->movi_list;
849 trk->cluster[cl][id].samplesInChunk = samplesInChunk;
850 trk->cluster[cl][id].size = size;
851 trk->cluster[cl][id].entries = samplesInChunk;
852 if(enc->codec_type == CODEC_TYPE_VIDEO) {
853 trk->cluster[cl][id].key_frame = enc->coded_frame->key_frame;
854 if(enc->coded_frame->pict_type == FF_I_TYPE)
855 trk->hasKeyframes = 1;
859 trk->sampleCount += samplesInChunk;
860 trk->mdat_size += size;
862 put_buffer(pb, buf, size);
864 put_flush_packet(pb);
868 static int mov_write_trailer(AVFormatContext *s)
870 MOVContext *mov = s->priv_data;
871 ByteIOContext *pb = &s->pb;
876 file_size = url_ftell(pb);
879 /* Write size of mdat tag */
880 for (i=0; i<MAX_STREAMS; i++) {
881 if(mov->tracks[i].ents_allocated > 0) {
882 j += mov->tracks[i].mdat_size;
885 url_fseek(pb, mov->mdat_pos, SEEK_SET);
887 url_fseek(pb, file_size, SEEK_SET);
889 mov_write_moov_tag(pb, mov);
891 for (i=0; i<MAX_STREAMS; i++) {
892 for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
893 av_free(mov->tracks[i].cluster[j]);
895 av_free(mov->tracks[i].cluster);
896 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
898 mov->tracks[i].cluster = NULL;
899 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
901 put_flush_packet(pb);
906 static AVOutputFormat mov_oformat = {
919 static AVOutputFormat _3gp_oformat = {
932 static AVOutputFormat mp4_oformat = {
945 int movenc_init(void)
947 av_register_output_format(&mov_oformat);
948 av_register_output_format(&_3gp_oformat);
949 av_register_output_format(&mp4_oformat);