Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavformat / movenc.c
blob496158c10af37dd0055eb2067ca6721dab5a39d7
1 /*
2 * MOV, 3GP, MP4 muxer
3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "movenc.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "riff.h"
28 #include "avio.h"
29 #include "isom.h"
30 #include "avc.h"
31 #include "libavcodec/get_bits.h"
32 #include "libavcodec/put_bits.h"
33 #include "libavcodec/vc1.h"
34 #include "internal.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"
40 #include "rtpenc.h"
41 #include "mov_chan.h"
43 #undef NDEBUG
44 #include <assert.h>
46 static const AVOption options[] = {
47 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
48 { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
49 { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
50 { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
51 { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
52 { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
53 { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
54 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
55 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
56 { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
57 { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
58 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
59 { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
60 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
61 { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
62 { NULL },
65 #define MOV_CLASS(flavor)\
66 static const AVClass flavor ## _muxer_class = {\
67 .class_name = #flavor " muxer",\
68 .item_name = av_default_item_name,\
69 .option = options,\
70 .version = LIBAVUTIL_VERSION_INT,\
73 //FIXME support 64 bit variant with wide placeholders
74 static int64_t update_size(AVIOContext *pb, int64_t pos)
76 int64_t curpos = avio_tell(pb);
77 avio_seek(pb, pos, SEEK_SET);
78 avio_wb32(pb, curpos - pos); /* rewrite size */
79 avio_seek(pb, curpos, SEEK_SET);
81 return curpos - pos;
84 /* Chunk offset atom */
85 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
87 int i;
88 int mode64 = 0; // use 32 bit size variant if possible
89 int64_t pos = avio_tell(pb);
90 avio_wb32(pb, 0); /* size */
91 if (pos > UINT32_MAX) {
92 mode64 = 1;
93 ffio_wfourcc(pb, "co64");
94 } else
95 ffio_wfourcc(pb, "stco");
96 avio_wb32(pb, 0); /* version & flags */
97 avio_wb32(pb, track->entry); /* entry count */
98 for (i=0; i<track->entry; i++) {
99 if(mode64 == 1)
100 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
101 else
102 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
104 return update_size(pb, pos);
107 /* Sample size atom */
108 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
110 int equalChunks = 1;
111 int i, j, entries = 0, tst = -1, oldtst = -1;
113 int64_t pos = avio_tell(pb);
114 avio_wb32(pb, 0); /* size */
115 ffio_wfourcc(pb, "stsz");
116 avio_wb32(pb, 0); /* version & flags */
118 for (i=0; i<track->entry; i++) {
119 tst = track->cluster[i].size/track->cluster[i].entries;
120 if(oldtst != -1 && tst != oldtst) {
121 equalChunks = 0;
123 oldtst = tst;
124 entries += track->cluster[i].entries;
126 if (equalChunks && track->entry) {
127 int sSize = track->entry ? track->cluster[0].size/track->cluster[0].entries : 0;
128 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
129 avio_wb32(pb, sSize); // sample size
130 avio_wb32(pb, entries); // sample count
132 else {
133 avio_wb32(pb, 0); // sample size
134 avio_wb32(pb, entries); // sample count
135 for (i=0; i<track->entry; i++) {
136 for (j=0; j<track->cluster[i].entries; j++) {
137 avio_wb32(pb, track->cluster[i].size /
138 track->cluster[i].entries);
142 return update_size(pb, pos);
145 /* Sample to chunk atom */
146 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
148 int index = 0, oldval = -1, i;
149 int64_t entryPos, curpos;
151 int64_t pos = avio_tell(pb);
152 avio_wb32(pb, 0); /* size */
153 ffio_wfourcc(pb, "stsc");
154 avio_wb32(pb, 0); // version & flags
155 entryPos = avio_tell(pb);
156 avio_wb32(pb, track->entry); // entry count
157 for (i=0; i<track->entry; i++) {
158 if (oldval != track->cluster[i].samples_in_chunk)
160 avio_wb32(pb, i+1); // first chunk
161 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
162 avio_wb32(pb, 0x1); // sample description index
163 oldval = track->cluster[i].samples_in_chunk;
164 index++;
167 curpos = avio_tell(pb);
168 avio_seek(pb, entryPos, SEEK_SET);
169 avio_wb32(pb, index); // rewrite size
170 avio_seek(pb, curpos, SEEK_SET);
172 return update_size(pb, pos);
175 /* Sync sample atom */
176 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
178 int64_t curpos, entryPos;
179 int i, index = 0;
180 int64_t pos = avio_tell(pb);
181 avio_wb32(pb, 0); // size
182 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
183 avio_wb32(pb, 0); // version & flags
184 entryPos = avio_tell(pb);
185 avio_wb32(pb, track->entry); // entry count
186 for (i=0; i<track->entry; i++) {
187 if (track->cluster[i].flags & flag) {
188 avio_wb32(pb, i+1);
189 index++;
192 curpos = avio_tell(pb);
193 avio_seek(pb, entryPos, SEEK_SET);
194 avio_wb32(pb, index); // rewrite size
195 avio_seek(pb, curpos, SEEK_SET);
196 return update_size(pb, pos);
199 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
201 avio_wb32(pb, 0x11); /* size */
202 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
203 else ffio_wfourcc(pb, "damr");
204 ffio_wfourcc(pb, "FFMP");
205 avio_w8(pb, 0); /* decoder version */
207 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
208 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
209 avio_w8(pb, 0x01); /* Frames per sample */
210 return 0x11;
213 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
215 GetBitContext gbc;
216 PutBitContext pbc;
217 uint8_t buf[3];
218 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
220 if (track->vos_len < 7)
221 return -1;
223 avio_wb32(pb, 11);
224 ffio_wfourcc(pb, "dac3");
226 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
227 fscod = get_bits(&gbc, 2);
228 frmsizecod = get_bits(&gbc, 6);
229 bsid = get_bits(&gbc, 5);
230 bsmod = get_bits(&gbc, 3);
231 acmod = get_bits(&gbc, 3);
232 if (acmod == 2) {
233 skip_bits(&gbc, 2); // dsurmod
234 } else {
235 if ((acmod & 1) && acmod != 1)
236 skip_bits(&gbc, 2); // cmixlev
237 if (acmod & 4)
238 skip_bits(&gbc, 2); // surmixlev
240 lfeon = get_bits1(&gbc);
242 init_put_bits(&pbc, buf, sizeof(buf));
243 put_bits(&pbc, 2, fscod);
244 put_bits(&pbc, 5, bsid);
245 put_bits(&pbc, 3, bsmod);
246 put_bits(&pbc, 3, acmod);
247 put_bits(&pbc, 1, lfeon);
248 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
249 put_bits(&pbc, 5, 0); // reserved
251 flush_put_bits(&pbc);
252 avio_write(pb, buf, sizeof(buf));
254 return 11;
258 * This function writes extradata "as is".
259 * Extradata must be formatted like a valid atom (with size and tag).
261 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
263 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
264 return track->enc->extradata_size;
267 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
269 int i = 3;
270 avio_w8(pb, tag);
271 for(; i>0; i--)
272 avio_w8(pb, (size>>(7*i)) | 0x80);
273 avio_w8(pb, size & 0x7F);
276 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
278 int64_t pos = avio_tell(pb);
279 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
281 avio_wb32(pb, 0); // size
282 ffio_wfourcc(pb, "esds");
283 avio_wb32(pb, 0); // Version
285 // ES descriptor
286 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
287 avio_wb16(pb, track->track_id);
288 avio_w8(pb, 0x00); // flags (= no flags)
290 // DecoderConfig descriptor
291 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
293 // Object type indication
294 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
295 track->enc->codec_id == AV_CODEC_ID_MP3) &&
296 track->enc->sample_rate > 24000)
297 avio_w8(pb, 0x6B); // 11172-3
298 else
299 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
301 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
302 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
303 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
304 avio_w8(pb, 0x15); // flags (= Audiostream)
305 else
306 avio_w8(pb, 0x11); // flags (= Visualstream)
308 avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
310 avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
311 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
312 avio_wb32(pb, 0); // vbr
313 else
314 avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
316 if (track->vos_len) {
317 // DecoderSpecific info descriptor
318 put_descr(pb, 0x05, track->vos_len);
319 avio_write(pb, track->vos_data, track->vos_len);
322 // SL descriptor
323 put_descr(pb, 0x06, 1);
324 avio_w8(pb, 0x02);
325 return update_size(pb, pos);
328 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
330 int64_t pos = avio_tell(pb);
331 avio_wb32(pb, 0);
332 avio_wl32(pb, track->tag); // store it byteswapped
333 track->enc->codec_tag = av_bswap16(track->tag >> 16);
334 ff_put_wav_header(pb, track->enc);
335 return update_size(pb, pos);
338 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
340 int64_t pos = avio_tell(pb);
341 avio_wb32(pb, 0);
342 ffio_wfourcc(pb, "wfex");
343 ff_put_wav_header(pb, track->enc);
344 return update_size(pb, pos);
347 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
349 uint32_t layout_tag, bitmap;
350 int64_t pos = avio_tell(pb);
352 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
353 track->enc->channel_layout,
354 &bitmap);
355 if (!layout_tag) {
356 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
357 "lack of channel information\n");
358 return 0;
361 avio_wb32(pb, 0); // Size
362 ffio_wfourcc(pb, "chan"); // Type
363 avio_w8(pb, 0); // Version
364 avio_wb24(pb, 0); // Flags
365 avio_wb32(pb, layout_tag); // mChannelLayoutTag
366 avio_wb32(pb, bitmap); // mChannelBitmap
367 avio_wb32(pb, 0); // mNumberChannelDescriptions
369 return update_size(pb, pos);
372 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
374 int64_t pos = avio_tell(pb);
376 avio_wb32(pb, 0); /* size */
377 ffio_wfourcc(pb, "wave");
379 avio_wb32(pb, 12); /* size */
380 ffio_wfourcc(pb, "frma");
381 avio_wl32(pb, track->tag);
383 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
384 /* useless atom needed by mplayer, ipod, not needed by quicktime */
385 avio_wb32(pb, 12); /* size */
386 ffio_wfourcc(pb, "mp4a");
387 avio_wb32(pb, 0);
388 mov_write_esds_tag(pb, track);
389 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
390 mov_write_amr_tag(pb, track);
391 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
392 mov_write_ac3_tag(pb, track);
393 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC) {
394 mov_write_extradata_tag(pb, track);
395 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
396 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
397 mov_write_ms_tag(pb, track);
400 avio_wb32(pb, 8); /* size */
401 avio_wb32(pb, 0); /* null tag */
403 return update_size(pb, pos);
406 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
408 uint8_t *unescaped;
409 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
410 int unescaped_size, seq_found = 0;
411 int level = 0, interlace = 0;
412 int packet_seq = track->vc1_info.packet_seq;
413 int packet_entry = track->vc1_info.packet_entry;
414 int slices = track->vc1_info.slices;
415 PutBitContext pbc;
417 if (track->start_dts == AV_NOPTS_VALUE) {
418 /* No packets written yet, vc1_info isn't authoritative yet. */
419 /* Assume inline sequence and entry headers. This will be
420 * overwritten at the end if the file is seekable. */
421 packet_seq = packet_entry = 1;
424 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
425 if (!unescaped)
426 return AVERROR(ENOMEM);
427 start = find_next_marker(track->vos_data, end);
428 for (next = start; next < end; start = next) {
429 GetBitContext gb;
430 int size;
431 next = find_next_marker(start + 4, end);
432 size = next - start - 4;
433 if (size <= 0)
434 continue;
435 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
436 init_get_bits(&gb, unescaped, 8 * unescaped_size);
437 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
438 int profile = get_bits(&gb, 2);
439 if (profile != PROFILE_ADVANCED) {
440 av_free(unescaped);
441 return AVERROR(ENOSYS);
443 seq_found = 1;
444 level = get_bits(&gb, 3);
445 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
446 * width, height */
447 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
448 skip_bits(&gb, 1); /* broadcast */
449 interlace = get_bits1(&gb);
450 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
453 if (!seq_found) {
454 av_free(unescaped);
455 return AVERROR(ENOSYS);
458 init_put_bits(&pbc, buf, 7);
459 /* VC1DecSpecStruc */
460 put_bits(&pbc, 4, 12); /* profile - advanced */
461 put_bits(&pbc, 3, level);
462 put_bits(&pbc, 1, 0); /* reserved */
463 /* VC1AdvDecSpecStruc */
464 put_bits(&pbc, 3, level);
465 put_bits(&pbc, 1, 0); /* cbr */
466 put_bits(&pbc, 6, 0); /* reserved */
467 put_bits(&pbc, 1, !interlace); /* no interlace */
468 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
469 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
470 put_bits(&pbc, 1, !slices); /* no slice code */
471 put_bits(&pbc, 1, 0); /* no bframe */
472 put_bits(&pbc, 1, 0); /* reserved */
473 put_bits32(&pbc, track->enc->time_base.den); /* framerate */
474 flush_put_bits(&pbc);
476 av_free(unescaped);
478 return 0;
481 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
483 uint8_t buf[7] = { 0 };
484 int ret;
486 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
487 return ret;
489 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
490 ffio_wfourcc(pb, "dvc1");
491 track->vc1_info.struct_offset = avio_tell(pb);
492 avio_write(pb, buf, sizeof(buf));
493 avio_write(pb, track->vos_data, track->vos_len);
495 return 0;
498 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
500 avio_wb32(pb, track->vos_len + 8);
501 ffio_wfourcc(pb, "glbl");
502 avio_write(pb, track->vos_data, track->vos_len);
503 return 8 + track->vos_len;
507 * Compute flags for 'lpcm' tag.
508 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
510 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
512 switch (codec_id) {
513 case AV_CODEC_ID_PCM_F32BE:
514 case AV_CODEC_ID_PCM_F64BE:
515 return 11;
516 case AV_CODEC_ID_PCM_F32LE:
517 case AV_CODEC_ID_PCM_F64LE:
518 return 9;
519 case AV_CODEC_ID_PCM_U8:
520 return 10;
521 case AV_CODEC_ID_PCM_S16BE:
522 case AV_CODEC_ID_PCM_S24BE:
523 case AV_CODEC_ID_PCM_S32BE:
524 return 14;
525 case AV_CODEC_ID_PCM_S8:
526 case AV_CODEC_ID_PCM_S16LE:
527 case AV_CODEC_ID_PCM_S24LE:
528 case AV_CODEC_ID_PCM_S32LE:
529 return 12;
530 default:
531 return 0;
535 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
537 int64_t next_dts;
539 if (cluster_idx >= track->entry)
540 return 0;
542 if (cluster_idx + 1 == track->entry)
543 next_dts = track->track_duration + track->start_dts;
544 else
545 next_dts = track->cluster[cluster_idx + 1].dts;
547 return next_dts - track->cluster[cluster_idx].dts;
550 static int get_samples_per_packet(MOVTrack *track)
552 int i, first_duration;
554 /* use 1 for raw PCM */
555 if (!track->audio_vbr)
556 return 1;
558 /* check to see if duration is constant for all clusters */
559 if (!track->entry)
560 return 0;
561 first_duration = get_cluster_duration(track, 0);
562 for (i = 1; i < track->entry; i++) {
563 if (get_cluster_duration(track, i) != first_duration)
564 return 0;
566 return first_duration;
569 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
571 int64_t pos = avio_tell(pb);
572 int version = 0;
573 uint32_t tag = track->tag;
575 if (track->mode == MODE_MOV) {
576 if (mov_get_lpcm_flags(track->enc->codec_id))
577 tag = AV_RL32("lpcm");
578 version = 2;
581 avio_wb32(pb, 0); /* size */
582 avio_wl32(pb, tag); // store it byteswapped
583 avio_wb32(pb, 0); /* Reserved */
584 avio_wb16(pb, 0); /* Reserved */
585 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
587 /* SoundDescription */
588 avio_wb16(pb, version); /* Version */
589 avio_wb16(pb, 0); /* Revision level */
590 avio_wb32(pb, 0); /* Reserved */
592 if (version == 2) {
593 avio_wb16(pb, 3);
594 avio_wb16(pb, 16);
595 avio_wb16(pb, 0xfffe);
596 avio_wb16(pb, 0);
597 avio_wb32(pb, 0x00010000);
598 avio_wb32(pb, 72);
599 avio_wb64(pb, av_double2int(track->enc->sample_rate));
600 avio_wb32(pb, track->enc->channels);
601 avio_wb32(pb, 0x7F000000);
602 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
603 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
604 avio_wb32(pb, track->sample_size);
605 avio_wb32(pb, get_samples_per_packet(track));
606 } else {
607 /* reserved for mp4/3gp */
608 avio_wb16(pb, 2);
609 avio_wb16(pb, 16);
610 avio_wb16(pb, 0);
612 avio_wb16(pb, 0); /* packet size (= 0) */
613 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
614 track->enc->sample_rate : 0);
615 avio_wb16(pb, 0); /* Reserved */
618 if(track->mode == MODE_MOV &&
619 (track->enc->codec_id == AV_CODEC_ID_AAC ||
620 track->enc->codec_id == AV_CODEC_ID_AC3 ||
621 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
622 track->enc->codec_id == AV_CODEC_ID_ALAC ||
623 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
624 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV))
625 mov_write_wave_tag(pb, track);
626 else if(track->tag == MKTAG('m','p','4','a'))
627 mov_write_esds_tag(pb, track);
628 else if(track->enc->codec_id == AV_CODEC_ID_AMR_NB)
629 mov_write_amr_tag(pb, track);
630 else if(track->enc->codec_id == AV_CODEC_ID_AC3)
631 mov_write_ac3_tag(pb, track);
632 else if(track->enc->codec_id == AV_CODEC_ID_ALAC)
633 mov_write_extradata_tag(pb, track);
634 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
635 mov_write_wfex_tag(pb, track);
636 else if (track->vos_len > 0)
637 mov_write_glbl_tag(pb, track);
639 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
640 mov_write_chan_tag(pb, track);
642 return update_size(pb, pos);
645 static int mov_write_d263_tag(AVIOContext *pb)
647 avio_wb32(pb, 0xf); /* size */
648 ffio_wfourcc(pb, "d263");
649 ffio_wfourcc(pb, "FFMP");
650 avio_w8(pb, 0); /* decoder version */
651 /* FIXME use AVCodecContext level/profile, when encoder will set values */
652 avio_w8(pb, 0xa); /* level */
653 avio_w8(pb, 0); /* profile */
654 return 0xf;
657 /* TODO: No idea about these values */
658 static int mov_write_svq3_tag(AVIOContext *pb)
660 avio_wb32(pb, 0x15);
661 ffio_wfourcc(pb, "SMI ");
662 ffio_wfourcc(pb, "SEQH");
663 avio_wb32(pb, 0x5);
664 avio_wb32(pb, 0xe2c0211d);
665 avio_wb32(pb, 0xc0000000);
666 avio_w8(pb, 0);
667 return 0x15;
670 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
672 int64_t pos = avio_tell(pb);
674 avio_wb32(pb, 0);
675 ffio_wfourcc(pb, "avcC");
676 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
677 return update_size(pb, pos);
680 /* also used by all avid codecs (dv, imx, meridien) and their variants */
681 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
683 int i;
684 avio_wb32(pb, 24); /* size */
685 ffio_wfourcc(pb, "ACLR");
686 ffio_wfourcc(pb, "ACLR");
687 ffio_wfourcc(pb, "0001");
688 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
689 avio_wb32(pb, 0); /* unknown */
691 avio_wb32(pb, 24); /* size */
692 ffio_wfourcc(pb, "APRG");
693 ffio_wfourcc(pb, "APRG");
694 ffio_wfourcc(pb, "0001");
695 avio_wb32(pb, 1); /* unknown */
696 avio_wb32(pb, 0); /* unknown */
698 avio_wb32(pb, 120); /* size */
699 ffio_wfourcc(pb, "ARES");
700 ffio_wfourcc(pb, "ARES");
701 ffio_wfourcc(pb, "0001");
702 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
703 avio_wb32(pb, track->enc->width);
704 /* values below are based on samples created with quicktime and avid codecs */
705 if (track->vos_data[5] & 2) { // interlaced
706 avio_wb32(pb, track->enc->height/2);
707 avio_wb32(pb, 2); /* unknown */
708 avio_wb32(pb, 0); /* unknown */
709 avio_wb32(pb, 4); /* unknown */
710 } else {
711 avio_wb32(pb, track->enc->height);
712 avio_wb32(pb, 1); /* unknown */
713 avio_wb32(pb, 0); /* unknown */
714 if (track->enc->height == 1080)
715 avio_wb32(pb, 5); /* unknown */
716 else
717 avio_wb32(pb, 6); /* unknown */
719 /* padding */
720 for (i = 0; i < 10; i++)
721 avio_wb64(pb, 0);
723 /* extra padding for stsd needed */
724 avio_wb32(pb, 0);
725 return 0;
728 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
730 int tag = track->enc->codec_tag;
732 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
733 return 0;
735 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
736 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
737 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
738 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
739 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
740 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
741 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
743 return tag;
746 static const AVCodecTag codec_ipod_tags[] = {
747 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
748 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
749 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
750 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
751 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
752 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
753 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
754 { AV_CODEC_ID_NONE, 0 },
757 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
759 int tag = track->enc->codec_tag;
761 // keep original tag for subs, ipod supports both formats
762 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
763 (tag == MKTAG('t','x','3','g') ||
764 tag == MKTAG('t','e','x','t'))))
765 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
767 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
768 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
769 "Quicktime/Ipod might not play the file\n");
771 return tag;
774 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
776 int tag;
778 if (track->enc->width == 720) /* SD */
779 if (track->enc->height == 480) /* NTSC */
780 if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
781 else tag = MKTAG('d','v','c',' ');
782 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
783 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
784 else tag = MKTAG('d','v','p','p');
785 else if (track->enc->height == 720) /* HD 720 line */
786 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
787 else tag = MKTAG('d','v','h','p');
788 else if (track->enc->height == 1080) /* HD 1080 line */
789 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
790 else tag = MKTAG('d','v','h','6');
791 else {
792 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
793 return 0;
796 return tag;
799 static const struct {
800 enum AVPixelFormat pix_fmt;
801 uint32_t tag;
802 unsigned bps;
803 } mov_pix_fmt_tags[] = {
804 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
805 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
806 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
807 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
808 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
809 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
810 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
811 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
812 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
813 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
814 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
815 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
816 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
817 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
820 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
822 int tag = track->enc->codec_tag;
823 int i;
825 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
826 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
827 tag = mov_pix_fmt_tags[i].tag;
828 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
829 break;
833 return tag;
836 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
838 int tag = track->enc->codec_tag;
840 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
841 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
842 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
843 track->enc->codec_id == AV_CODEC_ID_H263 ||
844 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
845 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
846 tag = mov_get_dv_codec_tag(s, track);
847 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
848 tag = mov_get_rawvideo_codec_tag(s, track);
849 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
850 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
851 if (!tag) { // if no mac fcc found, try with Microsoft tags
852 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
853 if (tag)
854 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
855 "the file may be unplayable!\n");
857 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
858 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
859 if (!tag) { // if no mac fcc found, try with Microsoft tags
860 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
861 if (ms_tag) {
862 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
863 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
864 "the file may be unplayable!\n");
867 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
868 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
871 return tag;
874 static const AVCodecTag codec_3gp_tags[] = {
875 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
876 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
877 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
878 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
879 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
880 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
881 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
882 { AV_CODEC_ID_NONE, 0 },
885 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
887 int tag;
889 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
890 tag = mp4_get_codec_tag(s, track);
891 else if (track->mode == MODE_ISM) {
892 tag = mp4_get_codec_tag(s, track);
893 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
894 tag = MKTAG('w', 'm', 'a', ' ');
895 } else if (track->mode == MODE_IPOD)
896 tag = ipod_get_codec_tag(s, track);
897 else if (track->mode & MODE_3GP)
898 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
899 else
900 tag = mov_get_codec_tag(s, track);
902 return tag;
905 /** Write uuid atom.
906 * Needed to make file play in iPods running newest firmware
907 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
909 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
911 avio_wb32(pb, 28);
912 ffio_wfourcc(pb, "uuid");
913 avio_wb32(pb, 0x6b6840f2);
914 avio_wb32(pb, 0x5f244fc5);
915 avio_wb32(pb, 0xba39a51b);
916 avio_wb32(pb, 0xcf0323f3);
917 avio_wb32(pb, 0x0);
918 return 28;
921 static const uint16_t fiel_data[] = {
922 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
925 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
927 unsigned mov_field_order = 0;
928 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
929 mov_field_order = fiel_data[track->enc->field_order];
930 else
931 return 0;
932 avio_wb32(pb, 10);
933 ffio_wfourcc(pb, "fiel");
934 avio_wb16(pb, mov_field_order);
935 return 10;
938 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
940 int64_t pos = avio_tell(pb);
941 avio_wb32(pb, 0); /* size */
942 avio_wl32(pb, track->tag); // store it byteswapped
943 avio_wb32(pb, 0); /* Reserved */
944 avio_wb16(pb, 0); /* Reserved */
945 avio_wb16(pb, 1); /* Data-reference index */
947 if (track->enc->extradata_size)
948 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
950 return update_size(pb, pos);
953 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
955 AVRational sar;
956 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
957 track->enc->sample_aspect_ratio.den, INT_MAX);
959 avio_wb32(pb, 16);
960 ffio_wfourcc(pb, "pasp");
961 avio_wb32(pb, sar.num);
962 avio_wb32(pb, sar.den);
963 return 16;
966 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
968 int64_t pos = avio_tell(pb);
969 char compressor_name[32] = { 0 };
971 avio_wb32(pb, 0); /* size */
972 avio_wl32(pb, track->tag); // store it byteswapped
973 avio_wb32(pb, 0); /* Reserved */
974 avio_wb16(pb, 0); /* Reserved */
975 avio_wb16(pb, 1); /* Data-reference index */
977 avio_wb16(pb, 0); /* Codec stream version */
978 avio_wb16(pb, 0); /* Codec stream revision (=0) */
979 if (track->mode == MODE_MOV) {
980 ffio_wfourcc(pb, "FFMP"); /* Vendor */
981 if(track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
982 avio_wb32(pb, 0); /* Temporal Quality */
983 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
984 } else {
985 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
986 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
988 } else {
989 avio_wb32(pb, 0); /* Reserved */
990 avio_wb32(pb, 0); /* Reserved */
991 avio_wb32(pb, 0); /* Reserved */
993 avio_wb16(pb, track->enc->width); /* Video width */
994 avio_wb16(pb, track->height); /* Video height */
995 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
996 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
997 avio_wb32(pb, 0); /* Data size (= 0) */
998 avio_wb16(pb, 1); /* Frame count (= 1) */
1000 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1001 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
1002 av_strlcpy(compressor_name,track->enc->codec->name,32);
1003 avio_w8(pb, strlen(compressor_name));
1004 avio_write(pb, compressor_name, 31);
1006 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1007 avio_wb16(pb, track->enc->bits_per_coded_sample);
1008 else
1009 avio_wb16(pb, 0x18); /* Reserved */
1010 avio_wb16(pb, 0xffff); /* Reserved */
1011 if(track->tag == MKTAG('m','p','4','v'))
1012 mov_write_esds_tag(pb, track);
1013 else if(track->enc->codec_id == AV_CODEC_ID_H263)
1014 mov_write_d263_tag(pb);
1015 else if(track->enc->codec_id == AV_CODEC_ID_SVQ3)
1016 mov_write_svq3_tag(pb);
1017 else if(track->enc->codec_id == AV_CODEC_ID_DNXHD)
1018 mov_write_avid_tag(pb, track);
1019 else if(track->enc->codec_id == AV_CODEC_ID_H264) {
1020 mov_write_avcc_tag(pb, track);
1021 if(track->mode == MODE_IPOD)
1022 mov_write_uuid_tag_ipod(pb);
1023 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1024 mov_write_fiel_tag(pb, track);
1025 else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1026 mov_write_dvc1_tag(pb, track);
1027 else if (track->vos_len > 0)
1028 mov_write_glbl_tag(pb, track);
1030 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1031 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1032 mov_write_pasp_tag(pb, track);
1035 return update_size(pb, pos);
1038 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1040 int64_t pos = avio_tell(pb);
1041 avio_wb32(pb, 0); /* size */
1042 ffio_wfourcc(pb, "rtp ");
1043 avio_wb32(pb, 0); /* Reserved */
1044 avio_wb16(pb, 0); /* Reserved */
1045 avio_wb16(pb, 1); /* Data-reference index */
1047 avio_wb16(pb, 1); /* Hint track version */
1048 avio_wb16(pb, 1); /* Highest compatible version */
1049 avio_wb32(pb, track->max_packet_size); /* Max packet size */
1051 avio_wb32(pb, 12); /* size */
1052 ffio_wfourcc(pb, "tims");
1053 avio_wb32(pb, track->timescale);
1055 return update_size(pb, pos);
1058 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1060 int64_t pos = avio_tell(pb);
1061 avio_wb32(pb, 0); /* size */
1062 ffio_wfourcc(pb, "stsd");
1063 avio_wb32(pb, 0); /* version & flags */
1064 avio_wb32(pb, 1); /* entry count */
1065 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1066 mov_write_video_tag(pb, track);
1067 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1068 mov_write_audio_tag(pb, track);
1069 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1070 mov_write_subtitle_tag(pb, track);
1071 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1072 mov_write_rtp_tag(pb, track);
1073 return update_size(pb, pos);
1076 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1078 MOVStts *ctts_entries;
1079 uint32_t entries = 0;
1080 uint32_t atom_size;
1081 int i;
1083 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1084 ctts_entries[0].count = 1;
1085 ctts_entries[0].duration = track->cluster[0].cts;
1086 for (i=1; i<track->entry; i++) {
1087 if (track->cluster[i].cts == ctts_entries[entries].duration) {
1088 ctts_entries[entries].count++; /* compress */
1089 } else {
1090 entries++;
1091 ctts_entries[entries].duration = track->cluster[i].cts;
1092 ctts_entries[entries].count = 1;
1095 entries++; /* last one */
1096 atom_size = 16 + (entries * 8);
1097 avio_wb32(pb, atom_size); /* size */
1098 ffio_wfourcc(pb, "ctts");
1099 avio_wb32(pb, 0); /* version & flags */
1100 avio_wb32(pb, entries); /* entry count */
1101 for (i=0; i<entries; i++) {
1102 avio_wb32(pb, ctts_entries[i].count);
1103 avio_wb32(pb, ctts_entries[i].duration);
1105 av_free(ctts_entries);
1106 return atom_size;
1109 /* Time to sample atom */
1110 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1112 MOVStts *stts_entries;
1113 uint32_t entries = -1;
1114 uint32_t atom_size;
1115 int i;
1117 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1118 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1119 stts_entries[0].count = track->sample_count;
1120 stts_entries[0].duration = 1;
1121 entries = 1;
1122 } else {
1123 stts_entries = track->entry ?
1124 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1125 NULL;
1126 for (i=0; i<track->entry; i++) {
1127 int duration = get_cluster_duration(track, i);
1128 if (i && duration == stts_entries[entries].duration) {
1129 stts_entries[entries].count++; /* compress */
1130 } else {
1131 entries++;
1132 stts_entries[entries].duration = duration;
1133 stts_entries[entries].count = 1;
1136 entries++; /* last one */
1138 atom_size = 16 + (entries * 8);
1139 avio_wb32(pb, atom_size); /* size */
1140 ffio_wfourcc(pb, "stts");
1141 avio_wb32(pb, 0); /* version & flags */
1142 avio_wb32(pb, entries); /* entry count */
1143 for (i=0; i<entries; i++) {
1144 avio_wb32(pb, stts_entries[i].count);
1145 avio_wb32(pb, stts_entries[i].duration);
1147 av_free(stts_entries);
1148 return atom_size;
1151 static int mov_write_dref_tag(AVIOContext *pb)
1153 avio_wb32(pb, 28); /* size */
1154 ffio_wfourcc(pb, "dref");
1155 avio_wb32(pb, 0); /* version & flags */
1156 avio_wb32(pb, 1); /* entry count */
1158 avio_wb32(pb, 0xc); /* size */
1159 ffio_wfourcc(pb, "url ");
1160 avio_wb32(pb, 1); /* version & flags */
1162 return 28;
1165 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1167 int64_t pos = avio_tell(pb);
1168 avio_wb32(pb, 0); /* size */
1169 ffio_wfourcc(pb, "stbl");
1170 mov_write_stsd_tag(pb, track);
1171 mov_write_stts_tag(pb, track);
1172 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1173 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1174 track->has_keyframes && track->has_keyframes < track->entry)
1175 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1176 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1177 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1178 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1179 track->flags & MOV_TRACK_CTTS)
1180 mov_write_ctts_tag(pb, track);
1181 mov_write_stsc_tag(pb, track);
1182 mov_write_stsz_tag(pb, track);
1183 mov_write_stco_tag(pb, track);
1184 return update_size(pb, pos);
1187 static int mov_write_dinf_tag(AVIOContext *pb)
1189 int64_t pos = avio_tell(pb);
1190 avio_wb32(pb, 0); /* size */
1191 ffio_wfourcc(pb, "dinf");
1192 mov_write_dref_tag(pb);
1193 return update_size(pb, pos);
1196 static int mov_write_nmhd_tag(AVIOContext *pb)
1198 avio_wb32(pb, 12);
1199 ffio_wfourcc(pb, "nmhd");
1200 avio_wb32(pb, 0);
1201 return 12;
1204 static int mov_write_gmhd_tag(AVIOContext *pb)
1206 avio_wb32(pb, 0x20); /* size */
1207 ffio_wfourcc(pb, "gmhd");
1208 avio_wb32(pb, 0x18); /* gmin size */
1209 ffio_wfourcc(pb, "gmin");/* generic media info */
1210 avio_wb32(pb, 0); /* version & flags */
1211 avio_wb16(pb, 0x40); /* graphics mode = */
1212 avio_wb16(pb, 0x8000); /* opColor (r?) */
1213 avio_wb16(pb, 0x8000); /* opColor (g?) */
1214 avio_wb16(pb, 0x8000); /* opColor (b?) */
1215 avio_wb16(pb, 0); /* balance */
1216 avio_wb16(pb, 0); /* reserved */
1217 return 0x20;
1220 static int mov_write_smhd_tag(AVIOContext *pb)
1222 avio_wb32(pb, 16); /* size */
1223 ffio_wfourcc(pb, "smhd");
1224 avio_wb32(pb, 0); /* version & flags */
1225 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1226 avio_wb16(pb, 0); /* reserved */
1227 return 16;
1230 static int mov_write_vmhd_tag(AVIOContext *pb)
1232 avio_wb32(pb, 0x14); /* size (always 0x14) */
1233 ffio_wfourcc(pb, "vmhd");
1234 avio_wb32(pb, 0x01); /* version & flags */
1235 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1236 return 0x14;
1239 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1241 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1242 int64_t pos = avio_tell(pb);
1244 if (!track) { /* no media --> data handler */
1245 hdlr = "dhlr";
1246 hdlr_type = "url ";
1247 descr = "DataHandler";
1248 } else {
1249 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1250 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1251 hdlr_type = "vide";
1252 descr = "VideoHandler";
1253 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1254 hdlr_type = "soun";
1255 descr = "SoundHandler";
1256 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1257 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1258 else hdlr_type = "text";
1259 descr = "SubtitleHandler";
1260 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1261 hdlr_type = "hint";
1262 descr = "HintHandler";
1266 avio_wb32(pb, 0); /* size */
1267 ffio_wfourcc(pb, "hdlr");
1268 avio_wb32(pb, 0); /* Version & flags */
1269 avio_write(pb, hdlr, 4); /* handler */
1270 ffio_wfourcc(pb, hdlr_type); /* handler type */
1271 avio_wb32(pb ,0); /* reserved */
1272 avio_wb32(pb ,0); /* reserved */
1273 avio_wb32(pb ,0); /* reserved */
1274 if (!track || track->mode == MODE_MOV)
1275 avio_w8(pb, strlen(descr)); /* pascal string */
1276 avio_write(pb, descr, strlen(descr)); /* handler description */
1277 if (track && track->mode != MODE_MOV)
1278 avio_w8(pb, 0); /* c string */
1279 return update_size(pb, pos);
1282 static int mov_write_hmhd_tag(AVIOContext *pb)
1284 /* This atom must be present, but leaving the values at zero
1285 * seems harmless. */
1286 avio_wb32(pb, 28); /* size */
1287 ffio_wfourcc(pb, "hmhd");
1288 avio_wb32(pb, 0); /* version, flags */
1289 avio_wb16(pb, 0); /* maxPDUsize */
1290 avio_wb16(pb, 0); /* avgPDUsize */
1291 avio_wb32(pb, 0); /* maxbitrate */
1292 avio_wb32(pb, 0); /* avgbitrate */
1293 avio_wb32(pb, 0); /* reserved */
1294 return 28;
1297 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1299 int64_t pos = avio_tell(pb);
1300 avio_wb32(pb, 0); /* size */
1301 ffio_wfourcc(pb, "minf");
1302 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1303 mov_write_vmhd_tag(pb);
1304 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1305 mov_write_smhd_tag(pb);
1306 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1307 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1308 else mov_write_nmhd_tag(pb);
1309 } else if (track->tag == MKTAG('r','t','p',' ')) {
1310 mov_write_hmhd_tag(pb);
1312 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1313 mov_write_hdlr_tag(pb, NULL);
1314 mov_write_dinf_tag(pb);
1315 mov_write_stbl_tag(pb, track);
1316 return update_size(pb, pos);
1319 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1321 int version = track->track_duration < INT32_MAX ? 0 : 1;
1323 if (track->mode == MODE_ISM)
1324 version = 1;
1326 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1327 ffio_wfourcc(pb, "mdhd");
1328 avio_w8(pb, version);
1329 avio_wb24(pb, 0); /* flags */
1330 if (version == 1) {
1331 avio_wb64(pb, track->time);
1332 avio_wb64(pb, track->time);
1333 } else {
1334 avio_wb32(pb, track->time); /* creation time */
1335 avio_wb32(pb, track->time); /* modification time */
1337 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1338 if (!track->entry)
1339 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1340 else
1341 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1342 avio_wb16(pb, track->language); /* language */
1343 avio_wb16(pb, 0); /* reserved (quality) */
1345 if(version!=0 && track->mode == MODE_MOV){
1346 av_log(NULL, AV_LOG_ERROR,
1347 "FATAL error, file duration too long for timebase, this file will not be\n"
1348 "playable with quicktime. Choose a different timebase or a different\n"
1349 "container format\n");
1352 return 32;
1355 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1357 int64_t pos = avio_tell(pb);
1358 avio_wb32(pb, 0); /* size */
1359 ffio_wfourcc(pb, "mdia");
1360 mov_write_mdhd_tag(pb, track);
1361 mov_write_hdlr_tag(pb, track);
1362 mov_write_minf_tag(pb, track);
1363 return update_size(pb, pos);
1366 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1368 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1369 track->timescale, AV_ROUND_UP);
1370 int version = duration < INT32_MAX ? 0 : 1;
1372 if (track->mode == MODE_ISM)
1373 version = 1;
1375 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1376 ffio_wfourcc(pb, "tkhd");
1377 avio_w8(pb, version);
1378 avio_wb24(pb, 0xf); /* flags (track enabled) */
1379 if (version == 1) {
1380 avio_wb64(pb, track->time);
1381 avio_wb64(pb, track->time);
1382 } else {
1383 avio_wb32(pb, track->time); /* creation time */
1384 avio_wb32(pb, track->time); /* modification time */
1386 avio_wb32(pb, track->track_id); /* track-id */
1387 avio_wb32(pb, 0); /* reserved */
1388 if (!track->entry)
1389 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1390 else
1391 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1393 avio_wb32(pb, 0); /* reserved */
1394 avio_wb32(pb, 0); /* reserved */
1395 avio_wb16(pb, 0); /* layer */
1396 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1397 /* Volume, only for audio */
1398 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1399 avio_wb16(pb, 0x0100);
1400 else
1401 avio_wb16(pb, 0);
1402 avio_wb16(pb, 0); /* reserved */
1404 /* Matrix structure */
1405 avio_wb32(pb, 0x00010000); /* reserved */
1406 avio_wb32(pb, 0x0); /* reserved */
1407 avio_wb32(pb, 0x0); /* reserved */
1408 avio_wb32(pb, 0x0); /* reserved */
1409 avio_wb32(pb, 0x00010000); /* reserved */
1410 avio_wb32(pb, 0x0); /* reserved */
1411 avio_wb32(pb, 0x0); /* reserved */
1412 avio_wb32(pb, 0x0); /* reserved */
1413 avio_wb32(pb, 0x40000000); /* reserved */
1415 /* Track width and height, for visual only */
1416 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1417 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1418 if(track->mode == MODE_MOV) {
1419 avio_wb32(pb, track->enc->width << 16);
1420 avio_wb32(pb, track->height << 16);
1421 } else {
1422 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1423 if(!sample_aspect_ratio || track->height != track->enc->height)
1424 sample_aspect_ratio = 1;
1425 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1426 avio_wb32(pb, track->height*0x10000);
1429 else {
1430 avio_wb32(pb, 0);
1431 avio_wb32(pb, 0);
1433 return 0x5c;
1436 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1438 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1439 track->enc->sample_aspect_ratio.den);
1441 int64_t pos = avio_tell(pb);
1443 avio_wb32(pb, 0); /* size */
1444 ffio_wfourcc(pb, "tapt");
1446 avio_wb32(pb, 20);
1447 ffio_wfourcc(pb, "clef");
1448 avio_wb32(pb, 0);
1449 avio_wb32(pb, width << 16);
1450 avio_wb32(pb, track->enc->height << 16);
1452 avio_wb32(pb, 20);
1453 ffio_wfourcc(pb, "enof");
1454 avio_wb32(pb, 0);
1455 avio_wb32(pb, track->enc->width << 16);
1456 avio_wb32(pb, track->enc->height << 16);
1458 return update_size(pb, pos);
1461 // This box seems important for the psp playback ... without it the movie seems to hang
1462 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1464 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1465 track->timescale, AV_ROUND_UP);
1466 int version = duration < INT32_MAX ? 0 : 1;
1467 int entry_size, entry_count, size;
1468 int64_t delay, start_ct = track->cluster[0].cts;
1469 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1470 track->timescale, AV_ROUND_DOWN);
1471 version |= delay < INT32_MAX ? 0 : 1;
1473 entry_size = (version == 1) ? 20 : 12;
1474 entry_count = 1 + (delay > 0);
1475 size = 24 + entry_count * entry_size;
1477 /* write the atom data */
1478 avio_wb32(pb, size);
1479 ffio_wfourcc(pb, "edts");
1480 avio_wb32(pb, size - 8);
1481 ffio_wfourcc(pb, "elst");
1482 avio_w8(pb, version);
1483 avio_wb24(pb, 0); /* flags */
1485 avio_wb32(pb, entry_count);
1486 if (delay > 0) { /* add an empty edit to delay presentation */
1487 if (version == 1) {
1488 avio_wb64(pb, delay);
1489 avio_wb64(pb, -1);
1490 } else {
1491 avio_wb32(pb, delay);
1492 avio_wb32(pb, -1);
1494 avio_wb32(pb, 0x00010000);
1497 /* duration */
1498 if (version == 1) {
1499 avio_wb64(pb, duration);
1500 avio_wb64(pb, start_ct);
1501 } else {
1502 avio_wb32(pb, duration);
1503 avio_wb32(pb, start_ct);
1505 avio_wb32(pb, 0x00010000);
1506 return size;
1509 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1511 avio_wb32(pb, 20); // size
1512 ffio_wfourcc(pb, "tref");
1513 avio_wb32(pb, 12); // size (subatom)
1514 avio_wl32(pb, track->tref_tag);
1515 avio_wb32(pb, track->tref_id);
1516 return 20;
1519 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1520 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1522 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1523 ffio_wfourcc(pb, "uuid");
1524 ffio_wfourcc(pb, "USMT");
1525 avio_wb32(pb, 0x21d24fce);
1526 avio_wb32(pb, 0xbb88695c);
1527 avio_wb32(pb, 0xfac9c740);
1528 avio_wb32(pb, 0x1c); // another size here!
1529 ffio_wfourcc(pb, "MTDT");
1530 avio_wb32(pb, 0x00010012);
1531 avio_wb32(pb, 0x0a);
1532 avio_wb32(pb, 0x55c40000);
1533 avio_wb32(pb, 0x1);
1534 avio_wb32(pb, 0x0);
1535 return 0x34;
1538 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1541 AVFormatContext *ctx = track->rtp_ctx;
1542 char buf[1000] = "";
1543 int len;
1545 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1546 NULL, NULL, 0, 0, ctx);
1547 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1548 len = strlen(buf);
1550 avio_wb32(pb, len + 24);
1551 ffio_wfourcc(pb, "udta");
1552 avio_wb32(pb, len + 16);
1553 ffio_wfourcc(pb, "hnti");
1554 avio_wb32(pb, len + 8);
1555 ffio_wfourcc(pb, "sdp ");
1556 avio_write(pb, buf, len);
1557 return len + 24;
1560 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1561 MOVTrack *track, AVStream *st)
1563 int64_t pos = avio_tell(pb);
1564 avio_wb32(pb, 0); /* size */
1565 ffio_wfourcc(pb, "trak");
1566 mov_write_tkhd_tag(pb, track, st);
1567 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1568 (track->entry && track->cluster[0].dts)) {
1569 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1570 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1572 if (track->tref_tag)
1573 mov_write_tref_tag(pb, track);
1574 mov_write_mdia_tag(pb, track);
1575 if (track->mode == MODE_PSP)
1576 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1577 if (track->tag == MKTAG('r','t','p',' '))
1578 mov_write_udta_sdp(pb, track);
1579 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1580 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1581 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1582 mov_write_tapt_tag(pb, track);
1584 return update_size(pb, pos);
1587 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1589 int i, has_audio = 0, has_video = 0;
1590 int64_t pos = avio_tell(pb);
1591 int audio_profile = mov->iods_audio_profile;
1592 int video_profile = mov->iods_video_profile;
1593 for (i = 0; i < mov->nb_streams; i++) {
1594 if(mov->tracks[i].entry > 0) {
1595 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1596 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1599 if (audio_profile < 0)
1600 audio_profile = 0xFF - has_audio;
1601 if (video_profile < 0)
1602 video_profile = 0xFF - has_video;
1603 avio_wb32(pb, 0x0); /* size */
1604 ffio_wfourcc(pb, "iods");
1605 avio_wb32(pb, 0); /* version & flags */
1606 put_descr(pb, 0x10, 7);
1607 avio_wb16(pb, 0x004f);
1608 avio_w8(pb, 0xff);
1609 avio_w8(pb, 0xff);
1610 avio_w8(pb, audio_profile);
1611 avio_w8(pb, video_profile);
1612 avio_w8(pb, 0xff);
1613 return update_size(pb, pos);
1616 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1618 avio_wb32(pb, 0x20); /* size */
1619 ffio_wfourcc(pb, "trex");
1620 avio_wb32(pb, 0); /* version & flags */
1621 avio_wb32(pb, track->track_id); /* track ID */
1622 avio_wb32(pb, 1); /* default sample description index */
1623 avio_wb32(pb, 0); /* default sample duration */
1624 avio_wb32(pb, 0); /* default sample size */
1625 avio_wb32(pb, 0); /* default sample flags */
1626 return 0;
1629 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1631 int64_t pos = avio_tell(pb);
1632 int i;
1633 avio_wb32(pb, 0x0); /* size */
1634 ffio_wfourcc(pb, "mvex");
1635 for (i = 0; i < mov->nb_streams; i++)
1636 mov_write_trex_tag(pb, &mov->tracks[i]);
1637 return update_size(pb, pos);
1640 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1642 int max_track_id = 1, i;
1643 int64_t max_track_len_temp, max_track_len = 0;
1644 int version;
1646 for (i=0; i<mov->nb_streams; i++) {
1647 if(mov->tracks[i].entry > 0) {
1648 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1649 MOV_TIMESCALE,
1650 mov->tracks[i].timescale,
1651 AV_ROUND_UP);
1652 if (max_track_len < max_track_len_temp)
1653 max_track_len = max_track_len_temp;
1654 if (max_track_id < mov->tracks[i].track_id)
1655 max_track_id = mov->tracks[i].track_id;
1659 version = max_track_len < UINT32_MAX ? 0 : 1;
1660 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1661 ffio_wfourcc(pb, "mvhd");
1662 avio_w8(pb, version);
1663 avio_wb24(pb, 0); /* flags */
1664 if (version == 1) {
1665 avio_wb64(pb, mov->time);
1666 avio_wb64(pb, mov->time);
1667 } else {
1668 avio_wb32(pb, mov->time); /* creation time */
1669 avio_wb32(pb, mov->time); /* modification time */
1671 avio_wb32(pb, MOV_TIMESCALE);
1672 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1674 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1675 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1676 avio_wb16(pb, 0); /* reserved */
1677 avio_wb32(pb, 0); /* reserved */
1678 avio_wb32(pb, 0); /* reserved */
1680 /* Matrix structure */
1681 avio_wb32(pb, 0x00010000); /* reserved */
1682 avio_wb32(pb, 0x0); /* reserved */
1683 avio_wb32(pb, 0x0); /* reserved */
1684 avio_wb32(pb, 0x0); /* reserved */
1685 avio_wb32(pb, 0x00010000); /* reserved */
1686 avio_wb32(pb, 0x0); /* reserved */
1687 avio_wb32(pb, 0x0); /* reserved */
1688 avio_wb32(pb, 0x0); /* reserved */
1689 avio_wb32(pb, 0x40000000); /* reserved */
1691 avio_wb32(pb, 0); /* reserved (preview time) */
1692 avio_wb32(pb, 0); /* reserved (preview duration) */
1693 avio_wb32(pb, 0); /* reserved (poster time) */
1694 avio_wb32(pb, 0); /* reserved (selection time) */
1695 avio_wb32(pb, 0); /* reserved (selection duration) */
1696 avio_wb32(pb, 0); /* reserved (current time) */
1697 avio_wb32(pb, max_track_id + 1); /* Next track id */
1698 return 0x6c;
1701 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1702 AVFormatContext *s)
1704 avio_wb32(pb, 33); /* size */
1705 ffio_wfourcc(pb, "hdlr");
1706 avio_wb32(pb, 0);
1707 avio_wb32(pb, 0);
1708 ffio_wfourcc(pb, "mdir");
1709 ffio_wfourcc(pb, "appl");
1710 avio_wb32(pb, 0);
1711 avio_wb32(pb, 0);
1712 avio_w8(pb, 0);
1713 return 33;
1716 /* helper function to write a data tag with the specified string as data */
1717 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1719 if(long_style){
1720 int size = 16 + strlen(data);
1721 avio_wb32(pb, size); /* size */
1722 ffio_wfourcc(pb, "data");
1723 avio_wb32(pb, 1);
1724 avio_wb32(pb, 0);
1725 avio_write(pb, data, strlen(data));
1726 return size;
1727 }else{
1728 if (!lang)
1729 lang = ff_mov_iso639_to_lang("und", 1);
1730 avio_wb16(pb, strlen(data)); /* string length */
1731 avio_wb16(pb, lang);
1732 avio_write(pb, data, strlen(data));
1733 return strlen(data) + 4;
1737 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1738 int size = 0;
1739 if (value && value[0]) {
1740 int64_t pos = avio_tell(pb);
1741 avio_wb32(pb, 0); /* size */
1742 ffio_wfourcc(pb, name);
1743 mov_write_string_data_tag(pb, value, lang, long_style);
1744 size = update_size(pb, pos);
1746 return size;
1749 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1750 const char *name, const char *tag,
1751 int long_style)
1753 int l, lang = 0, len, len2;
1754 AVDictionaryEntry *t, *t2 = NULL;
1755 char tag2[16];
1757 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1758 return 0;
1760 len = strlen(t->key);
1761 snprintf(tag2, sizeof(tag2), "%s-", tag);
1762 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1763 len2 = strlen(t2->key);
1764 if (len2 == len+4 && !strcmp(t->value, t2->value)
1765 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1766 lang = l;
1767 break;
1770 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1773 /* iTunes track number */
1774 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1775 AVFormatContext *s)
1777 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1778 int size = 0, track = t ? atoi(t->value) : 0;
1779 if (track) {
1780 avio_wb32(pb, 32); /* size */
1781 ffio_wfourcc(pb, "trkn");
1782 avio_wb32(pb, 24); /* size */
1783 ffio_wfourcc(pb, "data");
1784 avio_wb32(pb, 0); // 8 bytes empty
1785 avio_wb32(pb, 0);
1786 avio_wb16(pb, 0); // empty
1787 avio_wb16(pb, track); // track number
1788 avio_wb16(pb, 0); // total track number
1789 avio_wb16(pb, 0); // empty
1790 size = 32;
1792 return size;
1795 /* iTunes meta data list */
1796 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1797 AVFormatContext *s)
1799 int64_t pos = avio_tell(pb);
1800 avio_wb32(pb, 0); /* size */
1801 ffio_wfourcc(pb, "ilst");
1802 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1803 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1804 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1805 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1806 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1807 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1808 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1809 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1810 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1811 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1812 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1813 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1814 mov_write_string_metadata(s, pb, "desc", "description",1);
1815 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1816 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1817 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1818 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1819 mov_write_trkn_tag(pb, mov, s);
1820 return update_size(pb, pos);
1823 /* iTunes meta data tag */
1824 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1825 AVFormatContext *s)
1827 int size = 0;
1828 int64_t pos = avio_tell(pb);
1829 avio_wb32(pb, 0); /* size */
1830 ffio_wfourcc(pb, "meta");
1831 avio_wb32(pb, 0);
1832 mov_write_itunes_hdlr_tag(pb, mov, s);
1833 mov_write_ilst_tag(pb, mov, s);
1834 size = update_size(pb, pos);
1835 return size;
1838 static int utf8len(const uint8_t *b)
1840 int len=0;
1841 int val;
1842 while(*b){
1843 GET_UTF8(val, *b++, return -1;)
1844 len++;
1846 return len;
1849 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1851 int val;
1852 while(*b){
1853 GET_UTF8(val, *b++, return -1;)
1854 avio_wb16(pb, val);
1856 avio_wb16(pb, 0x00);
1857 return 0;
1860 static uint16_t language_code(const char *str)
1862 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1865 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1866 const char *tag, const char *str)
1868 int64_t pos = avio_tell(pb);
1869 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1870 if (!t || !utf8len(t->value))
1871 return 0;
1872 avio_wb32(pb, 0); /* size */
1873 ffio_wfourcc(pb, tag); /* type */
1874 avio_wb32(pb, 0); /* version + flags */
1875 if (!strcmp(tag, "yrrc"))
1876 avio_wb16(pb, atoi(t->value));
1877 else {
1878 avio_wb16(pb, language_code("eng")); /* language */
1879 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1880 if (!strcmp(tag, "albm") &&
1881 (t = av_dict_get(s->metadata, "track", NULL, 0)))
1882 avio_w8(pb, atoi(t->value));
1884 return update_size(pb, pos);
1887 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1889 int64_t pos = avio_tell(pb);
1890 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1892 avio_wb32(pb, 0); // size
1893 ffio_wfourcc(pb, "chpl");
1894 avio_wb32(pb, 0x01000000); // version + flags
1895 avio_wb32(pb, 0); // unknown
1896 avio_w8(pb, nb_chapters);
1898 for (i = 0; i < nb_chapters; i++) {
1899 AVChapter *c = s->chapters[i];
1900 AVDictionaryEntry *t;
1901 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1903 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1904 int len = FFMIN(strlen(t->value), 255);
1905 avio_w8(pb, len);
1906 avio_write(pb, t->value, len);
1907 } else
1908 avio_w8(pb, 0);
1910 return update_size(pb, pos);
1913 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1914 AVFormatContext *s)
1916 AVIOContext *pb_buf;
1917 int i, ret, size;
1918 uint8_t *buf;
1920 for (i = 0; i < s->nb_streams; i++)
1921 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1922 return 0;
1925 ret = avio_open_dyn_buf(&pb_buf);
1926 if(ret < 0)
1927 return ret;
1929 if (mov->mode & MODE_3GP) {
1930 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1931 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1932 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1933 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1934 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1935 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1936 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1937 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1938 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1939 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
1940 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1941 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1942 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1943 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
1944 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
1945 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1946 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1947 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1948 } else {
1949 /* iTunes meta data */
1950 mov_write_meta_tag(pb_buf, mov, s);
1953 if (s->nb_chapters)
1954 mov_write_chpl_tag(pb_buf, s);
1956 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1957 avio_wb32(pb, size+8);
1958 ffio_wfourcc(pb, "udta");
1959 avio_write(pb, buf, size);
1961 av_free(buf);
1963 return 0;
1966 static void mov_write_psp_udta_tag(AVIOContext *pb,
1967 const char *str, const char *lang, int type)
1969 int len = utf8len(str)+1;
1970 if(len<=0)
1971 return;
1972 avio_wb16(pb, len*2+10); /* size */
1973 avio_wb32(pb, type); /* type */
1974 avio_wb16(pb, language_code(lang)); /* language */
1975 avio_wb16(pb, 0x01); /* ? */
1976 ascii_to_wc(pb, str);
1979 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1981 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
1982 int64_t pos, pos2;
1984 if (title) {
1985 pos = avio_tell(pb);
1986 avio_wb32(pb, 0); /* size placeholder*/
1987 ffio_wfourcc(pb, "uuid");
1988 ffio_wfourcc(pb, "USMT");
1989 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1990 avio_wb32(pb, 0xbb88695c);
1991 avio_wb32(pb, 0xfac9c740);
1993 pos2 = avio_tell(pb);
1994 avio_wb32(pb, 0); /* size placeholder*/
1995 ffio_wfourcc(pb, "MTDT");
1996 avio_wb16(pb, 4);
1998 // ?
1999 avio_wb16(pb, 0x0C); /* size */
2000 avio_wb32(pb, 0x0B); /* type */
2001 avio_wb16(pb, language_code("und")); /* language */
2002 avio_wb16(pb, 0x0); /* ? */
2003 avio_wb16(pb, 0x021C); /* data */
2005 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2006 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2007 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2009 update_size(pb, pos2);
2010 return update_size(pb, pos);
2013 return 0;
2016 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2017 AVFormatContext *s)
2019 int i;
2020 int64_t pos = avio_tell(pb);
2021 avio_wb32(pb, 0); /* size placeholder*/
2022 ffio_wfourcc(pb, "moov");
2024 for (i=0; i<mov->nb_streams; i++) {
2025 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2026 continue;
2028 mov->tracks[i].time = mov->time;
2029 mov->tracks[i].track_id = i+1;
2032 if (mov->chapter_track)
2033 for (i=0; i<s->nb_streams; i++) {
2034 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2035 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2037 for (i = 0; i < mov->nb_streams; i++) {
2038 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2039 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2040 mov->tracks[i].tref_id =
2041 mov->tracks[mov->tracks[i].src_track].track_id;
2045 mov_write_mvhd_tag(pb, mov);
2046 if (mov->mode != MODE_MOV && !mov->iods_skip)
2047 mov_write_iods_tag(pb, mov);
2048 for (i=0; i<mov->nb_streams; i++) {
2049 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2050 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2053 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2054 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2056 if (mov->mode == MODE_PSP)
2057 mov_write_uuidusmt_tag(pb, s);
2058 else
2059 mov_write_udta_tag(pb, mov, s);
2061 return update_size(pb, pos);
2064 static void param_write_int(AVIOContext *pb, const char *name, int value)
2066 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2069 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2071 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2074 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2076 char buf[150];
2077 len = FFMIN(sizeof(buf)/2 - 1, len);
2078 ff_data_to_hex(buf, value, len, 0);
2079 buf[2*len] = '\0';
2080 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2083 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2085 int64_t pos = avio_tell(pb);
2086 int i;
2087 const uint8_t uuid[] = {
2088 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2089 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2092 avio_wb32(pb, 0);
2093 ffio_wfourcc(pb, "uuid");
2094 avio_write(pb, uuid, sizeof(uuid));
2095 avio_wb32(pb, 0);
2097 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2098 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2099 avio_printf(pb, "<head>\n");
2100 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2101 LIBAVFORMAT_IDENT);
2102 avio_printf(pb, "</head>\n");
2103 avio_printf(pb, "<body>\n");
2104 avio_printf(pb, "<switch>\n");
2105 for (i = 0; i < mov->nb_streams; i++) {
2106 MOVTrack *track = &mov->tracks[i];
2107 const char *type;
2108 /* track->track_id is initialized in write_moov, and thus isn't known
2109 * here yet */
2110 int track_id = i + 1;
2112 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2113 type = "video";
2114 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2115 type = "audio";
2116 } else {
2117 continue;
2119 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2120 track->enc->bit_rate);
2121 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2122 param_write_int(pb, "trackID", track_id);
2123 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2124 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2125 uint8_t *ptr;
2126 int size = track->enc->extradata_size;
2127 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2128 &size)) {
2129 param_write_hex(pb, "CodecPrivateData",
2130 ptr ? ptr : track->enc->extradata,
2131 size);
2132 av_free(ptr);
2134 param_write_string(pb, "FourCC", "H264");
2135 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2136 param_write_string(pb, "FourCC", "WVC1");
2137 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2138 track->enc->extradata_size);
2140 param_write_int(pb, "MaxWidth", track->enc->width);
2141 param_write_int(pb, "MaxHeight", track->enc->height);
2142 param_write_int(pb, "DisplayWidth", track->enc->width);
2143 param_write_int(pb, "DisplayHeight", track->enc->height);
2144 } else {
2145 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2146 param_write_string(pb, "FourCC", "AACL");
2147 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2148 param_write_string(pb, "FourCC", "WMAP");
2150 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2151 track->enc->extradata_size);
2152 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2153 track->enc->codec_id));
2154 param_write_int(pb, "Channels", track->enc->channels);
2155 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2156 param_write_int(pb, "BitsPerSample", 16);
2157 param_write_int(pb, "PacketSize", track->enc->block_align ?
2158 track->enc->block_align : 4);
2160 avio_printf(pb, "</%s>\n", type);
2162 avio_printf(pb, "</switch>\n");
2163 avio_printf(pb, "</body>\n");
2164 avio_printf(pb, "</smil>\n");
2166 return update_size(pb, pos);
2169 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2171 avio_wb32(pb, 16);
2172 ffio_wfourcc(pb, "mfhd");
2173 avio_wb32(pb, 0);
2174 avio_wb32(pb, mov->fragments);
2175 return 0;
2178 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2179 int64_t moof_offset)
2181 int64_t pos = avio_tell(pb);
2182 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2183 MOV_TFHD_BASE_DATA_OFFSET;
2184 if (!track->entry) {
2185 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2186 } else {
2187 flags |= MOV_TFHD_DEFAULT_FLAGS;
2190 /* Don't set a default sample size, the silverlight player refuses
2191 * to play files with that set. Don't set a default sample duration,
2192 * WMP freaks out if it is set. */
2193 if (track->mode == MODE_ISM)
2194 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION);
2196 avio_wb32(pb, 0); /* size placeholder */
2197 ffio_wfourcc(pb, "tfhd");
2198 avio_w8(pb, 0); /* version */
2199 avio_wb24(pb, flags);
2201 avio_wb32(pb, track->track_id); /* track-id */
2202 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2203 avio_wb64(pb, moof_offset);
2204 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2205 track->default_duration = get_cluster_duration(track, 0);
2206 avio_wb32(pb, track->default_duration);
2208 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2209 track->default_size = track->entry ? track->cluster[0].size : 1;
2210 avio_wb32(pb, track->default_size);
2211 } else
2212 track->default_size = -1;
2214 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2215 track->default_sample_flags =
2216 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2217 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2218 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2219 avio_wb32(pb, track->default_sample_flags);
2222 return update_size(pb, pos);
2225 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2227 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2228 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2231 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2233 int64_t pos = avio_tell(pb);
2234 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2235 int i;
2237 for (i = 0; i < track->entry; i++) {
2238 if (get_cluster_duration(track, i) != track->default_duration)
2239 flags |= MOV_TRUN_SAMPLE_DURATION;
2240 if (track->cluster[i].size != track->default_size)
2241 flags |= MOV_TRUN_SAMPLE_SIZE;
2242 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2243 flags |= MOV_TRUN_SAMPLE_FLAGS;
2245 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2246 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2247 if (track->flags & MOV_TRACK_CTTS)
2248 flags |= MOV_TRUN_SAMPLE_CTS;
2250 avio_wb32(pb, 0); /* size placeholder */
2251 ffio_wfourcc(pb, "trun");
2252 avio_w8(pb, 0); /* version */
2253 avio_wb24(pb, flags);
2255 avio_wb32(pb, track->entry); /* sample count */
2256 track->moof_size_offset = avio_tell(pb);
2257 avio_wb32(pb, 0); /* data offset */
2258 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2259 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2261 for (i = 0; i < track->entry; i++) {
2262 if (flags & MOV_TRUN_SAMPLE_DURATION)
2263 avio_wb32(pb, get_cluster_duration(track, i));
2264 if (flags & MOV_TRUN_SAMPLE_SIZE)
2265 avio_wb32(pb, track->cluster[i].size);
2266 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2267 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2268 if (flags & MOV_TRUN_SAMPLE_CTS)
2269 avio_wb32(pb, track->cluster[i].cts);
2272 return update_size(pb, pos);
2275 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2277 int64_t pos = avio_tell(pb);
2278 const uint8_t uuid[] = {
2279 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2280 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2283 avio_wb32(pb, 0); /* size placeholder */
2284 ffio_wfourcc(pb, "uuid");
2285 avio_write(pb, uuid, sizeof(uuid));
2286 avio_w8(pb, 1);
2287 avio_wb24(pb, 0);
2288 avio_wb64(pb, track->frag_start);
2289 avio_wb64(pb, track->start_dts + track->track_duration -
2290 track->cluster[0].dts);
2292 return update_size(pb, pos);
2295 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2296 MOVTrack *track, int entry)
2298 int n = track->nb_frag_info - 1 - entry, i;
2299 int size = 8 + 16 + 4 + 1 + 16*n;
2300 const uint8_t uuid[] = {
2301 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2302 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2305 if (entry < 0)
2306 return 0;
2308 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2309 avio_wb32(pb, size);
2310 ffio_wfourcc(pb, "uuid");
2311 avio_write(pb, uuid, sizeof(uuid));
2312 avio_w8(pb, 1);
2313 avio_wb24(pb, 0);
2314 avio_w8(pb, n);
2315 for (i = 0; i < n; i++) {
2316 int index = entry + 1 + i;
2317 avio_wb64(pb, track->frag_info[index].time);
2318 avio_wb64(pb, track->frag_info[index].duration);
2320 if (n < mov->ism_lookahead) {
2321 int free_size = 16*(mov->ism_lookahead - n);
2322 avio_wb32(pb, free_size);
2323 ffio_wfourcc(pb, "free");
2324 for (i = 0; i < free_size - 8; i++)
2325 avio_w8(pb, 0);
2328 return 0;
2331 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2332 MOVTrack *track)
2334 int64_t pos = avio_tell(pb);
2335 int i;
2336 for (i = 0; i < mov->ism_lookahead; i++) {
2337 /* Update the tfrf tag for the last ism_lookahead fragments,
2338 * nb_frag_info - 1 is the next fragment to be written. */
2339 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2341 avio_seek(pb, pos, SEEK_SET);
2342 return 0;
2345 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2346 MOVTrack *track, int64_t moof_offset)
2348 int64_t pos = avio_tell(pb);
2349 avio_wb32(pb, 0); /* size placeholder */
2350 ffio_wfourcc(pb, "traf");
2352 mov_write_tfhd_tag(pb, track, moof_offset);
2353 mov_write_trun_tag(pb, track);
2354 if (mov->mode == MODE_ISM) {
2355 mov_write_tfxd_tag(pb, track);
2357 if (mov->ism_lookahead) {
2358 int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
2360 track->tfrf_offset = avio_tell(pb);
2361 avio_wb32(pb, 8 + size);
2362 ffio_wfourcc(pb, "free");
2363 for (i = 0; i < size; i++)
2364 avio_w8(pb, 0);
2368 return update_size(pb, pos);
2371 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2373 int64_t pos = avio_tell(pb), end;
2374 int i, moof_size;
2376 avio_wb32(pb, 0); /* size placeholder */
2377 ffio_wfourcc(pb, "moof");
2379 mov_write_mfhd_tag(pb, mov);
2380 for (i = 0; i < mov->nb_streams; i++) {
2381 MOVTrack *track = &mov->tracks[i];
2382 if (tracks >= 0 && i != tracks)
2383 continue;
2384 if (!track->entry)
2385 continue;
2386 mov_write_traf_tag(pb, mov, track, pos);
2389 end = avio_tell(pb);
2390 moof_size = end - pos;
2391 for (i = 0; i < mov->nb_streams; i++) {
2392 MOVTrack *track = &mov->tracks[i];
2393 if (tracks >= 0 && i != tracks)
2394 continue;
2395 if (!track->entry)
2396 continue;
2397 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2398 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2400 avio_seek(pb, end, SEEK_SET);
2402 return update_size(pb, pos);
2405 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2407 int64_t pos = avio_tell(pb);
2408 int i;
2410 avio_wb32(pb, 0); /* size placeholder */
2411 ffio_wfourcc(pb, "tfra");
2412 avio_w8(pb, 1); /* version */
2413 avio_wb24(pb, 0);
2415 avio_wb32(pb, track->track_id);
2416 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2417 avio_wb32(pb, track->nb_frag_info);
2418 for (i = 0; i < track->nb_frag_info; i++) {
2419 avio_wb64(pb, track->frag_info[i].time);
2420 avio_wb64(pb, track->frag_info[i].offset);
2421 avio_w8(pb, 1); /* traf number */
2422 avio_w8(pb, 1); /* trun number */
2423 avio_w8(pb, 1); /* sample number */
2426 return update_size(pb, pos);
2429 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2431 int64_t pos = avio_tell(pb);
2432 int i;
2434 avio_wb32(pb, 0); /* size placeholder */
2435 ffio_wfourcc(pb, "mfra");
2436 /* An empty mfra atom is enough to indicate to the publishing point that
2437 * the stream has ended. */
2438 if (mov->flags & FF_MOV_FLAG_ISML)
2439 return update_size(pb, pos);
2441 for (i = 0; i < mov->nb_streams; i++) {
2442 MOVTrack *track = &mov->tracks[i];
2443 if (track->nb_frag_info)
2444 mov_write_tfra_tag(pb, track);
2447 avio_wb32(pb, 16);
2448 ffio_wfourcc(pb, "mfro");
2449 avio_wb32(pb, 0); /* version + flags */
2450 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2452 return update_size(pb, pos);
2455 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2457 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2458 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2460 mov->mdat_pos = avio_tell(pb);
2461 avio_wb32(pb, 0); /* size placeholder*/
2462 ffio_wfourcc(pb, "mdat");
2463 return 0;
2466 /* TODO: This needs to be more general */
2467 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2469 MOVMuxContext *mov = s->priv_data;
2470 int64_t pos = avio_tell(pb);
2471 int has_h264 = 0, has_video = 0;
2472 int minor = 0x200;
2473 int i;
2475 for (i = 0; i < s->nb_streams; i++) {
2476 AVStream *st = s->streams[i];
2477 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2478 has_video = 1;
2479 if (st->codec->codec_id == AV_CODEC_ID_H264)
2480 has_h264 = 1;
2483 avio_wb32(pb, 0); /* size */
2484 ffio_wfourcc(pb, "ftyp");
2486 if (mov->mode == MODE_3GP) {
2487 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2488 minor = has_h264 ? 0x100 : 0x200;
2489 } else if (mov->mode & MODE_3G2) {
2490 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2491 minor = has_h264 ? 0x20000 : 0x10000;
2492 }else if (mov->mode == MODE_PSP)
2493 ffio_wfourcc(pb, "MSNV");
2494 else if (mov->mode == MODE_MP4)
2495 ffio_wfourcc(pb, "isom");
2496 else if (mov->mode == MODE_IPOD)
2497 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2498 else if (mov->mode == MODE_ISM)
2499 ffio_wfourcc(pb, "isml");
2500 else
2501 ffio_wfourcc(pb, "qt ");
2503 avio_wb32(pb, minor);
2505 if(mov->mode == MODE_MOV)
2506 ffio_wfourcc(pb, "qt ");
2507 else if (mov->mode == MODE_ISM) {
2508 ffio_wfourcc(pb, "piff");
2509 ffio_wfourcc(pb, "iso2");
2510 } else {
2511 ffio_wfourcc(pb, "isom");
2512 ffio_wfourcc(pb, "iso2");
2513 if(has_h264)
2514 ffio_wfourcc(pb, "avc1");
2517 if (mov->mode == MODE_3GP)
2518 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2519 else if (mov->mode & MODE_3G2)
2520 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2521 else if (mov->mode == MODE_PSP)
2522 ffio_wfourcc(pb, "MSNV");
2523 else if (mov->mode == MODE_MP4)
2524 ffio_wfourcc(pb, "mp41");
2525 return update_size(pb, pos);
2528 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
2530 AVCodecContext *video_codec = s->streams[0]->codec;
2531 AVCodecContext *audio_codec = s->streams[1]->codec;
2532 int audio_rate = audio_codec->sample_rate;
2533 int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
2534 int audio_kbitrate = audio_codec->bit_rate / 1000;
2535 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2537 avio_wb32(pb, 0x94); /* size */
2538 ffio_wfourcc(pb, "uuid");
2539 ffio_wfourcc(pb, "PROF");
2541 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2542 avio_wb32(pb, 0xbb88695c);
2543 avio_wb32(pb, 0xfac9c740);
2545 avio_wb32(pb, 0x0); /* ? */
2546 avio_wb32(pb, 0x3); /* 3 sections ? */
2548 avio_wb32(pb, 0x14); /* size */
2549 ffio_wfourcc(pb, "FPRF");
2550 avio_wb32(pb, 0x0); /* ? */
2551 avio_wb32(pb, 0x0); /* ? */
2552 avio_wb32(pb, 0x0); /* ? */
2554 avio_wb32(pb, 0x2c); /* size */
2555 ffio_wfourcc(pb, "APRF");/* audio */
2556 avio_wb32(pb, 0x0);
2557 avio_wb32(pb, 0x2); /* TrackID */
2558 ffio_wfourcc(pb, "mp4a");
2559 avio_wb32(pb, 0x20f);
2560 avio_wb32(pb, 0x0);
2561 avio_wb32(pb, audio_kbitrate);
2562 avio_wb32(pb, audio_kbitrate);
2563 avio_wb32(pb, audio_rate);
2564 avio_wb32(pb, audio_codec->channels);
2566 avio_wb32(pb, 0x34); /* size */
2567 ffio_wfourcc(pb, "VPRF"); /* video */
2568 avio_wb32(pb, 0x0);
2569 avio_wb32(pb, 0x1); /* TrackID */
2570 if (video_codec->codec_id == AV_CODEC_ID_H264) {
2571 ffio_wfourcc(pb, "avc1");
2572 avio_wb16(pb, 0x014D);
2573 avio_wb16(pb, 0x0015);
2574 } else {
2575 ffio_wfourcc(pb, "mp4v");
2576 avio_wb16(pb, 0x0000);
2577 avio_wb16(pb, 0x0103);
2579 avio_wb32(pb, 0x0);
2580 avio_wb32(pb, video_kbitrate);
2581 avio_wb32(pb, video_kbitrate);
2582 avio_wb32(pb, frame_rate);
2583 avio_wb32(pb, frame_rate);
2584 avio_wb16(pb, video_codec->width);
2585 avio_wb16(pb, video_codec->height);
2586 avio_wb32(pb, 0x010001); /* ? */
2589 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2591 uint32_t c = -1;
2592 int i, closed_gop = 0;
2594 for (i = 0; i < pkt->size - 4; i++) {
2595 c = (c<<8) + pkt->data[i];
2596 if (c == 0x1b8) { // gop
2597 closed_gop = pkt->data[i+4]>>6 & 0x01;
2598 } else if (c == 0x100) { // pic
2599 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2600 if (!temp_ref || closed_gop) // I picture is not reordered
2601 *flags = MOV_SYNC_SAMPLE;
2602 else
2603 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2604 break;
2607 return 0;
2610 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2612 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2613 int seq = 0, entry = 0;
2614 int key = pkt->flags & AV_PKT_FLAG_KEY;
2615 start = find_next_marker(pkt->data, end);
2616 for (next = start; next < end; start = next) {
2617 next = find_next_marker(start + 4, end);
2618 switch (AV_RB32(start)) {
2619 case VC1_CODE_SEQHDR:
2620 seq = 1;
2621 break;
2622 case VC1_CODE_ENTRYPOINT:
2623 entry = 1;
2624 break;
2625 case VC1_CODE_SLICE:
2626 trk->vc1_info.slices = 1;
2627 break;
2630 if (!trk->entry && !fragment) {
2631 /* First packet in first fragment */
2632 trk->vc1_info.first_packet_seq = seq;
2633 trk->vc1_info.first_packet_entry = entry;
2634 } else if ((seq && !trk->vc1_info.packet_seq) ||
2635 (entry && !trk->vc1_info.packet_entry)) {
2636 int i;
2637 for (i = 0; i < trk->entry; i++)
2638 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2639 trk->has_keyframes = 0;
2640 if (seq)
2641 trk->vc1_info.packet_seq = 1;
2642 if (entry)
2643 trk->vc1_info.packet_entry = 1;
2644 if (!fragment) {
2645 /* First fragment */
2646 if ((!seq || trk->vc1_info.first_packet_seq) &&
2647 (!entry || trk->vc1_info.first_packet_entry)) {
2648 /* First packet had the same headers as this one, readd the
2649 * sync sample flag. */
2650 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2651 trk->has_keyframes = 1;
2655 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2656 key = seq && entry;
2657 else if (trk->vc1_info.packet_seq)
2658 key = seq;
2659 else if (trk->vc1_info.packet_entry)
2660 key = entry;
2661 if (key) {
2662 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2663 trk->has_keyframes++;
2667 static int mov_flush_fragment(AVFormatContext *s)
2669 MOVMuxContext *mov = s->priv_data;
2670 int i, first_track = -1;
2671 int64_t mdat_size = 0;
2673 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2674 return 0;
2676 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2677 int64_t pos = avio_tell(s->pb);
2678 int ret;
2679 AVIOContext *moov_buf;
2680 uint8_t *buf;
2681 int buf_size;
2683 for (i = 0; i < mov->nb_streams; i++)
2684 if (!mov->tracks[i].entry)
2685 break;
2686 /* Don't write the initial moov unless all tracks have data */
2687 if (i < mov->nb_streams)
2688 return 0;
2690 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
2691 return ret;
2692 mov_write_moov_tag(moov_buf, mov, s);
2693 buf_size = avio_close_dyn_buf(moov_buf, &buf);
2694 av_free(buf);
2695 for (i = 0; i < mov->nb_streams; i++)
2696 mov->tracks[i].data_offset = pos + buf_size + 8;
2698 mov_write_moov_tag(s->pb, mov, s);
2700 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2701 mov->mdat_buf = NULL;
2702 avio_wb32(s->pb, buf_size + 8);
2703 ffio_wfourcc(s->pb, "mdat");
2704 avio_write(s->pb, buf, buf_size);
2705 av_free(buf);
2707 mov->fragments++;
2708 mov->mdat_size = 0;
2709 for (i = 0; i < mov->nb_streams; i++) {
2710 if (mov->tracks[i].entry)
2711 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2712 mov->tracks[i].track_duration -
2713 mov->tracks[i].cluster[0].dts;
2714 mov->tracks[i].entry = 0;
2716 avio_flush(s->pb);
2717 return 0;
2720 for (i = 0; i < mov->nb_streams; i++) {
2721 MOVTrack *track = &mov->tracks[i];
2722 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2723 track->data_offset = 0;
2724 else
2725 track->data_offset = mdat_size;
2726 if (!track->mdat_buf)
2727 continue;
2728 mdat_size += avio_tell(track->mdat_buf);
2729 if (first_track < 0)
2730 first_track = i;
2733 if (!mdat_size)
2734 return 0;
2736 for (i = 0; i < mov->nb_streams; i++) {
2737 MOVTrack *track = &mov->tracks[i];
2738 int buf_size, write_moof = 1, moof_tracks = -1;
2739 uint8_t *buf;
2740 int64_t duration = 0;
2742 if (track->entry)
2743 duration = track->start_dts + track->track_duration -
2744 track->cluster[0].dts;
2745 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2746 if (!track->mdat_buf)
2747 continue;
2748 mdat_size = avio_tell(track->mdat_buf);
2749 moof_tracks = i;
2750 } else {
2751 write_moof = i == first_track;
2754 if (write_moof) {
2755 MOVFragmentInfo *info;
2756 avio_flush(s->pb);
2757 track->nb_frag_info++;
2758 track->frag_info = av_realloc(track->frag_info,
2759 sizeof(*track->frag_info) *
2760 track->nb_frag_info);
2761 info = &track->frag_info[track->nb_frag_info - 1];
2762 info->offset = avio_tell(s->pb);
2763 info->time = mov->tracks[i].frag_start;
2764 info->duration = duration;
2765 mov_write_tfrf_tags(s->pb, mov, track);
2767 mov_write_moof_tag(s->pb, mov, moof_tracks);
2768 info->tfrf_offset = track->tfrf_offset;
2769 mov->fragments++;
2771 avio_wb32(s->pb, mdat_size + 8);
2772 ffio_wfourcc(s->pb, "mdat");
2775 if (track->entry)
2776 track->frag_start += duration;
2777 track->entry = 0;
2778 if (!track->mdat_buf)
2779 continue;
2780 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2781 track->mdat_buf = NULL;
2783 avio_write(s->pb, buf, buf_size);
2784 av_free(buf);
2787 mov->mdat_size = 0;
2789 avio_flush(s->pb);
2790 return 0;
2793 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2795 MOVMuxContext *mov = s->priv_data;
2796 AVIOContext *pb = s->pb;
2797 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2798 AVCodecContext *enc = trk->enc;
2799 unsigned int samples_in_chunk = 0;
2800 int size= pkt->size;
2801 uint8_t *reformatted_data = NULL;
2803 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2804 int ret;
2805 if (mov->fragments > 0) {
2806 if (!trk->mdat_buf) {
2807 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2808 return ret;
2810 pb = trk->mdat_buf;
2811 } else {
2812 if (!mov->mdat_buf) {
2813 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2814 return ret;
2816 pb = mov->mdat_buf;
2820 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
2821 /* We must find out how many AMR blocks there are in one packet */
2822 static uint16_t packed_size[16] =
2823 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
2824 int len = 0;
2826 while (len < size && samples_in_chunk < 100) {
2827 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2828 samples_in_chunk++;
2830 if (samples_in_chunk > 1) {
2831 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2832 return -1;
2834 } else if (trk->sample_size)
2835 samples_in_chunk = size / trk->sample_size;
2836 else
2837 samples_in_chunk = 1;
2839 /* copy extradata if it exists */
2840 if (trk->vos_len == 0 && enc->extradata_size > 0) {
2841 trk->vos_len = enc->extradata_size;
2842 trk->vos_data = av_malloc(trk->vos_len);
2843 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
2846 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
2847 /* from x264 or from bytestream h264 */
2848 /* nal reformating needed */
2849 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2850 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2851 &size);
2852 avio_write(pb, reformatted_data, size);
2853 } else {
2854 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2856 } else {
2857 avio_write(pb, pkt->data, size);
2860 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
2861 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
2862 /* copy frame to create needed atoms */
2863 trk->vos_len = size;
2864 trk->vos_data = av_malloc(size);
2865 if (!trk->vos_data)
2866 return AVERROR(ENOMEM);
2867 memcpy(trk->vos_data, pkt->data, size);
2870 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
2871 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
2872 if (!trk->cluster)
2873 return -1;
2876 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2877 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
2878 trk->cluster[trk->entry].size = size;
2879 trk->cluster[trk->entry].entries = samples_in_chunk;
2880 trk->cluster[trk->entry].dts = pkt->dts;
2881 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
2882 /* First packet of a new fragment. We already wrote the duration
2883 * of the last packet of the previous fragment based on track_duration,
2884 * which might not exactly match our dts. Therefore adjust the dts
2885 * of this packet to be what the previous packets duration implies. */
2886 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
2888 if (trk->start_dts == AV_NOPTS_VALUE)
2889 trk->start_dts = pkt->dts;
2890 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
2892 if (pkt->pts == AV_NOPTS_VALUE) {
2893 av_log(s, AV_LOG_WARNING, "pts has no value\n");
2894 pkt->pts = pkt->dts;
2896 if (pkt->dts != pkt->pts)
2897 trk->flags |= MOV_TRACK_CTTS;
2898 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2899 trk->cluster[trk->entry].flags = 0;
2900 if (enc->codec_id == AV_CODEC_ID_VC1) {
2901 mov_parse_vc1_frame(pkt, trk, mov->fragments);
2902 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
2903 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
2904 trk->entry > 0) { // force sync sample for the first key frame
2905 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2906 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2907 trk->flags |= MOV_TRACK_STPS;
2908 } else {
2909 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2911 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2912 trk->has_keyframes++;
2914 trk->entry++;
2915 trk->sample_count += samples_in_chunk;
2916 mov->mdat_size += size;
2918 avio_flush(pb);
2920 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2921 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
2922 reformatted_data, size);
2923 av_free(reformatted_data);
2924 return 0;
2927 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2929 if (!pkt) {
2930 mov_flush_fragment(s);
2931 return 1;
2932 } else {
2933 MOVMuxContext *mov = s->priv_data;
2934 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2935 AVCodecContext *enc = trk->enc;
2936 int64_t frag_duration = 0;
2937 int size = pkt->size;
2939 if (!pkt->size) return 0; /* Discard 0 sized packets */
2941 if (trk->entry)
2942 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
2943 s->streams[pkt->stream_index]->time_base,
2944 AV_TIME_BASE_Q);
2945 if ((mov->max_fragment_duration &&
2946 frag_duration >= mov->max_fragment_duration) ||
2947 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
2948 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
2949 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
2950 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
2951 if (frag_duration >= mov->min_fragment_duration)
2952 mov_flush_fragment(s);
2955 return ff_mov_write_packet(s, pkt);
2959 // QuickTime chapters involve an additional text track with the chapter names
2960 // as samples, and a tref pointing from the other tracks to the chapter one.
2961 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2963 MOVMuxContext *mov = s->priv_data;
2964 MOVTrack *track = &mov->tracks[tracknum];
2965 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2966 int i, len;
2968 track->mode = mov->mode;
2969 track->tag = MKTAG('t','e','x','t');
2970 track->timescale = MOV_TIMESCALE;
2971 track->enc = avcodec_alloc_context3(NULL);
2972 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2974 for (i = 0; i < s->nb_chapters; i++) {
2975 AVChapter *c = s->chapters[i];
2976 AVDictionaryEntry *t;
2978 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
2979 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
2980 pkt.duration = end - pkt.dts;
2982 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2983 len = strlen(t->value);
2984 pkt.size = len+2;
2985 pkt.data = av_malloc(pkt.size);
2986 AV_WB16(pkt.data, len);
2987 memcpy(pkt.data+2, t->value, len);
2988 ff_mov_write_packet(s, &pkt);
2989 av_freep(&pkt.data);
2994 static int mov_write_header(AVFormatContext *s)
2996 AVIOContext *pb = s->pb;
2997 MOVMuxContext *mov = s->priv_data;
2998 AVDictionaryEntry *t;
2999 int i, hint_track = 0;
3001 /* Set the FRAGMENT flag if any of the fragmentation methods are
3002 * enabled. */
3003 if (mov->max_fragment_duration || mov->max_fragment_size ||
3004 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3005 FF_MOV_FLAG_FRAG_KEYFRAME |
3006 FF_MOV_FLAG_FRAG_CUSTOM))
3007 mov->flags |= FF_MOV_FLAG_FRAGMENT;
3009 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3010 * is enabled, we don't support non-seekable output at all. */
3011 if (!s->pb->seekable &&
3012 ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
3013 !(s->oformat && !strcmp(s->oformat->name, "ismv")))
3014 || mov->ism_lookahead)) {
3015 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3016 return -1;
3019 /* Default mode == MP4 */
3020 mov->mode = MODE_MP4;
3022 if (s->oformat != NULL) {
3023 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3024 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3025 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3026 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3027 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3028 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3030 mov_write_ftyp_tag(pb,s);
3031 if (mov->mode == MODE_PSP) {
3032 if (s->nb_streams != 2) {
3033 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3034 return -1;
3036 mov_write_uuidprof_tag(pb,s);
3040 mov->nb_streams = s->nb_streams;
3041 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
3042 mov->chapter_track = mov->nb_streams++;
3044 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3045 /* Add hint tracks for each audio and video stream */
3046 hint_track = mov->nb_streams;
3047 for (i = 0; i < s->nb_streams; i++) {
3048 AVStream *st = s->streams[i];
3049 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3050 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3051 mov->nb_streams++;
3056 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
3057 if (!mov->tracks)
3058 return AVERROR(ENOMEM);
3060 for(i=0; i<s->nb_streams; i++){
3061 AVStream *st= s->streams[i];
3062 MOVTrack *track= &mov->tracks[i];
3063 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3065 track->enc = st->codec;
3066 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3067 if (track->language < 0)
3068 track->language = 0;
3069 track->mode = mov->mode;
3070 track->tag = mov_find_codec_tag(s, track);
3071 if (!track->tag) {
3072 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
3073 "codec not currently supported in container\n", i);
3074 goto error;
3076 /* If hinting of this track is enabled by a later hint track,
3077 * this is updated. */
3078 track->hint_track = -1;
3079 track->start_dts = AV_NOPTS_VALUE;
3080 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
3081 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3082 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3083 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3084 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3085 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3086 goto error;
3088 track->height = track->tag>>24 == 'n' ? 486 : 576;
3090 track->timescale = st->codec->time_base.den;
3091 if (track->mode == MODE_MOV && track->timescale > 100000)
3092 av_log(s, AV_LOG_WARNING,
3093 "WARNING codec timebase is very high. If duration is too long,\n"
3094 "file may not be playable by quicktime. Specify a shorter timebase\n"
3095 "or choose different container.\n");
3096 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
3097 track->timescale = st->codec->sample_rate;
3098 /* set sample_size for PCM and ADPCM */
3099 if (av_get_bits_per_sample(st->codec->codec_id) ||
3100 st->codec->codec_id == AV_CODEC_ID_ILBC) {
3101 if (!st->codec->block_align) {
3102 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
3103 goto error;
3105 track->sample_size = st->codec->block_align;
3107 /* set audio_vbr for compressed audio */
3108 if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
3109 track->audio_vbr = 1;
3111 if (track->mode != MODE_MOV &&
3112 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3113 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3114 i, track->enc->sample_rate);
3115 goto error;
3117 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
3118 track->timescale = st->codec->time_base.den;
3120 if (!track->height)
3121 track->height = st->codec->height;
3122 /* The ism specific timescale isn't mandatory, but is assumed by
3123 * some tools, such as mp4split. */
3124 if (mov->mode == MODE_ISM)
3125 track->timescale = 10000000;
3127 avpriv_set_pts_info(st, 64, 1, track->timescale);
3129 /* copy extradata if it exists */
3130 if (st->codec->extradata_size) {
3131 track->vos_len = st->codec->extradata_size;
3132 track->vos_data = av_malloc(track->vos_len);
3133 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3137 if (mov->mode == MODE_ISM) {
3138 /* If no fragmentation options have been set, set a default. */
3139 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3140 FF_MOV_FLAG_FRAG_CUSTOM)) &&
3141 !mov->max_fragment_duration && !mov->max_fragment_size)
3142 mov->max_fragment_duration = 5000000;
3143 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
3144 FF_MOV_FLAG_FRAGMENT;
3147 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
3148 mov_write_mdat_tag(pb, mov);
3150 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3151 mov->time = ff_iso8601_to_unix_time(t->value);
3152 if (mov->time)
3153 mov->time += 0x7C25B080; // 1970 based -> 1904 based
3155 if (mov->chapter_track)
3156 mov_create_chapter_track(s, mov->chapter_track);
3158 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3159 /* Initialize the hint tracks for each audio and video stream */
3160 for (i = 0; i < s->nb_streams; i++) {
3161 AVStream *st = s->streams[i];
3162 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3163 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3164 ff_mov_init_hinting(s, hint_track, i);
3165 hint_track++;
3170 avio_flush(pb);
3172 if (mov->flags & FF_MOV_FLAG_ISML)
3173 mov_write_isml_manifest(pb, mov);
3175 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3176 mov_write_moov_tag(pb, mov, s);
3177 mov->fragments++;
3180 return 0;
3181 error:
3182 av_freep(&mov->tracks);
3183 return -1;
3186 static int mov_write_trailer(AVFormatContext *s)
3188 MOVMuxContext *mov = s->priv_data;
3189 AVIOContext *pb = s->pb;
3190 int res = 0;
3191 int i;
3193 int64_t moov_pos = avio_tell(pb);
3195 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3196 /* Write size of mdat tag */
3197 if (mov->mdat_size + 8 <= UINT32_MAX) {
3198 avio_seek(pb, mov->mdat_pos, SEEK_SET);
3199 avio_wb32(pb, mov->mdat_size + 8);
3200 } else {
3201 /* overwrite 'wide' placeholder atom */
3202 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
3203 /* special value: real atom size will be 64 bit value after
3204 * tag field */
3205 avio_wb32(pb, 1);
3206 ffio_wfourcc(pb, "mdat");
3207 avio_wb64(pb, mov->mdat_size + 16);
3209 avio_seek(pb, moov_pos, SEEK_SET);
3211 mov_write_moov_tag(pb, mov, s);
3212 } else {
3213 mov_flush_fragment(s);
3214 mov_write_mfra_tag(pb, mov);
3217 if (mov->chapter_track)
3218 av_freep(&mov->tracks[mov->chapter_track].enc);
3220 for (i=0; i<mov->nb_streams; i++) {
3221 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3222 ff_mov_close_hinting(&mov->tracks[i]);
3223 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
3224 mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
3225 int64_t off = avio_tell(pb);
3226 uint8_t buf[7];
3227 if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
3228 avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
3229 avio_write(pb, buf, 7);
3230 avio_seek(pb, off, SEEK_SET);
3233 av_freep(&mov->tracks[i].cluster);
3234 av_freep(&mov->tracks[i].frag_info);
3236 if (mov->tracks[i].vos_len)
3237 av_free(mov->tracks[i].vos_data);
3241 av_freep(&mov->tracks);
3243 return res;
3246 #if CONFIG_MOV_MUXER
3247 MOV_CLASS(mov)
3248 AVOutputFormat ff_mov_muxer = {
3249 .name = "mov",
3250 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3251 .extensions = "mov",
3252 .priv_data_size = sizeof(MOVMuxContext),
3253 .audio_codec = AV_CODEC_ID_AAC,
3254 .video_codec = CONFIG_LIBX264_ENCODER ?
3255 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3256 .write_header = mov_write_header,
3257 .write_packet = mov_write_packet,
3258 .write_trailer = mov_write_trailer,
3259 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3260 .codec_tag = (const AVCodecTag* const []){
3261 ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
3263 .priv_class = &mov_muxer_class,
3265 #endif
3266 #if CONFIG_TGP_MUXER
3267 MOV_CLASS(tgp)
3268 AVOutputFormat ff_tgp_muxer = {
3269 .name = "3gp",
3270 .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
3271 .extensions = "3gp",
3272 .priv_data_size = sizeof(MOVMuxContext),
3273 .audio_codec = AV_CODEC_ID_AMR_NB,
3274 .video_codec = AV_CODEC_ID_H263,
3275 .write_header = mov_write_header,
3276 .write_packet = mov_write_packet,
3277 .write_trailer = mov_write_trailer,
3278 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3279 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3280 .priv_class = &tgp_muxer_class,
3282 #endif
3283 #if CONFIG_MP4_MUXER
3284 MOV_CLASS(mp4)
3285 AVOutputFormat ff_mp4_muxer = {
3286 .name = "mp4",
3287 .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
3288 .mime_type = "application/mp4",
3289 .extensions = "mp4",
3290 .priv_data_size = sizeof(MOVMuxContext),
3291 .audio_codec = AV_CODEC_ID_AAC,
3292 .video_codec = CONFIG_LIBX264_ENCODER ?
3293 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3294 .write_header = mov_write_header,
3295 .write_packet = mov_write_packet,
3296 .write_trailer = mov_write_trailer,
3297 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3298 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3299 .priv_class = &mp4_muxer_class,
3301 #endif
3302 #if CONFIG_PSP_MUXER
3303 MOV_CLASS(psp)
3304 AVOutputFormat ff_psp_muxer = {
3305 .name = "psp",
3306 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
3307 .extensions = "mp4,psp",
3308 .priv_data_size = sizeof(MOVMuxContext),
3309 .audio_codec = AV_CODEC_ID_AAC,
3310 .video_codec = CONFIG_LIBX264_ENCODER ?
3311 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3312 .write_header = mov_write_header,
3313 .write_packet = mov_write_packet,
3314 .write_trailer = mov_write_trailer,
3315 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3316 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3317 .priv_class = &psp_muxer_class,
3319 #endif
3320 #if CONFIG_TG2_MUXER
3321 MOV_CLASS(tg2)
3322 AVOutputFormat ff_tg2_muxer = {
3323 .name = "3g2",
3324 .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
3325 .extensions = "3g2",
3326 .priv_data_size = sizeof(MOVMuxContext),
3327 .audio_codec = AV_CODEC_ID_AMR_NB,
3328 .video_codec = AV_CODEC_ID_H263,
3329 .write_header = mov_write_header,
3330 .write_packet = mov_write_packet,
3331 .write_trailer = mov_write_trailer,
3332 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3333 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3334 .priv_class = &tg2_muxer_class,
3336 #endif
3337 #if CONFIG_IPOD_MUXER
3338 MOV_CLASS(ipod)
3339 AVOutputFormat ff_ipod_muxer = {
3340 .name = "ipod",
3341 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
3342 .mime_type = "application/mp4",
3343 .extensions = "m4v,m4a",
3344 .priv_data_size = sizeof(MOVMuxContext),
3345 .audio_codec = AV_CODEC_ID_AAC,
3346 .video_codec = AV_CODEC_ID_H264,
3347 .write_header = mov_write_header,
3348 .write_packet = mov_write_packet,
3349 .write_trailer = mov_write_trailer,
3350 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3351 .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
3352 .priv_class = &ipod_muxer_class,
3354 #endif
3355 #if CONFIG_ISMV_MUXER
3356 MOV_CLASS(ismv)
3357 AVOutputFormat ff_ismv_muxer = {
3358 .name = "ismv",
3359 .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
3360 .mime_type = "application/mp4",
3361 .extensions = "ismv,isma",
3362 .priv_data_size = sizeof(MOVMuxContext),
3363 .audio_codec = AV_CODEC_ID_AAC,
3364 .video_codec = AV_CODEC_ID_H264,
3365 .write_header = mov_write_header,
3366 .write_packet = mov_write_packet,
3367 .write_trailer = mov_write_trailer,
3368 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
3369 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3370 .priv_class = &ismv_muxer_class,
3372 #endif