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