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 Libav.
9 * Libav 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 * Libav 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 Libav; 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/intfloat_readwrite.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/dict.h"
43 static const AVOption options[] = {
44 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), FF_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
45 { "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" },
46 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
50 static const AVClass mov_muxer_class = {
51 .class_name = "MOV/3GP/MP4/3G2 muxer",
52 .item_name = av_default_item_name,
54 .version = LIBAVUTIL_VERSION_INT,
57 //FIXME support 64 bit variant with wide placeholders
58 static int64_t updateSize(AVIOContext *pb, int64_t pos)
60 int64_t curpos = avio_tell(pb);
61 avio_seek(pb, pos, SEEK_SET);
62 avio_wb32(pb, curpos - pos); /* rewrite size */
63 avio_seek(pb, curpos, SEEK_SET);
68 /* Chunk offset atom */
69 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
72 int mode64 = 0; // use 32 bit size variant if possible
73 int64_t pos = avio_tell(pb);
74 avio_wb32(pb, 0); /* size */
75 if (pos > UINT32_MAX) {
77 ffio_wfourcc(pb, "co64");
79 ffio_wfourcc(pb, "stco");
80 avio_wb32(pb, 0); /* version & flags */
81 avio_wb32(pb, track->entry); /* entry count */
82 for (i=0; i<track->entry; i++) {
84 avio_wb64(pb, track->cluster[i].pos);
86 avio_wb32(pb, track->cluster[i].pos);
88 return updateSize(pb, pos);
91 /* Sample size atom */
92 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
95 int i, j, entries = 0, tst = -1, oldtst = -1;
97 int64_t pos = avio_tell(pb);
98 avio_wb32(pb, 0); /* size */
99 ffio_wfourcc(pb, "stsz");
100 avio_wb32(pb, 0); /* version & flags */
102 for (i=0; i<track->entry; i++) {
103 tst = track->cluster[i].size/track->cluster[i].entries;
104 if(oldtst != -1 && tst != oldtst) {
108 entries += track->cluster[i].entries;
111 int sSize = track->cluster[0].size/track->cluster[0].entries;
112 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
113 avio_wb32(pb, sSize); // sample size
114 avio_wb32(pb, entries); // sample count
117 avio_wb32(pb, 0); // sample size
118 avio_wb32(pb, entries); // sample count
119 for (i=0; i<track->entry; i++) {
120 for (j=0; j<track->cluster[i].entries; j++) {
121 avio_wb32(pb, track->cluster[i].size /
122 track->cluster[i].entries);
126 return updateSize(pb, pos);
129 /* Sample to chunk atom */
130 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
132 int index = 0, oldval = -1, i;
133 int64_t entryPos, curpos;
135 int64_t pos = avio_tell(pb);
136 avio_wb32(pb, 0); /* size */
137 ffio_wfourcc(pb, "stsc");
138 avio_wb32(pb, 0); // version & flags
139 entryPos = avio_tell(pb);
140 avio_wb32(pb, track->entry); // entry count
141 for (i=0; i<track->entry; i++) {
142 if(oldval != track->cluster[i].samplesInChunk)
144 avio_wb32(pb, i+1); // first chunk
145 avio_wb32(pb, track->cluster[i].samplesInChunk); // samples per chunk
146 avio_wb32(pb, 0x1); // sample description index
147 oldval = track->cluster[i].samplesInChunk;
151 curpos = avio_tell(pb);
152 avio_seek(pb, entryPos, SEEK_SET);
153 avio_wb32(pb, index); // rewrite size
154 avio_seek(pb, curpos, SEEK_SET);
156 return updateSize(pb, pos);
159 /* Sync sample atom */
160 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
162 int64_t curpos, entryPos;
164 int64_t pos = avio_tell(pb);
165 avio_wb32(pb, 0); // size
166 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
167 avio_wb32(pb, 0); // version & flags
168 entryPos = avio_tell(pb);
169 avio_wb32(pb, track->entry); // entry count
170 for (i=0; i<track->entry; i++) {
171 if (track->cluster[i].flags & flag) {
176 curpos = avio_tell(pb);
177 avio_seek(pb, entryPos, SEEK_SET);
178 avio_wb32(pb, index); // rewrite size
179 avio_seek(pb, curpos, SEEK_SET);
180 return updateSize(pb, pos);
183 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
185 avio_wb32(pb, 0x11); /* size */
186 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
187 else ffio_wfourcc(pb, "damr");
188 ffio_wfourcc(pb, "FFMP");
189 avio_w8(pb, 0); /* decoder version */
191 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
192 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
193 avio_w8(pb, 0x01); /* Frames per sample */
197 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
202 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
204 if (track->vosLen < 7)
208 ffio_wfourcc(pb, "dac3");
210 init_get_bits(&gbc, track->vosData+4, track->vosLen-4);
211 fscod = get_bits(&gbc, 2);
212 frmsizecod = get_bits(&gbc, 6);
213 bsid = get_bits(&gbc, 5);
214 bsmod = get_bits(&gbc, 3);
215 acmod = get_bits(&gbc, 3);
217 skip_bits(&gbc, 2); // dsurmod
219 if ((acmod & 1) && acmod != 1)
220 skip_bits(&gbc, 2); // cmixlev
222 skip_bits(&gbc, 2); // surmixlev
224 lfeon = get_bits1(&gbc);
226 init_put_bits(&pbc, buf, sizeof(buf));
227 put_bits(&pbc, 2, fscod);
228 put_bits(&pbc, 5, bsid);
229 put_bits(&pbc, 3, bsmod);
230 put_bits(&pbc, 3, acmod);
231 put_bits(&pbc, 1, lfeon);
232 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
233 put_bits(&pbc, 5, 0); // reserved
235 flush_put_bits(&pbc);
236 avio_write(pb, buf, sizeof(buf));
242 * This function writes extradata "as is".
243 * Extradata must be formated like a valid atom (with size and tag)
245 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
247 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
248 return track->enc->extradata_size;
251 static int mov_write_enda_tag(AVIOContext *pb)
254 ffio_wfourcc(pb, "enda");
255 avio_wb16(pb, 1); /* little endian */
259 static void putDescr(AVIOContext *pb, int tag, unsigned int size)
264 avio_w8(pb, (size>>(7*i)) | 0x80);
265 avio_w8(pb, size & 0x7F);
268 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
270 int64_t pos = avio_tell(pb);
271 int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0;
273 avio_wb32(pb, 0); // size
274 ffio_wfourcc(pb, "esds");
275 avio_wb32(pb, 0); // Version
278 putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1);
279 avio_wb16(pb, track->trackID);
280 avio_w8(pb, 0x00); // flags (= no flags)
282 // DecoderConfig descriptor
283 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
285 // Object type indication
286 if ((track->enc->codec_id == CODEC_ID_MP2 ||
287 track->enc->codec_id == CODEC_ID_MP3) &&
288 track->enc->sample_rate > 24000)
289 avio_w8(pb, 0x6B); // 11172-3
291 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
293 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
294 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
295 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
296 avio_w8(pb, 0x15); // flags (= Audiostream)
298 avio_w8(pb, 0x11); // flags (= Visualstream)
300 avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
301 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
303 avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
304 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
305 avio_wb32(pb, 0); // vbr
307 avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
310 // DecoderSpecific info descriptor
311 putDescr(pb, 0x05, track->vosLen);
312 avio_write(pb, track->vosData, track->vosLen);
316 putDescr(pb, 0x06, 1);
318 return updateSize(pb, pos);
321 static int mov_pcm_le_gt16(enum CodecID codec_id)
323 return codec_id == CODEC_ID_PCM_S24LE ||
324 codec_id == CODEC_ID_PCM_S32LE ||
325 codec_id == CODEC_ID_PCM_F32LE ||
326 codec_id == CODEC_ID_PCM_F64LE;
329 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
331 int64_t pos = avio_tell(pb);
333 avio_wl32(pb, track->tag); // store it byteswapped
334 track->enc->codec_tag = av_bswap16(track->tag >> 16);
335 ff_put_wav_header(pb, track->enc);
336 return updateSize(pb, pos);
339 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
341 int64_t pos = avio_tell(pb);
343 avio_wb32(pb, 0); /* size */
344 ffio_wfourcc(pb, "wave");
346 avio_wb32(pb, 12); /* size */
347 ffio_wfourcc(pb, "frma");
348 avio_wl32(pb, track->tag);
350 if (track->enc->codec_id == CODEC_ID_AAC) {
351 /* useless atom needed by mplayer, ipod, not needed by quicktime */
352 avio_wb32(pb, 12); /* size */
353 ffio_wfourcc(pb, "mp4a");
355 mov_write_esds_tag(pb, track);
356 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
357 mov_write_enda_tag(pb);
358 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
359 mov_write_amr_tag(pb, track);
360 } else if (track->enc->codec_id == CODEC_ID_AC3) {
361 mov_write_ac3_tag(pb, track);
362 } else if (track->enc->codec_id == CODEC_ID_ALAC) {
363 mov_write_extradata_tag(pb, track);
364 } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
365 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
366 mov_write_ms_tag(pb, track);
369 avio_wb32(pb, 8); /* size */
370 avio_wb32(pb, 0); /* null tag */
372 return updateSize(pb, pos);
375 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
377 avio_wb32(pb, track->vosLen+8);
378 ffio_wfourcc(pb, "glbl");
379 avio_write(pb, track->vosData, track->vosLen);
380 return 8+track->vosLen;
384 * Compute flags for 'lpcm' tag.
385 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
387 static int mov_get_lpcm_flags(enum CodecID codec_id)
390 case CODEC_ID_PCM_F32BE:
391 case CODEC_ID_PCM_F64BE:
393 case CODEC_ID_PCM_F32LE:
394 case CODEC_ID_PCM_F64LE:
396 case CODEC_ID_PCM_U8:
398 case CODEC_ID_PCM_S16BE:
399 case CODEC_ID_PCM_S24BE:
400 case CODEC_ID_PCM_S32BE:
402 case CODEC_ID_PCM_S8:
403 case CODEC_ID_PCM_S16LE:
404 case CODEC_ID_PCM_S24LE:
405 case CODEC_ID_PCM_S32LE:
412 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
414 int64_t pos = avio_tell(pb);
416 uint32_t tag = track->tag;
418 if (track->mode == MODE_MOV) {
419 if (track->timescale > UINT16_MAX) {
420 if (mov_get_lpcm_flags(track->enc->codec_id))
421 tag = AV_RL32("lpcm");
423 } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
424 track->enc->codec_id == CODEC_ID_ADPCM_MS ||
425 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
430 avio_wb32(pb, 0); /* size */
431 avio_wl32(pb, tag); // store it byteswapped
432 avio_wb32(pb, 0); /* Reserved */
433 avio_wb16(pb, 0); /* Reserved */
434 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
436 /* SoundDescription */
437 avio_wb16(pb, version); /* Version */
438 avio_wb16(pb, 0); /* Revision level */
439 avio_wb32(pb, 0); /* Reserved */
444 avio_wb16(pb, 0xfffe);
446 avio_wb32(pb, 0x00010000);
448 avio_wb64(pb, av_dbl2int(track->timescale));
449 avio_wb32(pb, track->enc->channels);
450 avio_wb32(pb, 0x7F000000);
451 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
452 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
453 avio_wb32(pb, track->sampleSize);
454 avio_wb32(pb, track->enc->frame_size);
456 if (track->mode == MODE_MOV) {
457 avio_wb16(pb, track->enc->channels);
458 if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
459 track->enc->codec_id == CODEC_ID_PCM_S8)
460 avio_wb16(pb, 8); /* bits per sample */
463 avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
464 } else { /* reserved for mp4/3gp */
470 avio_wb16(pb, 0); /* packet size (= 0) */
471 avio_wb16(pb, track->timescale); /* Time scale */
472 avio_wb16(pb, 0); /* Reserved */
475 if(version == 1) { /* SoundDescription V1 extended info */
476 avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
477 avio_wb32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
478 avio_wb32(pb, track->sampleSize); /* Bytes per frame */
479 avio_wb32(pb, 2); /* Bytes per sample */
482 if(track->mode == MODE_MOV &&
483 (track->enc->codec_id == CODEC_ID_AAC ||
484 track->enc->codec_id == CODEC_ID_AC3 ||
485 track->enc->codec_id == CODEC_ID_AMR_NB ||
486 track->enc->codec_id == CODEC_ID_ALAC ||
487 track->enc->codec_id == CODEC_ID_ADPCM_MS ||
488 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
489 mov_pcm_le_gt16(track->enc->codec_id)))
490 mov_write_wave_tag(pb, track);
491 else if(track->tag == MKTAG('m','p','4','a'))
492 mov_write_esds_tag(pb, track);
493 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
494 mov_write_amr_tag(pb, track);
495 else if(track->enc->codec_id == CODEC_ID_AC3)
496 mov_write_ac3_tag(pb, track);
497 else if(track->enc->codec_id == CODEC_ID_ALAC)
498 mov_write_extradata_tag(pb, track);
499 else if(track->vosLen > 0)
500 mov_write_glbl_tag(pb, track);
502 return updateSize(pb, pos);
505 static int mov_write_d263_tag(AVIOContext *pb)
507 avio_wb32(pb, 0xf); /* size */
508 ffio_wfourcc(pb, "d263");
509 ffio_wfourcc(pb, "FFMP");
510 avio_w8(pb, 0); /* decoder version */
511 /* FIXME use AVCodecContext level/profile, when encoder will set values */
512 avio_w8(pb, 0xa); /* level */
513 avio_w8(pb, 0); /* profile */
517 /* TODO: No idea about these values */
518 static int mov_write_svq3_tag(AVIOContext *pb)
521 ffio_wfourcc(pb, "SMI ");
522 ffio_wfourcc(pb, "SEQH");
524 avio_wb32(pb, 0xe2c0211d);
525 avio_wb32(pb, 0xc0000000);
530 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
532 int64_t pos = avio_tell(pb);
535 ffio_wfourcc(pb, "avcC");
536 ff_isom_write_avcc(pb, track->vosData, track->vosLen);
537 return updateSize(pb, pos);
540 /* also used by all avid codecs (dv, imx, meridien) and their variants */
541 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
544 avio_wb32(pb, 24); /* size */
545 ffio_wfourcc(pb, "ACLR");
546 ffio_wfourcc(pb, "ACLR");
547 ffio_wfourcc(pb, "0001");
548 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
549 avio_wb32(pb, 0); /* unknown */
551 avio_wb32(pb, 24); /* size */
552 ffio_wfourcc(pb, "APRG");
553 ffio_wfourcc(pb, "APRG");
554 ffio_wfourcc(pb, "0001");
555 avio_wb32(pb, 1); /* unknown */
556 avio_wb32(pb, 0); /* unknown */
558 avio_wb32(pb, 120); /* size */
559 ffio_wfourcc(pb, "ARES");
560 ffio_wfourcc(pb, "ARES");
561 ffio_wfourcc(pb, "0001");
562 avio_wb32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
563 avio_wb32(pb, track->enc->width);
564 /* values below are based on samples created with quicktime and avid codecs */
565 if (track->vosData[5] & 2) { // interlaced
566 avio_wb32(pb, track->enc->height/2);
567 avio_wb32(pb, 2); /* unknown */
568 avio_wb32(pb, 0); /* unknown */
569 avio_wb32(pb, 4); /* unknown */
571 avio_wb32(pb, track->enc->height);
572 avio_wb32(pb, 1); /* unknown */
573 avio_wb32(pb, 0); /* unknown */
574 if (track->enc->height == 1080)
575 avio_wb32(pb, 5); /* unknown */
577 avio_wb32(pb, 6); /* unknown */
580 for (i = 0; i < 10; i++)
583 /* extra padding for stsd needed */
588 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
590 int tag = track->enc->codec_tag;
592 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
595 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
596 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
597 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
598 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
599 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
600 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
605 static const AVCodecTag codec_ipod_tags[] = {
606 { CODEC_ID_H264, MKTAG('a','v','c','1') },
607 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
608 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
609 { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
610 { CODEC_ID_AC3, MKTAG('a','c','-','3') },
611 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
612 { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
613 { CODEC_ID_NONE, 0 },
616 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
618 int tag = track->enc->codec_tag;
620 // keep original tag for subs, ipod supports both formats
621 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
622 (tag == MKTAG('t','x','3','g') ||
623 tag == MKTAG('t','e','x','t'))))
624 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
626 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
627 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
628 "Quicktime/Ipod might not play the file\n");
633 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
637 if (track->enc->width == 720) /* SD */
638 if (track->enc->height == 480) /* NTSC */
639 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
640 else tag = MKTAG('d','v','c',' ');
641 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
642 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
643 else tag = MKTAG('d','v','p','p');
644 else if (track->enc->height == 720) /* HD 720 line */
645 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
646 else tag = MKTAG('d','v','h','p');
647 else if (track->enc->height == 1080) /* HD 1080 line */
648 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
649 else tag = MKTAG('d','v','h','6');
651 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
658 static const struct {
659 enum PixelFormat pix_fmt;
662 } mov_pix_fmt_tags[] = {
663 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
664 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
665 { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
666 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
667 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
668 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
669 { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
670 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
671 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
672 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
673 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
674 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
675 { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
676 { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
679 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
681 int tag = track->enc->codec_tag;
684 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
685 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
686 tag = mov_pix_fmt_tags[i].tag;
687 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
695 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
697 int tag = track->enc->codec_tag;
699 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
700 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
701 track->enc->codec_id == CODEC_ID_RAWVIDEO ||
702 track->enc->codec_id == CODEC_ID_H263 ||
703 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
704 if (track->enc->codec_id == CODEC_ID_DVVIDEO)
705 tag = mov_get_dv_codec_tag(s, track);
706 else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
707 tag = mov_get_rawvideo_codec_tag(s, track);
708 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
709 tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
710 if (!tag) { // if no mac fcc found, try with Microsoft tags
711 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
713 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
714 "the file may be unplayable!\n");
716 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
717 tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
718 if (!tag) { // if no mac fcc found, try with Microsoft tags
719 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
721 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
722 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
723 "the file may be unplayable!\n");
726 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
727 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
733 static const AVCodecTag codec_3gp_tags[] = {
734 { CODEC_ID_H263, MKTAG('s','2','6','3') },
735 { CODEC_ID_H264, MKTAG('a','v','c','1') },
736 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
737 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
738 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
739 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
740 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
741 { CODEC_ID_NONE, 0 },
744 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
746 int tag = track->enc->codec_tag;
748 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
749 tag = mp4_get_codec_tag(s, track);
750 else if (track->mode == MODE_IPOD)
751 tag = ipod_get_codec_tag(s, track);
752 else if (track->mode & MODE_3GP)
753 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
755 tag = mov_get_codec_tag(s, track);
761 * Needed to make file play in iPods running newest firmware
762 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
764 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
767 ffio_wfourcc(pb, "uuid");
768 avio_wb32(pb, 0x6b6840f2);
769 avio_wb32(pb, 0x5f244fc5);
770 avio_wb32(pb, 0xba39a51b);
771 avio_wb32(pb, 0xcf0323f3);
776 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
778 int64_t pos = avio_tell(pb);
779 avio_wb32(pb, 0); /* size */
780 avio_wl32(pb, track->tag); // store it byteswapped
781 avio_wb32(pb, 0); /* Reserved */
782 avio_wb16(pb, 0); /* Reserved */
783 avio_wb16(pb, 1); /* Data-reference index */
785 if (track->enc->extradata_size)
786 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
788 return updateSize(pb, pos);
791 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
794 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
795 track->enc->sample_aspect_ratio.den, INT_MAX);
798 ffio_wfourcc(pb, "pasp");
799 avio_wb32(pb, sar.num);
800 avio_wb32(pb, sar.den);
804 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
806 int64_t pos = avio_tell(pb);
807 char compressor_name[32];
809 avio_wb32(pb, 0); /* size */
810 avio_wl32(pb, track->tag); // store it byteswapped
811 avio_wb32(pb, 0); /* Reserved */
812 avio_wb16(pb, 0); /* Reserved */
813 avio_wb16(pb, 1); /* Data-reference index */
815 avio_wb16(pb, 0); /* Codec stream version */
816 avio_wb16(pb, 0); /* Codec stream revision (=0) */
817 if (track->mode == MODE_MOV) {
818 ffio_wfourcc(pb, "FFMP"); /* Vendor */
819 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
820 avio_wb32(pb, 0); /* Temporal Quality */
821 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
823 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
824 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
827 avio_wb32(pb, 0); /* Reserved */
828 avio_wb32(pb, 0); /* Reserved */
829 avio_wb32(pb, 0); /* Reserved */
831 avio_wb16(pb, track->enc->width); /* Video width */
832 avio_wb16(pb, track->height); /* Video height */
833 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
834 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
835 avio_wb32(pb, 0); /* Data size (= 0) */
836 avio_wb16(pb, 1); /* Frame count (= 1) */
838 memset(compressor_name,0,32);
839 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
840 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
841 av_strlcpy(compressor_name,track->enc->codec->name,32);
842 avio_w8(pb, strlen(compressor_name));
843 avio_write(pb, compressor_name, 31);
845 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
846 avio_wb16(pb, track->enc->bits_per_coded_sample);
848 avio_wb16(pb, 0x18); /* Reserved */
849 avio_wb16(pb, 0xffff); /* Reserved */
850 if(track->tag == MKTAG('m','p','4','v'))
851 mov_write_esds_tag(pb, track);
852 else if(track->enc->codec_id == CODEC_ID_H263)
853 mov_write_d263_tag(pb);
854 else if(track->enc->codec_id == CODEC_ID_SVQ3)
855 mov_write_svq3_tag(pb);
856 else if(track->enc->codec_id == CODEC_ID_DNXHD)
857 mov_write_avid_tag(pb, track);
858 else if(track->enc->codec_id == CODEC_ID_H264) {
859 mov_write_avcc_tag(pb, track);
860 if(track->mode == MODE_IPOD)
861 mov_write_uuid_tag_ipod(pb);
862 } else if(track->vosLen > 0)
863 mov_write_glbl_tag(pb, track);
865 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
866 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
867 mov_write_pasp_tag(pb, track);
870 return updateSize(pb, pos);
873 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
875 int64_t pos = avio_tell(pb);
876 avio_wb32(pb, 0); /* size */
877 ffio_wfourcc(pb, "rtp ");
878 avio_wb32(pb, 0); /* Reserved */
879 avio_wb16(pb, 0); /* Reserved */
880 avio_wb16(pb, 1); /* Data-reference index */
882 avio_wb16(pb, 1); /* Hint track version */
883 avio_wb16(pb, 1); /* Highest compatible version */
884 avio_wb32(pb, track->max_packet_size); /* Max packet size */
886 avio_wb32(pb, 12); /* size */
887 ffio_wfourcc(pb, "tims");
888 avio_wb32(pb, track->timescale);
890 return updateSize(pb, pos);
893 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
895 int64_t pos = avio_tell(pb);
896 avio_wb32(pb, 0); /* size */
897 ffio_wfourcc(pb, "stsd");
898 avio_wb32(pb, 0); /* version & flags */
899 avio_wb32(pb, 1); /* entry count */
900 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
901 mov_write_video_tag(pb, track);
902 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
903 mov_write_audio_tag(pb, track);
904 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
905 mov_write_subtitle_tag(pb, track);
906 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
907 mov_write_rtp_tag(pb, track);
908 return updateSize(pb, pos);
911 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
913 MOVStts *ctts_entries;
914 uint32_t entries = 0;
918 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
919 ctts_entries[0].count = 1;
920 ctts_entries[0].duration = track->cluster[0].cts;
921 for (i=1; i<track->entry; i++) {
922 if (track->cluster[i].cts == ctts_entries[entries].duration) {
923 ctts_entries[entries].count++; /* compress */
926 ctts_entries[entries].duration = track->cluster[i].cts;
927 ctts_entries[entries].count = 1;
930 entries++; /* last one */
931 atom_size = 16 + (entries * 8);
932 avio_wb32(pb, atom_size); /* size */
933 ffio_wfourcc(pb, "ctts");
934 avio_wb32(pb, 0); /* version & flags */
935 avio_wb32(pb, entries); /* entry count */
936 for (i=0; i<entries; i++) {
937 avio_wb32(pb, ctts_entries[i].count);
938 avio_wb32(pb, ctts_entries[i].duration);
940 av_free(ctts_entries);
944 /* Time to sample atom */
945 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
947 MOVStts *stts_entries;
948 uint32_t entries = -1;
952 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
953 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
954 stts_entries[0].count = track->sampleCount;
955 stts_entries[0].duration = 1;
958 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
959 for (i=0; i<track->entry; i++) {
960 int64_t duration = i + 1 == track->entry ?
961 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
962 track->cluster[i+1].dts - track->cluster[i].dts;
963 if (i && duration == stts_entries[entries].duration) {
964 stts_entries[entries].count++; /* compress */
967 stts_entries[entries].duration = duration;
968 stts_entries[entries].count = 1;
971 entries++; /* last one */
973 atom_size = 16 + (entries * 8);
974 avio_wb32(pb, atom_size); /* size */
975 ffio_wfourcc(pb, "stts");
976 avio_wb32(pb, 0); /* version & flags */
977 avio_wb32(pb, entries); /* entry count */
978 for (i=0; i<entries; i++) {
979 avio_wb32(pb, stts_entries[i].count);
980 avio_wb32(pb, stts_entries[i].duration);
982 av_free(stts_entries);
986 static int mov_write_dref_tag(AVIOContext *pb)
988 avio_wb32(pb, 28); /* size */
989 ffio_wfourcc(pb, "dref");
990 avio_wb32(pb, 0); /* version & flags */
991 avio_wb32(pb, 1); /* entry count */
993 avio_wb32(pb, 0xc); /* size */
994 ffio_wfourcc(pb, "url ");
995 avio_wb32(pb, 1); /* version & flags */
1000 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1002 int64_t pos = avio_tell(pb);
1003 avio_wb32(pb, 0); /* size */
1004 ffio_wfourcc(pb, "stbl");
1005 mov_write_stsd_tag(pb, track);
1006 mov_write_stts_tag(pb, track);
1007 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1008 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1009 track->hasKeyframes && track->hasKeyframes < track->entry)
1010 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1011 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1012 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1013 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1014 track->flags & MOV_TRACK_CTTS)
1015 mov_write_ctts_tag(pb, track);
1016 mov_write_stsc_tag(pb, track);
1017 mov_write_stsz_tag(pb, track);
1018 mov_write_stco_tag(pb, track);
1019 return updateSize(pb, pos);
1022 static int mov_write_dinf_tag(AVIOContext *pb)
1024 int64_t pos = avio_tell(pb);
1025 avio_wb32(pb, 0); /* size */
1026 ffio_wfourcc(pb, "dinf");
1027 mov_write_dref_tag(pb);
1028 return updateSize(pb, pos);
1031 static int mov_write_nmhd_tag(AVIOContext *pb)
1034 ffio_wfourcc(pb, "nmhd");
1039 static int mov_write_gmhd_tag(AVIOContext *pb)
1041 avio_wb32(pb, 0x20); /* size */
1042 ffio_wfourcc(pb, "gmhd");
1043 avio_wb32(pb, 0x18); /* gmin size */
1044 ffio_wfourcc(pb, "gmin");/* generic media info */
1045 avio_wb32(pb, 0); /* version & flags */
1046 avio_wb16(pb, 0x40); /* graphics mode = */
1047 avio_wb16(pb, 0x8000); /* opColor (r?) */
1048 avio_wb16(pb, 0x8000); /* opColor (g?) */
1049 avio_wb16(pb, 0x8000); /* opColor (b?) */
1050 avio_wb16(pb, 0); /* balance */
1051 avio_wb16(pb, 0); /* reserved */
1055 static int mov_write_smhd_tag(AVIOContext *pb)
1057 avio_wb32(pb, 16); /* size */
1058 ffio_wfourcc(pb, "smhd");
1059 avio_wb32(pb, 0); /* version & flags */
1060 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1061 avio_wb16(pb, 0); /* reserved */
1065 static int mov_write_vmhd_tag(AVIOContext *pb)
1067 avio_wb32(pb, 0x14); /* size (always 0x14) */
1068 ffio_wfourcc(pb, "vmhd");
1069 avio_wb32(pb, 0x01); /* version & flags */
1070 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1074 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1076 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1077 int64_t pos = avio_tell(pb);
1079 if (!track) { /* no media --> data handler */
1082 descr = "DataHandler";
1084 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1085 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1087 descr = "VideoHandler";
1088 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1090 descr = "SoundHandler";
1091 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1092 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1093 else hdlr_type = "text";
1094 descr = "SubtitleHandler";
1095 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1097 descr = "HintHandler";
1101 avio_wb32(pb, 0); /* size */
1102 ffio_wfourcc(pb, "hdlr");
1103 avio_wb32(pb, 0); /* Version & flags */
1104 avio_write(pb, hdlr, 4); /* handler */
1105 ffio_wfourcc(pb, hdlr_type); /* handler type */
1106 avio_wb32(pb ,0); /* reserved */
1107 avio_wb32(pb ,0); /* reserved */
1108 avio_wb32(pb ,0); /* reserved */
1109 if (!track || track->mode == MODE_MOV)
1110 avio_w8(pb, strlen(descr)); /* pascal string */
1111 avio_write(pb, descr, strlen(descr)); /* handler description */
1112 if (track && track->mode != MODE_MOV)
1113 avio_w8(pb, 0); /* c string */
1114 return updateSize(pb, pos);
1117 static int mov_write_hmhd_tag(AVIOContext *pb)
1119 /* This atom must be present, but leaving the values at zero
1120 * seems harmless. */
1121 avio_wb32(pb, 28); /* size */
1122 ffio_wfourcc(pb, "hmhd");
1123 avio_wb32(pb, 0); /* version, flags */
1124 avio_wb16(pb, 0); /* maxPDUsize */
1125 avio_wb16(pb, 0); /* avgPDUsize */
1126 avio_wb32(pb, 0); /* maxbitrate */
1127 avio_wb32(pb, 0); /* avgbitrate */
1128 avio_wb32(pb, 0); /* reserved */
1132 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1134 int64_t pos = avio_tell(pb);
1135 avio_wb32(pb, 0); /* size */
1136 ffio_wfourcc(pb, "minf");
1137 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1138 mov_write_vmhd_tag(pb);
1139 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1140 mov_write_smhd_tag(pb);
1141 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1142 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1143 else mov_write_nmhd_tag(pb);
1144 } else if (track->tag == MKTAG('r','t','p',' ')) {
1145 mov_write_hmhd_tag(pb);
1147 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1148 mov_write_hdlr_tag(pb, NULL);
1149 mov_write_dinf_tag(pb);
1150 mov_write_stbl_tag(pb, track);
1151 return updateSize(pb, pos);
1154 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1156 int version = track->trackDuration < INT32_MAX ? 0 : 1;
1158 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1159 ffio_wfourcc(pb, "mdhd");
1160 avio_w8(pb, version);
1161 avio_wb24(pb, 0); /* flags */
1163 avio_wb64(pb, track->time);
1164 avio_wb64(pb, track->time);
1166 avio_wb32(pb, track->time); /* creation time */
1167 avio_wb32(pb, track->time); /* modification time */
1169 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1170 (version == 1) ? avio_wb64(pb, track->trackDuration) : avio_wb32(pb, track->trackDuration); /* duration */
1171 avio_wb16(pb, track->language); /* language */
1172 avio_wb16(pb, 0); /* reserved (quality) */
1174 if(version!=0 && track->mode == MODE_MOV){
1175 av_log(NULL, AV_LOG_ERROR,
1176 "FATAL error, file duration too long for timebase, this file will not be\n"
1177 "playable with quicktime. Choose a different timebase or a different\n"
1178 "container format\n");
1184 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1186 int64_t pos = avio_tell(pb);
1187 avio_wb32(pb, 0); /* size */
1188 ffio_wfourcc(pb, "mdia");
1189 mov_write_mdhd_tag(pb, track);
1190 mov_write_hdlr_tag(pb, track);
1191 mov_write_minf_tag(pb, track);
1192 return updateSize(pb, pos);
1195 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1197 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1198 track->timescale, AV_ROUND_UP);
1199 int version = duration < INT32_MAX ? 0 : 1;
1201 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1202 ffio_wfourcc(pb, "tkhd");
1203 avio_w8(pb, version);
1204 avio_wb24(pb, 0xf); /* flags (track enabled) */
1206 avio_wb64(pb, track->time);
1207 avio_wb64(pb, track->time);
1209 avio_wb32(pb, track->time); /* creation time */
1210 avio_wb32(pb, track->time); /* modification time */
1212 avio_wb32(pb, track->trackID); /* track-id */
1213 avio_wb32(pb, 0); /* reserved */
1214 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1216 avio_wb32(pb, 0); /* reserved */
1217 avio_wb32(pb, 0); /* reserved */
1218 avio_wb32(pb, 0x0); /* reserved (Layer & Alternate group) */
1219 /* Volume, only for audio */
1220 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1221 avio_wb16(pb, 0x0100);
1224 avio_wb16(pb, 0); /* reserved */
1226 /* Matrix structure */
1227 avio_wb32(pb, 0x00010000); /* reserved */
1228 avio_wb32(pb, 0x0); /* reserved */
1229 avio_wb32(pb, 0x0); /* reserved */
1230 avio_wb32(pb, 0x0); /* reserved */
1231 avio_wb32(pb, 0x00010000); /* reserved */
1232 avio_wb32(pb, 0x0); /* reserved */
1233 avio_wb32(pb, 0x0); /* reserved */
1234 avio_wb32(pb, 0x0); /* reserved */
1235 avio_wb32(pb, 0x40000000); /* reserved */
1237 /* Track width and height, for visual only */
1238 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1239 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1240 if(track->mode == MODE_MOV) {
1241 avio_wb32(pb, track->enc->width << 16);
1242 avio_wb32(pb, track->height << 16);
1244 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1245 if(!sample_aspect_ratio || track->height != track->enc->height)
1246 sample_aspect_ratio = 1;
1247 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1248 avio_wb32(pb, track->height*0x10000);
1258 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1260 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1261 track->enc->sample_aspect_ratio.den);
1263 int64_t pos = avio_tell(pb);
1265 avio_wb32(pb, 0); /* size */
1266 ffio_wfourcc(pb, "tapt");
1269 ffio_wfourcc(pb, "clef");
1271 avio_wb32(pb, width << 16);
1272 avio_wb32(pb, track->enc->height << 16);
1275 ffio_wfourcc(pb, "enof");
1277 avio_wb32(pb, track->enc->width << 16);
1278 avio_wb32(pb, track->enc->height << 16);
1280 return updateSize(pb, pos);
1283 // This box seems important for the psp playback ... without it the movie seems to hang
1284 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1286 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1287 track->timescale, AV_ROUND_UP);
1288 int version = duration < INT32_MAX ? 0 : 1;
1289 int entry_size, entry_count, size;
1290 int64_t delay, start_ct = track->cluster[0].cts;
1291 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1292 track->timescale, AV_ROUND_DOWN);
1293 version |= delay < INT32_MAX ? 0 : 1;
1295 entry_size = (version == 1) ? 20 : 12;
1296 entry_count = 1 + (delay > 0);
1297 size = 24 + entry_count * entry_size;
1299 /* write the atom data */
1300 avio_wb32(pb, size);
1301 ffio_wfourcc(pb, "edts");
1302 avio_wb32(pb, size - 8);
1303 ffio_wfourcc(pb, "elst");
1304 avio_w8(pb, version);
1305 avio_wb24(pb, 0); /* flags */
1307 avio_wb32(pb, entry_count);
1308 if (delay > 0) { /* add an empty edit to delay presentation */
1310 avio_wb64(pb, delay);
1313 avio_wb32(pb, delay);
1316 avio_wb32(pb, 0x00010000);
1321 avio_wb64(pb, duration);
1322 avio_wb64(pb, start_ct);
1324 avio_wb32(pb, duration);
1325 avio_wb32(pb, start_ct);
1327 avio_wb32(pb, 0x00010000);
1331 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1333 avio_wb32(pb, 20); // size
1334 ffio_wfourcc(pb, "tref");
1335 avio_wb32(pb, 12); // size (subatom)
1336 avio_wl32(pb, track->tref_tag);
1337 avio_wb32(pb, track->tref_id);
1341 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1342 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1344 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1345 ffio_wfourcc(pb, "uuid");
1346 ffio_wfourcc(pb, "USMT");
1347 avio_wb32(pb, 0x21d24fce);
1348 avio_wb32(pb, 0xbb88695c);
1349 avio_wb32(pb, 0xfac9c740);
1350 avio_wb32(pb, 0x1c); // another size here!
1351 ffio_wfourcc(pb, "MTDT");
1352 avio_wb32(pb, 0x00010012);
1353 avio_wb32(pb, 0x0a);
1354 avio_wb32(pb, 0x55c40000);
1360 static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
1362 char buf[1000] = "";
1365 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
1366 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1369 avio_wb32(pb, len + 24);
1370 ffio_wfourcc(pb, "udta");
1371 avio_wb32(pb, len + 16);
1372 ffio_wfourcc(pb, "hnti");
1373 avio_wb32(pb, len + 8);
1374 ffio_wfourcc(pb, "sdp ");
1375 avio_write(pb, buf, len);
1379 static int mov_write_trak_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1381 int64_t pos = avio_tell(pb);
1382 avio_wb32(pb, 0); /* size */
1383 ffio_wfourcc(pb, "trak");
1384 mov_write_tkhd_tag(pb, track, st);
1385 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS || track->cluster[0].dts)
1386 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1387 if (track->tref_tag)
1388 mov_write_tref_tag(pb, track);
1389 mov_write_mdia_tag(pb, track);
1390 if (track->mode == MODE_PSP)
1391 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1392 if (track->tag == MKTAG('r','t','p',' '))
1393 mov_write_udta_sdp(pb, track->rtp_ctx, track->trackID);
1394 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1395 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1396 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1397 mov_write_tapt_tag(pb, track);
1399 return updateSize(pb, pos);
1403 /* TODO: Not sorted out, but not necessary either */
1404 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1406 avio_wb32(pb, 0x15); /* size */
1407 ffio_wfourcc(pb, "iods");
1408 avio_wb32(pb, 0); /* version & flags */
1409 avio_wb16(pb, 0x1007);
1411 avio_wb16(pb, 0x4fff);
1412 avio_wb16(pb, 0xfffe);
1413 avio_wb16(pb, 0x01ff);
1418 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1420 int maxTrackID = 1, i;
1421 int64_t maxTrackLenTemp, maxTrackLen = 0;
1424 for (i=0; i<mov->nb_streams; i++) {
1425 if(mov->tracks[i].entry > 0) {
1426 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration,
1428 mov->tracks[i].timescale,
1430 if(maxTrackLen < maxTrackLenTemp)
1431 maxTrackLen = maxTrackLenTemp;
1432 if(maxTrackID < mov->tracks[i].trackID)
1433 maxTrackID = mov->tracks[i].trackID;
1437 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1438 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1439 ffio_wfourcc(pb, "mvhd");
1440 avio_w8(pb, version);
1441 avio_wb24(pb, 0); /* flags */
1443 avio_wb64(pb, mov->time);
1444 avio_wb64(pb, mov->time);
1446 avio_wb32(pb, mov->time); /* creation time */
1447 avio_wb32(pb, mov->time); /* modification time */
1449 avio_wb32(pb, MOV_TIMESCALE);
1450 (version == 1) ? avio_wb64(pb, maxTrackLen) : avio_wb32(pb, maxTrackLen); /* duration of longest track */
1452 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1453 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1454 avio_wb16(pb, 0); /* reserved */
1455 avio_wb32(pb, 0); /* reserved */
1456 avio_wb32(pb, 0); /* reserved */
1458 /* Matrix structure */
1459 avio_wb32(pb, 0x00010000); /* reserved */
1460 avio_wb32(pb, 0x0); /* reserved */
1461 avio_wb32(pb, 0x0); /* reserved */
1462 avio_wb32(pb, 0x0); /* reserved */
1463 avio_wb32(pb, 0x00010000); /* reserved */
1464 avio_wb32(pb, 0x0); /* reserved */
1465 avio_wb32(pb, 0x0); /* reserved */
1466 avio_wb32(pb, 0x0); /* reserved */
1467 avio_wb32(pb, 0x40000000); /* reserved */
1469 avio_wb32(pb, 0); /* reserved (preview time) */
1470 avio_wb32(pb, 0); /* reserved (preview duration) */
1471 avio_wb32(pb, 0); /* reserved (poster time) */
1472 avio_wb32(pb, 0); /* reserved (selection time) */
1473 avio_wb32(pb, 0); /* reserved (selection duration) */
1474 avio_wb32(pb, 0); /* reserved (current time) */
1475 avio_wb32(pb, maxTrackID+1); /* Next track id */
1479 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1482 avio_wb32(pb, 33); /* size */
1483 ffio_wfourcc(pb, "hdlr");
1486 ffio_wfourcc(pb, "mdir");
1487 ffio_wfourcc(pb, "appl");
1494 /* helper function to write a data tag with the specified string as data */
1495 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1498 int size = 16 + strlen(data);
1499 avio_wb32(pb, size); /* size */
1500 ffio_wfourcc(pb, "data");
1503 avio_write(pb, data, strlen(data));
1507 lang = ff_mov_iso639_to_lang("und", 1);
1508 avio_wb16(pb, strlen(data)); /* string length */
1509 avio_wb16(pb, lang);
1510 avio_write(pb, data, strlen(data));
1511 return strlen(data) + 4;
1515 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1517 if (value && value[0]) {
1518 int64_t pos = avio_tell(pb);
1519 avio_wb32(pb, 0); /* size */
1520 ffio_wfourcc(pb, name);
1521 mov_write_string_data_tag(pb, value, lang, long_style);
1522 size= updateSize(pb, pos);
1527 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1528 const char *name, const char *tag,
1531 int l, lang = 0, len, len2;
1532 AVDictionaryEntry *t, *t2 = NULL;
1535 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1538 len = strlen(t->key);
1539 snprintf(tag2, sizeof(tag2), "%s-", tag);
1540 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1541 len2 = strlen(t2->key);
1542 if (len2 == len+4 && !strcmp(t->value, t2->value)
1543 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1548 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1551 /* iTunes track number */
1552 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1555 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1556 int size = 0, track = t ? atoi(t->value) : 0;
1558 avio_wb32(pb, 32); /* size */
1559 ffio_wfourcc(pb, "trkn");
1560 avio_wb32(pb, 24); /* size */
1561 ffio_wfourcc(pb, "data");
1562 avio_wb32(pb, 0); // 8 bytes empty
1564 avio_wb16(pb, 0); // empty
1565 avio_wb16(pb, track); // track number
1566 avio_wb16(pb, 0); // total track number
1567 avio_wb16(pb, 0); // empty
1573 /* iTunes meta data list */
1574 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1577 int64_t pos = avio_tell(pb);
1578 avio_wb32(pb, 0); /* size */
1579 ffio_wfourcc(pb, "ilst");
1580 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1581 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1582 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1583 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1584 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1585 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1586 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1587 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1588 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1589 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1590 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1591 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1592 mov_write_string_metadata(s, pb, "desc", "description",1);
1593 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1594 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1595 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1596 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1597 mov_write_trkn_tag(pb, mov, s);
1598 return updateSize(pb, pos);
1601 /* iTunes meta data tag */
1602 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1606 int64_t pos = avio_tell(pb);
1607 avio_wb32(pb, 0); /* size */
1608 ffio_wfourcc(pb, "meta");
1610 mov_write_itunes_hdlr_tag(pb, mov, s);
1611 mov_write_ilst_tag(pb, mov, s);
1612 size = updateSize(pb, pos);
1616 static int utf8len(const uint8_t *b)
1621 GET_UTF8(val, *b++, return -1;)
1627 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1631 GET_UTF8(val, *b++, return -1;)
1634 avio_wb16(pb, 0x00);
1638 static uint16_t language_code(const char *str)
1640 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1643 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1644 const char *tag, const char *str)
1646 int64_t pos = avio_tell(pb);
1647 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1648 if (!t || !utf8len(t->value))
1650 avio_wb32(pb, 0); /* size */
1651 ffio_wfourcc(pb, tag); /* type */
1652 avio_wb32(pb, 0); /* version + flags */
1653 if (!strcmp(tag, "yrrc"))
1654 avio_wb16(pb, atoi(t->value));
1656 avio_wb16(pb, language_code("eng")); /* language */
1657 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1658 if (!strcmp(tag, "albm") &&
1659 (t = av_dict_get(s->metadata, "track", NULL, 0)))
1660 avio_w8(pb, atoi(t->value));
1662 return updateSize(pb, pos);
1665 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1667 int64_t pos = avio_tell(pb);
1668 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1670 avio_wb32(pb, 0); // size
1671 ffio_wfourcc(pb, "chpl");
1672 avio_wb32(pb, 0x01000000); // version + flags
1673 avio_wb32(pb, 0); // unknown
1674 avio_w8(pb, nb_chapters);
1676 for (i = 0; i < nb_chapters; i++) {
1677 AVChapter *c = s->chapters[i];
1678 AVDictionaryEntry *t;
1679 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1681 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1682 int len = FFMIN(strlen(t->value), 255);
1684 avio_write(pb, t->value, len);
1688 return updateSize(pb, pos);
1691 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1694 AVIOContext *pb_buf;
1698 for (i = 0; i < s->nb_streams; i++)
1699 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1703 ret = avio_open_dyn_buf(&pb_buf);
1707 if (mov->mode & MODE_3GP) {
1708 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1709 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1710 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1711 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1712 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1713 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1714 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1715 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1716 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1717 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
1718 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1719 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1720 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1721 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
1722 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
1723 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1724 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1725 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1727 /* iTunes meta data */
1728 mov_write_meta_tag(pb_buf, mov, s);
1732 mov_write_chpl_tag(pb_buf, s);
1734 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1735 avio_wb32(pb, size+8);
1736 ffio_wfourcc(pb, "udta");
1737 avio_write(pb, buf, size);
1744 static void mov_write_psp_udta_tag(AVIOContext *pb,
1745 const char *str, const char *lang, int type)
1747 int len = utf8len(str)+1;
1750 avio_wb16(pb, len*2+10); /* size */
1751 avio_wb32(pb, type); /* type */
1752 avio_wb16(pb, language_code(lang)); /* language */
1753 avio_wb16(pb, 0x01); /* ? */
1754 ascii_to_wc(pb, str);
1757 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1759 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
1763 pos = avio_tell(pb);
1764 avio_wb32(pb, 0); /* size placeholder*/
1765 ffio_wfourcc(pb, "uuid");
1766 ffio_wfourcc(pb, "USMT");
1767 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1768 avio_wb32(pb, 0xbb88695c);
1769 avio_wb32(pb, 0xfac9c740);
1771 pos2 = avio_tell(pb);
1772 avio_wb32(pb, 0); /* size placeholder*/
1773 ffio_wfourcc(pb, "MTDT");
1777 avio_wb16(pb, 0x0C); /* size */
1778 avio_wb32(pb, 0x0B); /* type */
1779 avio_wb16(pb, language_code("und")); /* language */
1780 avio_wb16(pb, 0x0); /* ? */
1781 avio_wb16(pb, 0x021C); /* data */
1783 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1784 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
1785 // 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);
1786 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1788 updateSize(pb, pos2);
1789 return updateSize(pb, pos);
1795 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
1799 int64_t pos = avio_tell(pb);
1800 avio_wb32(pb, 0); /* size placeholder*/
1801 ffio_wfourcc(pb, "moov");
1803 for (i=0; i<mov->nb_streams; i++) {
1804 if(mov->tracks[i].entry <= 0) continue;
1806 mov->tracks[i].time = mov->time;
1807 mov->tracks[i].trackID = i+1;
1810 if (mov->chapter_track)
1811 for (i=0; i<s->nb_streams; i++) {
1812 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
1813 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID;
1815 for (i = 0; i < mov->nb_streams; i++) {
1816 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
1817 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
1818 mov->tracks[i].tref_id =
1819 mov->tracks[mov->tracks[i].src_track].trackID;
1823 mov_write_mvhd_tag(pb, mov);
1824 //mov_write_iods_tag(pb, mov);
1825 for (i=0; i<mov->nb_streams; i++) {
1826 if(mov->tracks[i].entry > 0) {
1827 mov_write_trak_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1831 if (mov->mode == MODE_PSP)
1832 mov_write_uuidusmt_tag(pb, s);
1834 mov_write_udta_tag(pb, mov, s);
1836 return updateSize(pb, pos);
1839 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
1841 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
1842 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
1844 mov->mdat_pos = avio_tell(pb);
1845 avio_wb32(pb, 0); /* size placeholder*/
1846 ffio_wfourcc(pb, "mdat");
1850 /* TODO: This needs to be more general */
1851 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
1853 MOVMuxContext *mov = s->priv_data;
1854 int64_t pos = avio_tell(pb);
1855 int has_h264 = 0, has_video = 0;
1859 for (i = 0; i < s->nb_streams; i++) {
1860 AVStream *st = s->streams[i];
1861 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1863 if (st->codec->codec_id == CODEC_ID_H264)
1867 avio_wb32(pb, 0); /* size */
1868 ffio_wfourcc(pb, "ftyp");
1870 if (mov->mode == MODE_3GP) {
1871 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
1872 minor = has_h264 ? 0x100 : 0x200;
1873 } else if (mov->mode & MODE_3G2) {
1874 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
1875 minor = has_h264 ? 0x20000 : 0x10000;
1876 }else if (mov->mode == MODE_PSP)
1877 ffio_wfourcc(pb, "MSNV");
1878 else if (mov->mode == MODE_MP4)
1879 ffio_wfourcc(pb, "isom");
1880 else if (mov->mode == MODE_IPOD)
1881 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
1883 ffio_wfourcc(pb, "qt ");
1885 avio_wb32(pb, minor);
1887 if(mov->mode == MODE_MOV)
1888 ffio_wfourcc(pb, "qt ");
1890 ffio_wfourcc(pb, "isom");
1891 ffio_wfourcc(pb, "iso2");
1893 ffio_wfourcc(pb, "avc1");
1896 if (mov->mode == MODE_3GP)
1897 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
1898 else if (mov->mode & MODE_3G2)
1899 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
1900 else if (mov->mode == MODE_PSP)
1901 ffio_wfourcc(pb, "MSNV");
1902 else if (mov->mode == MODE_MP4)
1903 ffio_wfourcc(pb, "mp41");
1904 return updateSize(pb, pos);
1907 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
1909 AVCodecContext *VideoCodec = s->streams[0]->codec;
1910 AVCodecContext *AudioCodec = s->streams[1]->codec;
1911 int AudioRate = AudioCodec->sample_rate;
1912 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1913 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1914 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1916 avio_wb32(pb, 0x94); /* size */
1917 ffio_wfourcc(pb, "uuid");
1918 ffio_wfourcc(pb, "PROF");
1920 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1921 avio_wb32(pb, 0xbb88695c);
1922 avio_wb32(pb, 0xfac9c740);
1924 avio_wb32(pb, 0x0); /* ? */
1925 avio_wb32(pb, 0x3); /* 3 sections ? */
1927 avio_wb32(pb, 0x14); /* size */
1928 ffio_wfourcc(pb, "FPRF");
1929 avio_wb32(pb, 0x0); /* ? */
1930 avio_wb32(pb, 0x0); /* ? */
1931 avio_wb32(pb, 0x0); /* ? */
1933 avio_wb32(pb, 0x2c); /* size */
1934 ffio_wfourcc(pb, "APRF");/* audio */
1936 avio_wb32(pb, 0x2); /* TrackID */
1937 ffio_wfourcc(pb, "mp4a");
1938 avio_wb32(pb, 0x20f);
1940 avio_wb32(pb, audio_kbitrate);
1941 avio_wb32(pb, audio_kbitrate);
1942 avio_wb32(pb, AudioRate);
1943 avio_wb32(pb, AudioCodec->channels);
1945 avio_wb32(pb, 0x34); /* size */
1946 ffio_wfourcc(pb, "VPRF"); /* video */
1948 avio_wb32(pb, 0x1); /* TrackID */
1949 if (VideoCodec->codec_id == CODEC_ID_H264) {
1950 ffio_wfourcc(pb, "avc1");
1951 avio_wb16(pb, 0x014D);
1952 avio_wb16(pb, 0x0015);
1954 ffio_wfourcc(pb, "mp4v");
1955 avio_wb16(pb, 0x0000);
1956 avio_wb16(pb, 0x0103);
1959 avio_wb32(pb, video_kbitrate);
1960 avio_wb32(pb, video_kbitrate);
1961 avio_wb32(pb, FrameRate);
1962 avio_wb32(pb, FrameRate);
1963 avio_wb16(pb, VideoCodec->width);
1964 avio_wb16(pb, VideoCodec->height);
1965 avio_wb32(pb, 0x010001); /* ? */
1968 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
1971 int i, closed_gop = 0;
1973 for (i = 0; i < pkt->size - 4; i++) {
1974 c = (c<<8) + pkt->data[i];
1975 if (c == 0x1b8) { // gop
1976 closed_gop = pkt->data[i+4]>>6 & 0x01;
1977 } else if (c == 0x100) { // pic
1978 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1979 if (!temp_ref || closed_gop) // I picture is not reordered
1980 *flags = MOV_SYNC_SAMPLE;
1982 *flags = MOV_PARTIAL_SYNC_SAMPLE;
1989 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1991 MOVMuxContext *mov = s->priv_data;
1992 AVIOContext *pb = s->pb;
1993 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1994 AVCodecContext *enc = trk->enc;
1995 unsigned int samplesInChunk = 0;
1996 int size= pkt->size;
1998 if (!s->pb->seekable) return 0; /* Can't handle that */
1999 if (!size) return 0; /* Discard 0 sized packets */
2001 if (enc->codec_id == CODEC_ID_AMR_NB) {
2002 /* We must find out how many AMR blocks there are in one packet */
2003 static uint16_t packed_size[16] =
2004 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
2007 while (len < size && samplesInChunk < 100) {
2008 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2011 if(samplesInChunk > 1){
2012 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2015 } else if (enc->codec_id == CODEC_ID_ADPCM_MS ||
2016 enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
2017 samplesInChunk = enc->frame_size;
2018 } else if (trk->sampleSize)
2019 samplesInChunk = size/trk->sampleSize;
2023 /* copy extradata if it exists */
2024 if (trk->vosLen == 0 && enc->extradata_size > 0) {
2025 trk->vosLen = enc->extradata_size;
2026 trk->vosData = av_malloc(trk->vosLen);
2027 memcpy(trk->vosData, enc->extradata, trk->vosLen);
2030 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
2031 /* from x264 or from bytestream h264 */
2032 /* nal reformating needed */
2033 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2035 avio_write(pb, pkt->data, size);
2038 if ((enc->codec_id == CODEC_ID_DNXHD ||
2039 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
2040 /* copy frame to create needed atoms */
2042 trk->vosData = av_malloc(size);
2044 return AVERROR(ENOMEM);
2045 memcpy(trk->vosData, pkt->data, size);
2048 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
2049 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
2054 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2055 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
2056 trk->cluster[trk->entry].size = size;
2057 trk->cluster[trk->entry].entries = samplesInChunk;
2058 trk->cluster[trk->entry].dts = pkt->dts;
2059 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
2061 if (pkt->pts == AV_NOPTS_VALUE) {
2062 av_log(s, AV_LOG_WARNING, "pts has no value\n");
2063 pkt->pts = pkt->dts;
2065 if (pkt->dts != pkt->pts)
2066 trk->flags |= MOV_TRACK_CTTS;
2067 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2068 trk->cluster[trk->entry].flags = 0;
2069 if (pkt->flags & AV_PKT_FLAG_KEY) {
2070 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
2071 trk->entry > 0) { // force sync sample for the first key frame
2072 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2073 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2074 trk->flags |= MOV_TRACK_STPS;
2076 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2078 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2079 trk->hasKeyframes++;
2082 trk->sampleCount += samplesInChunk;
2083 mov->mdat_size += size;
2087 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2088 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry);
2092 // QuickTime chapters involve an additional text track with the chapter names
2093 // as samples, and a tref pointing from the other tracks to the chapter one.
2094 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2096 MOVMuxContext *mov = s->priv_data;
2097 MOVTrack *track = &mov->tracks[tracknum];
2098 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2101 track->mode = mov->mode;
2102 track->tag = MKTAG('t','e','x','t');
2103 track->timescale = MOV_TIMESCALE;
2104 track->enc = avcodec_alloc_context();
2105 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2107 for (i = 0; i < s->nb_chapters; i++) {
2108 AVChapter *c = s->chapters[i];
2109 AVDictionaryEntry *t;
2111 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
2112 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
2113 pkt.duration = end - pkt.dts;
2115 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2116 len = strlen(t->value);
2118 pkt.data = av_malloc(pkt.size);
2119 AV_WB16(pkt.data, len);
2120 memcpy(pkt.data+2, t->value, len);
2121 ff_mov_write_packet(s, &pkt);
2122 av_freep(&pkt.data);
2127 static int mov_write_header(AVFormatContext *s)
2129 AVIOContext *pb = s->pb;
2130 MOVMuxContext *mov = s->priv_data;
2131 int i, hint_track = 0;
2133 if (!s->pb->seekable) {
2134 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
2138 /* Default mode == MP4 */
2139 mov->mode = MODE_MP4;
2141 if (s->oformat != NULL) {
2142 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
2143 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
2144 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
2145 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
2146 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
2148 mov_write_ftyp_tag(pb,s);
2149 if (mov->mode == MODE_PSP) {
2150 if (s->nb_streams != 2) {
2151 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
2154 mov_write_uuidprof_tag(pb,s);
2158 mov->nb_streams = s->nb_streams;
2159 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
2160 mov->chapter_track = mov->nb_streams++;
2162 #if FF_API_FLAG_RTP_HINT
2163 if (s->flags & AVFMT_FLAG_RTP_HINT) {
2164 av_log(s, AV_LOG_WARNING, "The RTP_HINT flag is deprecated, enable it "
2165 "via the -movflags rtphint muxer option "
2167 mov->flags |= FF_MOV_FLAG_RTP_HINT;
2170 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2171 /* Add hint tracks for each audio and video stream */
2172 hint_track = mov->nb_streams;
2173 for (i = 0; i < s->nb_streams; i++) {
2174 AVStream *st = s->streams[i];
2175 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2176 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2182 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
2184 return AVERROR(ENOMEM);
2186 for(i=0; i<s->nb_streams; i++){
2187 AVStream *st= s->streams[i];
2188 MOVTrack *track= &mov->tracks[i];
2189 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
2191 track->enc = st->codec;
2192 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2193 if (track->language < 0)
2194 track->language = 0;
2195 track->mode = mov->mode;
2196 track->tag = mov_find_codec_tag(s, track);
2198 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
2199 "codec not currently supported in container\n", i);
2202 /* If hinting of this track is enabled by a later hint track,
2203 * this is updated. */
2204 track->hint_track = -1;
2205 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2206 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
2207 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
2208 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
2209 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
2210 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
2213 track->height = track->tag>>24 == 'n' ? 486 : 576;
2215 track->timescale = st->codec->time_base.den;
2216 if (track->mode == MODE_MOV && track->timescale > 100000)
2217 av_log(s, AV_LOG_WARNING,
2218 "WARNING codec timebase is very high. If duration is too long,\n"
2219 "file may not be playable by quicktime. Specify a shorter timebase\n"
2220 "or choose different container.\n");
2221 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
2222 track->timescale = st->codec->sample_rate;
2223 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
2224 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
2226 }else if(st->codec->codec_id == CODEC_ID_ADPCM_MS ||
2227 st->codec->codec_id == CODEC_ID_ADPCM_IMA_WAV){
2228 if (!st->codec->block_align) {
2229 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
2232 track->sampleSize = st->codec->block_align;
2233 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
2234 track->audio_vbr = 1;
2236 st->codec->frame_size = 1;
2237 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
2239 if (track->mode != MODE_MOV) {
2240 if (track->timescale > UINT16_MAX) {
2241 av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
2242 "sample rate %dhz\n", i, track->timescale);
2245 if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
2246 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
2247 i, track->enc->sample_rate);
2251 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
2252 track->timescale = st->codec->time_base.den;
2255 track->height = st->codec->height;
2257 av_set_pts_info(st, 64, 1, track->timescale);
2260 mov_write_mdat_tag(pb, mov);
2261 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
2263 if (mov->chapter_track)
2264 mov_create_chapter_track(s, mov->chapter_track);
2266 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2267 /* Initialize the hint tracks for each audio and video stream */
2268 for (i = 0; i < s->nb_streams; i++) {
2269 AVStream *st = s->streams[i];
2270 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2271 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2272 ff_mov_init_hinting(s, hint_track, i);
2282 av_freep(&mov->tracks);
2286 static int mov_write_trailer(AVFormatContext *s)
2288 MOVMuxContext *mov = s->priv_data;
2289 AVIOContext *pb = s->pb;
2293 int64_t moov_pos = avio_tell(pb);
2295 /* Write size of mdat tag */
2296 if (mov->mdat_size+8 <= UINT32_MAX) {
2297 avio_seek(pb, mov->mdat_pos, SEEK_SET);
2298 avio_wb32(pb, mov->mdat_size+8);
2300 /* overwrite 'wide' placeholder atom */
2301 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
2302 avio_wb32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
2303 ffio_wfourcc(pb, "mdat");
2304 avio_wb64(pb, mov->mdat_size+16);
2306 avio_seek(pb, moov_pos, SEEK_SET);
2308 mov_write_moov_tag(pb, mov, s);
2310 if (mov->chapter_track)
2311 av_freep(&mov->tracks[mov->chapter_track].enc);
2313 for (i=0; i<mov->nb_streams; i++) {
2314 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
2315 ff_mov_close_hinting(&mov->tracks[i]);
2316 av_freep(&mov->tracks[i].cluster);
2318 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
2324 av_freep(&mov->tracks);
2329 #if CONFIG_MOV_MUXER
2330 AVOutputFormat ff_mov_muxer = {
2332 NULL_IF_CONFIG_SMALL("MOV format"),
2335 sizeof(MOVMuxContext),
2339 ff_mov_write_packet,
2341 .flags = AVFMT_GLOBALHEADER,
2342 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
2343 .priv_class = &mov_muxer_class,
2346 #if CONFIG_TGP_MUXER
2347 AVOutputFormat ff_tgp_muxer = {
2349 NULL_IF_CONFIG_SMALL("3GP format"),
2352 sizeof(MOVMuxContext),
2356 ff_mov_write_packet,
2358 .flags = AVFMT_GLOBALHEADER,
2359 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2360 .priv_class = &mov_muxer_class,
2363 #if CONFIG_MP4_MUXER
2364 AVOutputFormat ff_mp4_muxer = {
2366 NULL_IF_CONFIG_SMALL("MP4 format"),
2369 sizeof(MOVMuxContext),
2373 ff_mov_write_packet,
2375 .flags = AVFMT_GLOBALHEADER,
2376 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2377 .priv_class = &mov_muxer_class,
2380 #if CONFIG_PSP_MUXER
2381 AVOutputFormat ff_psp_muxer = {
2383 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2386 sizeof(MOVMuxContext),
2390 ff_mov_write_packet,
2392 .flags = AVFMT_GLOBALHEADER,
2393 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2394 .priv_class = &mov_muxer_class,
2397 #if CONFIG_TG2_MUXER
2398 AVOutputFormat ff_tg2_muxer = {
2400 NULL_IF_CONFIG_SMALL("3GP2 format"),
2403 sizeof(MOVMuxContext),
2407 ff_mov_write_packet,
2409 .flags = AVFMT_GLOBALHEADER,
2410 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2411 .priv_class = &mov_muxer_class,
2414 #if CONFIG_IPOD_MUXER
2415 AVOutputFormat ff_ipod_muxer = {
2417 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2420 sizeof(MOVMuxContext),
2424 ff_mov_write_packet,
2426 .flags = AVFMT_GLOBALHEADER,
2427 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
2428 .priv_class = &mov_muxer_class,