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