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