FFmpeg
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 "config_components.h"
25 
26 #include <stdint.h>
27 #include <inttypes.h>
28 
29 #include "movenc.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "dovi_isom.h"
33 #include "riff.h"
34 #include "avio.h"
35 #include "iamf_writer.h"
36 #include "isom.h"
37 #include "av1.h"
38 #include "avc.h"
39 #include "evc.h"
41 #include "libavcodec/dnxhddata.h"
42 #include "libavcodec/flac.h"
43 #include "libavcodec/get_bits.h"
44 
45 #include "libavcodec/internal.h"
46 #include "libavcodec/put_bits.h"
47 #include "libavcodec/vc1_common.h"
48 #include "libavcodec/raw.h"
49 #include "internal.h"
50 #include "libavutil/avstring.h"
52 #include "libavutil/csp.h"
53 #include "libavutil/intfloat.h"
54 #include "libavutil/mathematics.h"
55 #include "libavutil/libm.h"
56 #include "libavutil/mem.h"
57 #include "libavutil/opt.h"
58 #include "libavutil/dict.h"
59 #include "libavutil/pixdesc.h"
60 #include "libavutil/stereo3d.h"
61 #include "libavutil/timecode.h"
62 #include "libavutil/dovi_meta.h"
63 #include "libavutil/uuid.h"
64 #include "hevc.h"
65 #include "rtpenc.h"
66 #include "nal.h"
67 #include "mov_chan.h"
68 #include "movenc_ttml.h"
69 #include "mux.h"
70 #include "rawutils.h"
71 #include "ttmlenc.h"
72 #include "version.h"
73 #include "vpcc.h"
74 #include "vvc.h"
75 
76 static const AVOption options[] = {
77  { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
78  { "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},
79  { "encryption_key", "The media encryption key (hex)", offsetof(MOVMuxContext, encryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
80  { "encryption_kid", "The media encryption key identifier (hex)", offsetof(MOVMuxContext, encryption_kid), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
81  { "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 },
82  { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
83  { "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 },
84  { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
85  { "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},
86  { "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},
87  { "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},
88  { "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},
89  { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" },
90  { "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, .unit = "movflags" },
91  { "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, .unit = "movflags" },
92  { "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, .unit = "movflags" },
93  { "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, .unit = "movflags" },
94  { "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, .unit = "movflags" },
95  { "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, .unit = "movflags" },
96  { "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, .unit = "movflags" },
97  { "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, .unit = "movflags" },
98  { "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, .unit = "movflags" },
99  { "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, .unit = "movflags" },
100  { "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, .unit = "movflags" },
101  { "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, .unit = "movflags" },
102  { "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, .unit = "movflags" },
103  { "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, .unit = 0 },
104  { "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, .unit = "movflags" },
105  { "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, .unit = "movflags" },
106  { "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, .unit = "movflags" },
107  { "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, .unit = "movflags" },
108  { "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, .unit = "movflags" },
109  { "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, .unit = "movflags" },
110  { "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, .unit = "movflags" },
111  { "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, .unit = "movflags" },
112  { "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, .unit = "movflags" },
113  { "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, .unit = "movflags" },
114  { "hybrid_fragmented", "For recoverability, write a fragmented file that is converted to non-fragmented at the end.", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_HYBRID_FRAGMENTED}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" },
115  { "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},
116  { "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},
117  { "movie_timescale", "set movie timescale", offsetof(MOVMuxContext, movie_timescale), AV_OPT_TYPE_INT, {.i64 = MOV_TIMESCALE}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
118  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
119  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
120  { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
121  { "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},
122  { "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},
123  { "write_btrt", "force or disable writing btrt", offsetof(MOVMuxContext, write_btrt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
124  { "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, .unit = "prft"},
125  { "pts", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_PTS}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, .unit = "prft"},
126  { "wallclock", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_WALLCLOCK}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, .unit = "prft"},
127  { "write_tmcd", "force or disable writing tmcd", offsetof(MOVMuxContext, write_tmcd), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
128  { NULL },
129 };
130 
132  .class_name = "mov/mp4/tgp/psp/tg2/ipod/ismv/f4v muxer",
133  .item_name = av_default_item_name,
134  .option = options,
135  .version = LIBAVUTIL_VERSION_INT,
136 };
137 
138 static int get_moov_size(AVFormatContext *s);
140 
141 static int utf8len(const uint8_t *b)
142 {
143  int len = 0;
144  int val;
145  while (*b) {
146  GET_UTF8(val, *b++, return -1;)
147  len++;
148  }
149  return len;
150 }
151 
152 //FIXME support 64 bit variant with wide placeholders
154 {
155  int64_t curpos = avio_tell(pb);
156  avio_seek(pb, pos, SEEK_SET);
157  avio_wb32(pb, curpos - pos); /* rewrite size */
158  avio_seek(pb, curpos, SEEK_SET);
159 
160  return curpos - pos;
161 }
162 
163 static int co64_required(const MOVTrack *track)
164 {
165  if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
166  return 1;
167  return 0;
168 }
169 
170 static int is_cover_image(const AVStream *st)
171 {
172  /* Eg. AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS
173  * is encoded as sparse video track */
174  return st && st->disposition == AV_DISPOSITION_ATTACHED_PIC;
175 }
176 
177 static int rtp_hinting_needed(const AVStream *st)
178 {
179  /* Add hint tracks for each real audio and video stream */
180  if (is_cover_image(st))
181  return 0;
182  return st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
184 }
185 
186 /* Chunk offset atom */
187 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
188 {
189  int i;
190  int mode64 = co64_required(track); // use 32 bit size variant if possible
191  int64_t pos = avio_tell(pb);
192  avio_wb32(pb, 0); /* size */
193  if (mode64)
194  ffio_wfourcc(pb, "co64");
195  else
196  ffio_wfourcc(pb, "stco");
197  avio_wb32(pb, 0); /* version & flags */
198  avio_wb32(pb, track->chunkCount); /* entry count */
199  for (i = 0; i < track->entry; i++) {
200  if (!track->cluster[i].chunkNum)
201  continue;
202  if (mode64 == 1)
203  avio_wb64(pb, track->cluster[i].pos + track->data_offset);
204  else
205  avio_wb32(pb, track->cluster[i].pos + track->data_offset);
206  }
207  return update_size(pb, pos);
208 }
209 
210 /* Sample size atom */
211 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
212 {
213  int equalChunks = 1;
214  int i, j, entries = 0, tst = -1, oldtst = -1;
215 
216  int64_t pos = avio_tell(pb);
217  avio_wb32(pb, 0); /* size */
218  ffio_wfourcc(pb, "stsz");
219  avio_wb32(pb, 0); /* version & flags */
220 
221  for (i = 0; i < track->entry; i++) {
222  tst = track->cluster[i].size / track->cluster[i].entries;
223  if (oldtst != -1 && tst != oldtst)
224  equalChunks = 0;
225  oldtst = tst;
226  entries += track->cluster[i].entries;
227  }
228  if (equalChunks && track->entry) {
229  int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
230  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
231  avio_wb32(pb, sSize); // sample size
232  avio_wb32(pb, entries); // sample count
233  } else {
234  avio_wb32(pb, 0); // sample size
235  avio_wb32(pb, entries); // sample count
236  for (i = 0; i < track->entry; i++) {
237  for (j = 0; j < track->cluster[i].entries; j++) {
238  avio_wb32(pb, track->cluster[i].size /
239  track->cluster[i].entries);
240  }
241  }
242  }
243  return update_size(pb, pos);
244 }
245 
246 /* Sample to chunk atom */
247 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
248 {
249  int index = 0, oldval = -1, i;
250  int64_t entryPos, curpos;
251 
252  int64_t pos = avio_tell(pb);
253  avio_wb32(pb, 0); /* size */
254  ffio_wfourcc(pb, "stsc");
255  avio_wb32(pb, 0); // version & flags
256  entryPos = avio_tell(pb);
257  avio_wb32(pb, track->chunkCount); // entry count
258  for (i = 0; i < track->entry; i++) {
259  if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
260  avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
261  avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
262  avio_wb32(pb, 0x1); // sample description index
263  oldval = track->cluster[i].samples_in_chunk;
264  index++;
265  }
266  }
267  curpos = avio_tell(pb);
268  avio_seek(pb, entryPos, SEEK_SET);
269  avio_wb32(pb, index); // rewrite size
270  avio_seek(pb, curpos, SEEK_SET);
271 
272  return update_size(pb, pos);
273 }
274 
275 /* Sync sample atom */
276 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
277 {
278  int64_t curpos, entryPos;
279  int i, index = 0;
280  int64_t pos = avio_tell(pb);
281  avio_wb32(pb, 0); // size
282  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
283  avio_wb32(pb, 0); // version & flags
284  entryPos = avio_tell(pb);
285  avio_wb32(pb, track->entry); // entry count
286  for (i = 0; i < track->entry; i++) {
287  if (track->cluster[i].flags & flag) {
288  avio_wb32(pb, i + 1);
289  index++;
290  }
291  }
292  curpos = avio_tell(pb);
293  avio_seek(pb, entryPos, SEEK_SET);
294  avio_wb32(pb, index); // rewrite size
295  avio_seek(pb, curpos, SEEK_SET);
296  return update_size(pb, pos);
297 }
298 
299 /* Sample dependency atom */
300 static int mov_write_sdtp_tag(AVIOContext *pb, MOVTrack *track)
301 {
302  int i;
303  uint8_t leading, dependent, reference, redundancy;
304  int64_t pos = avio_tell(pb);
305  avio_wb32(pb, 0); // size
306  ffio_wfourcc(pb, "sdtp");
307  avio_wb32(pb, 0); // version & flags
308  for (i = 0; i < track->entry; i++) {
309  dependent = MOV_SAMPLE_DEPENDENCY_YES;
310  leading = reference = redundancy = MOV_SAMPLE_DEPENDENCY_UNKNOWN;
311  if (track->cluster[i].flags & MOV_DISPOSABLE_SAMPLE) {
312  reference = MOV_SAMPLE_DEPENDENCY_NO;
313  }
314  if (track->cluster[i].flags & MOV_SYNC_SAMPLE) {
315  dependent = MOV_SAMPLE_DEPENDENCY_NO;
316  }
317  avio_w8(pb, (leading << 6) | (dependent << 4) |
318  (reference << 2) | redundancy);
319  }
320  return update_size(pb, pos);
321 }
322 
323 #if CONFIG_IAMFENC
324 static int mov_write_iacb_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
325 {
326  AVIOContext *dyn_bc;
327  int64_t pos = avio_tell(pb);
328  uint8_t *dyn_buf = NULL;
329  int dyn_size;
330  int ret = avio_open_dyn_buf(&dyn_bc);
331  if (ret < 0)
332  return ret;
333 
334  avio_wb32(pb, 0);
335  ffio_wfourcc(pb, "iacb");
336  avio_w8(pb, 1); // configurationVersion
337 
338  ret = ff_iamf_write_descriptors(track->iamf, dyn_bc, s);
339  if (ret < 0)
340  return ret;
341 
342  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
343  ffio_write_leb(pb, dyn_size);
344  avio_write(pb, dyn_buf, dyn_size);
345  av_free(dyn_buf);
346 
347  return update_size(pb, pos);
348 }
349 #endif
350 
351 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
352 {
353  avio_wb32(pb, 0x11); /* size */
354  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
355  else ffio_wfourcc(pb, "damr");
356  ffio_wfourcc(pb, "FFMP");
357  avio_w8(pb, 0); /* decoder version */
358 
359  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
360  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
361  avio_w8(pb, 0x01); /* Frames per sample */
362  return 0x11;
363 }
364 
365 struct eac3_info {
367  uint8_t ec3_done;
368  uint8_t num_blocks;
369 
370  /* Layout of the EC3SpecificBox */
371  /* maximum bitrate */
372  uint16_t data_rate;
374  /* number of independent substreams */
375  uint8_t num_ind_sub;
376  struct {
377  /* sample rate code (see ff_ac3_sample_rate_tab) 2 bits */
378  uint8_t fscod;
379  /* bit stream identification 5 bits */
380  uint8_t bsid;
381  /* one bit reserved */
382  /* audio service mixing (not supported yet) 1 bit */
383  /* bit stream mode 3 bits */
384  uint8_t bsmod;
385  /* audio coding mode 3 bits */
386  uint8_t acmod;
387  /* sub woofer on 1 bit */
388  uint8_t lfeon;
389  /* 3 bits reserved */
390  /* number of dependent substreams associated with this substream 4 bits */
391  uint8_t num_dep_sub;
392  /* channel locations of the dependent substream(s), if any, 9 bits */
393  uint16_t chan_loc;
394  /* if there is no dependent substream, then one bit reserved instead */
395  } substream[1]; /* TODO: support 8 independent substreams */
396 };
397 
399 {
400  struct eac3_info *info = track->eac3_priv;
401  PutBitContext pbc;
402  uint8_t buf[3];
403 
404  if (!info || !info->ec3_done) {
406  "Cannot write moov atom before AC3 packets."
407  " Set the delay_moov flag to fix this.\n");
408  return AVERROR(EINVAL);
409  }
410 
411  if (info->substream[0].bsid > 8) {
413  "RealAudio AC-3/DolbyNet with bsid %d is not defined by the "
414  "ISOBMFF specification in ETSI TS 102 366!\n",
415  info->substream[0].bsid);
416  return AVERROR(EINVAL);
417  }
418 
419  if (info->ac3_bit_rate_code < 0) {
421  "No valid AC3 bit rate code for data rate of %d!\n",
422  info->data_rate);
423  return AVERROR(EINVAL);
424  }
425 
426  avio_wb32(pb, 11);
427  ffio_wfourcc(pb, "dac3");
428 
429  init_put_bits(&pbc, buf, sizeof(buf));
430  put_bits(&pbc, 2, info->substream[0].fscod);
431  put_bits(&pbc, 5, info->substream[0].bsid);
432  put_bits(&pbc, 3, info->substream[0].bsmod);
433  put_bits(&pbc, 3, info->substream[0].acmod);
434  put_bits(&pbc, 1, info->substream[0].lfeon);
435  put_bits(&pbc, 5, info->ac3_bit_rate_code); // bit_rate_code
436  put_bits(&pbc, 5, 0); // reserved
437 
438  flush_put_bits(&pbc);
439  avio_write(pb, buf, sizeof(buf));
440 
441  return 11;
442 }
443 
444 static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
445 {
446  AC3HeaderInfo *hdr = NULL;
447  struct eac3_info *info;
448  int num_blocks, ret;
449 
450  if (!track->eac3_priv) {
451  if (!(track->eac3_priv = av_mallocz(sizeof(*info))))
452  return AVERROR(ENOMEM);
453 
454  ((struct eac3_info *)track->eac3_priv)->ac3_bit_rate_code = -1;
455  }
456  info = track->eac3_priv;
457 
458  if (!info->pkt && !(info->pkt = av_packet_alloc()))
459  return AVERROR(ENOMEM);
460 
461  if ((ret = avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size)) < 0) {
462  if (ret == AVERROR(ENOMEM))
463  goto end;
464 
465  /* drop the packets until we see a good one */
466  if (!track->entry) {
467  av_log(mov->fc, AV_LOG_WARNING, "Dropping invalid packet from start of the stream\n");
468  ret = 0;
469  } else
471  goto end;
472  }
473 
474  info->data_rate = FFMAX(info->data_rate, hdr->bit_rate / 1000);
475  info->ac3_bit_rate_code = FFMAX(info->ac3_bit_rate_code,
476  hdr->ac3_bit_rate_code);
477  num_blocks = hdr->num_blocks;
478 
479  if (!info->ec3_done) {
480  /* AC-3 substream must be the first one */
481  if (hdr->bitstream_id <= 10 && hdr->substreamid != 0) {
482  ret = AVERROR(EINVAL);
483  goto end;
484  }
485 
486  /* this should always be the case, given that our AC-3 parser
487  * concatenates dependent frames to their independent parent */
490  /* substream ids must be incremental */
491  if (hdr->substreamid > info->num_ind_sub + 1) {
492  ret = AVERROR(EINVAL);
493  goto end;
494  }
495 
496  if (hdr->substreamid == info->num_ind_sub + 1) {
497  //info->num_ind_sub++;
498  avpriv_request_sample(mov->fc, "Multiple independent substreams");
500  goto end;
501  } else if (hdr->substreamid < info->num_ind_sub ||
502  hdr->substreamid == 0 && info->substream[0].bsid) {
503  info->ec3_done = 1;
504  goto concatenate;
505  }
506  } else {
507  if (hdr->substreamid != 0) {
508  avpriv_request_sample(mov->fc, "Multiple non EAC3 independent substreams");
510  goto end;
511  }
512  }
513 
514  /* fill the info needed for the "dec3" atom */
515  info->substream[hdr->substreamid].fscod = hdr->sr_code;
516  info->substream[hdr->substreamid].bsid = hdr->bitstream_id;
517  info->substream[hdr->substreamid].bsmod = hdr->bitstream_mode;
518  info->substream[hdr->substreamid].acmod = hdr->channel_mode;
519  info->substream[hdr->substreamid].lfeon = hdr->lfe_on;
520 
521  if (track->par->codec_id == AV_CODEC_ID_AC3) {
522  // with AC-3 we only require the information of a single packet,
523  // so we can finish as soon as the basic values of the bit stream
524  // have been set to the track's informational structure.
525  info->ec3_done = 1;
526  goto concatenate;
527  }
528 
529  /* Parse dependent substream(s), if any */
530  if (pkt->size != hdr->frame_size) {
531  int cumul_size = hdr->frame_size;
532  int parent = hdr->substreamid;
533 
534  while (cumul_size != pkt->size) {
535  GetBitContext gbc;
536  int i;
537  ret = avpriv_ac3_parse_header(&hdr, pkt->data + cumul_size, pkt->size - cumul_size);
538  if (ret < 0)
539  goto end;
541  ret = AVERROR(EINVAL);
542  goto end;
543  }
544  info->substream[parent].num_dep_sub++;
545  ret /= 8;
546 
547  /* header is parsed up to lfeon, but custom channel map may be needed */
548  init_get_bits8(&gbc, pkt->data + cumul_size + ret, pkt->size - cumul_size - ret);
549  /* skip bsid */
550  skip_bits(&gbc, 5);
551  /* skip volume control params */
552  for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
553  skip_bits(&gbc, 5); // skip dialog normalization
554  if (get_bits1(&gbc)) {
555  skip_bits(&gbc, 8); // skip compression gain word
556  }
557  }
558  /* get the dependent stream channel map, if exists */
559  if (get_bits1(&gbc))
560  info->substream[parent].chan_loc |= (get_bits(&gbc, 16) >> 5) & 0x1f;
561  else
562  info->substream[parent].chan_loc |= hdr->channel_mode;
563  cumul_size += hdr->frame_size;
564  }
565  }
566  }
567 
568 concatenate:
569  if (!info->num_blocks && num_blocks == 6) {
570  ret = pkt->size;
571  goto end;
572  }
573  else if (info->num_blocks + num_blocks > 6) {
575  goto end;
576  }
577 
578  if (!info->num_blocks) {
579  ret = av_packet_ref(info->pkt, pkt);
580  if (!ret)
581  info->num_blocks = num_blocks;
582  goto end;
583  } else {
584  if ((ret = av_grow_packet(info->pkt, pkt->size)) < 0)
585  goto end;
586  memcpy(info->pkt->data + info->pkt->size - pkt->size, pkt->data, pkt->size);
587  info->num_blocks += num_blocks;
588  info->pkt->duration += pkt->duration;
589  if (info->num_blocks != 6)
590  goto end;
592  av_packet_move_ref(pkt, info->pkt);
593  info->num_blocks = 0;
594  }
595  ret = pkt->size;
596 
597 end:
598  av_free(hdr);
599 
600  return ret;
601 }
602 
604 {
605  PutBitContext pbc;
606  uint8_t *buf;
607  struct eac3_info *info;
608  int size, i;
609 
610  if (!track->eac3_priv) {
612  "Cannot write moov atom before EAC3 packets parsed.\n");
613  return AVERROR(EINVAL);
614  }
615 
616  info = track->eac3_priv;
617  size = 2 + ((34 * (info->num_ind_sub + 1) + 7) >> 3);
618  buf = av_malloc(size);
619  if (!buf) {
620  return AVERROR(ENOMEM);
621  }
622 
623  init_put_bits(&pbc, buf, size);
624  put_bits(&pbc, 13, info->data_rate);
625  put_bits(&pbc, 3, info->num_ind_sub);
626  for (i = 0; i <= info->num_ind_sub; i++) {
627  put_bits(&pbc, 2, info->substream[i].fscod);
628  put_bits(&pbc, 5, info->substream[i].bsid);
629  put_bits(&pbc, 1, 0); /* reserved */
630  put_bits(&pbc, 1, 0); /* asvc */
631  put_bits(&pbc, 3, info->substream[i].bsmod);
632  put_bits(&pbc, 3, info->substream[i].acmod);
633  put_bits(&pbc, 1, info->substream[i].lfeon);
634  put_bits(&pbc, 5, 0); /* reserved */
635  put_bits(&pbc, 4, info->substream[i].num_dep_sub);
636  if (!info->substream[i].num_dep_sub) {
637  put_bits(&pbc, 1, 0); /* reserved */
638  } else {
639  put_bits(&pbc, 9, info->substream[i].chan_loc);
640  }
641  }
642  flush_put_bits(&pbc);
643  size = put_bytes_output(&pbc);
644 
645  avio_wb32(pb, size + 8);
646  ffio_wfourcc(pb, "dec3");
647  avio_write(pb, buf, size);
648 
649  av_free(buf);
650 
651  return size;
652 }
653 
654 /**
655  * This function writes extradata "as is".
656  * Extradata must be formatted like a valid atom (with size and tag).
657  */
659 {
660  avio_write(pb, track->par->extradata, track->par->extradata_size);
661  return track->par->extradata_size;
662 }
663 
665 {
666  avio_wb32(pb, 10);
667  ffio_wfourcc(pb, "enda");
668  avio_wb16(pb, 1); /* little endian */
669  return 10;
670 }
671 
673 {
674  avio_wb32(pb, 10);
675  ffio_wfourcc(pb, "enda");
676  avio_wb16(pb, 0); /* big endian */
677  return 10;
678 }
679 
680 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
681 {
682  int i = 3;
683  avio_w8(pb, tag);
684  for (; i > 0; i--)
685  avio_w8(pb, (size >> (7 * i)) | 0x80);
686  avio_w8(pb, size & 0x7F);
687 }
688 
689 static unsigned compute_avg_bitrate(MOVTrack *track)
690 {
691  uint64_t size = 0;
692  int i;
693  if (!track->track_duration)
694  return 0;
695  for (i = 0; i < track->entry; i++)
696  size += track->cluster[i].size;
697  return size * 8 * track->timescale / track->track_duration;
698 }
699 
701  uint32_t buffer_size; ///< Size of the decoding buffer for the elementary stream in bytes.
702  uint32_t max_bit_rate; ///< Maximum rate in bits/second over any window of one second.
703  uint32_t avg_bit_rate; ///< Average rate in bits/second over the entire presentation.
704 };
705 
707 {
708  const AVPacketSideData *sd = track->st ?
710  track->st->codecpar->nb_coded_side_data,
712  AVCPBProperties *props = sd ? (AVCPBProperties *)sd->data : NULL;
713  struct mpeg4_bit_rate_values bit_rates = { 0 };
714 
715  bit_rates.avg_bit_rate = compute_avg_bitrate(track);
716  if (!bit_rates.avg_bit_rate) {
717  // if the average bit rate cannot be calculated at this point, such as
718  // in the case of fragmented MP4, utilize the following values as
719  // fall-back in priority order:
720  //
721  // 1. average bit rate property
722  // 2. bit rate (usually average over the whole clip)
723  // 3. maximum bit rate property
724 
725  if (props && props->avg_bitrate) {
726  bit_rates.avg_bit_rate = props->avg_bitrate;
727  } else if (track->par->bit_rate) {
728  bit_rates.avg_bit_rate = track->par->bit_rate;
729  } else if (props && props->max_bitrate) {
730  bit_rates.avg_bit_rate = props->max_bitrate;
731  }
732  }
733 
734  // (FIXME should be max rate in any 1 sec window)
735  bit_rates.max_bit_rate = FFMAX(track->par->bit_rate,
736  bit_rates.avg_bit_rate);
737 
738  // utilize values from properties if we have them available
739  if (props) {
740  bit_rates.max_bit_rate = FFMAX(bit_rates.max_bit_rate,
741  props->max_bitrate);
742  bit_rates.buffer_size = props->buffer_size / 8;
743  }
744 
745  return bit_rates;
746 }
747 
748 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
749 {
750  struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track);
751  int64_t pos = avio_tell(pb);
752  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
753 
754  avio_wb32(pb, 0); // size
755  ffio_wfourcc(pb, "esds");
756  avio_wb32(pb, 0); // Version
757 
758  // ES descriptor
759  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
760  avio_wb16(pb, track->track_id);
761  avio_w8(pb, 0x00); // flags (= no flags)
762 
763  // DecoderConfig descriptor
764  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
765 
766  // Object type indication
767  if ((track->par->codec_id == AV_CODEC_ID_MP2 ||
768  track->par->codec_id == AV_CODEC_ID_MP3) &&
769  track->par->sample_rate > 24000)
770  avio_w8(pb, 0x6B); // 11172-3
771  else
773 
774  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
775  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
776  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
777  avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
778  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
779  avio_w8(pb, 0x15); // flags (= Audiostream)
780  else
781  avio_w8(pb, 0x11); // flags (= Visualstream)
782 
783  avio_wb24(pb, bit_rates.buffer_size); // Buffersize DB
784  avio_wb32(pb, bit_rates.max_bit_rate); // maxbitrate
785  avio_wb32(pb, bit_rates.avg_bit_rate);
786 
787  if (track->vos_len) {
788  // DecoderSpecific info descriptor
789  put_descr(pb, 0x05, track->vos_len);
790  avio_write(pb, track->vos_data, track->vos_len);
791  }
792 
793  // SL descriptor
794  put_descr(pb, 0x06, 1);
795  avio_w8(pb, 0x02);
796  return update_size(pb, pos);
797 }
798 
800 {
801  return codec_id == AV_CODEC_ID_PCM_S24LE ||
805 }
806 
808 {
809  return codec_id == AV_CODEC_ID_PCM_S24BE ||
813 }
814 
816 {
817  int ret;
818  int64_t pos = avio_tell(pb);
819  avio_wb32(pb, 0);
820  avio_wl32(pb, track->tag); // store it byteswapped
821  track->par->codec_tag = av_bswap16(track->tag >> 16);
822  if ((ret = ff_put_wav_header(s, pb, track->par, 0)) < 0)
823  return ret;
824  return update_size(pb, pos);
825 }
826 
828 {
829  int ret;
830  int64_t pos = avio_tell(pb);
831  avio_wb32(pb, 0);
832  ffio_wfourcc(pb, "wfex");
834  return ret;
835  return update_size(pb, pos);
836 }
837 
838 static int mov_write_dfla_tag(AVIOContext *pb, MOVTrack *track)
839 {
840  int64_t pos = avio_tell(pb);
841  avio_wb32(pb, 0);
842  ffio_wfourcc(pb, "dfLa");
843  avio_w8(pb, 0); /* version */
844  avio_wb24(pb, 0); /* flags */
845 
846  /* Expect the encoder to pass a METADATA_BLOCK_TYPE_STREAMINFO. */
847  if (track->par->extradata_size != FLAC_STREAMINFO_SIZE)
848  return AVERROR_INVALIDDATA;
849 
850  /* TODO: Write other METADATA_BLOCK_TYPEs if the encoder makes them available. */
851  avio_w8(pb, 1 << 7 | FLAC_METADATA_TYPE_STREAMINFO); /* LastMetadataBlockFlag << 7 | BlockType */
852  avio_wb24(pb, track->par->extradata_size); /* Length */
853  avio_write(pb, track->par->extradata, track->par->extradata_size); /* BlockData[Length] */
854 
855  return update_size(pb, pos);
856 }
857 
859 {
860  int64_t pos = avio_tell(pb);
861  int channels, channel_map;
862  avio_wb32(pb, 0);
863  ffio_wfourcc(pb, "dOps");
864  avio_w8(pb, 0); /* Version */
865  if (track->par->extradata_size < 19) {
866  av_log(s, AV_LOG_ERROR, "invalid extradata size\n");
867  return AVERROR_INVALIDDATA;
868  }
869  /* extradata contains an Ogg OpusHead, other than byte-ordering and
870  OpusHead's preceeding magic/version, OpusSpecificBox is currently
871  identical. */
872  channels = AV_RB8(track->par->extradata + 9);
873  channel_map = AV_RB8(track->par->extradata + 18);
874 
875  avio_w8(pb, channels); /* OuputChannelCount */
876  avio_wb16(pb, AV_RL16(track->par->extradata + 10)); /* PreSkip */
877  avio_wb32(pb, AV_RL32(track->par->extradata + 12)); /* InputSampleRate */
878  avio_wb16(pb, AV_RL16(track->par->extradata + 16)); /* OutputGain */
879  avio_w8(pb, channel_map); /* ChannelMappingFamily */
880  /* Write the rest of the header out without byte-swapping. */
881  if (channel_map) {
882  if (track->par->extradata_size < 21 + channels) {
883  av_log(s, AV_LOG_ERROR, "invalid extradata size\n");
884  return AVERROR_INVALIDDATA;
885  }
886  avio_write(pb, track->par->extradata + 19, 2 + channels); /* ChannelMappingTable */
887  }
888 
889  return update_size(pb, pos);
890 }
891 
893 {
894  int64_t pos = avio_tell(pb);
895  int length;
896  avio_wb32(pb, 0);
897  ffio_wfourcc(pb, "dmlp");
898 
899  if (track->vos_len < 20) {
901  "Cannot write moov atom before TrueHD packets."
902  " Set the delay_moov flag to fix this.\n");
903  return AVERROR(EINVAL);
904  }
905 
906  length = (AV_RB16(track->vos_data) & 0xFFF) * 2;
907  if (length < 20 || length > track->vos_len)
908  return AVERROR_INVALIDDATA;
909 
910  // Only TrueHD is supported
911  if (AV_RB32(track->vos_data + 4) != 0xF8726FBA)
912  return AVERROR_INVALIDDATA;
913 
914  avio_wb32(pb, AV_RB32(track->vos_data + 8)); /* format_info */
915  avio_wb16(pb, AV_RB16(track->vos_data + 18) << 1); /* peak_data_rate */
916  avio_wb32(pb, 0); /* reserved */
917 
918  return update_size(pb, pos);
919 }
920 
922 {
923  const AVDictionaryEntry *str = av_dict_get(track->st->metadata, "SA3D", NULL, 0);
924  AVChannelLayout ch_layout = { 0 };
925  int64_t pos;
926  int ambisonic_order, ambi_channels, non_diegetic_channels;
927  int i, ret;
928 
929  if (!str)
930  return 0;
931 
932  ret = av_channel_layout_from_string(&ch_layout, str->value);
933  if (ret < 0) {
934  if (ret == AVERROR(EINVAL)) {
935 invalid:
936  av_log(s, AV_LOG_ERROR, "Invalid SA3D layout: \"%s\"\n", str->value);
937  ret = 0;
938  }
939  av_channel_layout_uninit(&ch_layout);
940  return ret;
941  }
942 
943  if (track->st->codecpar->ch_layout.nb_channels != ch_layout.nb_channels)
944  goto invalid;
945 
946  ambisonic_order = av_channel_layout_ambisonic_order(&ch_layout);
947  if (ambisonic_order < 0)
948  goto invalid;
949 
950  ambi_channels = (ambisonic_order + 1LL) * (ambisonic_order + 1LL);
951  non_diegetic_channels = ch_layout.nb_channels - ambi_channels;
952  if (non_diegetic_channels &&
953  (non_diegetic_channels != 2 ||
955  goto invalid;
956 
957  av_log(s, AV_LOG_VERBOSE, "Inserting SA3D box with layout: \"%s\"\n", str->value);
958 
959  pos = avio_tell(pb);
960 
961  avio_wb32(pb, 0); // Size
962  ffio_wfourcc(pb, "SA3D");
963  avio_w8(pb, 0); // version
964  avio_w8(pb, (!!non_diegetic_channels) << 7); // head_locked_stereo and ambisonic_type
965  avio_wb32(pb, ambisonic_order); // ambisonic_order
966  avio_w8(pb, 0); // ambisonic_channel_ordering
967  avio_w8(pb, 0); // ambisonic_normalization
968  avio_wb32(pb, ch_layout.nb_channels); // num_channels
969  for (i = 0; i < ambi_channels; i++)
971  for (; i < ch_layout.nb_channels; i++)
972  avio_wb32(pb, av_channel_layout_channel_from_index(&ch_layout, i) + ambi_channels);
973 
974  av_channel_layout_uninit(&ch_layout);
975 
976  return update_size(pb, pos);
977 }
978 
980 {
981  uint32_t layout_tag, bitmap, *channel_desc;
982  int64_t pos = avio_tell(pb);
983  int num_desc, ret;
984 
985  if (track->multichannel_as_mono)
986  return 0;
987 
988  ret = ff_mov_get_channel_layout_tag(track->par, &layout_tag,
989  &bitmap, &channel_desc);
990 
991  if (ret < 0) {
992  if (ret == AVERROR(ENOSYS)) {
993  av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to "
994  "lack of channel information\n");
995  ret = 0;
996  }
997 
998  return ret;
999  }
1000 
1001  if (layout_tag == MOV_CH_LAYOUT_MONO && track->mono_as_fc > 0) {
1002  av_assert0(!channel_desc);
1003  channel_desc = av_malloc(sizeof(*channel_desc));
1004  if (!channel_desc)
1005  return AVERROR(ENOMEM);
1006 
1007  layout_tag = 0;
1008  bitmap = 0;
1009  *channel_desc = 3; // channel label "Center"
1010  }
1011 
1012  num_desc = layout_tag ? 0 : track->par->ch_layout.nb_channels;
1013 
1014  avio_wb32(pb, 0); // Size
1015  ffio_wfourcc(pb, "chan"); // Type
1016  avio_w8(pb, 0); // Version
1017  avio_wb24(pb, 0); // Flags
1018  avio_wb32(pb, layout_tag); // mChannelLayoutTag
1019  avio_wb32(pb, bitmap); // mChannelBitmap
1020  avio_wb32(pb, num_desc); // mNumberChannelDescriptions
1021 
1022  for (int i = 0; i < num_desc; i++) {
1023  avio_wb32(pb, channel_desc[i]); // mChannelLabel
1024  avio_wb32(pb, 0); // mChannelFlags
1025  avio_wl32(pb, 0); // mCoordinates[0]
1026  avio_wl32(pb, 0); // mCoordinates[1]
1027  avio_wl32(pb, 0); // mCoordinates[2]
1028  }
1029 
1030  av_free(channel_desc);
1031 
1032  return update_size(pb, pos);
1033 }
1034 
1036 {
1037  int64_t pos = avio_tell(pb);
1038 
1039  avio_wb32(pb, 0); /* size */
1040  ffio_wfourcc(pb, "wave");
1041 
1042  if (track->par->codec_id != AV_CODEC_ID_QDM2) {
1043  avio_wb32(pb, 12); /* size */
1044  ffio_wfourcc(pb, "frma");
1045  avio_wl32(pb, track->tag);
1046  }
1047 
1048  if (track->par->codec_id == AV_CODEC_ID_AAC) {
1049  /* useless atom needed by mplayer, ipod, not needed by quicktime */
1050  avio_wb32(pb, 12); /* size */
1051  ffio_wfourcc(pb, "mp4a");
1052  avio_wb32(pb, 0);
1053  mov_write_esds_tag(pb, track);
1054  } else if (mov_pcm_le_gt16(track->par->codec_id)) {
1055  mov_write_enda_tag(pb);
1056  } else if (mov_pcm_be_gt16(track->par->codec_id)) {
1058  } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
1059  mov_write_amr_tag(pb, track);
1060  } else if (track->par->codec_id == AV_CODEC_ID_AC3) {
1061  mov_write_ac3_tag(s, pb, track);
1062  } else if (track->par->codec_id == AV_CODEC_ID_EAC3) {
1063  mov_write_eac3_tag(s, pb, track);
1064  } else if (track->par->codec_id == AV_CODEC_ID_ALAC ||
1065  track->par->codec_id == AV_CODEC_ID_QDM2) {
1066  mov_write_extradata_tag(pb, track);
1067  } else if (track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1068  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
1069  mov_write_ms_tag(s, pb, track);
1070  }
1071 
1072  avio_wb32(pb, 8); /* size */
1073  avio_wb32(pb, 0); /* null tag */
1074 
1075  return update_size(pb, pos);
1076 }
1077 
1078 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
1079 {
1080  uint8_t *unescaped;
1081  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
1082  int unescaped_size, seq_found = 0;
1083  int level = 0, interlace = 0;
1084  int packet_seq = track->vc1_info.packet_seq;
1085  int packet_entry = track->vc1_info.packet_entry;
1086  int slices = track->vc1_info.slices;
1087  PutBitContext pbc;
1088 
1089  if (track->start_dts == AV_NOPTS_VALUE) {
1090  /* No packets written yet, vc1_info isn't authoritative yet. */
1091  /* Assume inline sequence and entry headers. */
1092  packet_seq = packet_entry = 1;
1094  "moov atom written before any packets, unable to write correct "
1095  "dvc1 atom. Set the delay_moov flag to fix this.\n");
1096  }
1097 
1098  unescaped = av_mallocz(track->vos_len + AV_INPUT_BUFFER_PADDING_SIZE);
1099  if (!unescaped)
1100  return AVERROR(ENOMEM);
1101  start = find_next_marker(track->vos_data, end);
1102  for (next = start; next < end; start = next) {
1103  GetBitContext gb;
1104  int size;
1105  next = find_next_marker(start + 4, end);
1106  size = next - start - 4;
1107  if (size <= 0)
1108  continue;
1109  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
1110  init_get_bits(&gb, unescaped, 8 * unescaped_size);
1111  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
1112  int profile = get_bits(&gb, 2);
1113  if (profile != PROFILE_ADVANCED) {
1114  av_free(unescaped);
1115  return AVERROR(ENOSYS);
1116  }
1117  seq_found = 1;
1118  level = get_bits(&gb, 3);
1119  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
1120  * width, height */
1121  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
1122  skip_bits(&gb, 1); /* broadcast */
1123  interlace = get_bits1(&gb);
1124  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
1125  }
1126  }
1127  if (!seq_found) {
1128  av_free(unescaped);
1129  return AVERROR(ENOSYS);
1130  }
1131 
1132  init_put_bits(&pbc, buf, 7);
1133  /* VC1DecSpecStruc */
1134  put_bits(&pbc, 4, 12); /* profile - advanced */
1135  put_bits(&pbc, 3, level);
1136  put_bits(&pbc, 1, 0); /* reserved */
1137  /* VC1AdvDecSpecStruc */
1138  put_bits(&pbc, 3, level);
1139  put_bits(&pbc, 1, 0); /* cbr */
1140  put_bits(&pbc, 6, 0); /* reserved */
1141  put_bits(&pbc, 1, !interlace); /* no interlace */
1142  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
1143  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
1144  put_bits(&pbc, 1, !slices); /* no slice code */
1145  put_bits(&pbc, 1, 0); /* no bframe */
1146  put_bits(&pbc, 1, 0); /* reserved */
1147 
1148  /* framerate */
1149  if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
1150  put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
1151  else
1152  put_bits32(&pbc, 0xffffffff);
1153 
1154  flush_put_bits(&pbc);
1155 
1156  av_free(unescaped);
1157 
1158  return 0;
1159 }
1160 
1161 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
1162 {
1163  uint8_t buf[7] = { 0 };
1164  int ret;
1165 
1166  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
1167  return ret;
1168 
1169  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
1170  ffio_wfourcc(pb, "dvc1");
1171  avio_write(pb, buf, sizeof(buf));
1172  avio_write(pb, track->vos_data, track->vos_len);
1173 
1174  return 0;
1175 }
1176 
1177 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
1178 {
1179  avio_wb32(pb, track->vos_len + 8);
1180  ffio_wfourcc(pb, "glbl");
1181  avio_write(pb, track->vos_data, track->vos_len);
1182  return 8 + track->vos_len;
1183 }
1184 
1185 /**
1186  * Compute flags for 'lpcm' tag.
1187  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1188  */
1190 {
1191  switch (codec_id) {
1192  case AV_CODEC_ID_PCM_F32BE:
1193  case AV_CODEC_ID_PCM_F64BE:
1194  return 11;
1195  case AV_CODEC_ID_PCM_F32LE:
1196  case AV_CODEC_ID_PCM_F64LE:
1197  return 9;
1198  case AV_CODEC_ID_PCM_U8:
1199  return 10;
1200  case AV_CODEC_ID_PCM_S16BE:
1201  case AV_CODEC_ID_PCM_S24BE:
1202  case AV_CODEC_ID_PCM_S32BE:
1203  return 14;
1204  case AV_CODEC_ID_PCM_S8:
1205  case AV_CODEC_ID_PCM_S16LE:
1206  case AV_CODEC_ID_PCM_S24LE:
1207  case AV_CODEC_ID_PCM_S32LE:
1208  return 12;
1209  default:
1210  return 0;
1211  }
1212 }
1213 
1214 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
1215 {
1216  int64_t next_dts;
1217 
1218  if (cluster_idx >= track->entry)
1219  return 0;
1220 
1221  if (cluster_idx + 1 == track->entry)
1222  next_dts = track->track_duration + track->start_dts;
1223  else
1224  next_dts = track->cluster[cluster_idx + 1].dts;
1225 
1226  next_dts -= track->cluster[cluster_idx].dts;
1227 
1228  av_assert0(next_dts >= 0);
1229  av_assert0(next_dts <= INT_MAX);
1230 
1231  return next_dts;
1232 }
1233 
1235 {
1236  int i, first_duration;
1237 
1238  /* use 1 for raw PCM */
1239  if (!track->audio_vbr)
1240  return 1;
1241 
1242  /* check to see if duration is constant for all clusters */
1243  if (!track->entry)
1244  return 0;
1245  first_duration = get_cluster_duration(track, 0);
1246  for (i = 1; i < track->entry; i++) {
1247  if (get_cluster_duration(track, i) != first_duration)
1248  return 0;
1249  }
1250  return first_duration;
1251 }
1252 
1253 static int mov_write_btrt_tag(AVIOContext *pb, MOVTrack *track)
1254 {
1255  int64_t pos = avio_tell(pb);
1256  struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track);
1257  if (!bit_rates.max_bit_rate && !bit_rates.avg_bit_rate &&
1258  !bit_rates.buffer_size)
1259  // no useful data to be written, skip
1260  return 0;
1261 
1262  avio_wb32(pb, 0); /* size */
1263  ffio_wfourcc(pb, "btrt");
1264 
1265  avio_wb32(pb, bit_rates.buffer_size);
1266  avio_wb32(pb, bit_rates.max_bit_rate);
1267  avio_wb32(pb, bit_rates.avg_bit_rate);
1268 
1269  return update_size(pb, pos);
1270 }
1271 
1273 {
1274  int64_t pos = avio_tell(pb);
1275  int config = 0;
1276  int ret;
1277  uint8_t *speaker_pos = NULL;
1278  const AVChannelLayout *layout = &track->par->ch_layout;
1279 
1281  if (ret || !config) {
1282  config = 0;
1283  speaker_pos = av_malloc(layout->nb_channels);
1284  if (!speaker_pos)
1285  return AVERROR(ENOMEM);
1287  speaker_pos, layout->nb_channels);
1288  if (ret) {
1289  char buf[128] = {0};
1290 
1291  av_freep(&speaker_pos);
1292  av_channel_layout_describe(layout, buf, sizeof(buf));
1293  av_log(s, AV_LOG_ERROR, "unsupported channel layout %s\n", buf);
1294  return ret;
1295  }
1296  }
1297 
1298  avio_wb32(pb, 0); /* size */
1299  ffio_wfourcc(pb, "chnl");
1300  avio_wb32(pb, 0); /* version & flags */
1301 
1302  avio_w8(pb, 1); /* stream_structure */
1303  avio_w8(pb, config);
1304  if (config) {
1305  avio_wb64(pb, 0);
1306  } else {
1307  avio_write(pb, speaker_pos, layout->nb_channels);
1308  av_freep(&speaker_pos);
1309  }
1310 
1311  return update_size(pb, pos);
1312 }
1313 
1315 {
1316  int64_t pos = avio_tell(pb);
1317  int format_flags;
1318  int sample_size;
1319 
1320  avio_wb32(pb, 0); /* size */
1321  ffio_wfourcc(pb, "pcmC");
1322  avio_wb32(pb, 0); /* version & flags */
1323 
1324  /* 0x01: indicates little-endian format */
1325  format_flags = (track->par->codec_id == AV_CODEC_ID_PCM_F32LE ||
1326  track->par->codec_id == AV_CODEC_ID_PCM_F64LE ||
1327  track->par->codec_id == AV_CODEC_ID_PCM_S16LE ||
1328  track->par->codec_id == AV_CODEC_ID_PCM_S24LE ||
1329  track->par->codec_id == AV_CODEC_ID_PCM_S32LE);
1330  avio_w8(pb, format_flags);
1331  sample_size = track->par->bits_per_raw_sample;
1332  if (!sample_size)
1333  sample_size = av_get_exact_bits_per_sample(track->par->codec_id);
1334  av_assert0(sample_size);
1335  avio_w8(pb, sample_size);
1336 
1337  return update_size(pb, pos);
1338 }
1339 
1341 {
1342  int64_t pos = avio_tell(pb);
1343  int version = 0;
1344  uint32_t tag = track->tag;
1345  int ret = 0;
1346 
1347  if (track->mode == MODE_MOV) {
1348  if (track->timescale > UINT16_MAX || !track->par->ch_layout.nb_channels) {
1349  if (mov_get_lpcm_flags(track->par->codec_id))
1350  tag = AV_RL32("lpcm");
1351  version = 2;
1352  } else if (track->audio_vbr || mov_pcm_le_gt16(track->par->codec_id) ||
1353  mov_pcm_be_gt16(track->par->codec_id) ||
1354  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1355  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
1356  track->par->codec_id == AV_CODEC_ID_QDM2) {
1357  version = 1;
1358  }
1359  }
1360 
1361  avio_wb32(pb, 0); /* size */
1362  if (mov->encryption_scheme != MOV_ENC_NONE) {
1363  ffio_wfourcc(pb, "enca");
1364  } else {
1365  avio_wl32(pb, tag); // store it byteswapped
1366  }
1367  avio_wb32(pb, 0); /* Reserved */
1368  avio_wb16(pb, 0); /* Reserved */
1369  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
1370 
1371  /* SoundDescription */
1372  avio_wb16(pb, version); /* Version */
1373  avio_wb16(pb, 0); /* Revision level */
1374  avio_wb32(pb, 0); /* Reserved */
1375 
1376  if (version == 2) {
1377  avio_wb16(pb, 3);
1378  avio_wb16(pb, 16);
1379  avio_wb16(pb, 0xfffe);
1380  avio_wb16(pb, 0);
1381  avio_wb32(pb, 0x00010000);
1382  avio_wb32(pb, 72);
1383  avio_wb64(pb, av_double2int(track->par->sample_rate));
1384  avio_wb32(pb, track->par->ch_layout.nb_channels);
1385  avio_wb32(pb, 0x7F000000);
1387  avio_wb32(pb, mov_get_lpcm_flags(track->par->codec_id));
1388  avio_wb32(pb, track->sample_size);
1389  avio_wb32(pb, get_samples_per_packet(track));
1390  } else {
1391  if (track->mode == MODE_MOV) {
1392  avio_wb16(pb, track->par->ch_layout.nb_channels);
1393  if (track->par->codec_id == AV_CODEC_ID_PCM_U8 ||
1394  track->par->codec_id == AV_CODEC_ID_PCM_S8)
1395  avio_wb16(pb, 8); /* bits per sample */
1396  else if (track->par->codec_id == AV_CODEC_ID_ADPCM_G726)
1397  avio_wb16(pb, track->par->bits_per_coded_sample);
1398  else
1399  avio_wb16(pb, 16);
1400  avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
1401  } else { /* reserved for mp4/3gp */
1402  avio_wb16(pb, track->tag == MKTAG('i', 'a', 'm', 'f') ?
1403  0 : track->par->ch_layout.nb_channels);
1404  if (track->par->codec_id == AV_CODEC_ID_FLAC ||
1405  track->par->codec_id == AV_CODEC_ID_ALAC) {
1406  avio_wb16(pb, track->par->bits_per_raw_sample);
1407  } else {
1408  avio_wb16(pb, 16);
1409  }
1410  avio_wb16(pb, 0);
1411  }
1412 
1413  avio_wb16(pb, 0); /* packet size (= 0) */
1414  if (track->tag == MKTAG('i','a','m','f'))
1415  avio_wb16(pb, 0); /* samplerate must be 0 for IAMF */
1416  else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1417  avio_wb16(pb, 48000);
1418  else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1419  avio_wb32(pb, track->par->sample_rate);
1420  else
1421  avio_wb16(pb, track->par->sample_rate <= UINT16_MAX ?
1422  track->par->sample_rate : 0);
1423 
1424  if (track->par->codec_id != AV_CODEC_ID_TRUEHD)
1425  avio_wb16(pb, 0); /* Reserved */
1426  }
1427 
1428  if (version == 1) { /* SoundDescription V1 extended info */
1429  if (mov_pcm_le_gt16(track->par->codec_id) ||
1430  mov_pcm_be_gt16(track->par->codec_id))
1431  avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
1432  else
1433  avio_wb32(pb, track->par->frame_size); /* Samples per packet */
1434  avio_wb32(pb, track->sample_size / track->par->ch_layout.nb_channels); /* Bytes per packet */
1435  avio_wb32(pb, track->sample_size); /* Bytes per frame */
1436  avio_wb32(pb, 2); /* Bytes per sample */
1437  }
1438 
1439  if (track->mode == MODE_MOV &&
1440  (track->par->codec_id == AV_CODEC_ID_AAC ||
1441  track->par->codec_id == AV_CODEC_ID_AC3 ||
1442  track->par->codec_id == AV_CODEC_ID_EAC3 ||
1443  track->par->codec_id == AV_CODEC_ID_AMR_NB ||
1444  track->par->codec_id == AV_CODEC_ID_ALAC ||
1445  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1446  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
1447  track->par->codec_id == AV_CODEC_ID_QDM2 ||
1448  (mov_pcm_le_gt16(track->par->codec_id) && version==1) ||
1449  (mov_pcm_be_gt16(track->par->codec_id) && version==1)))
1450  ret = mov_write_wave_tag(s, pb, track);
1451  else if (track->tag == MKTAG('m','p','4','a'))
1452  ret = mov_write_esds_tag(pb, track);
1453 #if CONFIG_IAMFENC
1454  else if (track->tag == MKTAG('i','a','m','f'))
1455  ret = mov_write_iacb_tag(mov->fc, pb, track);
1456 #endif
1457  else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
1458  ret = mov_write_amr_tag(pb, track);
1459  else if (track->par->codec_id == AV_CODEC_ID_AC3)
1460  ret = mov_write_ac3_tag(s, pb, track);
1461  else if (track->par->codec_id == AV_CODEC_ID_EAC3)
1462  ret = mov_write_eac3_tag(s, pb, track);
1463  else if (track->par->codec_id == AV_CODEC_ID_ALAC)
1464  ret = mov_write_extradata_tag(pb, track);
1465  else if (track->par->codec_id == AV_CODEC_ID_WMAPRO)
1466  ret = mov_write_wfex_tag(s, pb, track);
1467  else if (track->par->codec_id == AV_CODEC_ID_FLAC)
1468  ret = mov_write_dfla_tag(pb, track);
1469  else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1470  ret = mov_write_dops_tag(s, pb, track);
1471  else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1472  ret = mov_write_dmlp_tag(s, pb, track);
1473  else if (tag == MOV_MP4_IPCM_TAG || tag == MOV_MP4_FPCM_TAG) {
1474  if (track->par->ch_layout.nb_channels > 1)
1475  ret = mov_write_chnl_tag(s, pb, track);
1476  if (ret < 0)
1477  return ret;
1478  ret = mov_write_pcmc_tag(s, pb, track);
1479  } else if (track->vos_len > 0)
1480  ret = mov_write_glbl_tag(pb, track);
1481 
1482  if (ret < 0)
1483  return ret;
1484 
1485  if (track->mode == MODE_MP4 && track->par->codec_type == AVMEDIA_TYPE_AUDIO
1486  && ((ret = mov_write_SA3D_tag(s, pb, track)) < 0)) {
1487  return ret;
1488  }
1489 
1490  if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO
1491  && ((ret = mov_write_chan_tag(s, pb, track)) < 0)) {
1492  return ret;
1493  }
1494 
1495  if (mov->encryption_scheme != MOV_ENC_NONE
1496  && ((ret = ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid)) < 0)) {
1497  return ret;
1498  }
1499 
1500  if (mov->write_btrt &&
1501  ((ret = mov_write_btrt_tag(pb, track)) < 0))
1502  return ret;
1503 
1504  ret = update_size(pb, pos);
1505  return ret;
1506 }
1507 
1509 {
1510  avio_wb32(pb, 0xf); /* size */
1511  ffio_wfourcc(pb, "d263");
1512  ffio_wfourcc(pb, "FFMP");
1513  avio_w8(pb, 0); /* decoder version */
1514  /* FIXME use AVCodecContext level/profile, when encoder will set values */
1515  avio_w8(pb, 0xa); /* level */
1516  avio_w8(pb, 0); /* profile */
1517  return 0xf;
1518 }
1519 
1520 static int mov_write_av1c_tag(AVIOContext *pb, MOVTrack *track)
1521 {
1522  int64_t pos = avio_tell(pb);
1523 
1524  avio_wb32(pb, 0);
1525  ffio_wfourcc(pb, "av1C");
1526  ff_isom_write_av1c(pb, track->vos_data, track->vos_len, track->mode != MODE_AVIF);
1527  return update_size(pb, pos);
1528 }
1529 
1530 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
1531 {
1532  int64_t pos = avio_tell(pb);
1533 
1534  avio_wb32(pb, 0);
1535  ffio_wfourcc(pb, "avcC");
1536  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
1537  return update_size(pb, pos);
1538 }
1539 
1541 {
1542  int64_t pos = avio_tell(pb);
1543 
1544  avio_wb32(pb, 0);
1545  ffio_wfourcc(pb, "vpcC");
1546  ff_isom_write_vpcc(s, pb, track->vos_data, track->vos_len, track->par);
1547  return update_size(pb, pos);
1548 }
1549 
1550 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
1551 {
1552  int64_t pos = avio_tell(pb);
1553 
1554  avio_wb32(pb, 0);
1555  ffio_wfourcc(pb, "hvcC");
1556  if (track->tag == MKTAG('h','v','c','1'))
1557  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 1);
1558  else
1559  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
1560  return update_size(pb, pos);
1561 }
1562 
1563 static int mov_write_lhvc_tag(AVIOContext *pb, MOVTrack *track)
1564 {
1565  int64_t pos = avio_tell(pb);
1566  int ret;
1567 
1568  avio_wb32(pb, 0);
1569  ffio_wfourcc(pb, "lhvC");
1570  if (track->tag == MKTAG('h','v','c','1'))
1571  ret = ff_isom_write_lhvc(pb, track->vos_data, track->vos_len, 1);
1572  else
1573  ret = ff_isom_write_lhvc(pb, track->vos_data, track->vos_len, 0);
1574 
1575  if (ret < 0) {
1576  avio_seek(pb, pos, SEEK_SET);
1577  return ret;
1578  }
1579 
1580  return update_size(pb, pos);
1581 }
1582 
1583 static int mov_write_evcc_tag(AVIOContext *pb, MOVTrack *track)
1584 {
1585  int64_t pos = avio_tell(pb);
1586 
1587  avio_wb32(pb, 0);
1588  ffio_wfourcc(pb, "evcC");
1589 
1590  if (track->tag == MKTAG('e','v','c','1'))
1591  ff_isom_write_evcc(pb, track->vos_data, track->vos_len, 1);
1592  else
1593  ff_isom_write_evcc(pb, track->vos_data, track->vos_len, 0);
1594 
1595  return update_size(pb, pos);
1596 }
1597 
1598 static int mov_write_vvcc_tag(AVIOContext *pb, MOVTrack *track)
1599 {
1600  int64_t pos = avio_tell(pb);
1601 
1602  avio_wb32(pb, 0);
1603  ffio_wfourcc(pb, "vvcC");
1604 
1605  avio_w8 (pb, 0); /* version */
1606  avio_wb24(pb, 0); /* flags */
1607 
1608  if (track->tag == MKTAG('v','v','c','1'))
1609  ff_isom_write_vvcc(pb, track->vos_data, track->vos_len, 1);
1610  else
1611  ff_isom_write_vvcc(pb, track->vos_data, track->vos_len, 0);
1612  return update_size(pb, pos);
1613 }
1614 
1615 /* also used by all avid codecs (dv, imx, meridien) and their variants */
1616 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
1617 {
1618  int interlaced;
1619  int cid;
1620  int display_width = track->par->width;
1621 
1622  if (track->vos_data && track->vos_len > 0x29) {
1623  if (ff_dnxhd_parse_header_prefix(track->vos_data) != 0) {
1624  /* looks like a DNxHD bit stream */
1625  interlaced = (track->vos_data[5] & 2);
1626  cid = AV_RB32(track->vos_data + 0x28);
1627  } else {
1628  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1629  return 0;
1630  }
1631  } else {
1632  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1633  return 0;
1634  }
1635 
1636  avio_wb32(pb, 24); /* size */
1637  ffio_wfourcc(pb, "ACLR");
1638  ffio_wfourcc(pb, "ACLR");
1639  ffio_wfourcc(pb, "0001");
1640  if (track->par->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1642  avio_wb32(pb, 1); /* Corresponds to 709 in official encoder */
1643  } else { /* Full range (0-255) */
1644  avio_wb32(pb, 2); /* Corresponds to RGB in official encoder */
1645  }
1646  avio_wb32(pb, 0); /* unknown */
1647 
1648  if (track->tag == MKTAG('A','V','d','h')) {
1649  avio_wb32(pb, 32);
1650  ffio_wfourcc(pb, "ADHR");
1651  ffio_wfourcc(pb, "0001");
1652  avio_wb32(pb, cid);
1653  avio_wb32(pb, 0); /* unknown */
1654  avio_wb32(pb, 1); /* unknown */
1655  avio_wb32(pb, 0); /* unknown */
1656  avio_wb32(pb, 0); /* unknown */
1657  return 0;
1658  }
1659 
1660  avio_wb32(pb, 24); /* size */
1661  ffio_wfourcc(pb, "APRG");
1662  ffio_wfourcc(pb, "APRG");
1663  ffio_wfourcc(pb, "0001");
1664  avio_wb32(pb, 1); /* unknown */
1665  avio_wb32(pb, 0); /* unknown */
1666 
1667  avio_wb32(pb, 120); /* size */
1668  ffio_wfourcc(pb, "ARES");
1669  ffio_wfourcc(pb, "ARES");
1670  ffio_wfourcc(pb, "0001");
1671  avio_wb32(pb, cid); /* dnxhd cid, some id ? */
1672  if ( track->par->sample_aspect_ratio.num > 0
1673  && track->par->sample_aspect_ratio.den > 0)
1674  display_width = display_width * track->par->sample_aspect_ratio.num / track->par->sample_aspect_ratio.den;
1675  avio_wb32(pb, display_width);
1676  /* values below are based on samples created with quicktime and avid codecs */
1677  if (interlaced) {
1678  avio_wb32(pb, track->par->height / 2);
1679  avio_wb32(pb, 2); /* unknown */
1680  avio_wb32(pb, 0); /* unknown */
1681  avio_wb32(pb, 4); /* unknown */
1682  } else {
1683  avio_wb32(pb, track->par->height);
1684  avio_wb32(pb, 1); /* unknown */
1685  avio_wb32(pb, 0); /* unknown */
1686  if (track->par->height == 1080)
1687  avio_wb32(pb, 5); /* unknown */
1688  else
1689  avio_wb32(pb, 6); /* unknown */
1690  }
1691  /* padding */
1692  ffio_fill(pb, 0, 10 * 8);
1693 
1694  return 0;
1695 }
1696 
1697 static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
1698 {
1699  avio_wb32(pb, 12);
1700  ffio_wfourcc(pb, "DpxE");
1701  if (track->par->extradata_size >= 12 &&
1702  !memcmp(&track->par->extradata[4], "DpxE", 4)) {
1703  avio_wb32(pb, track->par->extradata[11]);
1704  } else {
1705  avio_wb32(pb, 1);
1706  }
1707  return 0;
1708 }
1709 
1711 {
1712  int tag;
1713 
1714  if (track->par->width == 720) { /* SD */
1715  if (track->par->height == 480) { /* NTSC */
1716  if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1717  else tag = MKTAG('d','v','c',' ');
1718  }else if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1719  else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1720  else tag = MKTAG('d','v','p','p');
1721  } else if (track->par->height == 720) { /* HD 720 line */
1722  if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
1723  else tag = MKTAG('d','v','h','p');
1724  } else if (track->par->height == 1080) { /* HD 1080 line */
1725  if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
1726  else tag = MKTAG('d','v','h','6');
1727  } else {
1728  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1729  return 0;
1730  }
1731 
1732  return tag;
1733 }
1734 
1736 {
1737  AVRational rational_framerate = st->avg_frame_rate;
1738  int rate = 0;
1739  if (rational_framerate.den != 0)
1740  rate = av_q2d(rational_framerate);
1741  return rate;
1742 }
1743 
1745 {
1746  int tag = track->par->codec_tag;
1748  AVStream *st = track->st;
1749  int rate = defined_frame_rate(s, st);
1750 
1751  if (!tag)
1752  tag = MKTAG('m', '2', 'v', '1'); //fallback tag
1753 
1754  if (track->par->format == AV_PIX_FMT_YUV420P) {
1755  if (track->par->width == 1280 && track->par->height == 720) {
1756  if (!interlaced) {
1757  if (rate == 24) tag = MKTAG('x','d','v','4');
1758  else if (rate == 25) tag = MKTAG('x','d','v','5');
1759  else if (rate == 30) tag = MKTAG('x','d','v','1');
1760  else if (rate == 50) tag = MKTAG('x','d','v','a');
1761  else if (rate == 60) tag = MKTAG('x','d','v','9');
1762  }
1763  } else if (track->par->width == 1440 && track->par->height == 1080) {
1764  if (!interlaced) {
1765  if (rate == 24) tag = MKTAG('x','d','v','6');
1766  else if (rate == 25) tag = MKTAG('x','d','v','7');
1767  else if (rate == 30) tag = MKTAG('x','d','v','8');
1768  } else {
1769  if (rate == 25) tag = MKTAG('x','d','v','3');
1770  else if (rate == 30) tag = MKTAG('x','d','v','2');
1771  }
1772  } else if (track->par->width == 1920 && track->par->height == 1080) {
1773  if (!interlaced) {
1774  if (rate == 24) tag = MKTAG('x','d','v','d');
1775  else if (rate == 25) tag = MKTAG('x','d','v','e');
1776  else if (rate == 30) tag = MKTAG('x','d','v','f');
1777  } else {
1778  if (rate == 25) tag = MKTAG('x','d','v','c');
1779  else if (rate == 30) tag = MKTAG('x','d','v','b');
1780  }
1781  }
1782  } else if (track->par->format == AV_PIX_FMT_YUV422P) {
1783  if (track->par->width == 1280 && track->par->height == 720) {
1784  if (!interlaced) {
1785  if (rate == 24) tag = MKTAG('x','d','5','4');
1786  else if (rate == 25) tag = MKTAG('x','d','5','5');
1787  else if (rate == 30) tag = MKTAG('x','d','5','1');
1788  else if (rate == 50) tag = MKTAG('x','d','5','a');
1789  else if (rate == 60) tag = MKTAG('x','d','5','9');
1790  }
1791  } else if (track->par->width == 1920 && track->par->height == 1080) {
1792  if (!interlaced) {
1793  if (rate == 24) tag = MKTAG('x','d','5','d');
1794  else if (rate == 25) tag = MKTAG('x','d','5','e');
1795  else if (rate == 30) tag = MKTAG('x','d','5','f');
1796  } else {
1797  if (rate == 25) tag = MKTAG('x','d','5','c');
1798  else if (rate == 30) tag = MKTAG('x','d','5','b');
1799  }
1800  }
1801  }
1802 
1803  return tag;
1804 }
1805 
1807 {
1808  int tag = track->par->codec_tag;
1810  AVStream *st = track->st;
1811  int rate = defined_frame_rate(s, st);
1812 
1813  if (!tag)
1814  tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
1815 
1816  if (track->par->format == AV_PIX_FMT_YUV420P10) {
1817  if (track->par->width == 960 && track->par->height == 720) {
1818  if (!interlaced) {
1819  if (rate == 24) tag = MKTAG('a','i','5','p');
1820  else if (rate == 25) tag = MKTAG('a','i','5','q');
1821  else if (rate == 30) tag = MKTAG('a','i','5','p');
1822  else if (rate == 50) tag = MKTAG('a','i','5','q');
1823  else if (rate == 60) tag = MKTAG('a','i','5','p');
1824  }
1825  } else if (track->par->width == 1440 && track->par->height == 1080) {
1826  if (!interlaced) {
1827  if (rate == 24) tag = MKTAG('a','i','5','3');
1828  else if (rate == 25) tag = MKTAG('a','i','5','2');
1829  else if (rate == 30) tag = MKTAG('a','i','5','3');
1830  } else {
1831  if (rate == 50) tag = MKTAG('a','i','5','5');
1832  else if (rate == 60) tag = MKTAG('a','i','5','6');
1833  }
1834  }
1835  } else if (track->par->format == AV_PIX_FMT_YUV422P10) {
1836  if (track->par->width == 1280 && track->par->height == 720) {
1837  if (!interlaced) {
1838  if (rate == 24) tag = MKTAG('a','i','1','p');
1839  else if (rate == 25) tag = MKTAG('a','i','1','q');
1840  else if (rate == 30) tag = MKTAG('a','i','1','p');
1841  else if (rate == 50) tag = MKTAG('a','i','1','q');
1842  else if (rate == 60) tag = MKTAG('a','i','1','p');
1843  }
1844  } else if (track->par->width == 1920 && track->par->height == 1080) {
1845  if (!interlaced) {
1846  if (rate == 24) tag = MKTAG('a','i','1','3');
1847  else if (rate == 25) tag = MKTAG('a','i','1','2');
1848  else if (rate == 30) tag = MKTAG('a','i','1','3');
1849  } else {
1850  if (rate == 25) tag = MKTAG('a','i','1','5');
1851  else if (rate == 50) tag = MKTAG('a','i','1','5');
1852  else if (rate == 60) tag = MKTAG('a','i','1','6');
1853  }
1854  } else if ( track->par->width == 4096 && track->par->height == 2160
1855  || track->par->width == 3840 && track->par->height == 2160
1856  || track->par->width == 2048 && track->par->height == 1080) {
1857  tag = MKTAG('a','i','v','x');
1858  }
1859  }
1860 
1861  return tag;
1862 }
1863 
1865 {
1866  int tag = track->par->codec_tag;
1867 
1868  if (!tag)
1869  tag = MKTAG('e', 'v', 'c', '1');
1870 
1871  return tag;
1872 }
1873 
1874 static const struct {
1876  uint32_t tag;
1877  unsigned bps;
1878 } mov_pix_fmt_tags[] = {
1879  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
1880  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
1881  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
1882  { AV_PIX_FMT_VYU444, MKTAG('v','3','0','8'), 0 },
1883  { AV_PIX_FMT_UYVA, MKTAG('v','4','0','8'), 0 },
1884  { AV_PIX_FMT_V30XLE, MKTAG('v','4','1','0'), 0 },
1885  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
1886  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
1887  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
1888  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
1889  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
1890  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
1891  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
1892  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
1893  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
1894  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
1895  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
1896  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1897 };
1898 
1900 {
1901  int tag = MKTAG('A','V','d','n');
1902  if (track->par->profile != AV_PROFILE_UNKNOWN &&
1903  track->par->profile != AV_PROFILE_DNXHD)
1904  tag = MKTAG('A','V','d','h');
1905  return tag;
1906 }
1907 
1909 {
1910  int tag = track->par->codec_tag;
1911  int i;
1912  enum AVPixelFormat pix_fmt;
1913 
1914  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1915  if (track->par->format == mov_pix_fmt_tags[i].pix_fmt) {
1916  tag = mov_pix_fmt_tags[i].tag;
1918  if (track->par->codec_tag == mov_pix_fmt_tags[i].tag)
1919  break;
1920  }
1921  }
1922 
1924  track->par->bits_per_coded_sample);
1925  if (tag == MKTAG('r','a','w',' ') &&
1926  track->par->format != pix_fmt &&
1927  track->par->format != AV_PIX_FMT_GRAY8 &&
1928  track->par->format != AV_PIX_FMT_NONE)
1929  av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
1930  av_get_pix_fmt_name(track->par->format));
1931  return tag;
1932 }
1933 
1934 static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1935 {
1936  unsigned int tag = track->par->codec_tag;
1937 
1938  // "rtp " is used to distinguish internally created RTP-hint tracks
1939  // (with rtp_ctx) from other tracks.
1940  if (tag == MKTAG('r','t','p',' '))
1941  tag = 0;
1942  if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1943  (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
1944  track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
1945  track->par->codec_id == AV_CODEC_ID_H263 ||
1946  track->par->codec_id == AV_CODEC_ID_H264 ||
1947  track->par->codec_id == AV_CODEC_ID_DNXHD ||
1948  track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1949  av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
1950  if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
1951  tag = mov_get_dv_codec_tag(s, track);
1952  else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
1953  tag = mov_get_rawvideo_codec_tag(s, track);
1954  else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1956  else if (track->par->codec_id == AV_CODEC_ID_H264)
1957  tag = mov_get_h264_codec_tag(s, track);
1958  else if (track->par->codec_id == AV_CODEC_ID_EVC)
1959  tag = mov_get_evc_codec_tag(s, track);
1960  else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
1961  tag = mov_get_dnxhd_codec_tag(s, track);
1962  else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1964  if (!tag) { // if no mac fcc found, try with Microsoft tags
1966  if (tag)
1967  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1968  "the file may be unplayable!\n");
1969  }
1970  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
1972  if (!tag) { // if no mac fcc found, try with Microsoft tags
1973  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
1974  if (ms_tag) {
1975  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1976  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1977  "the file may be unplayable!\n");
1978  }
1979  }
1980  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
1982  }
1983 
1984  return tag;
1985 }
1986 
1988  { AV_CODEC_ID_MJPEG, 0xD },
1989  { AV_CODEC_ID_PNG, 0xE },
1990  { AV_CODEC_ID_BMP, 0x1B },
1991  { AV_CODEC_ID_NONE, 0 },
1992 };
1993 
1994 static unsigned int validate_codec_tag(const AVCodecTag *const *tags,
1995  unsigned int tag, int codec_id)
1996 {
1997  int i;
1998 
1999  /**
2000  * Check that tag + id is in the table
2001  */
2002  for (i = 0; tags && tags[i]; i++) {
2003  const AVCodecTag *codec_tags = tags[i];
2004  while (codec_tags->id != AV_CODEC_ID_NONE) {
2005  if (ff_toupper4(codec_tags->tag) == ff_toupper4(tag) &&
2006  codec_tags->id == codec_id)
2007  return codec_tags->tag;
2008  codec_tags++;
2009  }
2010  }
2011  return 0;
2012 }
2013 
2014 static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
2015 {
2016  if (is_cover_image(track->st))
2018 
2019  if (track->mode == MODE_IPOD)
2020  if (!av_match_ext(s->url, "m4a") &&
2021  !av_match_ext(s->url, "m4v") &&
2022  !av_match_ext(s->url, "m4b"))
2023  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
2024  "Quicktime/Ipod might not play the file\n");
2025 
2026  if (track->mode == MODE_MOV) {
2027  return mov_get_codec_tag(s, track);
2028  } else
2029  return validate_codec_tag(s->oformat->codec_tag, track->par->codec_tag,
2030  track->par->codec_id);
2031 }
2032 
2033 /** Write uuid atom.
2034  * Needed to make file play in iPods running newest firmware
2035  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
2036  */
2038 {
2039  avio_wb32(pb, 28);
2040  ffio_wfourcc(pb, "uuid");
2041  avio_wb32(pb, 0x6b6840f2);
2042  avio_wb32(pb, 0x5f244fc5);
2043  avio_wb32(pb, 0xba39a51b);
2044  avio_wb32(pb, 0xcf0323f3);
2045  avio_wb32(pb, 0x0);
2046  return 28;
2047 }
2048 
2049 static const uint16_t fiel_data[] = {
2050  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
2051 };
2052 
2053 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
2054 {
2055  unsigned mov_field_order = 0;
2056  if (field_order < FF_ARRAY_ELEMS(fiel_data))
2057  mov_field_order = fiel_data[field_order];
2058  else
2059  return 0;
2060  avio_wb32(pb, 10);
2061  ffio_wfourcc(pb, "fiel");
2062  avio_wb16(pb, mov_field_order);
2063  return 10;
2064 }
2065 
2067 {
2068  MOVMuxContext *mov = s->priv_data;
2069  int ret = AVERROR_BUG;
2070  int64_t pos = avio_tell(pb);
2071  avio_wb32(pb, 0); /* size */
2072  avio_wl32(pb, track->tag); // store it byteswapped
2073  avio_wb32(pb, 0); /* Reserved */
2074  avio_wb16(pb, 0); /* Reserved */
2075  avio_wb16(pb, 1); /* Data-reference index */
2076 
2077  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
2078  mov_write_esds_tag(pb, track);
2079  else if (track->par->codec_id == AV_CODEC_ID_TTML) {
2080  switch (track->par->codec_tag) {
2081  case MOV_ISMV_TTML_TAG:
2082  // ISMV dfxp requires no extradata.
2083  break;
2084  case MOV_MP4_TTML_TAG:
2085  // As specified in 14496-30, XMLSubtitleSampleEntry
2086  // Namespace
2087  avio_put_str(pb, "http://www.w3.org/ns/ttml");
2088  // Empty schema_location
2089  avio_w8(pb, 0);
2090  // Empty auxiliary_mime_types
2091  avio_w8(pb, 0);
2092  break;
2093  default:
2095  "Unknown codec tag '%s' utilized for TTML stream with "
2096  "index %d (track id %d)!\n",
2097  av_fourcc2str(track->par->codec_tag), track->st->index,
2098  track->track_id);
2099  return AVERROR(EINVAL);
2100  }
2101  } else if (track->par->extradata_size)
2102  avio_write(pb, track->par->extradata, track->par->extradata_size);
2103 
2104  if (mov->write_btrt &&
2105  ((ret = mov_write_btrt_tag(pb, track)) < 0))
2106  return ret;
2107 
2108  return update_size(pb, pos);
2109 }
2110 
2112 {
2113  int8_t stereo_mode;
2114 
2115  if (stereo_3d->flags != 0) {
2116  av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d flags %x. st3d not written.\n", stereo_3d->flags);
2117  return 0;
2118  }
2119 
2120  switch (stereo_3d->type) {
2121  case AV_STEREO3D_2D:
2122  stereo_mode = 0;
2123  break;
2124  case AV_STEREO3D_TOPBOTTOM:
2125  stereo_mode = 1;
2126  break;
2128  stereo_mode = 2;
2129  break;
2130  default:
2131  av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d type %s. st3d not written.\n", av_stereo3d_type_name(stereo_3d->type));
2132  return 0;
2133  }
2134  avio_wb32(pb, 13); /* size */
2135  ffio_wfourcc(pb, "st3d");
2136  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2137  avio_w8(pb, stereo_mode);
2138  return 13;
2139 }
2140 
2142 {
2143  int64_t sv3d_pos, svhd_pos, proj_pos;
2144  const char* metadata_source = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : LIBAVFORMAT_IDENT;
2145 
2146  if (spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
2147  spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
2148  spherical_mapping->projection != AV_SPHERICAL_CUBEMAP) {
2149  av_log(s, AV_LOG_WARNING, "Unsupported projection %d. sv3d not written.\n", spherical_mapping->projection);
2150  return 0;
2151  }
2152 
2153  sv3d_pos = avio_tell(pb);
2154  avio_wb32(pb, 0); /* size */
2155  ffio_wfourcc(pb, "sv3d");
2156 
2157  svhd_pos = avio_tell(pb);
2158  avio_wb32(pb, 0); /* size */
2159  ffio_wfourcc(pb, "svhd");
2160  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2161  avio_put_str(pb, metadata_source);
2162  update_size(pb, svhd_pos);
2163 
2164  proj_pos = avio_tell(pb);
2165  avio_wb32(pb, 0); /* size */
2166  ffio_wfourcc(pb, "proj");
2167 
2168  avio_wb32(pb, 24); /* size */
2169  ffio_wfourcc(pb, "prhd");
2170  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2171  avio_wb32(pb, spherical_mapping->yaw);
2172  avio_wb32(pb, spherical_mapping->pitch);
2173  avio_wb32(pb, spherical_mapping->roll);
2174 
2175  switch (spherical_mapping->projection) {
2178  avio_wb32(pb, 28); /* size */
2179  ffio_wfourcc(pb, "equi");
2180  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2181  avio_wb32(pb, spherical_mapping->bound_top);
2182  avio_wb32(pb, spherical_mapping->bound_bottom);
2183  avio_wb32(pb, spherical_mapping->bound_left);
2184  avio_wb32(pb, spherical_mapping->bound_right);
2185  break;
2186  case AV_SPHERICAL_CUBEMAP:
2187  avio_wb32(pb, 20); /* size */
2188  ffio_wfourcc(pb, "cbmp");
2189  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2190  avio_wb32(pb, 0); /* layout */
2191  avio_wb32(pb, spherical_mapping->padding); /* padding */
2192  break;
2193  }
2194  update_size(pb, proj_pos);
2195 
2196  return update_size(pb, sv3d_pos);
2197 }
2198 
2199 static inline int64_t rescale_rational(AVRational q, int b)
2200 {
2201  return av_rescale(q.num, b, q.den);
2202 }
2203 
2205  const AVStereo3D *stereo3d)
2206 {
2207  if (!stereo3d->horizontal_field_of_view.num)
2208  return;
2209 
2210  avio_wb32(pb, 12); /* size */
2211  ffio_wfourcc(pb, "hfov");
2212  avio_wb32(pb, rescale_rational(stereo3d->horizontal_field_of_view, 1000));
2213 }
2214 
2216  const AVSphericalMapping *spherical_mapping)
2217 {
2218  avio_wb32(pb, 24); /* size */
2219  ffio_wfourcc(pb, "proj");
2220  avio_wb32(pb, 16); /* size */
2221  ffio_wfourcc(pb, "prji");
2222  avio_wb32(pb, 0); /* version + flags */
2223 
2224  switch (spherical_mapping->projection) {
2226  ffio_wfourcc(pb, "rect");
2227  break;
2229  ffio_wfourcc(pb, "equi");
2230  break;
2232  ffio_wfourcc(pb, "hequ");
2233  break;
2234  case AV_SPHERICAL_FISHEYE:
2235  ffio_wfourcc(pb, "fish");
2236  break;
2237  default:
2238  av_assert0(0);
2239  }
2240 }
2241 
2243  const AVStereo3D *stereo3d)
2244 {
2245  int64_t pos = avio_tell(pb);
2246  int view = 0;
2247 
2248  avio_wb32(pb, 0); /* size */
2249  ffio_wfourcc(pb, "eyes");
2250 
2251  // stri is mandatory
2252  avio_wb32(pb, 13); /* size */
2253  ffio_wfourcc(pb, "stri");
2254  avio_wb32(pb, 0); /* version + flags */
2255  switch (stereo3d->view) {
2256  case AV_STEREO3D_VIEW_LEFT:
2257  view |= 1 << 0;
2258  break;
2260  view |= 1 << 1;
2261  break;
2263  view |= (1 << 0) | (1 << 1);
2264  break;
2265  }
2266  view |= !!(stereo3d->flags & AV_STEREO3D_FLAG_INVERT) << 3;
2267  avio_w8(pb, view);
2268 
2269  // hero is optional
2270  if (stereo3d->primary_eye != AV_PRIMARY_EYE_NONE) {
2271  avio_wb32(pb, 13); /* size */
2272  ffio_wfourcc(pb, "hero");
2273  avio_wb32(pb, 0); /* version + flags */
2274  avio_w8(pb, stereo3d->primary_eye);
2275  }
2276 
2277  // it's not clear if cams is mandatory or optional
2278  if (stereo3d->baseline) {
2279  avio_wb32(pb, 24); /* size */
2280  ffio_wfourcc(pb, "cams");
2281  avio_wb32(pb, 16); /* size */
2282  ffio_wfourcc(pb, "blin");
2283  avio_wb32(pb, 0); /* version + flags */
2284  avio_wb32(pb, stereo3d->baseline);
2285  }
2286 
2287  // it's not clear if cmfy is mandatory or optional
2288  if (stereo3d->horizontal_disparity_adjustment.num) {
2289  avio_wb32(pb, 24); /* size */
2290  ffio_wfourcc(pb, "cmfy");
2291  avio_wb32(pb, 16); /* size */
2292  ffio_wfourcc(pb, "dadj");
2293  avio_wb32(pb, 0); /* version + flags */
2295  }
2296 
2297  return update_size(pb, pos);
2298 }
2299 
2301  const AVStereo3D *stereo3d,
2302  const AVSphericalMapping *spherical_mapping)
2303 {
2304  int64_t pos;
2305 
2306  if (spherical_mapping &&
2307  spherical_mapping->projection != AV_SPHERICAL_RECTILINEAR &&
2308  spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
2309  spherical_mapping->projection != AV_SPHERICAL_HALF_EQUIRECTANGULAR &&
2310  spherical_mapping->projection != AV_SPHERICAL_FISHEYE) {
2311  av_log(s, AV_LOG_WARNING, "Unsupported projection %d. proj not written.\n",
2312  spherical_mapping->projection);
2313  spherical_mapping = NULL;
2314  }
2315 
2316  if (stereo3d && (stereo3d->type == AV_STEREO3D_2D ||
2317  (!(stereo3d->flags & AV_STEREO3D_FLAG_INVERT) &&
2318  stereo3d->view == AV_STEREO3D_VIEW_UNSPEC &&
2319  stereo3d->primary_eye == AV_PRIMARY_EYE_NONE &&
2320  !stereo3d->baseline &&
2321  !stereo3d->horizontal_disparity_adjustment.num))) {
2322  av_log(s, AV_LOG_WARNING, "Unsupported stereo 3d metadata. eyes not written.\n");
2323  stereo3d = NULL;
2324  }
2325 
2326  if (!spherical_mapping && !stereo3d)
2327  return 0;
2328 
2329  pos = avio_tell(pb);
2330  avio_wb32(pb, 0); /* size */
2331  ffio_wfourcc(pb, "vexu");
2332 
2333  if (spherical_mapping)
2334  mov_write_vexu_proj_tag(s, pb, spherical_mapping);
2335 
2336  if (stereo3d)
2337  mov_write_eyes_tag(s, pb, stereo3d);
2338 
2339  return update_size(pb, pos);
2340 }
2341 
2343 {
2344  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
2345 
2346  avio_wb32(pb, 32); /* size = 8 + 24 */
2347  if (dovi->dv_profile > 10)
2348  ffio_wfourcc(pb, "dvwC");
2349  else if (dovi->dv_profile > 7)
2350  ffio_wfourcc(pb, "dvvC");
2351  else
2352  ffio_wfourcc(pb, "dvcC");
2353 
2354  ff_isom_put_dvcc_dvvc(s, buf, dovi);
2355  avio_write(pb, buf, sizeof(buf));
2356 
2357  return 32; /* 8 + 24 */
2358 }
2359 
2360 static int mov_write_clap_tag(AVIOContext *pb, MOVTrack *track,
2361  uint32_t top, uint32_t bottom,
2362  uint32_t left, uint32_t right)
2363 {
2364  uint32_t cropped_width = track->par->width - left - right;
2365  uint32_t cropped_height = track->height - top - bottom;
2366  AVRational horizOff =
2367  av_sub_q((AVRational) { track->par->width - cropped_width, 2 },
2368  (AVRational) { left, 1 });
2369  AVRational vertOff =
2370  av_sub_q((AVRational) { track->height - cropped_height, 2 },
2371  (AVRational) { top, 1 });
2372 
2373  avio_wb32(pb, 40);
2374  ffio_wfourcc(pb, "clap");
2375  avio_wb32(pb, cropped_width); /* apertureWidthN */
2376  avio_wb32(pb, 1); /* apertureWidthD */
2377  avio_wb32(pb, cropped_height); /* apertureHeightN */
2378  avio_wb32(pb, 1); /* apertureHeightD */
2379 
2380  avio_wb32(pb, -horizOff.num);
2381  avio_wb32(pb, horizOff.den);
2382  avio_wb32(pb, -vertOff.num);
2383  avio_wb32(pb, vertOff.den);
2384 
2385  return 40;
2386 }
2387 
2388 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
2389 {
2390  AVRational sar;
2391  av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
2392  track->par->sample_aspect_ratio.den, INT_MAX);
2393 
2394  avio_wb32(pb, 16);
2395  ffio_wfourcc(pb, "pasp");
2396  avio_wb32(pb, sar.num);
2397  avio_wb32(pb, sar.den);
2398  return 16;
2399 }
2400 
2401 static int mov_write_gama_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track, double gamma)
2402 {
2403  uint32_t gama = 0;
2404  if (gamma <= 0.0)
2405  gamma = av_csp_approximate_trc_gamma(track->par->color_trc);
2406  av_log(s, AV_LOG_DEBUG, "gamma value %g\n", gamma);
2407 
2408  if (gamma > 1e-6) {
2409  gama = (uint32_t)lrint((double)(1<<16) * gamma);
2410  av_log(s, AV_LOG_DEBUG, "writing gama value %"PRId32"\n", gama);
2411 
2412  av_assert0(track->mode == MODE_MOV);
2413  avio_wb32(pb, 12);
2414  ffio_wfourcc(pb, "gama");
2415  avio_wb32(pb, gama);
2416  return 12;
2417  } else {
2418  av_log(s, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
2419  }
2420  return 0;
2421 }
2422 
2423 static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track, int prefer_icc)
2424 {
2425  int64_t pos = avio_tell(pb);
2426 
2427  // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
2428  // Ref (MP4): ISO/IEC 14496-12:2012
2429 
2430  if (prefer_icc) {
2432  track->st->codecpar->nb_coded_side_data,
2434 
2435  if (sd) {
2436  avio_wb32(pb, 12 + sd->size);
2437  ffio_wfourcc(pb, "colr");
2438  ffio_wfourcc(pb, "prof");
2439  avio_write(pb, sd->data, sd->size);
2440  return 12 + sd->size;
2441  }
2442  else {
2443  av_log(NULL, AV_LOG_INFO, "no ICC profile found, will write nclx/nclc colour info instead\n");
2444  }
2445  }
2446 
2447  /* We should only ever be called for MOV, MP4 and AVIF. */
2448  av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4 ||
2449  track->mode == MODE_AVIF);
2450 
2451  avio_wb32(pb, 0); /* size */
2452  ffio_wfourcc(pb, "colr");
2453  if (track->mode == MODE_MP4 || track->mode == MODE_AVIF)
2454  ffio_wfourcc(pb, "nclx");
2455  else
2456  ffio_wfourcc(pb, "nclc");
2457  // Do not try to guess the color info if it is AVCOL_PRI_UNSPECIFIED.
2458  // e.g., Dolby Vision for Apple devices should be set to AVCOL_PRI_UNSPECIFIED. See
2459  // https://developer.apple.com/av-foundation/High-Dynamic-Range-Metadata-for-Apple-Devices.pdf
2460  avio_wb16(pb, track->par->color_primaries);
2461  avio_wb16(pb, track->par->color_trc);
2462  avio_wb16(pb, track->par->color_space);
2463  if (track->mode == MODE_MP4 || track->mode == MODE_AVIF) {
2464  int full_range = track->par->color_range == AVCOL_RANGE_JPEG;
2465  avio_w8(pb, full_range << 7);
2466  }
2467 
2468  return update_size(pb, pos);
2469 }
2470 
2471 static int mov_write_clli_tag(AVIOContext *pb, MOVTrack *track)
2472 {
2473  const AVPacketSideData *side_data;
2474  const AVContentLightMetadata *content_light_metadata;
2475 
2476  side_data = av_packet_side_data_get(track->st->codecpar->coded_side_data,
2477  track->st->codecpar->nb_coded_side_data,
2479  if (!side_data) {
2480  return 0;
2481  }
2482  content_light_metadata = (const AVContentLightMetadata*)side_data->data;
2483 
2484  avio_wb32(pb, 12); // size
2485  ffio_wfourcc(pb, "clli");
2486  avio_wb16(pb, content_light_metadata->MaxCLL);
2487  avio_wb16(pb, content_light_metadata->MaxFALL);
2488  return 12;
2489 }
2490 
2491 static int mov_write_mdcv_tag(AVIOContext *pb, MOVTrack *track)
2492 {
2493  const int chroma_den = 50000;
2494  const int luma_den = 10000;
2495  const AVPacketSideData *side_data;
2496  const AVMasteringDisplayMetadata *metadata = NULL;
2497 
2498  side_data = av_packet_side_data_get(track->st->codecpar->coded_side_data,
2499  track->st->codecpar->nb_coded_side_data,
2501  if (side_data)
2502  metadata = (const AVMasteringDisplayMetadata*)side_data->data;
2503  if (!metadata || !metadata->has_primaries || !metadata->has_luminance) {
2504  return 0;
2505  }
2506 
2507  avio_wb32(pb, 32); // size
2508  ffio_wfourcc(pb, "mdcv");
2509  avio_wb16(pb, rescale_rational(metadata->display_primaries[1][0], chroma_den));
2510  avio_wb16(pb, rescale_rational(metadata->display_primaries[1][1], chroma_den));
2511  avio_wb16(pb, rescale_rational(metadata->display_primaries[2][0], chroma_den));
2512  avio_wb16(pb, rescale_rational(metadata->display_primaries[2][1], chroma_den));
2513  avio_wb16(pb, rescale_rational(metadata->display_primaries[0][0], chroma_den));
2514  avio_wb16(pb, rescale_rational(metadata->display_primaries[0][1], chroma_den));
2515  avio_wb16(pb, rescale_rational(metadata->white_point[0], chroma_den));
2516  avio_wb16(pb, rescale_rational(metadata->white_point[1], chroma_den));
2517  avio_wb32(pb, rescale_rational(metadata->max_luminance, luma_den));
2518  avio_wb32(pb, rescale_rational(metadata->min_luminance, luma_den));
2519  return 32;
2520 }
2521 
2522 static int mov_write_amve_tag(AVIOContext *pb, MOVTrack *track)
2523 {
2524  const int illuminance_den = 10000;
2525  const int ambient_den = 50000;
2526  const AVPacketSideData *side_data;
2527  const AVAmbientViewingEnvironment *ambient;
2528 
2529 
2530  side_data = av_packet_side_data_get(track->st->codecpar->coded_side_data,
2531  track->st->codecpar->nb_coded_side_data,
2533 
2534  if (!side_data)
2535  return 0;
2536 
2537  ambient = (const AVAmbientViewingEnvironment*)side_data->data;
2538  if (!ambient || !ambient->ambient_illuminance.num)
2539  return 0;
2540 
2541  avio_wb32(pb, 16); // size
2542  ffio_wfourcc(pb, "amve");
2543  avio_wb32(pb, rescale_rational(ambient->ambient_illuminance, illuminance_den));
2544  avio_wb16(pb, rescale_rational(ambient->ambient_light_x, ambient_den));
2545  avio_wb16(pb, rescale_rational(ambient->ambient_light_y, ambient_den));
2546  return 16;
2547 }
2548 
2549 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
2550 {
2551  AVDictionaryEntry *encoder;
2552  int xdcam_res = (track->par->width == 1280 && track->par->height == 720)
2553  || (track->par->width == 1440 && track->par->height == 1080)
2554  || (track->par->width == 1920 && track->par->height == 1080);
2555 
2556  if ((track->mode == MODE_AVIF ||
2557  track->mode == MODE_MOV ||
2558  track->mode == MODE_MP4) &&
2559  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
2560  av_strlcpy(compressor_name, encoder->value, 32);
2561  } else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
2563  AVStream *st = track->st;
2564  int rate = defined_frame_rate(NULL, st);
2565  av_strlcatf(compressor_name, len, "XDCAM");
2566  if (track->par->format == AV_PIX_FMT_YUV422P) {
2567  av_strlcatf(compressor_name, len, " HD422");
2568  } else if(track->par->width == 1440) {
2569  av_strlcatf(compressor_name, len, " HD");
2570  } else
2571  av_strlcatf(compressor_name, len, " EX");
2572 
2573  av_strlcatf(compressor_name, len, " %d%c", track->par->height, interlaced ? 'i' : 'p');
2574 
2575  av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
2576  }
2577 }
2578 
2580 {
2581  int64_t pos = avio_tell(pb);
2582  // Write sane defaults:
2583  // all_ref_pics_intra = 0 : all samples can use any type of reference.
2584  // intra_pred_used = 1 : intra prediction may or may not be used.
2585  // max_ref_per_pic = 15 : reserved value to indicate that any number of
2586  // reference images can be used.
2587  uint8_t ccstValue = (0 << 7) | /* all_ref_pics_intra */
2588  (1 << 6) | /* intra_pred_used */
2589  (15 << 2); /* max_ref_per_pic */
2590  avio_wb32(pb, 0); /* size */
2591  ffio_wfourcc(pb, "ccst");
2592  avio_wb32(pb, 0); /* Version & flags */
2593  avio_w8(pb, ccstValue);
2594  avio_wb24(pb, 0); /* reserved */
2595  return update_size(pb, pos);
2596 }
2597 
2598 static int mov_write_aux_tag(AVIOContext *pb, const char *aux_type)
2599 {
2600  int64_t pos = avio_tell(pb);
2601  avio_wb32(pb, 0); /* size */
2602  ffio_wfourcc(pb, aux_type);
2603  avio_wb32(pb, 0); /* Version & flags */
2604  avio_write(pb, "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\0", 44);
2605  return update_size(pb, pos);
2606 }
2607 
2609 {
2610  int ret = AVERROR_BUG;
2611  int64_t pos = avio_tell(pb);
2612  const AVPacketSideData *sd;
2613  char compressor_name[32] = { 0 };
2614  int avid = 0;
2615 
2616  int uncompressed_ycbcr = ((track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVY422)
2617  || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_YUYV422)
2618  || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_VYU444)
2619  || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVA)
2620  || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_V30XLE)
2622  || track->par->codec_id == AV_CODEC_ID_V308
2623  || track->par->codec_id == AV_CODEC_ID_V408
2624  || track->par->codec_id == AV_CODEC_ID_V410
2625 #endif
2626  || track->par->codec_id == AV_CODEC_ID_V210);
2627 
2628  avio_wb32(pb, 0); /* size */
2629  if (mov->encryption_scheme != MOV_ENC_NONE) {
2630  ffio_wfourcc(pb, "encv");
2631  } else {
2632  avio_wl32(pb, track->tag); // store it byteswapped
2633  }
2634  avio_wb32(pb, 0); /* Reserved */
2635  avio_wb16(pb, 0); /* Reserved */
2636  avio_wb16(pb, 1); /* Data-reference index */
2637 
2638  if (uncompressed_ycbcr) {
2639  avio_wb16(pb, 2); /* Codec stream version */
2640  } else {
2641  avio_wb16(pb, 0); /* Codec stream version */
2642  }
2643  avio_wb16(pb, 0); /* Codec stream revision (=0) */
2644  if (track->mode == MODE_MOV) {
2645  ffio_wfourcc(pb, "FFMP"); /* Vendor */
2646  if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO || uncompressed_ycbcr) {
2647  avio_wb32(pb, 0); /* Temporal Quality */
2648  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
2649  } else {
2650  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
2651  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
2652  }
2653  } else {
2654  ffio_fill(pb, 0, 3 * 4); /* Reserved */
2655  }
2656  avio_wb16(pb, track->par->width); /* Video width */
2657  avio_wb16(pb, track->height); /* Video height */
2658  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
2659  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
2660  avio_wb32(pb, 0); /* Data size (= 0) */
2661  avio_wb16(pb, 1); /* Frame count (= 1) */
2662 
2663  find_compressor(compressor_name, 32, track);
2664  avio_w8(pb, strlen(compressor_name));
2665  avio_write(pb, compressor_name, 31);
2666 
2667  if (track->mode == MODE_MOV &&
2668  (track->par->codec_id == AV_CODEC_ID_V410 || track->par->codec_id == AV_CODEC_ID_V210))
2669  avio_wb16(pb, 0x18);
2670  else if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
2671  avio_wb16(pb, track->par->bits_per_coded_sample |
2672  (track->par->format == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
2673  else
2674  avio_wb16(pb, 0x18); /* Reserved */
2675 
2676  if (track->mode == MODE_MOV && track->par->format == AV_PIX_FMT_PAL8) {
2677  int pal_size, i;
2678  avio_wb16(pb, 0); /* Color table ID */
2679  avio_wb32(pb, 0); /* Color table seed */
2680  avio_wb16(pb, 0x8000); /* Color table flags */
2681  if (track->par->bits_per_coded_sample < 0 || track->par->bits_per_coded_sample > 8)
2682  return AVERROR(EINVAL);
2683  pal_size = 1 << track->par->bits_per_coded_sample;
2684  avio_wb16(pb, pal_size - 1); /* Color table size (zero-relative) */
2685  for (i = 0; i < pal_size; i++) {
2686  uint32_t rgb = track->palette[i];
2687  uint16_t r = (rgb >> 16) & 0xff;
2688  uint16_t g = (rgb >> 8) & 0xff;
2689  uint16_t b = rgb & 0xff;
2690  avio_wb16(pb, 0);
2691  avio_wb16(pb, (r << 8) | r);
2692  avio_wb16(pb, (g << 8) | g);
2693  avio_wb16(pb, (b << 8) | b);
2694  }
2695  } else
2696  avio_wb16(pb, 0xffff); /* Reserved */
2697 
2698  if (track->tag == MKTAG('m','p','4','v'))
2699  mov_write_esds_tag(pb, track);
2700  else if (track->par->codec_id == AV_CODEC_ID_H263)
2701  mov_write_d263_tag(pb);
2702  else if (track->par->codec_id == AV_CODEC_ID_AVUI ||
2703  track->par->codec_id == AV_CODEC_ID_SVQ3) {
2704  mov_write_extradata_tag(pb, track);
2705  avio_wb32(pb, 0);
2706  } else if (track->par->codec_id == AV_CODEC_ID_DNXHD) {
2707  mov_write_avid_tag(pb, track);
2708  avid = 1;
2709  } else if (track->par->codec_id == AV_CODEC_ID_HEVC) {
2710  mov_write_hvcc_tag(pb, track);
2711  if (track->st->disposition & AV_DISPOSITION_MULTILAYER) {
2712  ret = mov_write_lhvc_tag(pb, track);
2713  if (ret < 0)
2714  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'lhvC' atom for multilayer stream.\n");
2715  }
2716  } else if (track->par->codec_id == AV_CODEC_ID_VVC)
2717  mov_write_vvcc_tag(pb, track);
2718  else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
2719  mov_write_avcc_tag(pb, track);
2720  if (track->mode == MODE_IPOD)
2722  }
2723  else if (track->par->codec_id ==AV_CODEC_ID_EVC) {
2724  mov_write_evcc_tag(pb, track);
2725  } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
2726  mov_write_vpcc_tag(mov->fc, pb, track);
2727  } else if (track->par->codec_id == AV_CODEC_ID_AV1) {
2728  mov_write_av1c_tag(pb, track);
2729  } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
2730  mov_write_dvc1_tag(pb, track);
2731  else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
2732  track->par->codec_id == AV_CODEC_ID_VP6A) {
2733  /* Don't write any potential extradata here - the cropping
2734  * is signalled via the normal width/height fields. */
2735  } else if (track->par->codec_id == AV_CODEC_ID_R10K) {
2736  if (track->par->codec_tag == MKTAG('R','1','0','k'))
2737  mov_write_dpxe_tag(pb, track);
2738  } else if (track->vos_len > 0)
2739  mov_write_glbl_tag(pb, track);
2740 
2741  if (track->par->codec_id != AV_CODEC_ID_H264 &&
2742  track->par->codec_id != AV_CODEC_ID_MPEG4 &&
2743  track->par->codec_id != AV_CODEC_ID_DNXHD) {
2744  int field_order = track->par->field_order;
2745 
2746  if (field_order != AV_FIELD_UNKNOWN)
2747  mov_write_fiel_tag(pb, track, field_order);
2748  }
2749 
2750  if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
2751  if (track->mode == MODE_MOV)
2752  mov_write_gama_tag(s, pb, track, mov->gamma);
2753  else
2754  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
2755  }
2756  if (track->mode == MODE_MOV || track->mode == MODE_MP4 || track->mode == MODE_AVIF) {
2757  int has_color_info = track->par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
2758  track->par->color_trc != AVCOL_TRC_UNSPECIFIED &&
2760  if (has_color_info || mov->flags & FF_MOV_FLAG_WRITE_COLR ||
2763  int prefer_icc = mov->flags & FF_MOV_FLAG_PREFER_ICC || !has_color_info;
2764  mov_write_colr_tag(pb, track, prefer_icc);
2765  }
2766  } else if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
2767  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4 or AVIF.\n");
2768  }
2769 
2770  if (track->mode == MODE_MOV || track->mode == MODE_MP4) {
2771  mov_write_clli_tag(pb, track);
2772  mov_write_mdcv_tag(pb, track);
2773  mov_write_amve_tag(pb, track);
2774  }
2775 
2776  if (track->mode == MODE_MP4 && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
2778  track->st->codecpar->nb_coded_side_data,
2780  const AVPacketSideData *spherical_mapping = av_packet_side_data_get(track->st->codecpar->coded_side_data,
2781  track->st->codecpar->nb_coded_side_data,
2783  if (stereo_3d)
2784  mov_write_st3d_tag(s, pb, (AVStereo3D*)stereo_3d->data);
2785  if (spherical_mapping)
2786  mov_write_sv3d_tag(mov->fc, pb, (AVSphericalMapping*)spherical_mapping->data);
2787  }
2788 
2789  if (track->mode == MODE_MOV || (track->mode == MODE_MP4 &&
2791  const AVStereo3D *stereo3d = NULL;
2792  const AVSphericalMapping *spherical_mapping = NULL;
2793 
2795  track->st->codecpar->nb_coded_side_data,
2797  if (sd)
2798  stereo3d = (AVStereo3D *)sd->data;
2799 
2801  track->st->codecpar->nb_coded_side_data,
2803  if (sd)
2804  spherical_mapping = (AVSphericalMapping *)sd->data;
2805 
2806  if (stereo3d || spherical_mapping)
2807  mov_write_vexu_tag(s, pb, stereo3d, spherical_mapping);
2808  if (stereo3d)
2809  mov_write_hfov_tag(s, pb, stereo3d);
2810  }
2811 
2812  if (track->mode == MODE_MP4) {
2814  track->st->codecpar->nb_coded_side_data,
2816  if (dovi && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
2818  } else if (dovi) {
2819  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'dvcC'/'dvvC' box. Requires -strict unofficial.\n");
2820  }
2821  }
2822 
2823  if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num) {
2824  mov_write_pasp_tag(pb, track);
2825  }
2826 
2828  track->st->codecpar->nb_coded_side_data,
2830  if (sd && sd->size >= sizeof(uint32_t) * 4) {
2831  uint64_t top = AV_RL32(sd->data + 0);
2832  uint64_t bottom = AV_RL32(sd->data + 4);
2833  uint64_t left = AV_RL32(sd->data + 8);
2834  uint64_t right = AV_RL32(sd->data + 12);
2835 
2836  if ((left + right) >= track->par->width ||
2837  (top + bottom) >= track->height) {
2838  av_log(s, AV_LOG_ERROR, "Invalid cropping dimensions in stream side data\n");
2839  return AVERROR(EINVAL);
2840  }
2841  if (top || bottom || left || right)
2842  mov_write_clap_tag(pb, track, top, bottom, left, right);
2843  } else if (uncompressed_ycbcr)
2844  mov_write_clap_tag(pb, track, 0, 0, 0, 0);
2845 
2846  if (mov->encryption_scheme != MOV_ENC_NONE) {
2847  ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
2848  }
2849 
2850  if (mov->write_btrt &&
2851  ((ret = mov_write_btrt_tag(pb, track)) < 0))
2852  return ret;
2853 
2854  /* extra padding for avid stsd */
2855  /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
2856  if (avid)
2857  avio_wb32(pb, 0);
2858 
2859  if (track->mode == MODE_AVIF) {
2860  mov_write_ccst_tag(pb);
2861  if (mov->nb_streams > 0 && track == &mov->tracks[1])
2862  mov_write_aux_tag(pb, "auxi");
2863  }
2864 
2865  return update_size(pb, pos);
2866 }
2867 
2868 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
2869 {
2870  int64_t pos = avio_tell(pb);
2871  avio_wb32(pb, 0); /* size */
2872  ffio_wfourcc(pb, "rtp ");
2873  avio_wb32(pb, 0); /* Reserved */
2874  avio_wb16(pb, 0); /* Reserved */
2875  avio_wb16(pb, 1); /* Data-reference index */
2876 
2877  avio_wb16(pb, 1); /* Hint track version */
2878  avio_wb16(pb, 1); /* Highest compatible version */
2879  avio_wb32(pb, track->max_packet_size); /* Max packet size */
2880 
2881  avio_wb32(pb, 12); /* size */
2882  ffio_wfourcc(pb, "tims");
2883  avio_wb32(pb, track->timescale);
2884 
2885  return update_size(pb, pos);
2886 }
2887 
2888 static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
2889 {
2890  uint64_t str_size =strlen(reel_name);
2891  int64_t pos = avio_tell(pb);
2892 
2893  if (str_size >= UINT16_MAX){
2894  av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
2895  avio_wb16(pb, 0);
2896  return AVERROR(EINVAL);
2897  }
2898 
2899  avio_wb32(pb, 0); /* size */
2900  ffio_wfourcc(pb, "name"); /* Data format */
2901  avio_wb16(pb, str_size); /* string size */
2902  avio_wb16(pb, track->language); /* langcode */
2903  avio_write(pb, reel_name, str_size); /* reel name */
2904  return update_size(pb,pos);
2905 }
2906 
2907 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
2908 {
2909  int64_t pos = avio_tell(pb);
2910 #if 1
2911  int frame_duration;
2912  int nb_frames;
2913  AVDictionaryEntry *t = NULL;
2914 
2915  if (!track->st->avg_frame_rate.num || !track->st->avg_frame_rate.den) {
2916  av_log(NULL, AV_LOG_ERROR, "avg_frame_rate not set for tmcd track.\n");
2917  return AVERROR(EINVAL);
2918  } else {
2919  frame_duration = av_rescale(track->timescale, track->st->avg_frame_rate.den, track->st->avg_frame_rate.num);
2920  nb_frames = ROUNDED_DIV(track->st->avg_frame_rate.num, track->st->avg_frame_rate.den);
2921  }
2922 
2923  if (nb_frames > 255) {
2924  av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
2925  return AVERROR(EINVAL);
2926  }
2927 
2928  avio_wb32(pb, 0); /* size */
2929  ffio_wfourcc(pb, "tmcd"); /* Data format */
2930  avio_wb32(pb, 0); /* Reserved */
2931  avio_wb32(pb, 1); /* Data reference index */
2932  avio_wb32(pb, 0); /* Flags */
2933  avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
2934  avio_wb32(pb, track->timescale); /* Timescale */
2935  avio_wb32(pb, frame_duration); /* Frame duration */
2936  avio_w8(pb, nb_frames); /* Number of frames */
2937  avio_w8(pb, 0); /* Reserved */
2938 
2939  t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
2940  if (t && utf8len(t->value) && track->mode != MODE_MP4)
2941  mov_write_source_reference_tag(pb, track, t->value);
2942  else
2943  avio_wb16(pb, 0); /* zero size */
2944 #else
2945 
2946  avio_wb32(pb, 0); /* size */
2947  ffio_wfourcc(pb, "tmcd"); /* Data format */
2948  avio_wb32(pb, 0); /* Reserved */
2949  avio_wb32(pb, 1); /* Data reference index */
2950  if (track->par->extradata_size)
2951  avio_write(pb, track->par->extradata, track->par->extradata_size);
2952 #endif
2953  return update_size(pb, pos);
2954 }
2955 
2956 static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
2957 {
2958  int64_t pos = avio_tell(pb);
2959  avio_wb32(pb, 0); /* size */
2960  ffio_wfourcc(pb, "gpmd");
2961  avio_wb32(pb, 0); /* Reserved */
2962  avio_wb16(pb, 0); /* Reserved */
2963  avio_wb16(pb, 1); /* Data-reference index */
2964  avio_wb32(pb, 0); /* Reserved */
2965  return update_size(pb, pos);
2966 }
2967 
2969 {
2970  int64_t pos = avio_tell(pb);
2971  int ret = 0;
2972  avio_wb32(pb, 0); /* size */
2973  ffio_wfourcc(pb, "stsd");
2974  avio_wb32(pb, 0); /* version & flags */
2975  avio_wb32(pb, 1); /* entry count */
2976  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2977  ret = mov_write_video_tag(s, pb, mov, track);
2978  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2979  ret = mov_write_audio_tag(s, pb, mov, track);
2980  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2981  ret = mov_write_subtitle_tag(s, pb, track);
2982  else if (track->par->codec_tag == MKTAG('r','t','p',' '))
2983  ret = mov_write_rtp_tag(pb, track);
2984  else if (track->par->codec_tag == MKTAG('t','m','c','d'))
2985  ret = mov_write_tmcd_tag(pb, track);
2986  else if (track->par->codec_tag == MKTAG('g','p','m','d'))
2987  ret = mov_write_gpmd_tag(pb, track);
2988 
2989  if (ret < 0)
2990  return ret;
2991 
2992  return update_size(pb, pos);
2993 }
2994 
2996 {
2997  MOVMuxContext *mov = s->priv_data;
2998  MOVCtts *ctts_entries;
2999  uint32_t entries = 0;
3000  uint32_t atom_size;
3001  int i;
3002 
3003  ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
3004  if (!ctts_entries)
3005  return AVERROR(ENOMEM);
3006  ctts_entries[0].count = 1;
3007  ctts_entries[0].duration = track->cluster[0].cts;
3008  for (i = 1; i < track->entry; i++) {
3009  if (track->cluster[i].cts == ctts_entries[entries].duration) {
3010  ctts_entries[entries].count++; /* compress */
3011  } else {
3012  entries++;
3013  ctts_entries[entries].duration = track->cluster[i].cts;
3014  ctts_entries[entries].count = 1;
3015  }
3016  }
3017  entries++; /* last one */
3018  atom_size = 16 + (entries * 8);
3019  avio_wb32(pb, atom_size); /* size */
3020  ffio_wfourcc(pb, "ctts");
3022  avio_w8(pb, 1); /* version */
3023  else
3024  avio_w8(pb, 0); /* version */
3025  avio_wb24(pb, 0); /* flags */
3026  avio_wb32(pb, entries); /* entry count */
3027  for (i = 0; i < entries; i++) {
3028  avio_wb32(pb, ctts_entries[i].count);
3029  avio_wb32(pb, ctts_entries[i].duration);
3030  }
3031  av_free(ctts_entries);
3032  return atom_size;
3033 }
3034 
3035 /* Time to sample atom */
3036 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
3037 {
3038  MOVStts *stts_entries = NULL;
3039  uint32_t entries = -1;
3040  uint32_t atom_size;
3041  int i;
3042 
3043  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
3044  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
3045  if (!stts_entries)
3046  return AVERROR(ENOMEM);
3047  stts_entries[0].count = track->sample_count;
3048  stts_entries[0].duration = 1;
3049  entries = 1;
3050  } else {
3051  if (track->entry) {
3052  stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
3053  if (!stts_entries)
3054  return AVERROR(ENOMEM);
3055  }
3056  for (i = 0; i < track->entry; i++) {
3057  int duration = get_cluster_duration(track, i);
3058  if (i && duration == stts_entries[entries].duration) {
3059  stts_entries[entries].count++; /* compress */
3060  } else {
3061  entries++;
3062  stts_entries[entries].duration = duration;
3063  stts_entries[entries].count = 1;
3064  }
3065  }
3066  entries++; /* last one */
3067  }
3068  atom_size = 16 + (entries * 8);
3069  avio_wb32(pb, atom_size); /* size */
3070  ffio_wfourcc(pb, "stts");
3071  avio_wb32(pb, 0); /* version & flags */
3072  avio_wb32(pb, entries); /* entry count */
3073  for (i = 0; i < entries; i++) {
3074  avio_wb32(pb, stts_entries[i].count);
3075  avio_wb32(pb, stts_entries[i].duration);
3076  }
3077  av_free(stts_entries);
3078  return atom_size;
3079 }
3080 
3082 {
3083  avio_wb32(pb, 28); /* size */
3084  ffio_wfourcc(pb, "dref");
3085  avio_wb32(pb, 0); /* version & flags */
3086  avio_wb32(pb, 1); /* entry count */
3087 
3088  avio_wb32(pb, 0xc); /* size */
3089  //FIXME add the alis and rsrc atom
3090  ffio_wfourcc(pb, "url ");
3091  avio_wb32(pb, 1); /* version & flags */
3092 
3093  return 28;
3094 }
3095 
3097 {
3098  struct sgpd_entry {
3099  int count;
3100  int16_t roll_distance;
3101  int group_description_index;
3102  };
3103 
3104  struct sgpd_entry *sgpd_entries = NULL;
3105  int entries = -1;
3106  int group = 0;
3107  int i, j;
3108 
3109  const int OPUS_SEEK_PREROLL_MS = 80;
3110  int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
3111  (AVRational){1, 1000},
3112  (AVRational){1, 48000});
3113 
3114  if (!track->entry)
3115  return 0;
3116 
3117  sgpd_entries = av_malloc_array(track->entry, sizeof(*sgpd_entries));
3118  if (!sgpd_entries)
3119  return AVERROR(ENOMEM);
3120 
3122 
3123  if (track->par->codec_id == AV_CODEC_ID_OPUS) {
3124  for (i = 0; i < track->entry; i++) {
3125  int roll_samples_remaining = roll_samples;
3126  int distance = 0;
3127  for (j = i - 1; j >= 0; j--) {
3128  roll_samples_remaining -= get_cluster_duration(track, j);
3129  distance++;
3130  if (roll_samples_remaining <= 0)
3131  break;
3132  }
3133  /* We don't have enough preceeding samples to compute a valid
3134  roll_distance here, so this sample can't be independently
3135  decoded. */
3136  if (roll_samples_remaining > 0)
3137  distance = 0;
3138  /* Verify distance is a maximum of 32 (2.5ms) packets. */
3139  if (distance > 32)
3140  return AVERROR_INVALIDDATA;
3141  if (i && distance == sgpd_entries[entries].roll_distance) {
3142  sgpd_entries[entries].count++;
3143  } else {
3144  entries++;
3145  sgpd_entries[entries].count = 1;
3146  sgpd_entries[entries].roll_distance = distance;
3147  sgpd_entries[entries].group_description_index = distance ? ++group : 0;
3148  }
3149  }
3150  } else {
3151  entries++;
3152  sgpd_entries[entries].count = track->sample_count;
3153  sgpd_entries[entries].roll_distance = 1;
3154  sgpd_entries[entries].group_description_index = ++group;
3155  }
3156  entries++;
3157 
3158  if (!group) {
3159  av_free(sgpd_entries);
3160  return 0;
3161  }
3162 
3163  /* Write sgpd tag */
3164  avio_wb32(pb, 24 + (group * 2)); /* size */
3165  ffio_wfourcc(pb, "sgpd");
3166  avio_wb32(pb, 1 << 24); /* fullbox */
3167  ffio_wfourcc(pb, "roll");
3168  avio_wb32(pb, 2); /* default_length */
3169  avio_wb32(pb, group); /* entry_count */
3170  for (i = 0; i < entries; i++) {
3171  if (sgpd_entries[i].group_description_index) {
3172  avio_wb16(pb, -sgpd_entries[i].roll_distance); /* roll_distance */
3173  }
3174  }
3175 
3176  /* Write sbgp tag */
3177  avio_wb32(pb, 20 + (entries * 8)); /* size */
3178  ffio_wfourcc(pb, "sbgp");
3179  avio_wb32(pb, 0); /* fullbox */
3180  ffio_wfourcc(pb, "roll");
3181  avio_wb32(pb, entries); /* entry_count */
3182  for (i = 0; i < entries; i++) {
3183  avio_wb32(pb, sgpd_entries[i].count); /* sample_count */
3184  avio_wb32(pb, sgpd_entries[i].group_description_index); /* group_description_index */
3185  }
3186 
3187  av_free(sgpd_entries);
3188  return 0;
3189 }
3190 
3192 {
3193  int64_t pos = avio_tell(pb);
3194  int ret = 0;
3195 
3196  avio_wb32(pb, 0); /* size */
3197  ffio_wfourcc(pb, "stbl");
3198  if ((ret = mov_write_stsd_tag(s, pb, mov, track)) < 0)
3199  return ret;
3200  mov_write_stts_tag(pb, track);
3201  if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
3202  track->par->codec_id == AV_CODEC_ID_TRUEHD ||
3204  track->par->codec_tag == MKTAG('r','t','p',' ')) &&
3205  track->has_keyframes && track->has_keyframes < track->entry)
3206  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
3207  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && track->has_disposable)
3208  mov_write_sdtp_tag(pb, track);
3209  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
3211  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
3212  track->flags & MOV_TRACK_CTTS && track->entry) {
3213 
3214  if ((ret = mov_write_ctts_tag(s, pb, track)) < 0)
3215  return ret;
3216  }
3217  mov_write_stsc_tag(pb, track);
3218  mov_write_stsz_tag(pb, track);
3219  mov_write_stco_tag(pb, track);
3220  if (track->cenc.aes_ctr) {
3221  ff_mov_cenc_write_stbl_atoms(&track->cenc, pb);
3222  }
3223  if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
3224  mov_preroll_write_stbl_atoms(pb, track);
3225  }
3226  return update_size(pb, pos);
3227 }
3228 
3230 {
3231  int64_t pos = avio_tell(pb);
3232  avio_wb32(pb, 0); /* size */
3233  ffio_wfourcc(pb, "dinf");
3234  mov_write_dref_tag(pb);
3235  return update_size(pb, pos);
3236 }
3237 
3239 {
3240  avio_wb32(pb, 12);
3241  ffio_wfourcc(pb, "nmhd");
3242  avio_wb32(pb, 0);
3243  return 12;
3244 }
3245 
3247 {
3248  avio_wb32(pb, 12);
3249  ffio_wfourcc(pb, "sthd");
3250  avio_wb32(pb, 0);
3251  return 12;
3252 }
3253 
3254 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
3255 {
3256  int64_t pos = avio_tell(pb);
3257  const char *font = "Lucida Grande";
3258  avio_wb32(pb, 0); /* size */
3259  ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
3260  avio_wb32(pb, 0); /* version & flags */
3261  avio_wb16(pb, 0); /* text font */
3262  avio_wb16(pb, 0); /* text face */
3263  avio_wb16(pb, 12); /* text size */
3264  avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
3265  avio_wb16(pb, 0x0000); /* text color (red) */
3266  avio_wb16(pb, 0x0000); /* text color (green) */
3267  avio_wb16(pb, 0x0000); /* text color (blue) */
3268  avio_wb16(pb, 0xffff); /* background color (red) */
3269  avio_wb16(pb, 0xffff); /* background color (green) */
3270  avio_wb16(pb, 0xffff); /* background color (blue) */
3271  avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
3272  avio_write(pb, font, strlen(font)); /* font name */
3273  return update_size(pb, pos);
3274 }
3275 
3276 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
3277 {
3278  int64_t pos = avio_tell(pb);
3279  avio_wb32(pb, 0); /* size */
3280  ffio_wfourcc(pb, "gmhd");
3281  avio_wb32(pb, 0x18); /* gmin size */
3282  ffio_wfourcc(pb, "gmin");/* generic media info */
3283  avio_wb32(pb, 0); /* version & flags */
3284  avio_wb16(pb, 0x40); /* graphics mode = */
3285  avio_wb16(pb, 0x8000); /* opColor (r?) */
3286  avio_wb16(pb, 0x8000); /* opColor (g?) */
3287  avio_wb16(pb, 0x8000); /* opColor (b?) */
3288  avio_wb16(pb, 0); /* balance */
3289  avio_wb16(pb, 0); /* reserved */
3290 
3291  /*
3292  * This special text atom is required for
3293  * Apple Quicktime chapters. The contents
3294  * don't appear to be documented, so the
3295  * bytes are copied verbatim.
3296  */
3297  if (track->tag != MKTAG('c','6','0','8')) {
3298  avio_wb32(pb, 0x2C); /* size */
3299  ffio_wfourcc(pb, "text");
3300  avio_wb16(pb, 0x01);
3301  avio_wb32(pb, 0x00);
3302  avio_wb32(pb, 0x00);
3303  avio_wb32(pb, 0x00);
3304  avio_wb32(pb, 0x01);
3305  avio_wb32(pb, 0x00);
3306  avio_wb32(pb, 0x00);
3307  avio_wb32(pb, 0x00);
3308  avio_wb32(pb, 0x00004000);
3309  avio_wb16(pb, 0x0000);
3310  }
3311 
3312  if (track->par->codec_tag == MKTAG('t','m','c','d')) {
3313  int64_t tmcd_pos = avio_tell(pb);
3314  avio_wb32(pb, 0); /* size */
3315  ffio_wfourcc(pb, "tmcd");
3316  mov_write_tcmi_tag(pb, track);
3317  update_size(pb, tmcd_pos);
3318  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
3319  int64_t gpmd_pos = avio_tell(pb);
3320  avio_wb32(pb, 0); /* size */
3321  ffio_wfourcc(pb, "gpmd");
3322  avio_wb32(pb, 0); /* version */
3323  update_size(pb, gpmd_pos);
3324  }
3325  return update_size(pb, pos);
3326 }
3327 
3329 {
3330  avio_wb32(pb, 16); /* size */
3331  ffio_wfourcc(pb, "smhd");
3332  avio_wb32(pb, 0); /* version & flags */
3333  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
3334  avio_wb16(pb, 0); /* reserved */
3335  return 16;
3336 }
3337 
3339 {
3340  avio_wb32(pb, 0x14); /* size (always 0x14) */
3341  ffio_wfourcc(pb, "vmhd");
3342  avio_wb32(pb, 0x01); /* version & flags */
3343  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
3344  return 0x14;
3345 }
3346 
3347 static int is_clcp_track(MOVTrack *track)
3348 {
3349  return track->tag == MKTAG('c','7','0','8') ||
3350  track->tag == MKTAG('c','6','0','8');
3351 }
3352 
3354 {
3355  MOVMuxContext *mov = s->priv_data;
3356  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
3357  int64_t pos = avio_tell(pb);
3358  size_t descr_len;
3359 
3360  hdlr = "dhlr";
3361  hdlr_type = "url ";
3362  descr = "DataHandler";
3363 
3364  if (track) {
3365  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
3366  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3367  if (track->mode == MODE_AVIF) {
3368  hdlr_type = (track == &mov->tracks[0]) ? "pict" : "auxv";
3369  descr = "PictureHandler";
3370  } else {
3371  hdlr_type = "vide";
3372  descr = "VideoHandler";
3373  }
3374  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3375  hdlr_type = "soun";
3376  descr = "SoundHandler";
3377  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3378  if (is_clcp_track(track)) {
3379  hdlr_type = "clcp";
3380  descr = "ClosedCaptionHandler";
3381  } else {
3382  if (track->tag == MKTAG('t','x','3','g')) {
3383  hdlr_type = "sbtl";
3384  } else if (track->tag == MKTAG('m','p','4','s')) {
3385  hdlr_type = "subp";
3386  } else if (track->tag == MOV_MP4_TTML_TAG) {
3387  hdlr_type = "subt";
3388  } else {
3389  hdlr_type = "text";
3390  }
3391  descr = "SubtitleHandler";
3392  }
3393  } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
3394  hdlr_type = "hint";
3395  descr = "HintHandler";
3396  } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
3397  hdlr_type = "tmcd";
3398  descr = "TimeCodeHandler";
3399  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
3400  hdlr_type = "meta";
3401  descr = "GoPro MET"; // GoPro Metadata
3402  } else {
3404  "Unknown hdlr_type for %s, writing dummy values\n",
3405  av_fourcc2str(track->par->codec_tag));
3406  }
3407  if (track->st) {
3408  // hdlr.name is used by some players to identify the content title
3409  // of the track. So if an alternate handler description is
3410  // specified, use it.
3411  AVDictionaryEntry *t;
3412  t = av_dict_get(track->st->metadata, "handler_name", NULL, 0);
3413  if (t && utf8len(t->value))
3414  descr = t->value;
3415  }
3416  }
3417 
3418  if (mov->empty_hdlr_name) /* expressly allowed by QTFF and not prohibited in ISO 14496-12 8.4.3.3 */
3419  descr = "";
3420 
3421  avio_wb32(pb, 0); /* size */
3422  ffio_wfourcc(pb, "hdlr");
3423  avio_wb32(pb, 0); /* Version & flags */
3424  avio_write(pb, hdlr, 4); /* handler */
3425  ffio_wfourcc(pb, hdlr_type); /* handler type */
3426  avio_wb32(pb, 0); /* reserved */
3427  avio_wb32(pb, 0); /* reserved */
3428  avio_wb32(pb, 0); /* reserved */
3429  descr_len = strlen(descr);
3430  if (!track || track->mode == MODE_MOV)
3431  avio_w8(pb, descr_len); /* pascal string */
3432  avio_write(pb, descr, descr_len); /* handler description */
3433  if (track && track->mode != MODE_MOV)
3434  avio_w8(pb, 0); /* c string */
3435  return update_size(pb, pos);
3436 }
3437 
3438 static int mov_write_pitm_tag(AVIOContext *pb, int item_id)
3439 {
3440  int64_t pos = avio_tell(pb);
3441  avio_wb32(pb, 0); /* size */
3442  ffio_wfourcc(pb, "pitm");
3443  avio_wb32(pb, 0); /* Version & flags */
3444  avio_wb16(pb, item_id); /* item_id */
3445  return update_size(pb, pos);
3446 }
3447 
3449 {
3450  int64_t pos = avio_tell(pb);
3451  avio_wb32(pb, 0); /* size */
3452  ffio_wfourcc(pb, "iloc");
3453  avio_wb32(pb, 0); /* Version & flags */
3454  avio_w8(pb, (4 << 4) + 4); /* offset_size(4) and length_size(4) */
3455  avio_w8(pb, 0); /* base_offset_size(4) and reserved(4) */
3456  avio_wb16(pb, mov->nb_streams); /* item_count */
3457 
3458  for (int i = 0; i < mov->nb_streams; i++) {
3459  avio_wb16(pb, i + 1); /* item_id */
3460  avio_wb16(pb, 0); /* data_reference_index */
3461  avio_wb16(pb, 1); /* extent_count */
3462  mov->avif_extent_pos[i] = avio_tell(pb);
3463  avio_wb32(pb, 0); /* extent_offset (written later) */
3464  // For animated AVIF, we simply write the first packet's size.
3465  avio_wb32(pb, mov->avif_extent_length[i]); /* extent_length */
3466  }
3467 
3468  return update_size(pb, pos);
3469 }
3470 
3472 {
3473  int64_t iinf_pos = avio_tell(pb);
3474  avio_wb32(pb, 0); /* size */
3475  ffio_wfourcc(pb, "iinf");
3476  avio_wb32(pb, 0); /* Version & flags */
3477  avio_wb16(pb, mov->nb_streams); /* entry_count */
3478 
3479  for (int i = 0; i < mov->nb_streams; i++) {
3480  int64_t infe_pos = avio_tell(pb);
3481  avio_wb32(pb, 0); /* size */
3482  ffio_wfourcc(pb, "infe");
3483  avio_w8(pb, 0x2); /* Version */
3484  avio_wb24(pb, 0); /* flags */
3485  avio_wb16(pb, i + 1); /* item_id */
3486  avio_wb16(pb, 0); /* item_protection_index */
3487  avio_write(pb, "av01", 4); /* item_type */
3488  avio_write(pb, !i ? "Color\0" : "Alpha\0", 6); /* item_name */
3489  update_size(pb, infe_pos);
3490  }
3491 
3492  return update_size(pb, iinf_pos);
3493 }
3494 
3495 
3497 {
3498  int64_t auxl_pos;
3499  int64_t iref_pos = avio_tell(pb);
3500  avio_wb32(pb, 0); /* size */
3501  ffio_wfourcc(pb, "iref");
3502  avio_wb32(pb, 0); /* Version & flags */
3503 
3504  auxl_pos = avio_tell(pb);
3505  avio_wb32(pb, 0); /* size */
3506  ffio_wfourcc(pb, "auxl");
3507  avio_wb16(pb, 2); /* from_item_ID */
3508  avio_wb16(pb, 1); /* reference_count */
3509  avio_wb16(pb, 1); /* to_item_ID */
3510  update_size(pb, auxl_pos);
3511 
3512  return update_size(pb, iref_pos);
3513 }
3514 
3516  int stream_index)
3517 {
3518  int64_t pos = avio_tell(pb);
3519  avio_wb32(pb, 0); /* size */
3520  ffio_wfourcc(pb, "ispe");
3521  avio_wb32(pb, 0); /* Version & flags */
3522  avio_wb32(pb, s->streams[stream_index]->codecpar->width); /* image_width */
3523  avio_wb32(pb, s->streams[stream_index]->codecpar->height); /* image_height */
3524  return update_size(pb, pos);
3525 }
3526 
3528  int stream_index)
3529 {
3530  int64_t pos = avio_tell(pb);
3531  const AVPixFmtDescriptor *pixdesc =
3532  av_pix_fmt_desc_get(s->streams[stream_index]->codecpar->format);
3533  avio_wb32(pb, 0); /* size */
3534  ffio_wfourcc(pb, "pixi");
3535  avio_wb32(pb, 0); /* Version & flags */
3536  avio_w8(pb, pixdesc->nb_components); /* num_channels */
3537  for (int i = 0; i < pixdesc->nb_components; ++i) {
3538  avio_w8(pb, pixdesc->comp[i].depth); /* bits_per_channel */
3539  }
3540  return update_size(pb, pos);
3541 }
3542 
3544 {
3545  int64_t pos = avio_tell(pb);
3546  avio_wb32(pb, 0); /* size */
3547  ffio_wfourcc(pb, "ipco");
3548  for (int i = 0; i < mov->nb_streams; i++) {
3549  mov_write_ispe_tag(pb, mov, s, i);
3550  mov_write_pixi_tag(pb, mov, s, i);
3551  mov_write_av1c_tag(pb, &mov->tracks[i]);
3552  if (!i)
3553  mov_write_colr_tag(pb, &mov->tracks[0], 0);
3554  else
3555  mov_write_aux_tag(pb, "auxC");
3556  }
3557  return update_size(pb, pos);
3558 }
3559 
3561 {
3562  int64_t pos = avio_tell(pb);
3563  avio_wb32(pb, 0); /* size */
3564  ffio_wfourcc(pb, "ipma");
3565  avio_wb32(pb, 0); /* Version & flags */
3566  avio_wb32(pb, mov->nb_streams); /* entry_count */
3567 
3568  for (int i = 0, index = 1; i < mov->nb_streams; i++) {
3569  avio_wb16(pb, i + 1); /* item_ID */
3570  avio_w8(pb, 4); /* association_count */
3571 
3572  // ispe association.
3573  avio_w8(pb, index++); /* essential and property_index */
3574  // pixi association.
3575  avio_w8(pb, index++); /* essential and property_index */
3576  // av1C association.
3577  avio_w8(pb, 0x80 | index++); /* essential and property_index */
3578  // colr/auxC association.
3579  avio_w8(pb, index++); /* essential and property_index */
3580  }
3581  return update_size(pb, pos);
3582 }
3583 
3585 {
3586  int64_t pos = avio_tell(pb);
3587  avio_wb32(pb, 0); /* size */
3588  ffio_wfourcc(pb, "iprp");
3589  mov_write_ipco_tag(pb, mov, s);
3590  mov_write_ipma_tag(pb, mov, s);
3591  return update_size(pb, pos);
3592 }
3593 
3595 {
3596  /* This atom must be present, but leaving the values at zero
3597  * seems harmless. */
3598  avio_wb32(pb, 28); /* size */
3599  ffio_wfourcc(pb, "hmhd");
3600  avio_wb32(pb, 0); /* version, flags */
3601  avio_wb16(pb, 0); /* maxPDUsize */
3602  avio_wb16(pb, 0); /* avgPDUsize */
3603  avio_wb32(pb, 0); /* maxbitrate */
3604  avio_wb32(pb, 0); /* avgbitrate */
3605  avio_wb32(pb, 0); /* reserved */
3606  return 28;
3607 }
3608 
3610 {
3611  int64_t pos = avio_tell(pb);
3612  int ret;
3613 
3614  avio_wb32(pb, 0); /* size */
3615  ffio_wfourcc(pb, "minf");
3616  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
3617  mov_write_vmhd_tag(pb);
3618  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
3619  mov_write_smhd_tag(pb);
3620  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3621  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
3622  mov_write_gmhd_tag(pb, track);
3623  } else if (track->tag == MOV_MP4_TTML_TAG) {
3624  mov_write_sthd_tag(pb);
3625  } else {
3626  mov_write_nmhd_tag(pb);
3627  }
3628  } else if (track->tag == MKTAG('r','t','p',' ')) {
3629  mov_write_hmhd_tag(pb);
3630  } else if (track->tag == MKTAG('t','m','c','d')) {
3631  if (track->mode != MODE_MOV)
3632  mov_write_nmhd_tag(pb);
3633  else
3634  mov_write_gmhd_tag(pb, track);
3635  } else if (track->tag == MKTAG('g','p','m','d')) {
3636  mov_write_gmhd_tag(pb, track);
3637  }
3638  if (track->mode == MODE_MOV) /* ISO 14496-12 8.4.3.1 specifies hdlr only within mdia or meta boxes */
3639  mov_write_hdlr_tag(s, pb, NULL);
3640  mov_write_dinf_tag(pb);
3641  if ((ret = mov_write_stbl_tag(s, pb, mov, track)) < 0)
3642  return ret;
3643  return update_size(pb, pos);
3644 }
3645 
3646 static void get_pts_range(MOVMuxContext *mov, MOVTrack *track,
3647  int64_t *start, int64_t *end)
3648 {
3649  if (track->tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd) {
3650  // tmcd tracks gets track_duration set in mov_write_moov_tag from
3651  // another track's duration, while the end_pts may be left at zero.
3652  // Calculate the pts duration for that track instead.
3653  get_pts_range(mov, &mov->tracks[track->src_track], start, end);
3654  *start = av_rescale(*start, track->timescale,
3655  mov->tracks[track->src_track].timescale);
3656  *end = av_rescale(*end, track->timescale,
3657  mov->tracks[track->src_track].timescale);
3658  return;
3659  }
3660  if (track->end_pts != AV_NOPTS_VALUE &&
3661  track->start_dts != AV_NOPTS_VALUE &&
3662  track->start_cts != AV_NOPTS_VALUE) {
3663  *start = track->start_dts + track->start_cts;
3664  *end = track->end_pts;
3665  return;
3666  }
3667  *start = 0;
3668  *end = track->track_duration;
3669 }
3670 
3672 {
3673  int64_t start, end;
3674  get_pts_range(mov, track, &start, &end);
3675  return end - start;
3676 }
3677 
3678 // Calculate the actual duration of the track, after edits.
3679 // If it starts with a pts < 0, that is removed by the edit list.
3680 // If it starts with a pts > 0, the edit list adds a delay before that.
3681 // Thus, with edit lists enabled, the post-edit output of the file is
3682 // starting with pts=0.
3684 {
3685  int64_t start, end;
3686  get_pts_range(mov, track, &start, &end);
3687  if (mov->use_editlist != 0)
3688  start = 0;
3689  return end - start;
3690 }
3691 
3693 {
3694  if (track && track->mode == MODE_ISM)
3695  return 1;
3696  if (duration < INT32_MAX)
3697  return 0;
3698  return 1;
3699 }
3700 
3702  MOVTrack *track)
3703 {
3705  int version = mov_mdhd_mvhd_tkhd_version(mov, track, duration);
3706 
3707  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
3708  ffio_wfourcc(pb, "mdhd");
3709  avio_w8(pb, version);
3710  avio_wb24(pb, 0); /* flags */
3711  if (version == 1) {
3712  avio_wb64(pb, track->time);
3713  avio_wb64(pb, track->time);
3714  } else {
3715  avio_wb32(pb, track->time); /* creation time */
3716  avio_wb32(pb, track->time); /* modification time */
3717  }
3718  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
3719  if (!track->entry && mov->mode == MODE_ISM)
3720  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
3721  else if (!track->entry)
3722  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
3723  else
3724  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration); /* duration */
3725  avio_wb16(pb, track->language); /* language */
3726  avio_wb16(pb, 0); /* reserved (quality) */
3727 
3728  if (version != 0 && track->mode == MODE_MOV) {
3730  "FATAL error, file duration too long for timebase, this file will not be\n"
3731  "playable with QuickTime. Choose a different timebase with "
3732  "-video_track_timescale or a different container format\n");
3733  }
3734 
3735  return 32;
3736 }
3737 
3739  MOVMuxContext *mov, MOVTrack *track)
3740 {
3741  int64_t pos = avio_tell(pb);
3742  int ret;
3743 
3744  avio_wb32(pb, 0); /* size */
3745  ffio_wfourcc(pb, "mdia");
3746  mov_write_mdhd_tag(pb, mov, track);
3747  mov_write_hdlr_tag(s, pb, track);
3748  if ((ret = mov_write_minf_tag(s, pb, mov, track)) < 0)
3749  return ret;
3750  return update_size(pb, pos);
3751 }
3752 
3753 /* transformation matrix
3754  |a b u|
3755  |c d v|
3756  |tx ty w| */
3757 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
3758  int16_t d, int16_t tx, int16_t ty)
3759 {
3760  avio_wb32(pb, a << 16); /* 16.16 format */
3761  avio_wb32(pb, b << 16); /* 16.16 format */
3762  avio_wb32(pb, 0); /* u in 2.30 format */
3763  avio_wb32(pb, c << 16); /* 16.16 format */
3764  avio_wb32(pb, d << 16); /* 16.16 format */
3765  avio_wb32(pb, 0); /* v in 2.30 format */
3766  avio_wb32(pb, tx << 16); /* 16.16 format */
3767  avio_wb32(pb, ty << 16); /* 16.16 format */
3768  avio_wb32(pb, 1 << 30); /* w in 2.30 format */
3769 }
3770 
3772  MOVTrack *track, AVStream *st)
3773 {
3775  mov->movie_timescale, track->timescale,
3776  AV_ROUND_UP);
3777  int version;
3779  int group = 0;
3780 
3781  uint32_t *display_matrix = NULL;
3782  int i;
3783 
3784  if (mov->mode == MODE_AVIF)
3785  if (!mov->avif_loop_count)
3786  duration = INT64_MAX;
3787  else
3788  duration *= mov->avif_loop_count;
3789 
3790  if (st) {
3791  const AVPacketSideData *sd;
3792  if (mov->per_stream_grouping)
3793  group = st->index;
3794  else
3795  group = st->codecpar->codec_type;
3796 
3800  if (sd && sd->size == 9 * sizeof(*display_matrix))
3801  display_matrix = (uint32_t *)sd->data;
3802  }
3803 
3804  if (track->flags & MOV_TRACK_ENABLED)
3806 
3808 
3809  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
3810  ffio_wfourcc(pb, "tkhd");
3811  avio_w8(pb, version);
3812  avio_wb24(pb, flags);
3813  if (version == 1) {
3814  avio_wb64(pb, track->time);
3815  avio_wb64(pb, track->time);
3816  } else {
3817  avio_wb32(pb, track->time); /* creation time */
3818  avio_wb32(pb, track->time); /* modification time */
3819  }
3820  avio_wb32(pb, track->track_id); /* track-id */
3821  avio_wb32(pb, 0); /* reserved */
3822  if (!track->entry && mov->mode == MODE_ISM)
3823  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
3824  else if (!track->entry)
3825  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
3826  else
3827  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
3828 
3829  avio_wb32(pb, 0); /* reserved */
3830  avio_wb32(pb, 0); /* reserved */
3831  avio_wb16(pb, 0); /* layer */
3832  avio_wb16(pb, group); /* alternate group) */
3833  /* Volume, only for audio */
3834  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
3835  avio_wb16(pb, 0x0100);
3836  else
3837  avio_wb16(pb, 0);
3838  avio_wb16(pb, 0); /* reserved */
3839 
3840  /* Matrix structure */
3841  if (display_matrix) {
3842  for (i = 0; i < 9; i++)
3843  avio_wb32(pb, display_matrix[i]);
3844  } else {
3845  write_matrix(pb, 1, 0, 0, 1, 0, 0);
3846  }
3847  /* Track width and height, for visual only */
3848  if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
3849  track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
3850  int64_t track_width_1616;
3851  if (track->mode == MODE_MOV || track->mode == MODE_AVIF) {
3852  track_width_1616 = track->par->width * 0x10000ULL;
3853  } else {
3854  track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
3855  track->par->width * 0x10000LL,
3856  st->sample_aspect_ratio.den);
3857  if (!track_width_1616 ||
3858  track->height != track->par->height ||
3859  track_width_1616 > UINT32_MAX)
3860  track_width_1616 = track->par->width * 0x10000ULL;
3861  }
3862  if (track_width_1616 > UINT32_MAX) {
3863  av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");
3864  track_width_1616 = 0;
3865  }
3866  avio_wb32(pb, track_width_1616);
3867  if (track->height > 0xFFFF) {
3868  av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");
3869  avio_wb32(pb, 0);
3870  } else
3871  avio_wb32(pb, track->height * 0x10000U);
3872  } else {
3873  avio_wb32(pb, 0);
3874  avio_wb32(pb, 0);
3875  }
3876  return 0x5c;
3877 }
3878 
3879 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
3880 {
3882  track->par->sample_aspect_ratio.den);
3883 
3884  int64_t pos = avio_tell(pb);
3885 
3886  avio_wb32(pb, 0); /* size */
3887  ffio_wfourcc(pb, "tapt");
3888 
3889  avio_wb32(pb, 20);
3890  ffio_wfourcc(pb, "clef");
3891  avio_wb32(pb, 0);
3892  avio_wb32(pb, width << 16);
3893  avio_wb32(pb, track->par->height << 16);
3894 
3895  avio_wb32(pb, 20);
3896  ffio_wfourcc(pb, "prof");
3897  avio_wb32(pb, 0);
3898  avio_wb32(pb, width << 16);
3899  avio_wb32(pb, track->par->height << 16);
3900 
3901  avio_wb32(pb, 20);
3902  ffio_wfourcc(pb, "enof");
3903  avio_wb32(pb, 0);
3904  avio_wb32(pb, track->par->width << 16);
3905  avio_wb32(pb, track->par->height << 16);
3906 
3907  return update_size(pb, pos);
3908 }
3909 
3910 // This box is written in the following cases:
3911 // * Seems important for the psp playback. Without it the movie seems to hang.
3912 // * Used for specifying the looping behavior of animated AVIF (as specified
3913 // in Section 9.6 of the HEIF specification ISO/IEC 23008-12).
3915  MOVTrack *track)
3916 {
3918  mov->movie_timescale, track->timescale,
3919  AV_ROUND_UP);
3920  int version = duration < INT32_MAX ? 0 : 1;
3921  int entry_size, entry_count, size;
3922  int64_t delay, start_ct = track->start_cts;
3923  int64_t start_dts = track->start_dts;
3924  int flags = 0;
3925 
3926  if (track->entry) {
3927  if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
3928 
3929  av_log(mov->fc, AV_LOG_DEBUG,
3930  "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
3931  track->cluster[0].dts, track->cluster[0].cts,
3932  start_dts, start_ct, track->track_id);
3933  start_dts = track->cluster[0].dts;
3934  start_ct = track->cluster[0].cts;
3935  }
3936  }
3937 
3938  delay = av_rescale_rnd(start_dts + start_ct, mov->movie_timescale,
3939  track->timescale, AV_ROUND_DOWN);
3940 
3941  if (mov->mode == MODE_AVIF) {
3942  delay = 0;
3943  // Section 9.6.3 of ISO/IEC 23008-12: flags specifies repetition of the
3944  // edit list as follows: (flags & 1) equal to 0 specifies that the edit
3945  // list is not repeated, while (flags & 1) equal to 1 specifies that the
3946  // edit list is repeated.
3947  flags = mov->avif_loop_count != 1;
3948  start_ct = 0;
3949  }
3950 
3951  version |= delay < INT32_MAX ? 0 : 1;
3952 
3953  entry_size = (version == 1) ? 20 : 12;
3954  entry_count = 1 + (delay > 0);
3955  size = 24 + entry_count * entry_size;
3956 
3957  /* write the atom data */
3958  avio_wb32(pb, size);
3959  ffio_wfourcc(pb, "edts");
3960  avio_wb32(pb, size - 8);
3961  ffio_wfourcc(pb, "elst");
3962  avio_w8(pb, version);
3963  avio_wb24(pb, flags); /* flags */
3964 
3965  avio_wb32(pb, entry_count);
3966  if (delay > 0) { /* add an empty edit to delay presentation */
3967  /* In the positive delay case, the delay includes the cts
3968  * offset, and the second edit list entry below trims out
3969  * the same amount from the actual content. This makes sure
3970  * that the offset last sample is included in the edit
3971  * list duration as well. */
3972  if (version == 1) {
3973  avio_wb64(pb, delay);
3974  avio_wb64(pb, -1);
3975  } else {
3976  avio_wb32(pb, delay);
3977  avio_wb32(pb, -1);
3978  }
3979  avio_wb32(pb, 0x00010000);
3980  } else if (mov->mode != MODE_AVIF) {
3981  /* Avoid accidentally ending up with start_ct = -1 which has got a
3982  * special meaning. Normally start_ct should end up positive or zero
3983  * here, but use FFMIN in case dts is a small positive integer
3984  * rounded to 0 when represented in movie timescale units. */
3985  av_assert0(av_rescale_rnd(start_dts, mov->movie_timescale, track->timescale, AV_ROUND_DOWN) <= 0);
3986  start_ct = -FFMIN(start_dts, 0);
3987  /* Note, this delay is calculated from the pts of the first sample,
3988  * ensuring that we don't reduce the duration for cases with
3989  * dts<0 pts=0. */
3990  duration += delay;
3991  }
3992 
3993  /* For fragmented files, we don't know the full length yet. Setting
3994  * duration to 0 allows us to only specify the offset, including
3995  * the rest of the content (from all future fragments) without specifying
3996  * an explicit duration. */
3997  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3998  duration = 0;
3999 
4000  /* duration */
4001  if (version == 1) {
4002  avio_wb64(pb, duration);
4003  avio_wb64(pb, start_ct);
4004  } else {
4005  avio_wb32(pb, duration);
4006  avio_wb32(pb, start_ct);
4007  }
4008  avio_wb32(pb, 0x00010000);
4009  return size;
4010 }
4011 
4012 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
4013 {
4014  avio_wb32(pb, 20); // size
4015  ffio_wfourcc(pb, "tref");
4016  avio_wb32(pb, 12); // size (subatom)
4017  avio_wl32(pb, track->tref_tag);
4018  avio_wb32(pb, track->tref_id);
4019  return 20;
4020 }
4021 
4022 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
4024 {
4025  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
4026  ffio_wfourcc(pb, "uuid");
4027  ffio_wfourcc(pb, "USMT");
4028  avio_wb32(pb, 0x21d24fce);
4029  avio_wb32(pb, 0xbb88695c);
4030  avio_wb32(pb, 0xfac9c740);
4031  avio_wb32(pb, 0x1c); // another size here!
4032  ffio_wfourcc(pb, "MTDT");
4033  avio_wb32(pb, 0x00010012);
4034  avio_wb32(pb, 0x0a);
4035  avio_wb32(pb, 0x55c40000);
4036  avio_wb32(pb, 0x1);
4037  avio_wb32(pb, 0x0);
4038  return 0x34;
4039 }
4040 
4041 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
4042 {
4043  AVFormatContext *ctx = track->rtp_ctx;
4044  char buf[1000] = "";
4045  int len;
4046 
4047  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
4048  NULL, NULL, 0, 0, ctx);
4049  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
4050  len = strlen(buf);
4051 
4052  avio_wb32(pb, len + 24);
4053  ffio_wfourcc(pb, "udta");
4054  avio_wb32(pb, len + 16);
4055  ffio_wfourcc(pb, "hnti");
4056  avio_wb32(pb, len + 8);
4057  ffio_wfourcc(pb, "sdp ");
4058  avio_write(pb, buf, len);
4059  return len + 24;
4060 }
4061 
4063  const char *tag, const char *str)
4064 {
4065  int64_t pos = avio_tell(pb);
4066  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
4067  if (!t || !utf8len(t->value))
4068  return 0;
4069 
4070  avio_wb32(pb, 0); /* size */
4071  ffio_wfourcc(pb, tag); /* type */
4072  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
4073  return update_size(pb, pos);
4074 }
4075 
4076 static int mov_write_track_kind(AVIOContext *pb, const char *scheme_uri,
4077  const char *value)
4078 {
4079  int64_t pos = avio_tell(pb);
4080 
4081  /* Box|FullBox basics */
4082  avio_wb32(pb, 0); /* size placeholder */
4083  ffio_wfourcc(pb, (const unsigned char *)"kind");
4084  avio_w8(pb, 0); /* version = 0 */
4085  avio_wb24(pb, 0); /* flags = 0 */
4086 
4087  /* Required null-terminated scheme URI */
4088  avio_write(pb, (const unsigned char *)scheme_uri,
4089  strlen(scheme_uri));
4090  avio_w8(pb, 0);
4091 
4092  /* Optional value string */
4093  if (value && value[0])
4094  avio_write(pb, (const unsigned char *)value,
4095  strlen(value));
4096 
4097  avio_w8(pb, 0);
4098 
4099  return update_size(pb, pos);
4100 }
4101 
4103 {
4104  int ret = AVERROR_BUG;
4105 
4106  for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
4108 
4109  for (int j = 0; map.value_maps[j].disposition; j++) {
4110  const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
4111  if (!(st->disposition & value_map.disposition))
4112  continue;
4113 
4114  if ((ret = mov_write_track_kind(pb, map.scheme_uri, value_map.value)) < 0)
4115  return ret;
4116  }
4117  }
4118 
4119  return 0;
4120 }
4121 
4123  AVStream *st)
4124 {
4125  AVIOContext *pb_buf;
4126  int ret, size;
4127  uint8_t *buf;
4128 
4129  if (!st)
4130  return 0;
4131 
4132  ret = avio_open_dyn_buf(&pb_buf);
4133  if (ret < 0)
4134  return ret;
4135 
4136  if (mov->mode & (MODE_MP4|MODE_MOV))
4137  mov_write_track_metadata(pb_buf, st, "name", "title");
4138 
4139  if (mov->mode & MODE_MP4) {
4140  if ((ret = mov_write_track_kinds(pb_buf, st)) < 0)
4141  return ret;
4142  }
4143 
4144  if ((size = avio_get_dyn_buf(pb_buf, &buf)) > 0) {
4145  avio_wb32(pb, size + 8);
4146  ffio_wfourcc(pb, "udta");
4147  avio_write(pb, buf, size);
4148  }
4149  ffio_free_dyn_buf(&pb_buf);
4150 
4151  return 0;
4152 }
4153 
4155  MOVTrack *track, AVStream *st)
4156 {
4157  int64_t pos = avio_tell(pb);
4158  int entry_backup = track->entry;
4159  int chunk_backup = track->chunkCount;
4160  int ret;
4161 
4162  /* If we want to have an empty moov, but some samples already have been
4163  * buffered (delay_moov), pretend that no samples have been written yet. */
4164  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
4165  track->chunkCount = track->entry = 0;