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
27 #include "libavcodec/bitstream.h"
32 #define MOV_INDEX_CLUSTER_SIZE 16384
33 #define globalTimescale 1000
38 #define MODE_PSP 0x08 // example working PSP command line:
39 // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
41 #define MODE_IPOD 0x20
43 typedef struct MOVIentry
{
44 unsigned int flags
, size
;
46 unsigned int samplesInChunk
;
53 typedef struct MOVIndex
{
58 int64_t trackDuration
;
65 int tag
; ///< stsd fourcc
72 int height
; ///< active picture (w/o VBI) height for D-10/IMX
75 typedef struct MOVMuxContext
{
82 MOVTrack tracks
[MAX_STREAMS
];
85 //FIXME support 64 bit variant with wide placeholders
86 static int64_t updateSize(ByteIOContext
*pb
, int64_t pos
)
88 int64_t curpos
= url_ftell(pb
);
89 url_fseek(pb
, pos
, SEEK_SET
);
90 put_be32(pb
, curpos
- pos
); /* rewrite size */
91 url_fseek(pb
, curpos
, SEEK_SET
);
96 /* Chunk offset atom */
97 static int mov_write_stco_tag(ByteIOContext
*pb
, MOVTrack
*track
)
100 int mode64
= 0; // use 32 bit size variant if possible
101 int64_t pos
= url_ftell(pb
);
102 put_be32(pb
, 0); /* size */
103 if (pos
> UINT32_MAX
) {
108 put_be32(pb
, 0); /* version & flags */
109 put_be32(pb
, track
->entry
); /* entry count */
110 for (i
=0; i
<track
->entry
; i
++) {
112 put_be64(pb
, track
->cluster
[i
].pos
);
114 put_be32(pb
, track
->cluster
[i
].pos
);
116 return updateSize(pb
, pos
);
119 /* Sample size atom */
120 static int mov_write_stsz_tag(ByteIOContext
*pb
, MOVTrack
*track
)
123 int i
, j
, entries
= 0, tst
= -1, oldtst
= -1;
125 int64_t pos
= url_ftell(pb
);
126 put_be32(pb
, 0); /* size */
128 put_be32(pb
, 0); /* version & flags */
130 for (i
=0; i
<track
->entry
; i
++) {
131 tst
= track
->cluster
[i
].size
/track
->cluster
[i
].entries
;
132 if(oldtst
!= -1 && tst
!= oldtst
) {
136 entries
+= track
->cluster
[i
].entries
;
139 int sSize
= track
->cluster
[0].size
/track
->cluster
[0].entries
;
140 put_be32(pb
, sSize
); // sample size
141 put_be32(pb
, entries
); // sample count
144 put_be32(pb
, 0); // sample size
145 put_be32(pb
, entries
); // sample count
146 for (i
=0; i
<track
->entry
; i
++) {
147 for (j
=0; j
<track
->cluster
[i
].entries
; j
++) {
148 put_be32(pb
, track
->cluster
[i
].size
/
149 track
->cluster
[i
].entries
);
153 return updateSize(pb
, pos
);
156 /* Sample to chunk atom */
157 static int mov_write_stsc_tag(ByteIOContext
*pb
, MOVTrack
*track
)
159 int index
= 0, oldval
= -1, i
;
160 int64_t entryPos
, curpos
;
162 int64_t pos
= url_ftell(pb
);
163 put_be32(pb
, 0); /* size */
165 put_be32(pb
, 0); // version & flags
166 entryPos
= url_ftell(pb
);
167 put_be32(pb
, track
->entry
); // entry count
168 for (i
=0; i
<track
->entry
; i
++) {
169 if(oldval
!= track
->cluster
[i
].samplesInChunk
)
171 put_be32(pb
, i
+1); // first chunk
172 put_be32(pb
, track
->cluster
[i
].samplesInChunk
); // samples per chunk
173 put_be32(pb
, 0x1); // sample description index
174 oldval
= track
->cluster
[i
].samplesInChunk
;
178 curpos
= url_ftell(pb
);
179 url_fseek(pb
, entryPos
, SEEK_SET
);
180 put_be32(pb
, index
); // rewrite size
181 url_fseek(pb
, curpos
, SEEK_SET
);
183 return updateSize(pb
, pos
);
186 /* Sync sample atom */
187 static int mov_write_stss_tag(ByteIOContext
*pb
, MOVTrack
*track
)
189 int64_t curpos
, entryPos
;
191 int64_t pos
= url_ftell(pb
);
192 put_be32(pb
, 0); // size
194 put_be32(pb
, 0); // version & flags
195 entryPos
= url_ftell(pb
);
196 put_be32(pb
, track
->entry
); // entry count
197 for (i
=0; i
<track
->entry
; i
++) {
198 if(track
->cluster
[i
].key_frame
== 1) {
203 curpos
= url_ftell(pb
);
204 url_fseek(pb
, entryPos
, SEEK_SET
);
205 put_be32(pb
, index
); // rewrite size
206 url_fseek(pb
, curpos
, SEEK_SET
);
207 return updateSize(pb
, pos
);
210 static int mov_write_amr_tag(ByteIOContext
*pb
, MOVTrack
*track
)
212 put_be32(pb
, 0x11); /* size */
213 if (track
->mode
== MODE_MOV
) put_tag(pb
, "samr");
214 else put_tag(pb
, "damr");
216 put_byte(pb
, 0); /* decoder version */
218 put_be16(pb
, 0x81FF); /* Mode set (all modes for AMR_NB) */
219 put_byte(pb
, 0x00); /* Mode change period (no restriction) */
220 put_byte(pb
, 0x01); /* Frames per sample */
224 static int mov_write_ac3_tag(ByteIOContext
*pb
, MOVTrack
*track
)
229 int fscod
, bsid
, bsmod
, acmod
, lfeon
, frmsizecod
;
231 if (track
->vosLen
< 7)
237 init_get_bits(&gbc
, track
->vosData
+4, track
->vosLen
-4);
238 fscod
= get_bits(&gbc
, 2);
239 frmsizecod
= get_bits(&gbc
, 6);
240 bsid
= get_bits(&gbc
, 5);
241 bsmod
= get_bits(&gbc
, 3);
242 acmod
= get_bits(&gbc
, 3);
244 skip_bits(&gbc
, 2); // dsurmod
246 if ((acmod
& 1) && acmod
!= 1)
247 skip_bits(&gbc
, 2); // cmixlev
249 skip_bits(&gbc
, 2); // surmixlev
251 lfeon
= get_bits1(&gbc
);
253 init_put_bits(&pbc
, buf
, sizeof(buf
));
254 put_bits(&pbc
, 2, fscod
);
255 put_bits(&pbc
, 5, bsid
);
256 put_bits(&pbc
, 3, bsmod
);
257 put_bits(&pbc
, 3, acmod
);
258 put_bits(&pbc
, 1, lfeon
);
259 put_bits(&pbc
, 5, frmsizecod
>>1); // bit_rate_code
260 put_bits(&pbc
, 5, 0); // reserved
262 flush_put_bits(&pbc
);
263 put_buffer(pb
, buf
, sizeof(buf
));
269 * This function writes extradata "as is".
270 * Extradata must be formated like a valid atom (with size and tag)
272 static int mov_write_extradata_tag(ByteIOContext
*pb
, MOVTrack
*track
)
274 put_buffer(pb
, track
->enc
->extradata
, track
->enc
->extradata_size
);
275 return track
->enc
->extradata_size
;
278 static int mov_write_enda_tag(ByteIOContext
*pb
)
282 put_be16(pb
, 1); /* little endian */
286 static unsigned int descrLength(unsigned int len
)
289 for(i
=1; len
>>(7*i
); i
++);
293 static void putDescr(ByteIOContext
*pb
, int tag
, unsigned int size
)
295 int i
= descrLength(size
) - size
- 2;
298 put_byte(pb
, (size
>>(7*i
)) | 0x80);
299 put_byte(pb
, size
& 0x7F);
302 static int mov_write_esds_tag(ByteIOContext
*pb
, MOVTrack
*track
) // Basic
304 int64_t pos
= url_ftell(pb
);
305 int decoderSpecificInfoLen
= track
->vosLen
? descrLength(track
->vosLen
):0;
307 put_be32(pb
, 0); // size
309 put_be32(pb
, 0); // Version
312 putDescr(pb
, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen
) +
314 put_be16(pb
, track
->trackID
);
315 put_byte(pb
, 0x00); // flags (= no flags)
317 // DecoderConfig descriptor
318 putDescr(pb
, 0x04, 13 + decoderSpecificInfoLen
);
320 // Object type indication
321 if ((track
->enc
->codec_id
== CODEC_ID_MP2
||
322 track
->enc
->codec_id
== CODEC_ID_MP3
) &&
323 track
->enc
->sample_rate
> 24000)
324 put_byte(pb
, 0x6B); // 11172-3
326 put_byte(pb
, codec_get_tag(ff_mp4_obj_type
, track
->enc
->codec_id
));
328 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
329 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
330 if(track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
331 put_byte(pb
, 0x15); // flags (= Audiostream)
333 put_byte(pb
, 0x11); // flags (= Visualstream)
335 put_byte(pb
, track
->enc
->rc_buffer_size
>>(3+16)); // Buffersize DB (24 bits)
336 put_be16(pb
, (track
->enc
->rc_buffer_size
>>3)&0xFFFF); // Buffersize DB
338 put_be32(pb
, FFMAX(track
->enc
->bit_rate
, track
->enc
->rc_max_rate
)); // maxbitrate (FIXME should be max rate in any 1 sec window)
339 if(track
->enc
->rc_max_rate
!= track
->enc
->rc_min_rate
|| track
->enc
->rc_min_rate
==0)
340 put_be32(pb
, 0); // vbr
342 put_be32(pb
, track
->enc
->rc_max_rate
); // avg bitrate
345 // DecoderSpecific info descriptor
346 putDescr(pb
, 0x05, track
->vosLen
);
347 put_buffer(pb
, track
->vosData
, track
->vosLen
);
351 putDescr(pb
, 0x06, 1);
353 return updateSize(pb
, pos
);
356 static int mov_write_wave_tag(ByteIOContext
*pb
, MOVTrack
*track
)
358 int64_t pos
= url_ftell(pb
);
360 put_be32(pb
, 0); /* size */
363 put_be32(pb
, 12); /* size */
365 put_le32(pb
, track
->tag
);
367 if (track
->enc
->codec_id
== CODEC_ID_AAC
) {
368 /* useless atom needed by mplayer, ipod, not needed by quicktime */
369 put_be32(pb
, 12); /* size */
372 mov_write_esds_tag(pb
, track
);
373 } else if (track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
||
374 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
) {
375 mov_write_enda_tag(pb
);
376 } else if (track
->enc
->codec_id
== CODEC_ID_AMR_NB
) {
377 mov_write_amr_tag(pb
, track
);
378 } else if (track
->enc
->codec_id
== CODEC_ID_AC3
) {
379 mov_write_ac3_tag(pb
, track
);
380 } else if (track
->enc
->codec_id
== CODEC_ID_ALAC
) {
381 mov_write_extradata_tag(pb
, track
);
384 put_be32(pb
, 8); /* size */
385 put_be32(pb
, 0); /* null tag */
387 return updateSize(pb
, pos
);
390 static int mov_write_glbl_tag(ByteIOContext
*pb
, MOVTrack
*track
)
392 put_be32(pb
, track
->vosLen
+8);
394 put_buffer(pb
, track
->vosData
, track
->vosLen
);
395 return 8+track
->vosLen
;
398 static int mov_write_audio_tag(ByteIOContext
*pb
, MOVTrack
*track
)
400 int64_t pos
= url_ftell(pb
);
401 int version
= track
->mode
== MODE_MOV
&&
403 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
||
404 track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
);
406 put_be32(pb
, 0); /* size */
407 put_le32(pb
, track
->tag
); // store it byteswapped
408 put_be32(pb
, 0); /* Reserved */
409 put_be16(pb
, 0); /* Reserved */
410 put_be16(pb
, 1); /* Data-reference index, XXX == 1 */
412 /* SoundDescription */
413 put_be16(pb
, version
); /* Version */
414 put_be16(pb
, 0); /* Revision level */
415 put_be32(pb
, 0); /* Reserved */
417 if (track
->mode
== MODE_MOV
) {
418 put_be16(pb
, track
->enc
->channels
);
419 if (track
->enc
->codec_id
== CODEC_ID_PCM_U8
||
420 track
->enc
->codec_id
== CODEC_ID_PCM_S8
)
421 put_be16(pb
, 8); /* bits per sample */
424 put_be16(pb
, track
->audio_vbr
? -2 : 0); /* compression ID */
425 } else { /* reserved for mp4/3gp */
431 put_be16(pb
, 0); /* packet size (= 0) */
432 put_be16(pb
, track
->timescale
); /* Time scale */
433 put_be16(pb
, 0); /* Reserved */
435 if(version
== 1) { /* SoundDescription V1 extended info */
436 put_be32(pb
, track
->enc
->frame_size
); /* Samples per packet */
437 put_be32(pb
, track
->sampleSize
/ track
->enc
->channels
); /* Bytes per packet */
438 put_be32(pb
, track
->sampleSize
); /* Bytes per frame */
439 put_be32(pb
, 2); /* Bytes per sample */
442 if(track
->mode
== MODE_MOV
&&
443 (track
->enc
->codec_id
== CODEC_ID_AAC
||
444 track
->enc
->codec_id
== CODEC_ID_AC3
||
445 track
->enc
->codec_id
== CODEC_ID_AMR_NB
||
446 track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
||
447 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
||
448 track
->enc
->codec_id
== CODEC_ID_ALAC
))
449 mov_write_wave_tag(pb
, track
);
450 else if(track
->tag
== MKTAG('m','p','4','a'))
451 mov_write_esds_tag(pb
, track
);
452 else if(track
->enc
->codec_id
== CODEC_ID_AMR_NB
)
453 mov_write_amr_tag(pb
, track
);
454 else if(track
->enc
->codec_id
== CODEC_ID_AC3
)
455 mov_write_ac3_tag(pb
, track
);
456 else if(track
->enc
->codec_id
== CODEC_ID_ALAC
)
457 mov_write_extradata_tag(pb
, track
);
458 else if(track
->vosLen
> 0)
459 mov_write_glbl_tag(pb
, track
);
461 return updateSize(pb
, pos
);
464 static int mov_write_d263_tag(ByteIOContext
*pb
)
466 put_be32(pb
, 0xf); /* size */
469 put_byte(pb
, 0); /* decoder version */
470 /* FIXME use AVCodecContext level/profile, when encoder will set values */
471 put_byte(pb
, 0xa); /* level */
472 put_byte(pb
, 0); /* profile */
476 /* TODO: No idea about these values */
477 static int mov_write_svq3_tag(ByteIOContext
*pb
)
483 put_be32(pb
, 0xe2c0211d);
484 put_be32(pb
, 0xc0000000);
489 static int mov_write_avcc_tag(ByteIOContext
*pb
, MOVTrack
*track
)
491 int64_t pos
= url_ftell(pb
);
495 ff_isom_write_avcc(pb
, track
->vosData
, track
->vosLen
);
496 return updateSize(pb
, pos
);
499 /* also used by all avid codecs (dv, imx, meridien) and their variants */
500 static int mov_write_avid_tag(ByteIOContext
*pb
, MOVTrack
*track
)
503 put_be32(pb
, 24); /* size */
507 put_be32(pb
, 1); /* yuv 1 / rgb 2 ? */
508 put_be32(pb
, 0); /* unknown */
510 put_be32(pb
, 24); /* size */
514 put_be32(pb
, 1); /* unknown */
515 put_be32(pb
, 0); /* unknown */
517 put_be32(pb
, 120); /* size */
521 put_be32(pb
, AV_RB32(track
->vosData
+ 0x28)); /* dnxhd cid, some id ? */
522 put_be32(pb
, track
->enc
->width
);
523 /* values below are based on samples created with quicktime and avid codecs */
524 if (track
->vosData
[5] & 2) { // interlaced
525 put_be32(pb
, track
->enc
->height
/2);
526 put_be32(pb
, 2); /* unknown */
527 put_be32(pb
, 0); /* unknown */
528 put_be32(pb
, 4); /* unknown */
530 put_be32(pb
, track
->enc
->height
);
531 put_be32(pb
, 1); /* unknown */
532 put_be32(pb
, 0); /* unknown */
533 if (track
->enc
->height
== 1080)
534 put_be32(pb
, 5); /* unknown */
536 put_be32(pb
, 6); /* unknown */
539 for (i
= 0; i
< 10; i
++)
542 /* extra padding for stsd needed */
547 static const AVCodecTag codec_3gp_tags
[] = {
548 { CODEC_ID_H263
, MKTAG('s','2','6','3') },
549 { CODEC_ID_H264
, MKTAG('a','v','c','1') },
550 { CODEC_ID_MPEG4
, MKTAG('m','p','4','v') },
551 { CODEC_ID_AAC
, MKTAG('m','p','4','a') },
552 { CODEC_ID_AMR_NB
, MKTAG('s','a','m','r') },
553 { CODEC_ID_AMR_WB
, MKTAG('s','a','w','b') },
554 { CODEC_ID_MOV_TEXT
, MKTAG('t','x','3','g') },
555 { CODEC_ID_NONE
, 0 },
558 static const AVCodecTag mov_pix_fmt_tags
[] = {
559 { PIX_FMT_YUYV422
, MKTAG('y','u','v','s') },
560 { PIX_FMT_UYVY422
, MKTAG('2','v','u','y') },
561 { PIX_FMT_BGR555
, MKTAG('r','a','w',' ') },
562 { PIX_FMT_RGB24
, MKTAG('r','a','w',' ') },
563 { PIX_FMT_BGR32_1
, MKTAG('r','a','w',' ') },
566 static const AVCodecTag codec_ipod_tags
[] = {
567 { CODEC_ID_H264
, MKTAG('a','v','c','1') },
568 { CODEC_ID_MPEG4
, MKTAG('m','p','4','v') },
569 { CODEC_ID_AAC
, MKTAG('m','p','4','a') },
570 { CODEC_ID_ALAC
, MKTAG('a','l','a','c') },
571 { CODEC_ID_AC3
, MKTAG('a','c','-','3') },
572 { CODEC_ID_MOV_TEXT
, MKTAG('t','x','3','g') },
573 { CODEC_ID_NONE
, 0 },
576 static int mov_find_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
578 int tag
= track
->enc
->codec_tag
;
579 if (track
->mode
== MODE_MP4
|| track
->mode
== MODE_PSP
) {
580 if (!codec_get_tag(ff_mp4_obj_type
, track
->enc
->codec_id
))
582 if (track
->enc
->codec_id
== CODEC_ID_H264
) tag
= MKTAG('a','v','c','1');
583 else if (track
->enc
->codec_id
== CODEC_ID_AC3
) tag
= MKTAG('a','c','-','3');
584 else if (track
->enc
->codec_id
== CODEC_ID_DIRAC
) tag
= MKTAG('d','r','a','c');
585 else if (track
->enc
->codec_id
== CODEC_ID_MOV_TEXT
) tag
= MKTAG('t','x','3','g');
586 else if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) tag
= MKTAG('m','p','4','v');
587 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) tag
= MKTAG('m','p','4','a');
588 } else if (track
->mode
== MODE_IPOD
) {
589 tag
= codec_get_tag(codec_ipod_tags
, track
->enc
->codec_id
);
590 if (!match_ext(s
->filename
, "m4a") && !match_ext(s
->filename
, "m4v"))
591 av_log(s
, AV_LOG_WARNING
, "Warning, extension is not .m4a nor .m4v "
592 "Quicktime/Ipod might not play the file\n");
593 } else if (track
->mode
& MODE_3GP
) {
594 tag
= codec_get_tag(codec_3gp_tags
, track
->enc
->codec_id
);
595 } else if (!tag
|| (track
->enc
->strict_std_compliance
>= FF_COMPLIANCE_NORMAL
&&
596 (tag
== MKTAG('d','v','c','p') ||
597 track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
))) {
598 if (track
->enc
->codec_id
== CODEC_ID_DVVIDEO
) {
599 if (track
->enc
->height
== 480) /* NTSC */
600 if (track
->enc
->pix_fmt
== PIX_FMT_YUV422P
) tag
= MKTAG('d','v','5','n');
601 else tag
= MKTAG('d','v','c',' ');
602 else if (track
->enc
->pix_fmt
== PIX_FMT_YUV422P
) tag
= MKTAG('d','v','5','p');
603 else if (track
->enc
->pix_fmt
== PIX_FMT_YUV420P
) tag
= MKTAG('d','v','c','p');
604 else tag
= MKTAG('d','v','p','p');
605 } else if (track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
) {
606 tag
= codec_get_tag(mov_pix_fmt_tags
, track
->enc
->pix_fmt
);
607 if (!tag
) // restore tag
608 tag
= track
->enc
->codec_tag
;
610 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) {
611 tag
= codec_get_tag(codec_movvideo_tags
, track
->enc
->codec_id
);
612 if (!tag
) { // if no mac fcc found, try with Microsoft tags
613 tag
= codec_get_tag(codec_bmp_tags
, track
->enc
->codec_id
);
615 av_log(s
, AV_LOG_INFO
, "Warning, using MS style video codec tag, "
616 "the file may be unplayable!\n");
618 } else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) {
619 tag
= codec_get_tag(codec_movaudio_tags
, track
->enc
->codec_id
);
620 if (!tag
) { // if no mac fcc found, try with Microsoft tags
621 int ms_tag
= codec_get_tag(codec_wav_tags
, track
->enc
->codec_id
);
623 tag
= MKTAG('m', 's', ((ms_tag
>> 8) & 0xff), (ms_tag
& 0xff));
624 av_log(s
, AV_LOG_INFO
, "Warning, using MS style audio codec tag, "
625 "the file may be unplayable!\n");
628 } else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
629 tag
= codec_get_tag(ff_codec_movsubtitle_tags
, track
->enc
->codec_id
);
637 * Needed to make file play in iPods running newest firmware
638 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
640 static int mov_write_uuid_tag_ipod(ByteIOContext
*pb
)
644 put_be32(pb
, 0x6b6840f2);
645 put_be32(pb
, 0x5f244fc5);
646 put_be32(pb
, 0xba39a51b);
647 put_be32(pb
, 0xcf0323f3);
652 static int mov_write_subtitle_tag(ByteIOContext
*pb
, MOVTrack
*track
)
654 int64_t pos
= url_ftell(pb
);
655 put_be32(pb
, 0); /* size */
656 put_le32(pb
, track
->tag
); // store it byteswapped
657 put_be32(pb
, 0); /* Reserved */
658 put_be16(pb
, 0); /* Reserved */
659 put_be16(pb
, 1); /* Data-reference index */
661 if (track
->enc
->extradata_size
)
662 put_buffer(pb
, track
->enc
->extradata
, track
->enc
->extradata_size
);
664 return updateSize(pb
, pos
);
667 static int mov_write_video_tag(ByteIOContext
*pb
, MOVTrack
*track
)
669 int64_t pos
= url_ftell(pb
);
670 char compressor_name
[32];
672 put_be32(pb
, 0); /* size */
673 put_le32(pb
, track
->tag
); // store it byteswapped
674 put_be32(pb
, 0); /* Reserved */
675 put_be16(pb
, 0); /* Reserved */
676 put_be16(pb
, 1); /* Data-reference index */
678 put_be16(pb
, 0); /* Codec stream version */
679 put_be16(pb
, 0); /* Codec stream revision (=0) */
680 if (track
->mode
== MODE_MOV
) {
681 put_tag(pb
, "FFMP"); /* Vendor */
682 if(track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
) {
683 put_be32(pb
, 0); /* Temporal Quality */
684 put_be32(pb
, 0x400); /* Spatial Quality = lossless*/
686 put_be32(pb
, 0x200); /* Temporal Quality = normal */
687 put_be32(pb
, 0x200); /* Spatial Quality = normal */
690 put_be32(pb
, 0); /* Reserved */
691 put_be32(pb
, 0); /* Reserved */
692 put_be32(pb
, 0); /* Reserved */
694 put_be16(pb
, track
->enc
->width
); /* Video width */
695 put_be16(pb
, track
->height
); /* Video height */
696 put_be32(pb
, 0x00480000); /* Horizontal resolution 72dpi */
697 put_be32(pb
, 0x00480000); /* Vertical resolution 72dpi */
698 put_be32(pb
, 0); /* Data size (= 0) */
699 put_be16(pb
, 1); /* Frame count (= 1) */
701 memset(compressor_name
,0,32);
702 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
703 if (track
->mode
== MODE_MOV
&& track
->enc
->codec
&& track
->enc
->codec
->name
)
704 strncpy(compressor_name
,track
->enc
->codec
->name
,31);
705 put_byte(pb
, strlen(compressor_name
));
706 put_buffer(pb
, compressor_name
, 31);
708 if (track
->mode
== MODE_MOV
&& track
->enc
->bits_per_coded_sample
)
709 put_be16(pb
, track
->enc
->bits_per_coded_sample
);
711 put_be16(pb
, 0x18); /* Reserved */
712 put_be16(pb
, 0xffff); /* Reserved */
713 if(track
->tag
== MKTAG('m','p','4','v'))
714 mov_write_esds_tag(pb
, track
);
715 else if(track
->enc
->codec_id
== CODEC_ID_H263
)
716 mov_write_d263_tag(pb
);
717 else if(track
->enc
->codec_id
== CODEC_ID_SVQ3
)
718 mov_write_svq3_tag(pb
);
719 else if(track
->enc
->codec_id
== CODEC_ID_DNXHD
)
720 mov_write_avid_tag(pb
, track
);
721 else if(track
->enc
->codec_id
== CODEC_ID_H264
) {
722 mov_write_avcc_tag(pb
, track
);
723 if(track
->mode
== MODE_IPOD
)
724 mov_write_uuid_tag_ipod(pb
);
725 } else if(track
->vosLen
> 0)
726 mov_write_glbl_tag(pb
, track
);
728 return updateSize(pb
, pos
);
731 static int mov_write_stsd_tag(ByteIOContext
*pb
, MOVTrack
*track
)
733 int64_t pos
= url_ftell(pb
);
734 put_be32(pb
, 0); /* size */
736 put_be32(pb
, 0); /* version & flags */
737 put_be32(pb
, 1); /* entry count */
738 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
)
739 mov_write_video_tag(pb
, track
);
740 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
741 mov_write_audio_tag(pb
, track
);
742 else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
)
743 mov_write_subtitle_tag(pb
, track
);
744 return updateSize(pb
, pos
);
747 static int mov_write_ctts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
749 MOVStts
*ctts_entries
;
750 uint32_t entries
= 0;
754 ctts_entries
= av_malloc((track
->entry
+ 1) * sizeof(*ctts_entries
)); /* worst case */
755 ctts_entries
[0].count
= 1;
756 ctts_entries
[0].duration
= track
->cluster
[0].cts
;
757 for (i
=1; i
<track
->entry
; i
++) {
758 if (track
->cluster
[i
].cts
== ctts_entries
[entries
].duration
) {
759 ctts_entries
[entries
].count
++; /* compress */
762 ctts_entries
[entries
].duration
= track
->cluster
[i
].cts
;
763 ctts_entries
[entries
].count
= 1;
766 entries
++; /* last one */
767 atom_size
= 16 + (entries
* 8);
768 put_be32(pb
, atom_size
); /* size */
770 put_be32(pb
, 0); /* version & flags */
771 put_be32(pb
, entries
); /* entry count */
772 for (i
=0; i
<entries
; i
++) {
773 put_be32(pb
, ctts_entries
[i
].count
);
774 put_be32(pb
, ctts_entries
[i
].duration
);
776 av_free(ctts_entries
);
780 /* Time to sample atom */
781 static int mov_write_stts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
783 MOVStts
*stts_entries
;
784 uint32_t entries
= -1;
788 if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
&& !track
->audio_vbr
) {
789 stts_entries
= av_malloc(sizeof(*stts_entries
)); /* one entry */
790 stts_entries
[0].count
= track
->sampleCount
;
791 stts_entries
[0].duration
= 1;
794 stts_entries
= av_malloc(track
->entry
* sizeof(*stts_entries
)); /* worst case */
795 for (i
=0; i
<track
->entry
; i
++) {
796 int64_t duration
= i
+ 1 == track
->entry
?
797 track
->trackDuration
- track
->cluster
[i
].dts
+ track
->cluster
[0].dts
: /* readjusting */
798 track
->cluster
[i
+1].dts
- track
->cluster
[i
].dts
;
799 if (i
&& duration
== stts_entries
[entries
].duration
) {
800 stts_entries
[entries
].count
++; /* compress */
803 stts_entries
[entries
].duration
= duration
;
804 stts_entries
[entries
].count
= 1;
807 entries
++; /* last one */
809 atom_size
= 16 + (entries
* 8);
810 put_be32(pb
, atom_size
); /* size */
812 put_be32(pb
, 0); /* version & flags */
813 put_be32(pb
, entries
); /* entry count */
814 for (i
=0; i
<entries
; i
++) {
815 put_be32(pb
, stts_entries
[i
].count
);
816 put_be32(pb
, stts_entries
[i
].duration
);
818 av_free(stts_entries
);
822 static int mov_write_dref_tag(ByteIOContext
*pb
)
824 put_be32(pb
, 28); /* size */
826 put_be32(pb
, 0); /* version & flags */
827 put_be32(pb
, 1); /* entry count */
829 put_be32(pb
, 0xc); /* size */
831 put_be32(pb
, 1); /* version & flags */
836 static int mov_write_stbl_tag(ByteIOContext
*pb
, MOVTrack
*track
)
838 int64_t pos
= url_ftell(pb
);
839 put_be32(pb
, 0); /* size */
841 mov_write_stsd_tag(pb
, track
);
842 mov_write_stts_tag(pb
, track
);
843 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
&&
844 track
->hasKeyframes
&& track
->hasKeyframes
< track
->entry
)
845 mov_write_stss_tag(pb
, track
);
846 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
&&
848 mov_write_ctts_tag(pb
, track
);
849 mov_write_stsc_tag(pb
, track
);
850 mov_write_stsz_tag(pb
, track
);
851 mov_write_stco_tag(pb
, track
);
852 return updateSize(pb
, pos
);
855 static int mov_write_dinf_tag(ByteIOContext
*pb
)
857 int64_t pos
= url_ftell(pb
);
858 put_be32(pb
, 0); /* size */
860 mov_write_dref_tag(pb
);
861 return updateSize(pb
, pos
);
864 static int mov_write_nmhd_tag(ByteIOContext
*pb
)
872 static int mov_write_gmhd_tag(ByteIOContext
*pb
)
874 put_be32(pb
, 0x20); /* size */
876 put_be32(pb
, 0x18); /* gmin size */
877 put_tag(pb
, "gmin"); /* generic media info */
878 put_be32(pb
, 0); /* version & flags */
879 put_be16(pb
, 0x40); /* graphics mode = */
880 put_be16(pb
, 0x8000); /* opColor (r?) */
881 put_be16(pb
, 0x8000); /* opColor (g?) */
882 put_be16(pb
, 0x8000); /* opColor (b?) */
883 put_be16(pb
, 0); /* balance */
884 put_be16(pb
, 0); /* reserved */
888 static int mov_write_smhd_tag(ByteIOContext
*pb
)
890 put_be32(pb
, 16); /* size */
892 put_be32(pb
, 0); /* version & flags */
893 put_be16(pb
, 0); /* reserved (balance, normally = 0) */
894 put_be16(pb
, 0); /* reserved */
898 static int mov_write_vmhd_tag(ByteIOContext
*pb
)
900 put_be32(pb
, 0x14); /* size (always 0x14) */
902 put_be32(pb
, 0x01); /* version & flags */
903 put_be64(pb
, 0); /* reserved (graphics mode = copy) */
907 static int mov_write_hdlr_tag(ByteIOContext
*pb
, MOVTrack
*track
)
909 const char *hdlr
, *descr
= NULL
, *hdlr_type
= NULL
;
910 int64_t pos
= url_ftell(pb
);
912 if (!track
) { /* no media --> data handler */
915 descr
= "DataHandler";
917 hdlr
= (track
->mode
== MODE_MOV
) ? "mhlr" : "\0\0\0\0";
918 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) {
920 descr
= "VideoHandler";
921 } else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) {
923 descr
= "SoundHandler";
924 } else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
925 if (track
->mode
== MODE_IPOD
) hdlr_type
= "sbtl";
926 else hdlr_type
= "text";
927 descr
= "SubtitleHandler";
931 put_be32(pb
, 0); /* size */
933 put_be32(pb
, 0); /* Version & flags */
934 put_buffer(pb
, hdlr
, 4); /* handler */
935 put_tag(pb
, hdlr_type
); /* handler type */
936 put_be32(pb
,0); /* reserved */
937 put_be32(pb
,0); /* reserved */
938 put_be32(pb
,0); /* reserved */
939 put_byte(pb
, strlen(descr
)); /* string counter */
940 put_buffer(pb
, descr
, strlen(descr
)); /* handler description */
941 return updateSize(pb
, pos
);
944 static int mov_write_minf_tag(ByteIOContext
*pb
, MOVTrack
*track
)
946 int64_t pos
= url_ftell(pb
);
947 put_be32(pb
, 0); /* size */
949 if(track
->enc
->codec_type
== CODEC_TYPE_VIDEO
)
950 mov_write_vmhd_tag(pb
);
951 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
952 mov_write_smhd_tag(pb
);
953 else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
954 if (track
->mode
== MODE_MOV
) mov_write_gmhd_tag(pb
);
955 else mov_write_nmhd_tag(pb
);
957 if (track
->mode
== MODE_MOV
) /* FIXME: Why do it for MODE_MOV only ? */
958 mov_write_hdlr_tag(pb
, NULL
);
959 mov_write_dinf_tag(pb
);
960 mov_write_stbl_tag(pb
, track
);
961 return updateSize(pb
, pos
);
964 static int mov_write_mdhd_tag(ByteIOContext
*pb
, MOVTrack
*track
)
966 int version
= track
->trackDuration
< INT32_MAX
? 0 : 1;
968 (version
== 1) ? put_be32(pb
, 44) : put_be32(pb
, 32); /* size */
970 put_byte(pb
, version
);
971 put_be24(pb
, 0); /* flags */
973 put_be64(pb
, track
->time
);
974 put_be64(pb
, track
->time
);
976 put_be32(pb
, track
->time
); /* creation time */
977 put_be32(pb
, track
->time
); /* modification time */
979 put_be32(pb
, track
->timescale
); /* time scale (sample rate for audio) */
980 (version
== 1) ? put_be64(pb
, track
->trackDuration
) : put_be32(pb
, track
->trackDuration
); /* duration */
981 put_be16(pb
, track
->language
); /* language */
982 put_be16(pb
, 0); /* reserved (quality) */
984 if(version
!=0 && track
->mode
== MODE_MOV
){
985 av_log(NULL
, AV_LOG_ERROR
,
986 "FATAL error, file duration too long for timebase, this file will not be\n"
987 "playable with quicktime. Choose a different timebase or a different\n"
988 "container format\n");
994 static int mov_write_mdia_tag(ByteIOContext
*pb
, MOVTrack
*track
)
996 int64_t pos
= url_ftell(pb
);
997 put_be32(pb
, 0); /* size */
999 mov_write_mdhd_tag(pb
, track
);
1000 mov_write_hdlr_tag(pb
, track
);
1001 mov_write_minf_tag(pb
, track
);
1002 return updateSize(pb
, pos
);
1005 static int mov_write_tkhd_tag(ByteIOContext
*pb
, MOVTrack
*track
, AVStream
*st
)
1007 int64_t duration
= av_rescale_rnd(track
->trackDuration
, globalTimescale
, track
->timescale
, AV_ROUND_UP
);
1008 int version
= duration
< INT32_MAX
? 0 : 1;
1010 (version
== 1) ? put_be32(pb
, 104) : put_be32(pb
, 92); /* size */
1011 put_tag(pb
, "tkhd");
1012 put_byte(pb
, version
);
1013 put_be24(pb
, 0xf); /* flags (track enabled) */
1015 put_be64(pb
, track
->time
);
1016 put_be64(pb
, track
->time
);
1018 put_be32(pb
, track
->time
); /* creation time */
1019 put_be32(pb
, track
->time
); /* modification time */
1021 put_be32(pb
, track
->trackID
); /* track-id */
1022 put_be32(pb
, 0); /* reserved */
1023 (version
== 1) ? put_be64(pb
, duration
) : put_be32(pb
, duration
);
1025 put_be32(pb
, 0); /* reserved */
1026 put_be32(pb
, 0); /* reserved */
1027 put_be32(pb
, 0x0); /* reserved (Layer & Alternate group) */
1028 /* Volume, only for audio */
1029 if(track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
1030 put_be16(pb
, 0x0100);
1033 put_be16(pb
, 0); /* reserved */
1035 /* Matrix structure */
1036 put_be32(pb
, 0x00010000); /* reserved */
1037 put_be32(pb
, 0x0); /* reserved */
1038 put_be32(pb
, 0x0); /* reserved */
1039 put_be32(pb
, 0x0); /* reserved */
1040 put_be32(pb
, 0x00010000); /* reserved */
1041 put_be32(pb
, 0x0); /* reserved */
1042 put_be32(pb
, 0x0); /* reserved */
1043 put_be32(pb
, 0x0); /* reserved */
1044 put_be32(pb
, 0x40000000); /* reserved */
1046 /* Track width and height, for visual only */
1047 if(track
->enc
->codec_type
== CODEC_TYPE_VIDEO
||
1048 track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
1049 double sample_aspect_ratio
= av_q2d(st
->sample_aspect_ratio
);
1050 if(!sample_aspect_ratio
|| track
->height
!= track
->enc
->height
)
1051 sample_aspect_ratio
= 1;
1052 put_be32(pb
, sample_aspect_ratio
* track
->enc
->width
*0x10000);
1053 put_be32(pb
, track
->height
*0x10000);
1062 // This box seems important for the psp playback ... without it the movie seems to hang
1063 static int mov_write_edts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
1065 put_be32(pb
, 0x24); /* size */
1066 put_tag(pb
, "edts");
1067 put_be32(pb
, 0x1c); /* size */
1068 put_tag(pb
, "elst");
1072 put_be32(pb
, av_rescale_rnd(track
->trackDuration
, globalTimescale
, track
->timescale
, AV_ROUND_UP
)); /* duration ... doesn't seem to effect psp */
1074 put_be32(pb
, track
->cluster
[0].cts
); /* first pts is cts since dts is 0 */
1075 put_be32(pb
, 0x00010000);
1079 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1080 static int mov_write_uuid_tag_psp(ByteIOContext
*pb
, MOVTrack
*mov
)
1082 put_be32(pb
, 0x34); /* size ... reports as 28 in mp4box! */
1083 put_tag(pb
, "uuid");
1084 put_tag(pb
, "USMT");
1085 put_be32(pb
, 0x21d24fce);
1086 put_be32(pb
, 0xbb88695c);
1087 put_be32(pb
, 0xfac9c740);
1088 put_be32(pb
, 0x1c); // another size here!
1089 put_tag(pb
, "MTDT");
1090 put_be32(pb
, 0x00010012);
1092 put_be32(pb
, 0x55c40000);
1098 static int mov_write_trak_tag(ByteIOContext
*pb
, MOVTrack
*track
, AVStream
*st
)
1100 int64_t pos
= url_ftell(pb
);
1101 put_be32(pb
, 0); /* size */
1102 put_tag(pb
, "trak");
1103 mov_write_tkhd_tag(pb
, track
, st
);
1104 if (track
->mode
== MODE_PSP
|| track
->hasBframes
)
1105 mov_write_edts_tag(pb
, track
); // PSP Movies require edts box
1106 mov_write_mdia_tag(pb
, track
);
1107 if (track
->mode
== MODE_PSP
)
1108 mov_write_uuid_tag_psp(pb
,track
); // PSP Movies require this uuid box
1109 return updateSize(pb
, pos
);
1113 /* TODO: Not sorted out, but not necessary either */
1114 static int mov_write_iods_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1116 put_be32(pb
, 0x15); /* size */
1117 put_tag(pb
, "iods");
1118 put_be32(pb
, 0); /* version & flags */
1119 put_be16(pb
, 0x1007);
1121 put_be16(pb
, 0x4fff);
1122 put_be16(pb
, 0xfffe);
1123 put_be16(pb
, 0x01ff);
1128 static int mov_write_mvhd_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1130 int maxTrackID
= 1, i
;
1131 int64_t maxTrackLenTemp
, maxTrackLen
= 0;
1134 for (i
=0; i
<mov
->nb_streams
; i
++) {
1135 if(mov
->tracks
[i
].entry
> 0) {
1136 maxTrackLenTemp
= av_rescale_rnd(mov
->tracks
[i
].trackDuration
, globalTimescale
, mov
->tracks
[i
].timescale
, AV_ROUND_UP
);
1137 if(maxTrackLen
< maxTrackLenTemp
)
1138 maxTrackLen
= maxTrackLenTemp
;
1139 if(maxTrackID
< mov
->tracks
[i
].trackID
)
1140 maxTrackID
= mov
->tracks
[i
].trackID
;
1144 version
= maxTrackLen
< UINT32_MAX
? 0 : 1;
1145 (version
== 1) ? put_be32(pb
, 120) : put_be32(pb
, 108); /* size */
1146 put_tag(pb
, "mvhd");
1147 put_byte(pb
, version
);
1148 put_be24(pb
, 0); /* flags */
1150 put_be64(pb
, mov
->time
);
1151 put_be64(pb
, mov
->time
);
1153 put_be32(pb
, mov
->time
); /* creation time */
1154 put_be32(pb
, mov
->time
); /* modification time */
1156 put_be32(pb
, mov
->timescale
); /* timescale */
1157 (version
== 1) ? put_be64(pb
, maxTrackLen
) : put_be32(pb
, maxTrackLen
); /* duration of longest track */
1159 put_be32(pb
, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1160 put_be16(pb
, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1161 put_be16(pb
, 0); /* reserved */
1162 put_be32(pb
, 0); /* reserved */
1163 put_be32(pb
, 0); /* reserved */
1165 /* Matrix structure */
1166 put_be32(pb
, 0x00010000); /* reserved */
1167 put_be32(pb
, 0x0); /* reserved */
1168 put_be32(pb
, 0x0); /* reserved */
1169 put_be32(pb
, 0x0); /* reserved */
1170 put_be32(pb
, 0x00010000); /* reserved */
1171 put_be32(pb
, 0x0); /* reserved */
1172 put_be32(pb
, 0x0); /* reserved */
1173 put_be32(pb
, 0x0); /* reserved */
1174 put_be32(pb
, 0x40000000); /* reserved */
1176 put_be32(pb
, 0); /* reserved (preview time) */
1177 put_be32(pb
, 0); /* reserved (preview duration) */
1178 put_be32(pb
, 0); /* reserved (poster time) */
1179 put_be32(pb
, 0); /* reserved (selection time) */
1180 put_be32(pb
, 0); /* reserved (selection duration) */
1181 put_be32(pb
, 0); /* reserved (current time) */
1182 put_be32(pb
, maxTrackID
+1); /* Next track id */
1186 static int mov_write_itunes_hdlr_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1189 int64_t pos
= url_ftell(pb
);
1190 put_be32(pb
, 0); /* size */
1191 put_tag(pb
, "hdlr");
1194 put_tag(pb
, "mdir");
1195 put_tag(pb
, "appl");
1199 return updateSize(pb
, pos
);
1202 /* helper function to write a data tag with the specified string as data */
1203 static int mov_write_string_data_tag(ByteIOContext
*pb
, const char *data
, int lang
, int long_style
)
1206 int64_t pos
= url_ftell(pb
);
1207 put_be32(pb
, 0); /* size */
1208 put_tag(pb
, "data");
1211 put_buffer(pb
, data
, strlen(data
));
1212 return updateSize(pb
, pos
);
1214 put_be16(pb
, strlen(data
)); /* string length */
1216 put_buffer(pb
, data
, strlen(data
));
1217 return strlen(data
) + 4;
1221 static int mov_write_string_tag(ByteIOContext
*pb
, const char *name
, const char *value
, int lang
, int long_style
){
1223 if (value
&& value
[0]) {
1224 int64_t pos
= url_ftell(pb
);
1225 put_be32(pb
, 0); /* size */
1227 mov_write_string_data_tag(pb
, value
, lang
, long_style
);
1228 size
= updateSize(pb
, pos
);
1233 static int mov_write_string_metadata(AVFormatContext
*s
, ByteIOContext
*pb
,
1234 const char *name
, const char *tag
,
1237 int l
, lang
= 0, len
, len2
;
1238 AVMetadataTag
*t
, *t2
= NULL
;
1241 if (!(t
= av_metadata_get(s
->metadata
, tag
, NULL
, 0)))
1244 len
= strlen(t
->key
);
1245 snprintf(tag2
, sizeof(tag2
), "%s-", tag
);
1246 while ((t2
= av_metadata_get(s
->metadata
, tag2
, t2
, AV_METADATA_IGNORE_SUFFIX
))) {
1247 len2
= strlen(t2
->key
);
1248 if (len2
== len
+4 && !strcmp(t
->value
, t2
->value
)
1249 && (l
=ff_mov_iso639_to_lang(&t2
->key
[len2
-3], 0)) >= 0) {
1254 return mov_write_string_tag(pb
, name
, t
->value
, lang
, long_style
);
1257 /* iTunes track number */
1258 static int mov_write_trkn_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1261 AVMetadataTag
*t
= av_metadata_get(s
->metadata
, "track", NULL
, 0);
1262 int size
= 0, track
= t
? atoi(t
->value
) : 0;
1264 int64_t pos
= url_ftell(pb
);
1265 put_be32(pb
, 0); /* size */
1266 put_tag(pb
, "trkn");
1268 int64_t pos
= url_ftell(pb
);
1269 put_be32(pb
, 0); /* size */
1270 put_tag(pb
, "data");
1271 put_be32(pb
, 0); // 8 bytes empty
1273 put_be16(pb
, 0); // empty
1274 put_be16(pb
, track
); // track number
1275 put_be16(pb
, 0); // total track number
1276 put_be16(pb
, 0); // empty
1277 updateSize(pb
, pos
);
1279 size
= updateSize(pb
, pos
);
1284 /* iTunes meta data list */
1285 static int mov_write_ilst_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1288 int64_t pos
= url_ftell(pb
);
1289 put_be32(pb
, 0); /* size */
1290 put_tag(pb
, "ilst");
1291 mov_write_string_metadata(s
, pb
, "\251nam", "title" , 1);
1292 mov_write_string_metadata(s
, pb
, "\251ART", "author" , 1);
1293 mov_write_string_metadata(s
, pb
, "\251wrt", "author" , 1);
1294 mov_write_string_metadata(s
, pb
, "\251alb", "album" , 1);
1295 mov_write_string_metadata(s
, pb
, "\251day", "year" , 1);
1296 mov_write_string_tag(pb
, "\251too", LIBAVFORMAT_IDENT
, 0, 1);
1297 mov_write_string_metadata(s
, pb
, "\251cmt", "comment" , 1);
1298 mov_write_string_metadata(s
, pb
, "\251gen", "genre" , 1);
1299 mov_write_string_metadata(s
, pb
, "\251cpy", "copyright", 1);
1300 mov_write_trkn_tag(pb
, mov
, s
);
1301 return updateSize(pb
, pos
);
1304 /* iTunes meta data tag */
1305 static int mov_write_meta_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1309 int64_t pos
= url_ftell(pb
);
1310 put_be32(pb
, 0); /* size */
1311 put_tag(pb
, "meta");
1313 mov_write_itunes_hdlr_tag(pb
, mov
, s
);
1314 mov_write_ilst_tag(pb
, mov
, s
);
1315 size
= updateSize(pb
, pos
);
1319 static int utf8len(const uint8_t *b
)
1324 GET_UTF8(val
, *b
++, return -1;)
1330 static int ascii_to_wc(ByteIOContext
*pb
, const uint8_t *b
)
1334 GET_UTF8(val
, *b
++, return -1;)
1341 static uint16_t language_code(const char *str
)
1343 return (((str
[0]-0x60) & 0x1F) << 10) + (((str
[1]-0x60) & 0x1F) << 5) + ((str
[2]-0x60) & 0x1F);
1346 static int mov_write_3gp_udta_tag(ByteIOContext
*pb
, AVFormatContext
*s
,
1347 const char *tag
, const char *str
)
1349 int64_t pos
= url_ftell(pb
);
1350 AVMetadataTag
*t
= av_metadata_get(s
->metadata
, str
, NULL
, 0);
1351 if (!t
|| !utf8len(t
->value
))
1353 put_be32(pb
, 0); /* size */
1354 put_tag (pb
, tag
); /* type */
1355 put_be32(pb
, 0); /* version + flags */
1356 if (!strcmp(tag
, "yrrc"))
1357 put_be16(pb
, atoi(t
->value
));
1359 put_be16(pb
, language_code("eng")); /* language */
1360 ascii_to_wc(pb
, t
->value
);
1361 if (!strcmp(tag
, "albm") &&
1362 (t
= av_metadata_get(s
->metadata
, "year", NULL
, 0)))
1363 put_byte(pb
, atoi(t
->value
));
1365 return updateSize(pb
, pos
);
1368 static int mov_write_udta_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1371 ByteIOContext
*pb_buf
;
1375 for (i
= 0; i
< s
->nb_streams
; i
++)
1376 if (mov
->tracks
[i
].enc
->flags
& CODEC_FLAG_BITEXACT
) {
1380 ret
= url_open_dyn_buf(&pb_buf
);
1384 if (mov
->mode
& MODE_3GP
) {
1385 mov_write_3gp_udta_tag(pb_buf
, s
, "titl", "title");
1386 mov_write_3gp_udta_tag(pb_buf
, s
, "auth", "author");
1387 mov_write_3gp_udta_tag(pb_buf
, s
, "gnre", "genre");
1388 mov_write_3gp_udta_tag(pb_buf
, s
, "dscp", "comment");
1389 mov_write_3gp_udta_tag(pb_buf
, s
, "albm", "album");
1390 mov_write_3gp_udta_tag(pb_buf
, s
, "cprt", "copyright");
1391 mov_write_3gp_udta_tag(pb_buf
, s
, "yrrc", "year");
1392 } else if (mov
->mode
== MODE_MOV
) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1393 mov_write_string_metadata(s
, pb_buf
, "\251nam", "title" , 0);
1394 mov_write_string_metadata(s
, pb_buf
, "\251aut", "author" , 0);
1395 mov_write_string_metadata(s
, pb_buf
, "\251alb", "album" , 0);
1396 mov_write_string_metadata(s
, pb_buf
, "\251day", "year" , 0);
1397 mov_write_string_tag(pb_buf
, "\251enc", LIBAVFORMAT_IDENT
, 0, 0);
1398 mov_write_string_metadata(s
, pb_buf
, "\251des", "comment" , 0);
1399 mov_write_string_metadata(s
, pb_buf
, "\251gen", "genre" , 0);
1400 mov_write_string_metadata(s
, pb_buf
, "\251cpy", "copyright" , 0);
1402 /* iTunes meta data */
1403 mov_write_meta_tag(pb_buf
, mov
, s
);
1406 if ((size
= url_close_dyn_buf(pb_buf
, &buf
)) > 0) {
1407 put_be32(pb
, size
+8);
1408 put_tag(pb
, "udta");
1409 put_buffer(pb
, buf
, size
);
1416 static void mov_write_psp_udta_tag(ByteIOContext
*pb
,
1417 const char *str
, const char *lang
, int type
)
1419 int len
= utf8len(str
)+1;
1422 put_be16(pb
, len
*2+10); /* size */
1423 put_be32(pb
, type
); /* type */
1424 put_be16(pb
, language_code(lang
)); /* language */
1425 put_be16(pb
, 0x01); /* ? */
1426 ascii_to_wc(pb
, str
);
1429 static int mov_write_uuidusmt_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1431 AVMetadataTag
*title
= av_metadata_get(s
->metadata
, "title", NULL
, 0);
1435 pos
= url_ftell(pb
);
1436 put_be32(pb
, 0); /* size placeholder*/
1437 put_tag(pb
, "uuid");
1438 put_tag(pb
, "USMT");
1439 put_be32(pb
, 0x21d24fce); /* 96 bit UUID */
1440 put_be32(pb
, 0xbb88695c);
1441 put_be32(pb
, 0xfac9c740);
1443 pos2
= url_ftell(pb
);
1444 put_be32(pb
, 0); /* size placeholder*/
1445 put_tag(pb
, "MTDT");
1449 put_be16(pb
, 0x0C); /* size */
1450 put_be32(pb
, 0x0B); /* type */
1451 put_be16(pb
, language_code("und")); /* language */
1452 put_be16(pb
, 0x0); /* ? */
1453 put_be16(pb
, 0x021C); /* data */
1455 mov_write_psp_udta_tag(pb
, LIBAVCODEC_IDENT
, "eng", 0x04);
1456 mov_write_psp_udta_tag(pb
, title
->value
, "eng", 0x01);
1457 // 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);
1458 mov_write_psp_udta_tag(pb
, "2006/04/01 11:11:11", "und", 0x03);
1460 updateSize(pb
, pos2
);
1461 return updateSize(pb
, pos
);
1467 static int mov_write_moov_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1471 int64_t pos
= url_ftell(pb
);
1472 put_be32(pb
, 0); /* size placeholder*/
1473 put_tag(pb
, "moov");
1474 mov
->timescale
= globalTimescale
;
1476 for (i
=0; i
<mov
->nb_streams
; i
++) {
1477 if(mov
->tracks
[i
].entry
<= 0) continue;
1479 mov
->tracks
[i
].time
= mov
->time
;
1480 mov
->tracks
[i
].trackID
= i
+1;
1483 mov_write_mvhd_tag(pb
, mov
);
1484 //mov_write_iods_tag(pb, mov);
1485 for (i
=0; i
<mov
->nb_streams
; i
++) {
1486 if(mov
->tracks
[i
].entry
> 0) {
1487 mov_write_trak_tag(pb
, &(mov
->tracks
[i
]), s
->streams
[i
]);
1491 if (mov
->mode
== MODE_PSP
)
1492 mov_write_uuidusmt_tag(pb
, s
);
1494 mov_write_udta_tag(pb
, mov
, s
);
1496 return updateSize(pb
, pos
);
1499 static int mov_write_mdat_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1501 put_be32(pb
, 8); // placeholder for extended size field (64 bit)
1502 put_tag(pb
, mov
->mode
== MODE_MOV
? "wide" : "free");
1504 mov
->mdat_pos
= url_ftell(pb
);
1505 put_be32(pb
, 0); /* size placeholder*/
1506 put_tag(pb
, "mdat");
1510 /* TODO: This needs to be more general */
1511 static int mov_write_ftyp_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1513 MOVMuxContext
*mov
= s
->priv_data
;
1514 int64_t pos
= url_ftell(pb
);
1515 int has_h264
= 0, has_video
= 0;
1519 for (i
= 0; i
< s
->nb_streams
; i
++) {
1520 AVStream
*st
= s
->streams
[i
];
1521 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
)
1523 if (st
->codec
->codec_id
== CODEC_ID_H264
)
1527 put_be32(pb
, 0); /* size */
1528 put_tag(pb
, "ftyp");
1530 if (mov
->mode
== MODE_3GP
) {
1531 put_tag(pb
, has_h264
? "3gp6" : "3gp4");
1532 minor
= has_h264
? 0x100 : 0x200;
1533 } else if (mov
->mode
& MODE_3G2
) {
1534 put_tag(pb
, has_h264
? "3g2b" : "3g2a");
1535 minor
= has_h264
? 0x20000 : 0x10000;
1536 }else if (mov
->mode
== MODE_PSP
)
1537 put_tag(pb
, "MSNV");
1538 else if (mov
->mode
== MODE_MP4
)
1539 put_tag(pb
, "isom");
1540 else if (mov
->mode
== MODE_IPOD
)
1541 put_tag(pb
, has_video
? "M4V ":"M4A ");
1545 put_be32(pb
, minor
);
1547 if(mov
->mode
== MODE_MOV
)
1550 put_tag(pb
, "isom");
1551 put_tag(pb
, "iso2");
1553 put_tag(pb
, "avc1");
1556 if (mov
->mode
== MODE_3GP
)
1557 put_tag(pb
, has_h264
? "3gp6":"3gp4");
1558 else if (mov
->mode
& MODE_3G2
)
1559 put_tag(pb
, has_h264
? "3g2b":"3g2a");
1560 else if (mov
->mode
== MODE_PSP
)
1561 put_tag(pb
, "MSNV");
1562 else if (mov
->mode
== MODE_MP4
)
1563 put_tag(pb
, "mp41");
1564 return updateSize(pb
, pos
);
1567 static void mov_write_uuidprof_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1569 AVCodecContext
*VideoCodec
= s
->streams
[0]->codec
;
1570 AVCodecContext
*AudioCodec
= s
->streams
[1]->codec
;
1571 int AudioRate
= AudioCodec
->sample_rate
;
1572 int FrameRate
= ((VideoCodec
->time_base
.den
) * (0x10000))/ (VideoCodec
->time_base
.num
);
1573 int audio_kbitrate
= AudioCodec
->bit_rate
/ 1000;
1574 int video_kbitrate
= FFMIN(VideoCodec
->bit_rate
/ 1000, 800 - audio_kbitrate
);
1576 put_be32(pb
, 0x94); /* size */
1577 put_tag(pb
, "uuid");
1578 put_tag(pb
, "PROF");
1580 put_be32(pb
, 0x21d24fce); /* 96 bit UUID */
1581 put_be32(pb
, 0xbb88695c);
1582 put_be32(pb
, 0xfac9c740);
1584 put_be32(pb
, 0x0); /* ? */
1585 put_be32(pb
, 0x3); /* 3 sections ? */
1587 put_be32(pb
, 0x14); /* size */
1588 put_tag(pb
, "FPRF");
1589 put_be32(pb
, 0x0); /* ? */
1590 put_be32(pb
, 0x0); /* ? */
1591 put_be32(pb
, 0x0); /* ? */
1593 put_be32(pb
, 0x2c); /* size */
1594 put_tag(pb
, "APRF"); /* audio */
1596 put_be32(pb
, 0x2); /* TrackID */
1597 put_tag(pb
, "mp4a");
1598 put_be32(pb
, 0x20f);
1600 put_be32(pb
, audio_kbitrate
);
1601 put_be32(pb
, audio_kbitrate
);
1602 put_be32(pb
, AudioRate
);
1603 put_be32(pb
, AudioCodec
->channels
);
1605 put_be32(pb
, 0x34); /* size */
1606 put_tag(pb
, "VPRF"); /* video */
1608 put_be32(pb
, 0x1); /* TrackID */
1609 if (VideoCodec
->codec_id
== CODEC_ID_H264
) {
1610 put_tag(pb
, "avc1");
1611 put_be16(pb
, 0x014D);
1612 put_be16(pb
, 0x0015);
1614 put_tag(pb
, "mp4v");
1615 put_be16(pb
, 0x0000);
1616 put_be16(pb
, 0x0103);
1619 put_be32(pb
, video_kbitrate
);
1620 put_be32(pb
, video_kbitrate
);
1621 put_be32(pb
, FrameRate
);
1622 put_be32(pb
, FrameRate
);
1623 put_be16(pb
, VideoCodec
->width
);
1624 put_be16(pb
, VideoCodec
->height
);
1625 put_be32(pb
, 0x010001); /* ? */
1628 static int mov_write_header(AVFormatContext
*s
)
1630 ByteIOContext
*pb
= s
->pb
;
1631 MOVMuxContext
*mov
= s
->priv_data
;
1634 if (url_is_streamed(s
->pb
)) {
1635 av_log(s
, AV_LOG_ERROR
, "muxer does not support non seekable output\n");
1639 /* Default mode == MP4 */
1640 mov
->mode
= MODE_MP4
;
1642 if (s
->oformat
!= NULL
) {
1643 if (!strcmp("3gp", s
->oformat
->name
)) mov
->mode
= MODE_3GP
;
1644 else if (!strcmp("3g2", s
->oformat
->name
)) mov
->mode
= MODE_3GP
|MODE_3G2
;
1645 else if (!strcmp("mov", s
->oformat
->name
)) mov
->mode
= MODE_MOV
;
1646 else if (!strcmp("psp", s
->oformat
->name
)) mov
->mode
= MODE_PSP
;
1647 else if (!strcmp("ipod",s
->oformat
->name
)) mov
->mode
= MODE_IPOD
;
1649 mov_write_ftyp_tag(pb
,s
);
1650 if (mov
->mode
== MODE_PSP
) {
1651 if (s
->nb_streams
!= 2) {
1652 av_log(s
, AV_LOG_ERROR
, "PSP mode need one video and one audio stream\n");
1655 mov_write_uuidprof_tag(pb
,s
);
1659 for(i
=0; i
<s
->nb_streams
; i
++){
1660 AVStream
*st
= s
->streams
[i
];
1661 MOVTrack
*track
= &mov
->tracks
[i
];
1662 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
,0);
1664 track
->enc
= st
->codec
;
1665 track
->language
= ff_mov_iso639_to_lang(lang
?lang
->value
:"und", mov
->mode
!=MODE_MOV
);
1666 if (track
->language
< 0)
1667 track
->language
= 0;
1668 track
->mode
= mov
->mode
;
1669 track
->tag
= mov_find_codec_tag(s
, track
);
1671 av_log(s
, AV_LOG_ERROR
, "track %d: could not find tag, "
1672 "codec not currently supported in container\n", i
);
1675 if(st
->codec
->codec_type
== CODEC_TYPE_VIDEO
){
1676 if (track
->tag
== MKTAG('m','x','3','p') || track
->tag
== MKTAG('m','x','3','n') ||
1677 track
->tag
== MKTAG('m','x','4','p') || track
->tag
== MKTAG('m','x','4','n') ||
1678 track
->tag
== MKTAG('m','x','5','p') || track
->tag
== MKTAG('m','x','5','n')) {
1679 if (st
->codec
->width
!= 720 || (st
->codec
->height
!= 608 && st
->codec
->height
!= 512)) {
1680 av_log(s
, AV_LOG_ERROR
, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
1683 track
->height
= track
->tag
>>24 == 'n' ? 486 : 576;
1685 track
->height
= st
->codec
->height
;
1686 track
->timescale
= st
->codec
->time_base
.den
;
1687 av_set_pts_info(st
, 64, 1, st
->codec
->time_base
.den
);
1688 if (track
->mode
== MODE_MOV
&& track
->timescale
> 100000)
1689 av_log(s
, AV_LOG_WARNING
,
1690 "WARNING codec timebase is very high. If duration is too long,\n"
1691 "file may not be playable by quicktime. Specify a shorter timebase\n"
1692 "or choose different container.\n");
1693 }else if(st
->codec
->codec_type
== CODEC_TYPE_AUDIO
){
1694 track
->timescale
= st
->codec
->sample_rate
;
1695 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
1696 if(!st
->codec
->frame_size
&& !av_get_bits_per_sample(st
->codec
->codec_id
)) {
1697 av_log(s
, AV_LOG_ERROR
, "track %d: codec frame size is not set\n", i
);
1699 }else if(st
->codec
->frame_size
> 1){ /* assume compressed audio */
1700 track
->audio_vbr
= 1;
1702 st
->codec
->frame_size
= 1;
1703 track
->sampleSize
= (av_get_bits_per_sample(st
->codec
->codec_id
) >> 3) * st
->codec
->channels
;
1705 if(track
->mode
!= MODE_MOV
&&
1706 track
->enc
->codec_id
== CODEC_ID_MP3
&& track
->enc
->sample_rate
< 16000){
1707 av_log(s
, AV_LOG_ERROR
, "track %d: muxing mp3 at %dhz is not supported\n",
1708 i
, track
->enc
->sample_rate
);
1711 }else if(st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
){
1712 track
->timescale
= st
->codec
->time_base
.den
;
1713 av_set_pts_info(st
, 64, 1, st
->codec
->time_base
.den
);
1717 mov_write_mdat_tag(pb
, mov
);
1718 mov
->time
= s
->timestamp
+ 0x7C25B080; //1970 based -> 1904 based
1719 mov
->nb_streams
= s
->nb_streams
;
1721 put_flush_packet(pb
);
1726 static int mov_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
1728 MOVMuxContext
*mov
= s
->priv_data
;
1729 ByteIOContext
*pb
= s
->pb
;
1730 MOVTrack
*trk
= &mov
->tracks
[pkt
->stream_index
];
1731 AVCodecContext
*enc
= trk
->enc
;
1732 unsigned int samplesInChunk
= 0;
1733 int size
= pkt
->size
;
1735 if (url_is_streamed(s
->pb
)) return 0; /* Can't handle that */
1736 if (!size
) return 0; /* Discard 0 sized packets */
1738 if (enc
->codec_id
== CODEC_ID_AMR_NB
) {
1739 /* We must find out how many AMR blocks there are in one packet */
1740 static uint16_t packed_size
[16] =
1741 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1744 while (len
< size
&& samplesInChunk
< 100) {
1745 len
+= packed_size
[(pkt
->data
[len
] >> 3) & 0x0F];
1748 if(samplesInChunk
> 1){
1749 av_log(s
, AV_LOG_ERROR
, "fatal error, input is not a single packet, implement a AVParser for it\n");
1752 } else if (trk
->sampleSize
)
1753 samplesInChunk
= size
/trk
->sampleSize
;
1757 /* copy extradata if it exists */
1758 if (trk
->vosLen
== 0 && enc
->extradata_size
> 0) {
1759 trk
->vosLen
= enc
->extradata_size
;
1760 trk
->vosData
= av_malloc(trk
->vosLen
);
1761 memcpy(trk
->vosData
, enc
->extradata
, trk
->vosLen
);
1764 if (enc
->codec_id
== CODEC_ID_H264
&& trk
->vosLen
> 0 && *(uint8_t *)trk
->vosData
!= 1) {
1765 /* from x264 or from bytestream h264 */
1766 /* nal reformating needed */
1767 size
= ff_avc_parse_nal_units(pb
, pkt
->data
, pkt
->size
);
1769 put_buffer(pb
, pkt
->data
, size
);
1772 if ((enc
->codec_id
== CODEC_ID_DNXHD
||
1773 enc
->codec_id
== CODEC_ID_AC3
) && !trk
->vosLen
) {
1774 /* copy frame to create needed atoms */
1776 trk
->vosData
= av_malloc(size
);
1778 return AVERROR(ENOMEM
);
1779 memcpy(trk
->vosData
, pkt
->data
, size
);
1782 if (!(trk
->entry
% MOV_INDEX_CLUSTER_SIZE
)) {
1783 trk
->cluster
= av_realloc(trk
->cluster
, (trk
->entry
+ MOV_INDEX_CLUSTER_SIZE
) * sizeof(*trk
->cluster
));
1788 trk
->cluster
[trk
->entry
].pos
= url_ftell(pb
) - size
;
1789 trk
->cluster
[trk
->entry
].samplesInChunk
= samplesInChunk
;
1790 trk
->cluster
[trk
->entry
].size
= size
;
1791 trk
->cluster
[trk
->entry
].entries
= samplesInChunk
;
1792 trk
->cluster
[trk
->entry
].dts
= pkt
->dts
;
1793 trk
->trackDuration
= pkt
->dts
- trk
->cluster
[0].dts
+ pkt
->duration
;
1795 if (pkt
->pts
== AV_NOPTS_VALUE
) {
1796 av_log(s
, AV_LOG_WARNING
, "pts has no value\n");
1797 pkt
->pts
= pkt
->dts
;
1799 if (pkt
->dts
!= pkt
->pts
)
1800 trk
->hasBframes
= 1;
1801 trk
->cluster
[trk
->entry
].cts
= pkt
->pts
- pkt
->dts
;
1802 trk
->cluster
[trk
->entry
].key_frame
= !!(pkt
->flags
& PKT_FLAG_KEY
);
1803 if(trk
->cluster
[trk
->entry
].key_frame
)
1804 trk
->hasKeyframes
++;
1806 trk
->sampleCount
+= samplesInChunk
;
1807 mov
->mdat_size
+= size
;
1809 put_flush_packet(pb
);
1813 static int mov_write_trailer(AVFormatContext
*s
)
1815 MOVMuxContext
*mov
= s
->priv_data
;
1816 ByteIOContext
*pb
= s
->pb
;
1820 int64_t moov_pos
= url_ftell(pb
);
1822 /* Write size of mdat tag */
1823 if (mov
->mdat_size
+8 <= UINT32_MAX
) {
1824 url_fseek(pb
, mov
->mdat_pos
, SEEK_SET
);
1825 put_be32(pb
, mov
->mdat_size
+8);
1827 /* overwrite 'wide' placeholder atom */
1828 url_fseek(pb
, mov
->mdat_pos
- 8, SEEK_SET
);
1829 put_be32(pb
, 1); /* special value: real atom size will be 64 bit value after tag field */
1830 put_tag(pb
, "mdat");
1831 put_be64(pb
, mov
->mdat_size
+16);
1833 url_fseek(pb
, moov_pos
, SEEK_SET
);
1835 mov_write_moov_tag(pb
, mov
, s
);
1837 for (i
=0; i
<mov
->nb_streams
; i
++) {
1838 av_freep(&mov
->tracks
[i
].cluster
);
1840 if(mov
->tracks
[i
].vosLen
) av_free(mov
->tracks
[i
].vosData
);
1844 put_flush_packet(pb
);
1849 #if CONFIG_MOV_MUXER
1850 AVOutputFormat mov_muxer
= {
1852 NULL_IF_CONFIG_SMALL("MOV format"),
1855 sizeof(MOVMuxContext
),
1861 .flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
,
1862 .codec_tag
= (const AVCodecTag
* const []){codec_movvideo_tags
, codec_movaudio_tags
, 0},
1865 #if CONFIG_TGP_MUXER
1866 AVOutputFormat tgp_muxer
= {
1868 NULL_IF_CONFIG_SMALL("3GP format"),
1871 sizeof(MOVMuxContext
),
1877 .flags
= AVFMT_GLOBALHEADER
,
1878 .codec_tag
= (const AVCodecTag
* const []){codec_3gp_tags
, 0},
1881 #if CONFIG_MP4_MUXER
1882 AVOutputFormat mp4_muxer
= {
1884 NULL_IF_CONFIG_SMALL("MP4 format"),
1887 sizeof(MOVMuxContext
),
1893 .flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
,
1894 .codec_tag
= (const AVCodecTag
* const []){ff_mp4_obj_type
, 0},
1897 #if CONFIG_PSP_MUXER
1898 AVOutputFormat psp_muxer
= {
1900 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
1903 sizeof(MOVMuxContext
),
1909 .flags
= AVFMT_GLOBALHEADER
,
1910 .codec_tag
= (const AVCodecTag
* const []){ff_mp4_obj_type
, 0},
1913 #if CONFIG_TG2_MUXER
1914 AVOutputFormat tg2_muxer
= {
1916 NULL_IF_CONFIG_SMALL("3GP2 format"),
1919 sizeof(MOVMuxContext
),
1925 .flags
= AVFMT_GLOBALHEADER
,
1926 .codec_tag
= (const AVCodecTag
* const []){codec_3gp_tags
, 0},
1929 #if CONFIG_IPOD_MUXER
1930 AVOutputFormat ipod_muxer
= {
1932 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
1935 sizeof(MOVMuxContext
),
1941 .flags
= AVFMT_GLOBALHEADER
,
1942 .codec_tag
= (const AVCodecTag
* const []){ff_mp4_obj_type
, 0},