rtp: set the payload type as stream id
[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 == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
782 else tag = MKTAG('d','v','c',' ');
783 else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
784 else if (track->enc->pix_fmt == AV_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 AVPixelFormat pix_fmt;
802 uint32_t tag;
803 unsigned bps;
804 } mov_pix_fmt_tags[] = {
805 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
806 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
807 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
808 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
809 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
810 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
811 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
812 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
813 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
814 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
815 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
816 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
817 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
818 { AV_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, MOVTrack *track)
1540 {
1541
1542 AVFormatContext *ctx = track->rtp_ctx;
1543 char buf[1000] = "";
1544 int len;
1545
1546 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1547 NULL, NULL, 0, 0, ctx);
1548 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1549 len = strlen(buf);
1550
1551 avio_wb32(pb, len + 24);
1552 ffio_wfourcc(pb, "udta");
1553 avio_wb32(pb, len + 16);
1554 ffio_wfourcc(pb, "hnti");
1555 avio_wb32(pb, len + 8);
1556 ffio_wfourcc(pb, "sdp ");
1557 avio_write(pb, buf, len);
1558 return len + 24;
1559 }
1560
1561 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1562 MOVTrack *track, AVStream *st)
1563 {
1564 int64_t pos = avio_tell(pb);
1565 avio_wb32(pb, 0); /* size */
1566 ffio_wfourcc(pb, "trak");
1567 mov_write_tkhd_tag(pb, track, st);
1568 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1569 (track->entry && track->cluster[0].dts)) {
1570 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1571 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1572 }
1573 if (track->tref_tag)
1574 mov_write_tref_tag(pb, track);
1575 mov_write_mdia_tag(pb, track);
1576 if (track->mode == MODE_PSP)
1577 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1578 if (track->tag == MKTAG('r','t','p',' '))
1579 mov_write_udta_sdp(pb, track);
1580 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1581 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1582 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1583 mov_write_tapt_tag(pb, track);
1584 };
1585 return update_size(pb, pos);
1586 }
1587
1588 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1589 {
1590 int i, has_audio = 0, has_video = 0;
1591 int64_t pos = avio_tell(pb);
1592 int audio_profile = mov->iods_audio_profile;
1593 int video_profile = mov->iods_video_profile;
1594 for (i = 0; i < mov->nb_streams; i++) {
1595 if(mov->tracks[i].entry > 0) {
1596 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1597 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1598 }
1599 }
1600 if (audio_profile < 0)
1601 audio_profile = 0xFF - has_audio;
1602 if (video_profile < 0)
1603 video_profile = 0xFF - has_video;
1604 avio_wb32(pb, 0x0); /* size */
1605 ffio_wfourcc(pb, "iods");
1606 avio_wb32(pb, 0); /* version & flags */
1607 put_descr(pb, 0x10, 7);
1608 avio_wb16(pb, 0x004f);
1609 avio_w8(pb, 0xff);
1610 avio_w8(pb, 0xff);
1611 avio_w8(pb, audio_profile);
1612 avio_w8(pb, video_profile);
1613 avio_w8(pb, 0xff);
1614 return update_size(pb, pos);
1615 }
1616
1617 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1618 {
1619 avio_wb32(pb, 0x20); /* size */
1620 ffio_wfourcc(pb, "trex");
1621 avio_wb32(pb, 0); /* version & flags */
1622 avio_wb32(pb, track->track_id); /* track ID */
1623 avio_wb32(pb, 1); /* default sample description index */
1624 avio_wb32(pb, 0); /* default sample duration */
1625 avio_wb32(pb, 0); /* default sample size */
1626 avio_wb32(pb, 0); /* default sample flags */
1627 return 0;
1628 }
1629
1630 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1631 {
1632 int64_t pos = avio_tell(pb);
1633 int i;
1634 avio_wb32(pb, 0x0); /* size */
1635 ffio_wfourcc(pb, "mvex");
1636 for (i = 0; i < mov->nb_streams; i++)
1637 mov_write_trex_tag(pb, &mov->tracks[i]);
1638 return update_size(pb, pos);
1639 }
1640
1641 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1642 {
1643 int max_track_id = 1, i;
1644 int64_t max_track_len_temp, max_track_len = 0;
1645 int version;
1646
1647 for (i=0; i<mov->nb_streams; i++) {
1648 if(mov->tracks[i].entry > 0) {
1649 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1650 MOV_TIMESCALE,
1651 mov->tracks[i].timescale,
1652 AV_ROUND_UP);
1653 if (max_track_len < max_track_len_temp)
1654 max_track_len = max_track_len_temp;
1655 if (max_track_id < mov->tracks[i].track_id)
1656 max_track_id = mov->tracks[i].track_id;
1657 }
1658 }
1659
1660 version = max_track_len < UINT32_MAX ? 0 : 1;
1661 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1662 ffio_wfourcc(pb, "mvhd");
1663 avio_w8(pb, version);
1664 avio_wb24(pb, 0); /* flags */
1665 if (version == 1) {
1666 avio_wb64(pb, mov->time);
1667 avio_wb64(pb, mov->time);
1668 } else {
1669 avio_wb32(pb, mov->time); /* creation time */
1670 avio_wb32(pb, mov->time); /* modification time */
1671 }
1672 avio_wb32(pb, MOV_TIMESCALE);
1673 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1674
1675 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1676 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1677 avio_wb16(pb, 0); /* reserved */
1678 avio_wb32(pb, 0); /* reserved */
1679 avio_wb32(pb, 0); /* reserved */
1680
1681 /* Matrix structure */
1682 avio_wb32(pb, 0x00010000); /* reserved */
1683 avio_wb32(pb, 0x0); /* reserved */
1684 avio_wb32(pb, 0x0); /* reserved */
1685 avio_wb32(pb, 0x0); /* reserved */
1686 avio_wb32(pb, 0x00010000); /* reserved */
1687 avio_wb32(pb, 0x0); /* reserved */
1688 avio_wb32(pb, 0x0); /* reserved */
1689 avio_wb32(pb, 0x0); /* reserved */
1690 avio_wb32(pb, 0x40000000); /* reserved */
1691
1692 avio_wb32(pb, 0); /* reserved (preview time) */
1693 avio_wb32(pb, 0); /* reserved (preview duration) */
1694 avio_wb32(pb, 0); /* reserved (poster time) */
1695 avio_wb32(pb, 0); /* reserved (selection time) */
1696 avio_wb32(pb, 0); /* reserved (selection duration) */
1697 avio_wb32(pb, 0); /* reserved (current time) */
1698 avio_wb32(pb, max_track_id + 1); /* Next track id */
1699 return 0x6c;
1700 }
1701
1702 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1703 AVFormatContext *s)
1704 {
1705 avio_wb32(pb, 33); /* size */
1706 ffio_wfourcc(pb, "hdlr");
1707 avio_wb32(pb, 0);
1708 avio_wb32(pb, 0);
1709 ffio_wfourcc(pb, "mdir");
1710 ffio_wfourcc(pb, "appl");
1711 avio_wb32(pb, 0);
1712 avio_wb32(pb, 0);
1713 avio_w8(pb, 0);
1714 return 33;
1715 }
1716
1717 /* helper function to write a data tag with the specified string as data */
1718 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1719 {
1720 if(long_style){
1721 int size = 16 + strlen(data);
1722 avio_wb32(pb, size); /* size */
1723 ffio_wfourcc(pb, "data");
1724 avio_wb32(pb, 1);
1725 avio_wb32(pb, 0);
1726 avio_write(pb, data, strlen(data));
1727 return size;
1728 }else{
1729 if (!lang)
1730 lang = ff_mov_iso639_to_lang("und", 1);
1731 avio_wb16(pb, strlen(data)); /* string length */
1732 avio_wb16(pb, lang);
1733 avio_write(pb, data, strlen(data));
1734 return strlen(data) + 4;
1735 }
1736 }
1737
1738 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1739 int size = 0;
1740 if (value && value[0]) {
1741 int64_t pos = avio_tell(pb);
1742 avio_wb32(pb, 0); /* size */
1743 ffio_wfourcc(pb, name);
1744 mov_write_string_data_tag(pb, value, lang, long_style);
1745 size = update_size(pb, pos);
1746 }
1747 return size;
1748 }
1749
1750 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1751 const char *name, const char *tag,
1752 int long_style)
1753 {
1754 int l, lang = 0, len, len2;
1755 AVDictionaryEntry *t, *t2 = NULL;
1756 char tag2[16];
1757
1758 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1759 return 0;
1760
1761 len = strlen(t->key);
1762 snprintf(tag2, sizeof(tag2), "%s-", tag);
1763 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1764 len2 = strlen(t2->key);
1765 if (len2 == len+4 && !strcmp(t->value, t2->value)
1766 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1767 lang = l;
1768 break;
1769 }
1770 }
1771 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1772 }
1773
1774 /* iTunes track number */
1775 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1776 AVFormatContext *s)
1777 {
1778 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1779 int size = 0, track = t ? atoi(t->value) : 0;
1780 if (track) {
1781 avio_wb32(pb, 32); /* size */
1782 ffio_wfourcc(pb, "trkn");
1783 avio_wb32(pb, 24); /* size */
1784 ffio_wfourcc(pb, "data");
1785 avio_wb32(pb, 0); // 8 bytes empty
1786 avio_wb32(pb, 0);
1787 avio_wb16(pb, 0); // empty
1788 avio_wb16(pb, track); // track number
1789 avio_wb16(pb, 0); // total track number
1790 avio_wb16(pb, 0); // empty
1791 size = 32;
1792 }
1793 return size;
1794 }
1795
1796 /* iTunes meta data list */
1797 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1798 AVFormatContext *s)
1799 {
1800 int64_t pos = avio_tell(pb);
1801 avio_wb32(pb, 0); /* size */
1802 ffio_wfourcc(pb, "ilst");
1803 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1804 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1805 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1806 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1807 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1808 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1809 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1810 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1811 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1812 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1813 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1814 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1815 mov_write_string_metadata(s, pb, "desc", "description",1);
1816 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1817 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1818 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1819 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1820 mov_write_trkn_tag(pb, mov, s);
1821 return update_size(pb, pos);
1822 }
1823
1824 /* iTunes meta data tag */
1825 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1826 AVFormatContext *s)
1827 {
1828 int size = 0;
1829 int64_t pos = avio_tell(pb);
1830 avio_wb32(pb, 0); /* size */
1831 ffio_wfourcc(pb, "meta");
1832 avio_wb32(pb, 0);
1833 mov_write_itunes_hdlr_tag(pb, mov, s);
1834 mov_write_ilst_tag(pb, mov, s);
1835 size = update_size(pb, pos);
1836 return size;
1837 }
1838
1839 static int utf8len(const uint8_t *b)
1840 {
1841 int len=0;
1842 int val;
1843 while(*b){
1844 GET_UTF8(val, *b++, return -1;)
1845 len++;
1846 }
1847 return len;
1848 }
1849
1850 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1851 {
1852 int val;
1853 while(*b){
1854 GET_UTF8(val, *b++, return -1;)
1855 avio_wb16(pb, val);
1856 }
1857 avio_wb16(pb, 0x00);
1858 return 0;
1859 }
1860
1861 static uint16_t language_code(const char *str)
1862 {
1863 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1864 }
1865
1866 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1867 const char *tag, const char *str)
1868 {
1869 int64_t pos = avio_tell(pb);
1870 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1871 if (!t || !utf8len(t->value))
1872 return 0;
1873 avio_wb32(pb, 0); /* size */
1874 ffio_wfourcc(pb, tag); /* type */
1875 avio_wb32(pb, 0); /* version + flags */
1876 if (!strcmp(tag, "yrrc"))
1877 avio_wb16(pb, atoi(t->value));
1878 else {
1879 avio_wb16(pb, language_code("eng")); /* language */
1880 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1881 if (!strcmp(tag, "albm") &&
1882 (t = av_dict_get(s->metadata, "track", NULL, 0)))
1883 avio_w8(pb, atoi(t->value));
1884 }
1885 return update_size(pb, pos);
1886 }
1887
1888 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1889 {
1890 int64_t pos = avio_tell(pb);
1891 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1892
1893 avio_wb32(pb, 0); // size
1894 ffio_wfourcc(pb, "chpl");
1895 avio_wb32(pb, 0x01000000); // version + flags
1896 avio_wb32(pb, 0); // unknown
1897 avio_w8(pb, nb_chapters);
1898
1899 for (i = 0; i < nb_chapters; i++) {
1900 AVChapter *c = s->chapters[i];
1901 AVDictionaryEntry *t;
1902 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1903
1904 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1905 int len = FFMIN(strlen(t->value), 255);
1906 avio_w8(pb, len);
1907 avio_write(pb, t->value, len);
1908 } else
1909 avio_w8(pb, 0);
1910 }
1911 return update_size(pb, pos);
1912 }
1913
1914 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1915 AVFormatContext *s)
1916 {
1917 AVIOContext *pb_buf;
1918 int i, ret, size;
1919 uint8_t *buf;
1920
1921 for (i = 0; i < s->nb_streams; i++)
1922 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1923 return 0;
1924 }
1925
1926 ret = avio_open_dyn_buf(&pb_buf);
1927 if(ret < 0)
1928 return ret;
1929
1930 if (mov->mode & MODE_3GP) {
1931 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1932 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1933 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1934 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1935 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1936 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1937 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1938 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1939 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1940 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
1941 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1942 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1943 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1944 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
1945 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
1946 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1947 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1948 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1949 } else {
1950 /* iTunes meta data */
1951 mov_write_meta_tag(pb_buf, mov, s);
1952 }
1953
1954 if (s->nb_chapters)
1955 mov_write_chpl_tag(pb_buf, s);
1956
1957 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1958 avio_wb32(pb, size+8);
1959 ffio_wfourcc(pb, "udta");
1960 avio_write(pb, buf, size);
1961 }
1962 av_free(buf);
1963
1964 return 0;
1965 }
1966
1967 static void mov_write_psp_udta_tag(AVIOContext *pb,
1968 const char *str, const char *lang, int type)
1969 {
1970 int len = utf8len(str)+1;
1971 if(len<=0)
1972 return;
1973 avio_wb16(pb, len*2+10); /* size */
1974 avio_wb32(pb, type); /* type */
1975 avio_wb16(pb, language_code(lang)); /* language */
1976 avio_wb16(pb, 0x01); /* ? */
1977 ascii_to_wc(pb, str);
1978 }
1979
1980 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1981 {
1982 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
1983 int64_t pos, pos2;
1984
1985 if (title) {
1986 pos = avio_tell(pb);
1987 avio_wb32(pb, 0); /* size placeholder*/
1988 ffio_wfourcc(pb, "uuid");
1989 ffio_wfourcc(pb, "USMT");
1990 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1991 avio_wb32(pb, 0xbb88695c);
1992 avio_wb32(pb, 0xfac9c740);
1993
1994 pos2 = avio_tell(pb);
1995 avio_wb32(pb, 0); /* size placeholder*/
1996 ffio_wfourcc(pb, "MTDT");
1997 avio_wb16(pb, 4);
1998
1999 // ?
2000 avio_wb16(pb, 0x0C); /* size */
2001 avio_wb32(pb, 0x0B); /* type */
2002 avio_wb16(pb, language_code("und")); /* language */
2003 avio_wb16(pb, 0x0); /* ? */
2004 avio_wb16(pb, 0x021C); /* data */
2005
2006 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2007 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2008 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2009
2010 update_size(pb, pos2);
2011 return update_size(pb, pos);
2012 }
2013
2014 return 0;
2015 }
2016
2017 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
2018 AVFormatContext *s)
2019 {
2020 int i;
2021 int64_t pos = avio_tell(pb);
2022 avio_wb32(pb, 0); /* size placeholder*/
2023 ffio_wfourcc(pb, "moov");
2024
2025 for (i=0; i<mov->nb_streams; i++) {
2026 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2027 continue;
2028
2029 mov->tracks[i].time = mov->time;
2030 mov->tracks[i].track_id = i+1;
2031 }
2032
2033 if (mov->chapter_track)
2034 for (i=0; i<s->nb_streams; i++) {
2035 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2036 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2037 }
2038 for (i = 0; i < mov->nb_streams; i++) {
2039 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2040 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2041 mov->tracks[i].tref_id =
2042 mov->tracks[mov->tracks[i].src_track].track_id;
2043 }
2044 }
2045
2046 mov_write_mvhd_tag(pb, mov);
2047 if (mov->mode != MODE_MOV && !mov->iods_skip)
2048 mov_write_iods_tag(pb, mov);
2049 for (i=0; i<mov->nb_streams; i++) {
2050 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2051 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2052 }
2053 }
2054 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2055 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2056
2057 if (mov->mode == MODE_PSP)
2058 mov_write_uuidusmt_tag(pb, s);
2059 else
2060 mov_write_udta_tag(pb, mov, s);
2061
2062 return update_size(pb, pos);
2063 }
2064
2065 static void param_write_int(AVIOContext *pb, const char *name, int value)
2066 {
2067 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2068 }
2069
2070 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2071 {
2072 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2073 }
2074
2075 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2076 {
2077 char buf[150];
2078 len = FFMIN(sizeof(buf)/2 - 1, len);
2079 ff_data_to_hex(buf, value, len, 0);
2080 buf[2*len] = '\0';
2081 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2082 }
2083
2084 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
2085 {
2086 int64_t pos = avio_tell(pb);
2087 int i;
2088 const uint8_t uuid[] = {
2089 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2090 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2091 };
2092
2093 avio_wb32(pb, 0);
2094 ffio_wfourcc(pb, "uuid");
2095 avio_write(pb, uuid, sizeof(uuid));
2096 avio_wb32(pb, 0);
2097
2098 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2099 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2100 avio_printf(pb, "<head>\n");
2101 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2102 LIBAVFORMAT_IDENT);
2103 avio_printf(pb, "</head>\n");
2104 avio_printf(pb, "<body>\n");
2105 avio_printf(pb, "<switch>\n");
2106 for (i = 0; i < mov->nb_streams; i++) {
2107 MOVTrack *track = &mov->tracks[i];
2108 const char *type;
2109 /* track->track_id is initialized in write_moov, and thus isn't known
2110 * here yet */
2111 int track_id = i + 1;
2112
2113 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2114 type = "video";
2115 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2116 type = "audio";
2117 } else {
2118 continue;
2119 }
2120 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2121 track->enc->bit_rate);
2122 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2123 param_write_int(pb, "trackID", track_id);
2124 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2125 if (track->enc->codec_id == AV_CODEC_ID_H264) {
2126 uint8_t *ptr;
2127 int size = track->enc->extradata_size;
2128 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2129 &size)) {
2130 param_write_hex(pb, "CodecPrivateData",
2131 ptr ? ptr : track->enc->extradata,
2132 size);
2133 av_free(ptr);
2134 }
2135 param_write_string(pb, "FourCC", "H264");
2136 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2137 param_write_string(pb, "FourCC", "WVC1");
2138 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2139 track->enc->extradata_size);
2140 }
2141 param_write_int(pb, "MaxWidth", track->enc->width);
2142 param_write_int(pb, "MaxHeight", track->enc->height);
2143 param_write_int(pb, "DisplayWidth", track->enc->width);
2144 param_write_int(pb, "DisplayHeight", track->enc->height);
2145 } else {
2146 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2147 param_write_string(pb, "FourCC", "AACL");
2148 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2149 param_write_string(pb, "FourCC", "WMAP");
2150 }
2151 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2152 track->enc->extradata_size);
2153 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2154 track->enc->codec_id));
2155 param_write_int(pb, "Channels", track->enc->channels);
2156 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2157 param_write_int(pb, "BitsPerSample", 16);
2158 param_write_int(pb, "PacketSize", track->enc->block_align ?
2159 track->enc->block_align : 4);
2160 }
2161 avio_printf(pb, "</%s>\n", type);
2162 }
2163 avio_printf(pb, "</switch>\n");
2164 avio_printf(pb, "</body>\n");
2165 avio_printf(pb, "</smil>\n");
2166
2167 return update_size(pb, pos);
2168 }
2169
2170 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2171 {
2172 avio_wb32(pb, 16);
2173 ffio_wfourcc(pb, "mfhd");
2174 avio_wb32(pb, 0);
2175 avio_wb32(pb, mov->fragments);
2176 return 0;
2177 }
2178
2179 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2180 int64_t moof_offset)
2181 {
2182 int64_t pos = avio_tell(pb);
2183 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
2184 MOV_TFHD_BASE_DATA_OFFSET;
2185 if (!track->entry) {
2186 flags |= MOV_TFHD_DURATION_IS_EMPTY;
2187 } else {
2188 flags |= MOV_TFHD_DEFAULT_FLAGS;
2189 }
2190
2191 /* Don't set a default sample size, the silverlight player refuses
2192 * to play files with that set. Don't set a default sample duration,
2193 * WMP freaks out if it is set. */
2194 if (track->mode == MODE_ISM)
2195 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION);
2196
2197 avio_wb32(pb, 0); /* size placeholder */
2198 ffio_wfourcc(pb, "tfhd");
2199 avio_w8(pb, 0); /* version */
2200 avio_wb24(pb, flags);
2201
2202 avio_wb32(pb, track->track_id); /* track-id */
2203 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2204 avio_wb64(pb, moof_offset);
2205 if (flags & MOV_TFHD_DEFAULT_DURATION) {
2206 track->default_duration = get_cluster_duration(track, 0);
2207 avio_wb32(pb, track->default_duration);
2208 }
2209 if (flags & MOV_TFHD_DEFAULT_SIZE) {
2210 track->default_size = track->entry ? track->cluster[0].size : 1;
2211 avio_wb32(pb, track->default_size);
2212 } else
2213 track->default_size = -1;
2214
2215 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2216 track->default_sample_flags =
2217 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2218 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
2219 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
2220 avio_wb32(pb, track->default_sample_flags);
2221 }
2222
2223 return update_size(pb, pos);
2224 }
2225
2226 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2227 {
2228 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
2229 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
2230 }
2231
2232 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2233 {
2234 int64_t pos = avio_tell(pb);
2235 uint32_t flags = MOV_TRUN_DATA_OFFSET;
2236 int i;
2237
2238 for (i = 0; i < track->entry; i++) {
2239 if (get_cluster_duration(track, i) != track->default_duration)
2240 flags |= MOV_TRUN_SAMPLE_DURATION;
2241 if (track->cluster[i].size != track->default_size)
2242 flags |= MOV_TRUN_SAMPLE_SIZE;
2243 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2244 flags |= MOV_TRUN_SAMPLE_FLAGS;
2245 }
2246 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2247 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2248 if (track->flags & MOV_TRACK_CTTS)
2249 flags |= MOV_TRUN_SAMPLE_CTS;
2250
2251 avio_wb32(pb, 0); /* size placeholder */
2252 ffio_wfourcc(pb, "trun");
2253 avio_w8(pb, 0); /* version */
2254 avio_wb24(pb, flags);
2255
2256 avio_wb32(pb, track->entry); /* sample count */
2257 track->moof_size_offset = avio_tell(pb);
2258 avio_wb32(pb, 0); /* data offset */
2259 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2260 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2261
2262 for (i = 0; i < track->entry; i++) {
2263 if (flags & MOV_TRUN_SAMPLE_DURATION)
2264 avio_wb32(pb, get_cluster_duration(track, i));
2265 if (flags & MOV_TRUN_SAMPLE_SIZE)
2266 avio_wb32(pb, track->cluster[i].size);
2267 if (flags & MOV_TRUN_SAMPLE_FLAGS)
2268 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2269 if (flags & MOV_TRUN_SAMPLE_CTS)
2270 avio_wb32(pb, track->cluster[i].cts);
2271 }
2272
2273 return update_size(pb, pos);
2274 }
2275
2276 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2277 {
2278 int64_t pos = avio_tell(pb);
2279 const uint8_t uuid[] = {
2280 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2281 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2282 };
2283
2284 avio_wb32(pb, 0); /* size placeholder */
2285 ffio_wfourcc(pb, "uuid");
2286 avio_write(pb, uuid, sizeof(uuid));
2287 avio_w8(pb, 1);
2288 avio_wb24(pb, 0);
2289 avio_wb64(pb, track->frag_start);
2290 avio_wb64(pb, track->start_dts + track->track_duration -
2291 track->cluster[0].dts);
2292
2293 return update_size(pb, pos);
2294 }
2295
2296 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2297 MOVTrack *track, int entry)
2298 {
2299 int n = track->nb_frag_info - 1 - entry, i;
2300 int size = 8 + 16 + 4 + 1 + 16*n;
2301 const uint8_t uuid[] = {
2302 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2303 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2304 };
2305
2306 if (entry < 0)
2307 return 0;
2308
2309 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2310 avio_wb32(pb, size);
2311 ffio_wfourcc(pb, "uuid");
2312 avio_write(pb, uuid, sizeof(uuid));
2313 avio_w8(pb, 1);
2314 avio_wb24(pb, 0);
2315 avio_w8(pb, n);
2316 for (i = 0; i < n; i++) {
2317 int index = entry + 1 + i;
2318 avio_wb64(pb, track->frag_info[index].time);
2319 avio_wb64(pb, track->frag_info[index].duration);
2320 }
2321 if (n < mov->ism_lookahead) {
2322 int free_size = 16*(mov->ism_lookahead - n);
2323 avio_wb32(pb, free_size);
2324 ffio_wfourcc(pb, "free");
2325 for (i = 0; i < free_size - 8; i++)
2326 avio_w8(pb, 0);
2327 }
2328
2329 return 0;
2330 }
2331
2332 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2333 MOVTrack *track)
2334 {
2335 int64_t pos = avio_tell(pb);
2336 int i;
2337 for (i = 0; i < mov->ism_lookahead; i++) {
2338 /* Update the tfrf tag for the last ism_lookahead fragments,
2339 * nb_frag_info - 1 is the next fragment to be written. */
2340 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2341 }
2342 avio_seek(pb, pos, SEEK_SET);
2343 return 0;
2344 }
2345
2346 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2347 MOVTrack *track, int64_t moof_offset)
2348 {
2349 int64_t pos = avio_tell(pb);
2350 avio_wb32(pb, 0); /* size placeholder */
2351 ffio_wfourcc(pb, "traf");
2352
2353 mov_write_tfhd_tag(pb, track, moof_offset);
2354 mov_write_trun_tag(pb, track);
2355 if (mov->mode == MODE_ISM) {
2356 mov_write_tfxd_tag(pb, track);
2357
2358 if (mov->ism_lookahead) {
2359 int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
2360
2361 track->tfrf_offset = avio_tell(pb);
2362 avio_wb32(pb, 8 + size);
2363 ffio_wfourcc(pb, "free");
2364 for (i = 0; i < size; i++)
2365 avio_w8(pb, 0);
2366 }
2367 }
2368
2369 return update_size(pb, pos);
2370 }
2371
2372 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2373 {
2374 int64_t pos = avio_tell(pb), end;
2375 int i, moof_size;
2376
2377 avio_wb32(pb, 0); /* size placeholder */
2378 ffio_wfourcc(pb, "moof");
2379
2380 mov_write_mfhd_tag(pb, mov);
2381 for (i = 0; i < mov->nb_streams; i++) {
2382 MOVTrack *track = &mov->tracks[i];
2383 if (tracks >= 0 && i != tracks)
2384 continue;
2385 if (!track->entry)
2386 continue;
2387 mov_write_traf_tag(pb, mov, track, pos);
2388 }
2389
2390 end = avio_tell(pb);
2391 moof_size = end - pos;
2392 for (i = 0; i < mov->nb_streams; i++) {
2393 MOVTrack *track = &mov->tracks[i];
2394 if (tracks >= 0 && i != tracks)
2395 continue;
2396 if (!track->entry)
2397 continue;
2398 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2399 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2400 }
2401 avio_seek(pb, end, SEEK_SET);
2402
2403 return update_size(pb, pos);
2404 }
2405
2406 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2407 {
2408 int64_t pos = avio_tell(pb);
2409 int i;
2410
2411 avio_wb32(pb, 0); /* size placeholder */
2412 ffio_wfourcc(pb, "tfra");
2413 avio_w8(pb, 1); /* version */
2414 avio_wb24(pb, 0);
2415
2416 avio_wb32(pb, track->track_id);
2417 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2418 avio_wb32(pb, track->nb_frag_info);
2419 for (i = 0; i < track->nb_frag_info; i++) {
2420 avio_wb64(pb, track->frag_info[i].time);
2421 avio_wb64(pb, track->frag_info[i].offset);
2422 avio_w8(pb, 1); /* traf number */
2423 avio_w8(pb, 1); /* trun number */
2424 avio_w8(pb, 1); /* sample number */
2425 }
2426
2427 return update_size(pb, pos);
2428 }
2429
2430 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2431 {
2432 int64_t pos = avio_tell(pb);
2433 int i;
2434
2435 avio_wb32(pb, 0); /* size placeholder */
2436 ffio_wfourcc(pb, "mfra");
2437 /* An empty mfra atom is enough to indicate to the publishing point that
2438 * the stream has ended. */
2439 if (mov->flags & FF_MOV_FLAG_ISML)
2440 return update_size(pb, pos);
2441
2442 for (i = 0; i < mov->nb_streams; i++) {
2443 MOVTrack *track = &mov->tracks[i];
2444 if (track->nb_frag_info)
2445 mov_write_tfra_tag(pb, track);
2446 }
2447
2448 avio_wb32(pb, 16);
2449 ffio_wfourcc(pb, "mfro");
2450 avio_wb32(pb, 0); /* version + flags */
2451 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2452
2453 return update_size(pb, pos);
2454 }
2455
2456 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
2457 {
2458 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2459 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2460
2461 mov->mdat_pos = avio_tell(pb);
2462 avio_wb32(pb, 0); /* size placeholder*/
2463 ffio_wfourcc(pb, "mdat");
2464 return 0;
2465 }
2466
2467 /* TODO: This needs to be more general */
2468 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
2469 {
2470 MOVMuxContext *mov = s->priv_data;
2471 int64_t pos = avio_tell(pb);
2472 int has_h264 = 0, has_video = 0;
2473 int minor = 0x200;
2474 int i;
2475
2476 for (i = 0; i < s->nb_streams; i++) {
2477 AVStream *st = s->streams[i];
2478 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2479 has_video = 1;
2480 if (st->codec->codec_id == AV_CODEC_ID_H264)
2481 has_h264 = 1;
2482 }
2483
2484 avio_wb32(pb, 0); /* size */
2485 ffio_wfourcc(pb, "ftyp");
2486
2487 if (mov->mode == MODE_3GP) {
2488 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2489 minor = has_h264 ? 0x100 : 0x200;
2490 } else if (mov->mode & MODE_3G2) {
2491 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2492 minor = has_h264 ? 0x20000 : 0x10000;
2493 }else if (mov->mode == MODE_PSP)
2494 ffio_wfourcc(pb, "MSNV");
2495 else if (mov->mode == MODE_MP4)
2496 ffio_wfourcc(pb, "isom");
2497 else if (mov->mode == MODE_IPOD)
2498 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2499 else if (mov->mode == MODE_ISM)
2500 ffio_wfourcc(pb, "isml");
2501 else
2502 ffio_wfourcc(pb, "qt ");
2503
2504 avio_wb32(pb, minor);
2505
2506 if(mov->mode == MODE_MOV)
2507 ffio_wfourcc(pb, "qt ");
2508 else if (mov->mode == MODE_ISM) {
2509 ffio_wfourcc(pb, "piff");
2510 ffio_wfourcc(pb, "iso2");
2511 } else {
2512 ffio_wfourcc(pb, "isom");
2513 ffio_wfourcc(pb, "iso2");
2514 if(has_h264)
2515 ffio_wfourcc(pb, "avc1");
2516 }
2517
2518 if (mov->mode == MODE_3GP)
2519 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2520 else if (mov->mode & MODE_3G2)
2521 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2522 else if (mov->mode == MODE_PSP)
2523 ffio_wfourcc(pb, "MSNV");
2524 else if (mov->mode == MODE_MP4)
2525 ffio_wfourcc(pb, "mp41");
2526 return update_size(pb, pos);
2527 }
2528
2529 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
2530 {
2531 AVCodecContext *video_codec = s->streams[0]->codec;
2532 AVCodecContext *audio_codec = s->streams[1]->codec;
2533 int audio_rate = audio_codec->sample_rate;
2534 int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
2535 int audio_kbitrate = audio_codec->bit_rate / 1000;
2536 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2537
2538 avio_wb32(pb, 0x94); /* size */
2539 ffio_wfourcc(pb, "uuid");
2540 ffio_wfourcc(pb, "PROF");
2541
2542 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2543 avio_wb32(pb, 0xbb88695c);
2544 avio_wb32(pb, 0xfac9c740);
2545
2546 avio_wb32(pb, 0x0); /* ? */
2547 avio_wb32(pb, 0x3); /* 3 sections ? */
2548
2549 avio_wb32(pb, 0x14); /* size */
2550 ffio_wfourcc(pb, "FPRF");
2551 avio_wb32(pb, 0x0); /* ? */
2552 avio_wb32(pb, 0x0); /* ? */
2553 avio_wb32(pb, 0x0); /* ? */
2554
2555 avio_wb32(pb, 0x2c); /* size */
2556 ffio_wfourcc(pb, "APRF");/* audio */
2557 avio_wb32(pb, 0x0);
2558 avio_wb32(pb, 0x2); /* TrackID */
2559 ffio_wfourcc(pb, "mp4a");
2560 avio_wb32(pb, 0x20f);
2561 avio_wb32(pb, 0x0);
2562 avio_wb32(pb, audio_kbitrate);
2563 avio_wb32(pb, audio_kbitrate);
2564 avio_wb32(pb, audio_rate);
2565 avio_wb32(pb, audio_codec->channels);
2566
2567 avio_wb32(pb, 0x34); /* size */
2568 ffio_wfourcc(pb, "VPRF"); /* video */
2569 avio_wb32(pb, 0x0);
2570 avio_wb32(pb, 0x1); /* TrackID */
2571 if (video_codec->codec_id == AV_CODEC_ID_H264) {
2572 ffio_wfourcc(pb, "avc1");
2573 avio_wb16(pb, 0x014D);
2574 avio_wb16(pb, 0x0015);
2575 } else {
2576 ffio_wfourcc(pb, "mp4v");
2577 avio_wb16(pb, 0x0000);
2578 avio_wb16(pb, 0x0103);
2579 }
2580 avio_wb32(pb, 0x0);
2581 avio_wb32(pb, video_kbitrate);
2582 avio_wb32(pb, video_kbitrate);
2583 avio_wb32(pb, frame_rate);
2584 avio_wb32(pb, frame_rate);
2585 avio_wb16(pb, video_codec->width);
2586 avio_wb16(pb, video_codec->height);
2587 avio_wb32(pb, 0x010001); /* ? */
2588 }
2589
2590 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2591 {
2592 uint32_t c = -1;
2593 int i, closed_gop = 0;
2594
2595 for (i = 0; i < pkt->size - 4; i++) {
2596 c = (c<<8) + pkt->data[i];
2597 if (c == 0x1b8) { // gop
2598 closed_gop = pkt->data[i+4]>>6 & 0x01;
2599 } else if (c == 0x100) { // pic
2600 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2601 if (!temp_ref || closed_gop) // I picture is not reordered
2602 *flags = MOV_SYNC_SAMPLE;
2603 else
2604 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2605 break;
2606 }
2607 }
2608 return 0;
2609 }
2610
2611 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2612 {
2613 const uint8_t *start, *next, *end = pkt->data + pkt->size;
2614 int seq = 0, entry = 0;
2615 int key = pkt->flags & AV_PKT_FLAG_KEY;
2616 start = find_next_marker(pkt->data, end);
2617 for (next = start; next < end; start = next) {
2618 next = find_next_marker(start + 4, end);
2619 switch (AV_RB32(start)) {
2620 case VC1_CODE_SEQHDR:
2621 seq = 1;
2622 break;
2623 case VC1_CODE_ENTRYPOINT:
2624 entry = 1;
2625 break;
2626 case VC1_CODE_SLICE:
2627 trk->vc1_info.slices = 1;
2628 break;
2629 }
2630 }
2631 if (!trk->entry && !fragment) {
2632 /* First packet in first fragment */
2633 trk->vc1_info.first_packet_seq = seq;
2634 trk->vc1_info.first_packet_entry = entry;
2635 } else if ((seq && !trk->vc1_info.packet_seq) ||
2636 (entry && !trk->vc1_info.packet_entry)) {
2637 int i;
2638 for (i = 0; i < trk->entry; i++)
2639 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2640 trk->has_keyframes = 0;
2641 if (seq)
2642 trk->vc1_info.packet_seq = 1;
2643 if (entry)
2644 trk->vc1_info.packet_entry = 1;
2645 if (!fragment) {
2646 /* First fragment */
2647 if ((!seq || trk->vc1_info.first_packet_seq) &&
2648 (!entry || trk->vc1_info.first_packet_entry)) {
2649 /* First packet had the same headers as this one, readd the
2650 * sync sample flag. */
2651 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2652 trk->has_keyframes = 1;
2653 }
2654 }
2655 }
2656 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2657 key = seq && entry;
2658 else if (trk->vc1_info.packet_seq)
2659 key = seq;
2660 else if (trk->vc1_info.packet_entry)
2661 key = entry;
2662 if (key) {
2663 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2664 trk->has_keyframes++;
2665 }
2666 }
2667
2668 static int mov_flush_fragment(AVFormatContext *s)
2669 {
2670 MOVMuxContext *mov = s->priv_data;
2671 int i, first_track = -1;
2672 int64_t mdat_size = 0;
2673
2674 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2675 return 0;
2676
2677 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2678 int64_t pos = avio_tell(s->pb);
2679 int ret;
2680 AVIOContext *moov_buf;
2681 uint8_t *buf;
2682 int buf_size;
2683
2684 for (i = 0; i < mov->nb_streams; i++)
2685 if (!mov->tracks[i].entry)
2686 break;
2687 /* Don't write the initial moov unless all tracks have data */
2688 if (i < mov->nb_streams)
2689 return 0;
2690
2691 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
2692 return ret;
2693 mov_write_moov_tag(moov_buf, mov, s);
2694 buf_size = avio_close_dyn_buf(moov_buf, &buf);
2695 av_free(buf);
2696 for (i = 0; i < mov->nb_streams; i++)
2697 mov->tracks[i].data_offset = pos + buf_size + 8;
2698
2699 mov_write_moov_tag(s->pb, mov, s);
2700
2701 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2702 mov->mdat_buf = NULL;
2703 avio_wb32(s->pb, buf_size + 8);
2704 ffio_wfourcc(s->pb, "mdat");
2705 avio_write(s->pb, buf, buf_size);
2706 av_free(buf);
2707
2708 mov->fragments++;
2709 mov->mdat_size = 0;
2710 for (i = 0; i < mov->nb_streams; i++) {
2711 if (mov->tracks[i].entry)
2712 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2713 mov->tracks[i].track_duration -
2714 mov->tracks[i].cluster[0].dts;
2715 mov->tracks[i].entry = 0;
2716 }
2717 avio_flush(s->pb);
2718 return 0;
2719 }
2720
2721 for (i = 0; i < mov->nb_streams; i++) {
2722 MOVTrack *track = &mov->tracks[i];
2723 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2724 track->data_offset = 0;
2725 else
2726 track->data_offset = mdat_size;
2727 if (!track->mdat_buf)
2728 continue;
2729 mdat_size += avio_tell(track->mdat_buf);
2730 if (first_track < 0)
2731 first_track = i;
2732 }
2733
2734 if (!mdat_size)
2735 return 0;
2736
2737 for (i = 0; i < mov->nb_streams; i++) {
2738 MOVTrack *track = &mov->tracks[i];
2739 int buf_size, write_moof = 1, moof_tracks = -1;
2740 uint8_t *buf;
2741 int64_t duration = 0;
2742
2743 if (track->entry)
2744 duration = track->start_dts + track->track_duration -
2745 track->cluster[0].dts;
2746 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2747 if (!track->mdat_buf)
2748 continue;
2749 mdat_size = avio_tell(track->mdat_buf);
2750 moof_tracks = i;
2751 } else {
2752 write_moof = i == first_track;
2753 }
2754
2755 if (write_moof) {
2756 MOVFragmentInfo *info;
2757 avio_flush(s->pb);
2758 track->nb_frag_info++;
2759 track->frag_info = av_realloc(track->frag_info,
2760 sizeof(*track->frag_info) *
2761 track->nb_frag_info);
2762 info = &track->frag_info[track->nb_frag_info - 1];
2763 info->offset = avio_tell(s->pb);
2764 info->time = mov->tracks[i].frag_start;
2765 info->duration = duration;
2766 mov_write_tfrf_tags(s->pb, mov, track);
2767
2768 mov_write_moof_tag(s->pb, mov, moof_tracks);
2769 info->tfrf_offset = track->tfrf_offset;
2770 mov->fragments++;
2771
2772 avio_wb32(s->pb, mdat_size + 8);
2773 ffio_wfourcc(s->pb, "mdat");
2774 }
2775
2776 if (track->entry)
2777 track->frag_start += duration;
2778 track->entry = 0;
2779 if (!track->mdat_buf)
2780 continue;
2781 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2782 track->mdat_buf = NULL;
2783
2784 avio_write(s->pb, buf, buf_size);
2785 av_free(buf);
2786 }
2787
2788 mov->mdat_size = 0;
2789
2790 avio_flush(s->pb);
2791 return 0;
2792 }
2793
2794 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2795 {
2796 MOVMuxContext *mov = s->priv_data;
2797 AVIOContext *pb = s->pb;
2798 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2799 AVCodecContext *enc = trk->enc;
2800 unsigned int samples_in_chunk = 0;
2801 int size= pkt->size;
2802 uint8_t *reformatted_data = NULL;
2803
2804 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2805 int ret;
2806 if (mov->fragments > 0) {
2807 if (!trk->mdat_buf) {
2808 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2809 return ret;
2810 }
2811 pb = trk->mdat_buf;
2812 } else {
2813 if (!mov->mdat_buf) {
2814 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2815 return ret;
2816 }
2817 pb = mov->mdat_buf;
2818 }
2819 }
2820
2821 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
2822 /* We must find out how many AMR blocks there are in one packet */
2823 static uint16_t packed_size[16] =
2824 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
2825 int len = 0;
2826
2827 while (len < size && samples_in_chunk < 100) {
2828 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2829 samples_in_chunk++;
2830 }
2831 if (samples_in_chunk > 1) {
2832 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2833 return -1;
2834 }
2835 } else if (trk->sample_size)
2836 samples_in_chunk = size / trk->sample_size;
2837 else
2838 samples_in_chunk = 1;
2839
2840 /* copy extradata if it exists */
2841 if (trk->vos_len == 0 && enc->extradata_size > 0) {
2842 trk->vos_len = enc->extradata_size;
2843 trk->vos_data = av_malloc(trk->vos_len);
2844 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
2845 }
2846
2847 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
2848 /* from x264 or from bytestream h264 */
2849 /* nal reformating needed */
2850 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2851 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2852 &size);
2853 avio_write(pb, reformatted_data, size);
2854 } else {
2855 size =