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
28 #include "avio_internal.h"
33 #include "libavcodec/get_bits.h"
34 #include "libavcodec/put_bits.h"
35 #include "libavcodec/vc1.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/intfloat.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/dict.h"
49 static const AVOption options[] = {
50 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
51 { "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" },
52 { "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 },
53 { "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" },
54 { "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" },
55 { "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" },
56 { "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" },
57 { "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" },
58 { "faststart", "Run a second pass to put the index (moov atom) 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" },
59 { "omit_tfhd_offset", "Omit the base data offset in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_OMIT_TFHD_OFFSET}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
60 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
61 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
62 { "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},
63 { "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},
64 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
65 { "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},
66 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
67 { "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},
68 { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
69 { "video_track_timescale", "set timescale of all video tracks", offsetof(MOVMuxContext, video_track_timescale), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
73 #define MOV_CLASS(flavor)\
74 static const AVClass flavor ## _muxer_class = {\
75 .class_name = #flavor " muxer",\
76 .item_name = av_default_item_name,\
78 .version = LIBAVUTIL_VERSION_INT,\
81 static int get_moov_size(AVFormatContext *s);
83 //FIXME support 64 bit variant with wide placeholders
84 static int64_t update_size(AVIOContext *pb, int64_t pos)
86 int64_t curpos = avio_tell(pb);
87 avio_seek(pb, pos, SEEK_SET);
88 avio_wb32(pb, curpos - pos); /* rewrite size */
89 avio_seek(pb, curpos, SEEK_SET);
94 static int supports_edts(MOVMuxContext *mov)
96 // EDTS with fragments is tricky as we don't know the duration when its written
97 return (mov->use_editlist<0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) || mov->use_editlist>0;
100 static int co64_required(const MOVTrack *track)
102 if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
107 /* Chunk offset atom */
108 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
111 int mode64 = co64_required(track); // use 32 bit size variant if possible
112 int64_t pos = avio_tell(pb);
113 avio_wb32(pb, 0); /* size */
115 ffio_wfourcc(pb, "co64");
117 ffio_wfourcc(pb, "stco");
118 avio_wb32(pb, 0); /* version & flags */
119 avio_wb32(pb, track->chunkCount); /* entry count */
120 for (i = 0; i < track->entry; i++) {
121 if (!track->cluster[i].chunkNum)
124 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
126 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
128 return update_size(pb, pos);
131 /* Sample size atom */
132 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
135 int i, j, entries = 0, tst = -1, oldtst = -1;
137 int64_t pos = avio_tell(pb);
138 avio_wb32(pb, 0); /* size */
139 ffio_wfourcc(pb, "stsz");
140 avio_wb32(pb, 0); /* version & flags */
142 for (i = 0; i < track->entry; i++) {
143 tst = track->cluster[i].size / track->cluster[i].entries;
144 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
155 avio_wb32(pb, 0); // sample size
156 avio_wb32(pb, entries); // sample count
157 for (i = 0; i < track->entry; i++) {
158 for (j = 0; j < track->cluster[i].entries; j++) {
159 avio_wb32(pb, track->cluster[i].size /
160 track->cluster[i].entries);
164 return update_size(pb, pos);
167 /* Sample to chunk atom */
168 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
170 int index = 0, oldval = -1, i;
171 int64_t entryPos, curpos;
173 int64_t pos = avio_tell(pb);
174 avio_wb32(pb, 0); /* size */
175 ffio_wfourcc(pb, "stsc");
176 avio_wb32(pb, 0); // version & flags
177 entryPos = avio_tell(pb);
178 avio_wb32(pb, track->chunkCount); // entry count
179 for (i = 0; i < track->entry; i++) {
180 if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
181 avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
182 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
183 avio_wb32(pb, 0x1); // sample description index
184 oldval = track->cluster[i].samples_in_chunk;
188 curpos = avio_tell(pb);
189 avio_seek(pb, entryPos, SEEK_SET);
190 avio_wb32(pb, index); // rewrite size
191 avio_seek(pb, curpos, SEEK_SET);
193 return update_size(pb, pos);
196 /* Sync sample atom */
197 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
199 int64_t curpos, entryPos;
201 int64_t pos = avio_tell(pb);
202 avio_wb32(pb, 0); // size
203 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
204 avio_wb32(pb, 0); // version & flags
205 entryPos = avio_tell(pb);
206 avio_wb32(pb, track->entry); // entry count
207 for (i = 0; i < track->entry; i++) {
208 if (track->cluster[i].flags & flag) {
209 avio_wb32(pb, i + 1);
213 curpos = avio_tell(pb);
214 avio_seek(pb, entryPos, SEEK_SET);
215 avio_wb32(pb, index); // rewrite size
216 avio_seek(pb, curpos, SEEK_SET);
217 return update_size(pb, pos);
220 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
222 avio_wb32(pb, 0x11); /* size */
223 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
224 else ffio_wfourcc(pb, "damr");
225 ffio_wfourcc(pb, "FFMP");
226 avio_w8(pb, 0); /* decoder version */
228 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
229 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
230 avio_w8(pb, 0x01); /* Frames per sample */
234 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
239 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
241 if (track->vos_len < 7)
245 ffio_wfourcc(pb, "dac3");
247 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
248 fscod = get_bits(&gbc, 2);
249 frmsizecod = get_bits(&gbc, 6);
250 bsid = get_bits(&gbc, 5);
251 bsmod = get_bits(&gbc, 3);
252 acmod = get_bits(&gbc, 3);
254 skip_bits(&gbc, 2); // dsurmod
256 if ((acmod & 1) && acmod != 1)
257 skip_bits(&gbc, 2); // cmixlev
259 skip_bits(&gbc, 2); // surmixlev
261 lfeon = get_bits1(&gbc);
263 init_put_bits(&pbc, buf, sizeof(buf));
264 put_bits(&pbc, 2, fscod);
265 put_bits(&pbc, 5, bsid);
266 put_bits(&pbc, 3, bsmod);
267 put_bits(&pbc, 3, acmod);
268 put_bits(&pbc, 1, lfeon);
269 put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
270 put_bits(&pbc, 5, 0); // reserved
272 flush_put_bits(&pbc);
273 avio_write(pb, buf, sizeof(buf));
279 * This function writes extradata "as is".
280 * Extradata must be formatted like a valid atom (with size and tag).
282 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
284 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
285 return track->enc->extradata_size;
288 static int mov_write_enda_tag(AVIOContext *pb)
291 ffio_wfourcc(pb, "enda");
292 avio_wb16(pb, 1); /* little endian */
296 static int mov_write_enda_tag_be(AVIOContext *pb)
299 ffio_wfourcc(pb, "enda");
300 avio_wb16(pb, 0); /* big endian */
304 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
309 avio_w8(pb, (size >> (7 * i)) | 0x80);
310 avio_w8(pb, size & 0x7F);
313 static unsigned compute_avg_bitrate(MOVTrack *track)
317 if (!track->track_duration)
319 for (i = 0; i < track->entry; i++)
320 size += track->cluster[i].size;
321 return size * 8 * track->timescale / track->track_duration;
324 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
326 int64_t pos = avio_tell(pb);
327 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
328 unsigned avg_bitrate;
330 avio_wb32(pb, 0); // size
331 ffio_wfourcc(pb, "esds");
332 avio_wb32(pb, 0); // Version
335 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
336 avio_wb16(pb, track->track_id);
337 avio_w8(pb, 0x00); // flags (= no flags)
339 // DecoderConfig descriptor
340 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
342 // Object type indication
343 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
344 track->enc->codec_id == AV_CODEC_ID_MP3) &&
345 track->enc->sample_rate > 24000)
346 avio_w8(pb, 0x6B); // 11172-3
348 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
350 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
351 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
352 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
353 avio_w8(pb, 0x15); // flags (= Audiostream)
355 avio_w8(pb, 0x11); // flags (= Visualstream)
357 avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
359 avg_bitrate = compute_avg_bitrate(track);
360 // maxbitrate (FIXME should be max rate in any 1 sec window)
361 avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
362 avio_wb32(pb, avg_bitrate);
364 if (track->vos_len) {
365 // DecoderSpecific info descriptor
366 put_descr(pb, 0x05, track->vos_len);
367 avio_write(pb, track->vos_data, track->vos_len);
371 put_descr(pb, 0x06, 1);
373 return update_size(pb, pos);
376 static int mov_pcm_le_gt16(enum AVCodecID codec_id)
378 return codec_id == AV_CODEC_ID_PCM_S24LE ||
379 codec_id == AV_CODEC_ID_PCM_S32LE ||
380 codec_id == AV_CODEC_ID_PCM_F32LE ||
381 codec_id == AV_CODEC_ID_PCM_F64LE;
384 static int mov_pcm_be_gt16(enum AVCodecID codec_id)
386 return codec_id == AV_CODEC_ID_PCM_S24BE ||
387 codec_id == AV_CODEC_ID_PCM_S32BE ||
388 codec_id == AV_CODEC_ID_PCM_F32BE ||
389 codec_id == AV_CODEC_ID_PCM_F64BE;
392 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
394 int64_t pos = avio_tell(pb);
396 avio_wl32(pb, track->tag); // store it byteswapped
397 track->enc->codec_tag = av_bswap16(track->tag >> 16);
398 ff_put_wav_header(pb, track->enc);
399 return update_size(pb, pos);
402 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
404 int64_t pos = avio_tell(pb);
406 ffio_wfourcc(pb, "wfex");
407 ff_put_wav_header(pb, track->enc);
408 return update_size(pb, pos);
411 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
413 uint32_t layout_tag, bitmap;
414 int64_t pos = avio_tell(pb);
416 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
417 track->enc->channel_layout,
420 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
421 "lack of channel information\n");
425 avio_wb32(pb, 0); // Size
426 ffio_wfourcc(pb, "chan"); // Type
427 avio_w8(pb, 0); // Version
428 avio_wb24(pb, 0); // Flags
429 avio_wb32(pb, layout_tag); // mChannelLayoutTag
430 avio_wb32(pb, bitmap); // mChannelBitmap
431 avio_wb32(pb, 0); // mNumberChannelDescriptions
433 return update_size(pb, pos);
436 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
438 int64_t pos = avio_tell(pb);
440 avio_wb32(pb, 0); /* size */
441 ffio_wfourcc(pb, "wave");
443 if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
444 avio_wb32(pb, 12); /* size */
445 ffio_wfourcc(pb, "frma");
446 avio_wl32(pb, track->tag);
449 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
450 /* useless atom needed by mplayer, ipod, not needed by quicktime */
451 avio_wb32(pb, 12); /* size */
452 ffio_wfourcc(pb, "mp4a");
454 mov_write_esds_tag(pb, track);
455 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
456 mov_write_enda_tag(pb);
457 } else if (mov_pcm_be_gt16(track->enc->codec_id)) {
458 mov_write_enda_tag_be(pb);
459 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
460 mov_write_amr_tag(pb, track);
461 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
462 mov_write_ac3_tag(pb, track);
463 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
464 track->enc->codec_id == AV_CODEC_ID_QDM2) {
465 mov_write_extradata_tag(pb, track);
466 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
467 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
468 mov_write_ms_tag(pb, track);
471 avio_wb32(pb, 8); /* size */
472 avio_wb32(pb, 0); /* null tag */
474 return update_size(pb, pos);
477 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
480 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
481 int unescaped_size, seq_found = 0;
482 int level = 0, interlace = 0;
483 int packet_seq = track->vc1_info.packet_seq;
484 int packet_entry = track->vc1_info.packet_entry;
485 int slices = track->vc1_info.slices;
488 if (track->start_dts == AV_NOPTS_VALUE) {
489 /* No packets written yet, vc1_info isn't authoritative yet. */
490 /* Assume inline sequence and entry headers. This will be
491 * overwritten at the end if the file is seekable. */
492 packet_seq = packet_entry = 1;
495 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
497 return AVERROR(ENOMEM);
498 start = find_next_marker(track->vos_data, end);
499 for (next = start; next < end; start = next) {
502 next = find_next_marker(start + 4, end);
503 size = next - start - 4;
506 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
507 init_get_bits(&gb, unescaped, 8 * unescaped_size);
508 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
509 int profile = get_bits(&gb, 2);
510 if (profile != PROFILE_ADVANCED) {
512 return AVERROR(ENOSYS);
515 level = get_bits(&gb, 3);
516 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
518 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
519 skip_bits(&gb, 1); /* broadcast */
520 interlace = get_bits1(&gb);
521 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
526 return AVERROR(ENOSYS);
529 init_put_bits(&pbc, buf, 7);
530 /* VC1DecSpecStruc */
531 put_bits(&pbc, 4, 12); /* profile - advanced */
532 put_bits(&pbc, 3, level);
533 put_bits(&pbc, 1, 0); /* reserved */
534 /* VC1AdvDecSpecStruc */
535 put_bits(&pbc, 3, level);
536 put_bits(&pbc, 1, 0); /* cbr */
537 put_bits(&pbc, 6, 0); /* reserved */
538 put_bits(&pbc, 1, !interlace); /* no interlace */
539 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
540 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
541 put_bits(&pbc, 1, !slices); /* no slice code */
542 put_bits(&pbc, 1, 0); /* no bframe */
543 put_bits(&pbc, 1, 0); /* reserved */
544 put_bits32(&pbc, track->enc->time_base.den); /* framerate */
545 flush_put_bits(&pbc);
552 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
554 uint8_t buf[7] = { 0 };
557 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
560 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
561 ffio_wfourcc(pb, "dvc1");
562 track->vc1_info.struct_offset = avio_tell(pb);
563 avio_write(pb, buf, sizeof(buf));
564 avio_write(pb, track->vos_data, track->vos_len);
569 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
571 avio_wb32(pb, track->vos_len + 8);
572 ffio_wfourcc(pb, "glbl");
573 avio_write(pb, track->vos_data, track->vos_len);
574 return 8 + track->vos_len;
578 * Compute flags for 'lpcm' tag.
579 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
581 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
584 case AV_CODEC_ID_PCM_F32BE:
585 case AV_CODEC_ID_PCM_F64BE:
587 case AV_CODEC_ID_PCM_F32LE:
588 case AV_CODEC_ID_PCM_F64LE:
590 case AV_CODEC_ID_PCM_U8:
592 case AV_CODEC_ID_PCM_S16BE:
593 case AV_CODEC_ID_PCM_S24BE:
594 case AV_CODEC_ID_PCM_S32BE:
596 case AV_CODEC_ID_PCM_S8:
597 case AV_CODEC_ID_PCM_S16LE:
598 case AV_CODEC_ID_PCM_S24LE:
599 case AV_CODEC_ID_PCM_S32LE:
606 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
610 if (cluster_idx >= track->entry)
613 if (cluster_idx + 1 == track->entry)
614 next_dts = track->track_duration + track->start_dts;
616 next_dts = track->cluster[cluster_idx + 1].dts;
618 next_dts -= track->cluster[cluster_idx].dts;
620 av_assert0(next_dts >= 0);
621 av_assert0(next_dts <= INT_MAX);
626 static int get_samples_per_packet(MOVTrack *track)
628 int i, first_duration;
630 // return track->enc->frame_size;
632 /* use 1 for raw PCM */
633 if (!track->audio_vbr)
636 /* check to see if duration is constant for all clusters */
639 first_duration = get_cluster_duration(track, 0);
640 for (i = 1; i < track->entry; i++) {
641 if (get_cluster_duration(track, i) != first_duration)
644 return first_duration;
647 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
649 int64_t pos = avio_tell(pb);
651 uint32_t tag = track->tag;
653 if (track->mode == MODE_MOV) {
654 if (track->timescale > UINT16_MAX) {
655 if (mov_get_lpcm_flags(track->enc->codec_id))
656 tag = AV_RL32("lpcm");
658 } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
659 mov_pcm_be_gt16(track->enc->codec_id) ||
660 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
661 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
662 track->enc->codec_id == AV_CODEC_ID_QDM2) {
667 avio_wb32(pb, 0); /* size */
668 avio_wl32(pb, tag); // store it byteswapped
669 avio_wb32(pb, 0); /* Reserved */
670 avio_wb16(pb, 0); /* Reserved */
671 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
673 /* SoundDescription */
674 avio_wb16(pb, version); /* Version */
675 avio_wb16(pb, 0); /* Revision level */
676 avio_wb32(pb, 0); /* Reserved */
681 avio_wb16(pb, 0xfffe);
683 avio_wb32(pb, 0x00010000);
685 avio_wb64(pb, av_double2int(track->enc->sample_rate));
686 avio_wb32(pb, track->enc->channels);
687 avio_wb32(pb, 0x7F000000);
688 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
689 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
690 avio_wb32(pb, track->sample_size);
691 avio_wb32(pb, get_samples_per_packet(track));
693 if (track->mode == MODE_MOV) {
694 avio_wb16(pb, track->enc->channels);
695 if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
696 track->enc->codec_id == AV_CODEC_ID_PCM_S8)
697 avio_wb16(pb, 8); /* bits per sample */
700 avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
701 } else { /* reserved for mp4/3gp */
707 avio_wb16(pb, 0); /* packet size (= 0) */
708 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
709 track->enc->sample_rate : 0);
710 avio_wb16(pb, 0); /* Reserved */
713 if (version == 1) { /* SoundDescription V1 extended info */
714 if (mov_pcm_le_gt16(track->enc->codec_id) ||
715 mov_pcm_be_gt16(track->enc->codec_id))
716 avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
718 avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
719 avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
720 avio_wb32(pb, track->sample_size); /* Bytes per frame */
721 avio_wb32(pb, 2); /* Bytes per sample */
724 if (track->mode == MODE_MOV &&
725 (track->enc->codec_id == AV_CODEC_ID_AAC ||
726 track->enc->codec_id == AV_CODEC_ID_AC3 ||
727 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
728 track->enc->codec_id == AV_CODEC_ID_ALAC ||
729 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
730 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
731 track->enc->codec_id == AV_CODEC_ID_QDM2 ||
732 (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
733 (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
734 mov_write_wave_tag(pb, track);
735 else if (track->tag == MKTAG('m','p','4','a'))
736 mov_write_esds_tag(pb, track);
737 else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
738 mov_write_amr_tag(pb, track);
739 else if (track->enc->codec_id == AV_CODEC_ID_AC3)
740 mov_write_ac3_tag(pb, track);
741 else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
742 mov_write_extradata_tag(pb, track);
743 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
744 mov_write_wfex_tag(pb, track);
745 else if (track->vos_len > 0)
746 mov_write_glbl_tag(pb, track);
748 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
749 mov_write_chan_tag(pb, track);
751 return update_size(pb, pos);
754 static int mov_write_d263_tag(AVIOContext *pb)
756 avio_wb32(pb, 0xf); /* size */
757 ffio_wfourcc(pb, "d263");
758 ffio_wfourcc(pb, "FFMP");
759 avio_w8(pb, 0); /* decoder version */
760 /* FIXME use AVCodecContext level/profile, when encoder will set values */
761 avio_w8(pb, 0xa); /* level */
762 avio_w8(pb, 0); /* profile */
766 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
768 int64_t pos = avio_tell(pb);
771 ffio_wfourcc(pb, "avcC");
772 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
773 return update_size(pb, pos);
776 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
778 int64_t pos = avio_tell(pb);
781 ffio_wfourcc(pb, "hvcC");
782 ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
783 return update_size(pb, pos);
786 /* also used by all avid codecs (dv, imx, meridien) and their variants */
787 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
790 avio_wb32(pb, 24); /* size */
791 ffio_wfourcc(pb, "ACLR");
792 ffio_wfourcc(pb, "ACLR");
793 ffio_wfourcc(pb, "0001");
794 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
795 avio_wb32(pb, 0); /* unknown */
797 avio_wb32(pb, 24); /* size */
798 ffio_wfourcc(pb, "APRG");
799 ffio_wfourcc(pb, "APRG");
800 ffio_wfourcc(pb, "0001");
801 avio_wb32(pb, 1); /* unknown */
802 avio_wb32(pb, 0); /* unknown */
804 avio_wb32(pb, 120); /* size */
805 ffio_wfourcc(pb, "ARES");
806 ffio_wfourcc(pb, "ARES");
807 ffio_wfourcc(pb, "0001");
808 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
809 avio_wb32(pb, track->enc->width);
810 /* values below are based on samples created with quicktime and avid codecs */
811 if (track->vos_data[5] & 2) { // interlaced
812 avio_wb32(pb, track->enc->height / 2);
813 avio_wb32(pb, 2); /* unknown */
814 avio_wb32(pb, 0); /* unknown */
815 avio_wb32(pb, 4); /* unknown */
817 avio_wb32(pb, track->enc->height);
818 avio_wb32(pb, 1); /* unknown */
819 avio_wb32(pb, 0); /* unknown */
820 if (track->enc->height == 1080)
821 avio_wb32(pb, 5); /* unknown */
823 avio_wb32(pb, 6); /* unknown */
826 for (i = 0; i < 10; i++)
829 /* extra padding for stsd needed */
834 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
836 int tag = track->enc->codec_tag;
838 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
841 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
842 else if (track->enc->codec_id == AV_CODEC_ID_HEVC) tag = MKTAG('h','e','v','1');
843 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
844 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
845 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
846 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
847 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
848 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
853 static const AVCodecTag codec_ipod_tags[] = {
854 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
855 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
856 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
857 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
858 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
859 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
860 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
861 { AV_CODEC_ID_NONE, 0 },
864 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
866 int tag = track->enc->codec_tag;
868 // keep original tag for subs, ipod supports both formats
869 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
870 (tag == MKTAG('t', 'x', '3', 'g') ||
871 tag == MKTAG('t', 'e', 'x', 't'))))
872 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
874 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
875 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
876 "Quicktime/Ipod might not play the file\n");
881 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
885 if (track->enc->width == 720) { /* SD */
886 if (track->enc->height == 480) { /* NTSC */
887 if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
888 else tag = MKTAG('d','v','c',' ');
889 }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
890 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
891 else tag = MKTAG('d','v','p','p');
892 } else if (track->enc->height == 720) { /* HD 720 line */
893 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
894 else tag = MKTAG('d','v','h','p');
895 } else if (track->enc->height == 1080) { /* HD 1080 line */
896 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
897 else tag = MKTAG('d','v','h','6');
899 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
906 static AVRational find_fps(AVFormatContext *s, AVStream *st)
908 AVRational rate = {st->codec->time_base.den, st->codec->time_base.num};
909 /* if the codec time base makes no sense, try to fallback on stream frame rate */
910 if (av_timecode_check_frame_rate(rate) < 0) {
911 av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
912 rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
913 rate = st->avg_frame_rate;
919 static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
921 int tag = track->enc->codec_tag;
922 int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
923 AVStream *st = track->st;
924 int rate = av_q2d(find_fps(s, st));
927 tag = MKTAG('m', '2', 'v', '1'); //fallback tag
929 if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) {
930 if (track->enc->width == 1280 && track->enc->height == 720) {
932 if (rate == 24) tag = MKTAG('x','d','v','4');
933 else if (rate == 25) tag = MKTAG('x','d','v','5');
934 else if (rate == 30) tag = MKTAG('x','d','v','1');
935 else if (rate == 50) tag = MKTAG('x','d','v','a');
936 else if (rate == 60) tag = MKTAG('x','d','v','9');
938 } else if (track->enc->width == 1440 && track->enc->height == 1080) {
940 if (rate == 24) tag = MKTAG('x','d','v','6');
941 else if (rate == 25) tag = MKTAG('x','d','v','7');
942 else if (rate == 30) tag = MKTAG('x','d','v','8');
944 if (rate == 25) tag = MKTAG('x','d','v','3');
945 else if (rate == 30) tag = MKTAG('x','d','v','2');
947 } else if (track->enc->width == 1920 && track->enc->height == 1080) {
949 if (rate == 24) tag = MKTAG('x','d','v','d');
950 else if (rate == 25) tag = MKTAG('x','d','v','e');
951 else if (rate == 30) tag = MKTAG('x','d','v','f');
953 if (rate == 25) tag = MKTAG('x','d','v','c');
954 else if (rate == 30) tag = MKTAG('x','d','v','b');
957 } else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
958 if (track->enc->width == 1280 && track->enc->height == 720) {
960 if (rate == 24) tag = MKTAG('x','d','5','4');
961 else if (rate == 25) tag = MKTAG('x','d','5','5');
962 else if (rate == 30) tag = MKTAG('x','d','5','1');
963 else if (rate == 50) tag = MKTAG('x','d','5','a');
964 else if (rate == 60) tag = MKTAG('x','d','5','9');
966 } else if (track->enc->width == 1920 && track->enc->height == 1080) {
968 if (rate == 24) tag = MKTAG('x','d','5','d');
969 else if (rate == 25) tag = MKTAG('x','d','5','e');
970 else if (rate == 30) tag = MKTAG('x','d','5','f');
972 if (rate == 25) tag = MKTAG('x','d','5','c');
973 else if (rate == 30) tag = MKTAG('x','d','5','b');
981 static const struct {
982 enum AVPixelFormat pix_fmt;
985 } mov_pix_fmt_tags[] = {
986 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
987 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
988 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
989 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
990 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
991 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
992 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
993 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
994 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
995 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
996 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
997 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
998 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
999 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
1000 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1003 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
1005 int tag = track->enc->codec_tag;
1008 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1009 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
1010 tag = mov_pix_fmt_tags[i].tag;
1011 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
1012 if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
1020 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1022 int tag = track->enc->codec_tag;
1024 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1025 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
1026 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
1027 track->enc->codec_id == AV_CODEC_ID_H263 ||
1028 track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1029 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
1030 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
1031 tag = mov_get_dv_codec_tag(s, track);
1032 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
1033 tag = mov_get_rawvideo_codec_tag(s, track);
1034 else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1035 tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1036 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1037 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
1038 if (!tag) { // if no mac fcc found, try with Microsoft tags
1039 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
1041 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1042 "the file may be unplayable!\n");
1044 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1045 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
1046 if (!tag) { // if no mac fcc found, try with Microsoft tags
1047 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
1049 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1050 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1051 "the file may be unplayable!\n");
1054 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1055 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
1061 static const AVCodecTag codec_3gp_tags[] = {
1062 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
1063 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
1064 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
1065 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
1066 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
1067 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
1068 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1069 { AV_CODEC_ID_NONE, 0 },
1072 static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
1073 { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
1074 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
1075 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
1076 { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
1077 { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
1078 { AV_CODEC_ID_NONE, 0 },
1081 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
1085 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
1086 tag = mp4_get_codec_tag(s, track);
1087 else if (track->mode == MODE_ISM) {
1088 tag = mp4_get_codec_tag(s, track);
1089 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
1090 tag = MKTAG('w', 'm', 'a', ' ');
1091 } else if (track->mode == MODE_IPOD)
1092 tag = ipod_get_codec_tag(s, track);
1093 else if (track->mode & MODE_3GP)
1094 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
1095 else if (track->mode == MODE_F4V)
1096 tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
1098 tag = mov_get_codec_tag(s, track);
1103 /** Write uuid atom.
1104 * Needed to make file play in iPods running newest firmware
1105 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1107 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
1110 ffio_wfourcc(pb, "uuid");
1111 avio_wb32(pb, 0x6b6840f2);
1112 avio_wb32(pb, 0x5f244fc5);
1113 avio_wb32(pb, 0xba39a51b);
1114 avio_wb32(pb, 0xcf0323f3);
1119 static const uint16_t fiel_data[] = {
1120 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1123 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
1125 unsigned mov_field_order = 0;
1126 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
1127 mov_field_order = fiel_data[track->enc->field_order];
1131 ffio_wfourcc(pb, "fiel");
1132 avio_wb16(pb, mov_field_order);
1136 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
1138 int64_t pos = avio_tell(pb);
1139 avio_wb32(pb, 0); /* size */
1140 avio_wl32(pb, track->tag); // store it byteswapped
1141 avio_wb32(pb, 0); /* Reserved */
1142 avio_wb16(pb, 0); /* Reserved */
1143 avio_wb16(pb, 1); /* Data-reference index */
1145 if (track->enc->extradata_size)
1146 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1148 return update_size(pb, pos);
1151 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1154 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
1155 track->enc->sample_aspect_ratio.den, INT_MAX);
1158 ffio_wfourcc(pb, "pasp");
1159 avio_wb32(pb, sar.num);
1160 avio_wb32(pb, sar.den);
1164 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1166 int xdcam_res = (track->enc->width == 1280 && track->enc->height == 720)
1167 || (track->enc->width == 1440 && track->enc->height == 1080)
1168 || (track->enc->width == 1920 && track->enc->height == 1080);
1170 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name) {
1171 av_strlcpy(compressor_name, track->enc->codec->name, 32);
1172 } else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1173 int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
1174 AVStream *st = track->st;
1175 int rate = av_q2d(find_fps(NULL, st));
1176 av_strlcatf(compressor_name, len, "XDCAM");
1177 if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
1178 av_strlcatf(compressor_name, len, " HD422");
1179 } else if(track->enc->width == 1440) {
1180 av_strlcatf(compressor_name, len, " HD");
1182 av_strlcatf(compressor_name, len, " EX");
1184 av_strlcatf(compressor_name, len, " %d%c", track->enc->height, interlaced ? 'i' : 'p');
1186 av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1190 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
1192 int64_t pos = avio_tell(pb);
1193 char compressor_name[32] = { 0 };
1195 avio_wb32(pb, 0); /* size */
1196 avio_wl32(pb, track->tag); // store it byteswapped
1197 avio_wb32(pb, 0); /* Reserved */
1198 avio_wb16(pb, 0); /* Reserved */
1199 avio_wb16(pb, 1); /* Data-reference index */
1201 avio_wb16(pb, 0); /* Codec stream version */
1202 avio_wb16(pb, 0); /* Codec stream revision (=0) */
1203 if (track->mode == MODE_MOV) {
1204 ffio_wfourcc(pb, "FFMP"); /* Vendor */
1205 if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1206 avio_wb32(pb, 0); /* Temporal Quality */
1207 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1209 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1210 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1213 avio_wb32(pb, 0); /* Reserved */
1214 avio_wb32(pb, 0); /* Reserved */
1215 avio_wb32(pb, 0); /* Reserved */
1217 avio_wb16(pb, track->enc->width); /* Video width */
1218 avio_wb16(pb, track->height); /* Video height */
1219 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1220 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1221 avio_wb32(pb, 0); /* Data size (= 0) */
1222 avio_wb16(pb, 1); /* Frame count (= 1) */
1224 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1225 find_compressor(compressor_name, 32, track);
1226 avio_w8(pb, strlen(compressor_name));
1227 avio_write(pb, compressor_name, 31);
1229 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1230 avio_wb16(pb, track->enc->bits_per_coded_sample);
1232 avio_wb16(pb, 0x18); /* Reserved */
1233 avio_wb16(pb, 0xffff); /* Reserved */
1234 if (track->tag == MKTAG('m','p','4','v'))
1235 mov_write_esds_tag(pb, track);
1236 else if (track->enc->codec_id == AV_CODEC_ID_H263)
1237 mov_write_d263_tag(pb);
1238 else if (track->enc->codec_id == AV_CODEC_ID_AVUI ||
1239 track->enc->codec_id == AV_CODEC_ID_SVQ3) {
1240 mov_write_extradata_tag(pb, track);
1242 } else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
1243 mov_write_avid_tag(pb, track);
1244 else if (track->enc->codec_id == AV_CODEC_ID_HEVC)
1245 mov_write_hvcc_tag(pb, track);
1246 else if (track->enc->codec_id == AV_CODEC_ID_H264) {
1247 mov_write_avcc_tag(pb, track);
1248 if (track->mode == MODE_IPOD)
1249 mov_write_uuid_tag_ipod(pb);
1250 } else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1251 mov_write_dvc1_tag(pb, track);
1252 else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
1253 track->enc->codec_id == AV_CODEC_ID_VP6A) {
1254 /* Don't write any potential extradata here - the cropping
1255 * is signalled via the normal width/height fields. */
1256 } else if (track->vos_len > 0)
1257 mov_write_glbl_tag(pb, track);
1259 if (track->enc->codec_id != AV_CODEC_ID_H264 &&
1260 track->enc->codec_id != AV_CODEC_ID_MPEG4 &&
1261 track->enc->codec_id != AV_CODEC_ID_DNXHD)
1262 if (track->enc->field_order != AV_FIELD_UNKNOWN)
1263 mov_write_fiel_tag(pb, track);
1265 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1266 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1267 mov_write_pasp_tag(pb, track);
1270 return update_size(pb, pos);
1273 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1275 int64_t pos = avio_tell(pb);
1276 avio_wb32(pb, 0); /* size */
1277 ffio_wfourcc(pb, "rtp ");
1278 avio_wb32(pb, 0); /* Reserved */
1279 avio_wb16(pb, 0); /* Reserved */
1280 avio_wb16(pb, 1); /* Data-reference index */
1282 avio_wb16(pb, 1); /* Hint track version */
1283 avio_wb16(pb, 1); /* Highest compatible version */
1284 avio_wb32(pb, track->max_packet_size); /* Max packet size */
1286 avio_wb32(pb, 12); /* size */
1287 ffio_wfourcc(pb, "tims");
1288 avio_wb32(pb, track->timescale);
1290 return update_size(pb, pos);
1293 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1295 int64_t pos = avio_tell(pb);
1297 int frame_duration = av_rescale(track->timescale, track->enc->time_base.num, track->enc->time_base.den);
1298 int nb_frames = 1.0/av_q2d(track->enc->time_base) + 0.5;
1300 if (nb_frames > 255) {
1301 av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
1302 return AVERROR(EINVAL);
1305 avio_wb32(pb, 0); /* size */
1306 ffio_wfourcc(pb, "tmcd"); /* Data format */
1307 avio_wb32(pb, 0); /* Reserved */
1308 avio_wb32(pb, 1); /* Data reference index */
1309 avio_wb32(pb, 0); /* Flags */
1310 avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
1311 avio_wb32(pb, track->timescale); /* Timescale */
1312 avio_wb32(pb, frame_duration); /* Frame duration */
1313 avio_w8(pb, nb_frames); /* Number of frames */
1314 avio_wb24(pb, 0); /* Reserved */
1315 /* TODO: source reference string */
1318 avio_wb32(pb, 0); /* size */
1319 ffio_wfourcc(pb, "tmcd"); /* Data format */
1320 avio_wb32(pb, 0); /* Reserved */
1321 avio_wb32(pb, 1); /* Data reference index */
1322 if (track->enc->extradata_size)
1323 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1325 return update_size(pb, pos);
1328 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1330 int64_t pos = avio_tell(pb);
1331 avio_wb32(pb, 0); /* size */
1332 ffio_wfourcc(pb, "stsd");
1333 avio_wb32(pb, 0); /* version & flags */
1334 avio_wb32(pb, 1); /* entry count */
1335 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1336 mov_write_video_tag(pb, track);
1337 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1338 mov_write_audio_tag(pb, track);
1339 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1340 mov_write_subtitle_tag(pb, track);
1341 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1342 mov_write_rtp_tag(pb, track);
1343 else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1344 mov_write_tmcd_tag(pb, track);
1345 return update_size(pb, pos);
1348 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1350 MOVStts *ctts_entries;
1351 uint32_t entries = 0;
1355 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1356 ctts_entries[0].count = 1;
1357 ctts_entries[0].duration = track->cluster[0].cts;
1358 for (i = 1; i < track->entry; i++) {
1359 if (track->cluster[i].cts == ctts_entries[entries].duration) {
1360 ctts_entries[entries].count++; /* compress */
1363 ctts_entries[entries].duration = track->cluster[i].cts;
1364 ctts_entries[entries].count = 1;
1367 entries++; /* last one */
1368 atom_size = 16 + (entries * 8);
1369 avio_wb32(pb, atom_size); /* size */
1370 ffio_wfourcc(pb, "ctts");
1371 avio_wb32(pb, 0); /* version & flags */
1372 avio_wb32(pb, entries); /* entry count */
1373 for (i = 0; i < entries; i++) {
1374 avio_wb32(pb, ctts_entries[i].count);
1375 avio_wb32(pb, ctts_entries[i].duration);
1377 av_free(ctts_entries);
1381 /* Time to sample atom */
1382 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1384 MOVStts *stts_entries;
1385 uint32_t entries = -1;
1389 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1390 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1391 stts_entries[0].count = track->sample_count;
1392 stts_entries[0].duration = 1;
1395 stts_entries = track->entry ?
1396 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1398 for (i = 0; i < track->entry; i++) {
1399 int duration = get_cluster_duration(track, i);
1400 if (i && duration == stts_entries[entries].duration) {
1401 stts_entries[entries].count++; /* compress */
1404 stts_entries[entries].duration = duration;
1405 stts_entries[entries].count = 1;
1408 entries++; /* last one */
1410 atom_size = 16 + (entries * 8);
1411 avio_wb32(pb, atom_size); /* size */
1412 ffio_wfourcc(pb, "stts");
1413 avio_wb32(pb, 0); /* version & flags */
1414 avio_wb32(pb, entries); /* entry count */
1415 for (i = 0; i < entries; i++) {
1416 avio_wb32(pb, stts_entries[i].count);
1417 avio_wb32(pb, stts_entries[i].duration);
1419 av_free(stts_entries);
1423 static int mov_write_dref_tag(AVIOContext *pb)
1425 avio_wb32(pb, 28); /* size */
1426 ffio_wfourcc(pb, "dref");
1427 avio_wb32(pb, 0); /* version & flags */
1428 avio_wb32(pb, 1); /* entry count */
1430 avio_wb32(pb, 0xc); /* size */
1431 //FIXME add the alis and rsrc atom
1432 ffio_wfourcc(pb, "url ");
1433 avio_wb32(pb, 1); /* version & flags */
1438 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1440 int64_t pos = avio_tell(pb);
1441 avio_wb32(pb, 0); /* size */
1442 ffio_wfourcc(pb, "stbl");
1443 mov_write_stsd_tag(pb, track);
1444 mov_write_stts_tag(pb, track);
1445 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1446 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1447 track->has_keyframes && track->has_keyframes < track->entry)
1448 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1449 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1450 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1451 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1452 track->flags & MOV_TRACK_CTTS)
1453 mov_write_ctts_tag(pb, track);
1454 mov_write_stsc_tag(pb, track);
1455 mov_write_stsz_tag(pb, track);
1456 mov_write_stco_tag(pb, track);
1457 return update_size(pb, pos);
1460 static int mov_write_dinf_tag(AVIOContext *pb)
1462 int64_t pos = avio_tell(pb);
1463 avio_wb32(pb, 0); /* size */
1464 ffio_wfourcc(pb, "dinf");
1465 mov_write_dref_tag(pb);
1466 return update_size(pb, pos);
1469 static int mov_write_nmhd_tag(AVIOContext *pb)
1472 ffio_wfourcc(pb, "nmhd");
1477 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
1479 int64_t pos = avio_tell(pb);
1480 const char *font = "Lucida Grande";
1481 avio_wb32(pb, 0); /* size */
1482 ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
1483 avio_wb32(pb, 0); /* version & flags */
1484 avio_wb16(pb, 0); /* text font */
1485 avio_wb16(pb, 0); /* text face */
1486 avio_wb16(pb, 12); /* text size */
1487 avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
1488 avio_wb16(pb, 0x0000); /* text color (red) */
1489 avio_wb16(pb, 0x0000); /* text color (green) */
1490 avio_wb16(pb, 0x0000); /* text color (blue) */
1491 avio_wb16(pb, 0xffff); /* background color (red) */
1492 avio_wb16(pb, 0xffff); /* background color (green) */
1493 avio_wb16(pb, 0xffff); /* background color (blue) */
1494 avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
1495 avio_write(pb, font, strlen(font)); /* font name */
1496 return update_size(pb, pos);
1499 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
1501 int64_t pos = avio_tell(pb);
1502 avio_wb32(pb, 0); /* size */
1503 ffio_wfourcc(pb, "gmhd");
1504 avio_wb32(pb, 0x18); /* gmin size */
1505 ffio_wfourcc(pb, "gmin");/* generic media info */
1506 avio_wb32(pb, 0); /* version & flags */
1507 avio_wb16(pb, 0x40); /* graphics mode = */
1508 avio_wb16(pb, 0x8000); /* opColor (r?) */
1509 avio_wb16(pb, 0x8000); /* opColor (g?) */
1510 avio_wb16(pb, 0x8000); /* opColor (b?) */
1511 avio_wb16(pb, 0); /* balance */
1512 avio_wb16(pb, 0); /* reserved */
1515 * This special text atom is required for
1516 * Apple Quicktime chapters. The contents
1517 * don't appear to be documented, so the
1518 * bytes are copied verbatim.
1520 if (track->tag != MKTAG('c','6','0','8')) {
1521 avio_wb32(pb, 0x2C); /* size */
1522 ffio_wfourcc(pb, "text");
1523 avio_wb16(pb, 0x01);
1524 avio_wb32(pb, 0x00);
1525 avio_wb32(pb, 0x00);
1526 avio_wb32(pb, 0x00);
1527 avio_wb32(pb, 0x01);
1528 avio_wb32(pb, 0x00);
1529 avio_wb32(pb, 0x00);
1530 avio_wb32(pb, 0x00);
1531 avio_wb32(pb, 0x00004000);
1532 avio_wb16(pb, 0x0000);
1535 if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1536 int64_t tmcd_pos = avio_tell(pb);
1537 avio_wb32(pb, 0); /* size */
1538 ffio_wfourcc(pb, "tmcd");
1539 mov_write_tcmi_tag(pb, track);
1540 update_size(pb, tmcd_pos);
1542 return update_size(pb, pos);
1545 static int mov_write_smhd_tag(AVIOContext *pb)
1547 avio_wb32(pb, 16); /* size */
1548 ffio_wfourcc(pb, "smhd");
1549 avio_wb32(pb, 0); /* version & flags */
1550 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1551 avio_wb16(pb, 0); /* reserved */
1555 static int mov_write_vmhd_tag(AVIOContext *pb)
1557 avio_wb32(pb, 0x14); /* size (always 0x14) */
1558 ffio_wfourcc(pb, "vmhd");
1559 avio_wb32(pb, 0x01); /* version & flags */
1560 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1564 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1566 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1567 int64_t pos = avio_tell(pb);
1571 descr = "DataHandler";
1574 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1575 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1577 descr = "VideoHandler";
1578 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1580 descr = "SoundHandler";
1581 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1582 if (track->tag == MKTAG('c','6','0','8')) {
1584 descr = "ClosedCaptionHandler";
1586 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1587 else hdlr_type = "text";
1588 descr = "SubtitleHandler";
1590 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1592 descr = "HintHandler";
1593 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1595 descr = "TimeCodeHandler";
1598 av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1599 track->enc->codec_tag);
1601 av_log(track->enc, AV_LOG_WARNING,
1602 "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1603 tag_buf, track->enc->codec_tag);
1607 avio_wb32(pb, 0); /* size */
1608 ffio_wfourcc(pb, "hdlr");
1609 avio_wb32(pb, 0); /* Version & flags */
1610 avio_write(pb, hdlr, 4); /* handler */
1611 ffio_wfourcc(pb, hdlr_type); /* handler type */
1612 avio_wb32(pb, 0); /* reserved */
1613 avio_wb32(pb, 0); /* reserved */
1614 avio_wb32(pb, 0); /* reserved */
1615 if (!track || track->mode == MODE_MOV)
1616 avio_w8(pb, strlen(descr)); /* pascal string */
1617 avio_write(pb, descr, strlen(descr)); /* handler description */
1618 if (track && track->mode != MODE_MOV)
1619 avio_w8(pb, 0); /* c string */
1620 return update_size(pb, pos);
1623 static int mov_write_hmhd_tag(AVIOContext *pb)
1625 /* This atom must be present, but leaving the values at zero
1626 * seems harmless. */
1627 avio_wb32(pb, 28); /* size */
1628 ffio_wfourcc(pb, "hmhd");
1629 avio_wb32(pb, 0); /* version, flags */
1630 avio_wb16(pb, 0); /* maxPDUsize */
1631 avio_wb16(pb, 0); /* avgPDUsize */
1632 avio_wb32(pb, 0); /* maxbitrate */
1633 avio_wb32(pb, 0); /* avgbitrate */
1634 avio_wb32(pb, 0); /* reserved */
1638 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1640 int64_t pos = avio_tell(pb);
1641 avio_wb32(pb, 0); /* size */
1642 ffio_wfourcc(pb, "minf");
1643 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1644 mov_write_vmhd_tag(pb);
1645 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1646 mov_write_smhd_tag(pb);
1647 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1648 if (track->tag == MKTAG('t','e','x','t') || track->tag == MKTAG('c','6','0','8')) {
1649 mov_write_gmhd_tag(pb, track);
1651 mov_write_nmhd_tag(pb);
1653 } else if (track->tag == MKTAG('r','t','p',' ')) {
1654 mov_write_hmhd_tag(pb);
1655 } else if (track->tag == MKTAG('t','m','c','d')) {
1656 mov_write_gmhd_tag(pb, track);
1658 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1659 mov_write_hdlr_tag(pb, NULL);
1660 mov_write_dinf_tag(pb);
1661 mov_write_stbl_tag(pb, track);
1662 return update_size(pb, pos);
1665 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1667 int version = track->track_duration < INT32_MAX ? 0 : 1;
1669 if (track->mode == MODE_ISM)
1672 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1673 ffio_wfourcc(pb, "mdhd");
1674 avio_w8(pb, version);
1675 avio_wb24(pb, 0); /* flags */
1677 avio_wb64(pb, track->time);
1678 avio_wb64(pb, track->time);
1680 avio_wb32(pb, track->time); /* creation time */
1681 avio_wb32(pb, track->time); /* modification time */
1683 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1685 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1687 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1688 avio_wb16(pb, track->language); /* language */
1689 avio_wb16(pb, 0); /* reserved (quality) */
1691 if (version != 0 && track->mode == MODE_MOV) {
1692 av_log(NULL, AV_LOG_ERROR,
1693 "FATAL error, file duration too long for timebase, this file will not be\n"
1694 "playable with quicktime. Choose a different timebase or a different\n"
1695 "container format\n");
1701 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1703 int64_t pos = avio_tell(pb);
1704 avio_wb32(pb, 0); /* size */
1705 ffio_wfourcc(pb, "mdia");
1706 mov_write_mdhd_tag(pb, track);
1707 mov_write_hdlr_tag(pb, track);
1708 mov_write_minf_tag(pb, track);
1709 return update_size(pb, pos);
1712 /* transformation matrix
1716 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
1717 int16_t d, int16_t tx, int16_t ty)
1719 avio_wb32(pb, a << 16); /* 16.16 format */
1720 avio_wb32(pb, b << 16); /* 16.16 format */
1721 avio_wb32(pb, 0); /* u in 2.30 format */
1722 avio_wb32(pb, c << 16); /* 16.16 format */
1723 avio_wb32(pb, d << 16); /* 16.16 format */
1724 avio_wb32(pb, 0); /* v in 2.30 format */
1725 avio_wb32(pb, tx << 16); /* 16.16 format */
1726 avio_wb32(pb, ty << 16); /* 16.16 format */
1727 avio_wb32(pb, 1 << 30); /* w in 2.30 format */
1730 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1732 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1733 track->timescale, AV_ROUND_UP);
1734 int version = duration < INT32_MAX ? 0 : 1;
1737 if (track->mode == MODE_ISM)
1740 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1741 ffio_wfourcc(pb, "tkhd");
1742 avio_w8(pb, version);
1743 avio_wb24(pb, (track->flags & MOV_TRACK_ENABLED) ?
1744 MOV_TKHD_FLAG_ENABLED | MOV_TKHD_FLAG_IN_MOVIE :
1745 MOV_TKHD_FLAG_IN_MOVIE);
1747 avio_wb64(pb, track->time);
1748 avio_wb64(pb, track->time);
1750 avio_wb32(pb, track->time); /* creation time */
1751 avio_wb32(pb, track->time); /* modification time */
1753 avio_wb32(pb, track->track_id); /* track-id */
1754 avio_wb32(pb, 0); /* reserved */
1756 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1758 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1760 avio_wb32(pb, 0); /* reserved */
1761 avio_wb32(pb, 0); /* reserved */
1762 avio_wb16(pb, 0); /* layer */
1763 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1764 /* Volume, only for audio */
1765 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1766 avio_wb16(pb, 0x0100);
1769 avio_wb16(pb, 0); /* reserved */
1771 /* Matrix structure */
1772 if (st && st->metadata) {
1773 AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
1774 rotation = (rot && rot->value) ? atoi(rot->value) : 0;
1776 if (rotation == 90) {
1777 write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
1778 } else if (rotation == 180) {
1779 write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
1780 } else if (rotation == 270) {
1781 write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
1783 write_matrix(pb, 1, 0, 0, 1, 0, 0);
1785 /* Track width and height, for visual only */
1786 if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1787 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1788 if (track->mode == MODE_MOV) {
1789 avio_wb32(pb, track->enc->width << 16);
1790 avio_wb32(pb, track->height << 16);
1792 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1793 if (!sample_aspect_ratio || track->height != track->enc->height)
1794 sample_aspect_ratio = 1;
1795 avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1796 avio_wb32(pb, track->height * 0x10000);
1805 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1807 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1808 track->enc->sample_aspect_ratio.den);
1810 int64_t pos = avio_tell(pb);
1812 avio_wb32(pb, 0); /* size */
1813 ffio_wfourcc(pb, "tapt");
1816 ffio_wfourcc(pb, "clef");
1818 avio_wb32(pb, width << 16);
1819 avio_wb32(pb, track->enc->height << 16);
1822 ffio_wfourcc(pb, "prof");
1824 avio_wb32(pb, width << 16);
1825 avio_wb32(pb, track->enc->height << 16);
1828 ffio_wfourcc(pb, "enof");
1830 avio_wb32(pb, track->enc->width << 16);
1831 avio_wb32(pb, track->enc->height << 16);
1833 return update_size(pb, pos);
1836 // This box seems important for the psp playback ... without it the movie seems to hang
1837 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1839 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1840 track->timescale, AV_ROUND_UP);
1841 int version = duration < INT32_MAX ? 0 : 1;
1842 int entry_size, entry_count, size;
1843 int64_t delay, start_ct = track->cluster[0].cts;
1844 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1845 track->timescale, AV_ROUND_DOWN);
1846 version |= delay < INT32_MAX ? 0 : 1;
1848 entry_size = (version == 1) ? 20 : 12;
1849 entry_count = 1 + (delay > 0);
1850 size = 24 + entry_count * entry_size;
1852 /* write the atom data */
1853 avio_wb32(pb, size);
1854 ffio_wfourcc(pb, "edts");
1855 avio_wb32(pb, size - 8);
1856 ffio_wfourcc(pb, "elst");
1857 avio_w8(pb, version);
1858 avio_wb24(pb, 0); /* flags */
1860 avio_wb32(pb, entry_count);
1861 if (delay > 0) { /* add an empty edit to delay presentation */
1863 avio_wb64(pb, delay);
1866 avio_wb32(pb, delay);
1869 avio_wb32(pb, 0x00010000);
1871 av_assert0(av_rescale_rnd(track->cluster[0].dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
1872 start_ct = -FFMIN(track->cluster[0].dts, 0); //FFMIN needed due to rounding
1878 avio_wb64(pb, duration);
1879 avio_wb64(pb, start_ct);
1881 avio_wb32(pb, duration);
1882 avio_wb32(pb, start_ct);
1884 avio_wb32(pb, 0x00010000);
1888 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1890 avio_wb32(pb, 20); // size
1891 ffio_wfourcc(pb, "tref");
1892 avio_wb32(pb, 12); // size (subatom)
1893 avio_wl32(pb, track->tref_tag);
1894 avio_wb32(pb, track->tref_id);
1898 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1899 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1901 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1902 ffio_wfourcc(pb, "uuid");
1903 ffio_wfourcc(pb, "USMT");
1904 avio_wb32(pb, 0x21d24fce);
1905 avio_wb32(pb, 0xbb88695c);
1906 avio_wb32(pb, 0xfac9c740);
1907 avio_wb32(pb, 0x1c); // another size here!
1908 ffio_wfourcc(pb, "MTDT");
1909 avio_wb32(pb, 0x00010012);
1910 avio_wb32(pb, 0x0a);
1911 avio_wb32(pb, 0x55c40000);
1917 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1919 AVFormatContext *ctx = track->rtp_ctx;
1920 char buf[1000] = "";
1923 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1924 NULL, NULL, 0, 0, ctx);
1925 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1928 avio_wb32(pb, len + 24);
1929 ffio_wfourcc(pb, "udta");
1930 avio_wb32(pb, len + 16);
1931 ffio_wfourcc(pb, "hnti");
1932 avio_wb32(pb, len + 8);
1933 ffio_wfourcc(pb, "sdp ");
1934 avio_write(pb, buf, len);
1938 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1939 MOVTrack *track, AVStream *st)
1941 int64_t pos = avio_tell(pb);
1942 avio_wb32(pb, 0); /* size */
1943 ffio_wfourcc(pb, "trak");
1944 mov_write_tkhd_tag(pb, track, st);
1945 if (supports_edts(mov))
1946 mov_write_edts_tag(pb, track); // PSP Movies and several other cases require edts box
1947 if (track->tref_tag)
1948 mov_write_tref_tag(pb, track);
1949 mov_write_mdia_tag(pb, track);
1950 if (track->mode == MODE_PSP)
1951 mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
1952 if (track->tag == MKTAG('r','t','p',' '))
1953 mov_write_udta_sdp(pb, track);
1954 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1955 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1956 if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
1957 mov_write_tapt_tag(pb, track);
1959 return update_size(pb, pos);
1962 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1964 int i, has_audio = 0, has_video = 0;
1965 int64_t pos = avio_tell(pb);
1966 int audio_profile = mov->iods_audio_profile;
1967 int video_profile = mov->iods_video_profile;
1968 for (i = 0; i < mov->nb_streams; i++) {
1969 if (mov->tracks[i].entry > 0) {
1970 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1971 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1974 if (audio_profile < 0)
1975 audio_profile = 0xFF - has_audio;
1976 if (video_profile < 0)
1977 video_profile = 0xFF - has_video;
1978 avio_wb32(pb, 0x0); /* size */
1979 ffio_wfourcc(pb, "iods");
1980 avio_wb32(pb, 0); /* version & flags */
1981 put_descr(pb, 0x10, 7);
1982 avio_wb16(pb, 0x004f);
1985 avio_w8(pb, audio_profile);
1986 avio_w8(pb, video_profile);
1988 return update_size(pb, pos);
1991 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1993 avio_wb32(pb, 0x20); /* size */
1994 ffio_wfourcc(pb, "trex");
1995 avio_wb32(pb, 0); /* version & flags */
1996 avio_wb32(pb, track->track_id); /* track ID */
1997 avio_wb32(pb, 1); /* default sample description index */
1998 avio_wb32(pb, 0); /* default sample duration */
1999 avio_wb32(pb, 0); /* default sample size */
2000 avio_wb32(pb, 0); /* default sample flags */
2004 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
2006 int64_t pos = avio_tell(pb);
2008 avio_wb32(pb, 0x0); /* size */
2009 ffio_wfourcc(pb, "mvex");
2010 for (i = 0; i < mov->nb_streams; i++)
2011 mov_write_trex_tag(pb, &mov->tracks[i]);
2012 return update_size(pb, pos);
2015 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2017 int max_track_id = 1, i;
2018 int64_t max_track_len_temp, max_track_len = 0;
2021 for (i = 0; i < mov->nb_streams; i++) {
2022 if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
2023 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
2025 mov->tracks[i].timescale,
2027 if (max_track_len < max_track_len_temp)
2028 max_track_len = max_track_len_temp;
2029 if (max_track_id < mov->tracks[i].track_id)
2030 max_track_id = mov->tracks[i].track_id;
2034 version = max_track_len < UINT32_MAX ? 0 : 1;
2035 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
2036 ffio_wfourcc(pb, "mvhd");
2037 avio_w8(pb, version);
2038 avio_wb24(pb, 0); /* flags */
2040 avio_wb64(pb, mov->time);
2041 avio_wb64(pb, mov->time);
2043 avio_wb32(pb, mov->time); /* creation time */
2044 avio_wb32(pb, mov->time); /* modification time */
2046 avio_wb32(pb, MOV_TIMESCALE);
2047 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
2049 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
2050 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
2051 avio_wb16(pb, 0); /* reserved */
2052 avio_wb32(pb, 0); /* reserved */
2053 avio_wb32(pb, 0); /* reserved */
2055 /* Matrix structure */
2056 write_matrix(pb, 1, 0, 0, 1, 0, 0);
2058 avio_wb32(pb, 0); /* reserved (preview time) */
2059 avio_wb32(pb, 0); /* reserved (preview duration) */
2060 avio_wb32(pb, 0); /* reserved (poster time) */
2061 avio_wb32(pb, 0); /* reserved (selection time) */
2062 avio_wb32(pb, 0); /* reserved (selection duration) */
2063 avio_wb32(pb, 0); /* reserved (current time) */
2064 avio_wb32(pb, max_track_id + 1); /* Next track id */
2068 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
2071 avio_wb32(pb, 33); /* size */
2072 ffio_wfourcc(pb, "hdlr");
2075 ffio_wfourcc(pb, "mdir");
2076 ffio_wfourcc(pb, "appl");
2083 /* helper function to write a data tag with the specified string as data */
2084 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
2087 int size = 16 + strlen(data);
2088 avio_wb32(pb, size); /* size */
2089 ffio_wfourcc(pb, "data");
2092 avio_write(pb, data, strlen(data));
2096 lang = ff_mov_iso639_to_lang("und", 1);
2097 avio_wb16(pb, strlen(data)); /* string length */
2098 avio_wb16(pb, lang);
2099 avio_write(pb, data, strlen(data));
2100 return strlen(data) + 4;
2104 static int mov_write_string_tag(AVIOContext *pb, const char *name,
2105 const char *value, int lang, int long_style)
2108 if (value && value[0]) {
2109 int64_t pos = avio_tell(pb);
2110 avio_wb32(pb, 0); /* size */
2111 ffio_wfourcc(pb, name);
2112 mov_write_string_data_tag(pb, value, lang, long_style);
2113 size = update_size(pb, pos);
2118 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
2119 const char *name, const char *tag,
2122 int l, lang = 0, len, len2;
2123 AVDictionaryEntry *t, *t2 = NULL;
2126 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2129 len = strlen(t->key);
2130 snprintf(tag2, sizeof(tag2), "%s-", tag);
2131 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
2132 len2 = strlen(t2->key);
2133 if (len2 == len + 4 && !strcmp(t->value, t2->value)
2134 && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
2139 return mov_write_string_tag(pb, name, t->value, lang, long_style);
2142 /* iTunes bpm number */
2143 static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
2145 AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
2146 int size = 0, tmpo = t ? atoi(t->value) : 0;
2149 avio_wb32(pb, size);
2150 ffio_wfourcc(pb, "tmpo");
2151 avio_wb32(pb, size-8); /* size */
2152 ffio_wfourcc(pb, "data");
2153 avio_wb32(pb, 0x15); //type specifier
2155 avio_wb16(pb, tmpo); // data
2160 /* iTunes track or disc number */
2161 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
2162 AVFormatContext *s, int disc)
2164 AVDictionaryEntry *t = av_dict_get(s->metadata,
2165 disc ? "disc" : "track",
2167 int size = 0, track = t ? atoi(t->value) : 0;
2170 char *slash = strchr(t->value, '/');
2172 tracks = atoi(slash + 1);
2173 avio_wb32(pb, 32); /* size */
2174 ffio_wfourcc(pb, disc ? "disk" : "trkn");
2175 avio_wb32(pb, 24); /* size */
2176 ffio_wfourcc(pb, "data");
2177 avio_wb32(pb, 0); // 8 bytes empty
2179 avio_wb16(pb, 0); // empty
2180 avio_wb16(pb, track); // track / disc number
2181 avio_wb16(pb, tracks); // total track / disc number
2182 avio_wb16(pb, 0); // empty
2188 static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
2189 const char *name, const char *tag,
2192 AVDictionaryEntry *t = NULL;
2194 int size = 24 + len;
2196 if (len != 1 && len != 4)
2199 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2201 num = atoi(t->value);
2203 avio_wb32(pb, size);
2204 ffio_wfourcc(pb, name);
2205 avio_wb32(pb, size - 8);
2206 ffio_wfourcc(pb, "data");
2207 avio_wb32(pb, 0x15);
2209 if (len==4) avio_wb32(pb, num);
2210 else avio_w8 (pb, num);
2215 /* iTunes meta data list */
2216 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
2219 int64_t pos = avio_tell(pb);
2220 avio_wb32(pb, 0); /* size */
2221 ffio_wfourcc(pb, "ilst");
2222 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
2223 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
2224 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
2225 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
2226 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
2227 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
2228 if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
2229 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
2230 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
2231 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
2232 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2233 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
2234 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
2235 mov_write_string_metadata(s, pb, "desc", "description",1);
2236 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
2237 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
2238 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
2239 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
2240 mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
2241 mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
2242 mov_write_int8_metadata (s, pb, "stik", "media_type",1);
2243 mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
2244 mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
2245 mov_write_trkn_tag(pb, mov, s, 0); // track number
2246 mov_write_trkn_tag(pb, mov, s, 1); // disc number
2247 mov_write_tmpo_tag(pb, s);
2248 return update_size(pb, pos);
2251 /* iTunes meta data tag */
2252 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
2256 int64_t pos = avio_tell(pb);
2257 avio_wb32(pb, 0); /* size */
2258 ffio_wfourcc(pb, "meta");
2260 mov_write_itunes_hdlr_tag(pb, mov, s);
2261 mov_write_ilst_tag(pb, mov, s);
2262 size = update_size(pb, pos);
2266 static int utf8len(const uint8_t *b)
2271 GET_UTF8(val, *b++, return -1;)
2277 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2281 GET_UTF8(val, *b++, return -1;)
2284 avio_wb16(pb, 0x00);
2288 static uint16_t language_code(const char *str)
2290 return (((str[0] - 0x60) & 0x1F) << 10) +
2291 (((str[1] - 0x60) & 0x1F) << 5) +
2292 (( str[2] - 0x60) & 0x1F);
2295 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
2296 const char *tag, const char *str)
2298 int64_t pos = avio_tell(pb);
2299 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
2300 if (!t || !utf8len(t->value))
2302 avio_wb32(pb, 0); /* size */
2303 ffio_wfourcc(pb, tag); /* type */
2304 avio_wb32(pb, 0); /* version + flags */
2305 if (!strcmp(tag, "yrrc"))
2306 avio_wb16(pb, atoi(t->value));
2308 avio_wb16(pb, language_code("eng")); /* language */
2309 avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
2310 if (!strcmp(tag, "albm") &&
2311 (t = av_dict_get(s->metadata, "track", NULL, 0)))
2312 avio_w8(pb, atoi(t->value));
2314 return update_size(pb, pos);
2317 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
2319 int64_t pos = avio_tell(pb);
2320 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
2322 avio_wb32(pb, 0); // size
2323 ffio_wfourcc(pb, "chpl");
2324 avio_wb32(pb, 0x01000000); // version + flags
2325 avio_wb32(pb, 0); // unknown
2326 avio_w8(pb, nb_chapters);
2328 for (i = 0; i < nb_chapters; i++) {
2329 AVChapter *c = s->chapters[i];
2330 AVDictionaryEntry *t;
2331 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
2333 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2334 int len = FFMIN(strlen(t->value), 255);
2336 avio_write(pb, t->value, len);
2340 return update_size(pb, pos);
2343 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
2346 AVIOContext *pb_buf;
2350 for (i = 0; i < s->nb_streams; i++)
2351 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
2355 ret = avio_open_dyn_buf(&pb_buf);
2359 if (mov->mode & MODE_3GP) {
2360 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2361 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2362 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2363 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2364 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2365 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2366 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2367 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2368 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2369 mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
2370 mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
2371 mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
2372 mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
2373 mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
2374 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
2375 // currently ignored by mov.c
2376 mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
2377 // add support for libquicktime, this atom is also actually read by mov.c
2378 mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
2379 mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
2380 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
2382 /* iTunes meta data */
2383 mov_write_meta_tag(pb_buf, mov, s);
2387 mov_write_chpl_tag(pb_buf, s);
2389 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2390 avio_wb32(pb, size + 8);
2391 ffio_wfourcc(pb, "udta");
2392 avio_write(pb, buf, size);
2399 static void mov_write_psp_udta_tag(AVIOContext *pb,
2400 const char *str, const char *lang, int type)
2402 int len = utf8len(str) + 1;
2405 avio_wb16(pb, len * 2 + 10); /* size */
2406 avio_wb32(pb, type); /* type */
2407 avio_wb16(pb, language_code(lang)); /* language */
2408 avio_wb16(pb, 0x01); /* ? */
2409 ascii_to_wc(pb, str);
2412 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
2414 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2418 pos = avio_tell(pb);
2419 avio_wb32(pb, 0); /* size placeholder*/
2420 ffio_wfourcc(pb, "uuid");
2421 ffio_wfourcc(pb, "USMT");
2422 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2423 avio_wb32(pb, 0xbb88695c);
2424 avio_wb32(pb, 0xfac9c740);
2426 pos2 = avio_tell(pb);
2427 avio_wb32(pb, 0); /* size placeholder*/
2428 ffio_wfourcc(pb, "MTDT");
2432 avio_wb16(pb, 0x0C); /* size */
2433 avio_wb32(pb, 0x0B); /* type */
2434 avio_wb16(pb, language_code("und")); /* language */
2435 avio_wb16(pb, 0x0); /* ? */
2436 avio_wb16(pb, 0x021C); /* data */
2438 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2439 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2440 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2442 update_size(pb, pos2);
2443 return update_size(pb, pos);
2449 static void build_chunks(MOVTrack *trk)
2452 MOVIentry *chunk = &trk->cluster[0];
2453 uint64_t chunkSize = chunk->size;
2454 chunk->chunkNum = 1;
2455 if (trk->chunkCount)
2457 trk->chunkCount = 1;
2458 for (i = 1; i<trk->entry; i++){
2459 if (chunk->pos + chunkSize == trk->cluster[i].pos &&
2460 chunkSize + trk->cluster[i].size < (1<<20)){
2461 chunkSize += trk->cluster[i].size;
2462 chunk->samples_in_chunk += trk->cluster[i].entries;
2464 trk->cluster[i].chunkNum = chunk->chunkNum+1;
2465 chunk=&trk->cluster[i];
2466 chunkSize = chunk->size;
2472 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2476 int64_t pos = avio_tell(pb);
2477 avio_wb32(pb, 0); /* size placeholder*/
2478 ffio_wfourcc(pb, "moov");
2480 for (i = 0; i < mov->nb_streams; i++) {
2481 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2484 mov->tracks[i].time = mov->time;
2485 mov->tracks[i].track_id = i + 1;
2487 if (mov->tracks[i].entry)
2488 build_chunks(&mov->tracks[i]);
2491 if (mov->chapter_track)
2492 for (i = 0; i < s->nb_streams; i++) {
2493 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2494 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2496 for (i = 0; i < mov->nb_streams; i++) {
2497 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2498 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2499 mov->tracks[i].tref_id =
2500 mov->tracks[mov->tracks[i].src_track].track_id;
2503 for (i = 0; i < mov->nb_streams; i++) {
2504 if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
2505 int src_trk = mov->tracks[i].src_track;
2506 mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
2507 mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
2508 //src_trk may have a different timescale than the tmcd track
2509 mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
2510 mov->tracks[i].timescale,
2511 mov->tracks[src_trk].timescale);
2515 mov_write_mvhd_tag(pb, mov);
2516 if (mov->mode != MODE_MOV && !mov->iods_skip)
2517 mov_write_iods_tag(pb, mov);
2518 for (i = 0; i < mov->nb_streams; i++) {
2519 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2520 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2523 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2524 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2526 if (mov->mode == MODE_PSP)
2527 mov_write_uuidusmt_tag(pb, s);
2529 mov_write_udta_tag(pb, mov, s);
2531 return update_size(pb, pos);
2534 static void param_write_int(AVIOContext *pb, const char *name, int value)
2536 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2539 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2541 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2544 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2547 len = FFMIN(sizeof(buf) / 2 - 1, len);
2548 ff_data_to_hex(buf, value, len, 0);
2549 buf[2 * len] = '\0';
2550 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2553 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2555 int64_t pos = avio_tell(pb);
2557 static const uint8_t uuid[] = {
2558 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2559 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2563 ffio_wfourcc(pb, "uuid");
2564 avio_write(pb, uuid, sizeof(uuid));
2567 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2568 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2569 avio_printf(pb, "<head>\n");
2570 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2572 avio_printf(pb, "</head>\n");
2573 avio_printf(pb, "<body>\n");
2574 avio_printf(pb, "<switch>\n");
2575 for (i = 0; i < mov->nb_streams; i++) {
2576 MOVTrack *track = &mov->tracks[i];
2578 /* track->track_id is initialized in write_moov, and thus isn't known
2580 int track_id = i + 1;
2582 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2584 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2589 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2590 track->enc->bit_rate);
2591 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2592 param_write_int(pb, "trackID", track_id);
2593 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2594 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2596 int size = track->enc->extradata_size;
2597 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2599 param_write_hex(pb, "CodecPrivateData",
2600 ptr ? ptr : track->enc->extradata,
2604 param_write_string(pb, "FourCC", "H264");
2605 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2606 param_write_string(pb, "FourCC", "WVC1");
2607 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2608 track->enc->extradata_size);
2610 param_write_int(pb, "MaxWidth", track->enc->width);
2611 param_write_int(pb, "MaxHeight", track->enc->height);
2612 param_write_int(pb, "DisplayWidth", track->enc->width);
2613 param_write_int(pb, "DisplayHeight", track->enc->height);
2615 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2616 param_write_string(pb, "FourCC", "AACL");
2617 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2618 param_write_string(pb, "FourCC", "WMAP");
2620 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2621 track->enc->extradata_size);
2622 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2623 track->enc->codec_id));
2624 param_write_int(pb, "Channels", track->enc->channels);
2625 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2626 param_write_int(pb, "BitsPerSample", 16);
2627 param_write_int(pb, "PacketSize", track->enc->block_align ?
2628 track->enc->block_align : 4);
2630 avio_printf(pb, "</%s>\n", type);
2632 avio_printf(pb, "</switch>\n");
2633 avio_printf(pb, "</body>\n");
2634 avio_printf(pb, "</smil>\n");
2636 return update_size(pb, pos);
2639 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2642 ffio_wfourcc(pb, "mfhd");
2644 avio_wb32(pb, mov->fragments);
2648 static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov,
2649 MOVTrack *track, int64_t moof_offset)
2651 int64_t pos = avio_tell(pb);
2652 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2653 MOV_TFHD_BASE_DATA_OFFSET;
2654 if (!track->entry) {
2655 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2657 flags |= MOV_TFHD_DEFAULT_FLAGS;
2659 if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET)
2660 flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
2662 /* Don't set a default sample size, the silverlight player refuses
2663 * to play files with that set. Don't set a default sample duration,
2664 * WMP freaks out if it is set. Don't set a base data offset, PIFF
2665 * file format says it MUST NOT be set. */
2666 if (track->mode == MODE_ISM)
2667 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2668 MOV_TFHD_BASE_DATA_OFFSET);
2670 avio_wb32(pb, 0); /* size placeholder */
2671 ffio_wfourcc(pb, "tfhd");
2672 avio_w8(pb, 0); /* version */
2673 avio_wb24(pb, flags);
2675 avio_wb32(pb, track->track_id); /* track-id */
2676 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2677 avio_wb64(pb, moof_offset);
2678 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2679 track->default_duration = get_cluster_duration(track, 0);
2680 avio_wb32(pb, track->default_duration);
2682 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2683 track->default_size = track->entry ? track->cluster[0].size : 1;
2684 avio_wb32(pb, track->default_size);
2686 track->default_size = -1;
2688 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2689 track->default_sample_flags =
2690 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2691 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2692 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2693 avio_wb32(pb, track->default_sample_flags);
2696 return update_size(pb, pos);
2699 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2701 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2702 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2705 static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov,
2706 MOVTrack *track, int moof_size)
2708 int64_t pos = avio_tell(pb);
2709 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2712 for (i = 0; i < track->entry; i++) {
2713 if (get_cluster_duration(track, i) != track->default_duration)
2714 flags |= MOV_TRUN_SAMPLE_DURATION;
2715 if (track->cluster[i].size != track->default_size)
2716 flags |= MOV_TRUN_SAMPLE_SIZE;
2717 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2718 flags |= MOV_TRUN_SAMPLE_FLAGS;
2720 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2721 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2722 if (track->flags & MOV_TRACK_CTTS)
2723 flags |= MOV_TRUN_SAMPLE_CTS;
2725 avio_wb32(pb, 0); /* size placeholder */
2726 ffio_wfourcc(pb, "trun");
2727 avio_w8(pb, 0); /* version */
2728 avio_wb24(pb, flags);
2730 avio_wb32(pb, track->entry); /* sample count */
2731 if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
2732 !(mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) &&
2733 track->track_id != 1)
2734 avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
2736 avio_wb32(pb, moof_size + 8 + track->data_offset +
2737 track->cluster[0].pos); /* data offset */
2738 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2739 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2741 for (i = 0; i < track->entry; i++) {
2742 if (flags & MOV_TRUN_SAMPLE_DURATION)
2743 avio_wb32(pb, get_cluster_duration(track, i));
2744 if (flags & MOV_TRUN_SAMPLE_SIZE)
2745 avio_wb32(pb, track->cluster[i].size);
2746 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2747 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2748 if (flags & MOV_TRUN_SAMPLE_CTS)
2749 avio_wb32(pb, track->cluster[i].cts);
2752 return update_size(pb, pos);
2755 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2757 int64_t pos = avio_tell(pb);
2758 static const uint8_t uuid[] = {
2759 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2760 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2763 avio_wb32(pb, 0); /* size placeholder */
2764 ffio_wfourcc(pb, "uuid");
2765 avio_write(pb, uuid, sizeof(uuid));
2768 avio_wb64(pb, track->frag_start);
2769 avio_wb64(pb, track->start_dts + track->track_duration -
2770 track->cluster[0].dts);
2772 return update_size(pb, pos);
2775 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2776 MOVTrack *track, int entry)
2778 int n = track->nb_frag_info - 1 - entry, i;
2779 int size = 8 + 16 + 4 + 1 + 16*n;
2780 static const uint8_t uuid[] = {
2781 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2782 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2788 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2789 avio_wb32(pb, size);
2790 ffio_wfourcc(pb, "uuid");
2791 avio_write(pb, uuid, sizeof(uuid));
2795 for (i = 0; i < n; i++) {
2796 int index = entry + 1 + i;
2797 avio_wb64(pb, track->frag_info[index].time);
2798 avio_wb64(pb, track->frag_info[index].duration);
2800 if (n < mov->ism_lookahead) {
2801 int free_size = 16 * (mov->ism_lookahead - n);
2802 avio_wb32(pb, free_size);
2803 ffio_wfourcc(pb, "free");
2804 ffio_fill(pb, 0, free_size - 8);
2810 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2813 int64_t pos = avio_tell(pb);
2815 for (i = 0; i < mov->ism_lookahead; i++) {
2816 /* Update the tfrf tag for the last ism_lookahead fragments,
2817 * nb_frag_info - 1 is the next fragment to be written. */
2818 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2820 avio_seek(pb, pos, SEEK_SET);
2824 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2825 MOVTrack *track, int64_t moof_offset,
2828 int64_t pos = avio_tell(pb);
2829 avio_wb32(pb, 0); /* size placeholder */
2830 ffio_wfourcc(pb, "traf");
2832 mov_write_tfhd_tag(pb, mov, track, moof_offset);
2833 mov_write_trun_tag(pb, mov, track, moof_size);
2834 if (mov->mode == MODE_ISM) {
2835 mov_write_tfxd_tag(pb, track);
2837 if (mov->ism_lookahead) {
2838 int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
2840 track->tfrf_offset = avio_tell(pb);
2841 avio_wb32(pb, 8 + size);
2842 ffio_wfourcc(pb, "free");
2843 for (i = 0; i < size; i++)
2848 return update_size(pb, pos);
2851 static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
2852 int tracks, int moof_size)
2854 int64_t pos = avio_tell(pb);
2857 avio_wb32(pb, 0); /* size placeholder */
2858 ffio_wfourcc(pb, "moof");
2860 mov_write_mfhd_tag(pb, mov);
2861 for (i = 0; i < mov->nb_streams; i++) {
2862 MOVTrack *track = &mov->tracks[i];
2863 if (tracks >= 0 && i != tracks)
2867 mov_write_traf_tag(pb, mov, track, pos, moof_size);
2870 return update_size(pb, pos);
2873 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2875 AVIOContext *avio_buf;
2878 if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
2880 mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
2881 moof_size = ffio_close_null_buf(avio_buf);
2882 return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
2885 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2887 int64_t pos = avio_tell(pb);
2890 avio_wb32(pb, 0); /* size placeholder */
2891 ffio_wfourcc(pb, "tfra");
2892 avio_w8(pb, 1); /* version */
2895 avio_wb32(pb, track->track_id);
2896 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2897 avio_wb32(pb, track->nb_frag_info);
2898 for (i = 0; i < track->nb_frag_info; i++) {
2899 avio_wb64(pb, track->frag_info[i].time);
2900 avio_wb64(pb, track->frag_info[i].offset);
2901 avio_w8(pb, 1); /* traf number */
2902 avio_w8(pb, 1); /* trun number */
2903 avio_w8(pb, 1); /* sample number */
2906 return update_size(pb, pos);
2909 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2911 int64_t pos = avio_tell(pb);
2914 avio_wb32(pb, 0); /* size placeholder */
2915 ffio_wfourcc(pb, "mfra");
2916 /* An empty mfra atom is enough to indicate to the publishing point that
2917 * the stream has ended. */
2918 if (mov->flags & FF_MOV_FLAG_ISML)
2919 return update_size(pb, pos);
2921 for (i = 0; i < mov->nb_streams; i++) {
2922 MOVTrack *track = &mov->tracks[i];
2923 if (track->nb_frag_info)
2924 mov_write_tfra_tag(pb, track);
2928 ffio_wfourcc(pb, "mfro");
2929 avio_wb32(pb, 0); /* version + flags */
2930 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2932 return update_size(pb, pos);
2935 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2937 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2938 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2940 mov->mdat_pos = avio_tell(pb);
2941 avio_wb32(pb, 0); /* size placeholder*/
2942 ffio_wfourcc(pb, "mdat");
2946 /* TODO: This needs to be more general */
2947 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2949 MOVMuxContext *mov = s->priv_data;
2950 int64_t pos = avio_tell(pb);
2951 int has_h264 = 0, has_video = 0;
2955 for (i = 0; i < s->nb_streams; i++) {
2956 AVStream *st = s->streams[i];
2957 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2959 if (st->codec->codec_id == AV_CODEC_ID_H264)
2963 avio_wb32(pb, 0); /* size */
2964 ffio_wfourcc(pb, "ftyp");
2966 if (mov->mode == MODE_3GP) {
2967 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2968 minor = has_h264 ? 0x100 : 0x200;
2969 } else if (mov->mode & MODE_3G2) {
2970 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2971 minor = has_h264 ? 0x20000 : 0x10000;
2972 } else if (mov->mode == MODE_PSP)
2973 ffio_wfourcc(pb, "MSNV");
2974 else if (mov->mode == MODE_MP4)
2975 ffio_wfourcc(pb, "isom");
2976 else if (mov->mode == MODE_IPOD)
2977 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2978 else if (mov->mode == MODE_ISM)
2979 ffio_wfourcc(pb, "isml");
2980 else if (mov->mode == MODE_F4V)
2981 ffio_wfourcc(pb, "f4v ");
2983 ffio_wfourcc(pb, "qt ");
2985 avio_wb32(pb, minor);
2987 if (mov->mode == MODE_MOV)
2988 ffio_wfourcc(pb, "qt ");
2989 else if (mov->mode == MODE_ISM) {
2990 ffio_wfourcc(pb, "piff");
2991 ffio_wfourcc(pb, "iso2");
2993 ffio_wfourcc(pb, "isom");
2994 ffio_wfourcc(pb, "iso2");
2996 ffio_wfourcc(pb, "avc1");
2999 if (mov->mode == MODE_3GP)
3000 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
3001 else if (mov->mode & MODE_3G2)
3002 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
3003 else if (mov->mode == MODE_PSP)
3004 ffio_wfourcc(pb, "MSNV");
3005 else if (mov->mode == MODE_MP4)
3006 ffio_wfourcc(pb, "mp41");
3007 return update_size(pb, pos);
3010 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
3012 AVCodecContext *video_codec = s->streams[0]->codec;
3013 AVCodecContext *audio_codec = s->streams[1]->codec;
3014 int audio_rate = audio_codec->sample_rate;
3015 int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
3016 int audio_kbitrate = audio_codec->bit_rate / 1000;
3017 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
3019 avio_wb32(pb, 0x94); /* size */
3020 ffio_wfourcc(pb, "uuid");
3021 ffio_wfourcc(pb, "PROF");
3023 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3024 avio_wb32(pb, 0xbb88695c);
3025 avio_wb32(pb, 0xfac9c740);
3027 avio_wb32(pb, 0x0); /* ? */
3028 avio_wb32(pb, 0x3); /* 3 sections ? */
3030 avio_wb32(pb, 0x14); /* size */
3031 ffio_wfourcc(pb, "FPRF");
3032 avio_wb32(pb, 0x0); /* ? */
3033 avio_wb32(pb, 0x0); /* ? */
3034 avio_wb32(pb, 0x0); /* ? */
3036 avio_wb32(pb, 0x2c); /* size */
3037 ffio_wfourcc(pb, "APRF"); /* audio */
3039 avio_wb32(pb, 0x2); /* TrackID */
3040 ffio_wfourcc(pb, "mp4a");
3041 avio_wb32(pb, 0x20f);
3043 avio_wb32(pb, audio_kbitrate);
3044 avio_wb32(pb, audio_kbitrate);
3045 avio_wb32(pb, audio_rate);
3046 avio_wb32(pb, audio_codec->channels);
3048 avio_wb32(pb, 0x34); /* size */
3049 ffio_wfourcc(pb, "VPRF"); /* video */
3051 avio_wb32(pb, 0x1); /* TrackID */
3052 if (video_codec->codec_id == AV_CODEC_ID_H264) {
3053 ffio_wfourcc(pb, "avc1");
3054 avio_wb16(pb, 0x014D);
3055 avio_wb16(pb, 0x0015);
3057 ffio_wfourcc(pb, "mp4v");
3058 avio_wb16(pb, 0x0000);
3059 avio_wb16(pb, 0x0103);
3062 avio_wb32(pb, video_kbitrate);
3063 avio_wb32(pb, video_kbitrate);
3064 avio_wb32(pb, frame_rate);
3065 avio_wb32(pb, frame_rate);
3066 avio_wb16(pb, video_codec->width);
3067 avio_wb16(pb, video_codec->height);
3068 avio_wb32(pb, 0x010001); /* ? */
3071 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
3074 int i, closed_gop = 0;
3076 for (i = 0; i < pkt->size - 4; i++) {
3077 c = (c << 8) + pkt->data[i];
3078 if (c == 0x1b8) { // gop
3079 closed_gop = pkt->data[i + 4] >> 6 & 0x01;
3080 } else if (c == 0x100) { // pic
3081 int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
3082 if (!temp_ref || closed_gop) // I picture is not reordered
3083 *flags = MOV_SYNC_SAMPLE;
3085 *flags = MOV_PARTIAL_SYNC_SAMPLE;
3092 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
3094 const uint8_t *start, *next, *end = pkt->data + pkt->size;
3095 int seq = 0, entry = 0;
3096 int key = pkt->flags & AV_PKT_FLAG_KEY;
3097 start = find_next_marker(pkt->data, end);
3098 for (next = start; next < end; start = next) {
3099 next = find_next_marker(start + 4, end);
3100 switch (AV_RB32(start)) {
3101 case VC1_CODE_SEQHDR:
3104 case VC1_CODE_ENTRYPOINT:
3107 case VC1_CODE_SLICE:
3108 trk->vc1_info.slices = 1;
3112 if (!trk->entry && !fragment) {
3113 /* First packet in first fragment */
3114 trk->vc1_info.first_packet_seq = seq;
3115 trk->vc1_info.first_packet_entry = entry;
3116 } else if ((seq && !trk->vc1_info.packet_seq) ||
3117 (entry && !trk->vc1_info.packet_entry)) {
3119 for (i = 0; i < trk->entry; i++)
3120 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
3121 trk->has_keyframes = 0;
3123 trk->vc1_info.packet_seq = 1;
3125 trk->vc1_info.packet_entry = 1;
3127 /* First fragment */
3128 if ((!seq || trk->vc1_info.first_packet_seq) &&
3129 (!entry || trk->vc1_info.first_packet_entry)) {
3130 /* First packet had the same headers as this one, readd the
3131 * sync sample flag. */
3132 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
3133 trk->has_keyframes = 1;
3137 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
3139 else if (trk->vc1_info.packet_seq)
3141 else if (trk->vc1_info.packet_entry)
3144 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
3145 trk->has_keyframes++;
3149 static int mov_flush_fragment(AVFormatContext *s)
3151 MOVMuxContext *mov = s->priv_data;
3152 int i, first_track = -1;
3153 int64_t mdat_size = 0;
3155 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
3158 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
3159 int64_t pos = avio_tell(s->pb);
3161 int buf_size, moov_size;
3163 for (i = 0; i < mov->nb_streams; i++)
3164 if (!mov->tracks[i].entry)
3166 /* Don't write the initial moov unless all tracks have data */
3167 if (i < mov->nb_streams)
3170 moov_size = get_moov_size(s);
3171 for (i = 0; i < mov->nb_streams; i++)
3172 mov->tracks[i].data_offset = pos + moov_size + 8;
3174 mov_write_moov_tag(s->pb, mov, s);
3176 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
3177 mov->mdat_buf = NULL;
3178 avio_wb32(s->pb, buf_size + 8);
3179 ffio_wfourcc(s->pb, "mdat");
3180 avio_write(s->pb, buf, buf_size);
3185 for (i = 0; i < mov->nb_streams; i++) {
3186 if (mov->tracks[i].entry)
3187 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
3188 mov->tracks[i].track_duration -
3189 mov->tracks[i].cluster[0].dts;
3190 mov->tracks[i].entry = 0;
3196 for (i = 0; i < mov->nb_streams; i++) {
3197 MOVTrack *track = &mov->tracks[i];
3198 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
3199 track->data_offset = 0;
3201 track->data_offset = mdat_size;
3202 if (!track->mdat_buf)
3204 mdat_size += avio_tell(track->mdat_buf);
3205 if (first_track < 0)
3212 for (i = 0; i < mov->nb_streams; i++) {
3213 MOVTrack *track = &mov->tracks[i];
3214 int buf_size, write_moof = 1, moof_tracks = -1;
3216 int64_t duration = 0;
3219 duration = track->start_dts + track->track_duration -
3220 track->cluster[0].dts;
3221 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
3222 if (!track->mdat_buf)
3224 mdat_size = avio_tell(track->mdat_buf);
3227 write_moof = i == first_track;
3231 MOVFragmentInfo *info;
3233 track->nb_frag_info++;
3234 if (track->nb_frag_info >= track->frag_info_capacity) {
3235 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
3236 if (av_reallocp_array(&track->frag_info,
3238 sizeof(*track->frag_info)))
3239 return AVERROR(ENOMEM);
3240 track->frag_info_capacity = new_capacity;
3242 info = &track->frag_info[track->nb_frag_info - 1];
3243 info->offset = avio_tell(s->pb);
3244 info->time = mov->tracks[i].frag_start;
3245 info->duration = duration;
3246 mov_write_tfrf_tags(s->pb, mov, track);
3248 mov_write_moof_tag(s->pb, mov, moof_tracks);
3249 info->tfrf_offset = track->tfrf_offset;
3252 avio_wb32(s->pb, mdat_size + 8);
3253 ffio_wfourcc(s->pb, "mdat");
3257 track->frag_start += duration;
3259 if (!track->mdat_buf)
3261 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
3262 track->mdat_buf = NULL;
3264 avio_write(s->pb, buf, buf_size);
3274 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
3276 MOVMuxContext *mov = s->priv_data;
3277 AVIOContext *pb = s->pb;
3278 MOVTrack *trk = &mov->tracks[pkt->stream_index];
3279 AVCodecContext *enc = trk->enc;
3280 unsigned int samples_in_chunk = 0;
3281 int size = pkt->size;
3282 uint8_t *reformatted_data = NULL;
3285 int64_t duration = pkt->dts - trk->cluster[trk->entry - 1].dts;
3286 if (duration < 0 || duration > INT_MAX) {
3287 av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
3291 pkt->dts = trk->cluster[trk->entry - 1].dts + 1;
3292 pkt->pts = AV_NOPTS_VALUE;
3295 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3297 if (mov->fragments > 0) {
3298 if (!trk->mdat_buf) {
3299 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
3304 if (!mov->mdat_buf) {
3305 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
3312 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
3313 /* We must find out how many AMR blocks there are in one packet */
3314 static uint16_t packed_size[16] =
3315 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
3318 while (len < size && samples_in_chunk < 100) {
3319 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
3322 if (samples_in_chunk > 1) {
3323 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
3326 } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
3327 enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
3328 samples_in_chunk = enc->frame_size;
3329 } else if (trk->sample_size)
3330 samples_in_chunk = size / trk->sample_size;
3332 samples_in_chunk = 1;
3334 /* copy extradata if it exists */
3335 if (trk->vos_len == 0 && enc->extradata_size > 0) {
3336 trk->vos_len = enc->extradata_size;
3337 trk->vos_data = av_malloc(trk->vos_len);
3338 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
3341 if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
3342 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
3343 if (!s->streams[pkt->stream_index]->nb_frames) {
3344 av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
3345 "use audio bitstream filter 'aac_adtstoasc' to fix it "
3346 "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
3349 av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
3351 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
3352 /* from x264 or from bytestream h264 */
3353 /* nal reformating needed */
3354 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3355 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
3357 avio_write(pb, reformatted_data, size);
3359 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
3361 } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
3362 (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
3363 /* extradata is Annex B, assume the bitstream is too and convert it */
3364 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3365 ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
3366 avio_write(pb, reformatted_data, size);
3368 size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
3371 avio_write(pb, pkt->data, size);
3374 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
3375 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
3376 /* copy frame to create needed atoms */
3377 trk->vos_len = size;
3378 trk->vos_data = av_malloc(size);
3380 return AVERROR(ENOMEM);
3381 memcpy(trk->vos_data, pkt->data, size);
3384 if (trk->entry >= trk->cluster_capacity) {
3385 unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
3386 if (av_reallocp_array(&trk->cluster, new_capacity,
3387 sizeof(*trk->cluster)))
3388 return AVERROR(ENOMEM);
3389 trk->cluster_capacity = new_capacity;
3392 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
3393 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
3394 trk->cluster[trk->entry].chunkNum = 0;
3395 trk->cluster[trk->entry].size = size;
3396 trk->cluster[trk->entry].entries = samples_in_chunk;
3397 trk->cluster[trk->entry].dts = pkt->dts;
3398 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
3399 /* First packet of a new fragment. We already wrote the duration
3400 * of the last packet of the previous fragment based on track_duration,
3401 * which might not exactly match our dts. Therefore adjust the dts
3402 * of this packet to be what the previous packets duration implies. */
3403 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
3405 if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !supports_edts(mov)) {
3406 trk->cluster[trk->entry].dts = trk->start_dts = 0;
3408 if (trk->start_dts == AV_NOPTS_VALUE)
3409 trk->start_dts = pkt->dts;
3410 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
3411 trk->last_sample_is_subtitle_end = 0;
3413 if (pkt->pts == AV_NOPTS_VALUE) {
3414 av_log(s, AV_LOG_WARNING, "pts has no value\n");
3415 pkt->pts = pkt->dts;
3417 if (pkt->dts != pkt->pts)
3418 trk->flags |= MOV_TRACK_CTTS;
3419 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
3420 trk->cluster[trk->entry].flags = 0;
3421 if (enc->codec_id == AV_CODEC_ID_VC1) {
3422 mov_parse_vc1_frame(pkt, trk, mov->fragments);
3423 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
3424 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
3425 trk->entry > 0) { // force sync sample for the first key frame
3426 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3427 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3428 trk->flags |= MOV_TRACK_STPS;
3430 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3432 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
3433 trk->has_keyframes++;
3436 trk->sample_count += samples_in_chunk;
3437 mov->mdat_size += size;
3439 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
3440 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3441 reformatted_data, size);
3442 av_free(reformatted_data);
3446 static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
3448 MOVMuxContext *mov = s->priv_data;
3449 MOVTrack *trk = &mov->tracks[pkt->stream_index];
3450 AVCodecContext *enc = trk->enc;
3451 int64_t frag_duration = 0;
3452 int size = pkt->size;
3455 return 0; /* Discard 0 sized packets */
3457 if (trk->entry && pkt->stream_index < s->nb_streams)
3458 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3459 s->streams[pkt->stream_index]->time_base,
3461 if ((mov->max_fragment_duration &&
3462 frag_duration >= mov->max_fragment_duration) ||
3463 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3464 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3465 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
3466 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3467 if (frag_duration >= mov->min_fragment_duration)
3468 mov_flush_fragment(s);
3471 return ff_mov_write_packet(s, pkt);
3474 static int mov_write_subtitle_end_packet(AVFormatContext *s,
3478 uint8_t data[2] = {0};
3481 av_init_packet(&end);
3482 end.size = sizeof(data);
3487 end.stream_index = stream_index;
3489 ret = mov_write_single_packet(s, &end);
3490 av_free_packet(&end);
3495 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
3498 mov_flush_fragment(s);
3502 MOVMuxContext *mov = s->priv_data;
3504 if (!pkt->size) return 0; /* Discard 0 sized packets */
3507 * Subtitles require special handling.
3509 * 1) For full complaince, every track must have a sample at
3510 * dts == 0, which is rarely true for subtitles. So, as soon
3511 * as we see any packet with dts > 0, write an empty subtitle
3512 * at dts == 0 for any subtitle track with no samples in it.
3514 * 2) For each subtitle track, check if the current packet's
3515 * dts is past the duration of the last subtitle sample. If
3516 * so, we now need to write an end sample for that subtitle.
3518 * This must be done conditionally to allow for subtitles that
3519 * immediately replace each other, in which case an end sample
3520 * is not needed, and is, in fact, actively harmful.
3522 * 3) See mov_write_trailer for how the final end sample is
3525 for (i = 0; i < mov->nb_streams; i++) {
3526 MOVTrack *trk = &mov->tracks[i];
3529 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
3530 trk->track_duration < pkt->dts &&
3531 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
3532 ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
3533 if (ret < 0) return ret;
3534 trk->last_sample_is_subtitle_end = 1;
3538 return mov_write_single_packet(s, pkt);
3542 // QuickTime chapters involve an additional text track with the chapter names
3543 // as samples, and a tref pointing from the other tracks to the chapter one.
3544 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
3548 MOVMuxContext *mov = s->priv_data;
3549 MOVTrack *track = &mov->tracks[tracknum];
3550 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
3553 track->mode = mov->mode;
3554 track->tag = MKTAG('t','e','x','t');
3555 track->timescale = MOV_TIMESCALE;
3556 track->enc = avcodec_alloc_context3(NULL);
3558 return AVERROR(ENOMEM);
3559 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
3561 // These properties are required to make QT recognize the chapter track
3562 uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
3563 if (ff_alloc_extradata(track->enc, sizeof(chapter_properties)))
3564 return AVERROR(ENOMEM);
3565 memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
3567 if (avio_open_dyn_buf(&pb) >= 0) {
3571 /* Stub header (usually for Quicktime chapter track) */
3573 avio_wb32(pb, 0x01); // displayFlags
3574 avio_w8(pb, 0x00); // horizontal justification
3575 avio_w8(pb, 0x00); // vertical justification
3576 avio_w8(pb, 0x00); // bgColourRed
3577 avio_w8(pb, 0x00); // bgColourGreen
3578 avio_w8(pb, 0x00); // bgColourBlue
3579 avio_w8(pb, 0x00); // bgColourAlpha
3581 avio_wb16(pb, 0x00); // defTextBoxTop
3582 avio_wb16(pb, 0x00); // defTextBoxLeft
3583 avio_wb16(pb, 0x00); // defTextBoxBottom
3584 avio_wb16(pb, 0x00); // defTextBoxRight
3586 avio_wb16(pb, 0x00); // startChar
3587 avio_wb16(pb, 0x00); // endChar
3588 avio_wb16(pb, 0x01); // fontID
3589 avio_w8(pb, 0x00); // fontStyleFlags
3590 avio_w8(pb, 0x00); // fontSize
3591 avio_w8(pb, 0x00); // fgColourRed
3592 avio_w8(pb, 0x00); // fgColourGreen
3593 avio_w8(pb, 0x00); // fgColourBlue
3594 avio_w8(pb, 0x00); // fgColourAlpha
3596 avio_wb32(pb, 0x0D); // box size
3597 ffio_wfourcc(pb, "ftab"); // box atom name
3598 avio_wb16(pb, 0x01); // entry count
3600 avio_wb16(pb, 0x01); // font ID
3601 avio_w8(pb, 0x00); // font name length
3603 if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
3604 track->enc->extradata = buf;
3605 track->enc->extradata_size = size;
3612 for (i = 0; i < s->nb_chapters; i++) {
3613 AVChapter *c = s->chapters[i];
3614 AVDictionaryEntry *t;
3616 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3617 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3618 pkt.duration = end - pkt.dts;
3620 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3621 len = strlen(t->value);
3623 pkt.data = av_malloc(pkt.size);
3625 return AVERROR(ENOMEM);
3626 AV_WB16(pkt.data, len);
3627 memcpy(pkt.data + 2, t->value, len);
3628 ff_mov_write_packet(s, &pkt);
3629 av_freep(&pkt.data);
3636 static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
3639 MOVMuxContext *mov = s->priv_data;
3640 MOVTrack *track = &mov->tracks[index];
3641 AVStream *src_st = s->streams[src_index];
3643 AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
3644 AVRational rate = find_fps(s, src_st);
3646 /* compute the frame number */
3647 ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
3651 /* tmcd track based on video stream */
3652 track->mode = mov->mode;
3653 track->tag = MKTAG('t','m','c','d');
3654 track->src_track = src_index;
3655 track->timescale = mov->tracks[src_index].timescale;
3656 if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
3657 track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
3659 /* encode context: tmcd data stream */
3660 track->enc = avcodec_alloc_context3(NULL);
3661 track->enc->codec_type = AVMEDIA_TYPE_DATA;
3662 track->enc->codec_tag = track->tag;
3663 track->enc->time_base = av_inv_q(rate);
3665 /* the tmcd track just contains one packet with the frame number */
3666 pkt.data = av_malloc(pkt.size);
3667 AV_WB32(pkt.data, tc.start);
3668 ret = ff_mov_write_packet(s, &pkt);
3674 * st->disposition controls the "enabled" flag in the tkhd tag.
3675 * QuickTime will not play a track if it is not enabled. So make sure
3676 * that one track of each type (audio, video, subtitle) is enabled.
3678 * Subtitles are special. For audio and video, setting "enabled" also
3679 * makes the track "default" (i.e. it is rendered when played). For
3680 * subtitles, an "enabled" subtitle is not rendered by default, but
3681 * if no subtitle is enabled, the subtitle menu in QuickTime will be
3684 static void enable_tracks(AVFormatContext *s)
3686 MOVMuxContext *mov = s->priv_data;
3688 uint8_t enabled[AVMEDIA_TYPE_NB];
3689 int first[AVMEDIA_TYPE_NB];
3691 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3696 for (i = 0; i < s->nb_streams; i++) {
3697 AVStream *st = s->streams[i];
3699 if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
3700 st->codec->codec_type >= AVMEDIA_TYPE_NB)
3703 if (first[st->codec->codec_type] < 0)
3704 first[st->codec->codec_type] = i;
3705 if (st->disposition & AV_DISPOSITION_DEFAULT) {
3706 mov->tracks[i].flags |= MOV_TRACK_ENABLED;
3707 enabled[st->codec->codec_type] = 1;
3711 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3713 case AVMEDIA_TYPE_VIDEO:
3714 case AVMEDIA_TYPE_AUDIO:
3715 case AVMEDIA_TYPE_SUBTITLE:
3716 if (!enabled[i] && first[i] >= 0)
3717 mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
3723 static void mov_free(AVFormatContext *s)
3725 MOVMuxContext *mov = s->priv_data;
3728 if (mov->chapter_track) {
3729 if (mov->tracks[mov->chapter_track].enc)
3730 av_freep(&mov->tracks[mov->chapter_track].enc->extradata);
3731 av_freep(&mov->tracks[mov->chapter_track].enc);
3734 for (i = 0; i < mov->nb_streams; i++) {
3735 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3736 ff_mov_close_hinting(&mov->tracks[i]);
3737 else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
3738 av_freep(&mov->tracks[i].enc);
3739 av_freep(&mov->tracks[i].cluster);
3740 av_freep(&mov->tracks[i].frag_info);
3742 if (mov->tracks[i].vos_len)
3743 av_freep(&mov->tracks[i].vos_data);
3746 av_freep(&mov->tracks);
3749 static int mov_write_header(AVFormatContext *s)
3751 AVIOContext *pb = s->pb;
3752 MOVMuxContext *mov = s->priv_data;
3753 AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
3754 int i, ret, hint_track = 0, tmcd_track = 0;
3756 /* Default mode == MP4 */
3757 mov->mode = MODE_MP4;
3759 if (s->oformat != NULL) {
3760 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3761 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3762 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3763 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3764 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3765 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3766 else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
3769 /* Set the FRAGMENT flag if any of the fragmentation methods are
3771 if (mov->max_fragment_duration || mov->max_fragment_size ||
3772 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3773 FF_MOV_FLAG_FRAG_KEYFRAME |
3774 FF_MOV_FLAG_FRAG_CUSTOM))
3775 mov->flags |= FF_MOV_FLAG_FRAGMENT;
3777 /* Set other implicit flags immediately */
3778 if (mov->mode == MODE_ISM)
3779 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
3780 FF_MOV_FLAG_FRAGMENT;
3782 /* faststart: moov at the beginning of the file, if supported */
3783 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3784 if ((mov->flags & FF_MOV_FLAG_FRAGMENT) ||
3785 (s->flags & AVFMT_FLAG_CUSTOM_IO)) {
3786 av_log(s, AV_LOG_WARNING, "The faststart flag is incompatible "
3787 "with fragmentation and custom IO, disabling faststart\n");
3788 mov->flags &= ~FF_MOV_FLAG_FASTSTART;
3790 mov->reserved_moov_size = -1;
3793 if (!supports_edts(mov) && s->avoid_negative_ts < 0) {
3794 s->avoid_negative_ts = 2;
3797 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3798 * is enabled, we don't support non-seekable output at all. */
3799 if (!s->pb->seekable &&
3800 (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
3801 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3802 return AVERROR(EINVAL);
3805 mov_write_ftyp_tag(pb,s);
3806 if (mov->mode == MODE_PSP) {
3807 int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
3808 for (i = 0; i < s->nb_streams; i++) {
3809 AVStream *st = s->streams[i];
3810 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3812 else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
3818 if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
3819 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3820 return AVERROR(EINVAL);
3822 mov_write_uuidprof_tag(pb, s);
3825 mov->nb_streams = s->nb_streams;
3826 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
3827 mov->chapter_track = mov->nb_streams++;
3829 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3830 /* Add hint tracks for each audio and video stream */
3831 hint_track = mov->nb_streams;
3832 for (i = 0; i < s->nb_streams; i++) {
3833 AVStream *st = s->streams[i];
3834 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3835 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3841 if (mov->mode == MODE_MOV) {
3842 tmcd_track = mov->nb_streams;
3844 /* +1 tmcd track for each video stream with a timecode */
3845 for (i = 0; i < s->nb_streams; i++) {
3846 AVStream *st = s->streams[i];
3847 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3848 (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
3849 mov->nb_meta_tmcd++;
3852 /* check if there is already a tmcd track to remux */
3853 if (mov->nb_meta_tmcd) {
3854 for (i = 0; i < s->nb_streams; i++) {
3855 AVStream *st = s->streams[i];
3856 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
3857 av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
3858 "so timecode metadata are now ignored\n");
3859 mov->nb_meta_tmcd = 0;
3864 mov->nb_streams += mov->nb_meta_tmcd;
3867 // Reserve an extra stream for chapters for the case where chapters
3868 // are written in the trailer
3869 mov->tracks = av_mallocz((mov->nb_streams + 1) * sizeof(*mov->tracks));
3871 return AVERROR(ENOMEM);
3873 for (i = 0; i < s->nb_streams; i++) {
3874 AVStream *st= s->streams[i];
3875 MOVTrack *track= &mov->tracks[i];
3876 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3878 track->enc = st->codec;
3880 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3881 if (track->language < 0)
3882 track->language = 0;
3883 track->mode = mov->mode;
3884 track->tag = mov_find_codec_tag(s, track);
3886 av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
3887 "codec not currently supported in container\n",
3888 avcodec_get_name(st->codec->codec_id), i);
3889 ret = AVERROR(EINVAL);
3892 /* If hinting of this track is enabled by a later hint track,
3893 * this is updated. */
3894 track->hint_track = -1;
3895 track->start_dts = AV_NOPTS_VALUE;
3896 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3897 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3898 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3899 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3900 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3901 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3902 ret = AVERROR(EINVAL);
3905 track->height = track->tag >> 24 == 'n' ? 486 : 576;
3907 if (mov->video_track_timescale) {
3908 track->timescale = mov->video_track_timescale;
3910 track->timescale = st->codec->time_base.den;
3911 while(track->timescale < 10000)
3912 track->timescale *= 2;
3914 if (track->mode == MODE_MOV && track->timescale > 100000)
3915 av_log(s, AV_LOG_WARNING,
3916 "WARNING codec timebase is very high. If duration is too long,\n"
3917 "file may not be playable by quicktime. Specify a shorter timebase\n"
3918 "or choose different container.\n");
3919 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3920 track->timescale = st->codec->sample_rate;
3921 if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
3922 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
3923 track->audio_vbr = 1;
3924 }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
3925 st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
3926 st->codec->codec_id == AV_CODEC_ID_ILBC){
3927 if (!st->codec->block_align) {
3928 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
3929 ret = AVERROR(EINVAL);
3932 track->sample_size = st->codec->block_align;
3933 }else if (st->codec->frame_size > 1){ /* assume compressed audio */
3934 track->audio_vbr = 1;
3936 track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
3938 if (st->codec->codec_id == AV_CODEC_ID_ILBC ||
3939 st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_QT) {
3940 track->audio_vbr = 1;
3942 if (track->mode != MODE_MOV &&
3943 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3944 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3945 i, track->enc->sample_rate);
3946 ret = AVERROR(EINVAL);
3949 } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3950 track->timescale = st->codec->time_base.den;
3951 } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
3952 track->timescale = st->codec->time_base.den;
3954 track->timescale = MOV_TIMESCALE;
3957 track->height = st->codec->height;
3958 /* The ism specific timescale isn't mandatory, but is assumed by
3959 * some tools, such as mp4split. */
3960 if (mov->mode == MODE_ISM)
3961 track->timescale = 10000000;
3963 avpriv_set_pts_info(st, 64, 1, track->timescale);
3965 /* copy extradata if it exists */
3966 if (st->codec->extradata_size) {
3967 track->vos_len = st->codec->extradata_size;
3968 track->vos_data = av_malloc(track->vos_len);
3969 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3976 if (mov->reserved_moov_size){
3977 mov->reserved_moov_pos= avio_tell(pb);
3978 if (mov->reserved_moov_size > 0)
3979 avio_skip(pb, mov->reserved_moov_size);
3982 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3983 /* If no fragmentation options have been set, set a default. */
3984 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3985 FF_MOV_FLAG_FRAG_CUSTOM)) &&
3986 !mov->max_fragment_duration && !mov->max_fragment_size)
3987 mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME;
3989 if (mov->flags & FF_MOV_FLAG_FASTSTART)
3990 mov->reserved_moov_pos = avio_tell(pb);
3991 mov_write_mdat_tag(pb, mov);
3994 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3995 mov->time = ff_iso8601_to_unix_time(t->value);
3997 mov->time += 0x7C25B080; // 1970 based -> 1904 based
3999 if (mov->chapter_track)
4000 if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
4003 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
4004 /* Initialize the hint tracks for each audio and video stream */
4005 for (i = 0; i < s->nb_streams; i++) {
4006 AVStream *st = s->streams[i];
4007 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
4008 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
4009 if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
4016 if (mov->nb_meta_tmcd) {
4017 /* Initialize the tmcd tracks */
4018 for (i = 0; i < s->nb_streams; i++) {
4019 AVStream *st = s->streams[i];
4022 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
4024 t = av_dict_get(st->metadata, "timecode", NULL, 0);
4027 if ((ret = mov_create_timecode_track(s, tmcd_track, i, t->value)) < 0)
4036 if (mov->flags & FF_MOV_FLAG_ISML)
4037 mov_write_isml_manifest(pb, mov);
4039 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4040 mov_write_moov_tag(pb, mov, s);
4050 static int get_moov_size(AVFormatContext *s)
4053 AVIOContext *moov_buf;
4054 MOVMuxContext *mov = s->priv_data;
4056 if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
4058 mov_write_moov_tag(moov_buf, mov, s);
4059 return ffio_close_null_buf(moov_buf);
4063 * This function gets the moov size if moved to the top of the file: the chunk
4064 * offset table can switch between stco (32-bit entries) to co64 (64-bit
4065 * entries) when the moov is moved to the beginning, so the size of the moov
4066 * would change. It also updates the chunk offset tables.
4068 static int compute_moov_size(AVFormatContext *s)
4070 int i, moov_size, moov_size2;
4071 MOVMuxContext *mov = s->priv_data;
4073 moov_size = get_moov_size(s);
4077 for (i = 0; i < mov->nb_streams; i++)
4078 mov->tracks[i].data_offset += moov_size;
4080 moov_size2 = get_moov_size(s);
4084 /* if the size changed, we just switched from stco to co64 and need to
4085 * update the offsets */
4086 if (moov_size2 != moov_size)
4087 for (i = 0; i < mov->nb_streams; i++)
4088 mov->tracks[i].data_offset += moov_size2 - moov_size;
4093 static int shift_data(AVFormatContext *s)
4095 int ret = 0, moov_size;
4096 MOVMuxContext *mov = s->priv_data;
4097 int64_t pos, pos_end = avio_tell(s->pb);
4098 uint8_t *buf, *read_buf[2];
4099 int read_buf_id = 0;
4101 AVIOContext *read_pb;
4103 moov_size = compute_moov_size(s);
4107 buf = av_malloc(moov_size * 2);
4109 return AVERROR(ENOMEM);
4111 read_buf[1] = buf + moov_size;
4113 /* Shift the data: the AVIO context of the output can only be used for
4114 * writing, so we re-open the same output, but for reading. It also avoids
4115 * a read/seek/write/seek back and forth. */
4117 ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
4119 av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
4120 "the second pass (faststart)\n", s->filename);
4124 /* mark the end of the shift to up to the last data we wrote, and get ready
4126 pos_end = avio_tell(s->pb);
4127 avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
4129 /* start reading at where the new moov will be placed */
4130 avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
4131 pos = avio_tell(read_pb);
4133 #define READ_BLOCK do { \
4134 read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
4138 /* shift data by chunk of at most moov_size */
4143 n = read_size[read_buf_id];
4146 avio_write(s->pb, read_buf[read_buf_id], n);
4148 } while (pos < pos_end);
4149 avio_close(read_pb);
4156 static int mov_write_trailer(AVFormatContext *s)
4158 MOVMuxContext *mov = s->priv_data;
4159 AVIOContext *pb = s->pb;
4165 * Before actually writing the trailer, make sure that there are no
4166 * dangling subtitles, that need a terminating sample.
4168 for (i = 0; i < mov->nb_streams; i++) {
4169 MOVTrack *trk = &mov->tracks[i];
4170 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
4171 !trk->last_sample_is_subtitle_end) {
4172 mov_write_subtitle_end_packet(s, i, trk->track_duration);
4173 trk->last_sample_is_subtitle_end = 1;
4177 // If there were no chapters when the header was written, but there
4178 // are chapters now, write them in the trailer. This only works
4179 // when we are not doing fragments.
4180 if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
4181 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
4182 mov->chapter_track = mov->nb_streams++;
4183 if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
4188 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
4189 moov_pos = avio_tell(pb);
4191 /* Write size of mdat tag */
4192 if (mov->mdat_size + 8 <= UINT32_MAX) {
4193 avio_seek(pb, mov->mdat_pos, SEEK_SET);
4194 avio_wb32(pb, mov->mdat_size + 8);
4196 /* overwrite 'wide' placeholder atom */
4197 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
4198 /* special value: real atom size will be 64 bit value after
4201 ffio_wfourcc(pb, "mdat");
4202 avio_wb64(pb, mov->mdat_size + 16);
4204 avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
4206 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
4207 av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
4208 res = shift_data(s);
4210 avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
4211 mov_write_moov_tag(pb, mov, s);
4213 } else if (mov->reserved_moov_size > 0) {
4215 mov_write_moov_tag(pb, mov, s);
4216 size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
4218 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
4221 avio_wb32(pb, size);
4222 ffio_wfourcc(pb, "free");
4223 for (i = 0; i < size; i++)
4225 avio_seek(pb, moov_pos, SEEK_SET);
4227 mov_write_moov_tag(pb, mov, s);
4230 mov_flush_fragment(s);
4231 mov_write_mfra_tag(pb, mov);
4234 for (i = 0; i < mov->nb_streams; i++) {
4235 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
4236 mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
4237 int64_t off = avio_tell(pb);
4239 if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
4240 avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
4241 avio_write(pb, buf, 7);
4242 avio_seek(pb, off, SEEK_SET);
4253 #if CONFIG_MOV_MUXER
4255 AVOutputFormat ff_mov_muxer = {
4257 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
4258 .extensions = "mov",
4259 .priv_data_size = sizeof(MOVMuxContext),
4260 .audio_codec = AV_CODEC_ID_AAC,
4261 .video_codec = CONFIG_LIBX264_ENCODER ?
4262 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
4263 .write_header = mov_write_header,
4264 .write_packet = mov_write_packet,
4265 .write_trailer = mov_write_trailer,
4266 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4267 .codec_tag = (const AVCodecTag* const []){
4268 ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
4270 .priv_class = &mov_muxer_class,
4273 #if CONFIG_TGP_MUXER
4275 AVOutputFormat ff_tgp_muxer = {
4277 .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
4278 .extensions = "3gp",
4279 .priv_data_size = sizeof(MOVMuxContext),
4280 .audio_codec = AV_CODEC_ID_AMR_NB,
4281 .video_codec = AV_CODEC_ID_H263,
4282 .write_header = mov_write_header,
4283 .write_packet = mov_write_packet,
4284 .write_trailer = mov_write_trailer,
4285 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4286 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
4287 .priv_class = &tgp_muxer_class,
4290 #if CONFIG_MP4_MUXER
4292 AVOutputFormat ff_mp4_muxer = {
4294 .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
4295 .mime_type = "application/mp4",
4296 .extensions = "mp4",
4297 .priv_data_size = sizeof(MOVMuxContext),
4298 .audio_codec = AV_CODEC_ID_AAC,
4299 .video_codec = CONFIG_LIBX264_ENCODER ?
4300 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
4301 .write_header = mov_write_header,
4302 .write_packet = mov_write_packet,
4303 .write_trailer = mov_write_trailer,
4304 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4305 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4306 .priv_class = &mp4_muxer_class,
4309 #if CONFIG_PSP_MUXER
4311 AVOutputFormat ff_psp_muxer = {
4313 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
4314 .extensions = "mp4,psp",
4315 .priv_data_size = sizeof(MOVMuxContext),
4316 .audio_codec = AV_CODEC_ID_AAC,
4317 .video_codec = CONFIG_LIBX264_ENCODER ?
4318 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
4319 .write_header = mov_write_header,
4320 .write_packet = mov_write_packet,
4321 .write_trailer = mov_write_trailer,
4322 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4323 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4324 .priv_class = &psp_muxer_class,
4327 #if CONFIG_TG2_MUXER
4329 AVOutputFormat ff_tg2_muxer = {
4331 .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
4332 .extensions = "3g2",
4333 .priv_data_size = sizeof(MOVMuxContext),
4334 .audio_codec = AV_CODEC_ID_AMR_NB,
4335 .video_codec = AV_CODEC_ID_H263,
4336 .write_header = mov_write_header,
4337 .write_packet = mov_write_packet,
4338 .write_trailer = mov_write_trailer,
4339 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4340 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
4341 .priv_class = &tg2_muxer_class,
4344 #if CONFIG_IPOD_MUXER
4346 AVOutputFormat ff_ipod_muxer = {
4348 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
4349 .mime_type = "application/mp4",
4350 .extensions = "m4v,m4a",
4351 .priv_data_size = sizeof(MOVMuxContext),
4352 .audio_codec = AV_CODEC_ID_AAC,
4353 .video_codec = AV_CODEC_ID_H264,
4354 .write_header = mov_write_header,
4355 .write_packet = mov_write_packet,
4356 .write_trailer = mov_write_trailer,
4357 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4358 .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
4359 .priv_class = &ipod_muxer_class,
4362 #if CONFIG_ISMV_MUXER
4364 AVOutputFormat ff_ismv_muxer = {
4366 .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
4367 .mime_type = "application/mp4",
4368 .extensions = "ismv,isma",
4369 .priv_data_size = sizeof(MOVMuxContext),
4370 .audio_codec = AV_CODEC_ID_AAC,
4371 .video_codec = AV_CODEC_ID_H264,
4372 .write_header = mov_write_header,
4373 .write_packet = mov_write_packet,
4374 .write_trailer = mov_write_trailer,
4375 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
4376 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4377 .priv_class = &ismv_muxer_class,
4380 #if CONFIG_F4V_MUXER
4382 AVOutputFormat ff_f4v_muxer = {
4384 .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
4385 .mime_type = "application/f4v",
4386 .extensions = "f4v",
4387 .priv_data_size = sizeof(MOVMuxContext),
4388 .audio_codec = AV_CODEC_ID_AAC,
4389 .video_codec = AV_CODEC_ID_H264,
4390 .write_header = mov_write_header,
4391 .write_packet = mov_write_packet,
4392 .write_trailer = mov_write_trailer,
4393 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
4394 .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
4395 .priv_class = &f4v_muxer_class,