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