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