FFmpeg
Loading...
Searching...
No Matches
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 <math.h>
27#include <stdint.h>
28#include <inttypes.h>
29
30#include "movenc.h"
31#include "avformat.h"
32#include "avio_internal.h"
33#include "dovi_isom.h"
34#include "riff.h"
35#include "avio.h"
36#include "iamf_writer.h"
37#include "isom.h"
38#include "av1.h"
39#include "avc.h"
40#include "evc.h"
41#include "apv.h"
42#include "lcevc.h"
45#include "libavcodec/flac.h"
47#include "libavcodec/get_bits.h"
48
49#include "libavcodec/internal.h"
50#include "libavcodec/put_bits.h"
52#include "libavcodec/raw.h"
53#include "internal.h"
54#include "libavutil/avstring.h"
56#include "libavutil/csp.h"
57#include "libavutil/intfloat.h"
59#include "libavutil/mem.h"
60#include "libavutil/opt.h"
61#include "libavutil/dict.h"
62#include "libavutil/pixdesc.h"
63#include "libavutil/stereo3d.h"
64#include "libavutil/timecode.h"
65#include "libavutil/dovi_meta.h"
66#include "libavutil/uuid.h"
67#include "hevc.h"
68#include "rtpenc.h"
69#include "nal.h"
70#include "mov_chan.h"
71#include "movenc_ttml.h"
72#include "mux.h"
73#include "rawutils.h"
74#include "ttmlenc.h"
75#include "version.h"
76#include "vpcc.h"
77#include "vvc.h"
78
79static const AVOption options[] = {
80 { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
81 { "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},
82 { "encryption_key", "The media encryption key (hex)", offsetof(MOVMuxContext, encryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
83 { "encryption_kid", "The media encryption key identifier (hex)", offsetof(MOVMuxContext, encryption_kid), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
84 { "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 },
85 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
86 { "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 },
87 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
88 { "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},
89 { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
90 { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
91 { "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},
92 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" },
93 { "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" },
94 { "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" },
95 { "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" },
96 { "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" },
97 { "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" },
98 { "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" },
99 { "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" },
100 { "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" },
101 { "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" },
102 { "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" },
103 { "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" },
104 { "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" },
105 { "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" },
106 { "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 },
107 { "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" },
108 { "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" },
109 { "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" },
110 { "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" },
111 { "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" },
112 { "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" },
113 { "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" },
114 { "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" },
115 { "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" },
116 { "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" },
117 { "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" },
118 { "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},
119 { "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},
120 { "movie_timescale", "set movie timescale", offsetof(MOVMuxContext, movie_timescale), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
122 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
123 { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
124 { "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},
125 { "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},
126 { "write_btrt", "force or disable writing btrt", offsetof(MOVMuxContext, write_btrt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
127 { "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"},
128 { "pts", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_PTS}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, .unit = "prft"},
129 { "wallclock", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_WALLCLOCK}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, .unit = "prft"},
130 { "write_tmcd", "force or disable writing tmcd", offsetof(MOVMuxContext, write_tmcd), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
131 { NULL },
132};
133
135 .class_name = "mov/mp4/tgp/psp/tg2/ipod/ismv/f4v muxer",
136 .item_name = av_default_item_name,
137 .option = options,
138 .version = LIBAVUTIL_VERSION_INT,
139};
140
141static int get_moov_size(AVFormatContext *s);
143
144static int utf8len(const uint8_t *b)
145{
146 int len = 0;
147 int val;
148 while (*b) {
149 GET_UTF8(val, *b++, return -1;)
150 len++;
151 }
152 return len;
153}
154
155//FIXME support 64 bit variant with wide placeholders
157{
158 int64_t curpos = avio_tell(pb);
159 avio_seek(pb, pos, SEEK_SET);
160 avio_wb32(pb, curpos - pos); /* rewrite size */
161 avio_seek(pb, curpos, SEEK_SET);
162
163 return curpos - pos;
164}
165
167{
168 int64_t curpos = avio_tell(pb);
169 avio_seek(pb, pos, SEEK_SET);
170 avio_wb32(pb, curpos - pos); /* rewrite size */
171 avio_skip(pb, 4);
172 avio_w8(pb, version); /* rewrite version */
173 avio_seek(pb, curpos, SEEK_SET);
174
175 return curpos - pos;
176}
177
178static int co64_required(const MOVTrack *track)
179{
180 if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
181 return 1;
182 return 0;
183}
184
185static int is_cover_image(const AVStream *st)
186{
187 /* Eg. AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS
188 * is encoded as sparse video track */
189 return st && st->disposition == AV_DISPOSITION_ATTACHED_PIC;
190}
191
192static int rtp_hinting_needed(const AVStream *st)
193{
194 /* Add hint tracks for each real audio and video stream */
195 if (is_cover_image(st))
196 return 0;
197 return st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
199}
200
201/* Chunk offset atom */
203{
204 int i;
205 int mode64 = co64_required(track); // use 32 bit size variant if possible
206 int64_t pos = avio_tell(pb);
207 avio_wb32(pb, 0); /* size */
208 if (mode64)
209 ffio_wfourcc(pb, "co64");
210 else
211 ffio_wfourcc(pb, "stco");
212 avio_wb32(pb, 0); /* version & flags */
213 avio_wb32(pb, track->chunkCount); /* entry count */
214 for (i = 0; i < track->entry; i++) {
215 if (!track->cluster[i].chunkNum)
216 continue;
217 if (mode64 == 1)
218 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
219 else
220 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
221 }
222 return update_size(pb, pos);
223}
224
225/* Sample size atom */
227{
228 int equalChunks = 1;
229 int i, j, entries = 0, tst = -1, oldtst = -1;
230
231 int64_t pos = avio_tell(pb);
232 avio_wb32(pb, 0); /* size */
233 ffio_wfourcc(pb, "stsz");
234 avio_wb32(pb, 0); /* version & flags */
235
236 for (i = 0; i < track->entry; i++) {
237 tst = track->cluster[i].size / track->cluster[i].entries;
238 if (oldtst != -1 && tst != oldtst)
239 equalChunks = 0;
240 oldtst = tst;
241 entries += track->cluster[i].entries;
242 }
243 if (equalChunks && track->entry) {
244 int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
245 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
246 avio_wb32(pb, sSize); // sample size
247 avio_wb32(pb, entries); // sample count
248 } else {
249 avio_wb32(pb, 0); // sample size
250 avio_wb32(pb, entries); // sample count
251 for (i = 0; i < track->entry; i++) {
252 for (j = 0; j < track->cluster[i].entries; j++) {
253 avio_wb32(pb, track->cluster[i].size /
254 track->cluster[i].entries);
255 }
256 }
257 }
258 return update_size(pb, pos);
259}
260
261/* Sample to chunk atom */
263{
264 int index = 0, oldidx = -1, oldval = -1, i;
265 int64_t entryPos, curpos;
266
267 int64_t pos = avio_tell(pb);
268 avio_wb32(pb, 0); /* size */
269 ffio_wfourcc(pb, "stsc");
270 avio_wb32(pb, 0); // version & flags
271 entryPos = avio_tell(pb);
272 avio_wb32(pb, track->chunkCount); // entry count
273 for (i = 0; i < track->entry; i++) {
274 if ((oldval != track->cluster[i].samples_in_chunk ||
275 oldidx != track->cluster[i].stsd_index) && track->cluster[i].chunkNum) {
276 avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
277 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
278 avio_wb32(pb, track->cluster[i].stsd_index + 1); // sample description index
279 oldval = track->cluster[i].samples_in_chunk;
280 oldidx = track->cluster[i].stsd_index;
281 index++;
282 }
283 }
284 curpos = avio_tell(pb);
285 avio_seek(pb, entryPos, SEEK_SET);
286 avio_wb32(pb, index); // rewrite size
287 avio_seek(pb, curpos, SEEK_SET);
288
289 return update_size(pb, pos);
290}
291
292/* Sync sample atom */
293static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
294{
295 int64_t curpos, entryPos;
296 int i, index = 0;
297 int64_t pos = avio_tell(pb);
298 avio_wb32(pb, 0); // size
299 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
300 avio_wb32(pb, 0); // version & flags
301 entryPos = avio_tell(pb);
302 avio_wb32(pb, track->entry); // entry count
303 for (i = 0; i < track->entry; i++) {
304 if (track->cluster[i].flags & flag) {
305 avio_wb32(pb, i + 1);
306 index++;
307 }
308 }
309 curpos = avio_tell(pb);
310 avio_seek(pb, entryPos, SEEK_SET);
311 avio_wb32(pb, index); // rewrite size
312 avio_seek(pb, curpos, SEEK_SET);
313 return update_size(pb, pos);
314}
315
316/* Sample dependency atom */
318{
319 int i;
320 uint8_t leading, dependent, reference, redundancy;
321 int64_t pos = avio_tell(pb);
322 avio_wb32(pb, 0); // size
323 ffio_wfourcc(pb, "sdtp");
324 avio_wb32(pb, 0); // version & flags
325 for (i = 0; i < track->entry; i++) {
326 dependent = MOV_SAMPLE_DEPENDENCY_YES;
327 leading = reference = redundancy = MOV_SAMPLE_DEPENDENCY_UNKNOWN;
328 if (track->cluster[i].flags & MOV_DISPOSABLE_SAMPLE) {
329 reference = MOV_SAMPLE_DEPENDENCY_NO;
330 }
331 if (track->cluster[i].flags & MOV_SYNC_SAMPLE) {
332 dependent = MOV_SAMPLE_DEPENDENCY_NO;
333 }
334 avio_w8(pb, (leading << 6) | (dependent << 4) |
335 (reference << 2) | redundancy);
336 }
337 return update_size(pb, pos);
338}
339
340#if CONFIG_IAMFENC
341static int mov_write_iacb_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
342{
343 AVIOContext *dyn_bc;
344 int64_t pos = avio_tell(pb);
345 uint8_t *dyn_buf = NULL;
346 int dyn_size;
347 int ret = avio_open_dyn_buf(&dyn_bc);
348 if (ret < 0)
349 return ret;
350
351 avio_wb32(pb, 0);
352 ffio_wfourcc(pb, "iacb");
353 avio_w8(pb, 1); // configurationVersion
354
355 ret = ff_iamf_write_descriptors(track->iamf, dyn_bc, s);
356 if (ret < 0) {
357 ffio_free_dyn_buf(&dyn_bc);
358 return ret;
359 }
360
361 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
362 ffio_write_leb(pb, dyn_size);
363 avio_write(pb, dyn_buf, dyn_size);
364 av_free(dyn_buf);
365
366 return update_size(pb, pos);
367}
368#endif
369
371{
372 avio_wb32(pb, 0x11); /* size */
373 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
374 else ffio_wfourcc(pb, "damr");
375 ffio_wfourcc(pb, "FFMP");
376 avio_w8(pb, 0); /* decoder version */
377
378 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
379 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
380 avio_w8(pb, 0x01); /* Frames per sample */
381 return 0x11;
382}
383
384struct eac3_info {
386 uint8_t eof;
387 uint8_t ec3_done;
388 uint8_t num_blocks;
389
390 /* Layout of the EC3SpecificBox */
391 /* maximum bitrate */
392 uint16_t data_rate;
394 /* number of independent substreams */
395 uint8_t num_ind_sub;
396 struct {
397 /* sample rate code (see ff_ac3_sample_rate_tab) 2 bits */
398 uint8_t fscod;
399 /* bit stream identification 5 bits */
400 uint8_t bsid;
401 /* one bit reserved */
402 /* audio service mixing (not supported yet) 1 bit */
403 /* bit stream mode 3 bits */
404 uint8_t bsmod;
405 /* audio coding mode 3 bits */
406 uint8_t acmod;
407 /* sub woofer on 1 bit */
408 uint8_t lfeon;
409 /* 3 bits reserved */
410 /* number of dependent substreams associated with this substream 4 bits */
411 uint8_t num_dep_sub;
412 /* channel locations of the dependent substream(s), if any, 9 bits */
413 uint16_t chan_loc;
414 /* if there is no dependent substream, then one bit reserved instead */
415 } substream[1]; /* TODO: support 8 independent substreams */
416 /* indicates the decoding complexity, 8 bits */
418};
419
421{
422 struct eac3_info *info = track->eac3_priv;
423 PutBitContext pbc;
424 uint8_t buf[3];
425
426 if (!info || !info->ec3_done) {
428 "Cannot write moov atom before AC3 packets."
429 " Set the delay_moov flag to fix this.\n");
430 return AVERROR(EINVAL);
431 }
432
433 if (info->substream[0].bsid > 8) {
435 "RealAudio AC-3/DolbyNet with bsid %d is not defined by the "
436 "ISOBMFF specification in ETSI TS 102 366!\n",
437 info->substream[0].bsid);
438 return AVERROR(EINVAL);
439 }
440
441 if (info->ac3_bit_rate_code < 0) {
443 "No valid AC3 bit rate code for data rate of %d!\n",
444 info->data_rate);
445 return AVERROR(EINVAL);
446 }
447
448 avio_wb32(pb, 11);
449 ffio_wfourcc(pb, "dac3");
450
451 init_put_bits(&pbc, buf, sizeof(buf));
452 put_bits(&pbc, 2, info->substream[0].fscod);
453 put_bits(&pbc, 5, info->substream[0].bsid);
454 put_bits(&pbc, 3, info->substream[0].bsmod);
455 put_bits(&pbc, 3, info->substream[0].acmod);
456 put_bits(&pbc, 1, info->substream[0].lfeon);
457 put_bits(&pbc, 5, info->ac3_bit_rate_code); // bit_rate_code
458 put_bits(&pbc, 5, 0); // reserved
459
460 flush_put_bits(&pbc);
461 avio_write(pb, buf, sizeof(buf));
462
463 return 11;
464}
465
467{
468 AC3HeaderInfo *hdr = NULL;
469 struct eac3_info *info;
470 int num_blocks, ret;
471
472 if (!track->eac3_priv) {
473 if (!(track->eac3_priv = av_mallocz(sizeof(*info))))
474 return AVERROR(ENOMEM);
475
476 ((struct eac3_info *)track->eac3_priv)->ac3_bit_rate_code = -1;
477 }
478 info = track->eac3_priv;
479
480 if (!info->pkt && !(info->pkt = av_packet_alloc()))
481 return AVERROR(ENOMEM);
482
483 if (info->eof) {
484 av_assert1(!info->pkt->size);
485 ret = pkt->size;
486 goto end;
487 }
488
489 if ((ret = avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size)) < 0) {
490 if (ret == AVERROR(ENOMEM))
491 goto end;
492
493 /* drop the packets until we see a good one */
494 if (!track->entry) {
495 av_log(mov->fc, AV_LOG_WARNING, "Dropping invalid packet from start of the stream\n");
496 ret = 0;
497 } else
499 goto end;
500 }
501
502 info->data_rate = FFMAX(info->data_rate, hdr->bit_rate / 1000);
504 hdr->ac3_bit_rate_code);
506
507 num_blocks = hdr->num_blocks;
508
509 if (!info->ec3_done) {
510 /* AC-3 substream must be the first one */
511 if (hdr->bitstream_id <= 10 && hdr->substreamid != 0) {
512 ret = AVERROR(EINVAL);
513 goto end;
514 }
515
516 /* this should always be the case, given that our AC-3 parser
517 * concatenates dependent frames to their independent parent */
520 /* substream ids must be incremental */
521 if (hdr->substreamid > info->num_ind_sub + 1) {
522 ret = AVERROR(EINVAL);
523 goto end;
524 }
525
526 if (hdr->substreamid == info->num_ind_sub + 1) {
527 //info->num_ind_sub++;
528 avpriv_request_sample(mov->fc, "Multiple independent substreams");
530 goto end;
531 } else if (hdr->substreamid < info->num_ind_sub ||
532 hdr->substreamid == 0 && info->substream[0].bsid) {
533 info->ec3_done = 1;
534 goto concatenate;
535 }
536 } else {
537 if (hdr->substreamid != 0) {
538 avpriv_request_sample(mov->fc, "Multiple non EAC3 independent substreams");
540 goto end;
541 }
542 }
543
544 /* fill the info needed for the "dec3" atom */
545 info->substream[hdr->substreamid].fscod = hdr->sr_code;
546 info->substream[hdr->substreamid].bsid = hdr->bitstream_id;
547 info->substream[hdr->substreamid].bsmod = hdr->bitstream_mode;
548 info->substream[hdr->substreamid].acmod = hdr->channel_mode;
549 info->substream[hdr->substreamid].lfeon = hdr->lfe_on;
550
551 if (track->par->codec_id == AV_CODEC_ID_AC3) {
552 // with AC-3 we only require the information of a single packet,
553 // so we can finish as soon as the basic values of the bit stream
554 // have been set to the track's informational structure.
555 info->ec3_done = 1;
556 goto concatenate;
557 }
558
559 /* Parse dependent substream(s), if any */
560 if (pkt->size != hdr->frame_size) {
561 int cumul_size = hdr->frame_size;
562 int parent = hdr->substreamid;
563
564 while (cumul_size != pkt->size) {
565 ret = avpriv_ac3_parse_header(&hdr, pkt->data + cumul_size, pkt->size - cumul_size);
566 if (ret < 0)
567 goto end;
569 ret = AVERROR(EINVAL);
570 goto end;
571 }
572 info->substream[parent].num_dep_sub++;
573 ret /= 8;
574
575 /* get the dependent stream channel map, if exists */
576 if (hdr->channel_map_present) {
577 /* chanmap is a 16-bit field read MSB-first, so flag index
578 * i sits at bit (15 - i), matching the indexing of
579 * ff_eac3_custom_channel_map_locations. chan_loc bits 0-7
580 * carry flag indices 5-12, i.e. chanmap bit (10 - j).
581 * The mapping is not contiguous at the top: index 13
582 * (Lts/Rts) has no chan_loc bit and is skipped, and
583 * chan_loc bit 8 carries LFE2, which is index 14 and so
584 * chanmap bit 1. */
585 for (int j = 0; j < 8; j++) {
586 if ((hdr->channel_map >> (10 - j)) & 1)
587 info->substream[parent].chan_loc |= 1 << j;
588 }
589 if ((hdr->channel_map >> 1) & 1)
590 info->substream[parent].chan_loc |= 1 << 8;
591 } else {
592 info->substream[parent].chan_loc |= hdr->channel_mode;
593 }
594 cumul_size += hdr->frame_size;
595 }
596 }
597 }
598
599concatenate:
600 if (!info->num_blocks && num_blocks == 6) {
601 ret = pkt->size;
602 goto end;
603 }
604 else if (info->num_blocks + num_blocks > 6) {
606 goto end;
607 }
608
609 if (!info->num_blocks) {
610 ret = av_packet_ref(info->pkt, pkt);
611 if (!ret)
612 info->num_blocks = num_blocks;
613 goto end;
614 } else {
615 const AVPacketSideData *sd;
616 int64_t duration = pkt->duration;
617 if ((ret = av_grow_packet(info->pkt, pkt->size)) < 0)
618 goto end;
619 sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems, AV_PKT_DATA_SKIP_SAMPLES);
620 if (sd && sd->size >= 10) {
621 uint8_t *buf = av_packet_new_side_data(info->pkt, AV_PKT_DATA_SKIP_SAMPLES, sd->size);
622 if (buf)
623 memcpy(buf, sd->data, sd->size);
624 if (track->par->frame_size)
625 duration = FFMAX(av_rescale_q(track->par->frame_size, (AVRational){ 1, track->par->sample_rate },
626 track->st->time_base), duration);
627 }
628 memcpy(info->pkt->data + info->pkt->size - pkt->size, pkt->data, pkt->size);
629 info->num_blocks += num_blocks;
630 info->pkt->duration += duration;
631 if (info->num_blocks != 6)
632 goto end;
634 av_packet_move_ref(pkt, info->pkt);
635 info->num_blocks = 0;
636 }
637 ret = pkt->size;
638
639end:
640 av_free(hdr);
641
642 return ret;
643}
644
646{
647 PutBitContext pbc;
648 uint8_t *buf;
649 struct eac3_info *info;
650 int size, i;
651
652 if (!track->eac3_priv) {
654 "Cannot write moov atom before EAC3 packets parsed.\n");
655 return AVERROR(EINVAL);
656 }
657
658 info = track->eac3_priv;
659 size = 2 + (4 * (info->num_ind_sub + 1)) + (2 * !!info->complexity_index_type_a);
660 buf = av_malloc(size);
661 if (!buf) {
662 return AVERROR(ENOMEM);
663 }
664
665 init_put_bits(&pbc, buf, size);
666 put_bits(&pbc, 13, info->data_rate);
667 put_bits(&pbc, 3, info->num_ind_sub);
668 for (i = 0; i <= info->num_ind_sub; i++) {
669 put_bits(&pbc, 2, info->substream[i].fscod);
670 put_bits(&pbc, 5, info->substream[i].bsid);
671 put_bits(&pbc, 1, 0); /* reserved */
672 put_bits(&pbc, 1, 0); /* asvc */
673 put_bits(&pbc, 3, info->substream[i].bsmod);
674 put_bits(&pbc, 3, info->substream[i].acmod);
675 put_bits(&pbc, 1, info->substream[i].lfeon);
676 put_bits(&pbc, 3, 0); /* reserved */
677 put_bits(&pbc, 4, info->substream[i].num_dep_sub);
678 if (!info->substream[i].num_dep_sub) {
679 put_bits(&pbc, 1, 0); /* reserved */
680 } else {
681 put_bits(&pbc, 9, info->substream[i].chan_loc);
682 }
683 }
684 if (info->complexity_index_type_a) {
685 put_bits(&pbc, 7, 0); /* reserved */
686 put_bits(&pbc, 1, 1); // flag_eac3_extension_type_a
687 put_bits(&pbc, 8, info->complexity_index_type_a);
688 }
689 flush_put_bits(&pbc);
690 size = put_bytes_output(&pbc);
691
692 avio_wb32(pb, size + 8);
693 ffio_wfourcc(pb, "dec3");
694 avio_write(pb, buf, size);
695
696 av_free(buf);
697
698 return size;
699}
700
701/**
702 * This function writes extradata "as is".
703 * Extradata must be formatted like a valid atom (with size and tag).
704 */
706{
707 avio_write(pb, track->extradata[track->last_stsd_index], track->extradata_size[track->last_stsd_index]);
708 return track->extradata_size[track->last_stsd_index];
709}
710
712{
713 avio_wb32(pb, 10);
714 ffio_wfourcc(pb, "enda");
715 avio_wb16(pb, 1); /* little endian */
716 return 10;
717}
718
720{
721 avio_wb32(pb, 10);
722 ffio_wfourcc(pb, "enda");
723 avio_wb16(pb, 0); /* big endian */
724 return 10;
725}
726
727static void put_descr(AVIOContext *pb, int tag, unsigned int size)
728{
729 int i = 3;
730 avio_w8(pb, tag);
731 for (; i > 0; i--)
732 avio_w8(pb, (size >> (7 * i)) | 0x80);
733 avio_w8(pb, size & 0x7F);
734}
735
736static unsigned compute_avg_bitrate(MOVTrack *track)
737{
738 uint64_t size = 0;
739 int i;
740 if (!track->track_duration)
741 return 0;
742 for (i = 0; i < track->entry; i++)
743 size += track->cluster[i].size;
744 return size * 8 * track->timescale / track->track_duration;
745}
746
748 uint32_t buffer_size; ///< Size of the decoding buffer for the elementary stream in bytes.
749 uint32_t max_bit_rate; ///< Maximum rate in bits/second over any window of one second.
750 uint32_t avg_bit_rate; ///< Average rate in bits/second over the entire presentation.
751};
752
754{
755 const AVPacketSideData *sd = track->st ?
759 AVCPBProperties *props = sd ? (AVCPBProperties *)sd->data : NULL;
760 struct mpeg4_bit_rate_values bit_rates = { 0 };
761
762 bit_rates.avg_bit_rate = compute_avg_bitrate(track);
763 if (!bit_rates.avg_bit_rate) {
764 // if the average bit rate cannot be calculated at this point, such as
765 // in the case of fragmented MP4, utilize the following values as
766 // fall-back in priority order:
767 //
768 // 1. average bit rate property
769 // 2. bit rate (usually average over the whole clip)
770 // 3. maximum bit rate property
771
772 if (props && props->avg_bitrate) {
773 bit_rates.avg_bit_rate = props->avg_bitrate;
774 } else if (track->par->bit_rate) {
775 bit_rates.avg_bit_rate = track->par->bit_rate;
776 } else if (props && props->max_bitrate) {
777 bit_rates.avg_bit_rate = props->max_bitrate;
778 }
779 }
780
781 // (FIXME should be max rate in any 1 sec window)
782 bit_rates.max_bit_rate = FFMAX(track->par->bit_rate,
783 bit_rates.avg_bit_rate);
784
785 // utilize values from properties if we have them available
786 if (props) {
787 // no avg_bitrate signals that the track is VBR
788 if (!props->avg_bitrate)
789 bit_rates.avg_bit_rate = props->avg_bitrate;
790 bit_rates.max_bit_rate = FFMAX(bit_rates.max_bit_rate,
791 props->max_bitrate);
792 bit_rates.buffer_size = props->buffer_size / 8;
793 }
794
795 return bit_rates;
796}
797
798static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
799{
800 struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track);
801 int64_t pos = avio_tell(pb);
802 int decoder_specific_info_len = track->extradata_size[track->last_stsd_index] ?
803 5 + track->extradata_size[track->last_stsd_index] : 0;
804
805 avio_wb32(pb, 0); // size
806 ffio_wfourcc(pb, "esds");
807 avio_wb32(pb, 0); // Version
808
809 // ES descriptor
810 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
811 avio_wb16(pb, track->track_id);
812 avio_w8(pb, 0x00); // flags (= no flags)
813
814 // DecoderConfig descriptor
815 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
816
817 // Object type indication
818 if ((track->par->codec_id == AV_CODEC_ID_MP2 ||
819 track->par->codec_id == AV_CODEC_ID_MP3) &&
820 track->par->sample_rate > 24000)
821 avio_w8(pb, 0x6B); // 11172-3
822 else
824
825 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
826 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
828 avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
829 else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
830 avio_w8(pb, 0x15); // flags (= Audiostream)
831 else
832 avio_w8(pb, 0x11); // flags (= Visualstream)
833
834 avio_wb24(pb, bit_rates.buffer_size); // Buffersize DB
835 avio_wb32(pb, bit_rates.max_bit_rate); // maxbitrate
836 avio_wb32(pb, bit_rates.avg_bit_rate);
837
838 if (track->extradata_size[track->last_stsd_index]) {
839 // DecoderSpecific info descriptor
840 put_descr(pb, 0x05, track->extradata_size[track->last_stsd_index]);
841 avio_write(pb, track->extradata[track->last_stsd_index],
842 track->extradata_size[track->last_stsd_index]);
843 }
844
845 // SL descriptor
846 put_descr(pb, 0x06, 1);
847 avio_w8(pb, 0x02);
848 return update_size(pb, pos);
849}
850
858
866
868{
869 int ret;
870 int64_t pos = avio_tell(pb);
871 avio_wb32(pb, 0);
872 avio_wl32(pb, track->tag); // store it byteswapped
873 track->par->codec_tag = av_bswap16(track->tag >> 16);
874 if ((ret = ff_put_wav_header(s, pb, track->par, 0)) < 0)
875 return ret;
876 return update_size(pb, pos);
877}
878
880{
881 int ret;
882 int64_t pos = avio_tell(pb);
883 avio_wb32(pb, 0);
884 ffio_wfourcc(pb, "wfex");
886 return ret;
887 return update_size(pb, pos);
888}
889
891{
892 int64_t pos = avio_tell(pb);
893 avio_wb32(pb, 0);
894 ffio_wfourcc(pb, "dfLa");
895 avio_w8(pb, 0); /* version */
896 avio_wb24(pb, 0); /* flags */
897
898 /* Expect the encoder to pass a METADATA_BLOCK_TYPE_STREAMINFO. */
900 return AVERROR_INVALIDDATA;
901
902 /* TODO: Write other METADATA_BLOCK_TYPEs if the encoder makes them available. */
903 avio_w8(pb, 1 << 7 | FLAC_METADATA_TYPE_STREAMINFO); /* LastMetadataBlockFlag << 7 | BlockType */
904 avio_wb24(pb, track->extradata_size[track->last_stsd_index]); /* Length */
905 avio_write(pb, track->extradata[track->last_stsd_index], track->extradata_size[track->last_stsd_index]); /* BlockData[Length] */
906
907 return update_size(pb, pos);
908}
909
911{
912 int64_t pos = avio_tell(pb);
914 avio_wb32(pb, 0);
915 ffio_wfourcc(pb, "dOps");
916 avio_w8(pb, 0); /* Version */
917 if (track->extradata_size[track->last_stsd_index] < 19) {
918 av_log(s, AV_LOG_ERROR, "invalid extradata size\n");
919 return AVERROR_INVALIDDATA;
920 }
921 /* extradata contains an Ogg OpusHead, other than byte-ordering and
922 OpusHead's preceding magic/version, OpusSpecificBox is currently
923 identical. */
924 channels = AV_RB8(track->extradata[track->last_stsd_index] + 9);
925 channel_map = AV_RB8(track->extradata[track->last_stsd_index] + 18);
926
927 avio_w8(pb, channels); /* OuputChannelCount */
928 avio_wb16(pb, AV_RL16(track->extradata[track->last_stsd_index] + 10)); /* PreSkip */
929 avio_wb32(pb, AV_RL32(track->extradata[track->last_stsd_index] + 12)); /* InputSampleRate */
930 avio_wb16(pb, AV_RL16(track->extradata[track->last_stsd_index] + 16)); /* OutputGain */
931 avio_w8(pb, channel_map); /* ChannelMappingFamily */
932 /* Write the rest of the header out without byte-swapping. */
933 if (channel_map) {
934 if (track->extradata_size[track->last_stsd_index] < 21 + channels) {
935 av_log(s, AV_LOG_ERROR, "invalid extradata size\n");
936 return AVERROR_INVALIDDATA;
937 }
938 avio_write(pb, track->extradata[track->last_stsd_index] + 19, 2 + channels); /* ChannelMappingTable */
939 }
940
941 return update_size(pb, pos);
942}
943
945{
946 int64_t pos = avio_tell(pb);
947 int length;
948 avio_wb32(pb, 0);
949 ffio_wfourcc(pb, "dmlp");
950
951 if (track->extradata_size[track->last_stsd_index] < 20) {
953 "Cannot write moov atom before TrueHD packets."
954 " Set the delay_moov flag to fix this.\n");
955 return AVERROR(EINVAL);
956 }
957
958 length = (AV_RB16(track->extradata[track->last_stsd_index]) & 0xFFF) * 2;
959 if (length < 20 || length > track->extradata_size[track->last_stsd_index])
960 return AVERROR_INVALIDDATA;
961
962 // Only TrueHD is supported
963 if (AV_RB32(track->extradata[track->last_stsd_index] + 4) != 0xF8726FBA)
964 return AVERROR_INVALIDDATA;
965
966 avio_wb32(pb, AV_RB32(track->extradata[track->last_stsd_index] + 8)); /* format_info */
967 avio_wb16(pb, AV_RB16(track->extradata[track->last_stsd_index] + 18) << 1); /* peak_data_rate */
968 avio_wb32(pb, 0); /* reserved */
969
970 return update_size(pb, pos);
971}
972
974{
975 int64_t pos = avio_tell(pb);
976 int layout = 0;
977
978 if (!track->extradata_size[track->last_stsd_index] ||
979 track->extradata_size[track->last_stsd_index] > UINT16_MAX) {
981 "Invalid extradata size %d for MPEGH-H stream.\n",
982 track->extradata_size[track->last_stsd_index]);
983 return AVERROR(EINVAL);
984 }
985
986 avio_wb32(pb, 0);
987 ffio_wfourcc(pb, "mhaC");
988
989 avio_w8(pb, 1); // ConfigurationVersion
990 avio_w8(pb, track->par->profile * 5 + track->par->level);
991
992 for (size_t i = 0; i < FF_ARRAY_ELEMS(ff_mpa_cicp_channel_layout_masks); i++) {
994 AVChannelLayout ch_layout;
996 if (!av_channel_layout_compare(&track->par->ch_layout, &ch_layout)) {
997 layout = i;
998 break;
999 }
1000 }
1001 }
1002
1003 avio_w8(pb, layout);
1004 avio_wb16(pb, track->extradata_size[track->last_stsd_index]);
1005 avio_write(pb, track->extradata[track->last_stsd_index],
1006 track->extradata_size[track->last_stsd_index]);
1007
1008 return update_size(pb, pos);
1009}
1010
1012{
1013 const AVDictionaryEntry *str = av_dict_get(track->st->metadata, "SA3D", NULL, 0);
1014 AVChannelLayout ch_layout = { 0 };
1015 int64_t pos;
1016 int ambisonic_order, ambi_channels, non_diegetic_channels;
1017 int i, ret;
1018
1019 if (!str)
1020 return 0;
1021
1022 ret = av_channel_layout_from_string(&ch_layout, str->value);
1023 if (ret < 0) {
1024 if (ret == AVERROR(EINVAL)) {
1025invalid:
1026 av_log(s, AV_LOG_ERROR, "Invalid SA3D layout: \"%s\"\n", str->value);
1027 ret = 0;
1028 }
1029 av_channel_layout_uninit(&ch_layout);
1030 return ret;
1031 }
1032
1033 if (track->st->codecpar->ch_layout.nb_channels != ch_layout.nb_channels)
1034 goto invalid;
1035
1036 ambisonic_order = av_channel_layout_ambisonic_order(&ch_layout);
1037 if (ambisonic_order < 0)
1038 goto invalid;
1039
1040 ambi_channels = (ambisonic_order + 1LL) * (ambisonic_order + 1LL);
1041 non_diegetic_channels = ch_layout.nb_channels - ambi_channels;
1042 if (non_diegetic_channels &&
1043 (non_diegetic_channels != 2 ||
1045 goto invalid;
1046
1047 av_log(s, AV_LOG_VERBOSE, "Inserting SA3D box with layout: \"%s\"\n", str->value);
1048
1049 pos = avio_tell(pb);
1050
1051 avio_wb32(pb, 0); // Size
1052 ffio_wfourcc(pb, "SA3D");
1053 avio_w8(pb, 0); // version
1054 avio_w8(pb, (!!non_diegetic_channels) << 7); // head_locked_stereo and ambisonic_type
1055 avio_wb32(pb, ambisonic_order); // ambisonic_order
1056 avio_w8(pb, 0); // ambisonic_channel_ordering
1057 avio_w8(pb, 0); // ambisonic_normalization
1058 avio_wb32(pb, ch_layout.nb_channels); // num_channels
1059 for (i = 0; i < ambi_channels; i++)
1061 for (; i < ch_layout.nb_channels; i++)
1062 avio_wb32(pb, av_channel_layout_channel_from_index(&ch_layout, i) + ambi_channels);
1063
1064 av_channel_layout_uninit(&ch_layout);
1065
1066 return update_size(pb, pos);
1067}
1068
1070{
1071 uint32_t layout_tag, bitmap, *channel_desc;
1072 int64_t pos = avio_tell(pb);
1073 int num_desc, ret;
1074
1075 if (track->multichannel_as_mono)
1076 return 0;
1077
1078 ret = ff_mov_get_channel_layout_tag(track->par, &layout_tag,
1079 &bitmap, &channel_desc);
1080
1081 if (ret < 0) {
1082 if (ret == AVERROR(ENOSYS)) {
1083 av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to "
1084 "lack of channel information\n");
1085 ret = 0;
1086 }
1087
1088 return ret;
1089 }
1090
1091 /* no predefined tag found + ch_layout in AV_CHANNEL_ORDER_NATIVE
1092 * but bitstream channels not actually in native order */
1093 if (layout_tag == MOV_CH_LAYOUT_UNKNOWN)
1094 return 0;
1095
1096 if (layout_tag == MOV_CH_LAYOUT_MONO && track->mono_as_fc > 0) {
1097 av_assert0(!channel_desc);
1098 channel_desc = av_malloc(sizeof(*channel_desc));
1099 if (!channel_desc)
1100 return AVERROR(ENOMEM);
1101
1102 layout_tag = 0;
1103 bitmap = 0;
1104 *channel_desc = 3; // channel label "Center"
1105 }
1106
1107 num_desc = layout_tag ? 0 : track->par->ch_layout.nb_channels;
1108
1109 avio_wb32(pb, 0); // Size
1110 ffio_wfourcc(pb, "chan"); // Type
1111 avio_w8(pb, 0); // Version
1112 avio_wb24(pb, 0); // Flags
1113 avio_wb32(pb, layout_tag); // mChannelLayoutTag
1114 avio_wb32(pb, bitmap); // mChannelBitmap
1115 avio_wb32(pb, num_desc); // mNumberChannelDescriptions
1116
1117 for (int i = 0; i < num_desc; i++) {
1118 avio_wb32(pb, channel_desc[i]); // mChannelLabel
1119 avio_wb32(pb, 0); // mChannelFlags
1120 avio_wl32(pb, 0); // mCoordinates[0]
1121 avio_wl32(pb, 0); // mCoordinates[1]
1122 avio_wl32(pb, 0); // mCoordinates[2]
1123 }
1124
1125 av_free(channel_desc);
1126
1127 return update_size(pb, pos);
1128}
1129
1131{
1132 int64_t pos = avio_tell(pb);
1133
1134 avio_wb32(pb, 0); /* size */
1135 ffio_wfourcc(pb, "wave");
1136
1137 if (track->par->codec_id != AV_CODEC_ID_QDM2) {
1138 avio_wb32(pb, 12); /* size */
1139 ffio_wfourcc(pb, "frma");
1140 avio_wl32(pb, track->tag);
1141 }
1142
1143 if (track->par->codec_id == AV_CODEC_ID_AAC) {
1144 /* useless atom needed by mplayer, ipod, not needed by quicktime */
1145 avio_wb32(pb, 12); /* size */
1146 ffio_wfourcc(pb, "mp4a");
1147 avio_wb32(pb, 0);
1148 mov_write_esds_tag(pb, track);
1149 } else if (mov_pcm_le_gt16(track->par->codec_id)) {
1151 } else if (mov_pcm_be_gt16(track->par->codec_id)) {
1153 } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
1154 mov_write_amr_tag(pb, track);
1155 } else if (track->par->codec_id == AV_CODEC_ID_AC3) {
1156 mov_write_ac3_tag(s, pb, track);
1157 } else if (track->par->codec_id == AV_CODEC_ID_EAC3) {
1158 mov_write_eac3_tag(s, pb, track);
1159 } else if (track->par->codec_id == AV_CODEC_ID_ALAC ||
1160 track->par->codec_id == AV_CODEC_ID_QDM2) {
1161 mov_write_extradata_tag(pb, track);
1162 } else if (track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1164 mov_write_ms_tag(s, pb, track);
1165 }
1166
1167 avio_wb32(pb, 8); /* size */
1168 avio_wb32(pb, 0); /* null tag */
1169
1170 return update_size(pb, pos);
1171}
1172
1173static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
1174{
1175 uint8_t *unescaped;
1176 const uint8_t *start, *next, *end = track->extradata[track->last_stsd_index] +
1177 track->extradata_size[track->last_stsd_index];
1178 int unescaped_size, seq_found = 0;
1179 int level = 0, interlace = 0;
1180 int packet_seq = track->vc1_info.packet_seq;
1181 int packet_entry = track->vc1_info.packet_entry;
1182 int slices = track->vc1_info.slices;
1183 PutBitContext pbc;
1184
1185 if (track->start_dts == AV_NOPTS_VALUE) {
1186 /* No packets written yet, vc1_info isn't authoritative yet. */
1187 /* Assume inline sequence and entry headers. */
1188 packet_seq = packet_entry = 1;
1190 "moov atom written before any packets, unable to write correct "
1191 "dvc1 atom. Set the delay_moov flag to fix this.\n");
1192 }
1193
1195 if (!unescaped)
1196 return AVERROR(ENOMEM);
1197 start = find_next_marker(track->extradata[track->last_stsd_index], end);
1198 for (next = start; next < end; start = next) {
1199 GetBitContext gb;
1200 int size;
1201 next = find_next_marker(start + 4, end);
1202 size = next - start - 4;
1203 if (size <= 0)
1204 continue;
1205 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
1206 init_get_bits(&gb, unescaped, 8 * unescaped_size);
1207 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
1208 int profile = get_bits(&gb, 2);
1209 if (profile != PROFILE_ADVANCED) {
1210 av_free(unescaped);
1211 return AVERROR(ENOSYS);
1212 }
1213 seq_found = 1;
1214 level = get_bits(&gb, 3);
1215 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
1216 * width, height */
1217 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
1218 skip_bits(&gb, 1); /* broadcast */
1219 interlace = get_bits1(&gb);
1220 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
1221 }
1222 }
1223 if (!seq_found) {
1224 av_free(unescaped);
1225 return AVERROR(ENOSYS);
1226 }
1227
1228 init_put_bits(&pbc, buf, 7);
1229 /* VC1DecSpecStruc */
1230 put_bits(&pbc, 4, 12); /* profile - advanced */
1231 put_bits(&pbc, 3, level);
1232 put_bits(&pbc, 1, 0); /* reserved */
1233 /* VC1AdvDecSpecStruc */
1234 put_bits(&pbc, 3, level);
1235 put_bits(&pbc, 1, 0); /* cbr */
1236 put_bits(&pbc, 6, 0); /* reserved */
1237 put_bits(&pbc, 1, !interlace); /* no interlace */
1238 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
1239 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
1240 put_bits(&pbc, 1, !slices); /* no slice code */
1241 put_bits(&pbc, 1, 0); /* no bframe */
1242 put_bits(&pbc, 1, 0); /* reserved */
1243
1244 /* framerate */
1245 if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
1246 put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
1247 else
1248 put_bits32(&pbc, 0xffffffff);
1249
1250 flush_put_bits(&pbc);
1251
1252 av_free(unescaped);
1253
1254 return 0;
1255}
1256
1258{
1259 uint8_t buf[7] = { 0 };
1260 int ret;
1261
1262 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
1263 return ret;
1264
1265 avio_wb32(pb, track->extradata_size[track->last_stsd_index] + 8 + sizeof(buf));
1266 ffio_wfourcc(pb, "dvc1");
1267 avio_write(pb, buf, sizeof(buf));
1268 avio_write(pb, track->extradata[track->last_stsd_index],
1269 track->extradata_size[track->last_stsd_index]);
1270
1271 return 0;
1272}
1273
1275{
1276 avio_wb32(pb, track->extradata_size[track->last_stsd_index] + 8);
1277 ffio_wfourcc(pb, "glbl");
1278 avio_write(pb, track->extradata[track->last_stsd_index],
1279 track->extradata_size[track->last_stsd_index]);
1280 return 8 + track->extradata_size[track->last_stsd_index];
1281}
1282
1283/**
1284 * Compute flags for 'lpcm' tag.
1285 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1286 */
1288{
1289 switch (codec_id) {
1292 return 11;
1295 return 9;
1296 case AV_CODEC_ID_PCM_U8:
1297 return 10;
1301 return 14;
1302 case AV_CODEC_ID_PCM_S8:
1306 return 12;
1307 default:
1308 return 0;
1309 }
1310}
1311
1312static int get_cluster_duration(MOVTrack *track, int cluster_idx)
1313{
1314 int64_t next_dts;
1315
1316 if (cluster_idx >= track->entry)
1317 return 0;
1318
1319 if (cluster_idx + 1 == track->entry)
1320 next_dts = track->track_duration + track->start_dts;
1321 else
1322 next_dts = track->cluster[cluster_idx + 1].dts;
1323
1324 next_dts -= track->cluster[cluster_idx].dts;
1325
1326 av_assert0(next_dts >= 0);
1327 av_assert0(next_dts <= INT_MAX);
1328
1329 return next_dts;
1330}
1331
1333{
1334 int i, first_duration;
1335
1336 /* use 1 for raw PCM */
1337 if (!track->audio_vbr)
1338 return 1;
1339
1340 /* check to see if duration is constant for all clusters */
1341 if (!track->entry)
1342 return 0;
1343 first_duration = get_cluster_duration(track, 0);
1344 for (i = 1; i < track->entry; i++) {
1345 if (get_cluster_duration(track, i) != first_duration)
1346 return 0;
1347 }
1348 return first_duration;
1349}
1350
1352{
1353 int64_t pos = avio_tell(pb);
1354 struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track);
1355 if (!bit_rates.max_bit_rate && !bit_rates.avg_bit_rate &&
1356 !bit_rates.buffer_size)
1357 // no useful data to be written, skip
1358 return 0;
1359
1360 avio_wb32(pb, 0); /* size */
1361 ffio_wfourcc(pb, "btrt");
1362
1363 avio_wb32(pb, bit_rates.buffer_size);
1364 avio_wb32(pb, bit_rates.max_bit_rate);
1365 avio_wb32(pb, bit_rates.avg_bit_rate);
1366
1367 return update_size(pb, pos);
1368}
1369
1371{
1372 int64_t pos = avio_tell(pb);
1373 int config = 0;
1374 int ret;
1375 uint8_t *speaker_pos = NULL;
1376 const AVChannelLayout *layout = &track->par->ch_layout;
1377
1379 if (ret || !config) {
1380 config = 0;
1381 speaker_pos = av_malloc(layout->nb_channels);
1382 if (!speaker_pos)
1383 return AVERROR(ENOMEM);
1385 speaker_pos, layout->nb_channels);
1386 if (ret) {
1387 char buf[128] = {0};
1388
1389 av_freep(&speaker_pos);
1390 av_channel_layout_describe(layout, buf, sizeof(buf));
1391 av_log(s, AV_LOG_ERROR, "unsupported channel layout %s\n", buf);
1392 return ret;
1393 }
1394 }
1395
1396 avio_wb32(pb, 0); /* size */
1397 ffio_wfourcc(pb, "chnl");
1398 avio_wb32(pb, 0); /* version & flags */
1399
1400 avio_w8(pb, 1); /* stream_structure */
1401 avio_w8(pb, config);
1402 if (config) {
1403 avio_wb64(pb, 0);
1404 } else {
1405 avio_write(pb, speaker_pos, layout->nb_channels);
1406 av_freep(&speaker_pos);
1407 }
1408
1409 return update_size(pb, pos);
1410}
1411
1413{
1414 int64_t pos = avio_tell(pb);
1415 int format_flags;
1416 int sample_size;
1417
1418 avio_wb32(pb, 0); /* size */
1419 ffio_wfourcc(pb, "pcmC");
1420 avio_wb32(pb, 0); /* version & flags */
1421
1422 /* 0x01: indicates little-endian format */
1423 format_flags = (track->par->codec_id == AV_CODEC_ID_PCM_F32LE ||
1424 track->par->codec_id == AV_CODEC_ID_PCM_F64LE ||
1425 track->par->codec_id == AV_CODEC_ID_PCM_S16LE ||
1426 track->par->codec_id == AV_CODEC_ID_PCM_S24LE ||
1428 avio_w8(pb, format_flags);
1429 sample_size = track->par->bits_per_raw_sample;
1430 if (!sample_size)
1431 sample_size = av_get_exact_bits_per_sample(track->par->codec_id);
1432 av_assert0(sample_size);
1433 avio_w8(pb, sample_size);
1434
1435 return update_size(pb, pos);
1436}
1437
1439{
1440 int64_t pos = avio_tell(pb);
1441 avio_wb32(pb, 0); /* size */
1442 ffio_wfourcc(pb, "srat");
1443 avio_wb32(pb, 0); /* version & flags */
1444
1445 avio_wb32(pb, track->par->sample_rate);
1446
1447 return update_size(pb, pos);
1448}
1449
1451{
1452 int64_t pos = avio_tell(pb);
1453 int version = 0;
1454 uint32_t tag = track->tag;
1455 int ret = 0;
1456
1457 if (track->mode == MODE_MOV) {
1458 if (track->par->sample_rate > UINT16_MAX || !track->par->ch_layout.nb_channels ||
1459 track->par->codec_id == AV_CODEC_ID_EAC3 ||
1460 track->par->ch_layout.nb_channels > 2) {
1461 if (mov_get_lpcm_flags(track->par->codec_id))
1462 tag = AV_RL32("lpcm");
1463 version = 2;
1464 } else if (track->audio_vbr || mov_pcm_le_gt16(track->par->codec_id) ||
1465 mov_pcm_be_gt16(track->par->codec_id) ||
1466 track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1468 track->par->codec_id == AV_CODEC_ID_QDM2) {
1469 version = 1;
1470 }
1471 } else if (track->mode == MODE_MP4) {
1472 if (track->par->sample_rate > UINT16_MAX &&
1474 version = 1;
1475 }
1476
1477 avio_wb32(pb, 0); /* size */
1478 if (mov->encryption_scheme != MOV_ENC_NONE) {
1479 ffio_wfourcc(pb, "enca");
1480 } else {
1481 avio_wl32(pb, tag); // store it byteswapped
1482 }
1483 avio_wb32(pb, 0); /* Reserved */
1484 avio_wb16(pb, 0); /* Reserved */
1485 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
1486
1487 /* SoundDescription */
1488 avio_wb16(pb, version); /* Version */
1489 avio_wb16(pb, 0); /* Revision level */
1490 avio_wb32(pb, 0); /* Reserved */
1491
1492 if (version == 2) {
1493 avio_wb16(pb, 3);
1494 avio_wb16(pb, 16);
1495 avio_wb16(pb, 0xfffe);
1496 avio_wb16(pb, 0);
1497 avio_wb32(pb, 0x00010000);
1498 avio_wb32(pb, 72);
1499 avio_wb64(pb, av_double2int(track->par->sample_rate));
1500 avio_wb32(pb, track->par->ch_layout.nb_channels);
1501 avio_wb32(pb, 0x7F000000);
1504 avio_wb32(pb, track->sample_size);
1506 } else {
1507 unsigned sample_rate = track->par->sample_rate;
1508
1509 if (track->mode == MODE_MOV) {
1510 avio_wb16(pb, track->par->ch_layout.nb_channels);
1511 if (track->par->codec_id == AV_CODEC_ID_PCM_U8 ||
1512 track->par->codec_id == AV_CODEC_ID_PCM_S8)
1513 avio_wb16(pb, 8); /* bits per sample */
1514 else if (track->par->codec_id == AV_CODEC_ID_ADPCM_G726)
1515 avio_wb16(pb, track->par->bits_per_coded_sample);
1516 else
1517 avio_wb16(pb, 16);
1518 avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
1519 } else { /* reserved for mp4/3gp */
1520 avio_wb16(pb, track->tag == MKTAG('i', 'a', 'm', 'f') ?
1521 0 : track->par->ch_layout.nb_channels);
1522 if (track->par->codec_id == AV_CODEC_ID_FLAC ||
1523 track->par->codec_id == AV_CODEC_ID_ALAC) {
1524 avio_wb16(pb, track->par->bits_per_raw_sample);
1525 } else {
1526 avio_wb16(pb, 16);
1527 }
1528 avio_wb16(pb, 0);
1529
1530 while (sample_rate > UINT16_MAX)
1531 sample_rate >>= 1;
1532 }
1533
1534 avio_wb16(pb, 0); /* packet size (= 0) */
1535 if (track->tag == MKTAG('i','a','m','f'))
1536 avio_wb16(pb, 0); /* samplerate must be 0 for IAMF */
1537 else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1538 avio_wb16(pb, 48000);
1539 else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1540 avio_wb32(pb, track->par->sample_rate);
1541 else
1542 avio_wb16(pb, sample_rate);
1543
1544 if (track->par->codec_id != AV_CODEC_ID_TRUEHD)
1545 avio_wb16(pb, 0); /* Reserved */
1546 }
1547
1548 if (track->mode == MODE_MOV && version == 1) { /* SoundDescription V1 extended info */
1549 if (mov_pcm_le_gt16(track->par->codec_id) ||
1550 mov_pcm_be_gt16(track->par->codec_id))
1551 avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
1552 else
1553 avio_wb32(pb, track->par->frame_size); /* Samples per packet */
1554 avio_wb32(pb, track->sample_size / track->par->ch_layout.nb_channels); /* Bytes per packet */
1555 avio_wb32(pb, track->sample_size); /* Bytes per frame */
1556 avio_wb32(pb, 2); /* Bytes per sample */
1557 }
1558
1559 if (track->mode == MODE_MOV &&
1560 (track->par->codec_id == AV_CODEC_ID_AAC ||
1561 track->par->codec_id == AV_CODEC_ID_AC3 ||
1562 track->par->codec_id == AV_CODEC_ID_EAC3 ||
1563 track->par->codec_id == AV_CODEC_ID_AMR_NB ||
1564 track->par->codec_id == AV_CODEC_ID_ALAC ||
1565 track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1567 track->par->codec_id == AV_CODEC_ID_QDM2 ||
1568 (mov_pcm_le_gt16(track->par->codec_id) && version==1) ||
1569 (mov_pcm_be_gt16(track->par->codec_id) && version==1)))
1570 ret = mov_write_wave_tag(s, pb, track);
1571 else if (track->tag == MKTAG('m','p','4','a'))
1572 ret = mov_write_esds_tag(pb, track);
1573#if CONFIG_IAMFENC
1574 else if (track->tag == MKTAG('i','a','m','f'))
1575 ret = mov_write_iacb_tag(mov->fc, pb, track);
1576#endif
1577 else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
1578 ret = mov_write_amr_tag(pb, track);
1579 else if (track->par->codec_id == AV_CODEC_ID_AC3)
1580 ret = mov_write_ac3_tag(s, pb, track);
1581 else if (track->par->codec_id == AV_CODEC_ID_EAC3)
1582 ret = mov_write_eac3_tag(s, pb, track);
1583 else if (track->par->codec_id == AV_CODEC_ID_ALAC)
1584 ret = mov_write_extradata_tag(pb, track);
1585 else if (track->par->codec_id == AV_CODEC_ID_WMAPRO)
1586 ret = mov_write_wfex_tag(s, pb, track);
1587 else if (track->par->codec_id == AV_CODEC_ID_FLAC)
1588 ret = mov_write_dfla_tag(pb, track);
1589 else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1590 ret = mov_write_dops_tag(s, pb, track);
1591 else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1592 ret = mov_write_dmlp_tag(s, pb, track);
1593 else if (track->par->codec_id == AV_CODEC_ID_MPEGH_3D_AUDIO)
1594 ret = mov_write_mhac_tag(s, pb, track);
1595 else if (tag == MOV_MP4_IPCM_TAG || tag == MOV_MP4_FPCM_TAG) {
1596 if (track->par->sample_rate > UINT16_MAX)
1597 mov_write_srat_tag(pb, track);
1598 if (track->par->ch_layout.nb_channels > 1)
1599 ret = mov_write_chnl_tag(s, pb, track);
1600 if (ret < 0)
1601 return ret;
1602 ret = mov_write_pcmc_tag(s, pb, track);
1603 } else if (track->extradata_size[track->last_stsd_index] > 0)
1604 ret = mov_write_glbl_tag(pb, track);
1605
1606 if (ret < 0)
1607 return ret;
1608
1609 if (track->mode == MODE_MP4 && track->par->codec_type == AVMEDIA_TYPE_AUDIO
1610 && ((ret = mov_write_SA3D_tag(s, pb, track)) < 0)) {
1611 return ret;
1612 }
1613
1614 if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO
1615 && ((ret = mov_write_chan_tag(s, pb, track)) < 0)) {
1616 return ret;
1617 }
1618
1620 && ((ret = ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid)) < 0)) {
1621 return ret;
1622 }
1623
1624 if (mov->write_btrt &&
1625 ((ret = mov_write_btrt_tag(pb, track)) < 0))
1626 return ret;
1627
1628 if (track->mode == MODE_MP4)
1629 track->entry_version = version;
1630
1631 ret = update_size(pb, pos);
1632 return ret;
1633}
1634
1636{
1637 avio_wb32(pb, 0xf); /* size */
1638 ffio_wfourcc(pb, "d263");
1639 ffio_wfourcc(pb, "FFMP");
1640 avio_w8(pb, 0); /* decoder version */
1641 /* FIXME use AVCodecContext level/profile, when encoder will set values */
1642 avio_w8(pb, 0xa); /* level */
1643 avio_w8(pb, 0); /* profile */
1644 return 0xf;
1645}
1646
1648{
1649 int64_t pos = avio_tell(pb);
1650
1651 avio_wb32(pb, 0);
1652 ffio_wfourcc(pb, "av1C");
1653 ff_isom_write_av1c(pb, track->extradata[track->last_stsd_index],
1654 track->extradata_size[track->last_stsd_index], track->mode != MODE_AVIF);
1655 return update_size(pb, pos);
1656}
1657
1659{
1660 int64_t pos = avio_tell(pb);
1661
1662 avio_wb32(pb, 0);
1663 ffio_wfourcc(pb, "avcC");
1664 ff_isom_write_avcc(pb, track->extradata[track->last_stsd_index],
1665 track->extradata_size[track->last_stsd_index]);
1666 return update_size(pb, pos);
1667}
1668
1669/* AVS3 Intelligent Media Coding
1670 * Information Technology - Intelligent Media Coding
1671 * Part 6: Intelligent Media Format
1672 */
1673static int mov_write_av3c(AVIOContext *pb, const uint8_t *data, int len)
1674{
1675 if (len < 4)
1676 return AVERROR_INVALIDDATA;
1677
1678 if (data[0] == 1) {
1679 // In Avs3DecoderConfigurationRecord format
1680 avio_write(pb, data, len);
1681 return 0;
1682 }
1683
1684 avio_w8(pb, 1); // version
1685 avio_wb16(pb, len); // sequence_header_length
1686 avio_write(pb, data, len); // sequence_header
1687 avio_w8(pb, 0xFC); // Only support library_dependency_idc = 0
1688
1689 return 0;
1690}
1691
1693{
1694 int64_t pos = avio_tell(pb);
1695 avio_wb32(pb, 0);
1696 ffio_wfourcc(pb, "av3c");
1697 mov_write_av3c(pb, track->extradata[track->last_stsd_index],
1698 track->extradata_size[track->last_stsd_index]);
1699 return update_size(pb, pos);
1700}
1701
1703{
1704 int64_t pos = avio_tell(pb);
1705
1706 avio_wb32(pb, 0);
1707 ffio_wfourcc(pb, "vpcC");
1708 ff_isom_write_vpcc(s, pb, track->extradata[track->last_stsd_index],
1709 track->extradata_size[track->last_stsd_index], track->par);
1710 return update_size(pb, pos);
1711}
1712
1714{
1715 int64_t pos = avio_tell(pb);
1716
1717 avio_wb32(pb, 0);
1718 ffio_wfourcc(pb, "hvcC");
1719 if (track->tag == MKTAG('h','v','c','1'))
1720 ff_isom_write_hvcc(pb, track->extradata[track->last_stsd_index],
1721 track->extradata_size[track->last_stsd_index], 1, s);
1722 else
1723 ff_isom_write_hvcc(pb, track->extradata[track->last_stsd_index],
1724 track->extradata_size[track->last_stsd_index], 0, s);
1725 return update_size(pb, pos);
1726}
1727
1729{
1730 int64_t pos = avio_tell(pb);
1731 int ret;
1732
1733 avio_wb32(pb, 0);
1734 ffio_wfourcc(pb, "lhvC");
1735 if (track->tag == MKTAG('h','v','c','1'))
1736 ret = ff_isom_write_lhvc(pb, track->extradata[track->last_stsd_index],
1737 track->extradata_size[track->last_stsd_index], 1, s);
1738 else
1739 ret = ff_isom_write_lhvc(pb, track->extradata[track->last_stsd_index],
1740 track->extradata_size[track->last_stsd_index], 0, s);
1741
1742 if (ret < 0) {
1743 avio_seek(pb, pos, SEEK_SET);
1744 return ret;
1745 }
1746
1747 return update_size(pb, pos);
1748}
1749
1751{
1752 int64_t pos = avio_tell(pb);
1753
1754 avio_wb32(pb, 0);
1755 ffio_wfourcc(pb, "evcC");
1756
1757 if (track->tag == MKTAG('e','v','c','1'))
1758 ff_isom_write_evcc(pb, track->extradata[track->last_stsd_index],
1759 track->extradata_size[track->last_stsd_index], 1);
1760 else
1761 ff_isom_write_evcc(pb, track->extradata[track->last_stsd_index],
1762 track->extradata_size[track->last_stsd_index], 0);
1763
1764 return update_size(pb, pos);
1765}
1766
1768{
1769 int64_t pos = avio_tell(pb);
1770
1771 avio_wb32(pb, 0);
1772 ffio_wfourcc(pb, "lvcC");
1773
1774 ff_isom_write_lvcc(pb, track->extradata[track->last_stsd_index],
1775 track->extradata_size[track->last_stsd_index], s);
1776
1777 return update_size(pb, pos);
1778}
1779
1781{
1782 int64_t pos = avio_tell(pb);
1783
1784 avio_wb32(pb, 0);
1785 ffio_wfourcc(pb, "vvcC");
1786
1787 avio_w8 (pb, 0); /* version */
1788 avio_wb24(pb, 0); /* flags */
1789
1790 if (track->tag == MKTAG('v','v','c','1'))
1791 ff_isom_write_vvcc(pb, track->extradata[track->last_stsd_index],
1792 track->extradata_size[track->last_stsd_index], 1);
1793 else
1794 ff_isom_write_vvcc(pb, track->extradata[track->last_stsd_index],
1795 track->extradata_size[track->last_stsd_index], 0);
1796 return update_size(pb, pos);
1797}
1798
1800{
1801 int64_t pos = avio_tell(pb);
1802
1803 avio_wb32(pb, 0);
1804 ffio_wfourcc(pb, "apvC");
1805
1806 avio_w8 (pb, 0); /* version */
1807 avio_wb24(pb, 0); /* flags */
1808
1809 ff_isom_write_apvc(pb, track->apv, s);
1810
1811 return update_size(pb, pos);
1812}
1813
1814/* also used by all avid codecs (dv, imx, meridien) and their variants */
1815/* https://community.avid.com/forums/t/136517.aspx */
1817{
1818 int interlaced;
1819 int cid;
1820 int display_width = track->par->width;
1821 const uint8_t *extradata;
1822
1823 if (track->extradata[track->last_stsd_index] && track->extradata_size[track->last_stsd_index] > 0x29) {
1824 if (ff_dnxhd_parse_header_prefix(track->extradata[track->last_stsd_index]) != 0) {
1825 /* looks like a DNxHD bit stream */
1826 extradata = track->extradata[track->last_stsd_index];
1827 } else {
1828 av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1829 return 0;
1830 }
1831 } else {
1832 av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1833 return 0;
1834 }
1835
1836 cid = AV_RB32(extradata + 0x28);
1837
1838 avio_wb32(pb, 24); /* size */
1839 ffio_wfourcc(pb, "ACLR");
1840 ffio_wfourcc(pb, "ACLR");
1841 ffio_wfourcc(pb, "0001");
1842 // 1: CCIR (supercolors will be dropped, 16 will be displayed as black)
1843 // 2: FullRange (0 will be displayed as black, 16 will be displayed as dark grey)
1844 if (track->par->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1846 avio_wb32(pb, 1);
1847 } else {
1848 avio_wb32(pb, 2);
1849 }
1850 avio_wb32(pb, 0); /* reserved */
1851
1852 if (track->tag == MKTAG('A','V','d','h')) {
1853 int alp = extradata[0x07] & 1;
1854 int pma = (extradata[0x07] >> 2) & 1;
1855 int sbd = (extradata[0x21] >> 5) & 3;
1856 int ssc = (extradata[0x2C] >> 5) & 3;
1857 int clv = (extradata[0x2C] >> 1) & 3;
1858 int clf = extradata[0x2C] & 1;
1859
1860 avio_wb32(pb, 32);
1861 ffio_wfourcc(pb, "ADHR");
1862 ffio_wfourcc(pb, "0001");
1863 avio_wb32(pb, cid); // Compression ID
1864 // 0: 4:2:2 Sub Sampling
1865 // 1: 4:2:0 Sub Sampling
1866 // 2: 4:4:4 Sub Sampling
1867 avio_wb32(pb, ssc); // Sub Sampling Control
1868 // 1: 8-bits per sample
1869 // 2: 10-bits per sample
1870 // 3: 12-bits per sample
1871 avio_wb32(pb, sbd); // Sample Bit Depth
1872 // 0: Bitstream is encoded using the YCBCR format rules and tables
1873 // 1: Bitstream is encoded using the RGB format rules and tables – only Compression IDs 1256, 1270
1874 avio_wb16(pb, clf); // Color Format
1875 // 0: ITU-R BT.709
1876 // 1: ITU-R BT.2020
1877 // 2: ITU-R BT.2020 C
1878 // 3: Out-of-band
1879 avio_wb16(pb, clv); // Color Volume
1880 // 0: Alpha channel not present
1881 // 1: Alpha channel present
1882 avio_wb16(pb, alp); // Alpha Present
1883 // 0: Alpha has not been applied to video channels
1884 // 1: Alpha has been applied to the video channels prior to encoding
1885 avio_wb16(pb, pma); // Pre-Multiplied Alpha
1886 return 0;
1887 }
1888
1889 interlaced = extradata[5] & 2;
1890
1891 avio_wb32(pb, 24); /* size */
1892 ffio_wfourcc(pb, "APRG");
1893 ffio_wfourcc(pb, "APRG");
1894 ffio_wfourcc(pb, "0001");
1895 // 1 for progressive or 2 for interlaced
1896 if (interlaced)
1897 avio_wb32(pb, 2);
1898 else
1899 avio_wb32(pb, 1);
1900 avio_wb32(pb, 0); /* reserved */
1901
1902 avio_wb32(pb, 120); /* size */
1903 ffio_wfourcc(pb, "ARES");
1904 ffio_wfourcc(pb, "ARES");
1905 ffio_wfourcc(pb, "0001");
1906 avio_wb32(pb, cid); /* cid */
1907 if ( track->par->sample_aspect_ratio.num > 0
1908 && track->par->sample_aspect_ratio.den > 0)
1909 display_width = display_width * track->par->sample_aspect_ratio.num / track->par->sample_aspect_ratio.den;
1910 avio_wb32(pb, display_width); // field width
1911 if (interlaced) {
1912 avio_wb32(pb, track->par->height / 2); // field height
1913 avio_wb32(pb, 2); // num fields
1914 avio_wb32(pb, 0); // num black lines (must be 0)
1915 // 4: HD1080i
1916 // 5: HD1080P
1917 // 6: HD720P
1918 avio_wb32(pb, 4); // video format
1919 } else {
1920 avio_wb32(pb, track->par->height);
1921 avio_wb32(pb, 1); // num fields
1922 avio_wb32(pb, 0);
1923 if (track->par->height == 1080)
1924 avio_wb32(pb, 5);
1925 else
1926 avio_wb32(pb, 6);
1927 }
1928 /* padding */
1929 ffio_fill(pb, 0, 10 * 8);
1930
1931 return 0;
1932}
1933
1935{
1936 avio_wb32(pb, 12);
1937 ffio_wfourcc(pb, "DpxE");
1938 if (track->extradata_size[track->last_stsd_index] >= 12 &&
1939 !memcmp(&track->extradata[track->last_stsd_index][4], "DpxE", 4)) {
1940 avio_wb32(pb, track->extradata[track->last_stsd_index][11]);
1941 } else {
1942 avio_wb32(pb, 1);
1943 }
1944 return 0;
1945}
1946
1948{
1949 int tag;
1950
1951 if (track->par->width == 720) { /* SD */
1952 if (track->par->height == 480) { /* NTSC */
1953 if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1954 else tag = MKTAG('d','v','c',' ');
1955 }else if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1956 else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1957 else tag = MKTAG('d','v','p','p');
1958 } else if (track->par->height == 720) { /* HD 720 line */
1959 if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
1960 else tag = MKTAG('d','v','h','p');
1961 } else if (track->par->height == 1080) { /* HD 1080 line */
1962 if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
1963 else tag = MKTAG('d','v','h','6');
1964 } else {
1965 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1966 return 0;
1967 }
1968
1969 return tag;
1970}
1971
1973{
1974 AVRational rational_framerate = st->avg_frame_rate;
1975 int rate = 0;
1976 if (rational_framerate.den != 0)
1977 rate = av_q2d(rational_framerate);
1978 return rate;
1979}
1980
1982{
1983 int tag = track->par->codec_tag;
1985 AVStream *st = track->st;
1986 int rate = defined_frame_rate(s, st);
1987
1988 if (!tag)
1989 tag = MKTAG('m', '2', 'v', '1'); //fallback tag
1990
1991 if (track->par->format == AV_PIX_FMT_YUV420P) {
1992 if (track->par->width == 1280 && track->par->height == 720) {
1993 if (!interlaced) {
1994 if (rate == 24) tag = MKTAG('x','d','v','4');
1995 else if (rate == 25) tag = MKTAG('x','d','v','5');
1996 else if (rate == 30) tag = MKTAG('x','d','v','1');
1997 else if (rate == 50) tag = MKTAG('x','d','v','a');
1998 else if (rate == 60) tag = MKTAG('x','d','v','9');
1999 }
2000 } else if (track->par->width == 1440 && track->par->height == 1080) {
2001 if (!interlaced) {
2002 if (rate == 24) tag = MKTAG('x','d','v','6');
2003 else if (rate == 25) tag = MKTAG('x','d','v','7');
2004 else if (rate == 30) tag = MKTAG('x','d','v','8');
2005 } else {
2006 if (rate == 25) tag = MKTAG('x','d','v','3');
2007 else if (rate == 30) tag = MKTAG('x','d','v','2');
2008 }
2009 } else if (track->par->width == 1920 && track->par->height == 1080) {
2010 if (!interlaced) {
2011 if (rate == 24) tag = MKTAG('x','d','v','d');
2012 else if (rate == 25) tag = MKTAG('x','d','v','e');
2013 else if (rate == 30) tag = MKTAG('x','d','v','f');
2014 } else {
2015 if (rate == 25) tag = MKTAG('x','d','v','c');
2016 else if (rate == 30) tag = MKTAG('x','d','v','b');
2017 }
2018 }
2019 } else if (track->par->format == AV_PIX_FMT_YUV422P) {
2020 if (track->par->width == 1280 && track->par->height == 720) {
2021 if (!interlaced) {
2022 if (rate == 24) tag = MKTAG('x','d','5','4');
2023 else if (rate == 25) tag = MKTAG('x','d','5','5');
2024 else if (rate == 30) tag = MKTAG('x','d','5','1');
2025 else if (rate == 50) tag = MKTAG('x','d','5','a');
2026 else if (rate == 60) tag = MKTAG('x','d','5','9');
2027 }
2028 } else if (track->par->width == 1920 && track->par->height == 1080) {
2029 if (!interlaced) {
2030 if (rate == 24) tag = MKTAG('x','d','5','d');
2031 else if (rate == 25) tag = MKTAG('x','d','5','e');
2032 else if (rate == 30) tag = MKTAG('x','d','5','f');
2033 } else {
2034 if (rate == 25) tag = MKTAG('x','d','5','c');
2035 else if (rate == 30) tag = MKTAG('x','d','5','b');
2036 }
2037 }
2038 }
2039
2040 return tag;
2041}
2042
2044{
2045 int tag = track->par->codec_tag;
2047 AVStream *st = track->st;
2048 int rate = defined_frame_rate(s, st);
2049
2050 if (!tag)
2051 tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
2052
2053 if (track->par->profile == AV_PROFILE_UNKNOWN ||
2054 !(track->par->profile & AV_PROFILE_H264_INTRA))
2055 return tag;
2056
2057 if (track->par->format == AV_PIX_FMT_YUV420P10) {
2058 if (track->par->width == 960 && track->par->height == 720) {
2059 if (!interlaced) {
2060 if (rate == 24) tag = MKTAG('a','i','5','p');
2061 else if (rate == 25) tag = MKTAG('a','i','5','q');
2062 else if (rate == 30) tag = MKTAG('a','i','5','p');
2063 else if (rate == 50) tag = MKTAG('a','i','5','q');
2064 else if (rate == 60) tag = MKTAG('a','i','5','p');
2065 }
2066 } else if (track->par->width == 1440 && track->par->height == 1080) {
2067 if (!interlaced) {
2068 if (rate == 24) tag = MKTAG('a','i','5','3');
2069 else if (rate == 25) tag = MKTAG('a','i','5','2');
2070 else if (rate == 30) tag = MKTAG('a','i','5','3');
2071 } else {
2072 if (rate == 50) tag = MKTAG('a','i','5','5');
2073 else if (rate == 60) tag = MKTAG('a','i','5','6');
2074 }
2075 }
2076 } else if (track->par->format == AV_PIX_FMT_YUV422P10) {
2077 if (track->par->width == 1280 && track->par->height == 720) {
2078 if (!interlaced) {
2079 if (rate == 24) tag = MKTAG('a','i','1','p');
2080 else if (rate == 25) tag = MKTAG('a','i','1','q');
2081 else if (rate == 30) tag = MKTAG('a','i','1','p');
2082 else if (rate == 50) tag = MKTAG('a','i','1','q');
2083 else if (rate == 60) tag = MKTAG('a','i','1','p');
2084 }
2085 } else if (track->par->width == 1920 && track->par->height == 1080) {
2086 if (!interlaced) {
2087 if (rate == 24) tag = MKTAG('a','i','1','3');
2088 else if (rate == 25) tag = MKTAG('a','i','1','2');
2089 else if (rate == 30) tag = MKTAG('a','i','1','3');
2090 } else {
2091 if (rate == 25) tag = MKTAG('a','i','1','5');
2092 else if (rate == 50) tag = MKTAG('a','i','1','5');
2093 else if (rate == 60) tag = MKTAG('a','i','1','6');
2094 }
2095 } else if ( track->par->width == 4096 && track->par->height == 2160
2096 || track->par->width == 3840 && track->par->height == 2160
2097 || track->par->width == 2048 && track->par->height == 1080) {
2098 tag = MKTAG('a','i','v','x');
2099 }
2100 }
2101
2102 return tag;
2103}
2104
2106{
2107 int tag = track->par->codec_tag;
2108
2109 if (!tag)
2110 tag = MKTAG('e', 'v', 'c', '1');
2111
2112 return tag;
2113}
2114
2116{
2117 int tag = track->par->codec_tag;
2118
2119 if (!tag)
2120 tag = MKTAG('a', 'p', 'v', '1');
2121
2122 return tag;
2123}
2124
2125
2126static const struct {
2128 uint32_t tag;
2129 unsigned bps;
2130} mov_pix_fmt_tags[] = {
2131 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
2132 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
2133 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
2134 { AV_PIX_FMT_VYU444, MKTAG('v','3','0','8'), 0 },
2135 { AV_PIX_FMT_UYVA, MKTAG('v','4','0','8'), 0 },
2136 { AV_PIX_FMT_V30XLE, MKTAG('v','4','1','0'), 0 },
2137 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
2138 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
2139 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
2140 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
2141 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
2142 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
2143 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
2144 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
2145 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
2146 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
2147 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
2148 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
2150
2152{
2153 int tag = MKTAG('A','V','d','n');
2154 if (track->par->profile != AV_PROFILE_UNKNOWN &&
2155 track->par->profile != AV_PROFILE_DNXHD)
2156 tag = MKTAG('A','V','d','h');
2157 return tag;
2158}
2159
2161{
2162 int tag = track->par->codec_tag;
2163 int i;
2165
2166 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
2167 if (track->par->format == mov_pix_fmt_tags[i].pix_fmt) {
2168 tag = mov_pix_fmt_tags[i].tag;
2170 if (track->par->codec_tag == mov_pix_fmt_tags[i].tag)
2171 break;
2172 }
2173 }
2174
2176 track->par->bits_per_coded_sample);
2177 if (tag == MKTAG('r','a','w',' ') &&
2178 track->par->format != pix_fmt &&
2179 track->par->format != AV_PIX_FMT_GRAY8 &&
2180 track->par->format != AV_PIX_FMT_NONE)
2181 av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
2182 av_get_pix_fmt_name(track->par->format));
2183 return tag;
2184}
2185
2186static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
2187{
2188 unsigned int tag = track->par->codec_tag;
2189
2190 // "rtp " is used to distinguish internally created RTP-hint tracks
2191 // (with rtp_ctx) from other tracks.
2192 if (tag == MKTAG('r','t','p',' '))
2193 tag = 0;
2194 if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
2195 (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
2196 track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
2197 track->par->codec_id == AV_CODEC_ID_H263 ||
2198 track->par->codec_id == AV_CODEC_ID_H264 ||
2199 track->par->codec_id == AV_CODEC_ID_DNXHD ||
2200 track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2201 av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
2202 if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
2203 tag = mov_get_dv_codec_tag(s, track);
2204 else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
2206 else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO)
2208 else if (track->par->codec_id == AV_CODEC_ID_H264)
2209 tag = mov_get_h264_codec_tag(s, track);
2210 else if (track->par->codec_id == AV_CODEC_ID_EVC)
2211 tag = mov_get_evc_codec_tag(s, track);
2212 else if (track->par->codec_id == AV_CODEC_ID_APV)
2213 tag = mov_get_apv_codec_tag(s, track);
2214 else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
2215 tag = mov_get_dnxhd_codec_tag(s, track);
2216 else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2218 if (!tag) { // if no mac fcc found, try with Microsoft tags
2220 if (tag)
2221 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
2222 "the file may be unplayable!\n");
2223 }
2224 } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2226 if (!tag) { // if no mac fcc found, try with Microsoft tags
2227 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
2228 if (ms_tag) {
2229 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
2230 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
2231 "the file may be unplayable!\n");
2232 }
2233 }
2234 } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2236 }
2237
2238 return tag;
2239}
2240
2242 { AV_CODEC_ID_MJPEG, 0xD },
2243 { AV_CODEC_ID_PNG, 0xE },
2244 { AV_CODEC_ID_BMP, 0x1B },
2245 { AV_CODEC_ID_NONE, 0 },
2246};
2247
2248static unsigned int validate_codec_tag(const AVCodecTag *const *tags,
2249 unsigned int tag, int codec_id)
2250{
2251 int i;
2252
2253 /**
2254 * Check that tag + id is in the table
2255 */
2256 for (i = 0; tags && tags[i]; i++) {
2257 const AVCodecTag *codec_tags = tags[i];
2258 while (codec_tags->id != AV_CODEC_ID_NONE) {
2259 if (ff_toupper4(codec_tags->tag) == ff_toupper4(tag) &&
2260 codec_tags->id == codec_id)
2261 return codec_tags->tag;
2262 codec_tags++;
2263 }
2264 }
2265 return 0;
2266}
2267
2268static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
2269{
2270 if (is_cover_image(track->st))
2272
2273 if (track->mode == MODE_IPOD)
2274 if (!av_match_ext(s->url, "m4a") &&
2275 !av_match_ext(s->url, "m4v") &&
2276 !av_match_ext(s->url, "m4b"))
2277 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
2278 "Quicktime/Ipod might not play the file\n");
2279
2280 if (track->mode == MODE_MOV) {
2281 return mov_get_codec_tag(s, track);
2282 } else
2283 return validate_codec_tag(s->oformat->codec_tag, track->par->codec_tag,
2284 track->par->codec_id);
2285}
2286
2287/** Write uuid atom.
2288 * Needed to make file play in iPods running newest firmware
2289 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
2290 */
2292{
2293 avio_wb32(pb, 28);
2294 ffio_wfourcc(pb, "uuid");
2295 avio_wb32(pb, 0x6b6840f2);
2296 avio_wb32(pb, 0x5f244fc5);
2297 avio_wb32(pb, 0xba39a51b);
2298 avio_wb32(pb, 0xcf0323f3);
2299 avio_wb32(pb, 0x0);
2300 return 28;
2301}
2302
2303static const uint16_t fiel_data[] = {
2304 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
2305};
2306
2307static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
2308{
2309 unsigned mov_field_order = 0;
2310 if (field_order < FF_ARRAY_ELEMS(fiel_data))
2311 mov_field_order = fiel_data[field_order];
2312 else
2313 return 0;
2314 avio_wb32(pb, 10);
2315 ffio_wfourcc(pb, "fiel");
2316 avio_wb16(pb, mov_field_order);
2317 return 10;
2318}
2319
2321{
2322 MOVMuxContext *mov = s->priv_data;
2323 int ret = AVERROR_BUG;
2324 int64_t pos = avio_tell(pb);
2325 avio_wb32(pb, 0); /* size */
2326 avio_wl32(pb, track->tag); // store it byteswapped
2327 avio_wb32(pb, 0); /* Reserved */
2328 avio_wb16(pb, 0); /* Reserved */
2329 avio_wb16(pb, 1); /* Data-reference index */
2330
2331 if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
2332 mov_write_esds_tag(pb, track);
2333 else if (track->par->codec_id == AV_CODEC_ID_TTML) {
2334 switch (track->par->codec_tag) {
2335 case MOV_ISMV_TTML_TAG:
2336 // ISMV dfxp requires no extradata.
2337 break;
2338 case MOV_MP4_TTML_TAG:
2339 // As specified in 14496-30, XMLSubtitleSampleEntry
2340 // Namespace
2341 avio_put_str(pb, "http://www.w3.org/ns/ttml");
2342 // Empty schema_location
2343 avio_w8(pb, 0);
2344 // Empty auxiliary_mime_types
2345 avio_w8(pb, 0);
2346 break;
2347 default:
2349 "Unknown codec tag '%s' utilized for TTML stream with "
2350 "index %d (track id %d)!\n",
2351 av_fourcc2str(track->par->codec_tag), track->st->index,
2352 track->track_id);
2353 return AVERROR(EINVAL);
2354 }
2355 } else if (track->extradata_size[track->last_stsd_index])
2356 avio_write(pb, track->extradata[track->last_stsd_index], track->extradata_size[track->last_stsd_index]);
2357
2358 if (mov->write_btrt &&
2359 ((ret = mov_write_btrt_tag(pb, track)) < 0))
2360 return ret;
2361
2362 return update_size(pb, pos);
2363}
2364
2366{
2367 int8_t stereo_mode;
2368
2369 if (stereo_3d->flags != 0) {
2370 av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d flags %x. st3d not written.\n", stereo_3d->flags);
2371 return 0;
2372 }
2373
2374 switch (stereo_3d->type) {
2375 case AV_STEREO3D_2D:
2376 stereo_mode = 0;
2377 break;
2379 stereo_mode = 1;
2380 break;
2382 stereo_mode = 2;
2383 break;
2384 default:
2385 av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d type %s. st3d not written.\n", av_stereo3d_type_name(stereo_3d->type));
2386 return 0;
2387 }
2388 avio_wb32(pb, 13); /* size */
2389 ffio_wfourcc(pb, "st3d");
2390 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2391 avio_w8(pb, stereo_mode);
2392 return 13;
2393}
2394
2396{
2397 int64_t sv3d_pos, svhd_pos, proj_pos;
2398 const char* metadata_source = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : LIBAVFORMAT_IDENT;
2399
2400 if (spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
2401 spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
2402 spherical_mapping->projection != AV_SPHERICAL_CUBEMAP) {
2403 av_log(s, AV_LOG_WARNING, "Unsupported projection %d. sv3d not written.\n", spherical_mapping->projection);
2404 return 0;
2405 }
2406
2407 sv3d_pos = avio_tell(pb);
2408 avio_wb32(pb, 0); /* size */
2409 ffio_wfourcc(pb, "sv3d");
2410
2411 svhd_pos = avio_tell(pb);
2412 avio_wb32(pb, 0); /* size */
2413 ffio_wfourcc(pb, "svhd");
2414 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2415 avio_put_str(pb, metadata_source);
2416 update_size(pb, svhd_pos);
2417
2418 proj_pos = avio_tell(pb);
2419 avio_wb32(pb, 0); /* size */
2420 ffio_wfourcc(pb, "proj");
2421
2422 avio_wb32(pb, 24); /* size */
2423 ffio_wfourcc(pb, "prhd");
2424 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2425 avio_wb32(pb, spherical_mapping->yaw);
2426 avio_wb32(pb, spherical_mapping->pitch);
2427 avio_wb32(pb, spherical_mapping->roll);
2428
2429 switch (spherical_mapping->projection) {
2432 avio_wb32(pb, 28); /* size */
2433 ffio_wfourcc(pb, "equi");
2434 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2435 avio_wb32(pb, spherical_mapping->bound_top);
2436 avio_wb32(pb, spherical_mapping->bound_bottom);
2437 avio_wb32(pb, spherical_mapping->bound_left);
2438 avio_wb32(pb, spherical_mapping->bound_right);
2439 break;
2441 avio_wb32(pb, 20); /* size */
2442 ffio_wfourcc(pb, "cbmp");
2443 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2444 avio_wb32(pb, 0); /* layout */
2445 avio_wb32(pb, spherical_mapping->padding); /* padding */
2446 break;
2447 }
2448 update_size(pb, proj_pos);
2449
2450 return update_size(pb, sv3d_pos);
2451}
2452
2454{
2455 return av_rescale(q.num, b, q.den);
2456}
2457
2459 const AVStereo3D *stereo3d)
2460{
2461 if (!stereo3d->horizontal_field_of_view.num)
2462 return;
2463
2464 avio_wb32(pb, 12); /* size */
2465 ffio_wfourcc(pb, "hfov");
2467}
2468
2470 const AVSphericalMapping *spherical_mapping)
2471{
2472 avio_wb32(pb, 24); /* size */
2473 ffio_wfourcc(pb, "proj");
2474 avio_wb32(pb, 16); /* size */
2475 ffio_wfourcc(pb, "prji");
2476 avio_wb32(pb, 0); /* version + flags */
2477
2478 switch (spherical_mapping->projection) {
2480 ffio_wfourcc(pb, "rect");
2481 break;
2483 ffio_wfourcc(pb, "equi");
2484 break;
2486 ffio_wfourcc(pb, "hequ");
2487 break;
2489 ffio_wfourcc(pb, "fish");
2490 break;
2491 default:
2492 av_assert0(0);
2493 }
2494}
2495
2497 const AVStereo3D *stereo3d)
2498{
2499 int64_t pos = avio_tell(pb);
2500 int view = 0;
2501
2502 avio_wb32(pb, 0); /* size */
2503 ffio_wfourcc(pb, "eyes");
2504
2505 // stri is mandatory
2506 avio_wb32(pb, 13); /* size */
2507 ffio_wfourcc(pb, "stri");
2508 avio_wb32(pb, 0); /* version + flags */
2509 switch (stereo3d->view) {
2511 view |= 1 << 0;
2512 break;
2514 view |= 1 << 1;
2515 break;
2517 view |= (1 << 0) | (1 << 1);
2518 break;
2519 }
2520 view |= !!(stereo3d->flags & AV_STEREO3D_FLAG_INVERT) << 3;
2521 avio_w8(pb, view);
2522
2523 // hero is optional
2524 if (stereo3d->primary_eye != AV_PRIMARY_EYE_NONE) {
2525 avio_wb32(pb, 13); /* size */
2526 ffio_wfourcc(pb, "hero");
2527 avio_wb32(pb, 0); /* version + flags */
2528 avio_w8(pb, stereo3d->primary_eye);
2529 }
2530
2531 // it's not clear if cams is mandatory or optional
2532 if (stereo3d->baseline) {
2533 avio_wb32(pb, 24); /* size */
2534 ffio_wfourcc(pb, "cams");
2535 avio_wb32(pb, 16); /* size */
2536 ffio_wfourcc(pb, "blin");
2537 avio_wb32(pb, 0); /* version + flags */
2538 avio_wb32(pb, stereo3d->baseline);
2539 }
2540
2541 // it's not clear if cmfy is mandatory or optional
2542 if (stereo3d->horizontal_disparity_adjustment.num) {
2543 avio_wb32(pb, 24); /* size */
2544 ffio_wfourcc(pb, "cmfy");
2545 avio_wb32(pb, 16); /* size */
2546 ffio_wfourcc(pb, "dadj");
2547 avio_wb32(pb, 0); /* version + flags */
2549 }
2550
2551 return update_size(pb, pos);
2552}
2553
2555 const AVStereo3D *stereo3d,
2556 const AVSphericalMapping *spherical_mapping)
2557{
2558 int64_t pos;
2559
2560 if (spherical_mapping &&
2561 spherical_mapping->projection != AV_SPHERICAL_RECTILINEAR &&
2562 spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
2563 spherical_mapping->projection != AV_SPHERICAL_HALF_EQUIRECTANGULAR &&
2564 spherical_mapping->projection != AV_SPHERICAL_FISHEYE) {
2565 av_log(s, AV_LOG_WARNING, "Unsupported projection %d. proj not written.\n",
2566 spherical_mapping->projection);
2567 spherical_mapping = NULL;
2568 }
2569
2570 if (stereo3d && (stereo3d->type == AV_STEREO3D_2D ||
2571 (!(stereo3d->flags & AV_STEREO3D_FLAG_INVERT) &&
2572 stereo3d->view == AV_STEREO3D_VIEW_UNSPEC &&
2573 stereo3d->primary_eye == AV_PRIMARY_EYE_NONE &&
2574 !stereo3d->baseline &&
2575 !stereo3d->horizontal_disparity_adjustment.num))) {
2576 av_log(s, AV_LOG_WARNING, "Unsupported stereo 3d metadata. eyes not written.\n");
2577 stereo3d = NULL;
2578 }
2579
2580 if (!spherical_mapping && !stereo3d)
2581 return 0;
2582
2583 pos = avio_tell(pb);
2584 avio_wb32(pb, 0); /* size */
2585 ffio_wfourcc(pb, "vexu");
2586
2587 if (spherical_mapping)
2588 mov_write_vexu_proj_tag(s, pb, spherical_mapping);
2589
2590 if (stereo3d)
2591 mov_write_eyes_tag(s, pb, stereo3d);
2592
2593 return update_size(pb, pos);
2594}
2595
2597{
2598 uint8_t buf[ISOM_DVCC_DVVC_SIZE];
2599
2600 avio_wb32(pb, 32); /* size = 8 + 24 */
2601 if (dovi->dv_profile > 10)
2602 ffio_wfourcc(pb, "dvwC");
2603 else if (dovi->dv_profile > 7)
2604 ffio_wfourcc(pb, "dvvC");
2605 else
2606 ffio_wfourcc(pb, "dvcC");
2607
2608 ff_isom_put_dvcc_dvvc(s, buf, dovi);
2609 avio_write(pb, buf, sizeof(buf));
2610
2611 return 32; /* 8 + 24 */
2612}
2613
2615{
2616 avio_wb32(pb, 8 + sd->size);
2617 ffio_wfourcc(pb, "hvcE");
2618 avio_write(pb, sd->data, sd->size);
2619 return 8 + sd->size;
2620}
2621
2623 uint32_t top, uint32_t bottom,
2624 uint32_t left, uint32_t right)
2625{
2626 uint32_t cropped_width = track->par->width - left - right;
2627 uint32_t cropped_height = track->height - top - bottom;
2628 AVRational horizOff =
2629 av_sub_q((AVRational) { track->par->width - cropped_width, 2 },
2630 (AVRational) { left, 1 });
2631 AVRational vertOff =
2632 av_sub_q((AVRational) { track->height - cropped_height, 2 },
2633 (AVRational) { top, 1 });
2634
2635 avio_wb32(pb, 40);
2636 ffio_wfourcc(pb, "clap");
2637 avio_wb32(pb, cropped_width); /* apertureWidthN */
2638 avio_wb32(pb, 1); /* apertureWidthD */
2639 avio_wb32(pb, cropped_height); /* apertureHeightN */
2640 avio_wb32(pb, 1); /* apertureHeightD */
2641
2642 avio_wb32(pb, -horizOff.num);
2643 avio_wb32(pb, horizOff.den);
2644 avio_wb32(pb, -vertOff.num);
2645 avio_wb32(pb, vertOff.den);
2646
2647 return 40;
2648}
2649
2651{
2652 AVRational sar;
2653 av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
2654 track->par->sample_aspect_ratio.den, INT_MAX);
2655
2656 avio_wb32(pb, 16);
2657 ffio_wfourcc(pb, "pasp");
2658 avio_wb32(pb, sar.num);
2659 avio_wb32(pb, sar.den);
2660 return 16;
2661}
2662
2663static int mov_write_gama_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track, double gamma)
2664{
2665 uint32_t gama = 0;
2666 if (gamma <= 0.0)
2668 av_log(s, AV_LOG_DEBUG, "gamma value %g\n", gamma);
2669
2670 if (gamma > 1e-6) {
2671 gama = (uint32_t)lrint((double)(1<<16) * gamma);
2672 av_log(s, AV_LOG_DEBUG, "writing gama value %"PRId32"\n", gama);
2673
2674 av_assert0(track->mode == MODE_MOV);
2675 avio_wb32(pb, 12);
2676 ffio_wfourcc(pb, "gama");
2677 avio_wb32(pb, gama);
2678 return 12;
2679 } else {
2680 av_log(s, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
2681 }
2682 return 0;
2683}
2684
2685static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track, int prefer_icc)
2686{
2687 int64_t pos = avio_tell(pb);
2688
2689 // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
2690 // Ref (MP4): ISO/IEC 14496-12:2012
2691
2692 if (prefer_icc) {
2696
2697 if (sd) {
2698 avio_wb32(pb, 12 + sd->size);
2699 ffio_wfourcc(pb, "colr");
2700 ffio_wfourcc(pb, "prof");
2701 avio_write(pb, sd->data, sd->size);
2702 return 12 + sd->size;
2703 }
2704 else {
2705 av_log(NULL, AV_LOG_INFO, "no ICC profile found, will write nclx/nclc colour info instead\n");
2706 }
2707 }
2708
2709 /* We should only ever be called for MOV, MP4 and AVIF. */
2710 av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4 ||
2711 track->mode == MODE_AVIF);
2712
2713 avio_wb32(pb, 0); /* size */
2714 ffio_wfourcc(pb, "colr");
2715 if (track->mode == MODE_MP4 || track->mode == MODE_AVIF)
2716 ffio_wfourcc(pb, "nclx");
2717 else
2718 ffio_wfourcc(pb, "nclc");
2719 // Do not try to guess the color info if it is AVCOL_PRI_UNSPECIFIED.
2720 // e.g., Dolby Vision for Apple devices should be set to AVCOL_PRI_UNSPECIFIED. See
2721 // https://developer.apple.com/av-foundation/High-Dynamic-Range-Metadata-for-Apple-Devices.pdf
2722 avio_wb16(pb, track->par->color_primaries);
2723 avio_wb16(pb, track->par->color_trc);
2724 avio_wb16(pb, track->par->color_space);
2725 if (track->mode == MODE_MP4 || track->mode == MODE_AVIF) {
2726 int full_range = track->par->color_range == AVCOL_RANGE_JPEG;
2727 avio_w8(pb, full_range << 7);
2728 }
2729
2730 return update_size(pb, pos);
2731}
2732
2734{
2735 const AVPacketSideData *side_data;
2736 const AVContentLightMetadata *content_light_metadata;
2737
2741 if (!side_data) {
2742 return 0;
2743 }
2744 content_light_metadata = (const AVContentLightMetadata*)side_data->data;
2745
2746 avio_wb32(pb, 12); // size
2747 ffio_wfourcc(pb, "clli");
2748 avio_wb16(pb, content_light_metadata->MaxCLL);
2749 avio_wb16(pb, content_light_metadata->MaxFALL);
2750 return 12;
2751}
2752
2754{
2755 const int chroma_den = 50000;
2756 const int luma_den = 10000;
2757 const AVPacketSideData *side_data;
2759
2763 if (side_data)
2764 metadata = (const AVMasteringDisplayMetadata*)side_data->data;
2765 if (!metadata || !metadata->has_primaries || !metadata->has_luminance) {
2766 return 0;
2767 }
2768
2769 avio_wb32(pb, 32); // size
2770 ffio_wfourcc(pb, "mdcv");
2771 avio_wb16(pb, rescale_rational(metadata->display_primaries[1][0], chroma_den));
2772 avio_wb16(pb, rescale_rational(metadata->display_primaries[1][1], chroma_den));
2773 avio_wb16(pb, rescale_rational(metadata->display_primaries[2][0], chroma_den));
2774 avio_wb16(pb, rescale_rational(metadata->display_primaries[2][1], chroma_den));
2775 avio_wb16(pb, rescale_rational(metadata->display_primaries[0][0], chroma_den));
2776 avio_wb16(pb, rescale_rational(metadata->display_primaries[0][1], chroma_den));
2777 avio_wb16(pb, rescale_rational(metadata->white_point[0], chroma_den));
2778 avio_wb16(pb, rescale_rational(metadata->white_point[1], chroma_den));
2779 avio_wb32(pb, rescale_rational(metadata->max_luminance, luma_den));
2780 avio_wb32(pb, rescale_rational(metadata->min_luminance, luma_den));
2781 return 32;
2782}
2783
2785{
2786 const int illuminance_den = 10000;
2787 const int ambient_den = 50000;
2788 const AVPacketSideData *side_data;
2789 const AVAmbientViewingEnvironment *ambient;
2790
2791
2795
2796 if (!side_data)
2797 return 0;
2798
2799 ambient = (const AVAmbientViewingEnvironment*)side_data->data;
2800 if (!ambient || !ambient->ambient_illuminance.num)
2801 return 0;
2802
2803 avio_wb32(pb, 16); // size
2804 ffio_wfourcc(pb, "amve");
2805 avio_wb32(pb, rescale_rational(ambient->ambient_illuminance, illuminance_den));
2806 avio_wb16(pb, rescale_rational(ambient->ambient_light_x, ambient_den));
2807 avio_wb16(pb, rescale_rational(ambient->ambient_light_y, ambient_den));
2808 return 16;
2809}
2810
2811static void find_compressor(char * compressor_name, int len, MOVTrack *track)
2812{
2813 AVDictionaryEntry *encoder;
2814 int xdcam_res = (track->par->width == 1280 && track->par->height == 720)
2815 || (track->par->width == 1440 && track->par->height == 1080)
2816 || (track->par->width == 1920 && track->par->height == 1080);
2817
2818 if ((track->mode == MODE_AVIF ||
2819 track->mode == MODE_MOV ||
2820 track->mode == MODE_MP4) &&
2821 (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
2822 av_strlcpy(compressor_name, encoder->value, 32);
2823 } else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
2825 AVStream *st = track->st;
2826 int rate = defined_frame_rate(NULL, st);
2827 av_strlcatf(compressor_name, len, "XDCAM");
2828 if (track->par->format == AV_PIX_FMT_YUV422P) {
2829 av_strlcatf(compressor_name, len, " HD422");
2830 } else if(track->par->width == 1440) {
2831 av_strlcatf(compressor_name, len, " HD");
2832 } else
2833 av_strlcatf(compressor_name, len, " EX");
2834
2835 av_strlcatf(compressor_name, len, " %d%c", track->par->height, interlaced ? 'i' : 'p');
2836
2837 av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
2838 }
2839}
2840
2841#if CONFIG_AVIF_MUXER
2842static int mov_write_ccst_tag(AVIOContext *pb)
2843{
2844 int64_t pos = avio_tell(pb);
2845 // Write sane defaults:
2846 // all_ref_pics_intra = 0 : all samples can use any type of reference.
2847 // intra_pred_used = 1 : intra prediction may or may not be used.
2848 // max_ref_per_pic = 15 : reserved value to indicate that any number of
2849 // reference images can be used.
2850 uint8_t ccstValue = (0 << 7) | /* all_ref_pics_intra */
2851 (1 << 6) | /* intra_pred_used */
2852 (15 << 2); /* max_ref_per_pic */
2853 avio_wb32(pb, 0); /* size */
2854 ffio_wfourcc(pb, "ccst");
2855 avio_wb32(pb, 0); /* Version & flags */
2856 avio_w8(pb, ccstValue);
2857 avio_wb24(pb, 0); /* reserved */
2858 return update_size(pb, pos);
2859}
2860
2861static int mov_write_aux_tag(AVIOContext *pb, const char *aux_type)
2862{
2863 int64_t pos = avio_tell(pb);
2864 avio_wb32(pb, 0); /* size */
2865 ffio_wfourcc(pb, aux_type);
2866 avio_wb32(pb, 0); /* Version & flags */
2867 avio_write(pb, "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\0", 44);
2868 return update_size(pb, pos);
2869}
2870#endif
2871
2873{
2874 int ret = AVERROR_BUG;
2875 int64_t pos = avio_tell(pb);
2876 const AVPacketSideData *sd;
2877 char compressor_name[32] = { 0 };
2878 int avid = 0;
2879
2880 int uncompressed_ycbcr = ((track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVY422)
2881 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_YUYV422)
2882 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_VYU444)
2883 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVA)
2884 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_V30XLE)
2885 || track->par->codec_id == AV_CODEC_ID_V210);
2886
2887 avio_wb32(pb, 0); /* size */
2888 if (mov->encryption_scheme != MOV_ENC_NONE) {
2889 ffio_wfourcc(pb, "encv");
2890 } else {
2891 avio_wl32(pb, track->tag); // store it byteswapped
2892 }
2893 avio_wb32(pb, 0); /* Reserved */
2894 avio_wb16(pb, 0); /* Reserved */
2895 avio_wb16(pb, 1); /* Data-reference index */
2896
2897 if (uncompressed_ycbcr) {
2898 avio_wb16(pb, 2); /* Codec stream version */
2899 } else {
2900 avio_wb16(pb, 0); /* Codec stream version */
2901 }
2902 avio_wb16(pb, 0); /* Codec stream revision (=0) */
2903 if (track->mode == MODE_MOV) {
2904 ffio_wfourcc(pb, "FFMP"); /* Vendor */
2905 if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO || uncompressed_ycbcr) {
2906 avio_wb32(pb, 0); /* Temporal Quality */
2907 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
2908 } else {
2909 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
2910 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
2911 }
2912 } else {
2913 ffio_fill(pb, 0, 3 * 4); /* Reserved */
2914 }
2915 avio_wb16(pb, track->par->width); /* Video width */
2916 avio_wb16(pb, track->height); /* Video height */
2917 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
2918 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
2919 avio_wb32(pb, 0); /* Data size (= 0) */
2920 avio_wb16(pb, 1); /* Frame count (= 1) */
2921
2922 find_compressor(compressor_name, 32, track);
2923 avio_w8(pb, strlen(compressor_name));
2924 avio_write(pb, compressor_name, 31);
2925
2926 if (track->mode == MODE_MOV && track->par->codec_id == AV_CODEC_ID_V210)
2927 avio_wb16(pb, 0x18);
2928 else if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
2929 avio_wb16(pb, track->par->bits_per_coded_sample |
2930 (track->par->format == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
2931 else
2932 avio_wb16(pb, 0x18); /* Reserved */
2933
2934 if (track->mode == MODE_MOV && track->par->format == AV_PIX_FMT_PAL8) {
2935 int pal_size, i;
2936 avio_wb16(pb, 0); /* Color table ID */
2937 avio_wb32(pb, 0); /* Color table seed */
2938 avio_wb16(pb, 0x8000); /* Color table flags */
2939 if (track->par->bits_per_coded_sample < 0 || track->par->bits_per_coded_sample > 8)
2940 return AVERROR(EINVAL);
2941 pal_size = 1 << track->par->bits_per_coded_sample;
2942 avio_wb16(pb, pal_size - 1); /* Color table size (zero-relative) */
2943 for (i = 0; i < pal_size; i++) {
2944 uint32_t rgb = track->palette[i];
2945 uint16_t r = (rgb >> 16) & 0xff;
2946 uint16_t g = (rgb >> 8) & 0xff;
2947 uint16_t b = rgb & 0xff;
2948 avio_wb16(pb, 0);
2949 avio_wb16(pb, (r << 8) | r);
2950 avio_wb16(pb, (g << 8) | g);
2951 avio_wb16(pb, (b << 8) | b);
2952 }
2953 } else
2954 avio_wb16(pb, 0xffff); /* Reserved */
2955
2956 if (track->tag == MKTAG('m','p','4','v'))
2957 mov_write_esds_tag(pb, track);
2958 else if (track->par->codec_id == AV_CODEC_ID_H263)
2960 else if (track->par->codec_id == AV_CODEC_ID_AVUI ||
2961 track->par->codec_id == AV_CODEC_ID_SVQ3) {
2962 mov_write_extradata_tag(pb, track);
2963 avio_wb32(pb, 0);
2964 } else if (track->par->codec_id == AV_CODEC_ID_DNXHD) {
2965 mov_write_avid_tag(pb, track);
2966 avid = 1;
2967 } else if (track->par->codec_id == AV_CODEC_ID_HEVC) {
2968 mov_write_hvcc_tag(mov->fc, pb, track);
2970 ret = mov_write_lhvc_tag(mov->fc, pb, track);
2971 if (ret < 0)
2972 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'lhvC' atom for multilayer stream.\n");
2973 }
2974 } else if (track->par->codec_id == AV_CODEC_ID_VVC)
2975 mov_write_vvcc_tag(pb, track);
2976 else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
2977 mov_write_avcc_tag(pb, track);
2978 if (track->mode == MODE_IPOD)
2980 }
2981 else if (track->par->codec_id ==AV_CODEC_ID_EVC) {
2982 mov_write_evcc_tag(pb, track);
2983 } else if (track->par->codec_id == AV_CODEC_ID_LCEVC) {
2984 mov_write_lvcc_tag(mov->fc, pb, track);
2985 } else if (track->par->codec_id ==AV_CODEC_ID_APV) {
2986 mov_write_apvc_tag(mov->fc, pb, track);
2987 } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
2988 mov_write_vpcc_tag(mov->fc, pb, track);
2989 } else if (track->par->codec_id == AV_CODEC_ID_AV1) {
2990 mov_write_av1c_tag(pb, track);
2991 } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->extradata_size[track->last_stsd_index] > 0)
2992 mov_write_dvc1_tag(pb, track);
2993 else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
2994 track->par->codec_id == AV_CODEC_ID_VP6A) {
2995 /* Don't write any potential extradata here - the cropping
2996 * is signalled via the normal width/height fields. */
2997 } else if (track->par->codec_id == AV_CODEC_ID_R10K) {
2998 if (track->par->codec_tag == MKTAG('R','1','0','k'))
2999 mov_write_dpxe_tag(pb, track);
3000 } else if (track->par->codec_id == AV_CODEC_ID_AVS3) {
3001 mov_write_av3c_tag(pb, track);
3002 } else if (track->extradata_size[track->last_stsd_index] > 0)
3003 mov_write_glbl_tag(pb, track);
3004
3005 if (track->par->codec_id != AV_CODEC_ID_H264 &&
3006 track->par->codec_id != AV_CODEC_ID_MPEG4 &&
3007 track->par->codec_id != AV_CODEC_ID_DNXHD) {
3008 int field_order = track->par->field_order;
3009
3010 if (field_order != AV_FIELD_UNKNOWN)
3011 mov_write_fiel_tag(pb, track, field_order);
3012 }
3013
3014 if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
3015 if (track->mode == MODE_MOV)
3016 mov_write_gama_tag(s, pb, track, mov->gamma);
3017 else
3018 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
3019 }
3020 if (track->mode == MODE_MOV || track->mode == MODE_MP4 || track->mode == MODE_AVIF) {
3021 int has_color_info = track->par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
3022 track->par->color_trc != AVCOL_TRC_UNSPECIFIED &&
3024 if (has_color_info || mov->flags & FF_MOV_FLAG_WRITE_COLR ||
3027 int prefer_icc = mov->flags & FF_MOV_FLAG_PREFER_ICC || !has_color_info;
3028 mov_write_colr_tag(pb, track, prefer_icc);
3029 }
3030 } else if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
3031 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4 or AVIF.\n");
3032 }
3033
3034 if (track->mode == MODE_MOV || track->mode == MODE_MP4) {
3035 mov_write_clli_tag(pb, track);
3036 mov_write_mdcv_tag(pb, track);
3037 mov_write_amve_tag(pb, track);
3038 }
3039
3040 if (track->mode == MODE_MP4) {
3044 const AVPacketSideData *spherical_mapping = av_packet_side_data_get(track->st->codecpar->coded_side_data,
3047 if (stereo_3d && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)
3048 mov_write_st3d_tag(s, pb, (AVStereo3D*)stereo_3d->data);
3049 else if (stereo_3d)
3050 av_log(s, AV_LOG_WARNING, "Writing supported 'st3d' metadata requires -strict unofficial.\n");
3051 if (spherical_mapping && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)
3052 mov_write_sv3d_tag(mov->fc, pb, (AVSphericalMapping*)spherical_mapping->data);
3053 else if (spherical_mapping)
3054 av_log(s, AV_LOG_WARNING, "Writing supported 'sv3d' metadata requires -strict unofficial.\n");
3055 }
3056
3057 if (track->mode == MODE_MOV || (track->mode == MODE_MP4 &&
3059 const AVStereo3D *stereo3d = NULL;
3060 const AVSphericalMapping *spherical_mapping = NULL;
3061
3065 if (sd)
3066 stereo3d = (AVStereo3D *)sd->data;
3067
3071 if (sd)
3072 spherical_mapping = (AVSphericalMapping *)sd->data;
3073
3074 if (stereo3d || spherical_mapping)
3075 mov_write_vexu_tag(s, pb, stereo3d, spherical_mapping);
3076 if (stereo3d)
3077 mov_write_hfov_tag(s, pb, stereo3d);
3078 }
3079
3080 if (track->mode == MODE_MP4) {
3084 if (dovi && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
3086 } else if (dovi) {
3087 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'dvcC'/'dvvC' box. Requires -strict unofficial.\n");
3088 }
3089
3093 if (hvce && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
3094 mov_write_hvce_tag(pb, hvce);
3095 } else if (hvce) {
3096 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'hvcE' box. Requires -strict unofficial.\n");
3097 }
3098 }
3099
3100 if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num) {
3101 mov_write_pasp_tag(pb, track);
3102 }
3103
3107 if (sd && sd->size >= sizeof(uint32_t) * 4) {
3108 uint64_t top = AV_RL32(sd->data + 0);
3109 uint64_t bottom = AV_RL32(sd->data + 4);
3110 uint64_t left = AV_RL32(sd->data + 8);
3111 uint64_t right = AV_RL32(sd->data + 12);
3112
3113 if ((left + right) >= track->par->width ||
3114 (top + bottom) >= track->height) {
3115 av_log(s, AV_LOG_ERROR, "Invalid cropping dimensions in stream side data\n");
3116 return AVERROR(EINVAL);
3117 }
3118 if (top || bottom || left || right)
3119 mov_write_clap_tag(pb, track, top, bottom, left, right);
3120 } else if (uncompressed_ycbcr)
3121 mov_write_clap_tag(pb, track, 0, 0, 0, 0);
3122
3123 if (mov->encryption_scheme != MOV_ENC_NONE) {
3125 }
3126
3127 if (mov->write_btrt &&
3128 ((ret = mov_write_btrt_tag(pb, track)) < 0))
3129 return ret;
3130
3131 /* extra padding for avid stsd */
3132 /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
3133 if (avid)
3134 avio_wb32(pb, 0);
3135
3136#if CONFIG_AVIF_MUXER
3137 if (track->mode == MODE_AVIF) {
3138 mov_write_ccst_tag(pb);
3139 if (mov->nb_streams > 0 && track == &mov->tracks[1])
3140 mov_write_aux_tag(pb, "auxi");
3141 }
3142#endif
3143
3144 return update_size(pb, pos);
3145}
3146
3148{
3149 int64_t pos = avio_tell(pb);
3150 avio_wb32(pb, 0); /* size */
3151 ffio_wfourcc(pb, "rtp ");
3152 avio_wb32(pb, 0); /* Reserved */
3153 avio_wb16(pb, 0); /* Reserved */
3154 avio_wb16(pb, 1); /* Data-reference index */
3155
3156 avio_wb16(pb, 1); /* Hint track version */
3157 avio_wb16(pb, 1); /* Highest compatible version */
3158 avio_wb32(pb, track->max_packet_size); /* Max packet size */
3159
3160 avio_wb32(pb, 12); /* size */
3161 ffio_wfourcc(pb, "tims");
3162 avio_wb32(pb, track->timescale);
3163
3164 return update_size(pb, pos);
3165}
3166
3167static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
3168{
3169 uint64_t str_size =strlen(reel_name);
3170 int64_t pos = avio_tell(pb);
3171
3172 if (str_size >= UINT16_MAX){
3173 av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
3174 avio_wb16(pb, 0);
3175 return AVERROR(EINVAL);
3176 }
3177
3178 avio_wb32(pb, 0); /* size */
3179 ffio_wfourcc(pb, "name"); /* Data format */
3180 avio_wb16(pb, str_size); /* string size */
3181 avio_wb16(pb, track->language); /* langcode */
3182 avio_write(pb, reel_name, str_size); /* reel name */
3183 return update_size(pb,pos);
3184}
3185
3187{
3188 int64_t pos = avio_tell(pb);
3189#if 1
3190 int frame_duration;
3191 int nb_frames;
3193
3194 if (!track->st->avg_frame_rate.num || !track->st->avg_frame_rate.den) {
3195 av_log(NULL, AV_LOG_ERROR, "avg_frame_rate not set for tmcd track.\n");
3196 return AVERROR(EINVAL);
3197 } else {
3198 frame_duration = av_rescale(track->timescale, track->st->avg_frame_rate.den, track->st->avg_frame_rate.num);
3199 nb_frames = ROUNDED_DIV(track->st->avg_frame_rate.num, track->st->avg_frame_rate.den);
3200 }
3201
3202 if (nb_frames > 255) {
3203 av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
3204 return AVERROR(EINVAL);
3205 }
3206
3207 avio_wb32(pb, 0); /* size */
3208 ffio_wfourcc(pb, "tmcd"); /* Data format */
3209 avio_wb32(pb, 0); /* Reserved */
3210 avio_wb32(pb, 1); /* Data reference index */
3211 avio_wb32(pb, 0); /* Flags */
3212 avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
3213 avio_wb32(pb, track->timescale); /* Timescale */
3214 avio_wb32(pb, frame_duration); /* Frame duration */
3215 avio_w8(pb, nb_frames); /* Number of frames */
3216 avio_w8(pb, 0); /* Reserved */
3217
3218 t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
3219 if (t && utf8len(t->value) && track->mode != MODE_MP4)
3221 else
3222 avio_wb16(pb, 0); /* zero size */
3223#else
3224
3225 avio_wb32(pb, 0); /* size */
3226 ffio_wfourcc(pb, "tmcd"); /* Data format */
3227 avio_wb32(pb, 0); /* Reserved */
3228 avio_wb32(pb, 1); /* Data reference index */
3229 if (track->par->extradata_size)
3230 avio_write(pb, track->par->extradata, track->par->extradata_size);
3231#endif
3232 return update_size(pb, pos);
3233}
3234
3235static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
3236{
3237 int64_t pos = avio_tell(pb);
3238 avio_wb32(pb, 0); /* size */
3239 ffio_wfourcc(pb, "gpmd");
3240 avio_wb32(pb, 0); /* Reserved */
3241 avio_wb16(pb, 0); /* Reserved */
3242 avio_wb16(pb, 1); /* Data-reference index */
3243 avio_wb32(pb, 0); /* Reserved */
3244 return update_size(pb, pos);
3245}
3246
3248{
3249 int64_t pos = avio_tell(pb);
3250 int ret = 0;
3251 avio_wb32(pb, 0); /* size */
3252 ffio_wfourcc(pb, "stsd");
3253 avio_wb32(pb, 0); /* version & flags */
3254 avio_wb32(pb, track->stsd_count);
3255
3256 int stsd_index_back = track->last_stsd_index;
3257 for (track->last_stsd_index = 0;
3258 track->last_stsd_index < track->stsd_count;
3259 track->last_stsd_index++) {
3260 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
3261 ret = mov_write_video_tag(s, pb, mov, track);
3262 else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
3263 ret = mov_write_audio_tag(s, pb, mov, track);
3264 else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
3265 ret = mov_write_subtitle_tag(s, pb, track);
3266 else if (track->par->codec_tag == MKTAG('r','t','p',' '))
3267 ret = mov_write_rtp_tag(pb, track);
3268 else if (track->par->codec_tag == MKTAG('t','m','c','d'))
3269 ret = mov_write_tmcd_tag(pb, track);
3270 else if (track->par->codec_tag == MKTAG('g','p','m','d'))
3271 ret = mov_write_gpmd_tag(pb, track);
3272
3273 if (ret < 0)
3274 return ret;
3275 }
3276
3277 track->last_stsd_index = stsd_index_back;
3278
3279 return update_size_and_version(pb, pos, track->entry_version);
3280}
3281
3283{
3284 MOVMuxContext *mov = s->priv_data;
3285 MOVCtts *ctts_entries;
3286 uint32_t entries = 0;
3287 uint32_t atom_size;
3288 int i;
3289
3290 ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
3291 if (!ctts_entries)
3292 return AVERROR(ENOMEM);
3293 ctts_entries[0].count = 1;
3294 ctts_entries[0].offset = track->cluster[0].cts;
3295 for (i = 1; i < track->entry; i++) {
3296 if (track->cluster[i].cts == ctts_entries[entries].offset) {
3297 ctts_entries[entries].count++; /* compress */
3298 } else {
3299 entries++;
3300 ctts_entries[entries].offset = track->cluster[i].cts;
3301 ctts_entries[entries].count = 1;
3302 }
3303 }
3304 entries++; /* last one */
3305 atom_size = 16 + (entries * 8);
3306 avio_wb32(pb, atom_size); /* size */
3307 ffio_wfourcc(pb, "ctts");
3309 avio_w8(pb, 1); /* version */
3310 else
3311 avio_w8(pb, 0); /* version */
3312 avio_wb24(pb, 0); /* flags */
3313 avio_wb32(pb, entries); /* entry count */
3314 for (i = 0; i < entries; i++) {
3315 avio_wb32(pb, ctts_entries[i].count);
3316 avio_wb32(pb, ctts_entries[i].offset);
3317 }
3318 av_free(ctts_entries);
3319 return atom_size;
3320}
3321
3322/* Time to sample atom */
3324{
3325 MOVStts *stts_entries = NULL;
3326 uint32_t entries = -1;
3327 uint32_t atom_size;
3328 int i;
3329
3330 if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
3331 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
3332 if (!stts_entries)
3333 return AVERROR(ENOMEM);
3334 stts_entries[0].count = track->sample_count;
3335 stts_entries[0].duration = 1;
3336 entries = 1;
3337 } else {
3338 if (track->entry) {
3339 stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
3340 if (!stts_entries)
3341 return AVERROR(ENOMEM);
3342 }
3343 for (i = 0; i < track->entry; i++) {
3344 int duration = get_cluster_duration(track, i);
3345#if CONFIG_IAMFENC
3346 if (track->iamf && track->par->codec_id == AV_CODEC_ID_OPUS)
3347 duration = av_rescale(duration, 48000, track->par->sample_rate);
3348#endif
3349 if (i && duration == stts_entries[entries].duration) {
3350 stts_entries[entries].count++; /* compress */
3351 } else {
3352 entries++;
3353 stts_entries[entries].duration = duration;
3354 stts_entries[entries].count = 1;
3355 }
3356 }
3357 entries++; /* last one */
3358 }
3359 atom_size = 16 + (entries * 8);
3360 avio_wb32(pb, atom_size); /* size */
3361 ffio_wfourcc(pb, "stts");
3362 avio_wb32(pb, 0); /* version & flags */
3363 avio_wb32(pb, entries); /* entry count */
3364 for (i = 0; i < entries; i++) {
3365 avio_wb32(pb, stts_entries[i].count);
3366 avio_wb32(pb, stts_entries[i].duration);
3367 }
3368 av_free(stts_entries);
3369 return atom_size;
3370}
3371
3373{
3374 avio_wb32(pb, 28); /* size */
3375 ffio_wfourcc(pb, "dref");
3376 avio_wb32(pb, 0); /* version & flags */
3377 avio_wb32(pb, 1); /* entry count */
3378
3379 avio_wb32(pb, 0xc); /* size */
3380 //FIXME add the alis and rsrc atom
3381 ffio_wfourcc(pb, "url ");
3382 avio_wb32(pb, 1); /* version & flags */
3383
3384 return 28;
3385}
3386
3388{
3389 struct sgpd_entry {
3390 int count;
3391 int16_t roll_distance;
3392 int group_description_index;
3393 };
3394
3395 struct sgpd_entry *sgpd_entries = NULL;
3396 int entries = -1;
3397 int group = 0;
3398 int i, j;
3399
3400 const int OPUS_SEEK_PREROLL_MS = 80;
3401 int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
3402 (AVRational){1, 1000},
3403 (AVRational){1, 48000});
3404
3405 if (!track->entry)
3406 return 0;
3407
3408 sgpd_entries = av_malloc_array(track->entry, sizeof(*sgpd_entries));
3409 if (!sgpd_entries)
3410 return AVERROR(ENOMEM);
3411
3413
3414 if (track->par->codec_id == AV_CODEC_ID_OPUS) {
3415 for (i = 0; i < track->entry; i++) {
3416 int roll_samples_remaining = roll_samples;
3417 int distance = 0;
3418 for (j = i - 1; j >= 0; j--) {
3419 roll_samples_remaining -= get_cluster_duration(track, j);
3420 distance++;
3421 if (roll_samples_remaining <= 0)
3422 break;
3423 }
3424 /* We don't have enough preceding samples to compute a valid
3425 roll_distance here, so this sample can't be independently
3426 decoded. */
3427 if (roll_samples_remaining > 0)
3428 distance = 0;
3429 /* Verify distance is a maximum of 32 (2.5ms) packets. */
3430 if (distance > 32) {
3431 av_freep(&sgpd_entries);
3432 return AVERROR_INVALIDDATA;
3433 }
3434 if (i && distance == sgpd_entries[entries].roll_distance) {
3435 sgpd_entries[entries].count++;
3436 } else {
3437 entries++;
3438 sgpd_entries[entries].count = 1;
3439 sgpd_entries[entries].roll_distance = distance;
3440 sgpd_entries[entries].group_description_index = distance ? ++group : 0;
3441 }
3442 }
3443 } else {
3444 entries++;
3445 sgpd_entries[entries].count = track->sample_count;
3446 sgpd_entries[entries].roll_distance = 1;
3447 sgpd_entries[entries].group_description_index = ++group;
3448 }
3449 entries++;
3450
3451 if (!group) {
3452 av_free(sgpd_entries);
3453 return 0;
3454 }
3455
3456 /* Write sgpd tag */
3457 avio_wb32(pb, 24 + (group * 2)); /* size */
3458 ffio_wfourcc(pb, "sgpd");
3459 avio_wb32(pb, 1 << 24); /* fullbox */
3460 ffio_wfourcc(pb, "roll");
3461 avio_wb32(pb, 2); /* default_length */
3462 avio_wb32(pb, group); /* entry_count */
3463 for (i = 0; i < entries; i++) {
3464 if (sgpd_entries[i].group_description_index) {
3465 avio_wb16(pb, -sgpd_entries[i].roll_distance); /* roll_distance */
3466 }
3467 }
3468
3469 /* Write sbgp tag */
3470 avio_wb32(pb, 20 + (entries * 8)); /* size */
3471 ffio_wfourcc(pb, "sbgp");
3472 avio_wb32(pb, 0); /* fullbox */
3473 ffio_wfourcc(pb, "roll");
3474 avio_wb32(pb, entries); /* entry_count */
3475 for (i = 0; i < entries; i++) {
3476 avio_wb32(pb, sgpd_entries[i].count); /* sample_count */
3477 avio_wb32(pb, sgpd_entries[i].group_description_index); /* group_description_index */
3478 }
3479
3480 av_free(sgpd_entries);
3481 return 0;
3482}
3483
3485{
3486 int64_t pos = avio_tell(pb);
3487 int ret = 0;
3488
3489 avio_wb32(pb, 0); /* size */
3490 ffio_wfourcc(pb, "stbl");
3491 if ((ret = mov_write_stsd_tag(s, pb, mov, track)) < 0)
3492 return ret;
3493 mov_write_stts_tag(pb, track);
3494 if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
3495 track->par->codec_id == AV_CODEC_ID_TRUEHD ||
3497 (track->par->codec_id == AV_CODEC_ID_AAC && track->par->profile == AV_PROFILE_AAC_USAC) ||
3498 track->par->codec_tag == MKTAG('r','t','p',' ')) &&
3499 track->has_keyframes && track->has_keyframes < track->entry)
3501 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && track->has_disposable && track->entry)
3502 mov_write_sdtp_tag(pb, track);
3503 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
3505 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
3506 track->flags & MOV_TRACK_CTTS && track->entry) {
3507
3508 if ((ret = mov_write_ctts_tag(s, pb, track)) < 0)
3509 return ret;
3510 }
3511 mov_write_stsc_tag(pb, track);
3512 mov_write_stsz_tag(pb, track);
3513 mov_write_stco_tag(pb, track);
3514 if (track->cenc.aes_ctr && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3515 ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, 0);
3516 }
3517 if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
3519 }
3520 return update_size(pb, pos);
3521}
3522
3524{
3525 int64_t pos = avio_tell(pb);
3526 avio_wb32(pb, 0); /* size */
3527 ffio_wfourcc(pb, "dinf");
3529 return update_size(pb, pos);
3530}
3531
3533{
3534 avio_wb32(pb, 12);
3535 ffio_wfourcc(pb, "nmhd");
3536 avio_wb32(pb, 0);
3537 return 12;
3538}
3539
3541{
3542 avio_wb32(pb, 12);
3543 ffio_wfourcc(pb, "sthd");
3544 avio_wb32(pb, 0);
3545 return 12;
3546}
3547
3549{
3550 int64_t pos = avio_tell(pb);
3551 const char *font = "Lucida Grande";
3552 avio_wb32(pb, 0); /* size */
3553 ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
3554 avio_wb32(pb, 0); /* version & flags */
3555 avio_wb16(pb, 0); /* text font */
3556 avio_wb16(pb, 0); /* text face */
3557 avio_wb16(pb, 12); /* text size */
3558 avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
3559 avio_wb16(pb, 0x0000); /* text color (red) */
3560 avio_wb16(pb, 0x0000); /* text color (green) */
3561 avio_wb16(pb, 0x0000); /* text color (blue) */
3562 avio_wb16(pb, 0xffff); /* background color (red) */
3563 avio_wb16(pb, 0xffff); /* background color (green) */
3564 avio_wb16(pb, 0xffff); /* background color (blue) */
3565 avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
3566 avio_write(pb, font, strlen(font)); /* font name */
3567 return update_size(pb, pos);
3568}
3569
3571{
3572 int64_t pos = avio_tell(pb);
3573 avio_wb32(pb, 0); /* size */
3574 ffio_wfourcc(pb, "gmhd");
3575 avio_wb32(pb, 0x18); /* gmin size */
3576 ffio_wfourcc(pb, "gmin");/* generic media info */
3577 avio_wb32(pb, 0); /* version & flags */
3578 avio_wb16(pb, 0x40); /* graphics mode = */
3579 avio_wb16(pb, 0x8000); /* opColor (r?) */
3580 avio_wb16(pb, 0x8000); /* opColor (g?) */
3581 avio_wb16(pb, 0x8000); /* opColor (b?) */
3582 avio_wb16(pb, 0); /* balance */
3583 avio_wb16(pb, 0); /* reserved */
3584
3585 /*
3586 * This special text atom is required for
3587 * Apple Quicktime chapters. The contents
3588 * don't appear to be documented, so the
3589 * bytes are copied verbatim.
3590 */
3591 if (track->tag != MKTAG('c','6','0','8')) {
3592 avio_wb32(pb, 0x2C); /* size */
3593 ffio_wfourcc(pb, "text");
3594 avio_wb16(pb, 0x01);
3595 avio_wb32(pb, 0x00);
3596 avio_wb32(pb, 0x00);
3597 avio_wb32(pb, 0x00);
3598 avio_wb32(pb, 0x01);
3599 avio_wb32(pb, 0x00);
3600 avio_wb32(pb, 0x00);
3601 avio_wb32(pb, 0x00);
3602 avio_wb32(pb, 0x00004000);
3603 avio_wb16(pb, 0x0000);
3604 }
3605
3606 if (track->par->codec_tag == MKTAG('t','m','c','d')) {
3607 int64_t tmcd_pos = avio_tell(pb);
3608 avio_wb32(pb, 0); /* size */
3609 ffio_wfourcc(pb, "tmcd");
3610 mov_write_tcmi_tag(pb, track);
3611 update_size(pb, tmcd_pos);
3612 } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
3613 int64_t gpmd_pos = avio_tell(pb);
3614 avio_wb32(pb, 0); /* size */
3615 ffio_wfourcc(pb, "gpmd");
3616 avio_wb32(pb, 0); /* version */
3617 update_size(pb, gpmd_pos);
3618 }
3619 return update_size(pb, pos);
3620}
3621
3623{
3624 avio_wb32(pb, 16); /* size */
3625 ffio_wfourcc(pb, "smhd");
3626 avio_wb32(pb, 0); /* version & flags */
3627 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
3628 avio_wb16(pb, 0); /* reserved */
3629 return 16;
3630}
3631
3632static uint16_t mov_graphics_mode(MOVTrack *track)
3633{
3634 if (track->mode != MODE_MOV)
3635 return 0; /* ISO/IEC 14496-12 doesn't define any other modes */
3636
3637 switch (track->par->alpha_mode) {
3638 default: return MOV_GRAPHICS_MODE_COPY;
3641 }
3642}
3643
3645{
3646 avio_wb32(pb, 0x14); /* size (always 0x14) */
3647 ffio_wfourcc(pb, "vmhd");
3648 avio_wb32(pb, 0x01); /* version & flags */
3649 avio_wb16(pb, mov_graphics_mode(track));
3650 for (int i = 0; i < 3; i++)
3651 avio_wb16(pb, 0); /* opcolor */
3652 return 0x14;
3653}
3654
3655static int is_clcp_track(MOVTrack *track)
3656{
3657 return track->tag == MKTAG('c','7','0','8') ||
3658 track->tag == MKTAG('c','6','0','8');
3659}
3660
3662{
3663 MOVMuxContext *mov = s->priv_data;
3664 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
3665 int64_t pos = avio_tell(pb);
3666 size_t descr_len;
3667
3668 hdlr = "dhlr";
3669 hdlr_type = "url ";
3670 descr = "DataHandler";
3671
3672 if (track) {
3673 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
3674 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3675 if (track->mode == MODE_AVIF) {
3676 hdlr_type = (track == &mov->tracks[0]) ? "pict" : "auxv";
3677 descr = "PictureHandler";
3678 } else {
3679 hdlr_type = "vide";
3680 descr = "VideoHandler";
3681 }
3682 } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3683 hdlr_type = "soun";
3684 descr = "SoundHandler";
3685 } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3686 if (is_clcp_track(track)) {
3687 hdlr_type = "clcp";
3688 descr = "ClosedCaptionHandler";
3689 } else {
3690 if (track->tag == MKTAG('t','x','3','g')) {
3691 hdlr_type = "sbtl";
3692 } else if (track->tag == MKTAG('m','p','4','s')) {
3693 hdlr_type = "subp";
3694 } else if (track->tag == MOV_MP4_TTML_TAG) {
3695 hdlr_type = "subt";
3696 } else {
3697 hdlr_type = "text";
3698 }
3699 descr = "SubtitleHandler";
3700 }
3701 } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
3702 hdlr_type = "hint";
3703 descr = "HintHandler";
3704 } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
3705 hdlr_type = "tmcd";
3706 descr = "TimeCodeHandler";
3707 } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
3708 hdlr_type = "meta";
3709 descr = "GoPro MET"; // GoPro Metadata
3710 } else {
3712 "Unknown hdlr_type for %s, writing dummy values\n",
3713 av_fourcc2str(track->par->codec_tag));
3714 }
3715 if (track->st) {
3716 // hdlr.name is used by some players to identify the content title
3717 // of the track. So if an alternate handler description is
3718 // specified, use it.
3720 t = av_dict_get(track->st->metadata, "handler_name", NULL, 0);
3721 if (t && utf8len(t->value))
3722 descr = t->value;
3723 }
3724 }
3725
3726 if (mov->empty_hdlr_name) /* expressly allowed by QTFF and not prohibited in ISO 14496-12 8.4.3.3 */
3727 descr = "";
3728
3729 avio_wb32(pb, 0); /* size */
3730 ffio_wfourcc(pb, "hdlr");
3731 avio_wb32(pb, 0); /* Version & flags */
3732 avio_write(pb, hdlr, 4); /* handler */
3733 ffio_wfourcc(pb, hdlr_type); /* handler type */
3734 avio_wb32(pb, 0); /* reserved */
3735 avio_wb32(pb, 0); /* reserved */
3736 avio_wb32(pb, 0); /* reserved */
3737 descr_len = strlen(descr);
3738 if (!track || track->mode == MODE_MOV)
3739 avio_w8(pb, descr_len); /* pascal string */
3740 avio_write(pb, descr, descr_len); /* handler description */
3741 if (track && track->mode != MODE_MOV)
3742 avio_w8(pb, 0); /* c string */
3743 return update_size(pb, pos);
3744}
3745
3746#if CONFIG_AVIF_MUXER
3747static int mov_write_pitm_tag(AVIOContext *pb, int item_id)
3748{
3749 int64_t pos = avio_tell(pb);
3750 avio_wb32(pb, 0); /* size */
3751 ffio_wfourcc(pb, "pitm");
3752 avio_wb32(pb, 0); /* Version & flags */
3753 avio_wb16(pb, item_id); /* item_id */
3754 return update_size(pb, pos);
3755}
3756
3757static int mov_write_iloc_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3758{
3759 int64_t pos = avio_tell(pb);
3760 avio_wb32(pb, 0); /* size */
3761 ffio_wfourcc(pb, "iloc");
3762 avio_wb32(pb, 0); /* Version & flags */
3763 avio_w8(pb, (4 << 4) + 4); /* offset_size(4) and length_size(4) */
3764 avio_w8(pb, 0); /* base_offset_size(4) and reserved(4) */
3765 avio_wb16(pb, mov->nb_streams); /* item_count */
3766
3767 for (int i = 0; i < mov->nb_streams; i++) {
3768 avio_wb16(pb, i + 1); /* item_id */
3769 avio_wb16(pb, 0); /* data_reference_index */
3770 avio_wb16(pb, 1); /* extent_count */
3771 mov->avif_extent_pos[i] = avio_tell(pb);
3772 avio_wb32(pb, 0); /* extent_offset (written later) */
3773 // For animated AVIF, we simply write the first packet's size.
3774 avio_wb32(pb, mov->avif_extent_length[i]); /* extent_length */
3775 }
3776
3777 return update_size(pb, pos);
3778}
3779
3780static int mov_write_iinf_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3781{
3782 int64_t iinf_pos = avio_tell(pb);
3783 avio_wb32(pb, 0); /* size */
3784 ffio_wfourcc(pb, "iinf");
3785 avio_wb32(pb, 0); /* Version & flags */
3786 avio_wb16(pb, mov->nb_streams); /* entry_count */
3787
3788 av_assert2(mov->nb_streams <= 2);
3789 for (int i = 0; i < mov->nb_streams; i++) {
3790 int64_t infe_pos = avio_tell(pb);
3791 avio_wb32(pb, 0); /* size */
3792 ffio_wfourcc(pb, "infe");
3793 avio_w8(pb, 0x2); /* Version */
3794 avio_wb24(pb, !!i); /* flags (0x1 == hidden) */
3795 avio_wb16(pb, i + 1); /* item_id */
3796 avio_wb16(pb, 0); /* item_protection_index */
3797 avio_write(pb, "av01", 4); /* item_type */
3798 avio_write(pb, !i ? "Color\0" : "Alpha\0", 6); /* item_name */
3799 update_size(pb, infe_pos);
3800 }
3801
3802 return update_size(pb, iinf_pos);
3803}
3804
3805
3806static int mov_write_iref_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3807{
3808 int64_t auxl_pos;
3809 int64_t iref_pos = avio_tell(pb);
3810 avio_wb32(pb, 0); /* size */
3811 ffio_wfourcc(pb, "iref");
3812 avio_wb32(pb, 0); /* Version & flags */
3813
3814 auxl_pos = avio_tell(pb);
3815 avio_wb32(pb, 0); /* size */
3816 ffio_wfourcc(pb, "auxl");
3817 avio_wb16(pb, 2); /* from_item_ID */
3818 avio_wb16(pb, 1); /* reference_count */
3819 avio_wb16(pb, 1); /* to_item_ID */
3820 update_size(pb, auxl_pos);
3821
3822 return update_size(pb, iref_pos);
3823}
3824
3825static int mov_write_ispe_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s,
3826 int stream_index)
3827{
3828 int64_t pos = avio_tell(pb);
3829 avio_wb32(pb, 0); /* size */
3830 ffio_wfourcc(pb, "ispe");
3831 avio_wb32(pb, 0); /* Version & flags */
3832 avio_wb32(pb, s->streams[stream_index]->codecpar->width); /* image_width */
3833 avio_wb32(pb, s->streams[stream_index]->codecpar->height); /* image_height */
3834 return update_size(pb, pos);
3835}
3836
3837static int mov_write_pixi_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s,
3838 int stream_index)
3839{
3840 MOVTrack *trk = &mov->tracks[stream_index];
3841 int64_t pos = avio_tell(pb);
3842 const AVPixFmtDescriptor *pixdesc =
3843 av_pix_fmt_desc_get(s->streams[stream_index]->codecpar->format);
3844 int depth, nb_components;
3845 if (pixdesc->nb_components > 0) {
3846 depth = pixdesc->comp[0].depth;
3847 nb_components = pixdesc->nb_components;
3848 } else {
3851 trk->extradata_size[trk->last_stsd_index]);
3852 if (ret < 0)
3853 return ret;
3854 depth = seq.bitdepth;
3855 nb_components = seq.monochrome ? 1 : 3;
3856 }
3857 avio_wb32(pb, 0); /* size */
3858 ffio_wfourcc(pb, "pixi");
3859 avio_wb32(pb, 0); /* Version & flags */
3860 avio_w8(pb, nb_components); /* num_channels */
3861 for (int i = 0; i < nb_components; ++i)
3862 avio_w8(pb, depth); /* bits_per_channel */
3863 return update_size(pb, pos);
3864}
3865
3866static int mov_write_ipco_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3867{
3868 int64_t pos = avio_tell(pb);
3869 avio_wb32(pb, 0); /* size */
3870 ffio_wfourcc(pb, "ipco");
3871 for (int i = 0; i < mov->nb_streams; i++) {
3872 mov_write_ispe_tag(pb, mov, s, i);
3873 mov_write_pixi_tag(pb, mov, s, i);
3874 mov_write_av1c_tag(pb, &mov->tracks[i]);
3875 if (!i)
3876 mov_write_colr_tag(pb, &mov->tracks[0], 0);
3877 else
3878 mov_write_aux_tag(pb, "auxC");
3879 }
3880 return update_size(pb, pos);
3881}
3882
3883static int mov_write_ipma_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3884{
3885 int64_t pos = avio_tell(pb);
3886 avio_wb32(pb, 0); /* size */
3887 ffio_wfourcc(pb, "ipma");
3888 avio_wb32(pb, 0); /* Version & flags */
3889 avio_wb32(pb, mov->nb_streams); /* entry_count */
3890
3891 for (int i = 0, index = 1; i < mov->nb_streams; i++) {
3892 avio_wb16(pb, i + 1); /* item_ID */
3893 avio_w8(pb, 4); /* association_count */
3894
3895 // ispe association.
3896 avio_w8(pb, index++); /* essential and property_index */
3897 // pixi association.
3898 avio_w8(pb, index++); /* essential and property_index */
3899 // av1C association.
3900 avio_w8(pb, 0x80 | index++); /* essential and property_index */
3901 // colr/auxC association.
3902 avio_w8(pb, index++); /* essential and property_index */
3903 }
3904 return update_size(pb, pos);
3905}
3906
3907static int mov_write_iprp_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3908{
3909 int64_t pos = avio_tell(pb);
3910 avio_wb32(pb, 0); /* size */
3911 ffio_wfourcc(pb, "iprp");
3912 mov_write_ipco_tag(pb, mov, s);
3913 mov_write_ipma_tag(pb, mov, s);
3914 return update_size(pb, pos);
3915}
3916#endif
3917
3919{
3920 /* This atom must be present, but leaving the values at zero
3921 * seems harmless. */
3922 avio_wb32(pb, 28); /* size */
3923 ffio_wfourcc(pb, "hmhd");
3924 avio_wb32(pb, 0); /* version, flags */
3925 avio_wb16(pb, 0); /* maxPDUsize */
3926 avio_wb16(pb, 0); /* avgPDUsize */
3927 avio_wb32(pb, 0); /* maxbitrate */
3928 avio_wb32(pb, 0); /* avgbitrate */
3929 avio_wb32(pb, 0); /* reserved */
3930 return 28;
3931}
3932
3934{
3935 int64_t pos = avio_tell(pb);
3936 int ret;
3937
3938 avio_wb32(pb, 0); /* size */
3939 ffio_wfourcc(pb, "minf");
3940 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
3941 mov_write_vmhd_tag(pb, track);
3942 else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
3944 else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3945 if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
3946 mov_write_gmhd_tag(pb, track);
3947 } else if (track->tag == MOV_MP4_TTML_TAG) {
3949 } else {
3951 }
3952 } else if (track->tag == MKTAG('r','t','p',' ')) {
3954 } else if (track->tag == MKTAG('t','m','c','d')) {
3955 if (track->mode != MODE_MOV)
3957 else
3958 mov_write_gmhd_tag(pb, track);
3959 } else if (track->tag == MKTAG('g','p','m','d')) {
3960 mov_write_gmhd_tag(pb, track);
3961 }
3962 if (track->mode == MODE_MOV) /* ISO 14496-12 8.4.3.1 specifies hdlr only within mdia or meta boxes */
3965 if ((ret = mov_write_stbl_tag(s, pb, mov, track)) < 0)
3966 return ret;
3967 return update_size(pb, pos);
3968}
3969
3970static void get_pts_range(MOVMuxContext *mov, MOVTrack *track,
3971 int64_t *start, int64_t *end, int elst, int mdhd)
3972{
3973 if (track->tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd && track->nb_src_track) {
3974 // tmcd tracks gets track_duration set in mov_write_moov_tag from
3975 // another track's duration, while the end_pts may be left at zero.
3976 // Calculate the pts duration for that track instead.
3977 get_pts_range(mov, &mov->tracks[*track->src_track], start, end, elst, mdhd);
3978 *start = av_rescale(*start, track->timescale,
3979 mov->tracks[*track->src_track].timescale);
3980 *end = av_rescale(*end, track->timescale,
3981 mov->tracks[*track->src_track].timescale);
3982 return;
3983 }
3984 if (!mdhd && track->end_pts != AV_NOPTS_VALUE &&
3985 track->start_dts != AV_NOPTS_VALUE &&
3986 track->start_cts != AV_NOPTS_VALUE) {
3987 *start = track->start_dts + track->start_cts;
3988 *end = elst ? track->elst_end_pts : track->end_pts;
3989 return;
3990 }
3991 *start = 0;
3992 *end = track->track_duration;
3993}
3994
3996{
3997 int64_t start, end;
3998 get_pts_range(mov, track, &start, &end, 0, 1);
3999 return end - start;
4000}
4001
4002// Calculate the actual duration of the track, after edits.
4003// If it starts with a pts < 0, that is removed by the edit list.
4004// If it starts with a pts > 0, the edit list adds a delay before that.
4005// Thus, with edit lists enabled, the post-edit output of the file is
4006// starting with pts=0.
4008{
4009 int64_t start, end;
4010 get_pts_range(mov, track, &start, &end, 0, 0);
4011 if (mov->use_editlist != 0)
4012 start = 0;
4013 return end - start;
4014}
4015
4017{
4018 int64_t start, end;
4019 get_pts_range(mov, track, &start, &end, 1, 0);
4020 return end - start;
4021}
4022
4024{
4025 if (track && track->mode == MODE_ISM)
4026 return 1;
4027 if (duration < INT32_MAX)
4028 return 0;
4029 return 1;
4030}
4031
4033 MOVTrack *track)
4034{
4036 int version = mov_mdhd_mvhd_tkhd_version(mov, track, duration);
4037
4038 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
4039 ffio_wfourcc(pb, "mdhd");
4040 avio_w8(pb, version);
4041 avio_wb24(pb, 0); /* flags */
4042 if (version == 1) {
4043 avio_wb64(pb, track->time);
4044 avio_wb64(pb, track->time);
4045 } else {
4046 avio_wb32(pb, track->time); /* creation time */
4047 avio_wb32(pb, track->time); /* modification time */
4048 }
4049 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
4050 if (!track->entry && mov->mode == MODE_ISM)
4051 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
4052 else if (!track->entry)
4053 (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
4054 else
4055 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration); /* duration */
4056 avio_wb16(pb, track->language); /* language */
4057 avio_wb16(pb, 0); /* reserved (quality) */
4058
4059 if (version != 0 && track->mode == MODE_MOV) {
4061 "FATAL error, file duration too long for timebase, this file will not be\n"
4062 "playable with QuickTime. Choose a different timebase with "
4063 "-video_track_timescale or a different container format\n");
4064 }
4065
4066 return 32;
4067}
4068
4070 MOVMuxContext *mov, MOVTrack *track)
4071{
4072 int64_t pos = avio_tell(pb);
4073 int ret;
4074
4075 avio_wb32(pb, 0); /* size */
4076 ffio_wfourcc(pb, "mdia");
4077 mov_write_mdhd_tag(pb, mov, track);
4078 mov_write_hdlr_tag(s, pb, track);
4079 if ((ret = mov_write_minf_tag(s, pb, mov, track)) < 0)
4080 return ret;
4081 return update_size(pb, pos);
4082}
4083
4084/* transformation matrix
4085 |a b u|
4086 |c d v|
4087 |tx ty w| */
4088static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
4089 int16_t d, int16_t tx, int16_t ty)
4090{
4091 avio_wb32(pb, a << 16); /* 16.16 format */
4092 avio_wb32(pb, b << 16); /* 16.16 format */
4093 avio_wb32(pb, 0); /* u in 2.30 format */
4094 avio_wb32(pb, c << 16); /* 16.16 format */
4095 avio_wb32(pb, d << 16); /* 16.16 format */
4096 avio_wb32(pb, 0); /* v in 2.30 format */
4097 avio_wb32(pb, tx << 16); /* 16.16 format */
4098 avio_wb32(pb, ty << 16); /* 16.16 format */
4099 avio_wb32(pb, 1 << 30); /* w in 2.30 format */
4100}
4101
4103 MOVTrack *track, AVStream *st)
4104{
4106 mov->movie_timescale, track->timescale,
4107 AV_ROUND_UP);
4108 int version;
4110 int group = 0;
4111
4112 uint32_t *display_matrix = NULL;
4113 int i;
4114
4115#if CONFIG_AVIF_MUXER
4116 if (mov->mode == MODE_AVIF)
4117 if (!mov->avif_loop_count)
4118 duration = INT64_MAX;
4119 else
4120 duration *= mov->avif_loop_count;
4121#endif
4122
4123 if (st) {
4124 const AVPacketSideData *sd;
4125 if (mov->per_stream_grouping)
4126 group = st->index;
4127 else
4128 group = st->codecpar->codec_type;
4129
4133 if (sd && sd->size == 9 * sizeof(*display_matrix))
4134 display_matrix = (uint32_t *)sd->data;
4135 }
4136
4137 if (track->flags & MOV_TRACK_ENABLED)
4139
4141
4142 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
4143 ffio_wfourcc(pb, "tkhd");
4144 avio_w8(pb, version);
4145 avio_wb24(pb, flags);
4146 if (version == 1) {
4147 avio_wb64(pb, track->time);
4148 avio_wb64(pb, track->time);
4149 } else {
4150 avio_wb32(pb, track->time); /* creation time */
4151 avio_wb32(pb, track->time); /* modification time */
4152 }
4153 avio_wb32(pb, track->track_id); /* track-id */
4154 avio_wb32(pb, 0); /* reserved */
4155 if (!track->entry && mov->mode == MODE_ISM)
4156 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
4157 else if (!track->entry)
4158 (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
4159 else
4160 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
4161
4162 avio_wb32(pb, 0); /* reserved */
4163 avio_wb32(pb, 0); /* reserved */
4164 avio_wb16(pb, 0); /* layer */
4165 avio_wb16(pb, group); /* alternate group) */
4166 /* Volume, only for audio */
4167 if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
4168 avio_wb16(pb, 0x0100);
4169 else
4170 avio_wb16(pb, 0);
4171 avio_wb16(pb, 0); /* reserved */
4172
4173 /* Matrix structure */
4174 if (display_matrix) {
4175 for (i = 0; i < 9; i++)
4176 avio_wb32(pb, display_matrix[i]);
4177 } else {
4178 write_matrix(pb, 1, 0, 0, 1, 0, 0);
4179 }
4180 /* Track width and height, for visual only */
4181 if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
4182 track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
4183 int64_t track_width_1616;
4184 if (track->mode == MODE_MOV || track->mode == MODE_AVIF) {
4185 track_width_1616 = track->par->width * 0x10000ULL;
4186 } else {
4187 track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
4188 track->par->width * 0x10000LL,
4190 if (!track_width_1616 ||
4191 track->height != track->par->height ||
4192 track_width_1616 > UINT32_MAX)
4193 track_width_1616 = track->par->width * 0x10000ULL;
4194 }
4195 if (track_width_1616 > UINT32_MAX) {
4196 av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");
4197 track_width_1616 = 0;
4198 }
4199 avio_wb32(pb, track_width_1616);
4200 if (track->height > 0xFFFF) {
4201 av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");
4202 avio_wb32(pb, 0);
4203 } else
4204 avio_wb32(pb, track->height * 0x10000U);
4205 } else {
4206 avio_wb32(pb, 0);
4207 avio_wb32(pb, 0);
4208 }
4209 return 0x5c;
4210}
4211
4213{
4215 track->par->sample_aspect_ratio.den);
4216
4217 int64_t pos = avio_tell(pb);
4218
4219 avio_wb32(pb, 0); /* size */
4220 ffio_wfourcc(pb, "tapt");
4221
4222 avio_wb32(pb, 20);
4223 ffio_wfourcc(pb, "clef");
4224 avio_wb32(pb, 0);
4225 avio_wb32(pb, width << 16);
4226 avio_wb32(pb, track->par->height << 16);
4227
4228 avio_wb32(pb, 20);
4229 ffio_wfourcc(pb, "prof");
4230 avio_wb32(pb, 0);
4231 avio_wb32(pb, width << 16);
4232 avio_wb32(pb, track->par->height << 16);
4233
4234 avio_wb32(pb, 20);
4235 ffio_wfourcc(pb, "enof");
4236 avio_wb32(pb, 0);
4237 avio_wb32(pb, track->par->width << 16);
4238 avio_wb32(pb, track->par->height << 16);
4239
4240 return update_size(pb, pos);
4241}
4242
4243// This box is written in the following cases:
4244// * Seems important for the psp playback. Without it the movie seems to hang.
4245// * Used for specifying the looping behavior of animated AVIF (as specified
4246// in Section 9.6 of the HEIF specification ISO/IEC 23008-12).
4248 MOVTrack *track)
4249{
4251 mov->movie_timescale, track->timescale,
4252 AV_ROUND_UP);
4253 int version = duration < INT32_MAX ? 0 : 1;
4254 int entry_size, entry_count, size;
4255 int64_t delay, start_ct = track->start_cts;
4256 int64_t start_dts = track->start_dts;
4257 int flags = 0;
4258
4259 if (track->entry) {
4260 if (start_dts != track->cluster[0].dts || (start_ct != track->cluster[0].cts && track->cluster[0].dts >= 0)) {
4261
4262 av_log(mov->fc, AV_LOG_DEBUG,
4263 "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
4264 track->cluster[0].dts, track->cluster[0].cts,
4265 start_dts, start_ct, track->track_id);
4266 start_dts = track->cluster[0].dts;
4267 start_ct = track->cluster[0].cts;
4268 }
4269 }
4270
4271 delay = av_rescale_rnd(start_dts + start_ct, mov->movie_timescale,
4272 track->timescale, AV_ROUND_DOWN);
4273
4274#if CONFIG_AVIF_MUXER
4275 if (mov->mode == MODE_AVIF) {
4276 delay = 0;
4277 // Section 9.6.3 of ISO/IEC 23008-12: flags specifies repetition of the
4278 // edit list as follows: (flags & 1) equal to 0 specifies that the edit
4279 // list is not repeated, while (flags & 1) equal to 1 specifies that the
4280 // edit list is repeated.
4281 flags = mov->avif_loop_count != 1;
4282 start_ct = 0;
4283 }
4284#endif
4285
4286 version |= delay < INT32_MAX ? 0 : 1;
4287
4288 entry_size = (version == 1) ? 20 : 12;
4289 entry_count = 1 + (delay > 0);
4290 size = 24 + entry_count * entry_size;
4291
4292 /* write the atom data */
4293 avio_wb32(pb, size);
4294 ffio_wfourcc(pb, "edts");
4295 avio_wb32(pb, size - 8);
4296 ffio_wfourcc(pb, "elst");
4297 avio_w8(pb, version);
4298 avio_wb24(pb, flags); /* flags */
4299
4300 avio_wb32(pb, entry_count);
4301 if (delay > 0) { /* add an empty edit to delay presentation */
4302 /* In the positive delay case, the delay includes the cts
4303 * offset, and the second edit list entry below trims out
4304 * the same amount from the actual content. This makes sure
4305 * that the offset last sample is included in the edit
4306 * list duration as well. */
4307 if (version == 1) {
4308 avio_wb64(pb, delay);
4309 avio_wb64(pb, -1);
4310 } else {
4311 avio_wb32(pb, delay);
4312 avio_wb32(pb, -1);
4313 }
4314 avio_wb32(pb, 0x00010000);
4315 } else if (mov->mode != MODE_AVIF) {
4316 /* Avoid accidentally ending up with start_ct = -1 which has got a
4317 * special meaning. Normally start_ct should end up positive or zero
4318 * here, but use FFMIN in case dts is a small positive integer
4319 * rounded to 0 when represented in movie timescale units. */
4320 av_assert0(av_rescale_rnd(start_dts, mov->movie_timescale, track->timescale, AV_ROUND_DOWN) <= 0);
4321 start_ct = -FFMIN(start_dts, 0);
4322
4323#if CONFIG_IAMFENC
4324 if (track->iamf && track->par->codec_id == AV_CODEC_ID_OPUS)
4325 start_ct = av_rescale(start_ct, 48000, track->par->sample_rate);
4326#endif
4327 /* Note, this delay is calculated from the pts of the first sample,
4328 * ensuring that we don't reduce the duration for cases with
4329 * dts<0 pts=0. */
4330 duration += delay;
4331 }
4332
4333 /* For fragmented files, we don't know the full length yet. Setting
4334 * duration to 0 allows us to only specify the offset, including
4335 * the rest of the content (from all future fragments) without specifying
4336 * an explicit duration.
4337 *
4338 * For hybrid_fragmented during mov_write_trailer (mov->moov_written != 0),
4339 * don't reset duration to zero.
4340 */
4341 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
4343 duration = 0;
4344
4345 /* duration */
4346 if (version == 1) {
4347 avio_wb64(pb, duration);
4348 avio_wb64(pb, start_ct);
4349 } else {
4350 avio_wb32(pb, duration);
4351 avio_wb32(pb, start_ct);
4352 }
4353 avio_wb32(pb, 0x00010000);
4354 return size;
4355}
4356
4358{
4359 int64_t pos = avio_tell(pb);
4360 avio_wb32(pb, 0); // size placeholder
4361 ffio_wfourcc(pb, "tref");
4362
4363 for (int i = 0; i < track->nb_tref_tags; i++) {
4364 MovTag *tag = &track->tref_tags[i];
4365 avio_wb32(pb, 8 + tag->nb_id * sizeof(*tag->id)); // size (subatom)
4366 avio_wl32(pb, tag->name);
4367 for (int j = 0; j < tag->nb_id; j++)
4368 avio_wb32(pb, tag->id[j]);
4369 }
4370 return update_size(pb, pos);
4371}
4372
4373// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
4375{
4376 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
4377 ffio_wfourcc(pb, "uuid");
4378 ffio_wfourcc(pb, "USMT");
4379 avio_wb32(pb, 0x21d24fce);
4380 avio_wb32(pb, 0xbb88695c);
4381 avio_wb32(pb, 0xfac9c740);
4382 avio_wb32(pb, 0x1c); // another size here!
4383 ffio_wfourcc(pb, "MTDT");
4384 avio_wb32(pb, 0x00010012);
4385 avio_wb32(pb, 0x0a);
4386 avio_wb32(pb, 0x55c40000);
4387 avio_wb32(pb, 0x1);
4388 avio_wb32(pb, 0x0);
4389 return 0x34;
4390}
4391
4393{
4394 AVFormatContext *ctx = track->rtp_ctx;
4395 char buf[1000] = "";
4396 int len;
4397
4398 av_assert0(track->nb_src_track);
4399 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], *track->src_track,
4400 NULL, NULL, 0, 0, ctx);
4401 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
4402 len = strlen(buf);
4403
4404 avio_wb32(pb, len + 24);
4405 ffio_wfourcc(pb, "udta");
4406 avio_wb32(pb, len + 16);
4407 ffio_wfourcc(pb, "hnti");
4408 avio_wb32(pb, len + 8);
4409 ffio_wfourcc(pb, "sdp ");
4410 avio_write(pb, buf, len);
4411 return len + 24;
4412}
4413
4415 const char *tag, const char *str)
4416{
4417 int64_t pos = avio_tell(pb);
4418 AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
4419 if (!t || !utf8len(t->value))
4420 return 0;
4421
4422 avio_wb32(pb, 0); /* size */
4423 ffio_wfourcc(pb, tag); /* type */
4424 avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
4425 return update_size(pb, pos);
4426}
4427
4428static int mov_write_track_kind(AVIOContext *pb, const char *scheme_uri,
4429 const char *value)
4430{
4431 int64_t pos = avio_tell(pb);
4432
4433 /* Box|FullBox basics */
4434 avio_wb32(pb, 0); /* size placeholder */
4435 ffio_wfourcc(pb, (const unsigned char *)"kind");
4436 avio_w8(pb, 0); /* version = 0 */
4437 avio_wb24(pb, 0); /* flags = 0 */
4438
4439 /* Required null-terminated scheme URI */
4440 avio_write(pb, (const unsigned char *)scheme_uri,
4441 strlen(scheme_uri));
4442 avio_w8(pb, 0);
4443
4444 /* Optional value string */
4445 if (value && value[0])
4446 avio_write(pb, (const unsigned char *)value,
4447 strlen(value));
4448
4449 avio_w8(pb, 0);
4450
4451 return update_size(pb, pos);
4452}
4453
4455{
4456 int ret = AVERROR_BUG;
4457
4458 for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
4460
4461 for (int j = 0; map.value_maps[j].disposition; j++) {
4462 const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
4463 if (!(st->disposition & value_map.disposition))
4464 continue;
4465
4466 if ((ret = mov_write_track_kind(pb, map.scheme_uri, value_map.value)) < 0)
4467 return ret;
4468 }
4469 }
4470
4471 return 0;
4472}
4473
4475 AVStream *st)
4476{
4477 AVIOContext *pb_buf;
4478 int ret, size;
4479 uint8_t *buf;
4480
4481 if (!st)
4482 return 0;
4483
4484 ret = avio_open_dyn_buf(&pb_buf);
4485 if (ret < 0)
4486 return ret;
4487
4488 if (mov->mode & (MODE_MP4|MODE_MOV))
4489 mov_write_track_metadata(pb_buf, st, "name", "title");
4490
4491 if (mov->mode & MODE_MP4) {
4492 if ((ret = mov_write_track_kinds(pb_buf, st)) < 0)
4493 goto end;
4494 }
4495
4496 if ((size = avio_get_dyn_buf(pb_buf, &buf)) > 0) {
4497 avio_wb32(pb, size + 8);
4498 ffio_wfourcc(pb, "udta");
4499 avio_write(pb, buf, size);
4500 }
4501end:
4502 ffio_free_dyn_buf(&pb_buf);
4503
4504 return ret;
4505}
4506
4508 MOVTrack *track, AVStream *st)
4509{
4510 int64_t pos = avio_tell(pb);
4511 int entry_backup = track->entry;
4512 int chunk_backup = track->chunkCount;
4513 int ret;
4514
4515 /* If we want to have an empty moov, but some samples already have been
4516 * buffered (delay_moov), pretend that no samples have been written yet. */
4517 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
4518 track->chunkCount = track->entry = 0;
4519
4520 avio_wb32(pb, 0); /* size */
4521 ffio_wfourcc(pb, "trak");
4522 mov_write_tkhd_tag(pb, mov, track, st);
4523
4524 av_assert2(mov->use_editlist >= 0);
4525
4526 if (track->start_dts != AV_NOPTS_VALUE) {
4527 if (mov->use_editlist)
4528 mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
4529 else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
4530 av_log(mov->fc, AV_LOG_WARNING,
4531 "Not writing any edit list even though one would have been required\n");
4532 }
4533
4534 if (mov->is_animated_avif)
4535 mov_write_edts_tag(pb, mov, track);
4536
4537 if (track->nb_tref_tags)
4538 mov_write_tref_tag(pb, track);
4539
4540 if ((ret = mov_write_mdia_tag(s, pb, mov, track)) < 0)
4541 return ret;
4542 if (track->mode == MODE_PSP)
4543 mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
4544 if (track->tag == MKTAG('r','t','p',' '))
4545 mov_write_udta_sdp(pb, track);
4546 if (track->mode == MODE_MOV) {
4547 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
4548 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
4549 if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
4550 mov_write_tapt_tag(pb, track);
4551 }
4552 }
4553 if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
4554 mov_write_tapt_tag(pb, track);
4555 }
4556 }
4557 mov_write_track_udta_tag(pb, mov, st);
4558 track->entry = entry_backup;
4559 track->chunkCount = chunk_backup;
4560 return update_size(pb, pos);
4561}
4562
4564{
4565 int i, has_audio = 0, has_video = 0;
4566 int64_t pos = avio_tell(pb);
4567 int audio_profile = mov->iods_audio_profile;
4568 int video_profile = mov->iods_video_profile;
4569 for (i = 0; i < mov->nb_tracks; i++) {
4570 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4571 has_audio |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_AUDIO;
4572 has_video |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_VIDEO;
4573 }
4574 }
4575 if (audio_profile < 0)
4576 audio_profile = 0xFF - has_audio;
4577 if (video_profile < 0)
4578 video_profile = 0xFF - has_video;
4579 avio_wb32(pb, 0x0); /* size */
4580 ffio_wfourcc(pb, "iods");
4581 avio_wb32(pb, 0); /* version & flags */
4582 put_descr(pb, 0x10, 7);
4583 avio_wb16(pb, 0x004f);
4584 avio_w8(pb, 0xff);
4585 avio_w8(pb, 0xff);
4586 avio_w8(pb, audio_profile);
4587 avio_w8(pb, video_profile);
4588 avio_w8(pb, 0xff);
4589 return update_size(pb, pos);
4590}
4591
4593{
4594 avio_wb32(pb, 0x20); /* size */
4595 ffio_wfourcc(pb, "trex");
4596 avio_wb32(pb, 0); /* version & flags */
4597 avio_wb32(pb, track->track_id); /* track ID */
4598 avio_wb32(pb, 1); /* default sample description index */
4599 avio_wb32(pb, 0); /* default sample duration */
4600 avio_wb32(pb, 0); /* default sample size */
4601 avio_wb32(pb, 0); /* default sample flags */
4602 return 0;
4603}
4604
4606{
4607 int64_t pos = avio_tell(pb);
4608 int i;
4609 avio_wb32(pb, 0x0); /* size */
4610 ffio_wfourcc(pb, "mvex");
4611 for (i = 0; i < mov->nb_tracks; i++)
4612 mov_write_trex_tag(pb, &mov->tracks[i]);
4613 return update_size(pb, pos);
4614}
4615
4617{
4618 int max_track_id = 1, i;
4619 int64_t max_track_len = 0;
4620 int version;
4621 int timescale;
4622
4623 for (i = 0; i < mov->nb_tracks; i++) {
4624 if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
4625 int64_t max_track_len_temp = av_rescale_rnd(
4626 calc_pts_duration(mov, &mov->tracks[i]),
4627 mov->movie_timescale,
4628 mov->tracks[i].timescale,
4629 AV_ROUND_UP);
4630 if (max_track_len < max_track_len_temp)
4631 max_track_len = max_track_len_temp;
4632 if (max_track_id < mov->tracks[i].track_id)
4633 max_track_id = mov->tracks[i].track_id;
4634 }
4635 }
4636 /* If using delay_moov, make sure the output is the same as if no
4637 * samples had been written yet. */
4638 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4639 max_track_len = 0;
4640 max_track_id = 1;
4641 }
4642
4643 version = mov_mdhd_mvhd_tkhd_version(mov, NULL, max_track_len);
4644 avio_wb32(pb, version == 1 ? 120 : 108); /* size */
4645
4646 ffio_wfourcc(pb, "mvhd");
4647 avio_w8(pb, version);
4648 avio_wb24(pb, 0); /* flags */
4649 if (version == 1) {
4650 avio_wb64(pb, mov->time);
4651 avio_wb64(pb, mov->time);
4652 } else {
4653 avio_wb32(pb, mov->time); /* creation time */
4654 avio_wb32(pb, mov->time); /* modification time */
4655 }
4656
4657 timescale = mov->movie_timescale;
4658 if (mov->mode == MODE_AVIF && !timescale)
4659 timescale = mov->tracks[0].timescale;
4660
4661 avio_wb32(pb, timescale);
4662 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
4663
4664 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
4665 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
4666 ffio_fill(pb, 0, 2 + 2 * 4); /* reserved */
4667
4668 /* Matrix structure */
4669 write_matrix(pb, 1, 0, 0, 1, 0, 0);
4670
4671 avio_wb32(pb, 0); /* reserved (preview time) */
4672 avio_wb32(pb, 0); /* reserved (preview duration) */
4673 avio_wb32(pb, 0); /* reserved (poster time) */
4674 avio_wb32(pb, 0); /* reserved (selection time) */
4675 avio_wb32(pb, 0); /* reserved (selection duration) */
4676 avio_wb32(pb, 0); /* reserved (current time) */
4677 avio_wb32(pb, max_track_id + 1); /* Next track id */
4678 return 0x6c;
4679}
4680
4683{
4684 avio_wb32(pb, 33); /* size */
4685 ffio_wfourcc(pb, "hdlr");
4686 avio_wb32(pb, 0);
4687 avio_wb32(pb, 0);
4688 ffio_wfourcc(pb, "mdir");
4689 ffio_wfourcc(pb, "appl");
4690 avio_wb32(pb, 0);
4691 avio_wb32(pb, 0);
4692 avio_w8(pb, 0);
4693 return 33;
4694}
4695
4696/* helper function to write a data tag with the specified string as data */
4697static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
4698{
4699 size_t data_len = strlen(data);
4700 if (long_style) {
4701 int size = 16 + data_len;
4702 avio_wb32(pb, size); /* size */
4703 ffio_wfourcc(pb, "data");
4704 avio_wb32(pb, 1);
4705 avio_wb32(pb, 0);
4706 avio_write(pb, data, data_len);
4707 return size;
4708 } else {
4709 avio_wb16(pb, data_len); /* string length */
4710 if (!lang)
4711 lang = ff_mov_iso639_to_lang("und", 1);
4712 avio_wb16(pb, lang);
4713 avio_write(pb, data, data_len);
4714 return data_len + 4;
4715 }
4716}
4717
4718static int mov_write_string_tag(AVIOContext *pb, const char *name,
4719 const char *value, int lang, int long_style)
4720{
4721 int size = 0;
4722 if (value && value[0]) {
4723 int64_t pos = avio_tell(pb);
4724 avio_wb32(pb, 0); /* size */
4725 ffio_wfourcc(pb, name);
4726 mov_write_string_data_tag(pb, value, lang, long_style);
4727 size = update_size(pb, pos);
4728 }
4729 return size;
4730}
4731
4732static int mov_write_freeform_tag(AVIOContext *pb, const char *mean,
4733 const char *name, const char *data)
4734{
4735 if (!data || !data[0])
4736 return 0;
4737
4738 static const char stub_flags[4] = {0, 0, 0, 0};
4739
4740 int64_t entry_pos = avio_tell(pb);
4741 avio_wb32(pb, 0); /* size */
4742 ffio_wfourcc(pb, "----"); /* freeform */
4743
4744 size_t mean_len = strlen(mean);
4745 avio_wb32(pb, 12 + mean_len);
4746 ffio_wfourcc(pb, "mean");
4747 avio_write(pb, &stub_flags[0], sizeof(stub_flags));
4748 avio_write(pb, mean, mean_len);
4749
4750 size_t name_len = strlen(name);
4751 avio_wb32(pb, 12 + name_len);
4752 ffio_wfourcc(pb, "name");
4753 avio_write(pb, &stub_flags[0], sizeof(stub_flags));
4754 avio_write(pb, name, name_len);
4755
4757
4758 return update_size(pb, entry_pos);
4759}
4760
4762 const char *tag, int *lang)
4763{
4764 int l, len, len2;
4765 AVDictionaryEntry *t, *t2 = NULL;
4766 char tag2[16];
4767
4768 *lang = 0;
4769
4770 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
4771 return NULL;
4772
4773 len = strlen(t->key);
4774 snprintf(tag2, sizeof(tag2), "%s-", tag);
4775 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
4776 len2 = strlen(t2->key);
4777 if (len2 == len + 4 && !strcmp(t->value, t2->value)
4778 && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
4779 *lang = l;
4780 return t;
4781 }
4782 }
4783 return t;
4784}
4785
4787 const char *name, const char *tag,
4788 int long_style)
4789{
4790 int lang;
4792 if (!t)
4793 return 0;
4794 return mov_write_string_tag(pb, name, t->value, lang, long_style);
4795}
4796
4798 const char *mean, const char *name,
4799 const char *tag)
4800{
4801 AVDictionaryEntry *t = av_dict_get(s->metadata, tag, NULL, 0);
4802 if (!t)
4803 return 0;
4804 return mov_write_freeform_tag(pb, mean, name, t->value);
4805}
4806
4807/* iTunes bpm number */
4809{
4810 AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
4811 int size = 0, tmpo = t ? atoi(t->value) : 0;
4812 if (tmpo) {
4813 size = 26;
4814 avio_wb32(pb, size);
4815 ffio_wfourcc(pb, "tmpo");
4816 avio_wb32(pb, size-8); /* size */
4817 ffio_wfourcc(pb, "data");
4818 avio_wb32(pb, 0x15); //type specifier
4819 avio_wb32(pb, 0);
4820 avio_wb16(pb, tmpo); // data
4821 }
4822 return size;
4823}
4824
4825/* 3GPP TS 26.244 */
4827{
4828 int lang;
4829 int64_t pos = avio_tell(pb);
4830 double latitude, longitude, altitude;
4831 int32_t latitude_fix, longitude_fix, altitude_fix;
4832 AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
4833 const char *ptr, *place = "";
4834 char *end;
4835 static const char *astronomical_body = "earth";
4836 if (!t)
4837 return 0;
4838
4839 ptr = t->value;
4840 latitude = strtod(ptr, &end);
4841 if (end == ptr) {
4842 av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
4843 return 0;
4844 }
4845 ptr = end;
4846 longitude = strtod(ptr, &end);
4847 if (end == ptr) {
4848 av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
4849 return 0;
4850 }
4851 ptr = end;
4852 altitude = strtod(ptr, &end);
4853 /* If no altitude was present, the default 0 should be fine */
4854 if (*end == '/')
4855 place = end + 1;
4856
4857 latitude_fix = (int32_t) ((1 << 16) * latitude);
4858 longitude_fix = (int32_t) ((1 << 16) * longitude);
4859 altitude_fix = (int32_t) ((1 << 16) * altitude);
4860
4861 avio_wb32(pb, 0); /* size */
4862 ffio_wfourcc(pb, "loci"); /* type */
4863 avio_wb32(pb, 0); /* version + flags */
4864 avio_wb16(pb, lang);
4865 avio_write(pb, place, strlen(place) + 1);
4866 avio_w8(pb, 0); /* role of place (0 == shooting location, 1 == real location, 2 == fictional location) */
4867 avio_wb32(pb, longitude_fix);
4868 avio_wb32(pb, latitude_fix);
4869 avio_wb32(pb, altitude_fix);
4870 avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
4871 avio_w8(pb, 0); /* additional notes, null terminated string */
4872
4873 return update_size(pb, pos);
4874}
4875
4876/* iTunes track or disc number */
4878 AVFormatContext *s, int disc)
4879{
4880 AVDictionaryEntry *t = av_dict_get(s->metadata,
4881 disc ? "disc" : "track",
4882 NULL, 0);
4883 int size = 0, track = t ? atoi(t->value) : 0;
4884 if (track) {
4885 int tracks = 0;
4886 char *slash = strchr(t->value, '/');
4887 if (slash)
4888 tracks = atoi(slash + 1);
4889 avio_wb32(pb, 32); /* size */
4890 ffio_wfourcc(pb, disc ? "disk" : "trkn");
4891 avio_wb32(pb, 24); /* size */
4892 ffio_wfourcc(pb, "data");
4893 avio_wb32(pb, 0); // 8 bytes empty
4894 avio_wb32(pb, 0);
4895 avio_wb16(pb, 0); // empty
4896 avio_wb16(pb, track); // track / disc number
4897 avio_wb16(pb, tracks); // total track / disc number
4898 avio_wb16(pb, 0); // empty
4899 size = 32;
4900 }
4901 return size;
4902}
4903
4905 const char *name, const char *tag,
4906 int len)
4907{
4909 uint8_t num;
4910 int size = 24 + len;
4911
4912 if (len != 1 && len != 4)
4913 return -1;
4914
4915 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
4916 return 0;
4917 num = atoi(t->value);
4918
4919 avio_wb32(pb, size);
4920 ffio_wfourcc(pb, name);
4921 avio_wb32(pb, size - 8);
4922 ffio_wfourcc(pb, "data");
4923 avio_wb32(pb, 0x15);
4924 avio_wb32(pb, 0);
4925 if (len==4) avio_wb32(pb, num);
4926 else avio_w8 (pb, num);
4927
4928 return size;
4929}
4930
4932{
4933 MOVMuxContext *mov = s->priv_data;
4934 int64_t pos = 0;
4935
4936 for (int i = 0; i < mov->nb_streams; i++) {
4937 MOVTrack *trk = &mov->tracks[i];
4938
4939 if (!is_cover_image(trk->st) || trk->cover_image->size <= 0)
4940 continue;
4941
4942 if (!pos) {
4943 pos = avio_tell(pb);
4944 avio_wb32(pb, 0);
4945 ffio_wfourcc(pb, "covr");
4946 }
4947 avio_wb32(pb, 16 + trk->cover_image->size);
4948 ffio_wfourcc(pb, "data");
4949 avio_wb32(pb, trk->tag);
4950 avio_wb32(pb , 0);
4951 avio_write(pb, trk->cover_image->data, trk->cover_image->size);
4952 }
4953
4954 return pos ? update_size(pb, pos) : 0;
4955}
4956
4957/* iTunes meta data list */
4960{
4961 int64_t pos = avio_tell(pb);
4962 avio_wb32(pb, 0); /* size */
4963 ffio_wfourcc(pb, "ilst");
4964 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
4965 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
4966 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
4967 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
4968 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
4969 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
4970 if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
4971 if (!(s->flags & AVFMT_FLAG_BITEXACT))
4972 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
4973 }
4974 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
4975 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
4976 mov_write_string_metadata(s, pb, "cprt", "copyright", 1);
4977 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
4978 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
4979 mov_write_string_metadata(s, pb, "desc", "description",1);
4980 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
4981 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
4982 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
4983 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
4984 mov_write_string_metadata(s, pb, "keyw", "keywords" , 1);
4985 mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
4986 mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
4987 mov_write_int8_metadata (s, pb, "stik", "media_type",1);
4988 mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
4989 mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
4990 mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
4991 mov_write_covr(pb, s);
4992 mov_write_trkn_tag(pb, mov, s, 0); // track number
4993 mov_write_trkn_tag(pb, mov, s, 1); // disc number
4994 mov_write_tmpo_tag(pb, s);
4995 mov_write_custom_metadata(s, pb, "com.apple.iTunes", "DISCSUBTITLE", "disc_subtitle");
4996 return update_size(pb, pos);
4997}
4998
5001{
5002 avio_wb32(pb, 33); /* size */
5003 ffio_wfourcc(pb, "hdlr");
5004 avio_wb32(pb, 0);
5005 avio_wb32(pb, 0);
5006 ffio_wfourcc(pb, "mdta");
5007 avio_wb32(pb, 0);
5008 avio_wb32(pb, 0);
5009 avio_wb32(pb, 0);
5010 avio_w8(pb, 0);
5011 return 33;
5012}
5013
5016{
5017 const AVDictionaryEntry *t = NULL;
5018 int64_t pos = avio_tell(pb);
5019 int64_t curpos, entry_pos;
5020 int count = 0;
5021
5022 avio_wb32(pb, 0); /* size */
5023 ffio_wfourcc(pb, "keys");
5024 avio_wb32(pb, 0);
5025 entry_pos = avio_tell(pb);
5026 avio_wb32(pb, 0); /* entry count */
5027
5028 while (t = av_dict_iterate(s->metadata, t)) {
5029 size_t key_len = strlen(t->key);
5030 avio_wb32(pb, key_len + 8);
5031 ffio_wfourcc(pb, "mdta");
5032 avio_write(pb, t->key, key_len);
5033 count += 1;
5034 }
5035 curpos = avio_tell(pb);
5036 avio_seek(pb, entry_pos, SEEK_SET);
5037 avio_wb32(pb, count); // rewrite entry count
5038 avio_seek(pb, curpos, SEEK_SET);
5039
5040 return update_size(pb, pos);
5041}
5042
5045{
5046 const AVDictionaryEntry *t = NULL;
5047 int64_t pos = avio_tell(pb);
5048 int count = 1; /* keys are 1-index based */
5049
5050 avio_wb32(pb, 0); /* size */
5051 ffio_wfourcc(pb, "ilst");
5052
5053 while (t = av_dict_iterate(s->metadata, t)) {
5054 int64_t entry_pos = avio_tell(pb);
5055 avio_wb32(pb, 0); /* size */
5056 avio_wb32(pb, count); /* key */
5057 mov_write_string_data_tag(pb, t->value, 0, 1);
5058 update_size(pb, entry_pos);
5059 count += 1;
5060 }
5061 return update_size(pb, pos);
5062}
5063
5064/* meta data tags */
5067{
5068 int size = 0;
5069 int64_t pos = avio_tell(pb);
5070 avio_wb32(pb, 0); /* size */
5071 ffio_wfourcc(pb, "meta");
5072 avio_wb32(pb, 0);
5073 if (mov->flags & FF_MOV_FLAG_USE_MDTA) {
5074 mov_write_mdta_hdlr_tag(pb, mov, s);
5075 mov_write_mdta_keys_tag(pb, mov, s);
5076 mov_write_mdta_ilst_tag(pb, mov, s);
5077#if CONFIG_AVIF_MUXER
5078 } else if (mov->mode == MODE_AVIF) {
5079 mov_write_hdlr_tag(s, pb, &mov->tracks[0]);
5080 // We always write the primary item id as 1 since only one track is
5081 // supported for AVIF.
5082 mov_write_pitm_tag(pb, 1);
5083 mov_write_iloc_tag(pb, mov, s);
5084 mov_write_iinf_tag(pb, mov, s);
5085 if (mov->nb_streams > 1)
5086 mov_write_iref_tag(pb, mov, s);
5087 mov_write_iprp_tag(pb, mov, s);
5088#endif
5089 } else {
5090 /* iTunes metadata tag */
5091 mov_write_itunes_hdlr_tag(pb, mov, s);
5092 mov_write_ilst_tag(pb, mov, s);
5093 }
5094 size = update_size(pb, pos);
5095 return size;
5096}
5097
5099 const char *name, const char *key)
5100{
5101 int len;
5103
5104 if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
5105 return 0;
5106
5107 len = strlen(t->value);
5108 if (len > 0) {
5109 int size = len + 8;
5110 avio_wb32(pb, size);
5111 ffio_wfourcc(pb, name);
5112 avio_write(pb, t->value, len);
5113 return size;
5114 }
5115 return 0;
5116}
5117
5118static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
5119{
5120 int val;
5121 while (*b) {
5122 GET_UTF8(val, *b++, return -1;)
5123 avio_wb16(pb, val);
5124 }
5125 avio_wb16(pb, 0x00);
5126 return 0;
5127}
5128
5129static uint16_t language_code(const char *str)
5130{
5131 return (((str[0] - 0x60) & 0x1F) << 10) +
5132 (((str[1] - 0x60) & 0x1F) << 5) +
5133 (( str[2] - 0x60) & 0x1F);
5134}
5135
5137 const char *tag, const char *str)
5138{
5139 int64_t pos = avio_tell(pb);
5140 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
5141 if (!t || !utf8len(t->value))
5142 return 0;
5143 avio_wb32(pb, 0); /* size */
5144 ffio_wfourcc(pb, tag); /* type */
5145 avio_wb32(pb, 0); /* version + flags */
5146 if (!strcmp(tag, "yrrc"))
5147 avio_wb16(pb, atoi(t->value));
5148 else {
5149 avio_wb16(pb, language_code("eng")); /* language */
5150 avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
5151 if (!strcmp(tag, "albm") &&
5152 (t = av_dict_get(s->metadata, "track", NULL, 0)))
5153 avio_w8(pb, atoi(t->value));
5154 }
5155 return update_size(pb, pos);
5156}
5157
5159{
5160 int64_t pos = avio_tell(pb);
5161 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
5162
5163 avio_wb32(pb, 0); // size
5164 ffio_wfourcc(pb, "chpl");
5165 avio_wb32(pb, 0x01000000); // version + flags
5166 avio_wb32(pb, 0); // unknown
5167 avio_w8(pb, nb_chapters);
5168
5169 for (i = 0; i < nb_chapters; i++) {
5170 AVChapter *c = s->chapters[i];
5172 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
5173
5174 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
5175 int len = FFMIN(strlen(t->value), 255);
5176 avio_w8(pb, len);
5177 avio_write(pb, t->value, len);
5178 } else
5179 avio_w8(pb, 0);
5180 }
5181 return update_size(pb, pos);
5182}
5183
5186{
5187 AVIOContext *pb_buf;
5188 int ret, size;
5189 uint8_t *buf;
5190
5191 ret = avio_open_dyn_buf(&pb_buf);
5192 if (ret < 0)
5193 return ret;
5194
5195 if (mov->mode & MODE_3GP) {
5196 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
5197 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
5198 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
5199 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
5200 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
5201 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
5202 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
5203 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
5204 mov_write_loci_tag(s, pb_buf);
5205 } else if (mov->mode == MODE_MOV && !(mov->flags & FF_MOV_FLAG_USE_MDTA)) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
5206 mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
5207 mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
5208 mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
5209 mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
5210 mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
5211 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
5212 // currently ignored by mov.c
5213 mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
5214 // add support for libquicktime, this atom is also actually read by mov.c
5215 mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
5216 mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
5217 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
5218 mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
5219 mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
5220 mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
5221 mov_write_string_metadata(s, pb_buf, "\251key", "keywords", 0);
5222 mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
5223 } else {
5224 /* iTunes meta data */
5225 mov_write_meta_tag(pb_buf, mov, s);
5226 mov_write_loci_tag(s, pb_buf);
5227 }
5228
5229 if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
5230 mov_write_chpl_tag(pb_buf, s);
5231
5232 if ((size = avio_get_dyn_buf(pb_buf, &buf)) > 0) {
5233 avio_wb32(pb, size + 8);
5234 ffio_wfourcc(pb, "udta");
5235 avio_write(pb, buf, size);
5236 }
5237 ffio_free_dyn_buf(&pb_buf);
5238
5239 return 0;
5240}
5241
5243 const char *str, const char *lang, int type)
5244{
5245 int len = utf8len(str) + 1;
5246 if (len <= 0)
5247 return;
5248 avio_wb16(pb, len * 2 + 10); /* size */
5249 avio_wb32(pb, type); /* type */
5250 avio_wb16(pb, language_code(lang)); /* language */
5251 avio_wb16(pb, 0x01); /* ? */
5252 ascii_to_wc(pb, str);
5253}
5254
5256{
5257 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
5258 int64_t pos, pos2;
5259
5260 if (title) {
5261 pos = avio_tell(pb);
5262 avio_wb32(pb, 0); /* size placeholder*/
5263 ffio_wfourcc(pb, "uuid");
5264 ffio_wfourcc(pb, "USMT");
5265 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
5266 avio_wb32(pb, 0xbb88695c);
5267 avio_wb32(pb, 0xfac9c740);
5268
5269 pos2 = avio_tell(pb);
5270 avio_wb32(pb, 0); /* size placeholder*/
5271 ffio_wfourcc(pb, "MTDT");
5272 avio_wb16(pb, 4);
5273
5274 // ?
5275 avio_wb16(pb, 0x0C); /* size */
5276 avio_wb32(pb, 0x0B); /* type */
5277 avio_wb16(pb, language_code("und")); /* language */
5278 avio_wb16(pb, 0x0); /* ? */
5279 avio_wb16(pb, 0x021C); /* data */
5280
5281 if (!(s->flags & AVFMT_FLAG_BITEXACT))
5282 mov_write_psp_udta_tag(pb, LIBAVFORMAT_IDENT, "eng", 0x04);
5283 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
5284 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
5285
5286 update_size(pb, pos2);
5287 return update_size(pb, pos);
5288 }
5289
5290 return 0;
5291}
5292
5294{
5299 if (!sd)
5300 return 0;
5301
5303 for (AVEncryptionInitInfo *copy = info; copy; copy = copy->next) {
5304 int64_t pos;
5305
5306 if (!copy->data_size && !copy->num_key_ids)
5307 continue;
5308
5309 pos = avio_tell(pb);
5310 avio_wb32(pb, 0); /* size placeholder */
5311 ffio_wfourcc(pb, "pssh");
5312 avio_w8(pb, 1); /* version */
5313 avio_wb24(pb, 0);
5314 for (int i = 0; i < copy->system_id_size; i++)
5315 avio_w8(pb, copy->system_id[i]);
5316 avio_wb32(pb, copy->num_key_ids);
5317 for (int i = 0; i < copy->num_key_ids; i++)
5318 for (int j = 0; j < copy->key_id_size; j++)
5319 avio_w8(pb, copy->key_ids[i][j]);
5320 avio_wb32(pb, copy->data_size);
5321 avio_write(pb, copy->data, copy->data_size);
5322 update_size(pb, pos);
5323 }
5324
5326
5327 return 0;
5328}
5329
5330static void build_chunks(MOVTrack *trk)
5331{
5332 int i;
5333 MOVIentry *chunk = &trk->cluster[0];
5334 uint64_t chunkSize = chunk->size;
5335 chunk->chunkNum = 1;
5336 if (trk->chunkCount)
5337 return;
5338 trk->chunkCount = 1;
5339 for (i = 1; i<trk->entry; i++){
5340 if (chunk->pos + chunkSize == trk->cluster[i].pos &&
5341 chunk->stsd_index == trk->cluster[i].stsd_index &&
5342 chunkSize + trk->cluster[i].size < (1<<20)){
5343 chunkSize += trk->cluster[i].size;
5344 chunk->samples_in_chunk += trk->cluster[i].entries;
5345 } else {
5346 trk->cluster[i].chunkNum = chunk->chunkNum+1;
5347 chunk=&trk->cluster[i];
5348 chunkSize = chunk->size;
5349 trk->chunkCount++;
5350 }
5351 }
5352}
5353
5354/**
5355 * Assign track ids. If option "use_stream_ids_as_track_ids" is set,
5356 * the stream ids are used as track ids.
5357 *
5358 * This assumes mov->tracks and s->streams are in the same order and
5359 * there are no gaps in either of them (so mov->tracks[n] refers to
5360 * s->streams[n]).
5361 *
5362 * As an exception, there can be more entries in
5363 * s->streams than in mov->tracks, in which case new track ids are
5364 * generated (starting after the largest found stream id).
5365 */
5367{
5368 int i;
5369
5370 if (mov->track_ids_ok)
5371 return 0;
5372
5373 if (mov->use_stream_ids_as_track_ids) {
5374 int next_generated_track_id = 0;
5375 for (i = 0; i < mov->nb_streams; i++) {
5376 AVStream *st = mov->tracks[i].st;
5377 if (st->id > next_generated_track_id)
5378 next_generated_track_id = st->id;
5379 }
5380
5381 for (i = 0; i < mov->nb_tracks; i++) {
5382 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
5383 continue;
5384
5385 mov->tracks[i].track_id = i >= mov->nb_streams ? ++next_generated_track_id : mov->tracks[i].st->id;
5386 }
5387 } else {
5388 int last_track_id = 0;
5389 for (i = 0; i < mov->nb_tracks; i++) {
5390 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
5391 continue;
5392
5393 last_track_id =
5394 mov->tracks[i].track_id = (mov->tracks[i].st
5395 ? FFMAX(mov->tracks[i].st->index, last_track_id)
5396 : FFMAX(i, last_track_id)) + 1;
5397 }
5398 }
5399
5400 mov->track_ids_ok = 1;
5401
5402 return 0;
5403}
5404
5405static int mov_find_tref_id(MOVMuxContext *mov, const MovTag *tag, uint32_t id)
5406{
5407 for (int i = 0; i < tag->nb_id; i++) {
5408 if (tag->id[i] == id)
5409 return 1;
5410 }
5411 return 0;
5412}
5413
5414static int mov_add_tref_id(MOVMuxContext *mov, MovTag *tag, uint32_t id)
5415{
5416 int ret = mov_find_tref_id(mov, tag, id);
5417
5418 if (!ret) {
5419 uint32_t *tmp = av_realloc_array(tag->id, tag->nb_id + 1, sizeof(*tag->id));
5420 if (!tmp)
5421 return AVERROR(ENOMEM);
5422 tag->id = tmp;
5423 tag->id[tag->nb_id++] = id;
5424 }
5425
5426 return 0;
5427}
5428
5429static MovTag *mov_find_tref_tag(MOVMuxContext *mov, const MOVTrack *trk, uint32_t name)
5430{
5431 for (int i = 0; i < trk->nb_tref_tags; i++) {
5432 MovTag *entry = &trk->tref_tags[i];
5433
5434 if (entry->name == name)
5435 return entry;
5436 }
5437 return NULL;
5438}
5439
5441{
5442 MovTag *tag = mov_find_tref_tag(mov, trk, name);
5443
5444 if (!tag) {
5446 sizeof(*trk->tref_tags));
5447 if (!tmp)
5448 return NULL;
5449 trk->tref_tags = tmp;
5450 tag = &trk->tref_tags[trk->nb_tref_tags++];
5451 *tag = (MovTag){ .name = name };
5452 }
5453
5454 return tag;
5455}
5456
5459{
5460 int i;
5461 int64_t pos = avio_tell(pb);
5462 avio_wb32(pb, 0); /* size placeholder*/
5463 ffio_wfourcc(pb, "moov");
5464
5465 mov_setup_track_ids(mov, s);
5466
5467 for (i = 0; i < mov->nb_tracks; i++) {
5468 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
5469 continue;
5470
5471 mov->tracks[i].time = mov->time;
5472
5473 if (mov->tracks[i].entry)
5474 build_chunks(&mov->tracks[i]);
5475 }
5476
5477 if (mov->chapter_track)
5478 for (i = 0; i < mov->nb_streams; i++) {
5479 MovTag *tag = mov_add_tref_tag(mov, &mov->tracks[i], MKTAG('c','h','a','p'));
5480 if (!tag)
5481 return AVERROR(ENOMEM);
5482
5483 int ret = mov_add_tref_id(mov, tag, mov->tracks[mov->chapter_track].track_id);
5484 if (ret < 0)
5485 return ret;
5486 }
5487 for (i = 0; i < mov->nb_tracks; i++) {
5488 MOVTrack *track = &mov->tracks[i];
5489 if (track->tag == MKTAG('r','t','p',' ')) {
5490 MovTag *tag = mov_add_tref_tag(mov, track, MKTAG('h','i','n','t'));
5491 if (!tag)
5492 return AVERROR(ENOMEM);
5493
5494 av_assert0(track->nb_src_track);
5495 int ret = mov_add_tref_id(mov, tag, mov->tracks[*track->src_track].track_id);
5496 if (ret < 0)
5497 return ret;
5498 } else if (track->tag == MKTAG('l','v','c','1') && track->nb_src_track) {
5499 MovTag *tag = mov_add_tref_tag(mov, track, MKTAG('s','b','a','s'));
5500 if (!tag)
5501 return AVERROR(ENOMEM);
5502
5503 int ret = mov_add_tref_id(mov, tag, mov->tracks[*track->src_track].track_id);
5504 if (ret < 0)
5505 return ret;
5506 } else {
5507 if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
5511 if (sd && sd->size == sizeof(int)) {
5512 int *fallback = (int *)sd->data;
5513 if (*fallback >= 0 && *fallback < mov->nb_tracks) {
5514 MovTag *tag = mov_add_tref_tag(mov, track, MKTAG('f','a','l','l'));
5515 if (!tag)
5516 return AVERROR(ENOMEM);
5517
5518 int ret = mov_add_tref_id(mov, tag, mov->tracks[*fallback].track_id);
5519 if (ret < 0)
5520 return ret;
5521 }
5522 }
5523 }
5524 for (int j = 0; j < track->nb_src_track; j++) {
5525 int src_trk = track->src_track[j];
5526 MovTag *tag = mov_add_tref_tag(mov, &mov->tracks[src_trk], track->tag);
5527 if (!tag)
5528 return AVERROR(ENOMEM);
5529 int ret = mov_add_tref_id(mov, tag, track->track_id);
5530 if (ret < 0)
5531 return ret;
5532 }
5533 if (mov->nb_meta_tmcd && track->nb_src_track) {
5534 /* Derive the duration from the first source track, matching
5535 * the convention used by get_pts_range() and the rtp/lvc1
5536 * branches above. The source may use a different timescale. */
5537 int src_trk = track->src_track[0];
5538 track->track_duration = av_rescale(mov->tracks[src_trk].track_duration,
5539 track->timescale,
5540 mov->tracks[src_trk].timescale);
5541 }
5542 }
5543 }
5544
5545 mov_write_mvhd_tag(pb, mov);
5546 if (mov->mode != MODE_MOV && mov->mode != MODE_AVIF && !mov->iods_skip)
5547 mov_write_iods_tag(pb, mov);
5548 for (i = 0; i < mov->nb_tracks; i++) {
5549 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT ||
5550 mov->mode == MODE_AVIF) {
5551 int ret = mov_write_trak_tag(s, pb, mov, &(mov->tracks[i]), i < mov->nb_streams ? mov->tracks[i].st : NULL);
5552 if (ret < 0)
5553 return ret;
5554 }
5555 }
5556 /* Don't write mvex for hybrid_fragmented during mov_write_trailer
5557 * (mov->moov_written != 0)
5558 */
5559 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
5561 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
5562
5563 if (mov->mode == MODE_PSP)
5565 else if (mov->mode != MODE_AVIF)
5566 mov_write_udta_tag(pb, mov, s);
5567 for (i = 0; i < mov->nb_streams; i++)
5568 mov_write_pssh_tag(pb, mov->tracks[i].st);
5569
5570 return update_size(pb, pos);
5571}
5572
5573static void param_write_int(AVIOContext *pb, const char *name, int value)
5574{
5575 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
5576}
5577
5578static void param_write_string(AVIOContext *pb, const char *name, const char *value)
5579{
5580 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
5581}
5582
5583static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
5584{
5585 char buf[150];
5586 len = FFMIN(sizeof(buf) / 2 - 1, len);
5587 ff_data_to_hex(buf, value, len, 0);
5588 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
5589}
5590
5592{
5593 int64_t pos = avio_tell(pb);
5594 int i;
5595
5596 static const AVUUID uuid = {
5597 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
5598 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
5599 };
5600
5601 avio_wb32(pb, 0);
5602 ffio_wfourcc(pb, "uuid");
5603 avio_write(pb, uuid, AV_UUID_LEN);
5604 avio_wb32(pb, 0);
5605
5606 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
5607 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
5608 avio_printf(pb, "<head>\n");
5609 if (!(mov->fc->flags & AVFMT_FLAG_BITEXACT))
5610 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
5612 avio_printf(pb, "</head>\n");
5613 avio_printf(pb, "<body>\n");
5614 avio_printf(pb, "<switch>\n");
5615
5616 mov_setup_track_ids(mov, s);
5617
5618 for (i = 0; i < mov->nb_tracks; i++) {
5619 MOVTrack *track = &mov->tracks[i];
5620 struct mpeg4_bit_rate_values bit_rates =
5622 const char *type;
5623 int track_id = track->track_id;
5624 char track_name_buf[32] = { 0 };
5625
5626 AVStream *st = track->st;
5627 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
5628
5629 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && !is_cover_image(st)) {
5630 type = "video";
5631 } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
5632 type = "audio";
5633 } else {
5634 continue;
5635 }
5636
5637 avio_printf(pb, "<%s systemBitrate=\"%"PRIu32"\">\n", type,
5638 bit_rates.avg_bit_rate);
5639 param_write_int(pb, "systemBitrate", bit_rates.avg_bit_rate);
5640 param_write_int(pb, "trackID", track_id);
5641 param_write_string(pb, "systemLanguage", lang ? lang->value : "und");
5642
5643 /* Build track name piece by piece: */
5644 /* 1. track type */
5645 av_strlcat(track_name_buf, type, sizeof(track_name_buf));
5646 /* 2. track language, if available */
5647 if (lang)
5648 av_strlcatf(track_name_buf, sizeof(track_name_buf),
5649 "_%s", lang->value);
5650 /* 3. special type suffix */
5651 /* "_cc" = closed captions, "_ad" = audio_description */
5653 av_strlcat(track_name_buf, "_cc", sizeof(track_name_buf));
5655 av_strlcat(track_name_buf, "_ad", sizeof(track_name_buf));
5656
5657 param_write_string(pb, "trackName", track_name_buf);
5658
5659 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
5660 if (track->par->codec_id == AV_CODEC_ID_H264) {
5661 uint8_t *ptr;
5662 int size = track->extradata_size[track->last_stsd_index];
5664 &size)) {
5665 param_write_hex(pb, "CodecPrivateData",
5666 ptr ? ptr : track->extradata[track->last_stsd_index],
5667 size);
5668 av_free(ptr);
5669 }
5670 param_write_string(pb, "FourCC", "H264");
5671 } else if (track->par->codec_id == AV_CODEC_ID_VC1) {
5672 param_write_string(pb, "FourCC", "WVC1");
5673 param_write_hex(pb, "CodecPrivateData", track->extradata[track->last_stsd_index],
5674 track->extradata_size[track->last_stsd_index]);
5675 }
5676 param_write_int(pb, "MaxWidth", track->par->width);
5677 param_write_int(pb, "MaxHeight", track->par->height);
5678 param_write_int(pb, "DisplayWidth", track->par->width);
5679 param_write_int(pb, "DisplayHeight", track->par->height);
5680 } else {
5681 if (track->par->codec_id == AV_CODEC_ID_AAC) {
5682 switch (track->par->profile) {
5684 param_write_string(pb, "FourCC", "AACP");
5685 break;
5686 case AV_PROFILE_AAC_HE:
5687 param_write_string(pb, "FourCC", "AACH");
5688 break;
5689 default:
5690 param_write_string(pb, "FourCC", "AACL");
5691 }
5692 } else if (track->par->codec_id == AV_CODEC_ID_WMAPRO) {
5693 param_write_string(pb, "FourCC", "WMAP");
5694 }
5695 param_write_hex(pb, "CodecPrivateData", track->extradata[track->last_stsd_index],
5696 track->extradata_size[track->last_stsd_index]);
5698 track->par->codec_id));
5699 param_write_int(pb, "Channels", track->par->ch_layout.nb_channels);
5700 param_write_int(pb, "SamplingRate", track->tag == MKTAG('i','a','m','f') ?
5701 0 : track->par->sample_rate);
5702 param_write_int(pb, "BitsPerSample", 16);
5703 param_write_int(pb, "PacketSize", track->par->block_align ?
5704 track->par->block_align : 4);
5705 }
5706 avio_printf(pb, "</%s>\n", type);
5707 }
5708 avio_printf(pb, "</switch>\n");
5709 avio_printf(pb, "</body>\n");
5710 avio_printf(pb, "</smil>\n");
5711
5712 return update_size(pb, pos);
5713}
5714
5716{
5717 avio_wb32(pb, 16);
5718 ffio_wfourcc(pb, "mfhd");
5719 avio_wb32(pb, 0);
5720 avio_wb32(pb, mov->fragments);
5721 return 0;
5722}
5723
5729
5731 MOVTrack *track, int64_t moof_offset)
5732{
5733 int64_t pos = avio_tell(pb);
5736 if (!track->entry) {
5738 } else {
5740 }
5746 }
5747 /* CMAF requires all values to be explicit in tfhd atoms */
5748 if (mov->flags & FF_MOV_FLAG_CMAF)
5750
5751 /* Don't set a default sample size, the silverlight player refuses
5752 * to play files with that set. Don't set a default sample duration,
5753 * WMP freaks out if it is set. Don't set a base data offset, PIFF
5754 * file format says it MUST NOT be set. */
5755 if (track->mode == MODE_ISM)
5758
5759 avio_wb32(pb, 0); /* size placeholder */
5760 ffio_wfourcc(pb, "tfhd");
5761 avio_w8(pb, 0); /* version */
5762 avio_wb24(pb, flags);
5763
5764 avio_wb32(pb, track->track_id); /* track-id */
5766 avio_wb64(pb, moof_offset);
5767 if (flags & MOV_TFHD_STSD_ID) {
5768 avio_wb32(pb, 1);
5769 }
5771 track->default_duration = get_cluster_duration(track, 0);
5772 avio_wb32(pb, track->default_duration);
5773 }
5775 track->default_size = track->entry ? track->cluster[0].size : 1;
5776 avio_wb32(pb, track->default_size);
5777 } else
5778 track->default_size = -1;
5779
5781 /* Set the default flags based on the second sample, if available.
5782 * If the first sample is different, that can be signaled via a separate field. */
5783 if (track->entry > 1)
5784 track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
5785 else
5786 track->default_sample_flags =
5787 track->par->codec_type == AVMEDIA_TYPE_VIDEO ?
5790 avio_wb32(pb, track->default_sample_flags);
5791 }
5792
5793 return update_size(pb, pos);
5794}
5795
5797 MOVTrack *track, int moof_size,
5798 int first, int end)
5799{
5800 int64_t pos = avio_tell(pb);
5801 uint32_t flags = MOV_TRUN_DATA_OFFSET;
5802 int i;
5803
5804 for (i = first; i < end; i++) {
5805 if (get_cluster_duration(track, i) != track->default_duration)
5807 if (track->cluster[i].size != track->default_size)
5809 if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
5811 }
5812 if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > first &&
5813 get_sample_flags(track, &track->cluster[first]) != track->default_sample_flags)
5815 if (track->flags & MOV_TRACK_CTTS)
5817
5818 avio_wb32(pb, 0); /* size placeholder */
5819 ffio_wfourcc(pb, "trun");
5821 avio_w8(pb, 1); /* version */
5822 else
5823 avio_w8(pb, 0); /* version */
5824 avio_wb24(pb, flags);
5825
5826 avio_wb32(pb, end - first); /* sample count */
5829 !mov->first_trun)
5830 avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
5831 else
5832 avio_wb32(pb, moof_size + 8 + track->data_offset +
5833 track->cluster[first].pos); /* data offset */
5835 avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
5836
5837 for (i = first; i < end; i++) {
5839 avio_wb32(pb, get_cluster_duration(track, i));
5841 avio_wb32(pb, track->cluster[i].size);
5843 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
5845 avio_wb32(pb, track->cluster[i].cts);
5846 }
5847
5848 mov->first_trun = 0;
5849 return update_size(pb, pos);
5850}
5851
5853{
5854 int64_t pos = avio_tell(pb);
5855 static const uint8_t uuid[] = {
5856 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
5857 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
5858 };
5859
5860 avio_wb32(pb, 0); /* size placeholder */
5861 ffio_wfourcc(pb, "uuid");
5862 avio_write(pb, uuid, AV_UUID_LEN);
5863 avio_w8(pb, 1);
5864 avio_wb24(pb, 0);
5865 avio_wb64(pb, track->cluster[0].dts + track->cluster[0].cts);
5866 avio_wb64(pb, track->end_pts -
5867 (track->cluster[0].dts + track->cluster[0].cts));
5868
5869 return update_size(pb, pos);
5870}
5871
5873 MOVTrack *track, int entry)
5874{
5875 int n = track->nb_frag_info - 1 - entry, i;
5876 int size = 8 + 16 + 4 + 1 + 16*n;
5877 static const uint8_t uuid[] = {
5878 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
5879 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
5880 };
5881
5882 if (entry < 0)
5883 return 0;
5884
5885 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
5886 avio_wb32(pb, size);
5887 ffio_wfourcc(pb, "uuid");
5888 avio_write(pb, uuid, AV_UUID_LEN);
5889 avio_w8(pb, 1);
5890 avio_wb24(pb, 0);
5891 avio_w8(pb, n);
5892 for (i = 0; i < n; i++) {
5893 int index = entry + 1 + i;
5894 avio_wb64(pb, track->frag_info[index].time);
5895 avio_wb64(pb, track->frag_info[index].duration);
5896 }
5897 if (n < mov->ism_lookahead) {
5898 int free_size = 16 * (mov->ism_lookahead - n);
5899 avio_wb32(pb, free_size);
5900 ffio_wfourcc(pb, "free");
5901 ffio_fill(pb, 0, free_size - 8);
5902 }
5903
5904 return 0;
5905}
5906
5908 MOVTrack *track)
5909{
5910 int64_t pos = avio_tell(pb);
5911 int i;
5912 for (i = 0; i < mov->ism_lookahead; i++) {
5913 /* Update the tfrf tag for the last ism_lookahead fragments,
5914 * nb_frag_info - 1 is the next fragment to be written. */
5915 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
5916 }
5917 avio_seek(pb, pos, SEEK_SET);
5918 return 0;
5919}
5920
5921static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
5922 int size)
5923{
5924 int i;
5925 for (i = 0; i < mov->nb_tracks; i++) {
5926 MOVTrack *track = &mov->tracks[i];
5927 MOVFragmentInfo *info;
5928 if ((tracks >= 0 && i != tracks) || !track->entry)
5929 continue;
5930 track->nb_frag_info++;
5931 if (track->nb_frag_info >= track->frag_info_capacity) {
5932 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
5933 if (av_reallocp_array(&track->frag_info,
5934 new_capacity,
5935 sizeof(*track->frag_info)))
5936 return AVERROR(ENOMEM);
5937 track->frag_info_capacity = new_capacity;
5938 }
5939 info = &track->frag_info[track->nb_frag_info - 1];
5940 info->offset = avio_tell(pb);
5941 info->size = size;
5942 // Try to recreate the original pts for the first packet
5943 // from the fields we have stored
5944 info->time = track->cluster[0].dts + track->cluster[0].cts;
5945 info->duration = track->end_pts -
5946 (track->cluster[0].dts + track->cluster[0].cts);
5947 // If the pts is less than zero, we will have trimmed
5948 // away parts of the media track using an edit list,
5949 // and the corresponding start presentation time is zero.
5950 if (info->time < 0) {
5951 info->duration += info->time;
5952 info->time = 0;
5953 }
5954 info->tfrf_offset = 0;
5955 mov_write_tfrf_tags(pb, mov, track);
5956 }
5957 return 0;
5958}
5959
5960static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
5961{
5962 int i;
5963 for (i = 0; i < mov->nb_tracks; i++) {
5964 MOVTrack *track = &mov->tracks[i];
5965 if ((tracks >= 0 && i != tracks) || !track->entry)
5966 continue;
5967 if (track->nb_frag_info > max) {
5968 memmove(track->frag_info, track->frag_info + (track->nb_frag_info - max), max * sizeof(*track->frag_info));
5969 track->nb_frag_info = max;
5970 }
5971 }
5972}
5973
5975{
5976 int64_t pos = avio_tell(pb);
5977
5978 avio_wb32(pb, 0); /* size */
5979 ffio_wfourcc(pb, "tfdt");
5980 avio_w8(pb, 1); /* version */
5981 avio_wb24(pb, 0);
5982 avio_wb64(pb, track->cluster[0].dts - track->start_dts);
5983 return update_size(pb, pos);
5984}
5985
5987 MOVTrack *track, int64_t moof_offset,
5988 int moof_size)
5989{
5990 int64_t pos = avio_tell(pb);
5991 int i, start = 0;
5992 avio_wb32(pb, 0); /* size placeholder */
5993 ffio_wfourcc(pb, "traf");
5994
5995 mov_write_tfhd_tag(pb, mov, track, moof_offset);
5996 if (mov->mode != MODE_ISM)
5997 mov_write_tfdt_tag(pb, track);
5998 for (i = 1; i < track->entry; i++) {
5999 if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
6000 mov_write_trun_tag(pb, mov, track, moof_size, start, i);
6001 start = i;
6002 }
6003 }
6004 mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
6005 if (mov->mode == MODE_ISM) {
6006 mov_write_tfxd_tag(pb, track);
6007
6008 if (mov->ism_lookahead) {
6009 int size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
6010
6011 if (track->nb_frag_info > 0) {
6012 MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
6013 if (!info->tfrf_offset)
6014 info->tfrf_offset = avio_tell(pb);
6015 }
6016 avio_wb32(pb, 8 + size);
6017 ffio_wfourcc(pb, "free");
6018 ffio_fill(pb, 0, size);
6019 }
6020 }
6021
6022 if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
6023 ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, moof_offset);
6024
6025 return update_size(pb, pos);
6026}
6027
6029 int tracks, int moof_size)
6030{
6031 int64_t pos = avio_tell(pb);
6032 int i;
6033
6034 avio_wb32(pb, 0); /* size placeholder */
6035 ffio_wfourcc(pb, "moof");
6036 mov->first_trun = 1;
6037
6038 mov_write_mfhd_tag(pb, mov);
6039 for (i = 0; i < mov->nb_tracks; i++) {
6040 MOVTrack *track = &mov->tracks[i];
6041 if (tracks >= 0 && i != tracks)
6042 continue;
6043 if (!track->entry)
6044 continue;
6045 if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
6046 mov_write_pssh_tag(pb, track->st);
6047 mov_write_traf_tag(pb, mov, track, pos, moof_size);
6048 }
6049
6050 return update_size(pb, pos);
6051}
6052
6054 MOVTrack *track, int ref_size, int total_sidx_size)
6055{
6056 int64_t pos = avio_tell(pb), offset_pos, end_pos;
6057 int64_t presentation_time, duration, offset;
6058 unsigned starts_with_SAP;
6059 int i, entries;
6060
6061 if (track->entry) {
6062 entries = 1;
6063 presentation_time = track->cluster[0].dts + track->cluster[0].cts;
6064 duration = track->end_pts -
6065 (track->cluster[0].dts + track->cluster[0].cts);
6066 starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
6067
6068 // pts<0 should be cut away using edts
6069 if (presentation_time < 0) {
6070 duration += presentation_time;
6071 presentation_time = 0;
6072 }
6073 } else {
6074 entries = track->nb_frag_info;
6075 if (entries <= 0)
6076 return 0;
6077 presentation_time = track->frag_info[0].time;
6078 }
6079
6080 avio_wb32(pb, 0); /* size */
6081 ffio_wfourcc(pb, "sidx");
6082 avio_w8(pb, 1); /* version */
6083 avio_wb24(pb, 0);
6084 avio_wb32(pb, track->track_id); /* reference_ID */
6085 avio_wb32(pb, track->timescale); /* timescale */
6086 avio_wb64(pb, presentation_time); /* earliest_presentation_time */
6087 offset_pos = avio_tell(pb);
6088 avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
6089 avio_wb16(pb, 0); /* reserved */
6090
6091 avio_wb16(pb, entries); /* reference_count */
6092 for (i = 0; i < entries; i++) {
6093 if (!track->entry) {
6094 if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
6095 av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
6096 }
6097 duration = track->frag_info[i].duration;
6098 ref_size = track->frag_info[i].size;
6099 starts_with_SAP = 1;
6100 }
6101 avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
6102 avio_wb32(pb, duration); /* subsegment_duration */
6103 avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
6104 }
6105
6106 end_pos = avio_tell(pb);
6107 offset = pos + total_sidx_size - end_pos;
6108 avio_seek(pb, offset_pos, SEEK_SET);
6109 avio_wb64(pb, offset);
6110 avio_seek(pb, end_pos, SEEK_SET);
6111 return update_size(pb, pos);
6112}
6113
6115 int tracks, int ref_size)
6116{
6117 int i, round, ret;
6118 AVIOContext *avio_buf;
6119 int total_size = 0;
6120 for (round = 0; round < 2; round++) {
6121 // First run one round to calculate the total size of all
6122 // sidx atoms.
6123 // This would be much simpler if we'd only write one sidx
6124 // atom, for the first track in the moof.
6125 if (round == 0) {
6126 if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
6127 return ret;
6128 } else {
6129 avio_buf = pb;
6130 }
6131 for (i = 0; i < mov->nb_tracks; i++) {
6132 MOVTrack *track = &mov->tracks[i];
6133 if (tracks >= 0 && i != tracks)
6134 continue;
6135 // When writing a sidx for the full file, entry is 0, but
6136 // we want to include all tracks. ref_size is 0 in this case,
6137 // since we read it from frag_info instead.
6138 if (!track->entry && ref_size > 0)
6139 continue;
6140 total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
6141 total_size);
6142 }
6143 if (round == 0) {
6144 total_size = ffio_close_null_buf(avio_buf);
6145 if (total_size < 0)
6146 return total_size;
6147 }
6148 }
6149 return 0;
6150}
6151
6152static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
6153{
6154 int64_t pos = avio_tell(pb), pts_us, ntp_ts;
6155 MOVTrack *first_track;
6156 int flags = 24;
6157
6158 /* PRFT should be associated with at most one track. So, choosing only the
6159 * first track. */
6160 if (tracks > 0)
6161 return 0;
6162 first_track = &(mov->tracks[0]);
6163
6164 if (!first_track->entry) {
6165 av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, no entries in the track\n");
6166 return 0;
6167 }
6168
6169 if (first_track->cluster[0].pts == AV_NOPTS_VALUE) {
6170 av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, first PTS is invalid\n");
6171 return 0;
6172 }
6173
6174 if (mov->write_prft == MOV_PRFT_SRC_WALLCLOCK) {
6175 if (first_track->cluster[0].prft.wallclock) {
6176 /* Round the NTP time to whole milliseconds. */
6177 ntp_ts = ff_get_formatted_ntp_time((first_track->cluster[0].prft.wallclock / 1000) * 1000 +
6179 flags = first_track->cluster[0].prft.flags;
6180 } else
6182 } else if (mov->write_prft == MOV_PRFT_SRC_PTS) {
6183 pts_us = av_rescale_q(first_track->cluster[0].pts,
6184 first_track->st->time_base, AV_TIME_BASE_Q);
6185 ntp_ts = ff_get_formatted_ntp_time(pts_us + NTP_OFFSET_US);
6186 } else {
6187 av_log(mov->fc, AV_LOG_WARNING, "Unsupported PRFT box configuration: %d\n",
6188 mov->write_prft);
6189 return 0;
6190 }
6191
6192 avio_wb32(pb, 0); // Size place holder
6193 ffio_wfourcc(pb, "prft"); // Type
6194 avio_w8(pb, 1); // Version
6195 avio_wb24(pb, flags); // Flags
6196 avio_wb32(pb, first_track->track_id); // reference track ID
6197 avio_wb64(pb, ntp_ts); // NTP time stamp
6198 avio_wb64(pb, first_track->cluster[0].pts); //media time
6199 return update_size(pb, pos);
6200}
6201
6202static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
6203 int64_t mdat_size)
6204{
6205 AVIOContext *avio_buf;
6206 int ret, moof_size;
6207
6208 if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
6209 return ret;
6210 mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
6211 moof_size = ffio_close_null_buf(avio_buf);
6212 if (moof_size < 0)
6213 return moof_size;
6214
6215 if (mov->flags & FF_MOV_FLAG_DASH &&
6217 mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
6218
6219 if (mov->write_prft > MOV_PRFT_NONE && mov->write_prft < MOV_PRFT_NB)
6220 mov_write_prft_tag(pb, mov, tracks);
6221
6222 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX ||
6223 !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) ||
6224 mov->ism_lookahead) {
6225 if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
6226 return ret;
6227 if (!(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) &&
6229 mov_prune_frag_info(mov, tracks, mov->ism_lookahead + 1);
6230 }
6231 }
6232
6233 return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
6234}
6235
6237{
6238 int64_t pos = avio_tell(pb);
6239 int i;
6240
6241 avio_wb32(pb, 0); /* size placeholder */
6242 ffio_wfourcc(pb, "tfra");
6243 avio_w8(pb, 1); /* version */
6244 avio_wb24(pb, 0);
6245
6246 avio_wb32(pb, track->track_id);
6247 avio_wb32(pb, 0); /* length of traf/trun/sample num */
6248 avio_wb32(pb, track->nb_frag_info);
6249 for (i = 0; i < track->nb_frag_info; i++) {
6250 avio_wb64(pb, track->frag_info[i].time);
6251 avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
6252 avio_w8(pb, 1); /* traf number */
6253 avio_w8(pb, 1); /* trun number */
6254 avio_w8(pb, 1); /* sample number */
6255 }
6256
6257 return update_size(pb, pos);
6258}
6259
6261{
6262 AVIOContext *mfra_pb;
6263 int i, ret, sz;
6264 uint8_t *buf;
6265
6266 ret = avio_open_dyn_buf(&mfra_pb);
6267 if (ret < 0)
6268 return ret;
6269
6270 avio_wb32(mfra_pb, 0); /* size placeholder */
6271 ffio_wfourcc(mfra_pb, "mfra");
6272 /* An empty mfra atom is enough to indicate to the publishing point that
6273 * the stream has ended. */
6274 if (mov->flags & FF_MOV_FLAG_ISML)
6275 goto done_mfra;
6276
6277 for (i = 0; i < mov->nb_tracks; i++) {
6278 MOVTrack *track = &mov->tracks[i];
6279 if (track->nb_frag_info)
6280 mov_write_tfra_tag(mfra_pb, track);
6281 }
6282
6283 avio_wb32(mfra_pb, 16);
6284 ffio_wfourcc(mfra_pb, "mfro");
6285 avio_wb32(mfra_pb, 0); /* version + flags */
6286 avio_wb32(mfra_pb, avio_tell(mfra_pb) + 4);
6287
6288done_mfra:
6289
6290 sz = update_size(mfra_pb, 0);
6291 ret = avio_get_dyn_buf(mfra_pb, &buf);
6292 avio_write(pb, buf, ret);
6293 ffio_free_dyn_buf(&mfra_pb);
6294
6295 return sz;
6296}
6297
6299{
6300 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
6301 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
6302
6303 mov->mdat_pos = avio_tell(pb);
6304 avio_wb32(pb, 0); /* size placeholder*/
6305 ffio_wfourcc(pb, "mdat");
6306 return 0;
6307}
6308
6310 int has_h264, int has_video, int write_minor)
6311{
6312 MOVMuxContext *mov = s->priv_data;
6313 int minor = 0x200;
6314
6315 if (mov->major_brand && strlen(mov->major_brand) >= 4)
6316 ffio_wfourcc(pb, mov->major_brand);
6317 else if (mov->mode == MODE_3GP) {
6318 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
6319 minor = has_h264 ? 0x100 : 0x200;
6320 } else if (mov->mode == MODE_AVIF) {
6321 ffio_wfourcc(pb, mov->is_animated_avif ? "avis" : "avif");
6322 minor = 0;
6323 } else if (mov->mode & MODE_3G2) {
6324 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
6325 minor = has_h264 ? 0x20000 : 0x10000;
6326 } else if (mov->mode == MODE_PSP)
6327 ffio_wfourcc(pb, "MSNV");
6328 else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_FRAGMENT &&
6330 ffio_wfourcc(pb, "iso6"); // Required when using signed CTS offsets in trun boxes
6331 else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
6332 ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
6333 else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
6334 ffio_wfourcc(pb, "iso4");
6335 else if (mov->mode == MODE_MP4)
6336 ffio_wfourcc(pb, "isom");
6337 else if (mov->mode == MODE_IPOD)
6338 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
6339 else if (mov->mode == MODE_ISM)
6340 ffio_wfourcc(pb, "isml");
6341 else if (mov->mode == MODE_F4V)
6342 ffio_wfourcc(pb, "f4v ");
6343 else
6344 ffio_wfourcc(pb, "qt ");
6345
6346 if (write_minor)
6347 avio_wb32(pb, minor);
6348}
6349
6351{
6352 MOVMuxContext *mov = s->priv_data;
6353 int64_t pos = avio_tell(pb);
6354 int has_h264 = 0, has_av1 = 0, has_video = 0, has_dolby = 0, has_id3 = 0;
6355 int has_iamf = 0;
6356
6357#if CONFIG_IAMFENC
6358 for (int i = 0; i < s->nb_stream_groups; i++) {
6359 const AVStreamGroup *stg = s->stream_groups[i];
6360
6363 has_iamf = 1;
6364 break;
6365 }
6366 }
6367#endif
6368 for (int i = 0; i < mov->nb_streams; i++) {
6369 AVStream *st = mov->tracks[i].st;
6370 if (is_cover_image(st))
6371 continue;
6373 has_video = 1;
6375 has_h264 = 1;
6376 if (st->codecpar->codec_id == AV_CODEC_ID_AV1)
6377 has_av1 = 1;
6378 if (st->codecpar->codec_id == AV_CODEC_ID_AC3 ||
6384 has_dolby = 1;
6386 has_id3 = 1;
6387 }
6388
6389 avio_wb32(pb, 0); /* size */
6390 ffio_wfourcc(pb, "ftyp");
6391
6392 // Write major brand
6393 mov_write_ftyp_tag_internal(pb, s, has_h264, has_video, 1);
6394 // Write the major brand as the first compatible brand as well
6395 mov_write_ftyp_tag_internal(pb, s, has_h264, has_video, 0);
6396
6397 // Write compatible brands, ensuring that we don't write the major brand as a
6398 // compatible brand a second time.
6399 if (mov->mode == MODE_ISM) {
6400 ffio_wfourcc(pb, "piff");
6401#if CONFIG_AVIF_MUXER
6402 } else if (mov->mode == MODE_AVIF) {
6403 const MOVTrack *trk = &mov->tracks[0];
6404 const AVPixFmtDescriptor *pix_fmt_desc =
6405 av_pix_fmt_desc_get(s->streams[0]->codecpar->format);
6406 int depth = 0, log2_chroma_w, log2_chroma_h;
6407
6408 if (pix_fmt_desc->nb_components > 0) {
6409 depth = pix_fmt_desc->comp[0].depth;
6410 log2_chroma_w = pix_fmt_desc->log2_chroma_w;
6411 log2_chroma_h = pix_fmt_desc->log2_chroma_h;
6412 } else {
6415 trk->extradata_size[trk->last_stsd_index]);
6416 if (!ret) {
6417 depth = seq.bitdepth;
6418 log2_chroma_w = seq.chroma_subsampling_x;
6419 log2_chroma_h = seq.chroma_subsampling_y;
6420 }
6421 }
6422 if (mov->is_animated_avif) {
6423 // For animated AVIF, major brand is "avis". Add "avif" as a
6424 // compatible brand.
6425 ffio_wfourcc(pb, "avif");
6426 ffio_wfourcc(pb, "msf1");
6427 ffio_wfourcc(pb, "iso8");
6428 }
6429 ffio_wfourcc(pb, "mif1");
6430 ffio_wfourcc(pb, "miaf");
6431 if (depth == 8 || depth == 10) {
6432 // MA1B and MA1A brands are based on AV1 profile. Short hand for
6433 // computing that is based on chroma subsampling type. 420 chroma
6434 // subsampling is MA1B. 444 chroma subsampling is MA1A.
6435 if (!log2_chroma_w && !log2_chroma_h) {
6436 // 444 chroma subsampling.
6437 ffio_wfourcc(pb, "MA1A");
6438 } else {
6439 // 420 chroma subsampling.
6440 ffio_wfourcc(pb, "MA1B");
6441 }
6442 }
6443#endif
6444 } else if (mov->mode != MODE_MOV) {
6445 // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
6446 // brand, if not already the major brand. This is compatible with users that
6447 // don't understand tfdt.
6448 if (mov->mode == MODE_MP4) {
6449 if (mov->flags & FF_MOV_FLAG_CMAF)
6450 ffio_wfourcc(pb, "cmfc");
6452 ffio_wfourcc(pb, "iso6");
6453 if (has_av1)
6454 ffio_wfourcc(pb, "av01");
6455 if (has_dolby)
6456 ffio_wfourcc(pb, "dby1");
6457 if (has_iamf)
6458 ffio_wfourcc(pb, "iamf");
6459 } else {
6460 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
6461 ffio_wfourcc(pb, "iso6");
6463 ffio_wfourcc(pb, "iso5");
6465 ffio_wfourcc(pb, "iso4");
6466 }
6467 // Brands prior to iso5 can't be signaled when using default-base-is-moof
6468 if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
6469 // write isom for mp4 only if it it's not the major brand already.
6471 ffio_wfourcc(pb, "isom");
6472 ffio_wfourcc(pb, "iso2");
6473 if (has_h264)
6474 ffio_wfourcc(pb, "avc1");
6475 }
6476 }
6477
6478 if (mov->mode == MODE_MP4)
6479 ffio_wfourcc(pb, "mp41");
6480
6482 ffio_wfourcc(pb, "dash");
6483
6484 if (has_id3)
6485 ffio_wfourcc(pb, "aid3");
6486
6487 return update_size(pb, pos);
6488}
6489
6491{
6492 AVStream *video_st = s->streams[0];
6493 AVCodecParameters *video_par = s->streams[0]->codecpar;
6494 AVCodecParameters *audio_par = s->streams[1]->codecpar;
6495 int audio_rate = audio_par->sample_rate;
6496 int64_t frame_rate = video_st->avg_frame_rate.den ?
6497 (video_st->avg_frame_rate.num * 0x10000LL) / video_st->avg_frame_rate.den :
6498 0;
6499 int audio_kbitrate = audio_par->bit_rate / 1000;
6500 int video_kbitrate = FFMIN(video_par->bit_rate / 1000, 800 - audio_kbitrate);
6501
6502 if (frame_rate < 0 || frame_rate > INT32_MAX) {
6503 av_log(s, AV_LOG_ERROR, "Frame rate %f outside supported range\n", frame_rate / (double)0x10000);
6504 return AVERROR(EINVAL);
6505 }
6506
6507 avio_wb32(pb, 0x94); /* size */
6508 ffio_wfourcc(pb, "uuid");
6509 ffio_wfourcc(pb, "PROF");
6510
6511 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
6512 avio_wb32(pb, 0xbb88695c);
6513 avio_wb32(pb, 0xfac9c740);
6514
6515 avio_wb32(pb, 0x0); /* ? */
6516 avio_wb32(pb, 0x3); /* 3 sections ? */
6517
6518 avio_wb32(pb, 0x14); /* size */
6519 ffio_wfourcc(pb, "FPRF");
6520 avio_wb32(pb, 0x0); /* ? */
6521 avio_wb32(pb, 0x0); /* ? */
6522 avio_wb32(pb, 0x0); /* ? */
6523
6524 avio_wb32(pb, 0x2c); /* size */
6525 ffio_wfourcc(pb, "APRF"); /* audio */
6526 avio_wb32(pb, 0x0);
6527 avio_wb32(pb, 0x2); /* TrackID */
6528 ffio_wfourcc(pb, "mp4a");
6529 avio_wb32(pb, 0x20f);
6530 avio_wb32(pb, 0x0);
6531 avio_wb32(pb, audio_kbitrate);
6532 avio_wb32(pb, audio_kbitrate);
6533 avio_wb32(pb, audio_rate);
6534 avio_wb32(pb, audio_par->ch_layout.nb_channels);
6535
6536 avio_wb32(pb, 0x34); /* size */
6537 ffio_wfourcc(pb, "VPRF"); /* video */
6538 avio_wb32(pb, 0x0);
6539 avio_wb32(pb, 0x1); /* TrackID */
6540 if (video_par->codec_id == AV_CODEC_ID_H264) {
6541 ffio_wfourcc(pb, "avc1");
6542 avio_wb16(pb, 0x014D);
6543 avio_wb16(pb, 0x0015);
6544 } else {
6545 ffio_wfourcc(pb, "mp4v");
6546 avio_wb16(pb, 0x0000);
6547 avio_wb16(pb, 0x0103);
6548 }
6549 avio_wb32(pb, 0x0);
6550 avio_wb32(pb, video_kbitrate);
6551 avio_wb32(pb, video_kbitrate);
6552 avio_wb32(pb, frame_rate);
6553 avio_wb32(pb, frame_rate);
6554 avio_wb16(pb, video_par->width);
6555 avio_wb16(pb, video_par->height);
6556 avio_wb32(pb, 0x010001); /* ? */
6557
6558 return 0;
6559}
6560
6562{
6563 MOVMuxContext *mov = s->priv_data;
6564 int i;
6565
6567 if (mov->mode == MODE_PSP) {
6568 int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
6569 for (i = 0; i < mov->nb_streams; i++) {
6570 AVStream *st = mov->tracks[i].st;
6571 if (is_cover_image(st))
6572 continue;
6574 video_streams_nb++;
6575 else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
6576 audio_streams_nb++;
6577 else
6578 other_streams_nb++;
6579 }
6580
6581 if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
6582 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
6583 return AVERROR(EINVAL);
6584 }
6585 return mov_write_uuidprof_tag(pb, s);
6586 }
6587 return 0;
6588}
6589
6591{
6592 uint32_t c = -1;
6593 int i, closed_gop = 0;
6594
6595 for (i = 0; i < pkt->size - 4; i++) {
6596 c = (c << 8) + pkt->data[i];
6597 if (c == 0x1b8) { // gop
6598 closed_gop = pkt->data[i + 4] >> 6 & 0x01;
6599 } else if (c == 0x100) { // pic
6600 int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
6601 if (!temp_ref || closed_gop) // I picture is not reordered
6603 else
6605 break;
6606 }
6607 }
6608 return 0;
6609}
6610
6612{
6613 const uint8_t *start, *next, *end = pkt->data + pkt->size;
6614 int seq = 0, entry = 0;
6615 int key = pkt->flags & AV_PKT_FLAG_KEY;
6616 start = find_next_marker(pkt->data, end);
6617 for (next = start; next < end; start = next) {
6618 next = find_next_marker(start + 4, end);
6619 switch (AV_RB32(start)) {
6620 case VC1_CODE_SEQHDR:
6621 seq = 1;
6622 break;
6624 entry = 1;
6625 break;
6626 case VC1_CODE_SLICE:
6627 trk->vc1_info.slices = 1;
6628 break;
6629 }
6630 }
6631 if (!trk->entry && trk->vc1_info.first_packet_seen)
6633 if (!trk->entry && !trk->vc1_info.first_frag_written) {
6634 /* First packet in first fragment */
6635 trk->vc1_info.first_packet_seq = seq;
6637 trk->vc1_info.first_packet_seen = 1;
6638 } else if ((seq && !trk->vc1_info.packet_seq) ||
6639 (entry && !trk->vc1_info.packet_entry)) {
6640 int i;
6641 for (i = 0; i < trk->entry; i++)
6642 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
6643 trk->has_keyframes = 0;
6644 if (seq)
6645 trk->vc1_info.packet_seq = 1;
6646 if (entry)
6647 trk->vc1_info.packet_entry = 1;
6648 if (!trk->vc1_info.first_frag_written) {
6649 /* First fragment */
6650 if ((!seq || trk->vc1_info.first_packet_seq) &&
6651 (!entry || trk->vc1_info.first_packet_entry)) {
6652 /* First packet had the same headers as this one, readd the
6653 * sync sample flag. */
6654 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
6655 trk->has_keyframes = 1;
6656 }
6657 }
6658 }
6659 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
6660 key = seq && entry;
6661 else if (trk->vc1_info.packet_seq)
6662 key = seq;
6663 else if (trk->vc1_info.packet_entry)
6664 key = entry;
6665 if (key) {
6666 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
6667 trk->has_keyframes++;
6668 }
6669}
6670
6672{
6673 int length;
6674
6675 if (pkt->size < 8)
6676 return;
6677
6678 length = (AV_RB16(pkt->data) & 0xFFF) * 2;
6679 if (length < 8 || length > pkt->size)
6680 return;
6681
6682 if (AV_RB32(pkt->data + 4) == 0xF8726FBA) {
6683 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
6684 trk->has_keyframes++;
6685 }
6686
6687 return;
6688}
6689
6691{
6692 MOVMuxContext *mov = s->priv_data;
6693 int ret, buf_size;
6694 uint8_t *buf;
6695 int i, offset;
6696
6697 if (!track->mdat_buf)
6698 return 0;
6699 if (!mov->mdat_buf) {
6700 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
6701 return ret;
6702 }
6703 buf_size = avio_get_dyn_buf(track->mdat_buf, &buf);
6704
6705 offset = avio_tell(mov->mdat_buf);
6706 avio_write(mov->mdat_buf, buf, buf_size);
6708
6709 for (i = track->entries_flushed; i < track->entry; i++)
6710 track->cluster[i].pos += offset;
6711 track->entries_flushed = track->entry;
6712 return 0;
6713}
6714
6716{
6717 MOVMuxContext *mov = s->priv_data;
6718 AVPacket *squashed_packet = mov->pkt;
6719 int ret = AVERROR_BUG;
6720
6721 switch (track->st->codecpar->codec_id) {
6722 case AV_CODEC_ID_TTML: {
6723 int had_packets = !!track->squashed_packet_queue.head;
6724
6725 if ((ret = ff_mov_generate_squashed_ttml_packet(s, track, squashed_packet)) < 0) {
6726 goto finish_squash;
6727 }
6728
6729 // We have generated a padding packet (no actual input packets in
6730 // queue) and its duration is zero. Skipping writing it.
6731 if (!had_packets && squashed_packet->duration == 0) {
6732 goto finish_squash;
6733 }
6734
6735 track->end_reliable = 1;
6736 break;
6737 }
6738 default:
6739 ret = AVERROR(EINVAL);
6740 goto finish_squash;
6741 }
6742
6743 squashed_packet->stream_index = track->st->index;
6744
6745 ret = mov_write_single_packet(s, squashed_packet);
6746
6747finish_squash:
6748 av_packet_unref(squashed_packet);
6749
6750 return ret;
6751}
6752
6754{
6755 MOVMuxContext *mov = s->priv_data;
6756
6757 for (int i = 0; i < mov->nb_streams; i++) {
6758 MOVTrack *track = &mov->tracks[i];
6759 int ret = AVERROR_BUG;
6760
6761 if (track->squash_fragment_samples_to_one && !track->entry) {
6762 if ((ret = mov_write_squashed_packet(s, track)) < 0) {
6764 "Failed to write squashed packet for %s stream with "
6765 "index %d and track id %d. Error: %s\n",
6767 track->st->index, track->track_id,
6768 av_err2str(ret));
6769 return ret;
6770 }
6771 }
6772 }
6773
6774 return 0;
6775}
6776
6778 int64_t ref_pos)
6779{
6780 int i;
6781 if (!track->entry)
6782 return 0;
6784 for (i = 0; i < track->entry; i++)
6785 track->cluster[i].pos += ref_pos + track->data_offset;
6786 if (track->cluster_written == 0) {
6787 // First flush. Chunking for this fragment may already have been
6788 // done, either if we didn't use empty_moov, or if we did use
6789 // delay_moov. In either case, reset chunking here.
6790 for (i = 0; i < track->entry; i++) {
6791 track->cluster[i].chunkNum = 0;
6792 track->cluster[i].samples_in_chunk = track->cluster[i].entries;
6793 }
6794 }
6796 track->entry_written + track->entry,
6797 sizeof(*track->cluster)))
6798 return AVERROR(ENOMEM);
6799 memcpy(&track->cluster_written[track->entry_written],
6800 track->cluster, track->entry * sizeof(*track->cluster));
6801 track->entry_written += track->entry;
6802 }
6803 track->entry = 0;
6804 track->entries_flushed = 0;
6805 track->end_reliable = 0;
6806 return 0;
6807}
6808
6809static int mov_flush_fragment(AVFormatContext *s, int force)
6810{
6811 MOVMuxContext *mov = s->priv_data;
6812 int i, first_track = -1;
6813 int64_t mdat_size = 0, mdat_start = 0;
6814 int ret;
6815 int has_video = 0, starts_with_key = 0, first_video_track = 1;
6816
6817 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
6818 return 0;
6819
6820 // Check if we have any tracks that require squashing.
6821 // In that case, we'll have to write the packet here.
6822 if ((ret = mov_write_squashed_packets(s)) < 0)
6823 return ret;
6824
6825 // Try to fill in the duration of the last packet in each stream
6826 // from queued packets in the interleave queues. If the flushing
6827 // of fragments was triggered automatically by an AVPacket, we
6828 // already have reliable info for the end of that track, but other
6829 // tracks may need to be filled in.
6830 for (i = 0; i < mov->nb_streams; i++) {
6831 MOVTrack *track = &mov->tracks[i];
6832 if (!track->end_reliable) {
6833 const AVPacket *pkt = ff_interleaved_peek(s, i);
6834 if (pkt) {
6835 int64_t offset, dts, pts;
6837 pts = pkt->pts + offset;
6838 dts = pkt->dts + offset;
6839 if (track->dts_shift != AV_NOPTS_VALUE)
6840 dts += track->dts_shift;
6841 track->track_duration = dts - track->start_dts;
6842 if (pts != AV_NOPTS_VALUE)
6843 track->end_pts = pts;
6844 else
6845 track->end_pts = dts;
6846 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
6847 track->elst_end_pts = track->end_pts;
6848 }
6849 }
6850 }
6851
6852 for (i = 0; i < mov->nb_tracks; i++) {
6853 MOVTrack *track = &mov->tracks[i];
6854 if (track->entry <= 1)
6855 continue;
6856 // Sample durations are calculated as the diff of dts values,
6857 // but for the last sample in a fragment, we don't know the dts
6858 // of the first sample in the next fragment, so we have to rely
6859 // on what was set as duration in the AVPacket. Not all callers
6860 // set this though, so we might want to replace it with an
6861 // estimate if it currently is zero.
6862 if (get_cluster_duration(track, track->entry - 1) != 0)
6863 continue;
6864 // Use the duration (i.e. dts diff) of the second last sample for
6865 // the last one. This is a wild guess (and fatal if it turns out
6866 // to be too long), but probably the best we can do - having a zero
6867 // duration is bad as well.
6868 track->track_duration += get_cluster_duration(track, track->entry - 2);
6869 track->end_pts += get_cluster_duration(track, track->entry - 2);
6870 if (!mov->missing_duration_warned) {
6872 "Estimating the duration of the last packet in a "
6873 "fragment, consider setting the duration field in "
6874 "AVPacket instead.\n");
6875 mov->missing_duration_warned = 1;
6876 }
6877 }
6878
6879 if (!mov->moov_written) {
6880 int64_t pos = avio_tell(s->pb);
6881 uint8_t *buf;
6882 int buf_size, moov_size;
6883
6884 for (i = 0; i < mov->nb_tracks; i++)
6885 if (!mov->tracks[i].entry && !is_cover_image(mov->tracks[i].st))
6886 break;
6887 /* Don't write the initial moov unless all tracks have data */
6888 if (i < mov->nb_tracks && !force)
6889 return 0;
6890
6891 moov_size = get_moov_size(s);
6892 for (i = 0; i < mov->nb_tracks; i++)
6893 mov->tracks[i].data_offset = pos + moov_size + 8;
6894
6896 if (mov->flags & FF_MOV_FLAG_DELAY_MOOV &&
6899 if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
6900 return ret;
6901
6902 if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
6903 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
6904 mov->reserved_header_pos = avio_tell(s->pb);
6906 mov->moov_written = 1;
6907 return 0;
6908 }
6909
6910 if (mov->mdat_buf) {
6911 buf_size = avio_get_dyn_buf(mov->mdat_buf, &buf);
6912 avio_wb32(s->pb, buf_size + 8);
6913 ffio_wfourcc(s->pb, "mdat");
6914 avio_write(s->pb, buf, buf_size);
6916 }
6917
6918 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
6919 mov->reserved_header_pos = avio_tell(s->pb);
6920
6921 mov->moov_written = 1;
6922 mov->mdat_size = 0;
6923 for (i = 0; i < mov->nb_tracks; i++)
6924 mov_finish_fragment(mov, &mov->tracks[i], 0);
6926 return 0;
6927 }
6928
6929 if (mov->frag_interleave) {
6930 for (i = 0; i < mov->nb_tracks; i++) {
6931 MOVTrack *track = &mov->tracks[i];
6932 int ret;
6933 if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
6934 return ret;
6935 }
6936
6937 if (!mov->mdat_buf)
6938 return 0;
6939 mdat_size = avio_tell(mov->mdat_buf);
6940 }
6941
6942 for (i = 0; i < mov->nb_tracks; i++) {
6943 MOVTrack *track = &mov->tracks[i];
6945 track->data_offset = 0;
6946 else
6947 track->data_offset = mdat_size;
6948 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
6949 has_video = 1;
6950 if (first_video_track) {
6951 if (track->entry)
6952 starts_with_key = track->cluster[0].flags & MOV_SYNC_SAMPLE;
6953 first_video_track = 0;
6954 }
6955 }
6956 if (!track->entry)
6957 continue;
6958 if (track->mdat_buf)
6959 mdat_size += avio_tell(track->mdat_buf);
6960 if (first_track < 0)
6961 first_track = i;
6962 }
6963
6964 if (!mdat_size)
6965 return 0;
6966
6967 avio_write_marker(s->pb,
6968 av_rescale(mov->tracks[first_track].cluster[0].dts, AV_TIME_BASE, mov->tracks[first_track].timescale),
6969 (has_video ? starts_with_key : mov->tracks[first_track].cluster[0].flags & MOV_SYNC_SAMPLE) ? AVIO_DATA_MARKER_SYNC_POINT : AVIO_DATA_MARKER_BOUNDARY_POINT);
6970
6971 for (i = first_track; i < mov->nb_tracks; i++) {
6972 MOVTrack *track = &mov->tracks[i];
6973 int buf_size, write_moof = 1, moof_tracks = -1;
6974 uint8_t *buf;
6975
6976 if (!track->entry)
6977 continue;
6978 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
6979 mdat_size = avio_tell(track->mdat_buf);
6980 moof_tracks = i;
6981 } else {
6982 write_moof = i == first_track;
6983 }
6984
6985 if (write_moof) {
6987
6988 mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
6989 mov->fragments++;
6990
6991 if (track->cenc.aes_ctr)
6992 ff_mov_cenc_flush(&track->cenc);
6993
6994 avio_wb32(s->pb, mdat_size + 8);
6995 ffio_wfourcc(s->pb, "mdat");
6996 mdat_start = avio_tell(s->pb);
6997 }
6998
6999 mov_finish_fragment(mov, &mov->tracks[i], mdat_start);
7000 if (!mov->frag_interleave) {
7001 if (!track->mdat_buf)
7002 continue;
7003 buf_size = avio_get_dyn_buf(track->mdat_buf, &buf);
7004 avio_write(s->pb, buf, buf_size);
7006 } else {
7007 if (!mov->mdat_buf)
7008 continue;
7009 buf_size = avio_get_dyn_buf(mov->mdat_buf, &buf);
7010 avio_write(s->pb, buf, buf_size);
7012 }
7013 }
7014
7015 mov->mdat_size = 0;
7016
7018 return 0;
7019}
7020
7022{
7023 MOVMuxContext *mov = s->priv_data;
7024 int had_moov = mov->moov_written;
7025 int ret = mov_flush_fragment(s, force);
7026 if (ret < 0)
7027 return ret;
7028 // If using delay_moov, the first flush only wrote the moov,
7029 // not the actual moof+mdat pair, thus flush once again.
7030 if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
7031 ret = mov_flush_fragment(s, force);
7032 return ret;
7033}
7034
7036{
7037 int64_t ref;
7038 uint64_t duration;
7039
7040 if (trk->entry) {
7041 ref = trk->cluster[trk->entry - 1].dts;
7042 } else if ( trk->start_dts != AV_NOPTS_VALUE
7043 && !trk->frag_discont) {
7044 ref = trk->start_dts + trk->track_duration;
7045 } else
7046 ref = pkt->dts; // Skip tests for the first packet
7047
7048 if (trk->dts_shift != AV_NOPTS_VALUE) {
7049 /* With negative CTS offsets we have set an offset to the DTS,
7050 * reverse this for the check. */
7051 ref -= trk->dts_shift;
7052 }
7053
7054 duration = pkt->dts - ref;
7055 if (pkt->dts < ref || duration >= INT_MAX) {
7056 av_log(s, AV_LOG_WARNING, "Packet duration: %"PRId64" / dts: %"PRId64" in stream %d is out of range\n",
7057 duration, pkt->dts, pkt->stream_index);
7058
7059 pkt->dts = ref + 1;
7060 pkt->pts = AV_NOPTS_VALUE;
7061 }
7062
7063 if (pkt->duration < 0 || pkt->duration > INT_MAX) {
7064 av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" in stream %d is invalid\n", pkt->duration, pkt->stream_index);
7065 return AVERROR(EINVAL);
7066 }
7067 return 0;
7068}
7069
7071 const AVPacket *pkt, AVRational src_time_base)
7072{
7073 MOVMuxContext *mov = s->priv_data;
7074 AVStream *st;
7075 MOVTrack *track;
7076 int64_t offset, dts, pts, candidate_duration;
7077
7078 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT) ||
7079 stream_index < 0 || stream_index >= s->nb_streams ||
7080 pkt->dts == AV_NOPTS_VALUE)
7081 return 0;
7082
7083 st = s->streams[stream_index];
7084 track = st->priv_data;
7085 if (!track->entry || track->start_dts == AV_NOPTS_VALUE)
7086 return 0;
7087
7088 if (ff_get_muxer_ts_offset(s, stream_index, &offset) < 0)
7089 return 0;
7090
7091 dts = av_rescale_q(pkt->dts, src_time_base, st->time_base) + offset;
7092 pts = pkt->pts == AV_NOPTS_VALUE
7094 : av_rescale_q(pkt->pts, src_time_base, st->time_base) + offset;
7095 if (track->dts_shift != AV_NOPTS_VALUE)
7096 dts += track->dts_shift;
7097
7098 candidate_duration = dts - track->start_dts;
7099 if (dts <= track->cluster[track->entry - 1].dts ||
7100 candidate_duration < 0 || candidate_duration >= track->track_duration)
7101 return 0;
7102
7103 track->track_duration = candidate_duration;
7104 track->end_pts = pts != AV_NOPTS_VALUE ? pts : dts;
7105 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
7106 track->elst_end_pts = track->end_pts;
7107 track->end_reliable = 1;
7108
7109 return 1;
7110}
7111
7113{
7114 MOVMuxContext *mov = s->priv_data;
7115 AVIOContext *pb = s->pb;
7116 MOVTrack *trk;
7117 AVCodecParameters *par;
7119 unsigned int samples_in_chunk = 0;
7121 int size = pkt->size, ret = 0, offset = 0;
7122 size_t prft_size;
7123 uint8_t *reformatted_data = NULL;
7124
7125 if (pkt->stream_index < s->nb_streams)
7126 trk = s->streams[pkt->stream_index]->priv_data;
7127 else // Timecode or chapter
7128 trk = &mov->tracks[pkt->stream_index];
7129 par = trk->par;
7130
7131 ret = check_pkt(s, trk, pkt);
7132 if (ret < 0)
7133 return ret;
7134
7135 if (pkt->pts != AV_NOPTS_VALUE &&
7136 (uint64_t)pkt->dts - pkt->pts != (int32_t)((uint64_t)pkt->dts - pkt->pts)) {
7137 av_log(s, AV_LOG_WARNING, "pts/dts pair unsupported\n");
7138 return AVERROR_PATCHWELCOME;
7139 }
7140
7141 if (mov->flags & FF_MOV_FLAG_FRAGMENT || mov->mode == MODE_AVIF) {
7142 int ret;
7143 if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
7144 if (mov->frag_interleave && mov->fragments > 0) {
7145 if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
7146 if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
7147 return ret;
7148 }
7149 }
7150
7151 if (!trk->mdat_buf) {
7152 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
7153 return ret;
7154 }
7155 pb = trk->mdat_buf;
7156 } else {
7157 if (!mov->mdat_buf) {
7158 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
7159 return ret;
7160 }
7161 pb = mov->mdat_buf;
7162 }
7163 }
7164
7165 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
7166 /* We must find out how many AMR blocks there are in one packet */
7167 static const uint16_t packed_size[16] =
7168 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
7169 int len = 0;
7170
7171 while (len < size && samples_in_chunk < 100) {
7172 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
7173 samples_in_chunk++;
7174 }
7175 if (samples_in_chunk > 1) {
7176 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
7177 return -1;
7178 }
7179 } else if (par->codec_id == AV_CODEC_ID_ADPCM_MS ||
7181 samples_in_chunk = trk->par->frame_size;
7182 } else if (trk->sample_size)
7183 samples_in_chunk = size / trk->sample_size;
7184 else
7185 samples_in_chunk = 1;
7186
7187 if (samples_in_chunk < 1) {
7188 av_log(s, AV_LOG_ERROR, "fatal error, input packet contains no samples\n");
7189 return AVERROR_PATCHWELCOME;
7190 }
7191
7192 /* copy extradata if it exists */
7193 if (trk->extradata_size[0] == 0 && par->extradata_size > 0 &&
7194 !TAG_IS_AVCI(trk->tag) &&
7195 (par->codec_id != AV_CODEC_ID_DNXHD)) {
7196 trk->extradata[0] = av_memdup(par->extradata, par->extradata_size);
7197 if (!trk->extradata[0]) {
7198 ret = AVERROR(ENOMEM);
7199 goto err;
7200 }
7201 trk->extradata_size[0] = par->extradata_size;
7202 }
7203
7204 if ((par->codec_id == AV_CODEC_ID_DNXHD ||
7205 par->codec_id == AV_CODEC_ID_H264 ||
7206 par->codec_id == AV_CODEC_ID_HEVC ||
7207 par->codec_id == AV_CODEC_ID_VVC ||
7208 par->codec_id == AV_CODEC_ID_VP9 ||
7209 par->codec_id == AV_CODEC_ID_EVC ||
7210 par->codec_id == AV_CODEC_ID_LCEVC ||
7211 par->codec_id == AV_CODEC_ID_TRUEHD) && !trk->extradata_size[0] &&
7212 !TAG_IS_AVCI(trk->tag)) {
7213 /* copy frame to create needed atoms */
7214 trk->extradata_size[0] = size;
7216 if (!trk->extradata[0]) {
7217 ret = AVERROR(ENOMEM);
7218 goto err;
7219 }
7220 memcpy(trk->extradata[0], pkt->data, size);
7221 memset(trk->extradata[0] + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
7222 }
7223
7224 const AVPacketSideData *sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems, AV_PKT_DATA_NEW_EXTRADATA);
7225 if (pkt->size && sd && sd->size > 0) {
7226 int i;
7227 for (i = 0; i < trk->stsd_count; i++) {
7228 if (trk->extradata_size[i] == sd->size && !memcmp(trk->extradata[i], sd->data, sd->size))
7229 break;
7230 }
7231
7232 if (i < trk->stsd_count)
7233 trk->last_stsd_index = i;
7234 else if (trk->stsd_count <= INT_MAX - 1) {
7235 int new_count = trk->stsd_count + 1;
7236 uint8_t **extradata = av_realloc_array(trk->extradata, new_count, sizeof(*trk->extradata));
7237 if (!extradata)
7238 return AVERROR(ENOMEM);
7239 trk->extradata = extradata;
7240
7241 int *extradata_size = av_realloc_array(trk->extradata_size, new_count, sizeof(*trk->extradata_size));
7242 if (!extradata_size)
7243 return AVERROR(ENOMEM);
7244 trk->extradata_size = extradata_size;
7245
7246 trk->extradata[trk->stsd_count] = av_memdup(sd->data, sd->size);
7247 if (!trk->extradata[trk->stsd_count])
7248 return AVERROR(ENOMEM);
7249
7250 trk->extradata_size[trk->stsd_count] = sd->size;
7251 trk->last_stsd_index = trk->stsd_count;
7252 trk->stsd_count = new_count;
7253 } else
7254 return AVERROR(ENOMEM);
7255 }
7256
7257 if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
7258 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
7259 if (!trk->st->nb_frames) {
7260 av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
7261 "use the audio bitstream filter 'aac_adtstoasc' to fix it "
7262 "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
7263 return -1;
7264 }
7265 av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
7266 }
7267 if (par->codec_id == AV_CODEC_ID_H264 && trk->extradata_size[trk->last_stsd_index] > 0 &&
7268 *(uint8_t *)trk->extradata[trk->last_stsd_index] != 1 && !TAG_IS_AVCI(trk->tag)) {
7269 /* from x264 or from bytestream H.264 */
7270 /* NAL reformatting needed */
7271 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7272 ret = ff_nal_parse_units_buf(pkt->data, &reformatted_data,
7273 &size);
7274 if (ret < 0)
7275 return ret;
7276 avio_write(pb, reformatted_data, size);
7277 } else {
7278 if (trk->cenc.aes_ctr) {
7279 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
7280 if (size < 0) {
7281 ret = size;
7282 goto err;
7283 }
7284 } else {
7285 size = ff_nal_parse_units(pb, pkt->data, pkt->size);
7286 }
7287 }
7288 } else if (par->codec_id == AV_CODEC_ID_HEVC && trk->extradata_size[trk->last_stsd_index] > 6 &&
7289 (AV_RB24(trk->extradata[trk->last_stsd_index]) == 1 || AV_RB32(trk->extradata[trk->last_stsd_index]) == 1)) {
7290 /* extradata is Annex B, assume the bitstream is too and convert it */
7291 int filter_ps = (trk->tag == MKTAG('h','v','c','1'));
7292 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7293 ret = ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data,
7294 &size, filter_ps, NULL);
7295 if (ret < 0)
7296 return ret;
7297 avio_write(pb, reformatted_data, size);
7298 } else {
7299 if (trk->cenc.aes_ctr) {
7300 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
7301 if (size < 0) {
7302 ret = size;
7303 goto err;
7304 }
7305 } else {
7306 size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, filter_ps, NULL);
7307 }
7308 }
7309 } else if (par->codec_id == AV_CODEC_ID_VVC && trk->extradata_size[trk->last_stsd_index] > 6 &&
7310 (AV_RB24(trk->extradata[trk->last_stsd_index]) == 1 || AV_RB32(trk->extradata[trk->last_stsd_index]) == 1)) {
7311 /* extradata is Annex B, assume the bitstream is too and convert it */
7312 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7313 ret = ff_vvc_annexb2mp4_buf(pkt->data, &reformatted_data,
7314 &size, 0, NULL);
7315 if (ret < 0)
7316 return ret;
7317 avio_write(pb, reformatted_data, size);
7318 } else {
7319 size = ff_vvc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
7320 }
7321 } else if (par->codec_id == AV_CODEC_ID_LCEVC && trk->extradata_size[trk->last_stsd_index] > 0 &&
7322 *(uint8_t *)trk->extradata[trk->last_stsd_index] != 1) {
7323 /* extradata is Annex B, assume the bitstream is too and convert it */
7324 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7325 ret = ff_nal_parse_units_buf(pkt->data, &reformatted_data, &size);
7326 if (ret < 0)
7327 return ret;
7328 avio_write(pb, reformatted_data, size);
7329 } else {
7330 if (trk->cenc.aes_ctr) {
7331 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
7332 if (size < 0) {
7333 ret = size;
7334 goto err;
7335 }
7336 } else {
7337 size = ff_nal_parse_units(pb, pkt->data, pkt->size);
7338 }
7339 }
7340 } else if (par->codec_id == AV_CODEC_ID_AV1 && !trk->cenc.aes_ctr) {
7341 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7342 ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data,
7343 &size, &offset);
7344 if (ret < 0)
7345 return ret;
7346 avio_write(pb, reformatted_data, size);
7347 } else {
7348 size = ff_av1_filter_obus(pb, pkt->data, pkt->size);
7349 if (trk->mode == MODE_AVIF && !mov->avif_extent_length[pkt->stream_index]) {
7350 mov->avif_extent_length[pkt->stream_index] = size;
7351 }
7352 }
7353
7354 } else if (par->codec_id == AV_CODEC_ID_AC3 ||
7355 par->codec_id == AV_CODEC_ID_EAC3) {
7356 size = handle_eac3(mov, pkt, trk);
7357 if (size < 0)
7358 return size;
7359 else if (!size)
7360 goto end;
7361 avio_write(pb, pkt->data, size);
7362 } else if (par->codec_id == AV_CODEC_ID_EIA_608) {
7363 size = 8;
7364
7365 for (int i = 0; i < pkt->size; i += 3) {
7366 if (pkt->data[i] == 0xFC) {
7367 size += 2;
7368 }
7369 }
7370 avio_wb32(pb, size);
7371 ffio_wfourcc(pb, "cdat");
7372 for (int i = 0; i < pkt->size; i += 3) {
7373 if (pkt->data[i] == 0xFC) {
7374 avio_w8(pb, pkt->data[i + 1]);
7375 avio_w8(pb, pkt->data[i + 2]);
7376 }
7377 }
7378 } else if (par->codec_id == AV_CODEC_ID_APV) {
7379 ff_isom_parse_apvc(trk->apv, pkt, s);
7380 avio_wb32(s->pb, pkt->size);
7381 size += 4;
7382
7383 avio_write(s->pb, pkt->data, pkt->size);
7384 } else {
7385 if (trk->cenc.aes_ctr) {
7386 uint8_t *extradata = trk->extradata[trk->last_stsd_index];
7387 int extradata_size = trk->extradata_size[trk->last_stsd_index];
7388 if (par->codec_id == AV_CODEC_ID_H264 && extradata_size > 4) {
7389 int nal_size_length = (extradata[4] & 0x3) + 1;
7390 ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
7391 } else if(par->codec_id == AV_CODEC_ID_HEVC && extradata_size > 21) {
7392 int nal_size_length = (extradata[21] & 0x3) + 1;
7393 ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
7394 } else if(par->codec_id == AV_CODEC_ID_VVC) {
7396 } else if(par->codec_id == AV_CODEC_ID_AV1) {
7397 av_assert0(size == pkt->size);
7398 ret = ff_mov_cenc_av1_write_obus(s, &trk->cenc, pb, pkt);
7399 if (ret > 0) {
7400 size = ret;
7401 ret = 0;
7402 }
7403 } else {
7404 ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size);
7405 }
7406
7407 if (ret) {
7408 goto err;
7409 }
7410 } else {
7411 avio_write(pb, pkt->data, size);
7412 }
7413 }
7414
7415 if (trk->entry >= trk->cluster_capacity) {
7416 unsigned new_capacity = trk->entry + MOV_INDEX_CLUSTER_SIZE;
7417 void *cluster = av_realloc_array(trk->cluster, new_capacity, sizeof(*trk->cluster));
7418 if (!cluster) {
7419 ret = AVERROR(ENOMEM);
7420 goto err;
7421 }
7422 trk->cluster = cluster;
7423 trk->cluster_capacity = new_capacity;
7424 }
7425
7426 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
7427 trk->cluster[trk->entry].stsd_index = trk->last_stsd_index;
7428 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
7429 trk->cluster[trk->entry].chunkNum = 0;
7430 trk->cluster[trk->entry].size = size;
7431 trk->cluster[trk->entry].entries = samples_in_chunk;
7432 trk->cluster[trk->entry].dts = pkt->dts;
7433 trk->cluster[trk->entry].pts = pkt->pts;
7435 !trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
7436 if (!trk->frag_discont) {
7437 /* First packet of a new fragment. We already wrote the duration
7438 * of the last packet of the previous fragment based on track_duration,
7439 * which might not exactly match our dts. Therefore adjust the dts
7440 * of this packet to be what the previous packets duration implies. */
7441 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
7442 /* We also may have written the pts and the corresponding duration
7443 * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
7444 * the next fragment. This means the cts of the first sample must
7445 * be the same in all fragments, unless end_pts was updated by
7446 * the packet causing the fragment to be written. */
7447 if ((mov->flags & FF_MOV_FLAG_DASH &&
7449 mov->mode == MODE_ISM)
7450 pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
7451 } else {
7452 /* New fragment, but discontinuous from previous fragments.
7453 * Pretend the duration sum of the earlier fragments is
7454 * pkt->dts - trk->start_dts. */
7455 trk->end_pts = trk->elst_end_pts = AV_NOPTS_VALUE;
7456 trk->frag_discont = 0;
7457 }
7458 }
7459
7460 if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
7461 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
7462 /* Not using edit lists and shifting the first track to start from zero.
7463 * If the other streams start from a later timestamp, we won't be able
7464 * to signal the difference in starting time without an edit list.
7465 * Thus move the timestamp for this first sample to 0, increasing
7466 * its duration instead. */
7467 trk->cluster[trk->entry].dts = trk->start_dts = 0;
7468 }
7469 if (trk->start_dts == AV_NOPTS_VALUE) {
7470 trk->start_dts = pkt->dts;
7471 if (trk->frag_discont) {
7472 if (mov->use_editlist) {
7473 /* Pretend the whole stream started at pts=0, with earlier fragments
7474 * already written. If the stream started at pts=0, the duration sum
7475 * of earlier fragments would have been pkt->pts. */
7476 trk->start_dts = pkt->dts - pkt->pts;
7477 } else {
7478 /* Pretend the whole stream started at dts=0, with earlier fragments
7479 * already written, with a duration summing up to pkt->dts. */
7480 trk->start_dts = 0;
7481 }
7482 trk->frag_discont = 0;
7483 } else if (pkt->dts && mov->moov_written)
7485 "Track %d starts with a nonzero dts %"PRId64", while the moov "
7486 "already has been written. Set the delay_moov flag to handle "
7487 "this case.\n",
7488 pkt->stream_index, pkt->dts);
7489 }
7490
7491 sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems, AV_PKT_DATA_SKIP_SAMPLES);
7492 if (sd && sd->size >= 10 && trk->par->frame_size) {
7493 duration = FFMAX(av_rescale_q(trk->par->frame_size, (AVRational){ 1, trk->par->sample_rate },
7494 trk->st->time_base), pkt->duration);
7495 if (mov->use_editlist)
7496 pkt->duration = duration;
7497 duration -= av_rescale_q(AV_RL32(sd->data + 4), (AVRational){ 1, trk->par->sample_rate },
7498 trk->st->time_base);
7499 if (duration < 0)
7500 return AVERROR_INVALIDDATA;
7501 } else
7502 duration = pkt->duration;
7503
7504 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
7506
7507 if (pkt->pts == AV_NOPTS_VALUE) {
7508 av_log(s, AV_LOG_WARNING, "pts has no value\n");
7509 pkt->pts = pkt->dts;
7510 }
7511 if (pkt->dts != pkt->pts)
7512 trk->flags |= MOV_TRACK_CTTS;
7513 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
7514 trk->cluster[trk->entry].flags = 0;
7515 if (trk->start_cts == AV_NOPTS_VALUE || (pkt->dts <= 0 && trk->start_cts > pkt->pts - pkt->dts))
7516 trk->start_cts = pkt->pts - pkt->dts;
7517 if (trk->end_pts == AV_NOPTS_VALUE)
7518 trk->end_pts = trk->cluster[trk->entry].dts +
7519 trk->cluster[trk->entry].cts + duration;
7520 else
7521 trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
7522 trk->cluster[trk->entry].cts +
7523 duration);
7524 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
7525 trk->elst_end_pts = trk->end_pts;
7526
7527 if (par->codec_id == AV_CODEC_ID_VC1) {
7529 } else if (par->codec_id == AV_CODEC_ID_TRUEHD) {
7531 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
7532 if (mov->mode == MODE_MOV && par->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
7533 trk->entry > 0) { // force sync sample for the first key frame
7535 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
7536 trk->flags |= MOV_TRACK_STPS;
7537 } else {
7538 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
7539 }
7540 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
7541 trk->has_keyframes++;
7542 }
7543 if (pkt->flags & AV_PKT_FLAG_DISPOSABLE) {
7545 trk->has_disposable++;
7546 }
7547
7549 if (prft && prft_size == sizeof(AVProducerReferenceTime))
7550 memcpy(&trk->cluster[trk->entry].prft, prft, prft_size);
7551 else
7552 memset(&trk->cluster[trk->entry].prft, 0, sizeof(AVProducerReferenceTime));
7553
7554 trk->entry++;
7555 trk->sample_count += samples_in_chunk;
7556 mov->mdat_size += size;
7557
7558 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks)
7560 reformatted_data ? reformatted_data + offset
7561 : NULL, size);
7562
7563end:
7564err:
7565
7566 if (pkt->data != reformatted_data)
7567 av_free(reformatted_data);
7568 return ret;
7569}
7570
7572{
7573 MOVMuxContext *mov = s->priv_data;
7574 MOVTrack *trk = s->streams[pkt->stream_index]->priv_data;
7575 AVCodecParameters *par = trk->par;
7576 int64_t frag_duration = 0;
7577 int size = pkt->size;
7578
7579 int ret = check_pkt(s, trk, pkt);
7580 if (ret < 0)
7581 return ret;
7582
7583 if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
7584 for (int i = 0; i < mov->nb_streams; i++)
7585 mov->tracks[i].frag_discont = 1;
7587 }
7588
7590 if (trk->dts_shift == AV_NOPTS_VALUE)
7591 trk->dts_shift = pkt->pts - pkt->dts;
7592 pkt->dts += trk->dts_shift;
7593 }
7594
7595 if (trk->par->codec_id == AV_CODEC_ID_MP4ALS ||
7596 trk->par->codec_id == AV_CODEC_ID_AAC ||
7597 trk->par->codec_id == AV_CODEC_ID_AV1 ||
7598 trk->par->codec_id == AV_CODEC_ID_FLAC) {
7599 size_t side_size;
7600 uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
7601 /* Overwrite extradata only on flush packets or when no extradata was available during init */
7602 if (side_size > 0 && (!pkt->size || !trk->extradata_size[trk->last_stsd_index])) {
7603 void *newextra = av_malloc(side_size + AV_INPUT_BUFFER_PADDING_SIZE);
7604 if (!newextra)
7605 return AVERROR(ENOMEM);
7606 memset((uint8_t*)newextra + side_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
7607 memcpy(newextra, side, side_size);
7608 av_free(trk->extradata[trk->last_stsd_index]);
7609 trk->extradata[trk->last_stsd_index] = newextra;
7610 trk->extradata_size[trk->last_stsd_index] = side_size;
7611 }
7612 }
7613
7614 if (!pkt->size) {
7615 if (trk->start_dts == AV_NOPTS_VALUE && trk->frag_discont) {
7616 trk->start_dts = pkt->dts;
7617 if (pkt->pts != AV_NOPTS_VALUE)
7618 trk->start_cts = pkt->pts - pkt->dts;
7619 else
7620 trk->start_cts = 0;
7621 }
7622
7623 return 0; /* Discard 0 sized packets */
7624 }
7625
7626 if (trk->entry && pkt->stream_index < mov->nb_streams)
7627 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
7628 s->streams[pkt->stream_index]->time_base,
7630 if ((mov->max_fragment_duration &&
7631 frag_duration >= mov->max_fragment_duration) ||
7632 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
7635 trk->entry && pkt->flags & AV_PKT_FLAG_KEY) ||
7637 if (frag_duration >= mov->min_fragment_duration) {
7638 if (trk->entry) {
7639 // Set the duration of this track to line up with the next
7640 // sample in this track. This avoids relying on AVPacket
7641 // duration, but only helps for this particular track, not
7642 // for the other ones that are flushed at the same time.
7643 //
7644 // If we have trk->entry == 0, no fragment will be written
7645 // for this track, and we can't adjust the track end here.
7646 trk->track_duration = pkt->dts - trk->start_dts;
7647 if (pkt->pts != AV_NOPTS_VALUE)
7648 trk->end_pts = pkt->pts;
7649 else
7650 trk->end_pts = pkt->dts;
7651 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
7652 trk->elst_end_pts = trk->end_pts;
7653 trk->end_reliable = 1;
7654 }
7656 }
7657 }
7658
7659 return ff_mov_write_packet(s, pkt);
7660}
7661
7663 int stream_index,
7664 int64_t dts) {
7665 MOVMuxContext *mov = s->priv_data;
7666 AVPacket *end = mov->pkt;
7667 uint8_t data[2] = {0};
7668 int ret;
7669
7670 end->size = sizeof(data);
7671 end->data = data;
7672 end->pts = dts;
7673 end->dts = dts;
7674 end->duration = 0;
7675 end->stream_index = stream_index;
7676
7678 av_packet_unref(end);
7679
7680 return ret;
7681}
7682
7683#if CONFIG_IAMFENC
7684static int mov_build_iamf_packet(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt)
7685{
7686 uint8_t *data;
7687 int ret;
7688
7689 if (pkt->stream_index == trk->first_iamf_idx) {
7691 if (ret < 0)
7692 return ret;
7693 }
7694
7696 s->streams[pkt->stream_index]->id, pkt);
7697 if (ret < 0)
7698 return ret;
7699
7700 if (pkt->stream_index != trk->last_iamf_idx)
7701 return AVERROR(EAGAIN);
7702
7704 trk->iamf_buf = NULL;
7705 if (!ret) {
7706 if (pkt->size) {
7707 // Either all or none of the packets for a single
7708 // IA Sample may be empty.
7709 av_log(s, AV_LOG_ERROR, "Unexpected packet from "
7710 "stream #%d\n", pkt->stream_index);
7712 }
7713 av_free(data);
7714 return ret;
7715 }
7716
7717 av_buffer_unref(&pkt->buf);
7718 pkt->buf = av_buffer_create(data, ret, NULL, NULL, 0);
7719 if (!pkt->buf) {
7720 av_free(data);
7721 return AVERROR(ENOMEM);
7722 }
7723 pkt->data = data;
7724 pkt->size = ret;
7725 pkt->stream_index = trk->first_iamf_idx;
7726
7727 return avio_open_dyn_buf(&trk->iamf_buf);
7728}
7729#endif
7730
7732{
7733 int64_t pos = avio_tell(pb);
7734 const char *scheme_id_uri = "https://aomedia.org/emsg/ID3";
7735 const char *value = "";
7736
7737 av_assert0(st->time_base.num == 1);
7738
7742
7743 avio_wb32(pb, 0); /* size */
7744 ffio_wfourcc(pb, "emsg");
7745 avio_w8(pb, 1); /* version */
7746 avio_wb24(pb, 0);
7747 avio_wb32(pb, st->time_base.den); /* timescale */
7748 avio_wb64(pb, pkt->pts); /* presentation_time */
7749 avio_wb32(pb, 0xFFFFFFFFU); /* event_duration */
7750 avio_wb32(pb, 0); /* id */
7751 /* null terminated UTF8 strings */
7752 avio_write(pb, scheme_id_uri, strlen(scheme_id_uri) + 1);
7753 avio_write(pb, value, strlen(value) + 1);
7754 avio_write(pb, pkt->data, pkt->size);
7755
7756 return update_size(pb, pos);
7757}
7758
7760{
7761 MOVMuxContext *mov = s->priv_data;
7762 MOVTrack *trk;
7763
7764 if (!pkt) {
7766 return 1;
7767 }
7768
7769 if (s->streams[pkt->stream_index]->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
7770 mov_write_emsg_tag(s->pb, s->streams[pkt->stream_index], pkt);
7771 return 0;
7772 }
7773
7774 trk = s->streams[pkt->stream_index]->priv_data;
7775
7776#if CONFIG_IAMFENC
7777 if (trk->iamf) {
7778 int ret = mov_build_iamf_packet(s, trk, pkt);
7779 if (ret < 0) {
7780 if (ret == AVERROR(EAGAIN))
7781 return 0;
7782 av_log(s, AV_LOG_ERROR, "Error assembling an IAMF packet "
7783 "for stream #%d\n", trk->st->index);
7784 return ret;
7785 }
7786 }
7787#endif
7788
7789 if (is_cover_image(trk->st)) {
7790 int ret;
7791
7792 if (trk->st->nb_frames >= 1) {
7793 if (trk->st->nb_frames == 1)
7794 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
7795 " ignoring.\n", pkt->stream_index);
7796 return 0;
7797 }
7798
7799 if ((ret = av_packet_ref(trk->cover_image, pkt)) < 0)
7800 return ret;
7801
7802 return 0;
7803 } else {
7804 int i;
7805
7806 if (!pkt->size)
7807 return mov_write_single_packet(s, pkt); /* Passthrough. */
7808
7809 /*
7810 * Subtitles require special handling.
7811 *
7812 * 1) For full compliance, every track must have a sample at
7813 * dts == 0, which is rarely true for subtitles. So, as soon
7814 * as we see any packet with dts > 0, write an empty subtitle
7815 * at dts == 0 for any subtitle track with no samples in it.
7816 *
7817 * 2) For each subtitle track, check if the current packet's
7818 * dts is past the duration of the last subtitle sample. If
7819 * so, we now need to write an end sample for that subtitle.
7820 *
7821 * This must be done conditionally to allow for subtitles that
7822 * immediately replace each other, in which case an end sample
7823 * is not needed, and is, in fact, actively harmful.
7824 *
7825 * 3) See mov_write_trailer for how the final end sample is
7826 * handled.
7827 */
7828 for (i = 0; i < mov->nb_tracks; i++) {
7829 MOVTrack *trk = &mov->tracks[i];
7830 int ret;
7831
7832 if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
7833 trk->track_duration < pkt->dts &&
7834 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
7836 if (ret < 0) return ret;
7838 }
7839 }
7840
7842 /*
7843 * If the track has to have its samples squashed into one sample,
7844 * we just take it into the track's queue.
7845 * This will then be utilized as the samples get written in either
7846 * mov_flush_fragment or when the mux is finalized in
7847 * mov_write_trailer.
7848 */
7849 int ret = AVERROR_BUG;
7850
7851 if (pkt->pts == AV_NOPTS_VALUE) {
7853 "Packets without a valid presentation timestamp are "
7854 "not supported with packet squashing!\n");
7855 return AVERROR(EINVAL);
7856 }
7857
7858 /* The following will reset pkt and is only allowed to be used
7859 * because we return immediately. afterwards. */
7861 pkt, NULL, 0)) < 0) {
7862 return ret;
7863 }
7864
7865 return 0;
7866 }
7867
7868
7869 if (trk->mode == MODE_MOV && trk->par->codec_type == AVMEDIA_TYPE_VIDEO) {
7870 AVPacket *opkt = pkt;
7871 int reshuffle_ret, ret;
7872 if (trk->is_unaligned_qt_rgb) {
7873 int64_t bpc = trk->par->bits_per_coded_sample != 15 ? trk->par->bits_per_coded_sample : 16;
7874 int expected_stride = ((trk->par->width * bpc + 15) >> 4)*2;
7875 reshuffle_ret = ff_reshuffle_raw_rgb(s, &pkt, trk->par, expected_stride);
7876 if (reshuffle_ret < 0)
7877 return reshuffle_ret;
7878 } else
7879 reshuffle_ret = 0;
7880 if (trk->par->format == AV_PIX_FMT_PAL8 && !trk->pal_done) {
7881 ret = ff_get_packet_palette(s, opkt, reshuffle_ret, trk->palette);
7882 if (ret < 0)
7883 goto fail;
7884 if (ret)
7885 trk->pal_done++;
7886 } else if (trk->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
7887 (trk->par->format == AV_PIX_FMT_GRAY8 ||
7888 trk->par->format == AV_PIX_FMT_MONOBLACK)) {
7890 if (ret < 0)
7891 goto fail;
7892 for (i = 0; i < pkt->size; i++)
7893 pkt->data[i] = ~pkt->data[i];
7894 }
7895 if (reshuffle_ret) {
7897fail:
7898 if (reshuffle_ret)
7900 return ret;
7901 }
7902 }
7903
7904 return mov_write_single_packet(s, pkt);
7905 }
7906}
7907
7908// QuickTime chapters involve an additional text track with the chapter names
7909// as samples, and a tref pointing from the other tracks to the chapter one.
7911{
7912 static const uint8_t stub_header[] = {
7913 // TextSampleEntry
7914 0x00, 0x00, 0x00, 0x01, // displayFlags
7915 0x00, 0x00, // horizontal + vertical justification
7916 0x00, 0x00, 0x00, 0x00, // bgColourRed/Green/Blue/Alpha
7917 // BoxRecord
7918 0x00, 0x00, 0x00, 0x00, // defTextBoxTop/Left
7919 0x00, 0x00, 0x00, 0x00, // defTextBoxBottom/Right
7920 // StyleRecord
7921 0x00, 0x00, 0x00, 0x00, // startChar + endChar
7922 0x00, 0x01, // fontID
7923 0x00, 0x00, // fontStyleFlags + fontSize
7924 0x00, 0x00, 0x00, 0x00, // fgColourRed/Green/Blue/Alpha
7925 // FontTableBox
7926 0x00, 0x00, 0x00, 0x0D, // box size
7927 'f', 't', 'a', 'b', // box atom name
7928 0x00, 0x01, // entry count
7929 // FontRecord
7930 0x00, 0x01, // font ID
7931 0x00, // font name length
7932 };
7933 MOVMuxContext *mov = s->priv_data;
7934 MOVTrack *track = &mov->tracks[tracknum];
7935 AVPacket *pkt = mov->pkt;
7936 int i, len;
7937 int ret;
7938
7939 track->mode = mov->mode;
7940 track->tag = MKTAG('t','e','x','t');
7941 track->timescale = mov->movie_timescale;
7942 track->par = avcodec_parameters_alloc();
7943 if (!track->par)
7944 return AVERROR(ENOMEM);
7946 ret = ff_alloc_extradata(track->par, sizeof(stub_header));
7947 if (ret < 0)
7948 return ret;
7949 memcpy(track->par->extradata, stub_header, sizeof(stub_header));
7950
7951 if (track->extradata == NULL) {
7952 track->stsd_count = 1;
7953 track->extradata = av_calloc(1, sizeof(*track->extradata));
7954 track->extradata_size = av_calloc(1, sizeof(*track->extradata_size));
7955 if (!track->extradata || !track->extradata_size)
7956 return AVERROR(ENOMEM);
7957 }
7958
7959 track->extradata[0] = av_memdup(stub_header, sizeof(stub_header));
7960 if (!track->extradata[0])
7961 return AVERROR(ENOMEM);
7962 track->extradata_size[0] = sizeof(stub_header);
7963
7964 pkt->stream_index = tracknum;
7965 pkt->flags = AV_PKT_FLAG_KEY;
7966
7967 for (i = 0; i < s->nb_chapters; i++) {
7968 AVChapter *c = s->chapters[i];
7970
7971 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,mov->movie_timescale});
7972 pkt->pts = pkt->dts = av_rescale_q(c->start, c->time_base, (AVRational){1,mov->movie_timescale});
7973 pkt->duration = end - pkt->dts;
7974
7975 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
7976 static const char encd[12] = {
7977 0x00, 0x00, 0x00, 0x0C,
7978 'e', 'n', 'c', 'd',
7979 0x00, 0x00, 0x01, 0x00 };
7980 len = strlen(t->value);
7981 pkt->size = len + 2 + 12;
7982 pkt->data = av_malloc(pkt->size);
7983 if (!pkt->data) {
7985 return AVERROR(ENOMEM);
7986 }
7987 AV_WB16(pkt->data, len);
7988 memcpy(pkt->data + 2, t->value, len);
7989 memcpy(pkt->data + len + 2, encd, sizeof(encd));
7991 av_freep(&pkt->data);
7992 }
7993 }
7994
7995 av_packet_unref(mov->pkt);
7996
7997 return 0;
7998}
7999
8000
8001static int mov_check_timecode_track(AVFormatContext *s, AVTimecode *tc, AVStream *src_st, const char *tcstr)
8002{
8003 int ret;
8004
8005 /* compute the frame number */
8006 ret = av_timecode_init_from_string(tc, src_st->avg_frame_rate, tcstr, s);
8007 return ret;
8008}
8009
8010static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, AVTimecode tc)
8011{
8012 MOVMuxContext *mov = s->priv_data;
8013 MOVTrack *track = &mov->tracks[index];
8014 AVStream *src_st = mov->tracks[src_index].st;
8015 uint8_t data[4];
8016 AVPacket *pkt = mov->pkt;
8017 AVRational rate = src_st->avg_frame_rate;
8018 int ret;
8019
8020 /* tmcd track based on video stream */
8021 track->mode = mov->mode;
8022 track->tag = MKTAG('t','m','c','d');
8023 track->src_track = av_malloc(sizeof(*track->src_track));
8024 if (!track->src_track)
8025 return AVERROR(ENOMEM);
8026 *track->src_track = src_index;
8027 track->nb_src_track = 1;
8029 track->timescale = tc.fps * 1000;
8030 else if (tc.rate.den == 1001)
8031 track->timescale = tc.rate.num;
8032 else
8033 track->timescale = tc.fps;
8036
8037 /* set st to src_st for metadata access*/
8038 track->st = src_st;
8039
8040 /* encode context: tmcd data stream */
8041 track->par = avcodec_parameters_alloc();
8042 if (!track->par)
8043 return AVERROR(ENOMEM);
8045 track->par->codec_tag = track->tag;
8046 track->st->avg_frame_rate = rate;
8047
8048 /* the tmcd track just contains one packet with the frame number */
8049 pkt->data = data;
8050 pkt->stream_index = index;
8051 pkt->flags = AV_PKT_FLAG_KEY;
8052 pkt->pts = pkt->dts = av_rescale_q(tc.start, av_inv_q(rate), (AVRational){ 1, track->timescale });
8053 pkt->size = 4;
8054 AV_WB32(pkt->data, tc.start);
8057 return ret;
8058}
8059
8060/*
8061 * st->disposition controls the "enabled" flag in the tkhd tag.
8062 * QuickTime will not play a track if it is not enabled. So make sure
8063 * that one track of each type (audio, video, subtitle) is enabled.
8064 *
8065 * Subtitles are special. For audio and video, setting "enabled" also
8066 * makes the track "default" (i.e. it is rendered when played). For
8067 * subtitles, an "enabled" subtitle is not rendered by default, but
8068 * if no subtitle is enabled, the subtitle menu in QuickTime will be
8069 * empty!
8070 */
8072{
8073 MOVMuxContext *mov = s->priv_data;
8074 int i;
8075 int enabled[AVMEDIA_TYPE_NB];
8076 int first[AVMEDIA_TYPE_NB];
8077
8078 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
8079 enabled[i] = 0;
8080 first[i] = -1;
8081 }
8082
8083 for (i = 0; i < mov->nb_streams; i++) {
8084 AVStream *st = mov->tracks[i].st;
8085
8088 is_cover_image(st))
8089 continue;
8090
8091 if (first[st->codecpar->codec_type] < 0)
8092 first[st->codecpar->codec_type] = i;
8095 enabled[st->codecpar->codec_type]++;
8096 }
8097 }
8098
8099 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
8100 switch (i) {
8101 case AVMEDIA_TYPE_VIDEO:
8102 case AVMEDIA_TYPE_AUDIO:
8104 if (enabled[i] > 1)
8105 mov->per_stream_grouping = 1;
8106 if (!enabled[i] && first[i] >= 0)
8107 mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
8108 break;
8109 }
8110 }
8111}
8112
8114{
8115 MOVMuxContext *mov = s->priv_data;
8116
8117 for (int i = 0; i < s->nb_streams; i++)
8118 s->streams[i]->priv_data = NULL;
8119
8120 if (!mov->tracks)
8121 return;
8122
8123 if (mov->chapter_track) {
8125 }
8126
8127 for (int i = 0; i < mov->nb_tracks; i++) {
8128 MOVTrack *const track = &mov->tracks[i];
8129
8130 if (track->tag == MKTAG('r','t','p',' '))
8131 ff_mov_close_hinting(track);
8132 else if (track->tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
8133 av_freep(&track->par);
8134 av_freep(&track->cluster);
8135 av_freep(&track->cluster_written);
8136 av_freep(&track->frag_info);
8137 av_packet_free(&track->cover_image);
8138
8139 if (track->eac3_priv) {
8140 struct eac3_info *info = track->eac3_priv;
8141 av_packet_free(&info->pkt);
8142 av_freep(&track->eac3_priv);
8143 }
8144 for (int j = 0; j < track->stsd_count; j++)
8145 av_freep(&track->extradata[j]);
8146 av_freep(&track->extradata);
8147 av_freep(&track->extradata_size);
8148
8149 for (int i = 0; i < track->nb_tref_tags; i++)
8150 av_freep(&track->tref_tags[i].id);
8151 av_freep(&track->tref_tags);
8152 av_freep(&track->src_track);
8153
8154 ff_mov_cenc_free(&track->cenc);
8155 ffio_free_dyn_buf(&track->mdat_buf);
8156
8157#if CONFIG_IAMFENC
8158 ffio_free_dyn_buf(&track->iamf_buf);
8159 if (track->iamf)
8161 av_freep(&track->iamf);
8162#endif
8163 ff_isom_close_apvc(&track->apv);
8164
8166 }
8167
8168 av_freep(&mov->tracks);
8170}
8171
8172static uint32_t rgb_to_yuv(uint32_t rgb)
8173{
8174 uint8_t r, g, b;
8175 int y, cb, cr;
8176
8177 r = (rgb >> 16) & 0xFF;
8178 g = (rgb >> 8) & 0xFF;
8179 b = (rgb ) & 0xFF;
8180
8181 y = av_clip_uint8(( 16000 + 257 * r + 504 * g + 98 * b)/1000);
8182 cb = av_clip_uint8((128000 - 148 * r - 291 * g + 439 * b)/1000);
8183 cr = av_clip_uint8((128000 + 439 * r - 368 * g - 71 * b)/1000);
8184
8185 return (y << 16) | (cr << 8) | cb;
8186}
8187
8189 AVStream *st)
8190{
8191 int i, width = 720, height = 480;
8192 int have_palette = 0, have_size = 0;
8193 uint32_t palette[16];
8194 char *cur = st->codecpar->extradata;
8195
8196 while (cur && *cur) {
8197 if (strncmp("palette:", cur, 8) == 0) {
8198 int i, count;
8199 count = sscanf(cur + 8,
8200 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
8201 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
8202 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
8203 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
8204 &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
8205 &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
8206 &palette[ 8], &palette[ 9], &palette[10], &palette[11],
8207 &palette[12], &palette[13], &palette[14], &palette[15]);
8208
8209 for (i = 0; i < count; i++) {
8211 }
8212 have_palette = 1;
8213 } else if (!strncmp("size:", cur, 5)) {
8214 sscanf(cur + 5, "%dx%d", &width, &height);
8215 have_size = 1;
8216 }
8217 if (have_palette && have_size)
8218 break;
8219 cur += strcspn(cur, "\n\r");
8220 cur += strspn(cur, "\n\r");
8221 }
8222 if (have_palette) {
8224 if (!track->extradata[track->last_stsd_index])
8225 return AVERROR(ENOMEM);
8226 for (i = 0; i < 16; i++) {
8227 AV_WB32(track->extradata[track->last_stsd_index] + i * 4, palette[i]);
8228 }
8229 memset(track->extradata[track->last_stsd_index] + 16*4, 0, AV_INPUT_BUFFER_PADDING_SIZE);
8230 track->extradata_size[track->last_stsd_index] = 16 * 4;
8231 }
8232 st->codecpar->width = width;
8233 st->codecpar->height = track->height = height;
8234
8235 return 0;
8236}
8237
8238#if CONFIG_IAMFENC
8239static int mov_init_iamf_track(AVFormatContext *s)
8240{
8241 MOVMuxContext *mov = s->priv_data;
8242 MOVTrack *track;
8243 IAMFContext *iamf;
8244 int first_iamf_idx = INT_MAX, last_iamf_idx = 0;
8245 int nb_audio_elements = 0, nb_mix_presentations = 0;
8246 int ret;
8247
8248 for (int i = 0; i < s->nb_stream_groups; i++) {
8249 const AVStreamGroup *stg = s->stream_groups[i];
8250
8252 nb_audio_elements++;
8254 nb_mix_presentations++;
8255 }
8256
8257 if (!nb_audio_elements && !nb_mix_presentations)
8258 return 0;
8259
8260 if (nb_audio_elements < 1 || nb_audio_elements > 2 || nb_mix_presentations < 1) {
8261 av_log(s, AV_LOG_ERROR, "There must be >= 1 and <= 2 IAMF_AUDIO_ELEMENT and at least "
8262 "one IAMF_MIX_PRESENTATION stream groups to write a IMAF track\n");
8263 return AVERROR(EINVAL);
8264 }
8265
8266 iamf = av_mallocz(sizeof(*iamf));
8267 if (!iamf)
8268 return AVERROR(ENOMEM);
8269
8270
8271 for (int i = 0; i < s->nb_stream_groups; i++) {
8272 const AVStreamGroup *stg = s->stream_groups[i];
8273 switch(stg->type) {
8275 for (int j = 0; j < stg->nb_entries; j++) {
8276 first_iamf_idx = FFMIN(stg->entries[j]->stream->index, first_iamf_idx);
8277 last_iamf_idx = FFMAX(stg->entries[j]->stream->index, last_iamf_idx);
8278 }
8279
8280 ret = ff_iamf_add_audio_element(iamf, stg, s);
8281 break;
8283 ret = ff_iamf_add_mix_presentation(iamf, stg, s);
8284 break;
8285 default:
8286 av_assert0(0);
8287 }
8288 if (ret < 0) {
8290 av_free(iamf);
8291 return ret;
8292 }
8293 }
8294
8295 track = &mov->tracks[first_iamf_idx];
8296 track->iamf = iamf;
8297 track->first_iamf_idx = first_iamf_idx;
8298 track->last_iamf_idx = last_iamf_idx;
8299 track->tag = MKTAG('i','a','m','f');
8300
8301 for (int i = 0; i < s->nb_stream_groups; i++) {
8302 AVStreamGroup *stg = s->stream_groups[i];
8304 continue;
8305 for (int j = 0; j < stg->nb_entries; j++)
8306 stg->entries[j]->stream->priv_data = track;
8307 }
8308
8309 ret = avio_open_dyn_buf(&track->iamf_buf);
8310 if (ret < 0)
8311 return ret;
8312
8313 return 0;
8314}
8315#endif
8316
8318{
8319 MOVMuxContext *mov = s->priv_data;
8320 int has_iamf = 0;
8321 int i, ret;
8322
8323 mov->fc = s;
8324 mov->pkt = ffformatcontext(s)->pkt;
8325
8326 /* Default mode == MP4 */
8327 mov->mode = MODE_MP4;
8328
8329#define IS_MODE(muxer, config) (CONFIG_ ## config ## _MUXER && !strcmp(#muxer, s->oformat->name))
8330 if (IS_MODE(3gp, TGP)) mov->mode = MODE_3GP;
8331 else if (IS_MODE(3g2, TG2)) mov->mode = MODE_3GP|MODE_3G2;
8332 else if (IS_MODE(mov, MOV)) mov->mode = MODE_MOV;
8333 else if (IS_MODE(psp, PSP)) mov->mode = MODE_PSP;
8334 else if (IS_MODE(ipod, IPOD)) mov->mode = MODE_IPOD;
8335 else if (IS_MODE(ismv, ISMV)) mov->mode = MODE_ISM;
8336 else if (IS_MODE(f4v, F4V)) mov->mode = MODE_F4V;
8337 else if (IS_MODE(avif, AVIF)) mov->mode = MODE_AVIF;
8338#undef IS_MODE
8339
8340 if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
8342
8343 if (mov->mode == MODE_AVIF)
8345
8346 /* Set the FRAGMENT flag if any of the fragmentation methods are
8347 * enabled. */
8348 if (mov->max_fragment_duration || mov->max_fragment_size ||
8354
8357 av_log(s, AV_LOG_ERROR, "Setting both hybrid_fragmented and faststart is not supported.\n");
8358 return AVERROR(EINVAL);
8359 }
8360
8361 /* Set other implicit flags immediately */
8364
8365 if (mov->mode == MODE_ISM)
8368 if (mov->flags & FF_MOV_FLAG_DASH)
8371 if (mov->flags & FF_MOV_FLAG_CMAF)
8374
8375 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV && s->flags & AVFMT_FLAG_AUTO_BSF) {
8376 av_log(s, AV_LOG_VERBOSE, "Empty MOOV enabled; disabling automatic bitstream filtering\n");
8377 s->flags &= ~AVFMT_FLAG_AUTO_BSF;
8378 }
8379
8381 av_log(s, AV_LOG_WARNING, "Global SIDX enabled; Ignoring skip_sidx option\n");
8383 }
8384
8385 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
8386 mov->reserved_moov_size = -1;
8387 }
8388
8389 if (mov->use_editlist < 0) {
8390 mov->use_editlist = 1;
8391 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
8392 !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
8393 // If we can avoid needing an edit list by shifting the
8394 // tracks, prefer that over (trying to) write edit lists
8395 // in fragmented output.
8396 if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO ||
8397 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)
8398 mov->use_editlist = 0;
8399 }
8400 }
8401 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
8402 !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
8403 av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
8404
8405 if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist)
8407
8408 if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO &&
8410 s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO;
8411
8412 /* Clear the omit_tfhd_offset flag if default_base_moof is set;
8413 * if the latter is set that's enough and omit_tfhd_offset doesn't
8414 * add anything extra on top of that. */
8418
8419 if (mov->frag_interleave &&
8422 "Sample interleaving in fragments is mutually exclusive with "
8423 "omit_tfhd_offset and separate_moof\n");
8424 return AVERROR(EINVAL);
8425 }
8426
8427 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
8428 * is enabled, we don't support non-seekable output at all. */
8429 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
8430 (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead ||
8431 mov->mode == MODE_AVIF)) {
8432 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
8433 return AVERROR(EINVAL);
8434 }
8435
8436#if CONFIG_AVIF_MUXER
8437 /* AVIF output must have at most two video streams (one for YUV and one for
8438 * alpha). */
8439 if (mov->mode == MODE_AVIF) {
8440 if (s->nb_streams > 2) {
8441 av_log(s, AV_LOG_ERROR, "AVIF output requires exactly one or two streams\n");
8442 return AVERROR(EINVAL);
8443 }
8444 if (s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
8445 (s->nb_streams > 1 && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)) {
8446 av_log(s, AV_LOG_ERROR, "AVIF output supports only video streams\n");
8447 return AVERROR(EINVAL);
8448 }
8449 if (!av_pix_fmt_desc_get(s->streams[0]->codecpar->format)) {
8450 av_log(s, AV_LOG_ERROR, "AVIF output requires a known pixel format\n");
8451 return AVERROR(EINVAL);
8452 }
8453 if (s->nb_streams > 1) {
8454 const AVPixFmtDescriptor *pixdesc =
8455 av_pix_fmt_desc_get(s->streams[1]->codecpar->format);
8456 if (!pixdesc || pixdesc->nb_components != 1) {
8457 av_log(s, AV_LOG_ERROR, "Second stream for AVIF (alpha) output must have exactly one plane\n");
8458 return AVERROR(EINVAL);
8459 }
8460 }
8461 s->streams[0]->disposition |= AV_DISPOSITION_DEFAULT;
8462 }
8463#endif
8464
8465 for (i = 0; i < s->nb_stream_groups; i++) {
8466 AVStreamGroup *stg = s->stream_groups[i];
8467
8468 for (int j = 0; j < stg->nb_entries; j++) {
8469 if (stg->entries[j]->type != AV_STREAM_GROUP_ENTRY_STREAM) {
8470 av_log(s, AV_LOG_ERROR, "Unsupported Stream Group entry #%d "
8471 "in Stream Group id %"PRId64"\n",
8472 i, stg->id);
8473 return AVERROR(EINVAL);
8474 }
8475 }
8476
8477 if (stg->type == AV_STREAM_GROUP_PARAMS_LCEVC) {
8478 if (stg->nb_entries != 2) {
8479 av_log(s, AV_LOG_ERROR, "Exactly two Streams are supported for Stream Groups of type LCEVC\n");
8480 return AVERROR(EINVAL);
8481 }
8483 if (lcevc->el_index > 1)
8484 return AVERROR(EINVAL);
8485 AVStream *st = stg->entries[lcevc->el_index]->stream;
8486 if (st->codecpar->codec_id != AV_CODEC_ID_LCEVC) {
8487 av_log(s, AV_LOG_ERROR, "Stream #%u is not an LCEVC stream\n", lcevc->el_index);
8488 return AVERROR(EINVAL);
8489 }
8490 }
8491
8492#if CONFIG_IAMFENC
8494 continue;
8495
8496 for (int j = 0; j < stg->nb_entries; j++) {
8497 AVStream *st = stg->entries[j]->stream;
8498
8499 if (st->priv_data) {
8500 av_log(s, AV_LOG_ERROR, "Stream %d is present in more than one Stream Group of type "
8501 "IAMF Audio Element\n", j);
8502 return AVERROR(EINVAL);
8503 }
8504 st->priv_data = st;
8505 }
8506 has_iamf = 1;
8507
8508 if (!mov->nb_tracks) // We support one track for the entire IAMF structure
8509 mov->nb_tracks++;
8510#endif
8511 }
8512
8513 for (i = 0; i < s->nb_streams; i++) {
8514 AVStream *st = s->streams[i];
8515 if (st->priv_data)
8516 continue;
8517 // Don't produce a track in the output file for timed ID3 streams.
8519 // Leave priv_data set to NULL for these AVStreams that don't
8520 // have a corresponding track.
8521 continue;
8522 }
8523 st->priv_data = st;
8524 mov->nb_tracks++;
8525 }
8526
8527 mov->nb_streams = mov->nb_tracks;
8528
8529 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
8530 mov->chapter_track = mov->nb_tracks++;
8531
8532 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
8533 for (i = 0; i < s->nb_streams; i++)
8534 if (rtp_hinting_needed(s->streams[i]))
8535 mov->nb_tracks++;
8536 }
8537
8538 if (mov->write_btrt < 0) {
8539 mov->write_btrt = mov->mode == MODE_MP4;
8540 }
8541
8542 if ( mov->write_tmcd == -1 && (mov->mode == MODE_MOV || mov->mode == MODE_MP4)
8543 || mov->write_tmcd == 1) {
8544 AVDictionaryEntry *global_tcr = av_dict_get(s->metadata, "timecode",
8545 NULL, 0);
8546
8547 /* +1 tmcd track for each video stream with a timecode */
8548 for (i = 0; i < s->nb_streams; i++) {
8549 AVStream *st = s->streams[i];
8550 AVDictionaryEntry *t = global_tcr;
8552 (t || (t=av_dict_get(st->metadata, "timecode", NULL, 0)))) {
8553 AVTimecode tc;
8554 ret = mov_check_timecode_track(s, &tc, st, t->value);
8555 if (ret >= 0)
8556 mov->nb_meta_tmcd++;
8557 }
8558 }
8559
8560 /* check if there is already a tmcd track to remux */
8561 if (mov->nb_meta_tmcd) {
8562 for (i = 0; i < s->nb_streams; i++) {
8563 AVStream *st = s->streams[i];
8564 if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
8565 av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
8566 "so timecode metadata are now ignored\n");
8567 mov->nb_meta_tmcd = 0;
8568 }
8569 }
8570 }
8571
8572 mov->nb_tracks += mov->nb_meta_tmcd;
8573 }
8574
8575 // Reserve an extra stream for chapters for the case where chapters
8576 // are written in the trailer
8577 mov->tracks = av_calloc(mov->nb_tracks + 1, sizeof(*mov->tracks));
8578 if (!mov->tracks)
8579 return AVERROR(ENOMEM);
8580
8581 for (i = 0; i < mov->nb_tracks; i++) {
8582 MOVTrack *track = &mov->tracks[i];
8583
8584 track->stsd_count = 1;
8585 track->extradata = av_calloc(track->stsd_count, sizeof(*track->extradata));
8586 track->extradata_size = av_calloc(track->stsd_count, sizeof(*track->extradata_size));
8587 if (!track->extradata || !track->extradata_size)
8588 return AVERROR(ENOMEM);
8589 }
8590
8591 if (mov->encryption_scheme_str != NULL && strcmp(mov->encryption_scheme_str, "none") != 0) {
8592 if (strcmp(mov->encryption_scheme_str, "cenc-aes-ctr") == 0) {
8594
8596 av_log(s, AV_LOG_ERROR, "Invalid encryption key len %d expected %d\n",
8598 return AVERROR(EINVAL);
8599 }
8600
8601 if (mov->encryption_kid_len != CENC_KID_SIZE) {
8602 av_log(s, AV_LOG_ERROR, "Invalid encryption kid len %d expected %d\n",
8604 return AVERROR(EINVAL);
8605 }
8606 } else {
8607 av_log(s, AV_LOG_ERROR, "unsupported encryption scheme %s\n",
8609 return AVERROR(EINVAL);
8610 }
8611 }
8612
8613#if CONFIG_IAMFENC
8614 ret = mov_init_iamf_track(s);
8615 if (ret < 0)
8616 return ret;
8617#endif
8618
8619 for (int j = 0, i = 0; j < s->nb_streams; j++) {
8620 AVStream *st = s->streams[j];
8621
8622 if (st != st->priv_data) {
8623 if (has_iamf)
8624 i += has_iamf--;
8625 continue;
8626 }
8627 st->priv_data = &mov->tracks[i++];
8628 }
8629
8630 AVRational movie_timescale = (AVRational) { 0, 1 };
8631 for (i = 0; i < s->nb_streams; i++) {
8632 AVStream *st= s->streams[i];
8633 MOVTrack *track = st->priv_data;
8634
8635 if (!track)
8636 continue;
8637
8638 if (!track->st) {
8639 track->st = st;
8640 track->par = st->codecpar;
8641 }
8642
8643 movie_timescale = av_gcd_q(movie_timescale, st->time_base, INT_MAX, (AVRational){1,0});
8644 }
8645 if (!movie_timescale.den)
8646 movie_timescale = MOV_TIMESCALE_Q;
8647
8648 if (!mov->movie_timescale)
8649 mov->movie_timescale = FFMAX(movie_timescale.den, MOV_TIMESCALE);
8650
8651 for (i = 0; i < s->nb_streams; i++) {
8652 AVStream *st= s->streams[i];
8653 MOVTrack *track = st->priv_data;
8654 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
8655
8656 if (!track)
8657 continue;
8658
8659 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
8660 if (track->language < 0)
8661 track->language = 32767; // Unspecified Macintosh language code
8662 track->mode = mov->mode;
8663 if (!track->tag)
8664 track->tag = mov_find_codec_tag(s, track);
8665 if (!track->tag) {
8666 av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
8667 "codec not currently supported in container\n",
8669 return AVERROR(EINVAL);
8670 }
8671 /* If hinting of this track is enabled by a later hint track,
8672 * this is updated. */
8673 track->hint_track = -1;
8674 track->start_dts = AV_NOPTS_VALUE;
8675 track->start_cts = AV_NOPTS_VALUE;
8676 track->end_pts = AV_NOPTS_VALUE;
8677 track->dts_shift = AV_NOPTS_VALUE;
8679 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
8680 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
8681 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
8682 if (st->codecpar->width != 720 || (st->codecpar->height != 608 && st->codecpar->height != 512)) {
8683 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
8684 return AVERROR(EINVAL);
8685 }
8686 track->height = track->tag >> 24 == 'n' ? 486 : 576;
8687 }
8688 if (mov->video_track_timescale) {
8689 track->timescale = mov->video_track_timescale;
8690 if (mov->mode == MODE_ISM && mov->video_track_timescale != 10000000)
8691 av_log(s, AV_LOG_WARNING, "Warning: some tools, like mp4split, assume a timescale of 10000000 for ISMV.\n");
8692 } else {
8693 track->timescale = st->time_base.den;
8694 while(track->timescale < 10000)
8695 track->timescale *= 2;
8696 }
8697 if (st->codecpar->width > 65535 || st->codecpar->height > 65535) {
8698 av_log(s, AV_LOG_ERROR, "Resolution %dx%d too large for mov/mp4\n", st->codecpar->width, st->codecpar->height);
8699 return AVERROR(EINVAL);
8700 }
8701 if (track->mode == MODE_MOV && track->timescale > 100000)
8703 "WARNING codec timebase is very high. If duration is too long,\n"
8704 "file may not be playable by quicktime. Specify a shorter timebase\n"
8705 "or choose different container.\n");
8706 if (track->mode == MODE_MOV &&
8707 track->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
8708 track->tag == MKTAG('r','a','w',' ')) {
8709 enum AVPixelFormat pix_fmt = track->par->format;
8710 if (pix_fmt == AV_PIX_FMT_NONE && track->par->bits_per_coded_sample == 1)
8712 track->is_unaligned_qt_rgb =
8719 }
8720 if (track->par->codec_id == AV_CODEC_ID_VP9 && track->mode != MODE_MP4) {
8721 av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
8722 return AVERROR(EINVAL);
8723 } else if (track->par->codec_id == AV_CODEC_ID_AV1 &&
8724 track->mode != MODE_MP4 && track->mode != MODE_AVIF) {
8725 av_log(s, AV_LOG_ERROR, "%s only supported in MP4 and AVIF.\n", avcodec_get_name(track->par->codec_id));
8726 return AVERROR(EINVAL);
8727 } else if (track->par->codec_id == AV_CODEC_ID_VP8) {
8728 /* altref frames handling is not defined in the spec as of version v1.0,
8729 * so just forbid muxing VP8 streams altogether until a new version does */
8730 av_log(s, AV_LOG_ERROR, "VP8 muxing is currently not supported.\n");
8731 return AVERROR_PATCHWELCOME;
8732 } else if (track->par->codec_id == AV_CODEC_ID_APV) {
8733 ret = ff_isom_init_apvc(&track->apv, s);
8734 if (ret < 0)
8735 return ret;
8736 }
8737 if (is_cover_image(st)) {
8738 track->cover_image = av_packet_alloc();
8739 if (!track->cover_image)
8740 return AVERROR(ENOMEM);
8741 }
8742 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
8743 track->timescale = st->codecpar->sample_rate;
8745 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
8746 track->audio_vbr = 1;
8747 }else if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_MS ||
8750 if (!st->codecpar->block_align) {
8751 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
8752 return AVERROR(EINVAL);
8753 }
8754 track->sample_size = st->codecpar->block_align;
8755 }else if (st->codecpar->frame_size > 1){ /* assume compressed audio */
8756 track->audio_vbr = 1;
8757 }else{
8758 track->sample_size = (av_get_bits_per_sample(st->codecpar->codec_id) >> 3) *
8760 }
8761 if (st->codecpar->codec_id == AV_CODEC_ID_ILBC ||
8763 track->audio_vbr = 1;
8764 }
8765 if (track->mode != MODE_MOV &&
8766 track->par->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
8767 if (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
8768 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not standard, to mux anyway set strict to -1\n",
8769 i, track->par->sample_rate);
8770 return AVERROR(EINVAL);
8771 } else {
8772 av_log(s, AV_LOG_WARNING, "track %d: muxing mp3 at %dhz is not standard in MP4\n",
8773 i, track->par->sample_rate);
8774 }
8775 }
8776 if (track->par->codec_id == AV_CODEC_ID_FLAC ||
8777 track->par->codec_id == AV_CODEC_ID_TRUEHD ||
8778 track->par->codec_id == AV_CODEC_ID_OPUS) {
8779 if (track->mode != MODE_MP4) {
8780 av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
8781 return AVERROR(EINVAL);
8782 }
8783 if (track->par->codec_id == AV_CODEC_ID_TRUEHD &&
8784 s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
8786 "%s in MP4 support is experimental, add "
8787 "'-strict %d' if you want to use it.\n",
8789 return AVERROR_EXPERIMENTAL;
8790 }
8791 }
8792 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
8793 track->timescale = st->time_base.den;
8794
8795 if (track->par->codec_id == AV_CODEC_ID_TTML) {
8796 /* 14496-30 requires us to use a single sample per fragment
8797 for TTML, for which we define a per-track flag.
8798
8799 We set the flag in case we are receiving TTML paragraphs
8800 from the input, in other words in case we are not doing
8801 stream copy. */
8804
8805 if (track->mode != MODE_ISM &&
8806 track->par->codec_tag == MOV_ISMV_TTML_TAG &&
8807 s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
8809 "ISMV style TTML support with the 'dfxp' tag in "
8810 "non-ISMV formats is not officially supported. Add "
8811 "'-strict unofficial' if you want to use it.\n");
8812 return AVERROR_EXPERIMENTAL;
8813 }
8814 }
8815 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
8816 track->timescale = st->time_base.den;
8817 if (track->tag == MKTAG('t','m','c','d') &&
8818 st->codecpar->extradata_size >= 8)
8819 track->timecode_flags = AV_RB32(st->codecpar->extradata + 4);
8820 } else {
8821 track->timescale = mov->movie_timescale;
8822 }
8823 if (!track->height)
8824 track->height = st->codecpar->height;
8825 /* The Protected Interoperable File Format (PIFF) standard, used by ISMV recommends but
8826 doesn't mandate a track timescale of 10,000,000. The muxer allows a custom timescale
8827 for video tracks, so if user-set, it isn't overwritten */
8828 if (mov->mode == MODE_ISM &&
8831 track->timescale = 10000000;
8832 }
8833
8834 avpriv_set_pts_info(st, 64, 1, track->timescale);
8835
8837 ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
8838 (track->par->codec_id == AV_CODEC_ID_H264 || track->par->codec_id == AV_CODEC_ID_HEVC ||
8839 track->par->codec_id == AV_CODEC_ID_VVC || track->par->codec_id == AV_CODEC_ID_AV1),
8840 track->par->codec_id, s->flags & AVFMT_FLAG_BITEXACT);
8841 if (ret)
8842 return ret;
8843 }
8844 }
8845
8846 for (i = 0; i < s->nb_stream_groups; i++) {
8847 AVStreamGroup *stg = s->stream_groups[i];
8848
8849 switch (stg->type) {
8852 AVStream *st = stg->entries[lcevc->el_index]->stream;
8853 MOVTrack *track = st->priv_data;
8854
8855 for (int j = 0; j < mov->nb_tracks; j++) {
8856 MOVTrack *trk = &mov->tracks[j];
8857
8858 if (trk->st == stg->entries[!lcevc->el_index]->stream) {
8859 track->src_track = av_malloc(sizeof(*track->src_track));
8860 if (!track->src_track)
8861 return AVERROR(ENOMEM);
8862 *track->src_track = j;
8863 track->nb_src_track = 1;
8864 break;
8865 }
8866 }
8867
8868 track->par->width = lcevc->width;
8869 track->par->height = track->height = lcevc->height;
8870 break;
8871 }
8873 const AVStreamGroupTREF *tref = stg->params.tref;
8874 MOVTrack *track;
8875
8876 if (tref->metadata_index >= stg->nb_entries)
8877 return AVERROR(EINVAL);
8878
8879 track = stg->entries[tref->metadata_index]->stream->priv_data;
8880 for (int j = 0; j < stg->nb_entries; j++) {
8881 const AVStream *st2 = stg->entries[j]->stream;
8882 int index = -1;
8883
8884 if (j == tref->metadata_index)
8885 continue;
8886
8887 for (int k = 0; k < mov->nb_tracks; k++) {
8888 if (mov->tracks[k].st != st2)
8889 continue;
8890 index = k;
8891 break;
8892 }
8893 if (index < 0)
8894 return AVERROR(EINVAL);
8895
8896 int *tmp = av_realloc(track->src_track,
8897 (track->nb_src_track + 1) * sizeof(*track->src_track));
8898 if (!tmp)
8899 return AVERROR(ENOMEM);
8900 track->src_track = tmp;
8901 track->src_track[track->nb_src_track++] = index;
8902 }
8903 break;
8904 }
8905 default:
8906 break;
8907 }
8908 }
8909
8910 /* fill src_track for tmcd tracks not covered by stream groups or
8911 * created by this muxer, retaining the old behavior of src_track
8912 * being arbitrarily 0. */
8913 for (i = 0; i < s->nb_streams; i++) {
8914 AVStream *st = s->streams[i];
8915 MOVTrack *track = st->priv_data;
8916 if (st->codecpar->codec_tag != MKTAG('t','m','c','d') ||
8917 track->nb_src_track)
8918 continue;
8919
8920 track->src_track = av_malloc(sizeof(*track->src_track));
8921 if (!track->src_track)
8922 return AVERROR(ENOMEM);
8923 *track->src_track = 0;
8924 track->nb_src_track = 1;
8925 }
8926
8928 return 0;
8929}
8930
8932{
8933 AVIOContext *pb = s->pb;
8934 MOVMuxContext *mov = s->priv_data;
8935 int ret, hint_track = 0, tmcd_track = 0, nb_tracks = mov->nb_streams;
8936
8937 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
8938 nb_tracks++;
8939
8940 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
8941 hint_track = nb_tracks;
8942 for (int i = 0; i < mov->nb_streams; i++) {
8943 if (rtp_hinting_needed(mov->tracks[i].st))
8944 nb_tracks++;
8945 }
8946 }
8947
8948 if (mov->nb_meta_tmcd)
8949 tmcd_track = nb_tracks;
8950
8951 for (int i = 0; i < mov->nb_streams; i++) {
8952 MOVTrack *track = &mov->tracks[i];
8953 AVStream *st = track->st;
8954
8955 /* copy extradata if it exists */
8956 if (st->codecpar->extradata_size) {
8959 else if (!TAG_IS_AVCI(track->tag) && st->codecpar->codec_id != AV_CODEC_ID_DNXHD) {
8961 track->extradata[track->last_stsd_index] =
8963 if (!track->extradata[track->last_stsd_index]) {
8964 return AVERROR(ENOMEM);
8965 }
8966 memcpy(track->extradata[track->last_stsd_index],
8967 st->codecpar->extradata, track->extradata_size[track->last_stsd_index]);
8968 memset(track->extradata[track->last_stsd_index] + track->extradata_size[track->last_stsd_index],
8970 }
8971 }
8972
8976 continue;
8977
8978 for (int j = 0; j < mov->nb_streams; j++) {
8979 AVStream *stj= mov->tracks[j].st;
8980 MOVTrack *trackj= &mov->tracks[j];
8981 if (j == i)
8982 continue;
8983
8984 if (stj->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
8985 (trackj->par->ch_layout.nb_channels != 1 ||
8988 )
8989 track->mono_as_fc = -1;
8990
8991 if (stj->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
8994 trackj->par->ch_layout.nb_channels == 1 && track->mono_as_fc >= 0
8995 )
8996 track->mono_as_fc++;
8997
8998 if (stj->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
9001 trackj->language != track->language ||
9002 trackj->tag != track->tag
9003 )
9004 continue;
9005 track->multichannel_as_mono++;
9006 }
9007 }
9008
9009 if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV) ||
9011 if ((ret = mov_write_identification(pb, s)) < 0)
9012 return ret;
9013 }
9014
9015 if (mov->reserved_moov_size){
9016 mov->reserved_header_pos = avio_tell(pb);
9017 if (mov->reserved_moov_size > 0)
9018 avio_skip(pb, mov->reserved_moov_size);
9019 }
9020
9021 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
9022 /* If no fragmentation options have been set, set a default. */
9023 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
9029 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
9030 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
9031 mov->mdat_pos = avio_tell(pb);
9032 // The free/wide header that later will be converted into an
9033 // mdat, covering the initial moov and all the fragments.
9034 avio_wb32(pb, 0);
9035 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
9036 // Write an ftyp atom, hidden in a free/wide. This is neither
9037 // exposed while the file is written, as fragmented, nor when the
9038 // file is finalized into non-fragmented form. However, this allows
9039 // accessing a pristine, sequential ftyp+moov init segment, even
9040 // after the file is finalized. It also allows dumping the whole
9041 // contents of the mdat box, to get the fragmented form of the
9042 // file.
9043 if ((ret = mov_write_identification(pb, s)) < 0)
9044 return ret;
9045 update_size(pb, mov->mdat_pos);
9046 }
9047 } else if (mov->mode != MODE_AVIF) {
9048 if (mov->flags & FF_MOV_FLAG_FASTSTART)
9049 mov->reserved_header_pos = avio_tell(pb);
9050 mov_write_mdat_tag(pb, mov);
9051 }
9052
9054 if (mov->time)
9055 mov->time += 0x7C25B080; // 1970 based -> 1904 based
9056
9057 if (mov->chapter_track)
9058 if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
9059 return ret;
9060
9061 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
9062 for (int i = 0; i < mov->nb_streams; i++) {
9063 if (rtp_hinting_needed(mov->tracks[i].st)) {
9064 if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
9065 return ret;
9066 hint_track++;
9067 }
9068 }
9069 }
9070
9071 if (mov->nb_meta_tmcd) {
9072 const AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata,
9073 "timecode", NULL, 0);
9074 /* Initialize the tmcd tracks */
9075 for (int i = 0; i < mov->nb_streams; i++) {
9076 AVStream *st = mov->tracks[i].st;
9077 t = global_tcr;
9078
9080 AVTimecode tc;
9081 if (!t)
9082 t = av_dict_get(st->metadata, "timecode", NULL, 0);
9083 if (!t)
9084 continue;
9085 if (mov_check_timecode_track(s, &tc, st, t->value) < 0)
9086 continue;
9087 if ((ret = mov_create_timecode_track(s, tmcd_track, i, tc)) < 0)
9088 return ret;
9089 tmcd_track++;
9090 }
9091 }
9092 }
9093
9094 avio_flush(pb);
9095
9096 if (mov->flags & FF_MOV_FLAG_ISML)
9097 mov_write_isml_manifest(pb, mov, s);
9098
9099 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
9100 !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
9101 if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
9102 return ret;
9103 mov->moov_written = 1;
9104 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
9105 mov->reserved_header_pos = avio_tell(pb);
9106 }
9107
9108 return 0;
9109}
9110
9112{
9113 int ret;
9114 AVIOContext *moov_buf;
9115 MOVMuxContext *mov = s->priv_data;
9116
9117 if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
9118 return ret;
9119 if ((ret = mov_write_moov_tag(moov_buf, mov, s)) < 0)
9120 return ret;
9121 return ffio_close_null_buf(moov_buf);
9122}
9123
9125{
9126 int ret;
9127 AVIOContext *buf;
9128 MOVMuxContext *mov = s->priv_data;
9129
9130 if ((ret = ffio_open_null_buf(&buf)) < 0)
9131 return ret;
9132 mov_write_sidx_tags(buf, mov, -1, 0);
9133 return ffio_close_null_buf(buf);
9134}
9135
9136/*
9137 * This function gets the moov size if moved to the top of the file: the chunk
9138 * offset table can switch between stco (32-bit entries) to co64 (64-bit
9139 * entries) when the moov is moved to the beginning, so the size of the moov
9140 * would change. It also updates the chunk offset tables.
9141 */
9143{
9144 int i, moov_size, moov_size2;
9145 MOVMuxContext *mov = s->priv_data;
9146
9147 moov_size = get_moov_size(s);
9148 if (moov_size < 0)
9149 return moov_size;
9150
9151 for (i = 0; i < mov->nb_tracks; i++)
9152 mov->tracks[i].data_offset += moov_size;
9153
9154 moov_size2 = get_moov_size(s);
9155 if (moov_size2 < 0)
9156 return moov_size2;
9157
9158 /* if the size changed, we just switched from stco to co64 and need to
9159 * update the offsets */
9160 if (moov_size2 != moov_size)
9161 for (i = 0; i < mov->nb_tracks; i++)
9162 mov->tracks[i].data_offset += moov_size2 - moov_size;
9163
9164 return moov_size2;
9165}
9166
9168{
9169 int i, sidx_size;
9170 MOVMuxContext *mov = s->priv_data;
9171
9172 sidx_size = get_sidx_size(s);
9173 if (sidx_size < 0)
9174 return sidx_size;
9175
9176 for (i = 0; i < mov->nb_tracks; i++)
9177 mov->tracks[i].data_offset += sidx_size;
9178
9179 return sidx_size;
9180}
9181
9183{
9184 int moov_size;
9185 MOVMuxContext *mov = s->priv_data;
9186
9187 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
9188 moov_size = compute_sidx_size(s);
9189 else
9190 moov_size = compute_moov_size(s);
9191 if (moov_size < 0)
9192 return moov_size;
9193
9194 return ff_format_shift_data(s, mov->reserved_header_pos, moov_size);
9195}
9196
9198{
9199 MOVMuxContext *mov = s->priv_data;
9200 AVIOContext *pb = s->pb;
9201
9202 /* Write size of mdat tag */
9203 if (mov->mdat_size + 8 <= UINT32_MAX) {
9204 avio_seek(pb, mov->mdat_pos, SEEK_SET);
9205 avio_wb32(pb, mov->mdat_size + 8);
9207 ffio_wfourcc(pb, "mdat"); // overwrite the original free/wide into a mdat
9208 } else {
9209 /* overwrite 'wide' placeholder atom */
9210 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
9211 /* special value: real atom size will be 64 bit value after
9212 * tag field */
9213 avio_wb32(pb, 1);
9214 ffio_wfourcc(pb, "mdat");
9215 avio_wb64(pb, mov->mdat_size + 16);
9216 }
9217}
9218
9220{
9221 MOVMuxContext *mov = s->priv_data;
9222 AVIOContext *pb = s->pb;
9223 int res = 0;
9224 int i;
9225 int64_t moov_pos;
9226
9227 /*
9228 * Before actually writing the trailer, make sure that there are no
9229 * dangling subtitles, that need a terminating sample.
9230 */
9231 for (i = 0; i < mov->nb_tracks; i++) {
9232 MOVTrack *trk = &mov->tracks[i];
9233 if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
9237 }
9238 }
9239
9240 //Also check for a buffered EAC3 packet
9241 for (i = 0; i < mov->nb_tracks; i++) {
9242 MOVTrack *trk = &mov->tracks[i];
9243 if (trk->par->codec_id != AV_CODEC_ID_EAC3)
9244 continue;
9245 struct eac3_info *info = trk->eac3_priv;
9246 if (!info || !info->pkt || !info->pkt->size)
9247 continue;
9248
9249 av_packet_move_ref(mov->pkt, info->pkt);
9250 info->eof = 1;
9251 res = mov_write_single_packet(s, mov->pkt);
9252 if (res < 0)
9253 return res;
9254
9255 av_packet_unref(mov->pkt);
9256 }
9257
9258 // Check if we have any tracks that require squashing.
9259 // In that case, we'll have to write the packet here.
9260 if ((res = mov_write_squashed_packets(s)) < 0)
9261 return res;
9262
9263 // If there were no chapters when the header was written, but there
9264 // are chapters now, write them in the trailer. This only works
9265 // when we are not doing fragments.
9266 if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
9267 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
9268 mov->chapter_track = mov->nb_tracks++;
9269 if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
9270 return res;
9271 }
9272 }
9273
9274 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT) ||
9278 mov->mdat_size = avio_tell(pb) - mov->mdat_pos - 8;
9279 for (i = 0; i < mov->nb_tracks; i++) {
9280 MOVTrack *track = &mov->tracks[i];
9281 track->data_offset = 0;
9282 av_free(track->cluster);
9283 track->cluster = track->cluster_written;
9284 track->entry = track->entry_written;
9285 track->cluster_written = NULL;
9286 track->entry_written = 0;
9287 track->chunkCount = 0; // Force build_chunks to rebuild the list of chunks
9288 }
9289 // Clear the empty_moov flag, as we do want the moov to include
9290 // all the samples at this point.
9292 }
9293
9294 moov_pos = avio_tell(pb);
9295
9298
9299 avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_header_pos : moov_pos, SEEK_SET);
9300
9301 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
9302 av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
9303 res = shift_data(s);
9304 if (res < 0)
9305 return res;
9306 avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
9307 if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
9308 return res;
9309 } else if (mov->reserved_moov_size > 0) {
9310 int64_t size;
9311 if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
9312 return res;
9314 if (size < 8){
9315 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
9316 return AVERROR(EINVAL);
9317 }
9318 avio_wb32(pb, size);
9319 ffio_wfourcc(pb, "free");
9320 ffio_fill(pb, 0, size - 8);
9321 avio_seek(pb, moov_pos, SEEK_SET);
9322 } else {
9323 if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
9324 return res;
9325 }
9326
9328 // With hybrid fragmentation, only write the mdat size (hiding
9329 // the original moov and all the fragments within the mdat)
9330 // after we've successfully written the complete moov, to avoid
9331 // risk for an unreadable file if writing the final moov fails.
9333 }
9334
9335 res = 0;
9336 } else {
9338 for (i = 0; i < mov->nb_tracks; i++)
9339 mov->tracks[i].data_offset = 0;
9340 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) {
9341 int64_t end;
9342 av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
9343 res = shift_data(s);
9344 if (res < 0)
9345 return res;
9346 end = avio_tell(pb);
9347 avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
9348 mov_write_sidx_tags(pb, mov, -1, 0);
9349 avio_seek(pb, end, SEEK_SET);
9350 }
9351 if (!(mov->flags & FF_MOV_FLAG_SKIP_TRAILER)) {
9353 res = mov_write_mfra_tag(pb, mov);
9354 if (res < 0)
9355 return res;
9356 }
9357 }
9358
9359 return res;
9360}
9361
9363 const AVPacket *pkt)
9364{
9365 int ret = 1;
9366
9367 if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
9368 if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
9369 ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
9370 } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
9371 ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
9372 }
9373
9374 return ret;
9375}
9376
9377#if CONFIG_AVIF_MUXER
9378static int avif_write_trailer(AVFormatContext *s)
9379{
9380 AVIOContext *pb = s->pb;
9381 MOVMuxContext *mov = s->priv_data;
9382 int64_t pos_backup, extent_offsets[2];
9383 uint8_t *buf;
9384 int buf_size, moov_size;
9385
9386 if (mov->moov_written) return 0;
9387
9388 mov->is_animated_avif = s->streams[0]->nb_frames > 1;
9389 if (mov->is_animated_avif && mov->nb_streams > 1) {
9390 // For animated avif with alpha channel, we need to write a tref tag
9391 // with type "auxl".
9392 MovTag *tag = mov_add_tref_tag(mov, &mov->tracks[1], MKTAG('a', 'u', 'x', 'l'));
9393 if (!tag)
9394 return AVERROR(ENOMEM);
9395
9396 int ret = mov_add_tref_id(mov, tag, 1);
9397 if (ret < 0)
9398 return ret;
9399 }
9401 mov_write_meta_tag(pb, mov, s);
9402
9403 moov_size = get_moov_size(s);
9404 for (int i = 0; i < mov->nb_tracks; i++)
9405 mov->tracks[i].data_offset = avio_tell(pb) + moov_size + 8;
9406
9407 if (mov->is_animated_avif) {
9408 int ret;
9409 if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
9410 return ret;
9411 }
9412
9413 buf_size = avio_get_dyn_buf(mov->mdat_buf, &buf);
9414 avio_wb32(pb, buf_size + 8);
9415 ffio_wfourcc(pb, "mdat");
9416
9417 // The offset for the YUV planes is the starting position of mdat.
9418 extent_offsets[0] = avio_tell(pb);
9419 // The offset for alpha plane is YUV offset + YUV size.
9420 extent_offsets[1] = extent_offsets[0] + mov->avif_extent_length[0];
9421
9422 avio_write(pb, buf, buf_size);
9423
9424 // write extent offsets.
9425 pos_backup = avio_tell(pb);
9426 for (int i = 0; i < mov->nb_streams; i++) {
9427 if (extent_offsets[i] != (uint32_t)extent_offsets[i]) {
9428 av_log(s, AV_LOG_ERROR, "extent offset does not fit in 32 bits\n");
9429 return AVERROR_INVALIDDATA;
9430 }
9431 avio_seek(pb, mov->avif_extent_pos[i], SEEK_SET);
9432 avio_wb32(pb, extent_offsets[i]); /* rewrite offset */
9433 }
9434 avio_seek(pb, pos_backup, SEEK_SET);
9435
9436 return 0;
9437}
9438#endif
9439
9440#if CONFIG_TGP_MUXER || CONFIG_TG2_MUXER
9441static const AVCodecTag codec_3gp_tags[] = {
9442 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
9443 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
9444 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
9445 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
9446 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
9447 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
9448 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
9449 { AV_CODEC_ID_NONE, 0 },
9450};
9451static const AVCodecTag *const codec_3gp_tags_list[] = { codec_3gp_tags, NULL };
9452#endif
9453
9454static const AVCodecTag codec_mp4_tags[] = {
9455 { AV_CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
9456 { AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
9457 { AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '3') },
9458 { AV_CODEC_ID_HEVC, MKTAG('h', 'e', 'v', '1') },
9459 { AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1') },
9460 { AV_CODEC_ID_HEVC, MKTAG('d', 'v', 'h', '1') },
9461 { AV_CODEC_ID_VVC, MKTAG('v', 'v', 'c', '1') },
9462 { AV_CODEC_ID_VVC, MKTAG('v', 'v', 'i', '1') },
9463 { AV_CODEC_ID_EVC, MKTAG('e', 'v', 'c', '1') },
9464 { AV_CODEC_ID_LCEVC, MKTAG('l', 'v', 'c', '1') },
9465 { AV_CODEC_ID_APV, MKTAG('a', 'p', 'v', '1') },
9466 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', '4', 'v') },
9467 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', '4', 'v') },
9468 { AV_CODEC_ID_MJPEG, MKTAG('m', 'p', '4', 'v') },
9469 { AV_CODEC_ID_PNG, MKTAG('m', 'p', '4', 'v') },
9470 { AV_CODEC_ID_JPEG2000, MKTAG('m', 'p', '4', 'v') },
9471 { AV_CODEC_ID_VC1, MKTAG('v', 'c', '-', '1') },
9472 { AV_CODEC_ID_DIRAC, MKTAG('d', 'r', 'a', 'c') },
9473 { AV_CODEC_ID_TSCC2, MKTAG('m', 'p', '4', 'v') },
9474 { AV_CODEC_ID_VP9, MKTAG('v', 'p', '0', '9') },
9475 { AV_CODEC_ID_AV1, MKTAG('a', 'v', '0', '1') },
9476 { AV_CODEC_ID_AV1, MKTAG('d', 'a', 'v', '1') },
9477 { AV_CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
9478 { AV_CODEC_ID_APPLE_APAC, MKTAG('a', 'p', 'a', 'c') },
9479 { AV_CODEC_ID_ALAC, MKTAG('a', 'l', 'a', 'c') },
9480 { AV_CODEC_ID_MP4ALS, MKTAG('m', 'p', '4', 'a') },
9481 { AV_CODEC_ID_MP3, MKTAG('m', 'p', '4', 'a') },
9482 { AV_CODEC_ID_MP2, MKTAG('m', 'p', '4', 'a') },
9483 { AV_CODEC_ID_AC3, MKTAG('a', 'c', '-', '3') },
9484 { AV_CODEC_ID_EAC3, MKTAG('e', 'c', '-', '3') },
9485 { AV_CODEC_ID_DTS, MKTAG('m', 'p', '4', 'a') },
9486 { AV_CODEC_ID_TRUEHD, MKTAG('m', 'l', 'p', 'a') },
9487 { AV_CODEC_ID_FLAC, MKTAG('f', 'L', 'a', 'C') },
9488 { AV_CODEC_ID_OPUS, MKTAG('O', 'p', 'u', 's') },
9489 { AV_CODEC_ID_VORBIS, MKTAG('m', 'p', '4', 'a') },
9490 { AV_CODEC_ID_QCELP, MKTAG('m', 'p', '4', 'a') },
9491 { AV_CODEC_ID_EVRC, MKTAG('m', 'p', '4', 'a') },
9492 { AV_CODEC_ID_DVD_SUBTITLE, MKTAG('m', 'p', '4', 's') },
9493 { AV_CODEC_ID_MOV_TEXT, MKTAG('t', 'x', '3', 'g') },
9494 { AV_CODEC_ID_BIN_DATA, MKTAG('g', 'p', 'm', 'd') },
9495 { AV_CODEC_ID_MPEGH_3D_AUDIO, MKTAG('m', 'h', 'm', '1') },
9498 { AV_CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
9499
9500 /* ISO/IEC 23003-5 integer formats */
9507 /* ISO/IEC 23003-5 floating-point formats */
9512
9513 { AV_CODEC_ID_AVS3, MKTAG('a', 'v', 's', '3') },
9514
9515 { AV_CODEC_ID_NONE, 0 },
9516};
9517#if CONFIG_MP4_MUXER || CONFIG_PSP_MUXER
9518static const AVCodecTag *const mp4_codec_tags_list[] = { codec_mp4_tags, NULL };
9519#endif
9520
9521static const AVCodecTag codec_ism_tags[] = {
9522 { AV_CODEC_ID_WMAPRO , MKTAG('w', 'm', 'a', ' ') },
9524 { AV_CODEC_ID_NONE , 0 },
9525};
9526
9527static const AVCodecTag codec_ipod_tags[] = {
9528 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
9529 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
9530 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
9531 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
9532 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
9533 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
9534 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
9535 { AV_CODEC_ID_NONE, 0 },
9536};
9537
9538static const AVCodecTag codec_f4v_tags[] = {
9539 { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
9540 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
9541 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
9542 { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
9543 { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
9544 { AV_CODEC_ID_NONE, 0 },
9545};
9546
9547#if CONFIG_AVIF_MUXER
9548
9549static const AVOption avif_options[] = {
9550 { "movie_timescale", "set movie timescale", offsetof(MOVMuxContext, movie_timescale), AV_OPT_TYPE_INT, {.i64 = MOV_TIMESCALE}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
9551 { "loop", "Number of times to loop animated AVIF: 0 - infinite loop", offsetof(MOVMuxContext, avif_loop_count), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = 0 },
9552 { NULL },
9553};
9554static const AVCodecTag codec_avif_tags[] = {
9555 { AV_CODEC_ID_AV1, MKTAG('a','v','0','1') },
9556 { AV_CODEC_ID_NONE, 0 },
9557};
9558static const AVCodecTag *const codec_avif_tags_list[] = { codec_avif_tags, NULL };
9559
9560static const AVClass mov_avif_muxer_class = {
9561 .class_name = "avif muxer",
9562 .item_name = av_default_item_name,
9563 .option = avif_options,
9564 .version = LIBAVUTIL_VERSION_INT,
9565};
9566#endif
9567
9568#if CONFIG_MOV_MUXER
9570 .p.name = "mov",
9571 .p.long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
9572 .p.extensions = "mov",
9573 .priv_data_size = sizeof(MOVMuxContext),
9574 .p.audio_codec = AV_CODEC_ID_AAC,
9575 .p.video_codec = CONFIG_LIBX264_ENCODER ?
9577 .init = mov_init,
9581 .deinit = mov_free,
9583 .p.codec_tag = (const AVCodecTag* const []){
9585 },
9586 .check_bitstream = mov_check_bitstream,
9587 .p.priv_class = &mov_isobmff_muxer_class,
9588 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9589};
9590#endif
9591#if CONFIG_TGP_MUXER
9593 .p.name = "3gp",
9594 .p.long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
9595 .p.extensions = "3gp",
9596 .priv_data_size = sizeof(MOVMuxContext),
9597 .p.audio_codec = AV_CODEC_ID_AMR_NB,
9598 .p.video_codec = AV_CODEC_ID_H263,
9599 .init = mov_init,
9600 .write_header = mov_write_header,
9601 .write_packet = mov_write_packet,
9602 .write_trailer = mov_write_trailer,
9603 .deinit = mov_free,
9605 .p.codec_tag = codec_3gp_tags_list,
9606 .check_bitstream = mov_check_bitstream,
9607 .p.priv_class = &mov_isobmff_muxer_class,
9608 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9609};
9610#endif
9611#if CONFIG_MP4_MUXER
9613 .p.name = "mp4",
9614 .p.long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
9615 .p.mime_type = "video/mp4",
9616 .p.extensions = "mp4",
9617 .priv_data_size = sizeof(MOVMuxContext),
9618 .p.audio_codec = AV_CODEC_ID_AAC,
9619 .p.video_codec = CONFIG_LIBX264_ENCODER ?
9621 .init = mov_init,
9625 .deinit = mov_free,
9627 .p.codec_tag = mp4_codec_tags_list,
9629 .p.priv_class = &mov_isobmff_muxer_class,
9630 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9631};
9632#endif
9633#if CONFIG_PSP_MUXER
9635 .p.name = "psp",
9636 .p.long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
9637 .p.extensions = "mp4,psp",
9638 .priv_data_size = sizeof(MOVMuxContext),
9639 .p.audio_codec = AV_CODEC_ID_AAC,
9640 .p.video_codec = CONFIG_LIBX264_ENCODER ?
9642 .init = mov_init,
9646 .deinit = mov_free,
9648 .p.codec_tag = mp4_codec_tags_list,
9650 .p.priv_class = &mov_isobmff_muxer_class,
9651 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9652};
9653#endif
9654#if CONFIG_TG2_MUXER
9656 .p.name = "3g2",
9657 .p.long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
9658 .p.extensions = "3g2",
9659 .priv_data_size = sizeof(MOVMuxContext),
9660 .p.audio_codec = AV_CODEC_ID_AMR_NB,
9661 .p.video_codec = AV_CODEC_ID_H263,
9662 .init = mov_init,
9663 .write_header = mov_write_header,
9664 .write_packet = mov_write_packet,
9665 .write_trailer = mov_write_trailer,
9666 .deinit = mov_free,
9668 .p.codec_tag = codec_3gp_tags_list,
9669 .check_bitstream = mov_check_bitstream,
9670 .p.priv_class = &mov_isobmff_muxer_class,
9671 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9672};
9673#endif
9674#if CONFIG_IPOD_MUXER
9676 .p.name = "ipod",
9677 .p.long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
9678 .p.mime_type = "video/mp4",
9679 .p.extensions = "m4v,m4a,m4b",
9680 .priv_data_size = sizeof(MOVMuxContext),
9681 .p.audio_codec = AV_CODEC_ID_AAC,
9682 .p.video_codec = AV_CODEC_ID_H264,
9683 .init = mov_init,
9684 .write_header = mov_write_header,
9685 .write_packet = mov_write_packet,
9686 .write_trailer = mov_write_trailer,
9687 .deinit = mov_free,
9689 .p.codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
9690 .check_bitstream = mov_check_bitstream,
9691 .p.priv_class = &mov_isobmff_muxer_class,
9692 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9693};
9694#endif
9695#if CONFIG_ISMV_MUXER
9697 .p.name = "ismv",
9698 .p.long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
9699 .p.mime_type = "video/mp4",
9700 .p.extensions = "ismv,isma",
9701 .priv_data_size = sizeof(MOVMuxContext),
9702 .p.audio_codec = AV_CODEC_ID_AAC,
9703 .p.video_codec = AV_CODEC_ID_H264,
9704 .init = mov_init,
9705 .write_header = mov_write_header,
9706 .write_packet = mov_write_packet,
9707 .write_trailer = mov_write_trailer,
9708 .deinit = mov_free,
9710 .p.codec_tag = (const AVCodecTag* const []){
9712 .check_bitstream = mov_check_bitstream,
9713 .p.priv_class = &mov_isobmff_muxer_class,
9714 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9715};
9716#endif
9717#if CONFIG_F4V_MUXER
9719 .p.name = "f4v",
9720 .p.long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
9721 .p.mime_type = "application/f4v",
9722 .p.extensions = "f4v",
9723 .priv_data_size = sizeof(MOVMuxContext),
9724 .p.audio_codec = AV_CODEC_ID_AAC,
9725 .p.video_codec = AV_CODEC_ID_H264,
9726 .init = mov_init,
9727 .write_header = mov_write_header,
9728 .write_packet = mov_write_packet,
9729 .write_trailer = mov_write_trailer,
9730 .deinit = mov_free,
9731 .p.flags = AVFMT_GLOBALHEADER,
9732 .p.codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
9733 .check_bitstream = mov_check_bitstream,
9734 .p.priv_class = &mov_isobmff_muxer_class,
9735 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9736};
9737#endif
9738#if CONFIG_AVIF_MUXER
9740 .p.name = "avif",
9741 .p.long_name = NULL_IF_CONFIG_SMALL("AVIF"),
9742 .p.mime_type = "image/avif",
9743 .p.extensions = "avif",
9744 .priv_data_size = sizeof(MOVMuxContext),
9745 .p.video_codec = AV_CODEC_ID_AV1,
9746 .init = mov_init,
9747 .write_header = mov_write_header,
9748 .write_packet = mov_write_packet,
9749 .write_trailer = avif_write_trailer,
9750 .deinit = mov_free,
9751 .p.flags = AVFMT_GLOBALHEADER,
9752 .p.codec_tag = codec_avif_tags_list,
9753 .p.priv_class = &mov_avif_muxer_class,
9754 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9755};
9756#endif
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition ac3_parser.c:491
@ EAC3_FRAME_TYPE_DEPENDENT
Definition ac3defs.h:112
@ EAC3_FRAME_TYPE_INDEPENDENT
Definition ac3defs.h:111
@ EAC3_FRAME_TYPE_AC3_CONVERT
Definition ac3defs.h:113
static double val(void *priv, double ch)
Definition aeval.c:77
const FFOutputFormat ff_avif_muxer
const FFOutputFormat ff_psp_muxer
const FFOutputFormat ff_tg2_muxer
const FFOutputFormat ff_mp4_muxer
const FFOutputFormat ff_f4v_muxer
const FFOutputFormat ff_mov_muxer
const FFOutputFormat ff_ipod_muxer
const FFOutputFormat ff_ismv_muxer
const FFOutputFormat ff_tgp_muxer
#define entry
static AVStream * video_st
static AVFormatContext * ctx
static void chunk(Buf *b, const char type[4], const uint8_t *data, int n)
Definition apnggen.c:93
static const uint8_t palette[16][3]
Definition apnggen.c:37
channels
Definition aptx.h:31
static const uint8_t channel_map[8][8]
int32_t
int ff_av1_parse_seq_header(AV1SequenceParameters *seq, const uint8_t *buf, int size)
Parses a Sequence Header from the the provided buffer.
Definition av1.c:336
int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size, int *offset)
Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and return the result in a data bu...
Definition av1.c:88
int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size, int write_seq_header)
Writes AV1 extradata (Sequence Header and Metadata OBUs) to the provided AVIOContext.
Definition av1.c:399
int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size)
Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write the resulting bitstream ...
Definition av1.c:83
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition avc.c:32
int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
Definition avc.c:145
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
@ AV_STREAM_GROUP_ENTRY_STREAM
Definition avformat.h:1173
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition avformat.h:1570
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:678
@ AV_STREAM_GROUP_PARAMS_TREF
Definition avformat.h:1163
@ AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
Definition avformat.h:1160
@ AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
Definition avformat.h:1159
@ AV_STREAM_GROUP_PARAMS_LCEVC
Definition avformat.h:1162
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:503
#define AV_DISPOSITION_MULTILAYER
The video stream contains multiple layers, e.g.
Definition avformat.h:738
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition avformat.h:512
#define AVFMT_AVOID_NEG_TS_AUTO
Enabled when required by target format.
Definition avformat.h:1807
#define AVFMT_FLAG_AUTO_BSF
Add bitstream filters as requested by the muxer.
Definition avformat.h:1573
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:682
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition avformat.h:499
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition avformat.h:694
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO
Shift timestamps so that they start at 0.
Definition avformat.h:1810
#define AV_DISPOSITION_DEFAULT
The stream should be chosen by default among other streams of the same type, unless the user has expl...
Definition avformat.h:641
Buffered I/O operations.
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition aviobuf.c:464
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition aviobuf.c:376
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
@ AVIO_DATA_MARKER_BOUNDARY_POINT
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition avio.h:127
@ AVIO_DATA_MARKER_TRAILER
Trailer data, which doesn't contain actual content, but only for finalizing the output file.
Definition avio.h:139
@ AVIO_DATA_MARKER_HEADER
Header data; this needs to be present for the stream to be decodeable.
Definition avio.h:114
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition avio.h:145
@ AVIO_DATA_MARKER_SYNC_POINT
A point in the output bytestream where a decoder can start decoding (i.e.
Definition avio.h:121
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:507
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
void avio_wb24(AVIOContext *s, unsigned int val)
Definition aviobuf.c:458
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1378
void avio_wb64(AVIOContext *s, uint64_t val)
Definition aviobuf.c:434
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition aviobuf.c:228
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1333
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1345
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1406
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition aviobuf.c:1367
void ffio_write_leb(AVIOContext *s, unsigned val)
Definition aviobuf.c:915
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition aviobuf.c:1430
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition aviobuf.c:1440
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flag(name)
Definition cbs_h264.c:60
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
static void deinit(AVFormatContext *s)
Definition chromaprint.c:53
AVCodecParameters * avcodec_parameters_alloc(void)
Definition codec_par.c:57
void avcodec_parameters_free(AVCodecParameters **ppar)
Definition codec_par.c:67
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition common.h:477
#define ROUNDED_DIV(a, b)
Definition common.h:58
#define av_clip_uint8
Definition common.h:106
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Colorspace value utility functions for libavutil.
#define max(a, b)
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition defs.h:61
#define AV_PROFILE_AAC_HE
Definition defs.h:72
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition defs.h:62
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define FF_COMPLIANCE_NORMAL
Definition defs.h:60
#define AV_PROFILE_AAC_HE_V2
Definition defs.h:73
#define AV_PROFILE_H264_INTRA
Definition defs.h:122
@ AV_FIELD_UNKNOWN
Definition defs.h:228
@ AV_FIELD_PROGRESSIVE
Definition defs.h:229
#define AV_PROFILE_AAC_USAC
Definition defs.h:76
#define AV_PROFILE_DNXHD
Definition defs.h:89
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
Public dictionary API.
static av_always_inline uint64_t ff_dnxhd_parse_header_prefix(const uint8_t *buf)
Definition dnxhddata.h:85
void ff_isom_put_dvcc_dvvc(void *logctx, uint8_t out[ISOM_DVCC_DVVC_SIZE], const AVDOVIDecoderConfigurationRecord *dovi)
Definition dovi_isom.c:89
#define ISOM_DVCC_DVVC_SIZE
Definition dovi_isom.h:29
DOVI configuration.
enum AVCodecID id
Definition dts2pts.c:612
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
AVEncryptionInitInfo * av_encryption_init_info_get_side_data(const uint8_t *side_data, size_t side_data_size)
Creates a copy of the AVEncryptionInitInfo that is contained in the given side data.
void av_encryption_init_info_free(AVEncryptionInitInfo *info)
Frees the given encryption init info object.
double value
Definition eval.c:102
int ff_isom_write_evcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes EVC sample metadata to the provided AVIOContext.
Definition evc.c:298
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:203
const char * key
static int64_t duration
Definition ffplay.c:359
int ret
Definition ffplay.c:391
static unsigned int nb_streams
Definition ffprobe.c:390
static void write_header(FFV1Context *f)
Definition ffv1enc.c:384
FLAC (Free Lossless Audio Codec) common stuff.
@ FLAC_METADATA_TYPE_STREAMINFO
Definition flac.h:46
#define FLAC_STREAMINFO_SIZE
Definition flac.h:32
bitstream reader API header.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define fail
Definition test.h:479
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_BINARY
Underlying C type is a uint8_t* that is either NULL or points to an array allocated with the av_mallo...
Definition opt.h:285
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AV_CH_LAYOUT_STEREO
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:557
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:461
@ AV_CODEC_ID_DIRAC
Definition codec_id.h:166
@ AV_CODEC_ID_PCM_F32LE
Definition codec_id.h:354
@ AV_CODEC_ID_PCM_S24BE
Definition codec_id.h:346
@ AV_CODEC_ID_AVS3
Definition codec_id.h:245
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:463
@ AV_CODEC_ID_V210
Definition codec_id.h:177
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:611
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_CODEC_ID_VP6F
Definition codec_id.h:142
@ AV_CODEC_ID_EAC3
Definition codec_id.h:498
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:338
@ AV_CODEC_ID_APV
Definition codec_id.h:323
@ AV_CODEC_ID_LCEVC
Definition codec_id.h:614
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:333
@ AV_CODEC_ID_DVD_SUBTITLE
Definition codec_id.h:571
@ AV_CODEC_ID_TTML
Definition codec_id.h:595
@ AV_CODEC_ID_PCM_F32BE
Definition codec_id.h:353
@ AV_CODEC_ID_MP4ALS
Definition codec_id.h:503
@ AV_CODEC_ID_SVQ3
Definition codec_id.h:73
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_EVRC
Definition codec_id.h:529
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:334
@ AV_CODEC_ID_PCM_S24LE
Definition codec_id.h:345
@ AV_CODEC_ID_VVC
Definition codec_id.h:247
@ AV_CODEC_ID_FFV1
Definition codec_id.h:83
@ AV_CODEC_ID_PCM_S32LE
Definition codec_id.h:341
@ AV_CODEC_ID_PCM_S8
Definition codec_id.h:337
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_ADPCM_MS
Definition codec_id.h:379
@ AV_CODEC_ID_VC1
Definition codec_id.h:120
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_MPEGH_3D_AUDIO
Definition codec_id.h:549
@ AV_CODEC_ID_PCM_F64LE
Definition codec_id.h:356
@ AV_CODEC_ID_ALAC
Definition codec_id.h:474
@ AV_CODEC_ID_EIA_608
Definition codec_id.h:581
@ AV_CODEC_ID_AMR_NB
Definition codec_id.h:439
@ AV_CODEC_ID_VP6A
Definition codec_id.h:156
@ AV_CODEC_ID_MP2
Definition codec_id.h:458
@ AV_CODEC_ID_DVVIDEO
Definition codec_id.h:74
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition codec_id.h:373
@ AV_CODEC_ID_DTS
Definition codec_id.h:462
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_BIN_DATA
Definition codec_id.h:612
@ AV_CODEC_ID_ADPCM_G726
Definition codec_id.h:384
@ AV_CODEC_ID_AAC
Definition codec_id.h:460
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:502
@ AV_CODEC_ID_FLAC
Definition codec_id.h:470
@ AV_CODEC_ID_BMP
Definition codec_id.h:128
@ AV_CODEC_ID_PCM_F64BE
Definition codec_id.h:355
@ AV_CODEC_ID_H263
Definition codec_id.h:54
@ AV_CODEC_ID_QCELP
Definition codec_id.h:482
@ AV_CODEC_ID_AMR_WB
Definition codec_id.h:440
@ AV_CODEC_ID_AC3
Definition codec_id.h:461
@ AV_CODEC_ID_TSCC2
Definition codec_id.h:213
@ AV_CODEC_ID_QDM2
Definition codec_id.h:477
@ AV_CODEC_ID_PCM_S32BE
Definition codec_id.h:342
@ AV_CODEC_ID_DNXHD
Definition codec_id.h:149
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition codec_id.h:374
@ AV_CODEC_ID_R10K
Definition codec_id.h:195
@ AV_CODEC_ID_EVC
Definition codec_id.h:316
@ AV_CODEC_ID_APPLE_APAC
Definition codec_id.h:567
@ AV_CODEC_ID_AVUI
Definition codec_id.h:252
@ AV_CODEC_ID_ILBC
Definition codec_id.h:517
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:459
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
@ AV_CODEC_ID_OPUS
Definition codec_id.h:518
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
@ AV_CODEC_ID_WMAPRO
Definition codec_id.h:495
@ AV_CODEC_ID_MOV_TEXT
Definition codec_id.h:576
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition packet.c:571
@ AV_PKT_DATA_HEVC_CONF
Dolby Vision enhancement-layer HEVC decoder configuration.
Definition packet.h:384
@ AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT
Ambient viewing environment metadata, as defined by H.274.
Definition packet.h:327
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_PRFT
Producer Reference Time data corresponding to the AVProducerReferenceTime struct, usually exported by...
Definition packet.h:265
@ AV_PKT_DATA_ICC_PROFILE
ICC profile data consisting of an opaque octet buffer following the format described by ISO 15076-1.
Definition packet.h:271
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition packet.h:219
@ AV_PKT_DATA_SPHERICAL
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
Definition packet.h:225
@ AV_PKT_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition packet.h:105
@ AV_PKT_DATA_ENCRYPTION_INIT_INFO
This side data is encryption initialization data.
Definition packet.h:246
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition packet.h:142
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
@ AV_PKT_DATA_STEREO3D
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition packet.h:111
@ AV_PKT_DATA_FRAME_CROPPING
The number of pixels to discard from the top/bottom/left/right border of the decoded frame to obtain ...
Definition packet.h:340
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition packet.h:232
@ AV_PKT_DATA_FALLBACK_TRACK
This side data contains an integer value representing the stream index of a "fallback" track.
Definition packet.h:137
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
Definition packet.h:280
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
#define AV_PKT_FLAG_DISCARD
Flag is used to discard packets which are required to maintain valid decoder state but are not requir...
Definition packet.h:664
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition packet.c:121
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
#define AV_PKT_FLAG_DISPOSABLE
Flag is used to indicate packets that contain frames that can be discarded by the decoder.
Definition packet.h:676
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
Definition packet.c:517
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition packet.c:443
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition format.c:41
#define AES_CTR_KEY_SIZE
Definition aes_ctr.h:35
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
int av_channel_layout_ambisonic_order(const AVChannelLayout *channel_layout)
Return the order if the layout is n-th order standard-order ambisonic.
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
#define AV_CHANNEL_LAYOUT_MONO
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
@ AV_CHAN_AMBISONIC_BASE
Range of channels between AV_CHAN_AMBISONIC_BASE and AV_CHAN_AMBISONIC_END represent Ambisonic compon...
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition buffer.c:56
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition dict.h:75
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:43
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it.
Definition error.h:74
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
double av_csp_approximate_eotf_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable EOTF 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition csp.c:187
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
AVRational av_gcd_q(AVRational a, AVRational b, int max_den, AVRational def)
Return the best rational so that a and b are multiple of it.
Definition rational.c:188
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
Definition rational.c:101
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition mathematics.c:58
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AV_ROUND_DOWN
Round toward -infinity.
@ AV_ROUND_UP
Round toward +infinity.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition mem.c:326
#define av_fourcc2str(fourcc)
Definition avutil.h:323
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_NB
Definition avutil.h:205
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
Definition avstring.c:95
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
@ AV_SPHERICAL_RECTILINEAR
Video frame displays on a flat, rectangular 2D surface.
Definition spherical.h:78
@ AV_SPHERICAL_EQUIRECTANGULAR
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition spherical.h:52
@ AV_SPHERICAL_EQUIRECTANGULAR_TILE
Video represents a portion of a sphere mapped on a flat surface using equirectangular projection.
Definition spherical.h:68
@ AV_SPHERICAL_CUBEMAP
Video frame is split into 6 faces of a cube, and arranged on a 3x2 layout.
Definition spherical.h:61
@ AV_SPHERICAL_HALF_EQUIRECTANGULAR
Video frame displays as a 180 degree equirectangular projection.
Definition spherical.h:73
@ AV_SPHERICAL_FISHEYE
Fisheye projection (Apple).
Definition spherical.h:84
const char * av_stereo3d_type_name(unsigned int type)
Provide a human-readable name of a given stereo3d type.
Definition stereo3d.c:93
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition stereo3d.h:194
@ AV_STEREO3D_2D
Video is not stereoscopic (and metadata has to be there).
Definition stereo3d.h:52
@ AV_STEREO3D_TOPBOTTOM
Views are on top of each other.
Definition stereo3d.h:76
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition stereo3d.h:64
@ AV_PRIMARY_EYE_NONE
Neither eye.
Definition stereo3d.h:178
@ AV_STEREO3D_VIEW_RIGHT
Frame contains only the right view.
Definition stereo3d.h:163
@ AV_STEREO3D_VIEW_PACKED
Frame contains two packed views.
Definition stereo3d.h:153
@ AV_STEREO3D_VIEW_LEFT
Frame contains only the left view.
Definition stereo3d.h:158
@ AV_STEREO3D_VIEW_UNSPEC
Content is unspecified.
Definition stereo3d.h:168
int index
Definition gxfenc.c:90
int a
int ff_isom_write_lhvc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness, void *logctx)
Writes L-HEVC extradata (parameter sets with nuh_layer_id > 0, as a LHEVCDecoderConfigurationRecord) ...
Definition hevc.c:1408
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
Definition hevc.c:1252
int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to the provided AVIOContext.
Definition hevc.c:1204
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness, void *logctx)
Writes HEVC extradata (parameter sets and declarative SEI NAL units with nuh_layer_id == 0,...
Definition hevc.c:1401
const VDPAUPixFmtMap * map
int ff_iamf_write_descriptors(const IAMFContext *iamf, AVIOContext *pb, void *log_ctx)
int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb, unsigned audio_substream_id, const AVPacket *pkt)
int ff_iamf_add_mix_presentation(IAMFContext *iamf, const AVStreamGroup *stg, void *log_ctx)
int ff_iamf_add_audio_element(IAMFContext *iamf, const AVStreamGroup *stg, void *log_ctx)
int ff_iamf_write_parameter_blocks(const IAMFContext *iamf, AVIOContext *pb, const AVPacket *pkt, void *log_ctx)
#define r
Definition input.c:42
#define b
Definition input.c:43
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition intfloat.h:70
#define AV_WB32(p, v)
#define AV_RB8(x)
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_RL16(p)
#define AV_WB16(p, v)
#define AV_RB24(x)
#define AV_RB16(p)
const AVCodecTag ff_mp4_obj_type[]
Definition isom.c:34
int ff_mov_iso639_to_lang(const char lang[4], int mp4)
Definition isom.c:234
const struct MP4TrackKindMapping ff_mov_track_kind_table[]
Definition isom.c:465
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition isom.c:75
#define MOV_TRUN_SAMPLE_DURATION
Definition isom.h:428
#define MOV_GRAPHICS_MODE_COPY
Definition isom.h:455
#define MOV_TRUN_SAMPLE_CTS
Definition isom.h:431
#define MOV_TKHD_FLAG_ENABLED
Definition isom.h:443
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition isom.h:424
const AVCodecTag ff_codec_movaudio_tags[]
Definition isom_tags.c:307
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition isom.h:441
const AVCodecTag ff_codec_movvideo_tags[]
Definition isom_tags.c:29
#define MOV_TFHD_DEFAULT_SIZE
Definition isom.h:421
#define MOV_ISMV_TTML_TAG
Definition isom.h:505
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO
Definition isom.h:440
#define TAG_IS_AVCI(tag)
Definition isom.h:465
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition isom.h:427
#define MOV_GRAPHICS_MODE_PREMUL_BLACK_ALPHA
Definition isom.h:461
#define MOV_MP4_FPCM_TAG
Definition isom.h:507
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition isom.h:434
#define MOV_SAMPLE_DEPENDENCY_UNKNOWN
Definition isom.h:450
#define MOV_TFHD_STSD_ID
Definition isom.h:419
#define MOV_TRUN_SAMPLE_FLAGS
Definition isom.h:430
#define MOV_MP4_TTML_TAG
Definition isom.h:506
#define MOV_TFHD_DEFAULT_FLAGS
Definition isom.h:422
#define MOV_SAMPLE_DEPENDENCY_YES
Definition isom.h:451
#define MOV_TRUN_DATA_OFFSET
Definition isom.h:426
#define MOV_TFHD_BASE_DATA_OFFSET
Definition isom.h:418
#define MOV_MP4_IPCM_TAG
Definition isom.h:508
#define MOV_TFHD_DURATION_IS_EMPTY
Definition isom.h:423
#define MOV_TKHD_FLAG_IN_MOVIE
Definition isom.h:444
#define MOV_TRUN_SAMPLE_SIZE
Definition isom.h:429
#define MOV_SAMPLE_DEPENDENCY_NO
Definition isom.h:452
#define MOV_GRAPHICS_MODE_STRAIGHT_ALPHA
Definition isom.h:459
#define MOV_TFHD_DEFAULT_DURATION
Definition isom.h:420
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
enum MPFType type
Definition jpegmpfenc.c:55
int ff_isom_write_lvcc(AVIOContext *pb, const uint8_t *data, int len, void *logctx)
Definition lcevc.c:264
unsigned offset
Definition libaomenc.c:763
common internal api header.
unsigned int ff_toupper4(unsigned int x)
Definition to_upper4.h:29
void ff_isom_write_apvc(AVIOContext *pb, const APVDecoderConfigurationRecord *apvc, void *logctx)
Definition apv.c:80
int ff_isom_init_apvc(APVDecoderConfigurationRecord **papvc, void *logctx)
Definition apv.c:352
void ff_isom_close_apvc(APVDecoderConfigurationRecord **papvc)
Definition apv.c:375
int ff_isom_parse_apvc(APVDecoderConfigurationRecord *apvc, const AVPacket *pkt, void *logctx)
Definition apv.c:252
internal header for HEVC (de)muxer utilities
void ff_iamf_uninit_context(IAMFContext *c)
Definition iamf.c:172
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition utils.c:133
int ff_sdp_write_media(char *buff, int size, const AVStream *st, int idx, const char *dest_addr, const char *dest_type, int port, int ttl, AVFormatContext *fmt)
Append the media-specific SDP fragment for the media stream c to the buffer buff.
Definition sdp.c:956
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition utils.c:257
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition utils.c:473
uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
Get the NTP time stamp formatted as per the RFC-5905.
Definition utils.c:262
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:135
#define NTP_OFFSET_US
Definition internal.h:441
const AVPacket * ff_interleaved_peek(AVFormatContext *s, int stream)
Find the next packet in the interleaving queue for the given stream.
Definition mux.c:1088
static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
Definition mux.c:1101
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition mux.c:1339
int ff_get_muxer_ts_offset(AVFormatContext *s, int stream_index, int64_t *offset)
Definition mux.c:1072
void ff_packet_list_free(PacketList *list)
Wipe the list and unref all the packets in it.
int ff_packet_list_put(PacketList *list, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition packet_list.c:40
static unsigned int ff_is_ttml_stream_paragraph_based(const AVCodecParameters *codecpar)
Definition ttmlenc.h:28
Libavformat version macros.
#define LIBAVFORMAT_IDENT
Definition version.h:45
internal header for H.266/VVC (de)muxer utilities
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
Stereoscopic video.
version
Definition libkvazaar.c:313
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
uint64_t layout
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
int ff_mov_get_channel_positions_from_layout(const AVChannelLayout *layout, uint8_t *position, int position_num)
Get ISO/IEC 23001-8 OutputChannelPosition from AVChannelLayout.
Definition mov_chan.c:741
int ff_mov_get_channel_layout_tag(const AVCodecParameters *par, uint32_t *layout, uint32_t *bitmap, uint32_t **pchannel_desc)
Get the channel layout tag for the specified codec id and channel layout.
Definition mov_chan.c:476
int ff_mov_get_channel_config_from_layout(const AVChannelLayout *layout, int *config)
Get ISO/IEC 23001-8 ChannelConfiguration from AVChannelLayout.
Definition mov_chan.c:705
@ MOV_CH_LAYOUT_MONO
Definition mov_chan.h:56
#define MOV_CH_LAYOUT_UNKNOWN
Definition mov_chan.h:52
static int mov_write_stsd_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:3247
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:202
static int mov_write_av3c(AVIOContext *pb, const uint8_t *data, int len)
Definition movenc.c:1673
static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4592
static int get_moov_size(AVFormatContext *s)
Definition movenc.c:9111
static unsigned compute_avg_bitrate(MOVTrack *track)
Definition movenc.c:736
static int mov_write_sidx_tag(AVIOContext *pb, MOVTrack *track, int ref_size, int total_sidx_size)
Definition movenc.c:6053
static const AVCodecTag codec_cover_image_tags[]
Definition movenc.c:2241
static int mov_write_header(AVFormatContext *s)
Definition movenc.c:8931
static int mov_write_sdtp_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:317
static const AVCodecTag codec_ism_tags[]
Definition movenc.c:9521
static MovTag * mov_add_tref_tag(MOVMuxContext *mov, MOVTrack *trk, uint32_t name)
Definition movenc.c:5440
static int utf8len(const uint8_t *b)
Definition movenc.c:144
static AVDictionaryEntry * get_metadata_lang(AVFormatContext *s, const char *tag, int *lang)
Definition movenc.c:4761
static int mov_write_stbl_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:3484
static int mov_write_ac3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:420
static int mov_write_hvcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1713
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:226
int ff_mov_set_fragment_end_hint(AVFormatContext *s, int stream_index, const AVPacket *pkt, AVRational src_time_base)
Definition movenc.c:7070
static uint16_t mov_graphics_mode(MOVTrack *track)
Definition movenc.c:3632
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
Definition movenc.c:4374
static int mov_write_evcc_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1750
static int mov_write_raw_metadata_tag(AVFormatContext *s, AVIOContext *pb, const char *name, const char *key)
Definition movenc.c:5098
static int mov_write_dfla_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:890
static int mov_write_dinf_tag(AVIOContext *pb)
Definition movenc.c:3523
static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1934
#define IS_MODE(muxer, config)
static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c, int16_t d, int16_t tx, int16_t ty)
Definition movenc.c:4088
static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset, int moof_size)
Definition movenc.c:5986
uint32_t tag
Definition movenc.c:2128
static int mov_write_emsg_tag(AVIOContext *pb, AVStream *st, AVPacket *pkt)
Definition movenc.c:7731
static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:6690
static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:1981
static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4032
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition movenc.c:7910
static int mov_mdhd_mvhd_tkhd_version(MOVMuxContext *mov, MOVTrack *track, int64_t duration)
Definition movenc.c:4023
static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks, int size)
Definition movenc.c:5921
static int mov_write_mhac_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:973
static int shift_data(AVFormatContext *s)
Definition movenc.c:9182
static int mov_write_av3c_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1692
static int64_t update_size_and_version(AVIOContext *pb, int64_t pos, int version)
Definition movenc.c:166
static int mov_write_wfex_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:879
static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1069
unsigned bps
Definition movenc.c:2129
static int mov_write_gama_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track, double gamma)
Definition movenc.c:2663
static int mov_write_amve_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2784
static const AVCodecTag codec_ipod_tags[]
Definition movenc.c:9527
static int mov_write_eac3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:645
static int co64_required(const MOVTrack *track)
Definition movenc.c:178
static int mov_write_sthd_tag(AVIOContext *pb)
Definition movenc.c:3540
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2650
static int compute_moov_size(AVFormatContext *s)
Definition movenc.c:9142
static int mov_write_loci_tag(AVFormatContext *s, AVIOContext *pb)
Definition movenc.c:4826
static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:6236
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition movenc.c:5118
static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov, int tracks, int ref_size)
Definition movenc.c:6114
static int mov_write_vexu_tag(AVFormatContext *s, AVIOContext *pb, const AVStereo3D *stereo3d, const AVSphericalMapping *spherical_mapping)
Definition movenc.c:2554
static MovTag * mov_find_tref_tag(MOVMuxContext *mov, const MOVTrack *trk, uint32_t name)
Definition movenc.c:5429
static const AVClass mov_isobmff_muxer_class
Definition movenc.c:134
static int mov_preroll_write_stbl_atoms(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3387
static void mov_write_ftyp_tag_internal(AVIOContext *pb, AVFormatContext *s, int has_h264, int has_video, int write_minor)
Definition movenc.c:6309
static int mov_write_dmlp_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:944
static int mov_write_vvcc_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1780
static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3186
static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, AVTimecode tc)
Definition movenc.c:8010
static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track, AVStream *st)
Definition movenc.c:8188
static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
Definition movenc.c:6152
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1274
static void mov_write_mdat_size(AVFormatContext *s)
Definition movenc.c:9197
static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
Definition movenc.c:5724
static void enable_tracks(AVFormatContext *s)
Definition movenc.c:8071
static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:3933
static int mov_pcm_le_gt16(enum AVCodecID codec_id)
Definition movenc.c:851
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition movenc.c:5872
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1658
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
This function writes extradata "as is".
Definition movenc.c:705
static int mov_write_covr(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:4931
static int mov_write_srat_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1438
static int mov_write_smhd_tag(AVIOContext *pb)
Definition movenc.c:3622
static int mov_write_clap_tag(AVIOContext *pb, MOVTrack *track, uint32_t top, uint32_t bottom, uint32_t left, uint32_t right)
Definition movenc.c:2622
static int mov_finish_fragment(MOVMuxContext *mov, MOVTrack *track, int64_t ref_pos)
Definition movenc.c:6777
static int mov_write_mdta_keys_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5014
static void find_compressor(char *compressor_name, int len, MOVTrack *track)
Definition movenc.c:2811
static void mov_parse_truehd_frame(AVPacket *pkt, MOVTrack *trk)
Definition movenc.c:6671
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition movenc.c:4102
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:370
static int mov_write_sv3d_tag(AVFormatContext *s, AVIOContext *pb, AVSphericalMapping *spherical_mapping)
Definition movenc.c:2395
static int mov_get_h264_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2043
static int mov_get_dnxhd_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2151
static uint32_t rgb_to_yuv(uint32_t rgb)
Definition movenc.c:8172
static int mov_write_lhvc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1728
static struct mpeg4_bit_rate_values calculate_mpeg4_bit_rates(MOVTrack *track)
Definition movenc.c:753
static int mov_auto_flush_fragment(AVFormatContext *s, int force)
Definition movenc.c:7021
static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks, int64_t mdat_size)
Definition movenc.c:6202
static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov, int tracks, int moof_size)
Definition movenc.c:6028
static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4247
static int mov_write_enda_tag_be(AVIOContext *pb)
Definition movenc.c:719
static int mov_write_track_metadata(AVIOContext *pb, AVStream *st, const char *tag, const char *str)
Definition movenc.c:4414
static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
Definition movenc.c:5583
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:4958
static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
Definition movenc.c:5960
static int mov_write_d263_tag(AVIOContext *pb)
Definition movenc.c:1635
static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:4808
static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int len)
Definition movenc.c:4904
static int mov_check_timecode_track(AVFormatContext *s, AVTimecode *tc, AVStream *src_st, const char *tcstr)
Definition movenc.c:8001
static int mov_write_ms_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:867
static uint16_t language_code(const char *str)
Definition movenc.c:5129
static int mov_write_custom_metadata(AVFormatContext *s, AVIOContext *pb, const char *mean, const char *name, const char *tag)
Definition movenc.c:4797
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4357
static void param_write_string(AVIOContext *pb, const char *name, const char *value)
Definition movenc.c:5578
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:798
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1816
static int64_t update_size(AVIOContext *pb, int64_t pos)
Definition movenc.c:156
static int64_t calc_elst_duration(MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4016
static int mov_add_tref_id(MOVMuxContext *mov, MovTag *tag, uint32_t id)
Definition movenc.c:5414
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5457
static int64_t calc_pts_duration(MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4007
static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1257
static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:5907
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:5158
static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:1450
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style)
Definition movenc.c:4718
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3147
static int mov_write_dref_tag(AVIOContext *pb)
Definition movenc.c:3372
static int mov_write_hdlr_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3661
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:4563
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, const char *tag, const char *str)
Definition movenc.c:5136
static int mov_find_tref_id(MOVMuxContext *mov, const MovTag *tag, uint32_t id)
Definition movenc.c:5405
static int mov_write_subtitle_end_packet(AVFormatContext *s, int stream_index, int64_t dts)
Definition movenc.c:7662
static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3570
static int mov_write_dvcc_dvvc_tag(AVFormatContext *s, AVIOContext *pb, AVDOVIDecoderConfigurationRecord *dovi)
Definition movenc.c:2596
static int mov_write_pssh_tag(AVIOContext *pb, AVStream *st)
Definition movenc.c:5293
static int mov_init(AVFormatContext *s)
Definition movenc.c:8317
static void param_write_int(AVIOContext *pb, const char *name, int value)
Definition movenc.c:5573
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int long_style)
Definition movenc.c:4786
static int mov_get_evc_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2105
static int mov_write_mdta_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:4999
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:4616
static int mov_write_track_kind(AVIOContext *pb, const char *scheme_uri, const char *value)
Definition movenc.c:4428
static void mov_write_psp_udta_tag(AVIOContext *pb, const char *str, const char *lang, int type)
Definition movenc.c:5242
static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2268
static const uint16_t fiel_data[]
Definition movenc.c:2303
static int mov_write_apvc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1799
static int mov_write_lvcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1767
static int mov_write_dops_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:910
static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3548
static int64_t rescale_rational(AVRational q, int b)
Definition movenc.c:2453
static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:5715
static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset)
Definition movenc.c:5730
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3323
static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:6260
static int mov_write_clli_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2733
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:6298
static int mov_write_chnl_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1370
static int mov_write_vmhd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3644
static int mov_write_pcmc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1412
static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1702
static int mov_write_video_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:2872
static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
Definition movenc.c:3235
static int check_pkt(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt)
Definition movenc.c:7035
static int mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:6490
static int mov_write_identification(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:6561
static int mov_write_ctts_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3282
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s, int disc)
Definition movenc.c:4877
static int mov_write_wave_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1130
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4392
static int mov_write_mdia_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4069
static const AVCodecTag codec_f4v_tags[]
Definition movenc.c:9538
static int is_clcp_track(MOVTrack *track)
Definition movenc.c:3655
static int mov_write_trak_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition movenc.c:4507
static int compute_sidx_size(AVFormatContext *s)
Definition movenc.c:9167
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:4681
static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
Definition movenc.c:1173
static int mov_write_track_kinds(AVIOContext *pb, AVStream *st)
Definition movenc.c:4454
static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
Definition movenc.c:466
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:6350
static int get_sidx_size(AVFormatContext *s)
Definition movenc.c:9124
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:262
static int get_samples_per_packet(MOVTrack *track)
Definition movenc.c:1332
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5184
static int mov_pcm_be_gt16(enum AVCodecID codec_id)
Definition movenc.c:859
static int mov_write_trailer(AVFormatContext *s)
Definition movenc.c:9219
static int mov_write_btrt_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1351
static int rtp_hinting_needed(const AVStream *st)
Definition movenc.c:192
static int mov_write_nmhd_tag(AVIOContext *pb)
Definition movenc.c:3532
static int mov_get_lpcm_flags(enum AVCodecID codec_id)
Compute flags for 'lpcm' tag.
Definition movenc.c:1287
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2160
static int mov_write_squashed_packet(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:6715
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4212
static int defined_frame_rate(AVFormatContext *s, AVStream *st)
Definition movenc.c:1972
static void mov_free(AVFormatContext *s)
Definition movenc.c:8113
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
Write uuid atom.
Definition movenc.c:2291
static void put_descr(AVIOContext *pb, int tag, unsigned int size)
Definition movenc.c:727
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition movenc.c:7112
static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:5974
static int mov_setup_track_ids(MOVMuxContext *mov, AVFormatContext *s)
Assign track ids.
Definition movenc.c:5366
static const struct @225371303101201053203360341351022255162073206032 mov_pix_fmt_tags[]
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
Definition movenc.c:4697
static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
Definition movenc.c:6611
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:5852
static int mov_flush_fragment(AVFormatContext *s, int force)
Definition movenc.c:6809
static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
Definition movenc.c:7571
static int mov_write_squashed_packets(AVFormatContext *s)
Definition movenc.c:6753
static int mov_write_eyes_tag(AVFormatContext *s, AVIOContext *pb, const AVStereo3D *stereo3d)
Definition movenc.c:2496
static unsigned int validate_codec_tag(const AVCodecTag *const *tags, unsigned int tag, int codec_id)
Definition movenc.c:2248
static void build_chunks(MOVTrack *trk)
Definition movenc.c:5330
static void get_pts_range(MOVMuxContext *mov, MOVTrack *track, int64_t *start, int64_t *end, int elst, int mdhd)
Definition movenc.c:3970
static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:4605
static void mov_write_vexu_proj_tag(AVFormatContext *s, AVIOContext *pb, const AVSphericalMapping *spherical_mapping)
Definition movenc.c:2469
static int mov_write_st3d_tag(AVFormatContext *s, AVIOContext *pb, AVStereo3D *stereo_3d)
Definition movenc.c:2365
static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2186
static int mov_write_av1c_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1647
static int mov_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition movenc.c:9362
static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
Definition movenc.c:3167
static int get_cluster_duration(MOVTrack *track, int cluster_idx)
Definition movenc.c:1312
static int mov_write_enda_tag(AVIOContext *pb)
Definition movenc.c:711
static int64_t calc_samples_pts_duration(MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:3995
static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
Definition movenc.c:2307
static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track, int prefer_icc)
Definition movenc.c:2685
static int mov_write_mdta_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5043
static void mov_write_hfov_tag(AVFormatContext *s, AVIOContext *pb, const AVStereo3D *stereo3d)
Definition movenc.c:2458
static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVStream *st)
Definition movenc.c:4474
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition movenc.c:6590
static int mov_write_freeform_tag(AVIOContext *pb, const char *mean, const char *name, const char *data)
Definition movenc.c:4732
static int mov_write_SA3D_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1011
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition movenc.c:7759
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5065
static int mov_write_mdcv_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2753
static const AVCodecTag codec_mp4_tags[]
Definition movenc.c:9454
static int mov_get_apv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2115
static int mov_write_subtitle_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2320
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:5255
static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5591
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:1947
static int is_cover_image(const AVStream *st)
Definition movenc.c:185
static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int moof_size, int first, int end)
Definition movenc.c:5796
static int mov_write_hvce_tag(AVIOContext *pb, const AVPacketSideData *sd)
Definition movenc.c:2614
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
Definition movenc.c:293
static int mov_write_hmhd_tag(AVIOContext *pb)
Definition movenc.c:3918
#define FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS
Definition movenc.h:297
#define FF_MOV_FLAG_GLOBAL_SIDX
Definition movenc.h:292
int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, int track_index, int sample, uint8_t *sample_data, int sample_size)
Definition movenchint.c:405
#define FF_MOV_FLAG_FRAG_EVERY_FRAME
Definition movenc.h:298
#define FF_MOV_FLAG_DASH
Definition movenc.h:289
#define FF_MOV_FLAG_DEFAULT_BASE_MOOF
Definition movenc.h:288
#define FF_MOV_FLAG_WRITE_GAMA
Definition movenc.h:294
#define FF_MOV_FLAG_WRITE_COLR
Definition movenc.h:293
#define MOV_TRACK_CTTS
Definition movenc.h:114
#define FF_MOV_FLAG_DISABLE_CHPL
Definition movenc.h:287
#define MODE_3GP
Definition movenc.h:40
#define MODE_3G2
Definition movenc.h:43
#define FF_MOV_FLAG_FRAG_DISCONT
Definition movenc.h:290
#define MOV_TIMESCALE_Q
Definition movenc.h:34
#define MOV_FRAG_INFO_ALLOC_INCREMENT
Definition movenc.h:31
int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
Definition movenchint.c:30
#define MOV_DISPOSABLE_SAMPLE
Definition movenc.h:61
#define FF_MOV_FLAG_FASTSTART
Definition movenc.h:285
#define MOV_TRACK_STPS
Definition movenc.h:115
#define FF_MOV_FLAG_EMPTY_MOOV
Definition movenc.h:280
#define MODE_ISM
Definition movenc.h:45
#define FF_MOV_FLAG_SKIP_TRAILER
Definition movenc.h:296
#define MODE_MP4
Definition movenc.h:38
#define FF_MOV_FLAG_RTP_HINT
Definition movenc.h:278
#define FF_MOV_FLAG_PREFER_ICC
Definition movenc.h:301
@ MOV_ENC_CENC_AES_CTR
Definition movenc.h:201
@ MOV_ENC_NONE
Definition movenc.h:200
#define MODE_MOV
Definition movenc.h:39
void ff_mov_close_hinting(MOVTrack *track)
Definition movenchint.c:464
#define FF_MOV_FLAG_FRAG_KEYFRAME
Definition movenc.h:281
#define FF_MOV_FLAG_ISML
Definition movenc.h:284
#define MOV_TIMESCALE
Definition movenc.h:33
#define FF_MOV_FLAG_FRAG_CUSTOM
Definition movenc.h:283
#define FF_MOV_FLAG_USE_MDTA
Definition movenc.h:295
#define FF_MOV_FLAG_SKIP_SIDX
Definition movenc.h:299
#define FF_MOV_FLAG_DELAY_MOOV
Definition movenc.h:291
#define MODE_IPOD
Definition movenc.h:44
#define FF_MOV_FLAG_OMIT_TFHD_OFFSET
Definition movenc.h:286
#define FF_MOV_FLAG_FRAGMENT
Definition movenc.h:279
#define MODE_F4V
Definition movenc.h:46
#define FF_MOV_FLAG_HYBRID_FRAGMENTED
Definition movenc.h:302
@ MOV_PRFT_SRC_WALLCLOCK
Definition movenc.h:206
@ MOV_PRFT_NONE
Definition movenc.h:205
@ MOV_PRFT_SRC_PTS
Definition movenc.h:207
@ MOV_PRFT_NB
Definition movenc.h:208
#define FF_MOV_FLAG_SEPARATE_MOOF
Definition movenc.h:282
#define MOV_PARTIAL_SYNC_SAMPLE
Definition movenc.h:60
#define MOV_INDEX_CLUSTER_SIZE
Definition movenc.h:32
#define FF_MOV_FLAG_CMAF
Definition movenc.h:300
#define MODE_PSP
Definition movenc.h:41
#define MOV_TIMECODE_FLAG_DROPFRAME
Definition movenc.h:118
#define MOV_SYNC_SAMPLE
Definition movenc.h:59
#define MOV_TRACK_ENABLED
Definition movenc.h:116
#define MODE_AVIF
Definition movenc.h:47
int ff_mov_generate_squashed_ttml_packet(AVFormatContext *s, MOVTrack *track, AVPacket *pkt)
void ff_mov_cenc_flush(MOVMuxCencContext *ctx)
Clear subsample data.
Definition movenccenc.c:633
int ff_mov_cenc_av1_write_obus(AVFormatContext *s, MOVMuxCencContext *ctx, AVIOContext *pb, const AVPacket *pkt)
Definition movenccenc.c:387
int ff_mov_cenc_avc_parse_nal_units(MOVMuxCencContext *ctx, AVIOContext *pb, const uint8_t *buf_in, int size)
Parse AVC NAL units from annex B format, the nal size and type are written in the clear while the bod...
Definition movenccenc.c:198
int ff_mov_cenc_init(MOVMuxCencContext *ctx, uint8_t *encryption_key, int use_subsamples, enum AVCodecID codec_id, int bitexact)
Initialize a CENC context.
Definition movenccenc.c:600
void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext *ctx, AVIOContext *pb, int64_t moof_offset)
Write the cenc atoms that should reside inside stbl.
Definition movenccenc.c:542
int ff_mov_cenc_write_sinf_tag(MOVTrack *track, AVIOContext *pb, uint8_t *kid)
Write the sinf atom, contained inside stsd.
Definition movenccenc.c:568
void ff_mov_cenc_free(MOVMuxCencContext *ctx)
Free a CENC context.
Definition movenccenc.c:639
int ff_mov_cenc_avc_write_nal_units(AVFormatContext *s, MOVMuxCencContext *ctx, int nal_length_size, AVIOContext *pb, const uint8_t *buf_in, int size)
Write AVC NAL units that are in MP4 format, the nal size and type are written in the clear while the ...
Definition movenccenc.c:238
int ff_mov_cenc_write_packet(MOVMuxCencContext *ctx, AVIOContext *pb, const uint8_t *buf_in, int size)
Write a fully encrypted packet.
Definition movenccenc.c:173
#define CENC_KID_SIZE
Definition movenccenc.h:30
mpeg audio layer common tables.
const uint64_t ff_mpa_cicp_channel_layout_masks[20]
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
Definition mux.h:38
int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
Make shift_size amount of space at read_start by shifting data in the output at read_start until the ...
Definition mux_utils.c:71
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails.
Definition mux_utils.c:137
const char data[16]
Definition mxf.c:149
uint8_t interlaced
Definition mxfenc.c:2337
int profile
Definition mxfenc.c:2300
uint16_t cid
Definition mxfenc.c:2336
int ff_nal_parse_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition nal.c:113
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition nal.c:133
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
static float distance(float x, float y, int band)
#define OPUS_SEEK_PREROLL_MS
#define av_realloc(p, s)
Definition ops_static.c:54
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:548
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:758
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
Definition pixfmt.h:828
@ AVALPHA_MODE_PREMULTIPLIED
Alpha channel is multiplied into color values.
Definition pixfmt.h:827
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_UYVA
packed UYVA 4:4:4:4, 32bpp (1 Cr & Cb sample per 1x1 Y & A samples), UYVAUYVA...
Definition pixfmt.h:444
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition pixfmt.h:104
@ AV_PIX_FMT_V30XLE
packed VYUX 4:4:4 like XV30, 32bpp, (msb)10V 10Y 10U 2X(lsb), little-endian
Definition pixfmt.h:449
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:83
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_RGB555BE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition pixfmt.h:114
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
@ AV_PIX_FMT_RGB48BE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition pixfmt.h:109
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition pixfmt.h:113
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition pixfmt.h:115
@ AV_PIX_FMT_RGB565BE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition pixfmt.h:112
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition pixfmt.h:74
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_VYU444
packed VYU 4:4:4, 24bpp (1 Cr & Cb sample per 1x1 Y), VYUVYU...
Definition pixfmt.h:446
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:82
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:650
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:680
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:714
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
const char * name
Definition qsvenc.c:142
enum AVPixelFormat avpriv_pix_fmt_find(enum PixelFormatTagLists list, unsigned fourcc)
Definition raw.c:78
Raw Video Codec.
@ PIX_FMT_LIST_MOV
Definition raw.h:40
int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
Retrieves the palette from a packet, either from side data, or appended to the video data in the pack...
Definition rawutils.c:77
int ff_reshuffle_raw_rgb(AVFormatContext *s, AVPacket **ppkt, AVCodecParameters *par, int expected_stride)
Reshuffles the lines to use the user specified stride.
Definition rawutils.c:27
const AVCodecTag ff_codec_bmp_tags[]
Definition riff.c:36
const AVCodecTag ff_codec_wav_tags[]
Definition riff.c:526
internal header for RIFF based (de)muxers do NOT include this in end user applications
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition riff.h:53
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition riffenc.c:54
#define av_bswap16
Definition bswap.h:28
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition rtpenc.h:75
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition spdifenc.c:431
double strtod(const char *, char **)
Coded AC-3 header values up to the lfeon element, plus derived values.
uint8_t complexity_index_type_a
int substreamid
substream identification
int num_blocks
number of audio blocks
uint8_t monochrome
Definition av1.h:33
uint8_t chroma_subsampling_x
Definition av1.h:34
uint8_t chroma_subsampling_y
Definition av1.h:35
uint8_t bitdepth
Definition av1.h:32
Ambient viewing environment metadata as defined by H.274.
AVRational ambient_illuminance
Environmental illuminance of the ambient viewing environment in lux.
AVRational ambient_light_x
Normalized x chromaticity coordinate of the environmental ambient light in the nominal viewing enviro...
AVRational ambient_light_y
Normalized y chromaticity coordinate of the environmental ambient light in the nominal viewing enviro...
This structure describes the bitrate properties of an encoded bitstream.
Definition defs.h:298
int64_t avg_bitrate
Average bitrate of the stream, in bits per second.
Definition defs.h:313
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition defs.h:303
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition defs.h:319
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
enum AVColorSpace color_space
Definition codec_par.h:192
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int frame_size
Audio frame size, if known.
Definition codec_par.h:227
enum AVFieldOrder field_order
The order of the fields in interlaced video.
Definition codec_par.h:182
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition codec_par.h:135
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
int bits_per_raw_sample
The number of valid bits in each output sample.
Definition codec_par.h:130
AVRational sample_aspect_ratio
The aspect ratio (width/height) which a single pixel should have when displayed.
Definition codec_par.h:161
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
enum AVColorPrimaries color_primaries
Definition codec_par.h:190
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
enum AVColorTransferCharacteristic color_trc
Definition codec_par.h:191
enum AVAlphaMode alpha_mode
Video with alpha channel only.
Definition codec_par.h:261
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
enum AVColorRange color_range
Additional colorspace characteristics.
Definition codec_par.h:189
enum AVCodecID id
Definition internal.h:43
unsigned int tag
Definition internal.h:44
int depth
Number of bits in the component.
Definition pixdesc.h:57
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
unsigned MaxFALL
Max average light level per frame (cd/m^2).
unsigned MaxCLL
Max content light level (cd/m^2).
char * key
Definition dict.h:91
char * value
Definition dict.h:92
This describes info used to initialize an encryption key system.
Format I/O context.
Definition avformat.h:1401
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1553
int strict_std_compliance
Allow non-standard and experimental extension.
Definition avformat.h:1776
Bytestream IO Context.
Definition avio.h:160
Mastering display metadata capable of representing the color volume of the display used to master the...
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:431
uint8_t * data
Definition packet.h:432
This structure stores compressed data.
Definition packet.h:587
int stream_index
Definition packet.h:612
int size
Definition packet.h:611
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:628
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:609
uint8_t * data
Definition packet.h:610
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition pixdesc.h:71
This structure supplies correlation between a packet timestamp and a wall clock production time.
Definition defs.h:347
int64_t wallclock
A UTC timestamp, in microseconds, since Unix epoch (e.g, av_gettime()).
Definition defs.h:351
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
This structure describes how to handle spherical videos, outlining information about projection,...
Definition spherical.h:100
uint32_t bound_left
Distance from the left edge.
Definition spherical.h:185
enum AVSphericalProjection projection
Projection type.
Definition spherical.h:104
uint32_t bound_top
Distance from the top edge.
Definition spherical.h:186
uint32_t bound_bottom
Distance from the bottom edge.
Definition spherical.h:188
uint32_t bound_right
Distance from the right edge.
Definition spherical.h:187
int32_t pitch
Rotation around the right vector [-90, 90].
Definition spherical.h:145
int32_t roll
Rotation around the forward vector [-180, 180].
Definition spherical.h:146
int32_t yaw
Rotation around the up vector [-180, 180].
Definition spherical.h:144
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition spherical.h:200
Stereo 3D type: this structure describes how two videos are packed within a single video surface,...
Definition stereo3d.h:203
enum AVStereo3DType type
How views are packed within the video.
Definition stereo3d.h:207
uint32_t baseline
The distance between the centres of the lenses of the camera system, in micrometers.
Definition stereo3d.h:228
AVRational horizontal_disparity_adjustment
Relative shift of the left and right images, which changes the zero parallax plane.
Definition stereo3d.h:234
enum AVStereo3DPrimaryEye primary_eye
Which eye is the primary eye when rendering in 2D.
Definition stereo3d.h:222
int flags
Additional information about the frame packing.
Definition stereo3d.h:212
AVRational horizontal_field_of_view
Horizontal field of view, in degrees.
Definition stereo3d.h:239
enum AVStereo3DView view
Determines which views are packed.
Definition stereo3d.h:217
enum AVStreamGroupEntryType type
Definition avformat.h:1178
AVStream * stream
Definition avformat.h:1180
AVStreamGroupLayeredVideo is meant to define the relation between a base layer video stream and a sep...
Definition avformat.h:1104
int height
Height of the final image for presentation.
Definition avformat.h:1129
unsigned int el_index
Index of the enhancement layer stream in AVStreamGroup.
Definition avformat.h:1113
int width
Width of the final stream for presentation.
Definition avformat.h:1125
AVStreamGroupTREF is meant to define the relation between video, audio, or subtitle streams,...
Definition avformat.h:1148
unsigned int metadata_index
Index of the metadata stream in the AVStreamGroup.
Definition avformat.h:1154
unsigned int nb_entries
Number of elements in AVStreamGroup.entries.
Definition avformat.h:1286
enum AVStreamGroupParamsType type
Group type.
Definition avformat.h:1212
AVStreamGroupEntry ** entries
A list of streams or child groups in the group.
Definition avformat.h:1299
struct AVStreamGroupTREF * tref
Definition avformat.h:1229
union AVStreamGroup::@313244315204024013007202273061153315146016101155 params
Group type-specific parameters.
int64_t id
Group type-specific group ID.
Definition avformat.h:1204
struct AVStreamGroupLayeredVideo * layered_video
Definition avformat.h:1221
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:846
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:829
AVDictionary * metadata
Definition avformat.h:848
void * priv_data
Definition avformat.h:793
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:857
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
unsigned fps
frame per second; must be consistent with the rate field
Definition timecode.h:45
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition timecode.h:43
AVRational rate
frame rate in rational form
Definition timecode.h:44
int start
timecode frame start (first base frame number)
Definition timecode.h:42
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition internal.h:111
Definition isom.h:69
unsigned int count
Definition isom.h:70
int offset
Definition isom.h:71
int64_t offset
Definition movenc.h:81
int64_t tfrf_offset
Definition movenc.h:84
int64_t time
Definition movenc.h:82
int64_t duration
Definition movenc.h:83
uint32_t flags
Definition movenc.h:62
unsigned int chunkNum
Chunk number if the current entry is a chunk start otherwise 0.
Definition movenc.h:56
int64_t pts
Definition movenc.h:52
int cts
Definition movenc.h:58
unsigned int samples_in_chunk
Definition movenc.h:55
unsigned int stsd_index
Definition movenc.h:54
unsigned int entries
Definition movenc.h:57
AVProducerReferenceTime prft
Definition movenc.h:63
int64_t dts
Definition movenc.h:51
uint64_t pos
Definition movenc.h:50
unsigned int size
Definition movenc.h:53
struct AVAESCTR * aes_ctr
Definition movenccenc.h:41
int reserved_moov_size
0 for disabled, -1 for automatic, size otherwise
Definition movenc.h:241
char * major_brand
Definition movenc.h:244
int64_t mdat_pos
Definition movenc.h:219
AVFormatContext * fc
Definition movenc.h:247
AVPacket * pkt
Definition movenc.h:249
int nb_tracks
Definition movenc.h:216
int iods_skip
Definition movenc.h:226
int chapter_track
qt chapter track number
Definition movenc.h:218
uint64_t mdat_size
Definition movenc.h:220
char * encryption_scheme_str
Definition movenc.h:257
int movie_timescale
Definition movenc.h:270
float gamma
Definition movenc.h:252
int avif_loop_count
Definition movenc.h:275
uint8_t * encryption_key
Definition movenc.h:259
int nb_streams
Definition movenc.h:215
int fragments
Definition movenc.h:231
int encryption_key_len
Definition movenc.h:260
int write_btrt
Definition movenc.h:266
int avif_extent_length[2]
Definition movenc.h:273
int empty_hdlr_name
Definition movenc.h:269
int missing_duration_warned
Definition movenc.h:255
AVIOContext * mdat_buf
Definition movenc.h:236
uint8_t * encryption_kid
Definition movenc.h:261
int min_fragment_duration
Definition movenc.h:233
MOVPrftBox write_prft
Definition movenc.h:268
int64_t reserved_header_pos
Definition movenc.h:242
int per_stream_grouping
Definition movenc.h:246
int iods_video_profile
Definition movenc.h:227
MOVTrack * tracks
Definition movenc.h:221
int frag_interleave
Definition movenc.h:254
int max_fragment_duration
Definition movenc.h:232
int track_ids_ok
Definition movenc.h:265
int video_track_timescale
Definition movenc.h:239
int max_fragment_size
Definition movenc.h:234
int use_editlist
Definition movenc.h:251
int nb_meta_tmcd
number of new created tmcd track based on metadata (aka not data copy)
Definition movenc.h:217
int moov_written
Definition movenc.h:230
int use_stream_ids_as_track_ids
Definition movenc.h:264
MOVEncryptionScheme encryption_scheme
Definition movenc.h:258
int first_trun
Definition movenc.h:237
int64_t avif_extent_pos[2]
Definition movenc.h:272
int iods_audio_profile
Definition movenc.h:228
int is_animated_avif
Definition movenc.h:274
int write_tmcd
Definition movenc.h:267
int64_t time
Definition movenc.h:214
int encryption_kid_len
Definition movenc.h:262
int ism_lookahead
Definition movenc.h:235
Definition isom.h:64
unsigned int count
Definition isom.h:65
unsigned int duration
Definition isom.h:66
int track_id
Definition movenc.h:123
int mono_as_fc
Definition movenc.h:127
int entry
Definition movenc.h:97
int64_t start_cts
Definition movenc.h:138
AVPacket * cover_image
Definition movenc.h:157
int * src_track
the tracks that this hint (or tmcd) track describes
Definition movenc.h:146
uint32_t default_size
Definition movenc.h:154
struct MOVTrack::@041066134315003153207320140045232207171303033040 vc1_info
int nb_src_track
Definition movenc.h:145
int first_iamf_idx
Definition movenc.h:192
int first_packet_seen
Definition movenc.h:171
int64_t dts_shift
Definition movenc.h:142
int last_iamf_idx
Definition movenc.h:193
int audio_vbr
Definition movenc.h:133
int tag
stsd fourcc
Definition movenc.h:124
uint32_t flags
Definition movenc.h:117
uint32_t timecode_flags
Definition movenc.h:121
int * extradata_size
Definition movenc.h:107
int64_t data_offset
Definition movenc.h:160
int nb_tref_tags
Definition movenc.h:135
int language
Definition movenc.h:122
PacketList squashed_packet_queue
Definition movenc.h:189
AVCodecParameters * par
Definition movenc.h:126
unsigned frag_info_capacity
Definition movenc.h:166
int nb_frag_info
Definition movenc.h:164
int multichannel_as_mono
Definition movenc.h:128
AVIOContext * mdat_buf
Definition movenc.h:159
uint64_t time
Definition movenc.h:99
AVIOContext * iamf_buf
Definition movenc.h:194
int entry_version
Definition movenc.h:96
int packet_entry
Definition movenc.h:174
int is_unaligned_qt_rgb
Definition movenc.h:185
long sample_count
Definition movenc.h:109
uint32_t palette[AVPALETTE_COUNT]
Definition movenc.h:182
int has_disposable
Definition movenc.h:113
struct IAMFContext * iamf
Definition movenc.h:191
void * eac3_priv
Definition movenc.h:178
int64_t elst_end_pts
Definition movenc.h:140
MOVIentry * cluster_written
Definition movenc.h:131
int64_t end_pts
Definition movenc.h:139
uint8_t ** extradata
Definition movenc.h:106
unsigned int squash_fragment_samples_to_one
Definition movenc.h:187
int last_sample_is_subtitle_end
Definition movenc.h:101
MOVFragmentInfo * frag_info
Definition movenc.h:165
int entries_flushed
Definition movenc.h:162
int frag_discont
Definition movenc.h:161
uint32_t default_sample_flags
Definition movenc.h:153
int pal_done
Definition movenc.h:183
MovTag * tref_tags
Definition movenc.h:136
MOVIentry * cluster
Definition movenc.h:130
uint32_t max_packet_size
Definition movenc.h:150
int hint_track
the track that hints this track, -1 if no hint track is set
Definition movenc.h:144
struct APVDecoderConfigurationRecord * apv
Definition movenc.h:196
int64_t default_duration
Definition movenc.h:152
AVStream * st
Definition movenc.h:125
MOVMuxCencContext cenc
Definition movenc.h:180
int has_keyframes
Definition movenc.h:112
long sample_size
Definition movenc.h:110
int first_packet_entry
Definition movenc.h:170
int slices
Definition movenc.h:175
int entry_written
Definition movenc.h:97
int stsd_count
Definition movenc.h:104
int64_t start_dts
Definition movenc.h:137
int first_frag_written
Definition movenc.h:172
int mode
Definition movenc.h:95
unsigned timescale
Definition movenc.h:98
int packet_seq
Definition movenc.h:173
int first_packet_seq
Definition movenc.h:169
int64_t track_duration
Definition movenc.h:100
long chunkCount
Definition movenc.h:111
AVFormatContext * rtp_ctx
the format context for the hinting rtp muxer
Definition movenc.h:147
unsigned cluster_capacity
Definition movenc.h:132
int last_stsd_index
Definition movenc.h:105
int height
active picture (w/o VBI) height for D-10/IMX
Definition movenc.h:134
int end_reliable
Definition movenc.h:141
const char * value
Definition isom.h:512
uint32_t * id
trackID of the referenced track
Definition movenc.h:91
PacketListEntry * head
uint8_t num_ind_sub
Definition movenc.c:395
uint8_t fscod
Definition movenc.c:398
uint16_t data_rate
Definition movenc.c:392
int8_t ac3_bit_rate_code
Definition movenc.c:393
uint8_t complexity_index_type_a
Definition movenc.c:417
uint8_t num_blocks
Definition movenc.c:388
struct eac3_info::@032224065145233261074362213314253377146303165010 substream[1]
uint8_t acmod
Definition movenc.c:406
uint8_t bsid
Definition movenc.c:400
AVPacket * pkt
Definition movenc.c:385
uint8_t eof
Definition movenc.c:386
uint16_t chan_loc
Definition movenc.c:413
uint8_t ec3_done
Definition movenc.c:387
uint8_t num_dep_sub
Definition movenc.c:411
uint8_t lfeon
Definition movenc.c:408
uint8_t bsmod
Definition movenc.c:404
uint32_t avg_bit_rate
Average rate in bits/second over the entire presentation.
Definition movenc.c:750
uint32_t max_bit_rate
Maximum rate in bits/second over any window of one second.
Definition movenc.c:749
uint32_t buffer_size
Size of the decoding buffer for the elementary stream in bytes.
Definition movenc.c:748
Definition rpzaenc.c:60
uint8_t level
Definition svq3.c:208
#define lrint
Definition tablegen.h:53
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int num
Definition error.c:23
static int ref[MAX_W *MAX_W]
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition timecode.c:241
Timecode helpers header.
@ AV_TIMECODE_FLAG_DROPFRAME
timecode is drop frame
Definition timecode.h:36
static int64_t pts
int size
UUID parsing and serialization utilities.
#define AV_UUID_LEN
Definition uuid.h:57
uint8_t AVUUID[AV_UUID_LEN]
Definition uuid.h:60
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101
enum AVCodecID codec_id
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
Definition vc1_common.h:70
static av_always_inline const uint8_t * find_next_marker(const uint8_t *src, const uint8_t *end)
Find VC-1 marker in buffer.
Definition vc1_common.h:59
@ PROFILE_ADVANCED
Definition vc1_common.h:52
@ VC1_CODE_SEQHDR
Definition vc1_common.h:40
@ VC1_CODE_SLICE
Definition vc1_common.h:36
@ VC1_CODE_ENTRYPOINT
Definition vc1_common.h:39
const char * g
Definition vf_curves.c:128
static double cr(void *priv, double x, double y)
Definition vf_geq.c:248
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static float mean(const float *input, int size)
Definition vf_nnedi.c:792
static const Range full_range
static void copy(const float *p1, float *p2, const int length)
int len
int ff_isom_write_vpcc(void *logctx, AVIOContext *pb, const uint8_t *data, int len, const AVCodecParameters *par)
Writes VP codec configuration to the provided AVIOContext.
Definition vpcc.c:204
internal header for VPx codec configuration utilities.
static double c[64]
int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted H.266/VVC NAL units to a data buffer.
Definition vvc.c:864
int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes H.266/VVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext.
Definition vvc.c:885
int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count)
Writes Annex B formatted H.266/VVC NAL units to the provided AVIOContext.
Definition vvc.c:816