avformat: Remove non-compiling and/or silly commented-out printf/av_log statements
[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 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2006
2007 update_size(pb, pos2);
2008 return update_size(pb, pos);
2009 }
2010
2011 return 0;
2012 }
2013
2014 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2015 AVFormatContext *s)
2016 {
2017 int i;
2018 int64_t pos = avio_tell(pb);
2019 avio_wb32(pb, 0); /* size placeholder*/
2020 ffio_wfourcc(pb, "moov");
2021
2022 for (i=0; i<mov->nb_streams; i++) {
2023 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2024 continue;
2025
2026 mov->tracks[i].time = mov->time;
2027 mov->tracks[i].track_id = i+1;
2028 }
2029
2030 if (mov->chapter_track)
2031 for (i=0; i<s->nb_streams; i++) {
2032 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2033 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2034 }
2035 for (i = 0; i < mov->nb_streams; i++) {
2036 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2037 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2038 mov->tracks[i].tref_id =
2039 mov->tracks[mov->tracks[i].src_track].track_id;
2040 }
2041 }
2042
2043 mov_write_mvhd_tag(pb, mov);
2044 if (mov->mode != MODE_MOV && !mov->iods_skip)
2045 mov_write_iods_tag(pb, mov);
2046 for (i=0; i<mov->nb_streams; i++) {
2047 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2048 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2049 }
2050 }
2051 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2052 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2053
2054 if (mov->mode == MODE_PSP)
2055 mov_write_uuidusmt_tag(pb, s);
2056 else
2057 mov_write_udta_tag(pb, mov, s);
2058
2059 return update_size(pb, pos);
2060 }
2061
2062 static void param_write_int(AVIOContext *pb, const char *name, int value)
2063 {
2064 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2065 }
2066
2067 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2068 {
2069 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2070 }
2071
2072 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2073 {
2074 char buf[150];
2075 len = FFMIN(sizeof(buf)/2 - 1, len);
2076 ff_data_to_hex(buf, value, len, 0);
2077 buf[2*len] = '\0';
2078 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2079 }
2080
2081 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2082 {
2083 int64_t pos = avio_tell(pb);
2084 int i;
2085 const uint8_t uuid[] = {
2086 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2087 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2088 };
2089
2090 avio_wb32(pb, 0);
2091 ffio_wfourcc(pb, "uuid");
2092 avio_write(pb, uuid, sizeof(uuid));
2093 avio_wb32(pb, 0);
2094
2095 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2096 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2097 avio_printf(pb, "<head>\n");
2098 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2099 LIBAVFORMAT_IDENT);
2100 avio_printf(pb, "</head>\n");
2101 avio_printf(pb, "<body>\n");
2102 avio_printf(pb, "<switch>\n");
2103 for (i = 0; i < mov->nb_streams; i++) {
2104 MOVTrack *track = &mov->tracks[i];
2105 const char *type;
2106 /* track->track_id is initialized in write_moov, and thus isn't known
2107 * here yet */
2108 int track_id = i + 1;
2109
2110 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2111 type = "video";
2112 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2113 type = "audio";
2114 } else {
2115 continue;
2116 }
2117 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2118 track->enc->bit_rate);
2119 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2120 param_write_int(pb, "trackID", track_id);
2121 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2122 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2123 uint8_t *ptr;
2124 int size = track->enc->extradata_size;
2125 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2126 &size)) {
2127 param_write_hex(pb, "CodecPrivateData",
2128 ptr ? ptr : track->enc->extradata,
2129 size);
2130 av_free(ptr);
2131 }
2132 param_write_string(pb, "FourCC", "H264");
2133 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2134 param_write_string(pb, "FourCC", "WVC1");
2135 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2136 track->enc->extradata_size);
2137 }
2138 param_write_int(pb, "MaxWidth", track->enc->width);
2139 param_write_int(pb, "MaxHeight", track->enc->height);
2140 param_write_int(pb, "DisplayWidth", track->enc->width);
2141 param_write_int(pb, "DisplayHeight", track->enc->height);
2142 } else {
2143 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2144 param_write_string(pb, "FourCC", "AACL");
2145 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2146 param_write_string(pb, "FourCC", "WMAP");
2147 }
2148 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2149 track->enc->extradata_size);
2150 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2151 track->enc->codec_id));
2152 param_write_int(pb, "Channels", track->enc->channels);
2153 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2154 param_write_int(pb, "BitsPerSample", 16);
2155 param_write_int(pb, "PacketSize", track->enc->block_align ?
2156 track->enc->block_align : 4);
2157 }
2158 avio_printf(pb, "</%s>\n", type);
2159 }
2160 avio_printf(pb, "</switch>\n");
2161 avio_printf(pb, "</body>\n");
2162 avio_printf(pb, "</smil>\n");
2163
2164 return update_size(pb, pos);
2165 }
2166
2167 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2168 {
2169 avio_wb32(pb, 16);
2170 ffio_wfourcc(pb, "mfhd");
2171 avio_wb32(pb, 0);
2172 avio_wb32(pb, mov->fragments);
2173 return 0;
2174 }
2175
2176 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2177 int64_t moof_offset)
2178 {
2179 int64_t pos = avio_tell(pb);
2180 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2181 MOV_TFHD_BASE_DATA_OFFSET;
2182 if (!track->entry) {
2183 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2184 } else {
2185 flags |= MOV_TFHD_DEFAULT_FLAGS;
2186 }
2187
2188 /* Don't set a default sample size, the silverlight player refuses
2189 * to play files with that set. Don't set a default sample duration,
2190 * WMP freaks out if it is set. */
2191 if (track->mode == MODE_ISM)
2192 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION);
2193
2194 avio_wb32(pb, 0); /* size placeholder */
2195 ffio_wfourcc(pb, "tfhd");
2196 avio_w8(pb, 0); /* version */
2197 avio_wb24(pb, flags);
2198
2199 avio_wb32(pb, track->track_id); /* track-id */
2200 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2201 avio_wb64(pb, moof_offset);
2202 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2203 track->default_duration = get_cluster_duration(track, 0);
2204 avio_wb32(pb, track->default_duration);
2205 }
2206 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2207 track->default_size = track->entry ? track->cluster[0].size : 1;
2208 avio_wb32(pb, track->default_size);
2209 } else
2210 track->default_size = -1;
2211
2212 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2213 track->default_sample_flags =
2214 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2215 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2216 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2217 avio_wb32(pb, track->default_sample_flags);
2218 }
2219
2220 return update_size(pb, pos);
2221 }
2222
2223 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2224 {
2225 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2226 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2227 }
2228
2229 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2230 {
2231 int64_t pos = avio_tell(pb);
2232 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2233 int i;
2234
2235 for (i = 0; i < track->entry; i++) {
2236 if (get_cluster_duration(track, i) != track->default_duration)
2237 flags |= MOV_TRUN_SAMPLE_DURATION;
2238 if (track->cluster[i].size != track->default_size)
2239 flags |= MOV_TRUN_SAMPLE_SIZE;
2240 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2241 flags |= MOV_TRUN_SAMPLE_FLAGS;
2242 }
2243 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2244 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2245 if (track->flags & MOV_TRACK_CTTS)
2246 flags |= MOV_TRUN_SAMPLE_CTS;
2247
2248 avio_wb32(pb, 0); /* size placeholder */
2249 ffio_wfourcc(pb, "trun");
2250 avio_w8(pb, 0); /* version */
2251 avio_wb24(pb, flags);
2252
2253 avio_wb32(pb, track->entry); /* sample count */
2254 track->moof_size_offset = avio_tell(pb);
2255 avio_wb32(pb, 0); /* data offset */
2256 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2257 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2258
2259 for (i = 0; i < track->entry; i++) {
2260 if (flags & MOV_TRUN_SAMPLE_DURATION)
2261 avio_wb32(pb, get_cluster_duration(track, i));
2262 if (flags & MOV_TRUN_SAMPLE_SIZE)
2263 avio_wb32(pb, track->cluster[i].size);
2264 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2265 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2266 if (flags & MOV_TRUN_SAMPLE_CTS)
2267 avio_wb32(pb, track->cluster[i].cts);
2268 }
2269
2270 return update_size(pb, pos);
2271 }
2272
2273 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2274 {
2275 int64_t pos = avio_tell(pb);
2276 const uint8_t uuid[] = {
2277 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2278 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2279 };
2280
2281 avio_wb32(pb, 0); /* size placeholder */
2282 ffio_wfourcc(pb, "uuid");
2283 avio_write(pb, uuid, sizeof(uuid));
2284 avio_w8(pb, 1);
2285 avio_wb24(pb, 0);
2286 avio_wb64(pb, track->frag_start);
2287 avio_wb64(pb, track->start_dts + track->track_duration -
2288 track->cluster[0].dts);
2289
2290 return update_size(pb, pos);
2291 }
2292
2293 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2294 MOVTrack *track, int entry)
2295 {
2296 int n = track->nb_frag_info - 1 - entry, i;
2297 int size = 8 + 16 + 4 + 1 + 16*n;
2298 const uint8_t uuid[] = {
2299 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2300 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2301 };
2302
2303 if (entry < 0)
2304 return 0;
2305
2306 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2307 avio_wb32(pb, size);
2308 ffio_wfourcc(pb, "uuid");
2309 avio_write(pb, uuid, sizeof(uuid));
2310 avio_w8(pb, 1);
2311 avio_wb24(pb, 0);
2312 avio_w8(pb, n);
2313 for (i = 0; i < n; i++) {
2314 int index = entry + 1 + i;
2315 avio_wb64(pb, track->frag_info[index].time);
2316 avio_wb64(pb, track->frag_info[index].duration);
2317 }
2318 if (n < mov->ism_lookahead) {
2319 int free_size = 16*(mov->ism_lookahead - n);
2320 avio_wb32(pb, free_size);
2321 ffio_wfourcc(pb, "free");
2322 for (i = 0; i < free_size - 8; i++)
2323 avio_w8(pb, 0);
2324 }
2325
2326 return 0;
2327 }
2328
2329 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2330 MOVTrack *track)
2331 {
2332 int64_t pos = avio_tell(pb);
2333 int i;
2334 for (i = 0; i < mov->ism_lookahead; i++) {
2335 /* Update the tfrf tag for the last ism_lookahead fragments,
2336 * nb_frag_info - 1 is the next fragment to be written. */
2337 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2338 }
2339 avio_seek(pb, pos, SEEK_SET);
2340 return 0;
2341 }
2342
2343 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2344 MOVTrack *track, int64_t moof_offset)
2345 {
2346 int64_t pos = avio_tell(pb);
2347 avio_wb32(pb, 0); /* size placeholder */
2348 ffio_wfourcc(pb, "traf");
2349
2350 mov_write_tfhd_tag(pb, track, moof_offset);
2351 mov_write_trun_tag(pb, track);
2352 if (mov->mode == MODE_ISM) {
2353 mov_write_tfxd_tag(pb, track);
2354
2355 if (mov->ism_lookahead) {
2356 int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
2357
2358 track->tfrf_offset = avio_tell(pb);
2359 avio_wb32(pb, 8 + size);
2360 ffio_wfourcc(pb, "free");
2361 for (i = 0; i < size; i++)
2362 avio_w8(pb, 0);
2363 }
2364 }
2365
2366 return update_size(pb, pos);
2367 }
2368
2369 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2370 {
2371 int64_t pos = avio_tell(pb), end;
2372 int i, moof_size;
2373
2374 avio_wb32(pb, 0); /* size placeholder */
2375 ffio_wfourcc(pb, "moof");
2376
2377 mov_write_mfhd_tag(pb, mov);
2378 for (i = 0; i < mov->nb_streams; i++) {
2379 MOVTrack *track = &mov->tracks[i];
2380 if (tracks >= 0 && i != tracks)
2381 continue;
2382 if (!track->entry)
2383 continue;
2384 mov_write_traf_tag(pb, mov, track, pos);
2385 }
2386
2387 end = avio_tell(pb);
2388 moof_size = end - pos;
2389 for (i = 0; i < mov->nb_streams; i++) {
2390 MOVTrack *track = &mov->tracks[i];
2391 if (tracks >= 0 && i != tracks)
2392 continue;
2393 if (!track->entry)
2394 continue;
2395 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2396 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2397 }
2398 avio_seek(pb, end, SEEK_SET);
2399
2400 return update_size(pb, pos);
2401 }
2402
2403 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2404 {
2405 int64_t pos = avio_tell(pb);
2406 int i;
2407
2408 avio_wb32(pb, 0); /* size placeholder */
2409 ffio_wfourcc(pb, "tfra");
2410 avio_w8(pb, 1); /* version */
2411 avio_wb24(pb, 0);
2412
2413 avio_wb32(pb, track->track_id);
2414 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2415 avio_wb32(pb, track->nb_frag_info);
2416 for (i = 0; i < track->nb_frag_info; i++) {
2417 avio_wb64(pb, track->frag_info[i].time);
2418 avio_wb64(pb, track->frag_info[i].offset);
2419 avio_w8(pb, 1); /* traf number */
2420 avio_w8(pb, 1); /* trun number */
2421 avio_w8(pb, 1); /* sample number */
2422 }
2423
2424 return update_size(pb, pos);
2425 }
2426
2427 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2428 {
2429 int64_t pos = avio_tell(pb);
2430 int i;
2431
2432 avio_wb32(pb, 0); /* size placeholder */
2433 ffio_wfourcc(pb, "mfra");
2434 /* An empty mfra atom is enough to indicate to the publishing point that
2435 * the stream has ended. */
2436 if (mov->flags & FF_MOV_FLAG_ISML)
2437 return update_size(pb, pos);
2438
2439 for (i = 0; i < mov->nb_streams; i++) {
2440 MOVTrack *track = &mov->tracks[i];
2441 if (track->nb_frag_info)
2442 mov_write_tfra_tag(pb, track);
2443 }
2444
2445 avio_wb32(pb, 16);
2446 ffio_wfourcc(pb, "mfro");
2447 avio_wb32(pb, 0); /* version + flags */
2448 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2449
2450 return update_size(pb, pos);
2451 }
2452
2453 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2454 {
2455 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2456 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2457
2458 mov->mdat_pos = avio_tell(pb);
2459 avio_wb32(pb, 0); /* size placeholder*/
2460 ffio_wfourcc(pb, "mdat");
2461 return 0;
2462 }
2463
2464 /* TODO: This needs to be more general */
2465 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2466 {
2467 MOVMuxContext *mov = s->priv_data;
2468 int64_t pos = avio_tell(pb);
2469 int has_h264 = 0, has_video = 0;
2470 int minor = 0x200;
2471 int i;
2472
2473 for (i = 0; i < s->nb_streams; i++) {
2474 AVStream *st = s->streams[i];
2475 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2476 has_video = 1;
2477 if (st->codec->codec_id == AV_CODEC_ID_H264)
2478 has_h264 = 1;
2479 }
2480
2481 avio_wb32(pb, 0); /* size */
2482 ffio_wfourcc(pb, "ftyp");
2483
2484 if (mov->mode == MODE_3GP) {
2485 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2486 minor = has_h264 ? 0x100 : 0x200;
2487 } else if (mov->mode & MODE_3G2) {
2488 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2489 minor = has_h264 ? 0x20000 : 0x10000;
2490 }else if (mov->mode == MODE_PSP)
2491 ffio_wfourcc(pb, "MSNV");
2492 else if (mov->mode == MODE_MP4)
2493 ffio_wfourcc(pb, "isom");
2494 else if (mov->mode == MODE_IPOD)
2495 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2496 else if (mov->mode == MODE_ISM)
2497 ffio_wfourcc(pb, "isml");
2498 else
2499 ffio_wfourcc(pb, "qt ");
2500
2501 avio_wb32(pb, minor);
2502
2503 if(mov->mode == MODE_MOV)
2504 ffio_wfourcc(pb, "qt ");
2505 else if (mov->mode == MODE_ISM) {
2506 ffio_wfourcc(pb, "piff");
2507 ffio_wfourcc(pb, "iso2");
2508 } else {
2509 ffio_wfourcc(pb, "isom");
2510 ffio_wfourcc(pb, "iso2");
2511 if(has_h264)
2512 ffio_wfourcc(pb, "avc1");
2513 }
2514
2515 if (mov->mode == MODE_3GP)
2516 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2517 else if (mov->mode & MODE_3G2)
2518 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2519 else if (mov->mode == MODE_PSP)
2520 ffio_wfourcc(pb, "MSNV");
2521 else if (mov->mode == MODE_MP4)
2522 ffio_wfourcc(pb, "mp41");
2523 return update_size(pb, pos);
2524 }
2525
2526 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
2527 {
2528 AVCodecContext *video_codec = s->streams[0]->codec;
2529 AVCodecContext *audio_codec = s->streams[1]->codec;
2530 int audio_rate = audio_codec->sample_rate;
2531 int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
2532 int audio_kbitrate = audio_codec->bit_rate / 1000;
2533 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2534
2535 avio_wb32(pb, 0x94); /* size */
2536 ffio_wfourcc(pb, "uuid");
2537 ffio_wfourcc(pb, "PROF");
2538
2539 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2540 avio_wb32(pb, 0xbb88695c);
2541 avio_wb32(pb, 0xfac9c740);
2542
2543 avio_wb32(pb, 0x0); /* ? */
2544 avio_wb32(pb, 0x3); /* 3 sections ? */
2545
2546 avio_wb32(pb, 0x14); /* size */
2547 ffio_wfourcc(pb, "FPRF");
2548 avio_wb32(pb, 0x0); /* ? */
2549 avio_wb32(pb, 0x0); /* ? */
2550 avio_wb32(pb, 0x0); /* ? */
2551
2552 avio_wb32(pb, 0x2c); /* size */
2553 ffio_wfourcc(pb, "APRF");/* audio */
2554 avio_wb32(pb, 0x0);
2555 avio_wb32(pb, 0x2); /* TrackID */
2556 ffio_wfourcc(pb, "mp4a");
2557 avio_wb32(pb, 0x20f);
2558 avio_wb32(pb, 0x0);
2559 avio_wb32(pb, audio_kbitrate);
2560 avio_wb32(pb, audio_kbitrate);
2561 avio_wb32(pb, audio_rate);
2562 avio_wb32(pb, audio_codec->channels);
2563
2564 avio_wb32(pb, 0x34); /* size */
2565 ffio_wfourcc(pb, "VPRF"); /* video */
2566 avio_wb32(pb, 0x0);
2567 avio_wb32(pb, 0x1); /* TrackID */
2568 if (video_codec->codec_id == AV_CODEC_ID_H264) {
2569 ffio_wfourcc(pb, "avc1");
2570 avio_wb16(pb, 0x014D);
2571 avio_wb16(pb, 0x0015);
2572 } else {
2573 ffio_wfourcc(pb, "mp4v");
2574 avio_wb16(pb, 0x0000);
2575 avio_wb16(pb, 0x0103);
2576 }
2577 avio_wb32(pb, 0x0);
2578 avio_wb32(pb, video_kbitrate);
2579 avio_wb32(pb, video_kbitrate);
2580 avio_wb32(pb, frame_rate);
2581 avio_wb32(pb, frame_rate);
2582 avio_wb16(pb, video_codec->width);
2583 avio_wb16(pb, video_codec->height);
2584 avio_wb32(pb, 0x010001); /* ? */
2585 }
2586
2587 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2588 {
2589 uint32_t c = -1;
2590 int i, closed_gop = 0;
2591
2592 for (i = 0; i < pkt->size - 4; i++) {
2593 c = (c<<8) + pkt->data[i];
2594 if (c == 0x1b8) { // gop
2595 closed_gop = pkt->data[i+4]>>6 & 0x01;
2596 } else if (c == 0x100) { // pic
2597 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2598 if (!temp_ref || closed_gop) // I picture is not reordered
2599 *flags = MOV_SYNC_SAMPLE;
2600 else
2601 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2602 break;
2603 }
2604 }
2605 return 0;
2606 }
2607
2608 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2609 {
2610 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2611 int seq = 0, entry = 0;
2612 int key = pkt->flags & AV_PKT_FLAG_KEY;
2613 start = find_next_marker(pkt->data, end);
2614 for (next = start; next < end; start = next) {
2615 next = find_next_marker(start + 4, end);
2616 switch (AV_RB32(start)) {
2617 case VC1_CODE_SEQHDR:
2618 seq = 1;
2619 break;
2620 case VC1_CODE_ENTRYPOINT:
2621 entry = 1;
2622 break;
2623 case VC1_CODE_SLICE:
2624 trk->vc1_info.slices = 1;
2625 break;
2626 }
2627 }
2628 if (!trk->entry && !fragment) {
2629 /* First packet in first fragment */
2630 trk->vc1_info.first_packet_seq = seq;
2631 trk->vc1_info.first_packet_entry = entry;
2632 } else if ((seq && !trk->vc1_info.packet_seq) ||
2633 (entry && !trk->vc1_info.packet_entry)) {
2634 int i;
2635 for (i = 0; i < trk->entry; i++)
2636 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2637 trk->has_keyframes = 0;
2638 if (seq)
2639 trk->vc1_info.packet_seq = 1;
2640 if (entry)
2641 trk->vc1_info.packet_entry = 1;
2642 if (!fragment) {
2643 /* First fragment */
2644 if ((!seq || trk->vc1_info.first_packet_seq) &&
2645 (!entry || trk->vc1_info.first_packet_entry)) {
2646 /* First packet had the same headers as this one, readd the
2647 * sync sample flag. */
2648 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2649 trk->has_keyframes = 1;
2650 }
2651 }
2652 }
2653 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2654 key = seq && entry;
2655 else if (trk->vc1_info.packet_seq)
2656 key = seq;
2657 else if (trk->vc1_info.packet_entry)
2658 key = entry;
2659 if (key) {
2660 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2661 trk->has_keyframes++;
2662 }
2663 }
2664
2665 static int mov_flush_fragment(AVFormatContext *s)
2666 {
2667 MOVMuxContext *mov = s->priv_data;
2668 int i, first_track = -1;
2669 int64_t mdat_size = 0;
2670
2671 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2672 return 0;
2673
2674 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2675 int64_t pos = avio_tell(s->pb);
2676 int ret;
2677 AVIOContext *moov_buf;
2678 uint8_t *buf;
2679 int buf_size;
2680
2681 for (i = 0; i < mov->nb_streams; i++)
2682 if (!mov->tracks[i].entry)
2683 break;
2684 /* Don't write the initial moov unless all tracks have data */
2685 if (i < mov->nb_streams)
2686 return 0;
2687
2688 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
2689 return ret;
2690 mov_write_moov_tag(moov_buf, mov, s);
2691 buf_size = avio_close_dyn_buf(moov_buf, &buf);
2692 av_free(buf);
2693 for (i = 0; i < mov->nb_streams; i++)
2694 mov->tracks[i].data_offset = pos + buf_size + 8;
2695
2696 mov_write_moov_tag(s->pb, mov, s);
2697
2698 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2699 mov->mdat_buf = NULL;
2700 avio_wb32(s->pb, buf_size + 8);
2701 ffio_wfourcc(s->pb, "mdat");
2702 avio_write(s->pb, buf, buf_size);
2703 av_free(buf);
2704
2705 mov->fragments++;
2706 mov->mdat_size = 0;
2707 for (i = 0; i < mov->nb_streams; i++) {
2708 if (mov->tracks[i].entry)
2709 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2710 mov->tracks[i].track_duration -
2711 mov->tracks[i].cluster[0].dts;
2712 mov->tracks[i].entry = 0;
2713 }
2714 avio_flush(s->pb);
2715 return 0;
2716 }
2717
2718 for (i = 0; i < mov->nb_streams; i++) {
2719 MOVTrack *track = &mov->tracks[i];
2720 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2721 track->data_offset = 0;
2722 else
2723 track->data_offset = mdat_size;
2724 if (!track->mdat_buf)
2725 continue;
2726 mdat_size += avio_tell(track->mdat_buf);
2727 if (first_track < 0)
2728 first_track = i;
2729 }
2730
2731 if (!mdat_size)
2732 return 0;
2733
2734 for (i = 0; i < mov->nb_streams; i++) {
2735 MOVTrack *track = &mov->tracks[i];
2736 int buf_size, write_moof = 1, moof_tracks = -1;
2737 uint8_t *buf;
2738 int64_t duration = 0;
2739
2740 if (track->entry)
2741 duration = track->start_dts + track->track_duration -
2742 track->cluster[0].dts;
2743 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2744 if (!track->mdat_buf)
2745 continue;
2746 mdat_size = avio_tell(track->mdat_buf);
2747 moof_tracks = i;
2748 } else {
2749 write_moof = i == first_track;
2750 }
2751
2752 if (write_moof) {
2753 MOVFragmentInfo *info;
2754 avio_flush(s->pb);
2755 track->nb_frag_info++;
2756 track->frag_info = av_realloc(track->frag_info,
2757 sizeof(*track->frag_info) *
2758 track->nb_frag_info);
2759 info = &track->frag_info[track->nb_frag_info - 1];
2760 info->offset = avio_tell(s->pb);
2761 info->time = mov->tracks[i].frag_start;
2762 info->duration = duration;
2763 mov_write_tfrf_tags(s->pb, mov, track);
2764
2765 mov_write_moof_tag(s->pb, mov, moof_tracks);
2766 info->tfrf_offset = track->tfrf_offset;
2767 mov->fragments++;
2768
2769 avio_wb32(s->pb, mdat_size + 8);
2770 ffio_wfourcc(s->pb, "mdat");
2771 }
2772
2773 if (track->entry)
2774 track->frag_start += duration;
2775 track->entry = 0;
2776 if (!track->mdat_buf)
2777 continue;
2778 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2779 track->mdat_buf = NULL;
2780
2781 avio_write(s->pb, buf, buf_size);
2782 av_free(buf);
2783 }
2784
2785 mov->mdat_size = 0;
2786
2787 avio_flush(s->pb);
2788 return 0;
2789 }
2790
2791 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2792 {
2793 MOVMuxContext *mov = s->priv_data;
2794 AVIOContext *pb = s->pb;
2795 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2796 AVCodecContext *enc = trk->enc;
2797 unsigned int samples_in_chunk = 0;
2798 int size= pkt->size;
2799 uint8_t *reformatted_data = NULL;
2800
2801 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2802 int ret;
2803 if (mov->fragments > 0) {
2804 if (!trk->mdat_buf) {
2805 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2806 return ret;
2807 }
2808 pb = trk->mdat_buf;
2809 } else {
2810 if (!mov->mdat_buf) {
2811 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2812 return ret;
2813 }
2814 pb = mov->mdat_buf;
2815 }
2816 }
2817
2818 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
2819 /* We must find out how many AMR blocks there are in one packet */
2820 static uint16_t packed_size[16] =
2821 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
2822 int len = 0;
2823
2824 while (len < size && samples_in_chunk < 100) {
2825 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2826 samples_in_chunk++;
2827 }
2828 if (samples_in_chunk > 1) {
2829 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2830 return -1;
2831 }
2832 } else if (trk->sample_size)
2833 samples_in_chunk = size / trk->sample_size;
2834 else
2835 samples_in_chunk = 1;
2836
2837 /* copy extradata if it exists */
2838 if (trk->vos_len == 0 && enc->extradata_size > 0) {
2839 trk->vos_len = enc->extradata_size;
2840 trk->vos_data = av_malloc(trk->vos_len);
2841 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
2842 }
2843
2844 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
2845 /* from x264 or from bytestream h264 */
2846 /* nal reformating needed */
2847 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2848 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2849 &size);
2850 avio_write(pb, reformatted_data, size);
2851 } else {
2852 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2853 }
285