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/get_bits.h"
30 #include "libavcodec/put_bits.h"
35 #define MOV_INDEX_CLUSTER_SIZE 16384
36 #define globalTimescale 1000
41 #define MODE_PSP 0x08 // example working PSP command line:
42 // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
44 #define MODE_IPOD 0x20
46 typedef struct MOVIentry
{
49 unsigned int samplesInChunk
;
53 #define MOV_SYNC_SAMPLE 0x0001
54 #define MOV_PARTIAL_SYNC_SAMPLE 0x0002
58 typedef struct MOVIndex
{
63 int64_t trackDuration
;
67 #define MOV_TRACK_CTTS 0x0001
68 #define MOV_TRACK_STPS 0x0002
72 int tag
; ///< stsd fourcc
79 int height
; ///< active picture (w/o VBI) height for D-10/IMX
82 typedef struct MOVMuxContext
{
92 //FIXME support 64 bit variant with wide placeholders
93 static int64_t updateSize(ByteIOContext
*pb
, int64_t pos
)
95 int64_t curpos
= url_ftell(pb
);
96 url_fseek(pb
, pos
, SEEK_SET
);
97 put_be32(pb
, curpos
- pos
); /* rewrite size */
98 url_fseek(pb
, curpos
, SEEK_SET
);
103 /* Chunk offset atom */
104 static int mov_write_stco_tag(ByteIOContext
*pb
, MOVTrack
*track
)
107 int mode64
= 0; // use 32 bit size variant if possible
108 int64_t pos
= url_ftell(pb
);
109 put_be32(pb
, 0); /* size */
110 if (pos
> UINT32_MAX
) {
115 put_be32(pb
, 0); /* version & flags */
116 put_be32(pb
, track
->entry
); /* entry count */
117 for (i
=0; i
<track
->entry
; i
++) {
119 put_be64(pb
, track
->cluster
[i
].pos
);
121 put_be32(pb
, track
->cluster
[i
].pos
);
123 return updateSize(pb
, pos
);
126 /* Sample size atom */
127 static int mov_write_stsz_tag(ByteIOContext
*pb
, MOVTrack
*track
)
130 int i
, j
, entries
= 0, tst
= -1, oldtst
= -1;
132 int64_t pos
= url_ftell(pb
);
133 put_be32(pb
, 0); /* size */
135 put_be32(pb
, 0); /* version & flags */
137 for (i
=0; i
<track
->entry
; i
++) {
138 tst
= track
->cluster
[i
].size
/track
->cluster
[i
].entries
;
139 if(oldtst
!= -1 && tst
!= oldtst
) {
143 entries
+= track
->cluster
[i
].entries
;
146 int sSize
= track
->cluster
[0].size
/track
->cluster
[0].entries
;
147 put_be32(pb
, sSize
); // sample size
148 put_be32(pb
, entries
); // sample count
151 put_be32(pb
, 0); // sample size
152 put_be32(pb
, entries
); // sample count
153 for (i
=0; i
<track
->entry
; i
++) {
154 for (j
=0; j
<track
->cluster
[i
].entries
; j
++) {
155 put_be32(pb
, track
->cluster
[i
].size
/
156 track
->cluster
[i
].entries
);
160 return updateSize(pb
, pos
);
163 /* Sample to chunk atom */
164 static int mov_write_stsc_tag(ByteIOContext
*pb
, MOVTrack
*track
)
166 int index
= 0, oldval
= -1, i
;
167 int64_t entryPos
, curpos
;
169 int64_t pos
= url_ftell(pb
);
170 put_be32(pb
, 0); /* size */
172 put_be32(pb
, 0); // version & flags
173 entryPos
= url_ftell(pb
);
174 put_be32(pb
, track
->entry
); // entry count
175 for (i
=0; i
<track
->entry
; i
++) {
176 if(oldval
!= track
->cluster
[i
].samplesInChunk
)
178 put_be32(pb
, i
+1); // first chunk
179 put_be32(pb
, track
->cluster
[i
].samplesInChunk
); // samples per chunk
180 put_be32(pb
, 0x1); // sample description index
181 oldval
= track
->cluster
[i
].samplesInChunk
;
185 curpos
= url_ftell(pb
);
186 url_fseek(pb
, entryPos
, SEEK_SET
);
187 put_be32(pb
, index
); // rewrite size
188 url_fseek(pb
, curpos
, SEEK_SET
);
190 return updateSize(pb
, pos
);
193 /* Sync sample atom */
194 static int mov_write_stss_tag(ByteIOContext
*pb
, MOVTrack
*track
, uint32_t flag
)
196 int64_t curpos
, entryPos
;
198 int64_t pos
= url_ftell(pb
);
199 put_be32(pb
, 0); // size
200 put_tag(pb
, flag
== MOV_SYNC_SAMPLE
? "stss" : "stps");
201 put_be32(pb
, 0); // version & flags
202 entryPos
= url_ftell(pb
);
203 put_be32(pb
, track
->entry
); // entry count
204 for (i
=0; i
<track
->entry
; i
++) {
205 if (track
->cluster
[i
].flags
& flag
) {
210 curpos
= url_ftell(pb
);
211 url_fseek(pb
, entryPos
, SEEK_SET
);
212 put_be32(pb
, index
); // rewrite size
213 url_fseek(pb
, curpos
, SEEK_SET
);
214 return updateSize(pb
, pos
);
217 static int mov_write_amr_tag(ByteIOContext
*pb
, MOVTrack
*track
)
219 put_be32(pb
, 0x11); /* size */
220 if (track
->mode
== MODE_MOV
) put_tag(pb
, "samr");
221 else put_tag(pb
, "damr");
223 put_byte(pb
, 0); /* decoder version */
225 put_be16(pb
, 0x81FF); /* Mode set (all modes for AMR_NB) */
226 put_byte(pb
, 0x00); /* Mode change period (no restriction) */
227 put_byte(pb
, 0x01); /* Frames per sample */
231 static int mov_write_ac3_tag(ByteIOContext
*pb
, MOVTrack
*track
)
236 int fscod
, bsid
, bsmod
, acmod
, lfeon
, frmsizecod
;
238 if (track
->vosLen
< 7)
244 init_get_bits(&gbc
, track
->vosData
+4, track
->vosLen
-4);
245 fscod
= get_bits(&gbc
, 2);
246 frmsizecod
= get_bits(&gbc
, 6);
247 bsid
= get_bits(&gbc
, 5);
248 bsmod
= get_bits(&gbc
, 3);
249 acmod
= get_bits(&gbc
, 3);
251 skip_bits(&gbc
, 2); // dsurmod
253 if ((acmod
& 1) && acmod
!= 1)
254 skip_bits(&gbc
, 2); // cmixlev
256 skip_bits(&gbc
, 2); // surmixlev
258 lfeon
= get_bits1(&gbc
);
260 init_put_bits(&pbc
, buf
, sizeof(buf
));
261 put_bits(&pbc
, 2, fscod
);
262 put_bits(&pbc
, 5, bsid
);
263 put_bits(&pbc
, 3, bsmod
);
264 put_bits(&pbc
, 3, acmod
);
265 put_bits(&pbc
, 1, lfeon
);
266 put_bits(&pbc
, 5, frmsizecod
>>1); // bit_rate_code
267 put_bits(&pbc
, 5, 0); // reserved
269 flush_put_bits(&pbc
);
270 put_buffer(pb
, buf
, sizeof(buf
));
276 * This function writes extradata "as is".
277 * Extradata must be formated like a valid atom (with size and tag)
279 static int mov_write_extradata_tag(ByteIOContext
*pb
, MOVTrack
*track
)
281 put_buffer(pb
, track
->enc
->extradata
, track
->enc
->extradata_size
);
282 return track
->enc
->extradata_size
;
285 static int mov_write_enda_tag(ByteIOContext
*pb
)
289 put_be16(pb
, 1); /* little endian */
293 static unsigned int descrLength(unsigned int len
)
296 for(i
=1; len
>>(7*i
); i
++);
300 static void putDescr(ByteIOContext
*pb
, int tag
, unsigned int size
)
302 int i
= descrLength(size
) - size
- 2;
305 put_byte(pb
, (size
>>(7*i
)) | 0x80);
306 put_byte(pb
, size
& 0x7F);
309 static int mov_write_esds_tag(ByteIOContext
*pb
, MOVTrack
*track
) // Basic
311 int64_t pos
= url_ftell(pb
);
312 int decoderSpecificInfoLen
= track
->vosLen
? descrLength(track
->vosLen
):0;
314 put_be32(pb
, 0); // size
316 put_be32(pb
, 0); // Version
319 putDescr(pb
, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen
) +
321 put_be16(pb
, track
->trackID
);
322 put_byte(pb
, 0x00); // flags (= no flags)
324 // DecoderConfig descriptor
325 putDescr(pb
, 0x04, 13 + decoderSpecificInfoLen
);
327 // Object type indication
328 if ((track
->enc
->codec_id
== CODEC_ID_MP2
||
329 track
->enc
->codec_id
== CODEC_ID_MP3
) &&
330 track
->enc
->sample_rate
> 24000)
331 put_byte(pb
, 0x6B); // 11172-3
333 put_byte(pb
, ff_codec_get_tag(ff_mp4_obj_type
, track
->enc
->codec_id
));
335 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
336 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
337 if(track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
338 put_byte(pb
, 0x15); // flags (= Audiostream)
340 put_byte(pb
, 0x11); // flags (= Visualstream)
342 put_byte(pb
, track
->enc
->rc_buffer_size
>>(3+16)); // Buffersize DB (24 bits)
343 put_be16(pb
, (track
->enc
->rc_buffer_size
>>3)&0xFFFF); // Buffersize DB
345 put_be32(pb
, FFMAX(track
->enc
->bit_rate
, track
->enc
->rc_max_rate
)); // maxbitrate (FIXME should be max rate in any 1 sec window)
346 if(track
->enc
->rc_max_rate
!= track
->enc
->rc_min_rate
|| track
->enc
->rc_min_rate
==0)
347 put_be32(pb
, 0); // vbr
349 put_be32(pb
, track
->enc
->rc_max_rate
); // avg bitrate
352 // DecoderSpecific info descriptor
353 putDescr(pb
, 0x05, track
->vosLen
);
354 put_buffer(pb
, track
->vosData
, track
->vosLen
);
358 putDescr(pb
, 0x06, 1);
360 return updateSize(pb
, pos
);
363 static int mov_write_wave_tag(ByteIOContext
*pb
, MOVTrack
*track
)
365 int64_t pos
= url_ftell(pb
);
367 put_be32(pb
, 0); /* size */
370 put_be32(pb
, 12); /* size */
372 put_le32(pb
, track
->tag
);
374 if (track
->enc
->codec_id
== CODEC_ID_AAC
) {
375 /* useless atom needed by mplayer, ipod, not needed by quicktime */
376 put_be32(pb
, 12); /* size */
379 mov_write_esds_tag(pb
, track
);
380 } else if (track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
||
381 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
) {
382 mov_write_enda_tag(pb
);
383 } else if (track
->enc
->codec_id
== CODEC_ID_AMR_NB
) {
384 mov_write_amr_tag(pb
, track
);
385 } else if (track
->enc
->codec_id
== CODEC_ID_AC3
) {
386 mov_write_ac3_tag(pb
, track
);
387 } else if (track
->enc
->codec_id
== CODEC_ID_ALAC
) {
388 mov_write_extradata_tag(pb
, track
);
391 put_be32(pb
, 8); /* size */
392 put_be32(pb
, 0); /* null tag */
394 return updateSize(pb
, pos
);
397 static int mov_write_glbl_tag(ByteIOContext
*pb
, MOVTrack
*track
)
399 put_be32(pb
, track
->vosLen
+8);
401 put_buffer(pb
, track
->vosData
, track
->vosLen
);
402 return 8+track
->vosLen
;
405 static int mov_write_audio_tag(ByteIOContext
*pb
, MOVTrack
*track
)
407 int64_t pos
= url_ftell(pb
);
408 int version
= track
->mode
== MODE_MOV
&&
410 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
||
411 track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
);
413 put_be32(pb
, 0); /* size */
414 put_le32(pb
, track
->tag
); // store it byteswapped
415 put_be32(pb
, 0); /* Reserved */
416 put_be16(pb
, 0); /* Reserved */
417 put_be16(pb
, 1); /* Data-reference index, XXX == 1 */
419 /* SoundDescription */
420 put_be16(pb
, version
); /* Version */
421 put_be16(pb
, 0); /* Revision level */
422 put_be32(pb
, 0); /* Reserved */
424 if (track
->mode
== MODE_MOV
) {
425 put_be16(pb
, track
->enc
->channels
);
426 if (track
->enc
->codec_id
== CODEC_ID_PCM_U8
||
427 track
->enc
->codec_id
== CODEC_ID_PCM_S8
)
428 put_be16(pb
, 8); /* bits per sample */
431 put_be16(pb
, track
->audio_vbr
? -2 : 0); /* compression ID */
432 } else { /* reserved for mp4/3gp */
438 put_be16(pb
, 0); /* packet size (= 0) */
439 put_be16(pb
, track
->timescale
); /* Time scale */
440 put_be16(pb
, 0); /* Reserved */
442 if(version
== 1) { /* SoundDescription V1 extended info */
443 put_be32(pb
, track
->enc
->frame_size
); /* Samples per packet */
444 put_be32(pb
, track
->sampleSize
/ track
->enc
->channels
); /* Bytes per packet */
445 put_be32(pb
, track
->sampleSize
); /* Bytes per frame */
446 put_be32(pb
, 2); /* Bytes per sample */
449 if(track
->mode
== MODE_MOV
&&
450 (track
->enc
->codec_id
== CODEC_ID_AAC
||
451 track
->enc
->codec_id
== CODEC_ID_AC3
||
452 track
->enc
->codec_id
== CODEC_ID_AMR_NB
||
453 track
->enc
->codec_id
== CODEC_ID_PCM_S24LE
||
454 track
->enc
->codec_id
== CODEC_ID_PCM_S32LE
||
455 track
->enc
->codec_id
== CODEC_ID_ALAC
))
456 mov_write_wave_tag(pb
, track
);
457 else if(track
->tag
== MKTAG('m','p','4','a'))
458 mov_write_esds_tag(pb
, track
);
459 else if(track
->enc
->codec_id
== CODEC_ID_AMR_NB
)
460 mov_write_amr_tag(pb
, track
);
461 else if(track
->enc
->codec_id
== CODEC_ID_AC3
)
462 mov_write_ac3_tag(pb
, track
);
463 else if(track
->enc
->codec_id
== CODEC_ID_ALAC
)
464 mov_write_extradata_tag(pb
, track
);
465 else if(track
->vosLen
> 0)
466 mov_write_glbl_tag(pb
, track
);
468 return updateSize(pb
, pos
);
471 static int mov_write_d263_tag(ByteIOContext
*pb
)
473 put_be32(pb
, 0xf); /* size */
476 put_byte(pb
, 0); /* decoder version */
477 /* FIXME use AVCodecContext level/profile, when encoder will set values */
478 put_byte(pb
, 0xa); /* level */
479 put_byte(pb
, 0); /* profile */
483 /* TODO: No idea about these values */
484 static int mov_write_svq3_tag(ByteIOContext
*pb
)
490 put_be32(pb
, 0xe2c0211d);
491 put_be32(pb
, 0xc0000000);
496 static int mov_write_avcc_tag(ByteIOContext
*pb
, MOVTrack
*track
)
498 int64_t pos
= url_ftell(pb
);
502 ff_isom_write_avcc(pb
, track
->vosData
, track
->vosLen
);
503 return updateSize(pb
, pos
);
506 /* also used by all avid codecs (dv, imx, meridien) and their variants */
507 static int mov_write_avid_tag(ByteIOContext
*pb
, MOVTrack
*track
)
510 put_be32(pb
, 24); /* size */
514 put_be32(pb
, 1); /* yuv 1 / rgb 2 ? */
515 put_be32(pb
, 0); /* unknown */
517 put_be32(pb
, 24); /* size */
521 put_be32(pb
, 1); /* unknown */
522 put_be32(pb
, 0); /* unknown */
524 put_be32(pb
, 120); /* size */
528 put_be32(pb
, AV_RB32(track
->vosData
+ 0x28)); /* dnxhd cid, some id ? */
529 put_be32(pb
, track
->enc
->width
);
530 /* values below are based on samples created with quicktime and avid codecs */
531 if (track
->vosData
[5] & 2) { // interlaced
532 put_be32(pb
, track
->enc
->height
/2);
533 put_be32(pb
, 2); /* unknown */
534 put_be32(pb
, 0); /* unknown */
535 put_be32(pb
, 4); /* unknown */
537 put_be32(pb
, track
->enc
->height
);
538 put_be32(pb
, 1); /* unknown */
539 put_be32(pb
, 0); /* unknown */
540 if (track
->enc
->height
== 1080)
541 put_be32(pb
, 5); /* unknown */
543 put_be32(pb
, 6); /* unknown */
546 for (i
= 0; i
< 10; i
++)
549 /* extra padding for stsd needed */
554 static int mp4_get_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
556 int tag
= track
->enc
->codec_tag
;
558 if (!ff_codec_get_tag(ff_mp4_obj_type
, track
->enc
->codec_id
))
561 if (track
->enc
->codec_id
== CODEC_ID_H264
) tag
= MKTAG('a','v','c','1');
562 else if (track
->enc
->codec_id
== CODEC_ID_AC3
) tag
= MKTAG('a','c','-','3');
563 else if (track
->enc
->codec_id
== CODEC_ID_DIRAC
) tag
= MKTAG('d','r','a','c');
564 else if (track
->enc
->codec_id
== CODEC_ID_MOV_TEXT
) tag
= MKTAG('t','x','3','g');
565 else if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) tag
= MKTAG('m','p','4','v');
566 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) tag
= MKTAG('m','p','4','a');
571 static const AVCodecTag codec_ipod_tags
[] = {
572 { CODEC_ID_H264
, MKTAG('a','v','c','1') },
573 { CODEC_ID_MPEG4
, MKTAG('m','p','4','v') },
574 { CODEC_ID_AAC
, MKTAG('m','p','4','a') },
575 { CODEC_ID_ALAC
, MKTAG('a','l','a','c') },
576 { CODEC_ID_AC3
, MKTAG('a','c','-','3') },
577 { CODEC_ID_MOV_TEXT
, MKTAG('t','x','3','g') },
578 { CODEC_ID_MOV_TEXT
, MKTAG('t','e','x','t') },
579 { CODEC_ID_NONE
, 0 },
582 static int ipod_get_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
584 int tag
= track
->enc
->codec_tag
;
586 // keep original tag for subs, ipod supports both formats
587 if (!(track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
&&
588 (tag
== MKTAG('t','x','3','g') ||
589 tag
== MKTAG('t','e','x','t'))))
590 tag
= ff_codec_get_tag(codec_ipod_tags
, track
->enc
->codec_id
);
592 if (!match_ext(s
->filename
, "m4a") && !match_ext(s
->filename
, "m4v"))
593 av_log(s
, AV_LOG_WARNING
, "Warning, extension is not .m4a nor .m4v "
594 "Quicktime/Ipod might not play the file\n");
599 static int mov_get_dv_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
603 if (track
->enc
->height
== 480) /* NTSC */
604 if (track
->enc
->pix_fmt
== PIX_FMT_YUV422P
) tag
= MKTAG('d','v','5','n');
605 else tag
= MKTAG('d','v','c',' ');
606 else if (track
->enc
->pix_fmt
== PIX_FMT_YUV422P
) tag
= MKTAG('d','v','5','p');
607 else if (track
->enc
->pix_fmt
== PIX_FMT_YUV420P
) tag
= MKTAG('d','v','c','p');
608 else tag
= MKTAG('d','v','p','p');
613 static const struct {
614 enum PixelFormat pix_fmt
;
617 } mov_pix_fmt_tags
[] = {
618 { PIX_FMT_YUYV422
, MKTAG('y','u','v','s'), 0 },
619 { PIX_FMT_UYVY422
, MKTAG('2','v','u','y'), 0 },
620 { PIX_FMT_BGR555
, MKTAG('r','a','w',' '), 16 },
621 { PIX_FMT_RGB555LE
,MKTAG('L','5','5','5'), 16 },
622 { PIX_FMT_RGB565LE
,MKTAG('L','5','6','5'), 16 },
623 { PIX_FMT_RGB565BE
,MKTAG('B','5','6','5'), 16 },
624 { PIX_FMT_RGB24
, MKTAG('r','a','w',' '), 24 },
625 { PIX_FMT_BGR24
, MKTAG('2','4','B','G'), 24 },
626 { PIX_FMT_ARGB
, MKTAG('r','a','w',' '), 32 },
627 { PIX_FMT_BGRA
, MKTAG('B','G','R','A'), 32 },
628 { PIX_FMT_RGBA
, MKTAG('R','G','B','A'), 32 },
631 static int mov_get_rawvideo_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
633 int tag
= track
->enc
->codec_tag
;
636 for (i
= 0; i
< FF_ARRAY_ELEMS(mov_pix_fmt_tags
); i
++) {
637 if (track
->enc
->pix_fmt
== mov_pix_fmt_tags
[i
].pix_fmt
) {
638 tag
= mov_pix_fmt_tags
[i
].tag
;
639 track
->enc
->bits_per_coded_sample
= mov_pix_fmt_tags
[i
].bps
;
647 static int mov_get_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
649 int tag
= track
->enc
->codec_tag
;
651 if (!tag
|| (track
->enc
->strict_std_compliance
>= FF_COMPLIANCE_NORMAL
&&
652 (tag
== MKTAG('d','v','c','p') ||
653 track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
||
654 av_get_bits_per_sample(track
->enc
->codec_id
)))) { // pcm audio
655 if (track
->enc
->codec_id
== CODEC_ID_DVVIDEO
)
656 tag
= mov_get_dv_codec_tag(s
, track
);
657 else if (track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
)
658 tag
= mov_get_rawvideo_codec_tag(s
, track
);
659 else if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) {
660 tag
= ff_codec_get_tag(codec_movvideo_tags
, track
->enc
->codec_id
);
661 if (!tag
) { // if no mac fcc found, try with Microsoft tags
662 tag
= ff_codec_get_tag(ff_codec_bmp_tags
, track
->enc
->codec_id
);
664 av_log(s
, AV_LOG_INFO
, "Warning, using MS style video codec tag, "
665 "the file may be unplayable!\n");
667 } else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) {
668 tag
= ff_codec_get_tag(codec_movaudio_tags
, track
->enc
->codec_id
);
669 if (!tag
) { // if no mac fcc found, try with Microsoft tags
670 int ms_tag
= ff_codec_get_tag(ff_codec_wav_tags
, track
->enc
->codec_id
);
672 tag
= MKTAG('m', 's', ((ms_tag
>> 8) & 0xff), (ms_tag
& 0xff));
673 av_log(s
, AV_LOG_INFO
, "Warning, using MS style audio codec tag, "
674 "the file may be unplayable!\n");
677 } else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
)
678 tag
= ff_codec_get_tag(ff_codec_movsubtitle_tags
, track
->enc
->codec_id
);
684 static const AVCodecTag codec_3gp_tags
[] = {
685 { CODEC_ID_H263
, MKTAG('s','2','6','3') },
686 { CODEC_ID_H264
, MKTAG('a','v','c','1') },
687 { CODEC_ID_MPEG4
, MKTAG('m','p','4','v') },
688 { CODEC_ID_AAC
, MKTAG('m','p','4','a') },
689 { CODEC_ID_AMR_NB
, MKTAG('s','a','m','r') },
690 { CODEC_ID_AMR_WB
, MKTAG('s','a','w','b') },
691 { CODEC_ID_MOV_TEXT
, MKTAG('t','x','3','g') },
692 { CODEC_ID_NONE
, 0 },
695 static int mov_find_codec_tag(AVFormatContext
*s
, MOVTrack
*track
)
697 int tag
= track
->enc
->codec_tag
;
699 if (track
->mode
== MODE_MP4
|| track
->mode
== MODE_PSP
)
700 tag
= mp4_get_codec_tag(s
, track
);
701 else if (track
->mode
== MODE_IPOD
)
702 tag
= ipod_get_codec_tag(s
, track
);
703 else if (track
->mode
& MODE_3GP
)
704 tag
= ff_codec_get_tag(codec_3gp_tags
, track
->enc
->codec_id
);
706 tag
= mov_get_codec_tag(s
, track
);
712 * Needed to make file play in iPods running newest firmware
713 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
715 static int mov_write_uuid_tag_ipod(ByteIOContext
*pb
)
719 put_be32(pb
, 0x6b6840f2);
720 put_be32(pb
, 0x5f244fc5);
721 put_be32(pb
, 0xba39a51b);
722 put_be32(pb
, 0xcf0323f3);
727 static int mov_write_subtitle_tag(ByteIOContext
*pb
, MOVTrack
*track
)
729 int64_t pos
= url_ftell(pb
);
730 put_be32(pb
, 0); /* size */
731 put_le32(pb
, track
->tag
); // store it byteswapped
732 put_be32(pb
, 0); /* Reserved */
733 put_be16(pb
, 0); /* Reserved */
734 put_be16(pb
, 1); /* Data-reference index */
736 if (track
->enc
->extradata_size
)
737 put_buffer(pb
, track
->enc
->extradata
, track
->enc
->extradata_size
);
739 return updateSize(pb
, pos
);
742 static int mov_write_video_tag(ByteIOContext
*pb
, MOVTrack
*track
)
744 int64_t pos
= url_ftell(pb
);
745 char compressor_name
[32];
747 put_be32(pb
, 0); /* size */
748 put_le32(pb
, track
->tag
); // store it byteswapped
749 put_be32(pb
, 0); /* Reserved */
750 put_be16(pb
, 0); /* Reserved */
751 put_be16(pb
, 1); /* Data-reference index */
753 put_be16(pb
, 0); /* Codec stream version */
754 put_be16(pb
, 0); /* Codec stream revision (=0) */
755 if (track
->mode
== MODE_MOV
) {
756 put_tag(pb
, "FFMP"); /* Vendor */
757 if(track
->enc
->codec_id
== CODEC_ID_RAWVIDEO
) {
758 put_be32(pb
, 0); /* Temporal Quality */
759 put_be32(pb
, 0x400); /* Spatial Quality = lossless*/
761 put_be32(pb
, 0x200); /* Temporal Quality = normal */
762 put_be32(pb
, 0x200); /* Spatial Quality = normal */
765 put_be32(pb
, 0); /* Reserved */
766 put_be32(pb
, 0); /* Reserved */
767 put_be32(pb
, 0); /* Reserved */
769 put_be16(pb
, track
->enc
->width
); /* Video width */
770 put_be16(pb
, track
->height
); /* Video height */
771 put_be32(pb
, 0x00480000); /* Horizontal resolution 72dpi */
772 put_be32(pb
, 0x00480000); /* Vertical resolution 72dpi */
773 put_be32(pb
, 0); /* Data size (= 0) */
774 put_be16(pb
, 1); /* Frame count (= 1) */
776 memset(compressor_name
,0,32);
777 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
778 if (track
->mode
== MODE_MOV
&& track
->enc
->codec
&& track
->enc
->codec
->name
)
779 strncpy(compressor_name
,track
->enc
->codec
->name
,31);
780 put_byte(pb
, strlen(compressor_name
));
781 put_buffer(pb
, compressor_name
, 31);
783 if (track
->mode
== MODE_MOV
&& track
->enc
->bits_per_coded_sample
)
784 put_be16(pb
, track
->enc
->bits_per_coded_sample
);
786 put_be16(pb
, 0x18); /* Reserved */
787 put_be16(pb
, 0xffff); /* Reserved */
788 if(track
->tag
== MKTAG('m','p','4','v'))
789 mov_write_esds_tag(pb
, track
);
790 else if(track
->enc
->codec_id
== CODEC_ID_H263
)
791 mov_write_d263_tag(pb
);
792 else if(track
->enc
->codec_id
== CODEC_ID_SVQ3
)
793 mov_write_svq3_tag(pb
);
794 else if(track
->enc
->codec_id
== CODEC_ID_DNXHD
)
795 mov_write_avid_tag(pb
, track
);
796 else if(track
->enc
->codec_id
== CODEC_ID_H264
) {
797 mov_write_avcc_tag(pb
, track
);
798 if(track
->mode
== MODE_IPOD
)
799 mov_write_uuid_tag_ipod(pb
);
800 } else if(track
->vosLen
> 0)
801 mov_write_glbl_tag(pb
, track
);
803 return updateSize(pb
, pos
);
806 static int mov_write_stsd_tag(ByteIOContext
*pb
, MOVTrack
*track
)
808 int64_t pos
= url_ftell(pb
);
809 put_be32(pb
, 0); /* size */
811 put_be32(pb
, 0); /* version & flags */
812 put_be32(pb
, 1); /* entry count */
813 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
)
814 mov_write_video_tag(pb
, track
);
815 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
816 mov_write_audio_tag(pb
, track
);
817 else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
)
818 mov_write_subtitle_tag(pb
, track
);
819 return updateSize(pb
, pos
);
822 static int mov_write_ctts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
824 MOVStts
*ctts_entries
;
825 uint32_t entries
= 0;
829 ctts_entries
= av_malloc((track
->entry
+ 1) * sizeof(*ctts_entries
)); /* worst case */
830 ctts_entries
[0].count
= 1;
831 ctts_entries
[0].duration
= track
->cluster
[0].cts
;
832 for (i
=1; i
<track
->entry
; i
++) {
833 if (track
->cluster
[i
].cts
== ctts_entries
[entries
].duration
) {
834 ctts_entries
[entries
].count
++; /* compress */
837 ctts_entries
[entries
].duration
= track
->cluster
[i
].cts
;
838 ctts_entries
[entries
].count
= 1;
841 entries
++; /* last one */
842 atom_size
= 16 + (entries
* 8);
843 put_be32(pb
, atom_size
); /* size */
845 put_be32(pb
, 0); /* version & flags */
846 put_be32(pb
, entries
); /* entry count */
847 for (i
=0; i
<entries
; i
++) {
848 put_be32(pb
, ctts_entries
[i
].count
);
849 put_be32(pb
, ctts_entries
[i
].duration
);
851 av_free(ctts_entries
);
855 /* Time to sample atom */
856 static int mov_write_stts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
858 MOVStts
*stts_entries
;
859 uint32_t entries
= -1;
863 if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
&& !track
->audio_vbr
) {
864 stts_entries
= av_malloc(sizeof(*stts_entries
)); /* one entry */
865 stts_entries
[0].count
= track
->sampleCount
;
866 stts_entries
[0].duration
= 1;
869 stts_entries
= av_malloc(track
->entry
* sizeof(*stts_entries
)); /* worst case */
870 for (i
=0; i
<track
->entry
; i
++) {
871 int64_t duration
= i
+ 1 == track
->entry
?
872 track
->trackDuration
- track
->cluster
[i
].dts
+ track
->cluster
[0].dts
: /* readjusting */
873 track
->cluster
[i
+1].dts
- track
->cluster
[i
].dts
;
874 if (i
&& duration
== stts_entries
[entries
].duration
) {
875 stts_entries
[entries
].count
++; /* compress */
878 stts_entries
[entries
].duration
= duration
;
879 stts_entries
[entries
].count
= 1;
882 entries
++; /* last one */
884 atom_size
= 16 + (entries
* 8);
885 put_be32(pb
, atom_size
); /* size */
887 put_be32(pb
, 0); /* version & flags */
888 put_be32(pb
, entries
); /* entry count */
889 for (i
=0; i
<entries
; i
++) {
890 put_be32(pb
, stts_entries
[i
].count
);
891 put_be32(pb
, stts_entries
[i
].duration
);
893 av_free(stts_entries
);
897 static int mov_write_dref_tag(ByteIOContext
*pb
)
899 put_be32(pb
, 28); /* size */
901 put_be32(pb
, 0); /* version & flags */
902 put_be32(pb
, 1); /* entry count */
904 put_be32(pb
, 0xc); /* size */
906 put_be32(pb
, 1); /* version & flags */
911 static int mov_write_stbl_tag(ByteIOContext
*pb
, MOVTrack
*track
)
913 int64_t pos
= url_ftell(pb
);
914 put_be32(pb
, 0); /* size */
916 mov_write_stsd_tag(pb
, track
);
917 mov_write_stts_tag(pb
, track
);
918 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
&&
919 track
->hasKeyframes
&& track
->hasKeyframes
< track
->entry
)
920 mov_write_stss_tag(pb
, track
, MOV_SYNC_SAMPLE
);
921 if (track
->mode
== MODE_MOV
&& track
->flags
& MOV_TRACK_STPS
)
922 mov_write_stss_tag(pb
, track
, MOV_PARTIAL_SYNC_SAMPLE
);
923 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
&&
924 track
->flags
& MOV_TRACK_CTTS
)
925 mov_write_ctts_tag(pb
, track
);
926 mov_write_stsc_tag(pb
, track
);
927 mov_write_stsz_tag(pb
, track
);
928 mov_write_stco_tag(pb
, track
);
929 return updateSize(pb
, pos
);
932 static int mov_write_dinf_tag(ByteIOContext
*pb
)
934 int64_t pos
= url_ftell(pb
);
935 put_be32(pb
, 0); /* size */
937 mov_write_dref_tag(pb
);
938 return updateSize(pb
, pos
);
941 static int mov_write_nmhd_tag(ByteIOContext
*pb
)
949 static int mov_write_gmhd_tag(ByteIOContext
*pb
)
951 put_be32(pb
, 0x20); /* size */
953 put_be32(pb
, 0x18); /* gmin size */
954 put_tag(pb
, "gmin"); /* generic media info */
955 put_be32(pb
, 0); /* version & flags */
956 put_be16(pb
, 0x40); /* graphics mode = */
957 put_be16(pb
, 0x8000); /* opColor (r?) */
958 put_be16(pb
, 0x8000); /* opColor (g?) */
959 put_be16(pb
, 0x8000); /* opColor (b?) */
960 put_be16(pb
, 0); /* balance */
961 put_be16(pb
, 0); /* reserved */
965 static int mov_write_smhd_tag(ByteIOContext
*pb
)
967 put_be32(pb
, 16); /* size */
969 put_be32(pb
, 0); /* version & flags */
970 put_be16(pb
, 0); /* reserved (balance, normally = 0) */
971 put_be16(pb
, 0); /* reserved */
975 static int mov_write_vmhd_tag(ByteIOContext
*pb
)
977 put_be32(pb
, 0x14); /* size (always 0x14) */
979 put_be32(pb
, 0x01); /* version & flags */
980 put_be64(pb
, 0); /* reserved (graphics mode = copy) */
984 static int mov_write_hdlr_tag(ByteIOContext
*pb
, MOVTrack
*track
)
986 const char *hdlr
, *descr
= NULL
, *hdlr_type
= NULL
;
987 int64_t pos
= url_ftell(pb
);
989 if (!track
) { /* no media --> data handler */
992 descr
= "DataHandler";
994 hdlr
= (track
->mode
== MODE_MOV
) ? "mhlr" : "\0\0\0\0";
995 if (track
->enc
->codec_type
== CODEC_TYPE_VIDEO
) {
997 descr
= "VideoHandler";
998 } else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
) {
1000 descr
= "SoundHandler";
1001 } else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
1002 if (track
->tag
== MKTAG('t','x','3','g')) hdlr_type
= "sbtl";
1003 else hdlr_type
= "text";
1004 descr
= "SubtitleHandler";
1008 put_be32(pb
, 0); /* size */
1009 put_tag(pb
, "hdlr");
1010 put_be32(pb
, 0); /* Version & flags */
1011 put_buffer(pb
, hdlr
, 4); /* handler */
1012 put_tag(pb
, hdlr_type
); /* handler type */
1013 put_be32(pb
,0); /* reserved */
1014 put_be32(pb
,0); /* reserved */
1015 put_be32(pb
,0); /* reserved */
1016 if (!track
|| track
->mode
== MODE_MOV
)
1017 put_byte(pb
, strlen(descr
)); /* pascal string */
1018 put_buffer(pb
, descr
, strlen(descr
)); /* handler description */
1019 if (track
&& track
->mode
!= MODE_MOV
)
1020 put_byte(pb
, 0); /* c string */
1021 return updateSize(pb
, pos
);
1024 static int mov_write_minf_tag(ByteIOContext
*pb
, MOVTrack
*track
)
1026 int64_t pos
= url_ftell(pb
);
1027 put_be32(pb
, 0); /* size */
1028 put_tag(pb
, "minf");
1029 if(track
->enc
->codec_type
== CODEC_TYPE_VIDEO
)
1030 mov_write_vmhd_tag(pb
);
1031 else if (track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
1032 mov_write_smhd_tag(pb
);
1033 else if (track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
1034 if (track
->tag
== MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb
);
1035 else mov_write_nmhd_tag(pb
);
1037 if (track
->mode
== MODE_MOV
) /* FIXME: Why do it for MODE_MOV only ? */
1038 mov_write_hdlr_tag(pb
, NULL
);
1039 mov_write_dinf_tag(pb
);
1040 mov_write_stbl_tag(pb
, track
);
1041 return updateSize(pb
, pos
);
1044 static int mov_write_mdhd_tag(ByteIOContext
*pb
, MOVTrack
*track
)
1046 int version
= track
->trackDuration
< INT32_MAX
? 0 : 1;
1048 (version
== 1) ? put_be32(pb
, 44) : put_be32(pb
, 32); /* size */
1049 put_tag(pb
, "mdhd");
1050 put_byte(pb
, version
);
1051 put_be24(pb
, 0); /* flags */
1053 put_be64(pb
, track
->time
);
1054 put_be64(pb
, track
->time
);
1056 put_be32(pb
, track
->time
); /* creation time */
1057 put_be32(pb
, track
->time
); /* modification time */
1059 put_be32(pb
, track
->timescale
); /* time scale (sample rate for audio) */
1060 (version
== 1) ? put_be64(pb
, track
->trackDuration
) : put_be32(pb
, track
->trackDuration
); /* duration */
1061 put_be16(pb
, track
->language
); /* language */
1062 put_be16(pb
, 0); /* reserved (quality) */
1064 if(version
!=0 && track
->mode
== MODE_MOV
){
1065 av_log(NULL
, AV_LOG_ERROR
,
1066 "FATAL error, file duration too long for timebase, this file will not be\n"
1067 "playable with quicktime. Choose a different timebase or a different\n"
1068 "container format\n");
1074 static int mov_write_mdia_tag(ByteIOContext
*pb
, MOVTrack
*track
)
1076 int64_t pos
= url_ftell(pb
);
1077 put_be32(pb
, 0); /* size */
1078 put_tag(pb
, "mdia");
1079 mov_write_mdhd_tag(pb
, track
);
1080 mov_write_hdlr_tag(pb
, track
);
1081 mov_write_minf_tag(pb
, track
);
1082 return updateSize(pb
, pos
);
1085 static int mov_write_tkhd_tag(ByteIOContext
*pb
, MOVTrack
*track
, AVStream
*st
)
1087 int64_t duration
= av_rescale_rnd(track
->trackDuration
, globalTimescale
, track
->timescale
, AV_ROUND_UP
);
1088 int version
= duration
< INT32_MAX
? 0 : 1;
1090 (version
== 1) ? put_be32(pb
, 104) : put_be32(pb
, 92); /* size */
1091 put_tag(pb
, "tkhd");
1092 put_byte(pb
, version
);
1093 put_be24(pb
, 0xf); /* flags (track enabled) */
1095 put_be64(pb
, track
->time
);
1096 put_be64(pb
, track
->time
);
1098 put_be32(pb
, track
->time
); /* creation time */
1099 put_be32(pb
, track
->time
); /* modification time */
1101 put_be32(pb
, track
->trackID
); /* track-id */
1102 put_be32(pb
, 0); /* reserved */
1103 (version
== 1) ? put_be64(pb
, duration
) : put_be32(pb
, duration
);
1105 put_be32(pb
, 0); /* reserved */
1106 put_be32(pb
, 0); /* reserved */
1107 put_be32(pb
, 0x0); /* reserved (Layer & Alternate group) */
1108 /* Volume, only for audio */
1109 if(track
->enc
->codec_type
== CODEC_TYPE_AUDIO
)
1110 put_be16(pb
, 0x0100);
1113 put_be16(pb
, 0); /* reserved */
1115 /* Matrix structure */
1116 put_be32(pb
, 0x00010000); /* reserved */
1117 put_be32(pb
, 0x0); /* reserved */
1118 put_be32(pb
, 0x0); /* reserved */
1119 put_be32(pb
, 0x0); /* reserved */
1120 put_be32(pb
, 0x00010000); /* reserved */
1121 put_be32(pb
, 0x0); /* reserved */
1122 put_be32(pb
, 0x0); /* reserved */
1123 put_be32(pb
, 0x0); /* reserved */
1124 put_be32(pb
, 0x40000000); /* reserved */
1126 /* Track width and height, for visual only */
1127 if(track
->enc
->codec_type
== CODEC_TYPE_VIDEO
||
1128 track
->enc
->codec_type
== CODEC_TYPE_SUBTITLE
) {
1129 double sample_aspect_ratio
= av_q2d(st
->sample_aspect_ratio
);
1130 if(!sample_aspect_ratio
|| track
->height
!= track
->enc
->height
)
1131 sample_aspect_ratio
= 1;
1132 put_be32(pb
, sample_aspect_ratio
* track
->enc
->width
*0x10000);
1133 put_be32(pb
, track
->height
*0x10000);
1142 // This box seems important for the psp playback ... without it the movie seems to hang
1143 static int mov_write_edts_tag(ByteIOContext
*pb
, MOVTrack
*track
)
1145 put_be32(pb
, 0x24); /* size */
1146 put_tag(pb
, "edts");
1147 put_be32(pb
, 0x1c); /* size */
1148 put_tag(pb
, "elst");
1152 put_be32(pb
, av_rescale_rnd(track
->trackDuration
, globalTimescale
, track
->timescale
, AV_ROUND_UP
)); /* duration ... doesn't seem to effect psp */
1154 put_be32(pb
, track
->cluster
[0].cts
); /* first pts is cts since dts is 0 */
1155 put_be32(pb
, 0x00010000);
1159 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1160 static int mov_write_uuid_tag_psp(ByteIOContext
*pb
, MOVTrack
*mov
)
1162 put_be32(pb
, 0x34); /* size ... reports as 28 in mp4box! */
1163 put_tag(pb
, "uuid");
1164 put_tag(pb
, "USMT");
1165 put_be32(pb
, 0x21d24fce);
1166 put_be32(pb
, 0xbb88695c);
1167 put_be32(pb
, 0xfac9c740);
1168 put_be32(pb
, 0x1c); // another size here!
1169 put_tag(pb
, "MTDT");
1170 put_be32(pb
, 0x00010012);
1172 put_be32(pb
, 0x55c40000);
1178 static int mov_write_trak_tag(ByteIOContext
*pb
, MOVTrack
*track
, AVStream
*st
)
1180 int64_t pos
= url_ftell(pb
);
1181 put_be32(pb
, 0); /* size */
1182 put_tag(pb
, "trak");
1183 mov_write_tkhd_tag(pb
, track
, st
);
1184 if (track
->mode
== MODE_PSP
|| track
->flags
& MOV_TRACK_CTTS
)
1185 mov_write_edts_tag(pb
, track
); // PSP Movies require edts box
1186 mov_write_mdia_tag(pb
, track
);
1187 if (track
->mode
== MODE_PSP
)
1188 mov_write_uuid_tag_psp(pb
,track
); // PSP Movies require this uuid box
1189 return updateSize(pb
, pos
);
1193 /* TODO: Not sorted out, but not necessary either */
1194 static int mov_write_iods_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1196 put_be32(pb
, 0x15); /* size */
1197 put_tag(pb
, "iods");
1198 put_be32(pb
, 0); /* version & flags */
1199 put_be16(pb
, 0x1007);
1201 put_be16(pb
, 0x4fff);
1202 put_be16(pb
, 0xfffe);
1203 put_be16(pb
, 0x01ff);
1208 static int mov_write_mvhd_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1210 int maxTrackID
= 1, i
;
1211 int64_t maxTrackLenTemp
, maxTrackLen
= 0;
1214 for (i
=0; i
<mov
->nb_streams
; i
++) {
1215 if(mov
->tracks
[i
].entry
> 0) {
1216 maxTrackLenTemp
= av_rescale_rnd(mov
->tracks
[i
].trackDuration
, globalTimescale
, mov
->tracks
[i
].timescale
, AV_ROUND_UP
);
1217 if(maxTrackLen
< maxTrackLenTemp
)
1218 maxTrackLen
= maxTrackLenTemp
;
1219 if(maxTrackID
< mov
->tracks
[i
].trackID
)
1220 maxTrackID
= mov
->tracks
[i
].trackID
;
1224 version
= maxTrackLen
< UINT32_MAX
? 0 : 1;
1225 (version
== 1) ? put_be32(pb
, 120) : put_be32(pb
, 108); /* size */
1226 put_tag(pb
, "mvhd");
1227 put_byte(pb
, version
);
1228 put_be24(pb
, 0); /* flags */
1230 put_be64(pb
, mov
->time
);
1231 put_be64(pb
, mov
->time
);
1233 put_be32(pb
, mov
->time
); /* creation time */
1234 put_be32(pb
, mov
->time
); /* modification time */
1236 put_be32(pb
, mov
->timescale
); /* timescale */
1237 (version
== 1) ? put_be64(pb
, maxTrackLen
) : put_be32(pb
, maxTrackLen
); /* duration of longest track */
1239 put_be32(pb
, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1240 put_be16(pb
, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1241 put_be16(pb
, 0); /* reserved */
1242 put_be32(pb
, 0); /* reserved */
1243 put_be32(pb
, 0); /* reserved */
1245 /* Matrix structure */
1246 put_be32(pb
, 0x00010000); /* reserved */
1247 put_be32(pb
, 0x0); /* reserved */
1248 put_be32(pb
, 0x0); /* reserved */
1249 put_be32(pb
, 0x0); /* reserved */
1250 put_be32(pb
, 0x00010000); /* reserved */
1251 put_be32(pb
, 0x0); /* reserved */
1252 put_be32(pb
, 0x0); /* reserved */
1253 put_be32(pb
, 0x0); /* reserved */
1254 put_be32(pb
, 0x40000000); /* reserved */
1256 put_be32(pb
, 0); /* reserved (preview time) */
1257 put_be32(pb
, 0); /* reserved (preview duration) */
1258 put_be32(pb
, 0); /* reserved (poster time) */
1259 put_be32(pb
, 0); /* reserved (selection time) */
1260 put_be32(pb
, 0); /* reserved (selection duration) */
1261 put_be32(pb
, 0); /* reserved (current time) */
1262 put_be32(pb
, maxTrackID
+1); /* Next track id */
1266 static int mov_write_itunes_hdlr_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1269 int64_t pos
= url_ftell(pb
);
1270 put_be32(pb
, 0); /* size */
1271 put_tag(pb
, "hdlr");
1274 put_tag(pb
, "mdir");
1275 put_tag(pb
, "appl");
1279 return updateSize(pb
, pos
);
1282 /* helper function to write a data tag with the specified string as data */
1283 static int mov_write_string_data_tag(ByteIOContext
*pb
, const char *data
, int lang
, int long_style
)
1286 int64_t pos
= url_ftell(pb
);
1287 put_be32(pb
, 0); /* size */
1288 put_tag(pb
, "data");
1291 put_buffer(pb
, data
, strlen(data
));
1292 return updateSize(pb
, pos
);
1294 put_be16(pb
, strlen(data
)); /* string length */
1296 put_buffer(pb
, data
, strlen(data
));
1297 return strlen(data
) + 4;
1301 static int mov_write_string_tag(ByteIOContext
*pb
, const char *name
, const char *value
, int lang
, int long_style
){
1303 if (value
&& value
[0]) {
1304 int64_t pos
= url_ftell(pb
);
1305 put_be32(pb
, 0); /* size */
1307 mov_write_string_data_tag(pb
, value
, lang
, long_style
);
1308 size
= updateSize(pb
, pos
);
1313 static int mov_write_string_metadata(AVFormatContext
*s
, ByteIOContext
*pb
,
1314 const char *name
, const char *tag
,
1317 int l
, lang
= 0, len
, len2
;
1318 AVMetadataTag
*t
, *t2
= NULL
;
1321 if (!(t
= av_metadata_get(s
->metadata
, tag
, NULL
, 0)))
1324 len
= strlen(t
->key
);
1325 snprintf(tag2
, sizeof(tag2
), "%s-", tag
);
1326 while ((t2
= av_metadata_get(s
->metadata
, tag2
, t2
, AV_METADATA_IGNORE_SUFFIX
))) {
1327 len2
= strlen(t2
->key
);
1328 if (len2
== len
+4 && !strcmp(t
->value
, t2
->value
)
1329 && (l
=ff_mov_iso639_to_lang(&t2
->key
[len2
-3], 0)) >= 0) {
1334 return mov_write_string_tag(pb
, name
, t
->value
, lang
, long_style
);
1337 /* iTunes track number */
1338 static int mov_write_trkn_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1341 AVMetadataTag
*t
= av_metadata_get(s
->metadata
, "track", NULL
, 0);
1342 int size
= 0, track
= t
? atoi(t
->value
) : 0;
1344 int64_t pos
= url_ftell(pb
);
1345 put_be32(pb
, 0); /* size */
1346 put_tag(pb
, "trkn");
1348 int64_t pos
= url_ftell(pb
);
1349 put_be32(pb
, 0); /* size */
1350 put_tag(pb
, "data");
1351 put_be32(pb
, 0); // 8 bytes empty
1353 put_be16(pb
, 0); // empty
1354 put_be16(pb
, track
); // track number
1355 put_be16(pb
, 0); // total track number
1356 put_be16(pb
, 0); // empty
1357 updateSize(pb
, pos
);
1359 size
= updateSize(pb
, pos
);
1364 /* iTunes meta data list */
1365 static int mov_write_ilst_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1368 int64_t pos
= url_ftell(pb
);
1369 put_be32(pb
, 0); /* size */
1370 put_tag(pb
, "ilst");
1371 mov_write_string_metadata(s
, pb
, "\251nam", "title" , 1);
1372 mov_write_string_metadata(s
, pb
, "\251ART", "author" , 1);
1373 mov_write_string_metadata(s
, pb
, "\251wrt", "author" , 1);
1374 mov_write_string_metadata(s
, pb
, "\251alb", "album" , 1);
1375 mov_write_string_metadata(s
, pb
, "\251day", "year" , 1);
1376 mov_write_string_tag(pb
, "\251too", LIBAVFORMAT_IDENT
, 0, 1);
1377 mov_write_string_metadata(s
, pb
, "\251cmt", "comment" , 1);
1378 mov_write_string_metadata(s
, pb
, "\251gen", "genre" , 1);
1379 mov_write_string_metadata(s
, pb
, "\251cpy", "copyright", 1);
1380 mov_write_trkn_tag(pb
, mov
, s
);
1381 return updateSize(pb
, pos
);
1384 /* iTunes meta data tag */
1385 static int mov_write_meta_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1389 int64_t pos
= url_ftell(pb
);
1390 put_be32(pb
, 0); /* size */
1391 put_tag(pb
, "meta");
1393 mov_write_itunes_hdlr_tag(pb
, mov
, s
);
1394 mov_write_ilst_tag(pb
, mov
, s
);
1395 size
= updateSize(pb
, pos
);
1399 static int utf8len(const uint8_t *b
)
1404 GET_UTF8(val
, *b
++, return -1;)
1410 static int ascii_to_wc(ByteIOContext
*pb
, const uint8_t *b
)
1414 GET_UTF8(val
, *b
++, return -1;)
1421 static uint16_t language_code(const char *str
)
1423 return (((str
[0]-0x60) & 0x1F) << 10) + (((str
[1]-0x60) & 0x1F) << 5) + ((str
[2]-0x60) & 0x1F);
1426 static int mov_write_3gp_udta_tag(ByteIOContext
*pb
, AVFormatContext
*s
,
1427 const char *tag
, const char *str
)
1429 int64_t pos
= url_ftell(pb
);
1430 AVMetadataTag
*t
= av_metadata_get(s
->metadata
, str
, NULL
, 0);
1431 if (!t
|| !utf8len(t
->value
))
1433 put_be32(pb
, 0); /* size */
1434 put_tag (pb
, tag
); /* type */
1435 put_be32(pb
, 0); /* version + flags */
1436 if (!strcmp(tag
, "yrrc"))
1437 put_be16(pb
, atoi(t
->value
));
1439 put_be16(pb
, language_code("eng")); /* language */
1440 put_buffer(pb
, t
->value
, strlen(t
->value
)+1); /* UTF8 string value */
1441 if (!strcmp(tag
, "albm") &&
1442 (t
= av_metadata_get(s
->metadata
, "year", NULL
, 0)))
1443 put_byte(pb
, atoi(t
->value
));
1445 return updateSize(pb
, pos
);
1448 static int mov_write_udta_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1451 ByteIOContext
*pb_buf
;
1455 for (i
= 0; i
< s
->nb_streams
; i
++)
1456 if (mov
->tracks
[i
].enc
->flags
& CODEC_FLAG_BITEXACT
) {
1460 ret
= url_open_dyn_buf(&pb_buf
);
1464 if (mov
->mode
& MODE_3GP
) {
1465 mov_write_3gp_udta_tag(pb_buf
, s
, "titl", "title");
1466 mov_write_3gp_udta_tag(pb_buf
, s
, "auth", "author");
1467 mov_write_3gp_udta_tag(pb_buf
, s
, "gnre", "genre");
1468 mov_write_3gp_udta_tag(pb_buf
, s
, "dscp", "comment");
1469 mov_write_3gp_udta_tag(pb_buf
, s
, "albm", "album");
1470 mov_write_3gp_udta_tag(pb_buf
, s
, "cprt", "copyright");
1471 mov_write_3gp_udta_tag(pb_buf
, s
, "yrrc", "year");
1472 } else if (mov
->mode
== MODE_MOV
) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1473 mov_write_string_metadata(s
, pb_buf
, "\251nam", "title" , 0);
1474 mov_write_string_metadata(s
, pb_buf
, "\251aut", "author" , 0);
1475 mov_write_string_metadata(s
, pb_buf
, "\251alb", "album" , 0);
1476 mov_write_string_metadata(s
, pb_buf
, "\251day", "year" , 0);
1477 mov_write_string_tag(pb_buf
, "\251enc", LIBAVFORMAT_IDENT
, 0, 0);
1478 mov_write_string_metadata(s
, pb_buf
, "\251des", "comment" , 0);
1479 mov_write_string_metadata(s
, pb_buf
, "\251gen", "genre" , 0);
1480 mov_write_string_metadata(s
, pb_buf
, "\251cpy", "copyright" , 0);
1482 /* iTunes meta data */
1483 mov_write_meta_tag(pb_buf
, mov
, s
);
1486 if ((size
= url_close_dyn_buf(pb_buf
, &buf
)) > 0) {
1487 put_be32(pb
, size
+8);
1488 put_tag(pb
, "udta");
1489 put_buffer(pb
, buf
, size
);
1496 static void mov_write_psp_udta_tag(ByteIOContext
*pb
,
1497 const char *str
, const char *lang
, int type
)
1499 int len
= utf8len(str
)+1;
1502 put_be16(pb
, len
*2+10); /* size */
1503 put_be32(pb
, type
); /* type */
1504 put_be16(pb
, language_code(lang
)); /* language */
1505 put_be16(pb
, 0x01); /* ? */
1506 ascii_to_wc(pb
, str
);
1509 static int mov_write_uuidusmt_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1511 AVMetadataTag
*title
= av_metadata_get(s
->metadata
, "title", NULL
, 0);
1515 pos
= url_ftell(pb
);
1516 put_be32(pb
, 0); /* size placeholder*/
1517 put_tag(pb
, "uuid");
1518 put_tag(pb
, "USMT");
1519 put_be32(pb
, 0x21d24fce); /* 96 bit UUID */
1520 put_be32(pb
, 0xbb88695c);
1521 put_be32(pb
, 0xfac9c740);
1523 pos2
= url_ftell(pb
);
1524 put_be32(pb
, 0); /* size placeholder*/
1525 put_tag(pb
, "MTDT");
1529 put_be16(pb
, 0x0C); /* size */
1530 put_be32(pb
, 0x0B); /* type */
1531 put_be16(pb
, language_code("und")); /* language */
1532 put_be16(pb
, 0x0); /* ? */
1533 put_be16(pb
, 0x021C); /* data */
1535 mov_write_psp_udta_tag(pb
, LIBAVCODEC_IDENT
, "eng", 0x04);
1536 mov_write_psp_udta_tag(pb
, title
->value
, "eng", 0x01);
1537 // 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);
1538 mov_write_psp_udta_tag(pb
, "2006/04/01 11:11:11", "und", 0x03);
1540 updateSize(pb
, pos2
);
1541 return updateSize(pb
, pos
);
1547 static int mov_write_moov_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
,
1551 int64_t pos
= url_ftell(pb
);
1552 put_be32(pb
, 0); /* size placeholder*/
1553 put_tag(pb
, "moov");
1554 mov
->timescale
= globalTimescale
;
1556 for (i
=0; i
<mov
->nb_streams
; i
++) {
1557 if(mov
->tracks
[i
].entry
<= 0) continue;
1559 mov
->tracks
[i
].time
= mov
->time
;
1560 mov
->tracks
[i
].trackID
= i
+1;
1563 mov_write_mvhd_tag(pb
, mov
);
1564 //mov_write_iods_tag(pb, mov);
1565 for (i
=0; i
<mov
->nb_streams
; i
++) {
1566 if(mov
->tracks
[i
].entry
> 0) {
1567 mov_write_trak_tag(pb
, &(mov
->tracks
[i
]), s
->streams
[i
]);
1571 if (mov
->mode
== MODE_PSP
)
1572 mov_write_uuidusmt_tag(pb
, s
);
1574 mov_write_udta_tag(pb
, mov
, s
);
1576 return updateSize(pb
, pos
);
1579 static int mov_write_mdat_tag(ByteIOContext
*pb
, MOVMuxContext
*mov
)
1581 put_be32(pb
, 8); // placeholder for extended size field (64 bit)
1582 put_tag(pb
, mov
->mode
== MODE_MOV
? "wide" : "free");
1584 mov
->mdat_pos
= url_ftell(pb
);
1585 put_be32(pb
, 0); /* size placeholder*/
1586 put_tag(pb
, "mdat");
1590 /* TODO: This needs to be more general */
1591 static int mov_write_ftyp_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1593 MOVMuxContext
*mov
= s
->priv_data
;
1594 int64_t pos
= url_ftell(pb
);
1595 int has_h264
= 0, has_video
= 0;
1599 for (i
= 0; i
< s
->nb_streams
; i
++) {
1600 AVStream
*st
= s
->streams
[i
];
1601 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
)
1603 if (st
->codec
->codec_id
== CODEC_ID_H264
)
1607 put_be32(pb
, 0); /* size */
1608 put_tag(pb
, "ftyp");
1610 if (mov
->mode
== MODE_3GP
) {
1611 put_tag(pb
, has_h264
? "3gp6" : "3gp4");
1612 minor
= has_h264
? 0x100 : 0x200;
1613 } else if (mov
->mode
& MODE_3G2
) {
1614 put_tag(pb
, has_h264
? "3g2b" : "3g2a");
1615 minor
= has_h264
? 0x20000 : 0x10000;
1616 }else if (mov
->mode
== MODE_PSP
)
1617 put_tag(pb
, "MSNV");
1618 else if (mov
->mode
== MODE_MP4
)
1619 put_tag(pb
, "isom");
1620 else if (mov
->mode
== MODE_IPOD
)
1621 put_tag(pb
, has_video
? "M4V ":"M4A ");
1625 put_be32(pb
, minor
);
1627 if(mov
->mode
== MODE_MOV
)
1630 put_tag(pb
, "isom");
1631 put_tag(pb
, "iso2");
1633 put_tag(pb
, "avc1");
1636 if (mov
->mode
== MODE_3GP
)
1637 put_tag(pb
, has_h264
? "3gp6":"3gp4");
1638 else if (mov
->mode
& MODE_3G2
)
1639 put_tag(pb
, has_h264
? "3g2b":"3g2a");
1640 else if (mov
->mode
== MODE_PSP
)
1641 put_tag(pb
, "MSNV");
1642 else if (mov
->mode
== MODE_MP4
)
1643 put_tag(pb
, "mp41");
1644 return updateSize(pb
, pos
);
1647 static void mov_write_uuidprof_tag(ByteIOContext
*pb
, AVFormatContext
*s
)
1649 AVCodecContext
*VideoCodec
= s
->streams
[0]->codec
;
1650 AVCodecContext
*AudioCodec
= s
->streams
[1]->codec
;
1651 int AudioRate
= AudioCodec
->sample_rate
;
1652 int FrameRate
= ((VideoCodec
->time_base
.den
) * (0x10000))/ (VideoCodec
->time_base
.num
);
1653 int audio_kbitrate
= AudioCodec
->bit_rate
/ 1000;
1654 int video_kbitrate
= FFMIN(VideoCodec
->bit_rate
/ 1000, 800 - audio_kbitrate
);
1656 put_be32(pb
, 0x94); /* size */
1657 put_tag(pb
, "uuid");
1658 put_tag(pb
, "PROF");
1660 put_be32(pb
, 0x21d24fce); /* 96 bit UUID */
1661 put_be32(pb
, 0xbb88695c);
1662 put_be32(pb
, 0xfac9c740);
1664 put_be32(pb
, 0x0); /* ? */
1665 put_be32(pb
, 0x3); /* 3 sections ? */
1667 put_be32(pb
, 0x14); /* size */
1668 put_tag(pb
, "FPRF");
1669 put_be32(pb
, 0x0); /* ? */
1670 put_be32(pb
, 0x0); /* ? */
1671 put_be32(pb
, 0x0); /* ? */
1673 put_be32(pb
, 0x2c); /* size */
1674 put_tag(pb
, "APRF"); /* audio */
1676 put_be32(pb
, 0x2); /* TrackID */
1677 put_tag(pb
, "mp4a");
1678 put_be32(pb
, 0x20f);
1680 put_be32(pb
, audio_kbitrate
);
1681 put_be32(pb
, audio_kbitrate
);
1682 put_be32(pb
, AudioRate
);
1683 put_be32(pb
, AudioCodec
->channels
);
1685 put_be32(pb
, 0x34); /* size */
1686 put_tag(pb
, "VPRF"); /* video */
1688 put_be32(pb
, 0x1); /* TrackID */
1689 if (VideoCodec
->codec_id
== CODEC_ID_H264
) {
1690 put_tag(pb
, "avc1");
1691 put_be16(pb
, 0x014D);
1692 put_be16(pb
, 0x0015);
1694 put_tag(pb
, "mp4v");
1695 put_be16(pb
, 0x0000);
1696 put_be16(pb
, 0x0103);
1699 put_be32(pb
, video_kbitrate
);
1700 put_be32(pb
, video_kbitrate
);
1701 put_be32(pb
, FrameRate
);
1702 put_be32(pb
, FrameRate
);
1703 put_be16(pb
, VideoCodec
->width
);
1704 put_be16(pb
, VideoCodec
->height
);
1705 put_be32(pb
, 0x010001); /* ? */
1708 static int mov_write_header(AVFormatContext
*s
)
1710 ByteIOContext
*pb
= s
->pb
;
1711 MOVMuxContext
*mov
= s
->priv_data
;
1714 if (url_is_streamed(s
->pb
)) {
1715 av_log(s
, AV_LOG_ERROR
, "muxer does not support non seekable output\n");
1719 /* Default mode == MP4 */
1720 mov
->mode
= MODE_MP4
;
1722 if (s
->oformat
!= NULL
) {
1723 if (!strcmp("3gp", s
->oformat
->name
)) mov
->mode
= MODE_3GP
;
1724 else if (!strcmp("3g2", s
->oformat
->name
)) mov
->mode
= MODE_3GP
|MODE_3G2
;
1725 else if (!strcmp("mov", s
->oformat
->name
)) mov
->mode
= MODE_MOV
;
1726 else if (!strcmp("psp", s
->oformat
->name
)) mov
->mode
= MODE_PSP
;
1727 else if (!strcmp("ipod",s
->oformat
->name
)) mov
->mode
= MODE_IPOD
;
1729 mov_write_ftyp_tag(pb
,s
);
1730 if (mov
->mode
== MODE_PSP
) {
1731 if (s
->nb_streams
!= 2) {
1732 av_log(s
, AV_LOG_ERROR
, "PSP mode need one video and one audio stream\n");
1735 mov_write_uuidprof_tag(pb
,s
);
1739 mov
->tracks
= av_mallocz(s
->nb_streams
*sizeof(*mov
->tracks
));
1741 return AVERROR(ENOMEM
);
1743 for(i
=0; i
<s
->nb_streams
; i
++){
1744 AVStream
*st
= s
->streams
[i
];
1745 MOVTrack
*track
= &mov
->tracks
[i
];
1746 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
,0);
1748 track
->enc
= st
->codec
;
1749 track
->language
= ff_mov_iso639_to_lang(lang
?lang
->value
:"und", mov
->mode
!=MODE_MOV
);
1750 if (track
->language
< 0)
1751 track
->language
= 0;
1752 track
->mode
= mov
->mode
;
1753 track
->tag
= mov_find_codec_tag(s
, track
);
1755 av_log(s
, AV_LOG_ERROR
, "track %d: could not find tag, "
1756 "codec not currently supported in container\n", i
);
1759 if(st
->codec
->codec_type
== CODEC_TYPE_VIDEO
){
1760 if (track
->tag
== MKTAG('m','x','3','p') || track
->tag
== MKTAG('m','x','3','n') ||
1761 track
->tag
== MKTAG('m','x','4','p') || track
->tag
== MKTAG('m','x','4','n') ||
1762 track
->tag
== MKTAG('m','x','5','p') || track
->tag
== MKTAG('m','x','5','n')) {
1763 if (st
->codec
->width
!= 720 || (st
->codec
->height
!= 608 && st
->codec
->height
!= 512)) {
1764 av_log(s
, AV_LOG_ERROR
, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
1767 track
->height
= track
->tag
>>24 == 'n' ? 486 : 576;
1769 track
->timescale
= st
->codec
->time_base
.den
;
1770 if (track
->mode
== MODE_MOV
&& track
->timescale
> 100000)
1771 av_log(s
, AV_LOG_WARNING
,
1772 "WARNING codec timebase is very high. If duration is too long,\n"
1773 "file may not be playable by quicktime. Specify a shorter timebase\n"
1774 "or choose different container.\n");
1775 }else if(st
->codec
->codec_type
== CODEC_TYPE_AUDIO
){
1776 track
->timescale
= st
->codec
->sample_rate
;
1777 if(!st
->codec
->frame_size
&& !av_get_bits_per_sample(st
->codec
->codec_id
)) {
1778 av_log(s
, AV_LOG_ERROR
, "track %d: codec frame size is not set\n", i
);
1780 }else if(st
->codec
->frame_size
> 1){ /* assume compressed audio */
1781 track
->audio_vbr
= 1;
1783 st
->codec
->frame_size
= 1;
1784 track
->sampleSize
= (av_get_bits_per_sample(st
->codec
->codec_id
) >> 3) * st
->codec
->channels
;
1786 if(track
->mode
!= MODE_MOV
&&
1787 track
->enc
->codec_id
== CODEC_ID_MP3
&& track
->enc
->sample_rate
< 16000){
1788 av_log(s
, AV_LOG_ERROR
, "track %d: muxing mp3 at %dhz is not supported\n",
1789 i
, track
->enc
->sample_rate
);
1792 }else if(st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
){
1793 track
->timescale
= st
->codec
->time_base
.den
;
1796 track
->height
= st
->codec
->height
;
1798 av_set_pts_info(st
, 64, 1, track
->timescale
);
1801 mov_write_mdat_tag(pb
, mov
);
1802 mov
->time
= s
->timestamp
+ 0x7C25B080; //1970 based -> 1904 based
1803 mov
->nb_streams
= s
->nb_streams
;
1805 put_flush_packet(pb
);
1809 av_freep(&mov
->tracks
);
1813 static int mov_parse_mpeg2_frame(AVPacket
*pkt
, uint32_t *flags
)
1816 int i
, closed_gop
= 0;
1818 for (i
= 0; i
< pkt
->size
- 4; i
++) {
1819 c
= (c
<<8) + pkt
->data
[i
];
1820 if (c
== 0x1b8) { // gop
1821 closed_gop
= pkt
->data
[i
+4]>>6 & 0x01;
1822 } else if (c
== 0x100) { // pic
1823 int temp_ref
= (pkt
->data
[i
+1]<<2) | (pkt
->data
[i
+2]>>6);
1824 if (!temp_ref
|| closed_gop
) // I picture is not reordered
1825 *flags
= MOV_SYNC_SAMPLE
;
1827 *flags
= MOV_PARTIAL_SYNC_SAMPLE
;
1834 static int mov_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
1836 MOVMuxContext
*mov
= s
->priv_data
;
1837 ByteIOContext
*pb
= s
->pb
;
1838 MOVTrack
*trk
= &mov
->tracks
[pkt
->stream_index
];
1839 AVCodecContext
*enc
= trk
->enc
;
1840 unsigned int samplesInChunk
= 0;
1841 int size
= pkt
->size
;
1843 if (url_is_streamed(s
->pb
)) return 0; /* Can't handle that */
1844 if (!size
) return 0; /* Discard 0 sized packets */
1846 if (enc
->codec_id
== CODEC_ID_AMR_NB
) {
1847 /* We must find out how many AMR blocks there are in one packet */
1848 static uint16_t packed_size
[16] =
1849 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1852 while (len
< size
&& samplesInChunk
< 100) {
1853 len
+= packed_size
[(pkt
->data
[len
] >> 3) & 0x0F];
1856 if(samplesInChunk
> 1){
1857 av_log(s
, AV_LOG_ERROR
, "fatal error, input is not a single packet, implement a AVParser for it\n");
1860 } else if (trk
->sampleSize
)
1861 samplesInChunk
= size
/trk
->sampleSize
;
1865 /* copy extradata if it exists */
1866 if (trk
->vosLen
== 0 && enc
->extradata_size
> 0) {
1867 trk
->vosLen
= enc
->extradata_size
;
1868 trk
->vosData
= av_malloc(trk
->vosLen
);
1869 memcpy(trk
->vosData
, enc
->extradata
, trk
->vosLen
);
1872 if (enc
->codec_id
== CODEC_ID_H264
&& trk
->vosLen
> 0 && *(uint8_t *)trk
->vosData
!= 1) {
1873 /* from x264 or from bytestream h264 */
1874 /* nal reformating needed */
1875 size
= ff_avc_parse_nal_units(pb
, pkt
->data
, pkt
->size
);
1877 put_buffer(pb
, pkt
->data
, size
);
1880 if ((enc
->codec_id
== CODEC_ID_DNXHD
||
1881 enc
->codec_id
== CODEC_ID_AC3
) && !trk
->vosLen
) {
1882 /* copy frame to create needed atoms */
1884 trk
->vosData
= av_malloc(size
);
1886 return AVERROR(ENOMEM
);
1887 memcpy(trk
->vosData
, pkt
->data
, size
);
1890 if (!(trk
->entry
% MOV_INDEX_CLUSTER_SIZE
)) {
1891 trk
->cluster
= av_realloc(trk
->cluster
, (trk
->entry
+ MOV_INDEX_CLUSTER_SIZE
) * sizeof(*trk
->cluster
));
1896 trk
->cluster
[trk
->entry
].pos
= url_ftell(pb
) - size
;
1897 trk
->cluster
[trk
->entry
].samplesInChunk
= samplesInChunk
;
1898 trk
->cluster
[trk
->entry
].size
= size
;
1899 trk
->cluster
[trk
->entry
].entries
= samplesInChunk
;
1900 trk
->cluster
[trk
->entry
].dts
= pkt
->dts
;
1901 trk
->trackDuration
= pkt
->dts
- trk
->cluster
[0].dts
+ pkt
->duration
;
1903 if (pkt
->pts
== AV_NOPTS_VALUE
) {
1904 av_log(s
, AV_LOG_WARNING
, "pts has no value\n");
1905 pkt
->pts
= pkt
->dts
;
1907 if (pkt
->dts
!= pkt
->pts
)
1908 trk
->flags
|= MOV_TRACK_CTTS
;
1909 trk
->cluster
[trk
->entry
].cts
= pkt
->pts
- pkt
->dts
;
1910 trk
->cluster
[trk
->entry
].flags
= 0;
1911 if (pkt
->flags
& PKT_FLAG_KEY
) {
1912 if (mov
->mode
== MODE_MOV
&& enc
->codec_id
== CODEC_ID_MPEG2VIDEO
) {
1913 mov_parse_mpeg2_frame(pkt
, &trk
->cluster
[trk
->entry
].flags
);
1914 if (trk
->cluster
[trk
->entry
].flags
& MOV_PARTIAL_SYNC_SAMPLE
)
1915 trk
->flags
|= MOV_TRACK_STPS
;
1917 trk
->cluster
[trk
->entry
].flags
= MOV_SYNC_SAMPLE
;
1919 if (trk
->cluster
[trk
->entry
].flags
& MOV_SYNC_SAMPLE
)
1920 trk
->hasKeyframes
++;
1923 trk
->sampleCount
+= samplesInChunk
;
1924 mov
->mdat_size
+= size
;
1926 put_flush_packet(pb
);
1930 static int mov_write_trailer(AVFormatContext
*s
)
1932 MOVMuxContext
*mov
= s
->priv_data
;
1933 ByteIOContext
*pb
= s
->pb
;
1937 int64_t moov_pos
= url_ftell(pb
);
1939 /* Write size of mdat tag */
1940 if (mov
->mdat_size
+8 <= UINT32_MAX
) {
1941 url_fseek(pb
, mov
->mdat_pos
, SEEK_SET
);
1942 put_be32(pb
, mov
->mdat_size
+8);
1944 /* overwrite 'wide' placeholder atom */
1945 url_fseek(pb
, mov
->mdat_pos
- 8, SEEK_SET
);
1946 put_be32(pb
, 1); /* special value: real atom size will be 64 bit value after tag field */
1947 put_tag(pb
, "mdat");
1948 put_be64(pb
, mov
->mdat_size
+16);
1950 url_fseek(pb
, moov_pos
, SEEK_SET
);
1952 mov_write_moov_tag(pb
, mov
, s
);
1954 for (i
=0; i
<mov
->nb_streams
; i
++) {
1955 av_freep(&mov
->tracks
[i
].cluster
);
1957 if(mov
->tracks
[i
].vosLen
) av_free(mov
->tracks
[i
].vosData
);
1961 put_flush_packet(pb
);
1963 av_freep(&mov
->tracks
);
1968 #if CONFIG_MOV_MUXER
1969 AVOutputFormat mov_muxer
= {
1971 NULL_IF_CONFIG_SMALL("MOV format"),
1974 sizeof(MOVMuxContext
),
1980 .flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
,
1981 .codec_tag
= (const AVCodecTag
* const []){codec_movvideo_tags
, codec_movaudio_tags
, 0},
1984 #if CONFIG_TGP_MUXER
1985 AVOutputFormat tgp_muxer
= {
1987 NULL_IF_CONFIG_SMALL("3GP format"),
1990 sizeof(MOVMuxContext
),
1996 .flags
= AVFMT_GLOBALHEADER
,
1997 .codec_tag
= (const AVCodecTag
* const []){codec_3gp_tags
, 0},
2000 #if CONFIG_MP4_MUXER
2001 AVOutputFormat mp4_muxer
= {
2003 NULL_IF_CONFIG_SMALL("MP4 format"),
2006 sizeof(MOVMuxContext
),
2012 .flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
,
2013 .codec_tag
= (const AVCodecTag
* const []){ff_mp4_obj_type
, 0},
2016 #if CONFIG_PSP_MUXER
2017 AVOutputFormat psp_muxer
= {
2019 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2022 sizeof(MOVMuxContext
),
2028 .flags
= AVFMT_GLOBALHEADER
,
2029 .codec_tag
= (const AVCodecTag
* const []){ff_mp4_obj_type
, 0},
2032 #if CONFIG_TG2_MUXER
2033 AVOutputFormat tg2_muxer
= {
2035 NULL_IF_CONFIG_SMALL("3GP2 format"),
2038 sizeof(MOVMuxContext
),
2044 .flags
= AVFMT_GLOBALHEADER
,
2045 .codec_tag
= (const AVCodecTag
* const []){codec_3gp_tags
, 0},
2048 #if CONFIG_IPOD_MUXER
2049 AVOutputFormat ipod_muxer
= {
2051 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2054 sizeof(MOVMuxContext
),
2060 .flags
= AVFMT_GLOBALHEADER
,
2061 .codec_tag
= (const AVCodecTag
* const []){codec_ipod_tags
, 0},