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