3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavcodec/bitstream.h"
34 #define MOV_INDEX_CLUSTER_SIZE 16384
35 #define globalTimescale 1000
40 #define MODE_PSP 0x08 // example working PSP command line:
41 // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
43 #define MODE_IPOD 0x20
45 typedef struct MOVIentry
{
46 unsigned int flags
, size
;
48 unsigned int samplesInChunk
;
55 typedef struct MOVIndex
{
60 int64_t trackDuration
;
67 int tag
; ///< stsd fourcc
74 int height
; ///< active picture (w/o VBI) height for D-10/IMX
77 typedef struct MOVMuxContext
{
84 MOVTrack tracks
[MAX_STREAMS
];
87 //FIXME support 64 bit variant with wide placeholders
88 static int64_t updateSize(ByteIOContext
*pb
, int64_t pos
)
90 int64_t curpos
= url_ftell(pb
);
91 url_fseek(pb
, pos
, SEEK_SET
);
92 put_be32(pb
, curpos
- pos
); /* rewrite size */
93 url_fseek(pb
, curpos
, SEEK_SET
);
98 /* Chunk offset atom */
99 static int mov_write_stco_tag(ByteIOContext
*pb
, MOVTrack
*track
)
102 int mode64
= 0; // use 32 bit size variant if possible
103 int64_t pos
= url_ftell(pb
);
104 put_be32(pb
, 0); /* size */
105 if (pos
> UINT32_MAX
) {
110 put_be32(pb
, 0); /* version & flags */
111 put_be32(pb
, track
->entry
); /* entry count */
112 for (i
=0; i
<track
->entry
; i
++) {
114 put_be64(pb
, track
->cluster
[i
].pos
);
116 put_be32(pb
, track
->cluster
[i
].pos
);
118 return updateSize(pb
, pos
);
121 /* Sample size atom */
122 static int mov_write_stsz_tag(ByteIOContext
*pb
, MOVTrack
*track
)
125 int i
, j
, entries
= 0, tst
= -1, oldtst
= -1;
127 int64_t pos
= url_ftell(pb
);
128 put_be32(pb
, 0); /* size */
130 put_be32(pb
, 0); /* version & flags */
132 for (i
=0; i
<track
->entry
; i
++) {
133 tst
= track
->cluster
[i
].size
/track
->cluster
[i
].entries
;
134 if(oldtst
!= -1 && tst
!= oldtst
) {
138 entries
+= track
->cluster
[i
].entries
;
141 int sSize
= track
->cluster
[0].size
/track
->cluster
[0].entries
;
142 put_be32(pb
, sSize
); // sample size
143 put_be32(pb
, entries
); // sample count
146 put_be32(pb
, 0); // sample size
147 put_be32(pb
, entries
); // sample count
148 for (i
=0; i
<track
->entry
; i
++) {
149 for (j
=0; j
<track
->cluster
[i
].entries
; j
++) {
150 put_be32(pb
, track
->cluster
[i
].size
/
151 track
->cluster
[i
].entries
);
155 return updateSize(pb
, pos
);
158 /* Sample to chunk atom */
159 static int mov_write_stsc_tag(ByteIOContext
*pb
, MOVTrack
*track
)
161 int index
= 0, oldval
= -1, i
;
162 int64_t entryPos
, curpos
;
164 int64_t pos
= url_ftell(pb
);
165 put_be32(pb
, 0); /* size */
167 put_be32(pb
, 0); // version & flags
168 entryPos
= url_ftell(pb
);
169 put_be32(pb
, track
->entry
); // entry count
170 for (i
=0; i
<track
->entry
; i
++) {
171 if(oldval
!= track
->cluster
[i
].samplesInChunk
)
173 put_be32(pb
, i
+1); // first chunk
174 put_be32(pb
, track
->cluster
[i
].samplesInChunk
); // samples per chunk
175 put_be32(pb
, 0x1); // sample description index
176 oldval
= track
->cluster
[i
].samplesInChunk
;
180 curpos
= url_ftell(pb
);
181 url_fseek(pb
, entryPos
, SEEK_SET
);
182 put_be32(pb
, index
); // rewrite size
183 url_fseek(pb
, curpos
, SEEK_SET
);
185 return updateSize(pb
, pos
);
188 /* Sync sample atom */
189 static int mov_write_stss_tag(ByteIOContext
*pb
, MOVTrack
*track
)
191 int64_t curpos
, entryPos
;
193 int64_t pos
= url_ftell(pb
);
194 put_be32(pb
, 0); // size
196 put_be32(pb
, 0); // version & flags
197 entryPos
= url_ftell(pb
);
198 put_be32(pb
, track
->entry
); // entry count
199 for (i
=0; i
<track
->entry
; i
++) {
200 if(track
->cluster
[i
].key_frame
== 1) {
205 curpos
= url_ftell(pb
);
206 url_fseek(pb
, entryPos
, SEEK_SET
);
207 put_be32(pb
, index
); // rewrite size
208 url_fseek(pb
, curpos
, SEEK_SET
);
209 return updateSize(pb
, pos
);
212 static int mov_write_amr_tag(ByteIOContext
*pb
, MOVTrack
*track
)
214 put_be32(pb
, 0x11); /* size */
215 if (track
->mode
== MODE_MOV
) put_tag(pb
, "samr");
216 else put_tag(pb
, "damr");
218 put_byte(pb
, 0); /* decoder version */
220 put_be16(pb
, 0x81FF); /* Mode set (all modes for AMR_NB) */
221 put_byte(pb
, 0x00); /* Mode change period (no restriction) */
222 put_byte(pb
, 0x01); /* Frames per sample */
226 static int mov_write_ac3_tag(ByteIOContext
*pb
, MOVTrack
*track
)
231 int fscod
, bsid
, bsmod
, acmod
, lfeon
, frmsizecod
;
233 if (track
->vosLen
< 7)
239 init_get_bits(&gbc
, track
->vosData
+4, track
->vosLen
-4);
240 fscod
= get_bits(&gbc
, 2);
241 frmsizecod
= get_bits(&gbc
, 6);
242 bsid
= get_bits(&gbc
, 5);
243 bsmod
= get_bits(&gbc
, 3);
244 acmod
= get_bits(&gbc
, 3);
246 skip_bits(&gbc
, 2); // dsurmod
248 if ((acmod
& 1) && acmod
!= 1)
249 skip_bits(&gbc
, 2); // cmixlev
251 skip_bits(&gbc
, 2); // surmixlev
253 lfeon
= get_bits1(&gbc
);
255 init_put_bits(&pbc
, buf
, sizeof(buf
));
256 put_bits(&pbc
, 2, fscod
);
257 put_bits(&pbc
, 5, bsid
);
258 put_bits(&pbc
, 3, bsmod
);
259 put_bits(&pbc
, 3, acmod
);
260 put_bits(&pbc
, 1, lfeon
);
261 put_bits(&pbc
, 5, frmsizecod
>>1); // bit_rate_code
262 put_bits(&pbc
, 5, 0); // reserved
264 flush_put_bits(&pbc
);
265 put_buffer(pb
, buf
, sizeof(buf
));
271 * This function writes extradata "as is".
272 * Extradata must be formated like a valid atom (with size and tag)
274 static int mov_write_extradata_tag(ByteIOContext
*pb
, MOVTrack
*track
)
276 put_buffer(pb
, track
->enc
->extradata
, track
->enc
->extradata_size
);
277 return track
->enc
->extradata_size
;
280 static int mov_write_enda_tag(ByteIOContext
*pb
)
284 put_be16(pb
, 1); /* little endian */
288 static unsigned int descrLength(unsigned int len
)
291 for(i
=1; len
>>(7*i
); i
++);
295 static void putDescr(ByteIOContext
*pb
, int tag
, unsigned int size
)
297 int i
= descrLength(size
) - size
- 2;
300 put_byte(pb
, (size
>>(7*i
)) | 0x80);
301 put_byte(pb
, size
& 0x7F);
304 static int mov_write_esds_tag(ByteIOContext
*pb
, MOVTrack
*track
) // Basic
306 int64_t pos
= url_ftell(pb
);
307 int decoderSpecificInfoLen
= track
->vosLen
? descrLength(track
->vosLen
):0;
309 put_be32(pb
, 0); // size
311 put_be32(pb
, 0); // Version
314 putDescr(pb
, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen
) +
316 put_be16(pb
, track
->trackID
);
317 put_byte(pb
, 0x00); // flags (= no flags)
319 // DecoderConfig descriptor
320 putDescr(pb
, 0x04, 13 + decoderSpecificInfoLen
);
322 // Object type indication
323 if ((track
->enc
->codec_id
== CODEC_ID_MP2
||
324 track
->enc
->codec_id
== CODEC_ID_MP3
) &&
325 track
->enc
->sample_rate
> 24000)
326 put_byte(pb
, 0x6B); // 11172-3
328 put_byte(pb
, codec_get_tag(ff_mp4_obj_type
, track
->enc
->codec_id
));
330 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
331 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
332 if(track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
333 put_byte(pb
, 0x15); // flags (= Audiostream)
335 put_byte(pb
, 0x11); // flags (= Visualstream)
337 put_byte(pb
, track
->enc
->rc_buffer_size
>>(3+16)); // Buffersize DB (24 bits)
338 put_be16(pb
, (track
->enc
->rc_buffer_size
>>3)&0xFFFF); // Buffersize DB
340 put_be32(pb
, FFMAX(track
->enc
->bit_rate
, track
->enc
->rc_max_rate
)); // maxbitrate (FIXME should be max rate in any 1 sec window)
341 if(track
->enc
->rc_max_rate
!= track
->enc
->rc_min_rate
|| track
->enc
->rc_min_rate
==0)
342 put_be32(pb
, 0); // vbr
344 put_be32(pb
, track
->enc
->rc_max_rate
); // avg bitrate
347 // DecoderSpecific info descriptor
348 putDescr(pb
, 0x05, track
->vosLen
);
349 put_buffer(pb
, track
->vosData
, track
->vosLen
);
353 putDescr(pb
, 0x06, 1);
355 return updateSize(pb
, pos
);
358 static int mov_write_wave_tag(ByteIOContext
*pb
, MOVTrack
*track
)
360 int64_t pos
= url_ftell(pb
);
362 put_be32(pb
, 0); /* size */
365 put_be32(pb
, 12); /* size */
367 put_le32(pb
, track
->tag
);
369 if (track
->enc
->codec_id
== CODEC_ID_AAC
) {
370 /* useless atom needed by mplayer, ipod, not needed by quicktime */
371 put_be32(pb
, 12); /* size */
374 mov_write_esds_tag(pb
, track
);
375 } else if (track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
||
376 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
) {
377 mov_write_enda_tag(pb
);
378 } else if (track
->enc
->codec_id
== CODEC_ID_AMR_NB
) {
379 mov_write_amr_tag(pb
, track
);
380 } else if (track
->enc
->codec_id
== CODEC_ID_AC3
) {
381 mov_write_ac3_tag(pb
, track
);
382 } else if (track
->enc
->codec_id
== CODEC_ID_ALAC
) {
383 mov_write_extradata_tag(pb
, track
);
386 put_be32(pb
, 8); /* size */
387 put_be32(pb
, 0); /* null tag */
389 return updateSize(pb
, pos
);
392 static int mov_write_glbl_tag(ByteIOContext
*pb
, MOVTrack
*track
)
394 put_be32(pb
, track
->vosLen
+8);
396 put_buffer(pb
, track
->vosData
, track
->vosLen
);
397 return 8+track
->vosLen
;
400 static int mov_write_audio_tag(ByteIOContext
*pb
, MOVTrack
*track
)
402 int64_t pos
= url_ftell(pb
);
403 int version
= track
->mode
== MODE_MOV
&&
405 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
||
406 track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
);
408 put_be32(pb
, 0); /* size */
409 put_le32(pb
, track
->tag
); // store it byteswapped
410 put_be32(pb
, 0); /* Reserved */
411 put_be16(pb
, 0); /* Reserved */
412 put_be16(pb
, 1); /* Data-reference index, XXX == 1 */
414 /* SoundDescription */
415 put_be16(pb
, version
); /* Version */
416 put_be16(pb
, 0); /* Revision level */
417 put_be32(pb
, 0); /* Reserved */
419 if (track
->mode
== MODE_MOV
) {
420 put_be16(pb
, track
->enc
->channels
);
421 if (track
->enc
->codec_id
== CODEC_ID_PCM_U8
||
422 track
->enc
->codec_id
== CODEC_ID_PCM_S8
)
423 put_be16(pb
, 8); /* bits per sample */
426 put_be16(pb
, track
->audio_vbr
? -2 : 0); /* compression ID */
427 } else { /* reserved for mp4/3gp */
433 put_be16(pb
, 0); /* packet size (= 0) */
434 put_be16(pb
, track
->timescale
); /* Time scale */
435 put_be16(pb
, 0); /* Reserved */
437 if(version
== 1) { /* SoundDescription V1 extended info */
438 put_be32(pb
, track
->enc
->frame_size
); /* Samples per packet */
439 put_be32(pb
, track
->sampleSize
/ track
->enc
->channels
); /* Bytes per packet */
440 put_be32(pb
, track
->sampleSize
); /* Bytes per frame */
441 put_be32(pb
, 2); /* Bytes per sample */
444 if(track
->mode
== MODE_MOV
&&
445 (track
->enc
->codec_id
== CODEC_ID_AAC
||
446 track
->enc
->codec_id
== CODEC_ID_AC3
||
447 track
->enc
->codec_id
== CODEC_ID_AMR_NB
||
448 track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
||
449 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
||
450 track
->enc
->codec_id
== CODEC_ID_ALAC
))
451 mov_write_wave_tag(pb
, track
);
452 else if(track
->tag
== MKTAG('m','p','4','a'))
453 mov_write_esds_tag(pb
, track
);
454 else if(track
->enc
->codec_id
== CODEC_ID_AMR_NB
)
455 mov_write_amr_tag(pb
, track
);
456 else if(track
->enc
->codec_id
== CODEC_ID_AC3
)
457 mov_write_ac3_tag(pb
, track
);
458 else if(track
->enc
->codec_id
== CODEC_ID_ALAC
)
459 mov_write_extradata_tag(pb
, track
);
460 else if(track
->vosLen
> 0)
461 mov_write_glbl_tag(pb
, track
);
463 return updateSize(pb
, pos
);
466 static int mov_write_d263_tag(ByteIOContext
*pb
)
468 put_be32(pb
, 0xf); /* size */
471 put_byte(pb
, 0); /* decoder version */
472 /* FIXME use AVCodecContext level/profile, when encoder will set values */
473 put_byte(pb
, 0xa); /* level */
474 put_byte(pb
, 0); /* profile */
478 /* TODO: No idea about these values */
479 static int mov_write_svq3_tag(ByteIOContext
*pb
)
485 put_be32(pb
, 0xe2c0211d);
486 put_be32(pb
, 0xc0000000);
491 static int mov_write_avcc_tag(ByteIOContext
*pb
, MOVTrack
*track
)
493 int64_t pos
= url_ftell(pb
);
497 ff_isom_write_avcc(pb
, track
->vosData
, track
->vosLen
);
498 return updateSize(pb
, pos
);
501 /* also used by all avid codecs (dv, imx, meridien) and their variants */
502 static int mov_write_avid_tag(ByteIOContext
*pb
, MOVTrack
*track
)
505 put_be32(pb
, 24); /* size */
509 put_be32(pb
, 1); /* yuv 1 / rgb 2 ? */
510 put_be32(pb
, 0); /* unknown */
512 put_be32(pb
, 24); /* size */
516 put_be32(pb
, 1); /* unknown */
517 put_be32(pb
, 0); /* unknown */
519 put_be32(pb
, 120); /* size */
523 put_be32(pb
, AV_RB32(track
->vosData
+ 0x28)); /* dnxhd cid, some id ? */
524 put_be32(pb
, track
->enc
->width
);
525 /* values below are based on samples created with quicktime and avid codecs */
526 if (track
->vosData
[5] & 2) { // interlaced
527 put_be32(pb
, track
->enc
->height
/2);
528 put_be32(pb
, 2); /* unknown */
529 put_be32(pb
, 0); /* unknown */
530 put_be32(pb
, 4); /* unknown */
532 put_be32(pb
, track
->enc
->height
);
533 put_be32(pb
, 1); /* unknown */
534 put_be32(pb
, 0); /* unknown */
535 if (track
->enc
->height
== 1080)
536 put_be32(pb
, 5); /* unknown */
538 put_be32(pb
, 6); /* unknown */
541 for (i
= 0; i
< 10; i
++)
544 /* extra padding for stsd needed */
549 static const AVCodecTag codec_3gp_tags
[] = {
550 { CODEC_ID_H263
, MKTAG('s','2','6','3') },
551 { CODEC_ID_H264
, MKTAG('a','v','c','1') },
552 { CODEC_ID_MPEG4
, MKTAG('m','p','4','v') },
553 { CODEC_ID_AAC
, MKTAG('m','p','4','a') },
554 { CODEC_ID_AMR_NB
, MKTAG('s','a','m','r') },
555 { CODEC_ID_AMR_WB
, MKTAG('s','a','w','b') },
556 { CODEC_ID_MOV_TEXT
, MKTAG('t','x','3','g') },
557 { CODEC_ID_NONE
, 0 },
560 static const AVCodecTag mov_pix_fmt_tags
[] = {
561 { PIX_FMT_YUYV422
, MKTAG('y','u','v','s') },
562 { PIX_FMT_UYVY422
, MKTAG('2','v','u','y') },
563 { PIX_FMT_BGR555
, MKTAG('r','a','w',' ') },
564 { PIX_FMT_RGB24
, MKTAG('r','a','w',' ') },
565 { PIX_FMT_BGR32_1
, MKTAG('r','a','w',' ') },
568 static const AVCodecTag codec_ipod_tags
[] = {
569 { CODEC_ID_H264
, MKTAG('a','v','c','1') },
570 { CODEC_ID_MPEG4
, MKTAG('m','p','4','v') },
571 { CODEC_ID_AAC
, MKTAG('m','p','4','a') },
572 { CODEC_ID_ALAC
, MKTAG('a','l','a','c') },
573 { CODEC_ID_AC3
, MKTAG('a','c','-','3') },
574 { CODEC_ID_MOV_TEXT
, MKTAG('t','x','3','g') },
575 { CODEC_ID_MOV_TEXT
, MKTAG('t','e','x','t') },
576 { CODEC_ID_NONE
, 0 },
579 static int mov_find_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
581 int tag
= track
->enc
->codec_tag
;
582 if (track
->mode
== MODE_MP4
|| track
->mode
== MODE_PSP
) {
583 if (!codec_get_tag(ff_mp4_obj_type
, track
->enc
->codec_id
))
585 if (track
->enc
->codec_id
== CODEC_ID_H264
) tag
= MKTAG('a','v','c','1');
586 else if (track
->enc
->codec_id
== CODEC_ID_AC3
) tag
= MKTAG('a','c','-','3');
587 else if (track
->enc
->codec_id
== CODEC_ID_DIRAC
) tag
= MKTAG('d','r','a','c');
588 else if (track
->enc
->codec_id
== CODEC_ID_MOV_TEXT
) tag
= MKTAG('t','x','3','g');
589 else if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) tag
= MKTAG('m','p','4','v');
590 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) tag
= MKTAG('m','p','4','a');
591 } else if (track
->mode
== MODE_IPOD
) {
592 if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
&&
593 (tag
== MKTAG('t','x','3','g') ||
594 tag
== MKTAG('t','e','x','t')))
595 track
->tag
= tag
; // keep original tag
597 tag
= codec_get_tag(codec_ipod_tags
, track
->enc
->codec_id
);
598 if (!match_ext(s
->filename
, "m4a") && !match_ext(s
->filename
, "m4v"))
599 av_log(s
, AV_LOG_WARNING
, "Warning, extension is not .m4a nor .m4v "
600 "Quicktime/Ipod might not play the file\n");
601 } else if (track
->mode
& MODE_3GP
) {
602 tag
= codec_get_tag(codec_3gp_tags
, track
->enc
->codec_id
);
603 } else if (!tag
|| (track
->enc
->strict_std_compliance
>= FF_COMPLIANCE_NORMAL
&&
604 (tag
== MKTAG('d','v','c','p') ||
605 track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
))) {
606 if (track
->enc
->codec_id
== CODEC_ID_DVVIDEO
) {
607 if (track
->enc
->height
== 480) /* NTSC */
608 if (track
->enc
->pix_fmt
== PIX_FMT_YUV422P
) tag
= MKTAG('d','v','5','n');
609 else tag
= MKTAG('d','v','c',' ');
610 else if (track
->enc
->pix_fmt
== PIX_FMT_YUV422P
) tag
= MKTAG('d','v','5','p');
611 else if (track
->enc
->pix_fmt
== PIX_FMT_YUV420P
) tag
= MKTAG('d','v','c','p');
612 else tag
= MKTAG('d','v','p','p');
613 } else if (track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
) {
614 tag
= codec_get_tag(mov_pix_fmt_tags
, track
->enc
->pix_fmt
);
615 if (!tag
) // restore tag
616 tag
= track
->enc
->codec_tag
;
618 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) {
619 tag
= codec_get_tag(codec_movvideo_tags
, track
->enc
->codec_id
);
620 if (!tag
) { // if no mac fcc found, try with Microsoft tags
621 tag
= codec_get_tag(codec_bmp_tags
, track
->enc
->codec_id
);
623 av_log(s
, AV_LOG_INFO
, "Warning, using MS style video codec tag, "
624 "the file may be unplayable!\n");
626 } else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) {
627 tag
= codec_get_tag(codec_movaudio_tags
, track
->enc
->codec_id
);
628 if (!tag
) { // if no mac fcc found, try with Microsoft tags
629 int ms_tag
= codec_get_tag(codec_wav_tags
, track
->enc
->codec_id
);
631 tag
= MKTAG('m', 's', ((ms_tag
>> 8) & 0xff), (ms_tag
& 0xff));
632 av_log(s
, AV_LOG_INFO
, "Warning, using MS style audio codec tag, "
633 "the file may be unplayable!\n");
636 } else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
637 tag
= codec_get_tag(ff_codec_movsubtitle_tags
, track
->enc
->codec_id
);
645 * Needed to make file play in iPods running newest firmware
646 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
648 static int mov_write_uuid_tag_ipod(ByteIOContext
*pb
)
652 put_be32(pb
, 0x6b6840f2);
653 put_be32(pb
, 0x5f244fc5);
654 put_be32(pb
, 0xba39a51b);
655 put_be32(pb
, 0xcf0323f3);
660 static int mov_write_subtitle_tag(ByteIOContext
*pb
, MOVTrack
*track
)
662 int64_t pos
= url_ftell(pb
);
663 put_be32(pb
, 0); /* size */
664 put_le32(pb
, track
->tag
); // store it byteswapped
665 put_be32(pb
, 0); /* Reserved */
666 put_be16(pb
, 0); /* Reserved */
667 put_be16(pb
, 1); /* Data-reference index */
669 if (track
->enc
->extradata_size
)
670 put_buffer(pb
, track
->enc
->extradata
, track
->enc
->extradata_size
);
672 return updateSize(pb
, pos
);
675 static int mov_write_video_tag(ByteIOContext
*pb
, MOVTrack
*track
)
677 int64_t pos
= url_ftell(pb
);
678 char compressor_name
[32];
680 put_be32(pb
, 0); /* size */
681 put_le32(pb
, track
->tag
); // store it byteswapped
682 put_be32(pb
, 0); /* Reserved */
683 put_be16(pb
, 0); /* Reserved */
684 put_be16(pb
, 1); /* Data-reference index */
686 put_be16(pb
, 0); /* Codec stream version */
687 put_be16(pb
, 0); /* Codec stream revision (=0) */
688 if (track
->mode
== MODE_MOV
) {
689 put_tag(pb
, "FFMP"); /* Vendor */
690 if(track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
) {
691 put_be32(pb
, 0); /* Temporal Quality */
692 put_be32(pb
, 0x400); /* Spatial Quality = lossless*/
694 put_be32(pb
, 0x200); /* Temporal Quality = normal */
695 put_be32(pb
, 0x200); /* Spatial Quality = normal */
698 put_be32(pb
, 0); /* Reserved */
699 put_be32(pb
, 0); /* Reserved */
700 put_be32(pb
, 0); /* Reserved */
702 put_be16(pb
, track
->enc
->width
); /* Video width */
703 put_be16(pb
, track
->height
); /* Video height */
704 put_be32(pb
, 0x00480000); /* Horizontal resolution 72dpi */
705 put_be32(pb
, 0x00480000); /* Vertical resolution 72dpi */
706 put_be32(pb
, 0); /* Data size (= 0) */
707 put_be16(pb
, 1); /* Frame count (= 1) */
709 memset(compressor_name
,0,32);
710 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
711 if (track
->mode
== MODE_MOV
&& track
->enc
->codec
&& track
->enc
->codec
->name
)
712 strncpy(compressor_name
,track
->enc
->codec
->name
,31);
713 put_byte(pb
, strlen(compressor_name
));
714 put_buffer(pb
, compressor_name
, 31);
716 if (track
->mode
== MODE_MOV
&& track
->enc
->bits_per_coded_sample
)
717 put_be16(pb
, track
->enc
->bits_per_coded_sample
);
719 put_be16(pb
, 0x18); /* Reserved */
720 put_be16(pb
, 0xffff); /* Reserved */
721 if(track
->tag
== MKTAG('m','p','4','v'))
722 mov_write_esds_tag(pb
, track
);
723 else if(track
->enc
->codec_id
== CODEC_ID_H263
)
724 mov_write_d263_tag(pb
);
725 else if(track
->enc
->codec_id
== CODEC_ID_SVQ3
)
726 mov_write_svq3_tag(pb
);
727 else if(track
->enc
->codec_id
== CODEC_ID_DNXHD
)
728 mov_write_avid_tag(pb
, track
);
729 else if(track
->enc
->codec_id
== CODEC_ID_H264
) {
730 mov_write_avcc_tag(pb
, track
);
731 if(track
->mode
== MODE_IPOD
)
732 mov_write_uuid_tag_ipod(pb
);
733 } else if(track
->vosLen
> 0)
734 mov_write_glbl_tag(pb
, track
);
736 return updateSize(pb
, pos
);
739 static int mov_write_stsd_tag(ByteIOContext
*pb
, MOVTrack
*track
)
741 int64_t pos
= url_ftell(pb
);
742 put_be32(pb
, 0); /* size */
744 put_be32(pb
, 0); /* version & flags */
745 put_be32(pb
, 1); /* entry count */
746 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
)
747 mov_write_video_tag(pb
, track
);
748 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
749 mov_write_audio_tag(pb
, track
);
750 else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
)
751 mov_write_subtitle_tag(pb
, track
);
752 return updateSize(pb
, pos
);
755 static int mov_write_ctts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
757 MOVStts
*ctts_entries
;
758 uint32_t entries
= 0;
762 ctts_entries
= av_malloc((track
->entry
+ 1) * sizeof(*ctts_entries
)); /* worst case */
763 ctts_entries
[0].count
= 1;
764 ctts_entries
[0].duration
= track
->cluster
[0].cts
;
765 for (i
=1; i
<track
->entry
; i
++) {
766 if (track
->cluster
[i
].cts
== ctts_entries
[entries
].duration
) {
767 ctts_entries
[entries
].count
++; /* compress */
770 ctts_entries
[entries
].duration
= track
->cluster
[i
].cts
;
771 ctts_entries
[entries
].count
= 1;
774 entries
++; /* last one */
775 atom_size
= 16 + (entries
* 8);
776 put_be32(pb
, atom_size
); /* size */
778 put_be32(pb
, 0); /* version & flags */
779 put_be32(pb
, entries
); /* entry count */
780 for (i
=0; i
<entries
; i
++) {
781 put_be32(pb
, ctts_entries
[i
].count
);
782 put_be32(pb
, ctts_entries
[i
].duration
);
784 av_free(ctts_entries
);
788 /* Time to sample atom */
789 static int mov_write_stts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
791 MOVStts
*stts_entries
;
792 uint32_t entries
= -1;
796 if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
&& !track
->audio_vbr
) {
797 stts_entries
= av_malloc(sizeof(*stts_entries
)); /* one entry */
798 stts_entries
[0].count
= track
->sampleCount
;
799 stts_entries
[0].duration
= 1;
802 stts_entries
= av_malloc(track
->entry
* sizeof(*stts_entries
)); /* worst case */
803 for (i
=0; i
<track
->entry
; i
++) {
804 int64_t duration
= i
+ 1 == track
->entry
?
805 track
->trackDuration
- track
->cluster
[i
].dts
+ track
->cluster
[0].dts
: /* readjusting */
806 track
->cluster
[i
+1].dts
- track
->cluster
[i
].dts
;
807 if (i
&& duration
== stts_entries
[entries
].duration
) {
808 stts_entries
[entries
].count
++; /* compress */
811 stts_entries
[entries
].duration
= duration
;
812 stts_entries
[entries
].count
= 1;
815 entries
++; /* last one */
817 atom_size
= 16 + (entries
* 8);
818 put_be32(pb
, atom_size
); /* size */
820 put_be32(pb
, 0); /* version & flags */
821 put_be32(pb
, entries
); /* entry count */
822 for (i
=0; i
<entries
; i
++) {
823 put_be32(pb
, stts_entries
[i
].count
);
824 put_be32(pb
, stts_entries
[i
].duration
);
826 av_free(stts_entries
);
830 static int mov_write_dref_tag(ByteIOContext
*pb
)
832 put_be32(pb
, 28); /* size */
834 put_be32(pb
, 0); /* version & flags */
835 put_be32(pb
, 1); /* entry count */
837 put_be32(pb
, 0xc); /* size */
839 put_be32(pb
, 1); /* version & flags */
844 static int mov_write_stbl_tag(ByteIOContext
*pb
, MOVTrack
*track
)
846 int64_t pos
= url_ftell(pb
);
847 put_be32(pb
, 0); /* size */
849 mov_write_stsd_tag(pb
, track
);
850 mov_write_stts_tag(pb
, track
);
851 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
&&
852 track
->hasKeyframes
&& track
->hasKeyframes
< track
->entry
)
853 mov_write_stss_tag(pb
, track
);
854 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
&&
856 mov_write_ctts_tag(pb
, track
);
857 mov_write_stsc_tag(pb
, track
);
858 mov_write_stsz_tag(pb
, track
);
859 mov_write_stco_tag(pb
, track
);
860 return updateSize(pb
, pos
);
863 static int mov_write_dinf_tag(ByteIOContext
*pb
)
865 int64_t pos
= url_ftell(pb
);
866 put_be32(pb
, 0); /* size */
868 mov_write_dref_tag(pb
);
869 return updateSize(pb
, pos
);
872 static int mov_write_nmhd_tag(ByteIOContext
*pb
)
880 static int mov_write_gmhd_tag(ByteIOContext
*pb
)
882 put_be32(pb
, 0x20); /* size */
884 put_be32(pb
, 0x18); /* gmin size */
885 put_tag(pb
, "gmin"); /* generic media info */
886 put_be32(pb
, 0); /* version & flags */
887 put_be16(pb
, 0x40); /* graphics mode = */
888 put_be16(pb
, 0x8000); /* opColor (r?) */
889 put_be16(pb
, 0x8000); /* opColor (g?) */
890 put_be16(pb
, 0x8000); /* opColor (b?) */
891 put_be16(pb
, 0); /* balance */
892 put_be16(pb
, 0); /* reserved */
896 static int mov_write_smhd_tag(ByteIOContext
*pb
)
898 put_be32(pb
, 16); /* size */
900 put_be32(pb
, 0); /* version & flags */
901 put_be16(pb
, 0); /* reserved (balance, normally = 0) */
902 put_be16(pb
, 0); /* reserved */
906 static int mov_write_vmhd_tag(ByteIOContext
*pb
)
908 put_be32(pb
, 0x14); /* size (always 0x14) */
910 put_be32(pb
, 0x01); /* version & flags */
911 put_be64(pb
, 0); /* reserved (graphics mode = copy) */
915 static int mov_write_hdlr_tag(ByteIOContext
*pb
, MOVTrack
*track
)
917 const char *hdlr
, *descr
= NULL
, *hdlr_type
= NULL
;
918 int64_t pos
= url_ftell(pb
);
920 if (!track
) { /* no media --> data handler */
923 descr
= "DataHandler";
925 hdlr
= (track
->mode
== MODE_MOV
) ? "mhlr" : "\0\0\0\0";
926 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) {
928 descr
= "VideoHandler";
929 } else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) {
931 descr
= "SoundHandler";
932 } else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
933 if (track
->tag
== MKTAG('t','x','3','g')) hdlr_type
= "sbtl";
934 else hdlr_type
= "text";
935 descr
= "SubtitleHandler";
939 put_be32(pb
, 0); /* size */
941 put_be32(pb
, 0); /* Version & flags */
942 put_buffer(pb
, hdlr
, 4); /* handler */
943 put_tag(pb
, hdlr_type
); /* handler type */
944 put_be32(pb
,0); /* reserved */
945 put_be32(pb
,0); /* reserved */
946 put_be32(pb
,0); /* reserved */
947 put_byte(pb
, strlen(descr
)); /* string counter */
948 put_buffer(pb
, descr
, strlen(descr
)); /* handler description */
949 return updateSize(pb
, pos
);
952 static int mov_write_minf_tag(ByteIOContext
*pb
, MOVTrack
*track
)
954 int64_t pos
= url_ftell(pb
);
955 put_be32(pb
, 0); /* size */
957 if(track
->enc
->codec_type
== CODEC_TYPE_VIDEO
)
958 mov_write_vmhd_tag(pb
);
959 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
960 mov_write_smhd_tag(pb
);
961 else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
962 if (track
->tag
== MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb
);
963 else mov_write_nmhd_tag(pb
);
965 if (track
->mode
== MODE_MOV
) /* FIXME: Why do it for MODE_MOV only ? */
966 mov_write_hdlr_tag(pb
, NULL
);
967 mov_write_dinf_tag(pb
);
968 mov_write_stbl_tag(pb
, track
);
969 return updateSize(pb
, pos
);
972 static int mov_write_mdhd_tag(ByteIOContext
*pb
, MOVTrack
*track
)
974 int version
= track
->trackDuration
< INT32_MAX
? 0 : 1;
976 (version
== 1) ? put_be32(pb
, 44) : put_be32(pb
, 32); /* size */
978 put_byte(pb
, version
);
979 put_be24(pb
, 0); /* flags */
981 put_be64(pb
, track
->time
);
982 put_be64(pb
, track
->time
);
984 put_be32(pb
, track
->time
); /* creation time */
985 put_be32(pb
, track
->time
); /* modification time */
987 put_be32(pb
, track
->timescale
); /* time scale (sample rate for audio) */
988 (version
== 1) ? put_be64(pb
, track
->trackDuration
) : put_be32(pb
, track
->trackDuration
); /* duration */
989 put_be16(pb
, track
->language
); /* language */
990 put_be16(pb
, 0); /* reserved (quality) */
992 if(version
!=0 && track
->mode
== MODE_MOV
){
993 av_log(NULL
, AV_LOG_ERROR
,
994 "FATAL error, file duration too long for timebase, this file will not be\n"
995 "playable with quicktime. Choose a different timebase or a different\n"
996 "container format\n");
1002 static int mov_write_mdia_tag(ByteIOContext
*pb
, MOVTrack
*track
)
1004 int64_t pos
= url_ftell(pb
);
1005 put_be32(pb
, 0); /* size */
1006 put_tag(pb
, "mdia");
1007 mov_write_mdhd_tag(pb
, track
);
1008 mov_write_hdlr_tag(pb
, track
);
1009 mov_write_minf_tag(pb
, track
);
1010 return updateSize(pb
, pos
);
1013 static int mov_write_tkhd_tag(ByteIOContext
*pb
, MOVTrack
*track
, AVStream
*st
)
1015 int64_t duration
= av_rescale_rnd(track
->trackDuration
, globalTimescale
, track
->timescale
, AV_ROUND_UP
);
1016 int version
= duration
< INT32_MAX
? 0 : 1;
1018 (version
== 1) ? put_be32(pb
, 104) : put_be32(pb
, 92); /* size */
1019 put_tag(pb
, "tkhd");
1020 put_byte(pb
, version
);
1021 put_be24(pb
, 0xf); /* flags (track enabled) */
1023 put_be64(pb
, track
->time
);
1024 put_be64(pb
, track
->time
);
1026 put_be32(pb
, track
->time
); /* creation time */
1027 put_be32(pb
, track
->time
); /* modification time */
1029 put_be32(pb
, track
->trackID
); /* track-id */
1030 put_be32(pb
, 0); /* reserved */
1031 (version
== 1) ? put_be64(pb
, duration
) : put_be32(pb
, duration
);
1033 put_be32(pb
, 0); /* reserved */
1034 put_be32(pb
, 0); /* reserved */
1035 put_be32(pb
, 0x0); /* reserved (Layer & Alternate group) */
1036 /* Volume, only for audio */
1037 if(track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
1038 put_be16(pb
, 0x0100);
1041 put_be16(pb
, 0); /* reserved */
1043 /* Matrix structure */
1044 put_be32(pb
, 0x00010000); /* reserved */
1045 put_be32(pb
, 0x0); /* reserved */
1046 put_be32(pb
, 0x0); /* reserved */
1047 put_be32(pb
, 0x0); /* reserved */
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
, 0x40000000); /* reserved */
1054 /* Track width and height, for visual only */
1055 if(track
->enc
->codec_type
== CODEC_TYPE_VIDEO
||
1056 track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
1057 double sample_aspect_ratio
= av_q2d(st
->sample_aspect_ratio
);
1058 if(!sample_aspect_ratio
|| track
->height
!= track
->enc
->height
)
1059 sample_aspect_ratio
= 1;
1060 put_be32(pb
, sample_aspect_ratio
* track
->enc
->width
*0x10000);
1061 put_be32(pb
, track
->height
*0x10000);
1070 // This box seems important for the psp playback ... without it the movie seems to hang
1071 static int mov_write_edts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
1073 put_be32(pb
, 0x24); /* size */
1074 put_tag(pb
, "edts");
1075 put_be32(pb
, 0x1c); /* size */
1076 put_tag(pb
, "elst");
1080 put_be32(pb
, av_rescale_rnd(track
->trackDuration
, globalTimescale
, track
->timescale
, AV_ROUND_UP
)); /* duration ... doesn't seem to effect psp */
1082 put_be32(pb
, track
->cluster
[0].cts
); /* first pts is cts since dts is 0 */
1083 put_be32(pb
, 0x00010000);
1087 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1088 static int mov_write_uuid_tag_psp(ByteIOContext
*pb
, MOVTrack
*mov
)
1090 put_be32(pb
, 0x34); /* size ... reports as 28 in mp4box! */
1091 put_tag(pb
, "uuid");
1092 put_tag(pb
, "USMT");
1093 put_be32(pb
, 0x21d24fce);
1094 put_be32(pb
, 0xbb88695c);
1095 put_be32(pb
, 0xfac9c740);
1096 put_be32(pb
, 0x1c); // another size here!
1097 put_tag(pb
, "MTDT");
1098 put_be32(pb
, 0x00010012);
1100 put_be32(pb
, 0x55c40000);
1106 static int mov_write_trak_tag(ByteIOContext
*pb
, MOVTrack
*track
, AVStream
*st
)
1108 int64_t pos
= url_ftell(pb
);
1109 put_be32(pb
, 0); /* size */
1110 put_tag(pb
, "trak");
1111 mov_write_tkhd_tag(pb
, track
, st
);
1112 if (track
->mode
== MODE_PSP
|| track
->hasBframes
)
1113 mov_write_edts_tag(pb
, track
); // PSP Movies require edts box
1114 mov_write_mdia_tag(pb
, track
);
1115 if (track
->mode
== MODE_PSP
)
1116 mov_write_uuid_tag_psp(pb
,track
); // PSP Movies require this uuid box
1117 return updateSize(pb
, pos
);
1121 /* TODO: Not sorted out, but not necessary either */
1122 static int mov_write_iods_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1124 put_be32(pb
, 0x15); /* size */
1125 put_tag(pb
, "iods");
1126 put_be32(pb
, 0); /* version & flags */
1127 put_be16(pb
, 0x1007);
1129 put_be16(pb
, 0x4fff);
1130 put_be16(pb
, 0xfffe);
1131 put_be16(pb
, 0x01ff);
1136 static int mov_write_mvhd_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1138 int maxTrackID
= 1, i
;
1139 int64_t maxTrackLenTemp
, maxTrackLen
= 0;
1142 for (i
=0; i
<mov
->nb_streams
; i
++) {
1143 if(mov
->tracks
[i
].entry
> 0) {
1144 maxTrackLenTemp
= av_rescale_rnd(mov
->tracks
[i
].trackDuration
, globalTimescale
, mov
->tracks
[i
].timescale
, AV_ROUND_UP
);
1145 if(maxTrackLen
< maxTrackLenTemp
)
1146 maxTrackLen
= maxTrackLenTemp
;
1147 if(maxTrackID
< mov
->tracks
[i
].trackID
)
1148 maxTrackID
= mov
->tracks
[i
].trackID
;
1152 version
= maxTrackLen
< UINT32_MAX
? 0 : 1;
1153 (version
== 1) ? put_be32(pb
, 120) : put_be32(pb
, 108); /* size */
1154 put_tag(pb
, "mvhd");
1155 put_byte(pb
, version
);
1156 put_be24(pb
, 0); /* flags */
1158 put_be64(pb
, mov
->time
);
1159 put_be64(pb
, mov
->time
);
1161 put_be32(pb
, mov
->time
); /* creation time */
1162 put_be32(pb
, mov
->time
); /* modification time */
1164 put_be32(pb
, mov
->timescale
); /* timescale */
1165 (version
== 1) ? put_be64(pb
, maxTrackLen
) : put_be32(pb
, maxTrackLen
); /* duration of longest track */
1167 put_be32(pb
, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1168 put_be16(pb
, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1169 put_be16(pb
, 0); /* reserved */
1170 put_be32(pb
, 0); /* reserved */
1171 put_be32(pb
, 0); /* reserved */
1173 /* Matrix structure */
1174 put_be32(pb
, 0x00010000); /* reserved */
1175 put_be32(pb
, 0x0); /* reserved */
1176 put_be32(pb
, 0x0); /* reserved */
1177 put_be32(pb
, 0x0); /* reserved */
1178 put_be32(pb
, 0x00010000); /* reserved */
1179 put_be32(pb
, 0x0); /* reserved */
1180 put_be32(pb
, 0x0); /* reserved */
1181 put_be32(pb
, 0x0); /* reserved */
1182 put_be32(pb
, 0x40000000); /* reserved */
1184 put_be32(pb
, 0); /* reserved (preview time) */
1185 put_be32(pb
, 0); /* reserved (preview duration) */
1186 put_be32(pb
, 0); /* reserved (poster time) */
1187 put_be32(pb
, 0); /* reserved (selection time) */
1188 put_be32(pb
, 0); /* reserved (selection duration) */
1189 put_be32(pb
, 0); /* reserved (current time) */
1190 put_be32(pb
, maxTrackID
+1); /* Next track id */
1194 static int mov_write_itunes_hdlr_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1197 int64_t pos
= url_ftell(pb
);
1198 put_be32(pb
, 0); /* size */
1199 put_tag(pb
, "hdlr");
1202 put_tag(pb
, "mdir");
1203 put_tag(pb
, "appl");
1207 return updateSize(pb
, pos
);
1210 /* helper function to write a data tag with the specified string as data */
1211 static int mov_write_string_data_tag(ByteIOContext
*pb
, const char *data
, int lang
, int long_style
)
1214 int64_t pos
= url_ftell(pb
);
1215 put_be32(pb
, 0); /* size */
1216 put_tag(pb
, "data");
1219 put_buffer(pb
, data
, strlen(data
));
1220 return updateSize(pb
, pos
);
1222 put_be16(pb
, strlen(data
)); /* string length */
1224 put_buffer(pb
, data
, strlen(data
));
1225 return strlen(data
) + 4;
1229 static int mov_write_string_tag(ByteIOContext
*pb
, const char *name
, const char *value
, int lang
, int long_style
){
1231 if (value
&& value
[0]) {
1232 int64_t pos
= url_ftell(pb
);
1233 put_be32(pb
, 0); /* size */
1235 mov_write_string_data_tag(pb
, value
, lang
, long_style
);
1236 size
= updateSize(pb
, pos
);
1241 static int mov_write_string_metadata(AVFormatContext
*s
, ByteIOContext
*pb
,
1242 const char *name
, const char *tag
,
1245 int l
, lang
= 0, len
, len2
;
1246 AVMetadataTag
*t
, *t2
= NULL
;
1249 if (!(t
= av_metadata_get(s
->metadata
, tag
, NULL
, 0)))
1252 len
= strlen(t
->key
);
1253 snprintf(tag2
, sizeof(tag2
), "%s-", tag
);
1254 while ((t2
= av_metadata_get(s
->metadata
, tag2
, t2
, AV_METADATA_IGNORE_SUFFIX
))) {
1255 len2
= strlen(t2
->key
);
1256 if (len2
== len
+4 && !strcmp(t
->value
, t2
->value
)
1257 && (l
=ff_mov_iso639_to_lang(&t2
->key
[len2
-3], 0)) >= 0) {
1262 return mov_write_string_tag(pb
, name
, t
->value
, lang
, long_style
);
1265 /* iTunes track number */
1266 static int mov_write_trkn_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1269 AVMetadataTag
*t
= av_metadata_get(s
->metadata
, "track", NULL
, 0);
1270 int size
= 0, track
= t
? atoi(t
->value
) : 0;
1272 int64_t pos
= url_ftell(pb
);
1273 put_be32(pb
, 0); /* size */
1274 put_tag(pb
, "trkn");
1276 int64_t pos
= url_ftell(pb
);
1277 put_be32(pb
, 0); /* size */
1278 put_tag(pb
, "data");
1279 put_be32(pb
, 0); // 8 bytes empty
1281 put_be16(pb
, 0); // empty
1282 put_be16(pb
, track
); // track number
1283 put_be16(pb
, 0); // total track number
1284 put_be16(pb
, 0); // empty
1285 updateSize(pb
, pos
);
1287 size
= updateSize(pb
, pos
);
1292 /* iTunes meta data list */
1293 static int mov_write_ilst_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1296 int64_t pos
= url_ftell(pb
);
1297 put_be32(pb
, 0); /* size */
1298 put_tag(pb
, "ilst");
1299 mov_write_string_metadata(s
, pb
, "\251nam", "title" , 1);
1300 mov_write_string_metadata(s
, pb
, "\251ART", "author" , 1);
1301 mov_write_string_metadata(s
, pb
, "\251wrt", "author" , 1);
1302 mov_write_string_metadata(s
, pb
, "\251alb", "album" , 1);
1303 mov_write_string_metadata(s
, pb
, "\251day", "year" , 1);
1304 mov_write_string_tag(pb
, "\251too", LIBAVFORMAT_IDENT
, 0, 1);
1305 mov_write_string_metadata(s
, pb
, "\251cmt", "comment" , 1);
1306 mov_write_string_metadata(s
, pb
, "\251gen", "genre" , 1);
1307 mov_write_string_metadata(s
, pb
, "\251cpy", "copyright", 1);
1308 mov_write_trkn_tag(pb
, mov
, s
);
1309 return updateSize(pb
, pos
);
1312 /* iTunes meta data tag */
1313 static int mov_write_meta_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1317 int64_t pos
= url_ftell(pb
);
1318 put_be32(pb
, 0); /* size */
1319 put_tag(pb
, "meta");
1321 mov_write_itunes_hdlr_tag(pb
, mov
, s
);
1322 mov_write_ilst_tag(pb
, mov
, s
);
1323 size
= updateSize(pb
, pos
);
1327 static int utf8len(const uint8_t *b
)
1332 GET_UTF8(val
, *b
++, return -1;)
1338 static int ascii_to_wc(ByteIOContext
*pb
, const uint8_t *b
)
1342 GET_UTF8(val
, *b
++, return -1;)
1349 static uint16_t language_code(const char *str
)
1351 return (((str
[0]-0x60) & 0x1F) << 10) + (((str
[1]-0x60) & 0x1F) << 5) + ((str
[2]-0x60) & 0x1F);
1354 static int mov_write_3gp_udta_tag(ByteIOContext
*pb
, AVFormatContext
*s
,
1355 const char *tag
, const char *str
)
1357 int64_t pos
= url_ftell(pb
);
1358 AVMetadataTag
*t
= av_metadata_get(s
->metadata
, str
, NULL
, 0);
1359 if (!t
|| !utf8len(t
->value
))
1361 put_be32(pb
, 0); /* size */
1362 put_tag (pb
, tag
); /* type */
1363 put_be32(pb
, 0); /* version + flags */
1364 if (!strcmp(tag
, "yrrc"))
1365 put_be16(pb
, atoi(t
->value
));
1367 put_be16(pb
, language_code("eng")); /* language */
1368 ascii_to_wc(pb
, t
->value
);
1369 if (!strcmp(tag
, "albm") &&
1370 (t
= av_metadata_get(s
->metadata
, "year", NULL
, 0)))
1371 put_byte(pb
, atoi(t
->value
));
1373 return updateSize(pb
, pos
);
1376 static int mov_write_udta_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1379 ByteIOContext
*pb_buf
;
1383 for (i
= 0; i
< s
->nb_streams
; i
++)
1384 if (mov
->tracks
[i
].enc
->flags
& CODEC_FLAG_BITEXACT
) {
1388 ret
= url_open_dyn_buf(&pb_buf
);
1392 if (mov
->mode
& MODE_3GP
) {
1393 mov_write_3gp_udta_tag(pb_buf
, s
, "titl", "title");
1394 mov_write_3gp_udta_tag(pb_buf
, s
, "auth", "author");
1395 mov_write_3gp_udta_tag(pb_buf
, s
, "gnre", "genre");
1396 mov_write_3gp_udta_tag(pb_buf
, s
, "dscp", "comment");
1397 mov_write_3gp_udta_tag(pb_buf
, s
, "albm", "album");
1398 mov_write_3gp_udta_tag(pb_buf
, s
, "cprt", "copyright");
1399 mov_write_3gp_udta_tag(pb_buf
, s
, "yrrc", "year");
1400 } else if (mov
->mode
== MODE_MOV
) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1401 mov_write_string_metadata(s
, pb_buf
, "\251nam", "title" , 0);
1402 mov_write_string_metadata(s
, pb_buf
, "\251aut", "author" , 0);
1403 mov_write_string_metadata(s
, pb_buf
, "\251alb", "album" , 0);
1404 mov_write_string_metadata(s
, pb_buf
, "\251day", "year" , 0);
1405 mov_write_string_tag(pb_buf
, "\251enc", LIBAVFORMAT_IDENT
, 0, 0);
1406 mov_write_string_metadata(s
, pb_buf
, "\251des", "comment" , 0);
1407 mov_write_string_metadata(s
, pb_buf
, "\251gen", "genre" , 0);
1408 mov_write_string_metadata(s
, pb_buf
, "\251cpy", "copyright" , 0);
1410 /* iTunes meta data */
1411 mov_write_meta_tag(pb_buf
, mov
, s
);
1414 if ((size
= url_close_dyn_buf(pb_buf
, &buf
)) > 0) {
1415 put_be32(pb
, size
+8);
1416 put_tag(pb
, "udta");
1417 put_buffer(pb
, buf
, size
);
1424 static void mov_write_psp_udta_tag(ByteIOContext
*pb
,
1425 const char *str
, const char *lang
, int type
)
1427 int len
= utf8len(str
)+1;
1430 put_be16(pb
, len
*2+10); /* size */
1431 put_be32(pb
, type
); /* type */
1432 put_be16(pb
, language_code(lang
)); /* language */
1433 put_be16(pb
, 0x01); /* ? */
1434 ascii_to_wc(pb
, str
);
1437 static int mov_write_uuidusmt_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1439 AVMetadataTag
*title
= av_metadata_get(s
->metadata
, "title", NULL
, 0);
1443 pos
= url_ftell(pb
);
1444 put_be32(pb
, 0); /* size placeholder*/
1445 put_tag(pb
, "uuid");
1446 put_tag(pb
, "USMT");
1447 put_be32(pb
, 0x21d24fce); /* 96 bit UUID */
1448 put_be32(pb
, 0xbb88695c);
1449 put_be32(pb
, 0xfac9c740);
1451 pos2
= url_ftell(pb
);
1452 put_be32(pb
, 0); /* size placeholder*/
1453 put_tag(pb
, "MTDT");
1457 put_be16(pb
, 0x0C); /* size */
1458 put_be32(pb
, 0x0B); /* type */
1459 put_be16(pb
, language_code("und")); /* language */
1460 put_be16(pb
, 0x0); /* ? */
1461 put_be16(pb
, 0x021C); /* data */
1463 mov_write_psp_udta_tag(pb
, LIBAVCODEC_IDENT
, "eng", 0x04);
1464 mov_write_psp_udta_tag(pb
, title
->value
, "eng", 0x01);
1465 // 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);
1466 mov_write_psp_udta_tag(pb
, "2006/04/01 11:11:11", "und", 0x03);
1468 updateSize(pb
, pos2
);
1469 return updateSize(pb
, pos
);
1475 static int mov_write_moov_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1479 int64_t pos
= url_ftell(pb
);
1480 put_be32(pb
, 0); /* size placeholder*/
1481 put_tag(pb
, "moov");
1482 mov
->timescale
= globalTimescale
;
1484 for (i
=0; i
<mov
->nb_streams
; i
++) {
1485 if(mov
->tracks
[i
].entry
<= 0) continue;
1487 mov
->tracks
[i
].time
= mov
->time
;
1488 mov
->tracks
[i
].trackID
= i
+1;
1491 mov_write_mvhd_tag(pb
, mov
);
1492 //mov_write_iods_tag(pb, mov);
1493 for (i
=0; i
<mov
->nb_streams
; i
++) {
1494 if(mov
->tracks
[i
].entry
> 0) {
1495 mov_write_trak_tag(pb
, &(mov
->tracks
[i
]), s
->streams
[i
]);
1499 if (mov
->mode
== MODE_PSP
)
1500 mov_write_uuidusmt_tag(pb
, s
);
1502 mov_write_udta_tag(pb
, mov
, s
);
1504 return updateSize(pb
, pos
);
1507 static int mov_write_mdat_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1509 put_be32(pb
, 8); // placeholder for extended size field (64 bit)
1510 put_tag(pb
, mov
->mode
== MODE_MOV
? "wide" : "free");
1512 mov
->mdat_pos
= url_ftell(pb
);
1513 put_be32(pb
, 0); /* size placeholder*/
1514 put_tag(pb
, "mdat");
1518 /* TODO: This needs to be more general */
1519 static int mov_write_ftyp_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1521 MOVMuxContext
*mov
= s
->priv_data
;
1522 int64_t pos
= url_ftell(pb
);
1523 int has_h264
= 0, has_video
= 0;
1527 for (i
= 0; i
< s
->nb_streams
; i
++) {
1528 AVStream
*st
= s
->streams
[i
];
1529 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
)
1531 if (st
->codec
->codec_id
== CODEC_ID_H264
)
1535 put_be32(pb
, 0); /* size */
1536 put_tag(pb
, "ftyp");
1538 if (mov
->mode
== MODE_3GP
) {
1539 put_tag(pb
, has_h264
? "3gp6" : "3gp4");
1540 minor
= has_h264
? 0x100 : 0x200;
1541 } else if (mov
->mode
& MODE_3G2
) {
1542 put_tag(pb
, has_h264
? "3g2b" : "3g2a");
1543 minor
= has_h264
? 0x20000 : 0x10000;
1544 }else if (mov
->mode
== MODE_PSP
)
1545 put_tag(pb
, "MSNV");
1546 else if (mov
->mode
== MODE_MP4
)
1547 put_tag(pb
, "isom");
1548 else if (mov
->mode
== MODE_IPOD
)
1549 put_tag(pb
, has_video
? "M4V ":"M4A ");
1553 put_be32(pb
, minor
);
1555 if(mov
->mode
== MODE_MOV
)
1558 put_tag(pb
, "isom");
1559 put_tag(pb
, "iso2");
1561 put_tag(pb
, "avc1");
1564 if (mov
->mode
== MODE_3GP
)
1565 put_tag(pb
, has_h264
? "3gp6":"3gp4");
1566 else if (mov
->mode
& MODE_3G2
)
1567 put_tag(pb
, has_h264
? "3g2b":"3g2a");
1568 else if (mov
->mode
== MODE_PSP
)
1569 put_tag(pb
, "MSNV");
1570 else if (mov
->mode
== MODE_MP4
)
1571 put_tag(pb
, "mp41");
1572 return updateSize(pb
, pos
);
1575 static void mov_write_uuidprof_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1577 AVCodecContext
*VideoCodec
= s
->streams
[0]->codec
;
1578 AVCodecContext
*AudioCodec
= s
->streams
[1]->codec
;
1579 int AudioRate
= AudioCodec
->sample_rate
;
1580 int FrameRate
= ((VideoCodec
->time_base
.den
) * (0x10000))/ (VideoCodec
->time_base
.num
);
1581 int audio_kbitrate
= AudioCodec
->bit_rate
/ 1000;
1582 int video_kbitrate
= FFMIN(VideoCodec
->bit_rate
/ 1000, 800 - audio_kbitrate
);
1584 put_be32(pb
, 0x94); /* size */
1585 put_tag(pb
, "uuid");
1586 put_tag(pb
, "PROF");
1588 put_be32(pb
, 0x21d24fce); /* 96 bit UUID */
1589 put_be32(pb
, 0xbb88695c);
1590 put_be32(pb
, 0xfac9c740);
1592 put_be32(pb
, 0x0); /* ? */
1593 put_be32(pb
, 0x3); /* 3 sections ? */
1595 put_be32(pb
, 0x14); /* size */
1596 put_tag(pb
, "FPRF");
1597 put_be32(pb
, 0x0); /* ? */
1598 put_be32(pb
, 0x0); /* ? */
1599 put_be32(pb
, 0x0); /* ? */
1601 put_be32(pb
, 0x2c); /* size */
1602 put_tag(pb
, "APRF"); /* audio */
1604 put_be32(pb
, 0x2); /* TrackID */
1605 put_tag(pb
, "mp4a");
1606 put_be32(pb
, 0x20f);
1608 put_be32(pb
, audio_kbitrate
);
1609 put_be32(pb
, audio_kbitrate
);
1610 put_be32(pb
, AudioRate
);
1611 put_be32(pb
, AudioCodec
->channels
);
1613 put_be32(pb
, 0x34); /* size */
1614 put_tag(pb
, "VPRF"); /* video */
1616 put_be32(pb
, 0x1); /* TrackID */
1617 if (VideoCodec
->codec_id
== CODEC_ID_H264
) {
1618 put_tag(pb
, "avc1");
1619 put_be16(pb
, 0x014D);
1620 put_be16(pb
, 0x0015);
1622 put_tag(pb
, "mp4v");
1623 put_be16(pb
, 0x0000);
1624 put_be16(pb
, 0x0103);
1627 put_be32(pb
, video_kbitrate
);
1628 put_be32(pb
, video_kbitrate
);
1629 put_be32(pb
, FrameRate
);
1630 put_be32(pb
, FrameRate
);
1631 put_be16(pb
, VideoCodec
->width
);
1632 put_be16(pb
, VideoCodec
->height
);
1633 put_be32(pb
, 0x010001); /* ? */
1636 static int mov_write_header(AVFormatContext
*s
)
1638 ByteIOContext
*pb
= s
->pb
;
1639 MOVMuxContext
*mov
= s
->priv_data
;
1642 if (url_is_streamed(s
->pb
)) {
1643 av_log(s
, AV_LOG_ERROR
, "muxer does not support non seekable output\n");
1647 /* Default mode == MP4 */
1648 mov
->mode
= MODE_MP4
;
1650 if (s
->oformat
!= NULL
) {
1651 if (!strcmp("3gp", s
->oformat
->name
)) mov
->mode
= MODE_3GP
;
1652 else if (!strcmp("3g2", s
->oformat
->name
)) mov
->mode
= MODE_3GP
|MODE_3G2
;
1653 else if (!strcmp("mov", s
->oformat
->name
)) mov
->mode
= MODE_MOV
;
1654 else if (!strcmp("psp", s
->oformat
->name
)) mov
->mode
= MODE_PSP
;
1655 else if (!strcmp("ipod",s
->oformat
->name
)) mov
->mode
= MODE_IPOD
;
1657 mov_write_ftyp_tag(pb
,s
);
1658 if (mov
->mode
== MODE_PSP
) {
1659 if (s
->nb_streams
!= 2) {
1660 av_log(s
, AV_LOG_ERROR
, "PSP mode need one video and one audio stream\n");
1663 mov_write_uuidprof_tag(pb
,s
);
1667 for(i
=0; i
<s
->nb_streams
; i
++){
1668 AVStream
*st
= s
->streams
[i
];
1669 MOVTrack
*track
= &mov
->tracks
[i
];
1670 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
,0);
1672 track
->enc
= st
->codec
;
1673 track
->language
= ff_mov_iso639_to_lang(lang
?lang
->value
:"und", mov
->mode
!=MODE_MOV
);
1674 if (track
->language
< 0)
1675 track
->language
= 0;
1676 track
->mode
= mov
->mode
;
1677 track
->tag
= mov_find_codec_tag(s
, track
);
1679 av_log(s
, AV_LOG_ERROR
, "track %d: could not find tag, "
1680 "codec not currently supported in container\n", i
);
1683 if(st
->codec
->codec_type
== CODEC_TYPE_VIDEO
){
1684 if (track
->tag
== MKTAG('m','x','3','p') || track
->tag
== MKTAG('m','x','3','n') ||
1685 track
->tag
== MKTAG('m','x','4','p') || track
->tag
== MKTAG('m','x','4','n') ||
1686 track
->tag
== MKTAG('m','x','5','p') || track
->tag
== MKTAG('m','x','5','n')) {
1687 if (st
->codec
->width
!= 720 || (st
->codec
->height
!= 608 && st
->codec
->height
!= 512)) {
1688 av_log(s
, AV_LOG_ERROR
, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
1691 track
->height
= track
->tag
>>24 == 'n' ? 486 : 576;
1693 track
->timescale
= st
->codec
->time_base
.den
;
1694 av_set_pts_info(st
, 64, 1, st
->codec
->time_base
.den
);
1695 if (track
->mode
== MODE_MOV
&& track
->timescale
> 100000)
1696 av_log(s
, AV_LOG_WARNING
,
1697 "WARNING codec timebase is very high. If duration is too long,\n"
1698 "file may not be playable by quicktime. Specify a shorter timebase\n"
1699 "or choose different container.\n");
1700 }else if(st
->codec
->codec_type
== CODEC_TYPE_AUDIO
){
1701 track
->timescale
= st
->codec
->sample_rate
;
1702 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
1703 if(!st
->codec
->frame_size
&& !av_get_bits_per_sample(st
->codec
->codec_id
)) {
1704 av_log(s
, AV_LOG_ERROR
, "track %d: codec frame size is not set\n", i
);
1706 }else if(st
->codec
->frame_size
> 1){ /* assume compressed audio */
1707 track
->audio_vbr
= 1;
1709 st
->codec
->frame_size
= 1;
1710 track
->sampleSize
= (av_get_bits_per_sample(st
->codec
->codec_id
) >> 3) * st
->codec
->channels
;
1712 if(track
->mode
!= MODE_MOV
&&
1713 track
->enc
->codec_id
== CODEC_ID_MP3
&& track
->enc
->sample_rate
< 16000){
1714 av_log(s
, AV_LOG_ERROR
, "track %d: muxing mp3 at %dhz is not supported\n",
1715 i
, track
->enc
->sample_rate
);
1718 }else if(st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
){
1719 track
->timescale
= st
->codec
->time_base
.den
;
1720 av_set_pts_info(st
, 64, 1, st
->codec
->time_base
.den
);
1723 track
->height
= st
->codec
->height
;
1726 mov_write_mdat_tag(pb
, mov
);
1727 mov
->time
= s
->timestamp
+ 0x7C25B080; //1970 based -> 1904 based
1728 mov
->nb_streams
= s
->nb_streams
;
1730 put_flush_packet(pb
);
1735 static int mov_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
1737 MOVMuxContext
*mov
= s
->priv_data
;
1738 ByteIOContext
*pb
= s
->pb
;
1739 MOVTrack
*trk
= &mov
->tracks
[pkt
->stream_index
];
1740 AVCodecContext
*enc
= trk
->enc
;
1741 unsigned int samplesInChunk
= 0;
1742 int size
= pkt
->size
;
1744 if (url_is_streamed(s
->pb
)) return 0; /* Can't handle that */
1745 if (!size
) return 0; /* Discard 0 sized packets */
1747 if (enc
->codec_id
== CODEC_ID_AMR_NB
) {
1748 /* We must find out how many AMR blocks there are in one packet */
1749 static uint16_t packed_size
[16] =
1750 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1753 while (len
< size
&& samplesInChunk
< 100) {
1754 len
+= packed_size
[(pkt
->data
[len
] >> 3) & 0x0F];
1757 if(samplesInChunk
> 1){
1758 av_log(s
, AV_LOG_ERROR
, "fatal error, input is not a single packet, implement a AVParser for it\n");
1761 } else if (trk
->sampleSize
)
1762 samplesInChunk
= size
/trk
->sampleSize
;
1766 /* copy extradata if it exists */
1767 if (trk
->vosLen
== 0 && enc
->extradata_size
> 0) {
1768 trk
->vosLen
= enc
->extradata_size
;
1769 trk
->vosData
= av_malloc(trk
->vosLen
);
1770 memcpy(trk
->vosData
, enc
->extradata
, trk
->vosLen
);
1773 if (enc
->codec_id
== CODEC_ID_H264
&& trk
->vosLen
> 0 && *(uint8_t *)trk
->vosData
!= 1) {
1774 /* from x264 or from bytestream h264 */
1775 /* nal reformating needed */
1776 size
= ff_avc_parse_nal_units(pb
, pkt
->data
, pkt
->size
);
1778 put_buffer(pb
, pkt
->data
, size
);
1781 if ((enc
->codec_id
== CODEC_ID_DNXHD
||
1782 enc
->codec_id
== CODEC_ID_AC3
) && !trk
->vosLen
) {
1783 /* copy frame to create needed atoms */
1785 trk
->vosData
= av_malloc(size
);
1787 return AVERROR(ENOMEM
);
1788 memcpy(trk
->vosData
, pkt
->data
, size
);
1791 if (!(trk
->entry
% MOV_INDEX_CLUSTER_SIZE
)) {
1792 trk
->cluster
= av_realloc(trk
->cluster
, (trk
->entry
+ MOV_INDEX_CLUSTER_SIZE
) * sizeof(*trk
->cluster
));
1797 trk
->cluster
[trk
->entry
].pos
= url_ftell(pb
) - size
;
1798 trk
->cluster
[trk
->entry
].samplesInChunk
= samplesInChunk
;
1799 trk
->cluster
[trk
->entry
].size
= size
;
1800 trk
->cluster
[trk
->entry
].entries
= samplesInChunk
;
1801 trk
->cluster
[trk
->entry
].dts
= pkt
->dts
;
1802 trk
->trackDuration
= pkt
->dts
- trk
->cluster
[0].dts
+ pkt
->duration
;
1804 if (pkt
->pts
== AV_NOPTS_VALUE
) {
1805 av_log(s
, AV_LOG_WARNING
, "pts has no value\n");
1806 pkt
->pts
= pkt
->dts
;
1808 if (pkt
->dts
!= pkt
->pts
)
1809 trk
->hasBframes
= 1;
1810 trk
->cluster
[trk
->entry
].cts
= pkt
->pts
- pkt
->dts
;
1811 trk
->cluster
[trk
->entry
].key_frame
= !!(pkt
->flags
& PKT_FLAG_KEY
);
1812 if(trk
->cluster
[trk
->entry
].key_frame
)
1813 trk
->hasKeyframes
++;
1815 trk
->sampleCount
+= samplesInChunk
;
1816 mov
->mdat_size
+= size
;
1818 put_flush_packet(pb
);
1822 static int mov_write_trailer(AVFormatContext
*s
)
1824 MOVMuxContext
*mov
= s
->priv_data
;
1825 ByteIOContext
*pb
= s
->pb
;
1829 int64_t moov_pos
= url_ftell(pb
);
1831 /* Write size of mdat tag */
1832 if (mov
->mdat_size
+8 <= UINT32_MAX
) {
1833 url_fseek(pb
, mov
->mdat_pos
, SEEK_SET
);
1834 put_be32(pb
, mov
->mdat_size
+8);
1836 /* overwrite 'wide' placeholder atom */
1837 url_fseek(pb
, mov
->mdat_pos
- 8, SEEK_SET
);
1838 put_be32(pb
, 1); /* special value: real atom size will be 64 bit value after tag field */
1839 put_tag(pb
, "mdat");
1840 put_be64(pb
, mov
->mdat_size
+16);
1842 url_fseek(pb
, moov_pos
, SEEK_SET
);
1844 mov_write_moov_tag(pb
, mov
, s
);
1846 for (i
=0; i
<mov
->nb_streams
; i
++) {
1847 av_freep(&mov
->tracks
[i
].cluster
);
1849 if(mov
->tracks
[i
].vosLen
) av_free(mov
->tracks
[i
].vosData
);
1853 put_flush_packet(pb
);
1858 #if CONFIG_MOV_MUXER
1859 AVOutputFormat mov_muxer
= {
1861 NULL_IF_CONFIG_SMALL("MOV format"),
1864 sizeof(MOVMuxContext
),
1870 .flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
,
1871 .codec_tag
= (const AVCodecTag
* const []){codec_movvideo_tags
, codec_movaudio_tags
, 0},
1874 #if CONFIG_TGP_MUXER
1875 AVOutputFormat tgp_muxer
= {
1877 NULL_IF_CONFIG_SMALL("3GP format"),
1880 sizeof(MOVMuxContext
),
1886 .flags
= AVFMT_GLOBALHEADER
,
1887 .codec_tag
= (const AVCodecTag
* const []){codec_3gp_tags
, 0},
1890 #if CONFIG_MP4_MUXER
1891 AVOutputFormat mp4_muxer
= {
1893 NULL_IF_CONFIG_SMALL("MP4 format"),
1896 sizeof(MOVMuxContext
),
1902 .flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
,
1903 .codec_tag
= (const AVCodecTag
* const []){ff_mp4_obj_type
, 0},
1906 #if CONFIG_PSP_MUXER
1907 AVOutputFormat psp_muxer
= {
1909 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
1912 sizeof(MOVMuxContext
),
1918 .flags
= AVFMT_GLOBALHEADER
,
1919 .codec_tag
= (const AVCodecTag
* const []){ff_mp4_obj_type
, 0},
1922 #if CONFIG_TG2_MUXER
1923 AVOutputFormat tg2_muxer
= {
1925 NULL_IF_CONFIG_SMALL("3GP2 format"),
1928 sizeof(MOVMuxContext
),
1934 .flags
= AVFMT_GLOBALHEADER
,
1935 .codec_tag
= (const AVCodecTag
* const []){codec_3gp_tags
, 0},
1938 #if CONFIG_IPOD_MUXER
1939 AVOutputFormat ipod_muxer
= {
1941 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
1944 sizeof(MOVMuxContext
),
1950 .flags
= AVFMT_GLOBALHEADER
,
1951 .codec_tag
= (const AVCodecTag
* const []){codec_ipod_tags
, 0},