3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "avio_internal.h"
31 #include "libavcodec/get_bits.h"
32 #include "libavcodec/put_bits.h"
33 #include "libavcodec/vc1.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/intfloat.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/dict.h"
46 static const AVOption options[] = {
47 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
48 { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
49 { "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" },
50 { "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" },
51 { "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" },
52 { "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" },
53 { "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" },
54 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
55 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
56 { "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},
57 { "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},
58 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
59 { "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},
60 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
61 { "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},
65 #define MOV_CLASS(flavor)\
66 static const AVClass flavor ## _muxer_class = {\
67 .class_name = #flavor " muxer",\
68 .item_name = av_default_item_name,\
70 .version = LIBAVUTIL_VERSION_INT,\
73 //FIXME support 64 bit variant with wide placeholders
74 static int64_t update_size(AVIOContext *pb, int64_t pos)
76 int64_t curpos = avio_tell(pb);
77 avio_seek(pb, pos, SEEK_SET);
78 avio_wb32(pb, curpos - pos); /* rewrite size */
79 avio_seek(pb, curpos, SEEK_SET);
84 /* Chunk offset atom */
85 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
88 int mode64 = 0; // use 32 bit size variant if possible
89 int64_t pos = avio_tell(pb);
90 avio_wb32(pb, 0); /* size */
91 if (pos > UINT32_MAX) {
93 ffio_wfourcc(pb, "co64");
95 ffio_wfourcc(pb, "stco");
96 avio_wb32(pb, 0); /* version & flags */
97 avio_wb32(pb, track->entry); /* entry count */
98 for (i = 0; i < track->entry; i++) {
100 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
102 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
104 return update_size(pb, pos);
107 /* Sample size atom */
108 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
111 int i, j, entries = 0, tst = -1, oldtst = -1;
113 int64_t pos = avio_tell(pb);
114 avio_wb32(pb, 0); /* size */
115 ffio_wfourcc(pb, "stsz");
116 avio_wb32(pb, 0); /* version & flags */
118 for (i = 0; i < track->entry; i++) {
119 tst = track->cluster[i].size / track->cluster[i].entries;
120 if (oldtst != -1 && tst != oldtst)
123 entries += track->cluster[i].entries;
125 if (equalChunks && track->entry) {
126 int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
127 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
128 avio_wb32(pb, sSize); // sample size
129 avio_wb32(pb, entries); // sample count
131 avio_wb32(pb, 0); // sample size
132 avio_wb32(pb, entries); // sample count
133 for (i = 0; i < track->entry; i++) {
134 for (j = 0; j < track->cluster[i].entries; j++) {
135 avio_wb32(pb, track->cluster[i].size /
136 track->cluster[i].entries);
140 return update_size(pb, pos);
143 /* Sample to chunk atom */
144 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
146 int index = 0, oldval = -1, i;
147 int64_t entryPos, curpos;
149 int64_t pos = avio_tell(pb);
150 avio_wb32(pb, 0); /* size */
151 ffio_wfourcc(pb, "stsc");
152 avio_wb32(pb, 0); // version & flags
153 entryPos = avio_tell(pb);
154 avio_wb32(pb, track->entry); // entry count
155 for (i = 0; i < track->entry; i++) {
156 if (oldval != track->cluster[i].samples_in_chunk) {
157 avio_wb32(pb, i + 1); // first chunk
158 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
159 avio_wb32(pb, 0x1); // sample description index
160 oldval = track->cluster[i].samples_in_chunk;
164 curpos = avio_tell(pb);
165 avio_seek(pb, entryPos, SEEK_SET);
166 avio_wb32(pb, index); // rewrite size
167 avio_seek(pb, curpos, SEEK_SET);
169 return update_size(pb, pos);
172 /* Sync sample atom */
173 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
175 int64_t curpos, entryPos;
177 int64_t pos = avio_tell(pb);
178 avio_wb32(pb, 0); // size
179 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
180 avio_wb32(pb, 0); // version & flags
181 entryPos = avio_tell(pb);
182 avio_wb32(pb, track->entry); // entry count
183 for (i = 0; i < track->entry; i++) {
184 if (track->cluster[i].flags & flag) {
185 avio_wb32(pb, i + 1);
189 curpos = avio_tell(pb);
190 avio_seek(pb, entryPos, SEEK_SET);
191 avio_wb32(pb, index); // rewrite size
192 avio_seek(pb, curpos, SEEK_SET);
193 return update_size(pb, pos);
196 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
198 avio_wb32(pb, 0x11); /* size */
199 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
200 else ffio_wfourcc(pb, "damr");
201 ffio_wfourcc(pb, "FFMP");
202 avio_w8(pb, 0); /* decoder version */
204 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
205 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
206 avio_w8(pb, 0x01); /* Frames per sample */
210 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
215 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
217 if (track->vos_len < 7)
221 ffio_wfourcc(pb, "dac3");
223 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
224 fscod = get_bits(&gbc, 2);
225 frmsizecod = get_bits(&gbc, 6);
226 bsid = get_bits(&gbc, 5);
227 bsmod = get_bits(&gbc, 3);
228 acmod = get_bits(&gbc, 3);
230 skip_bits(&gbc, 2); // dsurmod
232 if ((acmod & 1) && acmod != 1)
233 skip_bits(&gbc, 2); // cmixlev
235 skip_bits(&gbc, 2); // surmixlev
237 lfeon = get_bits1(&gbc);
239 init_put_bits(&pbc, buf, sizeof(buf));
240 put_bits(&pbc, 2, fscod);
241 put_bits(&pbc, 5, bsid);
242 put_bits(&pbc, 3, bsmod);
243 put_bits(&pbc, 3, acmod);
244 put_bits(&pbc, 1, lfeon);
245 put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
246 put_bits(&pbc, 5, 0); // reserved
248 flush_put_bits(&pbc);
249 avio_write(pb, buf, sizeof(buf));
255 * This function writes extradata "as is".
256 * Extradata must be formatted like a valid atom (with size and tag).
258 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
260 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
261 return track->enc->extradata_size;
264 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
269 avio_w8(pb, (size >> (7 * i)) | 0x80);
270 avio_w8(pb, size & 0x7F);
273 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
275 int64_t pos = avio_tell(pb);
276 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
278 avio_wb32(pb, 0); // size
279 ffio_wfourcc(pb, "esds");
280 avio_wb32(pb, 0); // Version
283 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
284 avio_wb16(pb, track->track_id);
285 avio_w8(pb, 0x00); // flags (= no flags)
287 // DecoderConfig descriptor
288 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
290 // Object type indication
291 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
292 track->enc->codec_id == AV_CODEC_ID_MP3) &&
293 track->enc->sample_rate > 24000)
294 avio_w8(pb, 0x6B); // 11172-3
296 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
298 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
299 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
300 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
301 avio_w8(pb, 0x15); // flags (= Audiostream)
303 avio_w8(pb, 0x11); // flags (= Visualstream)
305 avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
307 avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
308 if (track->enc->rc_max_rate != track->enc->rc_min_rate ||
309 track->enc->rc_min_rate == 0)
310 avio_wb32(pb, 0); // vbr
312 avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
314 if (track->vos_len) {
315 // DecoderSpecific info descriptor
316 put_descr(pb, 0x05, track->vos_len);
317 avio_write(pb, track->vos_data, track->vos_len);
321 put_descr(pb, 0x06, 1);
323 return update_size(pb, pos);
326 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
328 int64_t pos = avio_tell(pb);
330 avio_wl32(pb, track->tag); // store it byteswapped
331 track->enc->codec_tag = av_bswap16(track->tag >> 16);
332 ff_put_wav_header(pb, track->enc);
333 return update_size(pb, pos);
336 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
338 int64_t pos = avio_tell(pb);
340 ffio_wfourcc(pb, "wfex");
341 ff_put_wav_header(pb, track->enc);
342 return update_size(pb, pos);
345 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
347 uint32_t layout_tag, bitmap;
348 int64_t pos = avio_tell(pb);
350 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
351 track->enc->channel_layout,
354 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
355 "lack of channel information\n");
359 avio_wb32(pb, 0); // Size
360 ffio_wfourcc(pb, "chan"); // Type
361 avio_w8(pb, 0); // Version
362 avio_wb24(pb, 0); // Flags
363 avio_wb32(pb, layout_tag); // mChannelLayoutTag
364 avio_wb32(pb, bitmap); // mChannelBitmap
365 avio_wb32(pb, 0); // mNumberChannelDescriptions
367 return update_size(pb, pos);
370 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
372 int64_t pos = avio_tell(pb);
374 avio_wb32(pb, 0); /* size */
375 ffio_wfourcc(pb, "wave");
377 avio_wb32(pb, 12); /* size */
378 ffio_wfourcc(pb, "frma");
379 avio_wl32(pb, track->tag);
381 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
382 /* useless atom needed by mplayer, ipod, not needed by quicktime */
383 avio_wb32(pb, 12); /* size */
384 ffio_wfourcc(pb, "mp4a");
386 mov_write_esds_tag(pb, track);
387 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
388 mov_write_amr_tag(pb, track);
389 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
390 mov_write_ac3_tag(pb, track);
391 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC) {
392 mov_write_extradata_tag(pb, track);
393 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
394 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
395 mov_write_ms_tag(pb, track);
398 avio_wb32(pb, 8); /* size */
399 avio_wb32(pb, 0); /* null tag */
401 return update_size(pb, pos);
404 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
407 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
408 int unescaped_size, seq_found = 0;
409 int level = 0, interlace = 0;
410 int packet_seq = track->vc1_info.packet_seq;
411 int packet_entry = track->vc1_info.packet_entry;
412 int slices = track->vc1_info.slices;
415 if (track->start_dts == AV_NOPTS_VALUE) {
416 /* No packets written yet, vc1_info isn't authoritative yet. */
417 /* Assume inline sequence and entry headers. This will be
418 * overwritten at the end if the file is seekable. */
419 packet_seq = packet_entry = 1;
422 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
424 return AVERROR(ENOMEM);
425 start = find_next_marker(track->vos_data, end);
426 for (next = start; next < end; start = next) {
429 next = find_next_marker(start + 4, end);
430 size = next - start - 4;
433 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
434 init_get_bits(&gb, unescaped, 8 * unescaped_size);
435 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
436 int profile = get_bits(&gb, 2);
437 if (profile != PROFILE_ADVANCED) {
439 return AVERROR(ENOSYS);
442 level = get_bits(&gb, 3);
443 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
445 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
446 skip_bits(&gb, 1); /* broadcast */
447 interlace = get_bits1(&gb);
448 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
453 return AVERROR(ENOSYS);
456 init_put_bits(&pbc, buf, 7);
457 /* VC1DecSpecStruc */
458 put_bits(&pbc, 4, 12); /* profile - advanced */
459 put_bits(&pbc, 3, level);
460 put_bits(&pbc, 1, 0); /* reserved */
461 /* VC1AdvDecSpecStruc */
462 put_bits(&pbc, 3, level);
463 put_bits(&pbc, 1, 0); /* cbr */
464 put_bits(&pbc, 6, 0); /* reserved */
465 put_bits(&pbc, 1, !interlace); /* no interlace */
466 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
467 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
468 put_bits(&pbc, 1, !slices); /* no slice code */
469 put_bits(&pbc, 1, 0); /* no bframe */
470 put_bits(&pbc, 1, 0); /* reserved */
471 put_bits32(&pbc, track->enc->time_base.den); /* framerate */
472 flush_put_bits(&pbc);
479 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
481 uint8_t buf[7] = { 0 };
484 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
487 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
488 ffio_wfourcc(pb, "dvc1");
489 track->vc1_info.struct_offset = avio_tell(pb);
490 avio_write(pb, buf, sizeof(buf));
491 avio_write(pb, track->vos_data, track->vos_len);
496 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
498 avio_wb32(pb, track->vos_len + 8);
499 ffio_wfourcc(pb, "glbl");
500 avio_write(pb, track->vos_data, track->vos_len);
501 return 8 + track->vos_len;
505 * Compute flags for 'lpcm' tag.
506 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
508 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
511 case AV_CODEC_ID_PCM_F32BE:
512 case AV_CODEC_ID_PCM_F64BE:
514 case AV_CODEC_ID_PCM_F32LE:
515 case AV_CODEC_ID_PCM_F64LE:
517 case AV_CODEC_ID_PCM_U8:
519 case AV_CODEC_ID_PCM_S16BE:
520 case AV_CODEC_ID_PCM_S24BE:
521 case AV_CODEC_ID_PCM_S32BE:
523 case AV_CODEC_ID_PCM_S8:
524 case AV_CODEC_ID_PCM_S16LE:
525 case AV_CODEC_ID_PCM_S24LE:
526 case AV_CODEC_ID_PCM_S32LE:
533 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
537 if (cluster_idx >= track->entry)
540 if (cluster_idx + 1 == track->entry)
541 next_dts = track->track_duration + track->start_dts;
543 next_dts = track->cluster[cluster_idx + 1].dts;
545 return next_dts - track->cluster[cluster_idx].dts;
548 static int get_samples_per_packet(MOVTrack *track)
550 int i, first_duration;
552 /* use 1 for raw PCM */
553 if (!track->audio_vbr)
556 /* check to see if duration is constant for all clusters */
559 first_duration = get_cluster_duration(track, 0);
560 for (i = 1; i < track->entry; i++) {
561 if (get_cluster_duration(track, i) != first_duration)
564 return first_duration;
567 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
569 int64_t pos = avio_tell(pb);
571 uint32_t tag = track->tag;
573 if (track->mode == MODE_MOV) {
574 if (mov_get_lpcm_flags(track->enc->codec_id))
575 tag = AV_RL32("lpcm");
579 avio_wb32(pb, 0); /* size */
580 avio_wl32(pb, tag); // store it byteswapped
581 avio_wb32(pb, 0); /* Reserved */
582 avio_wb16(pb, 0); /* Reserved */
583 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
585 /* SoundDescription */
586 avio_wb16(pb, version); /* Version */
587 avio_wb16(pb, 0); /* Revision level */
588 avio_wb32(pb, 0); /* Reserved */
593 avio_wb16(pb, 0xfffe);
595 avio_wb32(pb, 0x00010000);
597 avio_wb64(pb, av_double2int(track->enc->sample_rate));
598 avio_wb32(pb, track->enc->channels);
599 avio_wb32(pb, 0x7F000000);
600 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
601 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
602 avio_wb32(pb, track->sample_size);
603 avio_wb32(pb, get_samples_per_packet(track));
605 /* reserved for mp4/3gp */
610 avio_wb16(pb, 0); /* packet size (= 0) */
611 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
612 track->enc->sample_rate : 0);
613 avio_wb16(pb, 0); /* Reserved */
616 if (track->mode == MODE_MOV &&
617 (track->enc->codec_id == AV_CODEC_ID_AAC ||
618 track->enc->codec_id == AV_CODEC_ID_AC3 ||
619 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
620 track->enc->codec_id == AV_CODEC_ID_ALAC ||
621 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
622 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV))
623 mov_write_wave_tag(pb, track);
624 else if (track->tag == MKTAG('m','p','4','a'))
625 mov_write_esds_tag(pb, track);
626 else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
627 mov_write_amr_tag(pb, track);
628 else if (track->enc->codec_id == AV_CODEC_ID_AC3)
629 mov_write_ac3_tag(pb, track);
630 else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
631 mov_write_extradata_tag(pb, track);
632 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
633 mov_write_wfex_tag(pb, track);
634 else if (track->vos_len > 0)
635 mov_write_glbl_tag(pb, track);
637 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
638 mov_write_chan_tag(pb, track);
640 return update_size(pb, pos);
643 static int mov_write_d263_tag(AVIOContext *pb)
645 avio_wb32(pb, 0xf); /* size */
646 ffio_wfourcc(pb, "d263");
647 ffio_wfourcc(pb, "FFMP");
648 avio_w8(pb, 0); /* decoder version */
649 /* FIXME use AVCodecContext level/profile, when encoder will set values */
650 avio_w8(pb, 0xa); /* level */
651 avio_w8(pb, 0); /* profile */
655 /* TODO: No idea about these values */
656 static int mov_write_svq3_tag(AVIOContext *pb)
659 ffio_wfourcc(pb, "SMI ");
660 ffio_wfourcc(pb, "SEQH");
662 avio_wb32(pb, 0xe2c0211d);
663 avio_wb32(pb, 0xc0000000);
668 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
670 int64_t pos = avio_tell(pb);
673 ffio_wfourcc(pb, "avcC");
674 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
675 return update_size(pb, pos);
678 /* also used by all avid codecs (dv, imx, meridien) and their variants */
679 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
682 avio_wb32(pb, 24); /* size */
683 ffio_wfourcc(pb, "ACLR");
684 ffio_wfourcc(pb, "ACLR");
685 ffio_wfourcc(pb, "0001");
686 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
687 avio_wb32(pb, 0); /* unknown */
689 avio_wb32(pb, 24); /* size */
690 ffio_wfourcc(pb, "APRG");
691 ffio_wfourcc(pb, "APRG");
692 ffio_wfourcc(pb, "0001");
693 avio_wb32(pb, 1); /* unknown */
694 avio_wb32(pb, 0); /* unknown */
696 avio_wb32(pb, 120); /* size */
697 ffio_wfourcc(pb, "ARES");
698 ffio_wfourcc(pb, "ARES");
699 ffio_wfourcc(pb, "0001");
700 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
701 avio_wb32(pb, track->enc->width);
702 /* values below are based on samples created with quicktime and avid codecs */
703 if (track->vos_data[5] & 2) { // interlaced
704 avio_wb32(pb, track->enc->height / 2);
705 avio_wb32(pb, 2); /* unknown */
706 avio_wb32(pb, 0); /* unknown */
707 avio_wb32(pb, 4); /* unknown */
709 avio_wb32(pb, track->enc->height);
710 avio_wb32(pb, 1); /* unknown */
711 avio_wb32(pb, 0); /* unknown */
712 if (track->enc->height == 1080)
713 avio_wb32(pb, 5); /* unknown */
715 avio_wb32(pb, 6); /* unknown */
718 for (i = 0; i < 10; i++)
721 /* extra padding for stsd needed */
726 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
728 int tag = track->enc->codec_tag;
730 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
733 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
734 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
735 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
736 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
737 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
738 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
739 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
744 static const AVCodecTag codec_ipod_tags[] = {
745 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
746 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
747 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
748 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
749 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
750 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
751 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
752 { AV_CODEC_ID_NONE, 0 },
755 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
757 int tag = track->enc->codec_tag;
759 // keep original tag for subs, ipod supports both formats
760 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
761 (tag == MKTAG('t', 'x', '3', 'g') ||
762 tag == MKTAG('t', 'e', 'x', 't'))))
763 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
765 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
766 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
767 "Quicktime/Ipod might not play the file\n");
772 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
776 if (track->enc->width == 720) /* SD */
777 if (track->enc->height == 480) /* NTSC */
778 if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
779 else tag = MKTAG('d','v','c',' ');
780 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
781 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
782 else tag = MKTAG('d','v','p','p');
783 else if (track->enc->height == 720) /* HD 720 line */
784 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
785 else tag = MKTAG('d','v','h','p');
786 else if (track->enc->height == 1080) /* HD 1080 line */
787 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
788 else tag = MKTAG('d','v','h','6');
790 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
797 static const struct {
798 enum AVPixelFormat pix_fmt;
801 } mov_pix_fmt_tags[] = {
802 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
803 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
804 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
805 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
806 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
807 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
808 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
809 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
810 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
811 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
812 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
813 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
814 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
815 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
818 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
820 int tag = track->enc->codec_tag;
823 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
824 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
825 tag = mov_pix_fmt_tags[i].tag;
826 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
834 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
836 int tag = track->enc->codec_tag;
838 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
839 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
840 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
841 track->enc->codec_id == AV_CODEC_ID_H263 ||
842 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
843 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
844 tag = mov_get_dv_codec_tag(s, track);
845 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
846 tag = mov_get_rawvideo_codec_tag(s, track);
847 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
848 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
849 if (!tag) { // if no mac fcc found, try with Microsoft tags
850 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
852 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
853 "the file may be unplayable!\n");
855 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
856 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
857 if (!tag) { // if no mac fcc found, try with Microsoft tags
858 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
860 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
861 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
862 "the file may be unplayable!\n");
865 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
866 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
872 static const AVCodecTag codec_3gp_tags[] = {
873 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
874 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
875 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
876 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
877 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
878 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
879 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
880 { AV_CODEC_ID_NONE, 0 },
883 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
887 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
888 tag = mp4_get_codec_tag(s, track);
889 else if (track->mode == MODE_ISM) {
890 tag = mp4_get_codec_tag(s, track);
891 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
892 tag = MKTAG('w', 'm', 'a', ' ');
893 } else if (track->mode == MODE_IPOD)
894 tag = ipod_get_codec_tag(s, track);
895 else if (track->mode & MODE_3GP)
896 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
898 tag = mov_get_codec_tag(s, track);
904 * Needed to make file play in iPods running newest firmware
905 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
907 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
910 ffio_wfourcc(pb, "uuid");
911 avio_wb32(pb, 0x6b6840f2);
912 avio_wb32(pb, 0x5f244fc5);
913 avio_wb32(pb, 0xba39a51b);
914 avio_wb32(pb, 0xcf0323f3);
919 static const uint16_t fiel_data[] = {
920 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
923 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
925 unsigned mov_field_order = 0;
926 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
927 mov_field_order = fiel_data[track->enc->field_order];
931 ffio_wfourcc(pb, "fiel");
932 avio_wb16(pb, mov_field_order);
936 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
938 int64_t pos = avio_tell(pb);
939 avio_wb32(pb, 0); /* size */
940 avio_wl32(pb, track->tag); // store it byteswapped
941 avio_wb32(pb, 0); /* Reserved */
942 avio_wb16(pb, 0); /* Reserved */
943 avio_wb16(pb, 1); /* Data-reference index */
945 if (track->enc->extradata_size)
946 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
948 return update_size(pb, pos);
951 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
954 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
955 track->enc->sample_aspect_ratio.den, INT_MAX);
958 ffio_wfourcc(pb, "pasp");
959 avio_wb32(pb, sar.num);
960 avio_wb32(pb, sar.den);
964 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
966 int64_t pos = avio_tell(pb);
967 char compressor_name[32] = { 0 };
969 avio_wb32(pb, 0); /* size */
970 avio_wl32(pb, track->tag); // store it byteswapped
971 avio_wb32(pb, 0); /* Reserved */
972 avio_wb16(pb, 0); /* Reserved */
973 avio_wb16(pb, 1); /* Data-reference index */
975 avio_wb16(pb, 0); /* Codec stream version */
976 avio_wb16(pb, 0); /* Codec stream revision (=0) */
977 if (track->mode == MODE_MOV) {
978 ffio_wfourcc(pb, "FFMP"); /* Vendor */
979 if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
980 avio_wb32(pb, 0); /* Temporal Quality */
981 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
983 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
984 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
987 avio_wb32(pb, 0); /* Reserved */
988 avio_wb32(pb, 0); /* Reserved */
989 avio_wb32(pb, 0); /* Reserved */
991 avio_wb16(pb, track->enc->width); /* Video width */
992 avio_wb16(pb, track->height); /* Video height */
993 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
994 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
995 avio_wb32(pb, 0); /* Data size (= 0) */
996 avio_wb16(pb, 1); /* Frame count (= 1) */
998 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
999 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
1000 av_strlcpy(compressor_name, track->enc->codec->name, 32);
1001 avio_w8(pb, strlen(compressor_name));
1002 avio_write(pb, compressor_name, 31);
1004 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1005 avio_wb16(pb, track->enc->bits_per_coded_sample);
1007 avio_wb16(pb, 0x18); /* Reserved */
1008 avio_wb16(pb, 0xffff); /* Reserved */
1009 if (track->tag == MKTAG('m','p','4','v'))
1010 mov_write_esds_tag(pb, track);
1011 else if (track->enc->codec_id == AV_CODEC_ID_H263)
1012 mov_write_d263_tag(pb);
1013 else if (track->enc->codec_id == AV_CODEC_ID_SVQ3)
1014 mov_write_svq3_tag(pb);
1015 else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
1016 mov_write_avid_tag(pb, track);
1017 else if (track->enc->codec_id == AV_CODEC_ID_H264) {
1018 mov_write_avcc_tag(pb, track);
1019 if (track->mode == MODE_IPOD)
1020 mov_write_uuid_tag_ipod(pb);
1021 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1022 mov_write_fiel_tag(pb, track);
1023 else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1024 mov_write_dvc1_tag(pb, track);
1025 else if (track->vos_len > 0)
1026 mov_write_glbl_tag(pb, track);
1028 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1029 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1030 mov_write_pasp_tag(pb, track);
1033 return update_size(pb, pos);
1036 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1038 int64_t pos = avio_tell(pb);
1039 avio_wb32(pb, 0); /* size */
1040 ffio_wfourcc(pb, "rtp ");
1041 avio_wb32(pb, 0); /* Reserved */
1042 avio_wb16(pb, 0); /* Reserved */
1043 avio_wb16(pb, 1); /* Data-reference index */
1045 avio_wb16(pb, 1); /* Hint track version */
1046 avio_wb16(pb, 1); /* Highest compatible version */
1047 avio_wb32(pb, track->max_packet_size); /* Max packet size */
1049 avio_wb32(pb, 12); /* size */
1050 ffio_wfourcc(pb, "tims");
1051 avio_wb32(pb, track->timescale);
1053 return update_size(pb, pos);
1056 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1058 int64_t pos = avio_tell(pb);
1059 avio_wb32(pb, 0); /* size */
1060 ffio_wfourcc(pb, "stsd");
1061 avio_wb32(pb, 0); /* version & flags */
1062 avio_wb32(pb, 1); /* entry count */
1063 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1064 mov_write_video_tag(pb, track);
1065 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1066 mov_write_audio_tag(pb, track);
1067 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1068 mov_write_subtitle_tag(pb, track);
1069 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1070 mov_write_rtp_tag(pb, track);
1071 return update_size(pb, pos);
1074 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1076 MOVStts *ctts_entries;
1077 uint32_t entries = 0;
1081 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1082 ctts_entries[0].count = 1;
1083 ctts_entries[0].duration = track->cluster[0].cts;
1084 for (i = 1; i < track->entry; i++) {
1085 if (track->cluster[i].cts == ctts_entries[entries].duration) {
1086 ctts_entries[entries].count++; /* compress */
1089 ctts_entries[entries].duration = track->cluster[i].cts;
1090 ctts_entries[entries].count = 1;
1093 entries++; /* last one */
1094 atom_size = 16 + (entries * 8);
1095 avio_wb32(pb, atom_size); /* size */
1096 ffio_wfourcc(pb, "ctts");
1097 avio_wb32(pb, 0); /* version & flags */
1098 avio_wb32(pb, entries); /* entry count */
1099 for (i = 0; i < entries; i++) {
1100 avio_wb32(pb, ctts_entries[i].count);
1101 avio_wb32(pb, ctts_entries[i].duration);
1103 av_free(ctts_entries);
1107 /* Time to sample atom */
1108 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1110 MOVStts *stts_entries;
1111 uint32_t entries = -1;
1115 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1116 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1117 stts_entries[0].count = track->sample_count;
1118 stts_entries[0].duration = 1;
1121 stts_entries = track->entry ?
1122 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1124 for (i = 0; i < track->entry; i++) {
1125 int duration = get_cluster_duration(track, i);
1126 if (i && duration == stts_entries[entries].duration) {
1127 stts_entries[entries].count++; /* compress */
1130 stts_entries[entries].duration = duration;
1131 stts_entries[entries].count = 1;
1134 entries++; /* last one */
1136 atom_size = 16 + (entries * 8);
1137 avio_wb32(pb, atom_size); /* size */
1138 ffio_wfourcc(pb, "stts");
1139 avio_wb32(pb, 0); /* version & flags */
1140 avio_wb32(pb, entries); /* entry count */
1141 for (i = 0; i < entries; i++) {
1142 avio_wb32(pb, stts_entries[i].count);
1143 avio_wb32(pb, stts_entries[i].duration);
1145 av_free(stts_entries);
1149 static int mov_write_dref_tag(AVIOContext *pb)
1151 avio_wb32(pb, 28); /* size */
1152 ffio_wfourcc(pb, "dref");
1153 avio_wb32(pb, 0); /* version & flags */
1154 avio_wb32(pb, 1); /* entry count */
1156 avio_wb32(pb, 0xc); /* size */
1157 ffio_wfourcc(pb, "url ");
1158 avio_wb32(pb, 1); /* version & flags */
1163 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1165 int64_t pos = avio_tell(pb);
1166 avio_wb32(pb, 0); /* size */
1167 ffio_wfourcc(pb, "stbl");
1168 mov_write_stsd_tag(pb, track);
1169 mov_write_stts_tag(pb, track);
1170 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1171 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1172 track->has_keyframes && track->has_keyframes < track->entry)
1173 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1174 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1175 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1176 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1177 track->flags & MOV_TRACK_CTTS)
1178 mov_write_ctts_tag(pb, track);
1179 mov_write_stsc_tag(pb, track);
1180 mov_write_stsz_tag(pb, track);
1181 mov_write_stco_tag(pb, track);
1182 return update_size(pb, pos);
1185 static int mov_write_dinf_tag(AVIOContext *pb)
1187 int64_t pos = avio_tell(pb);
1188 avio_wb32(pb, 0); /* size */
1189 ffio_wfourcc(pb, "dinf");
1190 mov_write_dref_tag(pb);
1191 return update_size(pb, pos);
1194 static int mov_write_nmhd_tag(AVIOContext *pb)
1197 ffio_wfourcc(pb, "nmhd");
1202 static int mov_write_gmhd_tag(AVIOContext *pb)
1204 avio_wb32(pb, 0x20); /* size */
1205 ffio_wfourcc(pb, "gmhd");
1206 avio_wb32(pb, 0x18); /* gmin size */
1207 ffio_wfourcc(pb, "gmin");/* generic media info */
1208 avio_wb32(pb, 0); /* version & flags */
1209 avio_wb16(pb, 0x40); /* graphics mode = */
1210 avio_wb16(pb, 0x8000); /* opColor (r?) */
1211 avio_wb16(pb, 0x8000); /* opColor (g?) */
1212 avio_wb16(pb, 0x8000); /* opColor (b?) */
1213 avio_wb16(pb, 0); /* balance */
1214 avio_wb16(pb, 0); /* reserved */
1218 static int mov_write_smhd_tag(AVIOContext *pb)
1220 avio_wb32(pb, 16); /* size */
1221 ffio_wfourcc(pb, "smhd");
1222 avio_wb32(pb, 0); /* version & flags */
1223 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1224 avio_wb16(pb, 0); /* reserved */
1228 static int mov_write_vmhd_tag(AVIOContext *pb)
1230 avio_wb32(pb, 0x14); /* size (always 0x14) */
1231 ffio_wfourcc(pb, "vmhd");
1232 avio_wb32(pb, 0x01); /* version & flags */
1233 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1237 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1239 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1240 int64_t pos = avio_tell(pb);
1244 descr = "DataHandler";
1247 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1248 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1250 descr = "VideoHandler";
1251 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1253 descr = "SoundHandler";
1254 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1255 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1256 else hdlr_type = "text";
1257 descr = "SubtitleHandler";
1258 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1260 descr = "HintHandler";
1261 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1263 descr = "TimeCodeHandler";
1266 av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1267 track->enc->codec_tag);
1269 av_log(track->enc, AV_LOG_WARNING,
1270 "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1271 tag_buf, track->enc->codec_tag);
1275 avio_wb32(pb, 0); /* size */
1276 ffio_wfourcc(pb, "hdlr");
1277 avio_wb32(pb, 0); /* Version & flags */
1278 avio_write(pb, hdlr, 4); /* handler */
1279 ffio_wfourcc(pb, hdlr_type); /* handler type */
1280 avio_wb32(pb, 0); /* reserved */
1281 avio_wb32(pb, 0); /* reserved */
1282 avio_wb32(pb, 0); /* reserved */
1283 if (!track || track->mode == MODE_MOV)
1284 avio_w8(pb, strlen(descr)); /* pascal string */
1285 avio_write(pb, descr, strlen(descr)); /* handler description */
1286 if (track && track->mode != MODE_MOV)
1287 avio_w8(pb, 0); /* c string */
1288 return update_size(pb, pos);
1291 static int mov_write_hmhd_tag(AVIOContext *pb)
1293 /* This atom must be present, but leaving the values at zero
1294 * seems harmless. */
1295 avio_wb32(pb, 28); /* size */
1296 ffio_wfourcc(pb, "hmhd");
1297 avio_wb32(pb, 0); /* version, flags */
1298 avio_wb16(pb, 0); /* maxPDUsize */
1299 avio_wb16(pb, 0); /* avgPDUsize */
1300 avio_wb32(pb, 0); /* maxbitrate */
1301 avio_wb32(pb, 0); /* avgbitrate */
1302 avio_wb32(pb, 0); /* reserved */
1306 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1308 int64_t pos = avio_tell(pb);
1309 avio_wb32(pb, 0); /* size */
1310 ffio_wfourcc(pb, "minf");
1311 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1312 mov_write_vmhd_tag(pb);
1313 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1314 mov_write_smhd_tag(pb);
1315 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1316 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1317 else mov_write_nmhd_tag(pb);
1318 } else if (track->tag == MKTAG('r','t','p',' ')) {
1319 mov_write_hmhd_tag(pb);
1321 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1322 mov_write_hdlr_tag(pb, NULL);
1323 mov_write_dinf_tag(pb);
1324 mov_write_stbl_tag(pb, track);
1325 return update_size(pb, pos);
1328 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1330 int version = track->track_duration < INT32_MAX ? 0 : 1;
1332 if (track->mode == MODE_ISM)
1335 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1336 ffio_wfourcc(pb, "mdhd");
1337 avio_w8(pb, version);
1338 avio_wb24(pb, 0); /* flags */
1340 avio_wb64(pb, track->time);
1341 avio_wb64(pb, track->time);
1343 avio_wb32(pb, track->time); /* creation time */
1344 avio_wb32(pb, track->time); /* modification time */
1346 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1348 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1350 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1351 avio_wb16(pb, track->language); /* language */
1352 avio_wb16(pb, 0); /* reserved (quality) */
1354 if (version != 0 && track->mode == MODE_MOV) {
1355 av_log(NULL, AV_LOG_ERROR,
1356 "FATAL error, file duration too long for timebase, this file will not be\n"
1357 "playable with quicktime. Choose a different timebase or a different\n"
1358 "container format\n");
1364 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1366 int64_t pos = avio_tell(pb);
1367 avio_wb32(pb, 0); /* size */
1368 ffio_wfourcc(pb, "mdia");
1369 mov_write_mdhd_tag(pb, track);
1370 mov_write_hdlr_tag(pb, track);
1371 mov_write_minf_tag(pb, track);
1372 return update_size(pb, pos);
1375 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1377 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1378 track->timescale, AV_ROUND_UP);
1379 int version = duration < INT32_MAX ? 0 : 1;
1381 if (track->mode == MODE_ISM)
1384 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1385 ffio_wfourcc(pb, "tkhd");
1386 avio_w8(pb, version);
1387 avio_wb24(pb, 0xf); /* flags (track enabled) */
1389 avio_wb64(pb, track->time);
1390 avio_wb64(pb, track->time);
1392 avio_wb32(pb, track->time); /* creation time */
1393 avio_wb32(pb, track->time); /* modification time */
1395 avio_wb32(pb, track->track_id); /* track-id */
1396 avio_wb32(pb, 0); /* reserved */
1398 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1400 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1402 avio_wb32(pb, 0); /* reserved */
1403 avio_wb32(pb, 0); /* reserved */
1404 avio_wb16(pb, 0); /* layer */
1405 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1406 /* Volume, only for audio */
1407 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1408 avio_wb16(pb, 0x0100);
1411 avio_wb16(pb, 0); /* reserved */
1413 /* Matrix structure */
1414 avio_wb32(pb, 0x00010000); /* reserved */
1415 avio_wb32(pb, 0x0); /* reserved */
1416 avio_wb32(pb, 0x0); /* reserved */
1417 avio_wb32(pb, 0x0); /* reserved */
1418 avio_wb32(pb, 0x00010000); /* reserved */
1419 avio_wb32(pb, 0x0); /* reserved */
1420 avio_wb32(pb, 0x0); /* reserved */
1421 avio_wb32(pb, 0x0); /* reserved */
1422 avio_wb32(pb, 0x40000000); /* reserved */
1424 /* Track width and height, for visual only */
1425 if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1426 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1427 if (track->mode == MODE_MOV) {
1428 avio_wb32(pb, track->enc->width << 16);
1429 avio_wb32(pb, track->height << 16);
1431 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1432 if (!sample_aspect_ratio || track->height != track->enc->height)
1433 sample_aspect_ratio = 1;
1434 avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1435 avio_wb32(pb, track->height * 0x10000);
1444 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1446 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1447 track->enc->sample_aspect_ratio.den);
1449 int64_t pos = avio_tell(pb);
1451 avio_wb32(pb, 0); /* size */
1452 ffio_wfourcc(pb, "tapt");
1455 ffio_wfourcc(pb, "clef");
1457 avio_wb32(pb, width << 16);
1458 avio_wb32(pb, track->enc->height << 16);
1461 ffio_wfourcc(pb, "enof");
1463 avio_wb32(pb, track->enc->width << 16);
1464 avio_wb32(pb, track->enc->height << 16);
1466 return update_size(pb, pos);
1469 // This box seems important for the psp playback ... without it the movie seems to hang
1470 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1472 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1473 track->timescale, AV_ROUND_UP);
1474 int version = duration < INT32_MAX ? 0 : 1;
1475 int entry_size, entry_count, size;
1476 int64_t delay, start_ct = track->cluster[0].cts;
1477 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1478 track->timescale, AV_ROUND_DOWN);
1479 version |= delay < INT32_MAX ? 0 : 1;
1481 entry_size = (version == 1) ? 20 : 12;
1482 entry_count = 1 + (delay > 0);
1483 size = 24 + entry_count * entry_size;
1485 /* write the atom data */
1486 avio_wb32(pb, size);
1487 ffio_wfourcc(pb, "edts");
1488 avio_wb32(pb, size - 8);
1489 ffio_wfourcc(pb, "elst");
1490 avio_w8(pb, version);
1491 avio_wb24(pb, 0); /* flags */
1493 avio_wb32(pb, entry_count);
1494 if (delay > 0) { /* add an empty edit to delay presentation */
1496 avio_wb64(pb, delay);
1499 avio_wb32(pb, delay);
1502 avio_wb32(pb, 0x00010000);
1507 avio_wb64(pb, duration);
1508 avio_wb64(pb, start_ct);
1510 avio_wb32(pb, duration);
1511 avio_wb32(pb, start_ct);
1513 avio_wb32(pb, 0x00010000);
1517 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1519 avio_wb32(pb, 20); // size
1520 ffio_wfourcc(pb, "tref");
1521 avio_wb32(pb, 12); // size (subatom)
1522 avio_wl32(pb, track->tref_tag);
1523 avio_wb32(pb, track->tref_id);
1527 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1528 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1530 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1531 ffio_wfourcc(pb, "uuid");
1532 ffio_wfourcc(pb, "USMT");
1533 avio_wb32(pb, 0x21d24fce);
1534 avio_wb32(pb, 0xbb88695c);
1535 avio_wb32(pb, 0xfac9c740);
1536 avio_wb32(pb, 0x1c); // another size here!
1537 ffio_wfourcc(pb, "MTDT");
1538 avio_wb32(pb, 0x00010012);
1539 avio_wb32(pb, 0x0a);
1540 avio_wb32(pb, 0x55c40000);
1546 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1548 AVFormatContext *ctx = track->rtp_ctx;
1549 char buf[1000] = "";
1552 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1553 NULL, NULL, 0, 0, ctx);
1554 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1557 avio_wb32(pb, len + 24);
1558 ffio_wfourcc(pb, "udta");
1559 avio_wb32(pb, len + 16);
1560 ffio_wfourcc(pb, "hnti");
1561 avio_wb32(pb, len + 8);
1562 ffio_wfourcc(pb, "sdp ");
1563 avio_write(pb, buf, len);
1567 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1568 MOVTrack *track, AVStream *st)
1570 int64_t pos = avio_tell(pb);
1571 avio_wb32(pb, 0); /* size */
1572 ffio_wfourcc(pb, "trak");
1573 mov_write_tkhd_tag(pb, track, st);
1574 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1575 (track->entry && track->cluster[0].dts)) {
1576 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1577 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1579 if (track->tref_tag)
1580 mov_write_tref_tag(pb, track);
1581 mov_write_mdia_tag(pb, track);
1582 if (track->mode == MODE_PSP)
1583 mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
1584 if (track->tag == MKTAG('r','t','p',' '))
1585 mov_write_udta_sdp(pb, track);
1586 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1587 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1588 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1589 mov_write_tapt_tag(pb, track);
1591 return update_size(pb, pos);
1594 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1596 int i, has_audio = 0, has_video = 0;
1597 int64_t pos = avio_tell(pb);
1598 int audio_profile = mov->iods_audio_profile;
1599 int video_profile = mov->iods_video_profile;
1600 for (i = 0; i < mov->nb_streams; i++) {
1601 if (mov->tracks[i].entry > 0) {
1602 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1603 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1606 if (audio_profile < 0)
1607 audio_profile = 0xFF - has_audio;
1608 if (video_profile < 0)
1609 video_profile = 0xFF - has_video;
1610 avio_wb32(pb, 0x0); /* size */
1611 ffio_wfourcc(pb, "iods");
1612 avio_wb32(pb, 0); /* version & flags */
1613 put_descr(pb, 0x10, 7);
1614 avio_wb16(pb, 0x004f);
1617 avio_w8(pb, audio_profile);
1618 avio_w8(pb, video_profile);
1620 return update_size(pb, pos);
1623 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1625 avio_wb32(pb, 0x20); /* size */
1626 ffio_wfourcc(pb, "trex");
1627 avio_wb32(pb, 0); /* version & flags */
1628 avio_wb32(pb, track->track_id); /* track ID */
1629 avio_wb32(pb, 1); /* default sample description index */
1630 avio_wb32(pb, 0); /* default sample duration */
1631 avio_wb32(pb, 0); /* default sample size */
1632 avio_wb32(pb, 0); /* default sample flags */
1636 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1638 int64_t pos = avio_tell(pb);
1640 avio_wb32(pb, 0x0); /* size */
1641 ffio_wfourcc(pb, "mvex");
1642 for (i = 0; i < mov->nb_streams; i++)
1643 mov_write_trex_tag(pb, &mov->tracks[i]);
1644 return update_size(pb, pos);
1647 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1649 int max_track_id = 1, i;
1650 int64_t max_track_len_temp, max_track_len = 0;
1653 for (i = 0; i < mov->nb_streams; i++) {
1654 if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
1655 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1657 mov->tracks[i].timescale,
1659 if (max_track_len < max_track_len_temp)
1660 max_track_len = max_track_len_temp;
1661 if (max_track_id < mov->tracks[i].track_id)
1662 max_track_id = mov->tracks[i].track_id;
1666 version = max_track_len < UINT32_MAX ? 0 : 1;
1667 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1668 ffio_wfourcc(pb, "mvhd");
1669 avio_w8(pb, version);
1670 avio_wb24(pb, 0); /* flags */
1672 avio_wb64(pb, mov->time);
1673 avio_wb64(pb, mov->time);
1675 avio_wb32(pb, mov->time); /* creation time */
1676 avio_wb32(pb, mov->time); /* modification time */
1678 avio_wb32(pb, MOV_TIMESCALE);
1679 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1681 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1682 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1683 avio_wb16(pb, 0); /* reserved */
1684 avio_wb32(pb, 0); /* reserved */
1685 avio_wb32(pb, 0); /* reserved */
1687 /* Matrix structure */
1688 avio_wb32(pb, 0x00010000); /* reserved */
1689 avio_wb32(pb, 0x0); /* reserved */
1690 avio_wb32(pb, 0x0); /* reserved */
1691 avio_wb32(pb, 0x0); /* reserved */
1692 avio_wb32(pb, 0x00010000); /* reserved */
1693 avio_wb32(pb, 0x0); /* reserved */
1694 avio_wb32(pb, 0x0); /* reserved */
1695 avio_wb32(pb, 0x0); /* reserved */
1696 avio_wb32(pb, 0x40000000); /* reserved */
1698 avio_wb32(pb, 0); /* reserved (preview time) */
1699 avio_wb32(pb, 0); /* reserved (preview duration) */
1700 avio_wb32(pb, 0); /* reserved (poster time) */
1701 avio_wb32(pb, 0); /* reserved (selection time) */
1702 avio_wb32(pb, 0); /* reserved (selection duration) */
1703 avio_wb32(pb, 0); /* reserved (current time) */
1704 avio_wb32(pb, max_track_id + 1); /* Next track id */
1708 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1711 avio_wb32(pb, 33); /* size */
1712 ffio_wfourcc(pb, "hdlr");
1715 ffio_wfourcc(pb, "mdir");
1716 ffio_wfourcc(pb, "appl");
1723 /* helper function to write a data tag with the specified string as data */
1724 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1727 int size = 16 + strlen(data);
1728 avio_wb32(pb, size); /* size */
1729 ffio_wfourcc(pb, "data");
1732 avio_write(pb, data, strlen(data));
1736 lang = ff_mov_iso639_to_lang("und", 1);
1737 avio_wb16(pb, strlen(data)); /* string length */
1738 avio_wb16(pb, lang);
1739 avio_write(pb, data, strlen(data));
1740 return strlen(data) + 4;
1744 static int mov_write_string_tag(AVIOContext *pb, const char *name,
1745 const char *value, int lang, int long_style)
1748 if (value && value[0]) {
1749 int64_t pos = avio_tell(pb);
1750 avio_wb32(pb, 0); /* size */
1751 ffio_wfourcc(pb, name);
1752 mov_write_string_data_tag(pb, value, lang, long_style);
1753 size = update_size(pb, pos);
1758 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1759 const char *name, const char *tag,
1762 int l, lang = 0, len, len2;
1763 AVDictionaryEntry *t, *t2 = NULL;
1766 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1769 len = strlen(t->key);
1770 snprintf(tag2, sizeof(tag2), "%s-", tag);
1771 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1772 len2 = strlen(t2->key);
1773 if (len2 == len + 4 && !strcmp(t->value, t2->value)
1774 && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
1779 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1782 /* iTunes track number */
1783 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1786 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1787 int size = 0, track = t ? atoi(t->value) : 0;
1789 avio_wb32(pb, 32); /* size */
1790 ffio_wfourcc(pb, "trkn");
1791 avio_wb32(pb, 24); /* size */
1792 ffio_wfourcc(pb, "data");
1793 avio_wb32(pb, 0); // 8 bytes empty
1795 avio_wb16(pb, 0); // empty
1796 avio_wb16(pb, track); // track number
1797 avio_wb16(pb, 0); // total track number
1798 avio_wb16(pb, 0); // empty
1804 /* iTunes meta data list */
1805 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1808 int64_t pos = avio_tell(pb);
1809 avio_wb32(pb, 0); /* size */
1810 ffio_wfourcc(pb, "ilst");
1811 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1812 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1813 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1814 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1815 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1816 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1817 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1818 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1819 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1820 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1821 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1822 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1823 mov_write_string_metadata(s, pb, "desc", "description",1);
1824 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1825 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1826 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1827 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1828 mov_write_trkn_tag(pb, mov, s);
1829 return update_size(pb, pos);
1832 /* iTunes meta data tag */
1833 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1837 int64_t pos = avio_tell(pb);
1838 avio_wb32(pb, 0); /* size */
1839 ffio_wfourcc(pb, "meta");
1841 mov_write_itunes_hdlr_tag(pb, mov, s);
1842 mov_write_ilst_tag(pb, mov, s);
1843 size = update_size(pb, pos);
1847 static int utf8len(const uint8_t *b)
1852 GET_UTF8(val, *b++, return -1;)
1858 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1862 GET_UTF8(val, *b++, return -1;)
1865 avio_wb16(pb, 0x00);
1869 static uint16_t language_code(const char *str)
1871 return (((str[0] - 0x60) & 0x1F) << 10) +
1872 (((str[1] - 0x60) & 0x1F) << 5) +
1873 (( str[2] - 0x60) & 0x1F);
1876 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1877 const char *tag, const char *str)
1879 int64_t pos = avio_tell(pb);
1880 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1881 if (!t || !utf8len(t->value))
1883 avio_wb32(pb, 0); /* size */
1884 ffio_wfourcc(pb, tag); /* type */
1885 avio_wb32(pb, 0); /* version + flags */
1886 if (!strcmp(tag, "yrrc"))
1887 avio_wb16(pb, atoi(t->value));
1889 avio_wb16(pb, language_code("eng")); /* language */
1890 avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
1891 if (!strcmp(tag, "albm") &&
1892 (t = av_dict_get(s->metadata, "track", NULL, 0)))
1893 avio_w8(pb, atoi(t->value));
1895 return update_size(pb, pos);
1898 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1900 int64_t pos = avio_tell(pb);
1901 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1903 avio_wb32(pb, 0); // size
1904 ffio_wfourcc(pb, "chpl");
1905 avio_wb32(pb, 0x01000000); // version + flags
1906 avio_wb32(pb, 0); // unknown
1907 avio_w8(pb, nb_chapters);
1909 for (i = 0; i < nb_chapters; i++) {
1910 AVChapter *c = s->chapters[i];
1911 AVDictionaryEntry *t;
1912 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1914 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1915 int len = FFMIN(strlen(t->value), 255);
1917 avio_write(pb, t->value, len);
1921 return update_size(pb, pos);
1924 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1927 AVIOContext *pb_buf;
1931 for (i = 0; i < s->nb_streams; i++)
1932 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1936 ret = avio_open_dyn_buf(&pb_buf);
1940 if (mov->mode & MODE_3GP) {
1941 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1942 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1943 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1944 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1945 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1946 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1947 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1948 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1949 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1950 mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
1951 mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
1952 mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
1953 mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
1954 mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
1955 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
1956 mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
1957 mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
1958 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
1960 /* iTunes meta data */
1961 mov_write_meta_tag(pb_buf, mov, s);
1965 mov_write_chpl_tag(pb_buf, s);
1967 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1968 avio_wb32(pb, size + 8);
1969 ffio_wfourcc(pb, "udta");
1970 avio_write(pb, buf, size);
1977 static void mov_write_psp_udta_tag(AVIOContext *pb,
1978 const char *str, const char *lang, int type)
1980 int len = utf8len(str) + 1;
1983 avio_wb16(pb, len * 2 + 10); /* size */
1984 avio_wb32(pb, type); /* type */
1985 avio_wb16(pb, language_code(lang)); /* language */
1986 avio_wb16(pb, 0x01); /* ? */
1987 ascii_to_wc(pb, str);
1990 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1992 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
1996 pos = avio_tell(pb);
1997 avio_wb32(pb, 0); /* size placeholder*/
1998 ffio_wfourcc(pb, "uuid");
1999 ffio_wfourcc(pb, "USMT");
2000 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2001 avio_wb32(pb, 0xbb88695c);
2002 avio_wb32(pb, 0xfac9c740);
2004 pos2 = avio_tell(pb);
2005 avio_wb32(pb, 0); /* size placeholder*/
2006 ffio_wfourcc(pb, "MTDT");
2010 avio_wb16(pb, 0x0C); /* size */
2011 avio_wb32(pb, 0x0B); /* type */
2012 avio_wb16(pb, language_code("und")); /* language */
2013 avio_wb16(pb, 0x0); /* ? */
2014 avio_wb16(pb, 0x021C); /* data */
2016 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2017 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2018 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2020 update_size(pb, pos2);
2021 return update_size(pb, pos);
2027 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2031 int64_t pos = avio_tell(pb);
2032 avio_wb32(pb, 0); /* size placeholder*/
2033 ffio_wfourcc(pb, "moov");
2035 for (i = 0; i < mov->nb_streams; i++) {
2036 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2039 mov->tracks[i].time = mov->time;
2040 mov->tracks[i].track_id = i + 1;
2043 if (mov->chapter_track)
2044 for (i = 0; i < s->nb_streams; i++) {
2045 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2046 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2048 for (i = 0; i < mov->nb_streams; i++) {
2049 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2050 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2051 mov->tracks[i].tref_id =
2052 mov->tracks[mov->tracks[i].src_track].track_id;
2056 mov_write_mvhd_tag(pb, mov);
2057 if (mov->mode != MODE_MOV && !mov->iods_skip)
2058 mov_write_iods_tag(pb, mov);
2059 for (i = 0; i < mov->nb_streams; i++) {
2060 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2061 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2064 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2065 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2067 if (mov->mode == MODE_PSP)
2068 mov_write_uuidusmt_tag(pb, s);
2070 mov_write_udta_tag(pb, mov, s);
2072 return update_size(pb, pos);
2075 static void param_write_int(AVIOContext *pb, const char *name, int value)
2077 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2080 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2082 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2085 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2088 len = FFMIN(sizeof(buf) / 2 - 1, len);
2089 ff_data_to_hex(buf, value, len, 0);
2090 buf[2 * len] = '\0';
2091 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2094 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2096 int64_t pos = avio_tell(pb);
2098 const uint8_t uuid[] = {
2099 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2100 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2104 ffio_wfourcc(pb, "uuid");
2105 avio_write(pb, uuid, sizeof(uuid));
2108 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2109 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2110 avio_printf(pb, "<head>\n");
2111 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2113 avio_printf(pb, "</head>\n");
2114 avio_printf(pb, "<body>\n");
2115 avio_printf(pb, "<switch>\n");
2116 for (i = 0; i < mov->nb_streams; i++) {
2117 MOVTrack *track = &mov->tracks[i];
2119 /* track->track_id is initialized in write_moov, and thus isn't known
2121 int track_id = i + 1;
2123 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2125 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2130 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2131 track->enc->bit_rate);
2132 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2133 param_write_int(pb, "trackID", track_id);
2134 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2135 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2137 int size = track->enc->extradata_size;
2138 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2140 param_write_hex(pb, "CodecPrivateData",
2141 ptr ? ptr : track->enc->extradata,
2145 param_write_string(pb, "FourCC", "H264");
2146 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2147 param_write_string(pb, "FourCC", "WVC1");
2148 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2149 track->enc->extradata_size);
2151 param_write_int(pb, "MaxWidth", track->enc->width);
2152 param_write_int(pb, "MaxHeight", track->enc->height);
2153 param_write_int(pb, "DisplayWidth", track->enc->width);
2154 param_write_int(pb, "DisplayHeight", track->enc->height);
2156 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2157 param_write_string(pb, "FourCC", "AACL");
2158 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2159 param_write_string(pb, "FourCC", "WMAP");
2161 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2162 track->enc->extradata_size);
2163 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2164 track->enc->codec_id));
2165 param_write_int(pb, "Channels", track->enc->channels);
2166 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2167 param_write_int(pb, "BitsPerSample", 16);
2168 param_write_int(pb, "PacketSize", track->enc->block_align ?
2169 track->enc->block_align : 4);
2171 avio_printf(pb, "</%s>\n", type);
2173 avio_printf(pb, "</switch>\n");
2174 avio_printf(pb, "</body>\n");
2175 avio_printf(pb, "</smil>\n");
2177 return update_size(pb, pos);
2180 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2183 ffio_wfourcc(pb, "mfhd");
2185 avio_wb32(pb, mov->fragments);
2189 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2190 int64_t moof_offset)
2192 int64_t pos = avio_tell(pb);
2193 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2194 MOV_TFHD_BASE_DATA_OFFSET;
2195 if (!track->entry) {
2196 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2198 flags |= MOV_TFHD_DEFAULT_FLAGS;
2201 /* Don't set a default sample size, the silverlight player refuses
2202 * to play files with that set. Don't set a default sample duration,
2203 * WMP freaks out if it is set. Don't set a base data offset, PIFF
2204 * file format says it MUST NOT be set. */
2205 if (track->mode == MODE_ISM)
2206 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2207 MOV_TFHD_BASE_DATA_OFFSET);
2209 avio_wb32(pb, 0); /* size placeholder */
2210 ffio_wfourcc(pb, "tfhd");
2211 avio_w8(pb, 0); /* version */
2212 avio_wb24(pb, flags);
2214 avio_wb32(pb, track->track_id); /* track-id */
2215 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2216 avio_wb64(pb, moof_offset);
2217 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2218 track->default_duration = get_cluster_duration(track, 0);
2219 avio_wb32(pb, track->default_duration);
2221 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2222 track->default_size = track->entry ? track->cluster[0].size : 1;
2223 avio_wb32(pb, track->default_size);
2225 track->default_size = -1;
2227 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2228 track->default_sample_flags =
2229 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2230 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2231 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2232 avio_wb32(pb, track->default_sample_flags);
2235 return update_size(pb, pos);
2238 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2240 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2241 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2244 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2246 int64_t pos = avio_tell(pb);
2247 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2250 for (i = 0; i < track->entry; i++) {
2251 if (get_cluster_duration(track, i) != track->default_duration)
2252 flags |= MOV_TRUN_SAMPLE_DURATION;
2253 if (track->cluster[i].size != track->default_size)
2254 flags |= MOV_TRUN_SAMPLE_SIZE;
2255 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2256 flags |= MOV_TRUN_SAMPLE_FLAGS;
2258 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2259 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2260 if (track->flags & MOV_TRACK_CTTS)
2261 flags |= MOV_TRUN_SAMPLE_CTS;
2263 avio_wb32(pb, 0); /* size placeholder */
2264 ffio_wfourcc(pb, "trun");
2265 avio_w8(pb, 0); /* version */
2266 avio_wb24(pb, flags);
2268 avio_wb32(pb, track->entry); /* sample count */
2269 track->moof_size_offset = avio_tell(pb);
2270 avio_wb32(pb, 0); /* data offset */
2271 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2272 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2274 for (i = 0; i < track->entry; i++) {
2275 if (flags & MOV_TRUN_SAMPLE_DURATION)
2276 avio_wb32(pb, get_cluster_duration(track, i));
2277 if (flags & MOV_TRUN_SAMPLE_SIZE)
2278 avio_wb32(pb, track->cluster[i].size);
2279 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2280 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2281 if (flags & MOV_TRUN_SAMPLE_CTS)
2282 avio_wb32(pb, track->cluster[i].cts);
2285 return update_size(pb, pos);
2288 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2290 int64_t pos = avio_tell(pb);
2291 const uint8_t uuid[] = {
2292 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2293 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2296 avio_wb32(pb, 0); /* size placeholder */
2297 ffio_wfourcc(pb, "uuid");
2298 avio_write(pb, uuid, sizeof(uuid));
2301 avio_wb64(pb, track->frag_start);
2302 avio_wb64(pb, track->start_dts + track->track_duration -
2303 track->cluster[0].dts);
2305 return update_size(pb, pos);
2308 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2309 MOVTrack *track, int entry)
2311 int n = track->nb_frag_info - 1 - entry, i;
2312 int size = 8 + 16 + 4 + 1 + 16*n;
2313 const uint8_t uuid[] = {
2314 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2315 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2321 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2322 avio_wb32(pb, size);
2323 ffio_wfourcc(pb, "uuid");
2324 avio_write(pb, uuid, sizeof(uuid));
2328 for (i = 0; i < n; i++) {
2329 int index = entry + 1 + i;
2330 avio_wb64(pb, track->frag_info[index].time);
2331 avio_wb64(pb, track->frag_info[index].duration);
2333 if (n < mov->ism_lookahead) {
2334 int free_size = 16 * (mov->ism_lookahead - n);
2335 avio_wb32(pb, free_size);
2336 ffio_wfourcc(pb, "free");
2337 for (i = 0; i < free_size - 8; i++)
2344 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2347 int64_t pos = avio_tell(pb);
2349 for (i = 0; i < mov->ism_lookahead; i++) {
2350 /* Update the tfrf tag for the last ism_lookahead fragments,
2351 * nb_frag_info - 1 is the next fragment to be written. */
2352 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2354 avio_seek(pb, pos, SEEK_SET);
2358 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2359 MOVTrack *track, int64_t moof_offset)
2361 int64_t pos = avio_tell(pb);
2362 avio_wb32(pb, 0); /* size placeholder */
2363 ffio_wfourcc(pb, "traf");
2365 mov_write_tfhd_tag(pb, track, moof_offset);
2366 mov_write_trun_tag(pb, track);
2367 if (mov->mode == MODE_ISM) {
2368 mov_write_tfxd_tag(pb, track);
2370 if (mov->ism_lookahead) {
2371 int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
2373 track->tfrf_offset = avio_tell(pb);
2374 avio_wb32(pb, 8 + size);
2375 ffio_wfourcc(pb, "free");
2376 for (i = 0; i < size; i++)
2381 return update_size(pb, pos);
2384 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2386 int64_t pos = avio_tell(pb), end;
2389 avio_wb32(pb, 0); /* size placeholder */
2390 ffio_wfourcc(pb, "moof");
2392 mov_write_mfhd_tag(pb, mov);
2393 for (i = 0; i < mov->nb_streams; i++) {
2394 MOVTrack *track = &mov->tracks[i];
2395 if (tracks >= 0 && i != tracks)
2399 mov_write_traf_tag(pb, mov, track, pos);
2402 end = avio_tell(pb);
2403 moof_size = end - pos;
2404 for (i = 0; i < mov->nb_streams; i++) {
2405 MOVTrack *track = &mov->tracks[i];
2406 if (tracks >= 0 && i != tracks)
2410 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2411 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2413 avio_seek(pb, end, SEEK_SET);
2415 return update_size(pb, pos);
2418 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2420 int64_t pos = avio_tell(pb);
2423 avio_wb32(pb, 0); /* size placeholder */
2424 ffio_wfourcc(pb, "tfra");
2425 avio_w8(pb, 1); /* version */
2428 avio_wb32(pb, track->track_id);
2429 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2430 avio_wb32(pb, track->nb_frag_info);
2431 for (i = 0; i < track->nb_frag_info; i++) {
2432 avio_wb64(pb, track->frag_info[i].time);
2433 avio_wb64(pb, track->frag_info[i].offset);
2434 avio_w8(pb, 1); /* traf number */
2435 avio_w8(pb, 1); /* trun number */
2436 avio_w8(pb, 1); /* sample number */
2439 return update_size(pb, pos);
2442 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2444 int64_t pos = avio_tell(pb);
2447 avio_wb32(pb, 0); /* size placeholder */
2448 ffio_wfourcc(pb, "mfra");
2449 /* An empty mfra atom is enough to indicate to the publishing point that
2450 * the stream has ended. */
2451 if (mov->flags & FF_MOV_FLAG_ISML)
2452 return update_size(pb, pos);
2454 for (i = 0; i < mov->nb_streams; i++) {
2455 MOVTrack *track = &mov->tracks[i];
2456 if (track->nb_frag_info)
2457 mov_write_tfra_tag(pb, track);
2461 ffio_wfourcc(pb, "mfro");
2462 avio_wb32(pb, 0); /* version + flags */
2463 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2465 return update_size(pb, pos);
2468 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2470 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2471 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2473 mov->mdat_pos = avio_tell(pb);
2474 avio_wb32(pb, 0); /* size placeholder*/
2475 ffio_wfourcc(pb, "mdat");
2479 /* TODO: This needs to be more general */
2480 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2482 MOVMuxContext *mov = s->priv_data;
2483 int64_t pos = avio_tell(pb);
2484 int has_h264 = 0, has_video = 0;
2488 for (i = 0; i < s->nb_streams; i++) {
2489 AVStream *st = s->streams[i];
2490 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2492 if (st->codec->codec_id == AV_CODEC_ID_H264)
2496 avio_wb32(pb, 0); /* size */
2497 ffio_wfourcc(pb, "ftyp");
2499 if (mov->mode == MODE_3GP) {
2500 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2501 minor = has_h264 ? 0x100 : 0x200;
2502 } else if (mov->mode & MODE_3G2) {
2503 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2504 minor = has_h264 ? 0x20000 : 0x10000;
2505 } else if (mov->mode == MODE_PSP)
2506 ffio_wfourcc(pb, "MSNV");
2507 else if (mov->mode == MODE_MP4)
2508 ffio_wfourcc(pb, "isom");
2509 else if (mov->mode == MODE_IPOD)
2510 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2511 else if (mov->mode == MODE_ISM)
2512 ffio_wfourcc(pb, "isml");
2514 ffio_wfourcc(pb, "qt ");
2516 avio_wb32(pb, minor);
2518 if (mov->mode == MODE_MOV)
2519 ffio_wfourcc(pb, "qt ");
2520 else if (mov->mode == MODE_ISM) {
2521 ffio_wfourcc(pb, "piff");
2522 ffio_wfourcc(pb, "iso2");
2524 ffio_wfourcc(pb, "isom");
2525 ffio_wfourcc(pb, "iso2");
2527 ffio_wfourcc(pb, "avc1");
2530 if (mov->mode == MODE_3GP)
2531 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2532 else if (mov->mode & MODE_3G2)
2533 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2534 else if (mov->mode == MODE_PSP)
2535 ffio_wfourcc(pb, "MSNV");
2536 else if (mov->mode == MODE_MP4)
2537 ffio_wfourcc(pb, "mp41");
2538 return update_size(pb, pos);
2541 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
2543 AVCodecContext *video_codec = s->streams[0]->codec;
2544 AVCodecContext *audio_codec = s->streams[1]->codec;
2545 int audio_rate = audio_codec->sample_rate;
2546 int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
2547 int audio_kbitrate = audio_codec->bit_rate / 1000;
2548 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2550 avio_wb32(pb, 0x94); /* size */
2551 ffio_wfourcc(pb, "uuid");
2552 ffio_wfourcc(pb, "PROF");
2554 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2555 avio_wb32(pb, 0xbb88695c);
2556 avio_wb32(pb, 0xfac9c740);
2558 avio_wb32(pb, 0x0); /* ? */
2559 avio_wb32(pb, 0x3); /* 3 sections ? */
2561 avio_wb32(pb, 0x14); /* size */
2562 ffio_wfourcc(pb, "FPRF");
2563 avio_wb32(pb, 0x0); /* ? */
2564 avio_wb32(pb, 0x0); /* ? */
2565 avio_wb32(pb, 0x0); /* ? */
2567 avio_wb32(pb, 0x2c); /* size */
2568 ffio_wfourcc(pb, "APRF"); /* audio */
2570 avio_wb32(pb, 0x2); /* TrackID */
2571 ffio_wfourcc(pb, "mp4a");
2572 avio_wb32(pb, 0x20f);
2574 avio_wb32(pb, audio_kbitrate);
2575 avio_wb32(pb, audio_kbitrate);
2576 avio_wb32(pb, audio_rate);
2577 avio_wb32(pb, audio_codec->channels);
2579 avio_wb32(pb, 0x34); /* size */
2580 ffio_wfourcc(pb, "VPRF"); /* video */
2582 avio_wb32(pb, 0x1); /* TrackID */
2583 if (video_codec->codec_id == AV_CODEC_ID_H264) {
2584 ffio_wfourcc(pb, "avc1");
2585 avio_wb16(pb, 0x014D);
2586 avio_wb16(pb, 0x0015);
2588 ffio_wfourcc(pb, "mp4v");
2589 avio_wb16(pb, 0x0000);
2590 avio_wb16(pb, 0x0103);
2593 avio_wb32(pb, video_kbitrate);
2594 avio_wb32(pb, video_kbitrate);
2595 avio_wb32(pb, frame_rate);
2596 avio_wb32(pb, frame_rate);
2597 avio_wb16(pb, video_codec->width);
2598 avio_wb16(pb, video_codec->height);
2599 avio_wb32(pb, 0x010001); /* ? */
2602 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2605 int i, closed_gop = 0;
2607 for (i = 0; i < pkt->size - 4; i++) {
2608 c = (c << 8) + pkt->data[i];
2609 if (c == 0x1b8) { // gop
2610 closed_gop = pkt->data[i + 4] >> 6 & 0x01;
2611 } else if (c == 0x100) { // pic
2612 int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
2613 if (!temp_ref || closed_gop) // I picture is not reordered
2614 *flags = MOV_SYNC_SAMPLE;
2616 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2623 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2625 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2626 int seq = 0, entry = 0;
2627 int key = pkt->flags & AV_PKT_FLAG_KEY;
2628 start = find_next_marker(pkt->data, end);
2629 for (next = start; next < end; start = next) {
2630 next = find_next_marker(start + 4, end);
2631 switch (AV_RB32(start)) {
2632 case VC1_CODE_SEQHDR:
2635 case VC1_CODE_ENTRYPOINT:
2638 case VC1_CODE_SLICE:
2639 trk->vc1_info.slices = 1;
2643 if (!trk->entry && !fragment) {
2644 /* First packet in first fragment */
2645 trk->vc1_info.first_packet_seq = seq;
2646 trk->vc1_info.first_packet_entry = entry;
2647 } else if ((seq && !trk->vc1_info.packet_seq) ||
2648 (entry && !trk->vc1_info.packet_entry)) {
2650 for (i = 0; i < trk->entry; i++)
2651 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2652 trk->has_keyframes = 0;
2654 trk->vc1_info.packet_seq = 1;
2656 trk->vc1_info.packet_entry = 1;
2658 /* First fragment */
2659 if ((!seq || trk->vc1_info.first_packet_seq) &&
2660 (!entry || trk->vc1_info.first_packet_entry)) {
2661 /* First packet had the same headers as this one, readd the
2662 * sync sample flag. */
2663 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2664 trk->has_keyframes = 1;
2668 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2670 else if (trk->vc1_info.packet_seq)
2672 else if (trk->vc1_info.packet_entry)
2675 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2676 trk->has_keyframes++;
2680 static int mov_flush_fragment(AVFormatContext *s)
2682 MOVMuxContext *mov = s->priv_data;
2683 int i, first_track = -1;
2684 int64_t mdat_size = 0;
2686 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2689 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2690 int64_t pos = avio_tell(s->pb);
2692 AVIOContext *moov_buf;
2696 for (i = 0; i < mov->nb_streams; i++)
2697 if (!mov->tracks[i].entry)
2699 /* Don't write the initial moov unless all tracks have data */
2700 if (i < mov->nb_streams)
2703 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
2705 mov_write_moov_tag(moov_buf, mov, s);
2706 buf_size = avio_close_dyn_buf(moov_buf, &buf);
2708 for (i = 0; i < mov->nb_streams; i++)
2709 mov->tracks[i].data_offset = pos + buf_size + 8;
2711 mov_write_moov_tag(s->pb, mov, s);
2713 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2714 mov->mdat_buf = NULL;
2715 avio_wb32(s->pb, buf_size + 8);
2716 ffio_wfourcc(s->pb, "mdat");
2717 avio_write(s->pb, buf, buf_size);
2722 for (i = 0; i < mov->nb_streams; i++) {
2723 if (mov->tracks[i].entry)
2724 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2725 mov->tracks[i].track_duration -
2726 mov->tracks[i].cluster[0].dts;
2727 mov->tracks[i].entry = 0;
2733 for (i = 0; i < mov->nb_streams; i++) {
2734 MOVTrack *track = &mov->tracks[i];
2735 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2736 track->data_offset = 0;
2738 track->data_offset = mdat_size;
2739 if (!track->mdat_buf)
2741 mdat_size += avio_tell(track->mdat_buf);
2742 if (first_track < 0)
2749 for (i = 0; i < mov->nb_streams; i++) {
2750 MOVTrack *track = &mov->tracks[i];
2751 int buf_size, write_moof = 1, moof_tracks = -1;
2753 int64_t duration = 0;
2756 duration = track->start_dts + track->track_duration -
2757 track->cluster[0].dts;
2758 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2759 if (!track->mdat_buf)
2761 mdat_size = avio_tell(track->mdat_buf);
2764 write_moof = i == first_track;
2768 MOVFragmentInfo *info;
2770 track->nb_frag_info++;
2771 if (track->nb_frag_info >= track->frag_info_capacity) {
2772 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
2773 if (av_reallocp_array(&track->frag_info,
2775 sizeof(*track->frag_info)))
2776 return AVERROR(ENOMEM);
2777 track->frag_info_capacity = new_capacity;
2779 info = &track->frag_info[track->nb_frag_info - 1];
2780 info->offset = avio_tell(s->pb);
2781 info->time = mov->tracks[i].frag_start;
2782 info->duration = duration;
2783 mov_write_tfrf_tags(s->pb, mov, track);
2785 mov_write_moof_tag(s->pb, mov, moof_tracks);
2786 info->tfrf_offset = track->tfrf_offset;
2789 avio_wb32(s->pb, mdat_size + 8);
2790 ffio_wfourcc(s->pb, "mdat");
2794 track->frag_start += duration;
2796 if (!track->mdat_buf)
2798 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2799 track->mdat_buf = NULL;
2801 avio_write(s->pb, buf, buf_size);
2811 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2813 MOVMuxContext *mov = s->priv_data;
2814 AVIOContext *pb = s->pb;
2815 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2816 AVCodecContext *enc = trk->enc;
2817 unsigned int samples_in_chunk = 0;
2818 int size = pkt->size;
2819 uint8_t *reformatted_data = NULL;
2821 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2823 if (mov->fragments > 0) {
2824 if (!trk->mdat_buf) {
2825 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2830 if (!mov->mdat_buf) {
2831 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2838 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
2839 /* We must find out how many AMR blocks there are in one packet */
2840 static uint16_t packed_size[16] =
2841 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
2844 while (len < size && samples_in_chunk < 100) {
2845 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2848 if (samples_in_chunk > 1) {
2849 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2852 } else if (trk->sample_size)
2853 samples_in_chunk = size / trk->sample_size;
2855 samples_in_chunk = 1;
2857 /* copy extradata if it exists */
2858 if (trk->vos_len == 0 && enc->extradata_size > 0) {
2859 trk->vos_len = enc->extradata_size;
2860 trk->vos_data = av_malloc(trk->vos_len);
2861 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
2864 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
2865 /* from x264 or from bytestream h264 */
2866 /* nal reformating needed */
2867 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2868 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2870 avio_write(pb, reformatted_data, size);
2872 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2875 avio_write(pb, pkt->data, size);
2878 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
2879 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
2880 /* copy frame to create needed atoms */
2881 trk->vos_len = size;
2882 trk->vos_data = av_malloc(size);
2884 return AVERROR(ENOMEM);
2885 memcpy(trk->vos_data, pkt->data, size);
2888 if (trk->entry >= trk->cluster_capacity) {
2889 unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
2890 if (av_reallocp_array(&trk->cluster, new_capacity,
2891 sizeof(*trk->cluster)))
2892 return AVERROR(ENOMEM);
2893 trk->cluster_capacity = new_capacity;
2896 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2897 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
2898 trk->cluster[trk->entry].size = size;
2899 trk->cluster[trk->entry].entries = samples_in_chunk;
2900 trk->cluster[trk->entry].dts = pkt->dts;
2901 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
2902 /* First packet of a new fragment. We already wrote the duration
2903 * of the last packet of the previous fragment based on track_duration,
2904 * which might not exactly match our dts. Therefore adjust the dts
2905 * of this packet to be what the previous packets duration implies. */
2906 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
2908 if (trk->start_dts == AV_NOPTS_VALUE)
2909 trk->start_dts = pkt->dts;
2910 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
2912 if (pkt->pts == AV_NOPTS_VALUE) {
2913 av_log(s, AV_LOG_WARNING, "pts has no value\n");
2914 pkt->pts = pkt->dts;
2916 if (pkt->dts != pkt->pts)
2917 trk->flags |= MOV_TRACK_CTTS;
2918 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2919 trk->cluster[trk->entry].flags = 0;
2920 if (enc->codec_id == AV_CODEC_ID_VC1) {
2921 mov_parse_vc1_frame(pkt, trk, mov->fragments);
2922 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
2923 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
2924 trk->entry > 0) { // force sync sample for the first key frame
2925 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2926 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2927 trk->flags |= MOV_TRACK_STPS;
2929 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2931 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2932 trk->has_keyframes++;
2935 trk->sample_count += samples_in_chunk;
2936 mov->mdat_size += size;
2940 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2941 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
2942 reformatted_data, size);
2943 av_free(reformatted_data);
2947 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2950 mov_flush_fragment(s);
2953 MOVMuxContext *mov = s->priv_data;
2954 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2955 AVCodecContext *enc = trk->enc;
2956 int64_t frag_duration = 0;
2957 int size = pkt->size;
2960 return 0; /* Discard 0 sized packets */
2963 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
2964 s->streams[pkt->stream_index]->time_base,
2966 if ((mov->max_fragment_duration &&
2967 frag_duration >= mov->max_fragment_duration) ||
2968 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
2969 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
2970 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
2971 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
2972 if (frag_duration >= mov->min_fragment_duration)
2973 mov_flush_fragment(s);
2976 return ff_mov_write_packet(s, pkt);
2980 // QuickTime chapters involve an additional text track with the chapter names
2981 // as samples, and a tref pointing from the other tracks to the chapter one.
2982 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2984 MOVMuxContext *mov = s->priv_data;
2985 MOVTrack *track = &mov->tracks[tracknum];
2986 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2989 track->mode = mov->mode;
2990 track->tag = MKTAG('t','e','x','t');
2991 track->timescale = MOV_TIMESCALE;
2992 track->enc = avcodec_alloc_context3(NULL);
2993 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2995 for (i = 0; i < s->nb_chapters; i++) {
2996 AVChapter *c = s->chapters[i];
2997 AVDictionaryEntry *t;
2999 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3000 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3001 pkt.duration = end - pkt.dts;
3003 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3004 len = strlen(t->value);
3006 pkt.data = av_malloc(pkt.size);
3007 AV_WB16(pkt.data, len);
3008 memcpy(pkt.data + 2, t->value, len);
3009 ff_mov_write_packet(s, &pkt);
3010 av_freep(&pkt.data);
3015 static int mov_write_header(AVFormatContext *s)
3017 AVIOContext *pb = s->pb;
3018 MOVMuxContext *mov = s->priv_data;
3019 AVDictionaryEntry *t;
3020 int i, hint_track = 0;
3022 /* Set the FRAGMENT flag if any of the fragmentation methods are
3024 if (mov->max_fragment_duration || mov->max_fragment_size ||
3025 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3026 FF_MOV_FLAG_FRAG_KEYFRAME |
3027 FF_MOV_FLAG_FRAG_CUSTOM))
3028 mov->flags |= FF_MOV_FLAG_FRAGMENT;
3030 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3031 * is enabled, we don't support non-seekable output at all. */
3032 if (!s->pb->seekable &&
3033 ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
3034 !(s->oformat && !strcmp(s->oformat->name, "ismv")))
3035 || mov->ism_lookahead)) {
3036 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3040 /* Default mode == MP4 */
3041 mov->mode = MODE_MP4;
3043 if (s->oformat != NULL) {
3044 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3045 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3046 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3047 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3048 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3049 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3051 mov_write_ftyp_tag(pb,s);
3052 if (mov->mode == MODE_PSP) {
3053 if (s->nb_streams != 2) {
3054 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3057 mov_write_uuidprof_tag(pb, s);
3061 mov->nb_streams = s->nb_streams;
3062 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
3063 mov->chapter_track = mov->nb_streams++;
3065 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3066 /* Add hint tracks for each audio and video stream */
3067 hint_track = mov->nb_streams;
3068 for (i = 0; i < s->nb_streams; i++) {
3069 AVStream *st = s->streams[i];
3070 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3071 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3077 mov->tracks = av_mallocz(mov->nb_streams * sizeof(*mov->tracks));
3079 return AVERROR(ENOMEM);
3081 for (i = 0; i < s->nb_streams; i++) {
3082 AVStream *st= s->streams[i];
3083 MOVTrack *track= &mov->tracks[i];
3084 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3086 track->enc = st->codec;
3087 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3088 if (track->language < 0)
3089 track->language = 0;
3090 track->mode = mov->mode;
3091 track->tag = mov_find_codec_tag(s, track);
3093 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
3094 "codec not currently supported in container\n", i);
3097 /* If hinting of this track is enabled by a later hint track,
3098 * this is updated. */
3099 track->hint_track = -1;
3100 track->start_dts = AV_NOPTS_VALUE;
3101 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3102 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3103 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3104 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3105 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3106 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3109 track->height = track->tag >> 24 == 'n' ? 486 : 576;
3111 track->timescale = st->codec->time_base.den;
3112 if (track->mode == MODE_MOV && track->timescale > 100000)
3113 av_log(s, AV_LOG_WARNING,
3114 "WARNING codec timebase is very high. If duration is too long,\n"
3115 "file may not be playable by quicktime. Specify a shorter timebase\n"
3116 "or choose different container.\n");
3117 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3118 track->timescale = st->codec->sample_rate;
3119 /* set sample_size for PCM and ADPCM */
3120 if (av_get_bits_per_sample(st->codec->codec_id) ||
3121 st->codec->codec_id == AV_CODEC_ID_ILBC) {
3122 if (!st->codec->block_align) {
3123 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
3126 track->sample_size = st->codec->block_align;
3128 /* set audio_vbr for compressed audio */
3129 if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
3130 track->audio_vbr = 1;
3132 if (track->mode != MODE_MOV &&
3133 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3134 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3135 i, track->enc->sample_rate);
3138 } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3139 track->timescale = st->codec->time_base.den;
3140 } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
3141 track->timescale = st->codec->time_base.den;
3144 track->height = st->codec->height;
3145 /* The ism specific timescale isn't mandatory, but is assumed by
3146 * some tools, such as mp4split. */
3147 if (mov->mode == MODE_ISM)
3148 track->timescale = 10000000;
3150 avpriv_set_pts_info(st, 64, 1, track->timescale);
3152 /* copy extradata if it exists */
3153 if (st->codec->extradata_size) {
3154 track->vos_len = st->codec->extradata_size;
3155 track->vos_data = av_malloc(track->vos_len);
3156 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3160 if (mov->mode == MODE_ISM) {
3161 /* If no fragmentation options have been set, set a default. */
3162 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3163 FF_MOV_FLAG_FRAG_CUSTOM)) &&
3164 !mov->max_fragment_duration && !mov->max_fragment_size)
3165 mov->max_fragment_duration = 5000000;
3166 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
3167 FF_MOV_FLAG_FRAGMENT;
3170 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
3171 mov_write_mdat_tag(pb, mov);
3173 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3174 mov->time = ff_iso8601_to_unix_time(t->value);
3176 mov->time += 0x7C25B080; // 1970 based -> 1904 based
3178 if (mov->chapter_track)
3179 mov_create_chapter_track(s, mov->chapter_track);
3181 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3182 /* Initialize the hint tracks for each audio and video stream */
3183 for (i = 0; i < s->nb_streams; i++) {
3184 AVStream *st = s->streams[i];
3185 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3186 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3187 ff_mov_init_hinting(s, hint_track, i);
3195 if (mov->flags & FF_MOV_FLAG_ISML)
3196 mov_write_isml_manifest(pb, mov);
3198 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3199 mov_write_moov_tag(pb, mov, s);
3205 av_freep(&mov->tracks);
3209 static int mov_write_trailer(AVFormatContext *s)
3211 MOVMuxContext *mov = s->priv_data;
3212 AVIOContext *pb = s->pb;
3216 int64_t moov_pos = avio_tell(pb);
3218 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3219 /* Write size of mdat tag */
3220 if (mov->mdat_size + 8 <= UINT32_MAX) {
3221 avio_seek(pb, mov->mdat_pos, SEEK_SET);
3222 avio_wb32(pb, mov->mdat_size + 8);
3224 /* overwrite 'wide' placeholder atom */
3225 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
3226 /* special value: real atom size will be 64 bit value after
3229 ffio_wfourcc(pb, "mdat");
3230 avio_wb64(pb, mov->mdat_size + 16);
3232 avio_seek(pb, moov_pos, SEEK_SET);
3234 mov_write_moov_tag(pb, mov, s);
3236 mov_flush_fragment(s);
3237 mov_write_mfra_tag(pb, mov);
3240 if (mov->chapter_track)
3241 av_freep(&mov->tracks[mov->chapter_track].enc);
3243 for (i = 0; i < mov->nb_streams; i++) {
3244 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3245 ff_mov_close_hinting(&mov->tracks[i]);
3246 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
3247 mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
3248 int64_t off = avio_tell(pb);
3250 if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
3251 avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
3252 avio_write(pb, buf, 7);
3253 avio_seek(pb, off, SEEK_SET);
3256 av_freep(&mov->tracks[i].cluster);
3257 av_freep(&mov->tracks[i].frag_info);
3259 if (mov->tracks[i].vos_len)
3260 av_free(mov->tracks[i].vos_data);
3263 av_freep(&mov->tracks);
3268 #if CONFIG_MOV_MUXER
3270 AVOutputFormat ff_mov_muxer = {
3272 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3273 .extensions = "mov",
3274 .priv_data_size = sizeof(MOVMuxContext),
3275 .audio_codec = AV_CODEC_ID_AAC,
3276 .video_codec = CONFIG_LIBX264_ENCODER ?
3277 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3278 .write_header = mov_write_header,
3279 .write_packet = mov_write_packet,
3280 .write_trailer = mov_write_trailer,
3281 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
3282 .codec_tag = (const AVCodecTag* const []){
3283 ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
3285 .priv_class = &mov_muxer_class,
3288 #if CONFIG_TGP_MUXER
3290 AVOutputFormat ff_tgp_muxer = {
3292 .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
3293 .extensions = "3gp",
3294 .priv_data_size = sizeof(MOVMuxContext),
3295 .audio_codec = AV_CODEC_ID_AMR_NB,
3296 .video_codec = AV_CODEC_ID_H263,
3297 .write_header = mov_write_header,
3298 .write_packet = mov_write_packet,
3299 .write_trailer = mov_write_trailer,
3300 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
3301 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3302 .priv_class = &tgp_muxer_class,
3305 #if CONFIG_MP4_MUXER
3307 AVOutputFormat ff_mp4_muxer = {
3309 .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
3310 .mime_type = "application/mp4",
3311 .extensions = "mp4",
3312 .priv_data_size = sizeof(MOVMuxContext),
3313 .audio_codec = AV_CODEC_ID_AAC,
3314 .video_codec = CONFIG_LIBX264_ENCODER ?
3315 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3316 .write_header = mov_write_header,
3317 .write_packet = mov_write_packet,
3318 .write_trailer = mov_write_trailer,
3319 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
3320 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3321 .priv_class = &mp4_muxer_class,
3324 #if CONFIG_PSP_MUXER
3326 AVOutputFormat ff_psp_muxer = {
3328 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
3329 .extensions = "mp4,psp",
3330 .priv_data_size = sizeof(MOVMuxContext),
3331 .audio_codec = AV_CODEC_ID_AAC,
3332 .video_codec = CONFIG_LIBX264_ENCODER ?
3333 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3334 .write_header = mov_write_header,
3335 .write_packet = mov_write_packet,
3336 .write_trailer = mov_write_trailer,
3337 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
3338 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3339 .priv_class = &psp_muxer_class,
3342 #if CONFIG_TG2_MUXER
3344 AVOutputFormat ff_tg2_muxer = {
3346 .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
3347 .extensions = "3g2",
3348 .priv_data_size = sizeof(MOVMuxContext),
3349 .audio_codec = AV_CODEC_ID_AMR_NB,
3350 .video_codec = AV_CODEC_ID_H263,
3351 .write_header = mov_write_header,
3352 .write_packet = mov_write_packet,
3353 .write_trailer = mov_write_trailer,
3354 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
3355 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3356 .priv_class = &tg2_muxer_class,
3359 #if CONFIG_IPOD_MUXER
3361 AVOutputFormat ff_ipod_muxer = {
3363 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
3364 .mime_type = "application/mp4",
3365 .extensions = "m4v,m4a",
3366 .priv_data_size = sizeof(MOVMuxContext),
3367 .audio_codec = AV_CODEC_ID_AAC,
3368 .video_codec = AV_CODEC_ID_H264,
3369 .write_header = mov_write_header,
3370 .write_packet = mov_write_packet,
3371 .write_trailer = mov_write_trailer,
3372 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
3373 .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
3374 .priv_class = &ipod_muxer_class,
3377 #if CONFIG_ISMV_MUXER
3379 AVOutputFormat ff_ismv_muxer = {
3381 .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
3382 .mime_type = "application/mp4",
3383 .extensions = "ismv,isma",
3384 .priv_data_size = sizeof(MOVMuxContext),
3385 .audio_codec = AV_CODEC_ID_AAC,
3386 .video_codec = AV_CODEC_ID_H264,
3387 .write_header = mov_write_header,
3388 .write_packet = mov_write_packet,
3389 .write_trailer = mov_write_trailer,
3390 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
3391 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3392 .priv_class = &ismv_muxer_class,