3 * Copyright (c) 2003 Thomas Raivio.
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #define MOV_INDEX_CLUSTER_SIZE 16384
31 #define globalTimescale 1000
36 #define MODE_PSP 3 // example working PSP command line:
37 // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
40 typedef struct MOVIentry {
41 unsigned int flags, size;
43 unsigned int samplesInChunk;
50 typedef struct MOVIndex {
55 int64_t trackDuration;
72 typedef struct MOVContext {
79 MOVTrack tracks[MAX_STREAMS];
82 //FIXME supprt 64bit varaint with wide placeholders
83 static offset_t updateSize (ByteIOContext *pb, offset_t pos)
85 offset_t curpos = url_ftell(pb);
86 url_fseek(pb, pos, SEEK_SET);
87 put_be32(pb, curpos - pos); /* rewrite size */
88 url_fseek(pb, curpos, SEEK_SET);
93 /* Chunk offset atom */
94 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
97 int mode64 = 0; // use 32 bit size variant if possible
98 offset_t pos = url_ftell(pb);
99 put_be32(pb, 0); /* size */
100 if (pos > UINT32_MAX) {
105 put_be32(pb, 0); /* version & flags */
106 put_be32(pb, track->entry); /* entry count */
107 for (i=0; i<track->entry; i++) {
109 put_be64(pb, track->cluster[i].pos);
111 put_be32(pb, track->cluster[i].pos);
113 return updateSize (pb, pos);
116 /* Sample size atom */
117 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
120 int i, j, entries = 0, tst = -1, oldtst = -1;
122 offset_t pos = url_ftell(pb);
123 put_be32(pb, 0); /* size */
125 put_be32(pb, 0); /* version & flags */
127 for (i=0; i<track->entry; i++) {
128 tst = track->cluster[i].size/track->cluster[i].entries;
129 if(oldtst != -1 && tst != oldtst) {
133 entries += track->cluster[i].entries;
136 int sSize = track->cluster[0].size/track->cluster[0].entries;
137 put_be32(pb, sSize); // sample size
138 put_be32(pb, entries); // sample count
141 put_be32(pb, 0); // sample size
142 put_be32(pb, entries); // sample count
143 for (i=0; i<track->entry; i++) {
144 for ( j=0; j<track->cluster[i].entries; j++) {
145 put_be32(pb, track->cluster[i].size /
146 track->cluster[i].entries);
150 return updateSize (pb, pos);
153 /* Sample to chunk atom */
154 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
156 int index = 0, oldval = -1, i;
157 offset_t entryPos, curpos;
159 offset_t pos = url_ftell(pb);
160 put_be32(pb, 0); /* size */
162 put_be32(pb, 0); // version & flags
163 entryPos = url_ftell(pb);
164 put_be32(pb, track->entry); // entry count
165 for (i=0; i<track->entry; i++) {
166 if(oldval != track->cluster[i].samplesInChunk)
168 put_be32(pb, i+1); // first chunk
169 put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
170 put_be32(pb, 0x1); // sample description index
171 oldval = track->cluster[i].samplesInChunk;
175 curpos = url_ftell(pb);
176 url_fseek(pb, entryPos, SEEK_SET);
177 put_be32(pb, index); // rewrite size
178 url_fseek(pb, curpos, SEEK_SET);
180 return updateSize (pb, pos);
183 /* Sync sample atom */
184 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
186 offset_t curpos, entryPos;
188 offset_t pos = url_ftell(pb);
189 put_be32(pb, 0); // size
191 put_be32(pb, 0); // version & flags
192 entryPos = url_ftell(pb);
193 put_be32(pb, track->entry); // entry count
194 for (i=0; i<track->entry; i++) {
195 if(track->cluster[i].key_frame == 1) {
200 curpos = url_ftell(pb);
201 url_fseek(pb, entryPos, SEEK_SET);
202 put_be32(pb, index); // rewrite size
203 url_fseek(pb, curpos, SEEK_SET);
204 return updateSize (pb, pos);
207 static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
209 put_be32(pb, 0x11); /* size */
210 if (track->mode == MODE_MOV) put_tag(pb, "samr");
211 else put_tag(pb, "damr");
213 put_byte(pb, 0); /* decoder version */
215 put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
216 put_byte(pb, 0x00); /* Mode change period (no restriction) */
217 put_byte(pb, 0x01); /* Frames per sample */
221 static int mov_write_enda_tag(ByteIOContext *pb)
225 put_be16(pb, 1); /* little endian */
229 static unsigned int descrLength(unsigned int len)
232 for(i=1; len>>(7*i); i++);
236 static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
238 int i= descrLength(size) - size - 2;
241 put_byte(pb, (size>>(7*i)) | 0x80);
242 put_byte(pb, size & 0x7F);
245 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
247 offset_t pos = url_ftell(pb);
248 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
250 put_be32(pb, 0); // size
252 put_be32(pb, 0); // Version
255 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
257 put_be16(pb, track->trackID);
258 put_byte(pb, 0x00); // flags (= no flags)
260 // DecoderConfig descriptor
261 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
263 // Object type indication
264 put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
266 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
267 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
268 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
269 put_byte(pb, 0x15); // flags (= Audiostream)
271 put_byte(pb, 0x11); // flags (= Visualstream)
273 put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
274 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
276 put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
277 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
278 put_be32(pb, 0); // vbr
280 put_be32(pb, track->enc->rc_max_rate); // avg bitrate
284 // DecoderSpecific info descriptor
285 putDescr(pb, 0x05, track->vosLen);
286 put_buffer(pb, track->vosData, track->vosLen);
291 putDescr(pb, 0x06, 1);
293 return updateSize (pb, pos);
296 static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
298 offset_t pos = url_ftell(pb);
300 put_be32(pb, 0); /* size */
303 put_be32(pb, 12); /* size */
305 put_le32(pb, track->tag);
307 if (track->enc->codec_id == CODEC_ID_AAC) {
308 /* useless atom needed by mplayer, ipod, not needed by quicktime */
309 put_be32(pb, 12); /* size */
312 mov_write_esds_tag(pb, track);
313 } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
314 track->enc->codec_id == CODEC_ID_PCM_S32LE) {
315 mov_write_enda_tag(pb);
316 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
317 mov_write_amr_tag(pb, track);
320 put_be32(pb, 8); /* size */
321 put_be32(pb, 0); /* null tag */
323 return updateSize (pb, pos);
326 static const CodecTag codec_movaudio_tags[] = {
327 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
328 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
329 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
330 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
331 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
332 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
333 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
334 { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
335 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
336 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
337 { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
338 { CODEC_ID_PCM_S24LE, MKTAG('i', 'n', '2', '4') },
339 { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
340 { CODEC_ID_PCM_S32LE, MKTAG('i', 'n', '3', '2') },
341 { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
342 { CODEC_ID_NONE, 0 },
345 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
347 offset_t pos = url_ftell(pb);
348 int version = track->mode == MODE_MOV &&
350 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
351 track->enc->codec_id == CODEC_ID_PCM_S24LE);
353 put_be32(pb, 0); /* size */
354 put_le32(pb, track->tag); // store it byteswapped
355 put_be32(pb, 0); /* Reserved */
356 put_be16(pb, 0); /* Reserved */
357 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
359 /* SoundDescription */
360 put_be16(pb, version); /* Version */
361 put_be16(pb, 0); /* Revision level */
362 put_be32(pb, 0); /* Reserved */
364 put_be16(pb, track->mode == MODE_MOV ? track->enc->channels : 2); /* Number of channels */
365 /* FIXME 8 bit for 'raw ' in mov */
366 put_be16(pb, 16); /* Reserved */
368 put_be16(pb, track->mode == MODE_MOV && track->audio_vbr ? -2 : 0); /* compression ID */
369 put_be16(pb, 0); /* packet size (= 0) */
370 put_be16(pb, track->timescale); /* Time scale */
371 put_be16(pb, 0); /* Reserved */
373 if(version == 1) { /* SoundDescription V1 extended info */
374 put_be32(pb, track->enc->frame_size); /* Samples per packet */
375 put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
376 put_be32(pb, track->sampleSize); /* Bytes per frame */
377 put_be32(pb, 2); /* Bytes per sample */
380 if(track->mode == MODE_MOV &&
381 (track->enc->codec_id == CODEC_ID_AAC ||
382 track->enc->codec_id == CODEC_ID_AMR_NB ||
383 track->enc->codec_id == CODEC_ID_PCM_S24LE ||
384 track->enc->codec_id == CODEC_ID_PCM_S32LE))
385 mov_write_wave_tag(pb, track);
386 else if(track->enc->codec_id == CODEC_ID_AAC)
387 mov_write_esds_tag(pb, track);
388 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
389 mov_write_amr_tag(pb, track);
391 return updateSize (pb, pos);
394 static int mov_write_d263_tag(ByteIOContext *pb)
396 put_be32(pb, 0xf); /* size */
399 put_byte(pb, 0); /* decoder version */
400 /* FIXME use AVCodecContext level/profile, when encoder will set values */
401 put_byte(pb, 0xa); /* level */
402 put_byte(pb, 0); /* profile */
406 /* TODO: No idea about these values */
407 static int mov_write_svq3_tag(ByteIOContext *pb)
413 put_be32(pb, 0xe2c0211d);
414 put_be32(pb, 0xc0000000);
419 static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
421 uint8_t *a = p + 4 - ((int)p & 3);
423 for( end -= 3; p < a && p < end; p++ ) {
424 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
428 for( end -= 3; p < end; p += 4 ) {
429 uint32_t x = *(uint32_t*)p;
430 // if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
431 // if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
432 if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
434 if( p[0] == 0 && p[2] == 1 )
436 if( p[2] == 0 && p[3] == 1 )
440 if( p[2] == 0 && p[4] == 1 )
442 if( p[4] == 0 && p[5] == 1 )
448 for( end += 3; p < end; p++ ) {
449 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
456 static void avc_parse_nal_units(uint8_t **buf, int *size)
460 uint8_t *end = p + *size;
461 uint8_t *nal_start, *nal_end;
463 url_open_dyn_buf(&pb);
464 nal_start = avc_find_startcode(p, end);
465 while (nal_start < end) {
466 while(!*(nal_start++));
467 nal_end = avc_find_startcode(nal_start, end);
468 put_be32(&pb, nal_end - nal_start);
469 put_buffer(&pb, nal_start, nal_end - nal_start);
473 *size = url_close_dyn_buf(&pb, buf);
476 static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
478 offset_t pos = url_ftell(pb);
482 if (track->vosLen > 6) {
483 /* check for h264 start code */
484 if (BE_32(track->vosData) == 0x00000001) {
486 uint32_t sps_size=0, pps_size=0;
487 uint8_t *sps=0, *pps=0;
489 avc_parse_nal_units(&track->vosData, &track->vosLen);
490 buf = track->vosData;
491 end = track->vosData + track->vosLen;
493 /* look for sps and pps */
498 nal_type = buf[4] & 0x1f;
499 if (nal_type == 7) { /* SPS */
502 } else if (nal_type == 8) { /* PPS */
511 put_byte(pb, 1); /* version */
512 put_byte(pb, sps[1]); /* profile */
513 put_byte(pb, sps[2]); /* profile compat */
514 put_byte(pb, sps[3]); /* level */
515 put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
516 put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
518 put_be16(pb, sps_size);
519 put_buffer(pb, sps, sps_size);
520 put_byte(pb, 1); /* number of pps */
521 put_be16(pb, pps_size);
522 put_buffer(pb, pps, pps_size);
524 put_buffer(pb, track->vosData, track->vosLen);
527 return updateSize(pb, pos);
530 static const CodecTag codec_movvideo_tags[] = {
531 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
532 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
533 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
534 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') },
535 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
536 { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
537 /* special handling in mov_find_video_codec_tag */
538 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
539 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
540 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL */
541 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC */
542 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL */
543 { CODEC_ID_NONE, 0 },
546 static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
548 int tag = track->enc->codec_tag;
550 if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
551 if (track->enc->height == 480) { /* NTSC */
552 if (track->enc->pix_fmt == PIX_FMT_YUV422P)
553 tag = MKTAG('d', 'v', '5', 'n');
555 tag = MKTAG('d', 'v', 'c', ' ');
556 } else { /* assume PAL */
557 if (track->enc->pix_fmt == PIX_FMT_YUV422P)
558 tag = MKTAG('d', 'v', '5', 'p');
559 else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
560 tag = MKTAG('d', 'v', 'c', 'p');
562 tag = MKTAG('d', 'v', 'p', 'p');
564 } else if (track->enc->codec_id == CODEC_ID_H263) {
565 if (track->mode == MODE_MOV)
566 tag = MKTAG('h', '2', '6', '3');
568 tag = MKTAG('s', '2', '6', '3');
570 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
573 // if no mac fcc found, try with Microsoft tags
575 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
577 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
584 static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
586 int tag = track->enc->codec_tag;
588 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
590 // if no mac fcc found, try with Microsoft tags
592 int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
594 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
595 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
602 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
604 offset_t pos = url_ftell(pb);
605 char compressor_name[32];
607 put_be32(pb, 0); /* size */
608 put_le32(pb, track->tag); // store it byteswapped
609 put_be32(pb, 0); /* Reserved */
610 put_be16(pb, 0); /* Reserved */
611 put_be16(pb, 1); /* Data-reference index */
613 put_be16(pb, 0); /* Codec stream version */
614 put_be16(pb, 0); /* Codec stream revision (=0) */
615 if (track->mode == MODE_MOV) {
616 put_tag(pb, "FFMP"); /* Vendor */
617 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
618 put_be32(pb, 0); /* Temporal Quality */
619 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
621 put_be32(pb, 0x200); /* Temporal Quality = normal */
622 put_be32(pb, 0x200); /* Spatial Quality = normal */
625 put_be32(pb, 0); /* Reserved */
626 put_be32(pb, 0); /* Reserved */
627 put_be32(pb, 0); /* Reserved */
629 put_be16(pb, track->enc->width); /* Video width */
630 put_be16(pb, track->enc->height); /* Video height */
631 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
632 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
633 put_be32(pb, 0); /* Data size (= 0) */
634 put_be16(pb, 1); /* Frame count (= 1) */
636 memset(compressor_name,0,32);
637 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
638 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
639 strncpy(compressor_name,track->enc->codec->name,31);
640 put_byte(pb, strlen(compressor_name));
641 put_buffer(pb, compressor_name, 31);
643 put_be16(pb, 0x18); /* Reserved */
644 put_be16(pb, 0xffff); /* Reserved */
645 if(track->enc->codec_id == CODEC_ID_MPEG4)
646 mov_write_esds_tag(pb, track);
647 else if(track->enc->codec_id == CODEC_ID_H263)
648 mov_write_d263_tag(pb);
649 else if(track->enc->codec_id == CODEC_ID_SVQ3)
650 mov_write_svq3_tag(pb);
651 else if(track->enc->codec_id == CODEC_ID_H264)
652 mov_write_avcc_tag(pb, track);
654 return updateSize (pb, pos);
657 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
659 offset_t pos = url_ftell(pb);
660 put_be32(pb, 0); /* size */
662 put_be32(pb, 0); /* version & flags */
663 put_be32(pb, 1); /* entry count */
664 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
665 mov_write_video_tag(pb, track);
666 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
667 mov_write_audio_tag(pb, track);
668 return updateSize(pb, pos);
671 static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
673 Time2Sample *ctts_entries;
674 uint32_t entries = 0;
678 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
679 ctts_entries[0].count = 1;
680 ctts_entries[0].duration = track->cluster[0].cts;
681 for (i=1; i<track->entry; i++) {
682 if (track->cluster[i].cts == ctts_entries[entries].duration) {
683 ctts_entries[entries].count++; /* compress */
686 ctts_entries[entries].duration = track->cluster[i].cts;
687 ctts_entries[entries].count = 1;
690 entries++; /* last one */
691 atom_size = 16 + (entries * 8);
692 put_be32(pb, atom_size); /* size */
694 put_be32(pb, 0); /* version & flags */
695 put_be32(pb, entries); /* entry count */
696 for (i=0; i<entries; i++) {
697 put_be32(pb, ctts_entries[i].count);
698 put_be32(pb, ctts_entries[i].duration);
700 av_free(ctts_entries);
704 /* Time to sample atom */
705 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
707 Time2Sample *stts_entries;
708 uint32_t entries = -1;
712 if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
713 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
714 stts_entries[0].count = track->sampleCount;
715 stts_entries[0].duration = 1;
718 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
719 for (i=0; i<track->entry; i++) {
720 int64_t duration = i + 1 == track->entry ?
721 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
722 track->cluster[i+1].dts - track->cluster[i].dts;
723 if (i && duration == stts_entries[entries].duration) {
724 stts_entries[entries].count++; /* compress */
727 stts_entries[entries].duration = duration;
728 stts_entries[entries].count = 1;
731 entries++; /* last one */
733 atom_size = 16 + (entries * 8);
734 put_be32(pb, atom_size); /* size */
736 put_be32(pb, 0); /* version & flags */
737 put_be32(pb, entries); /* entry count */
738 for (i=0; i<entries; i++) {
739 put_be32(pb, stts_entries[i].count);
740 put_be32(pb, stts_entries[i].duration);
742 av_free(stts_entries);
746 static int mov_write_dref_tag(ByteIOContext *pb)
748 put_be32(pb, 28); /* size */
750 put_be32(pb, 0); /* version & flags */
751 put_be32(pb, 1); /* entry count */
753 put_be32(pb, 0xc); /* size */
755 put_be32(pb, 1); /* version & flags */
760 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
762 offset_t pos = url_ftell(pb);
763 put_be32(pb, 0); /* size */
765 mov_write_stsd_tag(pb, track);
766 mov_write_stts_tag(pb, track);
767 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
768 track->hasKeyframes < track->entry)
769 mov_write_stss_tag(pb, track);
770 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
772 mov_write_ctts_tag(pb, track);
773 mov_write_stsc_tag(pb, track);
774 mov_write_stsz_tag(pb, track);
775 mov_write_stco_tag(pb, track);
776 return updateSize(pb, pos);
779 static int mov_write_dinf_tag(ByteIOContext *pb)
781 offset_t pos = url_ftell(pb);
782 put_be32(pb, 0); /* size */
784 mov_write_dref_tag(pb);
785 return updateSize(pb, pos);
788 static int mov_write_smhd_tag(ByteIOContext *pb)
790 put_be32(pb, 16); /* size */
792 put_be32(pb, 0); /* version & flags */
793 put_be16(pb, 0); /* reserved (balance, normally = 0) */
794 put_be16(pb, 0); /* reserved */
798 static int mov_write_vmhd_tag(ByteIOContext *pb)
800 put_be32(pb, 0x14); /* size (always 0x14) */
802 put_be32(pb, 0x01); /* version & flags */
803 put_be64(pb, 0); /* reserved (graphics mode = copy) */
807 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
809 const char *descr, *hdlr, *hdlr_type;
810 offset_t pos = url_ftell(pb);
812 if (!track) { /* no media --> data handler */
815 descr = "DataHandler";
817 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
818 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
820 descr = "VideoHandler";
823 descr = "SoundHandler";
827 put_be32(pb, 0); /* size */
829 put_be32(pb, 0); /* Version & flags */
830 put_buffer(pb, hdlr, 4); /* handler */
831 put_tag(pb, hdlr_type); /* handler type */
832 put_be32(pb ,0); /* reserved */
833 put_be32(pb ,0); /* reserved */
834 put_be32(pb ,0); /* reserved */
835 put_byte(pb, strlen(descr)); /* string counter */
836 put_buffer(pb, descr, strlen(descr)); /* handler description */
837 return updateSize(pb, pos);
840 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
842 offset_t pos = url_ftell(pb);
843 put_be32(pb, 0); /* size */
845 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
846 mov_write_vmhd_tag(pb);
848 mov_write_smhd_tag(pb);
849 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
850 mov_write_hdlr_tag(pb, NULL);
851 mov_write_dinf_tag(pb);
852 mov_write_stbl_tag(pb, track);
853 return updateSize(pb, pos);
856 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
858 int version = track->trackDuration < INT32_MAX ? 0 : 1;
860 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
862 put_byte(pb, version);
863 put_be24(pb, 0); /* flags */
865 put_be64(pb, track->time);
866 put_be64(pb, track->time);
868 put_be32(pb, track->time); /* creation time */
869 put_be32(pb, track->time); /* modification time */
871 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
872 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
873 put_be16(pb, track->language); /* language */
874 put_be16(pb, 0); /* reserved (quality) */
878 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
880 offset_t pos = url_ftell(pb);
881 put_be32(pb, 0); /* size */
883 mov_write_mdhd_tag(pb, track);
884 mov_write_hdlr_tag(pb, track);
885 mov_write_minf_tag(pb, track);
886 return updateSize(pb, pos);
889 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
891 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
892 int version = duration < INT32_MAX ? 0 : 1;
894 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
896 put_byte(pb, version);
897 put_be24(pb, 0xf); /* flags (track enabled) */
899 put_be64(pb, track->time);
900 put_be64(pb, track->time);
902 put_be32(pb, track->time); /* creation time */
903 put_be32(pb, track->time); /* modification time */
905 put_be32(pb, track->trackID); /* track-id */
906 put_be32(pb, 0); /* reserved */
907 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
909 put_be32(pb, 0); /* reserved */
910 put_be32(pb, 0); /* reserved */
911 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
912 /* Volume, only for audio */
913 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
914 put_be16(pb, 0x0100);
917 put_be16(pb, 0); /* reserved */
919 /* Matrix structure */
920 put_be32(pb, 0x00010000); /* reserved */
921 put_be32(pb, 0x0); /* reserved */
922 put_be32(pb, 0x0); /* reserved */
923 put_be32(pb, 0x0); /* reserved */
924 put_be32(pb, 0x00010000); /* reserved */
925 put_be32(pb, 0x0); /* reserved */
926 put_be32(pb, 0x0); /* reserved */
927 put_be32(pb, 0x0); /* reserved */
928 put_be32(pb, 0x40000000); /* reserved */
930 /* Track width and height, for visual only */
931 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
932 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
933 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
934 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
935 put_be32(pb, track->enc->height*0x10000);
944 // This box seems important for the psp playback ... without it the movie seems to hang
945 static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
947 put_be32(pb, 0x24); /* size */
949 put_be32(pb, 0x1c); /* size */
954 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
956 put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
957 put_be32(pb, 0x00010000);
961 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
962 static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
964 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
967 put_be32(pb, 0x21d24fce);
968 put_be32(pb, 0xbb88695c);
969 put_be32(pb, 0xfac9c740);
970 put_be32(pb, 0x1c); // another size here!
972 put_be32(pb, 0x00010012);
974 put_be32(pb, 0x55c40000);
980 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
982 offset_t pos = url_ftell(pb);
983 put_be32(pb, 0); /* size */
985 mov_write_tkhd_tag(pb, track);
986 if (track->mode == MODE_PSP || track->hasBframes)
987 mov_write_edts_tag(pb, track); // PSP Movies require edts box
988 mov_write_mdia_tag(pb, track);
989 if (track->mode == MODE_PSP)
990 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
991 return updateSize(pb, pos);
995 /* TODO: Not sorted out, but not necessary either */
996 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
998 put_be32(pb, 0x15); /* size */
1000 put_be32(pb, 0); /* version & flags */
1001 put_be16(pb, 0x1007);
1003 put_be16(pb, 0x4fff);
1004 put_be16(pb, 0xfffe);
1005 put_be16(pb, 0x01ff);
1010 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1012 int maxTrackID = 1, i;
1013 int64_t maxTrackLenTemp, maxTrackLen = 0;
1016 for (i=0; i<mov->nb_streams; i++) {
1017 if(mov->tracks[i].entry > 0) {
1018 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1019 if(maxTrackLen < maxTrackLenTemp)
1020 maxTrackLen = maxTrackLenTemp;
1021 if(maxTrackID < mov->tracks[i].trackID)
1022 maxTrackID = mov->tracks[i].trackID;
1026 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1027 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1028 put_tag(pb, "mvhd");
1029 put_byte(pb, version);
1030 put_be24(pb, 0); /* flags */
1032 put_be64(pb, mov->time);
1033 put_be64(pb, mov->time);
1035 put_be32(pb, mov->time); /* creation time */
1036 put_be32(pb, mov->time); /* modification time */
1038 put_be32(pb, mov->timescale); /* timescale */
1039 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1041 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1042 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1043 put_be16(pb, 0); /* reserved */
1044 put_be32(pb, 0); /* reserved */
1045 put_be32(pb, 0); /* reserved */
1047 /* Matrix structure */
1048 put_be32(pb, 0x00010000); /* reserved */
1049 put_be32(pb, 0x0); /* reserved */
1050 put_be32(pb, 0x0); /* reserved */
1051 put_be32(pb, 0x0); /* reserved */
1052 put_be32(pb, 0x00010000); /* reserved */
1053 put_be32(pb, 0x0); /* reserved */
1054 put_be32(pb, 0x0); /* reserved */
1055 put_be32(pb, 0x0); /* reserved */
1056 put_be32(pb, 0x40000000); /* reserved */
1058 put_be32(pb, 0); /* reserved (preview time) */
1059 put_be32(pb, 0); /* reserved (preview duration) */
1060 put_be32(pb, 0); /* reserved (poster time) */
1061 put_be32(pb, 0); /* reserved (selection time) */
1062 put_be32(pb, 0); /* reserved (selection duration) */
1063 put_be32(pb, 0); /* reserved (current time) */
1064 put_be32(pb, maxTrackID+1); /* Next track id */
1068 static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1071 offset_t pos = url_ftell(pb);
1072 put_be32(pb, 0); /* size */
1073 put_tag(pb, "hdlr");
1076 put_tag(pb, "mdir");
1077 put_tag(pb, "appl");
1081 return updateSize(pb, pos);
1084 /* helper function to write a data tag with the specified string as data */
1085 static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1088 offset_t pos = url_ftell(pb);
1089 put_be32(pb, 0); /* size */
1090 put_tag(pb, "data");
1093 put_buffer(pb, data, strlen(data));
1094 return updateSize(pb, pos);
1096 put_be16(pb, strlen(data)); /* string length */
1098 put_buffer(pb, data, strlen(data));
1099 return strlen(data) + 4;
1103 static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1105 if ( value && value[0] ) {
1106 offset_t pos = url_ftell(pb);
1107 put_be32(pb, 0); /* size */
1109 mov_write_string_data_tag(pb, value, long_style);
1110 size= updateSize(pb, pos);
1116 static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1120 snprintf(year_str, sizeof(year_str), "%04d", year);
1121 return mov_write_string_tag(pb, "\251day", year_str, long_style);
1126 /* iTunes track number */
1127 static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1132 offset_t pos = url_ftell(pb);
1133 put_be32(pb, 0); /* size */
1134 put_tag(pb, "trkn");
1136 offset_t pos = url_ftell(pb);
1137 put_be32(pb, 0); /* size */
1138 put_tag(pb, "data");
1139 put_be32(pb, 0); // 8 bytes empty
1141 put_be16(pb, 0); // empty
1142 put_be16(pb, s->track); // track number
1143 put_be16(pb, 0); // total track number
1144 put_be16(pb, 0); // empty
1145 updateSize(pb, pos);
1147 size = updateSize(pb, pos);
1152 /* iTunes meta data list */
1153 static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1156 offset_t pos = url_ftell(pb);
1157 put_be32(pb, 0); /* size */
1158 put_tag(pb, "ilst");
1159 mov_write_string_tag(pb, "\251nam", s->title , 1);
1160 mov_write_string_tag(pb, "\251ART", s->author , 1);
1161 mov_write_string_tag(pb, "\251wrt", s->author , 1);
1162 mov_write_string_tag(pb, "\251alb", s->album , 1);
1163 mov_write_day_tag(pb, s->year ,1);
1164 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1165 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1166 mov_write_string_tag(pb, "\251cmt", s->comment , 1);
1167 mov_write_string_tag(pb, "\251gen", s->genre , 1);
1168 mov_write_trkn_tag(pb, mov, s);
1169 return updateSize(pb, pos);
1172 /* iTunes meta data tag */
1173 static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1178 // only save meta tag if required
1179 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1180 s->comment[0] || s->genre[0] || s->track ) {
1181 offset_t pos = url_ftell(pb);
1182 put_be32(pb, 0); /* size */
1183 put_tag(pb, "meta");
1185 mov_write_itunes_hdlr_tag(pb, mov, s);
1186 mov_write_ilst_tag(pb, mov, s);
1187 size = updateSize(pb, pos);
1192 static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1195 offset_t pos = url_ftell(pb);
1198 put_be32(pb, 0); /* size */
1199 put_tag(pb, "udta");
1201 /* iTunes meta data */
1202 mov_write_meta_tag(pb, mov, s);
1204 if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff isnt valid in mp4
1206 for (i=0; i<mov->nb_streams; i++) {
1207 if(mov->tracks[i].entry <= 0) continue;
1208 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1209 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1210 mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1215 mov_write_string_tag(pb, "\251nam", s->title , 0);
1216 mov_write_string_tag(pb, "\251aut", s->author , 0);
1217 mov_write_string_tag(pb, "\251alb", s->album , 0);
1218 mov_write_day_tag(pb, s->year, 0);
1219 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1220 mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1221 mov_write_string_tag(pb, "\251des", s->comment , 0);
1222 mov_write_string_tag(pb, "\251gen", s->genre , 0);
1225 return updateSize(pb, pos);
1228 static int utf8len(uint8_t *b){
1232 GET_UTF8(val, *b++, return -1;)
1238 static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1242 GET_UTF8(val, *b++, return -1;)
1249 static uint16_t language_code (const char *str)
1251 return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1254 static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1257 offset_t pos, curpos;
1261 pos = url_ftell(pb);
1262 put_be32(pb, 0); /* size placeholder*/
1263 put_tag(pb, "uuid");
1264 put_tag(pb, "USMT");
1265 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1266 put_be32(pb, 0xbb88695c );
1267 put_be32(pb, 0xfac9c740 );
1270 put_be32(pb, 0); /* size placeholder*/
1271 put_tag(pb, "MTDT");
1276 put_be16(pb, 0x0C); /* size */
1277 put_be32(pb, 0x0B); /* type */
1278 put_be16(pb, language_code("und")); /* language */
1279 put_be16(pb, 0x0); /* ? */
1280 put_be16(pb, 0x021C); /* data */
1284 len = utf8len(LIBAVCODEC_IDENT)+1;
1287 put_be16(pb, len*2+10); /* size */
1288 put_be32(pb, 0x04); /* type */
1289 put_be16(pb, language_code("eng")); /* language */
1290 put_be16(pb, 0x01); /* ? */
1291 ascii_to_wc(pb, LIBAVCODEC_IDENT);
1295 len = utf8len(s->title)+1;
1298 put_be16(pb, len*2+10); /* size */
1299 put_be32(pb, 0x01); /* type */
1300 put_be16(pb, language_code("eng")); /* language */
1301 put_be16(pb, 0x01); /* ? */
1302 ascii_to_wc (pb, s->title);
1306 // 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);
1307 len = utf8len("2006/04/01 11:11:11")+1;
1310 put_be16(pb, len*2+10); /* size */
1311 put_be32(pb, 0x03); /* type */
1312 put_be16(pb, language_code("und")); /* language */
1313 put_be16(pb, 0x01); /* ? */
1314 ascii_to_wc (pb, "2006/04/01 11:11:11");
1318 curpos = url_ftell(pb);
1319 url_fseek(pb, pos, SEEK_SET);
1321 url_fseek(pb, pos+24, SEEK_SET);
1322 put_be32(pb, size-24);
1323 url_fseek(pb, curpos, SEEK_SET);
1328 av_log(s, AV_LOG_ERROR, "not utf8\n");
1332 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1336 offset_t pos = url_ftell(pb);
1337 put_be32(pb, 0); /* size placeholder*/
1338 put_tag(pb, "moov");
1339 mov->timescale = globalTimescale;
1341 for (i=0; i<mov->nb_streams; i++) {
1342 if(mov->tracks[i].entry <= 0) continue;
1344 mov->tracks[i].time = mov->time;
1345 mov->tracks[i].trackID = i+1;
1348 mov_write_mvhd_tag(pb, mov);
1349 //mov_write_iods_tag(pb, mov);
1350 for (i=0; i<mov->nb_streams; i++) {
1351 if(mov->tracks[i].entry > 0) {
1352 mov_write_trak_tag(pb, &(mov->tracks[i]));
1356 if (mov->mode == MODE_PSP)
1357 mov_write_uuidusmt_tag(pb, s);
1359 mov_write_udta_tag(pb, mov, s);
1361 return updateSize(pb, pos);
1364 static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1366 put_be32(pb, 8); // placeholder for extended size field (64 bit)
1367 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1369 mov->mdat_pos = url_ftell(pb);
1370 put_be32(pb, 0); /* size placeholder*/
1371 put_tag(pb, "mdat");
1375 /* TODO: This needs to be more general */
1376 static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1378 MOVContext *mov = s->priv_data;
1380 put_be32(pb, 0x14 ); /* size */
1381 put_tag(pb, "ftyp");
1383 if ( mov->mode == MODE_3GP )
1384 put_tag(pb, "3gp4");
1385 else if ( mov->mode == MODE_3G2 )
1386 put_tag(pb, "3g2a");
1387 else if ( mov->mode == MODE_PSP )
1388 put_tag(pb, "MSNV");
1389 else if ( mov->mode == MODE_MP4 )
1390 put_tag(pb, "isom");
1394 put_be32(pb, 0x200 );
1396 if ( mov->mode == MODE_3GP )
1397 put_tag(pb, "3gp4");
1398 else if ( mov->mode == MODE_3G2 )
1399 put_tag(pb, "3g2a");
1400 else if ( mov->mode == MODE_PSP )
1401 put_tag(pb, "MSNV");
1402 else if ( mov->mode == MODE_MP4 )
1403 put_tag(pb, "mp41");
1408 static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1410 AVCodecContext *VideoCodec = s->streams[0]->codec;
1411 AVCodecContext *AudioCodec = s->streams[1]->codec;
1412 int AudioRate = AudioCodec->sample_rate;
1413 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1414 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1415 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1417 put_be32(pb, 0x94 ); /* size */
1418 put_tag(pb, "uuid");
1419 put_tag(pb, "PROF");
1421 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1422 put_be32(pb, 0xbb88695c );
1423 put_be32(pb, 0xfac9c740 );
1425 put_be32(pb, 0x0 ); /* ? */
1426 put_be32(pb, 0x3 ); /* 3 sections ? */
1428 put_be32(pb, 0x14 ); /* size */
1429 put_tag(pb, "FPRF");
1430 put_be32(pb, 0x0 ); /* ? */
1431 put_be32(pb, 0x0 ); /* ? */
1432 put_be32(pb, 0x0 ); /* ? */
1434 put_be32(pb, 0x2c ); /* size */
1435 put_tag(pb, "APRF"); /* audio */
1437 put_be32(pb, 0x2 ); /* TrackID */
1438 put_tag(pb, "mp4a");
1439 put_be32(pb, 0x20f );
1441 put_be32(pb, audio_kbitrate);
1442 put_be32(pb, audio_kbitrate);
1443 put_be32(pb, AudioRate );
1444 put_be32(pb, AudioCodec->channels );
1446 put_be32(pb, 0x34 ); /* size */
1447 put_tag(pb, "VPRF"); /* video */
1449 put_be32(pb, 0x1 ); /* TrackID */
1450 if (VideoCodec->codec_id == CODEC_ID_H264) {
1451 put_tag(pb, "avc1");
1452 put_be16(pb, 0x014D );
1453 put_be16(pb, 0x0015 );
1455 put_tag(pb, "mp4v");
1456 put_be16(pb, 0x0000 );
1457 put_be16(pb, 0x0103 );
1460 put_be32(pb, video_kbitrate);
1461 put_be32(pb, video_kbitrate);
1462 put_be32(pb, FrameRate);
1463 put_be32(pb, FrameRate);
1464 put_be16(pb, VideoCodec->width);
1465 put_be16(pb, VideoCodec->height);
1466 put_be32(pb, 0x010001); /* ? */
1469 static int mov_write_header(AVFormatContext *s)
1471 ByteIOContext *pb = &s->pb;
1472 MOVContext *mov = s->priv_data;
1475 /* Default mode == MP4 */
1476 mov->mode = MODE_MP4;
1478 if (s->oformat != NULL) {
1479 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1480 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1481 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1482 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1484 mov_write_ftyp_tag(pb,s);
1485 if ( mov->mode == MODE_PSP ) {
1486 if ( s->nb_streams != 2 ) {
1487 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1490 mov_write_uuidprof_tag(pb,s);
1494 for(i=0; i<s->nb_streams; i++){
1495 AVStream *st= s->streams[i];
1496 MOVTrack *track= &mov->tracks[i];
1498 track->enc = st->codec;
1499 track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1500 track->mode = mov->mode;
1501 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1502 track->tag = mov_find_video_codec_tag(s, track);
1503 track->timescale = st->codec->time_base.den;
1504 track->sampleDuration = st->codec->time_base.num;
1505 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1506 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1507 track->tag = mov_find_audio_codec_tag(s, track);
1508 track->timescale = st->codec->sample_rate;
1509 track->sampleDuration = st->codec->frame_size;
1510 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1511 switch(track->enc->codec_id){
1514 case CODEC_ID_AMR_NB:
1515 case CODEC_ID_AMR_WB:
1516 track->audio_vbr = 1;
1519 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1521 if (!st->codec->frame_size) {
1522 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1528 mov_write_mdat_tag(pb, mov);
1529 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1530 mov->nb_streams = s->nb_streams;
1532 put_flush_packet(pb);
1537 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1539 MOVContext *mov = s->priv_data;
1540 ByteIOContext *pb = &s->pb;
1541 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1542 AVCodecContext *enc = trk->enc;
1543 unsigned int samplesInChunk = 0;
1544 int size= pkt->size;
1546 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1547 if (!size) return 0; /* Discard 0 sized packets */
1549 if (enc->codec_id == CODEC_ID_AMR_NB) {
1550 /* We must find out how many AMR blocks there are in one packet */
1551 static uint16_t packed_size[16] =
1552 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1555 while (len < size && samplesInChunk < 100) {
1556 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1559 if(samplesInChunk > 1){
1560 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, inplement a AVParser for it\n");
1563 } else if (trk->sampleSize)
1564 samplesInChunk = size/trk->sampleSize;
1568 /* copy extradata if it exists */
1569 if (trk->vosLen == 0 && enc->extradata_size > 0) {
1570 trk->vosLen = enc->extradata_size;
1571 trk->vosData = av_malloc(trk->vosLen);
1572 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1575 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1576 /* from x264 or from bytestream h264 */
1577 /* nal reformating needed */
1578 avc_parse_nal_units(&pkt->data, &pkt->size);
1583 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1584 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1589 trk->cluster[trk->entry].pos = url_ftell(pb);
1590 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1591 trk->cluster[trk->entry].size = size;
1592 trk->cluster[trk->entry].entries = samplesInChunk;
1593 trk->cluster[trk->entry].dts = pkt->dts;
1594 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1596 if(enc->codec_type == CODEC_TYPE_VIDEO) {
1597 if (pkt->dts != pkt->pts)
1598 trk->hasBframes = 1;
1599 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1600 trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1601 if(trk->cluster[trk->entry].key_frame)
1602 trk->hasKeyframes++;
1605 trk->sampleCount += samplesInChunk;
1606 mov->mdat_size += size;
1608 put_buffer(pb, pkt->data, size);
1610 put_flush_packet(pb);
1614 static int mov_write_trailer(AVFormatContext *s)
1616 MOVContext *mov = s->priv_data;
1617 ByteIOContext *pb = &s->pb;
1621 offset_t moov_pos = url_ftell(pb);
1623 /* Write size of mdat tag */
1624 if (mov->mdat_size+8 <= UINT32_MAX) {
1625 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1626 put_be32(pb, mov->mdat_size+8);
1628 /* overwrite 'wide' placeholder atom */
1629 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1630 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1631 put_tag(pb, "mdat");
1632 put_be64(pb, mov->mdat_size+16);
1634 url_fseek(pb, moov_pos, SEEK_SET);
1636 mov_write_moov_tag(pb, mov, s);
1638 for (i=0; i<mov->nb_streams; i++) {
1639 av_freep(&mov->tracks[i].cluster);
1641 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1645 put_flush_packet(pb);
1650 #ifdef CONFIG_MOV_MUXER
1651 AVOutputFormat mov_muxer = {
1662 .flags = AVFMT_GLOBALHEADER,
1665 #ifdef CONFIG_TGP_MUXER
1666 AVOutputFormat tgp_muxer = {
1677 .flags = AVFMT_GLOBALHEADER,
1680 #ifdef CONFIG_MP4_MUXER
1681 AVOutputFormat mp4_muxer = {
1692 .flags = AVFMT_GLOBALHEADER,
1695 #ifdef CONFIG_PSP_MUXER
1696 AVOutputFormat psp_muxer = {
1707 .flags = AVFMT_GLOBALHEADER,
1710 #ifdef CONFIG_TG2_MUXER
1711 AVOutputFormat tg2_muxer = {
1722 .flags = AVFMT_GLOBALHEADER,