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