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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #define MOV_INDEX_CLUSTER_SIZE 16384
29 #define globalTimescale 1000
34 #define MODE_PSP 3 // example working PSP command line:
35 // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
38 typedef struct MOVIentry {
39 unsigned int flags, size;
41 unsigned int samplesInChunk;
47 typedef struct MOVIndex {
52 int64_t trackDuration;
67 typedef struct MOVContext {
74 MOVTrack tracks[MAX_STREAMS];
77 //FIXME supprt 64bit varaint with wide placeholders
78 static offset_t updateSize (ByteIOContext *pb, offset_t pos)
80 offset_t curpos = url_ftell(pb);
81 url_fseek(pb, pos, SEEK_SET);
82 put_be32(pb, curpos - pos); /* rewrite size */
83 url_fseek(pb, curpos, SEEK_SET);
88 /* Chunk offset atom */
89 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
92 int mode64 = 0; // use 32 bit size variant if possible
93 offset_t pos = url_ftell(pb);
94 put_be32(pb, 0); /* size */
95 if (pos > UINT32_MAX) {
100 put_be32(pb, 0); /* version & flags */
101 put_be32(pb, track->entry); /* entry count */
102 for (i=0; i<track->entry; i++) {
104 put_be64(pb, track->cluster[i].pos);
106 put_be32(pb, track->cluster[i].pos);
108 return updateSize (pb, pos);
111 /* Sample size atom */
112 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
115 int i, j, entries = 0, tst = -1, oldtst = -1;
117 offset_t pos = url_ftell(pb);
118 put_be32(pb, 0); /* size */
120 put_be32(pb, 0); /* version & flags */
122 for (i=0; i<track->entry; i++) {
123 tst = track->cluster[i].size/track->cluster[i].entries;
124 if(oldtst != -1 && tst != oldtst) {
128 entries += track->cluster[i].entries;
131 int sSize = track->cluster[0].size/track->cluster[0].entries;
132 put_be32(pb, sSize); // sample size
133 put_be32(pb, entries); // sample count
136 put_be32(pb, 0); // sample size
137 put_be32(pb, entries); // sample count
138 for (i=0; i<track->entry; i++) {
139 for ( j=0; j<track->cluster[i].entries; j++) {
140 put_be32(pb, track->cluster[i].size /
141 track->cluster[i].entries);
145 return updateSize (pb, pos);
148 /* Sample to chunk atom */
149 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
151 int index = 0, oldval = -1, i;
152 offset_t entryPos, curpos;
154 offset_t pos = url_ftell(pb);
155 put_be32(pb, 0); /* size */
157 put_be32(pb, 0); // version & flags
158 entryPos = url_ftell(pb);
159 put_be32(pb, track->entry); // entry count
160 for (i=0; i<track->entry; i++) {
161 if(oldval != track->cluster[i].samplesInChunk)
163 put_be32(pb, i+1); // first chunk
164 put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
165 put_be32(pb, 0x1); // sample description index
166 oldval = track->cluster[i].samplesInChunk;
170 curpos = url_ftell(pb);
171 url_fseek(pb, entryPos, SEEK_SET);
172 put_be32(pb, index); // rewrite size
173 url_fseek(pb, curpos, SEEK_SET);
175 return updateSize (pb, pos);
178 /* Sync sample atom */
179 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
181 offset_t curpos, entryPos;
183 offset_t pos = url_ftell(pb);
184 put_be32(pb, 0); // size
186 put_be32(pb, 0); // version & flags
187 entryPos = url_ftell(pb);
188 put_be32(pb, track->entry); // entry count
189 for (i=0; i<track->entry; i++) {
190 if(track->cluster[i].key_frame == 1) {
195 curpos = url_ftell(pb);
196 url_fseek(pb, entryPos, SEEK_SET);
197 put_be32(pb, index); // rewrite size
198 url_fseek(pb, curpos, SEEK_SET);
199 return updateSize (pb, pos);
202 static int mov_write_damr_tag(ByteIOContext *pb)
204 put_be32(pb, 0x11); /* size */
209 //put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
210 //put_be16(pb, 0xa); /* Mode change period (no restriction) */
211 put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
212 put_be16(pb, 1); /* Mode change period (no restriction) */
216 static int mov_write_samr_tag(ByteIOContext *pb)
218 put_be32(pb, 0x11); /* size */
223 put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
224 put_be16(pb, 0x5); /* Mode change period (no restriction) */
228 static int mov_write_enda_tag(ByteIOContext *pb)
232 put_be16(pb, 1); /* little endian */
236 static unsigned int descrLength(unsigned int len)
239 for(i=1; len>>(7*i); i++);
243 static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
245 int i= descrLength(size) - size - 2;
248 put_byte(pb, (size>>(7*i)) | 0x80);
249 put_byte(pb, size & 0x7F);
252 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
254 offset_t pos = url_ftell(pb);
255 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
257 put_be32(pb, 0); // size
259 put_be32(pb, 0); // Version
262 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
264 put_be16(pb, track->trackID);
265 put_byte(pb, 0x00); // flags (= no flags)
267 // DecoderConfig descriptor
268 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
270 // Object type indication
271 put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
273 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
274 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
275 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
276 put_byte(pb, 0x15); // flags (= Audiostream)
278 put_byte(pb, 0x11); // flags (= Visualstream)
280 put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
281 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
283 put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
284 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
285 put_be32(pb, 0); // vbr
287 put_be32(pb, track->enc->rc_max_rate); // avg bitrate
291 // DecoderSpecific info descriptor
292 putDescr(pb, 0x05, track->vosLen);
293 put_buffer(pb, track->vosData, track->vosLen);
298 putDescr(pb, 0x06, 1);
300 return updateSize (pb, pos);
303 static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
305 offset_t pos = url_ftell(pb);
307 put_be32(pb, 0); /* size */
310 put_be32(pb, 12); /* size */
312 put_le32(pb, track->tag);
314 if (track->enc->codec_id == CODEC_ID_AAC) {
315 /* useless atom needed by mplayer, ipod, not needed by quicktime */
316 put_be32(pb, 12); /* size */
319 mov_write_esds_tag(pb, track);
320 } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
321 track->enc->codec_id == CODEC_ID_PCM_S32LE) {
322 mov_write_enda_tag(pb);
323 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
324 mov_write_samr_tag(pb);
327 put_be32(pb, 8); /* size */
328 put_be32(pb, 0); /* null tag */
330 return updateSize (pb, pos);
333 static const CodecTag codec_movaudio_tags[] = {
334 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
335 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
336 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
337 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
338 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
339 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
340 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
341 { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
342 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
343 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
344 { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
345 { CODEC_ID_PCM_S24LE, MKTAG('i', 'n', '2', '4') },
346 { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
347 { CODEC_ID_PCM_S32LE, MKTAG('i', 'n', '3', '2') },
348 { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
349 { CODEC_ID_NONE, 0 },
352 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
354 offset_t pos = url_ftell(pb);
355 int version = track->mode == MODE_MOV &&
356 (track->enc->codec_id == CODEC_ID_AAC ||
357 track->enc->codec_id == CODEC_ID_MP3 ||
358 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
359 track->enc->codec_id == CODEC_ID_PCM_S24LE);
361 put_be32(pb, 0); /* size */
362 put_le32(pb, track->tag); // store it byteswapped
363 put_be32(pb, 0); /* Reserved */
364 put_be16(pb, 0); /* Reserved */
365 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
367 /* SoundDescription */
368 put_be16(pb, version); /* Version */
369 put_be16(pb, 0); /* Revision level */
370 put_be32(pb, 0); /* Reserved */
372 put_be16(pb, track->enc->channels); /* Number of channels */
373 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
374 to be a good way to get number of bits of audio */
375 put_be16(pb, 0x10); /* Reserved */
377 if(track->enc->codec_id == CODEC_ID_AAC ||
378 track->enc->codec_id == CODEC_ID_MP3 ||
379 track->enc->codec_id == CODEC_ID_AMR_NB) {
380 put_be16(pb, 0xfffe); /* compression ID (vbr)*/
382 put_be16(pb, 0); /* compression ID (= 0) */
384 put_be16(pb, 0); /* packet size (= 0) */
385 put_be16(pb, track->timescale); /* Time scale */
386 put_be16(pb, 0); /* Reserved */
389 /* SoundDescription V1 extended info */
390 put_be32(pb, track->enc->frame_size); /* Samples per packet */
391 /* Parameters tested on quicktime 6.5, 7 */
392 put_be32(pb, 1); /* Bytes per packet */
393 /* FIXME not correct */
394 /* 8 is the min value needed for in32 to work with quicktime 6.5 */
395 /* Value ignored by other codecs currently supported (others might need it) */
396 put_be32(pb, 8); /* Bytes per frame */
397 put_be32(pb, 2); /* Bytes per sample */
400 if(track->mode == MODE_MOV &&
401 (track->enc->codec_id == CODEC_ID_AAC ||
402 track->enc->codec_id == CODEC_ID_AMR_NB ||
403 track->enc->codec_id == CODEC_ID_PCM_S24LE ||
404 track->enc->codec_id == CODEC_ID_PCM_S32LE))
405 mov_write_wave_tag(pb, track);
406 else if(track->enc->codec_id == CODEC_ID_AAC)
407 mov_write_esds_tag(pb, track);
408 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
409 mov_write_damr_tag(pb);
411 return updateSize (pb, pos);
414 static int mov_write_d263_tag(ByteIOContext *pb)
416 put_be32(pb, 0xf); /* size */
424 /* TODO: No idea about these values */
425 static int mov_write_svq3_tag(ByteIOContext *pb)
431 put_be32(pb, 0xe2c0211d);
432 put_be32(pb, 0xc0000000);
437 static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
439 uint8_t *a = p + 4 - ((int)p & 3);
441 for( end -= 3; p < a && p < end; p++ ) {
442 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
446 for( end -= 3; p < end; p += 4 ) {
447 uint32_t x = *(uint32_t*)p;
448 // if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
449 // if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
450 if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
452 if( p[0] == 0 && p[2] == 1 )
454 if( p[2] == 0 && p[3] == 1 )
458 if( p[2] == 0 && p[4] == 1 )
460 if( p[4] == 0 && p[5] == 1 )
466 for( end += 3; p < end; p++ ) {
467 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
474 static void avc_parse_nal_units(uint8_t **buf, int *size)
478 uint8_t *end = p + *size;
479 uint8_t *nal_start, *nal_end;
481 url_open_dyn_buf(&pb);
482 nal_start = avc_find_startcode(p, end);
483 while (nal_start < end) {
484 while(!*(nal_start++));
485 nal_end = avc_find_startcode(nal_start, end);
486 put_be32(&pb, nal_end - nal_start);
487 put_buffer(&pb, nal_start, nal_end - nal_start);
491 *size = url_close_dyn_buf(&pb, buf);
494 static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
496 offset_t pos = url_ftell(pb);
500 if (track->vosLen > 6) {
501 /* check for h264 start code */
502 if (BE_32(track->vosData) == 0x00000001) {
504 uint32_t sps_size=0, pps_size=0;
505 uint8_t *sps=0, *pps=0;
507 avc_parse_nal_units(&track->vosData, &track->vosLen);
508 buf = track->vosData;
509 end = track->vosData + track->vosLen;
511 /* look for sps and pps */
516 nal_type = buf[4] & 0x1f;
517 if (nal_type == 7) { /* SPS */
520 } else if (nal_type == 8) { /* PPS */
529 put_byte(pb, 1); /* version */
530 put_byte(pb, sps[1]); /* profile */
531 put_byte(pb, sps[2]); /* profile compat */
532 put_byte(pb, sps[3]); /* level */
533 put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
534 put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
536 put_be16(pb, sps_size);
537 put_buffer(pb, sps, sps_size);
538 put_byte(pb, 1); /* number of pps */
539 put_be16(pb, pps_size);
540 put_buffer(pb, pps, pps_size);
542 put_buffer(pb, track->vosData, track->vosLen);
545 return updateSize(pb, pos);
548 static const CodecTag codec_movvideo_tags[] = {
549 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
550 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
551 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
552 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
553 { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
554 /* special handling in mov_find_video_codec_tag */
555 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
556 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
557 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL */
558 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC */
559 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL */
560 { CODEC_ID_NONE, 0 },
563 static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
565 int tag = track->enc->codec_tag;
567 if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
568 if (track->enc->height == 480) { /* NTSC */
569 if (track->enc->pix_fmt == PIX_FMT_YUV422P)
570 tag = MKTAG('d', 'v', '5', 'n');
572 tag = MKTAG('d', 'v', 'c', ' ');
573 } else { /* assume PAL */
574 if (track->enc->pix_fmt == PIX_FMT_YUV422P)
575 tag = MKTAG('d', 'v', '5', 'p');
576 else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
577 tag = MKTAG('d', 'v', 'c', 'p');
579 tag = MKTAG('d', 'v', 'p', 'p');
582 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
585 // if no mac fcc found, try with Microsoft tags
587 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
589 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
596 static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
598 int tag = track->enc->codec_tag;
600 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
602 // if no mac fcc found, try with Microsoft tags
604 int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
606 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
607 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
614 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
616 offset_t pos = url_ftell(pb);
617 char compressor_name[32];
619 put_be32(pb, 0); /* size */
620 put_le32(pb, track->tag); // store it byteswapped
621 put_be32(pb, 0); /* Reserved */
622 put_be16(pb, 0); /* Reserved */
623 put_be16(pb, 1); /* Data-reference index */
625 put_be16(pb, 0); /* Codec stream version */
626 put_be16(pb, 0); /* Codec stream revision (=0) */
627 put_tag(pb, "FFMP"); /* Vendor */
628 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
629 put_be32(pb, 0); /* Temporal Quality */
630 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
632 put_be32(pb, 0x200); /* Temporal Quality = normal */
633 put_be32(pb, 0x200); /* Spatial Quality = normal */
635 put_be16(pb, track->enc->width); /* Video width */
636 put_be16(pb, track->enc->height); /* Video height */
637 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
638 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
639 put_be32(pb, 0); /* Data size (= 0) */
640 put_be16(pb, 1); /* Frame count (= 1) */
642 memset(compressor_name,0,32);
643 if (track->enc->codec && track->enc->codec->name)
644 strncpy(compressor_name,track->enc->codec->name,31);
645 put_byte(pb, strlen(compressor_name));
646 put_buffer(pb, compressor_name, 31);
648 put_be16(pb, 0x18); /* Reserved */
649 put_be16(pb, 0xffff); /* Reserved */
650 if(track->enc->codec_id == CODEC_ID_MPEG4)
651 mov_write_esds_tag(pb, track);
652 else if(track->enc->codec_id == CODEC_ID_H263)
653 mov_write_d263_tag(pb);
654 else if(track->enc->codec_id == CODEC_ID_SVQ3)
655 mov_write_svq3_tag(pb);
656 else if(track->enc->codec_id == CODEC_ID_H264)
657 mov_write_avcc_tag(pb, track);
659 return updateSize (pb, pos);
662 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
664 offset_t pos = url_ftell(pb);
665 put_be32(pb, 0); /* size */
667 put_be32(pb, 0); /* version & flags */
668 put_be32(pb, 1); /* entry count */
669 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
670 mov_write_video_tag(pb, track);
671 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
672 mov_write_audio_tag(pb, track);
673 return updateSize(pb, pos);
676 static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
678 Time2Sample *ctts_entries;
679 uint32_t entries = 0;
683 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
684 ctts_entries[0].count = 1;
685 ctts_entries[0].duration = track->cluster[0].cts;
686 for (i=1; i<track->entry; i++) {
687 if (track->cluster[i].cts == ctts_entries[entries].duration) {
688 ctts_entries[entries].count++; /* compress */
691 ctts_entries[entries].duration = track->cluster[i].cts;
692 ctts_entries[entries].count = 1;
695 entries++; /* last one */
696 atom_size = 16 + (entries * 8);
697 put_be32(pb, atom_size); /* size */
699 put_be32(pb, 0); /* version & flags */
700 put_be32(pb, entries); /* entry count */
701 for (i=0; i<entries; i++) {
702 put_be32(pb, ctts_entries[i].count);
703 put_be32(pb, ctts_entries[i].duration);
705 av_free(ctts_entries);
710 /* Time to sample atom */
711 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
713 put_be32(pb, 0x18); /* size */
715 put_be32(pb, 0); /* version & flags */
716 put_be32(pb, 1); /* entry count */
718 put_be32(pb, track->sampleCount); /* sample count */
719 put_be32(pb, track->sampleDuration); /* sample duration */
723 static int mov_write_dref_tag(ByteIOContext *pb)
725 put_be32(pb, 28); /* size */
727 put_be32(pb, 0); /* version & flags */
728 put_be32(pb, 1); /* entry count */
730 put_be32(pb, 0xc); /* size */
732 put_be32(pb, 1); /* version & flags */
737 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
739 offset_t pos = url_ftell(pb);
740 put_be32(pb, 0); /* size */
742 mov_write_stsd_tag(pb, track);
743 mov_write_stts_tag(pb, track);
744 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
745 track->hasKeyframes < track->entry)
746 mov_write_stss_tag(pb, track);
747 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
749 mov_write_ctts_tag(pb, track);
750 mov_write_stsc_tag(pb, track);
751 mov_write_stsz_tag(pb, track);
752 mov_write_stco_tag(pb, track);
753 return updateSize(pb, pos);
756 static int mov_write_dinf_tag(ByteIOContext *pb)
758 offset_t pos = url_ftell(pb);
759 put_be32(pb, 0); /* size */
761 mov_write_dref_tag(pb);
762 return updateSize(pb, pos);
765 static int mov_write_smhd_tag(ByteIOContext *pb)
767 put_be32(pb, 16); /* size */
769 put_be32(pb, 0); /* version & flags */
770 put_be16(pb, 0); /* reserved (balance, normally = 0) */
771 put_be16(pb, 0); /* reserved */
775 static int mov_write_vmhd_tag(ByteIOContext *pb)
777 put_be32(pb, 0x14); /* size (always 0x14) */
779 put_be32(pb, 0x01); /* version & flags */
780 put_be64(pb, 0); /* reserved (graphics mode = copy) */
784 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
786 const char *descr, *hdlr, *hdlr_type;
787 offset_t pos = url_ftell(pb);
789 if (!track) { /* no media --> data handler */
792 descr = "DataHandler";
794 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
795 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
797 descr = "VideoHandler";
800 descr = "SoundHandler";
804 put_be32(pb, 0); /* size */
806 put_be32(pb, 0); /* Version & flags */
807 put_buffer(pb, hdlr, 4); /* handler */
808 put_tag(pb, hdlr_type); /* handler type */
809 put_be32(pb ,0); /* reserved */
810 put_be32(pb ,0); /* reserved */
811 put_be32(pb ,0); /* reserved */
812 put_byte(pb, strlen(descr)); /* string counter */
813 put_buffer(pb, descr, strlen(descr)); /* handler description */
814 return updateSize(pb, pos);
817 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
819 offset_t pos = url_ftell(pb);
820 put_be32(pb, 0); /* size */
822 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
823 mov_write_vmhd_tag(pb);
825 mov_write_smhd_tag(pb);
826 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
827 mov_write_hdlr_tag(pb, NULL);
828 mov_write_dinf_tag(pb);
829 mov_write_stbl_tag(pb, track);
830 return updateSize(pb, pos);
833 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
835 int version = track->trackDuration < INT32_MAX ? 0 : 1;
837 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
839 put_byte(pb, version);
840 put_be24(pb, 0); /* flags */
842 put_be64(pb, track->time);
843 put_be64(pb, track->time);
845 put_be32(pb, track->time); /* creation time */
846 put_be32(pb, track->time); /* modification time */
848 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
849 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
850 put_be16(pb, track->language); /* language */
851 put_be16(pb, 0); /* reserved (quality) */
855 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
857 offset_t pos = url_ftell(pb);
858 put_be32(pb, 0); /* size */
860 mov_write_mdhd_tag(pb, track);
861 mov_write_hdlr_tag(pb, track);
862 mov_write_minf_tag(pb, track);
863 return updateSize(pb, pos);
866 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
868 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
869 int version = duration < INT32_MAX ? 0 : 1;
871 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
873 put_byte(pb, version);
874 put_be24(pb, 0xf); /* flags (track enabled) */
876 put_be64(pb, track->time);
877 put_be64(pb, track->time);
879 put_be32(pb, track->time); /* creation time */
880 put_be32(pb, track->time); /* modification time */
882 put_be32(pb, track->trackID); /* track-id */
883 put_be32(pb, 0); /* reserved */
884 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
886 put_be32(pb, 0); /* reserved */
887 put_be32(pb, 0); /* reserved */
888 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
889 /* Volume, only for audio */
890 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
891 put_be16(pb, 0x0100);
894 put_be16(pb, 0); /* reserved */
896 /* Matrix structure */
897 put_be32(pb, 0x00010000); /* reserved */
898 put_be32(pb, 0x0); /* reserved */
899 put_be32(pb, 0x0); /* reserved */
900 put_be32(pb, 0x0); /* reserved */
901 put_be32(pb, 0x00010000); /* reserved */
902 put_be32(pb, 0x0); /* reserved */
903 put_be32(pb, 0x0); /* reserved */
904 put_be32(pb, 0x0); /* reserved */
905 put_be32(pb, 0x40000000); /* reserved */
907 /* Track width and height, for visual only */
908 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
909 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
910 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
911 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
912 put_be32(pb, track->enc->height*0x10000);
921 // This box seems important for the psp playback ... without it the movie seems to hang
922 static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
924 put_be32(pb, 0x24); /* size */
926 put_be32(pb, 0x1c); /* size */
931 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
933 if (track->hasBframes)
934 put_be32(pb, track->sampleDuration); /* first pts is 1 */
937 put_be32(pb, 0x00010000);
941 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
942 static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
944 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
947 put_be32(pb, 0x21d24fce);
948 put_be32(pb, 0xbb88695c);
949 put_be32(pb, 0xfac9c740);
950 put_be32(pb, 0x1c); // another size here!
952 put_be32(pb, 0x00010012);
954 put_be32(pb, 0x55c40000);
960 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
962 offset_t pos = url_ftell(pb);
963 put_be32(pb, 0); /* size */
965 mov_write_tkhd_tag(pb, track);
966 if (track->mode == MODE_PSP || track->hasBframes)
967 mov_write_edts_tag(pb, track); // PSP Movies require edts box
968 mov_write_mdia_tag(pb, track);
969 if (track->mode == MODE_PSP)
970 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
971 return updateSize(pb, pos);
975 /* TODO: Not sorted out, but not necessary either */
976 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
978 put_be32(pb, 0x15); /* size */
980 put_be32(pb, 0); /* version & flags */
981 put_be16(pb, 0x1007);
983 put_be16(pb, 0x4fff);
984 put_be16(pb, 0xfffe);
985 put_be16(pb, 0x01ff);
990 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
992 int maxTrackID = 1, i;
993 int64_t maxTrackLenTemp, maxTrackLen = 0;
996 for (i=0; i<mov->nb_streams; i++) {
997 if(mov->tracks[i].entry > 0) {
998 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
999 if(maxTrackLen < maxTrackLenTemp)
1000 maxTrackLen = maxTrackLenTemp;
1001 if(maxTrackID < mov->tracks[i].trackID)
1002 maxTrackID = mov->tracks[i].trackID;
1006 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1007 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1008 put_tag(pb, "mvhd");
1009 put_byte(pb, version);
1010 put_be24(pb, 0); /* flags */
1012 put_be64(pb, mov->time);
1013 put_be64(pb, mov->time);
1015 put_be32(pb, mov->time); /* creation time */
1016 put_be32(pb, mov->time); /* modification time */
1018 put_be32(pb, mov->timescale); /* timescale */
1019 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1021 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1022 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1023 put_be16(pb, 0); /* reserved */
1024 put_be32(pb, 0); /* reserved */
1025 put_be32(pb, 0); /* reserved */
1027 /* Matrix structure */
1028 put_be32(pb, 0x00010000); /* reserved */
1029 put_be32(pb, 0x0); /* reserved */
1030 put_be32(pb, 0x0); /* reserved */
1031 put_be32(pb, 0x0); /* reserved */
1032 put_be32(pb, 0x00010000); /* reserved */
1033 put_be32(pb, 0x0); /* reserved */
1034 put_be32(pb, 0x0); /* reserved */
1035 put_be32(pb, 0x0); /* reserved */
1036 put_be32(pb, 0x40000000); /* reserved */
1038 put_be32(pb, 0); /* reserved (preview time) */
1039 put_be32(pb, 0); /* reserved (preview duration) */
1040 put_be32(pb, 0); /* reserved (poster time) */
1041 put_be32(pb, 0); /* reserved (selection time) */
1042 put_be32(pb, 0); /* reserved (selection duration) */
1043 put_be32(pb, 0); /* reserved (current time) */
1044 put_be32(pb, maxTrackID+1); /* Next track id */
1048 static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1051 offset_t pos = url_ftell(pb);
1052 put_be32(pb, 0); /* size */
1053 put_tag(pb, "hdlr");
1056 put_tag(pb, "mdir");
1057 put_tag(pb, "appl");
1061 return updateSize(pb, pos);
1064 /* helper function to write a data tag with the specified string as data */
1065 static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1068 offset_t pos = url_ftell(pb);
1069 put_be32(pb, 0); /* size */
1070 put_tag(pb, "data");
1073 put_buffer(pb, data, strlen(data));
1074 return updateSize(pb, pos);
1076 put_be16(pb, strlen(data)); /* string length */
1078 put_buffer(pb, data, strlen(data));
1079 return strlen(data) + 4;
1083 static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1085 if ( value && value[0] ) {
1086 offset_t pos = url_ftell(pb);
1087 put_be32(pb, 0); /* size */
1089 mov_write_string_data_tag(pb, value, long_style);
1090 size= updateSize(pb, pos);
1096 static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1100 snprintf(year_str, sizeof(year_str), "%04d", year);
1101 return mov_write_string_tag(pb, "\251day", year_str, long_style);
1106 /* iTunes track number */
1107 static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1112 offset_t pos = url_ftell(pb);
1113 put_be32(pb, 0); /* size */
1114 put_tag(pb, "trkn");
1116 offset_t pos = url_ftell(pb);
1117 put_be32(pb, 0); /* size */
1118 put_tag(pb, "data");
1119 put_be32(pb, 0); // 8 bytes empty
1121 put_be16(pb, 0); // empty
1122 put_be16(pb, s->track); // track number
1123 put_be16(pb, 0); // total track number
1124 put_be16(pb, 0); // empty
1125 updateSize(pb, pos);
1127 size = updateSize(pb, pos);
1132 /* iTunes meta data list */
1133 static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1136 offset_t pos = url_ftell(pb);
1137 put_be32(pb, 0); /* size */
1138 put_tag(pb, "ilst");
1139 mov_write_string_tag(pb, "\251nam", s->title , 1);
1140 mov_write_string_tag(pb, "\251ART", s->author , 1);
1141 mov_write_string_tag(pb, "\251wrt", s->author , 1);
1142 mov_write_string_tag(pb, "\251alb", s->album , 1);
1143 mov_write_day_tag(pb, s->year ,1);
1144 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1145 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1146 mov_write_string_tag(pb, "\251cmt", s->comment , 1);
1147 mov_write_string_tag(pb, "\251gen", s->genre , 1);
1148 mov_write_trkn_tag(pb, mov, s);
1149 return updateSize(pb, pos);
1152 /* iTunes meta data tag */
1153 static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1158 // only save meta tag if required
1159 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1160 s->comment[0] || s->genre[0] || s->track ) {
1161 offset_t pos = url_ftell(pb);
1162 put_be32(pb, 0); /* size */
1163 put_tag(pb, "meta");
1165 mov_write_itunes_hdlr_tag(pb, mov, s);
1166 mov_write_ilst_tag(pb, mov, s);
1167 size = updateSize(pb, pos);
1172 static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1175 offset_t pos = url_ftell(pb);
1178 put_be32(pb, 0); /* size */
1179 put_tag(pb, "udta");
1181 /* iTunes meta data */
1182 mov_write_meta_tag(pb, mov, s);
1184 if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff isnt valid in mp4
1186 for (i=0; i<mov->nb_streams; i++) {
1187 if(mov->tracks[i].entry <= 0) continue;
1188 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1189 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1190 mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1195 mov_write_string_tag(pb, "\251nam", s->title , 0);
1196 mov_write_string_tag(pb, "\251aut", s->author , 0);
1197 mov_write_string_tag(pb, "\251alb", s->album , 0);
1198 mov_write_day_tag(pb, s->year, 0);
1199 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1200 mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1201 mov_write_string_tag(pb, "\251des", s->comment , 0);
1202 mov_write_string_tag(pb, "\251gen", s->genre , 0);
1205 return updateSize(pb, pos);
1209 static size_t ascii_to_wc (ByteIOContext *pb, char *b, size_t n)
1213 for (i = 0; i < n - 1; i++) {
1215 if (! (0x20 <= c && c <= 0x7f ))
1223 static uint16_t language_code (const char *str)
1225 return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1228 static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1231 offset_t pos, curpos;
1235 pos = url_ftell(pb);
1236 put_be32(pb, 0); /* size placeholder*/
1237 put_tag(pb, "uuid");
1238 put_tag(pb, "USMT");
1239 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1240 put_be32(pb, 0xbb88695c );
1241 put_be32(pb, 0xfac9c740 );
1244 put_be32(pb, 0); /* size placeholder*/
1245 put_tag(pb, "MTDT");
1250 put_be16(pb, 0x0C); /* size */
1251 put_be32(pb, 0x0B); /* type */
1252 put_be16(pb, language_code("und")); /* language */
1253 put_be16(pb, 0x0); /* ? */
1254 put_be16(pb, 0x021C); /* data */
1258 len = strlen(LIBAVCODEC_IDENT)+1;
1259 put_be16(pb, len*2+10); /* size */
1260 put_be32(pb, 0x04); /* type */
1261 put_be16(pb, language_code("eng")); /* language */
1262 put_be16(pb, 0x01); /* ? */
1263 ascii_to_wc(pb, LIBAVCODEC_IDENT, len);
1267 len = strlen(s->title)+1;
1268 put_be16(pb, len*2+10); /* size */
1269 put_be32(pb, 0x01); /* type */
1270 put_be16(pb, language_code("eng")); /* language */
1271 put_be16(pb, 0x01); /* ? */
1272 ascii_to_wc (pb, s->title, len);
1276 // snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
1277 len = strlen("2006/04/01 11:11:11")+1;
1278 put_be16(pb, len*2+10); /* size */
1279 put_be32(pb, 0x03); /* type */
1280 put_be16(pb, language_code("und")); /* language */
1281 put_be16(pb, 0x01); /* ? */
1282 ascii_to_wc (pb, "2006/04/01 11:11:11", len);
1286 curpos = url_ftell(pb);
1287 url_fseek(pb, pos, SEEK_SET);
1289 url_fseek(pb, pos+24, SEEK_SET);
1290 put_be32(pb, size-24);
1291 url_fseek(pb, curpos, SEEK_SET);
1297 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1301 offset_t pos = url_ftell(pb);
1302 put_be32(pb, 0); /* size placeholder*/
1303 put_tag(pb, "moov");
1304 mov->timescale = globalTimescale;
1306 for (i=0; i<mov->nb_streams; i++) {
1307 if(mov->tracks[i].entry <= 0) continue;
1309 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
1310 mov->tracks[i].timescale = mov->tracks[i].enc->time_base.den;
1311 mov->tracks[i].sampleDuration = mov->tracks[i].enc->time_base.num;
1312 } else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
1313 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1314 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1317 mov->tracks[i].trackDuration =
1318 (int64_t)mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
1319 mov->tracks[i].time = mov->time;
1320 mov->tracks[i].trackID = i+1;
1323 mov_write_mvhd_tag(pb, mov);
1324 //mov_write_iods_tag(pb, mov);
1325 for (i=0; i<mov->nb_streams; i++) {
1326 if(mov->tracks[i].entry > 0) {
1327 mov_write_trak_tag(pb, &(mov->tracks[i]));
1331 if (mov->mode == MODE_PSP)
1332 mov_write_uuidusmt_tag(pb, s);
1334 mov_write_udta_tag(pb, mov, s);
1336 return updateSize(pb, pos);
1339 static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1341 put_be32(pb, 8); // placeholder for extended size field (64 bit)
1342 put_tag(pb, "wide");
1344 mov->mdat_pos = url_ftell(pb);
1345 put_be32(pb, 0); /* size placeholder*/
1346 put_tag(pb, "mdat");
1350 /* TODO: This needs to be more general */
1351 static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1353 MOVContext *mov = s->priv_data;
1355 put_be32(pb, 0x14 ); /* size */
1356 put_tag(pb, "ftyp");
1358 if ( mov->mode == MODE_3GP )
1359 put_tag(pb, "3gp4");
1360 else if ( mov->mode == MODE_3G2 )
1361 put_tag(pb, "3g2a");
1362 else if ( mov->mode == MODE_PSP )
1363 put_tag(pb, "MSNV");
1364 else if ( mov->mode == MODE_MP4 )
1365 put_tag(pb, "isom");
1369 put_be32(pb, 0x200 );
1371 if ( mov->mode == MODE_3GP )
1372 put_tag(pb, "3gp4");
1373 else if ( mov->mode == MODE_3G2 )
1374 put_tag(pb, "3g2a");
1375 else if ( mov->mode == MODE_PSP )
1376 put_tag(pb, "MSNV");
1377 else if ( mov->mode == MODE_MP4 )
1378 put_tag(pb, "mp41");
1383 static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1385 AVCodecContext *VideoCodec = s->streams[0]->codec;
1386 AVCodecContext *AudioCodec = s->streams[1]->codec;
1387 int AudioRate = AudioCodec->sample_rate;
1388 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1389 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1390 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1392 put_be32(pb, 0x94 ); /* size */
1393 put_tag(pb, "uuid");
1394 put_tag(pb, "PROF");
1396 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1397 put_be32(pb, 0xbb88695c );
1398 put_be32(pb, 0xfac9c740 );
1400 put_be32(pb, 0x0 ); /* ? */
1401 put_be32(pb, 0x3 ); /* 3 sections ? */
1403 put_be32(pb, 0x14 ); /* size */
1404 put_tag(pb, "FPRF");
1405 put_be32(pb, 0x0 ); /* ? */
1406 put_be32(pb, 0x0 ); /* ? */
1407 put_be32(pb, 0x0 ); /* ? */
1409 put_be32(pb, 0x2c ); /* size */
1410 put_tag(pb, "APRF"); /* audio */
1412 put_be32(pb, 0x2 ); /* TrackID */
1413 put_tag(pb, "mp4a");
1414 put_be32(pb, 0x20f );
1416 put_be32(pb, audio_kbitrate);
1417 put_be32(pb, audio_kbitrate);
1418 put_be32(pb, AudioRate );
1419 put_be32(pb, AudioCodec->channels );
1421 put_be32(pb, 0x34 ); /* size */
1422 put_tag(pb, "VPRF"); /* video */
1424 put_be32(pb, 0x1 ); /* TrackID */
1425 if (VideoCodec->codec_id == CODEC_ID_H264) {
1426 put_tag(pb, "avc1");
1427 put_be16(pb, 0x014D );
1428 put_be16(pb, 0x0015 );
1430 put_tag(pb, "mp4v");
1431 put_be16(pb, 0x0000 );
1432 put_be16(pb, 0x0103 );
1435 put_be32(pb, video_kbitrate);
1436 put_be32(pb, video_kbitrate);
1437 put_be32(pb, FrameRate);
1438 put_be32(pb, FrameRate);
1439 put_be16(pb, VideoCodec->width);
1440 put_be16(pb, VideoCodec->height);
1441 put_be32(pb, 0x010001); /* ? */
1444 static int mov_write_header(AVFormatContext *s)
1446 ByteIOContext *pb = &s->pb;
1447 MOVContext *mov = s->priv_data;
1450 /* Default mode == MP4 */
1451 mov->mode = MODE_MP4;
1453 if (s->oformat != NULL) {
1454 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1455 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1456 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1457 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1459 mov_write_ftyp_tag(pb,s);
1460 if ( mov->mode == MODE_PSP ) {
1461 if ( s->nb_streams != 2 ) {
1462 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1465 mov_write_uuidprof_tag(pb,s);
1469 for(i=0; i<s->nb_streams; i++){
1470 AVStream *st= s->streams[i];
1471 MOVTrack *track= &mov->tracks[i];
1473 track->enc = st->codec;
1474 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1475 track->tag = mov_find_video_codec_tag(s, track);
1476 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1477 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1478 track->tag = mov_find_audio_codec_tag(s, track);
1479 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1481 track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1482 track->mode = mov->mode;
1485 mov_write_mdat_tag(pb, mov);
1486 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1487 mov->nb_streams = s->nb_streams;
1489 put_flush_packet(pb);
1494 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1496 MOVContext *mov = s->priv_data;
1497 ByteIOContext *pb = &s->pb;
1498 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1499 AVCodecContext *enc = trk->enc;
1500 unsigned int samplesInChunk = 0;
1501 int size= pkt->size;
1503 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1504 if (!size) return 0; /* Discard 0 sized packets */
1506 if (enc->codec_type == CODEC_TYPE_AUDIO) {
1507 switch (enc->codec_id) {
1508 case CODEC_ID_AMR_NB:
1509 { /* We must find out how many AMR blocks there are in one packet */
1510 static uint16_t packed_size[16] =
1511 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1514 while (len < size && samplesInChunk < 100) {
1515 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1520 case CODEC_ID_PCM_MULAW:
1521 case CODEC_ID_PCM_ALAW:
1522 samplesInChunk = size/enc->channels;
1524 case CODEC_ID_PCM_S16BE:
1525 case CODEC_ID_PCM_S16LE:
1526 samplesInChunk = size/(2*enc->channels);
1528 case CODEC_ID_PCM_S24BE:
1529 case CODEC_ID_PCM_S24LE:
1530 samplesInChunk = size/(3*enc->channels);
1532 case CODEC_ID_PCM_S32BE:
1533 case CODEC_ID_PCM_S32LE:
1534 samplesInChunk = size/(4*enc->channels);
1543 /* copy extradata if it exists */
1544 if (trk->vosLen == 0 && enc->extradata_size > 0) {
1545 trk->vosLen = enc->extradata_size;
1546 trk->vosData = av_malloc(trk->vosLen);
1547 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1550 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1551 /* from x264 or from bytestream h264 */
1552 /* nal reformating needed */
1553 avc_parse_nal_units(&pkt->data, &pkt->size);
1558 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1559 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1564 trk->cluster[trk->entry].pos = url_ftell(pb);
1565 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1566 trk->cluster[trk->entry].size = size;
1567 trk->cluster[trk->entry].entries = samplesInChunk;
1568 if(enc->codec_type == CODEC_TYPE_VIDEO) {
1569 if (pkt->dts != pkt->pts)
1570 trk->hasBframes = 1;
1571 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1572 trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1573 if(trk->cluster[trk->entry].key_frame)
1574 trk->hasKeyframes++;
1577 trk->sampleCount += samplesInChunk;
1578 mov->mdat_size += size;
1580 put_buffer(pb, pkt->data, size);
1582 put_flush_packet(pb);
1586 static int mov_write_trailer(AVFormatContext *s)
1588 MOVContext *mov = s->priv_data;
1589 ByteIOContext *pb = &s->pb;
1593 offset_t moov_pos = url_ftell(pb);
1595 /* Write size of mdat tag */
1596 if (mov->mdat_size+8 <= UINT32_MAX) {
1597 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1598 put_be32(pb, mov->mdat_size+8);
1600 /* overwrite 'wide' placeholder atom */
1601 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1602 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1603 put_tag(pb, "mdat");
1604 put_be64(pb, mov->mdat_size+16);
1606 url_fseek(pb, moov_pos, SEEK_SET);
1608 mov_write_moov_tag(pb, mov, s);
1610 for (i=0; i<mov->nb_streams; i++) {
1611 av_freep(&mov->tracks[i].cluster);
1613 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1617 put_flush_packet(pb);
1622 static AVOutputFormat mov_oformat = {
1633 .flags = AVFMT_GLOBALHEADER,
1636 static AVOutputFormat _3gp_oformat = {
1647 .flags = AVFMT_GLOBALHEADER,
1650 static AVOutputFormat mp4_oformat = {
1661 .flags = AVFMT_GLOBALHEADER,
1664 static AVOutputFormat psp_oformat = {
1675 .flags = AVFMT_GLOBALHEADER,
1678 static AVOutputFormat _3g2_oformat = {
1689 .flags = AVFMT_GLOBALHEADER,
1692 int movenc_init(void)
1694 av_register_output_format(&mov_oformat);
1695 av_register_output_format(&_3gp_oformat);
1696 av_register_output_format(&mp4_oformat);
1697 av_register_output_format(&psp_oformat);
1698 av_register_output_format(&_3g2_oformat);