Libav
movenc.c
Go to the documentation of this file.
1 /*
2  * MOV, 3GP, MP4 muxer
3  * Copyright (c) 2003 Thomas Raivio
4  * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <stdint.h>
25 
26 #include "movenc.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "riff.h"
30 #include "avio.h"
31 #include "isom.h"
32 #include "avc.h"
33 #include "libavcodec/get_bits.h"
34 #include "libavcodec/put_bits.h"
35 #include "libavcodec/vc1.h"
36 #include "internal.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/intfloat.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/dict.h"
42 #include "hevc.h"
43 #include "rtpenc.h"
44 #include "mov_chan.h"
45 
46 #undef NDEBUG
47 #include <assert.h>
48 
49 static const AVOption options[] = {
50  { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
51  { "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" },
52  { "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" },
53  { "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" },
54  { "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" },
55  { "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" },
56  { "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" },
57  { "faststart", "Run a second pass to put the index (moov atom) at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
58  { "omit_tfhd_offset", "Omit the base data offset in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_OMIT_TFHD_OFFSET}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
59  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
60  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
61  { "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},
62  { "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},
63  { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
64  { "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},
65  { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
66  { "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},
67  { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
68  { NULL },
69 };
70 
71 #define MOV_CLASS(flavor)\
72 static const AVClass flavor ## _muxer_class = {\
73  .class_name = #flavor " muxer",\
74  .item_name = av_default_item_name,\
75  .option = options,\
76  .version = LIBAVUTIL_VERSION_INT,\
77 };
78 
79 //FIXME support 64 bit variant with wide placeholders
80 static int64_t update_size(AVIOContext *pb, int64_t pos)
81 {
82  int64_t curpos = avio_tell(pb);
83  avio_seek(pb, pos, SEEK_SET);
84  avio_wb32(pb, curpos - pos); /* rewrite size */
85  avio_seek(pb, curpos, SEEK_SET);
86 
87  return curpos - pos;
88 }
89 
90 static int co64_required(const MOVTrack *track)
91 {
92  if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
93  return 1;
94  return 0;
95 }
96 
97 /* Chunk offset atom */
98 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
99 {
100  int i;
101  int mode64 = co64_required(track); // use 32 bit size variant if possible
102  int64_t pos = avio_tell(pb);
103  avio_wb32(pb, 0); /* size */
104  if (mode64) {
105  ffio_wfourcc(pb, "co64");
106  } else
107  ffio_wfourcc(pb, "stco");
108  avio_wb32(pb, 0); /* version & flags */
109  avio_wb32(pb, track->entry); /* entry count */
110  for (i = 0; i < track->entry; i++) {
111  if (mode64 == 1)
112  avio_wb64(pb, track->cluster[i].pos + track->data_offset);
113  else
114  avio_wb32(pb, track->cluster[i].pos + track->data_offset);
115  }
116  return update_size(pb, pos);
117 }
118 
119 /* Sample size atom */
120 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
121 {
122  int equalChunks = 1;
123  int i, j, entries = 0, tst = -1, oldtst = -1;
124 
125  int64_t pos = avio_tell(pb);
126  avio_wb32(pb, 0); /* size */
127  ffio_wfourcc(pb, "stsz");
128  avio_wb32(pb, 0); /* version & flags */
129 
130  for (i = 0; i < track->entry; i++) {
131  tst = track->cluster[i].size / track->cluster[i].entries;
132  if (oldtst != -1 && tst != oldtst)
133  equalChunks = 0;
134  oldtst = tst;
135  entries += track->cluster[i].entries;
136  }
137  if (equalChunks && track->entry) {
138  int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
139  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
140  avio_wb32(pb, sSize); // sample size
141  avio_wb32(pb, entries); // sample count
142  } else {
143  avio_wb32(pb, 0); // sample size
144  avio_wb32(pb, entries); // sample count
145  for (i = 0; i < track->entry; i++) {
146  for (j = 0; j < track->cluster[i].entries; j++) {
147  avio_wb32(pb, track->cluster[i].size /
148  track->cluster[i].entries);
149  }
150  }
151  }
152  return update_size(pb, pos);
153 }
154 
155 /* Sample to chunk atom */
156 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
157 {
158  int index = 0, oldval = -1, i;
159  int64_t entryPos, curpos;
160 
161  int64_t pos = avio_tell(pb);
162  avio_wb32(pb, 0); /* size */
163  ffio_wfourcc(pb, "stsc");
164  avio_wb32(pb, 0); // version & flags
165  entryPos = avio_tell(pb);
166  avio_wb32(pb, track->entry); // entry count
167  for (i = 0; i < track->entry; i++) {
168  if (oldval != track->cluster[i].samples_in_chunk) {
169  avio_wb32(pb, i + 1); // first chunk
170  avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
171  avio_wb32(pb, 0x1); // sample description index
172  oldval = track->cluster[i].samples_in_chunk;
173  index++;
174  }
175  }
176  curpos = avio_tell(pb);
177  avio_seek(pb, entryPos, SEEK_SET);
178  avio_wb32(pb, index); // rewrite size
179  avio_seek(pb, curpos, SEEK_SET);
180 
181  return update_size(pb, pos);
182 }
183 
184 /* Sync sample atom */
185 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
186 {
187  int64_t curpos, entryPos;
188  int i, index = 0;
189  int64_t pos = avio_tell(pb);
190  avio_wb32(pb, 0); // size
191  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
192  avio_wb32(pb, 0); // version & flags
193  entryPos = avio_tell(pb);
194  avio_wb32(pb, track->entry); // entry count
195  for (i = 0; i < track->entry; i++) {
196  if (track->cluster[i].flags & flag) {
197  avio_wb32(pb, i + 1);
198  index++;
199  }
200  }
201  curpos = avio_tell(pb);
202  avio_seek(pb, entryPos, SEEK_SET);
203  avio_wb32(pb, index); // rewrite size
204  avio_seek(pb, curpos, SEEK_SET);
205  return update_size(pb, pos);
206 }
207 
208 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
209 {
210  avio_wb32(pb, 0x11); /* size */
211  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
212  else ffio_wfourcc(pb, "damr");
213  ffio_wfourcc(pb, "FFMP");
214  avio_w8(pb, 0); /* decoder version */
215 
216  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
217  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
218  avio_w8(pb, 0x01); /* Frames per sample */
219  return 0x11;
220 }
221 
222 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
223 {
224  GetBitContext gbc;
225  PutBitContext pbc;
226  uint8_t buf[3];
227  int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
228 
229  if (track->vos_len < 7)
230  return -1;
231 
232  avio_wb32(pb, 11);
233  ffio_wfourcc(pb, "dac3");
234 
235  init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
236  fscod = get_bits(&gbc, 2);
237  frmsizecod = get_bits(&gbc, 6);
238  bsid = get_bits(&gbc, 5);
239  bsmod = get_bits(&gbc, 3);
240  acmod = get_bits(&gbc, 3);
241  if (acmod == 2) {
242  skip_bits(&gbc, 2); // dsurmod
243  } else {
244  if ((acmod & 1) && acmod != 1)
245  skip_bits(&gbc, 2); // cmixlev
246  if (acmod & 4)
247  skip_bits(&gbc, 2); // surmixlev
248  }
249  lfeon = get_bits1(&gbc);
250 
251  init_put_bits(&pbc, buf, sizeof(buf));
252  put_bits(&pbc, 2, fscod);
253  put_bits(&pbc, 5, bsid);
254  put_bits(&pbc, 3, bsmod);
255  put_bits(&pbc, 3, acmod);
256  put_bits(&pbc, 1, lfeon);
257  put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
258  put_bits(&pbc, 5, 0); // reserved
259 
260  flush_put_bits(&pbc);
261  avio_write(pb, buf, sizeof(buf));
262 
263  return 11;
264 }
265 
271 {
272  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
273  return track->enc->extradata_size;
274 }
275 
276 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
277 {
278  int i = 3;
279  avio_w8(pb, tag);
280  for (; i > 0; i--)
281  avio_w8(pb, (size >> (7 * i)) | 0x80);
282  avio_w8(pb, size & 0x7F);
283 }
284 
285 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
286 {
287  int64_t pos = avio_tell(pb);
288  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
289 
290  avio_wb32(pb, 0); // size
291  ffio_wfourcc(pb, "esds");
292  avio_wb32(pb, 0); // Version
293 
294  // ES descriptor
295  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
296  avio_wb16(pb, track->track_id);
297  avio_w8(pb, 0x00); // flags (= no flags)
298 
299  // DecoderConfig descriptor
300  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
301 
302  // Object type indication
303  if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
304  track->enc->codec_id == AV_CODEC_ID_MP3) &&
305  track->enc->sample_rate > 24000)
306  avio_w8(pb, 0x6B); // 11172-3
307  else
309 
310  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
311  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
312  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
313  avio_w8(pb, 0x15); // flags (= Audiostream)
314  else
315  avio_w8(pb, 0x11); // flags (= Visualstream)
316 
317  avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
318 
319  avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
320  if (track->enc->rc_max_rate != track->enc->rc_min_rate ||
321  track->enc->rc_min_rate == 0)
322  avio_wb32(pb, 0); // vbr
323  else
324  avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
325 
326  if (track->vos_len) {
327  // DecoderSpecific info descriptor
328  put_descr(pb, 0x05, track->vos_len);
329  avio_write(pb, track->vos_data, track->vos_len);
330  }
331 
332  // SL descriptor
333  put_descr(pb, 0x06, 1);
334  avio_w8(pb, 0x02);
335  return update_size(pb, pos);
336 }
337 
338 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
339 {
340  int64_t pos = avio_tell(pb);
341  avio_wb32(pb, 0);
342  avio_wl32(pb, track->tag); // store it byteswapped
343  track->enc->codec_tag = av_bswap16(track->tag >> 16);
344  ff_put_wav_header(pb, track->enc);
345  return update_size(pb, pos);
346 }
347 
348 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
349 {
350  int64_t pos = avio_tell(pb);
351  avio_wb32(pb, 0);
352  ffio_wfourcc(pb, "wfex");
353  ff_put_wav_header(pb, track->enc);
354  return update_size(pb, pos);
355 }
356 
357 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
358 {
359  uint32_t layout_tag, bitmap;
360  int64_t pos = avio_tell(pb);
361 
362  layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
363  track->enc->channel_layout,
364  &bitmap);
365  if (!layout_tag) {
366  av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
367  "lack of channel information\n");
368  return 0;
369  }
370 
371  avio_wb32(pb, 0); // Size
372  ffio_wfourcc(pb, "chan"); // Type
373  avio_w8(pb, 0); // Version
374  avio_wb24(pb, 0); // Flags
375  avio_wb32(pb, layout_tag); // mChannelLayoutTag
376  avio_wb32(pb, bitmap); // mChannelBitmap
377  avio_wb32(pb, 0); // mNumberChannelDescriptions
378 
379  return update_size(pb, pos);
380 }
381 
382 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
383 {
384  int64_t pos = avio_tell(pb);
385 
386  avio_wb32(pb, 0); /* size */
387  ffio_wfourcc(pb, "wave");
388 
389  avio_wb32(pb, 12); /* size */
390  ffio_wfourcc(pb, "frma");
391  avio_wl32(pb, track->tag);
392 
393  if (track->enc->codec_id == AV_CODEC_ID_AAC) {
394  /* useless atom needed by mplayer, ipod, not needed by quicktime */
395  avio_wb32(pb, 12); /* size */
396  ffio_wfourcc(pb, "mp4a");
397  avio_wb32(pb, 0);
398  mov_write_esds_tag(pb, track);
399  } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
400  mov_write_amr_tag(pb, track);
401  } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
402  mov_write_ac3_tag(pb, track);
403  } else if (track->enc->codec_id == AV_CODEC_ID_ALAC) {
404  mov_write_extradata_tag(pb, track);
405  } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
407  mov_write_ms_tag(pb, track);
408  }
409 
410  avio_wb32(pb, 8); /* size */
411  avio_wb32(pb, 0); /* null tag */
412 
413  return update_size(pb, pos);
414 }
415 
416 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
417 {
418  uint8_t *unescaped;
419  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
420  int unescaped_size, seq_found = 0;
421  int level = 0, interlace = 0;
422  int packet_seq = track->vc1_info.packet_seq;
423  int packet_entry = track->vc1_info.packet_entry;
424  int slices = track->vc1_info.slices;
425  PutBitContext pbc;
426 
427  if (track->start_dts == AV_NOPTS_VALUE) {
428  /* No packets written yet, vc1_info isn't authoritative yet. */
429  /* Assume inline sequence and entry headers. This will be
430  * overwritten at the end if the file is seekable. */
431  packet_seq = packet_entry = 1;
432  }
433 
434  unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
435  if (!unescaped)
436  return AVERROR(ENOMEM);
437  start = find_next_marker(track->vos_data, end);
438  for (next = start; next < end; start = next) {
439  GetBitContext gb;
440  int size;
441  next = find_next_marker(start + 4, end);
442  size = next - start - 4;
443  if (size <= 0)
444  continue;
445  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
446  init_get_bits(&gb, unescaped, 8 * unescaped_size);
447  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
448  int profile = get_bits(&gb, 2);
449  if (profile != PROFILE_ADVANCED) {
450  av_free(unescaped);
451  return AVERROR(ENOSYS);
452  }
453  seq_found = 1;
454  level = get_bits(&gb, 3);
455  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
456  * width, height */
457  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
458  skip_bits(&gb, 1); /* broadcast */
459  interlace = get_bits1(&gb);
460  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
461  }
462  }
463  if (!seq_found) {
464  av_free(unescaped);
465  return AVERROR(ENOSYS);
466  }
467 
468  init_put_bits(&pbc, buf, 7);
469  /* VC1DecSpecStruc */
470  put_bits(&pbc, 4, 12); /* profile - advanced */
471  put_bits(&pbc, 3, level);
472  put_bits(&pbc, 1, 0); /* reserved */
473  /* VC1AdvDecSpecStruc */
474  put_bits(&pbc, 3, level);
475  put_bits(&pbc, 1, 0); /* cbr */
476  put_bits(&pbc, 6, 0); /* reserved */
477  put_bits(&pbc, 1, !interlace); /* no interlace */
478  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
479  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
480  put_bits(&pbc, 1, !slices); /* no slice code */
481  put_bits(&pbc, 1, 0); /* no bframe */
482  put_bits(&pbc, 1, 0); /* reserved */
483  put_bits32(&pbc, track->enc->time_base.den); /* framerate */
484  flush_put_bits(&pbc);
485 
486  av_free(unescaped);
487 
488  return 0;
489 }
490 
491 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
492 {
493  uint8_t buf[7] = { 0 };
494  int ret;
495 
496  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
497  return ret;
498 
499  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
500  ffio_wfourcc(pb, "dvc1");
501  track->vc1_info.struct_offset = avio_tell(pb);
502  avio_write(pb, buf, sizeof(buf));
503  avio_write(pb, track->vos_data, track->vos_len);
504 
505  return 0;
506 }
507 
508 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
509 {
510  avio_wb32(pb, track->vos_len + 8);
511  ffio_wfourcc(pb, "glbl");
512  avio_write(pb, track->vos_data, track->vos_len);
513  return 8 + track->vos_len;
514 }
515 
521 {
522  switch (codec_id) {
525  return 11;
528  return 9;
529  case AV_CODEC_ID_PCM_U8:
530  return 10;
534  return 14;
535  case AV_CODEC_ID_PCM_S8:
539  return 12;
540  default:
541  return 0;
542  }
543 }
544 
545 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
546 {
547  int64_t next_dts;
548 
549  if (cluster_idx >= track->entry)
550  return 0;
551 
552  if (cluster_idx + 1 == track->entry)
553  next_dts = track->track_duration + track->start_dts;
554  else
555  next_dts = track->cluster[cluster_idx + 1].dts;
556 
557  return next_dts - track->cluster[cluster_idx].dts;
558 }
559 
561 {
562  int i, first_duration;
563 
564  /* use 1 for raw PCM */
565  if (!track->audio_vbr)
566  return 1;
567 
568  /* check to see if duration is constant for all clusters */
569  if (!track->entry)
570  return 0;
571  first_duration = get_cluster_duration(track, 0);
572  for (i = 1; i < track->entry; i++) {
573  if (get_cluster_duration(track, i) != first_duration)
574  return 0;
575  }
576  return first_duration;
577 }
578 
579 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
580 {
581  int64_t pos = avio_tell(pb);
582  int version = 0;
583  uint32_t tag = track->tag;
584 
585  if (track->mode == MODE_MOV) {
586  if (mov_get_lpcm_flags(track->enc->codec_id))
587  tag = AV_RL32("lpcm");
588  version = 2;
589  }
590 
591  avio_wb32(pb, 0); /* size */
592  avio_wl32(pb, tag); // store it byteswapped
593  avio_wb32(pb, 0); /* Reserved */
594  avio_wb16(pb, 0); /* Reserved */
595  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
596 
597  /* SoundDescription */
598  avio_wb16(pb, version); /* Version */
599  avio_wb16(pb, 0); /* Revision level */
600  avio_wb32(pb, 0); /* Reserved */
601 
602  if (version == 2) {
603  avio_wb16(pb, 3);
604  avio_wb16(pb, 16);
605  avio_wb16(pb, 0xfffe);
606  avio_wb16(pb, 0);
607  avio_wb32(pb, 0x00010000);
608  avio_wb32(pb, 72);
609  avio_wb64(pb, av_double2int(track->enc->sample_rate));
610  avio_wb32(pb, track->enc->channels);
611  avio_wb32(pb, 0x7F000000);
614  avio_wb32(pb, track->sample_size);
615  avio_wb32(pb, get_samples_per_packet(track));
616  } else {
617  /* reserved for mp4/3gp */
618  avio_wb16(pb, 2);
619  avio_wb16(pb, 16);
620  avio_wb16(pb, 0);
621 
622  avio_wb16(pb, 0); /* packet size (= 0) */
623  avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
624  track->enc->sample_rate : 0);
625  avio_wb16(pb, 0); /* Reserved */
626  }
627 
628  if (track->mode == MODE_MOV &&
629  (track->enc->codec_id == AV_CODEC_ID_AAC ||
630  track->enc->codec_id == AV_CODEC_ID_AC3 ||
631  track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
632  track->enc->codec_id == AV_CODEC_ID_ALAC ||
633  track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
635  mov_write_wave_tag(pb, track);
636  else if (track->tag == MKTAG('m','p','4','a'))
637  mov_write_esds_tag(pb, track);
638  else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
639  mov_write_amr_tag(pb, track);
640  else if (track->enc->codec_id == AV_CODEC_ID_AC3)
641  mov_write_ac3_tag(pb, track);
642  else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
643  mov_write_extradata_tag(pb, track);
644  else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
645  mov_write_wfex_tag(pb, track);
646  else if (track->vos_len > 0)
647  mov_write_glbl_tag(pb, track);
648 
649  if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
650  mov_write_chan_tag(pb, track);
651 
652  return update_size(pb, pos);
653 }
654 
656 {
657  avio_wb32(pb, 0xf); /* size */
658  ffio_wfourcc(pb, "d263");
659  ffio_wfourcc(pb, "FFMP");
660  avio_w8(pb, 0); /* decoder version */
661  /* FIXME use AVCodecContext level/profile, when encoder will set values */
662  avio_w8(pb, 0xa); /* level */
663  avio_w8(pb, 0); /* profile */
664  return 0xf;
665 }
666 
667 /* TODO: No idea about these values */
669 {
670  avio_wb32(pb, 0x15);
671  ffio_wfourcc(pb, "SMI ");
672  ffio_wfourcc(pb, "SEQH");
673  avio_wb32(pb, 0x5);
674  avio_wb32(pb, 0xe2c0211d);
675  avio_wb32(pb, 0xc0000000);
676  avio_w8(pb, 0);
677  return 0x15;
678 }
679 
680 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
681 {
682  int64_t pos = avio_tell(pb);
683 
684  avio_wb32(pb, 0);
685  ffio_wfourcc(pb, "avcC");
686  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
687  return update_size(pb, pos);
688 }
689 
690 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
691 {
692  int64_t pos = avio_tell(pb);
693 
694  avio_wb32(pb, 0);
695  ffio_wfourcc(pb, "hvcC");
696  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
697  return update_size(pb, pos);
698 }
699 
700 /* also used by all avid codecs (dv, imx, meridien) and their variants */
701 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
702 {
703  int i;
704  avio_wb32(pb, 24); /* size */
705  ffio_wfourcc(pb, "ACLR");
706  ffio_wfourcc(pb, "ACLR");
707  ffio_wfourcc(pb, "0001");
708  avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
709  avio_wb32(pb, 0); /* unknown */
710 
711  avio_wb32(pb, 24); /* size */
712  ffio_wfourcc(pb, "APRG");
713  ffio_wfourcc(pb, "APRG");
714  ffio_wfourcc(pb, "0001");
715  avio_wb32(pb, 1); /* unknown */
716  avio_wb32(pb, 0); /* unknown */
717 
718  avio_wb32(pb, 120); /* size */
719  ffio_wfourcc(pb, "ARES");
720  ffio_wfourcc(pb, "ARES");
721  ffio_wfourcc(pb, "0001");
722  avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
723  avio_wb32(pb, track->enc->width);
724  /* values below are based on samples created with quicktime and avid codecs */
725  if (track->vos_data[5] & 2) { // interlaced
726  avio_wb32(pb, track->enc->height / 2);
727  avio_wb32(pb, 2); /* unknown */
728  avio_wb32(pb, 0); /* unknown */
729  avio_wb32(pb, 4); /* unknown */
730  } else {
731  avio_wb32(pb, track->enc->height);
732  avio_wb32(pb, 1); /* unknown */
733  avio_wb32(pb, 0); /* unknown */
734  if (track->enc->height == 1080)
735  avio_wb32(pb, 5); /* unknown */
736  else
737  avio_wb32(pb, 6); /* unknown */
738  }
739  /* padding */
740  for (i = 0; i < 10; i++)
741  avio_wb64(pb, 0);
742 
743  /* extra padding for stsd needed */
744  avio_wb32(pb, 0);
745  return 0;
746 }
747 
749 {
750  int tag = track->enc->codec_tag;
751 
753  return 0;
754 
755  if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
756  else if (track->enc->codec_id == AV_CODEC_ID_HEVC) tag = MKTAG('h','e','v','1');
757  else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
758  else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
759  else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
760  else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
761  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
762  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
763 
764  return tag;
765 }
766 
767 static const AVCodecTag codec_ipod_tags[] = {
768  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
769  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
770  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
771  { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
772  { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
773  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
774  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
775  { AV_CODEC_ID_NONE, 0 },
776 };
777 
779 {
780  int tag = track->enc->codec_tag;
781 
782  // keep original tag for subs, ipod supports both formats
783  if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
784  (tag == MKTAG('t', 'x', '3', 'g') ||
785  tag == MKTAG('t', 'e', 'x', 't'))))
786  tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
787 
788  if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
789  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
790  "Quicktime/Ipod might not play the file\n");
791 
792  return tag;
793 }
794 
796 {
797  int tag;
798 
799  if (track->enc->width == 720) /* SD */
800  if (track->enc->height == 480) /* NTSC */
801  if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
802  else tag = MKTAG('d','v','c',' ');
803  else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
804  else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
805  else tag = MKTAG('d','v','p','p');
806  else if (track->enc->height == 720) /* HD 720 line */
807  if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
808  else tag = MKTAG('d','v','h','p');
809  else if (track->enc->height == 1080) /* HD 1080 line */
810  if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
811  else tag = MKTAG('d','v','h','6');
812  else {
813  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
814  return 0;
815  }
816 
817  return tag;
818 }
819 
820 static const struct {
822  uint32_t tag;
823  unsigned bps;
824 } mov_pix_fmt_tags[] = {
825  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
826  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
827  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
828  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
829  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
830  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
831  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
832  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
833  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
834  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
835  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
836  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
837  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
838  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
839 };
840 
842 {
843  int tag = track->enc->codec_tag;
844  int i;
845 
846  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
847  if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
848  tag = mov_pix_fmt_tags[i].tag;
849  track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
850  break;
851  }
852  }
853 
854  return tag;
855 }
856 
858 {
859  int tag = track->enc->codec_tag;
860 
861  if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
862  (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
863  track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
864  track->enc->codec_id == AV_CODEC_ID_H263 ||
865  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
866  if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
867  tag = mov_get_dv_codec_tag(s, track);
868  else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
869  tag = mov_get_rawvideo_codec_tag(s, track);
870  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
872  if (!tag) { // if no mac fcc found, try with Microsoft tags
874  if (tag)
875  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
876  "the file may be unplayable!\n");
877  }
878  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
880  if (!tag) { // if no mac fcc found, try with Microsoft tags
881  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
882  if (ms_tag) {
883  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
884  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
885  "the file may be unplayable!\n");
886  }
887  }
888  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
890  }
891 
892  return tag;
893 }
894 
895 static const AVCodecTag codec_3gp_tags[] = {
896  { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
897  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
898  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
899  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
900  { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
901  { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
902  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
903  { AV_CODEC_ID_NONE, 0 },
904 };
905 
906 static const AVCodecTag codec_f4v_tags[] = {
907  { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
908  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
909  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
910  { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
911  { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
912  { AV_CODEC_ID_NONE, 0 },
913 };
914 
916 {
917  int tag;
918 
919  if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
920  tag = mp4_get_codec_tag(s, track);
921  else if (track->mode == MODE_ISM) {
922  tag = mp4_get_codec_tag(s, track);
923  if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
924  tag = MKTAG('w', 'm', 'a', ' ');
925  } else if (track->mode == MODE_IPOD)
926  tag = ipod_get_codec_tag(s, track);
927  else if (track->mode & MODE_3GP)
928  tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
929  else if (track->mode == MODE_F4V)
930  tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
931  else
932  tag = mov_get_codec_tag(s, track);
933 
934  return tag;
935 }
936 
942 {
943  avio_wb32(pb, 28);
944  ffio_wfourcc(pb, "uuid");
945  avio_wb32(pb, 0x6b6840f2);
946  avio_wb32(pb, 0x5f244fc5);
947  avio_wb32(pb, 0xba39a51b);
948  avio_wb32(pb, 0xcf0323f3);
949  avio_wb32(pb, 0x0);
950  return 28;
951 }
952 
953 static const uint16_t fiel_data[] = {
954  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
955 };
956 
957 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
958 {
959  unsigned mov_field_order = 0;
960  if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
961  mov_field_order = fiel_data[track->enc->field_order];
962  else
963  return 0;
964  avio_wb32(pb, 10);
965  ffio_wfourcc(pb, "fiel");
966  avio_wb16(pb, mov_field_order);
967  return 10;
968 }
969 
971 {
972  int64_t pos = avio_tell(pb);
973  avio_wb32(pb, 0); /* size */
974  avio_wl32(pb, track->tag); // store it byteswapped
975  avio_wb32(pb, 0); /* Reserved */
976  avio_wb16(pb, 0); /* Reserved */
977  avio_wb16(pb, 1); /* Data-reference index */
978 
979  if (track->enc->extradata_size)
980  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
981 
982  return update_size(pb, pos);
983 }
984 
985 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
986 {
987  AVRational sar;
988  av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
989  track->enc->sample_aspect_ratio.den, INT_MAX);
990 
991  avio_wb32(pb, 16);
992  ffio_wfourcc(pb, "pasp");
993  avio_wb32(pb, sar.num);
994  avio_wb32(pb, sar.den);
995  return 16;
996 }
997 
998 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
999 {
1000  int64_t pos = avio_tell(pb);
1001  char compressor_name[32] = { 0 };
1002 
1003  avio_wb32(pb, 0); /* size */
1004  avio_wl32(pb, track->tag); // store it byteswapped
1005  avio_wb32(pb, 0); /* Reserved */
1006  avio_wb16(pb, 0); /* Reserved */
1007  avio_wb16(pb, 1); /* Data-reference index */
1008 
1009  avio_wb16(pb, 0); /* Codec stream version */
1010  avio_wb16(pb, 0); /* Codec stream revision (=0) */
1011  if (track->mode == MODE_MOV) {
1012  ffio_wfourcc(pb, "FFMP"); /* Vendor */
1013  if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1014  avio_wb32(pb, 0); /* Temporal Quality */
1015  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1016  } else {
1017  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1018  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1019  }
1020  } else {
1021  avio_wb32(pb, 0); /* Reserved */
1022  avio_wb32(pb, 0); /* Reserved */
1023  avio_wb32(pb, 0); /* Reserved */
1024  }
1025  avio_wb16(pb, track->enc->width); /* Video width */
1026  avio_wb16(pb, track->height); /* Video height */
1027  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1028  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1029  avio_wb32(pb, 0); /* Data size (= 0) */
1030  avio_wb16(pb, 1); /* Frame count (= 1) */
1031 
1032  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1033  if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
1034  av_strlcpy(compressor_name, track->enc->codec->name, 32);
1035  avio_w8(pb, strlen(compressor_name));
1036  avio_write(pb, compressor_name, 31);
1037 
1038  if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1039  avio_wb16(pb, track->enc->bits_per_coded_sample);
1040  else
1041  avio_wb16(pb, 0x18); /* Reserved */
1042  avio_wb16(pb, 0xffff); /* Reserved */
1043  if (track->tag == MKTAG('m','p','4','v'))
1044  mov_write_esds_tag(pb, track);
1045  else if (track->enc->codec_id == AV_CODEC_ID_H263)
1046  mov_write_d263_tag(pb);
1047  else if (track->enc->codec_id == AV_CODEC_ID_SVQ3)
1048  mov_write_svq3_tag(pb);
1049  else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
1050  mov_write_avid_tag(pb, track);
1051  else if (track->enc->codec_id == AV_CODEC_ID_HEVC)
1052  mov_write_hvcc_tag(pb, track);
1053  else if (track->enc->codec_id == AV_CODEC_ID_H264) {
1054  mov_write_avcc_tag(pb, track);
1055  if (track->mode == MODE_IPOD)
1057  } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1058  mov_write_fiel_tag(pb, track);
1059  else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1060  mov_write_dvc1_tag(pb, track);
1061  else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
1062  track->enc->codec_id == AV_CODEC_ID_VP6A) {
1063  /* Don't write any potential extradata here - the cropping
1064  * is signalled via the normal width/height fields. */
1065  } else if (track->vos_len > 0)
1066  mov_write_glbl_tag(pb, track);
1067 
1068  if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1069  track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1070  mov_write_pasp_tag(pb, track);
1071  }
1072 
1073  return update_size(pb, pos);
1074 }
1075 
1076 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1077 {
1078  int64_t pos = avio_tell(pb);
1079  avio_wb32(pb, 0); /* size */
1080  ffio_wfourcc(pb, "rtp ");
1081  avio_wb32(pb, 0); /* Reserved */
1082  avio_wb16(pb, 0); /* Reserved */
1083  avio_wb16(pb, 1); /* Data-reference index */
1084 
1085  avio_wb16(pb, 1); /* Hint track version */
1086  avio_wb16(pb, 1); /* Highest compatible version */
1087  avio_wb32(pb, track->max_packet_size); /* Max packet size */
1088 
1089  avio_wb32(pb, 12); /* size */
1090  ffio_wfourcc(pb, "tims");
1091  avio_wb32(pb, track->timescale);
1092 
1093  return update_size(pb, pos);
1094 }
1095 
1096 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1097 {
1098  int64_t pos = avio_tell(pb);
1099 
1100  avio_wb32(pb, 0); /* size */
1101  ffio_wfourcc(pb, "tmcd"); /* Data format */
1102  avio_wb32(pb, 0); /* Reserved */
1103  avio_wb32(pb, 1); /* Data reference index */
1104  if (track->enc->extradata_size)
1105  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1106  return update_size(pb, pos);
1107 }
1108 
1109 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1110 {
1111  int64_t pos = avio_tell(pb);
1112  avio_wb32(pb, 0); /* size */
1113  ffio_wfourcc(pb, "stsd");
1114  avio_wb32(pb, 0); /* version & flags */
1115  avio_wb32(pb, 1); /* entry count */
1116  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1117  mov_write_video_tag(pb, track);
1118  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1119  mov_write_audio_tag(pb, track);
1120  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1121  mov_write_subtitle_tag(pb, track);
1122  else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1123  mov_write_rtp_tag(pb, track);
1124  else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1125  mov_write_tmcd_tag(pb, track);
1126  return update_size(pb, pos);
1127 }
1128 
1129 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1130 {
1131  MOVStts *ctts_entries;
1132  uint32_t entries = 0;
1133  uint32_t atom_size;
1134  int i;
1135 
1136  ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1137  ctts_entries[0].count = 1;
1138  ctts_entries[0].duration = track->cluster[0].cts;
1139  for (i = 1; i < track->entry; i++) {
1140  if (track->cluster[i].cts == ctts_entries[entries].duration) {
1141  ctts_entries[entries].count++; /* compress */
1142  } else {
1143  entries++;
1144  ctts_entries[entries].duration = track->cluster[i].cts;
1145  ctts_entries[entries].count = 1;
1146  }
1147  }
1148  entries++; /* last one */
1149  atom_size = 16 + (entries * 8);
1150  avio_wb32(pb, atom_size); /* size */
1151  ffio_wfourcc(pb, "ctts");
1152  avio_wb32(pb, 0); /* version & flags */
1153  avio_wb32(pb, entries); /* entry count */
1154  for (i = 0; i < entries; i++) {
1155  avio_wb32(pb, ctts_entries[i].count);
1156  avio_wb32(pb, ctts_entries[i].duration);
1157  }
1158  av_free(ctts_entries);
1159  return atom_size;
1160 }
1161 
1162 /* Time to sample atom */
1163 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1164 {
1165  MOVStts *stts_entries;
1166  uint32_t entries = -1;
1167  uint32_t atom_size;
1168  int i;
1169 
1170  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1171  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1172  stts_entries[0].count = track->sample_count;
1173  stts_entries[0].duration = 1;
1174  entries = 1;
1175  } else {
1176  stts_entries = track->entry ?
1177  av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1178  NULL;
1179  for (i = 0; i < track->entry; i++) {
1180  int duration = get_cluster_duration(track, i);
1181  if (i && duration == stts_entries[entries].duration) {
1182  stts_entries[entries].count++; /* compress */
1183  } else {
1184  entries++;
1185  stts_entries[entries].duration = duration;
1186  stts_entries[entries].count = 1;
1187  }
1188  }
1189  entries++; /* last one */
1190  }
1191  atom_size = 16 + (entries * 8);
1192  avio_wb32(pb, atom_size); /* size */
1193  ffio_wfourcc(pb, "stts");
1194  avio_wb32(pb, 0); /* version & flags */
1195  avio_wb32(pb, entries); /* entry count */
1196  for (i = 0; i < entries; i++) {
1197  avio_wb32(pb, stts_entries[i].count);
1198  avio_wb32(pb, stts_entries[i].duration);
1199  }
1200  av_free(stts_entries);
1201  return atom_size;
1202 }
1203 
1205 {
1206  avio_wb32(pb, 28); /* size */
1207  ffio_wfourcc(pb, "dref");
1208  avio_wb32(pb, 0); /* version & flags */
1209  avio_wb32(pb, 1); /* entry count */
1210 
1211  avio_wb32(pb, 0xc); /* size */
1212  ffio_wfourcc(pb, "url ");
1213  avio_wb32(pb, 1); /* version & flags */
1214 
1215  return 28;
1216 }
1217 
1218 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1219 {
1220  int64_t pos = avio_tell(pb);
1221  avio_wb32(pb, 0); /* size */
1222  ffio_wfourcc(pb, "stbl");
1223  mov_write_stsd_tag(pb, track);
1224  mov_write_stts_tag(pb, track);
1225  if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1226  track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1227  track->has_keyframes && track->has_keyframes < track->entry)
1228  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1229  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1231  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1232  track->flags & MOV_TRACK_CTTS)
1233  mov_write_ctts_tag(pb, track);
1234  mov_write_stsc_tag(pb, track);
1235  mov_write_stsz_tag(pb, track);
1236  mov_write_stco_tag(pb, track);
1237  return update_size(pb, pos);
1238 }
1239 
1241 {
1242  int64_t pos = avio_tell(pb);
1243  avio_wb32(pb, 0); /* size */
1244  ffio_wfourcc(pb, "dinf");
1245  mov_write_dref_tag(pb);
1246  return update_size(pb, pos);
1247 }
1248 
1250 {
1251  avio_wb32(pb, 12);
1252  ffio_wfourcc(pb, "nmhd");
1253  avio_wb32(pb, 0);
1254  return 12;
1255 }
1256 
1258 {
1259  avio_wb32(pb, 0x20); /* size */
1260  ffio_wfourcc(pb, "gmhd");
1261  avio_wb32(pb, 0x18); /* gmin size */
1262  ffio_wfourcc(pb, "gmin");/* generic media info */
1263  avio_wb32(pb, 0); /* version & flags */
1264  avio_wb16(pb, 0x40); /* graphics mode = */
1265  avio_wb16(pb, 0x8000); /* opColor (r?) */
1266  avio_wb16(pb, 0x8000); /* opColor (g?) */
1267  avio_wb16(pb, 0x8000); /* opColor (b?) */
1268  avio_wb16(pb, 0); /* balance */
1269  avio_wb16(pb, 0); /* reserved */
1270  return 0x20;
1271 }
1272 
1274 {
1275  avio_wb32(pb, 16); /* size */
1276  ffio_wfourcc(pb, "smhd");
1277  avio_wb32(pb, 0); /* version & flags */
1278  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1279  avio_wb16(pb, 0); /* reserved */
1280  return 16;
1281 }
1282 
1284 {
1285  avio_wb32(pb, 0x14); /* size (always 0x14) */
1286  ffio_wfourcc(pb, "vmhd");
1287  avio_wb32(pb, 0x01); /* version & flags */
1288  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1289  return 0x14;
1290 }
1291 
1292 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1293 {
1294  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1295  int64_t pos = avio_tell(pb);
1296 
1297  hdlr = "dhlr";
1298  hdlr_type = "url ";
1299  descr = "DataHandler";
1300 
1301  if (track) {
1302  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1303  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1304  hdlr_type = "vide";
1305  descr = "VideoHandler";
1306  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1307  hdlr_type = "soun";
1308  descr = "SoundHandler";
1309  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1310  if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1311  else hdlr_type = "text";
1312  descr = "SubtitleHandler";
1313  } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1314  hdlr_type = "hint";
1315  descr = "HintHandler";
1316  } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1317  hdlr_type = "tmcd";
1318  descr = "TimeCodeHandler";
1319  } else {
1320  char tag_buf[32];
1321  av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1322  track->enc->codec_tag);
1323 
1324  av_log(track->enc, AV_LOG_WARNING,
1325  "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1326  tag_buf, track->enc->codec_tag);
1327  }
1328  }
1329 
1330  avio_wb32(pb, 0); /* size */
1331  ffio_wfourcc(pb, "hdlr");
1332  avio_wb32(pb, 0); /* Version & flags */
1333  avio_write(pb, hdlr, 4); /* handler */
1334  ffio_wfourcc(pb, hdlr_type); /* handler type */
1335  avio_wb32(pb, 0); /* reserved */
1336  avio_wb32(pb, 0); /* reserved */
1337  avio_wb32(pb, 0); /* reserved */
1338  if (!track || track->mode == MODE_MOV)
1339  avio_w8(pb, strlen(descr)); /* pascal string */
1340  avio_write(pb, descr, strlen(descr)); /* handler description */
1341  if (track && track->mode != MODE_MOV)
1342  avio_w8(pb, 0); /* c string */
1343  return update_size(pb, pos);
1344 }
1345 
1347 {
1348  /* This atom must be present, but leaving the values at zero
1349  * seems harmless. */
1350  avio_wb32(pb, 28); /* size */
1351  ffio_wfourcc(pb, "hmhd");
1352  avio_wb32(pb, 0); /* version, flags */
1353  avio_wb16(pb, 0); /* maxPDUsize */
1354  avio_wb16(pb, 0); /* avgPDUsize */
1355  avio_wb32(pb, 0); /* maxbitrate */
1356  avio_wb32(pb, 0); /* avgbitrate */
1357  avio_wb32(pb, 0); /* reserved */
1358  return 28;
1359 }
1360 
1361 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1362 {
1363  int64_t pos = avio_tell(pb);
1364  avio_wb32(pb, 0); /* size */
1365  ffio_wfourcc(pb, "minf");
1366  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1367  mov_write_vmhd_tag(pb);
1368  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1369  mov_write_smhd_tag(pb);
1370  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1371  if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1372  else mov_write_nmhd_tag(pb);
1373  } else if (track->tag == MKTAG('r','t','p',' ')) {
1374  mov_write_hmhd_tag(pb);
1375  } else if (track->tag == MKTAG('t','m','c','d')) {
1376  mov_write_gmhd_tag(pb);
1377  }
1378  if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1379  mov_write_hdlr_tag(pb, NULL);
1380  mov_write_dinf_tag(pb);
1381  mov_write_stbl_tag(pb, track);
1382  return update_size(pb, pos);
1383 }
1384 
1385 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1386 {
1387  int version = track->track_duration < INT32_MAX ? 0 : 1;
1388 
1389  if (track->mode == MODE_ISM)
1390  version = 1;
1391 
1392  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1393  ffio_wfourcc(pb, "mdhd");
1394  avio_w8(pb, version);
1395  avio_wb24(pb, 0); /* flags */
1396  if (version == 1) {
1397  avio_wb64(pb, track->time);
1398  avio_wb64(pb, track->time);
1399  } else {
1400  avio_wb32(pb, track->time); /* creation time */
1401  avio_wb32(pb, track->time); /* modification time */
1402  }
1403  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1404  if (!track->entry)
1405  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1406  else
1407  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1408  avio_wb16(pb, track->language); /* language */
1409  avio_wb16(pb, 0); /* reserved (quality) */
1410 
1411  if (version != 0 && track->mode == MODE_MOV) {
1413  "FATAL error, file duration too long for timebase, this file will not be\n"
1414  "playable with quicktime. Choose a different timebase or a different\n"
1415  "container format\n");
1416  }
1417 
1418  return 32;
1419 }
1420 
1421 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1422 {
1423  int64_t pos = avio_tell(pb);
1424  avio_wb32(pb, 0); /* size */
1425  ffio_wfourcc(pb, "mdia");
1426  mov_write_mdhd_tag(pb, track);
1427  mov_write_hdlr_tag(pb, track);
1428  mov_write_minf_tag(pb, track);
1429  return update_size(pb, pos);
1430 }
1431 
1432 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1433 {
1435  track->timescale, AV_ROUND_UP);
1436  int version = duration < INT32_MAX ? 0 : 1;
1437 
1438  if (track->mode == MODE_ISM)
1439  version = 1;
1440 
1441  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1442  ffio_wfourcc(pb, "tkhd");
1443  avio_w8(pb, version);
1444  avio_wb24(pb, (track->flags & MOV_TRACK_ENABLED) ?
1447  if (version == 1) {
1448  avio_wb64(pb, track->time);
1449  avio_wb64(pb, track->time);
1450  } else {
1451  avio_wb32(pb, track->time); /* creation time */
1452  avio_wb32(pb, track->time); /* modification time */
1453  }
1454  avio_wb32(pb, track->track_id); /* track-id */
1455  avio_wb32(pb, 0); /* reserved */
1456  if (!track->entry)
1457  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1458  else
1459  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1460 
1461  avio_wb32(pb, 0); /* reserved */
1462  avio_wb32(pb, 0); /* reserved */
1463  avio_wb16(pb, 0); /* layer */
1464  avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1465  /* Volume, only for audio */
1466  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1467  avio_wb16(pb, 0x0100);
1468  else
1469  avio_wb16(pb, 0);
1470  avio_wb16(pb, 0); /* reserved */
1471 
1472  /* Matrix structure */
1473  avio_wb32(pb, 0x00010000); /* reserved */
1474  avio_wb32(pb, 0x0); /* reserved */
1475  avio_wb32(pb, 0x0); /* reserved */
1476  avio_wb32(pb, 0x0); /* reserved */
1477  avio_wb32(pb, 0x00010000); /* reserved */
1478  avio_wb32(pb, 0x0); /* reserved */
1479  avio_wb32(pb, 0x0); /* reserved */
1480  avio_wb32(pb, 0x0); /* reserved */
1481  avio_wb32(pb, 0x40000000); /* reserved */
1482 
1483  /* Track width and height, for visual only */
1484  if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1485  track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1486  if (track->mode == MODE_MOV) {
1487  avio_wb32(pb, track->enc->width << 16);
1488  avio_wb32(pb, track->height << 16);
1489  } else {
1490  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1491  if (!sample_aspect_ratio || track->height != track->enc->height)
1492  sample_aspect_ratio = 1;
1493  avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1494  avio_wb32(pb, track->height * 0x10000);
1495  }
1496  } else {
1497  avio_wb32(pb, 0);
1498  avio_wb32(pb, 0);
1499  }
1500  return 0x5c;
1501 }
1502 
1503 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1504 {
1506  track->enc->sample_aspect_ratio.den);
1507 
1508  int64_t pos = avio_tell(pb);
1509 
1510  avio_wb32(pb, 0); /* size */
1511  ffio_wfourcc(pb, "tapt");
1512 
1513  avio_wb32(pb, 20);
1514  ffio_wfourcc(pb, "clef");
1515  avio_wb32(pb, 0);
1516  avio_wb32(pb, width << 16);
1517  avio_wb32(pb, track->enc->height << 16);
1518 
1519  avio_wb32(pb, 20);
1520  ffio_wfourcc(pb, "enof");
1521  avio_wb32(pb, 0);
1522  avio_wb32(pb, track->enc->width << 16);
1523  avio_wb32(pb, track->enc->height << 16);
1524 
1525  return update_size(pb, pos);
1526 }
1527 
1528 // This box seems important for the psp playback ... without it the movie seems to hang
1529 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1530 {
1532  track->timescale, AV_ROUND_UP);
1533  int version = duration < INT32_MAX ? 0 : 1;
1534  int entry_size, entry_count, size;
1535  int64_t delay, start_ct = track->cluster[0].cts;
1536  delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1537  track->timescale, AV_ROUND_DOWN);
1538  version |= delay < INT32_MAX ? 0 : 1;
1539 
1540  entry_size = (version == 1) ? 20 : 12;
1541  entry_count = 1 + (delay > 0);
1542  size = 24 + entry_count * entry_size;
1543 
1544  /* write the atom data */
1545  avio_wb32(pb, size);
1546  ffio_wfourcc(pb, "edts");
1547  avio_wb32(pb, size - 8);
1548  ffio_wfourcc(pb, "elst");
1549  avio_w8(pb, version);
1550  avio_wb24(pb, 0); /* flags */
1551 
1552  avio_wb32(pb, entry_count);
1553  if (delay > 0) { /* add an empty edit to delay presentation */
1554  if (version == 1) {
1555  avio_wb64(pb, delay);
1556  avio_wb64(pb, -1);
1557  } else {
1558  avio_wb32(pb, delay);
1559  avio_wb32(pb, -1);
1560  }
1561  avio_wb32(pb, 0x00010000);
1562  }
1563 
1564  /* duration */
1565  if (version == 1) {
1566  avio_wb64(pb, duration);
1567  avio_wb64(pb, start_ct);
1568  } else {
1569  avio_wb32(pb, duration);
1570  avio_wb32(pb, start_ct);
1571  }
1572  avio_wb32(pb, 0x00010000);
1573  return size;
1574 }
1575 
1576 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1577 {
1578  avio_wb32(pb, 20); // size
1579  ffio_wfourcc(pb, "tref");
1580  avio_wb32(pb, 12); // size (subatom)
1581  avio_wl32(pb, track->tref_tag);
1582  avio_wb32(pb, track->tref_id);
1583  return 20;
1584 }
1585 
1586 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1588 {
1589  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1590  ffio_wfourcc(pb, "uuid");
1591  ffio_wfourcc(pb, "USMT");
1592  avio_wb32(pb, 0x21d24fce);
1593  avio_wb32(pb, 0xbb88695c);
1594  avio_wb32(pb, 0xfac9c740);
1595  avio_wb32(pb, 0x1c); // another size here!
1596  ffio_wfourcc(pb, "MTDT");
1597  avio_wb32(pb, 0x00010012);
1598  avio_wb32(pb, 0x0a);
1599  avio_wb32(pb, 0x55c40000);
1600  avio_wb32(pb, 0x1);
1601  avio_wb32(pb, 0x0);
1602  return 0x34;
1603 }
1604 
1605 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1606 {
1607  AVFormatContext *ctx = track->rtp_ctx;
1608  char buf[1000] = "";
1609  int len;
1610 
1611  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1612  NULL, NULL, 0, 0, ctx);
1613  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1614  len = strlen(buf);
1615 
1616  avio_wb32(pb, len + 24);
1617  ffio_wfourcc(pb, "udta");
1618  avio_wb32(pb, len + 16);
1619  ffio_wfourcc(pb, "hnti");
1620  avio_wb32(pb, len + 8);
1621  ffio_wfourcc(pb, "sdp ");
1622  avio_write(pb, buf, len);
1623  return len + 24;
1624 }
1625 
1627  MOVTrack *track, AVStream *st)
1628 {
1629  int64_t pos = avio_tell(pb);
1630  avio_wb32(pb, 0); /* size */
1631  ffio_wfourcc(pb, "trak");
1632  mov_write_tkhd_tag(pb, track, st);
1633  if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1634  (track->entry && track->cluster[0].dts)) {
1635  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1636  mov_write_edts_tag(pb, track); // PSP Movies require edts box
1637  }
1638  if (track->tref_tag)
1639  mov_write_tref_tag(pb, track);
1640  mov_write_mdia_tag(pb, track);
1641  if (track->mode == MODE_PSP)
1642  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
1643  if (track->tag == MKTAG('r','t','p',' '))
1644  mov_write_udta_sdp(pb, track);
1645  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1646  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1647  if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1648  mov_write_tapt_tag(pb, track);
1649  }
1650  return update_size(pb, pos);
1651 }
1652 
1654 {
1655  int i, has_audio = 0, has_video = 0;
1656  int64_t pos = avio_tell(pb);
1657  int audio_profile = mov->iods_audio_profile;
1658  int video_profile = mov->iods_video_profile;
1659  for (i = 0; i < mov->nb_streams; i++) {
1660  if (mov->tracks[i].entry > 0) {
1661  has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1662  has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1663  }
1664  }
1665  if (audio_profile < 0)
1666  audio_profile = 0xFF - has_audio;
1667  if (video_profile < 0)
1668  video_profile = 0xFF - has_video;
1669  avio_wb32(pb, 0x0); /* size */
1670  ffio_wfourcc(pb, "iods");
1671  avio_wb32(pb, 0); /* version & flags */
1672  put_descr(pb, 0x10, 7);
1673  avio_wb16(pb, 0x004f);
1674  avio_w8(pb, 0xff);
1675  avio_w8(pb, 0xff);
1676  avio_w8(pb, audio_profile);
1677  avio_w8(pb, video_profile);
1678  avio_w8(pb, 0xff);
1679  return update_size(pb, pos);
1680 }
1681 
1682 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1683 {
1684  avio_wb32(pb, 0x20); /* size */
1685  ffio_wfourcc(pb, "trex");
1686  avio_wb32(pb, 0); /* version & flags */
1687  avio_wb32(pb, track->track_id); /* track ID */
1688  avio_wb32(pb, 1); /* default sample description index */
1689  avio_wb32(pb, 0); /* default sample duration */
1690  avio_wb32(pb, 0); /* default sample size */
1691  avio_wb32(pb, 0); /* default sample flags */
1692  return 0;
1693 }
1694 
1696 {
1697  int64_t pos = avio_tell(pb);
1698  int i;
1699  avio_wb32(pb, 0x0); /* size */
1700  ffio_wfourcc(pb, "mvex");
1701  for (i = 0; i < mov->nb_streams; i++)
1702  mov_write_trex_tag(pb, &mov->tracks[i]);
1703  return update_size(pb, pos);
1704 }
1705 
1707 {
1708  int max_track_id = 1, i;
1709  int64_t max_track_len_temp, max_track_len = 0;
1710  int version;
1711 
1712  for (i = 0; i < mov->nb_streams; i++) {
1713  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
1714  max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1715  MOV_TIMESCALE,
1716  mov->tracks[i].timescale,
1717  AV_ROUND_UP);
1718  if (max_track_len < max_track_len_temp)
1719  max_track_len = max_track_len_temp;
1720  if (max_track_id < mov->tracks[i].track_id)
1721  max_track_id = mov->tracks[i].track_id;
1722  }
1723  }
1724 
1725  version = max_track_len < UINT32_MAX ? 0 : 1;
1726  (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1727  ffio_wfourcc(pb, "mvhd");
1728  avio_w8(pb, version);
1729  avio_wb24(pb, 0); /* flags */
1730  if (version == 1) {
1731  avio_wb64(pb, mov->time);
1732  avio_wb64(pb, mov->time);
1733  } else {
1734  avio_wb32(pb, mov->time); /* creation time */
1735  avio_wb32(pb, mov->time); /* modification time */
1736  }
1737  avio_wb32(pb, MOV_TIMESCALE);
1738  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1739 
1740  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1741  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1742  avio_wb16(pb, 0); /* reserved */
1743  avio_wb32(pb, 0); /* reserved */
1744  avio_wb32(pb, 0); /* reserved */
1745 
1746  /* Matrix structure */
1747  avio_wb32(pb, 0x00010000); /* reserved */
1748  avio_wb32(pb, 0x0); /* reserved */
1749  avio_wb32(pb, 0x0); /* reserved */
1750  avio_wb32(pb, 0x0); /* reserved */
1751  avio_wb32(pb, 0x00010000); /* reserved */
1752  avio_wb32(pb, 0x0); /* reserved */
1753  avio_wb32(pb, 0x0); /* reserved */
1754  avio_wb32(pb, 0x0); /* reserved */
1755  avio_wb32(pb, 0x40000000); /* reserved */
1756 
1757  avio_wb32(pb, 0); /* reserved (preview time) */
1758  avio_wb32(pb, 0); /* reserved (preview duration) */
1759  avio_wb32(pb, 0); /* reserved (poster time) */
1760  avio_wb32(pb, 0); /* reserved (selection time) */
1761  avio_wb32(pb, 0); /* reserved (selection duration) */
1762  avio_wb32(pb, 0); /* reserved (current time) */
1763  avio_wb32(pb, max_track_id + 1); /* Next track id */
1764  return 0x6c;
1765 }
1766 
1768  AVFormatContext *s)
1769 {
1770  avio_wb32(pb, 33); /* size */
1771  ffio_wfourcc(pb, "hdlr");
1772  avio_wb32(pb, 0);
1773  avio_wb32(pb, 0);
1774  ffio_wfourcc(pb, "mdir");
1775  ffio_wfourcc(pb, "appl");
1776  avio_wb32(pb, 0);
1777  avio_wb32(pb, 0);
1778  avio_w8(pb, 0);
1779  return 33;
1780 }
1781 
1782 /* helper function to write a data tag with the specified string as data */
1783 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1784 {
1785  if (long_style) {
1786  int size = 16 + strlen(data);
1787  avio_wb32(pb, size); /* size */
1788  ffio_wfourcc(pb, "data");
1789  avio_wb32(pb, 1);
1790  avio_wb32(pb, 0);
1791  avio_write(pb, data, strlen(data));
1792  return size;
1793  } else {
1794  if (!lang)
1795  lang = ff_mov_iso639_to_lang("und", 1);
1796  avio_wb16(pb, strlen(data)); /* string length */
1797  avio_wb16(pb, lang);
1798  avio_write(pb, data, strlen(data));
1799  return strlen(data) + 4;
1800  }
1801 }
1802 
1803 static int mov_write_string_tag(AVIOContext *pb, const char *name,
1804  const char *value, int lang, int long_style)
1805 {
1806  int size = 0;
1807  if (value && value[0]) {
1808  int64_t pos = avio_tell(pb);
1809  avio_wb32(pb, 0); /* size */
1810  ffio_wfourcc(pb, name);
1811  mov_write_string_data_tag(pb, value, lang, long_style);
1812  size = update_size(pb, pos);
1813  }
1814  return size;
1815 }
1816 
1818  const char *name, const char *tag,
1819  int long_style)
1820 {
1821  int l, lang = 0, len, len2;
1822  AVDictionaryEntry *t, *t2 = NULL;
1823  char tag2[16];
1824 
1825  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1826  return 0;
1827 
1828  len = strlen(t->key);
1829  snprintf(tag2, sizeof(tag2), "%s-", tag);
1830  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1831  len2 = strlen(t2->key);
1832  if (len2 == len + 4 && !strcmp(t->value, t2->value)
1833  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
1834  lang = l;
1835  break;
1836  }
1837  }
1838  return mov_write_string_tag(pb, name, t->value, lang, long_style);
1839 }
1840 
1841 /* iTunes track number */
1843  AVFormatContext *s)
1844 {
1845  AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1846  int size = 0, track = t ? atoi(t->value) : 0;
1847  if (track) {
1848  avio_wb32(pb, 32); /* size */
1849  ffio_wfourcc(pb, "trkn");
1850  avio_wb32(pb, 24); /* size */
1851  ffio_wfourcc(pb, "data");
1852  avio_wb32(pb, 0); // 8 bytes empty
1853  avio_wb32(pb, 0);
1854  avio_wb16(pb, 0); // empty
1855  avio_wb16(pb, track); // track number
1856  avio_wb16(pb, 0); // total track number
1857  avio_wb16(pb, 0); // empty
1858  size = 32;
1859  }
1860  return size;
1861 }
1862 
1863 /* iTunes meta data list */
1865  AVFormatContext *s)
1866 {
1867  int64_t pos = avio_tell(pb);
1868  avio_wb32(pb, 0); /* size */
1869  ffio_wfourcc(pb, "ilst");
1870  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1871  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1872  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1873  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1874  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1875  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1876  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
1877  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1878  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1879  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1880  mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1881  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1882  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1883  mov_write_string_metadata(s, pb, "desc", "description",1);
1884  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1885  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1886  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1887  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1888  mov_write_trkn_tag(pb, mov, s);
1889  return update_size(pb, pos);
1890 }
1891 
1892 /* iTunes meta data tag */
1894  AVFormatContext *s)
1895 {
1896  int size = 0;
1897  int64_t pos = avio_tell(pb);
1898  avio_wb32(pb, 0); /* size */
1899  ffio_wfourcc(pb, "meta");
1900  avio_wb32(pb, 0);
1901  mov_write_itunes_hdlr_tag(pb, mov, s);
1902  mov_write_ilst_tag(pb, mov, s);
1903  size = update_size(pb, pos);
1904  return size;
1905 }
1906 
1907 static int utf8len(const uint8_t *b)
1908 {
1909  int len = 0;
1910  int val;
1911  while (*b) {
1912  GET_UTF8(val, *b++, return -1;)
1913  len++;
1914  }
1915  return len;
1916 }
1917 
1918 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1919 {
1920  int val;
1921  while (*b) {
1922  GET_UTF8(val, *b++, return -1;)
1923  avio_wb16(pb, val);
1924  }
1925  avio_wb16(pb, 0x00);
1926  return 0;
1927 }
1928 
1929 static uint16_t language_code(const char *str)
1930 {
1931  return (((str[0] - 0x60) & 0x1F) << 10) +
1932  (((str[1] - 0x60) & 0x1F) << 5) +
1933  (( str[2] - 0x60) & 0x1F);
1934 }
1935 
1937  const char *tag, const char *str)
1938 {
1939  int64_t pos = avio_tell(pb);
1940  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1941  if (!t || !utf8len(t->value))
1942  return 0;
1943  avio_wb32(pb, 0); /* size */
1944  ffio_wfourcc(pb, tag); /* type */
1945  avio_wb32(pb, 0); /* version + flags */
1946  if (!strcmp(tag, "yrrc"))
1947  avio_wb16(pb, atoi(t->value));
1948  else {
1949  avio_wb16(pb, language_code("eng")); /* language */
1950  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
1951  if (!strcmp(tag, "albm") &&
1952  (t = av_dict_get(s->metadata, "track", NULL, 0)))
1953  avio_w8(pb, atoi(t->value));
1954  }
1955  return update_size(pb, pos);
1956 }
1957 
1959 {
1960  int64_t pos = avio_tell(pb);
1961  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1962 
1963  avio_wb32(pb, 0); // size
1964  ffio_wfourcc(pb, "chpl");
1965  avio_wb32(pb, 0x01000000); // version + flags
1966  avio_wb32(pb, 0); // unknown
1967  avio_w8(pb, nb_chapters);
1968 
1969  for (i = 0; i < nb_chapters; i++) {
1970  AVChapter *c = s->chapters[i];
1972  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1973 
1974  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1975  int len = FFMIN(strlen(t->value), 255);
1976  avio_w8(pb, len);
1977  avio_write(pb, t->value, len);
1978  } else
1979  avio_w8(pb, 0);
1980  }
1981  return update_size(pb, pos);
1982 }
1983 
1985  AVFormatContext *s)
1986 {
1987  AVIOContext *pb_buf;
1988  int i, ret, size;
1989  uint8_t *buf;
1990 
1991  for (i = 0; i < s->nb_streams; i++)
1992  if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1993  return 0;
1994  }
1995 
1996  ret = avio_open_dyn_buf(&pb_buf);
1997  if (ret < 0)
1998  return ret;
1999 
2000  if (mov->mode & MODE_3GP) {
2001  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2002  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2003  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2004  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2005  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2006  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2007  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2008  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2009  } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2010  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
2011  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
2012  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
2013  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
2014  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
2015  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
2016  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
2017  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
2018  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
2019  } else {
2020  /* iTunes meta data */
2021  mov_write_meta_tag(pb_buf, mov, s);
2022  }
2023 
2024  if (s->nb_chapters)
2025  mov_write_chpl_tag(pb_buf, s);
2026 
2027  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2028  avio_wb32(pb, size + 8);
2029  ffio_wfourcc(pb, "udta");
2030  avio_write(pb, buf, size);
2031  }
2032  av_free(buf);
2033 
2034  return 0;
2035 }
2036 
2038  const char *str, const char *lang, int type)
2039 {
2040  int len = utf8len(str) + 1;
2041  if (len <= 0)
2042  return;
2043  avio_wb16(pb, len * 2 + 10); /* size */
2044  avio_wb32(pb, type); /* type */
2045  avio_wb16(pb, language_code(lang)); /* language */
2046  avio_wb16(pb, 0x01); /* ? */
2047  ascii_to_wc(pb, str);
2048 }
2049 
2051 {
2052  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2053  int64_t pos, pos2;
2054 
2055  if (title) {
2056  pos = avio_tell(pb);
2057  avio_wb32(pb, 0); /* size placeholder*/
2058  ffio_wfourcc(pb, "uuid");
2059  ffio_wfourcc(pb, "USMT");
2060  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2061  avio_wb32(pb, 0xbb88695c);
2062  avio_wb32(pb, 0xfac9c740);
2063 
2064  pos2 = avio_tell(pb);
2065  avio_wb32(pb, 0); /* size placeholder*/
2066  ffio_wfourcc(pb, "MTDT");
2067  avio_wb16(pb, 4);
2068 
2069  // ?
2070  avio_wb16(pb, 0x0C); /* size */
2071  avio_wb32(pb, 0x0B); /* type */
2072  avio_wb16(pb, language_code("und")); /* language */
2073  avio_wb16(pb, 0x0); /* ? */
2074  avio_wb16(pb, 0x021C); /* data */
2075 
2076  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2077  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2078  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2079 
2080  update_size(pb, pos2);
2081  return update_size(pb, pos);
2082  }
2083 
2084  return 0;
2085 }
2086 
2088  AVFormatContext *s)
2089 {
2090  int i;
2091  int64_t pos = avio_tell(pb);
2092  avio_wb32(pb, 0); /* size placeholder*/
2093  ffio_wfourcc(pb, "moov");
2094 
2095  for (i = 0; i < mov->nb_streams; i++) {
2096  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2097  continue;
2098 
2099  mov->tracks[i].time = mov->time;
2100  mov->tracks[i].track_id = i + 1;
2101  }
2102 
2103  if (mov->chapter_track)
2104  for (i = 0; i < s->nb_streams; i++) {
2105  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2106  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2107  }
2108  for (i = 0; i < mov->nb_streams; i++) {
2109  if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2110  mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2111  mov->tracks[i].tref_id =
2112  mov->tracks[mov->tracks[i].src_track].track_id;
2113  }
2114  }
2115 
2116  mov_write_mvhd_tag(pb, mov);
2117  if (mov->mode != MODE_MOV && !mov->iods_skip)
2118  mov_write_iods_tag(pb, mov);
2119  for (i = 0; i < mov->nb_streams; i++) {
2120  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2121  mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2122  }
2123  }
2124  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2125  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2126 
2127  if (mov->mode == MODE_PSP)
2128  mov_write_uuidusmt_tag(pb, s);
2129  else
2130  mov_write_udta_tag(pb, mov, s);
2131 
2132  return update_size(pb, pos);
2133 }
2134 
2135 static void param_write_int(AVIOContext *pb, const char *name, int value)
2136 {
2137  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2138 }
2139 
2140 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2141 {
2142  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2143 }
2144 
2145 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2146 {
2147  char buf[150];
2148  len = FFMIN(sizeof(buf) / 2 - 1, len);
2149  ff_data_to_hex(buf, value, len, 0);
2150  buf[2 * len] = '\0';
2151  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2152 }
2153 
2155 {
2156  int64_t pos = avio_tell(pb);
2157  int i;
2158  const uint8_t uuid[] = {
2159  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2160  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2161  };
2162 
2163  avio_wb32(pb, 0);
2164  ffio_wfourcc(pb, "uuid");
2165  avio_write(pb, uuid, sizeof(uuid));
2166  avio_wb32(pb, 0);
2167 
2168  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2169  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2170  avio_printf(pb, "<head>\n");
2171  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2173  avio_printf(pb, "</head>\n");
2174  avio_printf(pb, "<body>\n");
2175  avio_printf(pb, "<switch>\n");
2176  for (i = 0; i < mov->nb_streams; i++) {
2177  MOVTrack *track = &mov->tracks[i];
2178  const char *type;
2179  /* track->track_id is initialized in write_moov, and thus isn't known
2180  * here yet */
2181  int track_id = i + 1;
2182 
2183  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2184  type = "video";
2185  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2186  type = "audio";
2187  } else {
2188  continue;
2189  }
2190  avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2191  track->enc->bit_rate);
2192  param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2193  param_write_int(pb, "trackID", track_id);
2194  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2195  if (track->enc->codec_id == AV_CODEC_ID_H264) {
2196  uint8_t *ptr;
2197  int size = track->enc->extradata_size;
2198  if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2199  &size)) {
2200  param_write_hex(pb, "CodecPrivateData",
2201  ptr ? ptr : track->enc->extradata,
2202  size);
2203  av_free(ptr);
2204  }
2205  param_write_string(pb, "FourCC", "H264");
2206  } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2207  param_write_string(pb, "FourCC", "WVC1");
2208  param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2209  track->enc->extradata_size);
2210  }
2211  param_write_int(pb, "MaxWidth", track->enc->width);
2212  param_write_int(pb, "MaxHeight", track->enc->height);
2213  param_write_int(pb, "DisplayWidth", track->enc->width);
2214  param_write_int(pb, "DisplayHeight", track->enc->height);
2215  } else {
2216  if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2217  param_write_string(pb, "FourCC", "AACL");
2218  } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2219  param_write_string(pb, "FourCC", "WMAP");
2220  }
2221  param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2222  track->enc->extradata_size);
2224  track->enc->codec_id));
2225  param_write_int(pb, "Channels", track->enc->channels);
2226  param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2227  param_write_int(pb, "BitsPerSample", 16);
2228  param_write_int(pb, "PacketSize", track->enc->block_align ?
2229  track->enc->block_align : 4);
2230  }
2231  avio_printf(pb, "</%s>\n", type);
2232  }
2233  avio_printf(pb, "</switch>\n");
2234  avio_printf(pb, "</body>\n");
2235  avio_printf(pb, "</smil>\n");
2236 
2237  return update_size(pb, pos);
2238 }
2239 
2241 {
2242  avio_wb32(pb, 16);
2243  ffio_wfourcc(pb, "mfhd");
2244  avio_wb32(pb, 0);
2245  avio_wb32(pb, mov->fragments);
2246  return 0;
2247 }
2248 
2250  MOVTrack *track, int64_t moof_offset)
2251 {
2252  int64_t pos = avio_tell(pb);
2255  if (!track->entry) {
2256  flags |= MOV_TFHD_DURATION_IS_EMPTY;
2257  } else {
2258  flags |= MOV_TFHD_DEFAULT_FLAGS;
2259  }
2261  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
2262 
2263  /* Don't set a default sample size, the silverlight player refuses
2264  * to play files with that set. Don't set a default sample duration,
2265  * WMP freaks out if it is set. Don't set a base data offset, PIFF
2266  * file format says it MUST NOT be set. */
2267  if (track->mode == MODE_ISM)
2270 
2271  avio_wb32(pb, 0); /* size placeholder */
2272  ffio_wfourcc(pb, "tfhd");
2273  avio_w8(pb, 0); /* version */
2274  avio_wb24(pb, flags);
2275 
2276  avio_wb32(pb, track->track_id); /* track-id */
2277  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2278  avio_wb64(pb, moof_offset);
2279  if (flags & MOV_TFHD_DEFAULT_DURATION) {
2280  track->default_duration = get_cluster_duration(track, 0);
2281  avio_wb32(pb, track->default_duration);
2282  }
2283  if (flags & MOV_TFHD_DEFAULT_SIZE) {
2284  track->default_size = track->entry ? track->cluster[0].size : 1;
2285  avio_wb32(pb, track->default_size);
2286  } else
2287  track->default_size = -1;
2288 
2289  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2290  track->default_sample_flags =
2291  track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2294  avio_wb32(pb, track->default_sample_flags);
2295  }
2296 
2297  return update_size(pb, pos);
2298 }
2299 
2300 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2301 {
2304 }
2305 
2307  MOVTrack *track, int moof_size)
2308 {
2309  int64_t pos = avio_tell(pb);
2310  uint32_t flags = MOV_TRUN_DATA_OFFSET;
2311  int i;
2312 
2313  for (i = 0; i < track->entry; i++) {
2314  if (get_cluster_duration(track, i) != track->default_duration)
2315  flags |= MOV_TRUN_SAMPLE_DURATION;
2316  if (track->cluster[i].size != track->default_size)
2317  flags |= MOV_TRUN_SAMPLE_SIZE;
2318  if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2319  flags |= MOV_TRUN_SAMPLE_FLAGS;
2320  }
2321  if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2322  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2323  if (track->flags & MOV_TRACK_CTTS)
2324  flags |= MOV_TRUN_SAMPLE_CTS;
2325 
2326  avio_wb32(pb, 0); /* size placeholder */
2327  ffio_wfourcc(pb, "trun");
2328  avio_w8(pb, 0); /* version */
2329  avio_wb24(pb, flags);
2330 
2331  avio_wb32(pb, track->entry); /* sample count */
2332  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
2333  !(mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) &&
2334  track->track_id != 1)
2335  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
2336  else
2337  avio_wb32(pb, moof_size + 8 + track->data_offset +
2338  track->cluster[0].pos); /* data offset */
2339  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2340  avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2341 
2342  for (i = 0; i < track->entry; i++) {
2343  if (flags & MOV_TRUN_SAMPLE_DURATION)
2344  avio_wb32(pb, get_cluster_duration(track, i));
2345  if (flags & MOV_TRUN_SAMPLE_SIZE)
2346  avio_wb32(pb, track->cluster[i].size);
2347  if (flags & MOV_TRUN_SAMPLE_FLAGS)
2348  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2349  if (flags & MOV_TRUN_SAMPLE_CTS)
2350  avio_wb32(pb, track->cluster[i].cts);
2351  }
2352 
2353  return update_size(pb, pos);
2354 }
2355 
2356 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2357 {
2358  int64_t pos = avio_tell(pb);
2359  const uint8_t uuid[] = {
2360  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2361  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2362  };
2363 
2364  avio_wb32(pb, 0); /* size placeholder */
2365  ffio_wfourcc(pb, "uuid");
2366  avio_write(pb, uuid, sizeof(uuid));
2367  avio_w8(pb, 1);
2368  avio_wb24(pb, 0);
2369  avio_wb64(pb, track->frag_start);
2370  avio_wb64(pb, track->start_dts + track->track_duration -
2371  track->cluster[0].dts);
2372 
2373  return update_size(pb, pos);
2374 }
2375 
2377  MOVTrack *track, int entry)
2378 {
2379  int n = track->nb_frag_info - 1 - entry, i;
2380  int size = 8 + 16 + 4 + 1 + 16*n;
2381  const uint8_t uuid[] = {
2382  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2383  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2384  };
2385 
2386  if (entry < 0)
2387  return 0;
2388 
2389  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2390  avio_wb32(pb, size);
2391  ffio_wfourcc(pb, "uuid");
2392  avio_write(pb, uuid, sizeof(uuid));
2393  avio_w8(pb, 1);
2394  avio_wb24(pb, 0);
2395  avio_w8(pb, n);
2396  for (i = 0; i < n; i++) {
2397  int index = entry + 1 + i;
2398  avio_wb64(pb, track->frag_info[index].time);
2399  avio_wb64(pb, track->frag_info[index].duration);
2400  }
2401  if (n < mov->ism_lookahead) {
2402  int free_size = 16 * (mov->ism_lookahead - n);
2403  avio_wb32(pb, free_size);
2404  ffio_wfourcc(pb, "free");
2405  for (i = 0; i < free_size - 8; i++)
2406  avio_w8(pb, 0);
2407  }
2408 
2409  return 0;
2410 }
2411 
2413  MOVTrack *track)
2414 {
2415  int64_t pos = avio_tell(pb);
2416  int i;
2417  for (i = 0; i < mov->ism_lookahead; i++) {
2418  /* Update the tfrf tag for the last ism_lookahead fragments,
2419  * nb_frag_info - 1 is the next fragment to be written. */
2420  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2421  }
2422  avio_seek(pb, pos, SEEK_SET);
2423  return 0;
2424 }
2425 
2427  MOVTrack *track, int64_t moof_offset,
2428  int moof_size)
2429 {
2430  int64_t pos = avio_tell(pb);
2431  avio_wb32(pb, 0); /* size placeholder */
2432  ffio_wfourcc(pb, "traf");
2433 
2434  mov_write_tfhd_tag(pb, mov, track, moof_offset);
2435  mov_write_trun_tag(pb, mov, track, moof_size);
2436  if (mov->mode == MODE_ISM) {
2437  mov_write_tfxd_tag(pb, track);
2438 
2439  if (mov->ism_lookahead) {
2440  int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
2441 
2442  track->tfrf_offset = avio_tell(pb);
2443  avio_wb32(pb, 8 + size);
2444  ffio_wfourcc(pb, "free");
2445  for (i = 0; i < size; i++)
2446  avio_w8(pb, 0);
2447  }
2448  }
2449 
2450  return update_size(pb, pos);
2451 }
2452 
2454  int tracks, int moof_size)
2455 {
2456  int64_t pos = avio_tell(pb);
2457  int i;
2458 
2459  avio_wb32(pb, 0); /* size placeholder */
2460  ffio_wfourcc(pb, "moof");
2461 
2462  mov_write_mfhd_tag(pb, mov);
2463  for (i = 0; i < mov->nb_streams; i++) {
2464  MOVTrack *track = &mov->tracks[i];
2465  if (tracks >= 0 && i != tracks)
2466  continue;
2467  if (!track->entry)
2468  continue;
2469  mov_write_traf_tag(pb, mov, track, pos, moof_size);
2470  }
2471 
2472  return update_size(pb, pos);
2473 }
2474 
2475 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2476 {
2477  AVIOContext *avio_buf;
2478  int ret, moof_size;
2479 
2480  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
2481  return ret;
2482  mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
2483  moof_size = ffio_close_null_buf(avio_buf);
2484  return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
2485 }
2486 
2487 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2488 {
2489  int64_t pos = avio_tell(pb);
2490  int i;
2491 
2492  avio_wb32(pb, 0); /* size placeholder */
2493  ffio_wfourcc(pb, "tfra");
2494  avio_w8(pb, 1); /* version */
2495  avio_wb24(pb, 0);
2496 
2497  avio_wb32(pb, track->track_id);
2498  avio_wb32(pb, 0); /* length of traf/trun/sample num */
2499  avio_wb32(pb, track->nb_frag_info);
2500  for (i = 0; i < track->nb_frag_info; i++) {
2501  avio_wb64(pb, track->frag_info[i].time);
2502  avio_wb64(pb, track->frag_info[i].offset);
2503  avio_w8(pb, 1); /* traf number */
2504  avio_w8(pb, 1); /* trun number */
2505  avio_w8(pb, 1); /* sample number */
2506  }
2507 
2508  return update_size(pb, pos);
2509 }
2510 
2512 {
2513  int64_t pos = avio_tell(pb);
2514  int i;
2515 
2516  avio_wb32(pb, 0); /* size placeholder */
2517  ffio_wfourcc(pb, "mfra");
2518  /* An empty mfra atom is enough to indicate to the publishing point that
2519  * the stream has ended. */
2520  if (mov->flags & FF_MOV_FLAG_ISML)
2521  return update_size(pb, pos);
2522 
2523  for (i = 0; i < mov->nb_streams; i++) {
2524  MOVTrack *track = &mov->tracks[i];
2525  if (track->nb_frag_info)
2526  mov_write_tfra_tag(pb, track);
2527  }
2528 
2529  avio_wb32(pb, 16);
2530  ffio_wfourcc(pb, "mfro");
2531  avio_wb32(pb, 0); /* version + flags */
2532  avio_wb32(pb, avio_tell(pb) + 4 - pos);
2533 
2534  return update_size(pb, pos);
2535 }
2536 
2538 {
2539  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2540  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2541 
2542  mov->mdat_pos = avio_tell(pb);
2543  avio_wb32(pb, 0); /* size placeholder*/
2544  ffio_wfourcc(pb, "mdat");
2545  return 0;
2546 }
2547 
2548 /* TODO: This needs to be more general */
2550 {
2551  MOVMuxContext *mov = s->priv_data;
2552  int64_t pos = avio_tell(pb);
2553  int has_h264 = 0, has_video = 0;
2554  int minor = 0x200;
2555  int i;
2556 
2557  for (i = 0; i < s->nb_streams; i++) {
2558  AVStream *st = s->streams[i];
2559  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2560  has_video = 1;
2561  if (st->codec->codec_id == AV_CODEC_ID_H264)
2562  has_h264 = 1;
2563  }
2564 
2565  avio_wb32(pb, 0); /* size */
2566  ffio_wfourcc(pb, "ftyp");
2567 
2568  if (mov->major_brand && strlen(mov->major_brand) >= 4)
2569  ffio_wfourcc(pb, mov->major_brand);
2570  else if (mov->mode == MODE_3GP) {
2571  ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2572  minor = has_h264 ? 0x100 : 0x200;
2573  } else if (mov->mode & MODE_3G2) {
2574  ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2575  minor = has_h264 ? 0x20000 : 0x10000;
2576  } else if (mov->mode == MODE_PSP)
2577  ffio_wfourcc(pb, "MSNV");
2578  else if (mov->mode == MODE_MP4)
2579  ffio_wfourcc(pb, "isom");
2580  else if (mov->mode == MODE_IPOD)
2581  ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2582  else if (mov->mode == MODE_ISM)
2583  ffio_wfourcc(pb, "isml");
2584  else if (mov->mode == MODE_F4V)
2585  ffio_wfourcc(pb, "f4v ");
2586  else
2587  ffio_wfourcc(pb, "qt ");
2588 
2589  avio_wb32(pb, minor);
2590 
2591  if (mov->mode == MODE_MOV)
2592  ffio_wfourcc(pb, "qt ");
2593  else if (mov->mode == MODE_ISM) {
2594  ffio_wfourcc(pb, "piff");
2595  ffio_wfourcc(pb, "iso2");
2596  } else {
2597  ffio_wfourcc(pb, "isom");
2598  ffio_wfourcc(pb, "iso2");
2599  if (has_h264)
2600  ffio_wfourcc(pb, "avc1");
2601  }
2602 
2603  if (mov->mode == MODE_3GP)
2604  ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2605  else if (mov->mode & MODE_3G2)
2606  ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2607  else if (mov->mode == MODE_PSP)
2608  ffio_wfourcc(pb, "MSNV");
2609  else if (mov->mode == MODE_MP4)
2610  ffio_wfourcc(pb, "mp41");
2611  return update_size(pb, pos);
2612 }
2613 
2615 {
2616  AVCodecContext *video_codec = s->streams[0]->codec;
2617  AVCodecContext *audio_codec = s->streams[1]->codec;
2618  int audio_rate = audio_codec->sample_rate;
2619  int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
2620  int audio_kbitrate = audio_codec->bit_rate / 1000;
2621  int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2622 
2623  avio_wb32(pb, 0x94); /* size */
2624  ffio_wfourcc(pb, "uuid");
2625  ffio_wfourcc(pb, "PROF");
2626 
2627  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2628  avio_wb32(pb, 0xbb88695c);
2629  avio_wb32(pb, 0xfac9c740);
2630 
2631  avio_wb32(pb, 0x0); /* ? */
2632  avio_wb32(pb, 0x3); /* 3 sections ? */
2633 
2634  avio_wb32(pb, 0x14); /* size */
2635  ffio_wfourcc(pb, "FPRF");
2636  avio_wb32(pb, 0x0); /* ? */
2637  avio_wb32(pb, 0x0); /* ? */
2638  avio_wb32(pb, 0x0); /* ? */
2639 
2640  avio_wb32(pb, 0x2c); /* size */
2641  ffio_wfourcc(pb, "APRF"); /* audio */
2642  avio_wb32(pb, 0x0);
2643  avio_wb32(pb, 0x2); /* TrackID */
2644  ffio_wfourcc(pb, "mp4a");
2645  avio_wb32(pb, 0x20f);
2646  avio_wb32(pb, 0x0);
2647  avio_wb32(pb, audio_kbitrate);
2648  avio_wb32(pb, audio_kbitrate);
2649  avio_wb32(pb, audio_rate);
2650  avio_wb32(pb, audio_codec->channels);
2651 
2652  avio_wb32(pb, 0x34); /* size */
2653  ffio_wfourcc(pb, "VPRF"); /* video */
2654  avio_wb32(pb, 0x0);
2655  avio_wb32(pb, 0x1); /* TrackID */
2656  if (video_codec->codec_id == AV_CODEC_ID_H264) {
2657  ffio_wfourcc(pb, "avc1");
2658  avio_wb16(pb, 0x014D);
2659  avio_wb16(pb, 0x0015);
2660  } else {
2661  ffio_wfourcc(pb, "mp4v");
2662  avio_wb16(pb, 0x0000);
2663  avio_wb16(pb, 0x0103);
2664  }
2665  avio_wb32(pb, 0x0);
2666  avio_wb32(pb, video_kbitrate);
2667  avio_wb32(pb, video_kbitrate);
2668  avio_wb32(pb, frame_rate);
2669  avio_wb32(pb, frame_rate);
2670  avio_wb16(pb, video_codec->width);
2671  avio_wb16(pb, video_codec->height);
2672  avio_wb32(pb, 0x010001); /* ? */
2673 }
2674 
2675 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2676 {
2677  uint32_t c = -1;
2678  int i, closed_gop = 0;
2679 
2680  for (i = 0; i < pkt->size - 4; i++) {
2681  c = (c << 8) + pkt->data[i];
2682  if (c == 0x1b8) { // gop
2683  closed_gop = pkt->data[i + 4] >> 6 & 0x01;
2684  } else if (c == 0x100) { // pic
2685  int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
2686  if (!temp_ref || closed_gop) // I picture is not reordered
2687  *flags = MOV_SYNC_SAMPLE;
2688  else
2689  *flags = MOV_PARTIAL_SYNC_SAMPLE;
2690  break;
2691  }
2692  }
2693  return 0;
2694 }
2695 
2696 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2697 {
2698  const uint8_t *start, *next, *end = pkt->data + pkt->size;
2699  int seq = 0, entry = 0;
2700  int key = pkt->flags & AV_PKT_FLAG_KEY;
2701  start = find_next_marker(pkt->data, end);
2702  for (next = start; next < end; start = next) {
2703  next = find_next_marker(start + 4, end);
2704  switch (AV_RB32(start)) {
2705  case VC1_CODE_SEQHDR:
2706  seq = 1;
2707  break;
2708  case VC1_CODE_ENTRYPOINT:
2709  entry = 1;
2710  break;
2711  case VC1_CODE_SLICE:
2712  trk->vc1_info.slices = 1;
2713  break;
2714  }
2715  }
2716  if (!trk->entry && !fragment) {
2717  /* First packet in first fragment */
2718  trk->vc1_info.first_packet_seq = seq;
2719  trk->vc1_info.first_packet_entry = entry;
2720  } else if ((seq && !trk->vc1_info.packet_seq) ||
2721  (entry && !trk->vc1_info.packet_entry)) {
2722  int i;
2723  for (i = 0; i < trk->entry; i++)
2724  trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2725  trk->has_keyframes = 0;
2726  if (seq)
2727  trk->vc1_info.packet_seq = 1;
2728  if (entry)
2729  trk->vc1_info.packet_entry = 1;
2730  if (!fragment) {
2731  /* First fragment */
2732  if ((!seq || trk->vc1_info.first_packet_seq) &&
2733  (!entry || trk->vc1_info.first_packet_entry)) {
2734  /* First packet had the same headers as this one, readd the
2735  * sync sample flag. */
2736  trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2737  trk->has_keyframes = 1;
2738  }
2739  }
2740  }
2741  if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2742  key = seq && entry;
2743  else if (trk->vc1_info.packet_seq)
2744  key = seq;
2745  else if (trk->vc1_info.packet_entry)
2746  key = entry;
2747  if (key) {
2748  trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2749  trk->has_keyframes++;
2750  }
2751 }
2752 
2754 {
2755  MOVMuxContext *mov = s->priv_data;
2756  int i, first_track = -1;
2757  int64_t mdat_size = 0;
2758 
2759  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2760  return 0;
2761 
2762  if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2763  int64_t pos = avio_tell(s->pb);
2764  int ret;
2765  AVIOContext *moov_buf;
2766  uint8_t *buf;
2767  int buf_size;
2768 
2769  for (i = 0; i < mov->nb_streams; i++)
2770  if (!mov->tracks[i].entry)
2771  break;
2772  /* Don't write the initial moov unless all tracks have data */
2773  if (i < mov->nb_streams)
2774  return 0;
2775 
2776  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
2777  return ret;
2778  mov_write_moov_tag(moov_buf, mov, s);
2779  buf_size = ffio_close_null_buf(moov_buf);
2780  for (i = 0; i < mov->nb_streams; i++)
2781  mov->tracks[i].data_offset = pos + buf_size + 8;
2782 
2783  mov_write_moov_tag(s->pb, mov, s);
2784 
2785  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2786  mov->mdat_buf = NULL;
2787  avio_wb32(s->pb, buf_size + 8);
2788  ffio_wfourcc(s->pb, "mdat");
2789  avio_write(s->pb, buf, buf_size);
2790  av_free(buf);
2791 
2792  mov->fragments++;
2793  mov->mdat_size = 0;
2794  for (i = 0; i < mov->nb_streams; i++) {
2795  if (mov->tracks[i].entry)
2796  mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2797  mov->tracks[i].track_duration -
2798  mov->tracks[i].cluster[0].dts;
2799  mov->tracks[i].entry = 0;
2800  }
2801  avio_flush(s->pb);
2802  return 0;
2803  }
2804 
2805  for (i = 0; i < mov->nb_streams; i++) {
2806  MOVTrack *track = &mov->tracks[i];
2807  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2808  track->data_offset = 0;
2809  else
2810  track->data_offset = mdat_size;
2811  if (!track->mdat_buf)
2812  continue;
2813  mdat_size += avio_tell(track->mdat_buf);
2814  if (first_track < 0)
2815  first_track = i;
2816  }
2817 
2818  if (!mdat_size)
2819  return 0;
2820 
2821  for (i = 0; i < mov->nb_streams; i++) {
2822  MOVTrack *track = &mov->tracks[i];
2823  int buf_size, write_moof = 1, moof_tracks = -1;
2824  uint8_t *buf;
2825  int64_t duration = 0;
2826 
2827  if (track->entry)
2828  duration = track->start_dts + track->track_duration -
2829  track->cluster[0].dts;
2830  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2831  if (!track->mdat_buf)
2832  continue;
2833  mdat_size = avio_tell(track->mdat_buf);
2834  moof_tracks = i;
2835  } else {
2836  write_moof = i == first_track;
2837  }
2838 
2839  if (write_moof) {
2840  MOVFragmentInfo *info;
2841  avio_flush(s->pb);
2842  track->nb_frag_info++;
2843  if (track->nb_frag_info >= track->frag_info_capacity) {
2844  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
2845  if (av_reallocp_array(&track->frag_info,
2846  new_capacity,
2847  sizeof(*track->frag_info)))
2848  return AVERROR(ENOMEM);
2849  track->frag_info_capacity = new_capacity;
2850  }
2851  info = &track->frag_info[track->nb_frag_info - 1];
2852  info->offset = avio_tell(s->pb);
2853  info->time = mov->tracks[i].frag_start;
2854  info->duration = duration;
2855  mov_write_tfrf_tags(s->pb, mov, track);
2856 
2857  mov_write_moof_tag(s->pb, mov, moof_tracks);
2858  info->tfrf_offset = track->tfrf_offset;
2859  mov->fragments++;
2860 
2861  avio_wb32(s->pb, mdat_size + 8);
2862  ffio_wfourcc(s->pb, "mdat");
2863  }
2864 
2865  if (track->entry)
2866  track->frag_start += duration;
2867  track->entry = 0;
2868  if (!track->mdat_buf)
2869  continue;
2870  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2871  track->mdat_buf = NULL;
2872 
2873  avio_write(s->pb, buf, buf_size);
2874  av_free(buf);
2875  }
2876 
2877  mov->mdat_size = 0;
2878 
2879  avio_flush(s->pb);
2880  return 0;
2881 }
2882 
2884 {
2885  MOVMuxContext *mov = s->priv_data;
2886  AVIOContext *pb = s->pb;
2887  MOVTrack *trk = &mov->tracks[pkt->stream_index];
2888  AVCodecContext *enc = trk->enc;
2889  unsigned int samples_in_chunk = 0;
2890  int size = pkt->size;
2891  uint8_t *reformatted_data = NULL;
2892 
2893  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
2894  int ret;
2895  if (mov->fragments > 0) {
2896  if (!trk->mdat_buf) {
2897  if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2898  return ret;
2899  }
2900  pb = trk->mdat_buf;
2901  } else {
2902  if (!mov->mdat_buf) {
2903  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
2904  return ret;
2905  }
2906  pb = mov->mdat_buf;
2907  }
2908  }
2909 
2910  if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
2911  /* We must find out how many AMR blocks there are in one packet */
2912  static uint16_t packed_size[16] =
2913  {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
2914  int len = 0;
2915 
2916  while (len < size && samples_in_chunk < 100) {
2917  len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2918  samples_in_chunk++;
2919  }
2920  if (samples_in_chunk > 1) {
2921  av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2922  return -1;
2923  }
2924  } else if (trk->sample_size)
2925  samples_in_chunk = size / trk->sample_size;
2926  else
2927  samples_in_chunk = 1;
2928 
2929  /* copy extradata if it exists */
2930  if (trk->vos_len == 0 && enc->extradata_size > 0) {
2931  trk->vos_len = enc->extradata_size;
2932  trk->vos_data = av_malloc(trk->vos_len);
2933  memcpy(trk->vos_data, enc->extradata, trk->vos_len);
2934  }
2935 
2936  if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
2937  /* from x264 or from bytestream h264 */
2938  /* nal reformating needed */
2939  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2940  ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2941  &size);
2942  avio_write(pb, reformatted_data, size);
2943  } else {
2944  size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2945  }
2946  } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
2947  (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
2948  /* extradata is Annex B, assume the bitstream is too and convert it */
2949  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2950  ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
2951  avio_write(pb, reformatted_data, size);
2952  } else {
2953  size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
2954  }
2955  } else {
2956  avio_write(pb, pkt->data, size);
2957  }
2958 
2959  if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
2960  enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
2961  /* copy frame to create needed atoms */
2962  trk->vos_len = size;
2963  trk->vos_data = av_malloc(size);
2964  if (!trk->vos_data)
2965  return AVERROR(ENOMEM);
2966  memcpy(trk->vos_data, pkt->data, size);
2967  }
2968 
2969  if (trk->entry >= trk->cluster_capacity) {
2970  unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
2971  if (av_reallocp_array(&trk->cluster, new_capacity,
2972  sizeof(*trk->cluster)))
2973  return AVERROR(ENOMEM);
2974  trk->cluster_capacity = new_capacity;
2975  }
2976 
2977  trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2978  trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
2979  trk->cluster[trk->entry].size = size;
2980  trk->cluster[trk->entry].entries = samples_in_chunk;
2981  trk->cluster[trk->entry].dts = pkt->dts;
2982  if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
2983  /* First packet of a new fragment. We already wrote the duration
2984  * of the last packet of the previous fragment based on track_duration,
2985  * which might not exactly match our dts. Therefore adjust the dts
2986  * of this packet to be what the previous packets duration implies. */
2987  trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
2988  }
2989  if (trk->start_dts == AV_NOPTS_VALUE)
2990  trk->start_dts = pkt->dts;
2991  trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
2992 
2993  if (pkt->pts == AV_NOPTS_VALUE) {
2994  av_log(s, AV_LOG_WARNING, "pts has no value\n");
2995  pkt->pts = pkt->dts;
2996  }
2997  if (pkt->dts != pkt->pts)
2998  trk->flags |= MOV_TRACK_CTTS;
2999  trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
3000  trk->cluster[trk->entry].flags = 0;
3001  if (enc->codec_id == AV_CODEC_ID_VC1) {
3002  mov_parse_vc1_frame(pkt, trk, mov->fragments);
3003  } else if (pkt->flags & AV_PKT_FLAG_KEY) {
3004  if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
3005  trk->entry > 0) { // force sync sample for the first key frame
3006  mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3007  if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3008  trk->flags |= MOV_TRACK_STPS;
3009  } else {
3010  trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3011  }
3012  if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
3013  trk->has_keyframes++;
3014  }
3015  trk->entry++;
3016  trk->sample_count += samples_in_chunk;
3017  mov->mdat_size += size;
3018 
3019  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
3020  ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3021  reformatted_data, size);
3022  av_free(reformatted_data);
3023  return 0;
3024 }
3025 
3027 {
3028  if (!pkt) {
3029  mov_flush_fragment(s);
3030  return 1;
3031  } else {
3032  MOVMuxContext *mov = s->priv_data;
3033  MOVTrack *trk = &mov->tracks[pkt->stream_index];
3034  AVCodecContext *enc = trk->enc;
3035  int64_t frag_duration = 0;
3036  int size = pkt->size;
3037 
3038  if (!pkt->size)
3039  return 0; /* Discard 0 sized packets */
3040 
3041  if (trk->entry)
3042  frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3043  s->streams[pkt->stream_index]->time_base,
3044  AV_TIME_BASE_Q);
3045  if ((mov->max_fragment_duration &&
3046  frag_duration >= mov->max_fragment_duration) ||
3047  (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3048  (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3049  enc->codec_type == AVMEDIA_TYPE_VIDEO &&
3050  trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3051  if (frag_duration >= mov->min_fragment_duration)
3052  mov_flush_fragment(s);
3053  }
3054 
3055  return ff_mov_write_packet(s, pkt);
3056  }
3057 }
3058 
3059 // QuickTime chapters involve an additional text track with the chapter names
3060 // as samples, and a tref pointing from the other tracks to the chapter one.
3061 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
3062 {
3063  MOVMuxContext *mov = s->priv_data;
3064  MOVTrack *track = &mov->tracks[tracknum];
3065  AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
3066  int i, len;
3067  // These properties are required to make QT recognize the chapter track
3068  uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
3069 
3070  track->mode = mov->mode;
3071  track->tag = MKTAG('t','e','x','t');
3072  track->timescale = MOV_TIMESCALE;
3073  track->enc = avcodec_alloc_context3(NULL);
3074  if (!track->enc)
3075  return AVERROR(ENOMEM);
3077  track->enc->extradata = av_malloc(sizeof(chapter_properties));
3078  if (track->enc->extradata == NULL)
3079  return AVERROR(ENOMEM);
3080  track->enc->extradata_size = sizeof(chapter_properties);
3081  memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
3082 
3083  for (i = 0; i < s->nb_chapters; i++) {
3084  AVChapter *c = s->chapters[i];
3086 
3087  int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3088  pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3089  pkt.duration = end - pkt.dts;
3090 
3091  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3092  len = strlen(t->value);
3093  pkt.size = len + 2;
3094  pkt.data = av_malloc(pkt.size);
3095  if (!pkt.data)
3096  return AVERROR(ENOMEM);
3097  AV_WB16(pkt.data, len);
3098  memcpy(pkt.data + 2, t->value, len);
3099  ff_mov_write_packet(s, &pkt);
3100  av_freep(&pkt.data);
3101  }
3102  }
3103 
3104  return 0;
3105 }
3106 
3107 /*
3108  * st->disposition controls the "enabled" flag in the tkhd tag.
3109  * QuickTime will not play a track if it is not enabled. So make sure
3110  * that one track of each type (audio, video, subtitle) is enabled.
3111  *
3112  * Subtitles are special. For audio and video, setting "enabled" also
3113  * makes the track "default" (i.e. it is rendered when played). For
3114  * subtitles, an "enabled" subtitle is not rendered by default, but
3115  * if no subtitle is enabled, the subtitle menu in QuickTime will be
3116  * empty!
3117  */
3119 {
3120  MOVMuxContext *mov = s->priv_data;
3121  int i;
3122  uint8_t enabled[AVMEDIA_TYPE_NB];
3123  int first[AVMEDIA_TYPE_NB];
3124 
3125  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3126  enabled[i] = 0;
3127  first[i] = -1;
3128  }
3129 
3130  for (i = 0; i < s->nb_streams; i++) {
3131  AVStream *st = s->streams[i];
3132 
3133  if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
3134  st->codec->codec_type >= AVMEDIA_TYPE_NB)
3135  continue;
3136 
3137  if (first[st->codec->codec_type] < 0)
3138  first[st->codec->codec_type] = i;
3139  if (st->disposition & AV_DISPOSITION_DEFAULT) {
3140  mov->tracks[i].flags |= MOV_TRACK_ENABLED;
3141  enabled[st->codec->codec_type] = 1;
3142  }
3143  }
3144 
3145  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3146  switch (i) {
3147  case AVMEDIA_TYPE_VIDEO:
3148  case AVMEDIA_TYPE_AUDIO:
3149  case AVMEDIA_TYPE_SUBTITLE:
3150  if (!enabled[i] && first[i] >= 0)
3151  mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
3152  break;
3153  }
3154  }
3155 }
3156 
3157 static void mov_free(AVFormatContext *s)
3158 {
3159  MOVMuxContext *mov = s->priv_data;
3160  int i;
3161 
3162  if (mov->chapter_track) {
3163  if (mov->tracks[mov->chapter_track].enc)
3164  av_free(mov->tracks[mov->chapter_track].enc->extradata);
3165  av_freep(&mov->tracks[mov->chapter_track].enc);
3166  }
3167 
3168  for (i = 0; i < mov->nb_streams; i++) {
3169  if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3170  ff_mov_close_hinting(&mov->tracks[i]);
3171  av_freep(&mov->tracks[i].cluster);
3172  av_freep(&mov->tracks[i].frag_info);
3173 
3174  if (mov->tracks[i].vos_len)
3175  av_free(mov->tracks[i].vos_data);
3176  }
3177 
3178  av_freep(&mov->tracks);
3179 }
3180 
3182 {
3183  AVIOContext *pb = s->pb;
3184  MOVMuxContext *mov = s->priv_data;
3186  int i, hint_track = 0;
3187 
3188  /* Default mode == MP4 */
3189  mov->mode = MODE_MP4;
3190 
3191  if (s->oformat != NULL) {
3192  if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3193  else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3194  else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3195  else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3196  else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3197  else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3198  else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
3199  }
3200 
3201  /* Set the FRAGMENT flag if any of the fragmentation methods are
3202  * enabled. */
3203  if (mov->max_fragment_duration || mov->max_fragment_size ||
3204  mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3207  mov->flags |= FF_MOV_FLAG_FRAGMENT;
3208 
3209  /* Set other implicit flags immediately */
3210  if (mov->mode == MODE_ISM)
3213 
3214  /* faststart: moov at the beginning of the file, if supported */
3215  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3216  if ((mov->flags & FF_MOV_FLAG_FRAGMENT) ||
3217  (s->flags & AVFMT_FLAG_CUSTOM_IO)) {
3218  av_log(s, AV_LOG_WARNING, "The faststart flag is incompatible "
3219  "with fragmentation and custom IO, disabling faststart\n");
3220  mov->flags &= ~FF_MOV_FLAG_FASTSTART;
3221  }
3222  }
3223 
3224  /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3225  * is enabled, we don't support non-seekable output at all. */
3226  if (!s->pb->seekable &&
3227  (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
3228  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3229  return AVERROR(EINVAL);
3230  }
3231 
3232 
3233  mov_write_ftyp_tag(pb,s);
3234  if (mov->mode == MODE_PSP) {
3235  if (s->nb_streams != 2) {
3236  av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3237  return AVERROR(EINVAL);
3238  }
3239  mov_write_uuidprof_tag(pb, s);
3240  }
3241 
3242  mov->nb_streams = s->nb_streams;
3243  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
3244  mov->chapter_track = mov->nb_streams++;
3245 
3246  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3247  /* Add hint tracks for each audio and video stream */
3248  hint_track = mov->nb_streams;
3249  for (i = 0; i < s->nb_streams; i++) {
3250  AVStream *st = s->streams[i];
3251  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3253  mov->nb_streams++;
3254  }
3255  }
3256  }
3257 
3258  // Reserve an extra stream for chapters for the case where chapters
3259  // are written in the trailer
3260  mov->tracks = av_mallocz((mov->nb_streams + 1) * sizeof(*mov->tracks));
3261  if (!mov->tracks)
3262  return AVERROR(ENOMEM);
3263 
3264  for (i = 0; i < s->nb_streams; i++) {
3265  AVStream *st= s->streams[i];
3266  MOVTrack *track= &mov->tracks[i];
3267  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3268 
3269  track->enc = st->codec;
3270  track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3271  if (track->language < 0)
3272  track->language = 0;
3273  track->mode = mov->mode;
3274  track->tag = mov_find_codec_tag(s, track);
3275  if (!track->tag) {
3276  av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
3277  "codec not currently supported in container\n", i);
3278  goto error;
3279  }
3280  /* If hinting of this track is enabled by a later hint track,
3281  * this is updated. */
3282  track->hint_track = -1;
3283  track->start_dts = AV_NOPTS_VALUE;
3284  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3285  if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3286  track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3287  track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3288  if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3289  av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3290  goto error;
3291  }
3292  track->height = track->tag >> 24 == 'n' ? 486 : 576;
3293  }
3294  track->timescale = st->codec->time_base.den;
3295  if (track->mode == MODE_MOV && track->timescale > 100000)
3297  "WARNING codec timebase is very high. If duration is too long,\n"
3298  "file may not be playable by quicktime. Specify a shorter timebase\n"
3299  "or choose different container.\n");
3300  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3301  track->timescale = st->codec->sample_rate;
3302  /* set sample_size for PCM and ADPCM */
3304  st->codec->codec_id == AV_CODEC_ID_ILBC) {
3305  if (!st->codec->block_align) {
3306  av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
3307  goto error;
3308  }
3309  track->sample_size = st->codec->block_align;
3310  }
3311  /* set audio_vbr for compressed audio */
3312  if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
3313  track->audio_vbr = 1;
3314  }
3315  if (track->mode != MODE_MOV &&
3316  track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3317  av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3318  i, track->enc->sample_rate);
3319  goto error;
3320  }
3321  } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3322  track->timescale = st->codec->time_base.den;
3323  } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
3324  track->timescale = st->codec->time_base.den;
3325  }
3326  if (!track->height)
3327  track->height = st->codec->height;
3328  /* The ism specific timescale isn't mandatory, but is assumed by
3329  * some tools, such as mp4split. */
3330  if (mov->mode == MODE_ISM)
3331  track->timescale = 10000000;
3332 
3333  avpriv_set_pts_info(st, 64, 1, track->timescale);
3334 
3335  /* copy extradata if it exists */
3336  if (st->codec->extradata_size) {
3337  track->vos_len = st->codec->extradata_size;
3338  track->vos_data = av_malloc(track->vos_len);
3339  memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3340  }
3341  }
3342 
3343  enable_tracks(s);
3344 
3345  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3346  /* If no fragmentation options have been set, set a default. */
3347  if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3351  } else {
3352  if (mov->flags & FF_MOV_FLAG_FASTSTART)
3353  mov->reserved_moov_pos = avio_tell(pb);
3354  mov_write_mdat_tag(pb, mov);
3355  }
3356 
3357  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3358  mov->time = ff_iso8601_to_unix_time(t->value);
3359  if (mov->time)
3360  mov->time += 0x7C25B080; // 1970 based -> 1904 based
3361 
3362  if (mov->chapter_track)
3363  if (mov_create_chapter_track(s, mov->chapter_track) < 0)
3364  goto error;
3365 
3366  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3367  /* Initialize the hint tracks for each audio and video stream */
3368  for (i = 0; i < s->nb_streams; i++) {
3369  AVStream *st = s->streams[i];
3370  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3372  ff_mov_init_hinting(s, hint_track, i);
3373  hint_track++;
3374  }
3375  }
3376  }
3377 
3378  avio_flush(pb);
3379 
3380  if (mov->flags & FF_MOV_FLAG_ISML)
3381  mov_write_isml_manifest(pb, mov);
3382 
3383  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3384  mov_write_moov_tag(pb, mov, s);
3385  mov->fragments++;
3386  }
3387 
3388  return 0;
3389  error:
3390  mov_free(s);
3391  return -1;
3392 }
3393 
3395 {
3396  int ret;
3397  AVIOContext *moov_buf;
3398  MOVMuxContext *mov = s->priv_data;
3399 
3400  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
3401  return ret;
3402  mov_write_moov_tag(moov_buf, mov, s);
3403  return ffio_close_null_buf(moov_buf);
3404 }
3405 
3406 /*
3407  * This function gets the moov size if moved to the top of the file: the chunk
3408  * offset table can switch between stco (32-bit entries) to co64 (64-bit
3409  * entries) when the moov is moved to the beginning, so the size of the moov
3410  * would change. It also updates the chunk offset tables.
3411  */
3413 {
3414  int i, moov_size, moov_size2;
3415  MOVMuxContext *mov = s->priv_data;
3416 
3417  moov_size = get_moov_size(s);
3418  if (moov_size < 0)
3419  return moov_size;
3420 
3421  for (i = 0; i < mov->nb_streams; i++)
3422  mov->tracks[i].data_offset += moov_size;
3423 
3424  moov_size2 = get_moov_size(s);
3425  if (moov_size2 < 0)
3426  return moov_size2;
3427 
3428  /* if the size changed, we just switched from stco to co64 and need to
3429  * update the offsets */
3430  if (moov_size2 != moov_size)
3431  for (i = 0; i < mov->nb_streams; i++)
3432  mov->tracks[i].data_offset += moov_size2 - moov_size;
3433 
3434  return moov_size2;
3435 }
3436 
3438 {
3439  int ret = 0, moov_size;
3440  MOVMuxContext *mov = s->priv_data;
3441  int64_t pos, pos_end = avio_tell(s->pb);
3442  uint8_t *buf, *read_buf[2];
3443  int read_buf_id = 0;
3444  int read_size[2];
3445  AVIOContext *read_pb;
3446 
3447  moov_size = compute_moov_size(s);
3448  if (moov_size < 0)
3449  return moov_size;
3450 
3451  buf = av_malloc(moov_size * 2);
3452  if (!buf)
3453  return AVERROR(ENOMEM);
3454  read_buf[0] = buf;
3455  read_buf[1] = buf + moov_size;
3456 
3457  /* Shift the data: the AVIO context of the output can only be used for
3458  * writing, so we re-open the same output, but for reading. It also avoids
3459  * a read/seek/write/seek back and forth. */
3460  avio_flush(s->pb);
3461  ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
3462  if (ret < 0) {
3463  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
3464  "the second pass (faststart)\n", s->filename);
3465  goto end;
3466  }
3467 
3468  /* mark the end of the shift to up to the last data we wrote, and get ready
3469  * for writing */
3470  pos_end = avio_tell(s->pb);
3471  avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
3472 
3473  /* start reading at where the new moov will be placed */
3474  avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
3475  pos = avio_tell(read_pb);
3476 
3477 #define READ_BLOCK do { \
3478  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
3479  read_buf_id ^= 1; \
3480 } while (0)
3481 
3482  /* shift data by chunk of at most moov_size */
3483  READ_BLOCK;
3484  do {
3485  int n;
3486  READ_BLOCK;
3487  n = read_size[read_buf_id];
3488  if (n <= 0)
3489  break;
3490  avio_write(s->pb, read_buf[read_buf_id], n);
3491  pos += n;
3492  } while (pos < pos_end);
3493  avio_close(read_pb);
3494 
3495 end:
3496  av_free(buf);
3497  return ret;
3498 }
3499 
3501 {
3502  MOVMuxContext *mov = s->priv_data;
3503  AVIOContext *pb = s->pb;
3504  int res = 0;
3505  int i;
3506  int64_t moov_pos;
3507 
3508  // If there were no chapters when the header was written, but there
3509  // are chapters now, write them in the trailer. This only works
3510  // when we are not doing fragments.
3511  if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3512  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
3513  mov->chapter_track = mov->nb_streams++;
3514  if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
3515  goto error;
3516  }
3517  }
3518 
3519  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3520  moov_pos = avio_tell(pb);
3521 
3522  /* Write size of mdat tag */
3523  if (mov->mdat_size + 8 <= UINT32_MAX) {
3524  avio_seek(pb, mov->mdat_pos, SEEK_SET);
3525  avio_wb32(pb, mov->mdat_size + 8);
3526  } else {
3527  /* overwrite 'wide' placeholder atom */
3528  avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
3529  /* special value: real atom size will be 64 bit value after
3530  * tag field */
3531  avio_wb32(pb, 1);
3532  ffio_wfourcc(pb, "mdat");
3533  avio_wb64(pb, mov->mdat_size + 16);
3534  }
3535  avio_seek(pb, moov_pos, SEEK_SET);
3536 
3537  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3538  av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
3539  res = shift_data(s);
3540  if (res == 0) {
3541  avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
3542  mov_write_moov_tag(pb, mov, s);
3543  }
3544  } else {
3545  mov_write_moov_tag(pb, mov, s);
3546  }
3547  } else {
3548  mov_flush_fragment(s);
3549  mov_write_mfra_tag(pb, mov);
3550  }
3551 
3552  for (i = 0; i < mov->nb_streams; i++) {
3553  if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
3554  mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
3555  int64_t off = avio_tell(pb);
3556  uint8_t buf[7];
3557  if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
3558  avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
3559  avio_write(pb, buf, 7);
3560  avio_seek(pb, off, SEEK_SET);
3561  }
3562  }
3563  }
3564 
3565 error:
3566  mov_free(s);
3567 
3568  return res;
3569 }
3570 
3571 #if CONFIG_MOV_MUXER
3572 MOV_CLASS(mov)
3573 AVOutputFormat ff_mov_muxer = {
3574  .name = "mov",
3575  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3576  .extensions = "mov",
3577  .priv_data_size = sizeof(MOVMuxContext),
3578  .audio_codec = AV_CODEC_ID_AAC,
3579  .video_codec = CONFIG_LIBX264_ENCODER ?
3585  .codec_tag = (const AVCodecTag* const []){
3587  },
3588  .priv_class = &mov_muxer_class,
3589 };
3590 #endif
3591 #if CONFIG_TGP_MUXER
3592 MOV_CLASS(tgp)
3593 AVOutputFormat ff_tgp_muxer = {
3594  .name = "3gp",
3595  .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
3596  .extensions = "3gp",
3597  .priv_data_size = sizeof(MOVMuxContext),
3598  .audio_codec = AV_CODEC_ID_AMR_NB,
3599  .video_codec = AV_CODEC_ID_H263,
3604  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3605  .priv_class = &tgp_muxer_class,
3606 };
3607 #endif
3608 #if CONFIG_MP4_MUXER
3609 MOV_CLASS(mp4)
3610 AVOutputFormat ff_mp4_muxer = {
3611  .name = "mp4",
3612  .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
3613  .mime_type = "application/mp4",
3614  .extensions = "mp4",
3615  .priv_data_size = sizeof(MOVMuxContext),
3616  .audio_codec = AV_CODEC_ID_AAC,
3617  .video_codec = CONFIG_LIBX264_ENCODER ?
3618  AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3623  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3624  .priv_class = &mp4_muxer_class,
3625 };
3626 #endif
3627 #if CONFIG_PSP_MUXER
3628 MOV_CLASS(psp)
3629 AVOutputFormat ff_psp_muxer = {
3630  .name = "psp",
3631  .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
3632  .extensions = "mp4,psp",
3633  .priv_data_size = sizeof(MOVMuxContext),
3634  .audio_codec = AV_CODEC_ID_AAC,
3635  .video_codec = CONFIG_LIBX264_ENCODER ?
3636  AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3641  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3642  .priv_class = &psp_muxer_class,
3643 };
3644 #endif
3645 #if CONFIG_TG2_MUXER
3646 MOV_CLASS(tg2)
3647 AVOutputFormat ff_tg2_muxer = {
3648  .name = "3g2",
3649  .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
3650  .extensions = "3g2",
3651  .priv_data_size = sizeof(MOVMuxContext),
3652  .audio_codec = AV_CODEC_ID_AMR_NB,
3653  .video_codec = AV_CODEC_ID_H263,
3658  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3659  .priv_class = &tg2_muxer_class,
3660 };
3661 #endif
3662 #if CONFIG_IPOD_MUXER
3663 MOV_CLASS(ipod)
3664 AVOutputFormat ff_ipod_muxer = {
3665  .name = "ipod",
3666  .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
3667  .mime_type = "application/mp4",
3668  .extensions = "m4v,m4a",
3669  .priv_data_size = sizeof(MOVMuxContext),
3670  .audio_codec = AV_CODEC_ID_AAC,
3671  .video_codec = AV_CODEC_ID_H264,
3676  .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
3677  .priv_class = &ipod_muxer_class,
3678 };
3679 #endif
3680 #if CONFIG_ISMV_MUXER
3681 MOV_CLASS(ismv)
3682 AVOutputFormat ff_ismv_muxer = {
3683  .name = "ismv",
3684  .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
3685  .mime_type = "application/mp4",
3686  .extensions = "ismv,isma",
3687  .priv_data_size = sizeof(MOVMuxContext),
3688  .audio_codec = AV_CODEC_ID_AAC,
3689  .video_codec = AV_CODEC_ID_H264,
3694  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3695  .priv_class = &ismv_muxer_class,
3696 };
3697 #endif
3698 #if CONFIG_F4V_MUXER
3699 MOV_CLASS(f4v)
3700 AVOutputFormat ff_f4v_muxer = {
3701  .name = "f4v",
3702  .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
3703  .mime_type = "application/f4v",
3704  .extensions = "f4v",
3705  .priv_data_size = sizeof(MOVMuxContext),
3706  .audio_codec = AV_CODEC_ID_AAC,
3707  .video_codec = AV_CODEC_ID_H264,
3712  .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
3713  .priv_class = &f4v_muxer_class,
3714 };
3715 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1053
#define MOV_TRACK_STPS
Definition: movenc.h:88
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:772
static int utf8len(const uint8_t *b)
Definition: movenc.c:1907
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
This function writes extradata "as is".
Definition: movenc.c:270
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:84
int ff_mov_iso639_to_lang(const char lang[4], int mp4)
Definition: isom.c:318
internal header for HEVC (de)muxer utilities
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:187
const struct AVCodec * codec
Definition: avcodec.h:1063
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov, int tracks, int moof_size)
Definition: movenc.c:2453
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition: movenc.c:1918
Bytestream IO Context.
Definition: avio.h:68
int packet_entry
Definition: movenc.h:133
#define FF_MOV_FLAG_FASTSTART
Definition: movenc.h:173
static int get_cluster_duration(MOVTrack *track, int cluster_idx)
Definition: movenc.c:545
static int mov_write_dinf_tag(AVIOContext *pb)
Definition: movenc.c:1240
Buffered I/O operations.
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:252
int size
AVCodecContext * enc
Definition: movenc.h:94
int tag
stsd fourcc
Definition: movenc.h:93
int language
Definition: movenc.h:91
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:960
AVOption.
Definition: opt.h:233
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1007
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:222
static void mov_write_psp_udta_tag(AVIOContext *pb, const char *str, const char *lang, int type)
Definition: movenc.c:2037
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:171
unsigned int entries
Definition: movenc.h:50
struct MOVTrack::@111 vc1_info
AVIOContext * mdat_buf
Definition: movenc.h:159
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1096
Definition: isom.h:45
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static void param_write_int(AVIOContext *pb, const char *name, int value)
Definition: movenc.c:2135
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
#define FF_MOV_FLAG_FRAGMENT
Definition: movenc.h:167
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int nb_frag_info
Definition: movenc.h:124
int max_fragment_size
Definition: movenc.h:157
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3208
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:199
static int mov_write_dref_tag(AVIOContext *pb)
Definition: movenc.c:1204
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int long_style)
Definition: movenc.c:1817
static const AVCodecTag codec_f4v_tags[]
Definition: movenc.c:906
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, int track_index, int sample, uint8_t *sample_data, int sample_size)
Definition: movenchint.c:400
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:841
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:747
int num
numerator
Definition: rational.h:44
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:118
int size
Definition: avcodec.h:974
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:181
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
#define AVIO_FLAG_READ
read-only
Definition: avio.h:292
#define av_bswap16
Definition: bswap.h:31
#define AV_RB24
Definition: intreadwrite.h:64
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1422
static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:338
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
long sample_count
Definition: movenc.h:84
static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
Definition: movenc.c:2475
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:985
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2064
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
Put a string representing the codec tag codec_tag in buf.
Definition: utils.c:1715
#define FF_ARRAY_ELEMS(a)
static const struct @110 mov_pix_fmt_tags[]
#define FF_MOV_FLAG_ISML
Definition: movenc.h:172
#define MOV_TFHD_DURATION_IS_EMPTY
Definition: isom.h:174
AVDictionary * metadata
Definition: avformat.h:858
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
static uint16_t language_code(const char *str)
Definition: movenc.c:1929
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:416
static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1292
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:948
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1816
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition: movenc.c:2376
int64_t track_duration
Definition: movenc.h:83
static int64_t duration
Definition: avplay.c:246
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1173
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:508
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:193
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:116
static int shift_data(AVFormatContext *s)
Definition: movenc.c:3437
Format I/O context.
Definition: avformat.h:871
int64_t frag_start
Definition: movenc.h:121
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:156
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:3026
static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:957
Public dictionary API.
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2050
int64_t default_duration
Definition: movenc.h:113
#define READ_BLOCK
uint32_t flags
Definition: movenc.h:90
#define FF_MOV_FLAG_EMPTY_MOOV
Definition: movenc.h:168
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
uint8_t
Round toward +infinity.
Definition: mathematics.h:53
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
Definition: movenc.c:1587
unsigned frag_info_capacity
Definition: movenc.h:126
Opaque data information usually continuous.
Definition: avutil.h:189
uint64_t mdat_size
Definition: movenc.h:145
unsigned int samples_in_chunk
Definition: movenc.h:49
#define MOV_CLASS(flavor)
Definition: movenc.c:71
AVOptions.
static int get_samples_per_packet(MOVTrack *track)
Definition: movenc.c:560
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:67
#define AV_RB32
Definition: intreadwrite.h:130
static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:857
static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2614
#define b
Definition: input.c:52
static const AVOption options[]
Definition: movenc.c:49
#define MODE_PSP
Definition: movenc.h:38
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
const char * name
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1503
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition: movenc.c:3061
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:935
static int mov_write_vmhd_tag(AVIOContext *pb)
Definition: movenc.c:1283
#define MODE_MP4
Definition: movenc.h:35
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:115
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:208
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:97
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2087
const char data[16]
Definition: mxf.c:66
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
Definition: avc.c:164
int chapter_track
qt chapter track number
Definition: movenc.h:143
uint8_t * data
Definition: avcodec.h:973
#define FF_MOV_FLAG_OMIT_TFHD_OFFSET
Definition: movenc.h:174
static int flags
Definition: log.c:44
int64_t time
Definition: movenc.h:73
static void mov_free(AVFormatContext *s)
Definition: movenc.c:3157
static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1361
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:179
uint32_t tag
Definition: movenc.c:822
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1767
int fragments
Definition: movenc.h:154
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:970
static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:357
int iods_audio_profile
Definition: movenc.h:152
bitstream reader API header.
#define MOV_TIMESCALE
Definition: movenc.h:31
int max_fragment_duration
Definition: movenc.h:155
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:685
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:73
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:382
int64_t mdat_pos
Definition: movenc.h:144
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
void ff_mov_close_hinting(MOVTrack *track)
Definition: movenchint.c:458
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2481
static float t
Definition: output.c:52
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
Write uuid atom.
Definition: movenc.c:941
unsigned int size
Definition: movenc.h:48
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:995
static const uint16_t fiel_data[]
Definition: movenc.c:953
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:172
static int mov_write_nmhd_tag(AVIOContext *pb)
Definition: movenc.c:1249
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:263
int ism_lookahead
Definition: movenc.h:158
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:890
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
#define MODE_3G2
Definition: movenc.h:40
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:915
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:122
uint32_t tref_tag
Definition: movenc.h:102
uint32_t flags
Definition: movenc.h:54
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1605
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:167
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1064
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1938
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1893
int64_t tfrf_offset
Definition: movenc.h:75
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO
Definition: isom.h:190
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2537
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:120
int64_t duration
Definition: movenc.h:74
static void put_descr(AVIOContext *pb, int tag, unsigned int size)
Definition: movenc.c:276
static int mov_flush_fragment(AVFormatContext *s)
Definition: movenc.c:2753
int64_t struct_offset
Definition: movenc.h:129
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1864
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:701
#define AVERROR(e)
Definition: error.h:43
static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
Definition: movenc.c:2696
static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
Definition: movenc.c:416
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
#define MODE_3GP
Definition: movenc.h:37
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:794
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:98
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:369
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1142
static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
Definition: movenc.c:1432
AVChapter ** chapters
Definition: avformat.h:1054
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2115
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2154
int height
active picture (w/o VBI) height for D-10/IMX
Definition: movenc.h:101
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:139
#define FF_MOV_FLAG_SEPARATE_MOOF
Definition: movenc.h:170
enum AVCodecID codec_id
Definition: mov_chan.c:432
static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int moof_size)
Definition: movenc.c:2306
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:323
#define FFMAX(a, b)
Definition: common.h:55
int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to the provided AVIOContext.
Definition: hevc.c:1017
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:95
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:191
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
int mode
Definition: movenc.h:79
const AVCodecTag ff_mp4_obj_type[]
Definition: isom.c:32
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:286
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:96
static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1385
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
int off
Definition: dsputil_bfin.c:29
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1840
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2093
static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:348
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int vos_len
Definition: movenc.h:96
int iods_skip
Definition: movenc.h:150
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:509
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:923
int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition: avc.c:70
#define LIBAVFORMAT_IDENT
Definition: version.h:44
uint64_t pos
Definition: movenc.h:46
int64_t dts
Definition: movenc.h:47
#define MOV_FRAG_INFO_ALLOC_INCREMENT
Definition: movenc.h:29
#define MOV_TRACK_ENABLED
Definition: movenc.h:89
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1163
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
Definition: avcodec.h:1112
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1842
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
int64_t data_offset
Definition: movenc.h:120
char filename[1024]
input or output filename
Definition: avformat.h:943
static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset)
Definition: movenc.c:2249
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:110
#define FFMIN(a, b)
Definition: common.h:57
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1695
int audio_vbr
Definition: movenc.h:100
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:124
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:1958
#define MOV_TKHD_FLAG_IN_MOVIE
Definition: isom.h:194
#define MOV_PARTIAL_SYNC_SAMPLE
Definition: movenc.h:53
int width
picture width / height.
Definition: avcodec.h:1217
int64_t tfrf_offset
Definition: movenc.h:122
int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
Definition: movenchint.c:29
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:3294
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:680
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:406
static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:1626
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
Definition: hevc.c:1065
const char * name
Definition: avformat.h:437
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:795
long sample_size
Definition: movenc.h:85
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style)
Definition: movenc.c:1803
static int mov_write_smhd_tag(AVIOContext *pb)
Definition: movenc.c:1273
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:98
#define AV_RL32
Definition: intreadwrite.h:146
static void param_write_string(AVIOContext *pb, const char *name, const char *value)
Definition: movenc.c:2140
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:178
AVDictionary * metadata
Definition: avformat.h:749
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:982
static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:690
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:186
enum AVPixelFormat pix_fmt
Definition: movenc.c:821
static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1682
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:110
#define MOV_TRACK_CTTS
Definition: movenc.h:87
static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2240
int iods_video_profile
Definition: movenc.h:151
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:683
int64_t reserved_moov_pos
Definition: movenc.h:161
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:857
NULL
Definition: eval.c:55
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:652
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:285
#define MOV_INDEX_CLUSTER_SIZE
Definition: movenc.h:30
static int mov_write_d263_tag(AVIOContext *pb)
Definition: movenc.c:655
enum AVMediaType codec_type
Definition: avcodec.h:1062
version
Definition: ffv1enc.c:1080
#define CONFIG_LIBX264_ENCODER
Definition: config.h:992
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:235
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1129
int duration
Definition: isom.h:47
enum AVCodecID codec_id
Definition: avcodec.h:1065
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
int cts
Definition: movenc.h:51
int sample_rate
samples per second
Definition: avcodec.h:1779
AVIOContext * pb
I/O context.
Definition: avformat.h:913
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition: rtpenc.h:73
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
main external API structure.
Definition: avcodec.h:1054
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:177
#define FF_MOV_FLAG_FRAG_CUSTOM
Definition: movenc.h:171
int hint_track
the track that hints this track, -1 if no hint track is set
Definition: movenc.h:106
static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:998
static int mov_write_hmhd_tag(AVIOContext *pb)
Definition: movenc.c:1346
int slices
Definition: movenc.h:134
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1080
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition: movenc.c:2675
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:66
static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:491
int extradata_size
Definition: avcodec.h:1163
uint32_t default_size
Definition: movenc.h:115
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1529
#define FF_MOV_FLAG_RTP_HINT
Definition: movenc.h:166
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2330
int count
Definition: isom.h:46
Y , 16bpp, big-endian.
Definition: pixfmt.h:100
int index
Definition: gxfenc.c:72
static int co64_required(const MOVTrack *track)
Definition: movenc.c:90
rational number numerator/denominator
Definition: rational.h:43
uint64_t time
Definition: movenc.h:82
int first_packet_seq
Definition: movenc.h:130
static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
Definition: movenc.c:2145
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int64_t time
Definition: movenc.h:141
#define AV_WB16(p, d)
Definition: intreadwrite.h:213
int has_keyframes
Definition: movenc.h:86
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:997
int entry
Definition: movenc.h:80
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:173
MOVFragmentInfo * frag_info
Definition: movenc.h:125
char * major_brand
Definition: movenc.h:163
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:180
uint8_t * vos_data
Definition: movenc.h:97
int first_packet_entry
Definition: movenc.h:131
Round toward -infinity.
Definition: mathematics.h:52
static const AVCodecTag codec_3gp_tags[]
Definition: movenc.c:895
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:342
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:748
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:117
AVFormatContext * rtp_ctx
the format context for the hinting rtp muxer
Definition: movenc.h:108
uint8_t level
Definition: svq3.c:143
int track_id
Definition: movenc.h:92
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1984
static int mov_write_trailer(AVFormatContext *s)
Definition: movenc.c:3500
int64_t start
Definition: avformat.h:857
#define FF_MOV_FLAG_FRAG_KEYFRAME
Definition: movenc.h:169
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, const char *tag, const char *str)
Definition: movenc.c:1936
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1706
Main libavformat public API header.
static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2412
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx, const char *dest_addr, const char *dest_type, int port, int ttl, AVFormatContext *fmt)
Append the media-specific SDP fragment for the media stream c to the buffer buff. ...
Definition: sdp.c:694
int packet_seq
Definition: movenc.h:132
static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2487
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:88
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:169
static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
Definition: movenc.c:2300
#define MOV_SYNC_SAMPLE
Definition: movenc.h:52
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2356
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:112
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:738
uint32_t max_packet_size
Definition: movenc.h:111
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
Definition: riffenc.c:50
static int mov_write_gmhd_tag(AVIOContext *pb)
Definition: movenc.c:1257
int src_track
the track that this hint track describes
Definition: movenc.h:107
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:856
static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1421
static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset, int moof_size)
Definition: movenc.c:2426
char * key
Definition: dict.h:75
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:53
int den
denominator
Definition: rational.h:45
MOVIentry * cluster
Definition: movenc.h:98
unsigned bps
Definition: movenc.c:823
#define MODE_IPOD
Definition: movenc.h:41
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:579
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1653
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
Definition: movenc.c:185
char * value
Definition: dict.h:76
uint32_t default_sample_flags
Definition: movenc.h:114
unsigned timescale
Definition: movenc.h:81
int len
static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2511
int channels
number of audio channels
Definition: avcodec.h:1780
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
Definition: movenc.c:1783
void * priv_data
Format private data.
Definition: avformat.h:899
static int mov_write_header(AVFormatContext *s)
Definition: movenc.c:3181
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
static int64_t update_size(AVIOContext *pb, int64_t pos)
Definition: movenc.c:80
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:176
int64_t start_dts
Definition: movenc.h:104
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
Definition: vc1.h:424
#define LIBAVCODEC_IDENT
Definition: version.h:43
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
int tref_id
trackID of the referenced track
Definition: movenc.h:103
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2549
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
static const AVCodecTag codec_ipod_tags[]
Definition: movenc.c:767
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1776
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1076
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1576
#define MODE_F4V
Definition: movenc.h:43
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes HEVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext...
Definition: hevc.c:1081
unsigned cluster_capacity
Definition: movenc.h:99
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:2883
static int mov_get_lpcm_flags(enum AVCodecID codec_id)
Compute flags for 'lpcm' tag.
Definition: movenc.c:520
int stream_index
Definition: avcodec.h:975
static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1218
#define MODE_MOV
Definition: movenc.h:36
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:719
uint32_t ff_mov_get_channel_layout_tag(enum AVCodecID codec_id, uint64_t channel_layout, uint32_t *bitmap)
Get the channel layout tag for the specified codec id and channel layout.
Definition: mov_chan.c:493
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2122
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:778
static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1109
static int mov_write_svq3_tag(AVIOContext *pb)
Definition: movenc.c:668
#define MKTAG(a, b, c, d)
Definition: common.h:238
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:422
static void enable_tracks(AVFormatContext *s)
Definition: movenc.c:3118
#define MODE_ISM
Definition: movenc.h:42
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Definition: utils.c:3159
This structure stores compressed data.
Definition: avcodec.h:950
int64_t offset
Definition: movenc.h:72
int min_fragment_duration
Definition: movenc.h:156
MOVTrack * tracks
Definition: movenc.h:146
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2327
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
AVIOContext * mdat_buf
Definition: movenc.h:119
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
int nb_streams
Definition: movenc.h:142
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
static av_always_inline const uint8_t * find_next_marker(const uint8_t *src, const uint8_t *end)
Find VC-1 marker in buffer.
Definition: vc1.h:410
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:184
static int compute_moov_size(AVFormatContext *s)
Definition: movenc.c:3412
static int get_moov_size(AVFormatContext *s)
Definition: movenc.c:3394
bitstream writer API