0c6b09da8ebe2331f39e53d2aaa01d5fdd82871b
[libav.git] / libavformat / movenc.c
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>
6 *
7 * This file is part of Libav.
8 *
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.
13 *
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.
18 *
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
22 */
23
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"
42
43 #undef NDEBUG
44 #include <assert.h>
45
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 { "faststart", "Run a second pass to put the index (moov atom) at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
55 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
56 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
57 { "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},
58 { "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},
59 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
60 { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
61 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
62 { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
63 { NULL },
64 };
65
66 #define MOV_CLASS(flavor)\
67 static const AVClass flavor ## _muxer_class = {\
68 .class_name = #flavor " muxer",\
69 .item_name = av_default_item_name,\
70 .option = options,\
71 .version = LIBAVUTIL_VERSION_INT,\
72 };
73
74 //FIXME support 64 bit variant with wide placeholders
75 static int64_t update_size(AVIOContext *pb, int64_t pos)
76 {
77 int64_t curpos = avio_tell(pb);
78 avio_seek(pb, pos, SEEK_SET);
79 avio_wb32(pb, curpos - pos); /* rewrite size */
80 avio_seek(pb, curpos, SEEK_SET);
81
82 return curpos - pos;
83 }
84
85 static int co64_required(const MOVTrack *track)
86 {
87 if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
88 return 1;
89 return 0;
90 }
91
92 /* Chunk offset atom */
93 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
94 {
95 int i;
96 int mode64 = co64_required(track); // use 32 bit size variant if possible
97 int64_t pos = avio_tell(pb);
98 avio_wb32(pb, 0); /* size */
99 if (mode64) {
100 ffio_wfourcc(pb, "co64");
101 } else
102 ffio_wfourcc(pb, "stco");
103 avio_wb32(pb, 0); /* version & flags */
104 avio_wb32(pb, track->entry); /* entry count */
105 for (i = 0; i < track->entry; i++) {
106 if (mode64 == 1)
107 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
108 else
109 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
110 }
111 return update_size(pb, pos);
112 }
113
114 /* Sample size atom */
115 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
116 {
117 int equalChunks = 1;
118 int i, j, entries = 0, tst = -1, oldtst = -1;
119
120 int64_t pos = avio_tell(pb);
121 avio_wb32(pb, 0); /* size */
122 ffio_wfourcc(pb, "stsz");
123 avio_wb32(pb, 0); /* version & flags */
124
125 for (i = 0; i < track->entry; i++) {
126 tst = track->cluster[i].size / track->cluster[i].entries;
127 if (oldtst != -1 && tst != oldtst)
128 equalChunks = 0;
129 oldtst = tst;
130 entries += track->cluster[i].entries;
131 }
132 if (equalChunks && track->entry) {
133 int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
134 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
135 avio_wb32(pb, sSize); // sample size
136 avio_wb32(pb, entries); // sample count
137 } else {
138 avio_wb32(pb, 0); // sample size
139 avio_wb32(pb, entries); // sample count
140 for (i = 0; i < track->entry; i++) {
141 for (j = 0; j < track->cluster[i].entries; j++) {
142 avio_wb32(pb, track->cluster[i].size /
143 track->cluster[i].entries);
144 }
145 }
146 }
147 return update_size(pb, pos);
148 }
149
150 /* Sample to chunk atom */
151 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
152 {
153 int index = 0, oldval = -1, i;
154 int64_t entryPos, curpos;
155
156 int64_t pos = avio_tell(pb);
157 avio_wb32(pb, 0); /* size */
158 ffio_wfourcc(pb, "stsc");
159 avio_wb32(pb, 0); // version & flags
160 entryPos = avio_tell(pb);
161 avio_wb32(pb, track->entry); // entry count
162 for (i = 0; i < track->entry; i++) {
163 if (oldval != track->cluster[i].samples_in_chunk) {
164 avio_wb32(pb, i + 1); // first chunk
165 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
166 avio_wb32(pb, 0x1); // sample description index
167 oldval = track->cluster[i].samples_in_chunk;
168 index++;
169 }
170 }
171 curpos = avio_tell(pb);
172 avio_seek(pb, entryPos, SEEK_SET);
173 avio_wb32(pb, index); // rewrite size
174 avio_seek(pb, curpos, SEEK_SET);
175
176 return update_size(pb, pos);
177 }
178
179 /* Sync sample atom */
180 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
181 {
182 int64_t curpos, entryPos;
183 int i, index = 0;
184 int64_t pos = avio_tell(pb);
185 avio_wb32(pb, 0); // size
186 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
187 avio_wb32(pb, 0); // version & flags
188 entryPos = avio_tell(pb);
189 avio_wb32(pb, track->entry); // entry count
190 for (i = 0; i < track->entry; i++) {
191 if (track->cluster[i].flags & flag) {
192 avio_wb32(pb, i + 1);
193 index++;
194 }
195 }
196 curpos = avio_tell(pb);
197 avio_seek(pb, entryPos, SEEK_SET);
198 avio_wb32(pb, index); // rewrite size
199 avio_seek(pb, curpos, SEEK_SET);
200 return update_size(pb, pos);
201 }
202
203 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
204 {
205 avio_wb32(pb, 0x11); /* size */
206 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
207 else ffio_wfourcc(pb, "damr");
208 ffio_wfourcc(pb, "FFMP");
209 avio_w8(pb, 0); /* decoder version */
210
211 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
212 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
213 avio_w8(pb, 0x01); /* Frames per sample */
214 return 0x11;
215 }
216
217 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
218 {
219 GetBitContext gbc;
220 PutBitContext pbc;
221 uint8_t buf[3];
222 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
223
224 if (track->vos_len < 7)
225 return -1;
226
227 avio_wb32(pb, 11);
228 ffio_wfourcc(pb, "dac3");
229
230 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
231 fscod = get_bits(&gbc, 2);
232 frmsizecod = get_bits(&gbc, 6);
233 bsid = get_bits(&gbc, 5);
234 bsmod = get_bits(&gbc, 3);
235 acmod = get_bits(&gbc, 3);
236 if (acmod == 2) {
237 skip_bits(&gbc, 2); // dsurmod
238 } else {
239 if ((acmod & 1) && acmod != 1)
240 skip_bits(&gbc, 2); // cmixlev
241 if (acmod & 4)
242 skip_bits(&gbc, 2); // surmixlev
243 }
244 lfeon = get_bits1(&gbc);
245
246 init_put_bits(&pbc, buf, sizeof(buf));
247 put_bits(&pbc, 2, fscod);
248 put_bits(&pbc, 5, bsid);
249 put_bits(&pbc, 3, bsmod);
250 put_bits(&pbc, 3, acmod);
251 put_bits(&pbc, 1, lfeon);
252 put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
253 put_bits(&pbc, 5, 0); // reserved
254
255 flush_put_bits(&pbc);
256 avio_write(pb, buf, sizeof(buf));
257
258 return 11;
259 }
260
261 /**
262 * This function writes extradata "as is".
263 * Extradata must be formatted like a valid atom (with size and tag).
264 */
265 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
266 {
267 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
268 return track->enc->extradata_size;
269 }
270
271 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
272 {
273 int i = 3;
274 avio_w8(pb, tag);
275 for (; i > 0; i--)
276 avio_w8(pb, (size >> (7 * i)) | 0x80);
277 avio_w8(pb, size & 0x7F);
278 }
279
280 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
281 {
282 int64_t pos = avio_tell(pb);
283 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
284
285 avio_wb32(pb, 0); // size
286 ffio_wfourcc(pb, "esds");
287 avio_wb32(pb, 0); // Version
288
289 // ES descriptor
290 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
291 avio_wb16(pb, track->track_id);
292 avio_w8(pb, 0x00); // flags (= no flags)
293
294 // DecoderConfig descriptor
295 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
296
297 // Object type indication
298 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
299 track->enc->codec_id == AV_CODEC_ID_MP3) &&
300 track->enc->sample_rate > 24000)
301 avio_w8(pb, 0x6B); // 11172-3
302 else
303 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
304
305 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
306 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
307 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
308 avio_w8(pb, 0x15); // flags (= Audiostream)
309 else
310 avio_w8(pb, 0x11); // flags (= Visualstream)
311
312 avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
313
314 avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
315 if (track->enc->rc_max_rate != track->enc->rc_min_rate ||
316 track->enc->rc_min_rate == 0)
317 avio_wb32(pb, 0); // vbr
318 else
319 avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
320
321 if (track->vos_len) {
322 // DecoderSpecific info descriptor
323 put_descr(pb, 0x05, track->vos_len);
324 avio_write(pb, track->vos_data, track->vos_len);
325 }
326
327 // SL descriptor
328 put_descr(pb, 0x06, 1);
329 avio_w8(pb, 0x02);
330 return update_size(pb, pos);
331 }
332
333 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
334 {
335 int64_t pos = avio_tell(pb);
336 avio_wb32(pb, 0);
337 avio_wl32(pb, track->tag); // store it byteswapped
338 track->enc->codec_tag = av_bswap16(track->tag >> 16);
339 ff_put_wav_header(pb, track->enc);
340 return update_size(pb, pos);
341 }
342
343 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
344 {
345 int64_t pos = avio_tell(pb);
346 avio_wb32(pb, 0);
347 ffio_wfourcc(pb, "wfex");
348 ff_put_wav_header(pb, track->enc);
349 return update_size(pb, pos);
350 }
351
352 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
353 {
354 uint32_t layout_tag, bitmap;
355 int64_t pos = avio_tell(pb);
356
357 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
358 track->enc->channel_layout,
359 &bitmap);
360 if (!layout_tag) {
361 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
362 "lack of channel information\n");
363 return 0;
364 }
365
366 avio_wb32(pb, 0); // Size
367 ffio_wfourcc(pb, "chan"); // Type
368 avio_w8(pb, 0); // Version
369 avio_wb24(pb, 0); // Flags
370 avio_wb32(pb, layout_tag); // mChannelLayoutTag
371 avio_wb32(pb, bitmap); // mChannelBitmap
372 avio_wb32(pb, 0); // mNumberChannelDescriptions
373
374 return update_size(pb, pos);
375 }
376
377 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
378 {
379 int64_t pos = avio_tell(pb);
380
381 avio_wb32(pb, 0); /* size */
382 ffio_wfourcc(pb, "wave");
383
384 avio_wb32(pb, 12); /* size */
385 ffio_wfourcc(pb, "frma");
386 avio_wl32(pb, track->tag);
387
388 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
389 /* useless atom needed by mplayer, ipod, not needed by quicktime */
390 avio_wb32(pb, 12); /* size */
391 ffio_wfourcc(pb, "mp4a");
392 avio_wb32(pb, 0);
393 mov_write_esds_tag(pb, track);
394 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
395 mov_write_amr_tag(pb, track);
396 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
397 mov_write_ac3_tag(pb, track);
398 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC) {
399 mov_write_extradata_tag(pb, track);
400 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
401 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
402 mov_write_ms_tag(pb, track);
403 }
404
405 avio_wb32(pb, 8); /* size */
406 avio_wb32(pb, 0); /* null tag */
407
408 return update_size(pb, pos);
409 }
410
411 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
412 {
413 uint8_t *unescaped;
414 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
415 int unescaped_size, seq_found = 0;
416 int level = 0, interlace = 0;
417 int packet_seq = track->vc1_info.packet_seq;
418 int packet_entry = track->vc1_info.packet_entry;
419 int slices = track->vc1_info.slices;
420 PutBitContext pbc;
421
422 if (track->start_dts == AV_NOPTS_VALUE) {
423 /* No packets written yet, vc1_info isn't authoritative yet. */
424 /* Assume inline sequence and entry headers. This will be
425 * overwritten at the end if the file is seekable. */
426 packet_seq = packet_entry = 1;
427 }
428
429 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
430 if (!unescaped)
431 return AVERROR(ENOMEM);
432 start = find_next_marker(track->vos_data, end);
433 for (next = start; next < end; start = next) {
434 GetBitContext gb;
435 int size;
436 next = find_next_marker(start + 4, end);
437 size = next - start - 4;
438 if (size <= 0)
439 continue;
440 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
441 init_get_bits(&gb, unescaped, 8 * unescaped_size);
442 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
443 int profile = get_bits(&gb, 2);
444 if (profile != PROFILE_ADVANCED) {
445 av_free(unescaped);
446 return AVERROR(ENOSYS);
447 }
448 seq_found = 1;
449 level = get_bits(&gb, 3);
450 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
451 * width, height */
452 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
453 skip_bits(&gb, 1); /* broadcast */
454 interlace = get_bits1(&gb);
455 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
456 }
457 }
458 if (!seq_found) {
459 av_free(unescaped);
460 return AVERROR(ENOSYS);
461 }
462
463 init_put_bits(&pbc, buf, 7);
464 /* VC1DecSpecStruc */
465 put_bits(&pbc, 4, 12); /* profile - advanced */
466 put_bits(&pbc, 3, level);
467 put_bits(&pbc, 1, 0); /* reserved */
468 /* VC1AdvDecSpecStruc */
469 put_bits(&pbc, 3, level);
470 put_bits(&pbc, 1, 0); /* cbr */
471 put_bits(&pbc, 6, 0); /* reserved */
472 put_bits(&pbc, 1, !interlace); /* no interlace */
473 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
474 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
475 put_bits(&pbc, 1, !slices); /* no slice code */
476 put_bits(&pbc, 1, 0); /* no bframe */
477 put_bits(&pbc, 1, 0); /* reserved */
478 put_bits32(&pbc, track->enc->time_base.den); /* framerate */
479 flush_put_bits(&pbc);
480
481 av_free(unescaped);
482
483 return 0;
484 }
485
486 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
487 {
488 uint8_t buf[7] = { 0 };
489 int ret;
490
491 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
492 return ret;
493
494 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
495 ffio_wfourcc(pb, "dvc1");
496 track->vc1_info.struct_offset = avio_tell(pb);
497 avio_write(pb, buf, sizeof(buf));
498 avio_write(pb, track->vos_data, track->vos_len);
499
500 return 0;
501 }
502
503 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
504 {
505 avio_wb32(pb, track->vos_len + 8);
506 ffio_wfourcc(pb, "glbl");
507 avio_write(pb, track->vos_data, track->vos_len);
508 return 8 + track->vos_len;
509 }
510
511 /**
512 * Compute flags for 'lpcm' tag.
513 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
514 */
515 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
516 {
517 switch (codec_id) {
518 case AV_CODEC_ID_PCM_F32BE:
519 case AV_CODEC_ID_PCM_F64BE:
520 return 11;
521 case AV_CODEC_ID_PCM_F32LE:
522 case AV_CODEC_ID_PCM_F64LE:
523 return 9;
524 case AV_CODEC_ID_PCM_U8:
525 return 10;
526 case AV_CODEC_ID_PCM_S16BE:
527 case AV_CODEC_ID_PCM_S24BE:
528 case AV_CODEC_ID_PCM_S32BE:
529 return 14;
530 case AV_CODEC_ID_PCM_S8:
531 case AV_CODEC_ID_PCM_S16LE:
532 case AV_CODEC_ID_PCM_S24LE:
533 case AV_CODEC_ID_PCM_S32LE:
534 return 12;
535 default:
536 return 0;
537 }
538 }
539
540 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
541 {
542 int64_t next_dts;
543
544 if (cluster_idx >= track->entry)
545 return 0;
546
547 if (cluster_idx + 1 == track->entry)
548 next_dts = track->track_duration + track->start_dts;
549 else
550 next_dts = track->cluster[cluster_idx + 1].dts;
551
552 return next_dts - track->cluster[cluster_idx].dts;
553 }
554
555 static int get_samples_per_packet(MOVTrack *track)
556 {
557 int i, first_duration;
558
559 /* use 1 for raw PCM */
560 if (!track->audio_vbr)
561 return 1;
562
563 /* check to see if duration is constant for all clusters */
564 if (!track->entry)
565 return 0;
566 first_duration = get_cluster_duration(track, 0);
567 for (i = 1; i < track->entry; i++) {
568 if (get_cluster_duration(track, i) != first_duration)
569 return 0;
570 }
571 return first_duration;
572 }
573
574 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
575 {
576 int64_t pos = avio_tell(pb);
577 int version = 0;
578 uint32_t tag = track->tag;
579
580 if (track->mode == MODE_MOV) {
581 if (mov_get_lpcm_flags(track->enc->codec_id))
582 tag = AV_RL32("lpcm");
583 version = 2;
584 }
585
586 avio_wb32(pb, 0); /* size */
587 avio_wl32(pb, tag); // store it byteswapped
588 avio_wb32(pb, 0); /* Reserved */
589 avio_wb16(pb, 0); /* Reserved */
590 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
591
592 /* SoundDescription */
593 avio_wb16(pb, version); /* Version */
594 avio_wb16(pb, 0); /* Revision level */
595 avio_wb32(pb, 0); /* Reserved */
596
597 if (version == 2) {
598 avio_wb16(pb, 3);
599 avio_wb16(pb, 16);
600 avio_wb16(pb, 0xfffe);
601 avio_wb16(pb, 0);
602 avio_wb32(pb, 0x00010000);
603 avio_wb32(pb, 72);
604 avio_wb64(pb, av_double2int(track->enc->sample_rate));
605 avio_wb32(pb, track->enc->channels);
606 avio_wb32(pb, 0x7F000000);
607 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
608 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
609 avio_wb32(pb, track->sample_size);
610 avio_wb32(pb, get_samples_per_packet(track));
611 } else {
612 /* reserved for mp4/3gp */
613 avio_wb16(pb, 2);
614 avio_wb16(pb, 16);
615 avio_wb16(pb, 0);
616
617 avio_wb16(pb, 0); /* packet size (= 0) */
618 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
619 track->enc->sample_rate : 0);
620 avio_wb16(pb, 0); /* Reserved */
621 }
622
623 if (track->mode == MODE_MOV &&
624 (track->enc->codec_id == AV_CODEC_ID_AAC ||
625 track->enc->codec_id == AV_CODEC_ID_AC3 ||
626 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
627 track->enc->codec_id == AV_CODEC_ID_ALAC ||
628 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
629 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV))
630 mov_write_wave_tag(pb, track);
631 else if (track->tag == MKTAG('m','p','4','a'))
632 mov_write_esds_tag(pb, track);
633 else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
634 mov_write_amr_tag(pb, track);
635 else if (track->enc->codec_id == AV_CODEC_ID_AC3)
636 mov_write_ac3_tag(pb, track);
637 else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
638 mov_write_extradata_tag(pb, track);
639 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
640 mov_write_wfex_tag(pb, track);
641 else if (track->vos_len > 0)
642 mov_write_glbl_tag(pb, track);
643
644 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
645 mov_write_chan_tag(pb, track);
646
647 return update_size(pb, pos);
648 }
649
650 static int mov_write_d263_tag(AVIOContext *pb)
651 {
652 avio_wb32(pb, 0xf); /* size */
653 ffio_wfourcc(pb, "d263");
654 ffio_wfourcc(pb, "FFMP");
655 avio_w8(pb, 0); /* decoder version */
656 /* FIXME use AVCodecContext level/profile, when encoder will set values */
657 avio_w8(pb, 0xa); /* level */
658 avio_w8(pb, 0); /* profile */
659 return 0xf;
660 }
661
662 /* TODO: No idea about these values */
663 static int mov_write_svq3_tag(AVIOContext *pb)
664 {
665 avio_wb32(pb, 0x15);
666 ffio_wfourcc(pb, "SMI ");
667 ffio_wfourcc(pb, "SEQH");
668 avio_wb32(pb, 0x5);
669 avio_wb32(pb, 0xe2c0211d);
670 avio_wb32(pb, 0xc0000000);
671 avio_w8(pb, 0);
672 return 0x15;
673 }
674
675 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
676 {
677 int64_t pos = avio_tell(pb);
678
679 avio_wb32(pb, 0);
680 ffio_wfourcc(pb, "avcC");
681 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
682 return update_size(pb, pos);
683 }
684
685 /* also used by all avid codecs (dv, imx, meridien) and their variants */
686 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
687 {
688 int i;
689 avio_wb32(pb, 24); /* size */
690 ffio_wfourcc(pb, "ACLR");
691 ffio_wfourcc(pb, "ACLR");
692 ffio_wfourcc(pb, "0001");
693 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
694 avio_wb32(pb, 0); /* unknown */
695
696 avio_wb32(pb, 24); /* size */
697 ffio_wfourcc(pb, "APRG");
698 ffio_wfourcc(pb, "APRG");
699 ffio_wfourcc(pb, "0001");
700 avio_wb32(pb, 1); /* unknown */
701 avio_wb32(pb, 0); /* unknown */
702
703 avio_wb32(pb, 120); /* size */
704 ffio_wfourcc(pb, "ARES");
705 ffio_wfourcc(pb, "ARES");
706 ffio_wfourcc(pb, "0001");
707 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
708 avio_wb32(pb, track->enc->width);
709 /* values below are based on samples created with quicktime and avid codecs */
710 if (track->vos_data[5] & 2) { // interlaced
711 avio_wb32(pb, track->enc->height / 2);
712 avio_wb32(pb, 2); /* unknown */
713 avio_wb32(pb, 0); /* unknown */
714 avio_wb32(pb, 4); /* unknown */
715 } else {
716 avio_wb32(pb, track->enc->height);
717 avio_wb32(pb, 1); /* unknown */
718 avio_wb32(pb, 0); /* unknown */
719 if (track->enc->height == 1080)
720 avio_wb32(pb, 5); /* unknown */
721 else
722 avio_wb32(pb, 6); /* unknown */
723 }
724 /* padding */
725 for (i = 0; i < 10; i++)
726 avio_wb64(pb, 0);
727
728 /* extra padding for stsd needed */
729 avio_wb32(pb, 0);
730 return 0;
731 }
732
733 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
734 {
735 int tag = track->enc->codec_tag;
736
737 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
738 return 0;
739
740 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
741 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
742 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
743 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
744 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
745 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
746 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
747
748 return tag;
749 }
750
751 static const AVCodecTag codec_ipod_tags[] = {
752 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
753 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
754 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
755 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
756 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
757 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
758 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
759 { AV_CODEC_ID_NONE, 0 },
760 };
761
762 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
763 {
764 int tag = track->enc->codec_tag;
765
766 // keep original tag for subs, ipod supports both formats
767 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
768 (tag == MKTAG('t', 'x', '3', 'g') ||
769 tag == MKTAG('t', 'e', 'x', 't'))))
770 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
771
772 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
773 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
774 "Quicktime/Ipod might not play the file\n");
775
776 return tag;
777 }
778
779 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
780 {
781 int tag;
782
783 if (track->enc->width == 720) /* SD */
784 if (track->enc->height == 480) /* NTSC */
785 if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
786 else tag = MKTAG('d','v','c',' ');
787 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
788 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
789 else tag = MKTAG('d','v','p','p');
790 else if (track->enc->height == 720) /* HD 720 line */
791 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
792 else tag = MKTAG('d','v','h','p');
793 else if (track->enc->height == 1080) /* HD 1080 line */
794 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
795 else tag = MKTAG('d','v','h','6');
796 else {
797 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
798 return 0;
799 }
800
801 return tag;
802 }
803
804 static const struct {
805 enum AVPixelFormat pix_fmt;
806 uint32_t tag;
807 unsigned bps;
808 } mov_pix_fmt_tags[] = {
809 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
810 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
811 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
812 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
813 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
814 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
815 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
816 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
817 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
818 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
819 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
820 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
821 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
822 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
823 };
824
825 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
826 {
827 int tag = track->enc->codec_tag;
828 int i;
829
830 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
831 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
832 tag = mov_pix_fmt_tags[i].tag;
833 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
834 break;
835 }
836 }
837
838 return tag;
839 }
840
841 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
842 {
843 int tag = track->enc->codec_tag;
844
845 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
846 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
847 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
848 track->enc->codec_id == AV_CODEC_ID_H263 ||
849 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
850 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
851 tag = mov_get_dv_codec_tag(s, track);
852 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
853 tag = mov_get_rawvideo_codec_tag(s, track);
854 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
855 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
856 if (!tag) { // if no mac fcc found, try with Microsoft tags
857 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
858 if (tag)
859 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
860 "the file may be unplayable!\n");
861 }
862 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
863 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
864 if (!tag) { // if no mac fcc found, try with Microsoft tags
865 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
866 if (ms_tag) {
867 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
868 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
869 "the file may be unplayable!\n");
870 }
871 }
872 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
873 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
874 }
875
876 return tag;
877 }
878
879 static const AVCodecTag codec_3gp_tags[] = {
880 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
881 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
882 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
883 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
884 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
885 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
886 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
887 { AV_CODEC_ID_NONE, 0 },
888 };
889
890 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
891 {
892 int tag;
893
894 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
895 tag = mp4_get_codec_tag(s, track);
896 else if (track->mode == MODE_ISM) {
897 tag = mp4_get_codec_tag(s, track);
898 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
899 tag = MKTAG('w', 'm', 'a', ' ');
900 } else if (track->mode == MODE_IPOD)
901 tag = ipod_get_codec_tag(s, track);
902 else if (track->mode & MODE_3GP)
903 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
904 else
905 tag = mov_get_codec_tag(s, track);
906
907 return tag;
908 }
909
910 /** Write uuid atom.
911 * Needed to make file play in iPods running newest firmware
912 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
913 */
914 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
915 {
916 avio_wb32(pb, 28);
917 ffio_wfourcc(pb, "uuid");
918 avio_wb32(pb, 0x6b6840f2);
919 avio_wb32(pb, 0x5f244fc5);
920 avio_wb32(pb, 0xba39a51b);
921 avio_wb32(pb, 0xcf0323f3);
922 avio_wb32(pb, 0x0);
923 return 28;
924 }
925
926 static const uint16_t fiel_data[] = {
927 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
928 };
929
930 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
931 {
932 unsigned mov_field_order = 0;
933 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
934 mov_field_order = fiel_data[track->enc->field_order];
935 else
936 return 0;
937 avio_wb32(pb, 10);
938 ffio_wfourcc(pb, "fiel");
939 avio_wb16(pb, mov_field_order);
940 return 10;
941 }
942
943 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
944 {
945 int64_t pos = avio_tell(pb);
946 avio_wb32(pb, 0); /* size */
947 avio_wl32(pb, track->tag); // store it byteswapped
948 avio_wb32(pb, 0); /* Reserved */
949 avio_wb16(pb, 0); /* Reserved */
950 avio_wb16(pb, 1); /* Data-reference index */
951
952 if (track->enc->extradata_size)
953 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
954
955 return update_size(pb, pos);
956 }
957
958 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
959 {
960 AVRational sar;
961 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
962 track->enc->sample_aspect_ratio.den, INT_MAX);
963
964 avio_wb32(pb, 16);
965 ffio_wfourcc(pb, "pasp");
966 avio_wb32(pb, sar.num);
967 avio_wb32(pb, sar.den);
968 return 16;
969 }
970
971 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
972 {
973 int64_t pos = avio_tell(pb);
974 char compressor_name[32] = { 0 };
975
976 avio_wb32(pb, 0); /* size */
977 avio_wl32(pb, track->tag); // store it byteswapped
978 avio_wb32(pb, 0); /* Reserved */
979 avio_wb16(pb, 0); /* Reserved */
980 avio_wb16(pb, 1); /* Data-reference index */
981
982 avio_wb16(pb, 0); /* Codec stream version */
983 avio_wb16(pb, 0); /* Codec stream revision (=0) */
984 if (track->mode == MODE_MOV) {
985 ffio_wfourcc(pb, "FFMP"); /* Vendor */
986 if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
987 avio_wb32(pb, 0); /* Temporal Quality */
988 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
989 } else {
990 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
991 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
992 }
993 } else {
994 avio_wb32(pb, 0); /* Reserved */
995 avio_wb32(pb, 0); /* Reserved */
996 avio_wb32(pb, 0); /* Reserved */
997 }
998 avio_wb16(pb, track->enc->width); /* Video width */
999 avio_wb16(pb, track->height); /* Video height */
1000 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1001 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1002 avio_wb32(pb, 0); /* Data size (= 0) */
1003 avio_wb16(pb, 1); /* Frame count (= 1) */
1004
1005 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1006 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
1007 av_strlcpy(compressor_name, track->enc->codec->name, 32);
1008 avio_w8(pb, strlen(compressor_name));
1009 avio_write(pb, compressor_name, 31);
1010
1011 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1012 avio_wb16(pb, track->enc->bits_per_coded_sample);
1013 else
1014 avio_wb16(pb, 0x18); /* Reserved */
1015 avio_wb16(pb, 0xffff); /* Reserved */
1016 if (track->tag == MKTAG('m','p','4','v'))
1017 mov_write_esds_tag(pb, track);
1018 else if (track->enc->codec_id == AV_CODEC_ID_H263)
1019 mov_write_d263_tag(pb);
1020 else if (track->enc->codec_id == AV_CODEC_ID_SVQ3)
1021 mov_write_svq3_tag(pb);
1022 else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
1023 mov_write_avid_tag(pb, track);
1024 else if (track->enc->codec_id == AV_CODEC_ID_H264) {
1025 mov_write_avcc_tag(pb, track);
1026 if (track->mode == MODE_IPOD)
1027 mov_write_uuid_tag_ipod(pb);
1028 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1029 mov_write_fiel_tag(pb, track);
1030 else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1031 mov_write_dvc1_tag(pb, track);
1032 else if (track->vos_len > 0)
1033 mov_write_glbl_tag(pb, track);
1034
1035 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1036 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1037 mov_write_pasp_tag(pb, track);
1038 }
1039
1040 return update_size(pb, pos);
1041 }
1042
1043 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1044 {
1045 int64_t pos = avio_tell(pb);
1046 avio_wb32(pb, 0); /* size */
1047 ffio_wfourcc(pb, "rtp ");
1048 avio_wb32(pb, 0); /* Reserved */
1049 avio_wb16(pb, 0); /* Reserved */
1050 avio_wb16(pb, 1); /* Data-reference index */
1051
1052 avio_wb16(pb, 1); /* Hint track version */
1053 avio_wb16(pb, 1); /* Highest compatible version */
1054 avio_wb32(pb, track->max_packet_size); /* Max packet size */
1055
1056 avio_wb32(pb, 12); /* size */
1057 ffio_wfourcc(pb, "tims");
1058 avio_wb32(pb, track->timescale);
1059
1060 return update_size(pb, pos);
1061 }
1062
1063 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1064 {
1065 int64_t pos = avio_tell(pb);
1066 avio_wb32(pb, 0); /* size */
1067 ffio_wfourcc(pb, "stsd");
1068 avio_wb32(pb, 0); /* version & flags */
1069 avio_wb32(pb, 1); /* entry count */
1070 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1071 mov_write_video_tag(pb, track);
1072 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1073 mov_write_audio_tag(pb, track);
1074 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1075 mov_write_subtitle_tag(pb, track);
1076 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1077 mov_write_rtp_tag(pb, track);
1078 return update_size(pb, pos);
1079 }
1080
1081 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1082 {
1083 MOVStts *ctts_entries;
1084 uint32_t entries = 0;
1085 uint32_t atom_size;
1086 int i;
1087
1088 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1089 ctts_entries[0].count = 1;
1090 ctts_entries[0].duration = track->cluster[0].cts;
1091 for (i = 1; i < track->entry; i++) {
1092 if (track->cluster[i].cts == ctts_entries[entries].duration) {
1093 ctts_entries[entries].count++; /* compress */
1094 } else {
1095 entries++;
1096 ctts_entries[entries].duration = track->cluster[i].cts;
1097 ctts_entries[entries].count = 1;
1098 }
1099 }
1100 entries++; /* last one */
1101 atom_size = 16 + (entries * 8);
1102 avio_wb32(pb, atom_size); /* size */
1103 ffio_wfourcc(pb, "ctts");
1104 avio_wb32(pb, 0); /* version & flags */
1105 avio_wb32(pb, entries); /* entry count */
1106 for (i = 0; i < entries; i++) {
1107 avio_wb32(pb, ctts_entries[i].count);
1108 avio_wb32(pb, ctts_entries[i].duration);
1109 }
1110 av_free(ctts_entries);
1111 return atom_size;
1112 }
1113
1114 /* Time to sample atom */
1115 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1116 {
1117 MOVStts *stts_entries;
1118 uint32_t entries = -1;
1119 uint32_t atom_size;
1120 int i;
1121
1122 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1123 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1124 stts_entries[0].count = track->sample_count;
1125 stts_entries[0].duration = 1;
1126 entries = 1;
1127 } else {
1128 stts_entries = track->entry ?
1129 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1130 NULL;
1131 for (i = 0; i < track->entry; i++) {
1132 int duration = get_cluster_duration(track, i);
1133 if (i && duration == stts_entries[entries].duration) {
1134 stts_entries[entries].count++; /* compress */
1135 } else {
1136 entries++;
1137 stts_entries[entries].duration = duration;
1138 stts_entries[entries].count = 1;
1139 }
1140 }
1141 entries++; /* last one */
1142 }
1143 atom_size = 16 + (entries * 8);
1144 avio_wb32(pb, atom_size); /* size */
1145 ffio_wfourcc(pb, "stts");
1146 avio_wb32(pb, 0); /* version & flags */
1147 avio_wb32(pb, entries); /* entry count */
1148 for (i = 0; i < entries; i++) {
1149 avio_wb32(pb, stts_entries[i].count);
1150 avio_wb32(pb, stts_entries[i].duration);
1151 }
1152 av_free(stts_entries);
1153 return atom_size;
1154 }
1155
1156 static int mov_write_dref_tag(AVIOContext *pb)
1157 {
1158 avio_wb32(pb, 28); /* size */
1159 ffio_wfourcc(pb, "dref");
1160 avio_wb32(pb, 0); /* version & flags */
1161 avio_wb32(pb, 1); /* entry count */
1162
1163 avio_wb32(pb, 0xc); /* size */
1164 ffio_wfourcc(pb, "url ");
1165 avio_wb32(pb, 1); /* version & flags */
1166
1167 return 28;
1168 }
1169
1170 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1171 {
1172 int64_t pos = avio_tell(pb);
1173 avio_wb32(pb, 0); /* size */
1174 ffio_wfourcc(pb, "stbl");
1175 mov_write_stsd_tag(pb, track);
1176 mov_write_stts_tag(pb, track);
1177 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1178 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1179 track->has_keyframes && track->has_keyframes < track->entry)
1180 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1181 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1182 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1183 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1184 track->flags & MOV_TRACK_CTTS)
1185 mov_write_ctts_tag(pb, track);
1186 mov_write_stsc_tag(pb, track);
1187 mov_write_stsz_tag(pb, track);
1188 mov_write_stco_tag(pb, track);
1189 return update_size(pb, pos);
1190 }
1191
1192 static int mov_write_dinf_tag(AVIOContext *pb)
1193 {
1194 int64_t pos = avio_tell(pb);
1195 avio_wb32(pb, 0); /* size */
1196 ffio_wfourcc(pb, "dinf");
1197 mov_write_dref_tag(pb);
1198 return update_size(pb, pos);
1199 }
1200
1201 static int mov_write_nmhd_tag(AVIOContext *pb)
1202 {
1203 avio_wb32(pb, 12);
1204 ffio_wfourcc(pb, "nmhd");
1205 avio_wb32(pb, 0);
1206 return 12;
1207 }
1208
1209 static int mov_write_gmhd_tag(AVIOContext *pb)
1210 {
1211 avio_wb32(pb, 0x20); /* size */
1212 ffio_wfourcc(pb, "gmhd");
1213 avio_wb32(pb, 0x18); /* gmin size */
1214 ffio_wfourcc(pb, "gmin");/* generic media info */
1215 avio_wb32(pb, 0); /* version & flags */
1216 avio_wb16(pb, 0x40); /* graphics mode = */
1217 avio_wb16(pb, 0x8000); /* opColor (r?) */
1218 avio_wb16(pb, 0x8000); /* opColor (g?) */
1219 avio_wb16(pb, 0x8000); /* opColor (b?) */
1220 avio_wb16(pb, 0); /* balance */
1221 avio_wb16(pb, 0); /* reserved */
1222 return 0x20;
1223 }
1224
1225 static int mov_write_smhd_tag(AVIOContext *pb)
1226 {
1227 avio_wb32(pb, 16); /* size */
1228 ffio_wfourcc(pb, "smhd");
1229 avio_wb32(pb, 0); /* version & flags */
1230 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1231 avio_wb16(pb, 0); /* reserved */
1232 return 16;
1233 }
1234
1235 static int mov_write_vmhd_tag(AVIOContext *pb)
1236 {
1237 avio_wb32(pb, 0x14); /* size (always 0x14) */
1238 ffio_wfourcc(pb, "vmhd");
1239 avio_wb32(pb, 0x01); /* version & flags */
1240 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1241 return 0x14;
1242 }
1243
1244 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1245 {
1246 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1247 int64_t pos = avio_tell(pb);
1248
1249 hdlr = "dhlr";
1250 hdlr_type = "url ";
1251 descr = "DataHandler";
1252
1253 if (track) {
1254 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1255 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1256 hdlr_type = "vide";
1257 descr = "VideoHandler";
1258 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1259 hdlr_type = "soun";
1260 descr = "SoundHandler";
1261 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1262 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1263 else hdlr_type = "text";
1264 descr = "SubtitleHandler";
1265 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1266 hdlr_type = "hint";
1267 descr = "HintHandler";
1268 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1269 hdlr_type = "tmcd";
1270 descr = "TimeCodeHandler";
1271 } else {
1272 char tag_buf[32];
1273 av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1274 track->enc->codec_tag);
1275
1276 av_log(track->enc, AV_LOG_WARNING,
1277 "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1278 tag_buf, track->enc->codec_tag);
1279 }
1280 }
1281
1282 avio_wb32(pb, 0); /* size */
1283 ffio_wfourcc(pb, "hdlr");
1284 avio_wb32(pb, 0); /* Version & flags */
1285 avio_write(pb, hdlr, 4); /* handler */
1286 ffio_wfourcc(pb, hdlr_type); /* handler type */
1287 avio_wb32(pb, 0); /* reserved */
1288 avio_wb32(pb, 0); /* reserved */
1289 avio_wb32(pb, 0); /* reserved */
1290 if (!track || track->mode == MODE_MOV)
1291 avio_w8(pb, strlen(descr)); /* pascal string */
1292 avio_write(pb, descr, strlen(descr)); /* handler description */
1293 if (track && track->mode != MODE_MOV)
1294 avio_w8(pb, 0); /* c string */
1295 return update_size(pb, pos);
1296 }
1297
1298 static int mov_write_hmhd_tag(AVIOContext *pb)
1299 {
1300 /* This atom must be present, but leaving the values at zero
1301 * seems harmless. */
1302 avio_wb32(pb, 28); /* size */
1303 ffio_wfourcc(pb, "hmhd");
1304 avio_wb32(pb, 0); /* version, flags */
1305 avio_wb16(pb, 0); /* maxPDUsize */
1306 avio_wb16(pb, 0); /* avgPDUsize */
1307 avio_wb32(pb, 0); /* maxbitrate */
1308 avio_wb32(pb, 0); /* avgbitrate */
1309 avio_wb32(pb, 0); /* reserved */
1310 return 28;
1311 }
1312
1313 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1314 {
1315 int64_t pos = avio_tell(pb);
1316 avio_wb32(pb, 0); /* size */
1317 ffio_wfourcc(pb, "minf");
1318 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1319 mov_write_vmhd_tag(pb);
1320 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1321 mov_write_smhd_tag(pb);
1322 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1323 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1324 else mov_write_nmhd_tag(pb);
1325 } else if (track->tag == MKTAG('r','t','p',' ')) {
1326 mov_write_hmhd_tag(pb);
1327 }
1328 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1329 mov_write_hdlr_tag(pb, NULL);
1330 mov_write_dinf_tag(pb);
1331 mov_write_stbl_tag(pb, track);
1332 return update_size(pb, pos);
1333 }
1334
1335 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1336 {
1337 int version = track->track_duration < INT32_MAX ? 0 : 1;
1338
1339 if (track->mode == MODE_ISM)
1340 version = 1;
1341
1342 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1343 ffio_wfourcc(pb, "mdhd");
1344 avio_w8(pb, version);
1345 avio_wb24(pb, 0); /* flags */
1346 if (version == 1) {
1347 avio_wb64(pb, track->time);
1348 avio_wb64(pb, track->time);
1349 } else {
1350 avio_wb32(pb, track->time); /* creation time */
1351 avio_wb32(pb, track->time); /* modification time */
1352 }
1353 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1354 if (!track->entry)
1355 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1356 else
1357 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1358 avio_wb16(pb, track->language); /* language */
1359 avio_wb16(pb, 0); /* reserved (quality) */
1360
1361 if (version != 0 && track->mode == MODE_MOV) {
1362 av_log(NULL, AV_LOG_ERROR,
1363 "FATAL error, file duration too long for timebase, this file will not be\n"
1364 "playable with quicktime. Choose a different timebase or a different\n"
1365 "container format\n");
1366 }
1367
1368 return 32;
1369 }
1370
1371 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1372 {
1373 int64_t pos = avio_tell(pb);
1374 avio_wb32(pb, 0); /* size */
1375 ffio_wfourcc(pb, "mdia");
1376 mov_write_mdhd_tag(pb, track);
1377 mov_write_hdlr_tag(pb, track);
1378 mov_write_minf_tag(pb, track);
1379 return update_size(pb, pos);
1380 }
1381
1382 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1383 {
1384 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1385 track->timescale, AV_ROUND_UP);
1386 int version = duration < INT32_MAX ? 0 : 1;
1387
1388 if (track->mode == MODE_ISM)
1389 version = 1;
1390
1391 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1392 ffio_wfourcc(pb, "tkhd");
1393 avio_w8(pb, version);
1394 avio_wb24(pb, 0xf); /* flags (track enabled) */
1395 if (version == 1) {
1396 avio_wb64(pb, track->time);
1397 avio_wb64(pb, track->time);
1398 } else {
1399 avio_wb32(pb, track->time); /* creation time */
1400 avio_wb32(pb, track->time); /* modification time */
1401 }
1402 avio_wb32(pb, track->track_id); /* track-id */
1403 avio_wb32(pb, 0); /* reserved */
1404 if (!track->entry)
1405 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1406 else
1407 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1408
1409 avio_wb32(pb, 0); /* reserved */
1410 avio_wb32(pb, 0); /* reserved */
1411 avio_wb16(pb, 0); /* layer */
1412 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1413 /* Volume, only for audio */
1414 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1415 avio_wb16(pb, 0x0100);
1416 else
1417 avio_wb16(pb, 0);
1418 avio_wb16(pb, 0); /* reserved */
1419
1420 /* Matrix structure */
1421 avio_wb32(pb, 0x00010000); /* reserved */
1422 avio_wb32(pb, 0x0); /* reserved */
1423 avio_wb32(pb, 0x0); /* reserved */
1424 avio_wb32(pb, 0x0); /* reserved */
1425 avio_wb32(pb, 0x00010000); /* reserved */
1426 avio_wb32(pb, 0x0); /* reserved */
1427 avio_wb32(pb, 0x0); /* reserved */
1428 avio_wb32(pb, 0x0); /* reserved */
1429 avio_wb32(pb, 0x40000000); /* reserved */
1430
1431 /* Track width and height, for visual only */
1432 if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1433 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1434 if (track->mode == MODE_MOV) {
1435 avio_wb32(pb, track->enc->width << 16);
1436 avio_wb32(pb, track->height << 16);
1437 } else {
1438 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1439 if (!sample_aspect_ratio || track->height != track->enc->height)
1440 sample_aspect_ratio = 1;
1441 avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1442 avio_wb32(pb, track->height * 0x10000);
1443 }
1444 } else {
1445 avio_wb32(pb, 0);
1446 avio_wb32(pb, 0);
1447 }
1448 return 0x5c;
1449 }
1450
1451 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1452 {
1453 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1454 track->enc->sample_aspect_ratio.den);
1455
1456 int64_t pos = avio_tell(pb);
1457
1458 avio_wb32(pb, 0); /* size */
1459 ffio_wfourcc(pb, "tapt");
1460
1461 avio_wb32(pb, 20);
1462 ffio_wfourcc(pb, "clef");
1463 avio_wb32(pb, 0);
1464 avio_wb32(pb, width << 16);
1465 avio_wb32(pb, track->enc->height << 16);
1466
1467 avio_wb32(pb, 20);
1468 ffio_wfourcc(pb, "enof");
1469 avio_wb32(pb, 0);
1470 avio_wb32(pb, track->enc->width << 16);
1471 avio_wb32(pb, track->enc->height << 16);
1472
1473 return update_size(pb, pos);
1474 }
1475
1476 // This box seems important for the psp playback ... without it the movie seems to hang
1477 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1478 {
1479 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
1480 track->timescale, AV_ROUND_UP);
1481 int version = duration < INT32_MAX ? 0 : 1;
1482 int entry_size, entry_count, size;
1483 int64_t delay, start_ct = track->cluster[0].cts;
1484 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1485 track->timescale, AV_ROUND_DOWN);
1486 version |= delay < INT32_MAX ? 0 : 1;
1487
1488 entry_size = (version == 1) ? 20 : 12;
1489 entry_count = 1 + (delay > 0);
1490 size = 24 + entry_count * entry_size;
1491
1492 /* write the atom data */
1493 avio_wb32(pb, size);
1494 ffio_wfourcc(pb, "edts");
1495 avio_wb32(pb, size - 8);
1496 ffio_wfourcc(pb, "elst");
1497 avio_w8(pb, version);
1498 avio_wb24(pb, 0); /* flags */
1499
1500 avio_wb32(pb, entry_count);
1501 if (delay > 0) { /* add an empty edit to delay presentation */
1502 if (version == 1) {
1503 avio_wb64(pb, delay);
1504 avio_wb64(pb, -1);
1505 } else {
1506 avio_wb32(pb, delay);
1507 avio_wb32(pb, -1);
1508 }
1509 avio_wb32(pb, 0x00010000);
1510 }
1511
1512 /* duration */
1513 if (version == 1) {
1514 avio_wb64(pb, duration);
1515 avio_wb64(pb, start_ct);
1516 } else {
1517 avio_wb32(pb, duration);
1518 avio_wb32(pb, start_ct);
1519 }
1520 avio_wb32(pb, 0x00010000);
1521 return size;
1522 }
1523
1524 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1525 {
1526 avio_wb32(pb, 20); // size
1527 ffio_wfourcc(pb, "tref");
1528 avio_wb32(pb, 12); // size (subatom)
1529 avio_wl32(pb, track->tref_tag);
1530 avio_wb32(pb, track->tref_id);
1531 return 20;
1532 }
1533
1534 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1535 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1536 {
1537 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1538 ffio_wfourcc(pb, "uuid");
1539 ffio_wfourcc(pb, "USMT");
1540 avio_wb32(pb, 0x21d24fce);
1541 avio_wb32(pb, 0xbb88695c);
1542 avio_wb32(pb, 0xfac9c740);
1543 avio_wb32(pb, 0x1c); // another size here!
1544 ffio_wfourcc(pb, "MTDT");
1545 avio_wb32(pb, 0x00010012);
1546 avio_wb32(pb, 0x0a);
1547 avio_wb32(pb, 0x55c40000);
1548 avio_wb32(pb, 0x1);
1549 avio_wb32(pb, 0x0);
1550 return 0x34;
1551 }
1552
1553 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1554 {
1555 AVFormatContext *ctx = track->rtp_ctx;
1556 char buf[1000] = "";
1557 int len;
1558
1559 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1560 NULL, NULL, 0, 0, ctx);
1561 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1562 len = strlen(buf);
1563
1564 avio_wb32(pb, len + 24);
1565 ffio_wfourcc(pb, "udta");
1566 avio_wb32(pb, len + 16);
1567 ffio_wfourcc(pb, "hnti");
1568 avio_wb32(pb, len + 8);
1569 ffio_wfourcc(pb, "sdp ");
1570 avio_write(pb, buf, len);
1571 return len + 24;
1572 }
1573
1574 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1575 MOVTrack *track, AVStream *st)
1576 {
1577 int64_t pos = avio_tell(pb);
1578 avio_wb32(pb, 0); /* size */
1579 ffio_wfourcc(pb, "trak");
1580 mov_write_tkhd_tag(pb, track, st);
1581 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1582 (track->entry && track->cluster[0].dts)) {
1583 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1584 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1585 }
1586 if (track->tref_tag)
1587 mov_write_tref_tag(pb, track);
1588 mov_write_mdia_tag(pb, track);
1589 if (track->mode == MODE_PSP)
1590 mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
1591 if (track->tag == MKTAG('r','t','p',' '))
1592 mov_write_udta_sdp(pb, track);
1593 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1594 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1595 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1596 mov_write_tapt_tag(pb, track);
1597 }
1598 return update_size(pb, pos);
1599 }
1600
1601 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1602 {
1603 int i, has_audio = 0, has_video = 0;
1604 int64_t pos = avio_tell(pb);
1605 int audio_profile = mov->iods_audio_profile;
1606 int video_profile = mov->iods_video_profile;
1607 for (i = 0; i < mov->nb_streams; i++) {
1608 if (mov->tracks[i].entry > 0) {
1609 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1610 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1611 }
1612 }
1613 if (audio_profile < 0)
1614 audio_profile = 0xFF - has_audio;
1615 if (video_profile < 0)
1616 video_profile = 0xFF - has_video;
1617 avio_wb32(pb, 0x0); /* size */
1618 ffio_wfourcc(pb, "iods");
1619 avio_wb32(pb, 0); /* version & flags */
1620 put_descr(pb, 0x10, 7);
1621 avio_wb16(pb, 0x004f);
1622 avio_w8(pb, 0xff);
1623 avio_w8(pb, 0xff);
1624 avio_w8(pb, audio_profile);
1625 avio_w8(pb, video_profile);
1626 avio_w8(pb, 0xff);
1627 return update_size(pb, pos);
1628 }
1629
1630 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1631 {
1632 avio_wb32(pb, 0x20); /* size */
1633 ffio_wfourcc(pb, "trex");
1634 avio_wb32(pb, 0); /* version & flags */
1635 avio_wb32(pb, track->track_id); /* track ID */
1636 avio_wb32(pb, 1); /* default sample description index */
1637 avio_wb32(pb, 0); /* default sample duration */
1638 avio_wb32(pb, 0); /* default sample size */
1639 avio_wb32(pb, 0); /* default sample flags */
1640 return 0;
1641 }
1642
1643 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1644 {
1645 int64_t pos = avio_tell(pb);
1646 int i;
1647 avio_wb32(pb, 0x0); /* size */
1648 ffio_wfourcc(pb, "mvex");
1649 for (i = 0; i < mov->nb_streams; i++)
1650 mov_write_trex_tag(pb, &mov->tracks[i]);
1651 return update_size(pb, pos);
1652 }
1653
1654 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1655 {
1656 int max_track_id = 1, i;
1657 int64_t max_track_len_temp, max_track_len = 0;
1658 int version;
1659
1660 for (i = 0; i < mov->nb_streams; i++) {
1661 if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
1662 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1663 MOV_TIMESCALE,
1664 mov->tracks[i].timescale,
1665 AV_ROUND_UP);
1666 if (max_track_len < max_track_len_temp)
1667 max_track_len = max_track_len_temp;
1668 if (max_track_id < mov->tracks[i].track_id)
1669 max_track_id = mov->tracks[i].track_id;
1670 }
1671 }
1672
1673 version = max_track_len < UINT32_MAX ? 0 : 1;
1674 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1675 ffio_wfourcc(pb, "mvhd");
1676 avio_w8(pb, version);
1677 avio_wb24(pb, 0); /* flags */
1678 if (version == 1) {
1679 avio_wb64(pb, mov->time);
1680 avio_wb64(pb, mov->time);
1681 } else {
1682 avio_wb32(pb, mov->time); /* creation time */
1683 avio_wb32(pb, mov->time); /* modification time */
1684 }
1685 avio_wb32(pb, MOV_TIMESCALE);
1686 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1687
1688 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1689 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1690 avio_wb16(pb, 0); /* reserved */
1691 avio_wb32(pb, 0); /* reserved */
1692 avio_wb32(pb, 0); /* reserved */
1693
1694 /* Matrix structure */
1695 avio_wb32(pb, 0x00010000); /* reserved */
1696 avio_wb32(pb, 0x0); /* reserved */
1697 avio_wb32(pb, 0x0); /* reserved */
1698 avio_wb32(pb, 0x0); /* reserved */
1699 avio_wb32(pb, 0x00010000); /* reserved */
1700 avio_wb32(pb, 0x0); /* reserved */
1701 avio_wb32(pb, 0x0); /* reserved */
1702 avio_wb32(pb, 0x0); /* reserved */
1703 avio_wb32(pb, 0x40000000); /* reserved */
1704
1705 avio_wb32(pb, 0); /* reserved (preview time) */
1706 avio_wb32(pb, 0); /* reserved (preview duration) */
1707 avio_wb32(pb, 0); /* reserved (poster time) */
1708 avio_wb32(pb, 0); /* reserved (selection time) */
1709 avio_wb32(pb, 0); /* reserved (selection duration) */
1710 avio_wb32(pb, 0); /* reserved (current time) */
1711 avio_wb32(pb, max_track_id + 1); /* Next track id */
1712 return 0x6c;
1713 }
1714
1715 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1716 AVFormatContext *s)
1717 {
1718 avio_wb32(pb, 33); /* size */
1719 ffio_wfourcc(pb, "hdlr");
1720 avio_wb32(pb, 0);
1721 avio_wb32(pb, 0);
1722 ffio_wfourcc(pb, "mdir");
1723 ffio_wfourcc(pb, "appl");
1724 avio_wb32(pb, 0);
1725 avio_wb32(pb, 0);
1726 avio_w8(pb, 0);
1727 return 33;
1728 }
1729
1730 /* helper function to write a data tag with the specified string as data */
1731 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1732 {
1733 if (long_style) {
1734 int size = 16 + strlen(data);
1735 avio_wb32(pb, size); /* size */
1736 ffio_wfourcc(pb, "data");
1737 avio_wb32(pb, 1);
1738 avio_wb32(pb, 0);
1739 avio_write(pb, data, strlen(data));
1740 return size;
1741 } else {
1742 if (!lang)
1743 lang = ff_mov_iso639_to_lang("und", 1);
1744 avio_wb16(pb, strlen(data)); /* string length */
1745 avio_wb16(pb, lang);
1746 avio_write(pb, data, strlen(data));
1747 return strlen(data) + 4;
1748 }
1749 }
1750
1751 static int mov_write_string_tag(AVIOContext *pb, const char *name,
1752 const char *value, int lang, int long_style)
1753 {
1754 int size = 0;
1755 if (value && value[0]) {
1756 int64_t pos = avio_tell(pb);
1757 avio_wb32(pb, 0); /* size */
1758 ffio_wfourcc(pb, name);
1759 mov_write_string_data_tag(pb, value, lang, long_style);
1760 size = update_size(pb, pos);
1761 }
1762 return size;
1763 }
1764
1765 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1766 const char *name, const char *tag,
1767 int long_style)
1768 {
1769 int l, lang = 0, len, len2;
1770 AVDictionaryEntry *t, *t2 = NULL;
1771 char tag2[16];
1772
1773 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1774 return 0;
1775
1776 len = strlen(t->key);
1777 snprintf(tag2, sizeof(tag2), "%s-", tag);
1778 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1779 len2 = strlen(t2->key);
1780 if (len2 == len + 4 && !strcmp(t->value, t2->value)
1781 && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
1782 lang = l;
1783 break;
1784 }
1785 }
1786 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1787 }
1788
1789 /* iTunes track number */
1790 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1791 AVFormatContext *s)
1792 {
1793 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1794 int size = 0, track = t ? atoi(t->value) : 0;
1795 if (track) {
1796 avio_wb32(pb, 32); /* size */
1797 ffio_wfourcc(pb, "trkn");
1798 avio_wb32(pb, 24); /* size */
1799 ffio_wfourcc(pb, "data");
1800 avio_wb32(pb, 0); // 8 bytes empty
1801 avio_wb32(pb, 0);
1802 avio_wb16(pb, 0); // empty
1803 avio_wb16(pb, track); // track number
1804 avio_wb16(pb, 0); // total track number
1805 avio_wb16(pb, 0); // empty
1806 size = 32;
1807 }
1808 return size;
1809 }
1810
1811 /* iTunes meta data list */
1812 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1813 AVFormatContext *s)
1814 {
1815 int64_t pos = avio_tell(pb);
1816 avio_wb32(pb, 0); /* size */
1817 ffio_wfourcc(pb, "ilst");
1818 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1819 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1820 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1821 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1822 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1823 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1824 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1825 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1826 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1827 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1828 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1829 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1830 mov_write_string_metadata(s, pb, "desc", "description",1);
1831 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1832 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1833 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1834 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1835 mov_write_trkn_tag(pb, mov, s);
1836 return update_size(pb, pos);
1837 }
1838
1839 /* iTunes meta data tag */
1840 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1841 AVFormatContext *s)
1842 {
1843 int size = 0;
1844 int64_t pos = avio_tell(pb);
1845 avio_wb32(pb, 0); /* size */
1846 ffio_wfourcc(pb, "meta");
1847 avio_wb32(pb, 0);
1848 mov_write_itunes_hdlr_tag(pb, mov, s);
1849 mov_write_ilst_tag(pb, mov, s);
1850 size = update_size(pb, pos);
1851 return size;
1852 }
1853
1854 static int utf8len(const uint8_t *b)
1855 {
1856 int len = 0;
1857 int val;
1858 while (*b) {
1859 GET_UTF8(val, *b++, return -1;)
1860 len++;
1861 }
1862 return len;
1863 }
1864
1865 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1866 {
1867 int val;
1868 while (*b) {
1869 GET_UTF8(val, *b++, return -1;)
1870 avio_wb16(pb, val);
1871 }
1872 avio_wb16(pb, 0x00);
1873 return 0;
1874 }
1875
1876 static uint16_t language_code(const char *str)
1877 {
1878 return (((str[0] - 0x60) & 0x1F) << 10) +
1879 (((str[1] - 0x60) & 0x1F) << 5) +
1880 (( str[2] - 0x60) & 0x1F);
1881 }
1882
1883 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1884 const char *tag, const char *str)
1885 {
1886 int64_t pos = avio_tell(pb);
1887 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1888 if (!t || !utf8len(t->value))
1889 return 0;
1890 avio_wb32(pb, 0); /* size */
1891 ffio_wfourcc(pb, tag); /* type */
1892 avio_wb32(pb, 0); /* version + flags */
1893 if (!strcmp(tag, "yrrc"))
1894 avio_wb16(pb, atoi(t->value));
1895 else {
1896 avio_wb16(pb, language_code("eng")); /* language */
1897 avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
1898 if (!strcmp(tag, "albm") &&
1899 (t = av_dict_get(s->metadata, "track", NULL, 0)))
1900 avio_w8(pb, atoi(t->value));
1901 }
1902 return update_size(pb, pos);
1903 }
1904
1905 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1906 {
1907 int64_t pos = avio_tell(pb);
1908 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1909
1910 avio_wb32(pb, 0); // size
1911 ffio_wfourcc(pb, "chpl");
1912 avio_wb32(pb, 0x01000000); // version + flags
1913 avio_wb32(pb, 0); // unknown
1914 avio_w8(pb, nb_chapters);
1915
1916 for (i = 0; i < nb_chapters; i++) {
1917 AVChapter *c = s->chapters[i];
1918 AVDictionaryEntry *t;
1919 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1920
1921 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1922 int len = FFMIN(strlen(t->value), 255);
1923 avio_w8(pb, len);
1924 avio_write(pb, t->value, len);
1925 } else
1926 avio_w8(pb, 0);
1927 }
1928 return update_size(pb, pos);
1929 }
1930
1931 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1932 AVFormatContext *s)
1933 {
1934 AVIOContext *pb_buf;
1935 int i, ret, size;
1936 uint8_t *buf;
1937
1938 for (i = 0; i < s->nb_streams; i++)
1939 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1940 return 0;
1941 }
1942
1943 ret = avio_open_dyn_buf(&pb_buf);
1944 if (ret < 0)
1945 return ret;
1946
1947 if (mov->mode & MODE_3GP) {
1948 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1949 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1950 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1951 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1952 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1953 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1954 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1955 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1956 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1957 mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
1958 mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
1959 mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
1960 mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
1961 mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
1962 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
1963 mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
1964 mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
1965 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
1966 } else {
1967 /* iTunes meta data */
1968 mov_write_meta_tag(pb_buf, mov, s);
1969 }
1970
1971 if (s->nb_chapters)
1972 mov_write_chpl_tag(pb_buf, s);
1973
1974 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1975 avio_wb32(pb, size + 8);
1976 ffio_wfourcc(pb, "udta");
1977 avio_write(pb, buf, size);
1978 }
1979 av_free(buf);
1980
1981 return 0;
1982 }
1983
1984 static void mov_write_psp_udta_tag(AVIOContext *pb,
1985 const char *str, const char *lang, int type)
1986 {
1987 int len = utf8len(str) + 1;
1988 if (len <= 0)
1989 return;
1990 avio_wb16(pb, len * 2 + 10); /* size */
1991 avio_wb32(pb, type); /* type */
1992 avio_wb16(pb, language_code(lang)); /* language */
1993 avio_wb16(pb, 0x01); /* ? */
1994 ascii_to_wc(pb, str);
1995 }
1996
1997 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1998 {
1999 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2000 int64_t pos, pos2;
2001
2002 if (title) {
2003 pos = avio_tell(pb);
2004 avio_wb32(pb, 0); /* size placeholder*/
2005 ffio_wfourcc(pb, "uuid");
2006 ffio_wfourcc(pb, "USMT");
2007 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2008 avio_wb32(pb, 0xbb88695c);
2009 avio_wb32(pb, 0xfac9c740);
2010
2011 pos2 = avio_tell(pb);
2012 avio_wb32(pb, 0); /* size placeholder*/
2013 ffio_wfourcc(pb, "MTDT");
2014 avio_wb16(pb, 4);
2015
2016 // ?
2017 avio_wb16(pb, 0x0C); /* size */
2018 avio_wb32(pb, 0x0B); /* type */
2019 avio_wb16(pb, language_code("und")); /* language */
2020 avio_wb16(pb, 0x0); /* ? */
2021 avio_wb16(pb, 0x021C); /* data */
2022
2023 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2024 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2025 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2026
2027 update_size(pb, pos2);
2028 return update_size(pb, pos);
2029 }
2030
2031 return 0;
2032 }
2033
2034 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2035 AVFormatContext *s)
2036 {
2037 int i;
2038 int64_t pos = avio_tell(pb);
2039 avio_wb32(pb, 0); /* size placeholder*/
2040 ffio_wfourcc(pb, "moov");
2041
2042 for (i = 0; i < mov->nb_streams; i++) {
2043 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2044 continue;
2045
2046 mov->tracks[i].time = mov->time;
2047 mov->tracks[i].track_id = i + 1;
2048 }
2049
2050 if (mov->chapter_track)
2051 for (i = 0; i < s->nb_streams; i++) {
2052 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2053 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2054 }
2055 for (i = 0; i < mov->nb_streams; i++) {
2056 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2057 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2058 mov->tracks[i].tref_id =
2059 mov->tracks[mov->tracks[i].src_track].track_id;
2060 }
2061 }
2062
2063 mov_write_mvhd_tag(pb, mov);
2064 if (mov->mode != MODE_MOV && !mov->iods_skip)
2065 mov_write_iods_tag(pb, mov);
2066 for (i = 0; i < mov->nb_streams; i++) {
2067 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2068 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2069 }
2070 }
2071 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2072 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2073
2074 if (mov->mode == MODE_PSP)
2075 mov_write_uuidusmt_tag(pb, s);
2076 else
2077 mov_write_udta_tag(pb, mov, s);
2078
2079 return update_size(pb, pos);
2080 }
2081
2082 static void param_write_int(AVIOContext *pb, const char *name, int value)
2083 {
2084 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2085 }
2086
2087 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2088 {
2089 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2090 }
2091
2092 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2093 {
2094 char buf[150];
2095 len = FFMIN(sizeof(buf) / 2 - 1, len);
2096 ff_data_to_hex(buf, value, len, 0);
2097 buf[2 * len] = '\0';
2098 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2099 }
2100
2101 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2102 {
2103 int64_t pos = avio_tell(pb);
2104 int i;
2105 const uint8_t uuid[] = {
2106 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2107 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2108 };
2109
2110 avio_wb32(pb, 0);
2111 ffio_wfourcc(pb, "uuid");
2112 avio_write(pb, uuid, sizeof(uuid));
2113 avio_wb32(pb, 0);
2114
2115 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2116 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2117 avio_printf(pb, "<head>\n");
2118 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2119 LIBAVFORMAT_IDENT);
2120 avio_printf(pb, "</head>\n");
2121 avio_printf(pb, "<body>\n");
2122 avio_printf(pb, "<switch>\n");
2123 for (i = 0; i < mov->nb_streams; i++) {
2124 MOVTrack *track = &mov->tracks[i];
2125 const char *type;
2126 /* track->track_id is initialized in write_moov, and thus isn't known
2127 * here yet */
2128 int track_id = i + 1;
2129
2130 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2131 type = "video";
2132 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2133 type = "audio";
2134 } else {
2135 continue;
2136 }
2137 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2138 track->enc->bit_rate);
2139 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2140 param_write_int(pb, "trackID", track_id);
2141 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2142 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2143 uint8_t *ptr;
2144 int size = track->enc->extradata_size;
2145 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2146 &size)) {
2147 param_write_hex(pb, "CodecPrivateData",
2148 ptr ? ptr : track->enc->extradata,
2149 size);
2150 av_free(ptr);
2151 }
2152 param_write_string(pb, "FourCC", "H264");
2153 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2154 param_write_string(pb, "FourCC", "WVC1");
2155 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2156 track->enc->extradata_size);
2157 }
2158 param_write_int(pb, "MaxWidth", track->enc->width);
2159 param_write_int(pb, "MaxHeight", track->enc->height);
2160 param_write_int(pb, "DisplayWidth", track->enc->width);
2161 param_write_int(pb, "DisplayHeight", track->enc->height);
2162 } else {
2163 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2164 param_write_string(pb, "FourCC", "AACL");
2165 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2166 param_write_string(pb, "FourCC", "WMAP");
2167 }
2168 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2169 track->enc->extradata_size);
2170 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2171 track->enc->codec_id));
2172 param_write_int(pb, "Channels", track->enc->channels);
2173 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2174 param_write_int(pb, "BitsPerSample", 16);
2175 param_write_int(pb, "PacketSize", track->enc->block_align ?
2176 track->enc->block_align : 4);
2177 }
2178 avio_printf(pb, "</%s>\n", type);
2179 }
2180 avio_printf(pb, "</switch>\n");
2181 avio_printf(pb, "</body>\n");
2182 avio_printf(pb, "</smil>\n");
2183
2184 return update_size(pb, pos);
2185 }
2186
2187 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2188 {
2189 avio_wb32(pb, 16);
2190 ffio_wfourcc(pb, "mfhd");
2191 avio_wb32(pb, 0);
2192 avio_wb32(pb, mov->fragments);
2193 return 0;
2194 }
2195
2196 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2197 int64_t moof_offset)
2198 {
2199 int64_t pos = avio_tell(pb);
2200 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2201 MOV_TFHD_BASE_DATA_OFFSET;
2202 if (!track->entry) {
2203 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2204 } else {
2205 flags |= MOV_TFHD_DEFAULT_FLAGS;
2206 }
2207
2208 /* Don't set a default sample size, the silverlight player refuses
2209 * to play files with that set. Don't set a default sample duration,
2210 * WMP freaks out if it is set. Don't set a base data offset, PIFF
2211 * file format says it MUST NOT be set. */
2212 if (track->mode == MODE_ISM)
2213 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2214 MOV_TFHD_BASE_DATA_OFFSET);
2215
2216 avio_wb32(pb, 0); /* size placeholder */
2217 ffio_wfourcc(pb, "tfhd");
2218 avio_w8(pb, 0); /* version */
2219 avio_wb24(pb, flags);
2220
2221 avio_wb32(pb, track->track_id); /* track-id */
2222 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2223 avio_wb64(pb, moof_offset);
2224 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2225 track->default_duration = get_cluster_duration(track, 0);
2226 avio_wb32(pb, track->default_duration);
2227 }
2228 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2229 track->default_size = track->entry ? track->cluster[0].size : 1;
2230 avio_wb32(pb, track->default_size);
2231 } else
2232 track->default_size = -1;
2233
2234 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2235 track->default_sample_flags =
2236 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2237 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2238 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2239 avio_wb32(pb, track->default_sample_flags);
2240 }
2241
2242 return update_size(pb, pos);
2243 }
2244
2245 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2246 {
2247 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2248 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2249 }
2250
2251 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2252 {
2253 int64_t pos = avio_tell(pb);
2254 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2255 int i;
2256
2257 for (i = 0; i < track->entry; i++) {
2258 if (get_cluster_duration(track, i) != track->default_duration)
2259 flags |= MOV_TRUN_SAMPLE_DURATION;
2260 if (track->cluster[i].size != track->default_size)
2261 flags |= MOV_TRUN_SAMPLE_SIZE;
2262 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2263 flags |= MOV_TRUN_SAMPLE_FLAGS;
2264 }
2265 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2266 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2267 if (track->flags & MOV_TRACK_CTTS)
2268 flags |= MOV_TRUN_SAMPLE_CTS;
2269
2270 avio_wb32(pb, 0); /* size placeholder */
2271 ffio_wfourcc(pb, "trun");
2272 avio_w8(pb, 0); /* version */
2273 avio_wb24(pb, flags);
2274
2275 avio_wb32(pb, track->entry); /* sample count */
2276 track->moof_size_offset = avio_tell(pb);
2277 avio_wb32(pb, 0); /* data offset */
2278 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2279 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2280
2281 for (i = 0; i < track->entry; i++) {
2282 if (flags & MOV_TRUN_SAMPLE_DURATION)
2283 avio_wb32(pb, get_cluster_duration(track, i));
2284 if (flags & MOV_TRUN_SAMPLE_SIZE)
2285 avio_wb32(pb, track->cluster[i].size);
2286 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2287 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2288 if (flags & MOV_TRUN_SAMPLE_CTS)
2289 avio_wb32(pb, track->cluster[i].cts);
2290 }
2291
2292 return update_size(pb, pos);
2293 }
2294
2295 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2296 {
2297 int64_t pos = avio_tell(pb);
2298 const uint8_t uuid[] = {
2299 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2300 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2301 };
2302
2303 avio_wb32(pb, 0); /* size placeholder */
2304 ffio_wfourcc(pb, "uuid");
2305 avio_write(pb, uuid, sizeof(uuid));
2306 avio_w8(pb, 1);
2307 avio_wb24(pb, 0);
2308 avio_wb64(pb, track->frag_start);
2309 avio_wb64(pb, track->start_dts + track->track_duration -
2310 track->cluster[0].dts);
2311
2312 return update_size(pb, pos);
2313 }
2314
2315 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2316 MOVTrack *track, int entry)
2317 {
2318 int n = track->nb_frag_info - 1 - entry, i;
2319 int size = 8 + 16 + 4 + 1 + 16*n;
2320 const uint8_t uuid[] = {
2321 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2322 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2323 };
2324
2325 if (entry < 0)
2326 return 0;
2327
2328 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2329 avio_wb32(pb, size);
2330 ffio_wfourcc(pb, "uuid");
2331 avio_write(pb, uuid, sizeof(uuid));
2332 avio_w8(pb, 1);
2333 avio_wb24(pb, 0);
2334 avio_w8(pb, n);
2335 for (i = 0; i < n; i++) {
2336 int index = entry + 1 + i;
2337 avio_wb64(pb, track->frag_info[index].time);
2338 avio_wb64(pb, track->frag_info[index].duration);
2339 }
2340 if (n < mov->ism_lookahead) {
2341 int free_size = 16 * (mov->ism_lookahead - n);
2342 avio_wb32(pb, free_size);
2343 ffio_wfourcc(pb, "free");
2344 for (i = 0; i < free_size - 8; i++)
2345 avio_w8(pb, 0);
2346 }
2347
2348 return 0;
2349 }
2350
2351 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2352 MOVTrack *track)
2353 {
2354 int64_t pos = avio_tell(pb);
2355 int i;
2356 for (i = 0; i < mov->ism_lookahead; i++) {
2357 /* Update the tfrf tag for the last ism_lookahead fragments,
2358 * nb_frag_info - 1 is the next fragment to be written. */
2359 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2360 }
2361 avio_seek(pb, pos, SEEK_SET);
2362 return 0;
2363 }
2364
2365 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2366 MOVTrack *track, int64_t moof_offset)
2367 {
2368 int64_t pos = avio_tell(pb);
2369 avio_wb32(pb, 0); /* size placeholder */
2370 ffio_wfourcc(pb, "traf");
2371
2372 mov_write_tfhd_tag(pb, track, moof_offset);
2373 mov_write_trun_tag(pb, track);
2374 if (mov->mode == MODE_ISM) {
2375 mov_write_tfxd_tag(pb, track);
2376
2377 if (mov->ism_lookahead) {
2378 int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
2379
2380 track->tfrf_offset = avio_tell(pb);
2381 avio_wb32(pb, 8 + size);
2382 ffio_wfourcc(pb, "free");
2383 for (i = 0; i < size; i++)
2384 avio_w8(pb, 0);
2385 }
2386 }
2387
2388 return update_size(pb, pos);
2389 }
2390
2391 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2392 {
2393 int64_t pos = avio_tell(pb), end;
2394 int i, moof_size;
2395
2396 avio_wb32(pb, 0); /* size placeholder */
2397 ffio_wfourcc(pb, "moof");
2398
2399 mov_write_mfhd_tag(pb, mov);
2400 for (i = 0; i < mov->nb_streams; i++) {
2401 MOVTrack *track = &mov->tracks[i];
2402 if (tracks >= 0 && i != tracks)
2403 continue;
2404 if (!track->entry)
2405 continue;
2406 mov_write_traf_tag(pb, mov, track, pos);
2407 }
2408
2409 end = avio_tell(pb);
2410 moof_size = end - pos;
2411 for (i = 0; i < mov->nb_streams; i++) {
2412 MOVTrack *track = &mov->tracks[i];
2413 if (tracks >= 0 && i != tracks)
2414 continue;
2415 if (!track->entry)
2416 continue;
2417 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2418 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2419 }
2420 avio_seek(pb, end, SEEK_SET);
2421
2422 return update_size(pb, pos);
2423 }
2424
2425 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2426 {
2427 int64_t pos = avio_tell(pb);
2428 int i;
2429
2430 avio_wb32(pb, 0); /* size placeholder */
2431 ffio_wfourcc(pb, "tfra");
2432 avio_w8(pb, 1); /* version */
2433 avio_wb24(pb, 0);
2434
2435 avio_wb32(pb, track->track_id);
2436 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2437 avio_wb32(pb, track->nb_frag_info);
2438 for (i = 0; i < track->nb_frag_info; i++) {
2439 avio_wb64(pb, track->frag_info[i].time);
2440 avio_wb64(pb, track->frag_info[i].offset);
2441 avio_w8(pb, 1); /* traf number */
2442 avio_w8(pb, 1); /* trun number */
2443 avio_w8(pb, 1); /* sample number */
2444 }
2445
2446 return update_size(pb, pos);
2447 }
2448
2449 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2450 {
2451 int64_t pos = avio_tell(pb);
2452 int i;
2453
2454 avio_wb32(pb, 0); /* size placeholder */
2455 ffio_wfourcc(pb, "mfra");
2456 /* An empty mfra atom is enough to indicate to the publishing point that
2457 * the stream has ended. */
2458 if (mov->flags & FF_MOV_FLAG_ISML)
2459 return update_size(pb, pos);
2460
2461 for (i = 0; i < mov->nb_streams; i++) {
2462 MOVTrack *track = &mov->tracks[i];
2463 if (track->nb_frag_info)
2464 mov_write_tfra_tag(pb, track);
2465 }
2466
2467 avio_wb32(pb, 16);
2468 ffio_wfourcc(pb, "mfro");
2469 avio_wb32(pb, 0); /* version + flags */
2470 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2471
2472 return update_size(pb, pos);
2473 }
2474
2475 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2476 {
2477 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2478 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2479
2480 mov->mdat_pos = avio_tell(pb);
2481 avio_wb32(pb, 0); /* size placeholder*/
2482 ffio_wfourcc(pb, "mdat");
2483 return 0;
2484 }
2485
2486 /* TODO: This needs to be more general */
2487 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2488 {
2489 MOVMuxContext *mov = s->priv_data;
2490 int64_t pos = avio_tell(pb);
2491 int has_h264 = 0, has_video = 0;
2492 int minor = 0x200;
2493 int i;
2494
2495 for (i = 0; i < s->nb_streams; i++) {
2496 AVStream *st = s->streams[i];
2497 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2498 has_video = 1;
2499 if (st->codec->codec_id == AV_CODEC_ID_H264)
2500 has_h264 = 1;
2501 }
2502
2503 avio_wb32(pb, 0); /* size */
2504 ffio_wfourcc(pb, "ftyp");
2505
2506 if (mov->mode == MODE_3GP) {
2507 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2508 minor = has_h264 ? 0x100 : 0x200;
2509 } else if (mov->mode & MODE_3G2) {
2510 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2511 minor = has_h264 ? 0x20000 : 0x10000;
2512 } else if (mov->mode == MODE_PSP)
2513 ffio_wfourcc(pb, "MSNV");
2514 else if (mov->mode == MODE_MP4)
2515 ffio_wfourcc(pb, "isom");
2516 else if (mov->mode == MODE_IPOD)
2517 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2518 else if (mov->mode == MODE_ISM)
2519 ffio_wfourcc(pb, "isml");
2520 else
2521 ffio_wfourcc(pb, "qt ");
2522
2523 avio_wb32(pb, minor);
2524
2525 if (mov->mode == MODE_MOV)
2526 ffio_wfourcc(pb, "qt ");
2527 else if (mov->mode == MODE_ISM) {
2528 ffio_wfourcc(pb, "piff");
2529 ffio_wfourcc(pb, "iso2");
2530 } else {
2531 ffio_wfourcc(pb, "isom");
2532 ffio_wfourcc(pb, "iso2");
2533 if (has_h264)
2534 ffio_wfourcc(pb, "avc1");
2535 }
2536
2537 if (mov->mode == MODE_3GP)
2538 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2539 else if (mov->mode & MODE_3G2)
2540 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2541 else if (mov->mode == MODE_PSP)
2542 ffio_wfourcc(pb, "MSNV");
2543 else if (mov->mode == MODE_MP4)
2544 ffio_wfourcc(pb, "mp41");
2545 return update_size(pb, pos);
2546 }
2547
2548 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
2549 {
2550 AVCodecContext *video_codec = s->streams[0]->codec;
2551 AVCodecContext *audio_codec = s->streams[1]->codec;
2552 int audio_rate = audio_codec->sample_rate;
2553 int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
2554 int audio_kbitrate = audio_codec->bit_rate / 1000;
2555 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2556
2557 avio_wb32(pb, 0x94); /* size */
2558 ffio_wfourcc(pb, "uuid");
2559 ffio_wfourcc(pb, "PROF");
2560
2561 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2562 avio_wb32(pb, 0xbb88695c);
2563 avio_wb32(pb, 0xfac9c740);
2564
2565 avio_wb32(pb, 0x0); /* ? */
2566 avio_wb32(pb, 0x3); /* 3 sections ? */
2567
2568 avio_wb32(pb, 0x14); /* size */
2569 ffio_wfourcc(pb, "FPRF");
2570 avio_wb32(pb, 0x0); /* ? */
2571 avio_wb32(pb, 0x0); /* ? */
2572 avio_wb32(pb, 0x0); /* ? */
2573
2574 avio_wb32(pb, 0x2c); /* size */
2575 ffio_wfourcc(pb, "APRF"); /* audio */
2576 avio_wb32(pb, 0x0);
2577 avio_wb32(pb, 0x2); /* TrackID */
2578 ffio_wfourcc(pb, "mp4a");
2579 avio_wb32(pb, 0x20f);
2580 avio_wb32(pb, 0x0);
2581 avio_wb32(pb, audio_kbitrate);
2582 avio_wb32(pb, audio_kbitrate);
2583 avio_wb32(pb, audio_rate);
2584 avio_wb32(pb, audio_codec->channels);
2585
2586 avio_wb32(pb, 0x34); /* size */
2587 ffio_wfourcc(pb, "VPRF"); /* video */
2588 avio_wb32(pb, 0x0);
2589 avio_wb32(pb, 0x1); /* TrackID */
2590 if (video_codec->codec_id == AV_CODEC_ID_H264) {
2591 ffio_wfourcc(pb, "avc1");
2592 avio_wb16(pb, 0x014D);
2593 avio_wb16(pb, 0x0015);
2594 } else {
2595 ffio_wfourcc(pb, "mp4v");
2596 avio_wb16(pb, 0x0000);
2597 avio_wb16(pb, 0x0103);
2598 }
2599 avio_wb32(pb, 0x0);
2600 avio_wb32(pb, video_kbitrate);
2601 avio_wb32(pb, video_kbitrate);
2602 avio_wb32(pb, frame_rate);
2603 avio_wb32(pb, frame_rate);
2604 avio_wb16(pb, video_codec->width);
2605 avio_wb16(pb, video_codec->height);
2606 avio_wb32(pb, 0x010001); /* ? */
2607 }
2608
2609 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2610 {
2611 uint32_t c = -1;
2612 int i, closed_gop = 0;
2613
2614 for (i = 0; i < pkt->size - 4; i++) {
2615 c = (c << 8) + pkt->data[i];
2616 if (c == 0x1b8) { // gop
2617 closed_gop = pkt->data[i + 4] >> 6 & 0x01;
2618 } else if (c == 0x100) { // pic
2619 int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
2620 if (!temp_ref || closed_gop) // I picture is not reordered
2621 *flags = MOV_SYNC_SAMPLE;
2622 else
2623 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2624 break;
2625 }
2626 }
2627 return 0;
2628 }
2629
2630 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2631 {
2632 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2633 int seq = 0, entry = 0;
2634 int key = pkt->flags & AV_PKT_FLAG_KEY;
2635 start = find_next_marker(pkt->data, end);
2636 for (next = start; next < end; start = next) {
2637 next = find_next_marker(start + 4, end);
2638 switch (AV_RB32(start)) {
2639 case VC1_CODE_SEQHDR:
2640 seq = 1;
2641 break;
2642 case VC1_CODE_ENTRYPOINT:
2643 entry = 1;
2644 break;
2645 case VC1_CODE_SLICE:
2646 trk->vc1_info.slices = 1;
2647 break;
2648 }
2649 }
2650 if (!trk->entry && !fragment) {
2651 /* First packet in first fragment */
2652 trk->vc1_info.first_packet_seq = seq;
2653 trk->vc1_info.first_packet_entry = entry;
2654 } else if ((seq && !trk->vc1_info.packet_seq) ||
2655 (entry && !trk->vc1_info.packet_entry)) {
2656 int i;
2657 for (i = 0; i < trk->entry; i++)
2658 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2659 trk->has_keyframes = 0;
2660 if (seq)
2661 trk->vc1_info.packet_seq = 1;
2662 if (entry)
2663 trk->vc1_info.packet_entry = 1;
2664 if (!fragment) {
2665 /* First fragment */
2666 if ((!seq || trk->vc1_info.first_packet_seq) &&
2667 (!entry || trk->vc1_info.first_packet_entry)) {
2668 /* First packet had the same headers as this one, readd the
2669 * sync sample flag. */
2670 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2671 trk->has_keyframes = 1;
2672 }
2673 }
2674 }
2675 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2676 key = seq && entry;
2677 else if (trk->vc1_info.packet_seq)
2678 key = seq;
2679 else if (trk->vc1_info.packet_entry)
2680 key = entry;
2681 if (key) {
2682 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2683 trk->has_keyframes++;
2684 }
2685 }
2686
2687 static int mov_flush_fragment(AVFormatContext *s)
2688 {
2689 MOVMuxContext *mov = s->priv_data;
2690 int i, first_track = -1;
2691 int64_t mdat_size = 0;
2692
2693 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2694 return 0;
2695
2696 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2697 int64_t pos = avio_tell(s->pb);
2698 int ret;
2699 AVIOContext *moov_buf;
2700 uint8_t *buf;
2701 int buf_size;
2702
2703 for (i = 0; i < mov->nb_streams; i++)
2704 if (!mov->tracks[i].entry)
2705 break;
2706 /* Don't write the initial moov unless all tracks have data */
2707 if (i < mov->nb_streams)
2708 return 0;
2709
2710 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
2711 return ret;
2712 mov_write_moov_tag(moov_buf, mov, s);
2713 buf_size = avio_close_dyn_buf(moov_buf, &buf);
2714 av_free(buf);
2715 for (i = 0; i < mov->nb_streams; i++)
2716 mov->tracks[i].data_offset = pos + buf_size + 8;
2717
2718 mov_write_moov_tag(s->pb, mov, s);
2719
2720 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2721 mov->mdat_buf = NULL;
2722 avio_wb32(s->pb, buf_size + 8);
2723 ffio_wfourcc(s->pb, "mdat");
2724 avio_write(s->pb, buf, buf_size);
2725 av_free(buf);
2726
2727 mov->fragments++;
2728 mov->mdat_size = 0;
2729 for (i = 0; i < mov->nb_streams; i++) {
2730 if (mov->tracks[i].entry)
2731 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2732 mov->tracks[i].track_duration -
2733 mov->tracks[i].cluster[0].dts;
2734 mov->tracks[i].entry = 0;
2735 }
2736 avio_flush(s->pb);
2737 return 0;
2738 }
2739
2740 for (i = 0; i < mov->nb_streams; i++) {
2741 MOVTrack *track = &mov->tracks[i];
2742 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2743 track->data_offset = 0;
2744 else
2745 track->data_offset = mdat_size;
2746 if (!track->mdat_buf)
2747 continue;
2748 mdat_size += avio_tell(track->mdat_buf);
2749 if (first_track < 0)
2750 first_track = i;
2751 }
2752
2753 if (!mdat_size)
2754 return 0;
2755
2756 for (i = 0; i < mov->nb_streams; i++) {
2757 MOVTrack *track = &mov->tracks[i];
2758 int buf_size, write_moof = 1, moof_tracks = -1;
2759 uint8_t *buf;
2760 int64_t duration = 0;
2761
2762 if (track->entry)
2763 duration = track->start_dts + track->track_duration -
2764 track->cluster[0].dts;
2765 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2766 if (!track->mdat_buf)
2767 continue;
2768 mdat_size = avio_tell(track->mdat_buf);
2769 moof_tracks = i;
2770 } else {
2771 write_moof = i == first_track;
2772 }
2773
2774 if (write_moof) {
2775 MOVFragmentInfo *info;
2776 avio_flush(s->pb);
2777 track->nb_frag_info++;
2778 if (track->nb_frag_info >= track->frag_info_capacity) {
2779 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
2780 if (av_reallocp_array(&track->frag_info,
2781 new_capacity,
2782 sizeof(*track->frag_info)))
2783 return AVERROR(ENOMEM);
2784 track->frag_info_capacity = new_capacity;
2785 }
2786 info = &track->frag_info[track->nb_frag_info - 1];
2787 info->offset = avio_tell(s->pb);
2788 info->time = mov->tracks[i].frag_start;
2789 info->duration = duration;
2790 mov_write_tfrf_tags(s->pb, mov, track);
2791
2792 mov_write_moof_tag(s->pb, mov, moof_tracks);
2793 info->tfrf_offset = track->tfrf_offset;
2794 mov->fragments++;
2795
2796 avio_wb32(s->pb, mdat_size + 8);
2797 ffio_wfourcc(s->pb, "mdat");
2798 }
2799
2800 if (track->entry)
2801 track->frag_start += duration;
2802 track->entry = 0;
2803 if (!track->mdat_buf)
2804 continue;
2805 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2806 track->mdat_buf = NULL;
2807
2808 avio_write(s->pb, buf, buf_size);
2809 av_free(buf);
2810 }
2811
2812 mov->mdat_size = 0;
2813
2814 avio_flush(s->pb);
2815 return 0;
2816 }
2817
2818 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2819 {
2820 MOVMuxContext *mov = s->priv_data;
2821 AVIOContext *pb = s->pb;
2822 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2823 AVCodecContext *enc = trk->enc;
2824 unsigned int samples_in_chunk = 0;
2825 int size = pkt->size;
2826 uint8_t *reformatted_data = NULL;
2827
2828 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2829 int ret;
2830 if (mov->fragments > 0) {
2831 if (!trk->mdat_buf) {
2832 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2833 return ret;
2834 }
2835 pb = trk->mdat_buf;
2836 } else {
2837 if (!mov->mdat_buf) {
2838 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2839 return ret;
2840 }
2841 pb = mov->mdat_buf;
2842 }
2843 }
2844
2845 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
2846 /* We must find out how many AMR blocks there are in one packet */
2847 static uint16_t packed_size[16] =
2848 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
2849 int len = 0;
2850
2851 while (len < size && samples_in_chunk < 100) {
2852 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2853 samples_in_chunk++;
2854 }
2855 if (samples_in_chunk > 1) {
2856 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2857 return -1;
2858 }
2859 } else if (trk->sample_size)
2860 samples_in_chunk = size / trk->sample_size;
2861 else
2862 samples_in_chunk = 1;
2863
2864 /* copy extradata if it exists */
2865 if (trk->vos_len == 0 && enc->extradata_size > 0) {
2866 trk->vos_len = enc->extradata_size;
2867 trk->vos_data = av_malloc(trk->vos_len);
2868 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
2869 }
2870
2871 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
2872 /* from x264 or from bytestream h264 */
2873 /* nal reformating needed */
2874 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2875 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2876 &size);
2877 avio_write(pb, reformatted_data, size);
2878 } else {
2879 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2880 }
2881 } else {
2882 avio_write(pb, pkt->data, size);
2883 }
2884
2885 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
2886 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
2887 /* copy frame to create needed atoms */
2888 trk->vos_len = size;
2889 trk->vos_data = av_malloc(size);
2890 if (!trk->vos_data)
2891 return AVERROR(ENOMEM);
2892 memcpy(trk->vos_data, pkt->data, size);
2893 }
2894
2895 if (trk->entry >= trk->cluster_capacity) {
2896 unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
2897 if (av_reallocp_array(&trk->cluster, new_capacity,
2898 sizeof(*trk->cluster)))
2899 return AVERROR(ENOMEM);
2900 trk->cluster_capacity = new_capacity;
2901 }
2902
2903 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2904 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
2905 trk->cluster[trk->entry].size = size;
2906 trk->cluster[trk->entry].entries = samples_in_chunk;
2907 trk->cluster[trk->entry].dts = pkt->dts;
2908 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
2909 /* First packet of a new fragment. We already wrote the duration
2910 * of the last packet of the previous fragment based on track_duration,
2911 * which might not exactly match our dts. Therefore adjust the dts
2912 * of this packet to be what the previous packets duration implies. */
2913 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
2914 }
2915 if (trk->start_dts == AV_NOPTS_VALUE)
2916 trk->start_dts = pkt->dts;
2917 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
2918
2919 if (pkt->pts == AV_NOPTS_VALUE) {
2920 av_log(s, AV_LOG_WARNING, "pts has no value\n");
2921 pkt->pts = pkt->dts;
2922 }
2923 if (pkt->dts != pkt->pts)
2924 trk->flags |= MOV_TRACK_CTTS;
2925 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2926 trk->cluster[trk->entry].flags = 0;
2927 if (enc->codec_id == AV_CODEC_ID_VC1) {
2928 mov_parse_vc1_frame(pkt, trk, mov->fragments);
2929 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
2930 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
2931 trk->entry > 0) { // force sync sample for the first key frame
2932 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2933 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2934 trk->flags |= MOV_TRACK_STPS;
2935 } else {
2936 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2937 }
2938 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2939 trk->has_keyframes++;
2940 }
2941 trk->entry++;
2942 trk->sample_count += samples_in_chunk;
2943 mov->mdat_size += size;
2944
2945 avio_flush(pb);
2946
2947 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2948 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
2949 reformatted_data, size);
2950 av_free(reformatted_data);
2951 return 0;
2952 }
2953
2954 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2955 {
2956 if (!pkt) {
2957 mov_flush_fragment(s);
2958 return 1;
2959 } else {
2960 MOVMuxContext *mov = s->priv_data;
2961 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2962 AVCodecContext *enc = trk->enc;
2963 int64_t frag_duration = 0;
2964 int size = pkt->size;
2965
2966 if (!pkt->size)
2967 return 0; /* Discard 0 sized packets */
2968
2969 if (trk->entry)
2970 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
2971 s->streams[pkt->stream_index]->time_base,
2972 AV_TIME_BASE_Q);
2973 if ((mov->max_fragment_duration &&
2974 frag_duration >= mov->max_fragment_duration) ||
2975 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
2976 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
2977 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
2978 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
2979 if (frag_duration >= mov->min_fragment_duration)
2980 mov_flush_fragment(s);
2981 }
2982
2983 return ff_mov_write_packet(s, pkt);
2984 }
2985 }
2986
2987 // QuickTime chapters involve an additional text track with the chapter names
2988 // as samples, and a tref pointing from the other tracks to the chapter one.
2989 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
2990 {
2991 MOVMuxContext *mov = s->priv_data;
2992 MOVTrack *track = &mov->tracks[tracknum];
2993 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2994 int i, len;
2995 // These properties are required to make QT recognize the chapter track
2996 uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
2997
2998 track->mode = mov->mode;
2999 track->tag = MKTAG('t','e','x','t');
3000 track->timescale = MOV_TIMESCALE;
3001 track->enc = avcodec_alloc_context3(NULL);
3002 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
3003 track->enc->extradata = av_malloc(sizeof(chapter_properties));
3004 if (track->enc->extradata == NULL)
3005 return AVERROR(ENOMEM);
3006 track->enc->extradata_size = sizeof(chapter_properties);
3007 memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
3008
3009 for (i = 0; i < s->nb_chapters; i++) {
3010 AVChapter *c = s->chapters[i];
3011 AVDictionaryEntry *t;
3012
3013 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3014 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3015 pkt.duration = end - pkt.dts;
3016
3017 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3018 len = strlen(t->value);
3019 pkt.size = len + 2;
3020 pkt.data = av_malloc(pkt.size);
3021 AV_WB16(pkt.data, len);
3022 memcpy(pkt.data + 2, t->value, len);
3023 ff_mov_write_packet(s, &pkt);
3024 av_freep(&pkt.data);
3025 }
3026 }
3027
3028 return 0;
3029 }
3030
3031 static int mov_write_header(AVFormatContext *s)
3032 {
3033 AVIOContext *pb = s->pb;
3034 MOVMuxContext *mov = s->priv_data;
3035 AVDictionaryEntry *t;
3036 int i, hint_track = 0;
3037
3038 /* Set the FRAGMENT flag if any of the fragmentation methods are
3039 * enabled. */
3040 if (mov->max_fragment_duration || mov->max_fragment_size ||
3041 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3042 FF_MOV_FLAG_FRAG_KEYFRAME |
3043 FF_MOV_FLAG_FRAG_CUSTOM))
3044 mov->flags |= FF_MOV_FLAG_FRAGMENT;
3045
3046 /* faststart: moov at the beginning of the file, if supported */
3047 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3048 if ((mov->flags & FF_MOV_FLAG_FRAGMENT) ||
3049 (s->flags & AVFMT_FLAG_CUSTOM_IO))
3050 mov->flags &= ~FF_MOV_FLAG_FASTSTART;
3051 }
3052
3053 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3054 * is enabled, we don't support non-seekable output at all. */
3055 if (!s->pb->seekable &&
3056 ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
3057 !(s->oformat && !strcmp(s->oformat->name, "ismv")))
3058 || mov->ism_lookahead)) {
3059 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3060 return -1;
3061 }
3062
3063 /* Default mode == MP4 */
3064 mov->mode = MODE_MP4;
3065
3066 if (s->oformat != NULL) {
3067 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3068 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3069 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3070 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3071 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3072 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3073
3074 mov_write_ftyp_tag(pb,s);
3075 if (mov->mode == MODE_PSP) {
3076 if (s->nb_streams != 2) {
3077 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3078 return -1;
3079 }
3080 mov_write_uuidprof_tag(pb, s);
3081 }
3082 }
3083
3084 mov->nb_streams = s->nb_streams;
3085 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
3086 mov->chapter_track = mov->nb_streams++;
3087
3088 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3089 /* Add hint tracks for each audio and video stream */
3090 hint_track = mov->nb_streams;
3091 for (i = 0; i < s->nb_streams; i++) {
3092 AVStream *st = s->streams[i];
3093 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3094 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3095 mov->nb_streams++;
3096 }
3097 }
3098 }
3099
3100 // Reserve an extra stream for chapters for the case where chapters
3101 // are written in the trailer
3102 mov->tracks = av_mallocz((mov->nb_streams + 1) * sizeof(*mov->tracks));
3103 if (!mov->tracks)
3104 return AVERROR(ENOMEM);
3105
3106 for (i = 0; i < s->nb_streams; i++) {
3107 AVStream *st= s->streams[i];
3108 MOVTrack *track= &mov->tracks[i];
3109 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3110
3111 track->enc = st->codec;
3112 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3113 if (track->language < 0)
3114 track->language = 0;
3115 track->mode = mov->mode;
3116 track->tag = mov_find_codec_tag(s, track);
3117 if (!track->tag) {
3118 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
3119 "codec not currently supported in container\n", i);
3120 goto error;
3121 }
3122 /* If hinting of this track is enabled by a later hint track,
3123 * this is updated. */
3124 track->hint_track = -1;
3125 track->start_dts = AV_NOPTS_VALUE;
3126 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3127 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3128 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3129 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3130 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3131 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3132 goto error;
3133 }
3134 track->height = track->tag >> 24 == 'n' ? 486 : 576;
3135 }
3136 track->timescale = st->codec->time_base.den;
3137 if (track->mode == MODE_MOV && track->timescale > 100000)
3138 av_log(s, AV_LOG_WARNING,
3139 "WARNING codec timebase is very high. If duration is too long,\n"
3140 "file may not be playable by quicktime. Specify a shorter timebase\n"
3141 "or choose different container.\n");
3142 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3143 track->timescale = st->codec->sample_rate;
3144 /* set sample_size for PCM and ADPCM */
3145 if (av_get_bits_per_sample(st->codec->codec_id) ||
3146 st->codec->codec_id == AV_CODEC_ID_ILBC) {
3147 if (!st->codec->block_align) {
3148 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
3149 goto error;
3150 }
3151 track->sample_size = st->codec->block_align;
3152 }
3153 /* set audio_vbr for compressed audio */
3154 if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
3155 track->audio_vbr = 1;
3156 }
3157 if (track->mode != MODE_MOV &&
3158 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3159 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3160 i, track->enc->sample_rate);
3161 goto error;
3162 }
3163 } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3164 track->timescale = st->codec->time_base.den;
3165 } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
3166 track->timescale = st->codec->time_base.den;
3167 }
3168 if (!track->height)
3169 track->height = st->codec->height;
3170 /* The ism specific timescale isn't mandatory, but is assumed by
3171 * some tools, such as mp4split. */
3172 if (mov->mode == MODE_ISM)
3173 track->timescale = 10000000;
3174
3175 avpriv_set_pts_info(st, 64, 1, track->timescale);
3176
3177 /* copy extradata if it exists */
3178 if (st->codec->extradata_size) {
3179 track->vos_len = st->codec->extradata_size;
3180 track->vos_data = av_malloc(track->vos_len);
3181 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3182 }
3183 }
3184
3185 if (mov->mode == MODE_ISM) {
3186 /* If no fragmentation options have been set, set a default. */
3187 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3188 FF_MOV_FLAG_FRAG_CUSTOM)) &&
3189 !mov->max_fragment_duration && !mov->max_fragment_size)
3190 mov->max_fragment_duration = 5000000;
3191 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
3192 FF_MOV_FLAG_FRAGMENT;
3193 }
3194
3195 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3196 if (mov->flags & FF_MOV_FLAG_FASTSTART)
3197 mov->reserved_moov_pos = avio_tell(pb);
3198 mov_write_mdat_tag(pb, mov);
3199 }
3200
3201 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3202 mov->time = ff_iso8601_to_unix_time(t->value);
3203 if (mov->time)
3204 mov->time += 0x7C25B080; // 1970 based -> 1904 based
3205
3206 if (mov->chapter_track)
3207 mov_create_chapter_track(s, mov->chapter_track);
3208
3209 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3210 /* Initialize the hint tracks for each audio and video stream */
3211 for (i = 0; i < s->nb_streams; i++) {
3212 AVStream *st = s->streams[i];
3213 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3214 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3215 ff_mov_init_hinting(s, hint_track, i);
3216 hint_track++;
3217 }
3218 }
3219 }
3220
3221 avio_flush(pb);
3222
3223 if (mov->flags & FF_MOV_FLAG_ISML)
3224 mov_write_isml_manifest(pb, mov);
3225
3226 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3227 mov_write_moov_tag(pb, mov, s);
3228 mov->fragments++;
3229 }
3230
3231 return 0;
3232 error:
3233 av_freep(&mov->tracks);
3234 return -1;
3235 }
3236
3237 static int get_moov_size(AVFormatContext *s)
3238 {
3239 int ret;
3240 uint8_t *buf;
3241 AVIOContext *moov_buf;
3242 MOVMuxContext *mov = s->priv_data;
3243
3244 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
3245 return ret;
3246 mov_write_moov_tag(moov_buf, mov, s);
3247 ret = avio_close_dyn_buf(moov_buf, &buf);
3248 av_free(buf);