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