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 #include <inttypes.h>
26 
27 #include "movenc.h"
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "riff.h"
31 #include "avio.h"
32 #include "isom.h"
33 #include "avc.h"
34 #include "libavcodec/get_bits.h"
35 #include "libavcodec/put_bits.h"
36 #include "libavcodec/vc1_common.h"
37 #include "internal.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/intfloat.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/dict.h"
43 #include "hevc.h"
44 #include "rtpenc.h"
45 #include "mov_chan.h"
46 
47 static const AVOption options[] = {
48  { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
49  { "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" },
50  { "empty_moov", "Make the initial moov atom empty", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
51  { "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" },
52  { "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" },
53  { "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" },
54  { "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" },
55  { "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" },
56  { "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" },
57  { "disable_chpl", "Disable Nero chapter atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DISABLE_CHPL}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
58  { "default_base_moof", "Set the default-base-is-moof flag in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DEFAULT_BASE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
59  { "dash", "Write DASH compatible fragmented MP4", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DASH}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
60  { "frag_discont", "Signal that the next fragment is discontinuous from earlier ones", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_DISCONT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
61  { "delay_moov", "Delay writing the initial moov until the first fragment is cut, or until the first fragment flush", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DELAY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
62  { "global_sidx", "Write a global sidx index at the start of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_GLOBAL_SIDX}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
63  { "skip_trailer", "Skip writing the mfra/tfra/mfro trailer for fragmented files", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SKIP_TRAILER}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
64  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
65  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
66  { "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},
67  { "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},
68  { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
69  { "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},
70  { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
71  { "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},
72  { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
73  { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
74  { "fragment_index", "Fragment number of the next fragment", offsetof(MOVMuxContext, fragments), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
75  { "frag_interleave", "Interleave samples within fragments (max number of consecutive samples, lower is tighter interleaving, but with more overhead)", offsetof(MOVMuxContext, frag_interleave), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
76  { NULL },
77 };
78 
79 #define MOV_CLASS(flavor)\
80 static const AVClass flavor ## _muxer_class = {\
81  .class_name = #flavor " muxer",\
82  .item_name = av_default_item_name,\
83  .option = options,\
84  .version = LIBAVUTIL_VERSION_INT,\
85 };
86 
87 static int utf8len(const uint8_t *b)
88 {
89  int len = 0;
90  int val;
91  while (*b) {
92  GET_UTF8(val, *b++, return -1;)
93  len++;
94  }
95  return len;
96 }
97 
98 //FIXME support 64 bit variant with wide placeholders
99 static int64_t update_size(AVIOContext *pb, int64_t pos)
100 {
101  int64_t curpos = avio_tell(pb);
102  avio_seek(pb, pos, SEEK_SET);
103  avio_wb32(pb, curpos - pos); /* rewrite size */
104  avio_seek(pb, curpos, SEEK_SET);
105 
106  return curpos - pos;
107 }
108 
109 static int co64_required(const MOVTrack *track)
110 {
111  if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
112  return 1;
113  return 0;
114 }
115 
116 /* Chunk offset atom */
117 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
118 {
119  int i;
120  int mode64 = co64_required(track); // use 32 bit size variant if possible
121  int64_t pos = avio_tell(pb);
122  avio_wb32(pb, 0); /* size */
123  if (mode64) {
124  ffio_wfourcc(pb, "co64");
125  } else
126  ffio_wfourcc(pb, "stco");
127  avio_wb32(pb, 0); /* version & flags */
128  avio_wb32(pb, track->entry); /* entry count */
129  for (i = 0; i < track->entry; i++) {
130  if (mode64 == 1)
131  avio_wb64(pb, track->cluster[i].pos + track->data_offset);
132  else
133  avio_wb32(pb, track->cluster[i].pos + track->data_offset);
134  }
135  return update_size(pb, pos);
136 }
137 
138 /* Sample size atom */
139 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
140 {
141  int equalChunks = 1;
142  int i, j, entries = 0, tst = -1, oldtst = -1;
143 
144  int64_t pos = avio_tell(pb);
145  avio_wb32(pb, 0); /* size */
146  ffio_wfourcc(pb, "stsz");
147  avio_wb32(pb, 0); /* version & flags */
148 
149  for (i = 0; i < track->entry; i++) {
150  tst = track->cluster[i].size / track->cluster[i].entries;
151  if (oldtst != -1 && tst != oldtst)
152  equalChunks = 0;
153  oldtst = tst;
154  entries += track->cluster[i].entries;
155  }
156  if (equalChunks && track->entry) {
157  int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
158  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
159  avio_wb32(pb, sSize); // sample size
160  avio_wb32(pb, entries); // sample count
161  } else {
162  avio_wb32(pb, 0); // sample size
163  avio_wb32(pb, entries); // sample count
164  for (i = 0; i < track->entry; i++) {
165  for (j = 0; j < track->cluster[i].entries; j++) {
166  avio_wb32(pb, track->cluster[i].size /
167  track->cluster[i].entries);
168  }
169  }
170  }
171  return update_size(pb, pos);
172 }
173 
174 /* Sample to chunk atom */
175 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
176 {
177  int index = 0, oldval = -1, i;
178  int64_t entryPos, curpos;
179 
180  int64_t pos = avio_tell(pb);
181  avio_wb32(pb, 0); /* size */
182  ffio_wfourcc(pb, "stsc");
183  avio_wb32(pb, 0); // version & flags
184  entryPos = avio_tell(pb);
185  avio_wb32(pb, track->entry); // entry count
186  for (i = 0; i < track->entry; i++) {
187  if (oldval != track->cluster[i].samples_in_chunk) {
188  avio_wb32(pb, i + 1); // first chunk
189  avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
190  avio_wb32(pb, 0x1); // sample description index
191  oldval = track->cluster[i].samples_in_chunk;
192  index++;
193  }
194  }
195  curpos = avio_tell(pb);
196  avio_seek(pb, entryPos, SEEK_SET);
197  avio_wb32(pb, index); // rewrite size
198  avio_seek(pb, curpos, SEEK_SET);
199 
200  return update_size(pb, pos);
201 }
202 
203 /* Sync sample atom */
204 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
205 {
206  int64_t curpos, entryPos;
207  int i, index = 0;
208  int64_t pos = avio_tell(pb);
209  avio_wb32(pb, 0); // size
210  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
211  avio_wb32(pb, 0); // version & flags
212  entryPos = avio_tell(pb);
213  avio_wb32(pb, track->entry); // entry count
214  for (i = 0; i < track->entry; i++) {
215  if (track->cluster[i].flags & flag) {
216  avio_wb32(pb, i + 1);
217  index++;
218  }
219  }
220  curpos = avio_tell(pb);
221  avio_seek(pb, entryPos, SEEK_SET);
222  avio_wb32(pb, index); // rewrite size
223  avio_seek(pb, curpos, SEEK_SET);
224  return update_size(pb, pos);
225 }
226 
227 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
228 {
229  avio_wb32(pb, 0x11); /* size */
230  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
231  else ffio_wfourcc(pb, "damr");
232  ffio_wfourcc(pb, "FFMP");
233  avio_w8(pb, 0); /* decoder version */
234 
235  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
236  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
237  avio_w8(pb, 0x01); /* Frames per sample */
238  return 0x11;
239 }
240 
241 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
242 {
243  GetBitContext gbc;
244  PutBitContext pbc;
245  uint8_t buf[3];
246  int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
247 
248  if (track->vos_len < 7)
249  return -1;
250 
251  avio_wb32(pb, 11);
252  ffio_wfourcc(pb, "dac3");
253 
254  init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
255  fscod = get_bits(&gbc, 2);
256  frmsizecod = get_bits(&gbc, 6);
257  bsid = get_bits(&gbc, 5);
258  bsmod = get_bits(&gbc, 3);
259  acmod = get_bits(&gbc, 3);
260  if (acmod == 2) {
261  skip_bits(&gbc, 2); // dsurmod
262  } else {
263  if ((acmod & 1) && acmod != 1)
264  skip_bits(&gbc, 2); // cmixlev
265  if (acmod & 4)
266  skip_bits(&gbc, 2); // surmixlev
267  }
268  lfeon = get_bits1(&gbc);
269 
270  init_put_bits(&pbc, buf, sizeof(buf));
271  put_bits(&pbc, 2, fscod);
272  put_bits(&pbc, 5, bsid);
273  put_bits(&pbc, 3, bsmod);
274  put_bits(&pbc, 3, acmod);
275  put_bits(&pbc, 1, lfeon);
276  put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
277  put_bits(&pbc, 5, 0); // reserved
278 
279  flush_put_bits(&pbc);
280  avio_write(pb, buf, sizeof(buf));
281 
282  return 11;
283 }
284 
290 {
291  avio_write(pb, track->par->extradata, track->par->extradata_size);
292  return track->par->extradata_size;
293 }
294 
295 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
296 {
297  int i = 3;
298  avio_w8(pb, tag);
299  for (; i > 0; i--)
300  avio_w8(pb, (size >> (7 * i)) | 0x80);
301  avio_w8(pb, size & 0x7F);
302 }
303 
304 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
305 {
306  AVCPBProperties *props;
307  int64_t pos = avio_tell(pb);
308  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
309 
310  avio_wb32(pb, 0); // size
311  ffio_wfourcc(pb, "esds");
312  avio_wb32(pb, 0); // Version
313 
314  // ES descriptor
315  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
316  avio_wb16(pb, track->track_id);
317  avio_w8(pb, 0x00); // flags (= no flags)
318 
319  // DecoderConfig descriptor
320  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
321 
322  // Object type indication
323  if ((track->par->codec_id == AV_CODEC_ID_MP2 ||
324  track->par->codec_id == AV_CODEC_ID_MP3) &&
325  track->par->sample_rate > 24000)
326  avio_w8(pb, 0x6B); // 11172-3
327  else
329 
330  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
331  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
332  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
333  avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
334  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
335  avio_w8(pb, 0x15); // flags (= Audiostream)
336  else
337  avio_w8(pb, 0x11); // flags (= Visualstream)
338 
340  NULL);
341 
342  avio_wb24(pb, props ? props->buffer_size / 8 : 0); // Buffersize DB
343 
344  avio_wb32(pb, props ? FFMAX(props->max_bitrate, props->avg_bitrate) : track->par->bit_rate); // maxbitrate (FIXME should be max rate in any 1 sec window)
345  if (!props || !props->min_bitrate || props->max_bitrate != props->min_bitrate)
346  avio_wb32(pb, 0); // vbr
347  else
348  avio_wb32(pb, props->max_bitrate); // avg bitrate
349 
350  if (track->vos_len) {
351  // DecoderSpecific info descriptor
352  put_descr(pb, 0x05, track->vos_len);
353  avio_write(pb, track->vos_data, track->vos_len);
354  }
355 
356  // SL descriptor
357  put_descr(pb, 0x06, 1);
358  avio_w8(pb, 0x02);
359  return update_size(pb, pos);
360 }
361 
363 {
364  int64_t pos = avio_tell(pb);
365  avio_wb32(pb, 0);
366  avio_wl32(pb, track->tag); // store it byteswapped
367  track->par->codec_tag = av_bswap16(track->tag >> 16);
368  ff_put_wav_header(s, pb, track->st->codecpar);
369  return update_size(pb, pos);
370 }
371 
373 {
374  int64_t pos = avio_tell(pb);
375  avio_wb32(pb, 0);
376  ffio_wfourcc(pb, "wfex");
377  ff_put_wav_header(s, pb, track->st->codecpar);
378  return update_size(pb, pos);
379 }
380 
382 {
383  uint32_t layout_tag, bitmap;
384  int64_t pos = avio_tell(pb);
385 
386  layout_tag = ff_mov_get_channel_layout_tag(track->par->codec_id,
387  track->par->channel_layout,
388  &bitmap);
389  if (!layout_tag) {
390  av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to "
391  "lack of channel information\n");
392  return 0;
393  }
394 
395  avio_wb32(pb, 0); // Size
396  ffio_wfourcc(pb, "chan"); // Type
397  avio_w8(pb, 0); // Version
398  avio_wb24(pb, 0); // Flags
399  avio_wb32(pb, layout_tag); // mChannelLayoutTag
400  avio_wb32(pb, bitmap); // mChannelBitmap
401  avio_wb32(pb, 0); // mNumberChannelDescriptions
402 
403  return update_size(pb, pos);
404 }
405 
407 {
408  int64_t pos = avio_tell(pb);
409 
410  avio_wb32(pb, 0); /* size */
411  ffio_wfourcc(pb, "wave");
412 
413  avio_wb32(pb, 12); /* size */
414  ffio_wfourcc(pb, "frma");
415  avio_wl32(pb, track->tag);
416 
417  if (track->par->codec_id == AV_CODEC_ID_AAC) {
418  /* useless atom needed by mplayer, ipod, not needed by quicktime */
419  avio_wb32(pb, 12); /* size */
420  ffio_wfourcc(pb, "mp4a");
421  avio_wb32(pb, 0);
422  mov_write_esds_tag(pb, track);
423  } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
424  mov_write_amr_tag(pb, track);
425  } else if (track->par->codec_id == AV_CODEC_ID_AC3) {
426  mov_write_ac3_tag(pb, track);
427  } else if (track->par->codec_id == AV_CODEC_ID_ALAC) {
428  mov_write_extradata_tag(pb, track);
429  } else if (track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
431  mov_write_ms_tag(s, pb, track);
432  }
433 
434  avio_wb32(pb, 8); /* size */
435  avio_wb32(pb, 0); /* null tag */
436 
437  return update_size(pb, pos);
438 }
439 
440 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
441 {
442  uint8_t *unescaped;
443  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
444  int unescaped_size, seq_found = 0;
445  int level = 0, interlace = 0;
446  int packet_seq = track->vc1_info.packet_seq;
447  int packet_entry = track->vc1_info.packet_entry;
448  int slices = track->vc1_info.slices;
449  PutBitContext pbc;
450 
451  if (track->start_dts == AV_NOPTS_VALUE) {
452  /* No packets written yet, vc1_info isn't authoritative yet. */
453  /* Assume inline sequence and entry headers. */
454  packet_seq = packet_entry = 1;
456  "moov atom written before any packets, unable to write correct "
457  "dvc1 atom. Set the delay_moov flag to fix this.\n");
458  }
459 
460  unescaped = av_mallocz(track->vos_len + AV_INPUT_BUFFER_PADDING_SIZE);
461  if (!unescaped)
462  return AVERROR(ENOMEM);
463  start = find_next_marker(track->vos_data, end);
464  for (next = start; next < end; start = next) {
465  GetBitContext gb;
466  int size;
467  next = find_next_marker(start + 4, end);
468  size = next - start - 4;
469  if (size <= 0)
470  continue;
471  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
472  init_get_bits(&gb, unescaped, 8 * unescaped_size);
473  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
474  int profile = get_bits(&gb, 2);
475  if (profile != PROFILE_ADVANCED) {
476  av_free(unescaped);
477  return AVERROR(ENOSYS);
478  }
479  seq_found = 1;
480  level = get_bits(&gb, 3);
481  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
482  * width, height */
483  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
484  skip_bits(&gb, 1); /* broadcast */
485  interlace = get_bits1(&gb);
486  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
487  }
488  }
489  if (!seq_found) {
490  av_free(unescaped);
491  return AVERROR(ENOSYS);
492  }
493 
494  init_put_bits(&pbc, buf, 7);
495  /* VC1DecSpecStruc */
496  put_bits(&pbc, 4, 12); /* profile - advanced */
497  put_bits(&pbc, 3, level);
498  put_bits(&pbc, 1, 0); /* reserved */
499  /* VC1AdvDecSpecStruc */
500  put_bits(&pbc, 3, level);
501  put_bits(&pbc, 1, 0); /* cbr */
502  put_bits(&pbc, 6, 0); /* reserved */
503  put_bits(&pbc, 1, !interlace); /* no interlace */
504  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
505  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
506  put_bits(&pbc, 1, !slices); /* no slice code */
507  put_bits(&pbc, 1, 0); /* no bframe */
508  put_bits(&pbc, 1, 0); /* reserved */
509 
510  /* framerate */
511  if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
512  put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
513  else
514  put_bits32(&pbc, 0xffffffff);
515 
516  flush_put_bits(&pbc);
517 
518  av_free(unescaped);
519 
520  return 0;
521 }
522 
523 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
524 {
525  uint8_t buf[7] = { 0 };
526  int ret;
527 
528  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
529  return ret;
530 
531  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
532  ffio_wfourcc(pb, "dvc1");
533  avio_write(pb, buf, sizeof(buf));
534  avio_write(pb, track->vos_data, track->vos_len);
535 
536  return 0;
537 }
538 
539 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
540 {
541  avio_wb32(pb, track->vos_len + 8);
542  ffio_wfourcc(pb, "glbl");
543  avio_write(pb, track->vos_data, track->vos_len);
544  return 8 + track->vos_len;
545 }
546 
552 {
553  switch (codec_id) {
556  return 11;
559  return 9;
560  case AV_CODEC_ID_PCM_U8:
561  return 10;
565  return 14;
566  case AV_CODEC_ID_PCM_S8:
570  return 12;
571  default:
572  return 0;
573  }
574 }
575 
576 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
577 {
578  int64_t next_dts;
579 
580  if (cluster_idx >= track->entry)
581  return 0;
582 
583  if (cluster_idx + 1 == track->entry)
584  next_dts = track->track_duration + track->start_dts;
585  else
586  next_dts = track->cluster[cluster_idx + 1].dts;
587 
588  return next_dts - track->cluster[cluster_idx].dts;
589 }
590 
592 {
593  int i, first_duration;
594 
595  /* use 1 for raw PCM */
596  if (!track->audio_vbr)
597  return 1;
598 
599  /* check to see if duration is constant for all clusters */
600  if (!track->entry)
601  return 0;
602  first_duration = get_cluster_duration(track, 0);
603  for (i = 1; i < track->entry; i++) {
604  if (get_cluster_duration(track, i) != first_duration)
605  return 0;
606  }
607  return first_duration;
608 }
609 
611 {
612  int64_t pos = avio_tell(pb);
613  int version = 0;
614  uint32_t tag = track->tag;
615 
616  if (track->mode == MODE_MOV) {
617  if (mov_get_lpcm_flags(track->par->codec_id))
618  tag = AV_RL32("lpcm");
619  version = 2;
620  }
621 
622  avio_wb32(pb, 0); /* size */
623  avio_wl32(pb, tag); // store it byteswapped
624  avio_wb32(pb, 0); /* Reserved */
625  avio_wb16(pb, 0); /* Reserved */
626  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
627 
628  /* SoundDescription */
629  avio_wb16(pb, version); /* Version */
630  avio_wb16(pb, 0); /* Revision level */
631  avio_wb32(pb, 0); /* Reserved */
632 
633  if (version == 2) {
634  avio_wb16(pb, 3);
635  avio_wb16(pb, 16);
636  avio_wb16(pb, 0xfffe);
637  avio_wb16(pb, 0);
638  avio_wb32(pb, 0x00010000);
639  avio_wb32(pb, 72);
640  avio_wb64(pb, av_double2int(track->par->sample_rate));
641  avio_wb32(pb, track->par->channels);
642  avio_wb32(pb, 0x7F000000);
645  avio_wb32(pb, track->sample_size);
646  avio_wb32(pb, get_samples_per_packet(track));
647  } else {
648  /* reserved for mp4/3gp */
649  avio_wb16(pb, 2);
650  avio_wb16(pb, 16);
651  avio_wb16(pb, 0);
652 
653  avio_wb16(pb, 0); /* packet size (= 0) */
654  avio_wb16(pb, track->par->sample_rate <= UINT16_MAX ?
655  track->par->sample_rate : 0);
656  avio_wb16(pb, 0); /* Reserved */
657  }
658 
659  if (track->mode == MODE_MOV &&
660  (track->par->codec_id == AV_CODEC_ID_AAC ||
661  track->par->codec_id == AV_CODEC_ID_AC3 ||
662  track->par->codec_id == AV_CODEC_ID_AMR_NB ||
663  track->par->codec_id == AV_CODEC_ID_ALAC ||
664  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
666  mov_write_wave_tag(s, pb, track);
667  else if (track->tag == MKTAG('m','p','4','a'))
668  mov_write_esds_tag(pb, track);
669  else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
670  mov_write_amr_tag(pb, track);
671  else if (track->par->codec_id == AV_CODEC_ID_AC3)
672  mov_write_ac3_tag(pb, track);
673  else if (track->par->codec_id == AV_CODEC_ID_ALAC)
674  mov_write_extradata_tag(pb, track);
675  else if (track->par->codec_id == AV_CODEC_ID_WMAPRO)
676  mov_write_wfex_tag(s, pb, track);
677  else if (track->vos_len > 0)
678  mov_write_glbl_tag(pb, track);
679 
680  if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO)
681  mov_write_chan_tag(s, pb, track);
682 
683  return update_size(pb, pos);
684 }
685 
687 {
688  avio_wb32(pb, 0xf); /* size */
689  ffio_wfourcc(pb, "d263");
690  ffio_wfourcc(pb, "FFMP");
691  avio_w8(pb, 0); /* decoder version */
692  /* FIXME use AVCodecContext level/profile, when encoder will set values */
693  avio_w8(pb, 0xa); /* level */
694  avio_w8(pb, 0); /* profile */
695  return 0xf;
696 }
697 
698 /* TODO: No idea about these values */
700 {
701  avio_wb32(pb, 0x15);
702  ffio_wfourcc(pb, "SMI ");
703  ffio_wfourcc(pb, "SEQH");
704  avio_wb32(pb, 0x5);
705  avio_wb32(pb, 0xe2c0211d);
706  avio_wb32(pb, 0xc0000000);
707  avio_w8(pb, 0);
708  return 0x15;
709 }
710 
711 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
712 {
713  int64_t pos = avio_tell(pb);
714 
715  avio_wb32(pb, 0);
716  ffio_wfourcc(pb, "avcC");
717  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
718  return update_size(pb, pos);
719 }
720 
721 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
722 {
723  int64_t pos = avio_tell(pb);
724 
725  avio_wb32(pb, 0);
726  ffio_wfourcc(pb, "hvcC");
727  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
728  return update_size(pb, pos);
729 }
730 
731 /* also used by all avid codecs (dv, imx, meridien) and their variants */
732 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
733 {
734  int i;
735  avio_wb32(pb, 24); /* size */
736  ffio_wfourcc(pb, "ACLR");
737  ffio_wfourcc(pb, "ACLR");
738  ffio_wfourcc(pb, "0001");
739  avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
740  avio_wb32(pb, 0); /* unknown */
741 
742  avio_wb32(pb, 24); /* size */
743  ffio_wfourcc(pb, "APRG");
744  ffio_wfourcc(pb, "APRG");
745  ffio_wfourcc(pb, "0001");
746  avio_wb32(pb, 1); /* unknown */
747  avio_wb32(pb, 0); /* unknown */
748 
749  avio_wb32(pb, 120); /* size */
750  ffio_wfourcc(pb, "ARES");
751  ffio_wfourcc(pb, "ARES");
752  ffio_wfourcc(pb, "0001");
753  avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
754  avio_wb32(pb, track->par->width);
755  /* values below are based on samples created with quicktime and avid codecs */
756  if (track->vos_data[5] & 2) { // interlaced
757  avio_wb32(pb, track->par->height / 2);
758  avio_wb32(pb, 2); /* unknown */
759  avio_wb32(pb, 0); /* unknown */
760  avio_wb32(pb, 4); /* unknown */
761  } else {
762  avio_wb32(pb, track->par->height);
763  avio_wb32(pb, 1); /* unknown */
764  avio_wb32(pb, 0); /* unknown */
765  if (track->par->height == 1080)
766  avio_wb32(pb, 5); /* unknown */
767  else
768  avio_wb32(pb, 6); /* unknown */
769  }
770  /* padding */
771  for (i = 0; i < 10; i++)
772  avio_wb64(pb, 0);
773 
774  /* extra padding for stsd needed */
775  avio_wb32(pb, 0);
776  return 0;
777 }
778 
780 {
781  int tag = track->par->codec_tag;
782 
784  return 0;
785 
786  if (track->par->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
787  else if (track->par->codec_id == AV_CODEC_ID_HEVC) tag = MKTAG('h','e','v','1');
788  else if (track->par->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
789  else if (track->par->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
790  else if (track->par->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
791  else if (track->par->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
792  else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
793  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
794  else if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE) tag = MKTAG('m','p','4','s');
795 
796  return tag;
797 }
798 
799 static const AVCodecTag codec_ipod_tags[] = {
800  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
801  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
802  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
803  { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
804  { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
805  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
806  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
807  { AV_CODEC_ID_NONE, 0 },
808 };
809 
811 {
812  int tag = track->par->codec_tag;
813 
814  // keep original tag for subs, ipod supports both formats
815  if (!(track->par->codec_type == AVMEDIA_TYPE_SUBTITLE &&
816  (tag == MKTAG('t', 'x', '3', 'g') ||
817  tag == MKTAG('t', 'e', 'x', 't'))))
818  tag = ff_codec_get_tag(codec_ipod_tags, track->par->codec_id);
819 
820  if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
821  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
822  "Quicktime/Ipod might not play the file\n");
823 
824  return tag;
825 }
826 
828 {
829  int tag;
830 
831  if (track->par->width == 720) /* SD */
832  if (track->par->height == 480) /* NTSC */
833  if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
834  else tag = MKTAG('d','v','c',' ');
835  else if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
836  else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
837  else tag = MKTAG('d','v','p','p');
838  else if (track->par->height == 720) /* HD 720 line */
839  if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
840  else tag = MKTAG('d','v','h','p');
841  else if (track->par->height == 1080) /* HD 1080 line */
842  if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
843  else tag = MKTAG('d','v','h','6');
844  else {
845  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
846  return 0;
847  }
848 
849  return tag;
850 }
851 
852 static const struct {
854  uint32_t tag;
855  unsigned bps;
856 } mov_pix_fmt_tags[] = {
857  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
858  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
859  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
860  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
861  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
862  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
863  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
864  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
865  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
866  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
867  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
868  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
869  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
870  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
871 };
872 
874 {
875  int tag = track->par->codec_tag;
876  int i;
877 
878  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
879  if (track->par->format == mov_pix_fmt_tags[i].pix_fmt) {
880  tag = mov_pix_fmt_tags[i].tag;
881  track->par->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
882  break;
883  }
884  }
885 
886  return tag;
887 }
888 
890 {
891  int tag = track->par->codec_tag;
892 
893  if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
894  (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
895  track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
896  track->par->codec_id == AV_CODEC_ID_H263 ||
897  av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
898  if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
899  tag = mov_get_dv_codec_tag(s, track);
900  else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
901  tag = mov_get_rawvideo_codec_tag(s, track);
902  else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
904  if (!tag) { // if no mac fcc found, try with Microsoft tags
906  if (tag)
907  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
908  "the file may be unplayable!\n");
909  }
910  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
912  if (!tag) { // if no mac fcc found, try with Microsoft tags
913  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
914  if (ms_tag) {
915  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
916  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
917  "the file may be unplayable!\n");
918  }
919  }
920  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
922  }
923 
924  return tag;
925 }
926 
927 static const AVCodecTag codec_3gp_tags[] = {
928  { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
929  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
930  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
931  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
932  { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
933  { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
934  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
935  { AV_CODEC_ID_NONE, 0 },
936 };
937 
938 static const AVCodecTag codec_f4v_tags[] = {
939  { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
940  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
941  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
942  { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
943  { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
944  { AV_CODEC_ID_NONE, 0 },
945 };
946 
948 {
949  int tag;
950 
951  if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
952  tag = mp4_get_codec_tag(s, track);
953  else if (track->mode == MODE_ISM) {
954  tag = mp4_get_codec_tag(s, track);
955  if (!tag && track->par->codec_id == AV_CODEC_ID_WMAPRO)
956  tag = MKTAG('w', 'm', 'a', ' ');
957  } else if (track->mode == MODE_IPOD)
958  tag = ipod_get_codec_tag(s, track);
959  else if (track->mode & MODE_3GP)
960  tag = ff_codec_get_tag(codec_3gp_tags, track->par->codec_id);
961  else if (track->mode == MODE_F4V)
962  tag = ff_codec_get_tag(codec_f4v_tags, track->par->codec_id);
963  else
964  tag = mov_get_codec_tag(s, track);
965 
966  return tag;
967 }
968 
974 {
975  avio_wb32(pb, 28);
976  ffio_wfourcc(pb, "uuid");
977  avio_wb32(pb, 0x6b6840f2);
978  avio_wb32(pb, 0x5f244fc5);
979  avio_wb32(pb, 0xba39a51b);
980  avio_wb32(pb, 0xcf0323f3);
981  avio_wb32(pb, 0x0);
982  return 28;
983 }
984 
985 static const uint16_t fiel_data[] = {
986  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
987 };
988 
989 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
990 {
991  unsigned mov_field_order = 0;
992  if (track->par->field_order < FF_ARRAY_ELEMS(fiel_data))
993  mov_field_order = fiel_data[track->par->field_order];
994  else
995  return 0;
996  avio_wb32(pb, 10);
997  ffio_wfourcc(pb, "fiel");
998  avio_wb16(pb, mov_field_order);
999  return 10;
1000 }
1001 
1003 {
1004  int64_t pos = avio_tell(pb);
1005  avio_wb32(pb, 0); /* size */
1006  avio_wl32(pb, track->tag); // store it byteswapped
1007  avio_wb32(pb, 0); /* Reserved */
1008  avio_wb16(pb, 0); /* Reserved */
1009  avio_wb16(pb, 1); /* Data-reference index */
1010 
1011  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1012  mov_write_esds_tag(pb, track);
1013  else if (track->par->extradata_size)
1014  avio_write(pb, track->par->extradata, track->par->extradata_size);
1015 
1016  return update_size(pb, pos);
1017 }
1018 
1019 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1020 {
1021  AVRational sar;
1022  av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
1023  track->par->sample_aspect_ratio.den, INT_MAX);
1024 
1025  avio_wb32(pb, 16);
1026  ffio_wfourcc(pb, "pasp");
1027  avio_wb32(pb, sar.num);
1028  avio_wb32(pb, sar.den);
1029  return 16;
1030 }
1031 
1033 {
1034  AVDictionaryEntry *encoder;
1035  int64_t pos = avio_tell(pb);
1036  char compressor_name[32] = { 0 };
1037 
1038  avio_wb32(pb, 0); /* size */
1039  avio_wl32(pb, track->tag); // store it byteswapped
1040  avio_wb32(pb, 0); /* Reserved */
1041  avio_wb16(pb, 0); /* Reserved */
1042  avio_wb16(pb, 1); /* Data-reference index */
1043 
1044  avio_wb16(pb, 0); /* Codec stream version */
1045  avio_wb16(pb, 0); /* Codec stream revision (=0) */
1046  if (track->mode == MODE_MOV) {
1047  ffio_wfourcc(pb, "FFMP"); /* Vendor */
1048  if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1049  avio_wb32(pb, 0); /* Temporal Quality */
1050  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1051  } else {
1052  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1053  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1054  }
1055  } else {
1056  avio_wb32(pb, 0); /* Reserved */
1057  avio_wb32(pb, 0); /* Reserved */
1058  avio_wb32(pb, 0); /* Reserved */
1059  }
1060  avio_wb16(pb, track->par->width); /* Video width */
1061  avio_wb16(pb, track->height); /* Video height */
1062  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1063  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1064  avio_wb32(pb, 0); /* Data size (= 0) */
1065  avio_wb16(pb, 1); /* Frame count (= 1) */
1066 
1067  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1068  if (track->mode == MODE_MOV &&
1069  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0)))
1070  av_strlcpy(compressor_name, encoder->value, 32);
1071  avio_w8(pb, strlen(compressor_name));
1072  avio_write(pb, compressor_name, 31);
1073 
1074  if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
1075  avio_wb16(pb, track->par->bits_per_coded_sample);
1076  else
1077  avio_wb16(pb, 0x18); /* Reserved */
1078  avio_wb16(pb, 0xffff); /* Reserved */
1079  if (track->tag == MKTAG('m','p','4','v'))
1080  mov_write_esds_tag(pb, track);
1081  else if (track->par->codec_id == AV_CODEC_ID_H263)
1082  mov_write_d263_tag(pb);
1083  else if (track->par->codec_id == AV_CODEC_ID_SVQ3)
1084  mov_write_svq3_tag(pb);
1085  else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
1086  mov_write_avid_tag(pb, track);
1087  else if (track->par->codec_id == AV_CODEC_ID_HEVC)
1088  mov_write_hvcc_tag(pb, track);
1089  else if (track->par->codec_id == AV_CODEC_ID_H264) {
1090  mov_write_avcc_tag(pb, track);
1091  if (track->mode == MODE_IPOD)
1093  } else if (track->par->field_order != AV_FIELD_UNKNOWN)
1094  mov_write_fiel_tag(pb, track);
1095  else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1096  mov_write_dvc1_tag(pb, track);
1097  else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
1098  track->par->codec_id == AV_CODEC_ID_VP6A) {
1099  /* Don't write any potential extradata here - the cropping
1100  * is signalled via the normal width/height fields. */
1101  } else if (track->vos_len > 0)
1102  mov_write_glbl_tag(pb, track);
1103 
1104  if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num &&
1105  track->par->sample_aspect_ratio.den != track->par->sample_aspect_ratio.num) {
1106  mov_write_pasp_tag(pb, track);
1107  }
1108 
1109  return update_size(pb, pos);
1110 }
1111 
1112 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1113 {
1114  int64_t pos = avio_tell(pb);
1115  avio_wb32(pb, 0); /* size */
1116  ffio_wfourcc(pb, "rtp ");
1117  avio_wb32(pb, 0); /* Reserved */
1118  avio_wb16(pb, 0); /* Reserved */
1119  avio_wb16(pb, 1); /* Data-reference index */
1120 
1121  avio_wb16(pb, 1); /* Hint track version */
1122  avio_wb16(pb, 1); /* Highest compatible version */
1123  avio_wb32(pb, track->max_packet_size); /* Max packet size */
1124 
1125  avio_wb32(pb, 12); /* size */
1126  ffio_wfourcc(pb, "tims");
1127  avio_wb32(pb, track->timescale);
1128 
1129  return update_size(pb, pos);
1130 }
1131 
1132 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1133 {
1134  int64_t pos = avio_tell(pb);
1135 
1136  avio_wb32(pb, 0); /* size */
1137  ffio_wfourcc(pb, "tmcd"); /* Data format */
1138  avio_wb32(pb, 0); /* Reserved */
1139  avio_wb32(pb, 1); /* Data reference index */
1140  if (track->par->extradata_size)
1141  avio_write(pb, track->par->extradata, track->par->extradata_size);
1142  return update_size(pb, pos);
1143 }
1144 
1146 {
1147  int64_t pos = avio_tell(pb);
1148  avio_wb32(pb, 0); /* size */
1149  ffio_wfourcc(pb, "stsd");
1150  avio_wb32(pb, 0); /* version & flags */
1151  avio_wb32(pb, 1); /* entry count */
1152  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
1153  mov_write_video_tag(pb, track);
1154  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
1155  mov_write_audio_tag(s, pb, track);
1156  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
1157  mov_write_subtitle_tag(pb, track);
1158  else if (track->par->codec_tag == MKTAG('r','t','p',' '))
1159  mov_write_rtp_tag(pb, track);
1160  else if (track->par->codec_tag == MKTAG('t','m','c','d'))
1161  mov_write_tmcd_tag(pb, track);
1162  return update_size(pb, pos);
1163 }
1164 
1165 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1166 {
1167  MOVStts *ctts_entries;
1168  uint32_t entries = 0;
1169  uint32_t atom_size;
1170  int i;
1171 
1172  ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1173  if (!ctts_entries)
1174  return AVERROR(ENOMEM);
1175  ctts_entries[0].count = 1;
1176  ctts_entries[0].duration = track->cluster[0].cts;
1177  for (i = 1; i < track->entry; i++) {
1178  if (track->cluster[i].cts == ctts_entries[entries].duration) {
1179  ctts_entries[entries].count++; /* compress */
1180  } else {
1181  entries++;
1182  ctts_entries[entries].duration = track->cluster[i].cts;
1183  ctts_entries[entries].count = 1;
1184  }
1185  }
1186  entries++; /* last one */
1187  atom_size = 16 + (entries * 8);
1188  avio_wb32(pb, atom_size); /* size */
1189  ffio_wfourcc(pb, "ctts");
1190  avio_wb32(pb, 0); /* version & flags */
1191  avio_wb32(pb, entries); /* entry count */
1192  for (i = 0; i < entries; i++) {
1193  avio_wb32(pb, ctts_entries[i].count);
1194  avio_wb32(pb, ctts_entries[i].duration);
1195  }
1196  av_free(ctts_entries);
1197  return atom_size;
1198 }
1199 
1200 /* Time to sample atom */
1201 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1202 {
1203  MOVStts *stts_entries = NULL;
1204  uint32_t entries = -1;
1205  uint32_t atom_size;
1206  int i;
1207 
1208  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1209  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1210  if (!stts_entries)
1211  return AVERROR(ENOMEM);
1212  stts_entries[0].count = track->sample_count;
1213  stts_entries[0].duration = 1;
1214  entries = 1;
1215  } else {
1216  if (track->entry) {
1217  stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
1218  if (!stts_entries)
1219  return AVERROR(ENOMEM);
1220  }
1221  for (i = 0; i < track->entry; i++) {
1222  int duration = get_cluster_duration(track, i);
1223  if (i && duration == stts_entries[entries].duration) {
1224  stts_entries[entries].count++; /* compress */
1225  } else {
1226  entries++;
1227  stts_entries[entries].duration = duration;
1228  stts_entries[entries].count = 1;
1229  }
1230  }
1231  entries++; /* last one */
1232  }
1233  atom_size = 16 + (entries * 8);
1234  avio_wb32(pb, atom_size); /* size */
1235  ffio_wfourcc(pb, "stts");
1236  avio_wb32(pb, 0); /* version & flags */
1237  avio_wb32(pb, entries); /* entry count */
1238  for (i = 0; i < entries; i++) {
1239  avio_wb32(pb, stts_entries[i].count);
1240  avio_wb32(pb, stts_entries[i].duration);
1241  }
1242  av_free(stts_entries);
1243  return atom_size;
1244 }
1245 
1247 {
1248  avio_wb32(pb, 28); /* size */
1249  ffio_wfourcc(pb, "dref");
1250  avio_wb32(pb, 0); /* version & flags */
1251  avio_wb32(pb, 1); /* entry count */
1252 
1253  avio_wb32(pb, 0xc); /* size */
1254  ffio_wfourcc(pb, "url ");
1255  avio_wb32(pb, 1); /* version & flags */
1256 
1257  return 28;
1258 }
1259 
1261 {
1262  int64_t pos = avio_tell(pb);
1263  avio_wb32(pb, 0); /* size */
1264  ffio_wfourcc(pb, "stbl");
1265  mov_write_stsd_tag(s, pb, track);
1266  mov_write_stts_tag(pb, track);
1267  if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
1268  track->par->codec_tag == MKTAG('r','t','p',' ')) &&
1269  track->has_keyframes && track->has_keyframes < track->entry)
1270  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1271  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1273  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
1274  track->flags & MOV_TRACK_CTTS && track->entry)
1275  mov_write_ctts_tag(pb, track);
1276  mov_write_stsc_tag(pb, track);
1277  mov_write_stsz_tag(pb, track);
1278  mov_write_stco_tag(pb, track);
1279  return update_size(pb, pos);
1280 }
1281 
1283 {
1284  int64_t pos = avio_tell(pb);
1285  avio_wb32(pb, 0); /* size */
1286  ffio_wfourcc(pb, "dinf");
1287  mov_write_dref_tag(pb);
1288  return update_size(pb, pos);
1289 }
1290 
1292 {
1293  avio_wb32(pb, 12);
1294  ffio_wfourcc(pb, "nmhd");
1295  avio_wb32(pb, 0);
1296  return 12;
1297 }
1298 
1300 {
1301  avio_wb32(pb, 0x20); /* size */
1302  ffio_wfourcc(pb, "gmhd");
1303  avio_wb32(pb, 0x18); /* gmin size */
1304  ffio_wfourcc(pb, "gmin");/* generic media info */
1305  avio_wb32(pb, 0); /* version & flags */
1306  avio_wb16(pb, 0x40); /* graphics mode = */
1307  avio_wb16(pb, 0x8000); /* opColor (r?) */
1308  avio_wb16(pb, 0x8000); /* opColor (g?) */
1309  avio_wb16(pb, 0x8000); /* opColor (b?) */
1310  avio_wb16(pb, 0); /* balance */
1311  avio_wb16(pb, 0); /* reserved */
1312  return 0x20;
1313 }
1314 
1316 {
1317  avio_wb32(pb, 16); /* size */
1318  ffio_wfourcc(pb, "smhd");
1319  avio_wb32(pb, 0); /* version & flags */
1320  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1321  avio_wb16(pb, 0); /* reserved */
1322  return 16;
1323 }
1324 
1326 {
1327  avio_wb32(pb, 0x14); /* size (always 0x14) */
1328  ffio_wfourcc(pb, "vmhd");
1329  avio_wb32(pb, 0x01); /* version & flags */
1330  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1331  return 0x14;
1332 }
1333 
1334 static int is_clcp_track(MOVTrack *track)
1335 {
1336  return track->tag == MKTAG('c','7','0','8') ||
1337  track->tag == MKTAG('c','6','0','8');
1338 }
1339 
1341 {
1342  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1343  int64_t pos = avio_tell(pb);
1344 
1345  hdlr = "dhlr";
1346  hdlr_type = "url ";
1347  descr = "DataHandler";
1348 
1349  if (track) {
1350  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1351  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1352  hdlr_type = "vide";
1353  descr = "VideoHandler";
1354  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
1355  hdlr_type = "soun";
1356  descr = "SoundHandler";
1357  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1358  if (track->tag == MKTAG('t','x','3','g')) {
1359  hdlr_type = "sbtl";
1360  } else if (track->tag == MKTAG('m','p','4','s')) {
1361  hdlr_type = "subp";
1362  } else if (is_clcp_track(track)) {
1363  hdlr_type = "clcp";
1364  } else {
1365  hdlr_type = "text";
1366  }
1367  descr = "SubtitleHandler";
1368  } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
1369  hdlr_type = "hint";
1370  descr = "HintHandler";
1371  } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
1372  hdlr_type = "tmcd";
1373  descr = "TimeCodeHandler";
1374  } else {
1375  char tag_buf[32];
1376  av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1377  track->par->codec_tag);
1378 
1380  "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1381  tag_buf, track->par->codec_tag);
1382  }
1383  if (track->st) {
1384  // hdlr.name is used by some players to identify the content title
1385  // of the track. So if an alternate handler description is
1386  // specified, use it.
1387  AVDictionaryEntry *t;
1388  t = av_dict_get(track->st->metadata, "handler", NULL, 0);
1389  if (t && utf8len(t->value))
1390  descr = t->value;
1391  }
1392  }
1393 
1394  avio_wb32(pb, 0); /* size */
1395  ffio_wfourcc(pb, "hdlr");
1396  avio_wb32(pb, 0); /* Version & flags */
1397  avio_write(pb, hdlr, 4); /* handler */
1398  ffio_wfourcc(pb, hdlr_type); /* handler type */
1399  avio_wb32(pb, 0); /* reserved */
1400  avio_wb32(pb, 0); /* reserved */
1401  avio_wb32(pb, 0); /* reserved */
1402  if (!track || track->mode == MODE_MOV)
1403  avio_w8(pb, strlen(descr)); /* pascal string */
1404  avio_write(pb, descr, strlen(descr)); /* handler description */
1405  if (track && track->mode != MODE_MOV)
1406  avio_w8(pb, 0); /* c string */
1407  return update_size(pb, pos);
1408 }
1409 
1411 {
1412  /* This atom must be present, but leaving the values at zero
1413  * seems harmless. */
1414  avio_wb32(pb, 28); /* size */
1415  ffio_wfourcc(pb, "hmhd");
1416  avio_wb32(pb, 0); /* version, flags */
1417  avio_wb16(pb, 0); /* maxPDUsize */
1418  avio_wb16(pb, 0); /* avgPDUsize */
1419  avio_wb32(pb, 0); /* maxbitrate */
1420  avio_wb32(pb, 0); /* avgbitrate */
1421  avio_wb32(pb, 0); /* reserved */
1422  return 28;
1423 }
1424 
1426 {
1427  int64_t pos = avio_tell(pb);
1428  avio_wb32(pb, 0); /* size */
1429  ffio_wfourcc(pb, "minf");
1430  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
1431  mov_write_vmhd_tag(pb);
1432  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
1433  mov_write_smhd_tag(pb);
1434  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1435  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
1436  mov_write_gmhd_tag(pb);
1437  } else {
1438  mov_write_nmhd_tag(pb);
1439  }
1440  } else if (track->tag == MKTAG('r','t','p',' ')) {
1441  mov_write_hmhd_tag(pb);
1442  } else if (track->tag == MKTAG('t','m','c','d')) {
1443  mov_write_gmhd_tag(pb);
1444  }
1445  if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1446  mov_write_hdlr_tag(s, pb, NULL);
1447  mov_write_dinf_tag(pb);
1448  mov_write_stbl_tag(s, pb, track);
1449  return update_size(pb, pos);
1450 }
1451 
1453  MOVTrack *track)
1454 {
1455  int version = track->track_duration < INT32_MAX ? 0 : 1;
1456 
1457  if (track->mode == MODE_ISM)
1458  version = 1;
1459 
1460  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1461  ffio_wfourcc(pb, "mdhd");
1462  avio_w8(pb, version);
1463  avio_wb24(pb, 0); /* flags */
1464  if (version == 1) {
1465  avio_wb64(pb, track->time);
1466  avio_wb64(pb, track->time);
1467  } else {
1468  avio_wb32(pb, track->time); /* creation time */
1469  avio_wb32(pb, track->time); /* modification time */
1470  }
1471  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1472  if (!track->entry && mov->mode == MODE_ISM)
1473  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1474  else if (!track->entry)
1475  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
1476  else
1477  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1478  avio_wb16(pb, track->language); /* language */
1479  avio_wb16(pb, 0); /* reserved (quality) */
1480 
1481  if (version != 0 && track->mode == MODE_MOV) {
1483  "FATAL error, file duration too long for timebase, this file will not be\n"
1484  "playable with quicktime. Choose a different timebase or a different\n"
1485  "container format\n");
1486  }
1487 
1488  return 32;
1489 }
1490 
1492  MOVMuxContext *mov, MOVTrack *track)
1493 {
1494  int64_t pos = avio_tell(pb);
1495  avio_wb32(pb, 0); /* size */
1496  ffio_wfourcc(pb, "mdia");
1497  mov_write_mdhd_tag(pb, mov, track);
1498  mov_write_hdlr_tag(s, pb, track);
1499  mov_write_minf_tag(s, pb, track);
1500  return update_size(pb, pos);
1501 }
1502 
1504  MOVTrack *track, AVStream *st)
1505 {
1507  track->timescale, AV_ROUND_UP);
1508  int version = duration < INT32_MAX ? 0 : 1;
1510  int group = 0;
1511 
1512  uint32_t *display_matrix = NULL;
1513  int display_matrix_size, i;
1514 
1515  if (st) {
1516  if (mov->per_stream_grouping)
1517  group = st->index;
1518  else
1519  group = st->codecpar->codec_type;
1520 
1521  display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
1522  &display_matrix_size);
1523  if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))
1524  display_matrix = NULL;
1525  }
1526 
1527  if (track->flags & MOV_TRACK_ENABLED)
1528  flags |= MOV_TKHD_FLAG_ENABLED;
1529 
1530  if (track->mode == MODE_ISM)
1531  version = 1;
1532 
1533  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1534  ffio_wfourcc(pb, "tkhd");
1535  avio_w8(pb, version);
1536  avio_wb24(pb, flags);
1537  if (version == 1) {
1538  avio_wb64(pb, track->time);
1539  avio_wb64(pb, track->time);
1540  } else {
1541  avio_wb32(pb, track->time); /* creation time */
1542  avio_wb32(pb, track->time); /* modification time */
1543  }
1544  avio_wb32(pb, track->track_id); /* track-id */
1545  avio_wb32(pb, 0); /* reserved */
1546  if (!track->entry && mov->mode == MODE_ISM)
1547  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1548  else if (!track->entry)
1549  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
1550  else
1551  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1552 
1553  avio_wb32(pb, 0); /* reserved */
1554  avio_wb32(pb, 0); /* reserved */
1555  avio_wb16(pb, 0); /* layer */
1556  avio_wb16(pb, group); /* alternate group) */
1557  /* Volume, only for audio */
1558  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
1559  avio_wb16(pb, 0x0100);
1560  else
1561  avio_wb16(pb, 0);
1562  avio_wb16(pb, 0); /* reserved */
1563 
1564  /* Matrix structure */
1565  if (display_matrix) {
1566  for (i = 0; i < 9; i++)
1567  avio_wb32(pb, display_matrix[i]);
1568  } else {
1569  avio_wb32(pb, 0x00010000); /* reserved */
1570  avio_wb32(pb, 0x0); /* reserved */
1571  avio_wb32(pb, 0x0); /* reserved */
1572  avio_wb32(pb, 0x0); /* reserved */
1573  avio_wb32(pb, 0x00010000); /* reserved */
1574  avio_wb32(pb, 0x0); /* reserved */
1575  avio_wb32(pb, 0x0); /* reserved */
1576  avio_wb32(pb, 0x0); /* reserved */
1577  avio_wb32(pb, 0x40000000); /* reserved */
1578  }
1579 
1580  /* Track width and height, for visual only */
1581  if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
1582  track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1583  if (track->mode == MODE_MOV) {
1584  avio_wb32(pb, track->par->width << 16);
1585  avio_wb32(pb, track->height << 16);
1586  } else {
1587  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1588  if (!sample_aspect_ratio || track->height != track->par->height)
1589  sample_aspect_ratio = 1;
1590  avio_wb32(pb, sample_aspect_ratio * track->par->width * 0x10000);
1591  avio_wb32(pb, track->height * 0x10000);
1592  }
1593  } else {
1594  avio_wb32(pb, 0);
1595  avio_wb32(pb, 0);
1596  }
1597  return 0x5c;
1598 }
1599 
1600 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1601 {
1603  track->par->sample_aspect_ratio.den);
1604 
1605  int64_t pos = avio_tell(pb);
1606 
1607  avio_wb32(pb, 0); /* size */
1608  ffio_wfourcc(pb, "tapt");
1609 
1610  avio_wb32(pb, 20);
1611  ffio_wfourcc(pb, "clef");
1612  avio_wb32(pb, 0);
1613  avio_wb32(pb, width << 16);
1614  avio_wb32(pb, track->par->height << 16);
1615 
1616  avio_wb32(pb, 20);
1617  ffio_wfourcc(pb, "prof");
1618  avio_wb32(pb, 0);
1619  avio_wb32(pb, width << 16);
1620  avio_wb32(pb, track->par->height << 16);
1621 
1622  avio_wb32(pb, 20);
1623  ffio_wfourcc(pb, "enof");
1624  avio_wb32(pb, 0);
1625  avio_wb32(pb, track->par->width << 16);
1626  avio_wb32(pb, track->par->height << 16);
1627 
1628  return update_size(pb, pos);
1629 }
1630 
1631 // This box seems important for the psp playback ... without it the movie seems to hang
1633  MOVTrack *track)
1634 {
1636  track->timescale, AV_ROUND_UP);
1637  int version = duration < INT32_MAX ? 0 : 1;
1638  int entry_size, entry_count, size;
1639  int64_t delay, start_ct = track->start_cts;
1640  delay = av_rescale_rnd(track->start_dts + start_ct, MOV_TIMESCALE,
1641  track->timescale, AV_ROUND_DOWN);
1642  version |= delay < INT32_MAX ? 0 : 1;
1643 
1644  entry_size = (version == 1) ? 20 : 12;
1645  entry_count = 1 + (delay > 0);
1646  size = 24 + entry_count * entry_size;
1647 
1648  /* write the atom data */
1649  avio_wb32(pb, size);
1650  ffio_wfourcc(pb, "edts");
1651  avio_wb32(pb, size - 8);
1652  ffio_wfourcc(pb, "elst");
1653  avio_w8(pb, version);
1654  avio_wb24(pb, 0); /* flags */
1655 
1656  avio_wb32(pb, entry_count);
1657  if (delay > 0) { /* add an empty edit to delay presentation */
1658  /* In the positive delay case, the delay includes the cts
1659  * offset, and the second edit list entry below trims out
1660  * the same amount from the actual content. This makes sure
1661  * that the offset last sample is included in the edit
1662  * list duration as well. */
1663  if (version == 1) {
1664  avio_wb64(pb, delay);
1665  avio_wb64(pb, -1);
1666  } else {
1667  avio_wb32(pb, delay);
1668  avio_wb32(pb, -1);
1669  }
1670  avio_wb32(pb, 0x00010000);
1671  } else {
1672  /* Avoid accidentally ending up with start_ct = -1 which has got a
1673  * special meaning. Normally start_ct should end up positive or zero
1674  * here, but use FFMIN in case dts is a a small positive integer
1675  * rounded to 0 when represented in MOV_TIMESCALE units. */
1676  start_ct = -FFMIN(track->start_dts, 0);
1677  /* Note, this delay is calculated from the pts of the first sample,
1678  * ensuring that we don't reduce the duration for cases with
1679  * dts<0 pts=0. */
1680  duration += delay;
1681  }
1682 
1683  /* For fragmented files, we don't know the full length yet. Setting
1684  * duration to 0 allows us to only specify the offset, including
1685  * the rest of the content (from all future fragments) without specifying
1686  * an explicit duration. */
1687  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
1688  duration = 0;
1689 
1690  /* duration */
1691  if (version == 1) {
1692  avio_wb64(pb, duration);
1693  avio_wb64(pb, start_ct);
1694  } else {
1695  avio_wb32(pb, duration);
1696  avio_wb32(pb, start_ct);
1697  }
1698  avio_wb32(pb, 0x00010000);
1699  return size;
1700 }
1701 
1702 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1703 {
1704  avio_wb32(pb, 20); // size
1705  ffio_wfourcc(pb, "tref");
1706  avio_wb32(pb, 12); // size (subatom)
1707  avio_wl32(pb, track->tref_tag);
1708  avio_wb32(pb, track->tref_id);
1709  return 20;
1710 }
1711 
1712 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1714 {
1715  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1716  ffio_wfourcc(pb, "uuid");
1717  ffio_wfourcc(pb, "USMT");
1718  avio_wb32(pb, 0x21d24fce);
1719  avio_wb32(pb, 0xbb88695c);
1720  avio_wb32(pb, 0xfac9c740);
1721  avio_wb32(pb, 0x1c); // another size here!
1722  ffio_wfourcc(pb, "MTDT");
1723  avio_wb32(pb, 0x00010012);
1724  avio_wb32(pb, 0x0a);
1725  avio_wb32(pb, 0x55c40000);
1726  avio_wb32(pb, 0x1);
1727  avio_wb32(pb, 0x0);
1728  return 0x34;
1729 }
1730 
1731 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1732 {
1733  AVFormatContext *ctx = track->rtp_ctx;
1734  char buf[1000] = "";
1735  int len;
1736 
1737  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1738  NULL, NULL, 0, 0, ctx);
1739  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1740  len = strlen(buf);
1741 
1742  avio_wb32(pb, len + 24);
1743  ffio_wfourcc(pb, "udta");
1744  avio_wb32(pb, len + 16);
1745  ffio_wfourcc(pb, "hnti");
1746  avio_wb32(pb, len + 8);
1747  ffio_wfourcc(pb, "sdp ");
1748  avio_write(pb, buf, len);
1749  return len + 24;
1750 }
1751 
1753  const char *tag, const char *str)
1754 {
1755  int64_t pos = avio_tell(pb);
1756  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
1757  if (!t || !utf8len(t->value))
1758  return 0;
1759 
1760  avio_wb32(pb, 0); /* size */
1761  ffio_wfourcc(pb, tag); /* type */
1762  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
1763  return update_size(pb, pos);
1764 }
1765 
1767  AVStream *st)
1768 {
1769  AVIOContext *pb_buf;
1770  int ret, size;
1771  uint8_t *buf;
1772 
1773  if (!st)
1774  return 0;
1775 
1776  ret = avio_open_dyn_buf(&pb_buf);
1777  if (ret < 0)
1778  return ret;
1779 
1780  if (mov->mode & MODE_MP4)
1781  mov_write_track_metadata(pb_buf, st, "name", "title");
1782 
1783  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1784  avio_wb32(pb, size + 8);
1785  ffio_wfourcc(pb, "udta");
1786  avio_write(pb, buf, size);
1787  }
1788  av_free(buf);
1789 
1790  return 0;
1791 }
1792 
1794  MOVTrack *track, AVStream *st)
1795 {
1796  int64_t pos = avio_tell(pb);
1797  int entry_backup = track->entry;
1798  /* If we want to have an empty moov, but some samples already have been
1799  * buffered (delay_moov), pretend that no samples have been written yet. */
1800  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
1801  track->entry = 0;
1802 
1803  avio_wb32(pb, 0); /* size */
1804  ffio_wfourcc(pb, "trak");
1805  mov_write_tkhd_tag(pb, mov, track, st);
1806  if (track->start_dts != AV_NOPTS_VALUE &&
1807  (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1808  track->start_dts || is_clcp_track(track))) {
1809  if (mov->use_editlist)
1810  mov_write_edts_tag(pb, mov, track); // PSP Movies require edts box
1811  else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
1812  av_log(mov->fc, AV_LOG_WARNING,
1813  "Not writing any edit list even though one would have been required\n");
1814  }
1815  if (track->tref_tag)
1816  mov_write_tref_tag(pb, track);
1817  mov_write_mdia_tag(mov->fc, pb, mov, track);
1818  if (track->mode == MODE_PSP)
1819  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
1820  if (track->tag == MKTAG('r','t','p',' '))
1821  mov_write_udta_sdp(pb, track);
1822  if (track->mode == MODE_MOV) {
1823  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1824  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1825  if ((0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)) {
1826  mov_write_tapt_tag(pb, track);
1827  }
1828  }
1829  if (is_clcp_track(track)) {
1830  mov_write_tapt_tag(pb, track);
1831  }
1832  }
1833  mov_write_track_udta_tag(pb, mov, st);
1834  track->entry = entry_backup;
1835  return update_size(pb, pos);
1836 }
1837 
1839 {
1840  int i, has_audio = 0, has_video = 0;
1841  int64_t pos = avio_tell(pb);
1842  int audio_profile = mov->iods_audio_profile;
1843  int video_profile = mov->iods_video_profile;
1844  for (i = 0; i < mov->nb_streams; i++) {
1845  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
1846  has_audio |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_AUDIO;
1847  has_video |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_VIDEO;
1848  }
1849  }
1850  if (audio_profile < 0)
1851  audio_profile = 0xFF - has_audio;
1852  if (video_profile < 0)
1853  video_profile = 0xFF - has_video;
1854  avio_wb32(pb, 0x0); /* size */
1855  ffio_wfourcc(pb, "iods");
1856  avio_wb32(pb, 0); /* version & flags */
1857  put_descr(pb, 0x10, 7);
1858  avio_wb16(pb, 0x004f);
1859  avio_w8(pb, 0xff);
1860  avio_w8(pb, 0xff);
1861  avio_w8(pb, audio_profile);
1862  avio_w8(pb, video_profile);
1863  avio_w8(pb, 0xff);
1864  return update_size(pb, pos);
1865 }
1866 
1867 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1868 {
1869  avio_wb32(pb, 0x20); /* size */
1870  ffio_wfourcc(pb, "trex");
1871  avio_wb32(pb, 0); /* version & flags */
1872  avio_wb32(pb, track->track_id); /* track ID */
1873  avio_wb32(pb, 1); /* default sample description index */
1874  avio_wb32(pb, 0); /* default sample duration */
1875  avio_wb32(pb, 0); /* default sample size */
1876  avio_wb32(pb, 0); /* default sample flags */
1877  return 0;
1878 }
1879 
1881 {
1882  int64_t pos = avio_tell(pb);
1883  int i;
1884  avio_wb32(pb, 0x0); /* size */
1885  ffio_wfourcc(pb, "mvex");
1886  for (i = 0; i < mov->nb_streams; i++)
1887  mov_write_trex_tag(pb, &mov->tracks[i]);
1888  return update_size(pb, pos);
1889 }
1890 
1892 {
1893  int max_track_id = 1, i;
1894  int64_t max_track_len_temp, max_track_len = 0;
1895  int version;
1896 
1897  for (i = 0; i < mov->nb_streams; i++) {
1898  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
1899  max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1900  MOV_TIMESCALE,
1901  mov->tracks[i].timescale,
1902  AV_ROUND_UP);
1903  if (max_track_len < max_track_len_temp)
1904  max_track_len = max_track_len_temp;
1905  if (max_track_id < mov->tracks[i].track_id)
1906  max_track_id = mov->tracks[i].track_id;
1907  }
1908  }
1909  /* If using delay_moov, make sure the output is the same as if no
1910  * samples had been written yet. */
1911  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
1912  max_track_len = 0;
1913  max_track_id = 1;
1914  }
1915 
1916  version = max_track_len < UINT32_MAX ? 0 : 1;
1917  (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1918  ffio_wfourcc(pb, "mvhd");
1919  avio_w8(pb, version);
1920  avio_wb24(pb, 0); /* flags */
1921  if (version == 1) {
1922  avio_wb64(pb, mov->time);
1923  avio_wb64(pb, mov->time);
1924  } else {
1925  avio_wb32(pb, mov->time); /* creation time */
1926  avio_wb32(pb, mov->time); /* modification time */
1927  }
1928  avio_wb32(pb, MOV_TIMESCALE);
1929  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1930 
1931  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1932  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1933  avio_wb16(pb, 0); /* reserved */
1934  avio_wb32(pb, 0); /* reserved */
1935  avio_wb32(pb, 0); /* reserved */
1936 
1937  /* Matrix structure */
1938  avio_wb32(pb, 0x00010000); /* reserved */
1939  avio_wb32(pb, 0x0); /* reserved */
1940  avio_wb32(pb, 0x0); /* reserved */
1941  avio_wb32(pb, 0x0); /* reserved */
1942  avio_wb32(pb, 0x00010000); /* reserved */
1943  avio_wb32(pb, 0x0); /* reserved */
1944  avio_wb32(pb, 0x0); /* reserved */
1945  avio_wb32(pb, 0x0); /* reserved */
1946  avio_wb32(pb, 0x40000000); /* reserved */
1947 
1948  avio_wb32(pb, 0); /* reserved (preview time) */
1949  avio_wb32(pb, 0); /* reserved (preview duration) */
1950  avio_wb32(pb, 0); /* reserved (poster time) */
1951  avio_wb32(pb, 0); /* reserved (selection time) */
1952  avio_wb32(pb, 0); /* reserved (selection duration) */
1953  avio_wb32(pb, 0); /* reserved (current time) */
1954  avio_wb32(pb, max_track_id + 1); /* Next track id */
1955  return 0x6c;
1956 }
1957 
1959  AVFormatContext *s)
1960 {
1961  avio_wb32(pb, 33); /* size */
1962  ffio_wfourcc(pb, "hdlr");
1963  avio_wb32(pb, 0);
1964  avio_wb32(pb, 0);
1965  ffio_wfourcc(pb, "mdir");
1966  ffio_wfourcc(pb, "appl");
1967  avio_wb32(pb, 0);
1968  avio_wb32(pb, 0);
1969  avio_w8(pb, 0);
1970  return 33;
1971 }
1972 
1973 /* helper function to write a data tag with the specified string as data */
1974 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1975 {
1976  if (long_style) {
1977  int size = 16 + strlen(data);
1978  avio_wb32(pb, size); /* size */
1979  ffio_wfourcc(pb, "data");
1980  avio_wb32(pb, 1);
1981  avio_wb32(pb, 0);
1982  avio_write(pb, data, strlen(data));
1983  return size;
1984  } else {
1985  if (!lang)
1986  lang = ff_mov_iso639_to_lang("und", 1);
1987  avio_wb16(pb, strlen(data)); /* string length */
1988  avio_wb16(pb, lang);
1989  avio_write(pb, data, strlen(data));
1990  return strlen(data) + 4;
1991  }
1992 }
1993 
1994 static int mov_write_string_tag(AVIOContext *pb, const char *name,
1995  const char *value, int lang, int long_style)
1996 {
1997  int size = 0;
1998  if (value && value[0]) {
1999  int64_t pos = avio_tell(pb);
2000  avio_wb32(pb, 0); /* size */
2001  ffio_wfourcc(pb, name);
2002  mov_write_string_data_tag(pb, value, lang, long_style);
2003  size = update_size(pb, pos);
2004  }
2005  return size;
2006 }
2007 
2009  const char *tag, int *lang)
2010 {
2011  int l, len, len2;
2012  AVDictionaryEntry *t, *t2 = NULL;
2013  char tag2[16];
2014 
2015  *lang = 0;
2016 
2017  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2018  return NULL;
2019 
2020  len = strlen(t->key);
2021  snprintf(tag2, sizeof(tag2), "%s-", tag);
2022  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
2023  len2 = strlen(t2->key);
2024  if (len2 == len + 4 && !strcmp(t->value, t2->value)
2025  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
2026  *lang = l;
2027  return t;
2028  }
2029  }
2030  return t;
2031 }
2032 
2034  const char *name, const char *tag,
2035  int long_style)
2036 {
2037  int lang;
2038  AVDictionaryEntry *t = get_metadata_lang(s, tag, &lang);
2039  if (!t)
2040  return 0;
2041  return mov_write_string_tag(pb, name, t->value, lang, long_style);
2042 }
2043 
2044 /* 3GPP TS 26.244 */
2046 {
2047  int lang;
2048  int64_t pos = avio_tell(pb);
2049  double latitude, longitude, altitude;
2050  int32_t latitude_fix, longitude_fix, altitude_fix;
2051  AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
2052  const char *ptr, *place = "";
2053  char *end;
2054  const char *astronomical_body = "earth";
2055  if (!t)
2056  return 0;
2057 
2058  ptr = t->value;
2059  longitude = strtod(ptr, &end);
2060  if (end == ptr) {
2061  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
2062  return 0;
2063  }
2064  ptr = end;
2065  latitude = strtod(ptr, &end);
2066  if (end == ptr) {
2067  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
2068  return 0;
2069  }
2070  ptr = end;
2071  altitude = strtod(ptr, &end);
2072  /* If no altitude was present, the default 0 should be fine */
2073  if (*end == '/')
2074  place = end + 1;
2075 
2076  latitude_fix = (int32_t) ((1 << 16) * latitude);
2077  longitude_fix = (int32_t) ((1 << 16) * longitude);
2078  altitude_fix = (int32_t) ((1 << 16) * altitude);
2079 
2080  avio_wb32(pb, 0); /* size */
2081  ffio_wfourcc(pb, "loci"); /* type */
2082  avio_wb32(pb, 0); /* version + flags */
2083  avio_wb16(pb, lang);
2084  avio_write(pb, place, strlen(place) + 1);
2085  avio_w8(pb, 0); /* role of place (0 == shooting location, 1 == real location, 2 == fictional location) */
2086  avio_wb32(pb, latitude_fix);
2087  avio_wb32(pb, longitude_fix);
2088  avio_wb32(pb, altitude_fix);
2089  avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
2090  avio_w8(pb, 0); /* additional notes, null terminated string */
2091 
2092  return update_size(pb, pos);
2093 }
2094 
2095 /* iTunes track number */
2097  AVFormatContext *s)
2098 {
2099  AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
2100  int size = 0, track = t ? atoi(t->value) : 0;
2101  if (track) {
2102  avio_wb32(pb, 32); /* size */
2103  ffio_wfourcc(pb, "trkn");
2104  avio_wb32(pb, 24); /* size */
2105  ffio_wfourcc(pb, "data");
2106  avio_wb32(pb, 0); // 8 bytes empty
2107  avio_wb32(pb, 0);
2108  avio_wb16(pb, 0); // empty
2109  avio_wb16(pb, track); // track number
2110  avio_wb16(pb, 0); // total track number
2111  avio_wb16(pb, 0); // empty
2112  size = 32;
2113  }
2114  return size;
2115 }
2116 
2117 /* iTunes meta data list */
2119  AVFormatContext *s)
2120 {
2121  int64_t pos = avio_tell(pb);
2122  avio_wb32(pb, 0); /* size */
2123  ffio_wfourcc(pb, "ilst");
2124  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
2125  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
2126  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
2127  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
2128  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
2129  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
2130  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
2131  if (!(s->flags & AVFMT_FLAG_BITEXACT))
2132  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
2133  }
2134  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
2135  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
2136  mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2137  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
2138  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
2139  mov_write_string_metadata(s, pb, "desc", "description",1);
2140  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
2141  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
2142  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
2143  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
2144  mov_write_trkn_tag(pb, mov, s);
2145  return update_size(pb, pos);
2146 }
2147 
2148 /* iTunes meta data tag */
2150  AVFormatContext *s)
2151 {
2152  int size = 0;
2153  int64_t pos = avio_tell(pb);
2154  avio_wb32(pb, 0); /* size */
2155  ffio_wfourcc(pb, "meta");
2156  avio_wb32(pb, 0);
2157  mov_write_itunes_hdlr_tag(pb, mov, s);
2158  mov_write_ilst_tag(pb, mov, s);
2159  size = update_size(pb, pos);
2160  return size;
2161 }
2162 
2163 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2164 {
2165  int val;
2166  while (*b) {
2167  GET_UTF8(val, *b++, return -1;)
2168  avio_wb16(pb, val);
2169  }
2170  avio_wb16(pb, 0x00);
2171  return 0;
2172 }
2173 
2174 static uint16_t language_code(const char *str)
2175 {
2176  return (((str[0] - 0x60) & 0x1F) << 10) +
2177  (((str[1] - 0x60) & 0x1F) << 5) +
2178  (( str[2] - 0x60) & 0x1F);
2179 }
2180 
2182  const char *tag, const char *str)
2183 {
2184  int64_t pos = avio_tell(pb);
2185  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
2186  if (!t || !utf8len(t->value))
2187  return 0;
2188  avio_wb32(pb, 0); /* size */
2189  ffio_wfourcc(pb, tag); /* type */
2190  avio_wb32(pb, 0); /* version + flags */
2191  if (!strcmp(tag, "yrrc"))
2192  avio_wb16(pb, atoi(t->value));
2193  else {
2194  avio_wb16(pb, language_code("eng")); /* language */
2195  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
2196  if (!strcmp(tag, "albm") &&
2197  (t = av_dict_get(s->metadata, "track", NULL, 0)))
2198  avio_w8(pb, atoi(t->value));
2199  }
2200  return update_size(pb, pos);
2201 }
2202 
2204 {
2205  int64_t pos = avio_tell(pb);
2206  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
2207 
2208  avio_wb32(pb, 0); // size
2209  ffio_wfourcc(pb, "chpl");
2210  avio_wb32(pb, 0x01000000); // version + flags
2211  avio_wb32(pb, 0); // unknown
2212  avio_w8(pb, nb_chapters);
2213 
2214  for (i = 0; i < nb_chapters; i++) {
2215  AVChapter *c = s->chapters[i];
2216  AVDictionaryEntry *t;
2217  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
2218 
2219  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2220  int len = FFMIN(strlen(t->value), 255);
2221  avio_w8(pb, len);
2222  avio_write(pb, t->value, len);
2223  } else
2224  avio_w8(pb, 0);
2225  }
2226  return update_size(pb, pos);
2227 }
2228 
2230  AVFormatContext *s)
2231 {
2232  AVIOContext *pb_buf;
2233  int ret, size;
2234  uint8_t *buf;
2235 
2236  ret = avio_open_dyn_buf(&pb_buf);
2237  if (ret < 0)
2238  return ret;
2239 
2240  if (mov->mode & MODE_3GP) {
2241  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2242  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2243  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2244  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2245  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2246  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2247  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2248  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2249  mov_write_loci_tag(s, pb_buf);
2250  } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2251  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
2252  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
2253  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
2254  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
2255  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
2256  if (!(s->flags & AVFMT_FLAG_BITEXACT))
2257  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
2258  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
2259  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
2260  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
2261  mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
2262  mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
2263  mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
2264  } else {
2265  /* iTunes meta data */
2266  mov_write_meta_tag(pb_buf, mov, s);
2267  mov_write_loci_tag(s, pb_buf);
2268  }
2269 
2270  if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
2271  mov_write_chpl_tag(pb_buf, s);
2272 
2273  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2274  avio_wb32(pb, size + 8);
2275  ffio_wfourcc(pb, "udta");
2276  avio_write(pb, buf, size);
2277  }
2278  av_free(buf);
2279 
2280  return 0;
2281 }
2282 
2284  const char *str, const char *lang, int type)
2285 {
2286  int len = utf8len(str) + 1;
2287  if (len <= 0)
2288  return;
2289  avio_wb16(pb, len * 2 + 10); /* size */
2290  avio_wb32(pb, type); /* type */
2291  avio_wb16(pb, language_code(lang)); /* language */
2292  avio_wb16(pb, 0x01); /* ? */
2293  ascii_to_wc(pb, str);
2294 }
2295 
2297 {
2298  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2299  int64_t pos, pos2;
2300 
2301  if (title) {
2302  pos = avio_tell(pb);
2303  avio_wb32(pb, 0); /* size placeholder*/
2304  ffio_wfourcc(pb, "uuid");
2305  ffio_wfourcc(pb, "USMT");
2306  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2307  avio_wb32(pb, 0xbb88695c);
2308  avio_wb32(pb, 0xfac9c740);
2309 
2310  pos2 = avio_tell(pb);
2311  avio_wb32(pb, 0); /* size placeholder*/
2312  ffio_wfourcc(pb, "MTDT");
2313  avio_wb16(pb, 4);
2314 
2315  // ?
2316  avio_wb16(pb, 0x0C); /* size */
2317  avio_wb32(pb, 0x0B); /* type */
2318  avio_wb16(pb, language_code("und")); /* language */
2319  avio_wb16(pb, 0x0); /* ? */
2320  avio_wb16(pb, 0x021C); /* data */
2321 
2322  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2323  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2324  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2325 
2326  update_size(pb, pos2);
2327  return update_size(pb, pos);
2328  }
2329 
2330  return 0;
2331 }
2332 
2334  AVFormatContext *s)
2335 {
2336  int i;
2337  int64_t pos = avio_tell(pb);
2338  avio_wb32(pb, 0); /* size placeholder*/
2339  ffio_wfourcc(pb, "moov");
2340 
2341  for (i = 0; i < mov->nb_streams; i++) {
2342  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2343  continue;
2344 
2345  mov->tracks[i].time = mov->time;
2346  mov->tracks[i].track_id = i + 1;
2347  }
2348 
2349  if (mov->chapter_track)
2350  for (i = 0; i < s->nb_streams; i++) {
2351  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2352  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2353  }
2354  for (i = 0; i < mov->nb_streams; i++) {
2355  MOVTrack *track = &mov->tracks[i];
2356  if (track->tag == MKTAG('r','t','p',' ')) {
2357  track->tref_tag = MKTAG('h','i','n','t');
2358  track->tref_id = mov->tracks[track->src_track].track_id;
2359  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2360  int * fallback, size;
2361  fallback = (int*)av_stream_get_side_data(track->st,
2363  &size);
2364  if (fallback != NULL && size == sizeof(int)) {
2365  if (*fallback >= 0 && *fallback < mov->nb_streams) {
2366  track->tref_tag = MKTAG('f','a','l','l');
2367  track->tref_id = mov->tracks[*fallback].track_id;
2368  }
2369  }
2370  }
2371  }
2372 
2373  mov_write_mvhd_tag(pb, mov);
2374  if (mov->mode != MODE_MOV && !mov->iods_skip)
2375  mov_write_iods_tag(pb, mov);
2376  for (i = 0; i < mov->nb_streams; i++) {
2377  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2378  mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2379  }
2380  }
2381  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2382  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2383 
2384  if (mov->mode == MODE_PSP)
2385  mov_write_uuidusmt_tag(pb, s);
2386  else
2387  mov_write_udta_tag(pb, mov, s);
2388 
2389  return update_size(pb, pos);
2390 }
2391 
2392 static void param_write_int(AVIOContext *pb, const char *name, int value)
2393 {
2394  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2395 }
2396 
2397 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2398 {
2399  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2400 }
2401 
2402 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2403 {
2404  char buf[150];
2405  len = FFMIN(sizeof(buf) / 2 - 1, len);
2406  ff_data_to_hex(buf, value, len, 0);
2407  buf[2 * len] = '\0';
2408  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2409 }
2410 
2412 {
2413  int64_t pos = avio_tell(pb);
2414  int i;
2415  static const uint8_t uuid[] = {
2416  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2417  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2418  };
2419 
2420  avio_wb32(pb, 0);
2421  ffio_wfourcc(pb, "uuid");
2422  avio_write(pb, uuid, sizeof(uuid));
2423  avio_wb32(pb, 0);
2424 
2425  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2426  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2427  avio_printf(pb, "<head>\n");
2428  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2430  avio_printf(pb, "</head>\n");
2431  avio_printf(pb, "<body>\n");
2432  avio_printf(pb, "<switch>\n");
2433  for (i = 0; i < mov->nb_streams; i++) {
2434  MOVTrack *track = &mov->tracks[i];
2435  const char *type;
2436  /* track->track_id is initialized in write_moov, and thus isn't known
2437  * here yet */
2438  int track_id = i + 1;
2439 
2440  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2441  type = "video";
2442  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2443  type = "audio";
2444  } else {
2445  continue;
2446  }
2447  avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2448  track->par->bit_rate);
2449  param_write_int(pb, "systemBitrate", track->par->bit_rate);
2450  param_write_int(pb, "trackID", track_id);
2451  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2452  if (track->par->codec_id == AV_CODEC_ID_H264) {
2453  uint8_t *ptr;
2454  int size = track->par->extradata_size;
2455  if (!ff_avc_write_annexb_extradata(track->par->extradata, &ptr,
2456  &size)) {
2457  param_write_hex(pb, "CodecPrivateData",
2458  ptr ? ptr : track->par->extradata,
2459  size);
2460  av_free(ptr);
2461  }
2462  param_write_string(pb, "FourCC", "H264");
2463  } else if (track->par->codec_id == AV_CODEC_ID_VC1) {
2464  param_write_string(pb, "FourCC", "WVC1");
2465  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
2466  track->par->extradata_size);
2467  }
2468  param_write_int(pb, "MaxWidth", track->par->width);
2469  param_write_int(pb, "MaxHeight", track->par->height);
2470  param_write_int(pb, "DisplayWidth", track->par->width);
2471  param_write_int(pb, "DisplayHeight", track->par->height);
2472  } else {
2473  if (track->par->codec_id == AV_CODEC_ID_AAC) {
2474  param_write_string(pb, "FourCC", "AACL");
2475  } else if (track->par->codec_id == AV_CODEC_ID_WMAPRO) {
2476  param_write_string(pb, "FourCC", "WMAP");
2477  }
2478  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
2479  track->par->extradata_size);
2481  track->par->codec_id));
2482  param_write_int(pb, "Channels", track->par->channels);
2483  param_write_int(pb, "SamplingRate", track->par->sample_rate);
2484  param_write_int(pb, "BitsPerSample", 16);
2485  param_write_int(pb, "PacketSize", track->par->block_align ?
2486  track->par->block_align : 4);
2487  }
2488  avio_printf(pb, "</%s>\n", type);
2489  }
2490  avio_printf(pb, "</switch>\n");
2491  avio_printf(pb, "</body>\n");
2492  avio_printf(pb, "</smil>\n");
2493 
2494  return update_size(pb, pos);
2495 }
2496 
2498 {
2499  avio_wb32(pb, 16);
2500  ffio_wfourcc(pb, "mfhd");
2501  avio_wb32(pb, 0);
2502  avio_wb32(pb, mov->fragments);
2503  return 0;
2504 }
2505 
2506 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2507 {
2510 }
2511 
2513  MOVTrack *track, int64_t moof_offset)
2514 {
2515  int64_t pos = avio_tell(pb);
2518  if (!track->entry) {
2519  flags |= MOV_TFHD_DURATION_IS_EMPTY;
2520  } else {
2521  flags |= MOV_TFHD_DEFAULT_FLAGS;
2522  }
2524  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
2525  if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
2526  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
2528  }
2529 
2530  /* Don't set a default sample size, the silverlight player refuses
2531  * to play files with that set. Don't set a default sample duration,
2532  * WMP freaks out if it is set. Don't set a base data offset, PIFF
2533  * file format says it MUST NOT be set. */
2534  if (track->mode == MODE_ISM)
2537 
2538  avio_wb32(pb, 0); /* size placeholder */
2539  ffio_wfourcc(pb, "tfhd");
2540  avio_w8(pb, 0); /* version */
2541  avio_wb24(pb, flags);
2542 
2543  avio_wb32(pb, track->track_id); /* track-id */
2544  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2545  avio_wb64(pb, moof_offset);
2546  if (flags & MOV_TFHD_DEFAULT_DURATION) {
2547  track->default_duration = get_cluster_duration(track, 0);
2548  avio_wb32(pb, track->default_duration);
2549  }
2550  if (flags & MOV_TFHD_DEFAULT_SIZE) {
2551  track->default_size = track->entry ? track->cluster[0].size : 1;
2552  avio_wb32(pb, track->default_size);
2553  } else
2554  track->default_size = -1;
2555 
2556  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2557  /* Set the default flags based on the second sample, if available.
2558  * If the first sample is different, that can be signaled via a separate field. */
2559  if (track->entry > 1)
2560  track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
2561  else
2562  track->default_sample_flags =
2563  track->par->codec_type == AVMEDIA_TYPE_VIDEO ?
2566  avio_wb32(pb, track->default_sample_flags);
2567  }
2568 
2569  return update_size(pb, pos);
2570 }
2571 
2573  MOVTrack *track, int moof_size,
2574  int first, int end)
2575 {
2576  int64_t pos = avio_tell(pb);
2577  uint32_t flags = MOV_TRUN_DATA_OFFSET;
2578  int i;
2579 
2580  for (i = first; i < end; i++) {
2581  if (get_cluster_duration(track, i) != track->default_duration)
2582  flags |= MOV_TRUN_SAMPLE_DURATION;
2583  if (track->cluster[i].size != track->default_size)
2584  flags |= MOV_TRUN_SAMPLE_SIZE;
2585  if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2586  flags |= MOV_TRUN_SAMPLE_FLAGS;
2587  }
2588  if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
2589  get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
2590  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2591  if (track->flags & MOV_TRACK_CTTS)
2592  flags |= MOV_TRUN_SAMPLE_CTS;
2593 
2594  avio_wb32(pb, 0); /* size placeholder */
2595  ffio_wfourcc(pb, "trun");
2596  avio_w8(pb, 0); /* version */
2597  avio_wb24(pb, flags);
2598 
2599  avio_wb32(pb, end - first); /* sample count */
2600  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
2602  !mov->first_trun)
2603  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
2604  else
2605  avio_wb32(pb, moof_size + 8 + track->data_offset +
2606  track->cluster[first].pos); /* data offset */
2607  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2608  avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
2609 
2610  for (i = first; i < end; i++) {
2611  if (flags & MOV_TRUN_SAMPLE_DURATION)
2612  avio_wb32(pb, get_cluster_duration(track, i));
2613  if (flags & MOV_TRUN_SAMPLE_SIZE)
2614  avio_wb32(pb, track->cluster[i].size);
2615  if (flags & MOV_TRUN_SAMPLE_FLAGS)
2616  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2617  if (flags & MOV_TRUN_SAMPLE_CTS)
2618  avio_wb32(pb, track->cluster[i].cts);
2619  }
2620 
2621  mov->first_trun = 0;
2622  return update_size(pb, pos);
2623 }
2624 
2625 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2626 {
2627  int64_t pos = avio_tell(pb);
2628  static const uint8_t uuid[] = {
2629  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2630  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2631  };
2632 
2633  avio_wb32(pb, 0); /* size placeholder */
2634  ffio_wfourcc(pb, "uuid");
2635  avio_write(pb, uuid, sizeof(uuid));
2636  avio_w8(pb, 1);
2637  avio_wb24(pb, 0);
2638  avio_wb64(pb, track->start_dts + track->frag_start +
2639  track->cluster[0].cts);
2640  avio_wb64(pb, track->end_pts -
2641  (track->cluster[0].dts + track->cluster[0].cts));
2642 
2643  return update_size(pb, pos);
2644 }
2645 
2647  MOVTrack *track, int entry)
2648 {
2649  int n = track->nb_frag_info - 1 - entry, i;
2650  int size = 8 + 16 + 4 + 1 + 16*n;
2651  static const uint8_t uuid[] = {
2652  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2653  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2654  };
2655 
2656  if (entry < 0)
2657  return 0;
2658 
2659  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2660  avio_wb32(pb, size);
2661  ffio_wfourcc(pb, "uuid");
2662  avio_write(pb, uuid, sizeof(uuid));
2663  avio_w8(pb, 1);
2664  avio_wb24(pb, 0);
2665  avio_w8(pb, n);
2666  for (i = 0; i < n; i++) {
2667  int index = entry + 1 + i;
2668  avio_wb64(pb, track->frag_info[index].time);
2669  avio_wb64(pb, track->frag_info[index].duration);
2670  }
2671  if (n < mov->ism_lookahead) {
2672  int free_size = 16 * (mov->ism_lookahead - n);
2673  avio_wb32(pb, free_size);
2674  ffio_wfourcc(pb, "free");
2675  for (i = 0; i < free_size - 8; i++)
2676  avio_w8(pb, 0);
2677  }
2678 
2679  return 0;
2680 }
2681 
2683  MOVTrack *track)
2684 {
2685  int64_t pos = avio_tell(pb);
2686  int i;
2687  for (i = 0; i < mov->ism_lookahead; i++) {
2688  /* Update the tfrf tag for the last ism_lookahead fragments,
2689  * nb_frag_info - 1 is the next fragment to be written. */
2690  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2691  }
2692  avio_seek(pb, pos, SEEK_SET);
2693  return 0;
2694 }
2695 
2696 static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
2697  int size)
2698 {
2699  int i;
2700  for (i = 0; i < mov->nb_streams; i++) {
2701  MOVTrack *track = &mov->tracks[i];
2702  MOVFragmentInfo *info;
2703  if ((tracks >= 0 && i != tracks) || !track->entry)
2704  continue;
2705  track->nb_frag_info++;
2706  if (track->nb_frag_info >= track->frag_info_capacity) {
2707  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
2708  if (av_reallocp_array(&track->frag_info,
2709  new_capacity,
2710  sizeof(*track->frag_info)))
2711  return AVERROR(ENOMEM);
2712  track->frag_info_capacity = new_capacity;
2713  }
2714  info = &track->frag_info[track->nb_frag_info - 1];
2715  info->offset = avio_tell(pb);
2716  info->size = size;
2717  // Try to recreate the original pts for the first packet
2718  // from the fields we have stored
2719  info->time = track->start_dts + track->frag_start +
2720  track->cluster[0].cts;
2721  info->duration = track->end_pts -
2722  (track->cluster[0].dts + track->cluster[0].cts);
2723  // If the pts is less than zero, we will have trimmed
2724  // away parts of the media track using an edit list,
2725  // and the corresponding start presentation time is zero.
2726  if (info->time < 0) {
2727  info->duration += info->time;
2728  info->time = 0;
2729  }
2730  info->tfrf_offset = 0;
2731  mov_write_tfrf_tags(pb, mov, track);
2732  }
2733  return 0;
2734 }
2735 
2736 static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
2737 {
2738  int i;
2739  for (i = 0; i < mov->nb_streams; i++) {
2740  MOVTrack *track = &mov->tracks[i];
2741  if ((tracks >= 0 && i != tracks) || !track->entry)
2742  continue;
2743  if (track->nb_frag_info > max) {
2744  memmove(track->frag_info, track->frag_info + (track->nb_frag_info - max), max * sizeof(*track->frag_info));
2745  track->nb_frag_info = max;
2746  }
2747  }
2748 }
2749 
2750 static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
2751 {
2752  int64_t pos = avio_tell(pb);
2753 
2754  avio_wb32(pb, 0); /* size */
2755  ffio_wfourcc(pb, "tfdt");
2756  avio_w8(pb, 1); /* version */
2757  avio_wb24(pb, 0);
2758  avio_wb64(pb, track->frag_start);
2759  return update_size(pb, pos);
2760 }
2761 
2763  MOVTrack *track, int64_t moof_offset,
2764  int moof_size)
2765 {
2766  int64_t pos = avio_tell(pb);
2767  int i, start = 0;
2768  avio_wb32(pb, 0); /* size placeholder */
2769  ffio_wfourcc(pb, "traf");
2770 
2771  mov_write_tfhd_tag(pb, mov, track, moof_offset);
2772  if (mov->mode != MODE_ISM)
2773  mov_write_tfdt_tag(pb, track);
2774  for (i = 1; i < track->entry; i++) {
2775  if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
2776  mov_write_trun_tag(pb, mov, track, moof_size, start, i);
2777  start = i;
2778  }
2779  }
2780  mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
2781  if (mov->mode == MODE_ISM) {
2782  mov_write_tfxd_tag(pb, track);
2783 
2784  if (mov->ism_lookahead) {
2785  int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
2786 
2787  if (track->nb_frag_info > 0) {
2788  MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
2789  if (!info->tfrf_offset)
2790  info->tfrf_offset = avio_tell(pb);
2791  }
2792  avio_wb32(pb, 8 + size);
2793  ffio_wfourcc(pb, "free");
2794  for (i = 0; i < size; i++)
2795  avio_w8(pb, 0);
2796  }
2797  }
2798 
2799  return update_size(pb, pos);
2800 }
2801 
2803  int tracks, int moof_size)
2804 {
2805  int64_t pos = avio_tell(pb);
2806  int i;
2807 
2808  avio_wb32(pb, 0); /* size placeholder */
2809  ffio_wfourcc(pb, "moof");
2810  mov->first_trun = 1;
2811 
2812  mov_write_mfhd_tag(pb, mov);
2813  for (i = 0; i < mov->nb_streams; i++) {
2814  MOVTrack *track = &mov->tracks[i];
2815  if (tracks >= 0 && i != tracks)
2816  continue;
2817  if (!track->entry)
2818  continue;
2819  mov_write_traf_tag(pb, mov, track, pos, moof_size);
2820  }
2821 
2822  return update_size(pb, pos);
2823 }
2824 
2826  MOVTrack *track, int ref_size, int total_sidx_size)
2827 {
2828  int64_t pos = avio_tell(pb), offset_pos, end_pos;
2829  int64_t presentation_time, duration, offset;
2830  int starts_with_SAP, i, entries;
2831 
2832  if (track->entry) {
2833  entries = 1;
2834  presentation_time = track->start_dts + track->frag_start +
2835  track->cluster[0].cts;
2836  duration = track->end_pts -
2837  (track->cluster[0].dts + track->cluster[0].cts);
2838  starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
2839 
2840  // pts<0 should be cut away using edts
2841  if (presentation_time < 0) {
2842  duration += presentation_time;
2843  presentation_time = 0;
2844  }
2845  } else {
2846  entries = track->nb_frag_info;
2847  if (entries <= 0)
2848  return 0;
2849  presentation_time = track->frag_info[0].time;
2850  }
2851 
2852  avio_wb32(pb, 0); /* size */
2853  ffio_wfourcc(pb, "sidx");
2854  avio_w8(pb, 1); /* version */
2855  avio_wb24(pb, 0);
2856  avio_wb32(pb, track->track_id); /* reference_ID */
2857  avio_wb32(pb, track->timescale); /* timescale */
2858  avio_wb64(pb, presentation_time); /* earliest_presentation_time */
2859  offset_pos = avio_tell(pb);
2860  avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
2861  avio_wb16(pb, 0); /* reserved */
2862 
2863  avio_wb16(pb, entries); /* reference_count */
2864  for (i = 0; i < entries; i++) {
2865  if (!track->entry) {
2866  if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
2867  av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
2868  }
2869  duration = track->frag_info[i].duration;
2870  ref_size = track->frag_info[i].size;
2871  starts_with_SAP = 1;
2872  }
2873  avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
2874  avio_wb32(pb, duration); /* subsegment_duration */
2875  avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
2876  }
2877 
2878  end_pos = avio_tell(pb);
2879  offset = pos + total_sidx_size - end_pos;
2880  avio_seek(pb, offset_pos, SEEK_SET);
2881  avio_wb64(pb, offset);
2882  avio_seek(pb, end_pos, SEEK_SET);
2883  return update_size(pb, pos);
2884 }
2885 
2887  int tracks, int ref_size)
2888 {
2889  int i, round, ret;
2890  AVIOContext *avio_buf;
2891  int total_size = 0;
2892  for (round = 0; round < 2; round++) {
2893  // First run one round to calculate the total size of all
2894  // sidx atoms.
2895  // This would be much simpler if we'd only write one sidx
2896  // atom, for the first track in the moof.
2897  if (round == 0) {
2898  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
2899  return ret;
2900  } else {
2901  avio_buf = pb;
2902  }
2903  for (i = 0; i < mov->nb_streams; i++) {
2904  MOVTrack *track = &mov->tracks[i];
2905  if (tracks >= 0 && i != tracks)
2906  continue;
2907  // When writing a sidx for the full file, entry is 0, but
2908  // we want to include all tracks. ref_size is 0 in this case,
2909  // since we read it from frag_info instead.
2910  if (!track->entry && ref_size > 0)
2911  continue;
2912  total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
2913  total_size);
2914  }
2915  if (round == 0)
2916  total_size = ffio_close_null_buf(avio_buf);
2917  }
2918  return 0;
2919 }
2920 
2921 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
2922  int64_t mdat_size)
2923 {
2924  AVIOContext *avio_buf;
2925  int ret, moof_size;
2926 
2927  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
2928  return ret;
2929  mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
2930  moof_size = ffio_close_null_buf(avio_buf);
2931 
2932  if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX))
2933  mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
2934 
2935  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX ||
2936  !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) ||
2937  mov->ism_lookahead) {
2938  if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
2939  return ret;
2940  if (!(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) &&
2942  mov_prune_frag_info(mov, tracks, mov->ism_lookahead + 1);
2943  }
2944  }
2945 
2946  return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
2947 }
2948 
2949 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2950 {
2951  int64_t pos = avio_tell(pb);
2952  int i;
2953 
2954  avio_wb32(pb, 0); /* size placeholder */
2955  ffio_wfourcc(pb, "tfra");
2956  avio_w8(pb, 1); /* version */
2957  avio_wb24(pb, 0);
2958 
2959  avio_wb32(pb, track->track_id);
2960  avio_wb32(pb, 0); /* length of traf/trun/sample num */
2961  avio_wb32(pb, track->nb_frag_info);
2962  for (i = 0; i < track->nb_frag_info; i++) {
2963  avio_wb64(pb, track->frag_info[i].time);
2964  avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
2965  avio_w8(pb, 1); /* traf number */
2966  avio_w8(pb, 1); /* trun number */
2967  avio_w8(pb, 1); /* sample number */
2968  }
2969 
2970  return update_size(pb, pos);
2971 }
2972 
2974 {
2975  int64_t pos = avio_tell(pb);
2976  int i;
2977 
2978  avio_wb32(pb, 0); /* size placeholder */
2979  ffio_wfourcc(pb, "mfra");
2980  /* An empty mfra atom is enough to indicate to the publishing point that
2981  * the stream has ended. */
2982  if (mov->flags & FF_MOV_FLAG_ISML)
2983  return update_size(pb, pos);
2984 
2985  for (i = 0; i < mov->nb_streams; i++) {
2986  MOVTrack *track = &mov->tracks[i];
2987  if (track->nb_frag_info)
2988  mov_write_tfra_tag(pb, track);
2989  }
2990 
2991  avio_wb32(pb, 16);
2992  ffio_wfourcc(pb, "mfro");
2993  avio_wb32(pb, 0); /* version + flags */
2994  avio_wb32(pb, avio_tell(pb) + 4 - pos);
2995 
2996  return update_size(pb, pos);
2997 }
2998 
3000 {
3001  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
3002  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
3003 
3004  mov->mdat_pos = avio_tell(pb);
3005  avio_wb32(pb, 0); /* size placeholder*/
3006  ffio_wfourcc(pb, "mdat");
3007  return 0;
3008 }
3009 
3010 /* TODO: This needs to be more general */
3012 {
3013  MOVMuxContext *mov = s->priv_data;
3014  int64_t pos = avio_tell(pb);
3015  int has_h264 = 0, has_video = 0;
3016  int minor = 0x200;
3017  int i;
3018 
3019  for (i = 0; i < s->nb_streams; i++) {
3020  AVStream *st = s->streams[i];
3022  has_video = 1;
3023  if (st->codecpar->codec_id == AV_CODEC_ID_H264)
3024  has_h264 = 1;
3025  }
3026 
3027  avio_wb32(pb, 0); /* size */
3028  ffio_wfourcc(pb, "ftyp");
3029 
3030  if (mov->major_brand && strlen(mov->major_brand) >= 4)
3031  ffio_wfourcc(pb, mov->major_brand);
3032  else if (mov->mode == MODE_3GP) {
3033  ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
3034  minor = has_h264 ? 0x100 : 0x200;
3035  } else if (mov->mode & MODE_3G2) {
3036  ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
3037  minor = has_h264 ? 0x20000 : 0x10000;
3038  } else if (mov->mode == MODE_PSP)
3039  ffio_wfourcc(pb, "MSNV");
3040  else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
3041  ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
3042  else if (mov->mode == MODE_MP4)
3043  ffio_wfourcc(pb, "isom");
3044  else if (mov->mode == MODE_IPOD)
3045  ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
3046  else if (mov->mode == MODE_ISM)
3047  ffio_wfourcc(pb, "isml");
3048  else if (mov->mode == MODE_F4V)
3049  ffio_wfourcc(pb, "f4v ");
3050  else
3051  ffio_wfourcc(pb, "qt ");
3052 
3053  avio_wb32(pb, minor);
3054 
3055  if (mov->mode == MODE_MOV)
3056  ffio_wfourcc(pb, "qt ");
3057  else if (mov->mode == MODE_ISM) {
3058  ffio_wfourcc(pb, "piff");
3059  } else if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
3060  ffio_wfourcc(pb, "isom");
3061  ffio_wfourcc(pb, "iso2");
3062  if (has_h264)
3063  ffio_wfourcc(pb, "avc1");
3064  }
3065 
3066  // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
3067  // brand. This is compatible with users that don't understand tfdt.
3068  if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->mode != MODE_ISM)
3069  ffio_wfourcc(pb, "iso6");
3070 
3071  if (mov->mode == MODE_3GP)
3072  ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
3073  else if (mov->mode & MODE_3G2)
3074  ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
3075  else if (mov->mode == MODE_PSP)
3076  ffio_wfourcc(pb, "MSNV");
3077  else if (mov->mode == MODE_MP4)
3078  ffio_wfourcc(pb, "mp41");
3079 
3080  if (mov->flags & FF_MOV_FLAG_DASH && mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
3081  ffio_wfourcc(pb, "dash");
3082 
3083  return update_size(pb, pos);
3084 }
3085 
3087 {
3088  AVStream *video_st = s->streams[0];
3089  AVCodecParameters *video_par = s->streams[0]->codecpar;
3090  AVCodecParameters *audio_par = s->streams[1]->codecpar;
3091  int audio_rate = audio_par->sample_rate;
3092  // TODO: should be avg_frame_rate
3093  int frame_rate = ((video_st->time_base.den) * (0x10000)) / (video_st->time_base.num);
3094  int audio_kbitrate = audio_par->bit_rate / 1000;
3095  int video_kbitrate = FFMIN(video_par->bit_rate / 1000, 800 - audio_kbitrate);
3096 
3097  avio_wb32(pb, 0x94); /* size */
3098  ffio_wfourcc(pb, "uuid");
3099  ffio_wfourcc(pb, "PROF");
3100 
3101  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3102  avio_wb32(pb, 0xbb88695c);
3103  avio_wb32(pb, 0xfac9c740);
3104 
3105  avio_wb32(pb, 0x0); /* ? */
3106  avio_wb32(pb, 0x3); /* 3 sections ? */
3107 
3108  avio_wb32(pb, 0x14); /* size */
3109  ffio_wfourcc(pb, "FPRF");
3110  avio_wb32(pb, 0x0); /* ? */
3111  avio_wb32(pb, 0x0); /* ? */
3112  avio_wb32(pb, 0x0); /* ? */
3113 
3114  avio_wb32(pb, 0x2c); /* size */
3115  ffio_wfourcc(pb, "APRF"); /* audio */
3116  avio_wb32(pb, 0x0);
3117  avio_wb32(pb, 0x2); /* TrackID */
3118  ffio_wfourcc(pb, "mp4a");
3119  avio_wb32(pb, 0x20f);
3120  avio_wb32(pb, 0x0);
3121  avio_wb32(pb, audio_kbitrate);
3122  avio_wb32(pb, audio_kbitrate);
3123  avio_wb32(pb, audio_rate);
3124  avio_wb32(pb, audio_par->channels);
3125 
3126  avio_wb32(pb, 0x34); /* size */
3127  ffio_wfourcc(pb, "VPRF"); /* video */
3128  avio_wb32(pb, 0x0);
3129  avio_wb32(pb, 0x1); /* TrackID */
3130  if (video_par->codec_id == AV_CODEC_ID_H264) {
3131  ffio_wfourcc(pb, "avc1");
3132  avio_wb16(pb, 0x014D);
3133  avio_wb16(pb, 0x0015);
3134  } else {
3135  ffio_wfourcc(pb, "mp4v");
3136  avio_wb16(pb, 0x0000);
3137  avio_wb16(pb, 0x0103);
3138  }
3139  avio_wb32(pb, 0x0);
3140  avio_wb32(pb, video_kbitrate);
3141  avio_wb32(pb, video_kbitrate);
3142  avio_wb32(pb, frame_rate);
3143  avio_wb32(pb, frame_rate);
3144  avio_wb16(pb, video_par->width);
3145  avio_wb16(pb, video_par->height);
3146  avio_wb32(pb, 0x010001); /* ? */
3147 }
3148 
3150 {
3151  MOVMuxContext *mov = s->priv_data;
3152  mov_write_ftyp_tag(pb,s);
3153  if (mov->mode == MODE_PSP) {
3154  if (s->nb_streams != 2) {
3155  av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3156  return AVERROR(EINVAL);
3157  }
3158  mov_write_uuidprof_tag(pb, s);
3159  }
3160  return 0;
3161 }
3162 
3163 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
3164 {
3165  uint32_t c = -1;
3166  int i, closed_gop = 0;
3167 
3168  for (i = 0; i < pkt->size - 4; i++) {
3169  c = (c << 8) + pkt->data[i];
3170  if (c == 0x1b8) { // gop
3171  closed_gop = pkt->data[i + 4] >> 6 & 0x01;
3172  } else if (c == 0x100) { // pic
3173  int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
3174  if (!temp_ref || closed_gop) // I picture is not reordered
3175  *flags = MOV_SYNC_SAMPLE;
3176  else
3177  *flags = MOV_PARTIAL_SYNC_SAMPLE;
3178  break;
3179  }
3180  }
3181  return 0;
3182 }
3183 
3184 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
3185 {
3186  const uint8_t *start, *next, *end = pkt->data + pkt->size;
3187  int seq = 0, entry = 0;
3188  int key = pkt->flags & AV_PKT_FLAG_KEY;
3189  start = find_next_marker(pkt->data, end);
3190  for (next = start; next < end; start = next) {
3191  next = find_next_marker(start + 4, end);
3192  switch (AV_RB32(start)) {
3193  case VC1_CODE_SEQHDR:
3194  seq = 1;
3195  break;
3196  case VC1_CODE_ENTRYPOINT:
3197  entry = 1;
3198  break;
3199  case VC1_CODE_SLICE:
3200  trk->vc1_info.slices = 1;
3201  break;
3202  }
3203  }
3204  if (!trk->entry && trk->vc1_info.first_packet_seen)
3205  trk->vc1_info.first_frag_written = 1;
3206  if (!trk->entry && !trk->vc1_info.first_frag_written) {
3207  /* First packet in first fragment */
3208  trk->vc1_info.first_packet_seq = seq;
3209  trk->vc1_info.first_packet_entry = entry;
3210  trk->vc1_info.first_packet_seen = 1;
3211  } else if ((seq && !trk->vc1_info.packet_seq) ||
3212  (entry && !trk->vc1_info.packet_entry)) {
3213  int i;
3214  for (i = 0; i < trk->entry; i++)
3215  trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
3216  trk->has_keyframes = 0;
3217  if (seq)
3218  trk->vc1_info.packet_seq = 1;
3219  if (entry)
3220  trk->vc1_info.packet_entry = 1;
3221  if (!trk->vc1_info.first_frag_written) {
3222  /* First fragment */
3223  if ((!seq || trk->vc1_info.first_packet_seq) &&
3224  (!entry || trk->vc1_info.first_packet_entry)) {
3225  /* First packet had the same headers as this one, readd the
3226  * sync sample flag. */
3227  trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
3228  trk->has_keyframes = 1;
3229  }
3230  }
3231  }
3232  if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
3233  key = seq && entry;
3234  else if (trk->vc1_info.packet_seq)
3235  key = seq;
3236  else if (trk->vc1_info.packet_entry)
3237  key = entry;
3238  if (key) {
3239  trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
3240  trk->has_keyframes++;
3241  }
3242 }
3243 
3245 {
3246  MOVMuxContext *mov = s->priv_data;
3247  int ret, buf_size;
3248  uint8_t *buf;
3249  int i, offset;
3250 
3251  if (!track->mdat_buf)
3252  return 0;
3253  if (!mov->mdat_buf) {
3254  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
3255  return ret;
3256  }
3257  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
3258  track->mdat_buf = NULL;
3259 
3260  offset = avio_tell(mov->mdat_buf);
3261  avio_write(mov->mdat_buf, buf, buf_size);
3262  av_free(buf);
3263 
3264  for (i = track->entries_flushed; i < track->entry; i++)
3265  track->cluster[i].pos += offset;
3266  track->entries_flushed = track->entry;
3267  return 0;
3268 }
3269 
3270 static int mov_flush_fragment(AVFormatContext *s, int force)
3271 {
3272  MOVMuxContext *mov = s->priv_data;
3273  int i, first_track = -1;
3274  int64_t mdat_size = 0;
3275  int has_video = 0, starts_with_key = 0, first_video_track = 1;
3276 
3277  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
3278  return 0;
3279 
3280  // Try to fill in the duration of the last packet in each stream
3281  // from queued packets in the interleave queues. If the flushing
3282  // of fragments was triggered automatically by an AVPacket, we
3283  // already have reliable info for the end of that track, but other
3284  // tracks may need to be filled in.
3285  for (i = 0; i < s->nb_streams; i++) {
3286  MOVTrack *track = &mov->tracks[i];
3287  if (!track->end_reliable) {
3288  AVPacket pkt;
3289  if (!ff_interleaved_peek(s, i, &pkt, 1)) {
3290  track->track_duration = pkt.dts - track->start_dts;
3291  if (pkt.pts != AV_NOPTS_VALUE)
3292  track->end_pts = pkt.pts;
3293  else
3294  track->end_pts = pkt.dts;
3295  }
3296  }
3297  }
3298 
3299  for (i = 0; i < mov->nb_streams; i++) {
3300  MOVTrack *track = &mov->tracks[i];
3301  if (track->entry <= 1)
3302  continue;
3303  // Sample durations are calculated as the diff of dts values,
3304  // but for the last sample in a fragment, we don't know the dts
3305  // of the first sample in the next fragment, so we have to rely
3306  // on what was set as duration in the AVPacket. Not all callers
3307  // set this though, so we might want to replace it with an
3308  // estimate if it currently is zero.
3309  if (get_cluster_duration(track, track->entry - 1) != 0)
3310  continue;
3311  // Use the duration (i.e. dts diff) of the second last sample for
3312  // the last one. This is a wild guess (and fatal if it turns out
3313  // to be too long), but probably the best we can do - having a zero
3314  // duration is bad as well.
3315  track->track_duration += get_cluster_duration(track, track->entry - 2);
3316  track->end_pts += get_cluster_duration(track, track->entry - 2);
3317  if (!mov->missing_duration_warned) {
3319  "Estimating the duration of the last packet in a "
3320  "fragment, consider setting the duration field in "
3321  "AVPacket instead.\n");
3322  mov->missing_duration_warned = 1;
3323  }
3324  }
3325 
3326  if (!mov->moov_written) {
3327  int64_t pos = avio_tell(s->pb);
3328  int ret;
3329  AVIOContext *moov_buf;
3330  uint8_t *buf;
3331  int buf_size;
3332 
3333  for (i = 0; i < mov->nb_streams; i++)
3334  if (!mov->tracks[i].entry)
3335  break;
3336  /* Don't write the initial moov unless all tracks have data */
3337  if (i < mov->nb_streams && !force)
3338  return 0;
3339 
3340  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
3341  return ret;
3342  mov_write_moov_tag(moov_buf, mov, s);
3343  buf_size = ffio_close_null_buf(moov_buf);
3344  for (i = 0; i < mov->nb_streams; i++)
3345  mov->tracks[i].data_offset = pos + buf_size + 8;
3346 
3348  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
3350  mov_write_moov_tag(s->pb, mov, s);
3351 
3352  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
3353  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
3354  mov->reserved_header_pos = avio_tell(s->pb);
3355  avio_flush(s->pb);
3356  mov->moov_written = 1;
3357  return 0;
3358  }
3359 
3360  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
3361  mov->mdat_buf = NULL;
3362  avio_wb32(s->pb, buf_size + 8);
3363  ffio_wfourcc(s->pb, "mdat");
3364  avio_write(s->pb, buf, buf_size);
3365  av_free(buf);
3366 
3367  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
3368  mov->reserved_header_pos = avio_tell(s->pb);
3369 
3370  mov->moov_written = 1;
3371  mov->mdat_size = 0;
3372  for (i = 0; i < mov->nb_streams; i++) {
3373  if (mov->tracks[i].entry)
3374  mov->tracks[i].frag_start += mov->tracks[i].start_dts +
3375  mov->tracks[i].track_duration -
3376  mov->tracks[i].cluster[0].dts;
3377  mov->tracks[i].entry = 0;
3378  mov->tracks[i].end_reliable = 0;
3379  }
3380  avio_flush(s->pb);
3381  return 0;
3382  }
3383 
3384  if (mov->frag_interleave) {
3385  for (i = 0; i < mov->nb_streams; i++) {
3386  MOVTrack *track = &mov->tracks[i];
3387  int ret;
3388  if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
3389  return ret;
3390  }
3391 
3392  if (!mov->mdat_buf)
3393  return 0;
3394  mdat_size = avio_tell(mov->mdat_buf);
3395  }
3396 
3397  for (i = 0; i < mov->nb_streams; i++) {
3398  MOVTrack *track = &mov->tracks[i];
3399  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF || mov->frag_interleave)
3400  track->data_offset = 0;
3401  else
3402  track->data_offset = mdat_size;
3403  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3404  has_video = 1;
3405  if (first_video_track) {
3406  if (track->entry)
3407  starts_with_key = track->cluster[0].flags & MOV_SYNC_SAMPLE;
3408  first_video_track = 0;
3409  }
3410  }
3411  if (!track->entry)
3412  continue;
3413  if (track->mdat_buf)
3414  mdat_size += avio_tell(track->mdat_buf);
3415  if (first_track < 0)
3416  first_track = i;
3417  }
3418 
3419  if (!mdat_size)
3420  return 0;
3421 
3422  avio_write_marker(s->pb,
3423  av_rescale(mov->tracks[first_track].cluster[0].dts, AV_TIME_BASE, mov->tracks[first_track].timescale),
3424  (has_video ? starts_with_key : mov->tracks[first_track].cluster[0].flags & MOV_SYNC_SAMPLE) ? AVIO_DATA_MARKER_SYNC_POINT : AVIO_DATA_MARKER_BOUNDARY_POINT);
3425 
3426  for (i = 0; i < mov->nb_streams; i++) {
3427  MOVTrack *track = &mov->tracks[i];
3428  int buf_size, write_moof = 1, moof_tracks = -1;
3429  uint8_t *buf;
3430  int64_t duration = 0;
3431 
3432  if (track->entry)
3433  duration = track->start_dts + track->track_duration -
3434  track->cluster[0].dts;
3435  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
3436  if (!track->mdat_buf)
3437  continue;
3438  mdat_size = avio_tell(track->mdat_buf);
3439  moof_tracks = i;
3440  } else {
3441  write_moof = i == first_track;
3442  }
3443 
3444  if (write_moof) {
3445  avio_flush(s->pb);
3446 
3447  mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
3448  mov->fragments++;
3449 
3450  avio_wb32(s->pb, mdat_size + 8);
3451  ffio_wfourcc(s->pb, "mdat");
3452  }
3453 
3454  if (track->entry)
3455  track->frag_start += duration;
3456  track->entry = 0;
3457  track->entries_flushed = 0;
3458  track->end_reliable = 0;
3459  if (!mov->frag_interleave) {
3460  if (!track->mdat_buf)
3461  continue;
3462  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
3463  track->mdat_buf = NULL;
3464  } else {
3465  if (!mov->mdat_buf)
3466  continue;
3467  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
3468  mov->mdat_buf = NULL;
3469  }
3470 
3471  avio_write(s->pb, buf, buf_size);
3472  av_free(buf);
3473  }
3474 
3475  mov->mdat_size = 0;
3476 
3477  avio_flush(s->pb);
3478  return 0;
3479 }
3480 
3481 static int mov_auto_flush_fragment(AVFormatContext *s, int force)
3482 {
3483  MOVMuxContext *mov = s->priv_data;
3484  int had_moov = mov->moov_written;
3485  int ret = mov_flush_fragment(s, force);
3486  if (ret < 0)
3487  return ret;
3488  // If using delay_moov, the first flush only wrote the moov,
3489  // not the actual moof+mdat pair, thus flush once again.
3490  if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
3491  ret = mov_flush_fragment(s, force);
3492  return ret;
3493 }
3494 
3496 {
3497  MOVMuxContext *mov = s->priv_data;
3498  AVIOContext *pb = s->pb;
3499  MOVTrack *trk = &mov->tracks[pkt->stream_index];
3500  AVCodecParameters *par = trk->par;
3501  unsigned int samples_in_chunk = 0;
3502  int size = pkt->size, ret = 0;
3503  uint8_t *reformatted_data = NULL;
3504 
3505  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3506  int ret;
3507  if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3508  if (mov->frag_interleave && mov->fragments > 0) {
3509  if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
3510  if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
3511  return ret;
3512  }
3513  }
3514 
3515  if (!trk->mdat_buf) {
3516  if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
3517  return ret;
3518  }
3519  pb = trk->mdat_buf;
3520  } else {
3521  if (!mov->mdat_buf) {
3522  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
3523  return ret;
3524  }
3525  pb = mov->mdat_buf;
3526  }
3527  }
3528 
3529  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
3530  /* We must find out how many AMR blocks there are in one packet */
3531  static uint16_t packed_size[16] =
3532  {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
3533  int len = 0;
3534 
3535  while (len < size && samples_in_chunk < 100) {
3536  len += packed_size[(pkt->data[len] >> 3) & 0x0F];
3537  samples_in_chunk++;
3538  }
3539  if (samples_in_chunk > 1) {
3540  av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
3541  return -1;
3542  }
3543  } else if (trk->sample_size)
3544  samples_in_chunk = size / trk->sample_size;
3545  else
3546  samples_in_chunk = 1;
3547 
3548  /* copy extradata if it exists */
3549  if (trk->vos_len == 0 && par->extradata_size > 0) {
3550  trk->vos_len = par->extradata_size;
3551  trk->vos_data = av_malloc(trk->vos_len);
3552  if (!trk->vos_data) {
3553  ret = AVERROR(ENOMEM);
3554  goto err;
3555  }
3556  memcpy(trk->vos_data, par->extradata, trk->vos_len);
3557  }
3558 
3559  if (par->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
3560  /* from x264 or from bytestream H.264 */
3561  /* NAL reformatting needed */
3562  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3563  ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
3564  &size);
3565  avio_write(pb, reformatted_data, size);
3566  } else {
3567  size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
3568  }
3569  } else if (par->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
3570  (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
3571  /* extradata is Annex B, assume the bitstream is too and convert it */
3572  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3573  ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
3574  avio_write(pb, reformatted_data, size);
3575  } else {
3576  size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
3577  }
3578  } else {
3579  avio_write(pb, pkt->data, size);
3580  }
3581 
3582  if ((par->codec_id == AV_CODEC_ID_DNXHD ||
3583  par->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
3584  /* copy frame to create needed atoms */
3585  trk->vos_len = size;
3586  trk->vos_data = av_malloc(size);
3587  if (!trk->vos_data) {
3588  ret = AVERROR(ENOMEM);
3589  goto err;
3590  }
3591  memcpy(trk->vos_data, pkt->data, size);
3592  }
3593 
3594  if (trk->entry >= trk->cluster_capacity) {
3595  unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
3596  if (av_reallocp_array(&trk->cluster, new_capacity,
3597  sizeof(*trk->cluster))) {
3598  ret = AVERROR(ENOMEM);
3599  goto err;
3600  }
3601  trk->cluster_capacity = new_capacity;
3602  }
3603 
3604  trk->cluster[trk->entry].pos = avio_tell(pb) - size;
3605  trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
3606  trk->cluster[trk->entry].size = size;
3607  trk->cluster[trk->entry].entries = samples_in_chunk;
3608  trk->cluster[trk->entry].dts = pkt->dts;
3609  if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
3610  if (!trk->frag_discont) {
3611  /* First packet of a new fragment. We already wrote the duration
3612  * of the last packet of the previous fragment based on track_duration,
3613  * which might not exactly match our dts. Therefore adjust the dts
3614  * of this packet to be what the previous packets duration implies. */
3615  trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
3616  /* We also may have written the pts and the corresponding duration
3617  * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
3618  * the next fragment. This means the cts of the first sample must
3619  * be the same in all fragments, unless end_pts was updated by
3620  * the packet causing the fragment to be written. */
3621  if ((mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)) ||
3622  mov->mode == MODE_ISM)
3623  pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
3624  } else {
3625  /* New fragment, but discontinuous from previous fragments.
3626  * Pretend the duration sum of the earlier fragments is
3627  * pkt->dts - trk->start_dts. */
3628  trk->frag_start = pkt->dts - trk->start_dts;
3629  trk->end_pts = AV_NOPTS_VALUE;
3630  trk->frag_discont = 0;
3631  }
3632  }
3633  if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
3635  /* Not using edit lists and shifting the first track to start from zero.
3636  * If the other streams start from a later timestamp, we won't be able
3637  * to signal the difference in starting time without an edit list.
3638  * Thus move the timestamp for this first sample to 0, increasing
3639  * its duration instead. */
3640  trk->cluster[trk->entry].dts = trk->start_dts = 0;
3641  }
3642  if (trk->start_dts == AV_NOPTS_VALUE) {
3643  trk->start_dts = pkt->dts;
3644  if (trk->frag_discont) {
3645  if (mov->use_editlist) {
3646  /* Pretend the whole stream started at pts=0, with earlier fragments
3647  * already written. If the stream started at pts=0, the duration sum
3648  * of earlier fragments would have been pkt->pts. */
3649  trk->frag_start = pkt->pts;
3650  trk->start_dts = pkt->dts - pkt->pts;
3651  } else {
3652  /* Pretend the whole stream started at dts=0, with earlier fragments
3653  * already written, with a duration summing up to pkt->dts. */
3654  trk->frag_start = pkt->dts;
3655  trk->start_dts = 0;
3656  }
3657  trk->frag_discont = 0;
3658  } else if (pkt->dts && mov->moov_written)
3660  "Track %d starts with a nonzero dts %"PRId64", while the moov "
3661  "already has been written. Set the delay_moov flag to handle "
3662  "this case.\n",
3663  pkt->stream_index, pkt->dts);
3664  }
3665  trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
3666 
3667  if (pkt->pts == AV_NOPTS_VALUE) {
3668  av_log(s, AV_LOG_WARNING, "pts has no value\n");
3669  pkt->pts = pkt->dts;
3670  }
3671  if (pkt->dts != pkt->pts)
3672  trk->flags |= MOV_TRACK_CTTS;
3673  trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
3674  trk->cluster[trk->entry].flags = 0;
3675  if (trk->start_cts == AV_NOPTS_VALUE)
3676  trk->start_cts = pkt->pts - pkt->dts;
3677  if (trk->end_pts == AV_NOPTS_VALUE)
3678  trk->end_pts = trk->cluster[trk->entry].dts +
3679  trk->cluster[trk->entry].cts + pkt->duration;
3680  else
3681  trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
3682  trk->cluster[trk->entry].cts +
3683  pkt->duration);
3684 
3685  if (par->codec_id == AV_CODEC_ID_VC1) {
3686  mov_parse_vc1_frame(pkt, trk);
3687  } else if (pkt->flags & AV_PKT_FLAG_KEY) {
3688  if (mov->mode == MODE_MOV && par->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
3689  trk->entry > 0) { // force sync sample for the first key frame
3690  mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3691  if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3692  trk->flags |= MOV_TRACK_STPS;
3693  } else {
3694  trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3695  }
3696  if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
3697  trk->has_keyframes++;
3698  }
3699  trk->entry++;
3700  trk->sample_count += samples_in_chunk;
3701  mov->mdat_size += size;
3702 
3703  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
3704  ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3705  reformatted_data, size);
3706 
3707 err:
3708  av_free(reformatted_data);
3709  return ret;
3710 }
3711 
3713 {
3714  if (!pkt) {
3715  mov_flush_fragment(s, 1);
3716  return 1;
3717  } else {
3718  MOVMuxContext *mov = s->priv_data;
3719  MOVTrack *trk = &mov->tracks[pkt->stream_index];
3720  AVCodecParameters *par = trk->par;
3721  int64_t frag_duration = 0;
3722  int size = pkt->size;
3723 
3724  if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
3725  int i;
3726  for (i = 0; i < s->nb_streams; i++)
3727  mov->tracks[i].frag_discont = 1;
3729  }
3730 
3731  if (!pkt->size) {
3732  if (trk->start_dts == AV_NOPTS_VALUE && trk->frag_discont) {
3733  trk->start_dts = pkt->dts;
3734  if (pkt->pts != AV_NOPTS_VALUE)
3735  trk->start_cts = pkt->pts - pkt->dts;
3736  else
3737  trk->start_cts = 0;
3738  }
3739 
3740  return 0; /* Discard 0 sized packets */
3741  }
3742 
3743  if (trk->entry)
3744  frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3745  s->streams[pkt->stream_index]->time_base,
3746  AV_TIME_BASE_Q);
3747  if ((mov->max_fragment_duration &&
3748  frag_duration >= mov->max_fragment_duration) ||
3749  (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3750  (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3751  par->codec_type == AVMEDIA_TYPE_VIDEO &&
3752  trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3753  if (frag_duration >= mov->min_fragment_duration) {
3754  // Set the duration of this track to line up with the next
3755  // sample in this track. This avoids relying on AVPacket
3756  // duration, but only helps for this particular track, not
3757  // for the other ones that are flushed at the same time.
3758  trk->track_duration = pkt->dts - trk->start_dts;
3759  if (pkt->pts != AV_NOPTS_VALUE)
3760  trk->end_pts = pkt->pts;
3761  else
3762  trk->end_pts = pkt->dts;
3763  trk->end_reliable = 1;
3765  }
3766  }
3767 
3768  return ff_mov_write_packet(s, pkt);
3769  }
3770 }
3771 
3772 // QuickTime chapters involve an additional text track with the chapter names
3773 // as samples, and a tref pointing from the other tracks to the chapter one.
3774 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
3775 {
3776  MOVMuxContext *mov = s->priv_data;
3777  MOVTrack *track = &mov->tracks[tracknum];
3778  AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
3779  int i, len;
3780  // These properties are required to make QT recognize the chapter track
3781  uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
3782 
3783  track->mode = mov->mode;
3784  track->tag = MKTAG('t','e','x','t');
3785  track->timescale = MOV_TIMESCALE;
3786  track->par = avcodec_parameters_alloc();
3787  if (!track->par)
3788  return AVERROR(ENOMEM);
3790  track->par->extradata = av_malloc(sizeof(chapter_properties));
3791  if (!track->par->extradata)
3792  return AVERROR(ENOMEM);
3793  track->par->extradata_size = sizeof(chapter_properties);
3794  memcpy(track->par->extradata, chapter_properties, sizeof(chapter_properties));
3795 
3796  for (i = 0; i < s->nb_chapters; i++) {
3797  AVChapter *c = s->chapters[i];
3798  AVDictionaryEntry *t;
3799 
3800  int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3801  pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3802  pkt.duration = end - pkt.dts;
3803 
3804  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3805  static const char encd[12] = {
3806  0x00, 0x00, 0x00, 0x0C,
3807  'e', 'n', 'c', 'd',
3808  0x00, 0x00, 0x01, 0x00 };
3809  len = strlen(t->value);
3810  pkt.size = len + 2 + 12;
3811  pkt.data = av_malloc(pkt.size);
3812  if (!pkt.data)
3813  return AVERROR(ENOMEM);
3814  AV_WB16(pkt.data, len);
3815  memcpy(pkt.data + 2, t->value, len);
3816  memcpy(pkt.data + len + 2, encd, sizeof(encd));
3817  ff_mov_write_packet(s, &pkt);
3818  av_freep(&pkt.data);
3819  }
3820  }
3821 
3822  return 0;
3823 }
3824 
3825 /*
3826  * st->disposition controls the "enabled" flag in the tkhd tag.
3827  * QuickTime will not play a track if it is not enabled. So make sure
3828  * that one track of each type (audio, video, subtitle) is enabled.
3829  *
3830  * Subtitles are special. For audio and video, setting "enabled" also
3831  * makes the track "default" (i.e. it is rendered when played). For
3832  * subtitles, an "enabled" subtitle is not rendered by default, but
3833  * if no subtitle is enabled, the subtitle menu in QuickTime will be
3834  * empty!
3835  */
3837 {
3838  MOVMuxContext *mov = s->priv_data;
3839  int i;
3840  int enabled[AVMEDIA_TYPE_NB];
3841  int first[AVMEDIA_TYPE_NB];
3842 
3843  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3844  enabled[i] = 0;
3845  first[i] = -1;
3846  }
3847 
3848  for (i = 0; i < s->nb_streams; i++) {
3849  AVStream *st = s->streams[i];
3850 
3852  st->codecpar->codec_type >= AVMEDIA_TYPE_NB)
3853  continue;
3854 
3855  if (first[st->codecpar->codec_type] < 0)
3856  first[st->codecpar->codec_type] = i;
3857  if (st->disposition & AV_DISPOSITION_DEFAULT) {
3858  mov->tracks[i].flags |= MOV_TRACK_ENABLED;
3859  enabled[st->codecpar->codec_type]++;
3860  }
3861  }
3862 
3863  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3864  switch (i) {
3865  case AVMEDIA_TYPE_VIDEO:
3866  case AVMEDIA_TYPE_AUDIO:
3867  case AVMEDIA_TYPE_SUBTITLE:
3868  if (enabled[i] > 1)
3869  mov->per_stream_grouping = 1;
3870  if (!enabled[i] && first[i] >= 0)
3871  mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
3872  break;
3873  }
3874  }
3875 }
3876 
3877 static void mov_free(AVFormatContext *s)
3878 {
3879  MOVMuxContext *mov = s->priv_data;
3880  int i;
3881 
3882  if (mov->chapter_track)
3884 
3885  for (i = 0; i < mov->nb_streams; i++) {
3886  if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3887  ff_mov_close_hinting(&mov->tracks[i]);
3888  av_freep(&mov->tracks[i].cluster);
3889  av_freep(&mov->tracks[i].frag_info);
3890 
3891  if (mov->tracks[i].vos_len)
3892  av_free(mov->tracks[i].vos_data);
3893  }
3894 
3895  av_freep(&mov->tracks);
3896 }
3897 
3898 static uint32_t rgb_to_yuv(uint32_t rgb)
3899 {
3900  uint8_t r, g, b;
3901  int y, cb, cr;
3902 
3903  r = (rgb >> 16) & 0xFF;
3904  g = (rgb >> 8) & 0xFF;
3905  b = (rgb ) & 0xFF;
3906 
3907  y = av_clip_uint8( 16. + 0.257 * r + 0.504 * g + 0.098 * b);
3908  cb = av_clip_uint8(128. - 0.148 * r - 0.291 * g + 0.439 * b);
3909  cr = av_clip_uint8(128. + 0.439 * r - 0.368 * g - 0.071 * b);
3910 
3911  return (y << 16) | (cr << 8) | cb;
3912 }
3913 
3915  AVStream *st)
3916 {
3917  int i, width = 720, height = 480;
3918  int have_palette = 0, have_size = 0;
3919  uint32_t palette[16];
3920  char *cur = st->codecpar->extradata;
3921 
3922  while (cur && *cur) {
3923  if (strncmp("palette:", cur, 8) == 0) {
3924  int i, count;
3925  count = sscanf(cur + 8,
3926  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3927  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3928  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3929  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
3930  &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
3931  &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
3932  &palette[ 8], &palette[ 9], &palette[10], &palette[11],
3933  &palette[12], &palette[13], &palette[14], &palette[15]);
3934 
3935  for (i = 0; i < count; i++) {
3936  palette[i] = rgb_to_yuv(palette[i]);
3937  }
3938  have_palette = 1;
3939  } else if (!strncmp("size:", cur, 5)) {
3940  sscanf(cur + 5, "%dx%d", &width, &height);
3941  have_size = 1;
3942  }
3943  if (have_palette && have_size)
3944  break;
3945  cur += strcspn(cur, "\n\r");
3946  cur += strspn(cur, "\n\r");
3947  }
3948  if (have_palette) {
3949  track->vos_data = av_malloc(16*4);
3950  if (!track->vos_data)
3951  return AVERROR(ENOMEM);
3952  for (i = 0; i < 16; i++) {
3953  AV_WB32(track->vos_data + i * 4, palette[i]);
3954  }
3955  track->vos_len = 16 * 4;
3956  }
3957  st->codecpar->width = width;
3958  st->codecpar->height = track->height = height;
3959 
3960  return 0;
3961 }
3962 
3964 {
3965  AVIOContext *pb = s->pb;
3966  MOVMuxContext *mov = s->priv_data;
3967  AVDictionaryEntry *t;
3968  int i, ret, hint_track = 0;
3969 
3970  mov->fc = s;
3971 
3972  /* Default mode == MP4 */
3973  mov->mode = MODE_MP4;
3974 
3975  if (s->oformat) {
3976  if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3977  else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3978  else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3979  else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3980  else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3981  else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3982  else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
3983  }
3984 
3985  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
3986  mov->flags |= FF_MOV_FLAG_EMPTY_MOOV;
3987 
3988  /* Set the FRAGMENT flag if any of the fragmentation methods are
3989  * enabled. */
3990  if (mov->max_fragment_duration || mov->max_fragment_size ||
3991  mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3994  mov->flags |= FF_MOV_FLAG_FRAGMENT;
3995 
3996  /* Set other implicit flags immediately */
3997  if (mov->mode == MODE_ISM)
4000  if (mov->flags & FF_MOV_FLAG_DASH)
4003 
4004  if (mov->use_editlist < 0) {
4005  mov->use_editlist = 1;
4006  if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
4007  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
4008  // If we can avoid needing an edit list by shifting the
4009  // tracks, prefer that over (trying to) write edit lists
4010  // in fragmented output.
4013  mov->use_editlist = 0;
4014  }
4015  }
4016  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
4017  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
4018  av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
4019 
4022 
4023  /* Clear the omit_tfhd_offset flag if default_base_moof is set;
4024  * if the latter is set that's enough and omit_tfhd_offset doesn't
4025  * add anything extra on top of that. */
4026  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
4029 
4030  if (mov->frag_interleave &&
4032  av_log(s, AV_LOG_ERROR,
4033  "Sample interleaving in fragments is mutually exclusive with "
4034  "omit_tfhd_offset and separate_moof\n");
4035  return AVERROR(EINVAL);
4036  }
4037 
4038  /* Non-seekable output is ok if using fragmentation. If ism_lookahead
4039  * is enabled, we don't support non-seekable output at all. */
4040  if (!s->pb->seekable &&
4041  (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
4042  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
4043  return AVERROR(EINVAL);
4044  }
4045 
4046 
4047  if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
4048  if ((ret = mov_write_identification(pb, s)) < 0)
4049  return ret;
4050  }
4051 
4052  mov->nb_streams = s->nb_streams;
4053  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
4054  mov->chapter_track = mov->nb_streams++;
4055 
4056  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
4057  /* Add hint tracks for each audio and video stream */
4058  hint_track = mov->nb_streams;
4059  for (i = 0; i < s->nb_streams; i++) {
4060  AVStream *st = s->streams[i];
4061  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
4063  mov->nb_streams++;
4064  }
4065  }
4066  }
4067 
4068  // Reserve an extra stream for chapters for the case where chapters
4069  // are written in the trailer
4070  mov->tracks = av_mallocz((mov->nb_streams + 1) * sizeof(*mov->tracks));
4071  if (!mov->tracks)
4072  return AVERROR(ENOMEM);
4073 
4074  for (i = 0; i < s->nb_streams; i++) {
4075  AVStream *st= s->streams[i];
4076  MOVTrack *track= &mov->tracks[i];
4077  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
4078 
4079  track->st = st;
4080  track->par = st->codecpar;
4081  track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
4082  if (track->language < 0)
4083  track->language = 0;
4084  track->mode = mov->mode;
4085  track->tag = mov_find_codec_tag(s, track);
4086  if (!track->tag) {
4087  av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
4088  "codec not currently supported in container\n", i);
4089  goto error;
4090  }
4091  /* If hinting of this track is enabled by a later hint track,
4092  * this is updated. */
4093  track->hint_track = -1;
4094  track->start_dts = AV_NOPTS_VALUE;
4095  track->start_cts = AV_NOPTS_VALUE;
4096  track->end_pts = AV_NOPTS_VALUE;
4097  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
4098  if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
4099  track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
4100  track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
4101  if (st->codecpar->width != 720 || (st->codecpar->height != 608 && st->codecpar->height != 512)) {
4102  av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
4103  goto error;
4104  }
4105  track->height = track->tag >> 24 == 'n' ? 486 : 576;
4106  }
4107  track->timescale = st->time_base.den;
4108  if (track->mode == MODE_MOV && track->timescale > 100000)
4110  "WARNING codec timebase is very high. If duration is too long,\n"
4111  "file may not be playable by quicktime. Specify a shorter timebase\n"
4112  "or choose different container.\n");
4113  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
4114  track->timescale = st->codecpar->sample_rate;
4115  /* set sample_size for PCM and ADPCM */
4118  if (!st->codecpar->block_align) {
4119  av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
4120  goto error;
4121  }
4122  track->sample_size = st->codecpar->block_align;
4123  }
4124  /* set audio_vbr for compressed audio */
4125  if (av_get_bits_per_sample(st->codecpar->codec_id) < 8) {
4126  track->audio_vbr = 1;
4127  }
4128  if (track->mode != MODE_MOV &&
4129  track->par->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
4130  av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
4131  i, track->par->sample_rate);
4132  goto error;
4133  }
4134  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4135  track->timescale = st->time_base.den;
4136  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
4137  track->timescale = st->time_base.den;
4138  }
4139  if (!track->height)
4140  track->height = st->codecpar->height;
4141  /* The ism specific timescale isn't mandatory, but is assumed by
4142  * some tools, such as mp4split. */
4143  if (mov->mode == MODE_ISM)
4144  track->timescale = 10000000;
4145 
4146  avpriv_set_pts_info(st, 64, 1, track->timescale);
4147 
4148  /* copy extradata if it exists */
4149  if (st->codecpar->extradata_size) {
4152  else {
4153  track->vos_len = st->codecpar->extradata_size;
4154  track->vos_data = av_malloc(track->vos_len);
4155  if (!track->vos_data)
4156  goto error;
4157  memcpy(track->vos_data, st->codecpar->extradata, track->vos_len);
4158  }
4159  }
4160  }
4161 
4162  enable_tracks(s);
4163 
4164  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
4165  /* If no fragmentation options have been set, set a default. */
4166  if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
4170  } else {
4171  if (mov->flags & FF_MOV_FLAG_FASTSTART)
4172  mov->reserved_header_pos = avio_tell(pb);
4173  mov_write_mdat_tag(pb, mov);
4174  }
4175 
4176  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
4177  mov->time = ff_iso8601_to_unix_time(t->value);
4178  if (mov->time)
4179  mov->time += 0x7C25B080; // 1970 based -> 1904 based
4180 
4181  if (mov->chapter_track)
4182  if (mov_create_chapter_track(s, mov->chapter_track) < 0)
4183  goto error;
4184 
4185  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
4186  /* Initialize the hint tracks for each audio and video stream */
4187  for (i = 0; i < s->nb_streams; i++) {
4188  AVStream *st = s->streams[i];
4189  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
4191  ff_mov_init_hinting(s, hint_track, i);
4192  hint_track++;
4193  }
4194  }
4195  }
4196 
4197  avio_flush(pb);
4198 
4199  if (mov->flags & FF_MOV_FLAG_ISML)
4200  mov_write_isml_manifest(pb, mov);
4201 
4202  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
4203  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
4204  mov_write_moov_tag(pb, mov, s);
4205  avio_flush(pb);
4206  mov->moov_written = 1;
4207  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
4208  mov->reserved_header_pos = avio_tell(pb);
4209  }
4210 
4211  return 0;
4212  error:
4213  mov_free(s);
4214  return -1;
4215 }
4216 
4218 {
4219  int ret;
4220  AVIOContext *moov_buf;
4221  MOVMuxContext *mov = s->priv_data;
4222 
4223  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
4224  return ret;
4225  mov_write_moov_tag(moov_buf, mov, s);
4226  return ffio_close_null_buf(moov_buf);
4227 }
4228 
4230 {
4231  int ret;
4232  AVIOContext *buf;
4233  MOVMuxContext *mov = s->priv_data;
4234 
4235  if ((ret = ffio_open_null_buf(&buf)) < 0)
4236  return ret;
4237  mov_write_sidx_tags(buf, mov, -1, 0);
4238  return ffio_close_null_buf(buf);
4239 }
4240 
4241 /*
4242  * This function gets the moov size if moved to the top of the file: the chunk
4243  * offset table can switch between stco (32-bit entries) to co64 (64-bit
4244  * entries) when the moov is moved to the beginning, so the size of the moov
4245  * would change. It also updates the chunk offset tables.
4246  */
4248 {
4249  int i, moov_size, moov_size2;
4250  MOVMuxContext *mov = s->priv_data;
4251 
4252  moov_size = get_moov_size(s);
4253  if (moov_size < 0)
4254  return moov_size;
4255 
4256  for (i = 0; i < mov->nb_streams; i++)
4257  mov->tracks[i].data_offset += moov_size;
4258 
4259  moov_size2 = get_moov_size(s);
4260  if (moov_size2 < 0)
4261  return moov_size2;
4262 
4263  /* if the size changed, we just switched from stco to co64 and need to
4264  * update the offsets */
4265  if (moov_size2 != moov_size)
4266  for (i = 0; i < mov->nb_streams; i++)
4267  mov->tracks[i].data_offset += moov_size2 - moov_size;
4268 
4269  return moov_size2;
4270 }
4271 
4273 {
4274  int i, sidx_size;
4275  MOVMuxContext *mov = s->priv_data;
4276 
4277  sidx_size = get_sidx_size(s);
4278  if (sidx_size < 0)
4279  return sidx_size;
4280 
4281  for (i = 0; i < mov->nb_streams; i++)
4282  mov->tracks[i].data_offset += sidx_size;
4283 
4284  return sidx_size;
4285 }
4286 
4288 {
4289  int ret = 0, moov_size;
4290  MOVMuxContext *mov = s->priv_data;
4291  int64_t pos, pos_end = avio_tell(s->pb);
4292  uint8_t *buf, *read_buf[2];
4293  int read_buf_id = 0;
4294  int read_size[2];
4295  AVIOContext *read_pb;
4296 
4297  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
4298  moov_size = compute_sidx_size(s);
4299  else
4300  moov_size = compute_moov_size(s);
4301  if (moov_size < 0)
4302  return moov_size;
4303 
4304  buf = av_malloc(moov_size * 2);
4305  if (!buf)
4306  return AVERROR(ENOMEM);
4307  read_buf[0] = buf;
4308  read_buf[1] = buf + moov_size;
4309 
4310  /* Shift the data: the AVIO context of the output can only be used for
4311  * writing, so we re-open the same output, but for reading. It also avoids
4312  * a read/seek/write/seek back and forth. */
4313  avio_flush(s->pb);
4314  ret = s->io_open(s, &read_pb, s->filename, AVIO_FLAG_READ, NULL);
4315  if (ret < 0) {
4316  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
4317  "the second pass (faststart)\n", s->filename);
4318  goto end;
4319  }
4320 
4321  /* mark the end of the shift to up to the last data we wrote, and get ready
4322  * for writing */
4323  pos_end = avio_tell(s->pb);
4324  avio_seek(s->pb, mov->reserved_header_pos + moov_size, SEEK_SET);
4325 
4326  /* start reading at where the new moov will be placed */
4327  avio_seek(read_pb, mov->reserved_header_pos, SEEK_SET);
4328  pos = avio_tell(read_pb);
4329 
4330 #define READ_BLOCK do { \
4331  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
4332  read_buf_id ^= 1; \
4333 } while (0)
4334 
4335  /* shift data by chunk of at most moov_size */
4336  READ_BLOCK;
4337  do {
4338  int n;
4339  READ_BLOCK;
4340  n = read_size[read_buf_id];
4341  if (n <= 0)
4342  break;
4343  avio_write(s->pb, read_buf[read_buf_id], n);
4344  pos += n;
4345  } while (pos < pos_end);
4346  ff_format_io_close(s, &read_pb);
4347 
4348 end:
4349  av_free(buf);
4350  return ret;
4351 }
4352 
4354 {
4355  MOVMuxContext *mov = s->priv_data;
4356  AVIOContext *pb = s->pb;
4357  int res = 0;
4358  int i;
4359  int64_t moov_pos;
4360 
4361  // If there were no chapters when the header was written, but there
4362  // are chapters now, write them in the trailer. This only works
4363  // when we are not doing fragments.
4364  if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
4365  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
4366  mov->chapter_track = mov->nb_streams++;
4367  if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
4368  goto error;
4369  }
4370  }
4371 
4372  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
4373  moov_pos = avio_tell(pb);
4374 
4375  /* Write size of mdat tag */
4376  if (mov->mdat_size + 8 <= UINT32_MAX) {
4377  avio_seek(pb, mov->mdat_pos, SEEK_SET);
4378  avio_wb32(pb, mov->mdat_size + 8);
4379  } else {
4380  /* overwrite 'wide' placeholder atom */
4381  avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
4382  /* special value: real atom size will be 64 bit value after
4383  * tag field */
4384  avio_wb32(pb, 1);
4385  ffio_wfourcc(pb, "mdat");
4386  avio_wb64(pb, mov->mdat_size + 16);
4387  }
4388  avio_seek(pb, moov_pos, SEEK_SET);
4389 
4390  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
4391  av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
4392  res = shift_data(s);
4393  if (res == 0) {
4394  avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
4395  mov_write_moov_tag(pb, mov, s);
4396  }
4397  } else {
4398  mov_write_moov_tag(pb, mov, s);
4399  }
4400  } else {
4402  for (i = 0; i < mov->nb_streams; i++)
4403  mov->tracks[i].data_offset = 0;
4404  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) {
4405  av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
4406  res = shift_data(s);
4407  if (res == 0) {
4408  int64_t end = avio_tell(pb);
4409  avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
4410  mov_write_sidx_tags(pb, mov, -1, 0);
4411  avio_seek(pb, end, SEEK_SET);
4413  mov_write_mfra_tag(pb, mov);
4414  }
4415  } else if (!(mov->flags & FF_MOV_FLAG_SKIP_TRAILER)) {
4417  mov_write_mfra_tag(pb, mov);
4418  }
4419  }
4420 
4421 error:
4422  mov_free(s);
4423 
4424  return res;
4425 }
4426 
4427 #if CONFIG_MOV_MUXER
4428 MOV_CLASS(mov)
4429 AVOutputFormat ff_mov_muxer = {
4430  .name = "mov",
4431  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
4432  .extensions = "mov",
4433  .priv_data_size = sizeof(MOVMuxContext),
4434  .audio_codec = AV_CODEC_ID_AAC,
4435  .video_codec = CONFIG_LIBX264_ENCODER ?
4441  .codec_tag = (const AVCodecTag* const []){
4443  },
4444  .priv_class = &mov_muxer_class,
4445 };
4446 #endif
4447 #if CONFIG_TGP_MUXER
4448 MOV_CLASS(tgp)
4449 AVOutputFormat ff_tgp_muxer = {
4450  .name = "3gp",
4451  .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
4452  .extensions = "3gp",
4453  .priv_data_size = sizeof(MOVMuxContext),
4454  .audio_codec = AV_CODEC_ID_AMR_NB,
4455  .video_codec = AV_CODEC_ID_H263,
4460  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
4461  .priv_class = &tgp_muxer_class,
4462 };
4463 #endif
4464 #if CONFIG_MP4_MUXER
4465 MOV_CLASS(mp4)
4466 AVOutputFormat ff_mp4_muxer = {
4467  .name = "mp4",
4468  .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
4469  .mime_type = "application/mp4",
4470  .extensions = "mp4",
4471  .priv_data_size = sizeof(MOVMuxContext),
4472  .audio_codec = AV_CODEC_ID_AAC,
4473  .video_codec = CONFIG_LIBX264_ENCODER ?
4479  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4480  .priv_class = &mp4_muxer_class,
4481 };
4482 #endif
4483 #if CONFIG_PSP_MUXER
4484 MOV_CLASS(psp)
4485 AVOutputFormat ff_psp_muxer = {
4486  .name = "psp",
4487  .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
4488  .extensions = "mp4,psp",
4489  .priv_data_size = sizeof(MOVMuxContext),
4490  .audio_codec = AV_CODEC_ID_AAC,
4491  .video_codec = CONFIG_LIBX264_ENCODER ?
4497  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4498  .priv_class = &psp_muxer_class,
4499 };
4500 #endif
4501 #if CONFIG_TG2_MUXER
4502 MOV_CLASS(tg2)
4503 AVOutputFormat ff_tg2_muxer = {
4504  .name = "3g2",
4505  .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
4506  .extensions = "3g2",
4507  .priv_data_size = sizeof(MOVMuxContext),
4508  .audio_codec = AV_CODEC_ID_AMR_NB,
4509  .video_codec = AV_CODEC_ID_H263,
4514  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
4515  .priv_class = &tg2_muxer_class,
4516 };
4517 #endif
4518 #if CONFIG_IPOD_MUXER
4519 MOV_CLASS(ipod)
4520 AVOutputFormat ff_ipod_muxer = {
4521  .name = "ipod",
4522  .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
4523  .mime_type = "application/mp4",
4524  .extensions = "m4v,m4a",
4525  .priv_data_size = sizeof(MOVMuxContext),
4526  .audio_codec = AV_CODEC_ID_AAC,
4527  .video_codec = AV_CODEC_ID_H264,
4532  .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
4533  .priv_class = &ipod_muxer_class,
4534 };
4535 #endif
4536 #if CONFIG_ISMV_MUXER
4537 MOV_CLASS(ismv)
4538 AVOutputFormat ff_ismv_muxer = {
4539  .name = "ismv",
4540  .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
4541  .mime_type = "application/mp4",
4542  .extensions = "ismv,isma",
4543  .priv_data_size = sizeof(MOVMuxContext),
4544  .audio_codec = AV_CODEC_ID_AAC,
4545  .video_codec = AV_CODEC_ID_H264,
4550  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
4551  .priv_class = &ismv_muxer_class,
4552 };
4553 #endif
4554 #if CONFIG_F4V_MUXER
4555 MOV_CLASS(f4v)
4556 AVOutputFormat ff_f4v_muxer = {
4557  .name = "f4v",
4558  .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
4559  .mime_type = "application/f4v",
4560  .extensions = "f4v",
4561  .priv_data_size = sizeof(MOVMuxContext),
4562  .audio_codec = AV_CODEC_ID_AAC,
4563  .video_codec = AV_CODEC_ID_H264,
4568  .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
4569  .priv_class = &f4v_muxer_class,
4570 };
4571 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1137
#define MOV_TRACK_STPS
Definition: movenc.h:89
static int utf8len(const uint8_t *b)
Definition: movenc.c:87
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
This function writes extradata "as is".
Definition: movenc.c:289
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:78
int ff_mov_iso639_to_lang(const char lang[4], int mp4)
Definition: isom.c:355
internal header for HEVC (de)muxer utilities
#define FF_MOV_FLAG_DELAY_MOOV
Definition: movenc.h:195
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1270
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:392
static const struct @142 mov_pix_fmt_tags[]
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:182
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
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3540
static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov, int tracks, int moof_size)
Definition: movenc.c:2802
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition: movenc.c:2163
Bytestream IO Context.
Definition: avio.h:104
int packet_entry
Definition: movenc.h:140
#define FF_MOV_FLAG_FASTSTART
Definition: movenc.h:189
static int get_cluster_duration(MOVTrack *track, int cluster_idx)
Definition: movenc.c:576
static int mov_write_dinf_tag(AVIOContext *pb)
Definition: movenc.c:1282
int ff_interleaved_peek(AVFormatContext *s, int stream, AVPacket *pkt, int add_offset)
Find the next packet in the interleaving queue for the given stream.
Definition: mux.c:619
Buffered I/O operations.
int use_editlist
Definition: movenc.h:177
int size
#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:270
static int mov_flush_fragment(AVFormatContext *s, int force)
Definition: movenc.c:3270
int tag
stsd fourcc
Definition: movenc.h:94
int language
Definition: movenc.h:92
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1155
AVOption.
Definition: opt.h:234
static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2750
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1213
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:241
static void mov_write_psp_udta_tag(AVIOContext *pb, const char *str, const char *lang, int type)
Definition: movenc.c:2283
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:186
unsigned int entries
Definition: movenc.h:50
AVIOContext * mdat_buf
Definition: movenc.h:167
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:40
static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1132
Definition: isom.h:45
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
static void param_write_int(AVIOContext *pb, const char *name, int value)
Definition: movenc.c:2392
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
#define FF_MOV_FLAG_FRAGMENT
Definition: movenc.h:183
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:61
int nb_frag_info
Definition: movenc.h:130
int max_fragment_size
Definition: movenc.h:165
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:2986
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:187
static int mov_write_dref_tag(AVIOContext *pb)
Definition: movenc.c:1246
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:1145
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int long_style)
Definition: movenc.c:2033
static const AVCodecTag codec_f4v_tags[]
Definition: movenc.c:938
AVRational sample_aspect_ratio
Video only.
Definition: avcodec.h:3535
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
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:873
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
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:112
int index
stream index in AVFormatContext
Definition: avformat.h:706
int size
Definition: avcodec.h:1347
int flag
Definition: cpu.c:35
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:197
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
#define AVIO_FLAG_READ
read-only
Definition: avio.h:368
#define av_bswap16
Definition: bswap.h:31
#define AV_RB24
Definition: intreadwrite.h:64
This side data contains an integer value representing the stream index of a "fallback" track...
Definition: avcodec.h:1281
uint8_t * av_stream_get_side_data(AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:3265
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:85
int end_reliable
Definition: movenc.h:109
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par)
Definition: riffenc.c:50
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1019
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:1973
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:2071
#define FF_ARRAY_ELEMS(a)
#define FF_MOV_FLAG_ISML
Definition: movenc.h:188
static int mov_write_track_metadata(AVIOContext *pb, AVStream *st, const char *tag, const char *str)
Definition: movenc.c:1752
#define MOV_TFHD_DURATION_IS_EMPTY
Definition: isom.h:189
AVStream * st
Definition: movenc.h:95
AVDictionary * metadata
Definition: avformat.h:927
static uint16_t language_code(const char *str)
Definition: movenc.c:2174
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:429
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1143
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1211
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
int min_bitrate
Minimum bitrate of the stream, in bits per second.
Definition: avcodec.h:1150
AVCodecParameters * par
Definition: movenc.h:96
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition: movenc.c:2646
int64_t track_duration
Definition: movenc.h:84
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:190
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:202
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:539
int entries_flushed
Definition: movenc.h:128
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:209
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:110
static int shift_data(AVFormatContext *s)
Definition: movenc.c:4287
Trailer data, which doesn&#39;t contain actual content, but only for finalizing the output file...
Definition: avio.h:89
Format I/O context.
Definition: avformat.h:940
int64_t frag_start
Definition: movenc.h:126
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:175
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:3712
static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:989
static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks, int size)
Definition: movenc.c:2696
Public dictionary API.
int first_trun
Definition: movenc.h:168
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2296
int64_t default_duration
Definition: movenc.h:118
#define READ_BLOCK
uint32_t flags
Definition: movenc.h:91
#define FF_MOV_FLAG_EMPTY_MOOV
Definition: movenc.h:184
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:316
uint8_t
Round toward +infinity.
Definition: mathematics.h:53
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
Definition: movenc.c:1713
unsigned frag_info_capacity
Definition: movenc.h:132
Opaque data information usually continuous.
Definition: avutil.h:196
int missing_duration_warned
Definition: movenc.h:179
int width
Video only.
Definition: avcodec.h:3525
uint64_t mdat_size
Definition: movenc.h:152
unsigned int samples_in_chunk
Definition: movenc.h:49
#define MOV_CLASS(flavor)
Definition: movenc.c:79
AVOptions.
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:2798
static int get_samples_per_packet(MOVTrack *track)
Definition: movenc.c:591
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:70
int frag_interleave
Definition: movenc.h:178
#define AV_RB32
Definition: intreadwrite.h:130
static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:889
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: avcodec.h:1254
static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3086
#define b
Definition: input.c:52
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
A wrapper around AVFormatContext.io_close that should be used instead of calling the pointer directly...
Definition: utils.c:3317
static const AVOption options[]
Definition: movenc.c:47
#define MODE_PSP
Definition: movenc.h:38
static int mov_write_ms_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:362
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:422
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1600
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition: movenc.c:3774
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int64_t duration
Definition: movenc.c:63
static int mov_write_vmhd_tag(AVIOContext *pb)
Definition: movenc.c:1325
#define MODE_MP4
Definition: movenc.h:35
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:109
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:227
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:77
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:91
static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov, int tracks, int ref_size)
Definition: movenc.c:2886
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2333
double strtod(const char *, char **)
const char data[16]
Definition: mxf.c:70
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
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
int chapter_track
qt chapter track number
Definition: movenc.h:150
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1051
uint8_t * data
Definition: avcodec.h:1346
#define FF_MOV_FLAG_OMIT_TFHD_OFFSET
Definition: movenc.h:190
static int flags
Definition: log.c:50
int64_t time
Definition: movenc.h:73
static void mov_free(AVFormatContext *s)
Definition: movenc.c:3877
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:195
uint32_t tag
Definition: movenc.c:854
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1958
int fragments
Definition: movenc.h:162
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1002
int iods_audio_profile
Definition: movenc.h:159
bitstream reader API header.
#define MOV_TIMESCALE
Definition: movenc.h:31
int max_fragment_duration
Definition: movenc.h:163
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:77
static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks, int64_t mdat_size)
Definition: movenc.c:2921
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:1161
int64_t mdat_pos
Definition: movenc.h:151
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
void ff_mov_close_hinting(MOVTrack *track)
Definition: movenchint.c:458
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
Write uuid atom.
Definition: movenc.c:973
unsigned int size
Definition: movenc.h:48
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:66
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1068
#define FF_MOV_FLAG_DASH
Definition: movenc.h:193
static const uint16_t fiel_data[]
Definition: movenc.c:985
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:187
static int mov_write_nmhd_tag(AVIOContext *pb)
Definition: movenc.c:1291
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:264
int ism_lookahead
Definition: movenc.h:166
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:959
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
#define MODE_3G2
Definition: movenc.h:40
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:947
static int compute_sidx_size(AVFormatContext *s)
Definition: movenc.c:4272
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:99
uint32_t tref_tag
Definition: movenc.h:104
uint32_t flags
Definition: movenc.h:54
static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
Definition: movenc.c:3184
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1731
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:168
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2348
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2149
int64_t tfrf_offset
Definition: movenc.h:75
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO
Definition: isom.h:206
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2999
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:139
int64_t duration
Definition: movenc.h:74
static void put_descr(AVIOContext *pb, int tag, unsigned int size)
Definition: movenc.c:295
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2118
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:732
#define AVERROR(e)
Definition: error.h:43
static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
Definition: movenc.c:440
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
#define MODE_3GP
Definition: movenc.h:37
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:92
g
Definition: yuv2rgb.c:546
AVStream * video_st
Definition: movenc.c:59
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVChapter ** chapters
Definition: avformat.h:1138
static AVDictionaryEntry * get_metadata_lang(AVFormatContext *s, const char *tag, int *lang)
Definition: movenc.c:2008
static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int moof_size, int first, int end)
Definition: movenc.c:2572
static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track, AVStream *st)
Definition: movenc.c:3914
static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2411
static av_always_inline av_const double round(double x)
Definition: libm.h:151
int height
active picture (w/o VBI) height for D-10/IMX
Definition: movenc.h:103
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVStream *st)
Definition: movenc.c:1766
#define FF_MOV_FLAG_SEPARATE_MOOF
Definition: movenc.h:186
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:781
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:374
#define FFMAX(a, b)
Definition: common.h:64
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
void avcodec_parameters_free(AVCodecParameters **par)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: utils.c:2808
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:89
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:207
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:80
const AVCodecTag ff_mp4_obj_type[]
Definition: isom.c:34
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:323
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:90
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:63
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 bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
int vos_len
Definition: movenc.h:98
int iods_skip
Definition: movenc.h:157
static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:3244
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition: avc.c:70
static int mov_write_identification(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3149
#define LIBAVFORMAT_IDENT
Definition: version.h:44
int block_align
Audio only.
Definition: avcodec.h:3571
#define FF_MOV_FLAG_FRAG_DISCONT
Definition: movenc.h:194
static int mov_write_mdia_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1491
uint64_t pos
Definition: movenc.h:46
int first_frag_written
Definition: movenc.h:138
int64_t dts
Definition: movenc.h:47
const char * name
Definition: qsvenc.c:44
#define MOV_FRAG_INFO_ALLOC_INCREMENT
Definition: movenc.h:29
#define MOV_TRACK_ENABLED
Definition: movenc.h:90
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1201
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2096
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:236
int per_stream_grouping
Definition: movenc.h:174
int64_t data_offset
Definition: movenc.h:125
char filename[1024]
input or output filename
Definition: avformat.h:1016
static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset)
Definition: movenc.c:2512
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:86
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
#define FFMIN(a, b)
Definition: common.h:66
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1880
int audio_vbr
Definition: movenc.h:102
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2203
#define MOV_TKHD_FLAG_IN_MOVIE
Definition: isom.h:210
enum AVCodecID codec_id
Definition: avconv_vaapi.c:149
#define MOV_PARTIAL_SYNC_SAMPLE
Definition: movenc.h:53
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:3072
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:711
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:419
static int is_clcp_track(MOVTrack *track)
Definition: movenc.c:1334
static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:1793
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:416
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:450
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:827
long sample_size
Definition: movenc.h:86
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style)
Definition: movenc.c:1994
int frag_discont
Definition: movenc.h:127
static int mov_write_smhd_tag(AVIOContext *pb)
Definition: movenc.c:1315
static int mov_auto_flush_fragment(AVFormatContext *s, int force)
Definition: movenc.c:3481
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1234
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:117
#define AV_RL32
Definition: intreadwrite.h:146
static void param_write_string(AVIOContext *pb, const char *name, const char *value)
Definition: movenc.c:2397
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:194
AVDictionary * metadata
Definition: avformat.h:772
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1425
static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:721
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:193
static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1632
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1867
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:198
#define MOV_TRACK_CTTS
Definition: movenc.h:88
static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2497
int iods_video_profile
Definition: movenc.h:158
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:705
static int get_sidx_size(AVFormatContext *s)
Definition: movenc.c:4229
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:926
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:1140
NULL
Definition: eval.c:55
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:672
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:304
int64_t end_pts
Definition: movenc.h:108
#define MOV_INDEX_CLUSTER_SIZE
Definition: movenc.h:30
static int mov_write_d263_tag(AVIOContext *pb)
Definition: movenc.c:686
version
Definition: ffv1enc.c:1091
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
Definition: vc1_common.h:72
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:271
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1165
int duration
Definition: isom.h:47
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
int cts
Definition: movenc.h:51
AVIOContext * pb
I/O context.
Definition: avformat.h:982
static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
Definition: movenc.c:2736
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition: rtpenc.h:74
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:200
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
static uint32_t rgb_to_yuv(uint32_t rgb)
Definition: movenc.c:3898
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:193
#define FF_MOV_FLAG_FRAG_CUSTOM
Definition: movenc.h:187
int hint_track
the track that hints this track, -1 if no hint track is set
Definition: movenc.h:111
static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1032
static int mov_write_hmhd_tag(AVIOContext *pb)
Definition: movenc.c:1410
int slices
Definition: movenc.h:141
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition: movenc.c:3163
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:60
#define FF_MOV_FLAG_GLOBAL_SIDX
Definition: movenc.h:196
static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:523
uint32_t default_size
Definition: movenc.h:120
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
#define FF_MOV_FLAG_RTP_HINT
Definition: movenc.h:182
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2608
int count
Definition: isom.h:46
AVFormatContext * fc
Definition: movenc.h:175
Y , 16bpp, big-endian.
Definition: pixfmt.h:94
int index
Definition: gxfenc.c:72
static int mov_write_wfex_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:372
static int co64_required(const MOVTrack *track)
Definition: movenc.c:109
rational number numerator/denominator
Definition: rational.h:43
uint64_t time
Definition: movenc.h:83
int first_packet_seq
Definition: movenc.h:135
#define FF_MOV_FLAG_SKIP_TRAILER
Definition: movenc.h:197
static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
Definition: movenc.c:2402
static int mov_write_stsd_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1145
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
int64_t time
Definition: movenc.h:148
int has_keyframes
Definition: movenc.h:87
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1203
int entry
Definition: movenc.h:81
static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:381
static int mov_write_loci_tag(AVFormatContext *s, AVIOContext *pb)
Definition: movenc.c:2045
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:188
#define AVFMT_AVOID_NEG_TS_AUTO
Enabled when required by target format.
Definition: avformat.h:1235
MOVFragmentInfo * frag_info
Definition: movenc.h:131
char * major_brand
Definition: movenc.h:172
int64_t start_cts
Definition: movenc.h:107
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:196
uint8_t * vos_data
Definition: movenc.h:99
int first_packet_entry
Definition: movenc.h:136
Round toward -infinity.
Definition: mathematics.h:52
static const AVCodecTag codec_3gp_tags[]
Definition: movenc.c:927
static int mov_write_stbl_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1260
mfxU16 profile
Definition: qsvenc.c:43
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:1503
static int mov_write_hdlr_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1340
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:404
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:779
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:111
AVFormatContext * rtp_ctx
the format context for the hinting rtp muxer
Definition: movenc.h:113
uint8_t level
Definition: svq3.c:204
int track_id
Definition: movenc.h:93
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2229
static int mov_write_trailer(AVFormatContext *s)
Definition: movenc.c:4353
int height
Definition: gxfenc.c:72
int64_t start
Definition: avformat.h:926
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define FF_MOV_FLAG_FRAG_KEYFRAME
Definition: movenc.h:185
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO
Shift timestamps so that they start at 0.
Definition: avformat.h:1237
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, const char *tag, const char *str)
Definition: movenc.c:2181
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1891
Main libavformat public API header.
static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2682
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
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:810
int packet_seq
Definition: movenc.h:139
static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2949
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:184
static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
Definition: movenc.c:2506
struct MOVTrack::@143 vc1_info
#define MOV_SYNC_SAMPLE
Definition: movenc.h:52
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2625
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:106
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:761
uint32_t max_packet_size
Definition: movenc.h:116
static int mov_write_sidx_tag(AVIOContext *pb, MOVTrack *track, int ref_size, int total_sidx_size)
Definition: movenc.c:2825
static int mov_write_gmhd_tag(AVIOContext *pb)
Definition: movenc.c:1299
int src_track
the track that this hint track describes
Definition: movenc.h:112
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:925
static int mov_write_wave_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:406
static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset, int moof_size)
Definition: movenc.c:2762
char * key
Definition: dict.h:73
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
denominator
Definition: rational.h:45
MOVIentry * cluster
Definition: movenc.h:100
unsigned bps
Definition: movenc.c:855
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
#define MODE_IPOD
Definition: movenc.h:41
#define FF_MOV_FLAG_DISABLE_CHPL
Definition: movenc.h:191
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1838
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:71
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
Definition: movenc.c:204
char * value
Definition: dict.h:74
uint32_t default_sample_flags
Definition: movenc.h:119
unsigned timescale
Definition: movenc.h:82
int len
#define FF_MOV_FLAG_DEFAULT_BASE_MOOF
Definition: movenc.h:192
static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2973
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:1155
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
Definition: movenc.c:1974
void * priv_data
Format private data.
Definition: avformat.h:968
static int mov_write_header(AVFormatContext *s)
Definition: movenc.c:3963
This side data corresponds to the AVCPBProperties struct.
Definition: avcodec.h:1286
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
int64_t reserved_header_pos
Definition: movenc.h:170
static int64_t update_size(AVIOContext *pb, int64_t pos)
Definition: movenc.c:99
int bits_per_coded_sample
Definition: avcodec.h:3514
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:192
int64_t start_dts
Definition: movenc.h:106
static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1452
int channels
Audio only.
Definition: avcodec.h:3560
#define LIBAVCODEC_IDENT
Definition: version.h:42
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
int tref_id
trackID of the referenced track
Definition: movenc.h:105
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3011
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
int first_packet_seen
Definition: movenc.h:137
static const AVCodecTag codec_ipod_tags[]
Definition: movenc.c:799
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:60
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1112
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1702
int moov_written
Definition: movenc.h:161
#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
AVCodecParameters * codecpar
Definition: avformat.h:831
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3487
unsigned cluster_capacity
Definition: movenc.h:101
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:3495
static int mov_get_lpcm_flags(enum AVCodecID codec_id)
Compute flags for &#39;lpcm&#39; tag.
Definition: movenc.c:551
int stream_index
Definition: avcodec.h:1348
#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:742
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
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:810
#define AV_WB16(p, val)
Definition: intreadwrite.h:218
static int mov_write_svq3_tag(AVIOContext *pb)
Definition: movenc.c:699
#define CONFIG_LIBX264_ENCODER
Definition: config.h:1168
#define MKTAG(a, b, c, d)
Definition: common.h:256
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:435
static void enable_tracks(AVFormatContext *s)
Definition: movenc.c:3836
#define MODE_ISM
Definition: movenc.h:42
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Definition: utils.c:2937
This structure stores compressed data.
Definition: avcodec.h:1323
int64_t offset
Definition: movenc.h:72
int min_fragment_duration
Definition: movenc.h:164
MOVTrack * tracks
Definition: movenc.h:153
static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:610
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_common.h:58
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:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
AVIOContext * mdat_buf
Definition: movenc.h:124
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:64
int nb_streams
Definition: movenc.h:149
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:200
static int compute_moov_size(AVFormatContext *s)
Definition: movenc.c:4247
static int get_moov_size(AVFormatContext *s)
Definition: movenc.c:4217
bitstream writer API