3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "avio_internal.h"
31 #include "libavcodec/get_bits.h"
32 #include "libavcodec/put_bits.h"
33 #include "libavcodec/vc1.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/intfloat.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/dict.h"
46 static const AVOption options[] = {
47 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
48 { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
49 { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
50 { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
51 { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
52 { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
53 { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
54 { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
55 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
56 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
57 { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
58 { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
59 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
60 { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
61 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
62 { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
66 #define MOV_CLASS(flavor)\
67 static const AVClass flavor ## _muxer_class = {\
68 .class_name = #flavor " muxer",\
69 .item_name = av_default_item_name,\
71 .version = LIBAVUTIL_VERSION_INT,\
74 //FIXME support 64 bit variant with wide placeholders
75 static int64_t update_size(AVIOContext *pb, int64_t pos)
77 int64_t curpos = avio_tell(pb);
78 avio_seek(pb, pos, SEEK_SET);
79 avio_wb32(pb, curpos - pos); /* rewrite size */
80 avio_seek(pb, curpos, SEEK_SET);
85 /* Chunk offset atom */
86 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
89 int mode64 = 0; // use 32 bit size variant if possible
90 int64_t pos = avio_tell(pb);
91 avio_wb32(pb, 0); /* size */
92 if (pos > UINT32_MAX) {
94 ffio_wfourcc(pb, "co64");
96 ffio_wfourcc(pb, "stco");
97 avio_wb32(pb, 0); /* version & flags */
98 avio_wb32(pb, track->chunkCount); /* entry count */
99 for (i=0; i<track->entry; i++) {
100 if(!track->cluster[i].chunkNum)
103 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
105 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
107 return update_size(pb, pos);
110 /* Sample size atom */
111 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
114 int i, j, entries = 0, tst = -1, oldtst = -1;
116 int64_t pos = avio_tell(pb);
117 avio_wb32(pb, 0); /* size */
118 ffio_wfourcc(pb, "stsz");
119 avio_wb32(pb, 0); /* version & flags */
121 for (i=0; i<track->entry; i++) {
122 tst = track->cluster[i].size/track->cluster[i].entries;
123 if(oldtst != -1 && tst != oldtst) {
127 entries += track->cluster[i].entries;
129 if (equalChunks && track->entry) {
130 int sSize = track->entry ? track->cluster[0].size/track->cluster[0].entries : 0;
131 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
132 avio_wb32(pb, sSize); // sample size
133 avio_wb32(pb, entries); // sample count
136 avio_wb32(pb, 0); // sample size
137 avio_wb32(pb, entries); // sample count
138 for (i=0; i<track->entry; i++) {
139 for (j=0; j<track->cluster[i].entries; j++) {
140 avio_wb32(pb, track->cluster[i].size /
141 track->cluster[i].entries);
145 return update_size(pb, pos);
148 /* Sample to chunk atom */
149 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
151 int index = 0, oldval = -1, i;
152 int64_t entryPos, curpos;
154 int64_t pos = avio_tell(pb);
155 avio_wb32(pb, 0); /* size */
156 ffio_wfourcc(pb, "stsc");
157 avio_wb32(pb, 0); // version & flags
158 entryPos = avio_tell(pb);
159 avio_wb32(pb, track->chunkCount); // entry count
160 for (i=0; i<track->entry; i++) {
161 if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum)
163 avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
164 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
165 avio_wb32(pb, 0x1); // sample description index
166 oldval = track->cluster[i].samples_in_chunk;
170 curpos = avio_tell(pb);
171 avio_seek(pb, entryPos, SEEK_SET);
172 avio_wb32(pb, index); // rewrite size
173 avio_seek(pb, curpos, SEEK_SET);
175 return update_size(pb, pos);
178 /* Sync sample atom */
179 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
181 int64_t curpos, entryPos;
183 int64_t pos = avio_tell(pb);
184 avio_wb32(pb, 0); // size
185 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
186 avio_wb32(pb, 0); // version & flags
187 entryPos = avio_tell(pb);
188 avio_wb32(pb, track->entry); // entry count
189 for (i=0; i<track->entry; i++) {
190 if (track->cluster[i].flags & flag) {
195 curpos = avio_tell(pb);
196 avio_seek(pb, entryPos, SEEK_SET);
197 avio_wb32(pb, index); // rewrite size
198 avio_seek(pb, curpos, SEEK_SET);
199 return update_size(pb, pos);
202 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
204 avio_wb32(pb, 0x11); /* size */
205 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
206 else ffio_wfourcc(pb, "damr");
207 ffio_wfourcc(pb, "FFMP");
208 avio_w8(pb, 0); /* decoder version */
210 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
211 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
212 avio_w8(pb, 0x01); /* Frames per sample */
216 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
221 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
223 if (track->vos_len < 7)
227 ffio_wfourcc(pb, "dac3");
229 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
230 fscod = get_bits(&gbc, 2);
231 frmsizecod = get_bits(&gbc, 6);
232 bsid = get_bits(&gbc, 5);
233 bsmod = get_bits(&gbc, 3);
234 acmod = get_bits(&gbc, 3);
236 skip_bits(&gbc, 2); // dsurmod
238 if ((acmod & 1) && acmod != 1)
239 skip_bits(&gbc, 2); // cmixlev
241 skip_bits(&gbc, 2); // surmixlev
243 lfeon = get_bits1(&gbc);
245 init_put_bits(&pbc, buf, sizeof(buf));
246 put_bits(&pbc, 2, fscod);
247 put_bits(&pbc, 5, bsid);
248 put_bits(&pbc, 3, bsmod);
249 put_bits(&pbc, 3, acmod);
250 put_bits(&pbc, 1, lfeon);
251 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
252 put_bits(&pbc, 5, 0); // reserved
254 flush_put_bits(&pbc);
255 avio_write(pb, buf, sizeof(buf));
261 * This function writes extradata "as is".
262 * Extradata must be formatted like a valid atom (with size and tag).
264 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
266 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
267 return track->enc->extradata_size;
270 static int mov_write_enda_tag(AVIOContext *pb)
273 ffio_wfourcc(pb, "enda");
274 avio_wb16(pb, 1); /* little endian */
278 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
283 avio_w8(pb, (size>>(7*i)) | 0x80);
284 avio_w8(pb, size & 0x7F);
287 static unsigned compute_avg_bitrate(MOVTrack *track)
291 if (!track->track_duration)
293 for (i = 0; i < track->entry; i++)
294 size += track->cluster[i].size;
295 return size * 8 * track->timescale / track->track_duration;
298 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
300 int64_t pos = avio_tell(pb);
301 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
302 unsigned avg_bitrate;
304 avio_wb32(pb, 0); // size
305 ffio_wfourcc(pb, "esds");
306 avio_wb32(pb, 0); // Version
309 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
310 avio_wb16(pb, track->track_id);
311 avio_w8(pb, 0x00); // flags (= no flags)
313 // DecoderConfig descriptor
314 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
316 // Object type indication
317 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
318 track->enc->codec_id == AV_CODEC_ID_MP3) &&
319 track->enc->sample_rate > 24000)
320 avio_w8(pb, 0x6B); // 11172-3
322 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
324 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
325 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
326 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
327 avio_w8(pb, 0x15); // flags (= Audiostream)
329 avio_w8(pb, 0x11); // flags (= Visualstream)
331 avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
332 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
334 avg_bitrate = compute_avg_bitrate(track);
335 // maxbitrate (FIXME should be max rate in any 1 sec window)
336 avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
337 avio_wb32(pb, avg_bitrate);
339 if (track->vos_len) {
340 // DecoderSpecific info descriptor
341 put_descr(pb, 0x05, track->vos_len);
342 avio_write(pb, track->vos_data, track->vos_len);
346 put_descr(pb, 0x06, 1);
348 return update_size(pb, pos);
351 static int mov_pcm_le_gt16(enum AVCodecID codec_id)
353 return codec_id == AV_CODEC_ID_PCM_S24LE ||
354 codec_id == AV_CODEC_ID_PCM_S32LE ||
355 codec_id == AV_CODEC_ID_PCM_F32LE ||
356 codec_id == AV_CODEC_ID_PCM_F64LE;
359 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
361 int64_t pos = avio_tell(pb);
363 avio_wl32(pb, track->tag); // store it byteswapped
364 track->enc->codec_tag = av_bswap16(track->tag >> 16);
365 ff_put_wav_header(pb, track->enc);
366 return update_size(pb, pos);
369 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
371 int64_t pos = avio_tell(pb);
373 ffio_wfourcc(pb, "wfex");
374 ff_put_wav_header(pb, track->enc);
375 return update_size(pb, pos);
378 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
380 uint32_t layout_tag, bitmap;
381 int64_t pos = avio_tell(pb);
383 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
384 track->enc->channel_layout,
387 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
388 "lack of channel information\n");
392 avio_wb32(pb, 0); // Size
393 ffio_wfourcc(pb, "chan"); // Type
394 avio_w8(pb, 0); // Version
395 avio_wb24(pb, 0); // Flags
396 avio_wb32(pb, layout_tag); // mChannelLayoutTag
397 avio_wb32(pb, bitmap); // mChannelBitmap
398 avio_wb32(pb, 0); // mNumberChannelDescriptions
400 return update_size(pb, pos);
403 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
405 int64_t pos = avio_tell(pb);
407 avio_wb32(pb, 0); /* size */
408 ffio_wfourcc(pb, "wave");
410 if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
411 avio_wb32(pb, 12); /* size */
412 ffio_wfourcc(pb, "frma");
413 avio_wl32(pb, track->tag);
416 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
417 /* useless atom needed by mplayer, ipod, not needed by quicktime */
418 avio_wb32(pb, 12); /* size */
419 ffio_wfourcc(pb, "mp4a");
421 mov_write_esds_tag(pb, track);
422 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
423 mov_write_enda_tag(pb);
424 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
425 mov_write_amr_tag(pb, track);
426 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
427 mov_write_ac3_tag(pb, track);
428 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
429 track->enc->codec_id == AV_CODEC_ID_QDM2) {
430 mov_write_extradata_tag(pb, track);
431 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
432 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
433 mov_write_ms_tag(pb, track);
436 avio_wb32(pb, 8); /* size */
437 avio_wb32(pb, 0); /* null tag */
439 return update_size(pb, pos);
442 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
445 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
446 int unescaped_size, seq_found = 0;
447 int level = 0, interlace = 0;
448 int packet_seq = track->vc1_info.packet_seq;
449 int packet_entry = track->vc1_info.packet_entry;
450 int slices = track->vc1_info.slices;
453 if (track->start_dts == AV_NOPTS_VALUE) {
454 /* No packets written yet, vc1_info isn't authoritative yet. */
455 /* Assume inline sequence and entry headers. This will be
456 * overwritten at the end if the file is seekable. */
457 packet_seq = packet_entry = 1;
460 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
462 return AVERROR(ENOMEM);
463 start = find_next_marker(track->vos_data, end);
464 for (next = start; next < end; start = next) {
467 next = find_next_marker(start + 4, end);
468 size = next - start - 4;
471 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
472 init_get_bits(&gb, unescaped, 8 * unescaped_size);
473 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
474 int profile = get_bits(&gb, 2);
475 if (profile != PROFILE_ADVANCED) {
477 return AVERROR(ENOSYS);
480 level = get_bits(&gb, 3);
481 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
483 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
484 skip_bits(&gb, 1); /* broadcast */
485 interlace = get_bits1(&gb);
486 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
491 return AVERROR(ENOSYS);
494 init_put_bits(&pbc, buf, 7);
495 /* VC1DecSpecStruc */
496 put_bits(&pbc, 4, 12); /* profile - advanced */
497 put_bits(&pbc, 3, level);
498 put_bits(&pbc, 1, 0); /* reserved */
499 /* VC1AdvDecSpecStruc */
500 put_bits(&pbc, 3, level);
501 put_bits(&pbc, 1, 0); /* cbr */
502 put_bits(&pbc, 6, 0); /* reserved */
503 put_bits(&pbc, 1, !interlace); /* no interlace */
504 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
505 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
506 put_bits(&pbc, 1, !slices); /* no slice code */
507 put_bits(&pbc, 1, 0); /* no bframe */
508 put_bits(&pbc, 1, 0); /* reserved */
509 put_bits32(&pbc, track->enc->time_base.den); /* framerate */
510 flush_put_bits(&pbc);
517 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
519 uint8_t buf[7] = { 0 };
522 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
525 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
526 ffio_wfourcc(pb, "dvc1");
527 track->vc1_info.struct_offset = avio_tell(pb);
528 avio_write(pb, buf, sizeof(buf));
529 avio_write(pb, track->vos_data, track->vos_len);
534 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
536 avio_wb32(pb, track->vos_len + 8);
537 ffio_wfourcc(pb, "glbl");
538 avio_write(pb, track->vos_data, track->vos_len);
539 return 8 + track->vos_len;
543 * Compute flags for 'lpcm' tag.
544 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
546 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
549 case AV_CODEC_ID_PCM_F32BE:
550 case AV_CODEC_ID_PCM_F64BE:
552 case AV_CODEC_ID_PCM_F32LE:
553 case AV_CODEC_ID_PCM_F64LE:
555 case AV_CODEC_ID_PCM_U8:
557 case AV_CODEC_ID_PCM_S16BE:
558 case AV_CODEC_ID_PCM_S24BE:
559 case AV_CODEC_ID_PCM_S32BE:
561 case AV_CODEC_ID_PCM_S8:
562 case AV_CODEC_ID_PCM_S16LE:
563 case AV_CODEC_ID_PCM_S24LE:
564 case AV_CODEC_ID_PCM_S32LE:
571 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
575 if (cluster_idx >= track->entry)
578 if (cluster_idx + 1 == track->entry)
579 next_dts = track->track_duration + track->start_dts;
581 next_dts = track->cluster[cluster_idx + 1].dts;
583 return next_dts - track->cluster[cluster_idx].dts;
586 static int get_samples_per_packet(MOVTrack *track)
588 int i, first_duration;
590 // return track->enc->frame_size;
592 /* use 1 for raw PCM */
593 if (!track->audio_vbr)
596 /* check to see if duration is constant for all clusters */
599 first_duration = get_cluster_duration(track, 0);
600 for (i = 1; i < track->entry; i++) {
601 if (get_cluster_duration(track, i) != first_duration)
604 return first_duration;
607 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
609 int64_t pos = avio_tell(pb);
611 uint32_t tag = track->tag;
613 if (track->mode == MODE_MOV) {
614 if (track->timescale > UINT16_MAX) {
615 if (mov_get_lpcm_flags(track->enc->codec_id))
616 tag = AV_RL32("lpcm");
618 } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
619 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
620 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
621 track->enc->codec_id == AV_CODEC_ID_QDM2) {
626 avio_wb32(pb, 0); /* size */
627 avio_wl32(pb, tag); // store it byteswapped
628 avio_wb32(pb, 0); /* Reserved */
629 avio_wb16(pb, 0); /* Reserved */
630 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
632 /* SoundDescription */
633 avio_wb16(pb, version); /* Version */
634 avio_wb16(pb, 0); /* Revision level */
635 avio_wb32(pb, 0); /* Reserved */
640 avio_wb16(pb, 0xfffe);
642 avio_wb32(pb, 0x00010000);
644 avio_wb64(pb, av_double2int(track->enc->sample_rate));
645 avio_wb32(pb, track->enc->channels);
646 avio_wb32(pb, 0x7F000000);
647 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
648 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
649 avio_wb32(pb, track->sample_size);
650 avio_wb32(pb, get_samples_per_packet(track));
652 if (track->mode == MODE_MOV) {
653 avio_wb16(pb, track->enc->channels);
654 if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
655 track->enc->codec_id == AV_CODEC_ID_PCM_S8)
656 avio_wb16(pb, 8); /* bits per sample */
659 avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
660 } else { /* reserved for mp4/3gp */
666 avio_wb16(pb, 0); /* packet size (= 0) */
667 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
668 track->enc->sample_rate : 0);
669 avio_wb16(pb, 0); /* Reserved */
672 if(version == 1) { /* SoundDescription V1 extended info */
673 avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
674 avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
675 avio_wb32(pb, track->sample_size); /* Bytes per frame */
676 avio_wb32(pb, 2); /* Bytes per sample */
679 if(track->mode == MODE_MOV &&
680 (track->enc->codec_id == AV_CODEC_ID_AAC ||
681 track->enc->codec_id == AV_CODEC_ID_AC3 ||
682 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
683 track->enc->codec_id == AV_CODEC_ID_ALAC ||
684 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
685 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
686 track->enc->codec_id == AV_CODEC_ID_QDM2 ||
687 (mov_pcm_le_gt16(track->enc->codec_id) && version==1)))
688 mov_write_wave_tag(pb, track);
689 else if(track->tag == MKTAG('m','p','4','a'))
690 mov_write_esds_tag(pb, track);
691 else if(track->enc->codec_id == AV_CODEC_ID_AMR_NB)
692 mov_write_amr_tag(pb, track);
693 else if(track->enc->codec_id == AV_CODEC_ID_AC3)
694 mov_write_ac3_tag(pb, track);
695 else if(track->enc->codec_id == AV_CODEC_ID_ALAC)
696 mov_write_extradata_tag(pb, track);
697 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
698 mov_write_wfex_tag(pb, track);
699 else if (track->vos_len > 0)
700 mov_write_glbl_tag(pb, track);
702 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
703 mov_write_chan_tag(pb, track);
705 return update_size(pb, pos);
708 static int mov_write_d263_tag(AVIOContext *pb)
710 avio_wb32(pb, 0xf); /* size */
711 ffio_wfourcc(pb, "d263");
712 ffio_wfourcc(pb, "FFMP");
713 avio_w8(pb, 0); /* decoder version */
714 /* FIXME use AVCodecContext level/profile, when encoder will set values */
715 avio_w8(pb, 0xa); /* level */
716 avio_w8(pb, 0); /* profile */
720 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
722 int64_t pos = avio_tell(pb);
725 ffio_wfourcc(pb, "avcC");
726 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
727 return update_size(pb, pos);
730 /* also used by all avid codecs (dv, imx, meridien) and their variants */
731 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
734 avio_wb32(pb, 24); /* size */
735 ffio_wfourcc(pb, "ACLR");
736 ffio_wfourcc(pb, "ACLR");
737 ffio_wfourcc(pb, "0001");
738 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
739 avio_wb32(pb, 0); /* unknown */
741 avio_wb32(pb, 24); /* size */
742 ffio_wfourcc(pb, "APRG");
743 ffio_wfourcc(pb, "APRG");
744 ffio_wfourcc(pb, "0001");
745 avio_wb32(pb, 1); /* unknown */
746 avio_wb32(pb, 0); /* unknown */
748 avio_wb32(pb, 120); /* size */
749 ffio_wfourcc(pb, "ARES");
750 ffio_wfourcc(pb, "ARES");
751 ffio_wfourcc(pb, "0001");
752 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
753 avio_wb32(pb, track->enc->width);
754 /* values below are based on samples created with quicktime and avid codecs */
755 if (track->vos_data[5] & 2) { // interlaced
756 avio_wb32(pb, track->enc->height/2);
757 avio_wb32(pb, 2); /* unknown */
758 avio_wb32(pb, 0); /* unknown */
759 avio_wb32(pb, 4); /* unknown */
761 avio_wb32(pb, track->enc->height);
762 avio_wb32(pb, 1); /* unknown */
763 avio_wb32(pb, 0); /* unknown */
764 if (track->enc->height == 1080)
765 avio_wb32(pb, 5); /* unknown */
767 avio_wb32(pb, 6); /* unknown */
770 for (i = 0; i < 10; i++)
773 /* extra padding for stsd needed */
778 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
780 int tag = track->enc->codec_tag;
782 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
785 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
786 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
787 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
788 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
789 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
790 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
791 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
796 static const AVCodecTag codec_ipod_tags[] = {
797 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
798 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
799 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
800 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
801 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
802 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
803 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
804 { AV_CODEC_ID_NONE, 0 },
807 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
809 int tag = track->enc->codec_tag;
811 // keep original tag for subs, ipod supports both formats
812 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
813 (tag == MKTAG('t','x','3','g') ||
814 tag == MKTAG('t','e','x','t'))))
815 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
817 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
818 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
819 "Quicktime/Ipod might not play the file\n");
824 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
828 if (track->enc->width == 720) /* SD */
829 if (track->enc->height == 480) /* NTSC */
830 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
831 else tag = MKTAG('d','v','c',' ');
832 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
833 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
834 else tag = MKTAG('d','v','p','p');
835 else if (track->enc->height == 720) /* HD 720 line */
836 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
837 else tag = MKTAG('d','v','h','p');
838 else if (track->enc->height == 1080) /* HD 1080 line */
839 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
840 else tag = MKTAG('d','v','h','6');
842 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
849 static const struct {
850 enum PixelFormat pix_fmt;
853 } mov_pix_fmt_tags[] = {
854 { PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
855 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
856 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
857 { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
858 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
859 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
860 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
861 { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
862 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
863 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
864 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
865 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
866 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
867 { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
868 { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
871 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
873 int tag = track->enc->codec_tag;
876 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
877 if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag && track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
878 tag = mov_pix_fmt_tags[i].tag;
879 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
887 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
889 int tag = track->enc->codec_tag;
891 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
892 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
893 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
894 track->enc->codec_id == AV_CODEC_ID_H263 ||
895 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
896 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
897 tag = mov_get_dv_codec_tag(s, track);
898 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
899 tag = mov_get_rawvideo_codec_tag(s, track);
900 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
901 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
902 if (!tag) { // if no mac fcc found, try with Microsoft tags
903 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
905 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
906 "the file may be unplayable!\n");
908 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
909 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
910 if (!tag) { // if no mac fcc found, try with Microsoft tags
911 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
913 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
914 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
915 "the file may be unplayable!\n");
918 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
919 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
925 static const AVCodecTag codec_3gp_tags[] = {
926 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
927 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
928 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
929 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
930 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
931 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
932 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
933 { AV_CODEC_ID_NONE, 0 },
936 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
940 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
941 tag = mp4_get_codec_tag(s, track);
942 else if (track->mode == MODE_ISM) {
943 tag = mp4_get_codec_tag(s, track);
944 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
945 tag = MKTAG('w', 'm', 'a', ' ');
946 } else if (track->mode == MODE_IPOD)
947 tag = ipod_get_codec_tag(s, track);
948 else if (track->mode & MODE_3GP)
949 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
951 tag = mov_get_codec_tag(s, track);
957 * Needed to make file play in iPods running newest firmware
958 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
960 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
963 ffio_wfourcc(pb, "uuid");
964 avio_wb32(pb, 0x6b6840f2);
965 avio_wb32(pb, 0x5f244fc5);
966 avio_wb32(pb, 0xba39a51b);
967 avio_wb32(pb, 0xcf0323f3);
972 static const uint16_t fiel_data[] = {
973 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
976 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
978 unsigned mov_field_order = 0;
979 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
980 mov_field_order = fiel_data[track->enc->field_order];
984 ffio_wfourcc(pb, "fiel");
985 avio_wb16(pb, mov_field_order);
989 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
991 int64_t pos = avio_tell(pb);
992 avio_wb32(pb, 0); /* size */
993 avio_wl32(pb, track->tag); // store it byteswapped
994 avio_wb32(pb, 0); /* Reserved */
995 avio_wb16(pb, 0); /* Reserved */
996 avio_wb16(pb, 1); /* Data-reference index */
998 if (track->enc->extradata_size)
999 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1001 return update_size(pb, pos);
1004 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1007 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
1008 track->enc->sample_aspect_ratio.den, INT_MAX);
1011 ffio_wfourcc(pb, "pasp");
1012 avio_wb32(pb, sar.num);
1013 avio_wb32(pb, sar.den);
1017 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
1019 int64_t pos = avio_tell(pb);
1020 char compressor_name[32] = { 0 };
1022 avio_wb32(pb, 0); /* size */
1023 avio_wl32(pb, track->tag); // store it byteswapped
1024 avio_wb32(pb, 0); /* Reserved */
1025 avio_wb16(pb, 0); /* Reserved */
1026 avio_wb16(pb, 1); /* Data-reference index */
1028 avio_wb16(pb, 0); /* Codec stream version */
1029 avio_wb16(pb, 0); /* Codec stream revision (=0) */
1030 if (track->mode == MODE_MOV) {
1031 ffio_wfourcc(pb, "FFMP"); /* Vendor */
1032 if(track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1033 avio_wb32(pb, 0); /* Temporal Quality */
1034 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1036 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1037 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1040 avio_wb32(pb, 0); /* Reserved */
1041 avio_wb32(pb, 0); /* Reserved */
1042 avio_wb32(pb, 0); /* Reserved */
1044 avio_wb16(pb, track->enc->width); /* Video width */
1045 avio_wb16(pb, track->height); /* Video height */
1046 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1047 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1048 avio_wb32(pb, 0); /* Data size (= 0) */
1049 avio_wb16(pb, 1); /* Frame count (= 1) */
1051 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1052 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
1053 av_strlcpy(compressor_name,track->enc->codec->name,32);
1054 avio_w8(pb, strlen(compressor_name));
1055 avio_write(pb, compressor_name, 31);
1057 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1058 avio_wb16(pb, track->enc->bits_per_coded_sample);
1060 avio_wb16(pb, 0x18); /* Reserved */
1061 avio_wb16(pb, 0xffff); /* Reserved */
1062 if(track->tag == MKTAG('m','p','4','v'))
1063 mov_write_esds_tag(pb, track);
1064 else if(track->enc->codec_id == AV_CODEC_ID_H263)
1065 mov_write_d263_tag(pb);
1066 else if(track->enc->codec_id == AV_CODEC_ID_AVUI ||
1067 track->enc->codec_id == AV_CODEC_ID_SVQ3) {
1068 mov_write_extradata_tag(pb, track);
1070 } else if(track->enc->codec_id == AV_CODEC_ID_DNXHD)
1071 mov_write_avid_tag(pb, track);
1072 else if(track->enc->codec_id == AV_CODEC_ID_H264) {
1073 mov_write_avcc_tag(pb, track);
1074 if(track->mode == MODE_IPOD)
1075 mov_write_uuid_tag_ipod(pb);
1076 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1077 mov_write_fiel_tag(pb, track);
1078 else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1079 mov_write_dvc1_tag(pb, track);
1080 else if (track->vos_len > 0)
1081 mov_write_glbl_tag(pb, track);
1083 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1084 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1085 mov_write_pasp_tag(pb, track);
1088 return update_size(pb, pos);
1091 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1093 int64_t pos = avio_tell(pb);
1094 int frame_duration = track->enc->time_base.num;
1095 int nb_frames = (track->timescale + frame_duration/2) / frame_duration;
1097 avio_wb32(pb, 0); /* size */
1098 ffio_wfourcc(pb, "tmcd"); /* Data format */
1099 avio_wb32(pb, 0); /* Reserved */
1100 avio_wb32(pb, 1); /* Data reference index */
1101 avio_wb32(pb, 0); /* Flags */
1102 avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
1103 avio_wb32(pb, track->timescale); /* Timescale */
1104 avio_wb32(pb, frame_duration); /* Frame duration */
1105 avio_w8(pb, nb_frames); /* Number of frames */
1106 avio_wb24(pb, 0); /* Reserved */
1107 /* TODO: source reference string */
1108 return update_size(pb, pos);
1111 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1113 int64_t pos = avio_tell(pb);
1114 avio_wb32(pb, 0); /* size */
1115 ffio_wfourcc(pb, "rtp ");
1116 avio_wb32(pb, 0); /* Reserved */
1117 avio_wb16(pb, 0); /* Reserved */
1118 avio_wb16(pb, 1); /* Data-reference index */
1120 avio_wb16(pb, 1); /* Hint track version */
1121 avio_wb16(pb, 1); /* Highest compatible version */
1122 avio_wb32(pb, track->max_packet_size); /* Max packet size */
1124 avio_wb32(pb, 12); /* size */
1125 ffio_wfourcc(pb, "tims");
1126 avio_wb32(pb, track->timescale);
1128 return update_size(pb, pos);
1131 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1133 int64_t pos = avio_tell(pb);
1134 avio_wb32(pb, 0); /* size */
1135 ffio_wfourcc(pb, "stsd");
1136 avio_wb32(pb, 0); /* version & flags */
1137 avio_wb32(pb, 1); /* entry count */
1138 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1139 mov_write_video_tag(pb, track);
1140 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1141 mov_write_audio_tag(pb, track);
1142 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1143 mov_write_subtitle_tag(pb, track);
1144 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1145 mov_write_rtp_tag(pb, track);
1146 else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1147 mov_write_tmcd_tag(pb, track);
1148 return update_size(pb, pos);
1151 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1153 MOVStts *ctts_entries;
1154 uint32_t entries = 0;
1158 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1159 ctts_entries[0].count = 1;
1160 ctts_entries[0].duration = track->cluster[0].cts;
1161 for (i=1; i<track->entry; i++) {
1162 if (track->cluster[i].cts == ctts_entries[entries].duration) {
1163 ctts_entries[entries].count++; /* compress */
1166 ctts_entries[entries].duration = track->cluster[i].cts;
1167 ctts_entries[entries].count = 1;
1170 entries++; /* last one */
1171 atom_size = 16 + (entries * 8);
1172 avio_wb32(pb, atom_size); /* size */
1173 ffio_wfourcc(pb, "ctts");
1174 avio_wb32(pb, 0); /* version & flags */
1175 avio_wb32(pb, entries); /* entry count */
1176 for (i=0; i<entries; i++) {
1177 avio_wb32(pb, ctts_entries[i].count);
1178 avio_wb32(pb, ctts_entries[i].duration);
1180 av_free(ctts_entries);
1184 /* Time to sample atom */
1185 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1187 MOVStts *stts_entries;
1188 uint32_t entries = -1;
1192 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1193 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1194 stts_entries[0].count = track->sample_count;
1195 stts_entries[0].duration = 1;
1198 stts_entries = track->entry ?
1199 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1201 for (i=0; i<track->entry; i++) {
1202 int duration = get_cluster_duration(track, i);
1203 if (i && duration == stts_entries[entries].duration) {
1204 stts_entries[entries].count++; /* compress */
1207 stts_entries[entries].duration = duration;
1208 stts_entries[entries].count = 1;
1211 entries++; /* last one */
1213 atom_size = 16 + (entries * 8);
1214 avio_wb32(pb, atom_size); /* size */
1215 ffio_wfourcc(pb, "stts");
1216 avio_wb32(pb, 0); /* version & flags */
1217 avio_wb32(pb, entries); /* entry count */
1218 for (i=0; i<entries; i++) {
1219 avio_wb32(pb, stts_entries[i].count);
1220 avio_wb32(pb, stts_entries[i].duration);
1222 av_free(stts_entries);
1226 static int mov_write_dref_tag(AVIOContext *pb)
1228 avio_wb32(pb, 28); /* size */
1229 ffio_wfourcc(pb, "dref");
1230 avio_wb32(pb, 0); /* version & flags */
1231 avio_wb32(pb, 1); /* entry count */
1233 avio_wb32(pb, 0xc); /* size */
1234 //FIXME add the alis and rsrc atom
1235 ffio_wfourcc(pb, "url ");
1236 avio_wb32(pb, 1); /* version & flags */
1241 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1243 int64_t pos = avio_tell(pb);
1244 avio_wb32(pb, 0); /* size */
1245 ffio_wfourcc(pb, "stbl");
1246 mov_write_stsd_tag(pb, track);
1247 mov_write_stts_tag(pb, track);
1248 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1249 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1250 track->has_keyframes && track->has_keyframes < track->entry)
1251 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1252 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1253 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1254 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1255 track->flags & MOV_TRACK_CTTS)
1256 mov_write_ctts_tag(pb, track);
1257 mov_write_stsc_tag(pb, track);
1258 mov_write_stsz_tag(pb, track);
1259 mov_write_stco_tag(pb, track);
1260 return update_size(pb, pos);
1263 static int mov_write_dinf_tag(AVIOContext *pb)
1265 int64_t pos = avio_tell(pb);
1266 avio_wb32(pb, 0); /* size */
1267 ffio_wfourcc(pb, "dinf");
1268 mov_write_dref_tag(pb);
1269 return update_size(pb, pos);
1272 static int mov_write_nmhd_tag(AVIOContext *pb)
1275 ffio_wfourcc(pb, "nmhd");
1280 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
1282 int64_t pos = avio_tell(pb);
1283 const char *font = "Lucida Grande";
1284 avio_wb32(pb, 0); /* size */
1285 ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
1286 avio_wb32(pb, 0); /* version & flags */
1287 avio_wb16(pb, 0); /* text font */
1288 avio_wb16(pb, 0); /* text face */
1289 avio_wb16(pb, 12); /* text size */
1290 avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
1291 avio_wb16(pb, 0x0000); /* text color (red) */
1292 avio_wb16(pb, 0x0000); /* text color (green) */
1293 avio_wb16(pb, 0x0000); /* text color (blue) */
1294 avio_wb16(pb, 0xffff); /* background color (red) */
1295 avio_wb16(pb, 0xffff); /* background color (green) */
1296 avio_wb16(pb, 0xffff); /* background color (blue) */
1297 avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
1298 avio_write(pb, font, strlen(font)); /* font name */
1299 return update_size(pb, pos);
1302 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
1304 int64_t pos = avio_tell(pb);
1305 avio_wb32(pb, 0); /* size */
1306 ffio_wfourcc(pb, "gmhd");
1307 avio_wb32(pb, 0x18); /* gmin size */
1308 ffio_wfourcc(pb, "gmin");/* generic media info */
1309 avio_wb32(pb, 0); /* version & flags */
1310 avio_wb16(pb, 0x40); /* graphics mode = */
1311 avio_wb16(pb, 0x8000); /* opColor (r?) */
1312 avio_wb16(pb, 0x8000); /* opColor (g?) */
1313 avio_wb16(pb, 0x8000); /* opColor (b?) */
1314 avio_wb16(pb, 0); /* balance */
1315 avio_wb16(pb, 0); /* reserved */
1318 * This special text atom is required for
1319 * Apple Quicktime chapters. The contents
1320 * don't appear to be documented, so the
1321 * bytes are copied verbatim.
1323 avio_wb32(pb, 0x2C); /* size */
1324 ffio_wfourcc(pb, "text");
1325 avio_wb16(pb, 0x01);
1326 avio_wb32(pb, 0x00);
1327 avio_wb32(pb, 0x00);
1328 avio_wb32(pb, 0x00);
1329 avio_wb32(pb, 0x01);
1330 avio_wb32(pb, 0x00);
1331 avio_wb32(pb, 0x00);
1332 avio_wb32(pb, 0x00);
1333 avio_wb32(pb, 0x00004000);
1334 avio_wb16(pb, 0x0000);
1336 if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1337 int64_t tmcd_pos = avio_tell(pb);
1338 avio_wb32(pb, 0); /* size */
1339 ffio_wfourcc(pb, "tmcd");
1340 mov_write_tcmi_tag(pb, track);
1341 update_size(pb, tmcd_pos);
1343 return update_size(pb, pos);
1346 static int mov_write_smhd_tag(AVIOContext *pb)
1348 avio_wb32(pb, 16); /* size */
1349 ffio_wfourcc(pb, "smhd");
1350 avio_wb32(pb, 0); /* version & flags */
1351 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1352 avio_wb16(pb, 0); /* reserved */
1356 static int mov_write_vmhd_tag(AVIOContext *pb)
1358 avio_wb32(pb, 0x14); /* size (always 0x14) */
1359 ffio_wfourcc(pb, "vmhd");
1360 avio_wb32(pb, 0x01); /* version & flags */
1361 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1365 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1367 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1368 int64_t pos = avio_tell(pb);
1370 if (!track) { /* no media --> data handler */
1373 descr = "DataHandler";
1375 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1376 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1378 descr = "VideoHandler";
1379 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1381 descr = "SoundHandler";
1382 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1383 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1384 else hdlr_type = "text";
1385 descr = "SubtitleHandler";
1386 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1388 descr = "TimeCodeHandler";
1389 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1391 descr = "HintHandler";
1395 descr = "DataHandler";
1399 avio_wb32(pb, 0); /* size */
1400 ffio_wfourcc(pb, "hdlr");
1401 avio_wb32(pb, 0); /* Version & flags */
1402 avio_write(pb, hdlr, 4); /* handler */
1403 ffio_wfourcc(pb, hdlr_type); /* handler type */
1404 avio_wb32(pb ,0); /* reserved */
1405 avio_wb32(pb ,0); /* reserved */
1406 avio_wb32(pb ,0); /* reserved */
1407 if (!track || track->mode == MODE_MOV)
1408 avio_w8(pb, strlen(descr)); /* pascal string */
1409 avio_write(pb, descr, strlen(descr)); /* handler description */
1410 if (track && track->mode != MODE_MOV)
1411 avio_w8(pb, 0); /* c string */
1412 return update_size(pb, pos);
1415 static int mov_write_hmhd_tag(AVIOContext *pb)
1417 /* This atom must be present, but leaving the values at zero
1418 * seems harmless. */
1419 avio_wb32(pb, 28); /* size */
1420 ffio_wfourcc(pb, "hmhd");
1421 avio_wb32(pb, 0); /* version, flags */
1422 avio_wb16(pb, 0); /* maxPDUsize */
1423 avio_wb16(pb, 0); /* avgPDUsize */
1424 avio_wb32(pb, 0); /* maxbitrate */
1425 avio_wb32(pb, 0); /* avgbitrate */
1426 avio_wb32(pb, 0); /* reserved */
1430 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1432 int64_t pos = avio_tell(pb);
1433 avio_wb32(pb, 0); /* size */
1434 ffio_wfourcc(pb, "minf");
1435 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1436 mov_write_vmhd_tag(pb);
1437 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1438 mov_write_smhd_tag(pb);
1439 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1440 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb, track);
1441 else mov_write_nmhd_tag(pb);
1442 } else if (track->tag == MKTAG('t','m','c','d')) {
1443 mov_write_gmhd_tag(pb, track);
1444 } else if (track->tag == MKTAG('r','t','p',' ')) {
1445 mov_write_hmhd_tag(pb);
1447 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1448 mov_write_hdlr_tag(pb, NULL);
1449 mov_write_dinf_tag(pb);
1450 mov_write_stbl_tag(pb, track);
1451 return update_size(pb, pos);
1454 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1456 int version = track->track_duration < INT32_MAX ? 0 : 1;
1458 if (track->mode == MODE_ISM)
1461 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1462 ffio_wfourcc(pb, "mdhd");
1463 avio_w8(pb, version);
1464 avio_wb24(pb, 0); /* flags */
1466 avio_wb64(pb, track->time);
1467 avio_wb64(pb, track->time);
1469 avio_wb32(pb, track->time); /* creation time */
1470 avio_wb32(pb, track->time); /* modification time */
1472 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1474 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1476 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1477 avio_wb16(pb, track->language); /* language */
1478 avio_wb16(pb, 0); /* reserved (quality) */
1480 if(version!=0 && track->mode == MODE_MOV){
1481 av_log(NULL, AV_LOG_ERROR,
1482 "FATAL error, file duration too long for timebase, this file will not be\n"
1483 "playable with quicktime. Choose a different timebase or a different\n"
1484 "container format\n");
1490 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1492 int64_t pos = avio_tell(pb);
1493 avio_wb32(pb, 0); /* size */
1494 ffio_wfourcc(pb, "mdia");
1495 mov_write_mdhd_tag(pb, track);
1496 mov_write_hdlr_tag(pb, track);
1497 mov_write_minf_tag(pb, track);
1498 return update_size(pb, pos);
1501 /* transformation matrix
1505 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
1506 int16_t d, int16_t tx, int16_t ty)
1508 avio_wb32(pb, a << 16); /* 16.16 format */
1509 avio_wb32(pb, b << 16); /* 16.16 format */
1510 avio_wb32(pb, 0); /* u in 2.30 format */
1511 avio_wb32(pb, c << 16); /* 16.16 format */
1512 avio_wb32(pb, d << 16); /* 16.16 format */
1513 avio_wb32(pb, 0); /* v in 2.30 format */
1514 avio_wb32(pb, tx << 16); /* 16.16 format */
1515 avio_wb32(pb, ty << 16); /* 16.16 format */
1516 avio_wb32(pb, 1 << 30); /* w in 2.30 format */
1519 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1521 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1522 track->timescale, AV_ROUND_UP);
1523 int version = duration < INT32_MAX ? 0 : 1;
1526 if (track->mode == MODE_ISM)
1529 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1530 ffio_wfourcc(pb, "tkhd");
1531 avio_w8(pb, version);
1532 avio_wb24(pb, 0xf); /* flags (track enabled) */
1534 avio_wb64(pb, track->time);
1535 avio_wb64(pb, track->time);
1537 avio_wb32(pb, track->time); /* creation time */
1538 avio_wb32(pb, track->time); /* modification time */
1540 avio_wb32(pb, track->track_id); /* track-id */
1541 avio_wb32(pb, 0); /* reserved */
1543 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1545 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1547 avio_wb32(pb, 0); /* reserved */
1548 avio_wb32(pb, 0); /* reserved */
1549 avio_wb16(pb, 0); /* layer */
1550 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1551 /* Volume, only for audio */
1552 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1553 avio_wb16(pb, 0x0100);
1556 avio_wb16(pb, 0); /* reserved */
1558 /* Matrix structure */
1559 if (st && st->metadata) {
1560 AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
1561 rotation = (rot && rot->value) ? atoi(rot->value) : 0;
1563 if (rotation == 90) {
1564 write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
1565 } else if (rotation == 180) {
1566 write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
1567 } else if (rotation == 270) {
1568 write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
1570 write_matrix(pb, 1, 0, 0, 1, 0, 0);
1572 /* Track width and height, for visual only */
1573 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1574 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1575 if(track->mode == MODE_MOV) {
1576 avio_wb32(pb, track->enc->width << 16);
1577 avio_wb32(pb, track->height << 16);
1579 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1580 if(!sample_aspect_ratio || track->height != track->enc->height)
1581 sample_aspect_ratio = 1;
1582 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1583 avio_wb32(pb, track->height*0x10000);
1593 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1595 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1596 track->enc->sample_aspect_ratio.den);
1598 int64_t pos = avio_tell(pb);
1600 avio_wb32(pb, 0); /* size */
1601 ffio_wfourcc(pb, "tapt");
1604 ffio_wfourcc(pb, "clef");
1606 avio_wb32(pb, width << 16);
1607 avio_wb32(pb, track->enc->height << 16);
1610 ffio_wfourcc(pb, "enof");
1612 avio_wb32(pb, track->enc->width << 16);
1613 avio_wb32(pb, track->enc->height << 16);
1615 return update_size(pb, pos);
1618 // This box seems important for the psp playback ... without it the movie seems to hang
1619 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1621 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1622 track->timescale, AV_ROUND_UP);
1623 int version = duration < INT32_MAX ? 0 : 1;
1624 int entry_size, entry_count, size;
1625 int64_t delay, start_ct = track->cluster[0].cts;
1626 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1627 track->timescale, AV_ROUND_DOWN);
1628 version |= delay < INT32_MAX ? 0 : 1;
1630 entry_size = (version == 1) ? 20 : 12;
1631 entry_count = 1 + (delay > 0);
1632 size = 24 + entry_count * entry_size;
1634 /* write the atom data */
1635 avio_wb32(pb, size);
1636 ffio_wfourcc(pb, "edts");
1637 avio_wb32(pb, size - 8);
1638 ffio_wfourcc(pb, "elst");
1639 avio_w8(pb, version);
1640 avio_wb24(pb, 0); /* flags */
1642 avio_wb32(pb, entry_count);
1643 if (delay > 0) { /* add an empty edit to delay presentation */
1645 avio_wb64(pb, delay);
1648 avio_wb32(pb, delay);
1651 avio_wb32(pb, 0x00010000);
1656 avio_wb64(pb, duration);
1657 avio_wb64(pb, start_ct);
1659 avio_wb32(pb, duration);
1660 avio_wb32(pb, start_ct);
1662 avio_wb32(pb, 0x00010000);
1666 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1668 avio_wb32(pb, 20); // size
1669 ffio_wfourcc(pb, "tref");
1670 avio_wb32(pb, 12); // size (subatom)
1671 avio_wl32(pb, track->tref_tag);
1672 avio_wb32(pb, track->tref_id);
1676 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1677 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1679 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1680 ffio_wfourcc(pb, "uuid");
1681 ffio_wfourcc(pb, "USMT");
1682 avio_wb32(pb, 0x21d24fce);
1683 avio_wb32(pb, 0xbb88695c);
1684 avio_wb32(pb, 0xfac9c740);
1685 avio_wb32(pb, 0x1c); // another size here!
1686 ffio_wfourcc(pb, "MTDT");
1687 avio_wb32(pb, 0x00010012);
1688 avio_wb32(pb, 0x0a);
1689 avio_wb32(pb, 0x55c40000);
1695 static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
1697 char buf[1000] = "";
1700 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
1701 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1704 avio_wb32(pb, len + 24);
1705 ffio_wfourcc(pb, "udta");
1706 avio_wb32(pb, len + 16);
1707 ffio_wfourcc(pb, "hnti");
1708 avio_wb32(pb, len + 8);
1709 ffio_wfourcc(pb, "sdp ");
1710 avio_write(pb, buf, len);
1714 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1715 MOVTrack *track, AVStream *st)
1717 int64_t pos = avio_tell(pb);
1718 avio_wb32(pb, 0); /* size */
1719 ffio_wfourcc(pb, "trak");
1720 mov_write_tkhd_tag(pb, track, st);
1721 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) // EDTS with fragments is tricky as we dont know the duration when its written
1722 mov_write_edts_tag(pb, track); // PSP Movies and several other cases require edts box
1723 if (track->tref_tag)
1724 mov_write_tref_tag(pb, track);
1725 mov_write_mdia_tag(pb, track);
1726 if (track->mode == MODE_PSP)
1727 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1728 if (track->tag == MKTAG('r','t','p',' '))
1729 mov_write_udta_sdp(pb, track->rtp_ctx, track->track_id);
1730 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1731 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1732 if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
1733 mov_write_tapt_tag(pb, track);
1735 return update_size(pb, pos);
1738 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1740 int i, has_audio = 0, has_video = 0;
1741 int64_t pos = avio_tell(pb);
1742 int audio_profile = mov->iods_audio_profile;
1743 int video_profile = mov->iods_video_profile;
1744 for (i = 0; i < mov->nb_streams; i++) {
1745 if(mov->tracks[i].entry > 0) {
1746 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1747 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1750 if (audio_profile < 0)
1751 audio_profile = 0xFF - has_audio;
1752 if (video_profile < 0)
1753 video_profile = 0xFF - has_video;
1754 avio_wb32(pb, 0x0); /* size */
1755 ffio_wfourcc(pb, "iods");
1756 avio_wb32(pb, 0); /* version & flags */
1757 put_descr(pb, 0x10, 7);
1758 avio_wb16(pb, 0x004f);
1761 avio_w8(pb, audio_profile);
1762 avio_w8(pb, video_profile);
1764 return update_size(pb, pos);
1767 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1769 avio_wb32(pb, 0x20); /* size */
1770 ffio_wfourcc(pb, "trex");
1771 avio_wb32(pb, 0); /* version & flags */
1772 avio_wb32(pb, track->track_id); /* track ID */
1773 avio_wb32(pb, 1); /* default sample description index */
1774 avio_wb32(pb, 0); /* default sample duration */
1775 avio_wb32(pb, 0); /* default sample size */
1776 avio_wb32(pb, 0); /* default sample flags */
1780 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1782 int64_t pos = avio_tell(pb);
1784 avio_wb32(pb, 0x0); /* size */
1785 ffio_wfourcc(pb, "mvex");
1786 for (i = 0; i < mov->nb_streams; i++)
1787 mov_write_trex_tag(pb, &mov->tracks[i]);
1788 return update_size(pb, pos);
1791 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1793 int max_track_id = 1, i;
1794 int64_t max_track_len_temp, max_track_len = 0;
1797 for (i=0; i<mov->nb_streams; i++) {
1798 if(mov->tracks[i].entry > 0) {
1799 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1801 mov->tracks[i].timescale,
1803 if (max_track_len < max_track_len_temp)
1804 max_track_len = max_track_len_temp;
1805 if (max_track_id < mov->tracks[i].track_id)
1806 max_track_id = mov->tracks[i].track_id;
1810 version = max_track_len < UINT32_MAX ? 0 : 1;
1811 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1812 ffio_wfourcc(pb, "mvhd");
1813 avio_w8(pb, version);
1814 avio_wb24(pb, 0); /* flags */
1816 avio_wb64(pb, mov->time);
1817 avio_wb64(pb, mov->time);
1819 avio_wb32(pb, mov->time); /* creation time */
1820 avio_wb32(pb, mov->time); /* modification time */
1822 avio_wb32(pb, MOV_TIMESCALE);
1823 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1825 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1826 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1827 avio_wb16(pb, 0); /* reserved */
1828 avio_wb32(pb, 0); /* reserved */
1829 avio_wb32(pb, 0); /* reserved */
1831 /* Matrix structure */
1832 write_matrix(pb, 1, 0, 0, 1, 0, 0);
1834 avio_wb32(pb, 0); /* reserved (preview time) */
1835 avio_wb32(pb, 0); /* reserved (preview duration) */
1836 avio_wb32(pb, 0); /* reserved (poster time) */
1837 avio_wb32(pb, 0); /* reserved (selection time) */
1838 avio_wb32(pb, 0); /* reserved (selection duration) */
1839 avio_wb32(pb, 0); /* reserved (current time) */
1840 avio_wb32(pb, max_track_id + 1); /* Next track id */
1844 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1847 avio_wb32(pb, 33); /* size */
1848 ffio_wfourcc(pb, "hdlr");
1851 ffio_wfourcc(pb, "mdir");
1852 ffio_wfourcc(pb, "appl");
1859 /* helper function to write a data tag with the specified string as data */
1860 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1863 int size = 16 + strlen(data);
1864 avio_wb32(pb, size); /* size */
1865 ffio_wfourcc(pb, "data");
1868 avio_write(pb, data, strlen(data));
1872 lang = ff_mov_iso639_to_lang("und", 1);
1873 avio_wb16(pb, strlen(data)); /* string length */
1874 avio_wb16(pb, lang);
1875 avio_write(pb, data, strlen(data));
1876 return strlen(data) + 4;
1880 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1882 if (value && value[0]) {
1883 int64_t pos = avio_tell(pb);
1884 avio_wb32(pb, 0); /* size */
1885 ffio_wfourcc(pb, name);
1886 mov_write_string_data_tag(pb, value, lang, long_style);
1887 size = update_size(pb, pos);
1892 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1893 const char *name, const char *tag,
1896 int l, lang = 0, len, len2;
1897 AVDictionaryEntry *t, *t2 = NULL;
1900 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1903 len = strlen(t->key);
1904 snprintf(tag2, sizeof(tag2), "%s-", tag);
1905 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1906 len2 = strlen(t2->key);
1907 if (len2 == len+4 && !strcmp(t->value, t2->value)
1908 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1913 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1916 /* iTunes bpm number */
1917 static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
1919 AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
1920 int size = 0, tmpo = t ? atoi(t->value) : 0;
1923 avio_wb32(pb, size);
1924 ffio_wfourcc(pb, "tmpo");
1925 avio_wb32(pb, size-8); /* size */
1926 ffio_wfourcc(pb, "data");
1927 avio_wb32(pb, 0x15); //type specifier
1929 avio_wb16(pb, tmpo); // data
1934 /* iTunes track number */
1935 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1938 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1939 int size = 0, track = t ? atoi(t->value) : 0;
1941 avio_wb32(pb, 32); /* size */
1942 ffio_wfourcc(pb, "trkn");
1943 avio_wb32(pb, 24); /* size */
1944 ffio_wfourcc(pb, "data");
1945 avio_wb32(pb, 0); // 8 bytes empty
1947 avio_wb16(pb, 0); // empty
1948 avio_wb16(pb, track); // track number
1949 avio_wb16(pb, 0); // total track number
1950 avio_wb16(pb, 0); // empty
1956 /* iTunes meta data list */
1957 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1960 int64_t pos = avio_tell(pb);
1961 avio_wb32(pb, 0); /* size */
1962 ffio_wfourcc(pb, "ilst");
1963 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1964 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1965 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1966 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1967 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1968 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1969 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1970 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1971 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1972 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1973 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1974 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1975 mov_write_string_metadata(s, pb, "desc", "description",1);
1976 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1977 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1978 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1979 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1980 mov_write_trkn_tag(pb, mov, s);
1981 mov_write_tmpo_tag(pb, s);
1982 return update_size(pb, pos);
1985 /* iTunes meta data tag */
1986 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1990 int64_t pos = avio_tell(pb);
1991 avio_wb32(pb, 0); /* size */
1992 ffio_wfourcc(pb, "meta");
1994 mov_write_itunes_hdlr_tag(pb, mov, s);
1995 mov_write_ilst_tag(pb, mov, s);
1996 size = update_size(pb, pos);
2000 static int utf8len(const uint8_t *b)
2005 GET_UTF8(val, *b++, return -1;)
2011 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2015 GET_UTF8(val, *b++, return -1;)
2018 avio_wb16(pb, 0x00);
2022 static uint16_t language_code(const char *str)
2024 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
2027 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
2028 const char *tag, const char *str)
2030 int64_t pos = avio_tell(pb);
2031 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
2032 if (!t || !utf8len(t->value))
2034 avio_wb32(pb, 0); /* size */
2035 ffio_wfourcc(pb, tag); /* type */
2036 avio_wb32(pb, 0); /* version + flags */
2037 if (!strcmp(tag, "yrrc"))
2038 avio_wb16(pb, atoi(t->value));
2040 avio_wb16(pb, language_code("eng")); /* language */
2041 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
2042 if (!strcmp(tag, "albm") &&
2043 (t = av_dict_get(s->metadata, "track", NULL, 0)))
2044 avio_w8(pb, atoi(t->value));
2046 return update_size(pb, pos);
2049 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
2051 int64_t pos = avio_tell(pb);
2052 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
2054 avio_wb32(pb, 0); // size
2055 ffio_wfourcc(pb, "chpl");
2056 avio_wb32(pb, 0x01000000); // version + flags
2057 avio_wb32(pb, 0); // unknown
2058 avio_w8(pb, nb_chapters);
2060 for (i = 0; i < nb_chapters; i++) {
2061 AVChapter *c = s->chapters[i];
2062 AVDictionaryEntry *t;
2063 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
2065 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2066 int len = FFMIN(strlen(t->value), 255);
2068 avio_write(pb, t->value, len);
2072 return update_size(pb, pos);
2075 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
2078 AVIOContext *pb_buf;
2082 for (i = 0; i < s->nb_streams; i++)
2083 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
2087 ret = avio_open_dyn_buf(&pb_buf);
2091 if (mov->mode & MODE_3GP) {
2092 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2093 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2094 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2095 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2096 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2097 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2098 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2099 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2100 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2101 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
2102 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
2103 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
2104 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
2105 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
2106 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
2107 // currently ignored by mov.c
2108 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
2109 // add support for libquicktime, this atom is also actually read by mov.c
2110 mov_write_string_metadata(s, pb_buf, "\251cmt", "comment" , 0);
2111 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
2112 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
2114 /* iTunes meta data */
2115 mov_write_meta_tag(pb_buf, mov, s);
2119 mov_write_chpl_tag(pb_buf, s);
2121 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2122 avio_wb32(pb, size+8);
2123 ffio_wfourcc(pb, "udta");
2124 avio_write(pb, buf, size);
2131 static void mov_write_psp_udta_tag(AVIOContext *pb,
2132 const char *str, const char *lang, int type)
2134 int len = utf8len(str)+1;
2137 avio_wb16(pb, len*2+10); /* size */
2138 avio_wb32(pb, type); /* type */
2139 avio_wb16(pb, language_code(lang)); /* language */
2140 avio_wb16(pb, 0x01); /* ? */
2141 ascii_to_wc(pb, str);
2144 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
2146 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2150 pos = avio_tell(pb);
2151 avio_wb32(pb, 0); /* size placeholder*/
2152 ffio_wfourcc(pb, "uuid");
2153 ffio_wfourcc(pb, "USMT");
2154 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2155 avio_wb32(pb, 0xbb88695c);
2156 avio_wb32(pb, 0xfac9c740);
2158 pos2 = avio_tell(pb);
2159 avio_wb32(pb, 0); /* size placeholder*/
2160 ffio_wfourcc(pb, "MTDT");
2164 avio_wb16(pb, 0x0C); /* size */
2165 avio_wb32(pb, 0x0B); /* type */
2166 avio_wb16(pb, language_code("und")); /* language */
2167 avio_wb16(pb, 0x0); /* ? */
2168 avio_wb16(pb, 0x021C); /* data */
2170 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2171 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2172 // snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
2173 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2175 update_size(pb, pos2);
2176 return update_size(pb, pos);
2182 static void build_chunks(MOVTrack *trk)
2185 MOVIentry *chunk= &trk->cluster[0];
2186 uint64_t chunkSize = chunk->size;
2188 if (trk->chunkCount)
2191 for(i=1; i<trk->entry; i++){
2192 if(chunk->pos + chunkSize == trk->cluster[i].pos &&
2193 chunkSize + trk->cluster[i].size < (1<<20)){
2194 chunkSize += trk->cluster[i].size;
2195 chunk->samples_in_chunk += trk->cluster[i].entries;
2197 trk->cluster[i].chunkNum = chunk->chunkNum+1;
2198 chunk=&trk->cluster[i];
2199 chunkSize = chunk->size;
2205 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2209 int64_t pos = avio_tell(pb);
2210 avio_wb32(pb, 0); /* size placeholder*/
2211 ffio_wfourcc(pb, "moov");
2213 for (i=0; i<mov->nb_streams; i++) {
2214 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2217 mov->tracks[i].time = mov->time;
2218 mov->tracks[i].track_id = i+1;
2220 if (mov->tracks[i].entry)
2221 build_chunks(&mov->tracks[i]);
2224 if (mov->chapter_track)
2225 for (i=0; i<s->nb_streams; i++) {
2226 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2227 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2229 for (i = 0; i < mov->nb_streams; i++) {
2230 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2231 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2232 mov->tracks[i].tref_id =
2233 mov->tracks[mov->tracks[i].src_track].track_id;
2236 for (i = 0; i < mov->nb_streams; i++) {
2237 if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
2238 int src_trk = mov->tracks[i].src_track;
2239 mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
2240 mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
2241 mov->tracks[i].track_duration = mov->tracks[src_trk].track_duration;
2245 mov_write_mvhd_tag(pb, mov);
2246 if (mov->mode != MODE_MOV && !mov->iods_skip)
2247 mov_write_iods_tag(pb, mov);
2248 for (i=0; i<mov->nb_streams; i++) {
2249 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2250 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2253 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2254 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2256 if (mov->mode == MODE_PSP)
2257 mov_write_uuidusmt_tag(pb, s);
2259 mov_write_udta_tag(pb, mov, s);
2261 return update_size(pb, pos);
2264 static void param_write_int(AVIOContext *pb, const char *name, int value)
2266 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2269 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2271 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2274 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2277 len = FFMIN(sizeof(buf)/2 - 1, len);
2278 ff_data_to_hex(buf, value, len, 0);
2280 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2283 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2285 int64_t pos = avio_tell(pb);
2287 const uint8_t uuid[] = {
2288 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2289 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2293 ffio_wfourcc(pb, "uuid");
2294 avio_write(pb, uuid, sizeof(uuid));
2297 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2298 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2299 avio_printf(pb, "<head>\n");
2300 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2302 avio_printf(pb, "</head>\n");
2303 avio_printf(pb, "<body>\n");
2304 avio_printf(pb, "<switch>\n");
2305 for (i = 0; i < mov->nb_streams; i++) {
2306 MOVTrack *track = &mov->tracks[i];
2308 /* track->track_id is initialized in write_moov, and thus isn't known
2310 int track_id = i + 1;
2312 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2314 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2319 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2320 track->enc->bit_rate);
2321 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2322 param_write_int(pb, "trackID", track_id);
2323 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2324 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2326 int size = track->enc->extradata_size;
2327 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2329 param_write_hex(pb, "CodecPrivateData",
2330 ptr ? ptr : track->enc->extradata,
2334 param_write_string(pb, "FourCC", "H264");
2335 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2336 param_write_string(pb, "FourCC", "WVC1");
2337 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2338 track->enc->extradata_size);
2340 param_write_int(pb, "MaxWidth", track->enc->width);
2341 param_write_int(pb, "MaxHeight", track->enc->height);
2342 param_write_int(pb, "DisplayWidth", track->enc->width);
2343 param_write_int(pb, "DisplayHeight", track->enc->height);
2345 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2346 param_write_string(pb, "FourCC", "AACL");
2347 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2348 param_write_string(pb, "FourCC", "WMAP");
2350 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2351 track->enc->extradata_size);
2352 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2353 track->enc->codec_id));
2354 param_write_int(pb, "Channels", track->enc->channels);
2355 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2356 param_write_int(pb, "BitsPerSample", 16);
2357 param_write_int(pb, "PacketSize", track->enc->block_align ?
2358 track->enc->block_align : 4);
2360 avio_printf(pb, "</%s>\n", type);
2362 avio_printf(pb, "</switch>\n");
2363 avio_printf(pb, "</body>\n");
2364 avio_printf(pb, "</smil>\n");
2366 return update_size(pb, pos);
2369 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2372 ffio_wfourcc(pb, "mfhd");
2374 avio_wb32(pb, mov->fragments);
2378 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2379 int64_t moof_offset)
2381 int64_t pos = avio_tell(pb);
2382 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2383 MOV_TFHD_BASE_DATA_OFFSET;
2384 if (!track->entry) {
2385 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2387 flags |= MOV_TFHD_DEFAULT_FLAGS;
2390 /* Don't set a default sample size, the silverlight player refuses
2391 * to play files with that set. Don't set a default sample duration,
2392 * WMP freaks out if it is set. */
2393 if (track->mode == MODE_ISM)
2394 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION);
2396 avio_wb32(pb, 0); /* size placeholder */
2397 ffio_wfourcc(pb, "tfhd");
2398 avio_w8(pb, 0); /* version */
2399 avio_wb24(pb, flags);
2401 avio_wb32(pb, track->track_id); /* track-id */
2402 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2403 avio_wb64(pb, moof_offset);
2404 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2405 track->default_duration = get_cluster_duration(track, 0);
2406 avio_wb32(pb, track->default_duration);
2408 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2409 track->default_size = track->entry ? track->cluster[0].size : 1;
2410 avio_wb32(pb, track->default_size);
2412 track->default_size = -1;
2414 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2415 track->default_sample_flags =
2416 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2417 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2418 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2419 avio_wb32(pb, track->default_sample_flags);
2422 return update_size(pb, pos);
2425 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2427 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2428 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2431 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2433 int64_t pos = avio_tell(pb);
2434 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2437 for (i = 0; i < track->entry; i++) {
2438 if (get_cluster_duration(track, i) != track->default_duration)
2439 flags |= MOV_TRUN_SAMPLE_DURATION;
2440 if (track->cluster[i].size != track->default_size)
2441 flags |= MOV_TRUN_SAMPLE_SIZE;
2442 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2443 flags |= MOV_TRUN_SAMPLE_FLAGS;
2445 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2446 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2447 if (track->flags & MOV_TRACK_CTTS)
2448 flags |= MOV_TRUN_SAMPLE_CTS;
2450 avio_wb32(pb, 0); /* size placeholder */
2451 ffio_wfourcc(pb, "trun");
2452 avio_w8(pb, 0); /* version */
2453 avio_wb24(pb, flags);
2455 avio_wb32(pb, track->entry); /* sample count */
2456 track->moof_size_offset = avio_tell(pb);
2457 avio_wb32(pb, 0); /* data offset */
2458 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2459 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2461 for (i = 0; i < track->entry; i++) {
2462 if (flags & MOV_TRUN_SAMPLE_DURATION)
2463 avio_wb32(pb, get_cluster_duration(track, i));
2464 if (flags & MOV_TRUN_SAMPLE_SIZE)
2465 avio_wb32(pb, track->cluster[i].size);
2466 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2467 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2468 if (flags & MOV_TRUN_SAMPLE_CTS)
2469 avio_wb32(pb, track->cluster[i].cts);
2472 return update_size(pb, pos);
2475 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2477 int64_t pos = avio_tell(pb);
2478 const uint8_t uuid[] = {
2479 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2480 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2483 avio_wb32(pb, 0); /* size placeholder */
2484 ffio_wfourcc(pb, "uuid");
2485 avio_write(pb, uuid, sizeof(uuid));
2488 avio_wb64(pb, track->frag_start);
2489 avio_wb64(pb, track->start_dts + track->track_duration -
2490 track->cluster[0].dts);
2492 return update_size(pb, pos);
2495 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2496 MOVTrack *track, int entry)
2498 int n = track->nb_frag_info - 1 - entry, i;
2499 int size = 8 + 16 + 4 + 1 + 16*n;
2500 const uint8_t uuid[] = {
2501 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2502 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2508 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2509 avio_wb32(pb, size);
2510 ffio_wfourcc(pb, "uuid");
2511 avio_write(pb, uuid, sizeof(uuid));
2515 for (i = 0; i < n; i++) {
2516 int index = entry + 1 + i;
2517 avio_wb64(pb, track->frag_info[index].time);
2518 avio_wb64(pb, track->frag_info[index].duration);
2520 if (n < mov->ism_lookahead) {
2521 int free_size = 16*(mov->ism_lookahead - n);
2522 avio_wb32(pb, free_size);
2523 ffio_wfourcc(pb, "free");
2524 for (i = 0; i < free_size - 8; i++)
2531 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2534 int64_t pos = avio_tell(pb);
2536 for (i = 0; i < mov->ism_lookahead; i++) {
2537 /* Update the tfrf tag for the last ism_lookahead fragments,
2538 * nb_frag_info - 1 is the next fragment to be written. */
2539 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2541 avio_seek(pb, pos, SEEK_SET);
2545 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2546 MOVTrack *track, int64_t moof_offset)
2548 int64_t pos = avio_tell(pb);
2549 avio_wb32(pb, 0); /* size placeholder */
2550 ffio_wfourcc(pb, "traf");
2552 mov_write_tfhd_tag(pb, track, moof_offset);
2553 mov_write_trun_tag(pb, track);
2554 if (mov->mode == MODE_ISM) {
2555 mov_write_tfxd_tag(pb, track);
2557 if (mov->ism_lookahead) {
2558 int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
2560 track->tfrf_offset = avio_tell(pb);
2561 avio_wb32(pb, 8 + size);
2562 ffio_wfourcc(pb, "free");
2563 for (i = 0; i < size; i++)
2568 return update_size(pb, pos);
2571 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2573 int64_t pos = avio_tell(pb), end;
2576 avio_wb32(pb, 0); /* size placeholder */
2577 ffio_wfourcc(pb, "moof");
2579 mov_write_mfhd_tag(pb, mov);
2580 for (i = 0; i < mov->nb_streams; i++) {
2581 MOVTrack *track = &mov->tracks[i];
2582 if (tracks >= 0 && i != tracks)
2586 mov_write_traf_tag(pb, mov, track, pos);
2589 end = avio_tell(pb);
2590 moof_size = end - pos;
2591 for (i = 0; i < mov->nb_streams; i++) {
2592 MOVTrack *track = &mov->tracks[i];
2593 if (tracks >= 0 && i != tracks)
2597 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2598 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2600 avio_seek(pb, end, SEEK_SET);
2602 return update_size(pb, pos);
2605 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2607 int64_t pos = avio_tell(pb);
2610 avio_wb32(pb, 0); /* size placeholder */
2611 ffio_wfourcc(pb, "tfra");
2612 avio_w8(pb, 1); /* version */
2615 avio_wb32(pb, track->track_id);
2616 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2617 avio_wb32(pb, track->nb_frag_info);
2618 for (i = 0; i < track->nb_frag_info; i++) {
2619 avio_wb64(pb, track->frag_info[i].time);
2620 avio_wb64(pb, track->frag_info[i].offset);
2621 avio_w8(pb, 1); /* traf number */
2622 avio_w8(pb, 1); /* trun number */
2623 avio_w8(pb, 1); /* sample number */
2626 return update_size(pb, pos);
2629 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2631 int64_t pos = avio_tell(pb);
2634 avio_wb32(pb, 0); /* size placeholder */
2635 ffio_wfourcc(pb, "mfra");
2636 /* An empty mfra atom is enough to indicate to the publishing point that
2637 * the stream has ended. */
2638 if (mov->flags & FF_MOV_FLAG_ISML)
2639 return update_size(pb, pos);
2641 for (i = 0; i < mov->nb_streams; i++) {
2642 MOVTrack *track = &mov->tracks[i];
2643 if (track->nb_frag_info)
2644 mov_write_tfra_tag(pb, track);
2648 ffio_wfourcc(pb, "mfro");
2649 avio_wb32(pb, 0); /* version + flags */
2650 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2652 return update_size(pb, pos);
2655 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2657 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2658 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2660 mov->mdat_pos = avio_tell(pb);
2661 avio_wb32(pb, 0); /* size placeholder*/
2662 ffio_wfourcc(pb, "mdat");
2666 /* TODO: This needs to be more general */
2667 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2669 MOVMuxContext *mov = s->priv_data;
2670 int64_t pos = avio_tell(pb);
2671 int has_h264 = 0, has_video = 0;
2675 for (i = 0; i < s->nb_streams; i++) {
2676 AVStream *st = s->streams[i];
2677 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2679 if (st->codec->codec_id == AV_CODEC_ID_H264)
2683 avio_wb32(pb, 0); /* size */
2684 ffio_wfourcc(pb, "ftyp");
2686 if (mov->mode == MODE_3GP) {
2687 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2688 minor = has_h264 ? 0x100 : 0x200;
2689 } else if (mov->mode & MODE_3G2) {
2690 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2691 minor = has_h264 ? 0x20000 : 0x10000;
2692 }else if (mov->mode == MODE_PSP)
2693 ffio_wfourcc(pb, "MSNV");
2694 else if (mov->mode == MODE_MP4)
2695 ffio_wfourcc(pb, "isom");
2696 else if (mov->mode == MODE_IPOD)
2697 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2698 else if (mov->mode == MODE_ISM)
2699 ffio_wfourcc(pb, "isml");
2701 ffio_wfourcc(pb, "qt ");
2703 avio_wb32(pb, minor);
2705 if(mov->mode == MODE_MOV)
2706 ffio_wfourcc(pb, "qt ");
2707 else if (mov->mode == MODE_ISM) {
2708 ffio_wfourcc(pb, "piff");
2709 ffio_wfourcc(pb, "iso2");
2711 ffio_wfourcc(pb, "isom");
2712 ffio_wfourcc(pb, "iso2");
2714 ffio_wfourcc(pb, "avc1");
2717 if (mov->mode == MODE_3GP)
2718 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2719 else if (mov->mode & MODE_3G2)
2720 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2721 else if (mov->mode == MODE_PSP)
2722 ffio_wfourcc(pb, "MSNV");
2723 else if (mov->mode == MODE_MP4)
2724 ffio_wfourcc(pb, "mp41");
2725 return update_size(pb, pos);
2728 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
2730 AVCodecContext *video_codec = s->streams[0]->codec;
2731 AVCodecContext *audio_codec = s->streams[1]->codec;
2732 int audio_rate = audio_codec->sample_rate;
2733 int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
2734 int audio_kbitrate = audio_codec->bit_rate / 1000;
2735 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2737 avio_wb32(pb, 0x94); /* size */
2738 ffio_wfourcc(pb, "uuid");
2739 ffio_wfourcc(pb, "PROF");
2741 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2742 avio_wb32(pb, 0xbb88695c);
2743 avio_wb32(pb, 0xfac9c740);
2745 avio_wb32(pb, 0x0); /* ? */
2746 avio_wb32(pb, 0x3); /* 3 sections ? */
2748 avio_wb32(pb, 0x14); /* size */
2749 ffio_wfourcc(pb, "FPRF");
2750 avio_wb32(pb, 0x0); /* ? */
2751 avio_wb32(pb, 0x0); /* ? */
2752 avio_wb32(pb, 0x0); /* ? */
2754 avio_wb32(pb, 0x2c); /* size */
2755 ffio_wfourcc(pb, "APRF");/* audio */
2757 avio_wb32(pb, 0x2); /* TrackID */
2758 ffio_wfourcc(pb, "mp4a");
2759 avio_wb32(pb, 0x20f);
2761 avio_wb32(pb, audio_kbitrate);
2762 avio_wb32(pb, audio_kbitrate);
2763 avio_wb32(pb, audio_rate);
2764 avio_wb32(pb, audio_codec->channels);
2766 avio_wb32(pb, 0x34); /* size */
2767 ffio_wfourcc(pb, "VPRF"); /* video */
2769 avio_wb32(pb, 0x1); /* TrackID */
2770 if (video_codec->codec_id == AV_CODEC_ID_H264) {
2771 ffio_wfourcc(pb, "avc1");
2772 avio_wb16(pb, 0x014D);
2773 avio_wb16(pb, 0x0015);
2775 ffio_wfourcc(pb, "mp4v");
2776 avio_wb16(pb, 0x0000);
2777 avio_wb16(pb, 0x0103);
2780 avio_wb32(pb, video_kbitrate);
2781 avio_wb32(pb, video_kbitrate);
2782 avio_wb32(pb, frame_rate);
2783 avio_wb32(pb, frame_rate);
2784 avio_wb16(pb, video_codec->width);
2785 avio_wb16(pb, video_codec->height);
2786 avio_wb32(pb, 0x010001); /* ? */
2789 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2792 int i, closed_gop = 0;
2794 for (i = 0; i < pkt->size - 4; i++) {
2795 c = (c<<8) + pkt->data[i];
2796 if (c == 0x1b8) { // gop
2797 closed_gop = pkt->data[i+4]>>6 & 0x01;
2798 } else if (c == 0x100) { // pic
2799 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2800 if (!temp_ref || closed_gop) // I picture is not reordered
2801 *flags = MOV_SYNC_SAMPLE;
2803 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2810 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2812 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2813 int seq = 0, entry = 0;
2814 int key = pkt->flags & AV_PKT_FLAG_KEY;
2815 start = find_next_marker(pkt->data, end);
2816 for (next = start; next < end; start = next) {
2817 next = find_next_marker(start + 4, end);
2818 switch (AV_RB32(start)) {
2819 case VC1_CODE_SEQHDR:
2822 case VC1_CODE_ENTRYPOINT:
2825 case VC1_CODE_SLICE:
2826 trk->vc1_info.slices = 1;
2830 if (!trk->entry && !fragment) {
2831 /* First packet in first fragment */
2832 trk->vc1_info.first_packet_seq = seq;
2833 trk->vc1_info.first_packet_entry = entry;
2834 } else if ((seq && !trk->vc1_info.packet_seq) ||
2835 (entry && !trk->vc1_info.packet_entry)) {
2837 for (i = 0; i < trk->entry; i++)
2838 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2839 trk->has_keyframes = 0;
2841 trk->vc1_info.packet_seq = 1;
2843 trk->vc1_info.packet_entry = 1;
2845 /* First fragment */
2846 if ((!seq || trk->vc1_info.first_packet_seq) &&
2847 (!entry || trk->vc1_info.first_packet_entry)) {
2848 /* First packet had the same headers as this one, readd the
2849 * sync sample flag. */
2850 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2851 trk->has_keyframes = 1;
2855 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2857 else if (trk->vc1_info.packet_seq)
2859 else if (trk->vc1_info.packet_entry)
2862 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2863 trk->has_keyframes++;
2867 static int mov_flush_fragment(AVFormatContext *s)
2869 MOVMuxContext *mov = s->priv_data;
2870 int i, first_track = -1;
2871 int64_t mdat_size = 0;
2873 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2876 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2877 int64_t pos = avio_tell(s->pb);
2879 AVIOContext *moov_buf;
2883 for (i = 0; i < mov->nb_streams; i++)
2884 if (!mov->tracks[i].entry)
2886 /* Don't write the initial moov unless all tracks have data */
2887 if (i < mov->nb_streams)
2890 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
2892 mov_write_moov_tag(moov_buf, mov, s);
2893 buf_size = avio_close_dyn_buf(moov_buf, &buf);
2895 for (i = 0; i < mov->nb_streams; i++)
2896 mov->tracks[i].data_offset = pos + buf_size + 8;
2898 mov_write_moov_tag(s->pb, mov, s);
2900 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2901 mov->mdat_buf = NULL;
2902 avio_wb32(s->pb, buf_size + 8);
2903 ffio_wfourcc(s->pb, "mdat");
2904 avio_write(s->pb, buf, buf_size);
2909 for (i = 0; i < mov->nb_streams; i++) {
2910 if (mov->tracks[i].entry)
2911 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2912 mov->tracks[i].track_duration -
2913 mov->tracks[i].cluster[0].dts;
2914 mov->tracks[i].entry = 0;
2920 for (i = 0; i < mov->nb_streams; i++) {
2921 MOVTrack *track = &mov->tracks[i];
2922 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2923 track->data_offset = 0;
2925 track->data_offset = mdat_size;
2926 if (!track->mdat_buf)
2928 mdat_size += avio_tell(track->mdat_buf);
2929 if (first_track < 0)
2936 for (i = 0; i < mov->nb_streams; i++) {
2937 MOVTrack *track = &mov->tracks[i];
2938 int buf_size, write_moof = 1, moof_tracks = -1;
2940 int64_t duration = 0;
2943 duration = track->start_dts + track->track_duration -
2944 track->cluster[0].dts;
2945 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2946 if (!track->mdat_buf)
2948 mdat_size = avio_tell(track->mdat_buf);
2951 write_moof = i == first_track;
2955 MOVFragmentInfo *info;
2957 track->nb_frag_info++;
2958 track->frag_info = av_realloc(track->frag_info,
2959 sizeof(*track->frag_info) *
2960 track->nb_frag_info);
2961 info = &track->frag_info[track->nb_frag_info - 1];
2962 info->offset = avio_tell(s->pb);
2963 info->time = mov->tracks[i].frag_start;
2964 info->duration = duration;
2965 mov_write_tfrf_tags(s->pb, mov, track);
2967 mov_write_moof_tag(s->pb, mov, moof_tracks);
2968 info->tfrf_offset = track->tfrf_offset;
2971 avio_wb32(s->pb, mdat_size + 8);
2972 ffio_wfourcc(s->pb, "mdat");
2976 track->frag_start += duration;
2978 if (!track->mdat_buf)
2980 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2981 track->mdat_buf = NULL;
2983 avio_write(s->pb, buf, buf_size);
2993 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2995 MOVMuxContext *mov = s->priv_data;
2996 AVIOContext *pb = s->pb;
2997 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2998 AVCodecContext *enc = trk->enc;
2999 unsigned int samples_in_chunk = 0;
3000 int size= pkt->size;
3001 uint8_t *reformatted_data = NULL;
3003 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3005 if (mov->fragments > 0) {
3006 if (!trk->mdat_buf) {
3007 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
3012 if (!mov->mdat_buf) {
3013 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
3020 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
3021 /* We must find out how many AMR blocks there are in one packet */
3022 static uint16_t packed_size[16] =
3023 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
3026 while (len < size && samples_in_chunk < 100) {
3027 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
3030 if (samples_in_chunk > 1) {
3031 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
3034 } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
3035 enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
3036 samples_in_chunk = enc->frame_size;
3037 } else if (trk->sample_size)
3038 samples_in_chunk = size / trk->sample_size;
3040 samples_in_chunk = 1;
3042 /* copy extradata if it exists */
3043 if (trk->vos_len == 0 && enc->extradata_size > 0) {
3044 trk->vos_len = enc->extradata_size;
3045 trk->vos_data = av_malloc(trk->vos_len);
3046 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
3049 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
3050 /* from x264 or from bytestream h264 */
3051 /* nal reformating needed */
3052 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3053 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
3055 avio_write(pb, reformatted_data, size);
3057 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
3059 } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
3060 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
3061 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
3064 avio_write(pb, pkt->data, size);
3067 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
3068 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
3069 /* copy frame to create needed atoms */
3070 trk->vos_len = size;
3071 trk->vos_data = av_malloc(size);
3073 return AVERROR(ENOMEM);
3074 memcpy(trk->vos_data, pkt->data, size);
3077 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
3078 trk->cluster = av_realloc_f(trk->cluster, sizeof(*trk->cluster), (trk->entry + MOV_INDEX_CLUSTER_SIZE));
3083 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
3084 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
3085 trk->cluster[trk->entry].chunkNum = 0;
3086 trk->cluster[trk->entry].size = size;
3087 trk->cluster[trk->entry].entries = samples_in_chunk;
3088 trk->cluster[trk->entry].dts = pkt->dts;
3089 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
3090 /* First packet of a new fragment. We already wrote the duration
3091 * of the last packet of the previous fragment based on track_duration,
3092 * which might not exactly match our dts. Therefore adjust the dts
3093 * of this packet to be what the previous packets duration implies. */
3094 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
3096 if (trk->start_dts == AV_NOPTS_VALUE)
3097 trk->start_dts = pkt->dts;
3098 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
3099 trk->last_sample_is_subtitle_end = 0;
3101 if (pkt->pts == AV_NOPTS_VALUE) {
3102 av_log(s, AV_LOG_WARNING, "pts has no value\n");
3103 pkt->pts = pkt->dts;
3105 if (pkt->dts != pkt->pts)
3106 trk->flags |= MOV_TRACK_CTTS;
3107 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
3108 trk->cluster[trk->entry].flags = 0;
3109 if (enc->codec_id == AV_CODEC_ID_VC1) {
3110 mov_parse_vc1_frame(pkt, trk, mov->fragments);
3111 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
3112 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
3113 trk->entry > 0) { // force sync sample for the first key frame
3114 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3115 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3116 trk->flags |= MOV_TRACK_STPS;
3118 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3120 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
3121 trk->has_keyframes++;
3124 trk->sample_count += samples_in_chunk;
3125 mov->mdat_size += size;
3129 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
3130 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3131 reformatted_data, size);
3132 av_free(reformatted_data);
3136 static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
3138 MOVMuxContext *mov = s->priv_data;
3139 MOVTrack *trk = &mov->tracks[pkt->stream_index];
3140 AVCodecContext *enc = trk->enc;
3141 int64_t frag_duration = 0;
3142 int size = pkt->size;
3144 if (!pkt->size) return 0; /* Discard 0 sized packets */
3146 if (trk->entry && pkt->stream_index < s->nb_streams)
3147 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3148 s->streams[pkt->stream_index]->time_base,
3150 if ((mov->max_fragment_duration &&
3151 frag_duration >= mov->max_fragment_duration) ||
3152 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3153 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3154 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
3155 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3156 if (frag_duration >= mov->min_fragment_duration)
3157 mov_flush_fragment(s);
3160 return ff_mov_write_packet(s, pkt);
3163 static int mov_write_subtitle_end_packet(AVFormatContext *s,
3167 uint8_t data[2] = {0};
3170 av_init_packet(&end);
3171 end.size = sizeof(data);
3176 end.stream_index = stream_index;
3178 ret = mov_write_single_packet(s, &end);
3179 av_free_packet(&end);
3184 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
3187 mov_flush_fragment(s);
3191 MOVMuxContext *mov = s->priv_data;
3193 if (!pkt->size) return 0; /* Discard 0 sized packets */
3196 * Subtitles require special handling.
3198 * 1) For full complaince, every track must have a sample at
3199 * dts == 0, which is rarely true for subtitles. So, as soon
3200 * as we see any packet with dts > 0, write an empty subtitle
3201 * at dts == 0 for any subtitle track with no samples in it.
3203 * 2) For each subtitle track, check if the current packet's
3204 * dts is past the duration of the last subtitle sample. If
3205 * so, we now need to write an end sample for that subtitle.
3207 * This must be done conditionally to allow for subtitles that
3208 * immediately replace each other, in which case an end sample
3209 * is not needed, and is, in fact, actively harmful.
3211 * 3) See mov_write_trailer for how the final end sample is
3214 for (i = 0; i < mov->nb_streams; i++) {
3215 MOVTrack *trk = &mov->tracks[i];
3218 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
3219 trk->track_duration < pkt->dts &&
3220 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
3221 ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
3222 if (ret < 0) return ret;
3223 trk->last_sample_is_subtitle_end = 1;
3227 return mov_write_single_packet(s, pkt);
3231 // QuickTime chapters involve an additional text track with the chapter names
3232 // as samples, and a tref pointing from the other tracks to the chapter one.
3233 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
3237 MOVMuxContext *mov = s->priv_data;
3238 MOVTrack *track = &mov->tracks[tracknum];
3239 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
3242 track->mode = mov->mode;
3243 track->tag = MKTAG('t','e','x','t');
3244 track->timescale = MOV_TIMESCALE;
3245 track->enc = avcodec_alloc_context3(NULL);
3246 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
3248 if (avio_open_dyn_buf(&pb) >= 0) {
3252 /* Stub header (usually for Quicktime chapter track) */
3254 avio_wb32(pb, 0x01); // displayFlags
3255 avio_w8(pb, 0x00); // horizontal justification
3256 avio_w8(pb, 0x00); // vertical justification
3257 avio_w8(pb, 0x00); // bgColourRed
3258 avio_w8(pb, 0x00); // bgColourGreen
3259 avio_w8(pb, 0x00); // bgColourBlue
3260 avio_w8(pb, 0x00); // bgColourAlpha
3262 avio_wb16(pb, 0x00); // defTextBoxTop
3263 avio_wb16(pb, 0x00); // defTextBoxLeft
3264 avio_wb16(pb, 0x00); // defTextBoxBottom
3265 avio_wb16(pb, 0x00); // defTextBoxRight
3267 avio_wb16(pb, 0x00); // startChar
3268 avio_wb16(pb, 0x00); // endChar
3269 avio_wb16(pb, 0x01); // fontID
3270 avio_w8(pb, 0x00); // fontStyleFlags
3271 avio_w8(pb, 0x00); // fontSize
3272 avio_w8(pb, 0x00); // fgColourRed
3273 avio_w8(pb, 0x00); // fgColourGreen
3274 avio_w8(pb, 0x00); // fgColourBlue
3275 avio_w8(pb, 0x00); // fgColourAlpha
3277 avio_wb32(pb, 0x0D); // box size
3278 ffio_wfourcc(pb, "ftab"); // box atom name
3279 avio_wb16(pb, 0x01); // entry count
3281 avio_wb16(pb, 0x01); // font ID
3282 avio_w8(pb, 0x00); // font name length
3284 if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
3285 track->enc->extradata = buf;
3286 track->enc->extradata_size = size;
3292 for (i = 0; i < s->nb_chapters; i++) {
3293 AVChapter *c = s->chapters[i];
3294 AVDictionaryEntry *t;
3296 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3297 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3298 pkt.duration = end - pkt.dts;
3300 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3301 len = strlen(t->value);
3303 pkt.data = av_malloc(pkt.size);
3304 AV_WB16(pkt.data, len);
3305 memcpy(pkt.data+2, t->value, len);
3306 ff_mov_write_packet(s, &pkt);
3307 av_freep(&pkt.data);
3312 static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
3315 MOVMuxContext *mov = s->priv_data;
3316 MOVTrack *track = &mov->tracks[index];
3317 AVStream *src_st = s->streams[src_index];
3319 AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
3320 AVRational rate = {src_st->codec->time_base.den, src_st->codec->time_base.num};
3322 /* if the codec time base makes no sense, try to fallback on stream frame rate */
3323 if (av_timecode_check_frame_rate(rate) < 0) {
3324 av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
3325 rate.num, rate.den, src_st->avg_frame_rate.num, src_st->avg_frame_rate.den);
3326 rate = src_st->avg_frame_rate;
3329 /* compute the frame number */
3330 ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
3334 /* tmcd track based on video stream */
3335 track->mode = mov->mode;
3336 track->tag = MKTAG('t','m','c','d');
3337 track->src_track = src_index;
3338 track->timescale = src_st->codec->time_base.den;
3339 if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
3340 track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
3342 /* encode context: tmcd data stream */
3343 track->enc = avcodec_alloc_context3(NULL);
3344 track->enc->codec_type = AVMEDIA_TYPE_DATA;
3345 track->enc->codec_tag = track->tag;
3346 track->enc->time_base = src_st->codec->time_base;
3348 /* the tmcd track just contains one packet with the frame number */
3349 pkt.data = av_malloc(pkt.size);
3350 AV_WB32(pkt.data, tc.start);
3351 ret = ff_mov_write_packet(s, &pkt);
3356 static int mov_write_header(AVFormatContext *s)
3358 AVIOContext *pb = s->pb;
3359 MOVMuxContext *mov = s->priv_data;
3360 AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
3361 int i, hint_track = 0, tmcd_track = 0;
3363 /* Set the FRAGMENT flag if any of the fragmentation methods are
3365 if (mov->max_fragment_duration || mov->max_fragment_size ||
3366 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3367 FF_MOV_FLAG_FRAG_KEYFRAME |
3368 FF_MOV_FLAG_FRAG_CUSTOM))
3369 mov->flags |= FF_MOV_FLAG_FRAGMENT;
3371 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3372 * is enabled, we don't support non-seekable output at all. */
3373 if (!s->pb->seekable &&
3374 ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
3375 !(s->oformat && !strcmp(s->oformat->name, "ismv")))
3376 || mov->ism_lookahead)) {
3377 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3381 /* Default mode == MP4 */
3382 mov->mode = MODE_MP4;
3384 if (s->oformat != NULL) {
3385 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3386 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3387 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3388 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3389 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3390 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3392 mov_write_ftyp_tag(pb,s);
3393 if (mov->mode == MODE_PSP) {
3394 if (s->nb_streams != 2) {
3395 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3398 mov_write_uuidprof_tag(pb,s);
3402 mov->nb_streams = s->nb_streams;
3403 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
3404 mov->chapter_track = mov->nb_streams++;
3406 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3407 /* Add hint tracks for each audio and video stream */
3408 hint_track = mov->nb_streams;
3409 for (i = 0; i < s->nb_streams; i++) {
3410 AVStream *st = s->streams[i];
3411 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3412 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3418 if (mov->mode == MODE_MOV) {
3419 tmcd_track = mov->nb_streams;
3421 /* +1 tmcd track for each video stream with a timecode */
3422 for (i = 0; i < s->nb_streams; i++) {
3423 AVStream *st = s->streams[i];
3424 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3425 (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
3426 mov->nb_meta_tmcd++;
3429 /* check if there is already a tmcd track to remux */
3430 if (mov->nb_meta_tmcd) {
3431 for (i = 0; i < s->nb_streams; i++) {
3432 AVStream *st = s->streams[i];
3433 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
3434 av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
3435 "so timecode metadata are now ignored\n");
3436 mov->nb_meta_tmcd = 0;
3441 mov->nb_streams += mov->nb_meta_tmcd;
3444 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
3446 return AVERROR(ENOMEM);
3448 for(i=0; i<s->nb_streams; i++){
3449 AVStream *st= s->streams[i];
3450 MOVTrack *track= &mov->tracks[i];
3451 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3453 track->enc = st->codec;
3454 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3455 if (track->language < 0)
3456 track->language = 0;
3457 track->mode = mov->mode;
3458 track->tag = mov_find_codec_tag(s, track);
3460 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
3461 "codec not currently supported in container\n", i);
3464 /* If hinting of this track is enabled by a later hint track,
3465 * this is updated. */
3466 track->hint_track = -1;
3467 track->start_dts = AV_NOPTS_VALUE;
3468 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
3469 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3470 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3471 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3472 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3473 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3476 track->height = track->tag>>24 == 'n' ? 486 : 576;
3478 track->timescale = st->codec->time_base.den;
3479 if (track->mode == MODE_MOV && track->timescale > 100000)
3480 av_log(s, AV_LOG_WARNING,
3481 "WARNING codec timebase is very high. If duration is too long,\n"
3482 "file may not be playable by quicktime. Specify a shorter timebase\n"
3483 "or choose different container.\n");
3484 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
3485 track->timescale = st->codec->sample_rate;
3486 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
3487 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
3488 track->audio_vbr = 1;
3489 }else if(st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
3490 st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
3491 st->codec->codec_id == AV_CODEC_ID_ILBC){
3492 if (!st->codec->block_align) {
3493 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
3496 track->sample_size = st->codec->block_align;
3497 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
3498 track->audio_vbr = 1;
3500 track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
3502 if (track->mode != MODE_MOV &&
3503 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3504 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3505 i, track->enc->sample_rate);
3508 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
3509 track->timescale = st->codec->time_base.den;
3511 track->timescale = MOV_TIMESCALE;
3514 track->height = st->codec->height;
3515 /* The ism specific timescale isn't mandatory, but is assumed by
3516 * some tools, such as mp4split. */
3517 if (mov->mode == MODE_ISM)
3518 track->timescale = 10000000;
3520 avpriv_set_pts_info(st, 64, 1, track->timescale);
3522 /* copy extradata if it exists */
3523 if (st->codec->extradata_size) {
3524 track->vos_len = st->codec->extradata_size;
3525 track->vos_data = av_malloc(track->vos_len);
3526 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3530 if (mov->mode == MODE_ISM) {
3531 /* If no fragmentation options have been set, set a default. */
3532 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3533 FF_MOV_FLAG_FRAG_CUSTOM)) &&
3534 !mov->max_fragment_duration && !mov->max_fragment_size)
3535 mov->max_fragment_duration = 5000000;
3536 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
3537 FF_MOV_FLAG_FRAGMENT;
3540 if(mov->reserved_moov_size){
3541 mov->reserved_moov_pos= avio_tell(pb);
3542 avio_skip(pb, mov->reserved_moov_size);
3545 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
3546 mov_write_mdat_tag(pb, mov);
3548 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3549 mov->time = ff_iso8601_to_unix_time(t->value);
3551 mov->time += 0x7C25B080; // 1970 based -> 1904 based
3553 if (mov->chapter_track)
3554 mov_create_chapter_track(s, mov->chapter_track);
3556 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3557 /* Initialize the hint tracks for each audio and video stream */
3558 for (i = 0; i < s->nb_streams; i++) {
3559 AVStream *st = s->streams[i];
3560 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3561 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3562 ff_mov_init_hinting(s, hint_track, i);
3568 if (mov->nb_meta_tmcd) {
3569 /* Initialize the tmcd tracks */
3570 for (i = 0; i < s->nb_streams; i++) {
3571 AVStream *st = s->streams[i];
3574 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3576 t = av_dict_get(st->metadata, "timecode", NULL, 0);
3579 if (mov_create_timecode_track(s, tmcd_track, i, t->value) < 0)
3588 if (mov->flags & FF_MOV_FLAG_ISML)
3589 mov_write_isml_manifest(pb, mov);
3591 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3592 mov_write_moov_tag(pb, mov, s);
3598 av_freep(&mov->tracks);
3602 static int mov_write_trailer(AVFormatContext *s)
3604 MOVMuxContext *mov = s->priv_data;
3605 AVIOContext *pb = s->pb;
3611 * Before actually writing the trailer, make sure that there are no
3612 * dangling subtitles, that need a terminating sample.
3614 for (i = 0; i < mov->nb_streams; i++) {
3615 MOVTrack *trk = &mov->tracks[i];
3616 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
3617 !trk->last_sample_is_subtitle_end) {
3618 mov_write_subtitle_end_packet(s, i, trk->track_duration);
3619 trk->last_sample_is_subtitle_end = 1;
3623 moov_pos = avio_tell(pb);
3625 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3626 /* Write size of mdat tag */
3627 if (mov->mdat_size + 8 <= UINT32_MAX) {
3628 avio_seek(pb, mov->mdat_pos, SEEK_SET);
3629 avio_wb32(pb, mov->mdat_size + 8);
3631 /* overwrite 'wide' placeholder atom */
3632 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
3633 /* special value: real atom size will be 64 bit value after
3636 ffio_wfourcc(pb, "mdat");
3637 avio_wb64(pb, mov->mdat_size + 16);
3639 avio_seek(pb, mov->reserved_moov_size ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
3641 mov_write_moov_tag(pb, mov, s);
3642 if(mov->reserved_moov_size){
3643 int64_t size= mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
3645 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
3648 avio_wb32(pb, size);
3649 ffio_wfourcc(pb, "free");
3650 for(i=0; i<size; i++)
3652 avio_seek(pb, moov_pos, SEEK_SET);
3655 mov_flush_fragment(s);
3656 mov_write_mfra_tag(pb, mov);
3659 if (mov->chapter_track)
3660 av_freep(&mov->tracks[mov->chapter_track].enc);
3662 for (i=0; i<mov->nb_streams; i++) {
3663 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3664 ff_mov_close_hinting(&mov->tracks[i]);
3665 else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
3666 av_freep(&mov->tracks[i].enc);
3667 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
3668 mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
3669 int64_t off = avio_tell(pb);
3671 if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
3672 avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
3673 avio_write(pb, buf, 7);
3674 avio_seek(pb, off, SEEK_SET);
3677 av_freep(&mov->tracks[i].cluster);
3678 av_freep(&mov->tracks[i].frag_info);
3680 if (mov->tracks[i].vos_len)
3681 av_free(mov->tracks[i].vos_data);
3687 av_freep(&mov->tracks);
3692 #if CONFIG_MOV_MUXER
3694 AVOutputFormat ff_mov_muxer = {
3696 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3697 .extensions = "mov",
3698 .priv_data_size = sizeof(MOVMuxContext),
3699 .audio_codec = AV_CODEC_ID_AAC,
3700 .video_codec = CONFIG_LIBX264_ENCODER ?
3701 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3702 .write_header = mov_write_header,
3703 .write_packet = mov_write_packet,
3704 .write_trailer = mov_write_trailer,
3705 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3706 .codec_tag = (const AVCodecTag* const []){
3707 ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
3709 .priv_class = &mov_muxer_class,
3712 #if CONFIG_TGP_MUXER
3714 AVOutputFormat ff_tgp_muxer = {
3716 .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
3717 .extensions = "3gp",
3718 .priv_data_size = sizeof(MOVMuxContext),
3719 .audio_codec = AV_CODEC_ID_AMR_NB,
3720 .video_codec = AV_CODEC_ID_H263,
3721 .write_header = mov_write_header,
3722 .write_packet = mov_write_packet,
3723 .write_trailer = mov_write_trailer,
3724 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3725 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3726 .priv_class = &tgp_muxer_class,
3729 #if CONFIG_MP4_MUXER
3731 AVOutputFormat ff_mp4_muxer = {
3733 .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
3734 .mime_type = "application/mp4",
3735 .extensions = "mp4",
3736 .priv_data_size = sizeof(MOVMuxContext),
3737 .audio_codec = AV_CODEC_ID_AAC,
3738 .video_codec = CONFIG_LIBX264_ENCODER ?
3739 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3740 .write_header = mov_write_header,
3741 .write_packet = mov_write_packet,
3742 .write_trailer = mov_write_trailer,
3743 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3744 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3745 .priv_class = &mp4_muxer_class,
3748 #if CONFIG_PSP_MUXER
3750 AVOutputFormat ff_psp_muxer = {
3752 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
3753 .extensions = "mp4,psp",
3754 .priv_data_size = sizeof(MOVMuxContext),
3755 .audio_codec = AV_CODEC_ID_AAC,
3756 .video_codec = CONFIG_LIBX264_ENCODER ?
3757 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3758 .write_header = mov_write_header,
3759 .write_packet = mov_write_packet,
3760 .write_trailer = mov_write_trailer,
3761 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3762 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3763 .priv_class = &psp_muxer_class,
3766 #if CONFIG_TG2_MUXER
3768 AVOutputFormat ff_tg2_muxer = {
3770 .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
3771 .extensions = "3g2",
3772 .priv_data_size = sizeof(MOVMuxContext),
3773 .audio_codec = AV_CODEC_ID_AMR_NB,
3774 .video_codec = AV_CODEC_ID_H263,
3775 .write_header = mov_write_header,
3776 .write_packet = mov_write_packet,
3777 .write_trailer = mov_write_trailer,
3778 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3779 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3780 .priv_class = &tg2_muxer_class,
3783 #if CONFIG_IPOD_MUXER
3785 AVOutputFormat ff_ipod_muxer = {
3787 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
3788 .mime_type = "application/mp4",
3789 .extensions = "m4v,m4a",
3790 .priv_data_size = sizeof(MOVMuxContext),
3791 .audio_codec = AV_CODEC_ID_AAC,
3792 .video_codec = AV_CODEC_ID_H264,
3793 .write_header = mov_write_header,
3794 .write_packet = mov_write_packet,
3795 .write_trailer = mov_write_trailer,
3796 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3797 .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
3798 .priv_class = &ipod_muxer_class,
3801 #if CONFIG_ISMV_MUXER
3803 AVOutputFormat ff_ismv_muxer = {
3805 .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
3806 .mime_type = "application/mp4",
3807 .extensions = "ismv,isma",
3808 .priv_data_size = sizeof(MOVMuxContext),
3809 .audio_codec = AV_CODEC_ID_AAC,
3810 .video_codec = AV_CODEC_ID_H264,
3811 .write_header = mov_write_header,
3812 .write_packet = mov_write_packet,
3813 .write_trailer = mov_write_trailer,
3814 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3815 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3816 .priv_class = &ismv_muxer_class,