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