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"
33 #include "libavcodec/vc1.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/intfloat.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/dict.h"
46 static const AVOption options[] = {
47 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
48 { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
49 { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
50 { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
51 { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
52 { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
53 { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
54 { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
55 { "faststart", "Run a second pass to put the moov at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
56 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
57 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
58 { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
59 { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
60 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
61 { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
62 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
63 { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
64 { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
68 #define MOV_CLASS(flavor)\
69 static const AVClass flavor ## _muxer_class = {\
70 .class_name = #flavor " muxer",\
71 .item_name = av_default_item_name,\
73 .version = LIBAVUTIL_VERSION_INT,\
76 //FIXME support 64 bit variant with wide placeholders
77 static int64_t update_size(AVIOContext *pb, int64_t pos)
79 int64_t curpos = avio_tell(pb);
80 avio_seek(pb, pos, SEEK_SET);
81 avio_wb32(pb, curpos - pos); /* rewrite size */
82 avio_seek(pb, curpos, SEEK_SET);
87 static int supports_edts(MOVMuxContext *mov)
89 // EDTS with fragments is tricky as we dont know the duration when its written
90 return (mov->use_editlist<0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) || mov->use_editlist>0;
93 static int is_co64_required(const MOVTrack *track)
97 for (i = 0; i < track->entry; i++) {
98 if (!track->cluster[i].chunkNum)
100 if (track->cluster[i].pos + track->data_offset > UINT32_MAX)
106 /* Chunk offset atom */
107 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
110 int mode64 = is_co64_required(track); // use 32 bit size variant if possible
111 int64_t pos = avio_tell(pb);
112 avio_wb32(pb, 0); /* size */
114 ffio_wfourcc(pb, "co64");
116 ffio_wfourcc(pb, "stco");
117 avio_wb32(pb, 0); /* version & flags */
118 avio_wb32(pb, track->chunkCount); /* entry count */
119 for (i=0; i<track->entry; i++) {
120 if(!track->cluster[i].chunkNum)
123 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
125 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
127 return update_size(pb, pos);
130 /* Sample size atom */
131 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
134 int i, j, entries = 0, tst = -1, oldtst = -1;
136 int64_t pos = avio_tell(pb);
137 avio_wb32(pb, 0); /* size */
138 ffio_wfourcc(pb, "stsz");
139 avio_wb32(pb, 0); /* version & flags */
141 for (i=0; i<track->entry; i++) {
142 tst = track->cluster[i].size/track->cluster[i].entries;
143 if(oldtst != -1 && tst != oldtst) {
147 entries += track->cluster[i].entries;
149 if (equalChunks && track->entry) {
150 int sSize = track->entry ? track->cluster[0].size/track->cluster[0].entries : 0;
151 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
152 avio_wb32(pb, sSize); // sample size
153 avio_wb32(pb, entries); // sample count
156 avio_wb32(pb, 0); // sample size
157 avio_wb32(pb, entries); // sample count
158 for (i=0; i<track->entry; i++) {
159 for (j=0; j<track->cluster[i].entries; j++) {
160 avio_wb32(pb, track->cluster[i].size /
161 track->cluster[i].entries);
165 return update_size(pb, pos);
168 /* Sample to chunk atom */
169 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
171 int index = 0, oldval = -1, i;
172 int64_t entryPos, curpos;
174 int64_t pos = avio_tell(pb);
175 avio_wb32(pb, 0); /* size */
176 ffio_wfourcc(pb, "stsc");
177 avio_wb32(pb, 0); // version & flags
178 entryPos = avio_tell(pb);
179 avio_wb32(pb, track->chunkCount); // entry count
180 for (i=0; i<track->entry; i++) {
181 if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum)
183 avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
184 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
185 avio_wb32(pb, 0x1); // sample description index
186 oldval = track->cluster[i].samples_in_chunk;
190 curpos = avio_tell(pb);
191 avio_seek(pb, entryPos, SEEK_SET);
192 avio_wb32(pb, index); // rewrite size
193 avio_seek(pb, curpos, SEEK_SET);
195 return update_size(pb, pos);
198 /* Sync sample atom */
199 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
201 int64_t curpos, entryPos;
203 int64_t pos = avio_tell(pb);
204 avio_wb32(pb, 0); // size
205 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
206 avio_wb32(pb, 0); // version & flags
207 entryPos = avio_tell(pb);
208 avio_wb32(pb, track->entry); // entry count
209 for (i=0; i<track->entry; i++) {
210 if (track->cluster[i].flags & flag) {
215 curpos = avio_tell(pb);
216 avio_seek(pb, entryPos, SEEK_SET);
217 avio_wb32(pb, index); // rewrite size
218 avio_seek(pb, curpos, SEEK_SET);
219 return update_size(pb, pos);
222 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
224 avio_wb32(pb, 0x11); /* size */
225 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
226 else ffio_wfourcc(pb, "damr");
227 ffio_wfourcc(pb, "FFMP");
228 avio_w8(pb, 0); /* decoder version */
230 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
231 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
232 avio_w8(pb, 0x01); /* Frames per sample */
236 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
241 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
243 if (track->vos_len < 7)
247 ffio_wfourcc(pb, "dac3");
249 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
250 fscod = get_bits(&gbc, 2);
251 frmsizecod = get_bits(&gbc, 6);
252 bsid = get_bits(&gbc, 5);
253 bsmod = get_bits(&gbc, 3);
254 acmod = get_bits(&gbc, 3);
256 skip_bits(&gbc, 2); // dsurmod
258 if ((acmod & 1) && acmod != 1)
259 skip_bits(&gbc, 2); // cmixlev
261 skip_bits(&gbc, 2); // surmixlev
263 lfeon = get_bits1(&gbc);
265 init_put_bits(&pbc, buf, sizeof(buf));
266 put_bits(&pbc, 2, fscod);
267 put_bits(&pbc, 5, bsid);
268 put_bits(&pbc, 3, bsmod);
269 put_bits(&pbc, 3, acmod);
270 put_bits(&pbc, 1, lfeon);
271 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
272 put_bits(&pbc, 5, 0); // reserved
274 flush_put_bits(&pbc);
275 avio_write(pb, buf, sizeof(buf));
281 * This function writes extradata "as is".
282 * Extradata must be formatted like a valid atom (with size and tag).
284 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
286 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
287 return track->enc->extradata_size;
290 static int mov_write_enda_tag(AVIOContext *pb)
293 ffio_wfourcc(pb, "enda");
294 avio_wb16(pb, 1); /* little endian */
298 static int mov_write_enda_tag_be(AVIOContext *pb)
301 ffio_wfourcc(pb, "enda");
302 avio_wb16(pb, 0); /* big endian */
306 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
311 avio_w8(pb, (size>>(7*i)) | 0x80);
312 avio_w8(pb, size & 0x7F);
315 static unsigned compute_avg_bitrate(MOVTrack *track)
319 if (!track->track_duration)
321 for (i = 0; i < track->entry; i++)
322 size += track->cluster[i].size;
323 return size * 8 * track->timescale / track->track_duration;
326 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
328 int64_t pos = avio_tell(pb);
329 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
330 unsigned avg_bitrate;
332 avio_wb32(pb, 0); // size
333 ffio_wfourcc(pb, "esds");
334 avio_wb32(pb, 0); // Version
337 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
338 avio_wb16(pb, track->track_id);
339 avio_w8(pb, 0x00); // flags (= no flags)
341 // DecoderConfig descriptor
342 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
344 // Object type indication
345 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
346 track->enc->codec_id == AV_CODEC_ID_MP3) &&
347 track->enc->sample_rate > 24000)
348 avio_w8(pb, 0x6B); // 11172-3
350 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
352 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
353 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
354 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
355 avio_w8(pb, 0x15); // flags (= Audiostream)
357 avio_w8(pb, 0x11); // flags (= Visualstream)
359 avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
360 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
362 avg_bitrate = compute_avg_bitrate(track);
363 // maxbitrate (FIXME should be max rate in any 1 sec window)
364 avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
365 avio_wb32(pb, avg_bitrate);
367 if (track->vos_len) {
368 // DecoderSpecific info descriptor
369 put_descr(pb, 0x05, track->vos_len);
370 avio_write(pb, track->vos_data, track->vos_len);
374 put_descr(pb, 0x06, 1);
376 return update_size(pb, pos);
379 static int mov_pcm_le_gt16(enum AVCodecID codec_id)
381 return codec_id == AV_CODEC_ID_PCM_S24LE ||
382 codec_id == AV_CODEC_ID_PCM_S32LE ||
383 codec_id == AV_CODEC_ID_PCM_F32LE ||
384 codec_id == AV_CODEC_ID_PCM_F64LE;
387 static int mov_pcm_be_gt16(enum AVCodecID codec_id)
389 return codec_id == AV_CODEC_ID_PCM_S24BE ||
390 codec_id == AV_CODEC_ID_PCM_S32BE ||
391 codec_id == AV_CODEC_ID_PCM_F32BE ||
392 codec_id == AV_CODEC_ID_PCM_F64BE;
395 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
397 int64_t pos = avio_tell(pb);
399 avio_wl32(pb, track->tag); // store it byteswapped
400 track->enc->codec_tag = av_bswap16(track->tag >> 16);
401 ff_put_wav_header(pb, track->enc);
402 return update_size(pb, pos);
405 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
407 int64_t pos = avio_tell(pb);
409 ffio_wfourcc(pb, "wfex");
410 ff_put_wav_header(pb, track->enc);
411 return update_size(pb, pos);
414 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
416 uint32_t layout_tag, bitmap;
417 int64_t pos = avio_tell(pb);
419 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
420 track->enc->channel_layout,
423 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
424 "lack of channel information\n");
428 avio_wb32(pb, 0); // Size
429 ffio_wfourcc(pb, "chan"); // Type
430 avio_w8(pb, 0); // Version
431 avio_wb24(pb, 0); // Flags
432 avio_wb32(pb, layout_tag); // mChannelLayoutTag
433 avio_wb32(pb, bitmap); // mChannelBitmap
434 avio_wb32(pb, 0); // mNumberChannelDescriptions
436 return update_size(pb, pos);
439 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
441 int64_t pos = avio_tell(pb);
443 avio_wb32(pb, 0); /* size */
444 ffio_wfourcc(pb, "wave");
446 if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
447 avio_wb32(pb, 12); /* size */
448 ffio_wfourcc(pb, "frma");
449 avio_wl32(pb, track->tag);
452 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
453 /* useless atom needed by mplayer, ipod, not needed by quicktime */
454 avio_wb32(pb, 12); /* size */
455 ffio_wfourcc(pb, "mp4a");
457 mov_write_esds_tag(pb, track);
458 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
459 mov_write_enda_tag(pb);
460 } else if (mov_pcm_be_gt16(track->enc->codec_id)) {
461 mov_write_enda_tag_be(pb);
462 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
463 mov_write_amr_tag(pb, track);
464 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
465 mov_write_ac3_tag(pb, track);
466 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
467 track->enc->codec_id == AV_CODEC_ID_QDM2) {
468 mov_write_extradata_tag(pb, track);
469 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
470 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
471 mov_write_ms_tag(pb, track);
474 avio_wb32(pb, 8); /* size */
475 avio_wb32(pb, 0); /* null tag */
477 return update_size(pb, pos);
480 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
483 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
484 int unescaped_size, seq_found = 0;
485 int level = 0, interlace = 0;
486 int packet_seq = track->vc1_info.packet_seq;
487 int packet_entry = track->vc1_info.packet_entry;
488 int slices = track->vc1_info.slices;
491 if (track->start_dts == AV_NOPTS_VALUE) {
492 /* No packets written yet, vc1_info isn't authoritative yet. */
493 /* Assume inline sequence and entry headers. This will be
494 * overwritten at the end if the file is seekable. */
495 packet_seq = packet_entry = 1;
498 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
500 return AVERROR(ENOMEM);
501 start = find_next_marker(track->vos_data, end);
502 for (next = start; next < end; start = next) {
505 next = find_next_marker(start + 4, end);
506 size = next - start - 4;
509 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
510 init_get_bits(&gb, unescaped, 8 * unescaped_size);
511 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
512 int profile = get_bits(&gb, 2);
513 if (profile != PROFILE_ADVANCED) {
515 return AVERROR(ENOSYS);
518 level = get_bits(&gb, 3);
519 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
521 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
522 skip_bits(&gb, 1); /* broadcast */
523 interlace = get_bits1(&gb);
524 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
529 return AVERROR(ENOSYS);
532 init_put_bits(&pbc, buf, 7);
533 /* VC1DecSpecStruc */
534 put_bits(&pbc, 4, 12); /* profile - advanced */
535 put_bits(&pbc, 3, level);
536 put_bits(&pbc, 1, 0); /* reserved */
537 /* VC1AdvDecSpecStruc */
538 put_bits(&pbc, 3, level);
539 put_bits(&pbc, 1, 0); /* cbr */
540 put_bits(&pbc, 6, 0); /* reserved */
541 put_bits(&pbc, 1, !interlace); /* no interlace */
542 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
543 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
544 put_bits(&pbc, 1, !slices); /* no slice code */
545 put_bits(&pbc, 1, 0); /* no bframe */
546 put_bits(&pbc, 1, 0); /* reserved */
547 put_bits32(&pbc, track->enc->time_base.den); /* framerate */
548 flush_put_bits(&pbc);
555 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
557 uint8_t buf[7] = { 0 };
560 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
563 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
564 ffio_wfourcc(pb, "dvc1");
565 track->vc1_info.struct_offset = avio_tell(pb);
566 avio_write(pb, buf, sizeof(buf));
567 avio_write(pb, track->vos_data, track->vos_len);
572 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
574 avio_wb32(pb, track->vos_len + 8);
575 ffio_wfourcc(pb, "glbl");
576 avio_write(pb, track->vos_data, track->vos_len);
577 return 8 + track->vos_len;
581 * Compute flags for 'lpcm' tag.
582 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
584 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
587 case AV_CODEC_ID_PCM_F32BE:
588 case AV_CODEC_ID_PCM_F64BE:
590 case AV_CODEC_ID_PCM_F32LE:
591 case AV_CODEC_ID_PCM_F64LE:
593 case AV_CODEC_ID_PCM_U8:
595 case AV_CODEC_ID_PCM_S16BE:
596 case AV_CODEC_ID_PCM_S24BE:
597 case AV_CODEC_ID_PCM_S32BE:
599 case AV_CODEC_ID_PCM_S8:
600 case AV_CODEC_ID_PCM_S16LE:
601 case AV_CODEC_ID_PCM_S24LE:
602 case AV_CODEC_ID_PCM_S32LE:
609 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
613 if (cluster_idx >= track->entry)
616 if (cluster_idx + 1 == track->entry)
617 next_dts = track->track_duration + track->start_dts;
619 next_dts = track->cluster[cluster_idx + 1].dts;
621 return next_dts - track->cluster[cluster_idx].dts;
624 static int get_samples_per_packet(MOVTrack *track)
626 int i, first_duration;
628 // return track->enc->frame_size;
630 /* use 1 for raw PCM */
631 if (!track->audio_vbr)
634 /* check to see if duration is constant for all clusters */
637 first_duration = get_cluster_duration(track, 0);
638 for (i = 1; i < track->entry; i++) {
639 if (get_cluster_duration(track, i) != first_duration)
642 return first_duration;
645 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
647 int64_t pos = avio_tell(pb);
649 uint32_t tag = track->tag;
651 if (track->mode == MODE_MOV) {
652 if (track->timescale > UINT16_MAX) {
653 if (mov_get_lpcm_flags(track->enc->codec_id))
654 tag = AV_RL32("lpcm");
656 } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
657 mov_pcm_be_gt16(track->enc->codec_id) ||
658 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
659 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
660 track->enc->codec_id == AV_CODEC_ID_QDM2) {
665 avio_wb32(pb, 0); /* size */
666 avio_wl32(pb, tag); // store it byteswapped
667 avio_wb32(pb, 0); /* Reserved */
668 avio_wb16(pb, 0); /* Reserved */
669 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
671 /* SoundDescription */
672 avio_wb16(pb, version); /* Version */
673 avio_wb16(pb, 0); /* Revision level */
674 avio_wb32(pb, 0); /* Reserved */
679 avio_wb16(pb, 0xfffe);
681 avio_wb32(pb, 0x00010000);
683 avio_wb64(pb, av_double2int(track->enc->sample_rate));
684 avio_wb32(pb, track->enc->channels);
685 avio_wb32(pb, 0x7F000000);
686 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
687 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
688 avio_wb32(pb, track->sample_size);
689 avio_wb32(pb, get_samples_per_packet(track));
691 if (track->mode == MODE_MOV) {
692 avio_wb16(pb, track->enc->channels);
693 if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
694 track->enc->codec_id == AV_CODEC_ID_PCM_S8)
695 avio_wb16(pb, 8); /* bits per sample */
698 avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
699 } else { /* reserved for mp4/3gp */
705 avio_wb16(pb, 0); /* packet size (= 0) */
706 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
707 track->enc->sample_rate : 0);
708 avio_wb16(pb, 0); /* Reserved */
711 if(version == 1) { /* SoundDescription V1 extended info */
712 if (mov_pcm_le_gt16(track->enc->codec_id) ||
713 mov_pcm_be_gt16(track->enc->codec_id))
714 avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
716 avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
717 avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
718 avio_wb32(pb, track->sample_size); /* Bytes per frame */
719 avio_wb32(pb, 2); /* Bytes per sample */
722 if(track->mode == MODE_MOV &&
723 (track->enc->codec_id == AV_CODEC_ID_AAC ||
724 track->enc->codec_id == AV_CODEC_ID_AC3 ||
725 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
726 track->enc->codec_id == AV_CODEC_ID_ALAC ||
727 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
728 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
729 track->enc->codec_id == AV_CODEC_ID_QDM2 ||
730 (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
731 (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
732 mov_write_wave_tag(pb, track);
733 else if(track->tag == MKTAG('m','p','4','a'))
734 mov_write_esds_tag(pb, track);
735 else if(track->enc->codec_id == AV_CODEC_ID_AMR_NB)
736 mov_write_amr_tag(pb, track);
737 else if(track->enc->codec_id == AV_CODEC_ID_AC3)
738 mov_write_ac3_tag(pb, track);
739 else if(track->enc->codec_id == AV_CODEC_ID_ALAC)
740 mov_write_extradata_tag(pb, track);
741 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
742 mov_write_wfex_tag(pb, track);
743 else if (track->vos_len > 0)
744 mov_write_glbl_tag(pb, track);
746 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
747 mov_write_chan_tag(pb, track);
749 return update_size(pb, pos);
752 static int mov_write_d263_tag(AVIOContext *pb)
754 avio_wb32(pb, 0xf); /* size */
755 ffio_wfourcc(pb, "d263");
756 ffio_wfourcc(pb, "FFMP");
757 avio_w8(pb, 0); /* decoder version */
758 /* FIXME use AVCodecContext level/profile, when encoder will set values */
759 avio_w8(pb, 0xa); /* level */
760 avio_w8(pb, 0); /* profile */
764 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
766 int64_t pos = avio_tell(pb);
769 ffio_wfourcc(pb, "avcC");
770 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
771 return update_size(pb, pos);
774 /* also used by all avid codecs (dv, imx, meridien) and their variants */
775 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
778 avio_wb32(pb, 24); /* size */
779 ffio_wfourcc(pb, "ACLR");
780 ffio_wfourcc(pb, "ACLR");
781 ffio_wfourcc(pb, "0001");
782 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
783 avio_wb32(pb, 0); /* unknown */
785 avio_wb32(pb, 24); /* size */
786 ffio_wfourcc(pb, "APRG");
787 ffio_wfourcc(pb, "APRG");
788 ffio_wfourcc(pb, "0001");
789 avio_wb32(pb, 1); /* unknown */
790 avio_wb32(pb, 0); /* unknown */
792 avio_wb32(pb, 120); /* size */
793 ffio_wfourcc(pb, "ARES");
794 ffio_wfourcc(pb, "ARES");
795 ffio_wfourcc(pb, "0001");
796 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
797 avio_wb32(pb, track->enc->width);
798 /* values below are based on samples created with quicktime and avid codecs */
799 if (track->vos_data[5] & 2) { // interlaced
800 avio_wb32(pb, track->enc->height/2);
801 avio_wb32(pb, 2); /* unknown */
802 avio_wb32(pb, 0); /* unknown */
803 avio_wb32(pb, 4); /* unknown */
805 avio_wb32(pb, track->enc->height);
806 avio_wb32(pb, 1); /* unknown */
807 avio_wb32(pb, 0); /* unknown */
808 if (track->enc->height == 1080)
809 avio_wb32(pb, 5); /* unknown */
811 avio_wb32(pb, 6); /* unknown */
814 for (i = 0; i < 10; i++)
817 /* extra padding for stsd needed */
822 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
824 int tag = track->enc->codec_tag;
826 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
829 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
830 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
831 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
832 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
833 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
834 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
835 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
840 static const AVCodecTag codec_ipod_tags[] = {
841 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
842 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
843 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
844 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
845 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
846 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
847 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
848 { AV_CODEC_ID_NONE, 0 },
851 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
853 int tag = track->enc->codec_tag;
855 // keep original tag for subs, ipod supports both formats
856 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
857 (tag == MKTAG('t','x','3','g') ||
858 tag == MKTAG('t','e','x','t'))))
859 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
861 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
862 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
863 "Quicktime/Ipod might not play the file\n");
868 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
872 if (track->enc->width == 720) { /* SD */
873 if (track->enc->height == 480) { /* NTSC */
874 if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
875 else tag = MKTAG('d','v','c',' ');
876 }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
877 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
878 else tag = MKTAG('d','v','p','p');
879 } else if (track->enc->height == 720) { /* HD 720 line */
880 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
881 else tag = MKTAG('d','v','h','p');
882 } else if (track->enc->height == 1080) { /* HD 1080 line */
883 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
884 else tag = MKTAG('d','v','h','6');
886 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
893 static const struct {
894 enum AVPixelFormat pix_fmt;
897 } mov_pix_fmt_tags[] = {
898 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
899 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
900 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
901 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
902 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
903 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
904 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
905 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
906 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
907 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
908 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
909 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
910 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
911 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
912 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
915 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
917 int tag = track->enc->codec_tag;
920 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
921 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
922 tag = mov_pix_fmt_tags[i].tag;
923 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
924 if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
932 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
934 int tag = track->enc->codec_tag;
936 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
937 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
938 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
939 track->enc->codec_id == AV_CODEC_ID_H263 ||
940 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
941 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
942 tag = mov_get_dv_codec_tag(s, track);
943 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
944 tag = mov_get_rawvideo_codec_tag(s, track);
945 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
946 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
947 if (!tag) { // if no mac fcc found, try with Microsoft tags
948 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
950 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
951 "the file may be unplayable!\n");
953 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
954 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
955 if (!tag) { // if no mac fcc found, try with Microsoft tags
956 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
958 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
959 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
960 "the file may be unplayable!\n");
963 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
964 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
970 static const AVCodecTag codec_3gp_tags[] = {
971 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
972 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
973 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
974 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
975 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
976 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
977 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
978 { AV_CODEC_ID_NONE, 0 },
981 static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
982 { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
983 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
984 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
985 { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
986 { AV_CODEC_ID_NONE, 0 },
989 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
993 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
994 tag = mp4_get_codec_tag(s, track);
995 else if (track->mode == MODE_ISM) {
996 tag = mp4_get_codec_tag(s, track);
997 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
998 tag = MKTAG('w', 'm', 'a', ' ');
999 } else if (track->mode == MODE_IPOD)
1000 tag = ipod_get_codec_tag(s, track);
1001 else if (track->mode & MODE_3GP)
1002 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
1003 else if (track->mode & MODE_F4V)
1004 tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
1006 tag = mov_get_codec_tag(s, track);
1011 /** Write uuid atom.
1012 * Needed to make file play in iPods running newest firmware
1013 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1015 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
1018 ffio_wfourcc(pb, "uuid");
1019 avio_wb32(pb, 0x6b6840f2);
1020 avio_wb32(pb, 0x5f244fc5);
1021 avio_wb32(pb, 0xba39a51b);
1022 avio_wb32(pb, 0xcf0323f3);
1027 static const uint16_t fiel_data[] = {
1028 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1031 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
1033 unsigned mov_field_order = 0;
1034 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
1035 mov_field_order = fiel_data[track->enc->field_order];
1039 ffio_wfourcc(pb, "fiel");
1040 avio_wb16(pb, mov_field_order);
1044 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
1046 int64_t pos = avio_tell(pb);
1047 avio_wb32(pb, 0); /* size */
1048 avio_wl32(pb, track->tag); // store it byteswapped
1049 avio_wb32(pb, 0); /* Reserved */
1050 avio_wb16(pb, 0); /* Reserved */
1051 avio_wb16(pb, 1); /* Data-reference index */
1053 if (track->enc->extradata_size)
1054 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1056 return update_size(pb, pos);
1059 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1062 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
1063 track->enc->sample_aspect_ratio.den, INT_MAX);
1066 ffio_wfourcc(pb, "pasp");
1067 avio_wb32(pb, sar.num);
1068 avio_wb32(pb, sar.den);
1072 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
1074 int64_t pos = avio_tell(pb);
1075 char compressor_name[32] = { 0 };
1077 avio_wb32(pb, 0); /* size */
1078 avio_wl32(pb, track->tag); // store it byteswapped
1079 avio_wb32(pb, 0); /* Reserved */
1080 avio_wb16(pb, 0); /* Reserved */
1081 avio_wb16(pb, 1); /* Data-reference index */
1083 avio_wb16(pb, 0); /* Codec stream version */
1084 avio_wb16(pb, 0); /* Codec stream revision (=0) */
1085 if (track->mode == MODE_MOV) {
1086 ffio_wfourcc(pb, "FFMP"); /* Vendor */
1087 if(track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1088 avio_wb32(pb, 0); /* Temporal Quality */
1089 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1091 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1092 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1095 avio_wb32(pb, 0); /* Reserved */
1096 avio_wb32(pb, 0); /* Reserved */
1097 avio_wb32(pb, 0); /* Reserved */
1099 avio_wb16(pb, track->enc->width); /* Video width */
1100 avio_wb16(pb, track->height); /* Video height */
1101 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1102 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1103 avio_wb32(pb, 0); /* Data size (= 0) */
1104 avio_wb16(pb, 1); /* Frame count (= 1) */
1106 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1107 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
1108 av_strlcpy(compressor_name,track->enc->codec->name,32);
1109 avio_w8(pb, strlen(compressor_name));
1110 avio_write(pb, compressor_name, 31);
1112 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1113 avio_wb16(pb, track->enc->bits_per_coded_sample);
1115 avio_wb16(pb, 0x18); /* Reserved */
1116 avio_wb16(pb, 0xffff); /* Reserved */
1117 if(track->tag == MKTAG('m','p','4','v'))
1118 mov_write_esds_tag(pb, track);
1119 else if(track->enc->codec_id == AV_CODEC_ID_H263)
1120 mov_write_d263_tag(pb);
1121 else if(track->enc->codec_id == AV_CODEC_ID_AVUI ||
1122 track->enc->codec_id == AV_CODEC_ID_SVQ3) {
1123 mov_write_extradata_tag(pb, track);
1125 } else if(track->enc->codec_id == AV_CODEC_ID_DNXHD)
1126 mov_write_avid_tag(pb, track);
1127 else if(track->enc->codec_id == AV_CODEC_ID_H264) {
1128 mov_write_avcc_tag(pb, track);
1129 if(track->mode == MODE_IPOD)
1130 mov_write_uuid_tag_ipod(pb);
1131 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1132 mov_write_fiel_tag(pb, track);
1133 else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1134 mov_write_dvc1_tag(pb, track);
1135 else if (track->vos_len > 0)
1136 mov_write_glbl_tag(pb, track);
1138 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1139 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1140 mov_write_pasp_tag(pb, track);
1143 return update_size(pb, pos);
1146 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1148 int64_t pos = avio_tell(pb);
1149 int frame_duration = av_rescale(track->timescale, track->enc->time_base.num, track->enc->time_base.den);
1150 int nb_frames = (track->timescale + frame_duration/2) / frame_duration;
1152 avio_wb32(pb, 0); /* size */
1153 ffio_wfourcc(pb, "tmcd"); /* Data format */
1154 avio_wb32(pb, 0); /* Reserved */
1155 avio_wb32(pb, 1); /* Data reference index */
1156 avio_wb32(pb, 0); /* Flags */
1157 avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
1158 avio_wb32(pb, track->timescale); /* Timescale */
1159 avio_wb32(pb, frame_duration); /* Frame duration */
1160 avio_w8(pb, nb_frames); /* Number of frames */
1161 avio_wb24(pb, 0); /* Reserved */
1162 /* TODO: source reference string */
1163 return update_size(pb, pos);
1166 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1168 int64_t pos = avio_tell(pb);
1169 avio_wb32(pb, 0); /* size */
1170 ffio_wfourcc(pb, "rtp ");
1171 avio_wb32(pb, 0); /* Reserved */
1172 avio_wb16(pb, 0); /* Reserved */
1173 avio_wb16(pb, 1); /* Data-reference index */
1175 avio_wb16(pb, 1); /* Hint track version */
1176 avio_wb16(pb, 1); /* Highest compatible version */
1177 avio_wb32(pb, track->max_packet_size); /* Max packet size */
1179 avio_wb32(pb, 12); /* size */
1180 ffio_wfourcc(pb, "tims");
1181 avio_wb32(pb, track->timescale);
1183 return update_size(pb, pos);
1186 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1188 int64_t pos = avio_tell(pb);
1189 avio_wb32(pb, 0); /* size */
1190 ffio_wfourcc(pb, "stsd");
1191 avio_wb32(pb, 0); /* version & flags */
1192 avio_wb32(pb, 1); /* entry count */
1193 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1194 mov_write_video_tag(pb, track);
1195 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1196 mov_write_audio_tag(pb, track);
1197 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1198 mov_write_subtitle_tag(pb, track);
1199 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1200 mov_write_rtp_tag(pb, track);
1201 else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1202 mov_write_tmcd_tag(pb, track);
1203 return update_size(pb, pos);
1206 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1208 MOVStts *ctts_entries;
1209 uint32_t entries = 0;
1213 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1214 ctts_entries[0].count = 1;
1215 ctts_entries[0].duration = track->cluster[0].cts;
1216 for (i=1; i<track->entry; i++) {
1217 if (track->cluster[i].cts == ctts_entries[entries].duration) {
1218 ctts_entries[entries].count++; /* compress */
1221 ctts_entries[entries].duration = track->cluster[i].cts;
1222 ctts_entries[entries].count = 1;
1225 entries++; /* last one */
1226 atom_size = 16 + (entries * 8);
1227 avio_wb32(pb, atom_size); /* size */
1228 ffio_wfourcc(pb, "ctts");
1229 avio_wb32(pb, 0); /* version & flags */
1230 avio_wb32(pb, entries); /* entry count */
1231 for (i=0; i<entries; i++) {
1232 avio_wb32(pb, ctts_entries[i].count);
1233 avio_wb32(pb, ctts_entries[i].duration);
1235 av_free(ctts_entries);
1239 /* Time to sample atom */
1240 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1242 MOVStts *stts_entries;
1243 uint32_t entries = -1;
1247 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1248 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1249 stts_entries[0].count = track->sample_count;
1250 stts_entries[0].duration = 1;
1253 stts_entries = track->entry ?
1254 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1256 for (i=0; i<track->entry; i++) {
1257 int duration = get_cluster_duration(track, i);
1258 if (i && duration == stts_entries[entries].duration) {
1259 stts_entries[entries].count++; /* compress */
1262 stts_entries[entries].duration = duration;
1263 stts_entries[entries].count = 1;
1266 entries++; /* last one */
1268 atom_size = 16 + (entries * 8);
1269 avio_wb32(pb, atom_size); /* size */
1270 ffio_wfourcc(pb, "stts");
1271 avio_wb32(pb, 0); /* version & flags */
1272 avio_wb32(pb, entries); /* entry count */
1273 for (i=0; i<entries; i++) {
1274 avio_wb32(pb, stts_entries[i].count);
1275 avio_wb32(pb, stts_entries[i].duration);
1277 av_free(stts_entries);
1281 static int mov_write_dref_tag(AVIOContext *pb)
1283 avio_wb32(pb, 28); /* size */
1284 ffio_wfourcc(pb, "dref");
1285 avio_wb32(pb, 0); /* version & flags */
1286 avio_wb32(pb, 1); /* entry count */
1288 avio_wb32(pb, 0xc); /* size */
1289 //FIXME add the alis and rsrc atom
1290 ffio_wfourcc(pb, "url ");
1291 avio_wb32(pb, 1); /* version & flags */
1296 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1298 int64_t pos = avio_tell(pb);
1299 avio_wb32(pb, 0); /* size */
1300 ffio_wfourcc(pb, "stbl");
1301 mov_write_stsd_tag(pb, track);
1302 mov_write_stts_tag(pb, track);
1303 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1304 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1305 track->has_keyframes && track->has_keyframes < track->entry)
1306 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1307 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1308 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1309 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1310 track->flags & MOV_TRACK_CTTS)
1311 mov_write_ctts_tag(pb, track);
1312 mov_write_stsc_tag(pb, track);
1313 mov_write_stsz_tag(pb, track);
1314 mov_write_stco_tag(pb, track);
1315 return update_size(pb, pos);
1318 static int mov_write_dinf_tag(AVIOContext *pb)
1320 int64_t pos = avio_tell(pb);
1321 avio_wb32(pb, 0); /* size */
1322 ffio_wfourcc(pb, "dinf");
1323 mov_write_dref_tag(pb);
1324 return update_size(pb, pos);
1327 static int mov_write_nmhd_tag(AVIOContext *pb)
1330 ffio_wfourcc(pb, "nmhd");
1335 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
1337 int64_t pos = avio_tell(pb);
1338 const char *font = "Lucida Grande";
1339 avio_wb32(pb, 0); /* size */
1340 ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
1341 avio_wb32(pb, 0); /* version & flags */
1342 avio_wb16(pb, 0); /* text font */
1343 avio_wb16(pb, 0); /* text face */
1344 avio_wb16(pb, 12); /* text size */
1345 avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
1346 avio_wb16(pb, 0x0000); /* text color (red) */
1347 avio_wb16(pb, 0x0000); /* text color (green) */
1348 avio_wb16(pb, 0x0000); /* text color (blue) */
1349 avio_wb16(pb, 0xffff); /* background color (red) */
1350 avio_wb16(pb, 0xffff); /* background color (green) */
1351 avio_wb16(pb, 0xffff); /* background color (blue) */
1352 avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
1353 avio_write(pb, font, strlen(font)); /* font name */
1354 return update_size(pb, pos);
1357 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
1359 int64_t pos = avio_tell(pb);
1360 avio_wb32(pb, 0); /* size */
1361 ffio_wfourcc(pb, "gmhd");
1362 avio_wb32(pb, 0x18); /* gmin size */
1363 ffio_wfourcc(pb, "gmin");/* generic media info */
1364 avio_wb32(pb, 0); /* version & flags */
1365 avio_wb16(pb, 0x40); /* graphics mode = */
1366 avio_wb16(pb, 0x8000); /* opColor (r?) */
1367 avio_wb16(pb, 0x8000); /* opColor (g?) */
1368 avio_wb16(pb, 0x8000); /* opColor (b?) */
1369 avio_wb16(pb, 0); /* balance */
1370 avio_wb16(pb, 0); /* reserved */
1373 * This special text atom is required for
1374 * Apple Quicktime chapters. The contents
1375 * don't appear to be documented, so the
1376 * bytes are copied verbatim.
1378 if (track->tag != MKTAG('c','6','0','8')) {
1379 avio_wb32(pb, 0x2C); /* size */
1380 ffio_wfourcc(pb, "text");
1381 avio_wb16(pb, 0x01);
1382 avio_wb32(pb, 0x00);
1383 avio_wb32(pb, 0x00);
1384 avio_wb32(pb, 0x00);
1385 avio_wb32(pb, 0x01);
1386 avio_wb32(pb, 0x00);
1387 avio_wb32(pb, 0x00);
1388 avio_wb32(pb, 0x00);
1389 avio_wb32(pb, 0x00004000);
1390 avio_wb16(pb, 0x0000);
1393 if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1394 int64_t tmcd_pos = avio_tell(pb);
1395 avio_wb32(pb, 0); /* size */
1396 ffio_wfourcc(pb, "tmcd");
1397 mov_write_tcmi_tag(pb, track);
1398 update_size(pb, tmcd_pos);
1400 return update_size(pb, pos);
1403 static int mov_write_smhd_tag(AVIOContext *pb)
1405 avio_wb32(pb, 16); /* size */
1406 ffio_wfourcc(pb, "smhd");
1407 avio_wb32(pb, 0); /* version & flags */
1408 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1409 avio_wb16(pb, 0); /* reserved */
1413 static int mov_write_vmhd_tag(AVIOContext *pb)
1415 avio_wb32(pb, 0x14); /* size (always 0x14) */
1416 ffio_wfourcc(pb, "vmhd");
1417 avio_wb32(pb, 0x01); /* version & flags */
1418 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1422 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1424 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1425 int64_t pos = avio_tell(pb);
1427 if (!track) { /* no media --> data handler */
1430 descr = "DataHandler";
1432 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1433 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1435 descr = "VideoHandler";
1436 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1438 descr = "SoundHandler";
1439 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1440 if (track->tag == MKTAG('c','6','0','8')) {
1442 descr = "ClosedCaptionHandler";
1444 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1445 else hdlr_type = "text";
1446 descr = "SubtitleHandler";
1448 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1450 descr = "TimeCodeHandler";
1451 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1453 descr = "HintHandler";
1457 descr = "DataHandler";
1461 avio_wb32(pb, 0); /* size */
1462 ffio_wfourcc(pb, "hdlr");
1463 avio_wb32(pb, 0); /* Version & flags */
1464 avio_write(pb, hdlr, 4); /* handler */
1465 ffio_wfourcc(pb, hdlr_type); /* handler type */
1466 avio_wb32(pb ,0); /* reserved */
1467 avio_wb32(pb ,0); /* reserved */
1468 avio_wb32(pb ,0); /* reserved */
1469 if (!track || track->mode == MODE_MOV)
1470 avio_w8(pb, strlen(descr)); /* pascal string */
1471 avio_write(pb, descr, strlen(descr)); /* handler description */
1472 if (track && track->mode != MODE_MOV)
1473 avio_w8(pb, 0); /* c string */
1474 return update_size(pb, pos);
1477 static int mov_write_hmhd_tag(AVIOContext *pb)
1479 /* This atom must be present, but leaving the values at zero
1480 * seems harmless. */
1481 avio_wb32(pb, 28); /* size */
1482 ffio_wfourcc(pb, "hmhd");
1483 avio_wb32(pb, 0); /* version, flags */
1484 avio_wb16(pb, 0); /* maxPDUsize */
1485 avio_wb16(pb, 0); /* avgPDUsize */
1486 avio_wb32(pb, 0); /* maxbitrate */
1487 avio_wb32(pb, 0); /* avgbitrate */
1488 avio_wb32(pb, 0); /* reserved */
1492 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1494 int64_t pos = avio_tell(pb);
1495 avio_wb32(pb, 0); /* size */
1496 ffio_wfourcc(pb, "minf");
1497 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1498 mov_write_vmhd_tag(pb);
1499 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1500 mov_write_smhd_tag(pb);
1501 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1502 if (track->tag == MKTAG('t','e','x','t') || track->tag == MKTAG('c','6','0','8')) {
1503 mov_write_gmhd_tag(pb, track);
1505 mov_write_nmhd_tag(pb);
1507 } else if (track->tag == MKTAG('t','m','c','d')) {
1508 mov_write_gmhd_tag(pb, track);
1509 } else if (track->tag == MKTAG('r','t','p',' ')) {
1510 mov_write_hmhd_tag(pb);
1512 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1513 mov_write_hdlr_tag(pb, NULL);
1514 mov_write_dinf_tag(pb);
1515 mov_write_stbl_tag(pb, track);
1516 return update_size(pb, pos);
1519 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1521 int version = track->track_duration < INT32_MAX ? 0 : 1;
1523 if (track->mode == MODE_ISM)
1526 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1527 ffio_wfourcc(pb, "mdhd");
1528 avio_w8(pb, version);
1529 avio_wb24(pb, 0); /* flags */
1531 avio_wb64(pb, track->time);
1532 avio_wb64(pb, track->time);
1534 avio_wb32(pb, track->time); /* creation time */
1535 avio_wb32(pb, track->time); /* modification time */
1537 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1539 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1541 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1542 avio_wb16(pb, track->language); /* language */
1543 avio_wb16(pb, 0); /* reserved (quality) */
1545 if(version!=0 && track->mode == MODE_MOV){
1546 av_log(NULL, AV_LOG_ERROR,
1547 "FATAL error, file duration too long for timebase, this file will not be\n"
1548 "playable with quicktime. Choose a different timebase or a different\n"
1549 "container format\n");
1555 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1557 int64_t pos = avio_tell(pb);
1558 avio_wb32(pb, 0); /* size */
1559 ffio_wfourcc(pb, "mdia");
1560 mov_write_mdhd_tag(pb, track);
1561 mov_write_hdlr_tag(pb, track);
1562 mov_write_minf_tag(pb, track);
1563 return update_size(pb, pos);
1566 /* transformation matrix
1570 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
1571 int16_t d, int16_t tx, int16_t ty)
1573 avio_wb32(pb, a << 16); /* 16.16 format */
1574 avio_wb32(pb, b << 16); /* 16.16 format */
1575 avio_wb32(pb, 0); /* u in 2.30 format */
1576 avio_wb32(pb, c << 16); /* 16.16 format */
1577 avio_wb32(pb, d << 16); /* 16.16 format */
1578 avio_wb32(pb, 0); /* v in 2.30 format */
1579 avio_wb32(pb, tx << 16); /* 16.16 format */
1580 avio_wb32(pb, ty << 16); /* 16.16 format */
1581 avio_wb32(pb, 1 << 30); /* w in 2.30 format */
1584 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1586 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1587 track->timescale, AV_ROUND_UP);
1588 int version = duration < INT32_MAX ? 0 : 1;
1591 if (track->mode == MODE_ISM)
1594 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1595 ffio_wfourcc(pb, "tkhd");
1596 avio_w8(pb, version);
1597 avio_wb24(pb, 0xf); /* flags (track enabled) */
1599 avio_wb64(pb, track->time);
1600 avio_wb64(pb, track->time);
1602 avio_wb32(pb, track->time); /* creation time */
1603 avio_wb32(pb, track->time); /* modification time */
1605 avio_wb32(pb, track->track_id); /* track-id */
1606 avio_wb32(pb, 0); /* reserved */
1608 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1610 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1612 avio_wb32(pb, 0); /* reserved */
1613 avio_wb32(pb, 0); /* reserved */
1614 avio_wb16(pb, 0); /* layer */
1615 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1616 /* Volume, only for audio */
1617 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1618 avio_wb16(pb, 0x0100);
1621 avio_wb16(pb, 0); /* reserved */
1623 /* Matrix structure */
1624 if (st && st->metadata) {
1625 AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
1626 rotation = (rot && rot->value) ? atoi(rot->value) : 0;
1628 if (rotation == 90) {
1629 write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
1630 } else if (rotation == 180) {
1631 write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
1632 } else if (rotation == 270) {
1633 write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
1635 write_matrix(pb, 1, 0, 0, 1, 0, 0);
1637 /* Track width and height, for visual only */
1638 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1639 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1640 if(track->mode == MODE_MOV) {
1641 avio_wb32(pb, track->enc->width << 16);
1642 avio_wb32(pb, track->height << 16);
1644 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1645 if(!sample_aspect_ratio || track->height != track->enc->height)
1646 sample_aspect_ratio = 1;
1647 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1648 avio_wb32(pb, track->height*0x10000);
1658 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1660 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1661 track->enc->sample_aspect_ratio.den);
1663 int64_t pos = avio_tell(pb);
1665 avio_wb32(pb, 0); /* size */
1666 ffio_wfourcc(pb, "tapt");
1669 ffio_wfourcc(pb, "clef");
1671 avio_wb32(pb, width << 16);
1672 avio_wb32(pb, track->enc->height << 16);
1675 ffio_wfourcc(pb, "prof");
1677 avio_wb32(pb, width << 16);
1678 avio_wb32(pb, track->enc->height << 16);
1681 ffio_wfourcc(pb, "enof");
1683 avio_wb32(pb, track->enc->width << 16);
1684 avio_wb32(pb, track->enc->height << 16);
1686 return update_size(pb, pos);
1689 // This box seems important for the psp playback ... without it the movie seems to hang
1690 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1692 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1693 track->timescale, AV_ROUND_UP);
1694 int version = duration < INT32_MAX ? 0 : 1;
1695 int entry_size, entry_count, size;
1696 int64_t delay, start_ct = track->cluster[0].cts;
1697 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1698 track->timescale, AV_ROUND_DOWN);
1699 version |= delay < INT32_MAX ? 0 : 1;
1701 entry_size = (version == 1) ? 20 : 12;
1702 entry_count = 1 + (delay > 0);
1703 size = 24 + entry_count * entry_size;
1705 /* write the atom data */
1706 avio_wb32(pb, size);
1707 ffio_wfourcc(pb, "edts");
1708 avio_wb32(pb, size - 8);
1709 ffio_wfourcc(pb, "elst");
1710 avio_w8(pb, version);
1711 avio_wb24(pb, 0); /* flags */
1713 avio_wb32(pb, entry_count);
1714 if (delay > 0) { /* add an empty edit to delay presentation */
1716 avio_wb64(pb, delay);
1719 avio_wb32(pb, delay);
1722 avio_wb32(pb, 0x00010000);
1724 av_assert0(track->cluster[0].dts <= 0);
1725 start_ct = -track->cluster[0].dts;
1731 avio_wb64(pb, duration);
1732 avio_wb64(pb, start_ct);
1734 avio_wb32(pb, duration);
1735 avio_wb32(pb, start_ct);
1737 avio_wb32(pb, 0x00010000);
1741 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1743 avio_wb32(pb, 20); // size
1744 ffio_wfourcc(pb, "tref");
1745 avio_wb32(pb, 12); // size (subatom)
1746 avio_wl32(pb, track->tref_tag);
1747 avio_wb32(pb, track->tref_id);
1751 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1752 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1754 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1755 ffio_wfourcc(pb, "uuid");
1756 ffio_wfourcc(pb, "USMT");
1757 avio_wb32(pb, 0x21d24fce);
1758 avio_wb32(pb, 0xbb88695c);
1759 avio_wb32(pb, 0xfac9c740);
1760 avio_wb32(pb, 0x1c); // another size here!
1761 ffio_wfourcc(pb, "MTDT");
1762 avio_wb32(pb, 0x00010012);
1763 avio_wb32(pb, 0x0a);
1764 avio_wb32(pb, 0x55c40000);
1770 static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
1772 char buf[1000] = "";
1775 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
1776 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1779 avio_wb32(pb, len + 24);
1780 ffio_wfourcc(pb, "udta");
1781 avio_wb32(pb, len + 16);
1782 ffio_wfourcc(pb, "hnti");
1783 avio_wb32(pb, len + 8);
1784 ffio_wfourcc(pb, "sdp ");
1785 avio_write(pb, buf, len);
1789 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1790 MOVTrack *track, AVStream *st)
1792 int64_t pos = avio_tell(pb);
1793 avio_wb32(pb, 0); /* size */
1794 ffio_wfourcc(pb, "trak");
1795 mov_write_tkhd_tag(pb, track, st);
1796 if (supports_edts(mov))
1797 mov_write_edts_tag(pb, track); // PSP Movies and several other cases require edts box
1798 if (track->tref_tag)
1799 mov_write_tref_tag(pb, track);
1800 mov_write_mdia_tag(pb, track);
1801 if (track->mode == MODE_PSP)
1802 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1803 if (track->tag == MKTAG('r','t','p',' '))
1804 mov_write_udta_sdp(pb, track->rtp_ctx, track->track_id);
1805 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1806 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1807 if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
1808 mov_write_tapt_tag(pb, track);
1810 return update_size(pb, pos);
1813 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1815 int i, has_audio = 0, has_video = 0;
1816 int64_t pos = avio_tell(pb);
1817 int audio_profile = mov->iods_audio_profile;
1818 int video_profile = mov->iods_video_profile;
1819 for (i = 0; i < mov->nb_streams; i++) {
1820 if(mov->tracks[i].entry > 0) {
1821 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1822 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1825 if (audio_profile < 0)
1826 audio_profile = 0xFF - has_audio;
1827 if (video_profile < 0)
1828 video_profile = 0xFF - has_video;
1829 avio_wb32(pb, 0x0); /* size */
1830 ffio_wfourcc(pb, "iods");
1831 avio_wb32(pb, 0); /* version & flags */
1832 put_descr(pb, 0x10, 7);
1833 avio_wb16(pb, 0x004f);
1836 avio_w8(pb, audio_profile);
1837 avio_w8(pb, video_profile);
1839 return update_size(pb, pos);
1842 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1844 avio_wb32(pb, 0x20); /* size */
1845 ffio_wfourcc(pb, "trex");
1846 avio_wb32(pb, 0); /* version & flags */
1847 avio_wb32(pb, track->track_id); /* track ID */
1848 avio_wb32(pb, 1); /* default sample description index */
1849 avio_wb32(pb, 0); /* default sample duration */
1850 avio_wb32(pb, 0); /* default sample size */
1851 avio_wb32(pb, 0); /* default sample flags */
1855 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1857 int64_t pos = avio_tell(pb);
1859 avio_wb32(pb, 0x0); /* size */
1860 ffio_wfourcc(pb, "mvex");
1861 for (i = 0; i < mov->nb_streams; i++)
1862 mov_write_trex_tag(pb, &mov->tracks[i]);
1863 return update_size(pb, pos);
1866 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1868 int max_track_id = 1, i;
1869 int64_t max_track_len_temp, max_track_len = 0;
1872 for (i=0; i<mov->nb_streams; i++) {
1873 if(mov->tracks[i].entry > 0) {
1874 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1876 mov->tracks[i].timescale,
1878 if (max_track_len < max_track_len_temp)
1879 max_track_len = max_track_len_temp;
1880 if (max_track_id < mov->tracks[i].track_id)
1881 max_track_id = mov->tracks[i].track_id;
1885 version = max_track_len < UINT32_MAX ? 0 : 1;
1886 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1887 ffio_wfourcc(pb, "mvhd");
1888 avio_w8(pb, version);
1889 avio_wb24(pb, 0); /* flags */
1891 avio_wb64(pb, mov->time);
1892 avio_wb64(pb, mov->time);
1894 avio_wb32(pb, mov->time); /* creation time */
1895 avio_wb32(pb, mov->time); /* modification time */
1897 avio_wb32(pb, MOV_TIMESCALE);
1898 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1900 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1901 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1902 avio_wb16(pb, 0); /* reserved */
1903 avio_wb32(pb, 0); /* reserved */
1904 avio_wb32(pb, 0); /* reserved */
1906 /* Matrix structure */
1907 write_matrix(pb, 1, 0, 0, 1, 0, 0);
1909 avio_wb32(pb, 0); /* reserved (preview time) */
1910 avio_wb32(pb, 0); /* reserved (preview duration) */
1911 avio_wb32(pb, 0); /* reserved (poster time) */
1912 avio_wb32(pb, 0); /* reserved (selection time) */
1913 avio_wb32(pb, 0); /* reserved (selection duration) */
1914 avio_wb32(pb, 0); /* reserved (current time) */
1915 avio_wb32(pb, max_track_id + 1); /* Next track id */
1919 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1922 avio_wb32(pb, 33); /* size */
1923 ffio_wfourcc(pb, "hdlr");
1926 ffio_wfourcc(pb, "mdir");
1927 ffio_wfourcc(pb, "appl");
1934 /* helper function to write a data tag with the specified string as data */
1935 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1938 int size = 16 + strlen(data);
1939 avio_wb32(pb, size); /* size */
1940 ffio_wfourcc(pb, "data");
1943 avio_write(pb, data, strlen(data));
1947 lang = ff_mov_iso639_to_lang("und", 1);
1948 avio_wb16(pb, strlen(data)); /* string length */
1949 avio_wb16(pb, lang);
1950 avio_write(pb, data, strlen(data));
1951 return strlen(data) + 4;
1955 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1957 if (value && value[0]) {
1958 int64_t pos = avio_tell(pb);
1959 avio_wb32(pb, 0); /* size */
1960 ffio_wfourcc(pb, name);
1961 mov_write_string_data_tag(pb, value, lang, long_style);
1962 size = update_size(pb, pos);
1967 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1968 const char *name, const char *tag,
1971 int l, lang = 0, len, len2;
1972 AVDictionaryEntry *t, *t2 = NULL;
1975 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1978 len = strlen(t->key);
1979 snprintf(tag2, sizeof(tag2), "%s-", tag);
1980 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1981 len2 = strlen(t2->key);
1982 if (len2 == len+4 && !strcmp(t->value, t2->value)
1983 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1988 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1991 /* iTunes bpm number */
1992 static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
1994 AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
1995 int size = 0, tmpo = t ? atoi(t->value) : 0;
1998 avio_wb32(pb, size);
1999 ffio_wfourcc(pb, "tmpo");
2000 avio_wb32(pb, size-8); /* size */
2001 ffio_wfourcc(pb, "data");
2002 avio_wb32(pb, 0x15); //type specifier
2004 avio_wb16(pb, tmpo); // data
2009 /* iTunes track number */
2010 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
2013 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
2014 int size = 0, track = t ? atoi(t->value) : 0;
2016 avio_wb32(pb, 32); /* size */
2017 ffio_wfourcc(pb, "trkn");
2018 avio_wb32(pb, 24); /* size */
2019 ffio_wfourcc(pb, "data");
2020 avio_wb32(pb, 0); // 8 bytes empty
2022 avio_wb16(pb, 0); // empty
2023 avio_wb16(pb, track); // track number
2024 avio_wb16(pb, 0); // total track number
2025 avio_wb16(pb, 0); // empty
2031 static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
2032 const char *name, const char *tag,
2035 AVDictionaryEntry *t = NULL;
2038 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2040 num = t ? atoi(t->value) : 0;
2042 avio_wb32(pb, len+8);
2043 ffio_wfourcc(pb, name);
2044 if (len==4) avio_wb32(pb, num);
2045 else avio_w8 (pb, num);
2049 /* iTunes meta data list */
2050 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
2053 int64_t pos = avio_tell(pb);
2054 avio_wb32(pb, 0); /* size */
2055 ffio_wfourcc(pb, "ilst");
2056 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
2057 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
2058 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
2059 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
2060 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
2061 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
2062 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
2063 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
2064 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
2065 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2066 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
2067 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
2068 mov_write_string_metadata(s, pb, "desc", "description",1);
2069 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
2070 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
2071 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
2072 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
2073 mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
2074 mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
2075 mov_write_int8_metadata (s, pb, "stik", "media_type",1);
2076 mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
2077 mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
2078 mov_write_trkn_tag(pb, mov, s);
2079 mov_write_tmpo_tag(pb, s);
2080 return update_size(pb, pos);
2083 /* iTunes meta data tag */
2084 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
2088 int64_t pos = avio_tell(pb);
2089 avio_wb32(pb, 0); /* size */
2090 ffio_wfourcc(pb, "meta");
2092 mov_write_itunes_hdlr_tag(pb, mov, s);
2093 mov_write_ilst_tag(pb, mov, s);
2094 size = update_size(pb, pos);
2098 static int utf8len(const uint8_t *b)
2103 GET_UTF8(val, *b++, return -1;)
2109 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2113 GET_UTF8(val, *b++, return -1;)
2116 avio_wb16(pb, 0x00);
2120 static uint16_t language_code(const char *str)
2122 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
2125 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
2126 const char *tag, const char *str)
2128 int64_t pos = avio_tell(pb);
2129 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
2130 if (!t || !utf8len(t->value))
2132 avio_wb32(pb, 0); /* size */
2133 ffio_wfourcc(pb, tag); /* type */
2134 avio_wb32(pb, 0); /* version + flags */
2135 if (!strcmp(tag, "yrrc"))
2136 avio_wb16(pb, atoi(t->value));
2138 avio_wb16(pb, language_code("eng")); /* language */
2139 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
2140 if (!strcmp(tag, "albm") &&
2141 (t = av_dict_get(s->metadata, "track", NULL, 0)))
2142 avio_w8(pb, atoi(t->value));
2144 return update_size(pb, pos);
2147 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
2149 int64_t pos = avio_tell(pb);
2150 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
2152 avio_wb32(pb, 0); // size
2153 ffio_wfourcc(pb, "chpl");
2154 avio_wb32(pb, 0x01000000); // version + flags
2155 avio_wb32(pb, 0); // unknown
2156 avio_w8(pb, nb_chapters);
2158 for (i = 0; i < nb_chapters; i++) {
2159 AVChapter *c = s->chapters[i];
2160 AVDictionaryEntry *t;
2161 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
2163 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2164 int len = FFMIN(strlen(t->value), 255);
2166 avio_write(pb, t->value, len);
2170 return update_size(pb, pos);
2173 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
2176 AVIOContext *pb_buf;
2180 for (i = 0; i < s->nb_streams; i++)
2181 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
2185 ret = avio_open_dyn_buf(&pb_buf);
2189 if (mov->mode & MODE_3GP) {
2190 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2191 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2192 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2193 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2194 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2195 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2196 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2197 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2198 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2199 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
2200 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
2201 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
2202 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
2203 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
2204 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
2205 // currently ignored by mov.c
2206 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
2207 // add support for libquicktime, this atom is also actually read by mov.c
2208 mov_write_string_metadata(s, pb_buf, "\251cmt", "comment" , 0);
2209 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
2210 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
2212 /* iTunes meta data */
2213 mov_write_meta_tag(pb_buf, mov, s);
2217 mov_write_chpl_tag(pb_buf, s);
2219 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2220 avio_wb32(pb, size+8);
2221 ffio_wfourcc(pb, "udta");
2222 avio_write(pb, buf, size);
2229 static void mov_write_psp_udta_tag(AVIOContext *pb,
2230 const char *str, const char *lang, int type)
2232 int len = utf8len(str)+1;
2235 avio_wb16(pb, len*2+10); /* size */
2236 avio_wb32(pb, type); /* type */
2237 avio_wb16(pb, language_code(lang)); /* language */
2238 avio_wb16(pb, 0x01); /* ? */
2239 ascii_to_wc(pb, str);
2242 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
2244 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2248 pos = avio_tell(pb);
2249 avio_wb32(pb, 0); /* size placeholder*/
2250 ffio_wfourcc(pb, "uuid");
2251 ffio_wfourcc(pb, "USMT");
2252 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2253 avio_wb32(pb, 0xbb88695c);
2254 avio_wb32(pb, 0xfac9c740);
2256 pos2 = avio_tell(pb);
2257 avio_wb32(pb, 0); /* size placeholder*/
2258 ffio_wfourcc(pb, "MTDT");
2262 avio_wb16(pb, 0x0C); /* size */
2263 avio_wb32(pb, 0x0B); /* type */
2264 avio_wb16(pb, language_code("und")); /* language */
2265 avio_wb16(pb, 0x0); /* ? */
2266 avio_wb16(pb, 0x021C); /* data */
2268 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2269 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2270 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2272 update_size(pb, pos2);
2273 return update_size(pb, pos);
2279 static void build_chunks(MOVTrack *trk)
2282 MOVIentry *chunk= &trk->cluster[0];
2283 uint64_t chunkSize = chunk->size;
2285 if (trk->chunkCount)
2288 for(i=1; i<trk->entry; i++){
2289 if(chunk->pos + chunkSize == trk->cluster[i].pos &&
2290 chunkSize + trk->cluster[i].size < (1<<20)){
2291 chunkSize += trk->cluster[i].size;
2292 chunk->samples_in_chunk += trk->cluster[i].entries;
2294 trk->cluster[i].chunkNum = chunk->chunkNum+1;
2295 chunk=&trk->cluster[i];
2296 chunkSize = chunk->size;
2302 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2306 int64_t pos = avio_tell(pb);
2307 avio_wb32(pb, 0); /* size placeholder*/
2308 ffio_wfourcc(pb, "moov");
2310 for (i=0; i<mov->nb_streams; i++) {
2311 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2314 mov->tracks[i].time = mov->time;
2315 mov->tracks[i].track_id = i+1;
2317 if (mov->tracks[i].entry)
2318 build_chunks(&mov->tracks[i]);
2321 if (mov->chapter_track)
2322 for (i=0; i<s->nb_streams; i++) {
2323 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2324 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2326 for (i = 0; i < mov->nb_streams; i++) {
2327 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2328 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2329 mov->tracks[i].tref_id =
2330 mov->tracks[mov->tracks[i].src_track].track_id;
2333 for (i = 0; i < mov->nb_streams; i++) {
2334 if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
2335 int src_trk = mov->tracks[i].src_track;
2336 mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
2337 mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
2338 mov->tracks[i].track_duration = mov->tracks[src_trk].track_duration;
2342 mov_write_mvhd_tag(pb, mov);
2343 if (mov->mode != MODE_MOV && !mov->iods_skip)
2344 mov_write_iods_tag(pb, mov);
2345 for (i=0; i<mov->nb_streams; i++) {
2346 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2347 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2350 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2351 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2353 if (mov->mode == MODE_PSP)
2354 mov_write_uuidusmt_tag(pb, s);
2356 mov_write_udta_tag(pb, mov, s);
2358 return update_size(pb, pos);
2361 static void param_write_int(AVIOContext *pb, const char *name, int value)
2363 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2366 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2368 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2371 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2374 len = FFMIN(sizeof(buf)/2 - 1, len);
2375 ff_data_to_hex(buf, value, len, 0);
2377 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2380 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2382 int64_t pos = avio_tell(pb);
2384 const uint8_t uuid[] = {
2385 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2386 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2390 ffio_wfourcc(pb, "uuid");
2391 avio_write(pb, uuid, sizeof(uuid));
2394 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2395 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2396 avio_printf(pb, "<head>\n");
2397 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2399 avio_printf(pb, "</head>\n");
2400 avio_printf(pb, "<body>\n");
2401 avio_printf(pb, "<switch>\n");
2402 for (i = 0; i < mov->nb_streams; i++) {
2403 MOVTrack *track = &mov->tracks[i];
2405 /* track->track_id is initialized in write_moov, and thus isn't known
2407 int track_id = i + 1;
2409 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2411 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2416 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2417 track->enc->bit_rate);
2418 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2419 param_write_int(pb, "trackID", track_id);
2420 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2421 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2423 int size = track->enc->extradata_size;
2424 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2426 param_write_hex(pb, "CodecPrivateData",
2427 ptr ? ptr : track->enc->extradata,
2431 param_write_string(pb, "FourCC", "H264");
2432 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2433 param_write_string(pb, "FourCC", "WVC1");
2434 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2435 track->enc->extradata_size);
2437 param_write_int(pb, "MaxWidth", track->enc->width);
2438 param_write_int(pb, "MaxHeight", track->enc->height);
2439 param_write_int(pb, "DisplayWidth", track->enc->width);
2440 param_write_int(pb, "DisplayHeight", track->enc->height);
2442 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2443 param_write_string(pb, "FourCC", "AACL");
2444 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2445 param_write_string(pb, "FourCC", "WMAP");
2447 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2448 track->enc->extradata_size);
2449 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2450 track->enc->codec_id));
2451 param_write_int(pb, "Channels", track->enc->channels);
2452 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2453 param_write_int(pb, "BitsPerSample", 16);
2454 param_write_int(pb, "PacketSize", track->enc->block_align ?
2455 track->enc->block_align : 4);
2457 avio_printf(pb, "</%s>\n", type);
2459 avio_printf(pb, "</switch>\n");
2460 avio_printf(pb, "</body>\n");
2461 avio_printf(pb, "</smil>\n");
2463 return update_size(pb, pos);
2466 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2469 ffio_wfourcc(pb, "mfhd");
2471 avio_wb32(pb, mov->fragments);
2475 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2476 int64_t moof_offset)
2478 int64_t pos = avio_tell(pb);
2479 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2480 MOV_TFHD_BASE_DATA_OFFSET;
2481 if (!track->entry) {
2482 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2484 flags |= MOV_TFHD_DEFAULT_FLAGS;
2487 /* Don't set a default sample size, the silverlight player refuses
2488 * to play files with that set. Don't set a default sample duration,
2489 * WMP freaks out if it is set. */
2490 if (track->mode == MODE_ISM)
2491 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION);
2493 avio_wb32(pb, 0); /* size placeholder */
2494 ffio_wfourcc(pb, "tfhd");
2495 avio_w8(pb, 0); /* version */
2496 avio_wb24(pb, flags);
2498 avio_wb32(pb, track->track_id); /* track-id */
2499 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2500 avio_wb64(pb, moof_offset);
2501 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2502 track->default_duration = get_cluster_duration(track, 0);
2503 avio_wb32(pb, track->default_duration);
2505 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2506 track->default_size = track->entry ? track->cluster[0].size : 1;
2507 avio_wb32(pb, track->default_size);
2509 track->default_size = -1;
2511 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2512 track->default_sample_flags =
2513 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2514 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2515 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2516 avio_wb32(pb, track->default_sample_flags);
2519 return update_size(pb, pos);
2522 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2524 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2525 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2528 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2530 int64_t pos = avio_tell(pb);
2531 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2534 for (i = 0; i < track->entry; i++) {
2535 if (get_cluster_duration(track, i) != track->default_duration)
2536 flags |= MOV_TRUN_SAMPLE_DURATION;
2537 if (track->cluster[i].size != track->default_size)
2538 flags |= MOV_TRUN_SAMPLE_SIZE;
2539 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2540 flags |= MOV_TRUN_SAMPLE_FLAGS;
2542 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2543 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2544 if (track->flags & MOV_TRACK_CTTS)
2545 flags |= MOV_TRUN_SAMPLE_CTS;
2547 avio_wb32(pb, 0); /* size placeholder */
2548 ffio_wfourcc(pb, "trun");
2549 avio_w8(pb, 0); /* version */
2550 avio_wb24(pb, flags);
2552 avio_wb32(pb, track->entry); /* sample count */
2553 track->moof_size_offset = avio_tell(pb);
2554 avio_wb32(pb, 0); /* data offset */
2555 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2556 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2558 for (i = 0; i < track->entry; i++) {
2559 if (flags & MOV_TRUN_SAMPLE_DURATION)
2560 avio_wb32(pb, get_cluster_duration(track, i));
2561 if (flags & MOV_TRUN_SAMPLE_SIZE)
2562 avio_wb32(pb, track->cluster[i].size);
2563 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2564 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2565 if (flags & MOV_TRUN_SAMPLE_CTS)
2566 avio_wb32(pb, track->cluster[i].cts);
2569 return update_size(pb, pos);
2572 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2574 int64_t pos = avio_tell(pb);
2575 const uint8_t uuid[] = {
2576 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2577 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2580 avio_wb32(pb, 0); /* size placeholder */
2581 ffio_wfourcc(pb, "uuid");
2582 avio_write(pb, uuid, sizeof(uuid));
2585 avio_wb64(pb, track->frag_start);
2586 avio_wb64(pb, track->start_dts + track->track_duration -
2587 track->cluster[0].dts);
2589 return update_size(pb, pos);
2592 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2593 MOVTrack *track, int entry)
2595 int n = track->nb_frag_info - 1 - entry, i;
2596 int size = 8 + 16 + 4 + 1 + 16*n;
2597 const uint8_t uuid[] = {
2598 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2599 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2605 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2606 avio_wb32(pb, size);
2607 ffio_wfourcc(pb, "uuid");
2608 avio_write(pb, uuid, sizeof(uuid));
2612 for (i = 0; i < n; i++) {
2613 int index = entry + 1 + i;
2614 avio_wb64(pb, track->frag_info[index].time);
2615 avio_wb64(pb, track->frag_info[index].duration);
2617 if (n < mov->ism_lookahead) {
2618 int free_size = 16*(mov->ism_lookahead - n);
2619 avio_wb32(pb, free_size);
2620 ffio_wfourcc(pb, "free");
2621 for (i = 0; i < free_size - 8; i++)
2628 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2631 int64_t pos = avio_tell(pb);
2633 for (i = 0; i < mov->ism_lookahead; i++) {
2634 /* Update the tfrf tag for the last ism_lookahead fragments,
2635 * nb_frag_info - 1 is the next fragment to be written. */
2636 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2638 avio_seek(pb, pos, SEEK_SET);
2642 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2643 MOVTrack *track, int64_t moof_offset)
2645 int64_t pos = avio_tell(pb);
2646 avio_wb32(pb, 0); /* size placeholder */
2647 ffio_wfourcc(pb, "traf");
2649 mov_write_tfhd_tag(pb, track, moof_offset);
2650 mov_write_trun_tag(pb, track);
2651 if (mov->mode == MODE_ISM) {
2652 mov_write_tfxd_tag(pb, track);
2654 if (mov->ism_lookahead) {
2655 int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
2657 track->tfrf_offset = avio_tell(pb);
2658 avio_wb32(pb, 8 + size);
2659 ffio_wfourcc(pb, "free");
2660 for (i = 0; i < size; i++)
2665 return update_size(pb, pos);
2668 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2670 int64_t pos = avio_tell(pb), end;
2673 avio_wb32(pb, 0); /* size placeholder */
2674 ffio_wfourcc(pb, "moof");
2676 mov_write_mfhd_tag(pb, mov);
2677 for (i = 0; i < mov->nb_streams; i++) {
2678 MOVTrack *track = &mov->tracks[i];
2679 if (tracks >= 0 && i != tracks)
2683 mov_write_traf_tag(pb, mov, track, pos);
2686 end = avio_tell(pb);
2687 moof_size = end - pos;
2688 for (i = 0; i < mov->nb_streams; i++) {
2689 MOVTrack *track = &mov->tracks[i];
2690 if (tracks >= 0 && i != tracks)
2694 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2695 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2697 avio_seek(pb, end, SEEK_SET);
2699 return update_size(pb, pos);
2702 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2704 int64_t pos = avio_tell(pb);
2707 avio_wb32(pb, 0); /* size placeholder */
2708 ffio_wfourcc(pb, "tfra");
2709 avio_w8(pb, 1); /* version */
2712 avio_wb32(pb, track->track_id);
2713 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2714 avio_wb32(pb, track->nb_frag_info);
2715 for (i = 0; i < track->nb_frag_info; i++) {
2716 avio_wb64(pb, track->frag_info[i].time);
2717 avio_wb64(pb, track->frag_info[i].offset);
2718 avio_w8(pb, 1); /* traf number */
2719 avio_w8(pb, 1); /* trun number */
2720 avio_w8(pb, 1); /* sample number */
2723 return update_size(pb, pos);
2726 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2728 int64_t pos = avio_tell(pb);
2731 avio_wb32(pb, 0); /* size placeholder */
2732 ffio_wfourcc(pb, "mfra");
2733 /* An empty mfra atom is enough to indicate to the publishing point that
2734 * the stream has ended. */
2735 if (mov->flags & FF_MOV_FLAG_ISML)
2736 return update_size(pb, pos);
2738 for (i = 0; i < mov->nb_streams; i++) {
2739 MOVTrack *track = &mov->tracks[i];
2740 if (track->nb_frag_info)
2741 mov_write_tfra_tag(pb, track);
2745 ffio_wfourcc(pb, "mfro");
2746 avio_wb32(pb, 0); /* version + flags */
2747 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2749 return update_size(pb, pos);
2752 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2754 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2755 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2757 mov->mdat_pos = avio_tell(pb);
2758 avio_wb32(pb, 0); /* size placeholder*/
2759 ffio_wfourcc(pb, "mdat");
2763 /* TODO: This needs to be more general */
2764 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2766 MOVMuxContext *mov = s->priv_data;
2767 int64_t pos = avio_tell(pb);
2768 int has_h264 = 0, has_video = 0;
2772 for (i = 0; i < s->nb_streams; i++) {
2773 AVStream *st = s->streams[i];
2774 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2776 if (st->codec->codec_id == AV_CODEC_ID_H264)
2780 avio_wb32(pb, 0); /* size */
2781 ffio_wfourcc(pb, "ftyp");
2783 if (mov->mode == MODE_3GP) {
2784 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2785 minor = has_h264 ? 0x100 : 0x200;
2786 } else if (mov->mode & MODE_3G2) {
2787 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2788 minor = has_h264 ? 0x20000 : 0x10000;
2789 }else if (mov->mode == MODE_PSP)
2790 ffio_wfourcc(pb, "MSNV");
2791 else if (mov->mode == MODE_MP4)
2792 ffio_wfourcc(pb, "isom");
2793 else if (mov->mode == MODE_IPOD)
2794 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2795 else if (mov->mode == MODE_ISM)
2796 ffio_wfourcc(pb, "isml");
2797 else if (mov->mode == MODE_F4V)
2798 ffio_wfourcc(pb, "f4v ");
2800 ffio_wfourcc(pb, "qt ");
2802 avio_wb32(pb, minor);
2804 if(mov->mode == MODE_MOV)
2805 ffio_wfourcc(pb, "qt ");
2806 else if (mov->mode == MODE_ISM) {
2807 ffio_wfourcc(pb, "piff");
2808 ffio_wfourcc(pb, "iso2");
2810 ffio_wfourcc(pb, "isom");
2811 ffio_wfourcc(pb, "iso2");
2813 ffio_wfourcc(pb, "avc1");
2816 if (mov->mode == MODE_3GP)
2817 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2818 else if (mov->mode & MODE_3G2)
2819 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2820 else if (mov->mode == MODE_PSP)
2821 ffio_wfourcc(pb, "MSNV");
2822 else if (mov->mode == MODE_MP4)
2823 ffio_wfourcc(pb, "mp41");
2824 return update_size(pb, pos);
2827 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
2829 AVCodecContext *video_codec = s->streams[0]->codec;
2830 AVCodecContext *audio_codec = s->streams[1]->codec;
2831 int audio_rate = audio_codec->sample_rate;
2832 int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
2833 int audio_kbitrate = audio_codec->bit_rate / 1000;
2834 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2836 avio_wb32(pb, 0x94); /* size */
2837 ffio_wfourcc(pb, "uuid");
2838 ffio_wfourcc(pb, "PROF");
2840 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2841 avio_wb32(pb, 0xbb88695c);
2842 avio_wb32(pb, 0xfac9c740);
2844 avio_wb32(pb, 0x0); /* ? */
2845 avio_wb32(pb, 0x3); /* 3 sections ? */
2847 avio_wb32(pb, 0x14); /* size */
2848 ffio_wfourcc(pb, "FPRF");
2849 avio_wb32(pb, 0x0); /* ? */
2850 avio_wb32(pb, 0x0); /* ? */
2851 avio_wb32(pb, 0x0); /* ? */
2853 avio_wb32(pb, 0x2c); /* size */
2854 ffio_wfourcc(pb, "APRF");/* audio */
2856 avio_wb32(pb, 0x2); /* TrackID */
2857 ffio_wfourcc(pb, "mp4a");
2858 avio_wb32(pb, 0x20f);
2860 avio_wb32(pb, audio_kbitrate);
2861 avio_wb32(pb, audio_kbitrate);
2862 avio_wb32(pb, audio_rate);
2863 avio_wb32(pb, audio_codec->channels);
2865 avio_wb32(pb, 0x34); /* size */
2866 ffio_wfourcc(pb, "VPRF"); /* video */
2868 avio_wb32(pb, 0x1); /* TrackID */
2869 if (video_codec->codec_id == AV_CODEC_ID_H264) {
2870 ffio_wfourcc(pb, "avc1");
2871 avio_wb16(pb, 0x014D);
2872 avio_wb16(pb, 0x0015);
2874 ffio_wfourcc(pb, "mp4v");
2875 avio_wb16(pb, 0x0000);
2876 avio_wb16(pb, 0x0103);
2879 avio_wb32(pb, video_kbitrate);
2880 avio_wb32(pb, video_kbitrate);
2881 avio_wb32(pb, frame_rate);
2882 avio_wb32(pb, frame_rate);
2883 avio_wb16(pb, video_codec->width);
2884 avio_wb16(pb, video_codec->height);
2885 avio_wb32(pb, 0x010001); /* ? */
2888 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2891 int i, closed_gop = 0;
2893 for (i = 0; i < pkt->size - 4; i++) {
2894 c = (c<<8) + pkt->data[i];
2895 if (c == 0x1b8) { // gop
2896 closed_gop = pkt->data[i+4]>>6 & 0x01;
2897 } else if (c == 0x100) { // pic
2898 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2899 if (!temp_ref || closed_gop) // I picture is not reordered
2900 *flags = MOV_SYNC_SAMPLE;
2902 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2909 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2911 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2912 int seq = 0, entry = 0;
2913 int key = pkt->flags & AV_PKT_FLAG_KEY;
2914 start = find_next_marker(pkt->data, end);
2915 for (next = start; next < end; start = next) {
2916 next = find_next_marker(start + 4, end);
2917 switch (AV_RB32(start)) {
2918 case VC1_CODE_SEQHDR:
2921 case VC1_CODE_ENTRYPOINT:
2924 case VC1_CODE_SLICE:
2925 trk->vc1_info.slices = 1;
2929 if (!trk->entry && !fragment) {
2930 /* First packet in first fragment */
2931 trk->vc1_info.first_packet_seq = seq;
2932 trk->vc1_info.first_packet_entry = entry;
2933 } else if ((seq && !trk->vc1_info.packet_seq) ||
2934 (entry && !trk->vc1_info.packet_entry)) {
2936 for (i = 0; i < trk->entry; i++)
2937 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2938 trk->has_keyframes = 0;
2940 trk->vc1_info.packet_seq = 1;
2942 trk->vc1_info.packet_entry = 1;
2944 /* First fragment */
2945 if ((!seq || trk->vc1_info.first_packet_seq) &&
2946 (!entry || trk->vc1_info.first_packet_entry)) {
2947 /* First packet had the same headers as this one, readd the
2948 * sync sample flag. */
2949 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2950 trk->has_keyframes = 1;
2954 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2956 else if (trk->vc1_info.packet_seq)
2958 else if (trk->vc1_info.packet_entry)
2961 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2962 trk->has_keyframes++;
2966 static int get_moov_size(AVFormatContext *s)
2970 AVIOContext *moov_buf;
2971 MOVMuxContext *mov = s->priv_data;
2973 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
2975 mov_write_moov_tag(moov_buf, mov, s);
2976 ret = avio_close_dyn_buf(moov_buf, &buf);
2981 static int mov_flush_fragment(AVFormatContext *s)
2983 MOVMuxContext *mov = s->priv_data;
2984 int i, first_track = -1;
2985 int64_t mdat_size = 0;
2987 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2990 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2991 int64_t pos = avio_tell(s->pb);
2993 int buf_size, moov_size;
2995 for (i = 0; i < mov->nb_streams; i++)
2996 if (!mov->tracks[i].entry)
2998 /* Don't write the initial moov unless all tracks have data */
2999 if (i < mov->nb_streams)
3002 moov_size = get_moov_size(s);
3003 for (i = 0; i < mov->nb_streams; i++)
3004 mov->tracks[i].data_offset = pos + moov_size + 8;
3006 mov_write_moov_tag(s->pb, mov, s);
3008 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
3009 mov->mdat_buf = NULL;
3010 avio_wb32(s->pb, buf_size + 8);
3011 ffio_wfourcc(s->pb, "mdat");
3012 avio_write(s->pb, buf, buf_size);
3017 for (i = 0; i < mov->nb_streams; i++) {
3018 if (mov->tracks[i].entry)
3019 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
3020 mov->tracks[i].track_duration -
3021 mov->tracks[i].cluster[0].dts;
3022 mov->tracks[i].entry = 0;
3028 for (i = 0; i < mov->nb_streams; i++) {
3029 MOVTrack *track = &mov->tracks[i];
3030 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
3031 track->data_offset = 0;
3033 track->data_offset = mdat_size;
3034 if (!track->mdat_buf)
3036 mdat_size += avio_tell(track->mdat_buf);
3037 if (first_track < 0)
3044 for (i = 0; i < mov->nb_streams; i++) {
3045 MOVTrack *track = &mov->tracks[i];
3046 int buf_size, write_moof = 1, moof_tracks = -1;
3048 int64_t duration = 0;
3051 duration = track->start_dts + track->track_duration -
3052 track->cluster[0].dts;
3053 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
3054 if (!track->mdat_buf)
3056 mdat_size = avio_tell(track->mdat_buf);
3059 write_moof = i == first_track;
3063 MOVFragmentInfo *info;
3065 track->nb_frag_info++;
3066 track->frag_info = av_realloc(track->frag_info,
3067 sizeof(*track->frag_info) *
3068 track->nb_frag_info);
3069 info = &track->frag_info[track->nb_frag_info - 1];
3070 info->offset = avio_tell(s->pb);
3071 info->time = mov->tracks[i].frag_start;
3072 info->duration = duration;
3073 mov_write_tfrf_tags(s->pb, mov, track);
3075 mov_write_moof_tag(s->pb, mov, moof_tracks);
3076 info->tfrf_offset = track->tfrf_offset;
3079 avio_wb32(s->pb, mdat_size + 8);
3080 ffio_wfourcc(s->pb, "mdat");
3084 track->frag_start += duration;
3086 if (!track->mdat_buf)
3088 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
3089 track->mdat_buf = NULL;
3091 avio_write(s->pb, buf, buf_size);
3101 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
3103 MOVMuxContext *mov = s->priv_data;
3104 AVIOContext *pb = s->pb;
3105 MOVTrack *trk = &mov->tracks[pkt->stream_index];
3106 AVCodecContext *enc = trk->enc;
3107 unsigned int samples_in_chunk = 0;
3108 int size= pkt->size;
3109 uint8_t *reformatted_data = NULL;
3111 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3113 if (mov->fragments > 0) {
3114 if (!trk->mdat_buf) {
3115 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
3120 if (!mov->mdat_buf) {
3121 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
3128 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
3129 /* We must find out how many AMR blocks there are in one packet */
3130 static uint16_t packed_size[16] =
3131 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
3134 while (len < size && samples_in_chunk < 100) {
3135 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
3138 if (samples_in_chunk > 1) {
3139 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
3142 } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
3143 enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
3144 samples_in_chunk = enc->frame_size;
3145 } else if (trk->sample_size)
3146 samples_in_chunk = size / trk->sample_size;
3148 samples_in_chunk = 1;
3150 /* copy extradata if it exists */
3151 if (trk->vos_len == 0 && enc->extradata_size > 0) {
3152 trk->vos_len = enc->extradata_size;
3153 trk->vos_data = av_malloc(trk->vos_len);
3154 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
3157 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
3158 /* from x264 or from bytestream h264 */
3159 /* nal reformating needed */
3160 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3161 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
3163 avio_write(pb, reformatted_data, size);
3165 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
3167 } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
3168 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
3169 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
3172 avio_write(pb, pkt->data, size);
3175 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
3176 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
3177 /* copy frame to create needed atoms */
3178 trk->vos_len = size;
3179 trk->vos_data = av_malloc(size);
3181 return AVERROR(ENOMEM);
3182 memcpy(trk->vos_data, pkt->data, size);
3185 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
3186 trk->cluster = av_realloc_f(trk->cluster, sizeof(*trk->cluster), (trk->entry + MOV_INDEX_CLUSTER_SIZE));
3191 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
3192 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
3193 trk->cluster[trk->entry].chunkNum = 0;
3194 trk->cluster[trk->entry].size = size;
3195 trk->cluster[trk->entry].entries = samples_in_chunk;
3196 trk->cluster[trk->entry].dts = pkt->dts;
3197 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
3198 /* First packet of a new fragment. We already wrote the duration
3199 * of the last packet of the previous fragment based on track_duration,
3200 * which might not exactly match our dts. Therefore adjust the dts
3201 * of this packet to be what the previous packets duration implies. */
3202 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
3204 if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !supports_edts(mov)) {
3205 trk->cluster[trk->entry].dts = trk->start_dts = 0;
3207 if (trk->start_dts == AV_NOPTS_VALUE)
3208 trk->start_dts = pkt->dts;
3209 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
3210 trk->last_sample_is_subtitle_end = 0;
3212 if (pkt->pts == AV_NOPTS_VALUE) {
3213 av_log(s, AV_LOG_WARNING, "pts has no value\n");
3214 pkt->pts = pkt->dts;
3216 if (pkt->dts != pkt->pts)
3217 trk->flags |= MOV_TRACK_CTTS;
3218 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
3219 trk->cluster[trk->entry].flags = 0;
3220 if (enc->codec_id == AV_CODEC_ID_VC1) {
3221 mov_parse_vc1_frame(pkt, trk, mov->fragments);
3222 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
3223 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
3224 trk->entry > 0) { // force sync sample for the first key frame
3225 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3226 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3227 trk->flags |= MOV_TRACK_STPS;
3229 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3231 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
3232 trk->has_keyframes++;
3235 trk->sample_count += samples_in_chunk;
3236 mov->mdat_size += size;
3240 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
3241 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3242 reformatted_data, size);
3243 av_free(reformatted_data);
3247 static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
3249 MOVMuxContext *mov = s->priv_data;
3250 MOVTrack *trk = &mov->tracks[pkt->stream_index];
3251 AVCodecContext *enc = trk->enc;
3252 int64_t frag_duration = 0;
3253 int size = pkt->size;
3255 if (!pkt->size) return 0; /* Discard 0 sized packets */
3257 if (trk->entry && pkt->stream_index < s->nb_streams)
3258 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3259 s->streams[pkt->stream_index]->time_base,
3261 if ((mov->max_fragment_duration &&
3262 frag_duration >= mov->max_fragment_duration) ||
3263 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3264 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3265 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
3266 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3267 if (frag_duration >= mov->min_fragment_duration)
3268 mov_flush_fragment(s);
3271 return ff_mov_write_packet(s, pkt);
3274 static int mov_write_subtitle_end_packet(AVFormatContext *s,
3278 uint8_t data[2] = {0};
3281 av_init_packet(&end);
3282 end.size = sizeof(data);
3287 end.stream_index = stream_index;
3289 ret = mov_write_single_packet(s, &end);
3290 av_free_packet(&end);
3295 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
3298 mov_flush_fragment(s);
3302 MOVMuxContext *mov = s->priv_data;
3304 if (!pkt->size) return 0; /* Discard 0 sized packets */
3307 * Subtitles require special handling.
3309 * 1) For full complaince, every track must have a sample at
3310 * dts == 0, which is rarely true for subtitles. So, as soon
3311 * as we see any packet with dts > 0, write an empty subtitle
3312 * at dts == 0 for any subtitle track with no samples in it.
3314 * 2) For each subtitle track, check if the current packet's
3315 * dts is past the duration of the last subtitle sample. If
3316 * so, we now need to write an end sample for that subtitle.
3318 * This must be done conditionally to allow for subtitles that
3319 * immediately replace each other, in which case an end sample
3320 * is not needed, and is, in fact, actively harmful.
3322 * 3) See mov_write_trailer for how the final end sample is
3325 for (i = 0; i < mov->nb_streams; i++) {
3326 MOVTrack *trk = &mov->tracks[i];
3329 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
3330 trk->track_duration < pkt->dts &&
3331 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
3332 ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
3333 if (ret < 0) return ret;
3334 trk->last_sample_is_subtitle_end = 1;
3338 return mov_write_single_packet(s, pkt);
3342 // QuickTime chapters involve an additional text track with the chapter names
3343 // as samples, and a tref pointing from the other tracks to the chapter one.
3344 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
3348 MOVMuxContext *mov = s->priv_data;
3349 MOVTrack *track = &mov->tracks[tracknum];
3350 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
3353 track->mode = mov->mode;
3354 track->tag = MKTAG('t','e','x','t');
3355 track->timescale = MOV_TIMESCALE;
3356 track->enc = avcodec_alloc_context3(NULL);
3357 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
3359 if (avio_open_dyn_buf(&pb) >= 0) {
3363 /* Stub header (usually for Quicktime chapter track) */
3365 avio_wb32(pb, 0x01); // displayFlags
3366 avio_w8(pb, 0x00); // horizontal justification
3367 avio_w8(pb, 0x00); // vertical justification
3368 avio_w8(pb, 0x00); // bgColourRed
3369 avio_w8(pb, 0x00); // bgColourGreen
3370 avio_w8(pb, 0x00); // bgColourBlue
3371 avio_w8(pb, 0x00); // bgColourAlpha
3373 avio_wb16(pb, 0x00); // defTextBoxTop
3374 avio_wb16(pb, 0x00); // defTextBoxLeft
3375 avio_wb16(pb, 0x00); // defTextBoxBottom
3376 avio_wb16(pb, 0x00); // defTextBoxRight
3378 avio_wb16(pb, 0x00); // startChar
3379 avio_wb16(pb, 0x00); // endChar
3380 avio_wb16(pb, 0x01); // fontID
3381 avio_w8(pb, 0x00); // fontStyleFlags
3382 avio_w8(pb, 0x00); // fontSize
3383 avio_w8(pb, 0x00); // fgColourRed
3384 avio_w8(pb, 0x00); // fgColourGreen
3385 avio_w8(pb, 0x00); // fgColourBlue
3386 avio_w8(pb, 0x00); // fgColourAlpha
3388 avio_wb32(pb, 0x0D); // box size
3389 ffio_wfourcc(pb, "ftab"); // box atom name
3390 avio_wb16(pb, 0x01); // entry count
3392 avio_wb16(pb, 0x01); // font ID
3393 avio_w8(pb, 0x00); // font name length
3395 if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
3396 track->enc->extradata = buf;
3397 track->enc->extradata_size = size;
3403 for (i = 0; i < s->nb_chapters; i++) {
3404 AVChapter *c = s->chapters[i];
3405 AVDictionaryEntry *t;
3407 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3408 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3409 pkt.duration = end - pkt.dts;
3411 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3412 len = strlen(t->value);
3414 pkt.data = av_malloc(pkt.size);
3415 AV_WB16(pkt.data, len);
3416 memcpy(pkt.data+2, t->value, len);
3417 ff_mov_write_packet(s, &pkt);
3418 av_freep(&pkt.data);
3423 static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
3426 MOVMuxContext *mov = s->priv_data;
3427 MOVTrack *track = &mov->tracks[index];
3428 AVStream *src_st = s->streams[src_index];
3430 AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
3431 AVRational rate = {src_st->codec->time_base.den, src_st->codec->time_base.num};
3433 /* if the codec time base makes no sense, try to fallback on stream frame rate */
3434 if (av_timecode_check_frame_rate(rate) < 0) {
3435 av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
3436 rate.num, rate.den, src_st->avg_frame_rate.num, src_st->avg_frame_rate.den);
3437 rate = src_st->avg_frame_rate;
3440 /* compute the frame number */
3441 ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
3445 /* tmcd track based on video stream */
3446 track->mode = mov->mode;
3447 track->tag = MKTAG('t','m','c','d');
3448 track->src_track = src_index;
3449 track->timescale = mov->tracks[src_index].timescale;
3450 if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
3451 track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
3453 /* encode context: tmcd data stream */
3454 track->enc = avcodec_alloc_context3(NULL);
3455 track->enc->codec_type = AVMEDIA_TYPE_DATA;
3456 track->enc->codec_tag = track->tag;
3457 track->enc->time_base = src_st->codec->time_base;
3459 /* the tmcd track just contains one packet with the frame number */
3460 pkt.data = av_malloc(pkt.size);
3461 AV_WB32(pkt.data, tc.start);
3462 ret = ff_mov_write_packet(s, &pkt);
3467 static int mov_write_header(AVFormatContext *s)
3469 AVIOContext *pb = s->pb;
3470 MOVMuxContext *mov = s->priv_data;
3471 AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
3472 int i, hint_track = 0, tmcd_track = 0;
3474 /* Set the FRAGMENT flag if any of the fragmentation methods are
3476 if (mov->max_fragment_duration || mov->max_fragment_size ||
3477 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3478 FF_MOV_FLAG_FRAG_KEYFRAME |
3479 FF_MOV_FLAG_FRAG_CUSTOM))
3480 mov->flags |= FF_MOV_FLAG_FRAGMENT;
3482 /* faststart: moov at the beginning of the file, if supported */
3483 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3484 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3485 mov->flags &= ~FF_MOV_FLAG_FASTSTART;
3487 mov->reserved_moov_size = -1;
3490 if (!supports_edts(mov) && s->avoid_negative_ts < 0) {
3491 s->avoid_negative_ts = 1;
3494 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3495 * is enabled, we don't support non-seekable output at all. */
3496 if (!s->pb->seekable &&
3497 ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
3498 !(s->oformat && !strcmp(s->oformat->name, "ismv")))
3499 || mov->ism_lookahead)) {
3500 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3504 /* Default mode == MP4 */
3505 mov->mode = MODE_MP4;
3507 if (s->oformat != NULL) {
3508 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3509 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3510 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3511 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3512 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3513 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3514 else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
3516 mov_write_ftyp_tag(pb,s);
3517 if (mov->mode == MODE_PSP) {
3518 if (s->nb_streams != 2) {
3519 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3522 mov_write_uuidprof_tag(pb,s);
3526 mov->nb_streams = s->nb_streams;
3527 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
3528 mov->chapter_track = mov->nb_streams++;
3530 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3531 /* Add hint tracks for each audio and video stream */
3532 hint_track = mov->nb_streams;
3533 for (i = 0; i < s->nb_streams; i++) {
3534 AVStream *st = s->streams[i];
3535 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3536 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3542 if (mov->mode == MODE_MOV) {
3543 tmcd_track = mov->nb_streams;
3545 /* +1 tmcd track for each video stream with a timecode */
3546 for (i = 0; i < s->nb_streams; i++) {
3547 AVStream *st = s->streams[i];
3548 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3549 (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
3550 mov->nb_meta_tmcd++;
3553 /* check if there is already a tmcd track to remux */
3554 if (mov->nb_meta_tmcd) {
3555 for (i = 0; i < s->nb_streams; i++) {
3556 AVStream *st = s->streams[i];
3557 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
3558 av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
3559 "so timecode metadata are now ignored\n");
3560 mov->nb_meta_tmcd = 0;
3565 mov->nb_streams += mov->nb_meta_tmcd;
3568 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
3570 return AVERROR(ENOMEM);
3572 for(i=0; i<s->nb_streams; i++){
3573 AVStream *st= s->streams[i];
3574 MOVTrack *track= &mov->tracks[i];
3575 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3577 track->enc = st->codec;
3578 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3579 if (track->language < 0)
3580 track->language = 0;
3581 track->mode = mov->mode;
3582 track->tag = mov_find_codec_tag(s, track);
3584 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
3585 "codec not currently supported in container\n", i);
3588 /* If hinting of this track is enabled by a later hint track,
3589 * this is updated. */
3590 track->hint_track = -1;
3591 track->start_dts = AV_NOPTS_VALUE;
3592 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
3593 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3594 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3595 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3596 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3597 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3600 track->height = track->tag>>24 == 'n' ? 486 : 576;
3602 track->timescale = st->codec->time_base.den;
3603 while(track->timescale < 10000)
3604 track->timescale *= 2;
3605 if (track->mode == MODE_MOV && track->timescale > 100000)
3606 av_log(s, AV_LOG_WARNING,
3607 "WARNING codec timebase is very high. If duration is too long,\n"
3608 "file may not be playable by quicktime. Specify a shorter timebase\n"
3609 "or choose different container.\n");
3610 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
3611 track->timescale = st->codec->sample_rate;
3612 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
3613 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
3614 track->audio_vbr = 1;
3615 }else if(st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
3616 st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
3617 st->codec->codec_id == AV_CODEC_ID_ILBC){
3618 if (!st->codec->block_align) {
3619 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
3622 track->sample_size = st->codec->block_align;
3623 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
3624 track->audio_vbr = 1;
3626 track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
3628 if (track->mode != MODE_MOV &&
3629 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3630 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3631 i, track->enc->sample_rate);
3634 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
3635 track->timescale = st->codec->time_base.den;
3637 track->timescale = MOV_TIMESCALE;
3640 track->height = st->codec->height;
3641 /* The ism specific timescale isn't mandatory, but is assumed by
3642 * some tools, such as mp4split. */
3643 if (mov->mode == MODE_ISM)
3644 track->timescale = 10000000;
3646 avpriv_set_pts_info(st, 64, 1, track->timescale);
3648 /* copy extradata if it exists */
3649 if (st->codec->extradata_size) {
3650 track->vos_len = st->codec->extradata_size;
3651 track->vos_data = av_malloc(track->vos_len);
3652 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3656 if (mov->mode == MODE_ISM) {
3657 /* If no fragmentation options have been set, set a default. */
3658 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3659 FF_MOV_FLAG_FRAG_CUSTOM)) &&
3660 !mov->max_fragment_duration && !mov->max_fragment_size)
3661 mov->max_fragment_duration = 5000000;
3662 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
3663 FF_MOV_FLAG_FRAGMENT;
3666 if(mov->reserved_moov_size){
3667 mov->reserved_moov_pos= avio_tell(pb);
3668 if (mov->reserved_moov_size > 0)
3669 avio_skip(pb, mov->reserved_moov_size);
3672 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
3673 mov_write_mdat_tag(pb, mov);
3675 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3676 mov->time = ff_iso8601_to_unix_time(t->value);
3678 mov->time += 0x7C25B080; // 1970 based -> 1904 based
3680 if (mov->chapter_track)
3681 mov_create_chapter_track(s, mov->chapter_track);
3683 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3684 /* Initialize the hint tracks for each audio and video stream */
3685 for (i = 0; i < s->nb_streams; i++) {
3686 AVStream *st = s->streams[i];
3687 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3688 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3689 ff_mov_init_hinting(s, hint_track, i);
3695 if (mov->nb_meta_tmcd) {
3696 /* Initialize the tmcd tracks */
3697 for (i = 0; i < s->nb_streams; i++) {
3698 AVStream *st = s->streams[i];
3701 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3703 t = av_dict_get(st->metadata, "timecode", NULL, 0);
3706 if (mov_create_timecode_track(s, tmcd_track, i, t->value) < 0)
3715 if (mov->flags & FF_MOV_FLAG_ISML)
3716 mov_write_isml_manifest(pb, mov);
3718 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3719 mov_write_moov_tag(pb, mov, s);
3725 av_freep(&mov->tracks);
3730 * This function gets the moov size if moved to the top of the file: the chunk
3731 * offset table can switch between stco (32-bit entries) to co64 (64-bit
3732 * entries) when the moov is moved to the top, so the size of the moov would
3733 * change. It also updates the chunk offset tables.
3735 static int compute_moov_size(AVFormatContext *s)
3737 int i, moov_size, moov_size2;
3738 MOVMuxContext *mov = s->priv_data;
3740 moov_size = get_moov_size(s);
3744 for (i = 0; i < mov->nb_streams; i++)
3745 mov->tracks[i].data_offset += moov_size;
3747 moov_size2 = get_moov_size(s);
3751 /* if the size changed, we just switched from stco to co64 and needs to
3752 * update the offsets */
3753 if (moov_size2 != moov_size)
3754 for (i = 0; i < mov->nb_streams; i++)
3755 mov->tracks[i].data_offset += moov_size2 - moov_size;
3760 static int shift_data(AVFormatContext *s)
3762 int ret = 0, moov_size;
3763 MOVMuxContext *mov = s->priv_data;
3764 int64_t pos, pos_end = avio_tell(s->pb);
3765 uint8_t *buf, *read_buf[2];
3766 int read_buf_id = 0;
3768 AVIOContext *read_pb;
3770 moov_size = compute_moov_size(s);
3774 buf = av_malloc(moov_size * 2);
3776 return AVERROR(ENOMEM);
3778 read_buf[1] = buf + moov_size;
3780 /* Shift the data: the AVIO context of the output can only be used for
3781 * writing, so we re-open the same output, but for reading. It also avoids
3782 * a read/seek/write/seek back and forth. */
3784 ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
3786 av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
3787 "the second pass (faststart)\n", s->filename);
3791 /* mark the end of the shift to up to the last data we wrote, and get ready
3793 pos_end = avio_tell(s->pb);
3794 avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
3796 /* start reading at where the new moov will be placed */
3797 avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
3798 pos = avio_tell(read_pb);
3800 #define READ_BLOCK do { \
3801 read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
3805 /* shift data by chunk of at most moov_size */
3810 n = read_size[read_buf_id];
3813 avio_write(s->pb, read_buf[read_buf_id], n);
3815 } while (pos < pos_end);
3816 avio_close(read_pb);
3823 static int mov_write_trailer(AVFormatContext *s)
3825 MOVMuxContext *mov = s->priv_data;
3826 AVIOContext *pb = s->pb;
3832 * Before actually writing the trailer, make sure that there are no
3833 * dangling subtitles, that need a terminating sample.
3835 for (i = 0; i < mov->nb_streams; i++) {
3836 MOVTrack *trk = &mov->tracks[i];
3837 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
3838 !trk->last_sample_is_subtitle_end) {
3839 mov_write_subtitle_end_packet(s, i, trk->track_duration);
3840 trk->last_sample_is_subtitle_end = 1;
3844 moov_pos = avio_tell(pb);
3846 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3847 /* Write size of mdat tag */
3848 if (mov->mdat_size + 8 <= UINT32_MAX) {
3849 avio_seek(pb, mov->mdat_pos, SEEK_SET);
3850 avio_wb32(pb, mov->mdat_size + 8);
3852 /* overwrite 'wide' placeholder atom */
3853 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
3854 /* special value: real atom size will be 64 bit value after
3857 ffio_wfourcc(pb, "mdat");
3858 avio_wb64(pb, mov->mdat_size + 16);
3860 avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
3862 if (mov->reserved_moov_size == -1) {
3863 av_log(s, AV_LOG_INFO, "Starting second pass: moving header on top of the file\n");
3864 res = shift_data(s);
3866 avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
3867 mov_write_moov_tag(pb, mov, s);
3869 } else if (mov->reserved_moov_size > 0) {
3871 mov_write_moov_tag(pb, mov, s);
3872 size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
3874 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
3877 avio_wb32(pb, size);
3878 ffio_wfourcc(pb, "free");
3879 for(i=0; i<size; i++)
3881 avio_seek(pb, moov_pos, SEEK_SET);
3883 mov_write_moov_tag(pb, mov, s);
3886 mov_flush_fragment(s);
3887 mov_write_mfra_tag(pb, mov);
3890 if (mov->chapter_track)
3891 av_freep(&mov->tracks[mov->chapter_track].enc);
3893 for (i=0; i<mov->nb_streams; i++) {
3894 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3895 ff_mov_close_hinting(&mov->tracks[i]);
3896 else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
3897 av_freep(&mov->tracks[i].enc);
3898 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
3899 mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
3900 int64_t off = avio_tell(pb);
3902 if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
3903 avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
3904 avio_write(pb, buf, 7);
3905 avio_seek(pb, off, SEEK_SET);
3908 av_freep(&mov->tracks[i].cluster);
3909 av_freep(&mov->tracks[i].frag_info);
3911 if (mov->tracks[i].vos_len)
3912 av_free(mov->tracks[i].vos_data);
3916 av_freep(&mov->tracks);
3921 #if CONFIG_MOV_MUXER
3923 AVOutputFormat ff_mov_muxer = {
3925 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3926 .extensions = "mov",
3927 .priv_data_size = sizeof(MOVMuxContext),
3928 .audio_codec = AV_CODEC_ID_AAC,
3929 .video_codec = CONFIG_LIBX264_ENCODER ?
3930 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3931 .write_header = mov_write_header,
3932 .write_packet = mov_write_packet,
3933 .write_trailer = mov_write_trailer,
3934 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3935 .codec_tag = (const AVCodecTag* const []){
3936 ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
3938 .priv_class = &mov_muxer_class,
3941 #if CONFIG_TGP_MUXER
3943 AVOutputFormat ff_tgp_muxer = {
3945 .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
3946 .extensions = "3gp",
3947 .priv_data_size = sizeof(MOVMuxContext),
3948 .audio_codec = AV_CODEC_ID_AMR_NB,
3949 .video_codec = AV_CODEC_ID_H263,
3950 .write_header = mov_write_header,
3951 .write_packet = mov_write_packet,
3952 .write_trailer = mov_write_trailer,
3953 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3954 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3955 .priv_class = &tgp_muxer_class,
3958 #if CONFIG_MP4_MUXER
3960 AVOutputFormat ff_mp4_muxer = {
3962 .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
3963 .mime_type = "application/mp4",
3964 .extensions = "mp4",
3965 .priv_data_size = sizeof(MOVMuxContext),
3966 .audio_codec = AV_CODEC_ID_AAC,
3967 .video_codec = CONFIG_LIBX264_ENCODER ?
3968 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3969 .write_header = mov_write_header,
3970 .write_packet = mov_write_packet,
3971 .write_trailer = mov_write_trailer,
3972 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3973 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3974 .priv_class = &mp4_muxer_class,
3977 #if CONFIG_PSP_MUXER
3979 AVOutputFormat ff_psp_muxer = {
3981 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
3982 .extensions = "mp4,psp",
3983 .priv_data_size = sizeof(MOVMuxContext),
3984 .audio_codec = AV_CODEC_ID_AAC,
3985 .video_codec = CONFIG_LIBX264_ENCODER ?
3986 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3987 .write_header = mov_write_header,
3988 .write_packet = mov_write_packet,
3989 .write_trailer = mov_write_trailer,
3990 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3991 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3992 .priv_class = &psp_muxer_class,
3995 #if CONFIG_TG2_MUXER
3997 AVOutputFormat ff_tg2_muxer = {
3999 .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
4000 .extensions = "3g2",
4001 .priv_data_size = sizeof(MOVMuxContext),
4002 .audio_codec = AV_CODEC_ID_AMR_NB,
4003 .video_codec = AV_CODEC_ID_H263,
4004 .write_header = mov_write_header,
4005 .write_packet = mov_write_packet,
4006 .write_trailer = mov_write_trailer,
4007 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
4008 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
4009 .priv_class = &tg2_muxer_class,
4012 #if CONFIG_IPOD_MUXER
4014 AVOutputFormat ff_ipod_muxer = {
4016 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
4017 .mime_type = "application/mp4",
4018 .extensions = "m4v,m4a",
4019 .priv_data_size = sizeof(MOVMuxContext),
4020 .audio_codec = AV_CODEC_ID_AAC,
4021 .video_codec = AV_CODEC_ID_H264,
4022 .write_header = mov_write_header,
4023 .write_packet = mov_write_packet,
4024 .write_trailer = mov_write_trailer,
4025 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
4026 .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
4027 .priv_class = &ipod_muxer_class,
4030 #if CONFIG_ISMV_MUXER
4032 AVOutputFormat ff_ismv_muxer = {
4034 .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
4035 .mime_type = "application/mp4",
4036 .extensions = "ismv,isma",
4037 .priv_data_size = sizeof(MOVMuxContext),
4038 .audio_codec = AV_CODEC_ID_AAC,
4039 .video_codec = AV_CODEC_ID_H264,
4040 .write_header = mov_write_header,
4041 .write_packet = mov_write_packet,
4042 .write_trailer = mov_write_trailer,
4043 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
4044 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4045 .priv_class = &ismv_muxer_class,
4048 #if CONFIG_F4V_MUXER
4050 AVOutputFormat ff_f4v_muxer = {
4052 .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
4053 .mime_type = "application/f4v",
4054 .extensions = "f4v",
4055 .priv_data_size = sizeof(MOVMuxContext),
4056 .audio_codec = AV_CODEC_ID_AAC,
4057 .video_codec = AV_CODEC_ID_H264,
4058 .write_header = mov_write_header,
4059 .write_packet = mov_write_packet,
4060 .write_trailer = mov_write_trailer,
4061 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
4062 .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
4063 .priv_class = &f4v_muxer_class,