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