FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
8  *
9  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/ac3_parser.h"
35 #include "libavcodec/get_bits.h"
36 #include "libavcodec/put_bits.h"
37 #include "libavcodec/vc1_common.h"
38 #include "libavcodec/raw.h"
39 #include "internal.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/intfloat.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/libm.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/dict.h"
46 #include "libavutil/pixdesc.h"
47 #include "libavutil/timecode.h"
48 #include "libavutil/color_utils.h"
49 #include "hevc.h"
50 #include "rtpenc.h"
51 #include "mov_chan.h"
52 
53 static const AVOption options[] = {
54  { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
55  { "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" },
56  { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
57  { "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" },
58  { "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" },
59  { "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" },
60  { "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" },
61  { "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" },
62  { "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" },
63  { "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" },
64  { "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" },
65  { "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" },
66  { "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" },
67  { "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" },
68  { "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" },
69  { "write_colr", "Write colr atom (Experimental, may be renamed or changed, do not use from scripts)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_COLR}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
70  { "write_gama", "Write deprecated gama atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_GAMA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
71  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
72  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
73  { "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},
74  { "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},
75  { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
76  { "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},
77  { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
78  { "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},
79  { "video_track_timescale", "set timescale of all video tracks", offsetof(MOVMuxContext, video_track_timescale), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
80  { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
81  { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
82  { "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},
83  { "mov_gamma", "gamma value for gama atom", offsetof(MOVMuxContext, gamma), AV_OPT_TYPE_FLOAT, {.dbl = 0.0 }, 0.0, 10, AV_OPT_FLAG_ENCODING_PARAM},
84  { "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 },
85  { NULL },
86 };
87 
88 #define MOV_CLASS(flavor)\
89 static const AVClass flavor ## _muxer_class = {\
90  .class_name = #flavor " muxer",\
91  .item_name = av_default_item_name,\
92  .option = options,\
93  .version = LIBAVUTIL_VERSION_INT,\
94 };
95 
96 static int get_moov_size(AVFormatContext *s);
97 
98 static int utf8len(const uint8_t *b)
99 {
100  int len = 0;
101  int val;
102  while (*b) {
103  GET_UTF8(val, *b++, return -1;)
104  len++;
105  }
106  return len;
107 }
108 
109 //FIXME support 64 bit variant with wide placeholders
110 static int64_t update_size(AVIOContext *pb, int64_t pos)
111 {
112  int64_t curpos = avio_tell(pb);
113  avio_seek(pb, pos, SEEK_SET);
114  avio_wb32(pb, curpos - pos); /* rewrite size */
115  avio_seek(pb, curpos, SEEK_SET);
116 
117  return curpos - pos;
118 }
119 
120 static int co64_required(const MOVTrack *track)
121 {
122  if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
123  return 1;
124  return 0;
125 }
126 
127 /* Chunk offset atom */
128 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
129 {
130  int i;
131  int mode64 = co64_required(track); // use 32 bit size variant if possible
132  int64_t pos = avio_tell(pb);
133  avio_wb32(pb, 0); /* size */
134  if (mode64)
135  ffio_wfourcc(pb, "co64");
136  else
137  ffio_wfourcc(pb, "stco");
138  avio_wb32(pb, 0); /* version & flags */
139  avio_wb32(pb, track->chunkCount); /* entry count */
140  for (i = 0; i < track->entry; i++) {
141  if (!track->cluster[i].chunkNum)
142  continue;
143  if (mode64 == 1)
144  avio_wb64(pb, track->cluster[i].pos + track->data_offset);
145  else
146  avio_wb32(pb, track->cluster[i].pos + track->data_offset);
147  }
148  return update_size(pb, pos);
149 }
150 
151 /* Sample size atom */
152 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
153 {
154  int equalChunks = 1;
155  int i, j, entries = 0, tst = -1, oldtst = -1;
156 
157  int64_t pos = avio_tell(pb);
158  avio_wb32(pb, 0); /* size */
159  ffio_wfourcc(pb, "stsz");
160  avio_wb32(pb, 0); /* version & flags */
161 
162  for (i = 0; i < track->entry; i++) {
163  tst = track->cluster[i].size / track->cluster[i].entries;
164  if (oldtst != -1 && tst != oldtst)
165  equalChunks = 0;
166  oldtst = tst;
167  entries += track->cluster[i].entries;
168  }
169  if (equalChunks && track->entry) {
170  int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
171  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
172  avio_wb32(pb, sSize); // sample size
173  avio_wb32(pb, entries); // sample count
174  } else {
175  avio_wb32(pb, 0); // sample size
176  avio_wb32(pb, entries); // sample count
177  for (i = 0; i < track->entry; i++) {
178  for (j = 0; j < track->cluster[i].entries; j++) {
179  avio_wb32(pb, track->cluster[i].size /
180  track->cluster[i].entries);
181  }
182  }
183  }
184  return update_size(pb, pos);
185 }
186 
187 /* Sample to chunk atom */
188 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
189 {
190  int index = 0, oldval = -1, i;
191  int64_t entryPos, curpos;
192 
193  int64_t pos = avio_tell(pb);
194  avio_wb32(pb, 0); /* size */
195  ffio_wfourcc(pb, "stsc");
196  avio_wb32(pb, 0); // version & flags
197  entryPos = avio_tell(pb);
198  avio_wb32(pb, track->chunkCount); // entry count
199  for (i = 0; i < track->entry; i++) {
200  if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
201  avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
202  avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
203  avio_wb32(pb, 0x1); // sample description index
204  oldval = track->cluster[i].samples_in_chunk;
205  index++;
206  }
207  }
208  curpos = avio_tell(pb);
209  avio_seek(pb, entryPos, SEEK_SET);
210  avio_wb32(pb, index); // rewrite size
211  avio_seek(pb, curpos, SEEK_SET);
212 
213  return update_size(pb, pos);
214 }
215 
216 /* Sync sample atom */
217 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
218 {
219  int64_t curpos, entryPos;
220  int i, index = 0;
221  int64_t pos = avio_tell(pb);
222  avio_wb32(pb, 0); // size
223  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
224  avio_wb32(pb, 0); // version & flags
225  entryPos = avio_tell(pb);
226  avio_wb32(pb, track->entry); // entry count
227  for (i = 0; i < track->entry; i++) {
228  if (track->cluster[i].flags & flag) {
229  avio_wb32(pb, i + 1);
230  index++;
231  }
232  }
233  curpos = avio_tell(pb);
234  avio_seek(pb, entryPos, SEEK_SET);
235  avio_wb32(pb, index); // rewrite size
236  avio_seek(pb, curpos, SEEK_SET);
237  return update_size(pb, pos);
238 }
239 
240 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
241 {
242  avio_wb32(pb, 0x11); /* size */
243  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
244  else ffio_wfourcc(pb, "damr");
245  ffio_wfourcc(pb, "FFMP");
246  avio_w8(pb, 0); /* decoder version */
247 
248  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
249  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
250  avio_w8(pb, 0x01); /* Frames per sample */
251  return 0x11;
252 }
253 
254 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
255 {
256  GetBitContext gbc;
257  PutBitContext pbc;
258  uint8_t buf[3];
259  int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
260 
261  if (track->vos_len < 7)
262  return -1;
263 
264  avio_wb32(pb, 11);
265  ffio_wfourcc(pb, "dac3");
266 
267  init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
268  fscod = get_bits(&gbc, 2);
269  frmsizecod = get_bits(&gbc, 6);
270  bsid = get_bits(&gbc, 5);
271  bsmod = get_bits(&gbc, 3);
272  acmod = get_bits(&gbc, 3);
273  if (acmod == 2) {
274  skip_bits(&gbc, 2); // dsurmod
275  } else {
276  if ((acmod & 1) && acmod != 1)
277  skip_bits(&gbc, 2); // cmixlev
278  if (acmod & 4)
279  skip_bits(&gbc, 2); // surmixlev
280  }
281  lfeon = get_bits1(&gbc);
282 
283  init_put_bits(&pbc, buf, sizeof(buf));
284  put_bits(&pbc, 2, fscod);
285  put_bits(&pbc, 5, bsid);
286  put_bits(&pbc, 3, bsmod);
287  put_bits(&pbc, 3, acmod);
288  put_bits(&pbc, 1, lfeon);
289  put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
290  put_bits(&pbc, 5, 0); // reserved
291 
292  flush_put_bits(&pbc);
293  avio_write(pb, buf, sizeof(buf));
294 
295  return 11;
296 }
297 
298 struct eac3_info {
302 
303  /* Layout of the EC3SpecificBox */
304  /* maximum bitrate */
305  uint16_t data_rate;
306  /* number of independent substreams */
308  struct {
309  /* sample rate code (see ff_ac3_sample_rate_tab) 2 bits */
311  /* bit stream identification 5 bits */
313  /* one bit reserved */
314  /* audio service mixing (not supported yet) 1 bit */
315  /* bit stream mode 3 bits */
317  /* audio coding mode 3 bits */
319  /* sub woofer on 1 bit */
321  /* 3 bits reserved */
322  /* number of dependent substreams associated with this substream 4 bits */
324  /* channel locations of the dependent substream(s), if any, 9 bits */
325  uint16_t chan_loc;
326  /* if there is no dependent substream, then one bit reserved instead */
327  } substream[1]; /* TODO: support 8 independent substreams */
328 };
329 
330 #if CONFIG_AC3_PARSER
331 static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
332 {
333  GetBitContext gbc;
334  AC3HeaderInfo tmp, *hdr = &tmp;
335  struct eac3_info *info;
336  int num_blocks;
337 
338  if (!track->eac3_priv && !(track->eac3_priv = av_mallocz(sizeof(*info))))
339  return AVERROR(ENOMEM);
340  info = track->eac3_priv;
341 
342  init_get_bits(&gbc, pkt->data, pkt->size * 8);
343  if (avpriv_ac3_parse_header2(&gbc, &hdr) < 0) {
344  /* drop the packets until we see a good one */
345  if (!track->entry) {
346  av_log(mov, AV_LOG_WARNING, "Dropping invalid packet from start of the stream\n");
347  return 0;
348  }
349  return AVERROR_INVALIDDATA;
350  }
351 
352  info->data_rate = FFMAX(info->data_rate, hdr->bit_rate / 1000);
353  num_blocks = hdr->num_blocks;
354 
355  if (!info->ec3_done) {
356  /* AC-3 substream must be the first one */
357  if (hdr->bitstream_id <= 10 && hdr->substreamid != 0)
358  return AVERROR(EINVAL);
359 
360  /* this should always be the case, given that our AC-3 parser
361  * concatenates dependent frames to their independent parent */
363  /* substream ids must be incremental */
364  if (hdr->substreamid > info->num_ind_sub + 1)
365  return AVERROR(EINVAL);
366 
367  if (hdr->substreamid == info->num_ind_sub + 1) {
368  //info->num_ind_sub++;
369  avpriv_request_sample(track->enc, "Multiple independent substreams");
370  return AVERROR_PATCHWELCOME;
371  } else if (hdr->substreamid < info->num_ind_sub ||
372  hdr->substreamid == 0 && info->substream[0].bsid) {
373  info->ec3_done = 1;
374  goto concatenate;
375  }
376  }
377 
378  /* fill the info needed for the "dec3" atom */
379  info->substream[hdr->substreamid].fscod = hdr->sr_code;
380  info->substream[hdr->substreamid].bsid = hdr->bitstream_id;
381  info->substream[hdr->substreamid].bsmod = hdr->bitstream_mode;
382  info->substream[hdr->substreamid].acmod = hdr->channel_mode;
383  info->substream[hdr->substreamid].lfeon = hdr->lfe_on;
384 
385  /* Parse dependent substream(s), if any */
386  if (pkt->size != hdr->frame_size) {
387  int cumul_size = hdr->frame_size;
388  int parent = hdr->substreamid;
389 
390  while (cumul_size != pkt->size) {
391  int i;
392  init_get_bits(&gbc, pkt->data + cumul_size, (pkt->size - cumul_size) * 8);
393  if (avpriv_ac3_parse_header2(&gbc, &hdr) < 0)
394  return AVERROR_INVALIDDATA;
396  return AVERROR(EINVAL);
397  cumul_size += hdr->frame_size;
398  info->substream[parent].num_dep_sub++;
399 
400  /* header is parsed up to lfeon, but custom channel map may be needed */
401  /* skip bsid */
402  skip_bits(&gbc, 5);
403  /* skip volume control params */
404  for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
405  skip_bits(&gbc, 5); // skip dialog normalization
406  if (get_bits1(&gbc)) {
407  skip_bits(&gbc, 8); // skip compression gain word
408  }
409  }
410  /* get the dependent stream channel map, if exists */
411  if (get_bits1(&gbc))
412  info->substream[parent].chan_loc |= (get_bits(&gbc, 16) >> 5) & 0x1f;
413  else
414  info->substream[parent].chan_loc |= hdr->channel_mode;
415  }
416  }
417  }
418 
419 concatenate:
420  if (!info->num_blocks && num_blocks == 6)
421  return pkt->size;
422  else if (info->num_blocks + num_blocks > 6)
423  return AVERROR_INVALIDDATA;
424 
425  if (!info->num_blocks) {
426  int ret;
427  if ((ret = av_copy_packet(&info->pkt, pkt)) < 0)
428  return ret;
429  info->num_blocks = num_blocks;
430  return 0;
431  } else {
432  int ret;
433  if ((ret = av_grow_packet(&info->pkt, pkt->size)) < 0)
434  return ret;
435  memcpy(info->pkt.data + info->pkt.size - pkt->size, pkt->data, pkt->size);
436  info->num_blocks += num_blocks;
437  info->pkt.duration += pkt->duration;
438  if ((ret = av_copy_packet_side_data(&info->pkt, pkt)) < 0)
439  return ret;
440  if (info->num_blocks != 6)
441  return 0;
442  av_free_packet(pkt);
443  if ((ret = av_copy_packet(pkt, &info->pkt)) < 0)
444  return ret;
445  av_free_packet(&info->pkt);
446  info->num_blocks = 0;
447  }
448 
449  return pkt->size;
450 }
451 #endif
452 
453 static int mov_write_eac3_tag(AVIOContext *pb, MOVTrack *track)
454 {
455  PutBitContext pbc;
456  uint8_t *buf;
457  struct eac3_info *info;
458  int size, i;
459 
460  if (!track->eac3_priv)
461  return AVERROR(EINVAL);
462 
463  info = track->eac3_priv;
464  size = 2 + 4 * (info->num_ind_sub + 1);
465  buf = av_malloc(size);
466  if (!buf) {
467  size = AVERROR(ENOMEM);
468  goto end;
469  }
470 
471  init_put_bits(&pbc, buf, size);
472  put_bits(&pbc, 13, info->data_rate);
473  put_bits(&pbc, 3, info->num_ind_sub);
474  for (i = 0; i <= info->num_ind_sub; i++) {
475  put_bits(&pbc, 2, info->substream[i].fscod);
476  put_bits(&pbc, 5, info->substream[i].bsid);
477  put_bits(&pbc, 1, 0); /* reserved */
478  put_bits(&pbc, 1, 0); /* asvc */
479  put_bits(&pbc, 3, info->substream[i].bsmod);
480  put_bits(&pbc, 3, info->substream[i].acmod);
481  put_bits(&pbc, 1, info->substream[i].lfeon);
482  put_bits(&pbc, 5, 0); /* reserved */
483  put_bits(&pbc, 4, info->substream[i].num_dep_sub);
484  if (!info->substream[i].num_dep_sub) {
485  put_bits(&pbc, 1, 0); /* reserved */
486  size--;
487  } else {
488  put_bits(&pbc, 9, info->substream[i].chan_loc);
489  }
490  }
491  flush_put_bits(&pbc);
492 
493  avio_wb32(pb, size + 8);
494  ffio_wfourcc(pb, "dec3");
495  avio_write(pb, buf, size);
496 
497  av_free(buf);
498 
499 end:
500  av_free_packet(&info->pkt);
501  av_freep(&track->eac3_priv);
502 
503  return size;
504 }
505 
506 /**
507  * This function writes extradata "as is".
508  * Extradata must be formatted like a valid atom (with size and tag).
509  */
511 {
512  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
513  return track->enc->extradata_size;
514 }
515 
517 {
518  avio_wb32(pb, 10);
519  ffio_wfourcc(pb, "enda");
520  avio_wb16(pb, 1); /* little endian */
521  return 10;
522 }
523 
525 {
526  avio_wb32(pb, 10);
527  ffio_wfourcc(pb, "enda");
528  avio_wb16(pb, 0); /* big endian */
529  return 10;
530 }
531 
532 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
533 {
534  int i = 3;
535  avio_w8(pb, tag);
536  for (; i > 0; i--)
537  avio_w8(pb, (size >> (7 * i)) | 0x80);
538  avio_w8(pb, size & 0x7F);
539 }
540 
541 static unsigned compute_avg_bitrate(MOVTrack *track)
542 {
543  uint64_t size = 0;
544  int i;
545  if (!track->track_duration)
546  return 0;
547  for (i = 0; i < track->entry; i++)
548  size += track->cluster[i].size;
549  return size * 8 * track->timescale / track->track_duration;
550 }
551 
552 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
553 {
554  int64_t pos = avio_tell(pb);
555  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
556  unsigned avg_bitrate;
557 
558  avio_wb32(pb, 0); // size
559  ffio_wfourcc(pb, "esds");
560  avio_wb32(pb, 0); // Version
561 
562  // ES descriptor
563  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
564  avio_wb16(pb, track->track_id);
565  avio_w8(pb, 0x00); // flags (= no flags)
566 
567  // DecoderConfig descriptor
568  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
569 
570  // Object type indication
571  if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
572  track->enc->codec_id == AV_CODEC_ID_MP3) &&
573  track->enc->sample_rate > 24000)
574  avio_w8(pb, 0x6B); // 11172-3
575  else
577 
578  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
579  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
580  if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
581  avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
582  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
583  avio_w8(pb, 0x15); // flags (= Audiostream)
584  else
585  avio_w8(pb, 0x11); // flags (= Visualstream)
586 
587  avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
588 
589  avg_bitrate = compute_avg_bitrate(track);
590  // maxbitrate (FIXME should be max rate in any 1 sec window)
591  avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
592  avio_wb32(pb, avg_bitrate);
593 
594  if (track->vos_len) {
595  // DecoderSpecific info descriptor
596  put_descr(pb, 0x05, track->vos_len);
597  avio_write(pb, track->vos_data, track->vos_len);
598  }
599 
600  // SL descriptor
601  put_descr(pb, 0x06, 1);
602  avio_w8(pb, 0x02);
603  return update_size(pb, pos);
604 }
605 
607 {
608  return codec_id == AV_CODEC_ID_PCM_S24LE ||
609  codec_id == AV_CODEC_ID_PCM_S32LE ||
610  codec_id == AV_CODEC_ID_PCM_F32LE ||
611  codec_id == AV_CODEC_ID_PCM_F64LE;
612 }
613 
615 {
616  return codec_id == AV_CODEC_ID_PCM_S24BE ||
617  codec_id == AV_CODEC_ID_PCM_S32BE ||
618  codec_id == AV_CODEC_ID_PCM_F32BE ||
619  codec_id == AV_CODEC_ID_PCM_F64BE;
620 }
621 
622 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
623 {
624  int ret;
625  int64_t pos = avio_tell(pb);
626  avio_wb32(pb, 0);
627  avio_wl32(pb, track->tag); // store it byteswapped
628  track->enc->codec_tag = av_bswap16(track->tag >> 16);
629  if ((ret = ff_put_wav_header(pb, track->enc, 0)) < 0)
630  return ret;
631  return update_size(pb, pos);
632 }
633 
634 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
635 {
636  int ret;
637  int64_t pos = avio_tell(pb);
638  avio_wb32(pb, 0);
639  ffio_wfourcc(pb, "wfex");
640  if ((ret = ff_put_wav_header(pb, track->enc, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX)) < 0)
641  return ret;
642  return update_size(pb, pos);
643 }
644 
645 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
646 {
647  uint32_t layout_tag, bitmap;
648  int64_t pos = avio_tell(pb);
649 
650  layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
651  track->enc->channel_layout,
652  &bitmap);
653  if (!layout_tag) {
654  av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
655  "lack of channel information\n");
656  return 0;
657  }
658 
659  if (track->multichannel_as_mono)
660  return 0;
661 
662  avio_wb32(pb, 0); // Size
663  ffio_wfourcc(pb, "chan"); // Type
664  avio_w8(pb, 0); // Version
665  avio_wb24(pb, 0); // Flags
666  avio_wb32(pb, layout_tag); // mChannelLayoutTag
667  avio_wb32(pb, bitmap); // mChannelBitmap
668  avio_wb32(pb, 0); // mNumberChannelDescriptions
669 
670  return update_size(pb, pos);
671 }
672 
673 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
674 {
675  int64_t pos = avio_tell(pb);
676 
677  avio_wb32(pb, 0); /* size */
678  ffio_wfourcc(pb, "wave");
679 
680  if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
681  avio_wb32(pb, 12); /* size */
682  ffio_wfourcc(pb, "frma");
683  avio_wl32(pb, track->tag);
684  }
685 
686  if (track->enc->codec_id == AV_CODEC_ID_AAC) {
687  /* useless atom needed by mplayer, ipod, not needed by quicktime */
688  avio_wb32(pb, 12); /* size */
689  ffio_wfourcc(pb, "mp4a");
690  avio_wb32(pb, 0);
691  mov_write_esds_tag(pb, track);
692  } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
693  mov_write_enda_tag(pb);
694  } else if (mov_pcm_be_gt16(track->enc->codec_id)) {
696  } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
697  mov_write_amr_tag(pb, track);
698  } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
699  mov_write_ac3_tag(pb, track);
700  } else if (track->enc->codec_id == AV_CODEC_ID_EAC3) {
701  mov_write_eac3_tag(pb, track);
702  } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
703  track->enc->codec_id == AV_CODEC_ID_QDM2) {
704  mov_write_extradata_tag(pb, track);
705  } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
707  mov_write_ms_tag(pb, track);
708  }
709 
710  avio_wb32(pb, 8); /* size */
711  avio_wb32(pb, 0); /* null tag */
712 
713  return update_size(pb, pos);
714 }
715 
717 {
718  uint8_t *unescaped;
719  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
720  int unescaped_size, seq_found = 0;
721  int level = 0, interlace = 0;
722  int packet_seq = track->vc1_info.packet_seq;
723  int packet_entry = track->vc1_info.packet_entry;
724  int slices = track->vc1_info.slices;
725  PutBitContext pbc;
726 
727  if (track->start_dts == AV_NOPTS_VALUE) {
728  /* No packets written yet, vc1_info isn't authoritative yet. */
729  /* Assume inline sequence and entry headers. */
730  packet_seq = packet_entry = 1;
732  "moov atom written before any packets, unable to write correct "
733  "dvc1 atom. Set the delay_moov flag to fix this.\n");
734  }
735 
736  unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
737  if (!unescaped)
738  return AVERROR(ENOMEM);
739  start = find_next_marker(track->vos_data, end);
740  for (next = start; next < end; start = next) {
741  GetBitContext gb;
742  int size;
743  next = find_next_marker(start + 4, end);
744  size = next - start - 4;
745  if (size <= 0)
746  continue;
747  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
748  init_get_bits(&gb, unescaped, 8 * unescaped_size);
749  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
750  int profile = get_bits(&gb, 2);
751  if (profile != PROFILE_ADVANCED) {
752  av_free(unescaped);
753  return AVERROR(ENOSYS);
754  }
755  seq_found = 1;
756  level = get_bits(&gb, 3);
757  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
758  * width, height */
759  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
760  skip_bits(&gb, 1); /* broadcast */
761  interlace = get_bits1(&gb);
762  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
763  }
764  }
765  if (!seq_found) {
766  av_free(unescaped);
767  return AVERROR(ENOSYS);
768  }
769 
770  init_put_bits(&pbc, buf, 7);
771  /* VC1DecSpecStruc */
772  put_bits(&pbc, 4, 12); /* profile - advanced */
773  put_bits(&pbc, 3, level);
774  put_bits(&pbc, 1, 0); /* reserved */
775  /* VC1AdvDecSpecStruc */
776  put_bits(&pbc, 3, level);
777  put_bits(&pbc, 1, 0); /* cbr */
778  put_bits(&pbc, 6, 0); /* reserved */
779  put_bits(&pbc, 1, !interlace); /* no interlace */
780  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
781  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
782  put_bits(&pbc, 1, !slices); /* no slice code */
783  put_bits(&pbc, 1, 0); /* no bframe */
784  put_bits(&pbc, 1, 0); /* reserved */
785 
786  /* framerate */
787  if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
788  put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
789  else
790  put_bits32(&pbc, 0xffffffff);
791 
792  flush_put_bits(&pbc);
793 
794  av_free(unescaped);
795 
796  return 0;
797 }
798 
799 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
800 {
801  uint8_t buf[7] = { 0 };
802  int ret;
803 
804  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
805  return ret;
806 
807  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
808  ffio_wfourcc(pb, "dvc1");
809  avio_write(pb, buf, sizeof(buf));
810  avio_write(pb, track->vos_data, track->vos_len);
811 
812  return 0;
813 }
814 
815 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
816 {
817  avio_wb32(pb, track->vos_len + 8);
818  ffio_wfourcc(pb, "glbl");
819  avio_write(pb, track->vos_data, track->vos_len);
820  return 8 + track->vos_len;
821 }
822 
823 /**
824  * Compute flags for 'lpcm' tag.
825  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
826  */
828 {
829  switch (codec_id) {
832  return 11;
835  return 9;
836  case AV_CODEC_ID_PCM_U8:
837  return 10;
841  return 14;
842  case AV_CODEC_ID_PCM_S8:
846  return 12;
847  default:
848  return 0;
849  }
850 }
851 
852 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
853 {
854  int64_t next_dts;
855 
856  if (cluster_idx >= track->entry)
857  return 0;
858 
859  if (cluster_idx + 1 == track->entry)
860  next_dts = track->track_duration + track->start_dts;
861  else
862  next_dts = track->cluster[cluster_idx + 1].dts;
863 
864  next_dts -= track->cluster[cluster_idx].dts;
865 
866  av_assert0(next_dts >= 0);
867  av_assert0(next_dts <= INT_MAX);
868 
869  return next_dts;
870 }
871 
873 {
874  int i, first_duration;
875 
876 // return track->enc->frame_size;
877 
878  /* use 1 for raw PCM */
879  if (!track->audio_vbr)
880  return 1;
881 
882  /* check to see if duration is constant for all clusters */
883  if (!track->entry)
884  return 0;
885  first_duration = get_cluster_duration(track, 0);
886  for (i = 1; i < track->entry; i++) {
887  if (get_cluster_duration(track, i) != first_duration)
888  return 0;
889  }
890  return first_duration;
891 }
892 
893 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
894 {
895  int64_t pos = avio_tell(pb);
896  int version = 0;
897  uint32_t tag = track->tag;
898 
899  if (track->mode == MODE_MOV) {
900  if (track->timescale > UINT16_MAX) {
901  if (mov_get_lpcm_flags(track->enc->codec_id))
902  tag = AV_RL32("lpcm");
903  version = 2;
904  } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
905  mov_pcm_be_gt16(track->enc->codec_id) ||
906  track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
908  track->enc->codec_id == AV_CODEC_ID_QDM2) {
909  version = 1;
910  }
911  }
912 
913  avio_wb32(pb, 0); /* size */
914  avio_wl32(pb, tag); // store it byteswapped
915  avio_wb32(pb, 0); /* Reserved */
916  avio_wb16(pb, 0); /* Reserved */
917  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
918 
919  /* SoundDescription */
920  avio_wb16(pb, version); /* Version */
921  avio_wb16(pb, 0); /* Revision level */
922  avio_wb32(pb, 0); /* Reserved */
923 
924  if (version == 2) {
925  avio_wb16(pb, 3);
926  avio_wb16(pb, 16);
927  avio_wb16(pb, 0xfffe);
928  avio_wb16(pb, 0);
929  avio_wb32(pb, 0x00010000);
930  avio_wb32(pb, 72);
931  avio_wb64(pb, av_double2int(track->enc->sample_rate));
932  avio_wb32(pb, track->enc->channels);
933  avio_wb32(pb, 0x7F000000);
936  avio_wb32(pb, track->sample_size);
937  avio_wb32(pb, get_samples_per_packet(track));
938  } else {
939  if (track->mode == MODE_MOV) {
940  avio_wb16(pb, track->enc->channels);
941  if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
942  track->enc->codec_id == AV_CODEC_ID_PCM_S8)
943  avio_wb16(pb, 8); /* bits per sample */
944  else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_G726)
945  avio_wb16(pb, track->enc->bits_per_coded_sample);
946  else
947  avio_wb16(pb, 16);
948  avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
949  } else { /* reserved for mp4/3gp */
950  avio_wb16(pb, 2);
951  avio_wb16(pb, 16);
952  avio_wb16(pb, 0);
953  }
954 
955  avio_wb16(pb, 0); /* packet size (= 0) */
956  avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
957  track->enc->sample_rate : 0);
958  avio_wb16(pb, 0); /* Reserved */
959  }
960 
961  if (version == 1) { /* SoundDescription V1 extended info */
962  if (mov_pcm_le_gt16(track->enc->codec_id) ||
963  mov_pcm_be_gt16(track->enc->codec_id))
964  avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
965  else
966  avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
967  avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
968  avio_wb32(pb, track->sample_size); /* Bytes per frame */
969  avio_wb32(pb, 2); /* Bytes per sample */
970  }
971 
972  if (track->mode == MODE_MOV &&
973  (track->enc->codec_id == AV_CODEC_ID_AAC ||
974  track->enc->codec_id == AV_CODEC_ID_AC3 ||
975  track->enc->codec_id == AV_CODEC_ID_EAC3 ||
976  track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
977  track->enc->codec_id == AV_CODEC_ID_ALAC ||
978  track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
980  track->enc->codec_id == AV_CODEC_ID_QDM2 ||
981  (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
982  (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
983  mov_write_wave_tag(pb, track);
984  else if (track->tag == MKTAG('m','p','4','a'))
985  mov_write_esds_tag(pb, track);
986  else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
987  mov_write_amr_tag(pb, track);
988  else if (track->enc->codec_id == AV_CODEC_ID_AC3)
989  mov_write_ac3_tag(pb, track);
990  else if (track->enc->codec_id == AV_CODEC_ID_EAC3)
991  mov_write_eac3_tag(pb, track);
992  else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
993  mov_write_extradata_tag(pb, track);
994  else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
995  mov_write_wfex_tag(pb, track);
996  else if (track->vos_len > 0)
997  mov_write_glbl_tag(pb, track);
998 
999  if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1000  mov_write_chan_tag(pb, track);
1001 
1002  return update_size(pb, pos);
1003 }
1004 
1006 {
1007  avio_wb32(pb, 0xf); /* size */
1008  ffio_wfourcc(pb, "d263");
1009  ffio_wfourcc(pb, "FFMP");
1010  avio_w8(pb, 0); /* decoder version */
1011  /* FIXME use AVCodecContext level/profile, when encoder will set values */
1012  avio_w8(pb, 0xa); /* level */
1013  avio_w8(pb, 0); /* profile */
1014  return 0xf;
1015 }
1016 
1017 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
1018 {
1019  int64_t pos = avio_tell(pb);
1020 
1021  avio_wb32(pb, 0);
1022  ffio_wfourcc(pb, "avcC");
1023  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
1024  return update_size(pb, pos);
1025 }
1026 
1027 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
1028 {
1029  int64_t pos = avio_tell(pb);
1030 
1031  avio_wb32(pb, 0);
1032  ffio_wfourcc(pb, "hvcC");
1033  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
1034  return update_size(pb, pos);
1035 }
1036 
1037 /* also used by all avid codecs (dv, imx, meridien) and their variants */
1038 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
1039 {
1040  int i;
1041  int interlaced;
1042  int cid;
1043 
1044  if (track->vos_data && track->vos_len > 0x29) {
1045  if (track->vos_data[0] == 0x00 &&
1046  track->vos_data[1] == 0x00 &&
1047  track->vos_data[2] == 0x02 &&
1048  track->vos_data[3] == 0x80 &&
1049  (track->vos_data[4] == 0x01 || track->vos_data[4] == 0x02)) {
1050  /* looks like a DNxHD bit stream */
1051  interlaced = (track->vos_data[5] & 2);
1052  cid = AV_RB32(track->vos_data + 0x28);
1053  } else {
1054  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1055  return 0;
1056  }
1057  } else {
1058  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1059  return 0;
1060  }
1061 
1062  avio_wb32(pb, 24); /* size */
1063  ffio_wfourcc(pb, "ACLR");
1064  ffio_wfourcc(pb, "ACLR");
1065  ffio_wfourcc(pb, "0001");
1066  if (track->enc->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1068  avio_wb32(pb, 1); /* Corresponds to 709 in official encoder */
1069  } else { /* Full range (0-255) */
1070  avio_wb32(pb, 2); /* Corresponds to RGB in official encoder */
1071  }
1072  avio_wb32(pb, 0); /* unknown */
1073 
1074  avio_wb32(pb, 24); /* size */
1075  ffio_wfourcc(pb, "APRG");
1076  ffio_wfourcc(pb, "APRG");
1077  ffio_wfourcc(pb, "0001");
1078  avio_wb32(pb, 1); /* unknown */
1079  avio_wb32(pb, 0); /* unknown */
1080 
1081  avio_wb32(pb, 120); /* size */
1082  ffio_wfourcc(pb, "ARES");
1083  ffio_wfourcc(pb, "ARES");
1084  ffio_wfourcc(pb, "0001");
1085  avio_wb32(pb, cid); /* dnxhd cid, some id ? */
1086  avio_wb32(pb, track->enc->width);
1087  /* values below are based on samples created with quicktime and avid codecs */
1088  if (interlaced) {
1089  avio_wb32(pb, track->enc->height / 2);
1090  avio_wb32(pb, 2); /* unknown */
1091  avio_wb32(pb, 0); /* unknown */
1092  avio_wb32(pb, 4); /* unknown */
1093  } else {
1094  avio_wb32(pb, track->enc->height);
1095  avio_wb32(pb, 1); /* unknown */
1096  avio_wb32(pb, 0); /* unknown */
1097  if (track->enc->height == 1080)
1098  avio_wb32(pb, 5); /* unknown */
1099  else
1100  avio_wb32(pb, 6); /* unknown */
1101  }
1102  /* padding */
1103  for (i = 0; i < 10; i++)
1104  avio_wb64(pb, 0);
1105 
1106  return 0;
1107 }
1108 
1109 static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
1110 {
1111  avio_wb32(pb, 12);
1112  ffio_wfourcc(pb, "DpxE");
1113  if (track->enc->extradata_size >= 12 &&
1114  !memcmp(&track->enc->extradata[4], "DpxE", 4)) {
1115  avio_wb32(pb, track->enc->extradata[11]);
1116  } else {
1117  avio_wb32(pb, 1);
1118  }
1119  return 0;
1120 }
1121 
1123 {
1124  int tag = track->enc->codec_tag;
1125 
1127  return 0;
1128 
1129  if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
1130  else if (track->enc->codec_id == AV_CODEC_ID_HEVC) tag = MKTAG('h','e','v','1');
1131  else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
1132  else if (track->enc->codec_id == AV_CODEC_ID_EAC3) tag = MKTAG('e','c','-','3');
1133  else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
1134  else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
1135  else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
1136  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
1137  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
1138  else if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE) tag = MKTAG('m','p','4','s');
1139 
1140  return tag;
1141 }
1142 
1143 static const AVCodecTag codec_ipod_tags[] = {
1144  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
1145  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
1146  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
1147  { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
1148  { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
1149  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1150  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
1151  { AV_CODEC_ID_NONE, 0 },
1152 };
1153 
1155 {
1156  int tag = track->enc->codec_tag;
1157 
1158  // keep original tag for subs, ipod supports both formats
1159  if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1160  (tag == MKTAG('t', 'x', '3', 'g') ||
1161  tag == MKTAG('t', 'e', 'x', 't'))))
1162  tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
1163 
1164  if (!av_match_ext(s->filename, "m4a") &&
1165  !av_match_ext(s->filename, "m4b") &&
1166  !av_match_ext(s->filename, "m4v"))
1167  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a, .m4v nor .m4b "
1168  "Quicktime/Ipod might not play the file\n");
1169 
1170  return tag;
1171 }
1172 
1174 {
1175  int tag;
1176 
1177  if (track->enc->width == 720) { /* SD */
1178  if (track->enc->height == 480) { /* NTSC */
1179  if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1180  else tag = MKTAG('d','v','c',' ');
1181  }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1182  else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1183  else tag = MKTAG('d','v','p','p');
1184  } else if (track->enc->height == 720) { /* HD 720 line */
1185  if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
1186  else tag = MKTAG('d','v','h','p');
1187  } else if (track->enc->height == 1080) { /* HD 1080 line */
1188  if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
1189  else tag = MKTAG('d','v','h','6');
1190  } else {
1191  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1192  return 0;
1193  }
1194 
1195  return tag;
1196 }
1197 
1199 {
1200  AVRational rate = {st->codec->time_base.den, st->codec->time_base.num};
1201  /* if the codec time base makes no sense, try to fallback on stream frame rate */
1202  if (av_timecode_check_frame_rate(rate) < 0) {
1203  av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
1204  rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
1205  rate = st->avg_frame_rate;
1206  }
1207 
1208  return rate;
1209 }
1210 
1212 {
1213  int tag = track->enc->codec_tag;
1215  AVStream *st = track->st;
1216  int rate = av_q2d(find_fps(s, st));
1217 
1218  if (!tag)
1219  tag = MKTAG('m', '2', 'v', '1'); //fallback tag
1220 
1221  if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) {
1222  if (track->enc->width == 1280 && track->enc->height == 720) {
1223  if (!interlaced) {
1224  if (rate == 24) tag = MKTAG('x','d','v','4');
1225  else if (rate == 25) tag = MKTAG('x','d','v','5');
1226  else if (rate == 30) tag = MKTAG('x','d','v','1');
1227  else if (rate == 50) tag = MKTAG('x','d','v','a');
1228  else if (rate == 60) tag = MKTAG('x','d','v','9');
1229  }
1230  } else if (track->enc->width == 1440 && track->enc->height == 1080) {
1231  if (!interlaced) {
1232  if (rate == 24) tag = MKTAG('x','d','v','6');
1233  else if (rate == 25) tag = MKTAG('x','d','v','7');
1234  else if (rate == 30) tag = MKTAG('x','d','v','8');
1235  } else {
1236  if (rate == 25) tag = MKTAG('x','d','v','3');
1237  else if (rate == 30) tag = MKTAG('x','d','v','2');
1238  }
1239  } else if (track->enc->width == 1920 && track->enc->height == 1080) {
1240  if (!interlaced) {
1241  if (rate == 24) tag = MKTAG('x','d','v','d');
1242  else if (rate == 25) tag = MKTAG('x','d','v','e');
1243  else if (rate == 30) tag = MKTAG('x','d','v','f');
1244  } else {
1245  if (rate == 25) tag = MKTAG('x','d','v','c');
1246  else if (rate == 30) tag = MKTAG('x','d','v','b');
1247  }
1248  }
1249  } else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
1250  if (track->enc->width == 1280 && track->enc->height == 720) {
1251  if (!interlaced) {
1252  if (rate == 24) tag = MKTAG('x','d','5','4');
1253  else if (rate == 25) tag = MKTAG('x','d','5','5');
1254  else if (rate == 30) tag = MKTAG('x','d','5','1');
1255  else if (rate == 50) tag = MKTAG('x','d','5','a');
1256  else if (rate == 60) tag = MKTAG('x','d','5','9');
1257  }
1258  } else if (track->enc->width == 1920 && track->enc->height == 1080) {
1259  if (!interlaced) {
1260  if (rate == 24) tag = MKTAG('x','d','5','d');
1261  else if (rate == 25) tag = MKTAG('x','d','5','e');
1262  else if (rate == 30) tag = MKTAG('x','d','5','f');
1263  } else {
1264  if (rate == 25) tag = MKTAG('x','d','5','c');
1265  else if (rate == 30) tag = MKTAG('x','d','5','b');
1266  }
1267  }
1268  }
1269 
1270  return tag;
1271 }
1272 
1274 {
1275  int tag = track->enc->codec_tag;
1277  AVStream *st = track->st;
1278  int rate = av_q2d(find_fps(s, st));
1279 
1280  if (!tag)
1281  tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
1282 
1283  if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P10) {
1284  if (track->enc->width == 960 && track->enc->height == 720) {
1285  if (!interlaced) {
1286  if (rate == 24) tag = MKTAG('a','i','5','p');
1287  else if (rate == 25) tag = MKTAG('a','i','5','q');
1288  else if (rate == 30) tag = MKTAG('a','i','5','p');
1289  else if (rate == 50) tag = MKTAG('a','i','5','q');
1290  else if (rate == 60) tag = MKTAG('a','i','5','p');
1291  }
1292  } else if (track->enc->width == 1440 && track->enc->height == 1080) {
1293  if (!interlaced) {
1294  if (rate == 24) tag = MKTAG('a','i','5','3');
1295  else if (rate == 25) tag = MKTAG('a','i','5','2');
1296  else if (rate == 30) tag = MKTAG('a','i','5','3');
1297  } else {
1298  if (rate == 50) tag = MKTAG('a','i','5','5');
1299  else if (rate == 60) tag = MKTAG('a','i','5','6');
1300  }
1301  }
1302  } else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P10) {
1303  if (track->enc->width == 1280 && track->enc->height == 720) {
1304  if (!interlaced) {
1305  if (rate == 24) tag = MKTAG('a','i','1','p');
1306  else if (rate == 25) tag = MKTAG('a','i','1','q');
1307  else if (rate == 30) tag = MKTAG('a','i','1','p');
1308  else if (rate == 50) tag = MKTAG('a','i','1','q');
1309  else if (rate == 60) tag = MKTAG('a','i','1','p');
1310  }
1311  } else if (track->enc->width == 1920 && track->enc->height == 1080) {
1312  if (!interlaced) {
1313  if (rate == 24) tag = MKTAG('a','i','1','3');
1314  else if (rate == 25) tag = MKTAG('a','i','1','2');
1315  else if (rate == 30) tag = MKTAG('a','i','1','3');
1316  } else {
1317  if (rate == 25) tag = MKTAG('a','i','1','5');
1318  else if (rate == 50) tag = MKTAG('a','i','1','5');
1319  else if (rate == 60) tag = MKTAG('a','i','1','6');
1320  }
1321  } else if ( track->enc->width == 4096 && track->enc->height == 2160
1322  || track->enc->width == 3840 && track->enc->height == 2160
1323  || track->enc->width == 2048 && track->enc->height == 1080) {
1324  tag = MKTAG('a','i','v','x');
1325  }
1326  }
1327 
1328  return tag;
1329 }
1330 
1331 static const struct {
1333  uint32_t tag;
1334  unsigned bps;
1335 } mov_pix_fmt_tags[] = {
1336  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
1337  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
1338  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
1339  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
1340  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
1341  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
1342  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
1343  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
1344  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
1345  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
1346  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
1347  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
1348  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
1349  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
1350  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1351 };
1352 
1354 {
1355  int tag = track->enc->codec_tag;
1356  int i;
1357  enum AVPixelFormat pix_fmt;
1358 
1359  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1360  if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
1361  tag = mov_pix_fmt_tags[i].tag;
1362  track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
1363  if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
1364  break;
1365  }
1366  }
1367 
1369  track->enc->bits_per_coded_sample);
1370  if (tag == MKTAG('r','a','w',' ') &&
1371  track->enc->pix_fmt != pix_fmt &&
1372  track->enc->pix_fmt != AV_PIX_FMT_NONE)
1373  av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
1374  av_get_pix_fmt_name(track->enc->pix_fmt));
1375  return tag;
1376 }
1377 
1379 {
1380  int tag = track->enc->codec_tag;
1381 
1382  if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1383  (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
1384  track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
1385  track->enc->codec_id == AV_CODEC_ID_H263 ||
1386  track->enc->codec_id == AV_CODEC_ID_H264 ||
1387  track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1388  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
1389  if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
1390  tag = mov_get_dv_codec_tag(s, track);
1391  else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
1392  tag = mov_get_rawvideo_codec_tag(s, track);
1393  else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1394  tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1395  else if (track->enc->codec_id == AV_CODEC_ID_H264)
1396  tag = mov_get_h264_codec_tag(s, track);
1397  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1399  if (!tag) { // if no mac fcc found, try with Microsoft tags
1401  if (tag)
1402  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1403  "the file may be unplayable!\n");
1404  }
1405  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1407  if (!tag) { // if no mac fcc found, try with Microsoft tags
1408  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
1409  if (ms_tag) {
1410  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1411  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1412  "the file may be unplayable!\n");
1413  }
1414  }
1415  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1417  }
1418 
1419  return tag;
1420 }
1421 
1422 static const AVCodecTag codec_3gp_tags[] = {
1423  { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
1424  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
1425  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
1426  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
1427  { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
1428  { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
1429  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
1430  { AV_CODEC_ID_NONE, 0 },
1431 };
1432 
1433 static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
1434  { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
1435  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
1436  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
1437  { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
1438  { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
1439  { AV_CODEC_ID_NONE, 0 },
1440 };
1441 
1443 {
1444  int tag;
1445 
1446  if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
1447  tag = mp4_get_codec_tag(s, track);
1448  else if (track->mode == MODE_ISM) {
1449  tag = mp4_get_codec_tag(s, track);
1450  if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
1451  tag = MKTAG('w', 'm', 'a', ' ');
1452  } else if (track->mode == MODE_IPOD)
1453  tag = ipod_get_codec_tag(s, track);
1454  else if (track->mode & MODE_3GP)
1455  tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
1456  else if (track->mode == MODE_F4V)
1457  tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
1458  else
1459  tag = mov_get_codec_tag(s, track);
1460 
1461  return tag;
1462 }
1463 
1464 /** Write uuid atom.
1465  * Needed to make file play in iPods running newest firmware
1466  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1467  */
1469 {
1470  avio_wb32(pb, 28);
1471  ffio_wfourcc(pb, "uuid");
1472  avio_wb32(pb, 0x6b6840f2);
1473  avio_wb32(pb, 0x5f244fc5);
1474  avio_wb32(pb, 0xba39a51b);
1475  avio_wb32(pb, 0xcf0323f3);
1476  avio_wb32(pb, 0x0);
1477  return 28;
1478 }
1479 
1480 static const uint16_t fiel_data[] = {
1481  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1482 };
1483 
1484 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
1485 {
1486  unsigned mov_field_order = 0;
1487  if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
1488  mov_field_order = fiel_data[track->enc->field_order];
1489  else
1490  return 0;
1491  avio_wb32(pb, 10);
1492  ffio_wfourcc(pb, "fiel");
1493  avio_wb16(pb, mov_field_order);
1494  return 10;
1495 }
1496 
1498 {
1499  int64_t pos = avio_tell(pb);
1500  avio_wb32(pb, 0); /* size */
1501  avio_wl32(pb, track->tag); // store it byteswapped
1502  avio_wb32(pb, 0); /* Reserved */
1503  avio_wb16(pb, 0); /* Reserved */
1504  avio_wb16(pb, 1); /* Data-reference index */
1505 
1506  if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1507  mov_write_esds_tag(pb, track);
1508  else if (track->enc->extradata_size)
1509  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1510 
1511  return update_size(pb, pos);
1512 }
1513 
1514 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1515 {
1516  AVRational sar;
1517  av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
1518  track->enc->sample_aspect_ratio.den, INT_MAX);
1519 
1520  avio_wb32(pb, 16);
1521  ffio_wfourcc(pb, "pasp");
1522  avio_wb32(pb, sar.num);
1523  avio_wb32(pb, sar.den);
1524  return 16;
1525 }
1526 
1527 static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
1528 {
1529  uint32_t gama = 0;
1530  if (gamma <= 0.0)
1531  {
1532  gamma = avpriv_get_gamma_from_trc(track->enc->color_trc);
1533  }
1534  av_log(pb, AV_LOG_DEBUG, "gamma value %g\n", gamma);
1535 
1536  if (gamma > 1e-6) {
1537  gama = (uint32_t)lrint((double)(1<<16) * gamma);
1538  av_log(pb, AV_LOG_DEBUG, "writing gama value %d\n", gama);
1539 
1540  av_assert0(track->mode == MODE_MOV);
1541  avio_wb32(pb, 12);
1542  ffio_wfourcc(pb, "gama");
1543  avio_wb32(pb, gama);
1544  return 12;
1545  }
1546  else {
1547  av_log(pb, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
1548  }
1549  return 0;
1550 }
1551 
1552 static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
1553 {
1554  // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
1555  // Ref (MP4): ISO/IEC 14496-12:2012
1556 
1557  if (track->enc->color_primaries == AVCOL_PRI_UNSPECIFIED &&
1558  track->enc->color_trc == AVCOL_TRC_UNSPECIFIED &&
1559  track->enc->colorspace == AVCOL_SPC_UNSPECIFIED) {
1560  if ((track->enc->width >= 1920 && track->enc->height >= 1080)
1561  || (track->enc->width == 1280 && track->enc->height == 720)) {
1562  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt709\n");
1564  } else if (track->enc->width == 720 && track->height == 576) {
1565  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt470bg\n");
1567  } else if (track->enc->width == 720 &&
1568  (track->height == 486 || track->height == 480)) {
1569  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming smpte170\n");
1571  } else {
1572  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, unable to assume anything\n");
1573  }
1574  switch (track->enc->color_primaries) {
1575  case AVCOL_PRI_BT709:
1576  track->enc->color_trc = AVCOL_TRC_BT709;
1577  track->enc->colorspace = AVCOL_SPC_BT709;
1578  break;
1579  case AVCOL_PRI_SMPTE170M:
1580  case AVCOL_PRI_BT470BG:
1581  track->enc->color_trc = AVCOL_TRC_BT709;
1583  break;
1584  }
1585  }
1586 
1587  /* We should only ever be called by MOV or MP4. */
1588  av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4);
1589 
1590  avio_wb32(pb, 18 + (track->mode == MODE_MP4));
1591  ffio_wfourcc(pb, "colr");
1592  if (track->mode == MODE_MP4)
1593  ffio_wfourcc(pb, "nclx");
1594  else
1595  ffio_wfourcc(pb, "nclc");
1596  switch (track->enc->color_primaries) {
1597  case AVCOL_PRI_BT709: avio_wb16(pb, 1); break;
1598  case AVCOL_PRI_SMPTE170M:
1599  case AVCOL_PRI_SMPTE240M: avio_wb16(pb, 6); break;
1600  case AVCOL_PRI_BT470BG: avio_wb16(pb, 5); break;
1601  default: avio_wb16(pb, 2);
1602  }
1603  switch (track->enc->color_trc) {
1604  case AVCOL_TRC_BT709: avio_wb16(pb, 1); break;
1605  case AVCOL_TRC_SMPTE170M: avio_wb16(pb, 1); break; // remapped
1606  case AVCOL_TRC_SMPTE240M: avio_wb16(pb, 7); break;
1607  default: avio_wb16(pb, 2);
1608  }
1609  switch (track->enc->colorspace) {
1610  case AVCOL_SPC_BT709: avio_wb16(pb, 1); break;
1611  case AVCOL_SPC_BT470BG:
1612  case AVCOL_SPC_SMPTE170M: avio_wb16(pb, 6); break;
1613  case AVCOL_SPC_SMPTE240M: avio_wb16(pb, 7); break;
1614  default: avio_wb16(pb, 2);
1615  }
1616 
1617  if (track->mode == MODE_MP4) {
1618  int full_range = track->enc->color_range == AVCOL_RANGE_JPEG;
1619  avio_w8(pb, full_range << 7);
1620  return 19;
1621  } else {
1622  return 18;
1623  }
1624 }
1625 
1626 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1627 {
1628  AVDictionaryEntry *encoder;
1629  int xdcam_res = (track->enc->width == 1280 && track->enc->height == 720)
1630  || (track->enc->width == 1440 && track->enc->height == 1080)
1631  || (track->enc->width == 1920 && track->enc->height == 1080);
1632 
1633  if (track->mode == MODE_MOV &&
1634  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
1635  av_strlcpy(compressor_name, encoder->value, 32);
1636  } else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1638  AVStream *st = track->st;
1639  int rate = av_q2d(find_fps(NULL, st));
1640  av_strlcatf(compressor_name, len, "XDCAM");
1641  if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
1642  av_strlcatf(compressor_name, len, " HD422");
1643  } else if(track->enc->width == 1440) {
1644  av_strlcatf(compressor_name, len, " HD");
1645  } else
1646  av_strlcatf(compressor_name, len, " EX");
1647 
1648  av_strlcatf(compressor_name, len, " %d%c", track->enc->height, interlaced ? 'i' : 'p');
1649 
1650  av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1651  }
1652 }
1653 
1655 {
1656  int64_t pos = avio_tell(pb);
1657  char compressor_name[32] = { 0 };
1658  int avid = 0;
1659 
1660  avio_wb32(pb, 0); /* size */
1661  avio_wl32(pb, track->tag); // store it byteswapped
1662  avio_wb32(pb, 0); /* Reserved */
1663  avio_wb16(pb, 0); /* Reserved */
1664  avio_wb16(pb, 1); /* Data-reference index */
1665 
1666  avio_wb16(pb, 0); /* Codec stream version */
1667  avio_wb16(pb, 0); /* Codec stream revision (=0) */
1668  if (track->mode == MODE_MOV) {
1669  ffio_wfourcc(pb, "FFMP"); /* Vendor */
1670  if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1671  avio_wb32(pb, 0); /* Temporal Quality */
1672  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1673  } else {
1674  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1675  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1676  }
1677  } else {
1678  avio_wb32(pb, 0); /* Reserved */
1679  avio_wb32(pb, 0); /* Reserved */
1680  avio_wb32(pb, 0); /* Reserved */
1681  }
1682  avio_wb16(pb, track->enc->width); /* Video width */
1683  avio_wb16(pb, track->height); /* Video height */
1684  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1685  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1686  avio_wb32(pb, 0); /* Data size (= 0) */
1687  avio_wb16(pb, 1); /* Frame count (= 1) */
1688 
1689  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1690  find_compressor(compressor_name, 32, track);
1691  avio_w8(pb, strlen(compressor_name));
1692  avio_write(pb, compressor_name, 31);
1693 
1694  if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1695  avio_wb16(pb, track->enc->bits_per_coded_sample);
1696  else
1697  avio_wb16(pb, 0x18); /* Reserved */
1698  avio_wb16(pb, 0xffff); /* Reserved */
1699  if (track->tag == MKTAG('m','p','4','v'))
1700  mov_write_esds_tag(pb, track);
1701  else if (track->enc->codec_id == AV_CODEC_ID_H263)
1702  mov_write_d263_tag(pb);
1703  else if (track->enc->codec_id == AV_CODEC_ID_AVUI ||
1704  track->enc->codec_id == AV_CODEC_ID_SVQ3) {
1705  mov_write_extradata_tag(pb, track);
1706  avio_wb32(pb, 0);
1707  } else if (track->enc->codec_id == AV_CODEC_ID_DNXHD) {
1708  mov_write_avid_tag(pb, track);
1709  avid = 1;
1710  } else if (track->enc->codec_id == AV_CODEC_ID_HEVC)
1711  mov_write_hvcc_tag(pb, track);
1712  else if (track->enc->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
1713  mov_write_avcc_tag(pb, track);
1714  if (track->mode == MODE_IPOD)
1716  } else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1717  mov_write_dvc1_tag(pb, track);
1718  else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
1719  track->enc->codec_id == AV_CODEC_ID_VP6A) {
1720  /* Don't write any potential extradata here - the cropping
1721  * is signalled via the normal width/height fields. */
1722  } else if (track->enc->codec_id == AV_CODEC_ID_R10K) {
1723  if (track->enc->codec_tag == MKTAG('R','1','0','k'))
1724  mov_write_dpxe_tag(pb, track);
1725  } else if (track->vos_len > 0)
1726  mov_write_glbl_tag(pb, track);
1727 
1728  if (track->enc->codec_id != AV_CODEC_ID_H264 &&
1729  track->enc->codec_id != AV_CODEC_ID_MPEG4 &&
1730  track->enc->codec_id != AV_CODEC_ID_DNXHD)
1731  if (track->enc->field_order != AV_FIELD_UNKNOWN)
1732  mov_write_fiel_tag(pb, track);
1733 
1734  if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
1735  if (track->mode == MODE_MOV)
1736  mov_write_gama_tag(pb, track, mov->gamma);
1737  else
1738  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
1739  }
1740  if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
1741  if (track->mode == MODE_MOV || track->mode == MODE_MP4)
1742  mov_write_colr_tag(pb, track);
1743  else
1744  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4.\n");
1745  }
1746 
1747  if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1748  track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1749  mov_write_pasp_tag(pb, track);
1750  }
1751 
1752  /* extra padding for avid stsd */
1753  /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
1754  if (avid)
1755  avio_wb32(pb, 0);
1756 
1757  return update_size(pb, pos);
1758 }
1759 
1760 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1761 {
1762  int64_t pos = avio_tell(pb);
1763  avio_wb32(pb, 0); /* size */
1764  ffio_wfourcc(pb, "rtp ");
1765  avio_wb32(pb, 0); /* Reserved */
1766  avio_wb16(pb, 0); /* Reserved */
1767  avio_wb16(pb, 1); /* Data-reference index */
1768 
1769  avio_wb16(pb, 1); /* Hint track version */
1770  avio_wb16(pb, 1); /* Highest compatible version */
1771  avio_wb32(pb, track->max_packet_size); /* Max packet size */
1772 
1773  avio_wb32(pb, 12); /* size */
1774  ffio_wfourcc(pb, "tims");
1775  avio_wb32(pb, track->timescale);
1776 
1777  return update_size(pb, pos);
1778 }
1779 
1780 static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
1781 {
1782  uint64_t str_size =strlen(reel_name);
1783  int64_t pos = avio_tell(pb);
1784 
1785  if (str_size >= UINT16_MAX){
1786  av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
1787  avio_wb16(pb, 0);
1788  return AVERROR(EINVAL);
1789  }
1790 
1791  avio_wb32(pb, 0); /* size */
1792  ffio_wfourcc(pb, "name"); /* Data format */
1793  avio_wb16(pb, str_size); /* string size */
1794  avio_wb16(pb, track->language); /* langcode */
1795  avio_write(pb, reel_name, str_size); /* reel name */
1796  return update_size(pb,pos);
1797 }
1798 
1799 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1800 {
1801  int64_t pos = avio_tell(pb);
1802 #if 1
1803  int frame_duration = av_rescale(track->timescale, track->enc->time_base.num, track->enc->time_base.den);
1804  int nb_frames = ROUNDED_DIV(track->enc->time_base.den, track->enc->time_base.num);
1805  AVDictionaryEntry *t = NULL;
1806 
1807  if (nb_frames > 255) {
1808  av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
1809  return AVERROR(EINVAL);
1810  }
1811 
1812  avio_wb32(pb, 0); /* size */
1813  ffio_wfourcc(pb, "tmcd"); /* Data format */
1814  avio_wb32(pb, 0); /* Reserved */
1815  avio_wb32(pb, 1); /* Data reference index */
1816  avio_wb32(pb, 0); /* Flags */
1817  avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
1818  avio_wb32(pb, track->timescale); /* Timescale */
1819  avio_wb32(pb, frame_duration); /* Frame duration */
1820  avio_w8(pb, nb_frames); /* Number of frames */
1821  avio_w8(pb, 0); /* Reserved */
1822 
1823  if (track->st)
1824  t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
1825 
1826  if (t && utf8len(t->value))
1827  mov_write_source_reference_tag(pb, track, t->value);
1828  else
1829  avio_wb16(pb, 0); /* zero size */
1830 #else
1831 
1832  avio_wb32(pb, 0); /* size */
1833  ffio_wfourcc(pb, "tmcd"); /* Data format */
1834  avio_wb32(pb, 0); /* Reserved */
1835  avio_wb32(pb, 1); /* Data reference index */
1836  if (track->enc->extradata_size)
1837  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1838 #endif
1839  return update_size(pb, pos);
1840 }
1841 
1843 {
1844  int64_t pos = avio_tell(pb);
1845  avio_wb32(pb, 0); /* size */
1846  ffio_wfourcc(pb, "stsd");
1847  avio_wb32(pb, 0); /* version & flags */
1848  avio_wb32(pb, 1); /* entry count */
1849  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1850  mov_write_video_tag(pb, mov, track);
1851  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1852  mov_write_audio_tag(pb, track);
1853  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1854  mov_write_subtitle_tag(pb, track);
1855  else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1856  mov_write_rtp_tag(pb, track);
1857  else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1858  mov_write_tmcd_tag(pb, track);
1859  return update_size(pb, pos);
1860 }
1861 
1862 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1863 {
1864  MOVStts *ctts_entries;
1865  uint32_t entries = 0;
1866  uint32_t atom_size;
1867  int i;
1868 
1869  ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
1870  if (!ctts_entries)
1871  return AVERROR(ENOMEM);
1872  ctts_entries[0].count = 1;
1873  ctts_entries[0].duration = track->cluster[0].cts;
1874  for (i = 1; i < track->entry; i++) {
1875  if (track->cluster[i].cts == ctts_entries[entries].duration) {
1876  ctts_entries[entries].count++; /* compress */
1877  } else {
1878  entries++;
1879  ctts_entries[entries].duration = track->cluster[i].cts;
1880  ctts_entries[entries].count = 1;
1881  }
1882  }
1883  entries++; /* last one */
1884  atom_size = 16 + (entries * 8);
1885  avio_wb32(pb, atom_size); /* size */
1886  ffio_wfourcc(pb, "ctts");
1887  avio_wb32(pb, 0); /* version & flags */
1888  avio_wb32(pb, entries); /* entry count */
1889  for (i = 0; i < entries; i++) {
1890  avio_wb32(pb, ctts_entries[i].count);
1891  avio_wb32(pb, ctts_entries[i].duration);
1892  }
1893  av_free(ctts_entries);
1894  return atom_size;
1895 }
1896 
1897 /* Time to sample atom */
1898 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1899 {
1900  MOVStts *stts_entries = NULL;
1901  uint32_t entries = -1;
1902  uint32_t atom_size;
1903  int i;
1904 
1905  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1906  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1907  if (!stts_entries)
1908  return AVERROR(ENOMEM);
1909  stts_entries[0].count = track->sample_count;
1910  stts_entries[0].duration = 1;
1911  entries = 1;
1912  } else {
1913  if (track->entry) {
1914  stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
1915  if (!stts_entries)
1916  return AVERROR(ENOMEM);
1917  }
1918  for (i = 0; i < track->entry; i++) {
1919  int duration = get_cluster_duration(track, i);
1920  if (i && duration == stts_entries[entries].duration) {
1921  stts_entries[entries].count++; /* compress */
1922  } else {
1923  entries++;
1924  stts_entries[entries].duration = duration;
1925  stts_entries[entries].count = 1;
1926  }
1927  }
1928  entries++; /* last one */
1929  }
1930  atom_size = 16 + (entries * 8);
1931  avio_wb32(pb, atom_size); /* size */
1932  ffio_wfourcc(pb, "stts");
1933  avio_wb32(pb, 0); /* version & flags */
1934  avio_wb32(pb, entries); /* entry count */
1935  for (i = 0; i < entries; i++) {
1936  avio_wb32(pb, stts_entries[i].count);
1937  avio_wb32(pb, stts_entries[i].duration);
1938  }
1939  av_free(stts_entries);
1940  return atom_size;
1941 }
1942 
1944 {
1945  avio_wb32(pb, 28); /* size */
1946  ffio_wfourcc(pb, "dref");
1947  avio_wb32(pb, 0); /* version & flags */
1948  avio_wb32(pb, 1); /* entry count */
1949 
1950  avio_wb32(pb, 0xc); /* size */
1951  //FIXME add the alis and rsrc atom
1952  ffio_wfourcc(pb, "url ");
1953  avio_wb32(pb, 1); /* version & flags */
1954 
1955  return 28;
1956 }
1957 
1959 {
1960  int64_t pos = avio_tell(pb);
1961  int ret;
1962 
1963  avio_wb32(pb, 0); /* size */
1964  ffio_wfourcc(pb, "stbl");
1965  mov_write_stsd_tag(pb, mov, track);
1966  mov_write_stts_tag(pb, track);
1967  if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1968  track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1969  track->has_keyframes && track->has_keyframes < track->entry)
1970  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1971  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1973  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1974  track->flags & MOV_TRACK_CTTS && track->entry) {
1975 
1976  if ((ret = mov_write_ctts_tag(pb, track)) < 0)
1977  return ret;
1978  }
1979  mov_write_stsc_tag(pb, track);
1980  mov_write_stsz_tag(pb, track);
1981  mov_write_stco_tag(pb, track);
1982  return update_size(pb, pos);
1983 }
1984 
1986 {
1987  int64_t pos = avio_tell(pb);
1988  avio_wb32(pb, 0); /* size */
1989  ffio_wfourcc(pb, "dinf");
1990  mov_write_dref_tag(pb);
1991  return update_size(pb, pos);
1992 }
1993 
1995 {
1996  avio_wb32(pb, 12);
1997  ffio_wfourcc(pb, "nmhd");
1998  avio_wb32(pb, 0);
1999  return 12;
2000 }
2001 
2002 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
2003 {
2004  int64_t pos = avio_tell(pb);
2005  const char *font = "Lucida Grande";
2006  avio_wb32(pb, 0); /* size */
2007  ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
2008  avio_wb32(pb, 0); /* version & flags */
2009  avio_wb16(pb, 0); /* text font */
2010  avio_wb16(pb, 0); /* text face */
2011  avio_wb16(pb, 12); /* text size */
2012  avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
2013  avio_wb16(pb, 0x0000); /* text color (red) */
2014  avio_wb16(pb, 0x0000); /* text color (green) */
2015  avio_wb16(pb, 0x0000); /* text color (blue) */
2016  avio_wb16(pb, 0xffff); /* background color (red) */
2017  avio_wb16(pb, 0xffff); /* background color (green) */
2018  avio_wb16(pb, 0xffff); /* background color (blue) */
2019  avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
2020  avio_write(pb, font, strlen(font)); /* font name */
2021  return update_size(pb, pos);
2022 }
2023 
2024 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
2025 {
2026  int64_t pos = avio_tell(pb);
2027  avio_wb32(pb, 0); /* size */
2028  ffio_wfourcc(pb, "gmhd");
2029  avio_wb32(pb, 0x18); /* gmin size */
2030  ffio_wfourcc(pb, "gmin");/* generic media info */
2031  avio_wb32(pb, 0); /* version & flags */
2032  avio_wb16(pb, 0x40); /* graphics mode = */
2033  avio_wb16(pb, 0x8000); /* opColor (r?) */
2034  avio_wb16(pb, 0x8000); /* opColor (g?) */
2035  avio_wb16(pb, 0x8000); /* opColor (b?) */
2036  avio_wb16(pb, 0); /* balance */
2037  avio_wb16(pb, 0); /* reserved */
2038 
2039  /*
2040  * This special text atom is required for
2041  * Apple Quicktime chapters. The contents
2042  * don't appear to be documented, so the
2043  * bytes are copied verbatim.
2044  */
2045  if (track->tag != MKTAG('c','6','0','8')) {
2046  avio_wb32(pb, 0x2C); /* size */
2047  ffio_wfourcc(pb, "text");
2048  avio_wb16(pb, 0x01);
2049  avio_wb32(pb, 0x00);
2050  avio_wb32(pb, 0x00);
2051  avio_wb32(pb, 0x00);
2052  avio_wb32(pb, 0x01);
2053  avio_wb32(pb, 0x00);
2054  avio_wb32(pb, 0x00);
2055  avio_wb32(pb, 0x00);
2056  avio_wb32(pb, 0x00004000);
2057  avio_wb16(pb, 0x0000);
2058  }
2059 
2060  if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
2061  int64_t tmcd_pos = avio_tell(pb);
2062  avio_wb32(pb, 0); /* size */
2063  ffio_wfourcc(pb, "tmcd");
2064  mov_write_tcmi_tag(pb, track);
2065  update_size(pb, tmcd_pos);
2066  }
2067  return update_size(pb, pos);
2068 }
2069 
2071 {
2072  avio_wb32(pb, 16); /* size */
2073  ffio_wfourcc(pb, "smhd");
2074  avio_wb32(pb, 0); /* version & flags */
2075  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
2076  avio_wb16(pb, 0); /* reserved */
2077  return 16;
2078 }
2079 
2081 {
2082  avio_wb32(pb, 0x14); /* size (always 0x14) */
2083  ffio_wfourcc(pb, "vmhd");
2084  avio_wb32(pb, 0x01); /* version & flags */
2085  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
2086  return 0x14;
2087 }
2088 
2089 static int is_clcp_track(MOVTrack *track)
2090 {
2091  return track->tag == MKTAG('c','7','0','8') ||
2092  track->tag == MKTAG('c','6','0','8');
2093 }
2094 
2095 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
2096 {
2097  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
2098  int64_t pos = avio_tell(pb);
2099 
2100  hdlr = "dhlr";
2101  hdlr_type = "url ";
2102  descr = "DataHandler";
2103 
2104  if (track) {
2105  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
2106  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2107  hdlr_type = "vide";
2108  descr = "VideoHandler";
2109  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2110  hdlr_type = "soun";
2111  descr = "SoundHandler";
2112  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2113  if (is_clcp_track(track)) {
2114  hdlr_type = "clcp";
2115  descr = "ClosedCaptionHandler";
2116  } else {
2117  if (track->tag == MKTAG('t','x','3','g')) {
2118  hdlr_type = "sbtl";
2119  } else if (track->tag == MKTAG('m','p','4','s')) {
2120  hdlr_type = "subp";
2121  } else {
2122  hdlr_type = "text";
2123  }
2124  descr = "SubtitleHandler";
2125  }
2126  } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
2127  hdlr_type = "hint";
2128  descr = "HintHandler";
2129  } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
2130  hdlr_type = "tmcd";
2131  descr = "TimeCodeHandler";
2132  } else {
2133  char tag_buf[32];
2134  av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
2135  track->enc->codec_tag);
2136 
2137  av_log(track->enc, AV_LOG_WARNING,
2138  "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
2139  tag_buf, track->enc->codec_tag);
2140  }
2141  if (track->st) {
2142  // hdlr.name is used by some players to identify the content title
2143  // of the track. So if an alternate handler description is
2144  // specified, use it.
2145  AVDictionaryEntry *t;
2146  t = av_dict_get(track->st->metadata, "handler", NULL, 0);
2147  if (t && utf8len(t->value))
2148  descr = t->value;
2149  }
2150  }
2151 
2152  avio_wb32(pb, 0); /* size */
2153  ffio_wfourcc(pb, "hdlr");
2154  avio_wb32(pb, 0); /* Version & flags */
2155  avio_write(pb, hdlr, 4); /* handler */
2156  ffio_wfourcc(pb, hdlr_type); /* handler type */
2157  avio_wb32(pb, 0); /* reserved */
2158  avio_wb32(pb, 0); /* reserved */
2159  avio_wb32(pb, 0); /* reserved */
2160  if (!track || track->mode == MODE_MOV)
2161  avio_w8(pb, strlen(descr)); /* pascal string */
2162  avio_write(pb, descr, strlen(descr)); /* handler description */
2163  if (track && track->mode != MODE_MOV)
2164  avio_w8(pb, 0); /* c string */
2165  return update_size(pb, pos);
2166 }
2167 
2169 {
2170  /* This atom must be present, but leaving the values at zero
2171  * seems harmless. */
2172  avio_wb32(pb, 28); /* size */
2173  ffio_wfourcc(pb, "hmhd");
2174  avio_wb32(pb, 0); /* version, flags */
2175  avio_wb16(pb, 0); /* maxPDUsize */
2176  avio_wb16(pb, 0); /* avgPDUsize */
2177  avio_wb32(pb, 0); /* maxbitrate */
2178  avio_wb32(pb, 0); /* avgbitrate */
2179  avio_wb32(pb, 0); /* reserved */
2180  return 28;
2181 }
2182 
2184 {
2185  int64_t pos = avio_tell(pb);
2186  int ret;
2187 
2188  avio_wb32(pb, 0); /* size */
2189  ffio_wfourcc(pb, "minf");
2190  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
2191  mov_write_vmhd_tag(pb);
2192  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
2193  mov_write_smhd_tag(pb);
2194  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2195  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
2196  mov_write_gmhd_tag(pb, track);
2197  } else {
2198  mov_write_nmhd_tag(pb);
2199  }
2200  } else if (track->tag == MKTAG('r','t','p',' ')) {
2201  mov_write_hmhd_tag(pb);
2202  } else if (track->tag == MKTAG('t','m','c','d')) {
2203  mov_write_gmhd_tag(pb, track);
2204  }
2205  if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
2206  mov_write_hdlr_tag(pb, NULL);
2207  mov_write_dinf_tag(pb);
2208  if ((ret = mov_write_stbl_tag(pb, mov, track)) < 0)
2209  return ret;
2210  return update_size(pb, pos);
2211 }
2212 
2214  MOVTrack *track)
2215 {
2216  int version = track->track_duration < INT32_MAX ? 0 : 1;
2217 
2218  if (track->mode == MODE_ISM)
2219  version = 1;
2220 
2221  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
2222  ffio_wfourcc(pb, "mdhd");
2223  avio_w8(pb, version);
2224  avio_wb24(pb, 0); /* flags */
2225  if (version == 1) {
2226  avio_wb64(pb, track->time);
2227  avio_wb64(pb, track->time);
2228  } else {
2229  avio_wb32(pb, track->time); /* creation time */
2230  avio_wb32(pb, track->time); /* modification time */
2231  }
2232  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
2233  if (!track->entry && mov->mode == MODE_ISM)
2234  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2235  else if (!track->entry)
2236  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2237  else
2238  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
2239  avio_wb16(pb, track->language); /* language */
2240  avio_wb16(pb, 0); /* reserved (quality) */
2241 
2242  if (version != 0 && track->mode == MODE_MOV) {
2244  "FATAL error, file duration too long for timebase, this file will not be\n"
2245  "playable with quicktime. Choose a different timebase or a different\n"
2246  "container format\n");
2247  }
2248 
2249  return 32;
2250 }
2251 
2253  MOVTrack *track)
2254 {
2255  int64_t pos = avio_tell(pb);
2256  int ret;
2257 
2258  avio_wb32(pb, 0); /* size */
2259  ffio_wfourcc(pb, "mdia");
2260  mov_write_mdhd_tag(pb, mov, track);
2261  mov_write_hdlr_tag(pb, track);
2262  if ((ret = mov_write_minf_tag(pb, mov, track)) < 0)
2263  return ret;
2264  return update_size(pb, pos);
2265 }
2266 
2267 /* transformation matrix
2268  |a b u|
2269  |c d v|
2270  |tx ty w| */
2271 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
2272  int16_t d, int16_t tx, int16_t ty)
2273 {
2274  avio_wb32(pb, a << 16); /* 16.16 format */
2275  avio_wb32(pb, b << 16); /* 16.16 format */
2276  avio_wb32(pb, 0); /* u in 2.30 format */
2277  avio_wb32(pb, c << 16); /* 16.16 format */
2278  avio_wb32(pb, d << 16); /* 16.16 format */
2279  avio_wb32(pb, 0); /* v in 2.30 format */
2280  avio_wb32(pb, tx << 16); /* 16.16 format */
2281  avio_wb32(pb, ty << 16); /* 16.16 format */
2282  avio_wb32(pb, 1 << 30); /* w in 2.30 format */
2283 }
2284 
2286  MOVTrack *track, AVStream *st)
2287 {
2289  track->timescale, AV_ROUND_UP);
2290  int version = duration < INT32_MAX ? 0 : 1;
2292  int rotation = 0;
2293  int group = 0;
2294 
2295  uint32_t *display_matrix = NULL;
2296  int display_matrix_size, i;
2297 
2298  if (st) {
2299  if (mov->per_stream_grouping)
2300  group = st->index;
2301  else
2302  group = st->codec->codec_type;
2303 
2304  display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
2305  &display_matrix_size);
2306  if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))
2307  display_matrix = NULL;
2308  }
2309 
2310  if (track->flags & MOV_TRACK_ENABLED)
2311  flags |= MOV_TKHD_FLAG_ENABLED;
2312 
2313  if (track->mode == MODE_ISM)
2314  version = 1;
2315 
2316  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
2317  ffio_wfourcc(pb, "tkhd");
2318  avio_w8(pb, version);
2319  avio_wb24(pb, flags);
2320  if (version == 1) {
2321  avio_wb64(pb, track->time);
2322  avio_wb64(pb, track->time);
2323  } else {
2324  avio_wb32(pb, track->time); /* creation time */
2325  avio_wb32(pb, track->time); /* modification time */
2326  }
2327  avio_wb32(pb, track->track_id); /* track-id */
2328  avio_wb32(pb, 0); /* reserved */
2329  if (!track->entry && mov->mode == MODE_ISM)
2330  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2331  else if (!track->entry)
2332  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2333  else
2334  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
2335 
2336  avio_wb32(pb, 0); /* reserved */
2337  avio_wb32(pb, 0); /* reserved */
2338  avio_wb16(pb, 0); /* layer */
2339  avio_wb16(pb, group); /* alternate group) */
2340  /* Volume, only for audio */
2341  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
2342  avio_wb16(pb, 0x0100);
2343  else
2344  avio_wb16(pb, 0);
2345  avio_wb16(pb, 0); /* reserved */
2346 
2347  /* Matrix structure */
2348  if (st && st->metadata) {
2349  AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
2350  rotation = (rot && rot->value) ? atoi(rot->value) : 0;
2351  }
2352  if (display_matrix) {
2353  for (i = 0; i < 9; i++)
2354  avio_wb32(pb, display_matrix[i]);
2355  } else if (rotation == 90) {
2356  write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
2357  } else if (rotation == 180) {
2358  write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
2359  } else if (rotation == 270) {
2360  write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
2361  } else {
2362  write_matrix(pb, 1, 0, 0, 1, 0, 0);
2363  }
2364  /* Track width and height, for visual only */
2365  if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
2366  track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
2367  if (track->mode == MODE_MOV) {
2368  avio_wb32(pb, track->enc->width << 16);
2369  avio_wb32(pb, track->height << 16);
2370  } else {
2371  int64_t track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
2372  track->enc->width * 0x10000LL,
2373  st->sample_aspect_ratio.den);
2374  if (!track_width_1616 ||
2375  track->height != track->enc->height ||
2376  track_width_1616 > UINT32_MAX)
2377  track_width_1616 = track->enc->width * 0x10000U;
2378  avio_wb32(pb, track_width_1616);
2379  avio_wb32(pb, track->height * 0x10000U);
2380  }
2381  } else {
2382  avio_wb32(pb, 0);
2383  avio_wb32(pb, 0);
2384  }
2385  return 0x5c;
2386 }
2387 
2388 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
2389 {
2391  track->enc->sample_aspect_ratio.den);
2392 
2393  int64_t pos = avio_tell(pb);
2394 
2395  avio_wb32(pb, 0); /* size */
2396  ffio_wfourcc(pb, "tapt");
2397 
2398  avio_wb32(pb, 20);
2399  ffio_wfourcc(pb, "clef");
2400  avio_wb32(pb, 0);
2401  avio_wb32(pb, width << 16);
2402  avio_wb32(pb, track->enc->height << 16);
2403 
2404  avio_wb32(pb, 20);
2405  ffio_wfourcc(pb, "prof");
2406  avio_wb32(pb, 0);
2407  avio_wb32(pb, width << 16);
2408  avio_wb32(pb, track->enc->height << 16);
2409 
2410  avio_wb32(pb, 20);
2411  ffio_wfourcc(pb, "enof");
2412  avio_wb32(pb, 0);
2413  avio_wb32(pb, track->enc->width << 16);
2414  avio_wb32(pb, track->enc->height << 16);
2415 
2416  return update_size(pb, pos);
2417 }
2418 
2419 // This box seems important for the psp playback ... without it the movie seems to hang
2421  MOVTrack *track)
2422 {
2424  track->timescale, AV_ROUND_UP);
2425  int version = duration < INT32_MAX ? 0 : 1;
2426  int entry_size, entry_count, size;
2427  int64_t delay, start_ct = track->start_cts;
2428  int64_t start_dts = track->start_dts;
2429 
2430  if (track->entry) {
2431  if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
2432 
2433  av_log(mov->fc, AV_LOG_DEBUG,
2434  "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
2435  track->cluster[0].dts, track->cluster[0].cts,
2436  start_dts, start_ct, track->track_id);
2437  start_dts = track->cluster[0].dts;
2438  start_ct = track->cluster[0].cts;
2439  }
2440  }
2441 
2442  delay = av_rescale_rnd(start_dts + start_ct, MOV_TIMESCALE,
2443  track->timescale, AV_ROUND_DOWN);
2444  version |= delay < INT32_MAX ? 0 : 1;
2445 
2446  entry_size = (version == 1) ? 20 : 12;
2447  entry_count = 1 + (delay > 0);
2448  size = 24 + entry_count * entry_size;
2449 
2450  /* write the atom data */
2451  avio_wb32(pb, size);
2452  ffio_wfourcc(pb, "edts");
2453  avio_wb32(pb, size - 8);
2454  ffio_wfourcc(pb, "elst");
2455  avio_w8(pb, version);
2456  avio_wb24(pb, 0); /* flags */
2457 
2458  avio_wb32(pb, entry_count);
2459  if (delay > 0) { /* add an empty edit to delay presentation */
2460  /* In the positive delay case, the delay includes the cts
2461  * offset, and the second edit list entry below trims out
2462  * the same amount from the actual content. This makes sure
2463  * that the offsetted last sample is included in the edit
2464  * list duration as well. */
2465  if (version == 1) {
2466  avio_wb64(pb, delay);
2467  avio_wb64(pb, -1);
2468  } else {
2469  avio_wb32(pb, delay);
2470  avio_wb32(pb, -1);
2471  }
2472  avio_wb32(pb, 0x00010000);
2473  } else {
2474  /* Avoid accidentally ending up with start_ct = -1 which has got a
2475  * special meaning. Normally start_ct should end up positive or zero
2476  * here, but use FFMIN in case dts is a a small positive integer
2477  * rounded to 0 when represented in MOV_TIMESCALE units. */
2478  av_assert0(av_rescale_rnd(start_dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
2479  start_ct = -FFMIN(start_dts, 0);
2480  /* Note, this delay is calculated from the pts of the first sample,
2481  * ensuring that we don't reduce the duration for cases with
2482  * dts<0 pts=0. */
2483  duration += delay;
2484  }
2485 
2486  /* For fragmented files, we don't know the full length yet. Setting
2487  * duration to 0 allows us to only specify the offset, including
2488  * the rest of the content (from all future fragments) without specifying
2489  * an explicit duration. */
2490  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2491  duration = 0;
2492 
2493  /* duration */
2494  if (version == 1) {
2495  avio_wb64(pb, duration);
2496  avio_wb64(pb, start_ct);
2497  } else {
2498  avio_wb32(pb, duration);
2499  avio_wb32(pb, start_ct);
2500  }
2501  avio_wb32(pb, 0x00010000);
2502  return size;
2503 }
2504 
2505 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
2506 {
2507  avio_wb32(pb, 20); // size
2508  ffio_wfourcc(pb, "tref");
2509  avio_wb32(pb, 12); // size (subatom)
2510  avio_wl32(pb, track->tref_tag);
2511  avio_wb32(pb, track->tref_id);
2512  return 20;
2513 }
2514 
2515 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
2517 {
2518  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
2519  ffio_wfourcc(pb, "uuid");
2520  ffio_wfourcc(pb, "USMT");
2521  avio_wb32(pb, 0x21d24fce);
2522  avio_wb32(pb, 0xbb88695c);
2523  avio_wb32(pb, 0xfac9c740);
2524  avio_wb32(pb, 0x1c); // another size here!
2525  ffio_wfourcc(pb, "MTDT");
2526  avio_wb32(pb, 0x00010012);
2527  avio_wb32(pb, 0x0a);
2528  avio_wb32(pb, 0x55c40000);
2529  avio_wb32(pb, 0x1);
2530  avio_wb32(pb, 0x0);
2531  return 0x34;
2532 }
2533 
2534 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
2535 {
2536  AVFormatContext *ctx = track->rtp_ctx;
2537  char buf[1000] = "";
2538  int len;
2539 
2540  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
2541  NULL, NULL, 0, 0, ctx);
2542  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
2543  len = strlen(buf);
2544 
2545  avio_wb32(pb, len + 24);
2546  ffio_wfourcc(pb, "udta");
2547  avio_wb32(pb, len + 16);
2548  ffio_wfourcc(pb, "hnti");
2549  avio_wb32(pb, len + 8);
2550  ffio_wfourcc(pb, "sdp ");
2551  avio_write(pb, buf, len);
2552  return len + 24;
2553 }
2554 
2556  const char *tag, const char *str)
2557 {
2558  int64_t pos = avio_tell(pb);
2559  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
2560  if (!t || !utf8len(t->value))
2561  return 0;
2562 
2563  avio_wb32(pb, 0); /* size */
2564  ffio_wfourcc(pb, tag); /* type */
2565  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
2566  return update_size(pb, pos);
2567 }
2568 
2570  AVStream *st)
2571 {
2572  AVIOContext *pb_buf;
2573  int ret, size;
2574  uint8_t *buf;
2575 
2576  if (!st || mov->fc->flags & AVFMT_FLAG_BITEXACT)
2577  return 0;
2578 
2579  ret = avio_open_dyn_buf(&pb_buf);
2580  if (ret < 0)
2581  return ret;
2582 
2583  if (mov->mode & MODE_MP4)
2584  mov_write_track_metadata(pb_buf, st, "name", "title");
2585 
2586  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2587  avio_wb32(pb, size + 8);
2588  ffio_wfourcc(pb, "udta");
2589  avio_write(pb, buf, size);
2590  }
2591  av_free(buf);
2592 
2593  return 0;
2594 }
2595 
2597  MOVTrack *track, AVStream *st)
2598 {
2599  int64_t pos = avio_tell(pb);
2600  int entry_backup = track->entry;
2601  int chunk_backup = track->chunkCount;
2602  int ret;
2603 
2604  /* If we want to have an empty moov, but some samples already have been
2605  * buffered (delay_moov), pretend that no samples have been written yet. */
2606  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
2607  track->chunkCount = track->entry = 0;
2608 
2609  avio_wb32(pb, 0); /* size */
2610  ffio_wfourcc(pb, "trak");
2611  mov_write_tkhd_tag(pb, mov, track, st);
2612 
2613  av_assert2(mov->use_editlist >= 0);
2614 
2615  if (track->start_dts != AV_NOPTS_VALUE) {
2616  if (mov->use_editlist)
2617  mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
2618  else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
2619  av_log(mov->fc, AV_LOG_WARNING,
2620  "Not writing any edit list even though one would have been required\n");
2621  }
2622 
2623  if (track->tref_tag)
2624  mov_write_tref_tag(pb, track);
2625 
2626  if ((ret = mov_write_mdia_tag(pb, mov, track)) < 0)
2627  return ret;
2628  if (track->mode == MODE_PSP)
2629  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
2630  if (track->tag == MKTAG('r','t','p',' '))
2631  mov_write_udta_sdp(pb, track);
2632  if (track->mode == MODE_MOV) {
2633  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2634  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
2635  if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
2636  mov_write_tapt_tag(pb, track);
2637  }
2638  }
2639  if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
2640  mov_write_tapt_tag(pb, track);
2641  }
2642  }
2643  mov_write_track_udta_tag(pb, mov, st);
2644  track->entry = entry_backup;
2645  track->chunkCount = chunk_backup;
2646  return update_size(pb, pos);
2647 }
2648 
2650 {
2651  int i, has_audio = 0, has_video = 0;
2652  int64_t pos = avio_tell(pb);
2653  int audio_profile = mov->iods_audio_profile;
2654  int video_profile = mov->iods_video_profile;
2655  for (i = 0; i < mov->nb_streams; i++) {
2656  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
2657  has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
2658  has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
2659  }
2660  }
2661  if (audio_profile < 0)
2662  audio_profile = 0xFF - has_audio;
2663  if (video_profile < 0)
2664  video_profile = 0xFF - has_video;
2665  avio_wb32(pb, 0x0); /* size */
2666  ffio_wfourcc(pb, "iods");
2667  avio_wb32(pb, 0); /* version & flags */
2668  put_descr(pb, 0x10, 7);
2669  avio_wb16(pb, 0x004f);
2670  avio_w8(pb, 0xff);
2671  avio_w8(pb, 0xff);
2672  avio_w8(pb, audio_profile);
2673  avio_w8(pb, video_profile);
2674  avio_w8(pb, 0xff);
2675  return update_size(pb, pos);
2676 }
2677 
2678 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
2679 {
2680  avio_wb32(pb, 0x20); /* size */
2681  ffio_wfourcc(pb, "trex");
2682  avio_wb32(pb, 0); /* version & flags */
2683  avio_wb32(pb, track->track_id); /* track ID */
2684  avio_wb32(pb, 1); /* default sample description index */
2685  avio_wb32(pb, 0); /* default sample duration */
2686  avio_wb32(pb, 0); /* default sample size */
2687  avio_wb32(pb, 0); /* default sample flags */
2688  return 0;
2689 }
2690 
2692 {
2693  int64_t pos = avio_tell(pb);
2694  int i;
2695  avio_wb32(pb, 0x0); /* size */
2696  ffio_wfourcc(pb, "mvex");
2697  for (i = 0; i < mov->nb_streams; i++)
2698  mov_write_trex_tag(pb, &mov->tracks[i]);
2699  return update_size(pb, pos);
2700 }
2701 
2703 {
2704  int max_track_id = 1, i;
2705  int64_t max_track_len = 0;
2706  int version;
2707 
2708  for (i = 0; i < mov->nb_streams; i++) {
2709  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
2710  int64_t max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
2711  MOV_TIMESCALE,
2712  mov->tracks[i].timescale,
2713  AV_ROUND_UP);
2714  if (max_track_len < max_track_len_temp)
2715  max_track_len = max_track_len_temp;
2716  if (max_track_id < mov->tracks[i].track_id)
2717  max_track_id = mov->tracks[i].track_id;
2718  }
2719  }
2720  /* If using delay_moov, make sure the output is the same as if no
2721  * samples had been written yet. */
2722  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
2723  max_track_len = 0;
2724  max_track_id = 1;
2725  }
2726 
2727  version = max_track_len < UINT32_MAX ? 0 : 1;
2728  avio_wb32(pb, version == 1 ? 120 : 108); /* size */
2729 
2730  ffio_wfourcc(pb, "mvhd");
2731  avio_w8(pb, version);
2732  avio_wb24(pb, 0); /* flags */
2733  if (version == 1) {
2734  avio_wb64(pb, mov->time);
2735  avio_wb64(pb, mov->time);
2736  } else {
2737  avio_wb32(pb, mov->time); /* creation time */
2738  avio_wb32(pb, mov->time); /* modification time */
2739  }
2740  avio_wb32(pb, MOV_TIMESCALE);
2741  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
2742 
2743  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
2744  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
2745  avio_wb16(pb, 0); /* reserved */
2746  avio_wb32(pb, 0); /* reserved */
2747  avio_wb32(pb, 0); /* reserved */
2748 
2749  /* Matrix structure */
2750  write_matrix(pb, 1, 0, 0, 1, 0, 0);
2751 
2752  avio_wb32(pb, 0); /* reserved (preview time) */
2753  avio_wb32(pb, 0); /* reserved (preview duration) */
2754  avio_wb32(pb, 0); /* reserved (poster time) */
2755  avio_wb32(pb, 0); /* reserved (selection time) */
2756  avio_wb32(pb, 0); /* reserved (selection duration) */
2757  avio_wb32(pb, 0); /* reserved (current time) */
2758  avio_wb32(pb, max_track_id + 1); /* Next track id */
2759  return 0x6c;
2760 }
2761 
2763  AVFormatContext *s)
2764 {
2765  avio_wb32(pb, 33); /* size */
2766  ffio_wfourcc(pb, "hdlr");
2767  avio_wb32(pb, 0);
2768  avio_wb32(pb, 0);
2769  ffio_wfourcc(pb, "mdir");
2770  ffio_wfourcc(pb, "appl");
2771  avio_wb32(pb, 0);
2772  avio_wb32(pb, 0);
2773  avio_w8(pb, 0);
2774  return 33;
2775 }
2776 
2777 /* helper function to write a data tag with the specified string as data */
2778 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
2779 {
2780  if (long_style) {
2781  int size = 16 + strlen(data);
2782  avio_wb32(pb, size); /* size */
2783  ffio_wfourcc(pb, "data");
2784  avio_wb32(pb, 1);
2785  avio_wb32(pb, 0);
2786  avio_write(pb, data, strlen(data));
2787  return size;
2788  } else {
2789  if (!lang)
2790  lang = ff_mov_iso639_to_lang("und", 1);
2791  avio_wb16(pb, strlen(data)); /* string length */
2792  avio_wb16(pb, lang);
2793  avio_write(pb, data, strlen(data));
2794  return strlen(data) + 4;
2795  }
2796 }
2797 
2798 static int mov_write_string_tag(AVIOContext *pb, const char *name,
2799  const char *value, int lang, int long_style)
2800 {
2801  int size = 0;
2802  if (value && value[0]) {
2803  int64_t pos = avio_tell(pb);
2804  avio_wb32(pb, 0); /* size */
2805  ffio_wfourcc(pb, name);
2806  mov_write_string_data_tag(pb, value, lang, long_style);
2807  size = update_size(pb, pos);
2808  }
2809  return size;
2810 }
2811 
2813  const char *name, const char *tag,
2814  int long_style)
2815 {
2816  int l, lang = 0, len, len2;
2817  AVDictionaryEntry *t, *t2 = NULL;
2818  char tag2[16];
2819 
2820  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2821  return 0;
2822 
2823  len = strlen(t->key);
2824  snprintf(tag2, sizeof(tag2), "%s-", tag);
2825  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
2826  len2 = strlen(t2->key);
2827  if (len2 == len + 4 && !strcmp(t->value, t2->value)
2828  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
2829  lang = l;
2830  break;
2831  }
2832  }
2833  return mov_write_string_tag(pb, name, t->value, lang, long_style);
2834 }
2835 
2836 /* iTunes bpm number */
2838 {
2839  AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
2840  int size = 0, tmpo = t ? atoi(t->value) : 0;
2841  if (tmpo) {
2842  size = 26;
2843  avio_wb32(pb, size);
2844  ffio_wfourcc(pb, "tmpo");
2845  avio_wb32(pb, size-8); /* size */
2846  ffio_wfourcc(pb, "data");
2847  avio_wb32(pb, 0x15); //type specifier
2848  avio_wb32(pb, 0);
2849  avio_wb16(pb, tmpo); // data
2850  }
2851  return size;
2852 }
2853 
2854 /* iTunes track or disc number */
2856  AVFormatContext *s, int disc)
2857 {
2859  disc ? "disc" : "track",
2860  NULL, 0);
2861  int size = 0, track = t ? atoi(t->value) : 0;
2862  if (track) {
2863  int tracks = 0;
2864  char *slash = strchr(t->value, '/');
2865  if (slash)
2866  tracks = atoi(slash + 1);
2867  avio_wb32(pb, 32); /* size */
2868  ffio_wfourcc(pb, disc ? "disk" : "trkn");
2869  avio_wb32(pb, 24); /* size */
2870  ffio_wfourcc(pb, "data");
2871  avio_wb32(pb, 0); // 8 bytes empty
2872  avio_wb32(pb, 0);
2873  avio_wb16(pb, 0); // empty
2874  avio_wb16(pb, track); // track / disc number
2875  avio_wb16(pb, tracks); // total track / disc number
2876  avio_wb16(pb, 0); // empty
2877  size = 32;
2878  }
2879  return size;
2880 }
2881 
2883  const char *name, const char *tag,
2884  int len)
2885 {
2886  AVDictionaryEntry *t = NULL;
2887  uint8_t num;
2888  int size = 24 + len;
2889 
2890  if (len != 1 && len != 4)
2891  return -1;
2892 
2893  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
2894  return 0;
2895  num = atoi(t->value);
2896 
2897  avio_wb32(pb, size);
2898  ffio_wfourcc(pb, name);
2899  avio_wb32(pb, size - 8);
2900  ffio_wfourcc(pb, "data");
2901  avio_wb32(pb, 0x15);
2902  avio_wb32(pb, 0);
2903  if (len==4) avio_wb32(pb, num);
2904  else avio_w8 (pb, num);
2905 
2906  return size;
2907 }
2908 
2909 /* iTunes meta data list */
2911  AVFormatContext *s)
2912 {
2913  int64_t pos = avio_tell(pb);
2914  avio_wb32(pb, 0); /* size */
2915  ffio_wfourcc(pb, "ilst");
2916  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
2917  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
2918  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
2919  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
2920  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
2921  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
2922  if (!mov->exact &&
2923  !mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
2924  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
2925  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
2926  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
2927  mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2928  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
2929  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
2930  mov_write_string_metadata(s, pb, "desc", "description",1);
2931  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
2932  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
2933  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
2934  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
2935  mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
2936  mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
2937  mov_write_int8_metadata (s, pb, "stik", "media_type",1);
2938  mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
2939  mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
2940  mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
2941  mov_write_trkn_tag(pb, mov, s, 0); // track number
2942  mov_write_trkn_tag(pb, mov, s, 1); // disc number
2943  mov_write_tmpo_tag(pb, s);
2944  return update_size(pb, pos);
2945 }
2946 
2947 /* iTunes meta data tag */
2949  AVFormatContext *s)
2950 {
2951  int size = 0;
2952  int64_t pos = avio_tell(pb);
2953  avio_wb32(pb, 0); /* size */
2954  ffio_wfourcc(pb, "meta");
2955  avio_wb32(pb, 0);
2956  mov_write_itunes_hdlr_tag(pb, mov, s);
2957  mov_write_ilst_tag(pb, mov, s);
2958  size = update_size(pb, pos);
2959  return size;
2960 }
2961 
2963  const char *name, const char *key)
2964 {
2965  int len;
2966  AVDictionaryEntry *t;
2967 
2968  if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
2969  return 0;
2970 
2971  len = strlen(t->value);
2972  if (len > 0) {
2973  int size = len + 8;
2974  avio_wb32(pb, size);
2975  ffio_wfourcc(pb, name);
2976  avio_write(pb, t->value, len);
2977  return size;
2978  }
2979  return 0;
2980 }
2981 
2982 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2983 {
2984  int val;
2985  while (*b) {
2986  GET_UTF8(val, *b++, return -1;)
2987  avio_wb16(pb, val);
2988  }
2989  avio_wb16(pb, 0x00);
2990  return 0;
2991 }
2992 
2993 static uint16_t language_code(const char *str)
2994 {
2995  return (((str[0] - 0x60) & 0x1F) << 10) +
2996  (((str[1] - 0x60) & 0x1F) << 5) +
2997  (( str[2] - 0x60) & 0x1F);
2998 }
2999 
3001  const char *tag, const char *str)
3002 {
3003  int64_t pos = avio_tell(pb);
3004  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
3005  if (!t || !utf8len(t->value))
3006  return 0;
3007  avio_wb32(pb, 0); /* size */
3008  ffio_wfourcc(pb, tag); /* type */
3009  avio_wb32(pb, 0); /* version + flags */
3010  if (!strcmp(tag, "yrrc"))
3011  avio_wb16(pb, atoi(t->value));
3012  else {
3013  avio_wb16(pb, language_code("eng")); /* language */
3014  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
3015  if (!strcmp(tag, "albm") &&
3016  (t = av_dict_get(s->metadata, "track", NULL, 0)))
3017  avio_w8(pb, atoi(t->value));
3018  }
3019  return update_size(pb, pos);
3020 }
3021 
3023 {
3024  int64_t pos = avio_tell(pb);
3025  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
3026 
3027  avio_wb32(pb, 0); // size
3028  ffio_wfourcc(pb, "chpl");
3029  avio_wb32(pb, 0x01000000); // version + flags
3030  avio_wb32(pb, 0); // unknown
3031  avio_w8(pb, nb_chapters);
3032 
3033  for (i = 0; i < nb_chapters; i++) {
3034  AVChapter *c = s->chapters[i];
3035  AVDictionaryEntry *t;
3036  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
3037 
3038  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3039  int len = FFMIN(strlen(t->value), 255);
3040  avio_w8(pb, len);
3041  avio_write(pb, t->value, len);
3042  } else
3043  avio_w8(pb, 0);
3044  }
3045  return update_size(pb, pos);
3046 }
3047 
3049  AVFormatContext *s)
3050 {
3051  AVIOContext *pb_buf;
3052  int ret, size;
3053  uint8_t *buf;
3054 
3055  ret = avio_open_dyn_buf(&pb_buf);
3056  if (ret < 0)
3057  return ret;
3058 
3059  if (mov->mode & MODE_3GP) {
3060  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
3061  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
3062  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
3063  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
3064  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
3065  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
3066  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
3067  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
3068  } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
3069  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
3070  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
3071  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
3072  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
3073  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
3074  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
3075  // currently ignored by mov.c
3076  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
3077  // add support for libquicktime, this atom is also actually read by mov.c
3078  mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
3079  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
3080  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
3081  mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
3082  mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
3083  mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
3084  mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
3085  } else {
3086  /* iTunes meta data */
3087  mov_write_meta_tag(pb_buf, mov, s);
3088  }
3089 
3090  if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
3091  mov_write_chpl_tag(pb_buf, s);
3092 
3093  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3094  avio_wb32(pb, size + 8);
3095  ffio_wfourcc(pb, "udta");
3096  avio_write(pb, buf, size);
3097  }
3098  av_free(buf);
3099 
3100  return 0;
3101 }
3102 
3104  const char *str, const char *lang, int type)
3105 {
3106  int len = utf8len(str) + 1;
3107  if (len <= 0)
3108  return;
3109  avio_wb16(pb, len * 2 + 10); /* size */
3110  avio_wb32(pb, type); /* type */
3111  avio_wb16(pb, language_code(lang)); /* language */
3112  avio_wb16(pb, 0x01); /* ? */
3113  ascii_to_wc(pb, str);
3114 }
3115 
3117 {
3118  MOVMuxContext *mov = s->priv_data;
3119  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
3120  int64_t pos, pos2;
3121 
3122  if (title) {
3123  pos = avio_tell(pb);
3124  avio_wb32(pb, 0); /* size placeholder*/
3125  ffio_wfourcc(pb, "uuid");
3126  ffio_wfourcc(pb, "USMT");
3127  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3128  avio_wb32(pb, 0xbb88695c);
3129  avio_wb32(pb, 0xfac9c740);
3130 
3131  pos2 = avio_tell(pb);
3132  avio_wb32(pb, 0); /* size placeholder*/
3133  ffio_wfourcc(pb, "MTDT");
3134  avio_wb16(pb, 4);
3135 
3136  // ?
3137  avio_wb16(pb, 0x0C); /* size */
3138  avio_wb32(pb, 0x0B); /* type */
3139  avio_wb16(pb, language_code("und")); /* language */
3140  avio_wb16(pb, 0x0); /* ? */
3141  avio_wb16(pb, 0x021C); /* data */
3142 
3143  if (!mov->exact)
3144  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
3145  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
3146  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
3147 
3148  update_size(pb, pos2);
3149  return update_size(pb, pos);
3150  }
3151 
3152  return 0;
3153 }
3154 
3155 static void build_chunks(MOVTrack *trk)
3156 {
3157  int i;
3158  MOVIentry *chunk = &trk->cluster[0];
3159  uint64_t chunkSize = chunk->size;
3160  chunk->chunkNum = 1;
3161  if (trk->chunkCount)
3162  return;
3163  trk->chunkCount = 1;
3164  for (i = 1; i<trk->entry; i++){
3165  if (chunk->pos + chunkSize == trk->cluster[i].pos &&
3166  chunkSize + trk->cluster[i].size < (1<<20)){
3167  chunkSize += trk->cluster[i].size;
3168  chunk->samples_in_chunk += trk->cluster[i].entries;
3169  } else {
3170  trk->cluster[i].chunkNum = chunk->chunkNum+1;
3171  chunk=&trk->cluster[i];
3172  chunkSize = chunk->size;
3173  trk->chunkCount++;
3174  }
3175  }
3176 }
3177 
3179  AVFormatContext *s)
3180 {
3181  int i;
3182  int64_t pos = avio_tell(pb);
3183  avio_wb32(pb, 0); /* size placeholder*/
3184  ffio_wfourcc(pb, "moov");
3185 
3186  for (i = 0; i < mov->nb_streams; i++) {
3187  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3188  continue;
3189 
3190  mov->tracks[i].time = mov->time;
3191  mov->tracks[i].track_id = i + 1;
3192 
3193  if (mov->tracks[i].entry)
3194  build_chunks(&mov->tracks[i]);
3195  }
3196 
3197  if (mov->chapter_track)
3198  for (i = 0; i < s->nb_streams; i++) {
3199  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
3200  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
3201  }
3202  for (i = 0; i < mov->nb_streams; i++) {
3203  if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
3204  mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
3205  mov->tracks[i].tref_id =
3206  mov->tracks[mov->tracks[i].src_track].track_id;
3207  }
3208  }
3209  for (i = 0; i < mov->nb_streams; i++) {
3210  if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
3211  int src_trk = mov->tracks[i].src_track;
3212  mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
3213  mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
3214  //src_trk may have a different timescale than the tmcd track
3215  mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
3216  mov->tracks[i].timescale,
3217  mov->tracks[src_trk].timescale);
3218  }
3219  }
3220 
3221  mov_write_mvhd_tag(pb, mov);
3222  if (mov->mode != MODE_MOV && !mov->iods_skip)
3223  mov_write_iods_tag(pb, mov);
3224  for (i = 0; i < mov->nb_streams; i++) {
3225  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
3226  int ret = mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
3227  if (ret < 0)
3228  return ret;
3229  }
3230  }
3231  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3232  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
3233 
3234  if (mov->mode == MODE_PSP)
3235  mov_write_uuidusmt_tag(pb, s);
3236  else
3237  mov_write_udta_tag(pb, mov, s);
3238 
3239  return update_size(pb, pos);
3240 }
3241 
3242 static void param_write_int(AVIOContext *pb, const char *name, int value)
3243 {
3244  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
3245 }
3246 
3247 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
3248 {
3249  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
3250 }
3251 
3252 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
3253 {
3254  char buf[150];
3255  len = FFMIN(sizeof(buf) / 2 - 1, len);
3256  ff_data_to_hex(buf, value, len, 0);
3257  buf[2 * len] = '\0';
3258  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
3259 }
3260 
3262 {
3263  int64_t pos = avio_tell(pb);
3264  int i;
3265  static const uint8_t uuid[] = {
3266  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3267  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3268  };
3269 
3270  avio_wb32(pb, 0);
3271  ffio_wfourcc(pb, "uuid");
3272  avio_write(pb, uuid, sizeof(uuid));
3273  avio_wb32(pb, 0);
3274 
3275  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
3276  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
3277  avio_printf(pb, "<head>\n");
3278  if (!mov->exact)
3279  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
3281  avio_printf(pb, "</head>\n");
3282  avio_printf(pb, "<body>\n");
3283  avio_printf(pb, "<switch>\n");
3284  for (i = 0; i < mov->nb_streams; i++) {
3285  MOVTrack *track = &mov->tracks[i];
3286  const char *type;
3287  /* track->track_id is initialized in write_moov, and thus isn't known
3288  * here yet */
3289  int track_id = i + 1;
3290 
3291  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
3292  type = "video";
3293  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
3294  type = "audio";
3295  } else {
3296  continue;
3297  }
3298  avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
3299  track->enc->bit_rate);
3300  param_write_int(pb, "systemBitrate", track->enc->bit_rate);
3301  param_write_int(pb, "trackID", track_id);
3302  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
3303  if (track->enc->codec_id == AV_CODEC_ID_H264) {
3304  uint8_t *ptr;
3305  int size = track->enc->extradata_size;
3306  if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
3307  &size)) {
3308  param_write_hex(pb, "CodecPrivateData",
3309  ptr ? ptr : track->enc->extradata,
3310  size);
3311  av_free(ptr);
3312  }
3313  param_write_string(pb, "FourCC", "H264");
3314  } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
3315  param_write_string(pb, "FourCC", "WVC1");
3316  param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
3317  track->enc->extradata_size);
3318  }
3319  param_write_int(pb, "MaxWidth", track->enc->width);
3320  param_write_int(pb, "MaxHeight", track->enc->height);
3321  param_write_int(pb, "DisplayWidth", track->enc->width);
3322  param_write_int(pb, "DisplayHeight", track->enc->height);
3323  } else {
3324  if (track->enc->codec_id == AV_CODEC_ID_AAC) {
3325  param_write_string(pb, "FourCC", "AACL");
3326  } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
3327  param_write_string(pb, "FourCC", "WMAP");
3328  }
3329  param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
3330  track->enc->extradata_size);
3332  track->enc->codec_id));
3333  param_write_int(pb, "Channels", track->enc->channels);
3334  param_write_int(pb, "SamplingRate", track->enc->sample_rate);
3335  param_write_int(pb, "BitsPerSample", 16);
3336  param_write_int(pb, "PacketSize", track->enc->block_align ?
3337  track->enc->block_align : 4);
3338  }
3339  avio_printf(pb, "</%s>\n", type);
3340  }
3341  avio_printf(pb, "</switch>\n");
3342  avio_printf(pb, "</body>\n");
3343  avio_printf(pb, "</smil>\n");
3344 
3345  return update_size(pb, pos);
3346 }
3347 
3349 {
3350  avio_wb32(pb, 16);
3351  ffio_wfourcc(pb, "mfhd");
3352  avio_wb32(pb, 0);
3353  avio_wb32(pb, mov->fragments);
3354  return 0;
3355 }
3356 
3357 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
3358 {
3361 }
3362 
3364  MOVTrack *track, int64_t moof_offset)
3365 {
3366  int64_t pos = avio_tell(pb);
3369  if (!track->entry) {
3370  flags |= MOV_TFHD_DURATION_IS_EMPTY;
3371  } else {
3372  flags |= MOV_TFHD_DEFAULT_FLAGS;
3373  }
3375  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
3376  if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
3377  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
3379  }
3380 
3381  /* Don't set a default sample size, the silverlight player refuses
3382  * to play files with that set. Don't set a default sample duration,
3383  * WMP freaks out if it is set. Don't set a base data offset, PIFF
3384  * file format says it MUST NOT be set. */
3385  if (track->mode == MODE_ISM)
3388 
3389  avio_wb32(pb, 0); /* size placeholder */
3390  ffio_wfourcc(pb, "tfhd");
3391  avio_w8(pb, 0); /* version */
3392  avio_wb24(pb, flags);
3393 
3394  avio_wb32(pb, track->track_id); /* track-id */
3395  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
3396  avio_wb64(pb, moof_offset);
3397  if (flags & MOV_TFHD_DEFAULT_DURATION) {
3398  track->default_duration = get_cluster_duration(track, 0);
3399  avio_wb32(pb, track->default_duration);
3400  }
3401  if (flags & MOV_TFHD_DEFAULT_SIZE) {
3402  track->default_size = track->entry ? track->cluster[0].size : 1;
3403  avio_wb32(pb, track->default_size);
3404  } else
3405  track->default_size = -1;
3406 
3407  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
3408  /* Set the default flags based on the second sample, if available.
3409  * If the first sample is different, that can be signaled via a separate field. */
3410  if (track->entry > 1)
3411  track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
3412  else
3413  track->default_sample_flags =
3414  track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
3417  avio_wb32(pb, track->default_sample_flags);
3418  }
3419 
3420  return update_size(pb, pos);
3421 }
3422 
3424  MOVTrack *track, int moof_size,
3425  int first, int end)
3426 {
3427  int64_t pos = avio_tell(pb);
3428  uint32_t flags = MOV_TRUN_DATA_OFFSET;
3429  int i;
3430 
3431  for (i = first; i < end; i++) {
3432  if (get_cluster_duration(track, i) != track->default_duration)
3433  flags |= MOV_TRUN_SAMPLE_DURATION;
3434  if (track->cluster[i].size != track->default_size)
3435  flags |= MOV_TRUN_SAMPLE_SIZE;
3436  if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
3437  flags |= MOV_TRUN_SAMPLE_FLAGS;
3438  }
3439  if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
3440  get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
3441  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
3442  if (track->flags & MOV_TRACK_CTTS)
3443  flags |= MOV_TRUN_SAMPLE_CTS;
3444 
3445  avio_wb32(pb, 0); /* size placeholder */
3446  ffio_wfourcc(pb, "trun");
3447  avio_w8(pb, 0); /* version */
3448  avio_wb24(pb, flags);
3449 
3450  avio_wb32(pb, end - first); /* sample count */
3451  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
3453  !mov->first_trun)
3454  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
3455  else
3456  avio_wb32(pb, moof_size + 8 + track->data_offset +
3457  track->cluster[first].pos); /* data offset */
3458  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
3459  avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
3460 
3461  for (i = first; i < end; i++) {
3462  if (flags & MOV_TRUN_SAMPLE_DURATION)
3463  avio_wb32(pb, get_cluster_duration(track, i));
3464  if (flags & MOV_TRUN_SAMPLE_SIZE)
3465  avio_wb32(pb, track->cluster[i].size);
3466  if (flags & MOV_TRUN_SAMPLE_FLAGS)
3467  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
3468  if (flags & MOV_TRUN_SAMPLE_CTS)
3469  avio_wb32(pb, track->cluster[i].cts);
3470  }
3471 
3472  mov->first_trun = 0;
3473  return update_size(pb, pos);
3474 }
3475 
3476 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
3477 {
3478  int64_t pos = avio_tell(pb);
3479  static const uint8_t uuid[] = {
3480  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
3481  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
3482  };
3483 
3484  avio_wb32(pb, 0); /* size placeholder */
3485  ffio_wfourcc(pb, "uuid");
3486  avio_write(pb, uuid, sizeof(uuid));
3487  avio_w8(pb, 1);
3488  avio_wb24(pb, 0);
3489  avio_wb64(pb, track->start_dts + track->frag_start +
3490  track->cluster[0].cts);
3491  avio_wb64(pb, track->end_pts -
3492  (track->cluster[0].dts + track->cluster[0].cts));
3493 
3494  return update_size(pb, pos);
3495 }
3496 
3498  MOVTrack *track, int entry)
3499 {
3500  int n = track->nb_frag_info - 1 - entry, i;
3501  int size = 8 + 16 + 4 + 1 + 16*n;
3502  static const uint8_t uuid[] = {
3503  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
3504  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
3505  };
3506 
3507  if (entry < 0)
3508  return 0;
3509 
3510  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
3511  avio_wb32(pb, size);
3512  ffio_wfourcc(pb, "uuid");
3513  avio_write(pb, uuid, sizeof(uuid));
3514  avio_w8(pb, 1);
3515  avio_wb24(pb, 0);
3516  avio_w8(pb, n);
3517  for (i = 0; i < n; i++) {
3518  int index = entry + 1 + i;
3519  avio_wb64(pb, track->frag_info[index].time);
3520  avio_wb64(pb, track->frag_info[index].duration);
3521  }
3522  if (n < mov->ism_lookahead) {
3523  int free_size = 16 * (mov->ism_lookahead - n);
3524  avio_wb32(pb, free_size);
3525  ffio_wfourcc(pb, "free");
3526  ffio_fill(pb, 0, free_size - 8);
3527  }
3528 
3529  return 0;
3530 }
3531 
3533  MOVTrack *track)
3534 {
3535  int64_t pos = avio_tell(pb);
3536  int i;
3537  for (i = 0; i < mov->ism_lookahead; i++) {
3538  /* Update the tfrf tag for the last ism_lookahead fragments,
3539  * nb_frag_info - 1 is the next fragment to be written. */
3540  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
3541  }
3542  avio_seek(pb, pos, SEEK_SET);
3543  return 0;
3544 }
3545 
3546 static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
3547  int size)
3548 {
3549  int i;
3550  for (i = 0; i < mov->nb_streams; i++) {
3551  MOVTrack *track = &mov->tracks[i];
3552  MOVFragmentInfo *info;
3553  if ((tracks >= 0 && i != tracks) || !track->entry)
3554  continue;
3555  track->nb_frag_info++;
3556  if (track->nb_frag_info >= track->frag_info_capacity) {
3557  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
3558  if (av_reallocp_array(&track->frag_info,
3559  new_capacity,
3560  sizeof(*track->frag_info)))
3561  return AVERROR(ENOMEM);
3562  track->frag_info_capacity = new_capacity;
3563  }
3564  info = &track->frag_info[track->nb_frag_info - 1];
3565  info->offset = avio_tell(pb);
3566  info->size = size;
3567  // Try to recreate the original pts for the first packet
3568  // from the fields we have stored
3569  info->time = track->start_dts + track->frag_start +
3570  track->cluster[0].cts;
3571  info->duration = track->end_pts -
3572  (track->cluster[0].dts + track->cluster[0].cts);
3573  // If the pts is less than zero, we will have trimmed
3574  // away parts of the media track using an edit list,
3575  // and the corresponding start presentation time is zero.
3576  if (info->time < 0) {
3577  info->duration += info->time;
3578  info->time = 0;
3579  }
3580  info->tfrf_offset = 0;
3581  mov_write_tfrf_tags(pb, mov, track);
3582  }
3583  return 0;
3584 }
3585 
3586 static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
3587 {
3588  int64_t pos = avio_tell(pb);
3589 
3590  avio_wb32(pb, 0); /* size */
3591  ffio_wfourcc(pb, "tfdt");
3592  avio_w8(pb, 1); /* version */
3593  avio_wb24(pb, 0);
3594  avio_wb64(pb, track->frag_start);
3595  return update_size(pb, pos);
3596 }
3597 
3599  MOVTrack *track, int64_t moof_offset,
3600  int moof_size)
3601 {
3602  int64_t pos = avio_tell(pb);
3603  int i, start = 0;
3604  avio_wb32(pb, 0); /* size placeholder */
3605  ffio_wfourcc(pb, "traf");
3606 
3607  mov_write_tfhd_tag(pb, mov, track, moof_offset);
3608  if (mov->mode != MODE_ISM)
3609  mov_write_tfdt_tag(pb, track);
3610  for (i = 1; i < track->entry; i++) {
3611  if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
3612  mov_write_trun_tag(pb, mov, track, moof_size, start, i);
3613  start = i;
3614  }
3615  }
3616  mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
3617  if (mov->mode == MODE_ISM) {
3618  mov_write_tfxd_tag(pb, track);
3619 
3620  if (mov->ism_lookahead) {
3621  int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
3622 
3623  if (track->nb_frag_info > 0) {
3624  MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
3625  if (!info->tfrf_offset)
3626  info->tfrf_offset = avio_tell(pb);
3627  }
3628  avio_wb32(pb, 8 + size);
3629  ffio_wfourcc(pb, "free");
3630  for (i = 0; i < size; i++)
3631  avio_w8(pb, 0);
3632  }
3633  }
3634 
3635  return update_size(pb, pos);
3636 }
3637 
3639  int tracks, int moof_size)
3640 {
3641  int64_t pos = avio_tell(pb);
3642  int i;
3643 
3644  avio_wb32(pb, 0); /* size placeholder */
3645  ffio_wfourcc(pb, "moof");
3646  mov->first_trun = 1;
3647 
3648  mov_write_mfhd_tag(pb, mov);
3649  for (i = 0; i < mov->nb_streams; i++) {
3650  MOVTrack *track = &mov->tracks[i];
3651  if (tracks >= 0 && i != tracks)
3652  continue;
3653  if (!track->entry)
3654  continue;
3655  mov_write_traf_tag(pb, mov, track, pos, moof_size);
3656  }
3657 
3658  return update_size(pb, pos);
3659 }
3660 
3662  MOVTrack *track, int ref_size, int total_sidx_size)
3663 {
3664  int64_t pos = avio_tell(pb), offset_pos, end_pos;
3665  int64_t presentation_time, duration, offset;
3666  int starts_with_SAP, i, entries;
3667 
3668  if (track->entry) {
3669  entries = 1;
3670  presentation_time = track->start_dts + track->frag_start +
3671  track->cluster[0].cts;
3672  duration = track->end_pts -
3673  (track->cluster[0].dts + track->cluster[0].cts);
3674  starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
3675 
3676  // pts<0 should be cut away using edts
3677  if (presentation_time < 0) {
3678  duration += presentation_time;
3679  presentation_time = 0;
3680  }
3681  } else {
3682  entries = track->nb_frag_info;
3683  presentation_time = track->frag_info[0].time;
3684  }
3685 
3686  avio_wb32(pb, 0); /* size */
3687  ffio_wfourcc(pb, "sidx");
3688  avio_w8(pb, 1); /* version */
3689  avio_wb24(pb, 0);
3690  avio_wb32(pb, track->track_id); /* reference_ID */
3691  avio_wb32(pb, track->timescale); /* timescale */
3692  avio_wb64(pb, presentation_time); /* earliest_presentation_time */
3693  offset_pos = avio_tell(pb);
3694  avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
3695  avio_wb16(pb, 0); /* reserved */
3696 
3697  avio_wb16(pb, entries); /* reference_count */
3698  for (i = 0; i < entries; i++) {
3699  if (!track->entry) {
3700  if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
3701  av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
3702  }
3703  duration = track->frag_info[i].duration;
3704  ref_size = track->frag_info[i].size;
3705  starts_with_SAP = 1;
3706  }
3707  avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
3708  avio_wb32(pb, duration); /* subsegment_duration */
3709  avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
3710  }
3711 
3712  end_pos = avio_tell(pb);
3713  offset = pos + total_sidx_size - end_pos;
3714  avio_seek(pb, offset_pos, SEEK_SET);
3715  avio_wb64(pb, offset);
3716  avio_seek(pb, end_pos, SEEK_SET);
3717  return update_size(pb, pos);
3718 }
3719 
3721  int tracks, int ref_size)
3722 {
3723  int i, round, ret;
3724  AVIOContext *avio_buf;
3725  int total_size = 0;
3726  for (round = 0; round < 2; round++) {
3727  // First run one round to calculate the total size of all
3728  // sidx atoms.
3729  // This would be much simpler if we'd only write one sidx
3730  // atom, for the first track in the moof.
3731  if (round == 0) {
3732  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
3733  return ret;
3734  } else {
3735  avio_buf = pb;
3736  }
3737  for (i = 0; i < mov->nb_streams; i++) {
3738  MOVTrack *track = &mov->tracks[i];
3739  if (tracks >= 0 && i != tracks)
3740  continue;
3741  // When writing a sidx for the full file, entry is 0, but
3742  // we want to include all tracks. ref_size is 0 in this case,
3743  // since we read it from frag_info instead.
3744  if (!track->entry && ref_size > 0)
3745  continue;
3746  total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
3747  total_size);
3748  }
3749  if (round == 0)
3750  total_size = ffio_close_null_buf(avio_buf);
3751  }
3752  return 0;
3753 }
3754 
3755 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
3756  int64_t mdat_size)
3757 {
3758  AVIOContext *avio_buf;
3759  int ret, moof_size;
3760 
3761  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
3762  return ret;
3763  mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
3764  moof_size = ffio_close_null_buf(avio_buf);
3765 
3766  if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_FASTSTART))
3767  mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
3768 
3769  if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
3770  return ret;
3771 
3772  return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
3773 }
3774 
3775 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
3776 {
3777  int64_t pos = avio_tell(pb);
3778  int i;
3779 
3780  avio_wb32(pb, 0); /* size placeholder */
3781  ffio_wfourcc(pb, "tfra");
3782  avio_w8(pb, 1); /* version */
3783  avio_wb24(pb, 0);
3784 
3785  avio_wb32(pb, track->track_id);
3786  avio_wb32(pb, 0); /* length of traf/trun/sample num */
3787  avio_wb32(pb, track->nb_frag_info);
3788  for (i = 0; i < track->nb_frag_info; i++) {
3789  avio_wb64(pb, track->frag_info[i].time);
3790  avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
3791  avio_w8(pb, 1); /* traf number */
3792  avio_w8(pb, 1); /* trun number */
3793  avio_w8(pb, 1); /* sample number */
3794  }
3795 
3796  return update_size(pb, pos);
3797 }
3798 
3800 {
3801  int64_t pos = avio_tell(pb);
3802  int i;
3803 
3804  avio_wb32(pb, 0); /* size placeholder */
3805  ffio_wfourcc(pb, "mfra");
3806  /* An empty mfra atom is enough to indicate to the publishing point that
3807  * the stream has ended. */
3808  if (mov->flags & FF_MOV_FLAG_ISML)
3809  return update_size(pb, pos);
3810 
3811  for (i = 0; i < mov->nb_streams; i++) {
3812  MOVTrack *track = &mov->tracks[i];
3813  if (track->nb_frag_info)
3814  mov_write_tfra_tag(pb, track);
3815  }
3816 
3817  avio_wb32(pb, 16);
3818  ffio_wfourcc(pb, "mfro");
3819  avio_wb32(pb, 0); /* version + flags */
3820  avio_wb32(pb, avio_tell(pb) + 4 - pos);
3821 
3822  return update_size(pb, pos);
3823 }
3824 
3826 {
3827  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
3828  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
3829 
3830  mov->mdat_pos = avio_tell(pb);
3831  avio_wb32(pb, 0); /* size placeholder*/
3832  ffio_wfourcc(pb, "mdat");
3833  return 0;
3834 }
3835 
3836 /* TODO: This needs to be more general */
3838 {
3839  MOVMuxContext *mov = s->priv_data;
3840  int64_t pos = avio_tell(pb);
3841  int has_h264 = 0, has_video = 0;
3842  int minor = 0x200;
3843  int i;
3844 
3845  for (i = 0; i < s->nb_streams; i++) {
3846  AVStream *st = s->streams[i];
3847  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3848  has_video = 1;
3849  if (st->codec->codec_id == AV_CODEC_ID_H264)
3850  has_h264 = 1;
3851  }
3852 
3853  avio_wb32(pb, 0); /* size */
3854  ffio_wfourcc(pb, "ftyp");
3855 
3856  if (mov->major_brand && strlen(mov->major_brand) >= 4)
3857  ffio_wfourcc(pb, mov->major_brand);
3858  else if (mov->mode == MODE_3GP) {
3859  ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
3860  minor = has_h264 ? 0x100 : 0x200;
3861  } else if (mov->mode & MODE_3G2) {
3862  ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
3863  minor = has_h264 ? 0x20000 : 0x10000;
3864  } else if (mov->mode == MODE_PSP)
3865  ffio_wfourcc(pb, "MSNV");
3866  else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
3867  ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
3868  else if (mov->mode == MODE_MP4)
3869  ffio_wfourcc(pb, "isom");
3870  else if (mov->mode == MODE_IPOD)
3871  ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
3872  else if (mov->mode == MODE_ISM)
3873  ffio_wfourcc(pb, "isml");
3874  else if (mov->mode == MODE_F4V)
3875  ffio_wfourcc(pb, "f4v ");
3876  else
3877  ffio_wfourcc(pb, "qt ");
3878 
3879  avio_wb32(pb, minor);
3880 
3881  if (mov->mode == MODE_MOV)
3882  ffio_wfourcc(pb, "qt ");
3883  else if (mov->mode == MODE_ISM) {
3884  ffio_wfourcc(pb, "piff");
3885  } else if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
3886  ffio_wfourcc(pb, "isom");
3887  ffio_wfourcc(pb, "iso2");
3888  if (has_h264)
3889  ffio_wfourcc(pb, "avc1");
3890  }
3891 
3892  // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
3893  // brand. This is compatible with users that don't understand tfdt.
3894  if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->mode != MODE_ISM)
3895  ffio_wfourcc(pb, "iso6");
3896 
3897  if (mov->mode == MODE_3GP)
3898  ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
3899  else if (mov->mode & MODE_3G2)
3900  ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
3901  else if (mov->mode == MODE_PSP)
3902  ffio_wfourcc(pb, "MSNV");
3903  else if (mov->mode == MODE_MP4)
3904  ffio_wfourcc(pb, "mp41");
3905 
3906  if (mov->flags & FF_MOV_FLAG_DASH && mov->flags & FF_MOV_FLAG_FASTSTART)
3907  ffio_wfourcc(pb, "dash");
3908 
3909  return update_size(pb, pos);
3910 }
3911 
3913 {
3914  AVStream *video_st = s->streams[0];
3915  AVCodecContext *video_codec = s->streams[0]->codec;
3916  AVCodecContext *audio_codec = s->streams[1]->codec;
3917  int audio_rate = audio_codec->sample_rate;
3918  // TODO: should be avg_frame_rate
3919  int frame_rate = ((video_st->time_base.den) * (0x10000)) / (video_st->time_base.num);
3920  int audio_kbitrate = audio_codec->bit_rate / 1000;
3921  int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
3922 
3923  avio_wb32(pb, 0x94); /* size */
3924  ffio_wfourcc(pb, "uuid");
3925  ffio_wfourcc(pb, "PROF");
3926 
3927  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3928  avio_wb32(pb, 0xbb88695c);
3929  avio_wb32(pb, 0xfac9c740);
3930 
3931  avio_wb32(pb, 0x0); /* ? */
3932  avio_wb32(pb, 0x3); /* 3 sections ? */
3933 
3934  avio_wb32(pb, 0x14); /* size */
3935  ffio_wfourcc(pb, "FPRF");
3936  avio_wb32(pb, 0x0); /* ? */
3937  avio_wb32(pb, 0x0); /* ? */
3938  avio_wb32(pb, 0x0); /* ? */
3939 
3940  avio_wb32(pb, 0x2c); /* size */
3941  ffio_wfourcc(pb, "APRF"); /* audio */
3942  avio_wb32(pb, 0x0);
3943  avio_wb32(pb, 0x2); /* TrackID */
3944  ffio_wfourcc(pb, "mp4a");
3945  avio_wb32(pb, 0x20f);
3946  avio_wb32(pb, 0x0);
3947  avio_wb32(pb, audio_kbitrate);
3948  avio_wb32(pb, audio_kbitrate);
3949  avio_wb32(pb, audio_rate);
3950  avio_wb32(pb, audio_codec->channels);
3951 
3952  avio_wb32(pb, 0x34); /* size */
3953  ffio_wfourcc(pb, "VPRF"); /* video */
3954  avio_wb32(pb, 0x0);
3955  avio_wb32(pb, 0x1); /* TrackID */
3956  if (video_codec->codec_id == AV_CODEC_ID_H264) {
3957  ffio_wfourcc(pb, "avc1");
3958  avio_wb16(pb, 0x014D);
3959  avio_wb16(pb, 0x0015);
3960  } else {
3961  ffio_wfourcc(pb, "mp4v");
3962  avio_wb16(pb, 0x0000);
3963  avio_wb16(pb, 0x0103);
3964  }
3965  avio_wb32(pb, 0x0);
3966  avio_wb32(pb, video_kbitrate);
3967  avio_wb32(pb, video_kbitrate);
3968  avio_wb32(pb, frame_rate);
3969  avio_wb32(pb, frame_rate);
3970  avio_wb16(pb, video_codec->width);
3971  avio_wb16(pb, video_codec->height);
3972  avio_wb32(pb, 0x010001); /* ? */
3973 }
3974 
3976 {
3977  MOVMuxContext *mov = s->priv_data;
3978  int i;
3979 
3980  mov_write_ftyp_tag(pb,s);
3981  if (mov->mode == MODE_PSP) {
3982  int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
3983  for (i = 0; i < s->nb_streams; i++) {
3984  AVStream *st = s->streams[i];
3985  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3986  video_streams_nb++;
3987  else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
3988  audio_streams_nb++;
3989  else
3990  other_streams_nb++;
3991  }
3992 
3993  if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
3994  av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3995  return AVERROR(EINVAL);
3996  }
3997  mov_write_uuidprof_tag(pb, s);
3998  }
3999  return 0;
4000 }
4001 
4002 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
4003 {
4004  uint32_t c = -1;
4005  int i, closed_gop = 0;
4006 
4007  for (i = 0; i < pkt->size - 4; i++) {
4008  c = (c << 8) + pkt->data[i];
4009  if (c == 0x1b8) { // gop
4010  closed_gop = pkt->data[i + 4] >> 6 & 0x01;
4011  } else if (c == 0x100) { // pic
4012  int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
4013  if (!temp_ref || closed_gop) // I picture is not reordered
4014  *flags = MOV_SYNC_SAMPLE;
4015  else
4016  *flags = MOV_PARTIAL_SYNC_SAMPLE;
4017  break;
4018  }
4019  }
4020  return 0;
4021 }
4022 
4023 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
4024 {
4025  const uint8_t *start, *next, *end = pkt->data + pkt->size;
4026  int seq = 0, entry = 0;
4027  int key = pkt->flags & AV_PKT_FLAG_KEY;
4028  start = find_next_marker(pkt->data, end);
4029  for (next = start; next < end; start = next) {
4030  next = find_next_marker(start + 4, end);
4031  switch (AV_RB32(start)) {
4032  case VC1_CODE_SEQHDR:
4033  seq = 1;
4034  break;
4035  case VC1_CODE_ENTRYPOINT:
4036  entry = 1;
4037  break;
4038  case VC1_CODE_SLICE:
4039  trk->vc1_info.slices = 1;
4040  break;
4041  }
4042  }
4043  if (!trk->entry && trk->vc1_info.first_packet_seen)
4044  trk->vc1_info.first_frag_written = 1;
4045  if (!trk->entry && !trk->vc1_info.first_frag_written) {
4046  /* First packet in first fragment */
4047  trk->vc1_info.first_packet_seq = seq;
4048  trk->vc1_info.first_packet_entry = entry;
4049  trk->vc1_info.first_packet_seen = 1;
4050  } else if ((seq && !trk->vc1_info.packet_seq) ||
4051  (entry && !trk->vc1_info.packet_entry)) {
4052  int i;
4053  for (i = 0; i < trk->entry; i++)
4054  trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
4055  trk->has_keyframes = 0;
4056  if (seq)
4057  trk->vc1_info.packet_seq = 1;
4058  if (entry)
4059  trk->vc1_info.packet_entry = 1;
4060  if (!trk->vc1_info.first_frag_written) {
4061  /* First fragment */
4062  if ((!seq || trk->vc1_info.first_packet_seq) &&
4063  (!entry || trk->vc1_info.first_packet_entry)) {
4064  /* First packet had the same headers as this one, readd the
4065  * sync sample flag. */
4066  trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
4067  trk->has_keyframes = 1;
4068  }
4069  }
4070  }
4071  if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
4072  key = seq && entry;
4073  else if (trk->vc1_info.packet_seq)
4074  key = seq;
4075  else if (trk->vc1_info.packet_entry)
4076  key = entry;
4077  if (key) {
4078  trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
4079  trk->has_keyframes++;
4080  }
4081 }
4082 
4084 {
4085  MOVMuxContext *mov = s->priv_data;
4086  int ret, buf_size;
4087  uint8_t *buf;
4088  int i, offset;
4089 
4090  if (!track->mdat_buf)
4091  return 0;
4092  if (!mov->mdat_buf) {
4093  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
4094  return ret;
4095  }
4096  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
4097  track->mdat_buf = NULL;
4098 
4099  offset = avio_tell(mov->mdat_buf);
4100  avio_write(mov->mdat_buf, buf, buf_size);
4101  av_free(buf);
4102 
4103  for (i = track->entries_flushed; i < track->entry; i++)
4104  track->cluster[i].pos += offset;
4105  track->entries_flushed = track->entry;
4106  return 0;
4107 }
4108 
4110 {
4111  MOVMuxContext *mov = s->priv_data;
4112  int i, first_track = -1;
4113  int64_t mdat_size = 0;
4114  int ret;
4115 
4116  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
4117  return 0;
4118 
4119  for (i = 0; i < mov->nb_streams; i++) {
4120  MOVTrack *track = &mov->tracks[i];
4121  if (track->entry <= 1)
4122  continue;
4123  // Sample durations are calculated as the diff of dts values,
4124  // but for the last sample in a fragment, we don't know the dts
4125  // of the first sample in the next fragment, so we have to rely
4126  // on what was set as duration in the AVPacket. Not all callers
4127  // set this though, so we might want to replace it with an
4128  // estimate if it currently is zero.
4129  if (get_cluster_duration(track, track->entry - 1) != 0)
4130  continue;
4131  // Use the duration (i.e. dts diff) of the second last sample for
4132  // the last one. This is a wild guess (and fatal if it turns out
4133  // to be too long), but probably the best we can do - having a zero
4134  // duration is bad as well.
4135  track->track_duration += get_cluster_duration(track, track->entry - 2);
4136  track->end_pts += get_cluster_duration(track, track->entry - 2);
4137  if (!mov->missing_duration_warned) {
4139  "Estimating the duration of the last packet in a "
4140  "fragment, consider setting the duration field in "
4141  "AVPacket instead.\n");
4142  mov->missing_duration_warned = 1;
4143  }
4144  }
4145 
4146  if (!mov->moov_written) {
4147  int64_t pos = avio_tell(s->pb);
4148  uint8_t *buf;
4149  int buf_size, moov_size;
4150 
4151  for (i = 0; i < mov->nb_streams; i++)
4152  if (!mov->tracks[i].entry)
4153  break;
4154  /* Don't write the initial moov unless all tracks have data */
4155  if (i < mov->nb_streams)
4156  return 0;
4157 
4158  moov_size = get_moov_size(s);
4159  for (i = 0; i < mov->nb_streams; i++)
4160  mov->tracks[i].data_offset = pos + moov_size + 8;
4161 
4164  if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
4165  return ret;
4166 
4167  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
4168  if (mov->flags & FF_MOV_FLAG_FASTSTART)
4169  mov->reserved_moov_pos = avio_tell(s->pb);
4170  avio_flush(s->pb);
4171  mov->moov_written = 1;
4172  return 0;
4173  }
4174 
4175  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
4176  mov->mdat_buf = NULL;
4177  avio_wb32(s->pb, buf_size + 8);
4178  ffio_wfourcc(s->pb, "mdat");
4179  avio_write(s->pb, buf, buf_size);
4180  av_free(buf);
4181 
4182  mov->moov_written = 1;
4183  mov->mdat_size = 0;
4184  for (i = 0; i < mov->nb_streams; i++) {
4185  if (mov->tracks[i].entry)
4186  mov->tracks[i].frag_start += mov->tracks[i].start_dts +
4187  mov->tracks[i].track_duration -
4188  mov->tracks[i].cluster[0].dts;
4189  mov->tracks[i].entry = 0;
4190  }
4191  avio_flush(s->pb);
4192  return 0;
4193  }
4194 
4195  if (mov->frag_interleave) {
4196  for (i = 0; i < mov->nb_streams; i++) {
4197  MOVTrack *track = &mov->tracks[i];
4198  int ret;
4199  if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
4200  return ret;
4201  }
4202 
4203  if (!mov->mdat_buf)
4204  return 0;
4205  mdat_size = avio_tell(mov->mdat_buf);
4206  }
4207 
4208  for (i = 0; i < mov->nb_streams; i++) {
4209  MOVTrack *track = &mov->tracks[i];
4210  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF || mov->frag_interleave)
4211  track->data_offset = 0;
4212  else
4213  track->data_offset = mdat_size;
4214  if (!track->entry)
4215  continue;
4216  if (track->mdat_buf)
4217  mdat_size += avio_tell(track->mdat_buf);
4218  if (first_track < 0)
4219  first_track = i;
4220  }
4221 
4222  if (!mdat_size)
4223  return 0;
4224 
4225  for (i = 0; i < mov->nb_streams; i++) {
4226  MOVTrack *track = &mov->tracks[i];
4227  int buf_size, write_moof = 1, moof_tracks = -1;
4228  uint8_t *buf;
4229  int64_t duration = 0;
4230 
4231  if (track->entry)
4232  duration = track->start_dts + track->track_duration -
4233  track->cluster[0].dts;
4234  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
4235  if (!track->mdat_buf)
4236  continue;
4237  mdat_size = avio_tell(track->mdat_buf);
4238  moof_tracks = i;
4239  } else {
4240  write_moof = i == first_track;
4241  }
4242 
4243  if (write_moof) {
4244  avio_flush(s->pb);
4245 
4246  mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
4247  mov->fragments++;
4248 
4249  avio_wb32(s->pb, mdat_size + 8);
4250  ffio_wfourcc(s->pb, "mdat");
4251  }
4252 
4253  if (track->entry)
4254  track->frag_start += duration;
4255  track->entry = 0;
4256  track->entries_flushed = 0;
4257  if (!mov->frag_interleave) {
4258  if (!track->mdat_buf)
4259  continue;
4260  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
4261  track->mdat_buf = NULL;
4262  } else {
4263  if (!mov->mdat_buf)
4264  continue;
4265  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
4266  mov->mdat_buf = NULL;
4267  }
4268 
4269  avio_write(s->pb, buf, buf_size);
4270  av_free(buf);
4271  }
4272 
4273  mov->mdat_size = 0;
4274 
4275  avio_flush(s->pb);
4276  return 0;
4277 }
4278 
4280 {
4281  MOVMuxContext *mov = s->priv_data;
4282  int had_moov = mov->moov_written;
4283  int ret = mov_flush_fragment(s);
4284  if (ret < 0)
4285  return ret;
4286  // If using delay_moov, the first flush only wrote the moov,
4287  // not the actual moof+mdat pair, thus flush once again.
4288  if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
4289  ret = mov_flush_fragment(s);
4290  return ret;
4291 }
4292 
4294 {
4295  MOVMuxContext *mov = s->priv_data;
4296  AVIOContext *pb = s->pb;
4297  MOVTrack *trk = &mov->tracks[pkt->stream_index];
4298  AVCodecContext *enc = trk->enc;
4299  unsigned int samples_in_chunk = 0;
4300  int size = pkt->size, ret = 0;
4301  uint8_t *reformatted_data = NULL;
4302 
4303  if (trk->entry) {
4304  int64_t duration = pkt->dts - trk->cluster[trk->entry - 1].dts;
4305  if (duration < 0 || duration > INT_MAX) {
4306  av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
4307  duration, pkt->dts
4308  );
4309 
4310  pkt->dts = trk->cluster[trk->entry - 1].dts + 1;
4311  pkt->pts = AV_NOPTS_VALUE;
4312  }
4313  if (pkt->duration < 0) {
4314  av_log(s, AV_LOG_ERROR, "Application provided duration: %d is invalid\n", pkt->duration);
4315  return AVERROR(EINVAL);
4316  }
4317  }
4318  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
4319  int ret;
4320  if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4321  if (mov->frag_interleave && mov->fragments > 0) {
4322  if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
4323  if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
4324  return ret;
4325  }
4326  }
4327 
4328  if (!trk->mdat_buf) {
4329  if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
4330  return ret;
4331  }
4332  pb = trk->mdat_buf;
4333  } else {
4334  if (!mov->mdat_buf) {
4335  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
4336  return ret;
4337  }
4338  pb = mov->mdat_buf;
4339  }
4340  }
4341 
4342  if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
4343  /* We must find out how many AMR blocks there are in one packet */
4344  static uint16_t packed_size[16] =
4345  {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
4346  int len = 0;
4347 
4348  while (len < size && samples_in_chunk < 100) {
4349  len += packed_size[(pkt->data[len] >> 3) & 0x0F];
4350  samples_in_chunk++;
4351  }
4352  if (samples_in_chunk > 1) {
4353  av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
4354  return -1;
4355  }
4356  } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
4358  samples_in_chunk = enc->frame_size;
4359  } else if (trk->sample_size)
4360  samples_in_chunk = size / trk->sample_size;
4361  else
4362  samples_in_chunk = 1;
4363 
4364  /* copy extradata if it exists */
4365  if (trk->vos_len == 0 && enc->extradata_size > 0 &&
4366  !TAG_IS_AVCI(trk->tag) &&
4367  (enc->codec_id != AV_CODEC_ID_DNXHD)) {
4368  trk->vos_len = enc->extradata_size;
4369  trk->vos_data = av_malloc(trk->vos_len);
4370  if (!trk->vos_data) {
4371  ret = AVERROR(ENOMEM);
4372  goto err;
4373  }
4374  memcpy(trk->vos_data, enc->extradata, trk->vos_len);
4375  }
4376 
4377  if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
4378  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
4379  if (!s->streams[pkt->stream_index]->nb_frames) {
4380  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
4381  "use the audio bitstream filter 'aac_adtstoasc' to fix it "
4382  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
4383  return -1;
4384  }
4385  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
4386  }
4387  if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1 && !TAG_IS_AVCI(trk->tag)) {
4388  /* from x264 or from bytestream h264 */
4389  /* nal reformating needed */
4390  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
4391  ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
4392  &size);
4393  avio_write(pb, reformatted_data, size);
4394  } else {
4395  size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
4396  }
4397  } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
4398  (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
4399  /* extradata is Annex B, assume the bitstream is too and convert it */
4400  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
4401  ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
4402  avio_write(pb, reformatted_data, size);
4403  } else {
4404  size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
4405  }
4406 #if CONFIG_AC3_PARSER
4407  } else if (enc->codec_id == AV_CODEC_ID_EAC3) {
4408  size = handle_eac3(mov, pkt, trk);
4409  if (size < 0)
4410  return size;
4411  else if (!size)
4412  goto end;
4413  avio_write(pb, pkt->data, size);
4414 #endif
4415  } else {
4416  avio_write(pb, pkt->data, size);
4417  }
4418 
4419  if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
4420  enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
4421  /* copy frame to create needed atoms */
4422  trk->vos_len = size;
4423  trk->vos_data = av_malloc(size);
4424  if (!trk->vos_data) {
4425  ret = AVERROR(ENOMEM);
4426  goto err;
4427  }
4428  memcpy(trk->vos_data, pkt->data, size);
4429  }
4430 
4431  if (trk->entry >= trk->cluster_capacity) {
4432  unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
4433  if (av_reallocp_array(&trk->cluster, new_capacity,
4434  sizeof(*trk->cluster))) {
4435  ret = AVERROR(ENOMEM);
4436  goto err;
4437  }
4438  trk->cluster_capacity = new_capacity;
4439  }
4440 
4441  trk->cluster[trk->entry].pos = avio_tell(pb) - size;
4442  trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
4443  trk->cluster[trk->entry].chunkNum = 0;
4444  trk->cluster[trk->entry].size = size;
4445  trk->cluster[trk->entry].entries = samples_in_chunk;
4446  trk->cluster[trk->entry].dts = pkt->dts;
4447  if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
4448  if (!trk->frag_discont) {
4449  /* First packet of a new fragment. We already wrote the duration
4450  * of the last packet of the previous fragment based on track_duration,
4451  * which might not exactly match our dts. Therefore adjust the dts
4452  * of this packet to be what the previous packets duration implies. */
4453  trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
4454  /* We also may have written the pts and the corresponding duration
4455  * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
4456  * the next fragment. This means the cts of the first sample must
4457  * be the same in all fragments. */
4458  if ((mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_FASTSTART)) ||
4459  mov->mode == MODE_ISM)
4460  pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
4461  } else {
4462  /* New fragment, but discontinuous from previous fragments.
4463  * Pretend the duration sum of the earlier fragments is
4464  * pkt->dts - trk->start_dts. */
4465  trk->frag_start = pkt->dts - trk->start_dts;
4466  trk->end_pts = AV_NOPTS_VALUE;
4467  trk->frag_discont = 0;
4468  }
4469  }
4470 
4471  if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
4473  /* Not using edit lists and shifting the first track to start from zero.
4474  * If the other streams start from a later timestamp, we won't be able
4475  * to signal the difference in starting time without an edit list.
4476  * Thus move the timestamp for this first sample to 0, increasing
4477  * its duration instead. */
4478  trk->cluster[trk->entry].dts = trk->start_dts = 0;
4479  }
4480  if (trk->start_dts == AV_NOPTS_VALUE) {
4481  trk->start_dts = pkt->dts;
4482  if (trk->frag_discont) {
4483  /* Pretend the whole stream started at dts=0, with earlier fragments
4484  * already written, with a duration summing up to pkt->dts. */
4485  trk->frag_start = pkt->dts;
4486  trk->start_dts = 0;
4487  trk->frag_discont = 0;
4488  } else if (pkt->dts && mov->moov_written)
4490  "Track %d starts with a nonzero dts %"PRId64", while the moov "
4491  "already has been written. Set the delay_moov flag to handle "
4492  "this case.\n",
4493  pkt->stream_index, pkt->dts);
4494  }
4495  trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
4496  trk->last_sample_is_subtitle_end = 0;
4497 
4498  if (pkt->pts == AV_NOPTS_VALUE) {
4499  av_log(s, AV_LOG_WARNING, "pts has no value\n");
4500  pkt->pts = pkt->dts;
4501  }
4502  if (pkt->dts != pkt->pts)
4503  trk->flags |= MOV_TRACK_CTTS;
4504  trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
4505  trk->cluster[trk->entry].flags = 0;
4506  if (trk->start_cts == AV_NOPTS_VALUE)
4507  trk->start_cts = pkt->pts - pkt->dts;
4508  if (trk->end_pts == AV_NOPTS_VALUE)
4509  trk->end_pts = trk->cluster[trk->entry].dts +
4510  trk->cluster[trk->entry].cts + pkt->duration;
4511  else
4512  trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
4513  trk->cluster[trk->entry].cts +
4514  pkt->duration);
4515 
4516  if (enc->codec_id == AV_CODEC_ID_VC1) {
4517  mov_parse_vc1_frame(pkt, trk);
4518  } else if (pkt->flags & AV_PKT_FLAG_KEY) {
4519  if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
4520  trk->entry > 0) { // force sync sample for the first key frame
4521  mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
4522  if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
4523  trk->flags |= MOV_TRACK_STPS;
4524  } else {
4525  trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
4526  }
4527  if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
4528  trk->has_keyframes++;
4529  }
4530  trk->entry++;
4531  trk->sample_count += samples_in_chunk;
4532  mov->mdat_size += size;
4533 
4534  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
4535  ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
4536  reformatted_data, size);
4537 
4538 end:
4539 err:
4540 
4541  av_free(reformatted_data);
4542  return ret;
4543 }
4544 
4546 {
4547  MOVMuxContext *mov = s->priv_data;
4548  MOVTrack *trk = &mov->tracks[pkt->stream_index];
4549  AVCodecContext *enc = trk->enc;
4550  int64_t frag_duration = 0;
4551  int size = pkt->size;
4552 
4553  if (!pkt->size)
4554  return 0; /* Discard 0 sized packets */
4555 
4556  if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
4557  int i;
4558  for (i = 0; i < s->nb_streams; i++)
4559  mov->tracks[i].frag_discont = 1;
4561  }
4562 
4563  if (trk->entry && pkt->stream_index < s->nb_streams)
4564  frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
4565  s->streams[pkt->stream_index]->time_base,
4566  AV_TIME_BASE_Q);
4567  if ((mov->max_fragment_duration &&
4568  frag_duration >= mov->max_fragment_duration) ||
4569  (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
4570  (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
4571  enc->codec_type == AVMEDIA_TYPE_VIDEO &&
4572  trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
4573  if (frag_duration >= mov->min_fragment_duration) {
4574  // Set the duration of this track to line up with the next
4575  // sample in this track. This avoids relying on AVPacket
4576  // duration, but only helps for this particular track, not
4577  // for the other ones that are flushed at the same time.
4578  trk->track_duration = pkt->dts - trk->start_dts;
4579  trk->end_pts = pkt->pts;
4581  }
4582  }
4583 
4584  return ff_mov_write_packet(s, pkt);
4585 }
4586 
4588  int stream_index,
4589  int64_t dts) {
4590  AVPacket end;
4591  uint8_t data[2] = {0};
4592  int ret;
4593 
4594  av_init_packet(&end);
4595  end.size = sizeof(data);
4596  end.data = data;
4597  end.pts = dts;
4598  end.dts = dts;
4599  end.duration = 0;
4600  end.stream_index = stream_index;
4601 
4602  ret = mov_write_single_packet(s, &end);
4603  av_free_packet(&end);
4604 
4605  return ret;
4606 }
4607 
4609 {
4610  if (!pkt) {
4611  mov_flush_fragment(s);
4612  return 1;
4613  } else {
4614  int i;
4615  MOVMuxContext *mov = s->priv_data;
4616 
4617  if (!pkt->size) return 0; /* Discard 0 sized packets */
4618 
4619  /*
4620  * Subtitles require special handling.
4621  *
4622  * 1) For full complaince, every track must have a sample at
4623  * dts == 0, which is rarely true for subtitles. So, as soon
4624  * as we see any packet with dts > 0, write an empty subtitle
4625  * at dts == 0 for any subtitle track with no samples in it.
4626  *
4627  * 2) For each subtitle track, check if the current packet's
4628  * dts is past the duration of the last subtitle sample. If
4629  * so, we now need to write an end sample for that subtitle.
4630  *
4631  * This must be done conditionally to allow for subtitles that
4632  * immediately replace each other, in which case an end sample
4633  * is not needed, and is, in fact, actively harmful.
4634  *
4635  * 3) See mov_write_trailer for how the final end sample is
4636  * handled.
4637  */
4638  for (i = 0; i < mov->nb_streams; i++) {
4639  MOVTrack *trk = &mov->tracks[i];
4640  int ret;
4641 
4642  if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
4643  trk->track_duration < pkt->dts &&
4644  (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
4646  if (ret < 0) return ret;
4647  trk->last_sample_is_subtitle_end = 1;
4648  }
4649  }
4650 
4651  return mov_write_single_packet(s, pkt);
4652  }
4653 }
4654 
4655 // QuickTime chapters involve an additional text track with the chapter names
4656 // as samples, and a tref pointing from the other tracks to the chapter one.
4657 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
4658 {
4659  AVIOContext *pb;
4660 
4661  MOVMuxContext *mov = s->priv_data;
4662  MOVTrack *track = &mov->tracks[tracknum];
4663  AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
4664  int i, len;
4665 
4666  track->mode = mov->mode;
4667  track->tag = MKTAG('t','e','x','t');
4668  track->timescale = MOV_TIMESCALE;
4669  track->enc = avcodec_alloc_context3(NULL);
4670  if (!track->enc)
4671  return AVERROR(ENOMEM);
4673 #if 0
4674  // These properties are required to make QT recognize the chapter track
4675  uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
4676  if (ff_alloc_extradata(track->enc, sizeof(chapter_properties)))
4677  return AVERROR(ENOMEM);
4678  memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
4679 #else
4680  if (avio_open_dyn_buf(&pb) >= 0) {
4681  int size;
4682  uint8_t *buf;
4683 
4684  /* Stub header (usually for Quicktime chapter track) */
4685  // TextSampleEntry
4686  avio_wb32(pb, 0x01); // displayFlags
4687  avio_w8(pb, 0x00); // horizontal justification
4688  avio_w8(pb, 0x00); // vertical justification
4689  avio_w8(pb, 0x00); // bgColourRed
4690  avio_w8(pb, 0x00); // bgColourGreen
4691  avio_w8(pb, 0x00); // bgColourBlue
4692  avio_w8(pb, 0x00); // bgColourAlpha
4693  // BoxRecord
4694  avio_wb16(pb, 0x00); // defTextBoxTop
4695  avio_wb16(pb, 0x00); // defTextBoxLeft
4696  avio_wb16(pb, 0x00); // defTextBoxBottom
4697  avio_wb16(pb, 0x00); // defTextBoxRight
4698  // StyleRecord
4699  avio_wb16(pb, 0x00); // startChar
4700  avio_wb16(pb, 0x00); // endChar
4701  avio_wb16(pb, 0x01); // fontID
4702  avio_w8(pb, 0x00); // fontStyleFlags
4703  avio_w8(pb, 0x00); // fontSize
4704  avio_w8(pb, 0x00); // fgColourRed
4705  avio_w8(pb, 0x00); // fgColourGreen
4706  avio_w8(pb, 0x00); // fgColourBlue
4707  avio_w8(pb, 0x00); // fgColourAlpha
4708  // FontTableBox
4709  avio_wb32(pb, 0x0D); // box size
4710  ffio_wfourcc(pb, "ftab"); // box atom name
4711  avio_wb16(pb, 0x01); // entry count
4712  // FontRecord
4713  avio_wb16(pb, 0x01); // font ID
4714  avio_w8(pb, 0x00); // font name length
4715 
4716  if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
4717  track->enc->extradata = buf;
4718  track->enc->extradata_size = size;
4719  } else {
4720  av_freep(&buf);
4721  }
4722  }
4723 #endif
4724 
4725  for (i = 0; i < s->nb_chapters; i++) {
4726  AVChapter *c = s->chapters[i];
4727  AVDictionaryEntry *t;
4728 
4729  int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
4730  pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
4731  pkt.duration = end - pkt.dts;
4732 
4733  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
4734  const char encd[12] = {
4735  0x00, 0x00, 0x00, 0x0C,
4736  'e', 'n', 'c', 'd',
4737  0x00, 0x00, 0x01, 0x00 };
4738  len = strlen(t->value);
4739  pkt.size = len + 2 + 12;
4740  pkt.data = av_malloc(pkt.size);
4741  if (!pkt.data)
4742  return AVERROR(ENOMEM);
4743  AV_WB16(pkt.data, len);
4744  memcpy(pkt.data + 2, t->value, len);
4745  memcpy(pkt.data + len + 2, encd, sizeof(encd));
4746  ff_mov_write_packet(s, &pkt);
4747  av_freep(&pkt.data);
4748  }
4749  }
4750 
4751  return 0;
4752 }
4753 
4754 static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
4755 {
4756  int ret;
4757  MOVMuxContext *mov = s->priv_data;
4758  MOVTrack *track = &mov->tracks[index];
4759  AVStream *src_st = s->streams[src_index];
4760  AVTimecode tc;
4761  AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
4762  AVRational rate = find_fps(s, src_st);
4763 
4764  /* compute the frame number */
4765  ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
4766  if (ret < 0)
4767  return ret;
4768 
4769  /* tmcd track based on video stream */
4770  track->mode = mov->mode;
4771  track->tag = MKTAG('t','m','c','d');
4772  track->src_track = src_index;
4773  track->timescale = mov->tracks[src_index].timescale;
4776 
4777  /* set st to src_st for metadata access*/
4778  track->st = src_st;
4779 
4780  /* encode context: tmcd data stream */
4781  track->enc = avcodec_alloc_context3(NULL);
4782  if (!track->enc)
4783  return AVERROR(ENOMEM);
4784  track->enc->codec_type = AVMEDIA_TYPE_DATA;
4785  track->enc->codec_tag = track->tag;
4786  track->enc->time_base = av_inv_q(rate);
4787 
4788  /* the tmcd track just contains one packet with the frame number */
4789  pkt.data = av_malloc(pkt.size);
4790  if (!pkt.data)
4791  return AVERROR(ENOMEM);
4792  AV_WB32(pkt.data, tc.start);
4793  ret = ff_mov_write_packet(s, &pkt);
4794  av_free(pkt.data);
4795  return ret;
4796 }
4797 
4798 /*
4799  * st->disposition controls the "enabled" flag in the tkhd tag.
4800  * QuickTime will not play a track if it is not enabled. So make sure
4801  * that one track of each type (audio, video, subtitle) is enabled.
4802  *
4803  * Subtitles are special. For audio and video, setting "enabled" also
4804  * makes the track "default" (i.e. it is rendered when played). For
4805  * subtitles, an "enabled" subtitle is not rendered by default, but
4806  * if no subtitle is enabled, the subtitle menu in QuickTime will be
4807  * empty!
4808  */
4810 {
4811  MOVMuxContext *mov = s->priv_data;
4812  int i;
4813  int enabled[AVMEDIA_TYPE_NB];
4814  int first[AVMEDIA_TYPE_NB];
4815 
4816  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
4817  enabled[i] = 0;
4818  first[i] = -1;
4819  }
4820 
4821  for (i = 0; i < s->nb_streams; i++) {
4822  AVStream *st = s->streams[i];
4823 
4824  if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
4825  st->codec->codec_type >= AVMEDIA_TYPE_NB)
4826  continue;
4827 
4828  if (first[st->codec->codec_type] < 0)
4829  first[st->codec->codec_type] = i;
4830  if (st->disposition & AV_DISPOSITION_DEFAULT) {
4831  mov->tracks[i].flags |= MOV_TRACK_ENABLED;
4832  enabled[st->codec->codec_type]++;
4833  }
4834  }
4835 
4836  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
4837  switch (i) {
4838  case AVMEDIA_TYPE_VIDEO:
4839  case AVMEDIA_TYPE_AUDIO:
4840  case AVMEDIA_TYPE_SUBTITLE:
4841  if (enabled[i] > 1)
4842  mov->per_stream_grouping = 1;
4843  if (!enabled[i] && first[i] >= 0)
4844  mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
4845  break;
4846  }
4847  }
4848 }
4849 
4851 {
4852  MOVMuxContext *mov = s->priv_data;
4853  int i;
4854 
4855  if (mov->chapter_track) {
4856  if (mov->tracks[mov->chapter_track].enc)
4857  av_freep(&mov->tracks[mov->chapter_track].enc->extradata);
4858  av_freep(&mov->tracks[mov->chapter_track].enc);
4859  }
4860 
4861  for (i = 0; i < mov->nb_streams; i++) {
4862  if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
4863  ff_mov_close_hinting(&mov->tracks[i]);
4864  else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
4865  av_freep(&mov->tracks[i].enc);
4866  av_freep(&mov->tracks[i].cluster);
4867  av_freep(&mov->tracks[i].frag_info);
4868 
4869  if (mov->tracks[i].vos_len)
4870  av_freep(&mov->tracks[i].vos_data);
4871  }
4872 
4873  av_freep(&mov->tracks);
4874 }
4875 
4876 static uint32_t rgb_to_yuv(uint32_t rgb)
4877 {
4878  uint8_t r, g, b;
4879  int y, cb, cr;
4880 
4881  r = (rgb >> 16) & 0xFF;
4882  g = (rgb >> 8) & 0xFF;
4883  b = (rgb ) & 0xFF;
4884 
4885  y = av_clip_uint8(( 16000 + 257 * r + 504 * g + 98 * b)/1000);
4886  cb = av_clip_uint8((128000 - 148 * r - 291 * g + 439 * b)/1000);
4887  cr = av_clip_uint8((128000 + 439 * r - 368 * g - 71 * b)/1000);
4888 
4889  return (y << 16) | (cr << 8) | cb;
4890 }
4891 
4893  AVStream *st)
4894 {
4895  int i, width = 720, height = 480;
4896  int have_palette = 0, have_size = 0;
4897  uint32_t palette[16];
4898  char *cur = st->codec->extradata;
4899 
4900  while (cur && *cur) {
4901  if (strncmp("palette:", cur, 8) == 0) {
4902  int i, count;
4903  count = sscanf(cur + 8,
4904  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
4905  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
4906  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
4907  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
4908  &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
4909  &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
4910  &palette[ 8], &palette[ 9], &palette[10], &palette[11],
4911  &palette[12], &palette[13], &palette[14], &palette[15]);
4912 
4913  for (i = 0; i < count; i++) {
4914  palette[i] = rgb_to_yuv(palette[i]);
4915  }
4916  have_palette = 1;
4917  } else if (!strncmp("size:", cur, 5)) {
4918  sscanf(cur + 5, "%dx%d", &width, &height);
4919  have_size = 1;
4920  }
4921  if (have_palette && have_size)
4922  break;
4923  cur += strcspn(cur, "\n\r");
4924  cur += strspn(cur, "\n\r");
4925  }
4926  if (have_palette) {
4927  track->vos_data = av_malloc(16*4);
4928  if (!track->vos_data)
4929  return AVERROR(ENOMEM);
4930  for (i = 0; i < 16; i++) {
4931  AV_WB32(track->vos_data + i * 4, palette[i]);
4932  }
4933  track->vos_len = 16 * 4;
4934  }
4935  st->codec->width = width;
4936  st->codec->height = track->height = height;
4937 
4938  return 0;
4939 }
4940 
4942 {
4943  AVIOContext *pb = s->pb;
4944  MOVMuxContext *mov = s->priv_data;
4945  AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
4946  int i, ret, hint_track = 0, tmcd_track = 0;
4947 
4948  mov->fc = s;
4949 
4950  /* Default mode == MP4 */
4951  mov->mode = MODE_MP4;
4952 
4953  if (s->oformat) {
4954  if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
4955  else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
4956  else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
4957  else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
4958  else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
4959  else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
4960  else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
4961  }
4962 
4963  if (s->flags & AVFMT_FLAG_BITEXACT)
4964  mov->exact = 1;
4965 
4966  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
4967  mov->flags |= FF_MOV_FLAG_EMPTY_MOOV;
4968 
4969  /* Set the FRAGMENT flag if any of the fragmentation methods are
4970  * enabled. */
4971  if (mov->max_fragment_duration || mov->max_fragment_size ||
4972  mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
4975  mov->flags |= FF_MOV_FLAG_FRAGMENT;
4976 
4977  /* Set other implicit flags immediately */
4978  if (mov->mode == MODE_ISM)
4981  if (mov->flags & FF_MOV_FLAG_DASH)
4984 
4985  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
4986  mov->reserved_moov_size = -1;
4987  }
4988 
4989  if (mov->use_editlist < 0) {
4990  mov->use_editlist = 1;
4991  if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
4992  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
4993  // If we can avoid needing an edit list by shifting the
4994  // tracks, prefer that over (trying to) write edit lists
4995  // in fragmented output.
4998  mov->use_editlist = 0;
4999  }
5000  }
5001  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
5002  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
5003  av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
5004 
5007 
5008  /* Clear the omit_tfhd_offset flag if default_base_moof is set;
5009  * if the latter is set that's enough and omit_tfhd_offset doesn't
5010  * add anything extra on top of that. */
5011  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
5014 
5015  if (mov->frag_interleave &&
5017  av_log(s, AV_LOG_ERROR,
5018  "Sample interleaving in fragments is mutually exclusive with "
5019  "omit_tfhd_offset and separate_moof\n");
5020  return AVERROR(EINVAL);
5021  }
5022 
5023  /* Non-seekable output is ok if using fragmentation. If ism_lookahead
5024  * is enabled, we don't support non-seekable output at all. */
5025  if (!s->pb->seekable &&
5026  (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
5027  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
5028  return AVERROR(EINVAL);
5029  }
5030 
5031  if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
5032  if ((ret = mov_write_identification(pb, s)) < 0)
5033  return ret;
5034  }
5035 
5036  mov->nb_streams = s->nb_streams;
5037  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
5038  mov->chapter_track = mov->nb_streams++;
5039 
5040  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
5041  /* Add hint tracks for each audio and video stream */
5042  hint_track = mov->nb_streams;
5043  for (i = 0; i < s->nb_streams; i++) {
5044  AVStream *st = s->streams[i];
5045  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
5047  mov->nb_streams++;
5048  }
5049  }
5050  }
5051 
5052  if (mov->mode == MODE_MOV) {
5053  tmcd_track = mov->nb_streams;
5054 
5055  /* +1 tmcd track for each video stream with a timecode */
5056  for (i = 0; i < s->nb_streams; i++) {
5057  AVStream *st = s->streams[i];
5058  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
5059  (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
5060  mov->nb_meta_tmcd++;
5061  }
5062 
5063  /* check if there is already a tmcd track to remux */
5064  if (mov->nb_meta_tmcd) {
5065  for (i = 0; i < s->nb_streams; i++) {
5066  AVStream *st = s->streams[i];
5067  if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
5068  av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
5069  "so timecode metadata are now ignored\n");
5070  mov->nb_meta_tmcd = 0;
5071  }
5072  }
5073  }
5074 
5075  mov->nb_streams += mov->nb_meta_tmcd;
5076  }
5077 
5078  // Reserve an extra stream for chapters for the case where chapters
5079  // are written in the trailer
5080  mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks));
5081  if (!mov->tracks)
5082  return AVERROR(ENOMEM);
5083 
5084  for (i = 0; i < s->nb_streams; i++) {
5085  AVStream *st= s->streams[i];
5086  MOVTrack *track= &mov->tracks[i];
5087  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
5088 
5089  track->st = st;
5090  track->enc = st->codec;
5091  track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
5092  if (track->language < 0)
5093  track->language = 0;
5094  track->mode = mov->mode;
5095  track->tag = mov_find_codec_tag(s, track);
5096  if (!track->tag) {
5097  av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
5098  "codec not currently supported in container\n",
5099  avcodec_get_name(st->codec->codec_id), i);
5100  ret = AVERROR(EINVAL);
5101  goto error;
5102  }
5103  /* If hinting of this track is enabled by a later hint track,
5104  * this is updated. */
5105  track->hint_track = -1;
5106  track->start_dts = AV_NOPTS_VALUE;
5107  track->start_cts = AV_NOPTS_VALUE;
5108  track->end_pts = AV_NOPTS_VALUE;
5109  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
5110  if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
5111  track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
5112  track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
5113  if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
5114  av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
5115  ret = AVERROR(EINVAL);
5116  goto error;
5117  }
5118  track->height = track->tag >> 24 == 'n' ? 486 : 576;
5119  }
5120  if (mov->video_track_timescale) {
5121  track->timescale = mov->video_track_timescale;
5122  } else {
5123  track->timescale = st->time_base.den;
5124  while(track->timescale < 10000)
5125  track->timescale *= 2;
5126  }
5127  if (st->codec->width > 65535 || st->codec->height > 65535) {
5128  av_log(s, AV_LOG_ERROR, "Resolution %dx%d too large for mov/mp4\n", st->codec->width, st->codec->height);
5129  ret = AVERROR(EINVAL);
5130  goto error;
5131  }
5132  if (track->mode == MODE_MOV && track->timescale > 100000)
5134  "WARNING codec timebase is very high. If duration is too long,\n"
5135  "file may not be playable by quicktime. Specify a shorter timebase\n"
5136  "or choose different container.\n");
5137  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
5138  track->timescale = st->codec->sample_rate;
5139  if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
5140  av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
5141  track->audio_vbr = 1;
5142  }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
5144  st->codec->codec_id == AV_CODEC_ID_ILBC){
5145  if (!st->codec->block_align) {
5146  av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
5147  ret = AVERROR(EINVAL);
5148  goto error;
5149  }
5150  track->sample_size = st->codec->block_align;
5151  }else if (st->codec->frame_size > 1){ /* assume compressed audio */
5152  track->audio_vbr = 1;
5153  }else{
5154  track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
5155  }
5156  if (st->codec->codec_id == AV_CODEC_ID_ILBC ||
5158  track->audio_vbr = 1;
5159  }
5160  if (track->mode != MODE_MOV &&
5161  track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
5163  av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not standard, to mux anyway set strict to -1\n",
5164  i, track->enc->sample_rate);
5165  ret = AVERROR(EINVAL);
5166  goto error;
5167  } else {
5168  av_log(s, AV_LOG_WARNING, "track %d: muxing mp3 at %dhz is not standard in MP4\n",
5169  i, track->enc->sample_rate);
5170  }
5171  }
5172  } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
5173  track->timescale = st->time_base.den;
5174  } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
5175  track->timescale = st->time_base.den;
5176  } else {
5177  track->timescale = MOV_TIMESCALE;
5178  }
5179  if (!track->height)
5180  track->height = st->codec->height;
5181  /* The ism specific timescale isn't mandatory, but is assumed by
5182  * some tools, such as mp4split. */
5183  if (mov->mode == MODE_ISM)
5184  track->timescale = 10000000;
5185 
5186  avpriv_set_pts_info(st, 64, 1, track->timescale);
5187 
5188  /* copy extradata if it exists */
5189  if (st->codec->extradata_size) {
5192  else if (!TAG_IS_AVCI(track->tag) && st->codec->codec_id != AV_CODEC_ID_DNXHD) {
5193  track->vos_len = st->codec->extradata_size;
5194  track->vos_data = av_malloc(track->vos_len);
5195  if (!track->vos_data) {
5196  ret = AVERROR(ENOMEM);
5197  goto error;
5198  }
5199  memcpy(track->vos_data, st->codec->extradata, track->vos_len);
5200  }
5201  }
5202  }
5203 
5204  for (i = 0; i < s->nb_streams; i++) {
5205  int j;
5206  AVStream *st= s->streams[i];
5207  MOVTrack *track= &mov->tracks[i];
5208 
5209  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
5211  continue;
5212 
5213  for (j = 0; j < s->nb_streams; j++) {
5214  AVStream *stj= s->streams[j];
5215  MOVTrack *trackj= &mov->tracks[j];
5216  if (j == i)
5217  continue;
5218 
5219  if (stj->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
5220  trackj->enc->channel_layout != AV_CH_LAYOUT_MONO ||
5221  trackj->language != track->language ||
5222  trackj->tag != track->tag
5223  )
5224  continue;
5225  track->multichannel_as_mono++;
5226  }
5227  }
5228 
5229  enable_tracks(s);
5230 
5231 
5232  if (mov->reserved_moov_size){
5233  mov->reserved_moov_pos= avio_tell(pb);
5234  if (mov->reserved_moov_size > 0)
5235  avio_skip(pb, mov->reserved_moov_size);
5236  }
5237 
5238  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
5239  /* If no fragmentation options have been set, set a default. */
5240  if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
5244  } else {
5245  if (mov->flags & FF_MOV_FLAG_FASTSTART)
5246  mov->reserved_moov_pos = avio_tell(pb);
5247  mov_write_mdat_tag(pb, mov);
5248  }
5249 
5250  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
5251  mov->time = ff_iso8601_to_unix_time(t->value);
5252  if (mov->time)
5253  mov->time += 0x7C25B080; // 1970 based -> 1904 based
5254 
5255  if (mov->chapter_track)
5256  if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
5257  goto error;
5258 
5259  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
5260  /* Initialize the hint tracks for each audio and video stream */
5261  for (i = 0; i < s->nb_streams; i++) {
5262  AVStream *st = s->streams[i];
5263  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
5265  if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
5266  goto error;
5267  hint_track++;
5268  }
5269  }
5270  }
5271 
5272  if (mov->nb_meta_tmcd) {
5273  /* Initialize the tmcd tracks */
5274  for (i = 0; i < s->nb_streams; i++) {
5275  AVStream *st = s->streams[i];
5276  t = global_tcr;
5277 
5278  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
5279  if (!t)
5280  t = av_dict_get(st->metadata, "timecode", NULL, 0);
5281  if (!t)
5282  continue;
5283  if ((ret = mov_create_timecode_track(s, tmcd_track, i, t->value)) < 0)
5284  goto error;
5285  tmcd_track++;
5286  }
5287  }
5288  }
5289 
5290  avio_flush(pb);
5291 
5292  if (mov->flags & FF_MOV_FLAG_ISML)
5293  mov_write_isml_manifest(pb, mov);
5294 
5295  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
5296  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
5297  if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
5298  return ret;
5299  mov->moov_written = 1;
5300  if (mov->flags & FF_MOV_FLAG_FASTSTART)
5301  mov->reserved_moov_pos = avio_tell(pb);
5302  }
5303 
5304  return 0;
5305  error:
5306  mov_free(s);
5307  return ret;
5308 }
5309 
5311 {
5312  int ret;
5313  AVIOContext *moov_buf;
5314  MOVMuxContext *mov = s->priv_data;
5315 
5316  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
5317  return ret;
5318  if ((ret = mov_write_moov_tag(moov_buf, mov, s)) < 0)
5319  return ret;
5320  return ffio_close_null_buf(moov_buf);
5321 }
5322 
5324 {
5325  int ret;
5326  AVIOContext *buf;
5327  MOVMuxContext *mov = s->priv_data;
5328 
5329  if ((ret = ffio_open_null_buf(&buf)) < 0)
5330  return ret;
5331  mov_write_sidx_tags(buf, mov, -1, 0);
5332  return ffio_close_null_buf(buf);
5333 }
5334 
5335 /*
5336  * This function gets the moov size if moved to the top of the file: the chunk
5337  * offset table can switch between stco (32-bit entries) to co64 (64-bit
5338  * entries) when the moov is moved to the beginning, so the size of the moov
5339  * would change. It also updates the chunk offset tables.
5340  */
5342 {
5343  int i, moov_size, moov_size2;
5344  MOVMuxContext *mov = s->priv_data;
5345 
5346  moov_size = get_moov_size(s);
5347  if (moov_size < 0)
5348  return moov_size;
5349 
5350  for (i = 0; i < mov->nb_streams; i++)
5351  mov->tracks[i].data_offset += moov_size;
5352 
5353  moov_size2 = get_moov_size(s);
5354  if (moov_size2 < 0)
5355  return moov_size2;
5356 
5357  /* if the size changed, we just switched from stco to co64 and need to
5358  * update the offsets */
5359  if (moov_size2 != moov_size)
5360  for (i = 0; i < mov->nb_streams; i++)
5361  mov->tracks[i].data_offset += moov_size2 - moov_size;
5362 
5363  return moov_size2;
5364 }
5365 
5367 {
5368  int i, sidx_size;
5369  MOVMuxContext *mov = s->priv_data;
5370 
5371  sidx_size = get_sidx_size(s);
5372  if (sidx_size < 0)
5373  return sidx_size;
5374 
5375  for (i = 0; i < mov->nb_streams; i++)
5376  mov->tracks[i].data_offset += sidx_size;
5377 
5378  return sidx_size;
5379 }
5380 
5382 {
5383  int ret = 0, moov_size;
5384  MOVMuxContext *mov = s->priv_data;
5385  int64_t pos, pos_end = avio_tell(s->pb);
5386  uint8_t *buf, *read_buf[2];
5387  int read_buf_id = 0;
5388  int read_size[2];
5389  AVIOContext *read_pb;
5390 
5391  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
5392  moov_size = compute_sidx_size(s);
5393  else
5394  moov_size = compute_moov_size(s);
5395  if (moov_size < 0)
5396  return moov_size;
5397 
5398  buf = av_malloc(moov_size * 2);
5399  if (!buf)
5400  return AVERROR(ENOMEM);
5401  read_buf[0] = buf;
5402  read_buf[1] = buf + moov_size;
5403 
5404  /* Shift the data: the AVIO context of the output can only be used for
5405  * writing, so we re-open the same output, but for reading. It also avoids
5406  * a read/seek/write/seek back and forth. */
5407  avio_flush(s->pb);
5408  ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
5409  if (ret < 0) {
5410  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
5411  "the second pass (faststart)\n", s->filename);
5412  goto end;
5413  }
5414 
5415  /* mark the end of the shift to up to the last data we wrote, and get ready
5416  * for writing */
5417  pos_end = avio_tell(s->pb);
5418  avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
5419 
5420  /* start reading at where the new moov will be placed */
5421  avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
5422  pos = avio_tell(read_pb);
5423 
5424 #define READ_BLOCK do { \
5425  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
5426  read_buf_id ^= 1; \
5427 } while (0)
5428 
5429  /* shift data by chunk of at most moov_size */
5430  READ_BLOCK;
5431  do {
5432  int n;
5433  READ_BLOCK;
5434  n = read_size[read_buf_id];
5435  if (n <= 0)
5436  break;
5437  avio_write(s->pb, read_buf[read_buf_id], n);
5438  pos += n;
5439  } while (pos < pos_end);
5440  avio_close(read_pb);
5441 
5442 end:
5443  av_free(buf);
5444  return ret;
5445 }
5446 
5448 {
5449  MOVMuxContext *mov = s->priv_data;
5450  AVIOContext *pb = s->pb;
5451  int res = 0;
5452  int i;
5453  int64_t moov_pos;
5454 
5455  /*
5456  * Before actually writing the trailer, make sure that there are no
5457  * dangling subtitles, that need a terminating sample.
5458  */
5459  for (i = 0; i < mov->nb_streams; i++) {
5460  MOVTrack *trk = &mov->tracks[i];
5461  if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
5464  trk->last_sample_is_subtitle_end = 1;
5465  }
5466  }
5467 
5468  // If there were no chapters when the header was written, but there
5469  // are chapters now, write them in the trailer. This only works
5470  // when we are not doing fragments.
5471  if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
5472  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
5473  mov->chapter_track = mov->nb_streams++;
5474  if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
5475  goto error;
5476  }
5477  }
5478 
5479  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
5480  moov_pos = avio_tell(pb);
5481 
5482  /* Write size of mdat tag */
5483  if (mov->mdat_size + 8 <= UINT32_MAX) {
5484  avio_seek(pb, mov->mdat_pos, SEEK_SET);
5485  avio_wb32(pb, mov->mdat_size + 8);
5486  } else {
5487  /* overwrite 'wide' placeholder atom */
5488  avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
5489  /* special value: real atom size will be 64 bit value after
5490  * tag field */
5491  avio_wb32(pb, 1);
5492  ffio_wfourcc(pb, "mdat");
5493  avio_wb64(pb, mov->mdat_size + 16);
5494  }
5495  avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
5496 
5497  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
5498  av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
5499  res = shift_data(s);
5500  if (res == 0) {
5501  avio_seek(pb, mov->reserved_moov_pos, SEEK_SET);
5502  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
5503  goto error;
5504  }
5505  } else if (mov->reserved_moov_size > 0) {
5506  int64_t size;
5507  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
5508  goto error;
5509  size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
5510  if (size < 8){
5511  av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
5512  res = AVERROR(EINVAL);
5513  goto error;
5514  }
5515  avio_wb32(pb, size);
5516  ffio_wfourcc(pb, "free");
5517  ffio_fill(pb, 0, size - 8);
5518  avio_seek(pb, moov_pos, SEEK_SET);
5519  } else {
5520  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
5521  goto error;
5522  }
5523  res = 0;
5524  } else {
5526  for (i = 0; i < mov->nb_streams; i++)
5527  mov->tracks[i].data_offset = 0;
5528  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
5529  av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
5530  res = shift_data(s);
5531  if (res == 0) {
5532  int64_t end = avio_tell(pb);
5533  avio_seek(pb, mov->reserved_moov_pos, SEEK_SET);
5534  mov_write_sidx_tags(pb, mov, -1, 0);
5535  avio_seek(pb, end, SEEK_SET);
5536  mov_write_mfra_tag(pb, mov);
5537  }
5538  } else {
5539  mov_write_mfra_tag(pb, mov);
5540  }
5541  }
5542 
5543 error:
5544  mov_free(s);
5545 
5546  return res;
5547 }
5548 
5549 #if CONFIG_MOV_MUXER
5550 MOV_CLASS(mov)
5551 AVOutputFormat ff_mov_muxer = {
5552  .name = "mov",
5553  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
5554  .extensions = "mov",
5555  .priv_data_size = sizeof(MOVMuxContext),
5556  .audio_codec = AV_CODEC_ID_AAC,
5557  .video_codec = CONFIG_LIBX264_ENCODER ?
5563  .codec_tag = (const AVCodecTag* const []){
5565  },
5566  .priv_class = &mov_muxer_class,
5567 };
5568 #endif
5569 #if CONFIG_TGP_MUXER
5570 MOV_CLASS(tgp)
5571 AVOutputFormat ff_tgp_muxer = {
5572  .name = "3gp",
5573  .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
5574  .extensions = "3gp",
5575  .priv_data_size = sizeof(MOVMuxContext),
5576  .audio_codec = AV_CODEC_ID_AMR_NB,
5577  .video_codec = AV_CODEC_ID_H263,
5582  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
5583  .priv_class = &tgp_muxer_class,
5584 };
5585 #endif
5586 #if CONFIG_MP4_MUXER
5587 MOV_CLASS(mp4)
5588 AVOutputFormat ff_mp4_muxer = {
5589  .name = "mp4",
5590  .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
5591  .mime_type = "application/mp4",
5592  .extensions = "mp4",
5593  .priv_data_size = sizeof(MOVMuxContext),
5594  .audio_codec = AV_CODEC_ID_AAC,
5595  .video_codec = CONFIG_LIBX264_ENCODER ?
5596  AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
5601  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
5602  .priv_class = &mp4_muxer_class,
5603 };
5604 #endif
5605 #if CONFIG_PSP_MUXER
5606 MOV_CLASS(psp)
5607 AVOutputFormat ff_psp_muxer = {
5608  .name = "psp",
5609  .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
5610  .extensions = "mp4,psp",
5611  .priv_data_size = sizeof(MOVMuxContext),
5612  .audio_codec = AV_CODEC_ID_AAC,
5613  .video_codec = CONFIG_LIBX264_ENCODER ?
5614  AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
5619  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
5620  .priv_class = &psp_muxer_class,
5621 };
5622 #endif
5623 #if CONFIG_TG2_MUXER
5624 MOV_CLASS(tg2)
5625 AVOutputFormat ff_tg2_muxer = {
5626  .name = "3g2",
5627  .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
5628  .extensions = "3g2",
5629  .priv_data_size = sizeof(MOVMuxContext),
5630  .audio_codec = AV_CODEC_ID_AMR_NB,
5631  .video_codec = AV_CODEC_ID_H263,
5636  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
5637  .priv_class = &tg2_muxer_class,
5638 };
5639 #endif
5640 #if CONFIG_IPOD_MUXER
5641 MOV_CLASS(ipod)
5642 AVOutputFormat ff_ipod_muxer = {
5643  .name = "ipod",
5644  .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
5645  .mime_type = "application/mp4",
5646  .extensions = "m4v,m4a",
5647  .priv_data_size = sizeof(MOVMuxContext),
5648  .audio_codec = AV_CODEC_ID_AAC,
5649  .video_codec = AV_CODEC_ID_H264,
5654  .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
5655  .priv_class = &ipod_muxer_class,
5656 };
5657 #endif
5658 #if CONFIG_ISMV_MUXER
5659 MOV_CLASS(ismv)
5660 AVOutputFormat ff_ismv_muxer = {
5661  .name = "ismv",
5662  .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
5663  .mime_type = "application/mp4",
5664  .extensions = "ismv,isma",
5665  .priv_data_size = sizeof(MOVMuxContext),
5666  .audio_codec = AV_CODEC_ID_AAC,
5667  .video_codec = AV_CODEC_ID_H264,
5672  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
5673  .priv_class = &ismv_muxer_class,
5674 };
5675 #endif
5676 #if CONFIG_F4V_MUXER
5677 MOV_CLASS(f4v)
5678 AVOutputFormat ff_f4v_muxer = {
5679  .name = "f4v",
5680  .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
5681  .mime_type = "application/f4v",
5682  .extensions = "f4v",
5683  .priv_data_size = sizeof(MOVMuxContext),
5684  .audio_codec = AV_CODEC_ID_AAC,
5685  .video_codec = AV_CODEC_ID_H264,
5690  .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
5691  .priv_class = &f4v_muxer_class,
5692 };
5693 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1471
#define MOV_TRACK_STPS
Definition: movenc.h:92
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:909
static int utf8len(const uint8_t *b)
Definition: movenc.c:98
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:502
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
This function writes extradata "as is".
Definition: movenc.c:510
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:83
int ff_mov_iso639_to_lang(const char lang[4], int mp4)
Definition: isom.c:351
uint8_t bitstream_mode
Definition: ac3.h:184
internal header for HEVC (de)muxer utilities
#define FF_MOV_FLAG_DELAY_MOOV
Definition: movenc.h:212
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:410
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:202
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov, int tracks, int moof_size)
Definition: movenc.c:3638
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition: movenc.c:2982
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int packet_entry
Definition: movenc.h:147
#define FF_MOV_FLAG_FASTSTART
Definition: movenc.h:206
long chunkCount
Definition: movenc.h:89
static int get_cluster_duration(MOVTrack *track, int cluster_idx)
Definition: movenc.c:852
static int mov_write_dinf_tag(AVIOContext *pb)
Definition: movenc.c:1985
Buffered I/O operations.
int use_editlist
Definition: movenc.h:192
#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:334
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
AVCodecContext * enc
Definition: movenc.h:103
int tag
stsd fourcc
Definition: movenc.h:101
static AVRational find_fps(AVFormatContext *s, AVStream *st)
Definition: movenc.c:1198
int language
Definition: movenc.h:99
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1125
AVOption.
Definition: opt.h:255
static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3586
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1183
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:254
static void mov_write_psp_udta_tag(AVIOContext *pb, const char *str, const char *lang, int type)
Definition: movenc.c:3103
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:217
unsigned int entries
Definition: movenc.h:51
AVIOContext * mdat_buf
Definition: movenc.h:179
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:63
static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1799
Definition: isom.h:45
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
static void param_write_int(AVIOContext *pb, const char *name, int value)
Definition: movenc.c:3242
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define FF_MOV_FLAG_FRAGMENT
Definition: movenc.h:200
static void find_compressor(char *compressor_name, int len, MOVTrack *track)
Definition: movenc.c:1626
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:65
static int mov_write_subtitle_end_packet(AVFormatContext *s, int stream_index, int64_t dts)
Definition: movenc.c:4587
static int mov_write_enda_tag_be(AVIOContext *pb)
Definition: movenc.c:524
int nb_frag_info
Definition: movenc.h:137
int max_fragment_size
Definition: movenc.h:177
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:4006
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:217
const char * g
Definition: vf_curves.c:108
static int mov_write_dref_tag(AVIOContext *pb)
Definition: movenc.c:1943
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int long_style)
Definition: movenc.c:2812
static const AVCodecTag codec_f4v_tags[]
Definition: movenc.c:1433
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:506
static int mov_pcm_be_gt16(enum AVCodecID codec_id)
Definition: movenc.c:614
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:401
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1960
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1353
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:914
int num
numerator
Definition: rational.h:44
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:117
int index
stream index in AVFormatContext
Definition: avformat.h:843
int size
Definition: avcodec.h:1163
const char * b
Definition: vf_curves.c:109
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:228
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:203
#define AVIO_FLAG_READ
read-only
Definition: avio.h:460
#define av_bswap16
Definition: bswap.h:31
static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1109
static int mov_write_mdia_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2252
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1623
static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:622
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above ...
Definition: pixfmt.h:507
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
uint8_t * av_stream_get_side_data(AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:4476
#define tc
Definition: regdef.h:69
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:87
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1514
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:276
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2703
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:2990
#define FF_ARRAY_ELEMS(a)
int version
Definition: avisynth_c.h:629
#define FF_MOV_FLAG_ISML
Definition: movenc.h:205
static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2002
static int mov_write_track_metadata(AVIOContext *pb, AVStream *st, const char *tag, const char *str)
Definition: movenc.c:2555
static AVPacket pkt
#define MOV_TFHD_DURATION_IS_EMPTY
Definition: isom.h:220
static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1654
AVStream * st
Definition: movenc.h:102
static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1552
static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
Definition: movenc.c:1780
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:85
static uint16_t language_code(const char *str)
Definition: movenc.c:2993
struct MOVTrack::@161 vc1_info
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:481
static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2095
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1113
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2022
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1550
uint16_t chan_loc
Definition: movenc.c:325
static int mov_write_minf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2183
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition: movenc.c:3497
int64_t track_duration
Definition: movenc.h:85
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:221
static const struct @159 mov_pix_fmt_tags[]
int last_sample_is_subtitle_end
Definition: movenc.h:86
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1369
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:815
int entries_flushed
Definition: movenc.h:135
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:240
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:115
static int shift_data(AVFormatContext *s)
Definition: movenc.c:5381
Format I/O context.
Definition: avformat.h:1272
int64_t frag_start
Definition: movenc.h:133
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:188
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s, int disc)
Definition: movenc.c:2855
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:4608
static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1484
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks, int size)
Definition: movenc.c:3546
static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:4545
Public dictionary API.
double avpriv_get_gamma_from_trc(enum AVColorTransferCharacteristic trc)
Determine a suitable 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition: color_utils.c:24
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:192
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:96
int first_trun
Definition: movenc.h:180
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3116
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int64_t default_duration
Definition: movenc.h:125
#define READ_BLOCK
uint32_t flags
Definition: movenc.h:94
#define FF_MOV_FLAG_EMPTY_MOOV
Definition: movenc.h:201
#define FF_MOV_FLAG_WRITE_COLR
Definition: movenc.h:213
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:318
if()
Definition: avfilter.c:975
uint8_t
Round toward +infinity.
Definition: mathematics.h:74
static int nb_streams
Definition: ffprobe.c:226
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
Definition: movenc.c:2516
unsigned frag_info_capacity
Definition: movenc.h:139
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:196
int missing_duration_warned
Definition: movenc.h:196
uint64_t mdat_size
Definition: movenc.h:162
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
unsigned int samples_in_chunk
Definition: movenc.h:49
#define MOV_CLASS(flavor)
Definition: movenc.c:88
AVOptions.
uint8_t lfe_on
Definition: ac3.h:186
static int get_samples_per_packet(MOVTrack *track)
Definition: movenc.c:872
static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1211
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:71
int frag_interleave
Definition: movenc.h:195
static int mov_pcm_le_gt16(enum AVCodecID codec_id)
Definition: movenc.c:606
timecode is drop frame
Definition: timecode.h:36
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1378
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: avcodec.h:1032
static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3912
static const AVOption options[]
Definition: movenc.c:53
#define MODE_PSP
Definition: movenc.h:38
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1355
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2388
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition: movenc.c:4657
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:85
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
static int mov_write_vmhd_tag(AVIOContext *pb)
Definition: movenc.c:2080
#define MODE_MP4
Definition: movenc.h:35
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:114
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:240
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:96
static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov, int tracks, int ref_size)
Definition: movenc.c:3720
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3178
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
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:39
int chapter_track
qt chapter track number
Definition: movenc.h:160
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1383
uint8_t * data
Definition: avcodec.h:1162
#define FF_MOV_FLAG_OMIT_TFHD_OFFSET
Definition: movenc.h:207
int start
timecode frame start (first base frame number)
Definition: timecode.h:42
int64_t time
Definition: movenc.h:74
static void mov_free(AVFormatContext *s)
Definition: movenc.c:4850
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:226
uint32_t tag
Definition: movenc.c:1333
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2762
int fragments
Definition: movenc.h:174
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1497
static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:645
int iods_audio_profile
Definition: movenc.h:171
bitstream reader API header.
#define MOV_TIMESCALE
Definition: movenc.h:31
int max_fragment_duration
Definition: movenc.h:175
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:81
static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks, int64_t mdat_size)
Definition: movenc.c:3755
uint8_t bitstream_id
Definition: ac3.h:183
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:673
ptrdiff_t size
Definition: opengl_enc.c:101
int64_t mdat_pos
Definition: movenc.h:161
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:365
void ff_mov_close_hinting(MOVTrack *track)
Definition: movenchint.c:459
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2720
static int64_t duration
Definition: ffplay.c:321
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
Write uuid atom.
Definition: movenc.c:1468
unsigned int size
Definition: movenc.h:48
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:177
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1400
#define FF_MOV_FLAG_DASH
Definition: movenc.h:210
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1180
static const uint16_t fiel_data[]
Definition: movenc.c:1480
#define av_log(a,...)
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:218
static int mov_write_nmhd_tag(AVIOContext *pb)
Definition: movenc.c:1994
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:285
int ism_lookahead
Definition: movenc.h:178
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1291
#define ROUNDED_DIV(a, b)
Definition: common.h:55
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1208
#define MODE_3G2
Definition: movenc.h:40
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1442
static int compute_sidx_size(AVFormatContext *s)
Definition: movenc.c:5366
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
uint32_t tref_tag
Definition: movenc.h:112
struct eac3_info::@160 substream[1]
#define U(x)
Definition: vp56_arith.h:37
uint8_t lfeon
Definition: movenc.c:320
uint32_t flags
Definition: movenc.h:55
static int mov_write_eac3_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:453
#define MOV_TIMECODE_FLAG_DROPFRAME
Definition: movenc.h:95
static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
Definition: movenc.c:4023
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2534
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:213
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1482
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3318
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2948
int64_t tfrf_offset
Definition: movenc.h:76
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO
Definition: isom.h:237
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3825
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:152
int64_t duration
Definition: movenc.h:75
uint8_t frame_type
Definition: ac3.h:187
int profile
Definition: mxfenc.c:1804
static void put_descr(AVIOContext *pb, int tag, unsigned int size)
Definition: movenc.c:532
static int mov_flush_fragment(AVFormatContext *s)
Definition: movenc.c:4109
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2910
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1038
#define AVERROR(e)
Definition: error.h:43
uint8_t acmod
Definition: movenc.c:318
static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int len)
Definition: movenc.c:2882
int video_track_timescale
Definition: movenc.h:182
static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
Definition: movenc.c:716
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
#define MODE_3GP
Definition: movenc.h:37
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:937
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:97
uint8_t sr_code
Definition: ac3.h:182
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2546
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:421
AVChapter ** chapters
Definition: avformat.h:1472
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:461
static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int moof_size, int first, int end)
Definition: movenc.c:3423
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2327
#define FF_MOV_FLAG_WRITE_GAMA
Definition: movenc.h:214
static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track, AVStream *st)
Definition: movenc.c:4892
Coded AC-3 header values up to the lfeon element, plus derived values.
Definition: ac3.h:176
static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3261
static av_always_inline av_const double round(double x)
Definition: libm.h:162
int height
active picture (w/o VBI) height for D-10/IMX
Definition: movenc.h:111
static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVStream *st)
Definition: movenc.c:2569
#define FF_MOV_FLAG_SEPARATE_MOOF
Definition: movenc.h:203
enum AVCodecID codec_id
Definition: mov_chan.c:433
uint8_t bsmod
Definition: movenc.c:316
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:925
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:376
#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:1025
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:94
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:238
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:83
int mode
Definition: movenc.h:81
const AVCodecTag ff_mp4_obj_type[]
Definition: isom.c:34
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:318
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:95
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1168
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2304
static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:634
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int vos_len
Definition: movenc.h:106
int iods_skip
Definition: movenc.h:169
static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:4083
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:630
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1328
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:3975
static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
Definition: movenc.c:1527
#define LIBAVFORMAT_IDENT
Definition: version.h:44
#define FF_MOV_FLAG_FRAG_DISCONT
Definition: movenc.h:211
uint64_t pos
Definition: movenc.h:46
int first_frag_written
Definition: movenc.h:145
int64_t dts
Definition: movenc.h:47
#define MOV_FRAG_INFO_ALLOC_INCREMENT
Definition: movenc.h:29
#define MOV_TRACK_ENABLED
Definition: movenc.h:93
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1898
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
int bit_rate
the average bitrate
Definition: avcodec.h:1305
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:197
int per_stream_grouping
Definition: movenc.h:189
int64_t data_offset
Definition: movenc.h:132
char filename[1024]
input or output filename
Definition: avformat.h:1348
static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset)
Definition: movenc.c:3363
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:127
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:163
#define FFMIN(a, b)
Definition: common.h:66
int nb_meta_tmcd
number of new created tmcd track based on metadata (aka not data copy)
Definition: movenc.h:159
Raw Video Codec.
float y
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2691
uint8_t interlaced
Definition: mxfenc.c:1805
int audio_vbr
Definition: movenc.h:110
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:147
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3022
uint32_t bit_rate
Definition: ac3.h:203
#define MOV_TKHD_FLAG_IN_MOVIE
Definition: isom.h:241
ret
Definition: avfilter.c:974
#define MOV_PARTIAL_SYNC_SAMPLE
Definition: movenc.h:54
int width
picture width / height.
Definition: avcodec.h:1414
int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
Definition: movenchint.c:29
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
AVPacket pkt
Definition: movenc.c:299
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:4094
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1017
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:471
static int is_clcp_track(MOVTrack *track)
Definition: movenc.c:2089
static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:2596
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:466
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:434
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:1073
const char * name
Definition: avformat.h:513
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1173
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1939
long sample_size
Definition: movenc.h:88
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style)
Definition: movenc.c:2798
int frag_discont
Definition: movenc.h:134
static int mov_write_smhd_tag(AVIOContext *pb)
Definition: movenc.c:2070
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1573
int reserved_moov_size
0 for disabled, -1 for automatic, size otherwise
Definition: movenc.h:184
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:128
static void param_write_string(AVIOContext *pb, const char *name, const char *value)
Definition: movenc.c:3247
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:225
int n
Definition: avisynth_c.h:547
AVDictionary * metadata
Definition: avformat.h:916
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1027
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:2420
enum AVPixelFormat pix_fmt
Definition: movenc.c:1332
static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2678
static int mov_get_h264_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1273
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
static int mov_write_raw_metadata_tag(AVFormatContext *s, AVIOContext *pb, const char *name, const char *key)
Definition: movenc.c:2962
#define MOV_TRACK_CTTS
Definition: movenc.h:91
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:2970
int avpriv_ac3_parse_header2(GetBitContext *gbc, AC3HeaderInfo **phdr)
Parse AC-3 frame header.
Definition: ac3_parser.c:50
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:523
static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3348
int iods_video_profile
Definition: movenc.h:170
Stream structure.
Definition: avformat.h:842
static int get_sidx_size(AVFormatContext *s)
Definition: movenc.c:5323
int64_t reserved_moov_pos
Definition: movenc.h:185
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1238
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2005
also ITU-R BT1361
Definition: pixfmt.h:479
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:797
enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
Definition: utils.c:1129
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:552
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:484
int64_t end_pts
Definition: movenc.h:116
#define MOV_INDEX_CLUSTER_SIZE
Definition: movenc.h:30
static int mov_write_d263_tag(AVIOContext *pb)
Definition: movenc.c:1005
enum AVMediaType codec_type
Definition: avcodec.h:1249
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:85
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
Definition: vc1_common.h:70
functionally identical to above
Definition: pixfmt.h:468
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:264
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1862
int duration
Definition: isom.h:47
enum AVCodecID codec_id
Definition: avcodec.h:1258
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:253
int cts
Definition: movenc.h:52
Timecode helpers header.
int sample_rate
samples per second
Definition: avcodec.h:1985
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition: rtpenc.h:74
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:155
main external API structure.
Definition: avcodec.h:1241
static uint32_t rgb_to_yuv(uint32_t rgb)
Definition: movenc.c:4876
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:224
#define FF_MOV_FLAG_FRAG_CUSTOM
Definition: movenc.h:204
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Copy packet, including contents.
Definition: avpacket.c:265
uint32_t timecode_flags
Definition: movenc.h:98
int hint_track
the track that hints this track, -1 if no hint track is set
Definition: movenc.h:118
static int mov_write_hmhd_tag(AVIOContext *pb)
Definition: movenc.c:2168
int slices
Definition: movenc.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1273
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition: movenc.c:4002
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:64
static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:799
static void build_chunks(MOVTrack *trk)
Definition: movenc.c:3155
int ff_alloc_extradata(AVCodecContext *avctx, int size)
Allocate extradata with additional FF_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:2864
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1356
Replacements for frequently missing libm functions.
uint32_t default_size
Definition: movenc.h:127
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:364
float gamma
Definition: movenc.h:193
#define FF_MOV_FLAG_RTP_HINT
Definition: movenc.h:199
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:297
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int count
Definition: isom.h:46
AVFormatContext * fc
Definition: movenc.h:190
Y , 16bpp, big-endian.
Definition: pixfmt.h:99
int index
Definition: gxfenc.c:89
unsigned int chunkNum
Chunk number if the current entry is a chunk start otherwise 0.
Definition: movenc.h:50
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1953
static int co64_required(const MOVTrack *track)
Definition: movenc.c:120
rational number numerator/denominator
Definition: rational.h:43
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1946
uint64_t time
Definition: movenc.h:84
int first_packet_seq
Definition: movenc.h:142
static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
Definition: movenc.c:3252
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:410
int64_t time
Definition: movenc.h:157
uint16_t frame_size
Definition: ac3.h:205
int has_keyframes
Definition: movenc.h:90
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1173
int entry
Definition: movenc.h:82
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
static av_always_inline av_const long int lrint(double x)
Definition: libm.h:148
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:219
#define AVFMT_AVOID_NEG_TS_AUTO
Enabled when required by target format.
Definition: avformat.h:1574
MOVFragmentInfo * frag_info
Definition: movenc.h:138
#define snprintf
Definition: snprintf.h:34
char * major_brand
Definition: movenc.h:187
int64_t start_cts
Definition: movenc.h:115
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:227
uint8_t * vos_data
Definition: movenc.h:107
int first_packet_entry
Definition: movenc.h:143
#define TAG_IS_AVCI(tag)
Definition: isom.h:245
Round toward -infinity.
Definition: mathematics.h:73
static const AVCodecTag codec_3gp_tags[]
Definition: movenc.c:1422
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:2285
static int mov_write_enda_tag(AVIOContext *pb)
Definition: movenc.c:516
static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2024
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:422
static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c, int16_t d, int16_t tx, int16_t ty)
Definition: movenc.c:2271
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:365
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1122
uint16_t data_rate
Definition: movenc.c:305
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
static int flags
Definition: cpu.c:47
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:116
static int mov_write_stbl_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1958
AVFormatContext * rtp_ctx
the format context for the hinting rtp muxer
Definition: movenc.h:120
uint8_t level
Definition: svq3.c:150
int track_id
Definition: movenc.h:100
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3048
static int mov_write_trailer(AVFormatContext *s)
Definition: movenc.c:5447
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:522
int64_t start
Definition: avformat.h:1238
#define FF_MOV_FLAG_FRAG_KEYFRAME
Definition: movenc.h:202
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO
Shift timestamps so that they start at 0.
Definition: avformat.h:1576
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, const char *tag, const char *str)
Definition: movenc.c:3000
int palette
Definition: v4l.c:60
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2702
Main libavformat public API header.
static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:3532
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
static int mov_write_stsd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1842
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:824
int packet_seq
Definition: movenc.h:146
static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3775
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:215
static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
Definition: movenc.c:3357
#define MOV_SYNC_SAMPLE
Definition: movenc.h:53
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3476
static double c[64]
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:111
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:905
uint32_t max_packet_size
Definition: movenc.h:123
static int mov_write_sidx_tag(AVIOContext *pb, MOVTrack *track, int ref_size, int total_sidx_size)
Definition: movenc.c:3661
int src_track
the track that this hint (or tmcd) track describes
Definition: movenc.h:119
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1237
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:111
int multichannel_as_mono
Definition: movenc.h:104
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:903
static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset, int moof_size)
Definition: movenc.c:3598
char * key
Definition: dict.h:87
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:108
unsigned bps
Definition: movenc.c:1334
int av_timecode_check_frame_rate(AVRational rate)
Check if the timecode feature is available for the given frame rate.
Definition: timecode.c:177
static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
Definition: movenc.c:4754
#define MODE_IPOD
Definition: movenc.h:41
static int mov_auto_flush_fragment(AVFormatContext *s)
Definition: movenc.c:4279
static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2837
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:893
#define FF_MOV_FLAG_DISABLE_CHPL
Definition: movenc.h:208
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2649
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
Definition: movenc.c:217
#define av_free(p)
char * value
Definition: dict.h:88
uint32_t default_sample_flags
Definition: movenc.h:126
unsigned timescale
Definition: movenc.h:83
int len
#define FF_MOV_FLAG_DEFAULT_BASE_MOOF
Definition: movenc.h:209
static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3799
int substreamid
substream identification
Definition: ac3.h:188
int channels
number of audio channels
Definition: avcodec.h:1986
uint8_t ec3_done
Definition: movenc.c:300
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
Definition: movenc.c:2778
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
void * priv_data
Format private data.
Definition: avformat.h:1300
static int mov_write_header(AVFormatContext *s)
Definition: movenc.c:4941
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
static int64_t update_size(AVIOContext *pb, int64_t pos)
Definition: movenc.c:110
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:223
int64_t start_dts
Definition: movenc.h:114
static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2213
#define LIBAVCODEC_IDENT
Definition: version.h:43
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1161
int tref_id
trackID of the referenced track
Definition: movenc.h:113
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3837
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:467
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:326
int first_packet_seen
Definition: movenc.h:144
#define av_freep(p)
const PixelFormatTag avpriv_pix_fmt_bps_mov[]
Definition: raw.c:256
static const AVCodecTag codec_ipod_tags[]
Definition: movenc.c:1143
static unsigned compute_avg_bitrate(MOVTrack *track)
Definition: movenc.c:541
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1982
void INT64 start
Definition: avisynth_c.h:553
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:72
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1760
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2505
int moov_written
Definition: movenc.h:173
#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:1089
#define av_malloc_array(a, b)
uint8_t fscod
Definition: movenc.c:310
unsigned cluster_capacity
Definition: movenc.h:109
int num_blocks
number of audio blocks
Definition: ac3.h:192
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:4293
uint8_t channel_mode
Definition: ac3.h:185
static int mov_get_lpcm_flags(enum AVCodecID codec_id)
Compute flags for 'lpcm' tag.
Definition: movenc.c:827
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2011
int stream_index
Definition: avcodec.h:1164
#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:884
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:494
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:97
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1154
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:315
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:490
int av_copy_packet_side_data(AVPacket *dst, const AVPacket *src)
Copy packet side data.
Definition: avpacket.c:223
uint8_t num_blocks
Definition: movenc.c:301
void * eac3_priv
Definition: movenc.h:151
static void enable_tracks(AVFormatContext *s)
Definition: movenc.c:4809
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
#define MODE_ISM
Definition: movenc.h:42
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:85
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Definition: utils.c:3957
This structure stores compressed data.
Definition: avcodec.h:1139
int64_t offset
Definition: movenc.h:73
int min_fragment_duration
Definition: movenc.h:176
MOVTrack * tracks
Definition: movenc.h:163
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
uint8_t num_ind_sub
Definition: movenc.c:307
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:59
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:250
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2543
uint8_t num_dep_sub
Definition: movenc.c:323
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155
uint8_t bsid
Definition: movenc.c:312
AVIOContext * mdat_buf
Definition: movenc.h:131
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
#define t2
Definition: regdef.h:30
#define FFMAX3(a, b, c)
Definition: common.h:65
int nb_streams
Definition: movenc.h:158
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:231
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:53
static int compute_moov_size(AVFormatContext *s)
Definition: movenc.c:5341
const char * name
Definition: opengl_enc.c:103
static int width
static int get_moov_size(AVFormatContext *s)
Definition: movenc.c:5310
bitstream writer API