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
26 #include "avio_internal.h"
31 #include "libavcodec/get_bits.h"
32 #include "libavcodec/put_bits.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/dict.h"
42 static const AVOption options[] = {
43 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), FF_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
44 { "rtphint", "Add RTP hint tracks", 0, FF_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
45 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
49 static const AVClass mov_muxer_class = {
50 .class_name = "MOV/3GP/MP4/3G2 muxer",
51 .item_name = av_default_item_name,
53 .version = LIBAVUTIL_VERSION_INT,
56 //FIXME support 64 bit variant with wide placeholders
57 static int64_t updateSize(AVIOContext *pb, int64_t pos)
59 int64_t curpos = avio_tell(pb);
60 avio_seek(pb, pos, SEEK_SET);
61 avio_wb32(pb, curpos - pos); /* rewrite size */
62 avio_seek(pb, curpos, SEEK_SET);
67 /* Chunk offset atom */
68 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
71 int mode64 = 0; // use 32 bit size variant if possible
72 int64_t pos = avio_tell(pb);
73 avio_wb32(pb, 0); /* size */
74 if (pos > UINT32_MAX) {
76 ffio_wfourcc(pb, "co64");
78 ffio_wfourcc(pb, "stco");
79 avio_wb32(pb, 0); /* version & flags */
80 avio_wb32(pb, track->entry); /* entry count */
81 for (i=0; i<track->entry; i++) {
83 avio_wb64(pb, track->cluster[i].pos);
85 avio_wb32(pb, track->cluster[i].pos);
87 return updateSize(pb, pos);
90 /* Sample size atom */
91 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
94 int i, j, entries = 0, tst = -1, oldtst = -1;
96 int64_t pos = avio_tell(pb);
97 avio_wb32(pb, 0); /* size */
98 ffio_wfourcc(pb, "stsz");
99 avio_wb32(pb, 0); /* version & flags */
101 for (i=0; i<track->entry; i++) {
102 tst = track->cluster[i].size/track->cluster[i].entries;
103 if(oldtst != -1 && tst != oldtst) {
107 entries += track->cluster[i].entries;
110 int sSize = track->cluster[0].size/track->cluster[0].entries;
111 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
112 avio_wb32(pb, sSize); // sample size
113 avio_wb32(pb, entries); // sample count
116 avio_wb32(pb, 0); // sample size
117 avio_wb32(pb, entries); // sample count
118 for (i=0; i<track->entry; i++) {
119 for (j=0; j<track->cluster[i].entries; j++) {
120 avio_wb32(pb, track->cluster[i].size /
121 track->cluster[i].entries);
125 return updateSize(pb, pos);
128 /* Sample to chunk atom */
129 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
131 int index = 0, oldval = -1, i;
132 int64_t entryPos, curpos;
134 int64_t pos = avio_tell(pb);
135 avio_wb32(pb, 0); /* size */
136 ffio_wfourcc(pb, "stsc");
137 avio_wb32(pb, 0); // version & flags
138 entryPos = avio_tell(pb);
139 avio_wb32(pb, track->entry); // entry count
140 for (i=0; i<track->entry; i++) {
141 if(oldval != track->cluster[i].samplesInChunk)
143 avio_wb32(pb, i+1); // first chunk
144 avio_wb32(pb, track->cluster[i].samplesInChunk); // samples per chunk
145 avio_wb32(pb, 0x1); // sample description index
146 oldval = track->cluster[i].samplesInChunk;
150 curpos = avio_tell(pb);
151 avio_seek(pb, entryPos, SEEK_SET);
152 avio_wb32(pb, index); // rewrite size
153 avio_seek(pb, curpos, SEEK_SET);
155 return updateSize(pb, pos);
158 /* Sync sample atom */
159 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
161 int64_t curpos, entryPos;
163 int64_t pos = avio_tell(pb);
164 avio_wb32(pb, 0); // size
165 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
166 avio_wb32(pb, 0); // version & flags
167 entryPos = avio_tell(pb);
168 avio_wb32(pb, track->entry); // entry count
169 for (i=0; i<track->entry; i++) {
170 if (track->cluster[i].flags & flag) {
175 curpos = avio_tell(pb);
176 avio_seek(pb, entryPos, SEEK_SET);
177 avio_wb32(pb, index); // rewrite size
178 avio_seek(pb, curpos, SEEK_SET);
179 return updateSize(pb, pos);
182 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
184 avio_wb32(pb, 0x11); /* size */
185 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
186 else ffio_wfourcc(pb, "damr");
187 ffio_wfourcc(pb, "FFMP");
188 avio_w8(pb, 0); /* decoder version */
190 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
191 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
192 avio_w8(pb, 0x01); /* Frames per sample */
196 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
201 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
203 if (track->vosLen < 7)
207 ffio_wfourcc(pb, "dac3");
209 init_get_bits(&gbc, track->vosData+4, track->vosLen-4);
210 fscod = get_bits(&gbc, 2);
211 frmsizecod = get_bits(&gbc, 6);
212 bsid = get_bits(&gbc, 5);
213 bsmod = get_bits(&gbc, 3);
214 acmod = get_bits(&gbc, 3);
216 skip_bits(&gbc, 2); // dsurmod
218 if ((acmod & 1) && acmod != 1)
219 skip_bits(&gbc, 2); // cmixlev
221 skip_bits(&gbc, 2); // surmixlev
223 lfeon = get_bits1(&gbc);
225 init_put_bits(&pbc, buf, sizeof(buf));
226 put_bits(&pbc, 2, fscod);
227 put_bits(&pbc, 5, bsid);
228 put_bits(&pbc, 3, bsmod);
229 put_bits(&pbc, 3, acmod);
230 put_bits(&pbc, 1, lfeon);
231 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
232 put_bits(&pbc, 5, 0); // reserved
234 flush_put_bits(&pbc);
235 avio_write(pb, buf, sizeof(buf));
241 * This function writes extradata "as is".
242 * Extradata must be formated like a valid atom (with size and tag)
244 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
246 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
247 return track->enc->extradata_size;
250 static int mov_write_enda_tag(AVIOContext *pb)
253 ffio_wfourcc(pb, "enda");
254 avio_wb16(pb, 1); /* little endian */
258 static void putDescr(AVIOContext *pb, int tag, unsigned int size)
263 avio_w8(pb, (size>>(7*i)) | 0x80);
264 avio_w8(pb, size & 0x7F);
267 static unsigned compute_avg_bitrate(MOVTrack *track)
271 for (i = 0; i < track->entry; i++)
272 size += track->cluster[i].size;
273 return size * 8 * track->timescale / track->trackDuration;
276 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
278 int64_t pos = avio_tell(pb);
279 int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0;
280 unsigned avg_bitrate;
282 avio_wb32(pb, 0); // size
283 ffio_wfourcc(pb, "esds");
284 avio_wb32(pb, 0); // Version
287 putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1);
288 avio_wb16(pb, track->trackID);
289 avio_w8(pb, 0x00); // flags (= no flags)
291 // DecoderConfig descriptor
292 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
294 // Object type indication
295 if ((track->enc->codec_id == CODEC_ID_MP2 ||
296 track->enc->codec_id == CODEC_ID_MP3) &&
297 track->enc->sample_rate > 24000)
298 avio_w8(pb, 0x6B); // 11172-3
300 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
302 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
303 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
304 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
305 avio_w8(pb, 0x15); // flags (= Audiostream)
307 avio_w8(pb, 0x11); // flags (= Visualstream)
309 avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
310 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
312 avg_bitrate = compute_avg_bitrate(track);
313 // maxbitrate (FIXME should be max rate in any 1 sec window)
314 avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
315 avio_wb32(pb, avg_bitrate);
318 // DecoderSpecific info descriptor
319 putDescr(pb, 0x05, track->vosLen);
320 avio_write(pb, track->vosData, track->vosLen);
324 putDescr(pb, 0x06, 1);
326 return updateSize(pb, pos);
329 static int mov_pcm_le_gt16(enum CodecID codec_id)
331 return codec_id == CODEC_ID_PCM_S24LE ||
332 codec_id == CODEC_ID_PCM_S32LE ||
333 codec_id == CODEC_ID_PCM_F32LE ||
334 codec_id == CODEC_ID_PCM_F64LE;
337 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
339 int64_t pos = avio_tell(pb);
341 avio_wl32(pb, track->tag); // store it byteswapped
342 track->enc->codec_tag = av_bswap16(track->tag >> 16);
343 ff_put_wav_header(pb, track->enc);
344 return updateSize(pb, pos);
347 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
349 int64_t pos = avio_tell(pb);
351 avio_wb32(pb, 0); /* size */
352 ffio_wfourcc(pb, "wave");
354 avio_wb32(pb, 12); /* size */
355 ffio_wfourcc(pb, "frma");
356 avio_wl32(pb, track->tag);
358 if (track->enc->codec_id == CODEC_ID_AAC) {
359 /* useless atom needed by mplayer, ipod, not needed by quicktime */
360 avio_wb32(pb, 12); /* size */
361 ffio_wfourcc(pb, "mp4a");
363 mov_write_esds_tag(pb, track);
364 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
365 mov_write_enda_tag(pb);
366 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
367 mov_write_amr_tag(pb, track);
368 } else if (track->enc->codec_id == CODEC_ID_AC3) {
369 mov_write_ac3_tag(pb, track);
370 } else if (track->enc->codec_id == CODEC_ID_ALAC) {
371 mov_write_extradata_tag(pb, track);
372 } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
373 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
374 mov_write_ms_tag(pb, track);
377 avio_wb32(pb, 8); /* size */
378 avio_wb32(pb, 0); /* null tag */
380 return updateSize(pb, pos);
383 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
385 avio_wb32(pb, track->vosLen+8);
386 ffio_wfourcc(pb, "glbl");
387 avio_write(pb, track->vosData, track->vosLen);
388 return 8+track->vosLen;
392 * Compute flags for 'lpcm' tag.
393 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
395 static int mov_get_lpcm_flags(enum CodecID codec_id)
398 case CODEC_ID_PCM_F32BE:
399 case CODEC_ID_PCM_F64BE:
401 case CODEC_ID_PCM_F32LE:
402 case CODEC_ID_PCM_F64LE:
404 case CODEC_ID_PCM_U8:
406 case CODEC_ID_PCM_S16BE:
407 case CODEC_ID_PCM_S24BE:
408 case CODEC_ID_PCM_S32BE:
410 case CODEC_ID_PCM_S8:
411 case CODEC_ID_PCM_S16LE:
412 case CODEC_ID_PCM_S24LE:
413 case CODEC_ID_PCM_S32LE:
420 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
422 int64_t pos = avio_tell(pb);
424 uint32_t tag = track->tag;
426 if (track->mode == MODE_MOV) {
427 if (track->timescale > UINT16_MAX) {
428 if (mov_get_lpcm_flags(track->enc->codec_id))
429 tag = AV_RL32("lpcm");
431 } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
432 track->enc->codec_id == CODEC_ID_ADPCM_MS ||
433 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
438 avio_wb32(pb, 0); /* size */
439 avio_wl32(pb, tag); // store it byteswapped
440 avio_wb32(pb, 0); /* Reserved */
441 avio_wb16(pb, 0); /* Reserved */
442 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
444 /* SoundDescription */
445 avio_wb16(pb, version); /* Version */
446 avio_wb16(pb, 0); /* Revision level */
447 avio_wb32(pb, 0); /* Reserved */
452 avio_wb16(pb, 0xfffe);
454 avio_wb32(pb, 0x00010000);
456 avio_wb64(pb, av_dbl2int(track->timescale));
457 avio_wb32(pb, track->enc->channels);
458 avio_wb32(pb, 0x7F000000);
459 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
460 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
461 avio_wb32(pb, track->sampleSize);
462 avio_wb32(pb, track->enc->frame_size);
464 if (track->mode == MODE_MOV) {
465 avio_wb16(pb, track->enc->channels);
466 if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
467 track->enc->codec_id == CODEC_ID_PCM_S8)
468 avio_wb16(pb, 8); /* bits per sample */
471 avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
472 } else { /* reserved for mp4/3gp */
478 avio_wb16(pb, 0); /* packet size (= 0) */
479 avio_wb16(pb, track->timescale); /* Time scale */
480 avio_wb16(pb, 0); /* Reserved */
483 if(version == 1) { /* SoundDescription V1 extended info */
484 avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
485 avio_wb32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
486 avio_wb32(pb, track->sampleSize); /* Bytes per frame */
487 avio_wb32(pb, 2); /* Bytes per sample */
490 if(track->mode == MODE_MOV &&
491 (track->enc->codec_id == CODEC_ID_AAC ||
492 track->enc->codec_id == CODEC_ID_AC3 ||
493 track->enc->codec_id == CODEC_ID_AMR_NB ||
494 track->enc->codec_id == CODEC_ID_ALAC ||
495 track->enc->codec_id == CODEC_ID_ADPCM_MS ||
496 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
497 mov_pcm_le_gt16(track->enc->codec_id)))
498 mov_write_wave_tag(pb, track);
499 else if(track->tag == MKTAG('m','p','4','a'))
500 mov_write_esds_tag(pb, track);
501 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
502 mov_write_amr_tag(pb, track);
503 else if(track->enc->codec_id == CODEC_ID_AC3)
504 mov_write_ac3_tag(pb, track);
505 else if(track->enc->codec_id == CODEC_ID_ALAC)
506 mov_write_extradata_tag(pb, track);
507 else if(track->vosLen > 0)
508 mov_write_glbl_tag(pb, track);
510 return updateSize(pb, pos);
513 static int mov_write_d263_tag(AVIOContext *pb)
515 avio_wb32(pb, 0xf); /* size */
516 ffio_wfourcc(pb, "d263");
517 ffio_wfourcc(pb, "FFMP");
518 avio_w8(pb, 0); /* decoder version */
519 /* FIXME use AVCodecContext level/profile, when encoder will set values */
520 avio_w8(pb, 0xa); /* level */
521 avio_w8(pb, 0); /* profile */
525 /* TODO: No idea about these values */
526 static int mov_write_svq3_tag(AVIOContext *pb)
529 ffio_wfourcc(pb, "SMI ");
530 ffio_wfourcc(pb, "SEQH");
532 avio_wb32(pb, 0xe2c0211d);
533 avio_wb32(pb, 0xc0000000);
538 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
540 int64_t pos = avio_tell(pb);
543 ffio_wfourcc(pb, "avcC");
544 ff_isom_write_avcc(pb, track->vosData, track->vosLen);
545 return updateSize(pb, pos);
548 /* also used by all avid codecs (dv, imx, meridien) and their variants */
549 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
552 avio_wb32(pb, 24); /* size */
553 ffio_wfourcc(pb, "ACLR");
554 ffio_wfourcc(pb, "ACLR");
555 ffio_wfourcc(pb, "0001");
556 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
557 avio_wb32(pb, 0); /* unknown */
559 avio_wb32(pb, 24); /* size */
560 ffio_wfourcc(pb, "APRG");
561 ffio_wfourcc(pb, "APRG");
562 ffio_wfourcc(pb, "0001");
563 avio_wb32(pb, 1); /* unknown */
564 avio_wb32(pb, 0); /* unknown */
566 avio_wb32(pb, 120); /* size */
567 ffio_wfourcc(pb, "ARES");
568 ffio_wfourcc(pb, "ARES");
569 ffio_wfourcc(pb, "0001");
570 avio_wb32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
571 avio_wb32(pb, track->enc->width);
572 /* values below are based on samples created with quicktime and avid codecs */
573 if (track->vosData[5] & 2) { // interlaced
574 avio_wb32(pb, track->enc->height/2);
575 avio_wb32(pb, 2); /* unknown */
576 avio_wb32(pb, 0); /* unknown */
577 avio_wb32(pb, 4); /* unknown */
579 avio_wb32(pb, track->enc->height);
580 avio_wb32(pb, 1); /* unknown */
581 avio_wb32(pb, 0); /* unknown */
582 if (track->enc->height == 1080)
583 avio_wb32(pb, 5); /* unknown */
585 avio_wb32(pb, 6); /* unknown */
588 for (i = 0; i < 10; i++)
591 /* extra padding for stsd needed */
596 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
598 int tag = track->enc->codec_tag;
600 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
603 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
604 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
605 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
606 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
607 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
608 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
613 static const AVCodecTag codec_ipod_tags[] = {
614 { CODEC_ID_H264, MKTAG('a','v','c','1') },
615 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
616 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
617 { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
618 { CODEC_ID_AC3, MKTAG('a','c','-','3') },
619 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
620 { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
621 { CODEC_ID_NONE, 0 },
624 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
626 int tag = track->enc->codec_tag;
628 // keep original tag for subs, ipod supports both formats
629 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
630 (tag == MKTAG('t','x','3','g') ||
631 tag == MKTAG('t','e','x','t'))))
632 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
634 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
635 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
636 "Quicktime/Ipod might not play the file\n");
641 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
645 if (track->enc->width == 720) /* SD */
646 if (track->enc->height == 480) /* NTSC */
647 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
648 else tag = MKTAG('d','v','c',' ');
649 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
650 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
651 else tag = MKTAG('d','v','p','p');
652 else if (track->enc->height == 720) /* HD 720 line */
653 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
654 else tag = MKTAG('d','v','h','p');
655 else if (track->enc->height == 1080) /* HD 1080 line */
656 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
657 else tag = MKTAG('d','v','h','6');
659 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
666 static const struct {
667 enum PixelFormat pix_fmt;
670 } mov_pix_fmt_tags[] = {
671 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
672 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
673 { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
674 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
675 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
676 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
677 { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
678 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
679 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
680 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
681 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
682 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
683 { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
684 { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
687 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
689 int tag = track->enc->codec_tag;
692 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
693 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
694 tag = mov_pix_fmt_tags[i].tag;
695 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
703 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
705 int tag = track->enc->codec_tag;
707 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
708 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
709 track->enc->codec_id == CODEC_ID_RAWVIDEO ||
710 track->enc->codec_id == CODEC_ID_H263 ||
711 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
712 if (track->enc->codec_id == CODEC_ID_DVVIDEO)
713 tag = mov_get_dv_codec_tag(s, track);
714 else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
715 tag = mov_get_rawvideo_codec_tag(s, track);
716 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
717 tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
718 if (!tag) { // if no mac fcc found, try with Microsoft tags
719 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
721 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
722 "the file may be unplayable!\n");
724 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
725 tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
726 if (!tag) { // if no mac fcc found, try with Microsoft tags
727 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
729 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
730 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
731 "the file may be unplayable!\n");
734 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
735 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
741 static const AVCodecTag codec_3gp_tags[] = {
742 { CODEC_ID_H263, MKTAG('s','2','6','3') },
743 { CODEC_ID_H264, MKTAG('a','v','c','1') },
744 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
745 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
746 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
747 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
748 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
749 { CODEC_ID_NONE, 0 },
752 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
754 int tag = track->enc->codec_tag;
756 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
757 tag = mp4_get_codec_tag(s, track);
758 else if (track->mode == MODE_IPOD)
759 tag = ipod_get_codec_tag(s, track);
760 else if (track->mode & MODE_3GP)
761 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
763 tag = mov_get_codec_tag(s, track);
769 * Needed to make file play in iPods running newest firmware
770 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
772 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
775 ffio_wfourcc(pb, "uuid");
776 avio_wb32(pb, 0x6b6840f2);
777 avio_wb32(pb, 0x5f244fc5);
778 avio_wb32(pb, 0xba39a51b);
779 avio_wb32(pb, 0xcf0323f3);
784 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
786 int64_t pos = avio_tell(pb);
787 avio_wb32(pb, 0); /* size */
788 avio_wl32(pb, track->tag); // store it byteswapped
789 avio_wb32(pb, 0); /* Reserved */
790 avio_wb16(pb, 0); /* Reserved */
791 avio_wb16(pb, 1); /* Data-reference index */
793 if (track->enc->extradata_size)
794 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
796 return updateSize(pb, pos);
799 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
802 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
803 track->enc->sample_aspect_ratio.den, INT_MAX);
806 ffio_wfourcc(pb, "pasp");
807 avio_wb32(pb, sar.num);
808 avio_wb32(pb, sar.den);
812 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
814 int64_t pos = avio_tell(pb);
815 char compressor_name[32];
817 avio_wb32(pb, 0); /* size */
818 avio_wl32(pb, track->tag); // store it byteswapped
819 avio_wb32(pb, 0); /* Reserved */
820 avio_wb16(pb, 0); /* Reserved */
821 avio_wb16(pb, 1); /* Data-reference index */
823 avio_wb16(pb, 0); /* Codec stream version */
824 avio_wb16(pb, 0); /* Codec stream revision (=0) */
825 if (track->mode == MODE_MOV) {
826 ffio_wfourcc(pb, "FFMP"); /* Vendor */
827 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
828 avio_wb32(pb, 0); /* Temporal Quality */
829 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
831 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
832 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
835 avio_wb32(pb, 0); /* Reserved */
836 avio_wb32(pb, 0); /* Reserved */
837 avio_wb32(pb, 0); /* Reserved */
839 avio_wb16(pb, track->enc->width); /* Video width */
840 avio_wb16(pb, track->height); /* Video height */
841 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
842 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
843 avio_wb32(pb, 0); /* Data size (= 0) */
844 avio_wb16(pb, 1); /* Frame count (= 1) */
846 memset(compressor_name,0,32);
847 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
848 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
849 av_strlcpy(compressor_name,track->enc->codec->name,32);
850 avio_w8(pb, strlen(compressor_name));
851 avio_write(pb, compressor_name, 31);
853 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
854 avio_wb16(pb, track->enc->bits_per_coded_sample);
856 avio_wb16(pb, 0x18); /* Reserved */
857 avio_wb16(pb, 0xffff); /* Reserved */
858 if(track->tag == MKTAG('m','p','4','v'))
859 mov_write_esds_tag(pb, track);
860 else if(track->enc->codec_id == CODEC_ID_H263)
861 mov_write_d263_tag(pb);
862 else if(track->enc->codec_id == CODEC_ID_SVQ3)
863 mov_write_svq3_tag(pb);
864 else if(track->enc->codec_id == CODEC_ID_DNXHD)
865 mov_write_avid_tag(pb, track);
866 else if(track->enc->codec_id == CODEC_ID_H264) {
867 mov_write_avcc_tag(pb, track);
868 if(track->mode == MODE_IPOD)
869 mov_write_uuid_tag_ipod(pb);
870 } else if(track->vosLen > 0)
871 mov_write_glbl_tag(pb, track);
873 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
874 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
875 mov_write_pasp_tag(pb, track);
878 return updateSize(pb, pos);
881 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
883 int64_t pos = avio_tell(pb);
884 avio_wb32(pb, 0); /* size */
885 ffio_wfourcc(pb, "rtp ");
886 avio_wb32(pb, 0); /* Reserved */
887 avio_wb16(pb, 0); /* Reserved */
888 avio_wb16(pb, 1); /* Data-reference index */
890 avio_wb16(pb, 1); /* Hint track version */
891 avio_wb16(pb, 1); /* Highest compatible version */
892 avio_wb32(pb, track->max_packet_size); /* Max packet size */
894 avio_wb32(pb, 12); /* size */
895 ffio_wfourcc(pb, "tims");
896 avio_wb32(pb, track->timescale);
898 return updateSize(pb, pos);
901 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
903 int64_t pos = avio_tell(pb);
904 avio_wb32(pb, 0); /* size */
905 ffio_wfourcc(pb, "stsd");
906 avio_wb32(pb, 0); /* version & flags */
907 avio_wb32(pb, 1); /* entry count */
908 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
909 mov_write_video_tag(pb, track);
910 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
911 mov_write_audio_tag(pb, track);
912 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
913 mov_write_subtitle_tag(pb, track);
914 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
915 mov_write_rtp_tag(pb, track);
916 return updateSize(pb, pos);
919 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
921 MOVStts *ctts_entries;
922 uint32_t entries = 0;
926 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
927 ctts_entries[0].count = 1;
928 ctts_entries[0].duration = track->cluster[0].cts;
929 for (i=1; i<track->entry; i++) {
930 if (track->cluster[i].cts == ctts_entries[entries].duration) {
931 ctts_entries[entries].count++; /* compress */
934 ctts_entries[entries].duration = track->cluster[i].cts;
935 ctts_entries[entries].count = 1;
938 entries++; /* last one */
939 atom_size = 16 + (entries * 8);
940 avio_wb32(pb, atom_size); /* size */
941 ffio_wfourcc(pb, "ctts");
942 avio_wb32(pb, 0); /* version & flags */
943 avio_wb32(pb, entries); /* entry count */
944 for (i=0; i<entries; i++) {
945 avio_wb32(pb, ctts_entries[i].count);
946 avio_wb32(pb, ctts_entries[i].duration);
948 av_free(ctts_entries);
952 /* Time to sample atom */
953 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
955 MOVStts *stts_entries;
956 uint32_t entries = -1;
960 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
961 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
962 stts_entries[0].count = track->sampleCount;
963 stts_entries[0].duration = 1;
966 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
967 for (i=0; i<track->entry; i++) {
968 int64_t duration = i + 1 == track->entry ?
969 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
970 track->cluster[i+1].dts - track->cluster[i].dts;
971 if (i && duration == stts_entries[entries].duration) {
972 stts_entries[entries].count++; /* compress */
975 stts_entries[entries].duration = duration;
976 stts_entries[entries].count = 1;
979 entries++; /* last one */
981 atom_size = 16 + (entries * 8);
982 avio_wb32(pb, atom_size); /* size */
983 ffio_wfourcc(pb, "stts");
984 avio_wb32(pb, 0); /* version & flags */
985 avio_wb32(pb, entries); /* entry count */
986 for (i=0; i<entries; i++) {
987 avio_wb32(pb, stts_entries[i].count);
988 avio_wb32(pb, stts_entries[i].duration);
990 av_free(stts_entries);
994 static int mov_write_dref_tag(AVIOContext *pb)
996 avio_wb32(pb, 28); /* size */
997 ffio_wfourcc(pb, "dref");
998 avio_wb32(pb, 0); /* version & flags */
999 avio_wb32(pb, 1); /* entry count */
1001 avio_wb32(pb, 0xc); /* size */
1002 ffio_wfourcc(pb, "url ");
1003 avio_wb32(pb, 1); /* version & flags */
1008 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1010 int64_t pos = avio_tell(pb);
1011 avio_wb32(pb, 0); /* size */
1012 ffio_wfourcc(pb, "stbl");
1013 mov_write_stsd_tag(pb, track);
1014 mov_write_stts_tag(pb, track);
1015 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1016 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1017 track->hasKeyframes && track->hasKeyframes < track->entry)
1018 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1019 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1020 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1021 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1022 track->flags & MOV_TRACK_CTTS)
1023 mov_write_ctts_tag(pb, track);
1024 mov_write_stsc_tag(pb, track);
1025 mov_write_stsz_tag(pb, track);
1026 mov_write_stco_tag(pb, track);
1027 return updateSize(pb, pos);
1030 static int mov_write_dinf_tag(AVIOContext *pb)
1032 int64_t pos = avio_tell(pb);
1033 avio_wb32(pb, 0); /* size */
1034 ffio_wfourcc(pb, "dinf");
1035 mov_write_dref_tag(pb);
1036 return updateSize(pb, pos);
1039 static int mov_write_nmhd_tag(AVIOContext *pb)
1042 ffio_wfourcc(pb, "nmhd");
1047 static int mov_write_gmhd_tag(AVIOContext *pb)
1049 avio_wb32(pb, 0x20); /* size */
1050 ffio_wfourcc(pb, "gmhd");
1051 avio_wb32(pb, 0x18); /* gmin size */
1052 ffio_wfourcc(pb, "gmin");/* generic media info */
1053 avio_wb32(pb, 0); /* version & flags */
1054 avio_wb16(pb, 0x40); /* graphics mode = */
1055 avio_wb16(pb, 0x8000); /* opColor (r?) */
1056 avio_wb16(pb, 0x8000); /* opColor (g?) */
1057 avio_wb16(pb, 0x8000); /* opColor (b?) */
1058 avio_wb16(pb, 0); /* balance */
1059 avio_wb16(pb, 0); /* reserved */
1063 static int mov_write_smhd_tag(AVIOContext *pb)
1065 avio_wb32(pb, 16); /* size */
1066 ffio_wfourcc(pb, "smhd");
1067 avio_wb32(pb, 0); /* version & flags */
1068 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1069 avio_wb16(pb, 0); /* reserved */
1073 static int mov_write_vmhd_tag(AVIOContext *pb)
1075 avio_wb32(pb, 0x14); /* size (always 0x14) */
1076 ffio_wfourcc(pb, "vmhd");
1077 avio_wb32(pb, 0x01); /* version & flags */
1078 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1082 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1084 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1085 int64_t pos = avio_tell(pb);
1087 if (!track) { /* no media --> data handler */
1090 descr = "DataHandler";
1092 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1093 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1095 descr = "VideoHandler";
1096 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1098 descr = "SoundHandler";
1099 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1100 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1101 else hdlr_type = "text";
1102 descr = "SubtitleHandler";
1103 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1105 descr = "HintHandler";
1109 avio_wb32(pb, 0); /* size */
1110 ffio_wfourcc(pb, "hdlr");
1111 avio_wb32(pb, 0); /* Version & flags */
1112 avio_write(pb, hdlr, 4); /* handler */
1113 ffio_wfourcc(pb, hdlr_type); /* handler type */
1114 avio_wb32(pb ,0); /* reserved */
1115 avio_wb32(pb ,0); /* reserved */
1116 avio_wb32(pb ,0); /* reserved */
1117 if (!track || track->mode == MODE_MOV)
1118 avio_w8(pb, strlen(descr)); /* pascal string */
1119 avio_write(pb, descr, strlen(descr)); /* handler description */
1120 if (track && track->mode != MODE_MOV)
1121 avio_w8(pb, 0); /* c string */
1122 return updateSize(pb, pos);
1125 static int mov_write_hmhd_tag(AVIOContext *pb)
1127 /* This atom must be present, but leaving the values at zero
1128 * seems harmless. */
1129 avio_wb32(pb, 28); /* size */
1130 ffio_wfourcc(pb, "hmhd");
1131 avio_wb32(pb, 0); /* version, flags */
1132 avio_wb16(pb, 0); /* maxPDUsize */
1133 avio_wb16(pb, 0); /* avgPDUsize */
1134 avio_wb32(pb, 0); /* maxbitrate */
1135 avio_wb32(pb, 0); /* avgbitrate */
1136 avio_wb32(pb, 0); /* reserved */
1140 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1142 int64_t pos = avio_tell(pb);
1143 avio_wb32(pb, 0); /* size */
1144 ffio_wfourcc(pb, "minf");
1145 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1146 mov_write_vmhd_tag(pb);
1147 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1148 mov_write_smhd_tag(pb);
1149 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1150 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1151 else mov_write_nmhd_tag(pb);
1152 } else if (track->tag == MKTAG('r','t','p',' ')) {
1153 mov_write_hmhd_tag(pb);
1155 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1156 mov_write_hdlr_tag(pb, NULL);
1157 mov_write_dinf_tag(pb);
1158 mov_write_stbl_tag(pb, track);
1159 return updateSize(pb, pos);
1162 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1164 int version = track->trackDuration < INT32_MAX ? 0 : 1;
1166 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1167 ffio_wfourcc(pb, "mdhd");
1168 avio_w8(pb, version);
1169 avio_wb24(pb, 0); /* flags */
1171 avio_wb64(pb, track->time);
1172 avio_wb64(pb, track->time);
1174 avio_wb32(pb, track->time); /* creation time */
1175 avio_wb32(pb, track->time); /* modification time */
1177 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1178 (version == 1) ? avio_wb64(pb, track->trackDuration) : avio_wb32(pb, track->trackDuration); /* duration */
1179 avio_wb16(pb, track->language); /* language */
1180 avio_wb16(pb, 0); /* reserved (quality) */
1182 if(version!=0 && track->mode == MODE_MOV){
1183 av_log(NULL, AV_LOG_ERROR,
1184 "FATAL error, file duration too long for timebase, this file will not be\n"
1185 "playable with quicktime. Choose a different timebase or a different\n"
1186 "container format\n");
1192 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1194 int64_t pos = avio_tell(pb);
1195 avio_wb32(pb, 0); /* size */
1196 ffio_wfourcc(pb, "mdia");
1197 mov_write_mdhd_tag(pb, track);
1198 mov_write_hdlr_tag(pb, track);
1199 mov_write_minf_tag(pb, track);
1200 return updateSize(pb, pos);
1203 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1205 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1206 track->timescale, AV_ROUND_UP);
1207 int version = duration < INT32_MAX ? 0 : 1;
1209 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1210 ffio_wfourcc(pb, "tkhd");
1211 avio_w8(pb, version);
1212 avio_wb24(pb, 0xf); /* flags (track enabled) */
1214 avio_wb64(pb, track->time);
1215 avio_wb64(pb, track->time);
1217 avio_wb32(pb, track->time); /* creation time */
1218 avio_wb32(pb, track->time); /* modification time */
1220 avio_wb32(pb, track->trackID); /* track-id */
1221 avio_wb32(pb, 0); /* reserved */
1222 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1224 avio_wb32(pb, 0); /* reserved */
1225 avio_wb32(pb, 0); /* reserved */
1226 avio_wb32(pb, 0x0); /* reserved (Layer & Alternate group) */
1227 /* Volume, only for audio */
1228 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1229 avio_wb16(pb, 0x0100);
1232 avio_wb16(pb, 0); /* reserved */
1234 /* Matrix structure */
1235 avio_wb32(pb, 0x00010000); /* reserved */
1236 avio_wb32(pb, 0x0); /* reserved */
1237 avio_wb32(pb, 0x0); /* reserved */
1238 avio_wb32(pb, 0x0); /* reserved */
1239 avio_wb32(pb, 0x00010000); /* reserved */
1240 avio_wb32(pb, 0x0); /* reserved */
1241 avio_wb32(pb, 0x0); /* reserved */
1242 avio_wb32(pb, 0x0); /* reserved */
1243 avio_wb32(pb, 0x40000000); /* reserved */
1245 /* Track width and height, for visual only */
1246 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1247 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1248 if(track->mode == MODE_MOV) {
1249 avio_wb32(pb, track->enc->width << 16);
1250 avio_wb32(pb, track->height << 16);
1252 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1253 if(!sample_aspect_ratio || track->height != track->enc->height)
1254 sample_aspect_ratio = 1;
1255 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1256 avio_wb32(pb, track->height*0x10000);
1266 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1268 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1269 track->enc->sample_aspect_ratio.den);
1271 int64_t pos = avio_tell(pb);
1273 avio_wb32(pb, 0); /* size */
1274 ffio_wfourcc(pb, "tapt");
1277 ffio_wfourcc(pb, "clef");
1279 avio_wb32(pb, width << 16);
1280 avio_wb32(pb, track->enc->height << 16);
1283 ffio_wfourcc(pb, "enof");
1285 avio_wb32(pb, track->enc->width << 16);
1286 avio_wb32(pb, track->enc->height << 16);
1288 return updateSize(pb, pos);
1291 // This box seems important for the psp playback ... without it the movie seems to hang
1292 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1294 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1295 track->timescale, AV_ROUND_UP);
1296 int version = duration < INT32_MAX ? 0 : 1;
1297 int entry_size, entry_count, size;
1298 int64_t delay, start_ct = track->cluster[0].cts;
1299 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1300 track->timescale, AV_ROUND_DOWN);
1301 version |= delay < INT32_MAX ? 0 : 1;
1303 entry_size = (version == 1) ? 20 : 12;
1304 entry_count = 1 + (delay > 0);
1305 size = 24 + entry_count * entry_size;
1307 /* write the atom data */
1308 avio_wb32(pb, size);
1309 ffio_wfourcc(pb, "edts");
1310 avio_wb32(pb, size - 8);
1311 ffio_wfourcc(pb, "elst");
1312 avio_w8(pb, version);
1313 avio_wb24(pb, 0); /* flags */
1315 avio_wb32(pb, entry_count);
1316 if (delay > 0) { /* add an empty edit to delay presentation */
1318 avio_wb64(pb, delay);
1321 avio_wb32(pb, delay);
1324 avio_wb32(pb, 0x00010000);
1329 avio_wb64(pb, duration);
1330 avio_wb64(pb, start_ct);
1332 avio_wb32(pb, duration);
1333 avio_wb32(pb, start_ct);
1335 avio_wb32(pb, 0x00010000);
1339 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1341 avio_wb32(pb, 20); // size
1342 ffio_wfourcc(pb, "tref");
1343 avio_wb32(pb, 12); // size (subatom)
1344 avio_wl32(pb, track->tref_tag);
1345 avio_wb32(pb, track->tref_id);
1349 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1350 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1352 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1353 ffio_wfourcc(pb, "uuid");
1354 ffio_wfourcc(pb, "USMT");
1355 avio_wb32(pb, 0x21d24fce);
1356 avio_wb32(pb, 0xbb88695c);
1357 avio_wb32(pb, 0xfac9c740);
1358 avio_wb32(pb, 0x1c); // another size here!
1359 ffio_wfourcc(pb, "MTDT");
1360 avio_wb32(pb, 0x00010012);
1361 avio_wb32(pb, 0x0a);
1362 avio_wb32(pb, 0x55c40000);
1368 static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
1370 char buf[1000] = "";
1373 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
1374 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1377 avio_wb32(pb, len + 24);
1378 ffio_wfourcc(pb, "udta");
1379 avio_wb32(pb, len + 16);
1380 ffio_wfourcc(pb, "hnti");
1381 avio_wb32(pb, len + 8);
1382 ffio_wfourcc(pb, "sdp ");
1383 avio_write(pb, buf, len);
1387 static int mov_write_trak_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1389 int64_t pos = avio_tell(pb);
1390 avio_wb32(pb, 0); /* size */
1391 ffio_wfourcc(pb, "trak");
1392 mov_write_tkhd_tag(pb, track, st);
1393 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS || track->cluster[0].dts)
1394 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1395 if (track->tref_tag)
1396 mov_write_tref_tag(pb, track);
1397 mov_write_mdia_tag(pb, track);
1398 if (track->mode == MODE_PSP)
1399 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1400 if (track->tag == MKTAG('r','t','p',' '))
1401 mov_write_udta_sdp(pb, track->rtp_ctx, track->trackID);
1402 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1403 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1404 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1405 mov_write_tapt_tag(pb, track);
1407 return updateSize(pb, pos);
1411 /* TODO: Not sorted out, but not necessary either */
1412 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1414 avio_wb32(pb, 0x15); /* size */
1415 ffio_wfourcc(pb, "iods");
1416 avio_wb32(pb, 0); /* version & flags */
1417 avio_wb16(pb, 0x1007);
1419 avio_wb16(pb, 0x4fff);
1420 avio_wb16(pb, 0xfffe);
1421 avio_wb16(pb, 0x01ff);
1426 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1428 int maxTrackID = 1, i;
1429 int64_t maxTrackLenTemp, maxTrackLen = 0;
1432 for (i=0; i<mov->nb_streams; i++) {
1433 if(mov->tracks[i].entry > 0) {
1434 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration,
1436 mov->tracks[i].timescale,
1438 if(maxTrackLen < maxTrackLenTemp)
1439 maxTrackLen = maxTrackLenTemp;
1440 if(maxTrackID < mov->tracks[i].trackID)
1441 maxTrackID = mov->tracks[i].trackID;
1445 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1446 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1447 ffio_wfourcc(pb, "mvhd");
1448 avio_w8(pb, version);
1449 avio_wb24(pb, 0); /* flags */
1451 avio_wb64(pb, mov->time);
1452 avio_wb64(pb, mov->time);
1454 avio_wb32(pb, mov->time); /* creation time */
1455 avio_wb32(pb, mov->time); /* modification time */
1457 avio_wb32(pb, MOV_TIMESCALE);
1458 (version == 1) ? avio_wb64(pb, maxTrackLen) : avio_wb32(pb, maxTrackLen); /* duration of longest track */
1460 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1461 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1462 avio_wb16(pb, 0); /* reserved */
1463 avio_wb32(pb, 0); /* reserved */
1464 avio_wb32(pb, 0); /* reserved */
1466 /* Matrix structure */
1467 avio_wb32(pb, 0x00010000); /* reserved */
1468 avio_wb32(pb, 0x0); /* reserved */
1469 avio_wb32(pb, 0x0); /* reserved */
1470 avio_wb32(pb, 0x0); /* reserved */
1471 avio_wb32(pb, 0x00010000); /* reserved */
1472 avio_wb32(pb, 0x0); /* reserved */
1473 avio_wb32(pb, 0x0); /* reserved */
1474 avio_wb32(pb, 0x0); /* reserved */
1475 avio_wb32(pb, 0x40000000); /* reserved */
1477 avio_wb32(pb, 0); /* reserved (preview time) */
1478 avio_wb32(pb, 0); /* reserved (preview duration) */
1479 avio_wb32(pb, 0); /* reserved (poster time) */
1480 avio_wb32(pb, 0); /* reserved (selection time) */
1481 avio_wb32(pb, 0); /* reserved (selection duration) */
1482 avio_wb32(pb, 0); /* reserved (current time) */
1483 avio_wb32(pb, maxTrackID+1); /* Next track id */
1487 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1490 avio_wb32(pb, 33); /* size */
1491 ffio_wfourcc(pb, "hdlr");
1494 ffio_wfourcc(pb, "mdir");
1495 ffio_wfourcc(pb, "appl");
1502 /* helper function to write a data tag with the specified string as data */
1503 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1506 int size = 16 + strlen(data);
1507 avio_wb32(pb, size); /* size */
1508 ffio_wfourcc(pb, "data");
1511 avio_write(pb, data, strlen(data));
1515 lang = ff_mov_iso639_to_lang("und", 1);
1516 avio_wb16(pb, strlen(data)); /* string length */
1517 avio_wb16(pb, lang);
1518 avio_write(pb, data, strlen(data));
1519 return strlen(data) + 4;
1523 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1525 if (value && value[0]) {
1526 int64_t pos = avio_tell(pb);
1527 avio_wb32(pb, 0); /* size */
1528 ffio_wfourcc(pb, name);
1529 mov_write_string_data_tag(pb, value, lang, long_style);
1530 size= updateSize(pb, pos);
1535 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1536 const char *name, const char *tag,
1539 int l, lang = 0, len, len2;
1540 AVDictionaryEntry *t, *t2 = NULL;
1543 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1546 len = strlen(t->key);
1547 snprintf(tag2, sizeof(tag2), "%s-", tag);
1548 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1549 len2 = strlen(t2->key);
1550 if (len2 == len+4 && !strcmp(t->value, t2->value)
1551 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1556 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1559 /* iTunes track number */
1560 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1563 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1564 int size = 0, track = t ? atoi(t->value) : 0;
1566 avio_wb32(pb, 32); /* size */
1567 ffio_wfourcc(pb, "trkn");
1568 avio_wb32(pb, 24); /* size */
1569 ffio_wfourcc(pb, "data");
1570 avio_wb32(pb, 0); // 8 bytes empty
1572 avio_wb16(pb, 0); // empty
1573 avio_wb16(pb, track); // track number
1574 avio_wb16(pb, 0); // total track number
1575 avio_wb16(pb, 0); // empty
1581 /* iTunes meta data list */
1582 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1585 int64_t pos = avio_tell(pb);
1586 avio_wb32(pb, 0); /* size */
1587 ffio_wfourcc(pb, "ilst");
1588 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1589 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1590 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1591 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1592 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1593 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1594 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1595 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1596 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1597 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1598 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1599 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1600 mov_write_string_metadata(s, pb, "desc", "description",1);
1601 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1602 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1603 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1604 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1605 mov_write_trkn_tag(pb, mov, s);
1606 return updateSize(pb, pos);
1609 /* iTunes meta data tag */
1610 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1614 int64_t pos = avio_tell(pb);
1615 avio_wb32(pb, 0); /* size */
1616 ffio_wfourcc(pb, "meta");
1618 mov_write_itunes_hdlr_tag(pb, mov, s);
1619 mov_write_ilst_tag(pb, mov, s);
1620 size = updateSize(pb, pos);
1624 static int utf8len(const uint8_t *b)
1629 GET_UTF8(val, *b++, return -1;)
1635 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1639 GET_UTF8(val, *b++, return -1;)
1642 avio_wb16(pb, 0x00);
1646 static uint16_t language_code(const char *str)
1648 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1651 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1652 const char *tag, const char *str)
1654 int64_t pos = avio_tell(pb);
1655 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1656 if (!t || !utf8len(t->value))
1658 avio_wb32(pb, 0); /* size */
1659 ffio_wfourcc(pb, tag); /* type */
1660 avio_wb32(pb, 0); /* version + flags */
1661 if (!strcmp(tag, "yrrc"))
1662 avio_wb16(pb, atoi(t->value));
1664 avio_wb16(pb, language_code("eng")); /* language */
1665 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1666 if (!strcmp(tag, "albm") &&
1667 (t = av_dict_get(s->metadata, "track", NULL, 0)))
1668 avio_w8(pb, atoi(t->value));
1670 return updateSize(pb, pos);
1673 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1675 int64_t pos = avio_tell(pb);
1676 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1678 avio_wb32(pb, 0); // size
1679 ffio_wfourcc(pb, "chpl");
1680 avio_wb32(pb, 0x01000000); // version + flags
1681 avio_wb32(pb, 0); // unknown
1682 avio_w8(pb, nb_chapters);
1684 for (i = 0; i < nb_chapters; i++) {
1685 AVChapter *c = s->chapters[i];
1686 AVDictionaryEntry *t;
1687 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1689 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1690 int len = FFMIN(strlen(t->value), 255);
1692 avio_write(pb, t->value, len);
1696 return updateSize(pb, pos);
1699 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1702 AVIOContext *pb_buf;
1706 for (i = 0; i < s->nb_streams; i++)
1707 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1711 ret = avio_open_dyn_buf(&pb_buf);
1715 if (mov->mode & MODE_3GP) {
1716 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1717 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1718 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1719 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1720 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1721 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1722 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1723 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1724 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1725 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
1726 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1727 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1728 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1729 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
1730 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
1731 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1732 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1733 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1735 /* iTunes meta data */
1736 mov_write_meta_tag(pb_buf, mov, s);
1740 mov_write_chpl_tag(pb_buf, s);
1742 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1743 avio_wb32(pb, size+8);
1744 ffio_wfourcc(pb, "udta");
1745 avio_write(pb, buf, size);
1752 static void mov_write_psp_udta_tag(AVIOContext *pb,
1753 const char *str, const char *lang, int type)
1755 int len = utf8len(str)+1;
1758 avio_wb16(pb, len*2+10); /* size */
1759 avio_wb32(pb, type); /* type */
1760 avio_wb16(pb, language_code(lang)); /* language */
1761 avio_wb16(pb, 0x01); /* ? */
1762 ascii_to_wc(pb, str);
1765 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1767 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
1771 pos = avio_tell(pb);
1772 avio_wb32(pb, 0); /* size placeholder*/
1773 ffio_wfourcc(pb, "uuid");
1774 ffio_wfourcc(pb, "USMT");
1775 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1776 avio_wb32(pb, 0xbb88695c);
1777 avio_wb32(pb, 0xfac9c740);
1779 pos2 = avio_tell(pb);
1780 avio_wb32(pb, 0); /* size placeholder*/
1781 ffio_wfourcc(pb, "MTDT");
1785 avio_wb16(pb, 0x0C); /* size */
1786 avio_wb32(pb, 0x0B); /* type */
1787 avio_wb16(pb, language_code("und")); /* language */
1788 avio_wb16(pb, 0x0); /* ? */
1789 avio_wb16(pb, 0x021C); /* data */
1791 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1792 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
1793 // 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);
1794 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1796 updateSize(pb, pos2);
1797 return updateSize(pb, pos);
1803 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
1807 int64_t pos = avio_tell(pb);
1808 avio_wb32(pb, 0); /* size placeholder*/
1809 ffio_wfourcc(pb, "moov");
1811 for (i=0; i<mov->nb_streams; i++) {
1812 if(mov->tracks[i].entry <= 0) continue;
1814 mov->tracks[i].time = mov->time;
1815 mov->tracks[i].trackID = i+1;
1818 if (mov->chapter_track)
1819 for (i=0; i<s->nb_streams; i++) {
1820 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
1821 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID;
1823 for (i = 0; i < mov->nb_streams; i++) {
1824 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
1825 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
1826 mov->tracks[i].tref_id =
1827 mov->tracks[mov->tracks[i].src_track].trackID;
1831 mov_write_mvhd_tag(pb, mov);
1832 //mov_write_iods_tag(pb, mov);
1833 for (i=0; i<mov->nb_streams; i++) {
1834 if(mov->tracks[i].entry > 0) {
1835 mov_write_trak_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1839 if (mov->mode == MODE_PSP)
1840 mov_write_uuidusmt_tag(pb, s);
1842 mov_write_udta_tag(pb, mov, s);
1844 return updateSize(pb, pos);
1847 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
1849 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
1850 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
1852 mov->mdat_pos = avio_tell(pb);
1853 avio_wb32(pb, 0); /* size placeholder*/
1854 ffio_wfourcc(pb, "mdat");
1858 /* TODO: This needs to be more general */
1859 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
1861 MOVMuxContext *mov = s->priv_data;
1862 int64_t pos = avio_tell(pb);
1863 int has_h264 = 0, has_video = 0;
1867 for (i = 0; i < s->nb_streams; i++) {
1868 AVStream *st = s->streams[i];
1869 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1871 if (st->codec->codec_id == CODEC_ID_H264)
1875 avio_wb32(pb, 0); /* size */
1876 ffio_wfourcc(pb, "ftyp");
1878 if (mov->mode == MODE_3GP) {
1879 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
1880 minor = has_h264 ? 0x100 : 0x200;
1881 } else if (mov->mode & MODE_3G2) {
1882 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
1883 minor = has_h264 ? 0x20000 : 0x10000;
1884 }else if (mov->mode == MODE_PSP)
1885 ffio_wfourcc(pb, "MSNV");
1886 else if (mov->mode == MODE_MP4)
1887 ffio_wfourcc(pb, "isom");
1888 else if (mov->mode == MODE_IPOD)
1889 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
1891 ffio_wfourcc(pb, "qt ");
1893 avio_wb32(pb, minor);
1895 if(mov->mode == MODE_MOV)
1896 ffio_wfourcc(pb, "qt ");
1898 ffio_wfourcc(pb, "isom");
1899 ffio_wfourcc(pb, "iso2");
1901 ffio_wfourcc(pb, "avc1");
1904 if (mov->mode == MODE_3GP)
1905 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
1906 else if (mov->mode & MODE_3G2)
1907 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
1908 else if (mov->mode == MODE_PSP)
1909 ffio_wfourcc(pb, "MSNV");
1910 else if (mov->mode == MODE_MP4)
1911 ffio_wfourcc(pb, "mp41");
1912 return updateSize(pb, pos);
1915 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
1917 AVCodecContext *VideoCodec = s->streams[0]->codec;
1918 AVCodecContext *AudioCodec = s->streams[1]->codec;
1919 int AudioRate = AudioCodec->sample_rate;
1920 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1921 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1922 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1924 avio_wb32(pb, 0x94); /* size */
1925 ffio_wfourcc(pb, "uuid");
1926 ffio_wfourcc(pb, "PROF");
1928 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1929 avio_wb32(pb, 0xbb88695c);
1930 avio_wb32(pb, 0xfac9c740);
1932 avio_wb32(pb, 0x0); /* ? */
1933 avio_wb32(pb, 0x3); /* 3 sections ? */
1935 avio_wb32(pb, 0x14); /* size */
1936 ffio_wfourcc(pb, "FPRF");
1937 avio_wb32(pb, 0x0); /* ? */
1938 avio_wb32(pb, 0x0); /* ? */
1939 avio_wb32(pb, 0x0); /* ? */
1941 avio_wb32(pb, 0x2c); /* size */
1942 ffio_wfourcc(pb, "APRF");/* audio */
1944 avio_wb32(pb, 0x2); /* TrackID */
1945 ffio_wfourcc(pb, "mp4a");
1946 avio_wb32(pb, 0x20f);
1948 avio_wb32(pb, audio_kbitrate);
1949 avio_wb32(pb, audio_kbitrate);
1950 avio_wb32(pb, AudioRate);
1951 avio_wb32(pb, AudioCodec->channels);
1953 avio_wb32(pb, 0x34); /* size */
1954 ffio_wfourcc(pb, "VPRF"); /* video */
1956 avio_wb32(pb, 0x1); /* TrackID */
1957 if (VideoCodec->codec_id == CODEC_ID_H264) {
1958 ffio_wfourcc(pb, "avc1");
1959 avio_wb16(pb, 0x014D);
1960 avio_wb16(pb, 0x0015);
1962 ffio_wfourcc(pb, "mp4v");
1963 avio_wb16(pb, 0x0000);
1964 avio_wb16(pb, 0x0103);
1967 avio_wb32(pb, video_kbitrate);
1968 avio_wb32(pb, video_kbitrate);
1969 avio_wb32(pb, FrameRate);
1970 avio_wb32(pb, FrameRate);
1971 avio_wb16(pb, VideoCodec->width);
1972 avio_wb16(pb, VideoCodec->height);
1973 avio_wb32(pb, 0x010001); /* ? */
1976 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
1979 int i, closed_gop = 0;
1981 for (i = 0; i < pkt->size - 4; i++) {
1982 c = (c<<8) + pkt->data[i];
1983 if (c == 0x1b8) { // gop
1984 closed_gop = pkt->data[i+4]>>6 & 0x01;
1985 } else if (c == 0x100) { // pic
1986 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1987 if (!temp_ref || closed_gop) // I picture is not reordered
1988 *flags = MOV_SYNC_SAMPLE;
1990 *flags = MOV_PARTIAL_SYNC_SAMPLE;
1997 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1999 MOVMuxContext *mov = s->priv_data;
2000 AVIOContext *pb = s->pb;
2001 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2002 AVCodecContext *enc = trk->enc;
2003 unsigned int samplesInChunk = 0;
2004 int size= pkt->size;
2006 if (!s->pb->seekable) return 0; /* Can't handle that */
2007 if (!size) return 0; /* Discard 0 sized packets */
2009 if (enc->codec_id == CODEC_ID_AMR_NB) {
2010 /* We must find out how many AMR blocks there are in one packet */
2011 static uint16_t packed_size[16] =
2012 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
2015 while (len < size && samplesInChunk < 100) {
2016 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2019 if(samplesInChunk > 1){
2020 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2023 } else if (enc->codec_id == CODEC_ID_ADPCM_MS ||
2024 enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
2025 samplesInChunk = enc->frame_size;
2026 } else if (trk->sampleSize)
2027 samplesInChunk = size/trk->sampleSize;
2031 /* copy extradata if it exists */
2032 if (trk->vosLen == 0 && enc->extradata_size > 0) {
2033 trk->vosLen = enc->extradata_size;
2034 trk->vosData = av_malloc(trk->vosLen);
2035 memcpy(trk->vosData, enc->extradata, trk->vosLen);
2038 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
2039 /* from x264 or from bytestream h264 */
2040 /* nal reformating needed */
2041 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2042 } else if (enc->codec_id == CODEC_ID_AAC && pkt->size > 2 &&
2043 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
2044 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
2047 avio_write(pb, pkt->data, size);
2050 if ((enc->codec_id == CODEC_ID_DNXHD ||
2051 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
2052 /* copy frame to create needed atoms */
2054 trk->vosData = av_malloc(size);
2056 return AVERROR(ENOMEM);
2057 memcpy(trk->vosData, pkt->data, size);
2060 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
2061 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
2066 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2067 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
2068 trk->cluster[trk->entry].size = size;
2069 trk->cluster[trk->entry].entries = samplesInChunk;
2070 trk->cluster[trk->entry].dts = pkt->dts;
2071 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
2073 if (pkt->pts == AV_NOPTS_VALUE) {
2074 av_log(s, AV_LOG_WARNING, "pts has no value\n");
2075 pkt->pts = pkt->dts;
2077 if (pkt->dts != pkt->pts)
2078 trk->flags |= MOV_TRACK_CTTS;
2079 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2080 trk->cluster[trk->entry].flags = 0;
2081 if (pkt->flags & AV_PKT_FLAG_KEY) {
2082 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
2083 trk->entry > 0) { // force sync sample for the first key frame
2084 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2085 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2086 trk->flags |= MOV_TRACK_STPS;
2088 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2090 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2091 trk->hasKeyframes++;
2094 trk->sampleCount += samplesInChunk;
2095 mov->mdat_size += size;
2099 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2100 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry);
2104 // QuickTime chapters involve an additional text track with the chapter names
2105 // as samples, and a tref pointing from the other tracks to the chapter one.
2106 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2108 MOVMuxContext *mov = s->priv_data;
2109 MOVTrack *track = &mov->tracks[tracknum];
2110 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2113 track->mode = mov->mode;
2114 track->tag = MKTAG('t','e','x','t');
2115 track->timescale = MOV_TIMESCALE;
2116 track->enc = avcodec_alloc_context();
2117 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2119 for (i = 0; i < s->nb_chapters; i++) {
2120 AVChapter *c = s->chapters[i];
2121 AVDictionaryEntry *t;
2123 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
2124 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
2125 pkt.duration = end - pkt.dts;
2127 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2128 len = strlen(t->value);
2130 pkt.data = av_malloc(pkt.size);
2131 AV_WB16(pkt.data, len);
2132 memcpy(pkt.data+2, t->value, len);
2133 ff_mov_write_packet(s, &pkt);
2134 av_freep(&pkt.data);
2139 static int mov_write_header(AVFormatContext *s)
2141 AVIOContext *pb = s->pb;
2142 MOVMuxContext *mov = s->priv_data;
2143 int i, hint_track = 0;
2145 if (!s->pb->seekable) {
2146 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
2150 /* Default mode == MP4 */
2151 mov->mode = MODE_MP4;
2153 if (s->oformat != NULL) {
2154 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
2155 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
2156 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
2157 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
2158 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
2160 mov_write_ftyp_tag(pb,s);
2161 if (mov->mode == MODE_PSP) {
2162 if (s->nb_streams != 2) {
2163 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
2166 mov_write_uuidprof_tag(pb,s);
2170 mov->nb_streams = s->nb_streams;
2171 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
2172 mov->chapter_track = mov->nb_streams++;
2174 #if FF_API_FLAG_RTP_HINT
2175 if (s->flags & AVFMT_FLAG_RTP_HINT) {
2176 av_log(s, AV_LOG_WARNING, "The RTP_HINT flag is deprecated, enable it "
2177 "via the -movflags rtphint muxer option "
2179 mov->flags |= FF_MOV_FLAG_RTP_HINT;
2182 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2183 /* Add hint tracks for each audio and video stream */
2184 hint_track = mov->nb_streams;
2185 for (i = 0; i < s->nb_streams; i++) {
2186 AVStream *st = s->streams[i];
2187 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2188 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2194 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
2196 return AVERROR(ENOMEM);
2198 for(i=0; i<s->nb_streams; i++){
2199 AVStream *st= s->streams[i];
2200 MOVTrack *track= &mov->tracks[i];
2201 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
2203 track->enc = st->codec;
2204 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2205 if (track->language < 0)
2206 track->language = 0;
2207 track->mode = mov->mode;
2208 track->tag = mov_find_codec_tag(s, track);
2210 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
2211 "codec not currently supported in container\n", i);
2214 /* If hinting of this track is enabled by a later hint track,
2215 * this is updated. */
2216 track->hint_track = -1;
2217 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2218 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
2219 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
2220 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
2221 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
2222 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
2225 track->height = track->tag>>24 == 'n' ? 486 : 576;
2227 track->timescale = st->codec->time_base.den;
2228 if (track->mode == MODE_MOV && track->timescale > 100000)
2229 av_log(s, AV_LOG_WARNING,
2230 "WARNING codec timebase is very high. If duration is too long,\n"
2231 "file may not be playable by quicktime. Specify a shorter timebase\n"
2232 "or choose different container.\n");
2233 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
2234 track->timescale = st->codec->sample_rate;
2235 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
2236 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
2238 }else if(st->codec->codec_id == CODEC_ID_ADPCM_MS ||
2239 st->codec->codec_id == CODEC_ID_ADPCM_IMA_WAV){
2240 if (!st->codec->block_align) {
2241 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
2244 track->sampleSize = st->codec->block_align;
2245 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
2246 track->audio_vbr = 1;
2248 st->codec->frame_size = 1;
2249 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
2251 if (track->mode != MODE_MOV) {
2252 if (track->timescale > UINT16_MAX) {
2253 av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
2254 "sample rate %dhz\n", i, track->timescale);
2257 if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
2258 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
2259 i, track->enc->sample_rate);
2263 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
2264 track->timescale = st->codec->time_base.den;
2267 track->height = st->codec->height;
2269 av_set_pts_info(st, 64, 1, track->timescale);
2272 mov_write_mdat_tag(pb, mov);
2273 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
2275 if (mov->chapter_track)
2276 mov_create_chapter_track(s, mov->chapter_track);
2278 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2279 /* Initialize the hint tracks for each audio and video stream */
2280 for (i = 0; i < s->nb_streams; i++) {
2281 AVStream *st = s->streams[i];
2282 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2283 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2284 ff_mov_init_hinting(s, hint_track, i);
2294 av_freep(&mov->tracks);
2298 static int mov_write_trailer(AVFormatContext *s)
2300 MOVMuxContext *mov = s->priv_data;
2301 AVIOContext *pb = s->pb;
2305 int64_t moov_pos = avio_tell(pb);
2307 /* Write size of mdat tag */
2308 if (mov->mdat_size+8 <= UINT32_MAX) {
2309 avio_seek(pb, mov->mdat_pos, SEEK_SET);
2310 avio_wb32(pb, mov->mdat_size+8);
2312 /* overwrite 'wide' placeholder atom */
2313 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
2314 avio_wb32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
2315 ffio_wfourcc(pb, "mdat");
2316 avio_wb64(pb, mov->mdat_size+16);
2318 avio_seek(pb, moov_pos, SEEK_SET);
2320 mov_write_moov_tag(pb, mov, s);
2322 if (mov->chapter_track)
2323 av_freep(&mov->tracks[mov->chapter_track].enc);
2325 for (i=0; i<mov->nb_streams; i++) {
2326 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
2327 ff_mov_close_hinting(&mov->tracks[i]);
2328 av_freep(&mov->tracks[i].cluster);
2330 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
2336 av_freep(&mov->tracks);
2341 #if CONFIG_MOV_MUXER
2342 AVOutputFormat ff_mov_muxer = {
2344 NULL_IF_CONFIG_SMALL("MOV format"),
2347 sizeof(MOVMuxContext),
2351 ff_mov_write_packet,
2353 .flags = AVFMT_GLOBALHEADER,
2354 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
2355 .priv_class = &mov_muxer_class,
2358 #if CONFIG_TGP_MUXER
2359 AVOutputFormat ff_tgp_muxer = {
2361 NULL_IF_CONFIG_SMALL("3GP format"),
2364 sizeof(MOVMuxContext),
2368 ff_mov_write_packet,
2370 .flags = AVFMT_GLOBALHEADER,
2371 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2372 .priv_class = &mov_muxer_class,
2375 #if CONFIG_MP4_MUXER
2376 AVOutputFormat ff_mp4_muxer = {
2378 NULL_IF_CONFIG_SMALL("MP4 format"),
2381 sizeof(MOVMuxContext),
2385 ff_mov_write_packet,
2387 .flags = AVFMT_GLOBALHEADER,
2388 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2389 .priv_class = &mov_muxer_class,
2392 #if CONFIG_PSP_MUXER
2393 AVOutputFormat ff_psp_muxer = {
2395 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2398 sizeof(MOVMuxContext),
2402 ff_mov_write_packet,
2404 .flags = AVFMT_GLOBALHEADER,
2405 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2406 .priv_class = &mov_muxer_class,
2409 #if CONFIG_TG2_MUXER
2410 AVOutputFormat ff_tg2_muxer = {
2412 NULL_IF_CONFIG_SMALL("3GP2 format"),
2415 sizeof(MOVMuxContext),
2419 ff_mov_write_packet,
2421 .flags = AVFMT_GLOBALHEADER,
2422 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2423 .priv_class = &mov_muxer_class,
2426 #if CONFIG_IPOD_MUXER
2427 AVOutputFormat ff_ipod_muxer = {
2429 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2432 sizeof(MOVMuxContext),
2436 ff_mov_write_packet,
2438 .flags = AVFMT_GLOBALHEADER,
2439 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
2440 .priv_class = &mov_muxer_class,