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
799{
800 switch (codec_id) {
801 case AV_CODEC_ID_AAC:
806 return 1;
807 default:
808 return 0;
809 }
810}
811
812static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
813{
814 struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track);
815 int64_t pos = avio_tell(pb);
816 int write_dsi = track->extradata_size[track->last_stsd_index] &&
818 int decoder_specific_info_len = write_dsi ?
819 5 + track->extradata_size[track->last_stsd_index] : 0;
820
821 avio_wb32(pb, 0); // size
822 ffio_wfourcc(pb, "esds");
823 avio_wb32(pb, 0); // Version
824
825 // ES descriptor
826 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
827 avio_wb16(pb, track->track_id);
828 avio_w8(pb, 0x00); // flags (= no flags)
829
830 // DecoderConfig descriptor
831 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
832
833 // Object type indication
834 if ((track->par->codec_id == AV_CODEC_ID_MP2 ||
835 track->par->codec_id == AV_CODEC_ID_MP3) &&
836 track->par->sample_rate > 24000)
837 avio_w8(pb, 0x6B); // 11172-3
838 else
840
841 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
842 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
844 avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
845 else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
846 avio_w8(pb, 0x15); // flags (= Audiostream)
847 else
848 avio_w8(pb, 0x11); // flags (= Visualstream)
849
850 avio_wb24(pb, bit_rates.buffer_size); // Buffersize DB
851 avio_wb32(pb, bit_rates.max_bit_rate); // maxbitrate
852 avio_wb32(pb, bit_rates.avg_bit_rate);
853
854 if (write_dsi) {
855 // DecoderSpecific info descriptor
856 put_descr(pb, 0x05, track->extradata_size[track->last_stsd_index]);
857 avio_write(pb, track->extradata[track->last_stsd_index],
858 track->extradata_size[track->last_stsd_index]);
859 }
860
861 // SL descriptor
862 put_descr(pb, 0x06, 1);
863 avio_w8(pb, 0x02);
864 return update_size(pb, pos);
865}
866
874
882
884{
885 int ret;
886 int64_t pos = avio_tell(pb);
887 avio_wb32(pb, 0);
888 avio_wl32(pb, track->tag); // store it byteswapped
889 track->par->codec_tag = av_bswap16(track->tag >> 16);
890 if ((ret = ff_put_wav_header(s, pb, track->par, 0)) < 0)
891 return ret;
892 return update_size(pb, pos);
893}
894
896{
897 int ret;
898 int64_t pos = avio_tell(pb);
899 avio_wb32(pb, 0);
900 ffio_wfourcc(pb, "wfex");
902 return ret;
903 return update_size(pb, pos);
904}
905
907{
908 int64_t pos = avio_tell(pb);
909 avio_wb32(pb, 0);
910 ffio_wfourcc(pb, "dfLa");
911 avio_w8(pb, 0); /* version */
912 avio_wb24(pb, 0); /* flags */
913
914 /* Expect the encoder to pass a METADATA_BLOCK_TYPE_STREAMINFO. */
916 return AVERROR_INVALIDDATA;
917
918 /* TODO: Write other METADATA_BLOCK_TYPEs if the encoder makes them available. */
919 avio_w8(pb, 1 << 7 | FLAC_METADATA_TYPE_STREAMINFO); /* LastMetadataBlockFlag << 7 | BlockType */
920 avio_wb24(pb, track->extradata_size[track->last_stsd_index]); /* Length */
921 avio_write(pb, track->extradata[track->last_stsd_index], track->extradata_size[track->last_stsd_index]); /* BlockData[Length] */
922
923 return update_size(pb, pos);
924}
925
927{
928 int64_t pos = avio_tell(pb);
930 avio_wb32(pb, 0);
931 ffio_wfourcc(pb, "dOps");
932 avio_w8(pb, 0); /* Version */
933 if (track->extradata_size[track->last_stsd_index] < 19) {
934 av_log(s, AV_LOG_ERROR, "invalid extradata size\n");
935 return AVERROR_INVALIDDATA;
936 }
937 /* extradata contains an Ogg OpusHead, other than byte-ordering and
938 OpusHead's preceding magic/version, OpusSpecificBox is currently
939 identical. */
940 channels = AV_RB8(track->extradata[track->last_stsd_index] + 9);
941 channel_map = AV_RB8(track->extradata[track->last_stsd_index] + 18);
942
943 avio_w8(pb, channels); /* OuputChannelCount */
944 avio_wb16(pb, AV_RL16(track->extradata[track->last_stsd_index] + 10)); /* PreSkip */
945 avio_wb32(pb, AV_RL32(track->extradata[track->last_stsd_index] + 12)); /* InputSampleRate */
946 avio_wb16(pb, AV_RL16(track->extradata[track->last_stsd_index] + 16)); /* OutputGain */
947 avio_w8(pb, channel_map); /* ChannelMappingFamily */
948 /* Write the rest of the header out without byte-swapping. */
949 if (channel_map) {
950 if (track->extradata_size[track->last_stsd_index] < 21 + channels) {
951 av_log(s, AV_LOG_ERROR, "invalid extradata size\n");
952 return AVERROR_INVALIDDATA;
953 }
954 avio_write(pb, track->extradata[track->last_stsd_index] + 19, 2 + channels); /* ChannelMappingTable */
955 }
956
957 return update_size(pb, pos);
958}
959
961{
962 int64_t pos = avio_tell(pb);
963 int length;
964 avio_wb32(pb, 0);
965 ffio_wfourcc(pb, "dmlp");
966
967 if (track->extradata_size[track->last_stsd_index] < 20) {
969 "Cannot write moov atom before TrueHD packets."
970 " Set the delay_moov flag to fix this.\n");
971 return AVERROR(EINVAL);
972 }
973
974 length = (AV_RB16(track->extradata[track->last_stsd_index]) & 0xFFF) * 2;
975 if (length < 20 || length > track->extradata_size[track->last_stsd_index])
976 return AVERROR_INVALIDDATA;
977
978 // Only TrueHD is supported
979 if (AV_RB32(track->extradata[track->last_stsd_index] + 4) != 0xF8726FBA)
980 return AVERROR_INVALIDDATA;
981
982 avio_wb32(pb, AV_RB32(track->extradata[track->last_stsd_index] + 8)); /* format_info */
983 avio_wb16(pb, AV_RB16(track->extradata[track->last_stsd_index] + 18) << 1); /* peak_data_rate */
984 avio_wb32(pb, 0); /* reserved */
985
986 return update_size(pb, pos);
987}
988
990{
991 int64_t pos = avio_tell(pb);
992 int layout = 0;
993
994 if (!track->extradata_size[track->last_stsd_index] ||
995 track->extradata_size[track->last_stsd_index] > UINT16_MAX) {
997 "Invalid extradata size %d for MPEGH-H stream.\n",
998 track->extradata_size[track->last_stsd_index]);
999 return AVERROR(EINVAL);
1000 }
1001
1002 avio_wb32(pb, 0);
1003 ffio_wfourcc(pb, "mhaC");
1004
1005 avio_w8(pb, 1); // ConfigurationVersion
1006 avio_w8(pb, track->par->profile * 5 + track->par->level);
1007
1008 for (size_t i = 0; i < FF_ARRAY_ELEMS(ff_mpa_cicp_channel_layout_masks); i++) {
1010 AVChannelLayout ch_layout;
1012 if (!av_channel_layout_compare(&track->par->ch_layout, &ch_layout)) {
1013 layout = i;
1014 break;
1015 }
1016 }
1017 }
1018
1019 avio_w8(pb, layout);
1020 avio_wb16(pb, track->extradata_size[track->last_stsd_index]);
1021 avio_write(pb, track->extradata[track->last_stsd_index],
1022 track->extradata_size[track->last_stsd_index]);
1023
1024 return update_size(pb, pos);
1025}
1026
1028{
1029 const AVDictionaryEntry *str = av_dict_get(track->st->metadata, "SA3D", NULL, 0);
1030 AVChannelLayout ch_layout = { 0 };
1031 int64_t pos;
1032 int ambisonic_order, ambi_channels, non_diegetic_channels;
1033 int i, ret;
1034
1035 if (!str)
1036 return 0;
1037
1038 ret = av_channel_layout_from_string(&ch_layout, str->value);
1039 if (ret < 0) {
1040 if (ret == AVERROR(EINVAL)) {
1041invalid:
1042 av_log(s, AV_LOG_ERROR, "Invalid SA3D layout: \"%s\"\n", str->value);
1043 ret = 0;
1044 }
1045 av_channel_layout_uninit(&ch_layout);
1046 return ret;
1047 }
1048
1049 if (track->st->codecpar->ch_layout.nb_channels != ch_layout.nb_channels)
1050 goto invalid;
1051
1052 ambisonic_order = av_channel_layout_ambisonic_order(&ch_layout);
1053 if (ambisonic_order < 0)
1054 goto invalid;
1055
1056 ambi_channels = (ambisonic_order + 1LL) * (ambisonic_order + 1LL);
1057 non_diegetic_channels = ch_layout.nb_channels - ambi_channels;
1058 if (non_diegetic_channels &&
1059 (non_diegetic_channels != 2 ||
1061 goto invalid;
1062
1063 av_log(s, AV_LOG_VERBOSE, "Inserting SA3D box with layout: \"%s\"\n", str->value);
1064
1065 pos = avio_tell(pb);
1066
1067 avio_wb32(pb, 0); // Size
1068 ffio_wfourcc(pb, "SA3D");
1069 avio_w8(pb, 0); // version
1070 avio_w8(pb, (!!non_diegetic_channels) << 7); // head_locked_stereo and ambisonic_type
1071 avio_wb32(pb, ambisonic_order); // ambisonic_order
1072 avio_w8(pb, 0); // ambisonic_channel_ordering
1073 avio_w8(pb, 0); // ambisonic_normalization
1074 avio_wb32(pb, ch_layout.nb_channels); // num_channels
1075 for (i = 0; i < ambi_channels; i++)
1077 for (; i < ch_layout.nb_channels; i++)
1078 avio_wb32(pb, av_channel_layout_channel_from_index(&ch_layout, i) + ambi_channels);
1079
1080 av_channel_layout_uninit(&ch_layout);
1081
1082 return update_size(pb, pos);
1083}
1084
1086{
1087 uint32_t layout_tag, bitmap, *channel_desc;
1088 int64_t pos = avio_tell(pb);
1089 int num_desc, ret;
1090
1091 if (track->multichannel_as_mono)
1092 return 0;
1093
1094 ret = ff_mov_get_channel_layout_tag(track->par, &layout_tag,
1095 &bitmap, &channel_desc);
1096
1097 if (ret < 0) {
1098 if (ret == AVERROR(ENOSYS)) {
1099 av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to "
1100 "lack of channel information\n");
1101 ret = 0;
1102 }
1103
1104 return ret;
1105 }
1106
1107 /* no predefined tag found + ch_layout in AV_CHANNEL_ORDER_NATIVE
1108 * but bitstream channels not actually in native order */
1109 if (layout_tag == MOV_CH_LAYOUT_UNKNOWN)
1110 return 0;
1111
1112 if (layout_tag == MOV_CH_LAYOUT_MONO && track->mono_as_fc > 0) {
1113 av_assert0(!channel_desc);
1114 channel_desc = av_malloc(sizeof(*channel_desc));
1115 if (!channel_desc)
1116 return AVERROR(ENOMEM);
1117
1118 layout_tag = 0;
1119 bitmap = 0;
1120 *channel_desc = 3; // channel label "Center"
1121 }
1122
1123 num_desc = layout_tag ? 0 : track->par->ch_layout.nb_channels;
1124
1125 avio_wb32(pb, 0); // Size
1126 ffio_wfourcc(pb, "chan"); // Type
1127 avio_w8(pb, 0); // Version
1128 avio_wb24(pb, 0); // Flags
1129 avio_wb32(pb, layout_tag); // mChannelLayoutTag
1130 avio_wb32(pb, bitmap); // mChannelBitmap
1131 avio_wb32(pb, num_desc); // mNumberChannelDescriptions
1132
1133 for (int i = 0; i < num_desc; i++) {
1134 avio_wb32(pb, channel_desc[i]); // mChannelLabel
1135 avio_wb32(pb, 0); // mChannelFlags
1136 avio_wl32(pb, 0); // mCoordinates[0]
1137 avio_wl32(pb, 0); // mCoordinates[1]
1138 avio_wl32(pb, 0); // mCoordinates[2]
1139 }
1140
1141 av_free(channel_desc);
1142
1143 return update_size(pb, pos);
1144}
1145
1147{
1148 int64_t pos = avio_tell(pb);
1149
1150 avio_wb32(pb, 0); /* size */
1151 ffio_wfourcc(pb, "wave");
1152
1153 if (track->par->codec_id != AV_CODEC_ID_QDM2) {
1154 avio_wb32(pb, 12); /* size */
1155 ffio_wfourcc(pb, "frma");
1156 avio_wl32(pb, track->tag);
1157 }
1158
1159 if (track->par->codec_id == AV_CODEC_ID_AAC) {
1160 /* useless atom needed by mplayer, ipod, not needed by quicktime */
1161 avio_wb32(pb, 12); /* size */
1162 ffio_wfourcc(pb, "mp4a");
1163 avio_wb32(pb, 0);
1164 mov_write_esds_tag(pb, track);
1165 } else if (mov_pcm_le_gt16(track->par->codec_id)) {
1167 } else if (mov_pcm_be_gt16(track->par->codec_id)) {
1169 } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
1170 mov_write_amr_tag(pb, track);
1171 } else if (track->par->codec_id == AV_CODEC_ID_AC3) {
1172 mov_write_ac3_tag(s, pb, track);
1173 } else if (track->par->codec_id == AV_CODEC_ID_EAC3) {
1174 mov_write_eac3_tag(s, pb, track);
1175 } else if (track->par->codec_id == AV_CODEC_ID_ALAC ||
1176 track->par->codec_id == AV_CODEC_ID_QDM2) {
1177 mov_write_extradata_tag(pb, track);
1178 } else if (track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1180 mov_write_ms_tag(s, pb, track);
1181 }
1182
1183 avio_wb32(pb, 8); /* size */
1184 avio_wb32(pb, 0); /* null tag */
1185
1186 return update_size(pb, pos);
1187}
1188
1189static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
1190{
1191 uint8_t *unescaped;
1192 const uint8_t *start, *next, *end = track->extradata[track->last_stsd_index] +
1193 track->extradata_size[track->last_stsd_index];
1194 int unescaped_size, seq_found = 0;
1195 int level = 0, interlace = 0;
1196 int packet_seq = track->vc1_info.packet_seq;
1197 int packet_entry = track->vc1_info.packet_entry;
1198 int slices = track->vc1_info.slices;
1199 PutBitContext pbc;
1200
1201 if (track->start_dts == AV_NOPTS_VALUE) {
1202 /* No packets written yet, vc1_info isn't authoritative yet. */
1203 /* Assume inline sequence and entry headers. */
1204 packet_seq = packet_entry = 1;
1206 "moov atom written before any packets, unable to write correct "
1207 "dvc1 atom. Set the delay_moov flag to fix this.\n");
1208 }
1209
1211 if (!unescaped)
1212 return AVERROR(ENOMEM);
1213 start = find_next_marker(track->extradata[track->last_stsd_index], end);
1214 for (next = start; next < end; start = next) {
1215 GetBitContext gb;
1216 int size;
1217 next = find_next_marker(start + 4, end);
1218 size = next - start - 4;
1219 if (size <= 0)
1220 continue;
1221 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
1222 init_get_bits(&gb, unescaped, 8 * unescaped_size);
1223 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
1224 int profile = get_bits(&gb, 2);
1225 if (profile != PROFILE_ADVANCED) {
1226 av_free(unescaped);
1227 return AVERROR(ENOSYS);
1228 }
1229 seq_found = 1;
1230 level = get_bits(&gb, 3);
1231 /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
1232 * width, height */
1233 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
1234 skip_bits(&gb, 1); /* broadcast */
1235 interlace = get_bits1(&gb);
1236 skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
1237 }
1238 }
1239 if (!seq_found) {
1240 av_free(unescaped);
1241 return AVERROR(ENOSYS);
1242 }
1243
1244 init_put_bits(&pbc, buf, 7);
1245 /* VC1DecSpecStruc */
1246 put_bits(&pbc, 4, 12); /* profile - advanced */
1247 put_bits(&pbc, 3, level);
1248 put_bits(&pbc, 1, 0); /* reserved */
1249 /* VC1AdvDecSpecStruc */
1250 put_bits(&pbc, 3, level);
1251 put_bits(&pbc, 1, 0); /* cbr */
1252 put_bits(&pbc, 6, 0); /* reserved */
1253 put_bits(&pbc, 1, !interlace); /* no interlace */
1254 put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
1255 put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
1256 put_bits(&pbc, 1, !slices); /* no slice code */
1257 put_bits(&pbc, 1, 0); /* no bframe */
1258 put_bits(&pbc, 1, 0); /* reserved */
1259
1260 /* framerate */
1261 if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
1262 put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
1263 else
1264 put_bits32(&pbc, 0xffffffff);
1265
1266 flush_put_bits(&pbc);
1267
1268 av_free(unescaped);
1269
1270 return 0;
1271}
1272
1274{
1275 uint8_t buf[7] = { 0 };
1276 int ret;
1277
1278 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
1279 return ret;
1280
1281 avio_wb32(pb, track->extradata_size[track->last_stsd_index] + 8 + sizeof(buf));
1282 ffio_wfourcc(pb, "dvc1");
1283 avio_write(pb, buf, sizeof(buf));
1284 avio_write(pb, track->extradata[track->last_stsd_index],
1285 track->extradata_size[track->last_stsd_index]);
1286
1287 return 0;
1288}
1289
1291{
1292 avio_wb32(pb, track->extradata_size[track->last_stsd_index] + 8);
1293 ffio_wfourcc(pb, "glbl");
1294 avio_write(pb, track->extradata[track->last_stsd_index],
1295 track->extradata_size[track->last_stsd_index]);
1296 return 8 + track->extradata_size[track->last_stsd_index];
1297}
1298
1299/**
1300 * Compute flags for 'lpcm' tag.
1301 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1302 */
1304{
1305 switch (codec_id) {
1308 return 11;
1311 return 9;
1312 case AV_CODEC_ID_PCM_U8:
1313 return 10;
1317 return 14;
1318 case AV_CODEC_ID_PCM_S8:
1322 return 12;
1323 default:
1324 return 0;
1325 }
1326}
1327
1328static int get_cluster_duration(MOVTrack *track, int cluster_idx)
1329{
1330 int64_t next_dts;
1331
1332 if (cluster_idx >= track->entry)
1333 return 0;
1334
1335 if (cluster_idx + 1 == track->entry)
1336 next_dts = track->track_duration + track->start_dts;
1337 else
1338 next_dts = track->cluster[cluster_idx + 1].dts;
1339
1340 next_dts -= track->cluster[cluster_idx].dts;
1341
1342 av_assert0(next_dts >= 0);
1343 av_assert0(next_dts <= INT_MAX);
1344
1345 return next_dts;
1346}
1347
1349{
1350 int i, first_duration;
1351
1352 /* use 1 for raw PCM */
1353 if (!track->audio_vbr)
1354 return 1;
1355
1356 /* check to see if duration is constant for all clusters */
1357 if (!track->entry)
1358 return 0;
1359 first_duration = get_cluster_duration(track, 0);
1360 for (i = 1; i < track->entry; i++) {
1361 if (get_cluster_duration(track, i) != first_duration)
1362 return 0;
1363 }
1364 return first_duration;
1365}
1366
1368{
1369 int64_t pos = avio_tell(pb);
1370 struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track);
1371 if (!bit_rates.max_bit_rate && !bit_rates.avg_bit_rate &&
1372 !bit_rates.buffer_size)
1373 // no useful data to be written, skip
1374 return 0;
1375
1376 avio_wb32(pb, 0); /* size */
1377 ffio_wfourcc(pb, "btrt");
1378
1379 avio_wb32(pb, bit_rates.buffer_size);
1380 avio_wb32(pb, bit_rates.max_bit_rate);
1381 avio_wb32(pb, bit_rates.avg_bit_rate);
1382
1383 return update_size(pb, pos);
1384}
1385
1387{
1388 int64_t pos = avio_tell(pb);
1389 int config = 0;
1390 int ret;
1391 uint8_t *speaker_pos = NULL;
1392 const AVChannelLayout *layout = &track->par->ch_layout;
1393
1395 if (ret || !config) {
1396 config = 0;
1397 speaker_pos = av_malloc(layout->nb_channels);
1398 if (!speaker_pos)
1399 return AVERROR(ENOMEM);
1401 speaker_pos, layout->nb_channels);
1402 if (ret) {
1403 char buf[128] = {0};
1404
1405 av_freep(&speaker_pos);
1406 av_channel_layout_describe(layout, buf, sizeof(buf));
1407 av_log(s, AV_LOG_ERROR, "unsupported channel layout %s\n", buf);
1408 return ret;
1409 }
1410 }
1411
1412 avio_wb32(pb, 0); /* size */
1413 ffio_wfourcc(pb, "chnl");
1414 avio_wb32(pb, 0); /* version & flags */
1415
1416 avio_w8(pb, 1); /* stream_structure */
1417 avio_w8(pb, config);
1418 if (config) {
1419 avio_wb64(pb, 0);
1420 } else {
1421 avio_write(pb, speaker_pos, layout->nb_channels);
1422 av_freep(&speaker_pos);
1423 }
1424
1425 return update_size(pb, pos);
1426}
1427
1429{
1430 int64_t pos = avio_tell(pb);
1431 int format_flags;
1432 int sample_size;
1433
1434 avio_wb32(pb, 0); /* size */
1435 ffio_wfourcc(pb, "pcmC");
1436 avio_wb32(pb, 0); /* version & flags */
1437
1438 /* 0x01: indicates little-endian format */
1439 format_flags = (track->par->codec_id == AV_CODEC_ID_PCM_F32LE ||
1440 track->par->codec_id == AV_CODEC_ID_PCM_F64LE ||
1441 track->par->codec_id == AV_CODEC_ID_PCM_S16LE ||
1442 track->par->codec_id == AV_CODEC_ID_PCM_S24LE ||
1444 avio_w8(pb, format_flags);
1445 sample_size = track->par->bits_per_raw_sample;
1446 if (!sample_size)
1447 sample_size = av_get_exact_bits_per_sample(track->par->codec_id);
1448 av_assert0(sample_size);
1449 avio_w8(pb, sample_size);
1450
1451 return update_size(pb, pos);
1452}
1453
1455{
1456 int64_t pos = avio_tell(pb);
1457 avio_wb32(pb, 0); /* size */
1458 ffio_wfourcc(pb, "srat");
1459 avio_wb32(pb, 0); /* version & flags */
1460
1461 avio_wb32(pb, track->par->sample_rate);
1462
1463 return update_size(pb, pos);
1464}
1465
1467{
1468 int64_t pos = avio_tell(pb);
1469 int version = 0;
1470 uint32_t tag = track->tag;
1471 int ret = 0;
1472
1473 if (track->mode == MODE_MOV) {
1474 if (track->par->sample_rate > UINT16_MAX || !track->par->ch_layout.nb_channels ||
1475 track->par->codec_id == AV_CODEC_ID_EAC3 ||
1476 track->par->ch_layout.nb_channels > 2) {
1477 if (mov_get_lpcm_flags(track->par->codec_id))
1478 tag = AV_RL32("lpcm");
1479 version = 2;
1480 } else if (track->audio_vbr || mov_pcm_le_gt16(track->par->codec_id) ||
1481 mov_pcm_be_gt16(track->par->codec_id) ||
1482 track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1484 track->par->codec_id == AV_CODEC_ID_QDM2) {
1485 version = 1;
1486 }
1487 } else if (track->mode == MODE_MP4) {
1488 if (track->par->sample_rate > UINT16_MAX &&
1490 version = 1;
1491 }
1492
1493 avio_wb32(pb, 0); /* size */
1494 if (mov->encryption_scheme != MOV_ENC_NONE) {
1495 ffio_wfourcc(pb, "enca");
1496 } else {
1497 avio_wl32(pb, tag); // store it byteswapped
1498 }
1499 avio_wb32(pb, 0); /* Reserved */
1500 avio_wb16(pb, 0); /* Reserved */
1501 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
1502
1503 /* SoundDescription */
1504 avio_wb16(pb, version); /* Version */
1505 avio_wb16(pb, 0); /* Revision level */
1506 avio_wb32(pb, 0); /* Reserved */
1507
1508 if (version == 2) {
1509 avio_wb16(pb, 3);
1510 avio_wb16(pb, 16);
1511 avio_wb16(pb, 0xfffe);
1512 avio_wb16(pb, 0);
1513 avio_wb32(pb, 0x00010000);
1514 avio_wb32(pb, 72);
1515 avio_wb64(pb, av_double2int(track->par->sample_rate));
1516 avio_wb32(pb, track->par->ch_layout.nb_channels);
1517 avio_wb32(pb, 0x7F000000);
1520 avio_wb32(pb, track->sample_size);
1522 } else {
1523 unsigned sample_rate = track->par->sample_rate;
1524
1525 if (track->mode == MODE_MOV) {
1526 avio_wb16(pb, track->par->ch_layout.nb_channels);
1527 if (track->par->codec_id == AV_CODEC_ID_PCM_U8 ||
1528 track->par->codec_id == AV_CODEC_ID_PCM_S8)
1529 avio_wb16(pb, 8); /* bits per sample */
1530 else if (track->par->codec_id == AV_CODEC_ID_ADPCM_G726)
1531 avio_wb16(pb, track->par->bits_per_coded_sample);
1532 else
1533 avio_wb16(pb, 16);
1534 avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
1535 } else { /* reserved for mp4/3gp */
1536 avio_wb16(pb, track->tag == MKTAG('i', 'a', 'm', 'f') ?
1537 0 : track->par->ch_layout.nb_channels);
1538 if (track->par->codec_id == AV_CODEC_ID_FLAC ||
1539 track->par->codec_id == AV_CODEC_ID_ALAC) {
1540 avio_wb16(pb, track->par->bits_per_raw_sample);
1541 } else {
1542 avio_wb16(pb, 16);
1543 }
1544 avio_wb16(pb, 0);
1545
1546 while (sample_rate > UINT16_MAX)
1547 sample_rate >>= 1;
1548 }
1549
1550 avio_wb16(pb, 0); /* packet size (= 0) */
1551 if (track->tag == MKTAG('i','a','m','f'))
1552 avio_wb16(pb, 0); /* samplerate must be 0 for IAMF */
1553 else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1554 avio_wb16(pb, 48000);
1555 else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1556 avio_wb32(pb, track->par->sample_rate);
1557 else
1558 avio_wb16(pb, sample_rate);
1559
1560 if (track->par->codec_id != AV_CODEC_ID_TRUEHD)
1561 avio_wb16(pb, 0); /* Reserved */
1562 }
1563
1564 if (track->mode == MODE_MOV && version == 1) { /* SoundDescription V1 extended info */
1565 if (mov_pcm_le_gt16(track->par->codec_id) ||
1566 mov_pcm_be_gt16(track->par->codec_id))
1567 avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
1568 else
1569 avio_wb32(pb, track->par->frame_size); /* Samples per packet */
1570 avio_wb32(pb, track->sample_size / track->par->ch_layout.nb_channels); /* Bytes per packet */
1571 avio_wb32(pb, track->sample_size); /* Bytes per frame */
1572 avio_wb32(pb, 2); /* Bytes per sample */
1573 }
1574
1575 if (track->mode == MODE_MOV &&
1576 (track->par->codec_id == AV_CODEC_ID_AAC ||
1577 track->par->codec_id == AV_CODEC_ID_AC3 ||
1578 track->par->codec_id == AV_CODEC_ID_EAC3 ||
1579 track->par->codec_id == AV_CODEC_ID_AMR_NB ||
1580 track->par->codec_id == AV_CODEC_ID_ALAC ||
1581 track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1583 track->par->codec_id == AV_CODEC_ID_QDM2 ||
1584 (mov_pcm_le_gt16(track->par->codec_id) && version==1) ||
1585 (mov_pcm_be_gt16(track->par->codec_id) && version==1)))
1586 ret = mov_write_wave_tag(s, pb, track);
1587 else if (track->tag == MKTAG('m','p','4','a'))
1588 ret = mov_write_esds_tag(pb, track);
1589#if CONFIG_IAMFENC
1590 else if (track->tag == MKTAG('i','a','m','f'))
1591 ret = mov_write_iacb_tag(mov->fc, pb, track);
1592#endif
1593 else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
1594 ret = mov_write_amr_tag(pb, track);
1595 else if (track->par->codec_id == AV_CODEC_ID_AC3)
1596 ret = mov_write_ac3_tag(s, pb, track);
1597 else if (track->par->codec_id == AV_CODEC_ID_EAC3)
1598 ret = mov_write_eac3_tag(s, pb, track);
1599 else if (track->par->codec_id == AV_CODEC_ID_ALAC)
1600 ret = mov_write_extradata_tag(pb, track);
1601 else if (track->par->codec_id == AV_CODEC_ID_WMAPRO)
1602 ret = mov_write_wfex_tag(s, pb, track);
1603 else if (track->par->codec_id == AV_CODEC_ID_FLAC)
1604 ret = mov_write_dfla_tag(pb, track);
1605 else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1606 ret = mov_write_dops_tag(s, pb, track);
1607 else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1608 ret = mov_write_dmlp_tag(s, pb, track);
1609 else if (track->par->codec_id == AV_CODEC_ID_MPEGH_3D_AUDIO)
1610 ret = mov_write_mhac_tag(s, pb, track);
1611 else if (tag == MOV_MP4_IPCM_TAG || tag == MOV_MP4_FPCM_TAG) {
1612 if (track->par->sample_rate > UINT16_MAX)
1613 mov_write_srat_tag(pb, track);
1614 if (track->par->ch_layout.nb_channels > 1)
1615 ret = mov_write_chnl_tag(s, pb, track);
1616 if (ret < 0)
1617 return ret;
1618 ret = mov_write_pcmc_tag(s, pb, track);
1619 } else if (track->extradata_size[track->last_stsd_index] > 0)
1620 ret = mov_write_glbl_tag(pb, track);
1621
1622 if (ret < 0)
1623 return ret;
1624
1625 if (track->mode == MODE_MP4 && track->par->codec_type == AVMEDIA_TYPE_AUDIO
1626 && ((ret = mov_write_SA3D_tag(s, pb, track)) < 0)) {
1627 return ret;
1628 }
1629
1630 if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO
1631 && ((ret = mov_write_chan_tag(s, pb, track)) < 0)) {
1632 return ret;
1633 }
1634
1636 && ((ret = ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid)) < 0)) {
1637 return ret;
1638 }
1639
1640 if (mov->write_btrt &&
1641 ((ret = mov_write_btrt_tag(pb, track)) < 0))
1642 return ret;
1643
1644 if (track->mode == MODE_MP4)
1645 track->entry_version = version;
1646
1647 ret = update_size(pb, pos);
1648 return ret;
1649}
1650
1652{
1653 avio_wb32(pb, 0xf); /* size */
1654 ffio_wfourcc(pb, "d263");
1655 ffio_wfourcc(pb, "FFMP");
1656 avio_w8(pb, 0); /* decoder version */
1657 /* FIXME use AVCodecContext level/profile, when encoder will set values */
1658 avio_w8(pb, 0xa); /* level */
1659 avio_w8(pb, 0); /* profile */
1660 return 0xf;
1661}
1662
1664{
1665 int64_t pos = avio_tell(pb);
1666
1667 avio_wb32(pb, 0);
1668 ffio_wfourcc(pb, "av1C");
1669 ff_isom_write_av1c(pb, track->extradata[track->last_stsd_index],
1670 track->extradata_size[track->last_stsd_index], track->mode != MODE_AVIF);
1671 return update_size(pb, pos);
1672}
1673
1675{
1676 int64_t pos = avio_tell(pb);
1677
1678 avio_wb32(pb, 0);
1679 ffio_wfourcc(pb, "avcC");
1680 ff_isom_write_avcc(pb, track->extradata[track->last_stsd_index],
1681 track->extradata_size[track->last_stsd_index]);
1682 return update_size(pb, pos);
1683}
1684
1685/* AVS3 Intelligent Media Coding
1686 * Information Technology - Intelligent Media Coding
1687 * Part 6: Intelligent Media Format
1688 */
1689static int mov_write_av3c(AVIOContext *pb, const uint8_t *data, int len)
1690{
1691 if (len < 4)
1692 return AVERROR_INVALIDDATA;
1693
1694 if (data[0] == 1) {
1695 // In Avs3DecoderConfigurationRecord format
1696 avio_write(pb, data, len);
1697 return 0;
1698 }
1699
1700 avio_w8(pb, 1); // version
1701 avio_wb16(pb, len); // sequence_header_length
1702 avio_write(pb, data, len); // sequence_header
1703 avio_w8(pb, 0xFC); // Only support library_dependency_idc = 0
1704
1705 return 0;
1706}
1707
1709{
1710 int64_t pos = avio_tell(pb);
1711 avio_wb32(pb, 0);
1712 ffio_wfourcc(pb, "av3c");
1713 mov_write_av3c(pb, track->extradata[track->last_stsd_index],
1714 track->extradata_size[track->last_stsd_index]);
1715 return update_size(pb, pos);
1716}
1717
1719{
1720 int64_t pos = avio_tell(pb);
1721
1722 avio_wb32(pb, 0);
1723 ffio_wfourcc(pb, "vpcC");
1724 ff_isom_write_vpcc(s, pb, track->extradata[track->last_stsd_index],
1725 track->extradata_size[track->last_stsd_index], track->par);
1726 return update_size(pb, pos);
1727}
1728
1730{
1731 int64_t pos = avio_tell(pb);
1732
1733 avio_wb32(pb, 0);
1734 ffio_wfourcc(pb, "hvcC");
1735 if (track->tag == MKTAG('h','v','c','1'))
1736 ff_isom_write_hvcc(pb, track->extradata[track->last_stsd_index],
1737 track->extradata_size[track->last_stsd_index], 1, s);
1738 else
1739 ff_isom_write_hvcc(pb, track->extradata[track->last_stsd_index],
1740 track->extradata_size[track->last_stsd_index], 0, s);
1741 return update_size(pb, pos);
1742}
1743
1745{
1746 int64_t pos = avio_tell(pb);
1747 int ret;
1748
1749 avio_wb32(pb, 0);
1750 ffio_wfourcc(pb, "lhvC");
1751 if (track->tag == MKTAG('h','v','c','1'))
1752 ret = ff_isom_write_lhvc(pb, track->extradata[track->last_stsd_index],
1753 track->extradata_size[track->last_stsd_index], 1, s);
1754 else
1755 ret = ff_isom_write_lhvc(pb, track->extradata[track->last_stsd_index],
1756 track->extradata_size[track->last_stsd_index], 0, s);
1757
1758 if (ret < 0) {
1759 avio_seek(pb, pos, SEEK_SET);
1760 return ret;
1761 }
1762
1763 return update_size(pb, pos);
1764}
1765
1767{
1768 int64_t pos = avio_tell(pb);
1769
1770 avio_wb32(pb, 0);
1771 ffio_wfourcc(pb, "evcC");
1772
1773 if (track->tag == MKTAG('e','v','c','1'))
1774 ff_isom_write_evcc(pb, track->extradata[track->last_stsd_index],
1775 track->extradata_size[track->last_stsd_index], 1);
1776 else
1777 ff_isom_write_evcc(pb, track->extradata[track->last_stsd_index],
1778 track->extradata_size[track->last_stsd_index], 0);
1779
1780 return update_size(pb, pos);
1781}
1782
1784{
1785 int64_t pos = avio_tell(pb);
1786
1787 avio_wb32(pb, 0);
1788 ffio_wfourcc(pb, "lvcC");
1789
1790 ff_isom_write_lvcc(pb, track->extradata[track->last_stsd_index],
1791 track->extradata_size[track->last_stsd_index], s);
1792
1793 return update_size(pb, pos);
1794}
1795
1797{
1798 int64_t pos = avio_tell(pb);
1799
1800 avio_wb32(pb, 0);
1801 ffio_wfourcc(pb, "vvcC");
1802
1803 avio_w8 (pb, 0); /* version */
1804 avio_wb24(pb, 0); /* flags */
1805
1806 if (track->tag == MKTAG('v','v','c','1'))
1807 ff_isom_write_vvcc(pb, track->extradata[track->last_stsd_index],
1808 track->extradata_size[track->last_stsd_index], 1);
1809 else
1810 ff_isom_write_vvcc(pb, track->extradata[track->last_stsd_index],
1811 track->extradata_size[track->last_stsd_index], 0);
1812 return update_size(pb, pos);
1813}
1814
1816{
1817 int64_t pos = avio_tell(pb);
1818
1819 avio_wb32(pb, 0);
1820 ffio_wfourcc(pb, "apvC");
1821
1822 avio_w8 (pb, 0); /* version */
1823 avio_wb24(pb, 0); /* flags */
1824
1825 ff_isom_write_apvc(pb, track->apv, s);
1826
1827 return update_size(pb, pos);
1828}
1829
1830/* also used by all avid codecs (dv, imx, meridien) and their variants */
1831/* https://community.avid.com/forums/t/136517.aspx */
1833{
1834 int interlaced;
1835 int cid;
1836 int display_width = track->par->width;
1837 const uint8_t *extradata;
1838
1839 if (track->extradata[track->last_stsd_index] && track->extradata_size[track->last_stsd_index] > 0x29) {
1840 if (ff_dnxhd_parse_header_prefix(track->extradata[track->last_stsd_index]) != 0) {
1841 /* looks like a DNxHD bit stream */
1842 extradata = track->extradata[track->last_stsd_index];
1843 } else {
1844 av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1845 return 0;
1846 }
1847 } else {
1848 av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1849 return 0;
1850 }
1851
1852 cid = AV_RB32(extradata + 0x28);
1853
1854 avio_wb32(pb, 24); /* size */
1855 ffio_wfourcc(pb, "ACLR");
1856 ffio_wfourcc(pb, "ACLR");
1857 ffio_wfourcc(pb, "0001");
1858 // 1: CCIR (supercolors will be dropped, 16 will be displayed as black)
1859 // 2: FullRange (0 will be displayed as black, 16 will be displayed as dark grey)
1860 if (track->par->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1862 avio_wb32(pb, 1);
1863 } else {
1864 avio_wb32(pb, 2);
1865 }
1866 avio_wb32(pb, 0); /* reserved */
1867
1868 if (track->tag == MKTAG('A','V','d','h')) {
1869 int alp = extradata[0x07] & 1;
1870 int pma = (extradata[0x07] >> 2) & 1;
1871 int sbd = (extradata[0x21] >> 5) & 3;
1872 int ssc = (extradata[0x2C] >> 5) & 3;
1873 int clv = (extradata[0x2C] >> 1) & 3;
1874 int clf = extradata[0x2C] & 1;
1875
1876 avio_wb32(pb, 32);
1877 ffio_wfourcc(pb, "ADHR");
1878 ffio_wfourcc(pb, "0001");
1879 avio_wb32(pb, cid); // Compression ID
1880 // 0: 4:2:2 Sub Sampling
1881 // 1: 4:2:0 Sub Sampling
1882 // 2: 4:4:4 Sub Sampling
1883 avio_wb32(pb, ssc); // Sub Sampling Control
1884 // 1: 8-bits per sample
1885 // 2: 10-bits per sample
1886 // 3: 12-bits per sample
1887 avio_wb32(pb, sbd); // Sample Bit Depth
1888 // 0: Bitstream is encoded using the YCBCR format rules and tables
1889 // 1: Bitstream is encoded using the RGB format rules and tables – only Compression IDs 1256, 1270
1890 avio_wb16(pb, clf); // Color Format
1891 // 0: ITU-R BT.709
1892 // 1: ITU-R BT.2020
1893 // 2: ITU-R BT.2020 C
1894 // 3: Out-of-band
1895 avio_wb16(pb, clv); // Color Volume
1896 // 0: Alpha channel not present
1897 // 1: Alpha channel present
1898 avio_wb16(pb, alp); // Alpha Present
1899 // 0: Alpha has not been applied to video channels
1900 // 1: Alpha has been applied to the video channels prior to encoding
1901 avio_wb16(pb, pma); // Pre-Multiplied Alpha
1902 return 0;
1903 }
1904
1905 interlaced = extradata[5] & 2;
1906
1907 avio_wb32(pb, 24); /* size */
1908 ffio_wfourcc(pb, "APRG");
1909 ffio_wfourcc(pb, "APRG");
1910 ffio_wfourcc(pb, "0001");
1911 // 1 for progressive or 2 for interlaced
1912 if (interlaced)
1913 avio_wb32(pb, 2);
1914 else
1915 avio_wb32(pb, 1);
1916 avio_wb32(pb, 0); /* reserved */
1917
1918 avio_wb32(pb, 120); /* size */
1919 ffio_wfourcc(pb, "ARES");
1920 ffio_wfourcc(pb, "ARES");
1921 ffio_wfourcc(pb, "0001");
1922 avio_wb32(pb, cid); /* cid */
1923 if ( track->par->sample_aspect_ratio.num > 0
1924 && track->par->sample_aspect_ratio.den > 0)
1925 display_width = display_width * track->par->sample_aspect_ratio.num / track->par->sample_aspect_ratio.den;
1926 avio_wb32(pb, display_width); // field width
1927 if (interlaced) {
1928 avio_wb32(pb, track->par->height / 2); // field height
1929 avio_wb32(pb, 2); // num fields
1930 avio_wb32(pb, 0); // num black lines (must be 0)
1931 // 4: HD1080i
1932 // 5: HD1080P
1933 // 6: HD720P
1934 avio_wb32(pb, 4); // video format
1935 } else {
1936 avio_wb32(pb, track->par->height);
1937 avio_wb32(pb, 1); // num fields
1938 avio_wb32(pb, 0);
1939 if (track->par->height == 1080)
1940 avio_wb32(pb, 5);
1941 else
1942 avio_wb32(pb, 6);
1943 }
1944 /* padding */
1945 ffio_fill(pb, 0, 10 * 8);
1946
1947 return 0;
1948}
1949
1951{
1952 avio_wb32(pb, 12);
1953 ffio_wfourcc(pb, "DpxE");
1954 if (track->extradata_size[track->last_stsd_index] >= 12 &&
1955 !memcmp(&track->extradata[track->last_stsd_index][4], "DpxE", 4)) {
1956 avio_wb32(pb, track->extradata[track->last_stsd_index][11]);
1957 } else {
1958 avio_wb32(pb, 1);
1959 }
1960 return 0;
1961}
1962
1964{
1965 int tag;
1966
1967 if (track->par->width == 720) { /* SD */
1968 if (track->par->height == 480) { /* NTSC */
1969 if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1970 else tag = MKTAG('d','v','c',' ');
1971 }else if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1972 else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1973 else tag = MKTAG('d','v','p','p');
1974 } else if (track->par->height == 720) { /* HD 720 line */
1975 if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
1976 else tag = MKTAG('d','v','h','p');
1977 } else if (track->par->height == 1080) { /* HD 1080 line */
1978 if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
1979 else tag = MKTAG('d','v','h','6');
1980 } else {
1981 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1982 return 0;
1983 }
1984
1985 return tag;
1986}
1987
1989{
1990 AVRational rational_framerate = st->avg_frame_rate;
1991 int rate = 0;
1992 if (rational_framerate.den != 0)
1993 rate = av_q2d(rational_framerate);
1994 return rate;
1995}
1996
1998{
1999 int tag = track->par->codec_tag;
2001 AVStream *st = track->st;
2002 int rate = defined_frame_rate(s, st);
2003
2004 if (!tag)
2005 tag = MKTAG('m', '2', 'v', '1'); //fallback tag
2006
2007 if (track->par->format == AV_PIX_FMT_YUV420P) {
2008 if (track->par->width == 1280 && track->par->height == 720) {
2009 if (!interlaced) {
2010 if (rate == 24) tag = MKTAG('x','d','v','4');
2011 else if (rate == 25) tag = MKTAG('x','d','v','5');
2012 else if (rate == 30) tag = MKTAG('x','d','v','1');
2013 else if (rate == 50) tag = MKTAG('x','d','v','a');
2014 else if (rate == 60) tag = MKTAG('x','d','v','9');
2015 }
2016 } else if (track->par->width == 1440 && track->par->height == 1080) {
2017 if (!interlaced) {
2018 if (rate == 24) tag = MKTAG('x','d','v','6');
2019 else if (rate == 25) tag = MKTAG('x','d','v','7');
2020 else if (rate == 30) tag = MKTAG('x','d','v','8');
2021 } else {
2022 if (rate == 25) tag = MKTAG('x','d','v','3');
2023 else if (rate == 30) tag = MKTAG('x','d','v','2');
2024 }
2025 } else if (track->par->width == 1920 && track->par->height == 1080) {
2026 if (!interlaced) {
2027 if (rate == 24) tag = MKTAG('x','d','v','d');
2028 else if (rate == 25) tag = MKTAG('x','d','v','e');
2029 else if (rate == 30) tag = MKTAG('x','d','v','f');
2030 } else {
2031 if (rate == 25) tag = MKTAG('x','d','v','c');
2032 else if (rate == 30) tag = MKTAG('x','d','v','b');
2033 }
2034 }
2035 } else if (track->par->format == AV_PIX_FMT_YUV422P) {
2036 if (track->par->width == 1280 && track->par->height == 720) {
2037 if (!interlaced) {
2038 if (rate == 24) tag = MKTAG('x','d','5','4');
2039 else if (rate == 25) tag = MKTAG('x','d','5','5');
2040 else if (rate == 30) tag = MKTAG('x','d','5','1');
2041 else if (rate == 50) tag = MKTAG('x','d','5','a');
2042 else if (rate == 60) tag = MKTAG('x','d','5','9');
2043 }
2044 } else if (track->par->width == 1920 && track->par->height == 1080) {
2045 if (!interlaced) {
2046 if (rate == 24) tag = MKTAG('x','d','5','d');
2047 else if (rate == 25) tag = MKTAG('x','d','5','e');
2048 else if (rate == 30) tag = MKTAG('x','d','5','f');
2049 } else {
2050 if (rate == 25) tag = MKTAG('x','d','5','c');
2051 else if (rate == 30) tag = MKTAG('x','d','5','b');
2052 }
2053 }
2054 }
2055
2056 return tag;
2057}
2058
2060{
2061 int tag = track->par->codec_tag;
2063 AVStream *st = track->st;
2064 int rate = defined_frame_rate(s, st);
2065
2066 if (!tag)
2067 tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
2068
2069 if (track->par->profile == AV_PROFILE_UNKNOWN ||
2070 !(track->par->profile & AV_PROFILE_H264_INTRA))
2071 return tag;
2072
2073 if (track->par->format == AV_PIX_FMT_YUV420P10) {
2074 if (track->par->width == 960 && track->par->height == 720) {
2075 if (!interlaced) {
2076 if (rate == 24) tag = MKTAG('a','i','5','p');
2077 else if (rate == 25) tag = MKTAG('a','i','5','q');
2078 else if (rate == 30) tag = MKTAG('a','i','5','p');
2079 else if (rate == 50) tag = MKTAG('a','i','5','q');
2080 else if (rate == 60) tag = MKTAG('a','i','5','p');
2081 }
2082 } else if (track->par->width == 1440 && track->par->height == 1080) {
2083 if (!interlaced) {
2084 if (rate == 24) tag = MKTAG('a','i','5','3');
2085 else if (rate == 25) tag = MKTAG('a','i','5','2');
2086 else if (rate == 30) tag = MKTAG('a','i','5','3');
2087 } else {
2088 if (rate == 50) tag = MKTAG('a','i','5','5');
2089 else if (rate == 60) tag = MKTAG('a','i','5','6');
2090 }
2091 }
2092 } else if (track->par->format == AV_PIX_FMT_YUV422P10) {
2093 if (track->par->width == 1280 && track->par->height == 720) {
2094 if (!interlaced) {
2095 if (rate == 24) tag = MKTAG('a','i','1','p');
2096 else if (rate == 25) tag = MKTAG('a','i','1','q');
2097 else if (rate == 30) tag = MKTAG('a','i','1','p');
2098 else if (rate == 50) tag = MKTAG('a','i','1','q');
2099 else if (rate == 60) tag = MKTAG('a','i','1','p');
2100 }
2101 } else if (track->par->width == 1920 && track->par->height == 1080) {
2102 if (!interlaced) {
2103 if (rate == 24) tag = MKTAG('a','i','1','3');
2104 else if (rate == 25) tag = MKTAG('a','i','1','2');
2105 else if (rate == 30) tag = MKTAG('a','i','1','3');
2106 } else {
2107 if (rate == 25) tag = MKTAG('a','i','1','5');
2108 else if (rate == 50) tag = MKTAG('a','i','1','5');
2109 else if (rate == 60) tag = MKTAG('a','i','1','6');
2110 }
2111 } else if ( track->par->width == 4096 && track->par->height == 2160
2112 || track->par->width == 3840 && track->par->height == 2160
2113 || track->par->width == 2048 && track->par->height == 1080) {
2114 tag = MKTAG('a','i','v','x');
2115 }
2116 }
2117
2118 return tag;
2119}
2120
2122{
2123 int tag = track->par->codec_tag;
2124
2125 if (!tag)
2126 tag = MKTAG('e', 'v', 'c', '1');
2127
2128 return tag;
2129}
2130
2132{
2133 int tag = track->par->codec_tag;
2134
2135 if (!tag)
2136 tag = MKTAG('a', 'p', 'v', '1');
2137
2138 return tag;
2139}
2140
2141
2142static const struct {
2144 uint32_t tag;
2145 unsigned bps;
2146} mov_pix_fmt_tags[] = {
2147 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
2148 { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
2149 { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
2150 { AV_PIX_FMT_VYU444, MKTAG('v','3','0','8'), 0 },
2151 { AV_PIX_FMT_UYVA, MKTAG('v','4','0','8'), 0 },
2152 { AV_PIX_FMT_V30XLE, MKTAG('v','4','1','0'), 0 },
2153 { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
2154 { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
2155 { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
2156 { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
2157 { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
2158 { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
2159 { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
2160 { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
2161 { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
2162 { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
2163 { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
2164 { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
2166
2168{
2169 int tag = MKTAG('A','V','d','n');
2170 if (track->par->profile != AV_PROFILE_UNKNOWN &&
2171 track->par->profile != AV_PROFILE_DNXHD)
2172 tag = MKTAG('A','V','d','h');
2173 return tag;
2174}
2175
2177{
2178 int tag = track->par->codec_tag;
2179 int i;
2181
2182 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
2183 if (track->par->format == mov_pix_fmt_tags[i].pix_fmt) {
2184 tag = mov_pix_fmt_tags[i].tag;
2186 if (track->par->codec_tag == mov_pix_fmt_tags[i].tag)
2187 break;
2188 }
2189 }
2190
2192 track->par->bits_per_coded_sample);
2193 if (tag == MKTAG('r','a','w',' ') &&
2194 track->par->format != pix_fmt &&
2195 track->par->format != AV_PIX_FMT_GRAY8 &&
2196 track->par->format != AV_PIX_FMT_NONE)
2197 av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
2198 av_get_pix_fmt_name(track->par->format));
2199 return tag;
2200}
2201
2202static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
2203{
2204 unsigned int tag = track->par->codec_tag;
2205
2206 // "rtp " is used to distinguish internally created RTP-hint tracks
2207 // (with rtp_ctx) from other tracks.
2208 if (tag == MKTAG('r','t','p',' '))
2209 tag = 0;
2210 if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
2211 (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
2212 track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
2213 track->par->codec_id == AV_CODEC_ID_H263 ||
2214 track->par->codec_id == AV_CODEC_ID_H264 ||
2215 track->par->codec_id == AV_CODEC_ID_DNXHD ||
2216 track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2217 av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
2218 if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
2219 tag = mov_get_dv_codec_tag(s, track);
2220 else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
2222 else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO)
2224 else if (track->par->codec_id == AV_CODEC_ID_H264)
2225 tag = mov_get_h264_codec_tag(s, track);
2226 else if (track->par->codec_id == AV_CODEC_ID_EVC)
2227 tag = mov_get_evc_codec_tag(s, track);
2228 else if (track->par->codec_id == AV_CODEC_ID_APV)
2229 tag = mov_get_apv_codec_tag(s, track);
2230 else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
2231 tag = mov_get_dnxhd_codec_tag(s, track);
2232 else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2234 if (!tag) { // if no mac fcc found, try with Microsoft tags
2236 if (tag)
2237 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
2238 "the file may be unplayable!\n");
2239 }
2240 } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2242 if (!tag) { // if no mac fcc found, try with Microsoft tags
2243 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
2244 if (ms_tag) {
2245 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
2246 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
2247 "the file may be unplayable!\n");
2248 }
2249 }
2250 } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2252 }
2253
2254 return tag;
2255}
2256
2258 { AV_CODEC_ID_MJPEG, 0xD },
2259 { AV_CODEC_ID_PNG, 0xE },
2260 { AV_CODEC_ID_BMP, 0x1B },
2261 { AV_CODEC_ID_NONE, 0 },
2262};
2263
2264static unsigned int validate_codec_tag(const AVCodecTag *const *tags,
2265 unsigned int tag, int codec_id)
2266{
2267 int i;
2268
2269 /**
2270 * Check that tag + id is in the table
2271 */
2272 for (i = 0; tags && tags[i]; i++) {
2273 const AVCodecTag *codec_tags = tags[i];
2274 while (codec_tags->id != AV_CODEC_ID_NONE) {
2275 if (ff_toupper4(codec_tags->tag) == ff_toupper4(tag) &&
2276 codec_tags->id == codec_id)
2277 return codec_tags->tag;
2278 codec_tags++;
2279 }
2280 }
2281 return 0;
2282}
2283
2284static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
2285{
2286 if (is_cover_image(track->st))
2288
2289 if (track->mode == MODE_IPOD)
2290 if (!av_match_ext(s->url, "m4a") &&
2291 !av_match_ext(s->url, "m4v") &&
2292 !av_match_ext(s->url, "m4b"))
2293 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
2294 "Quicktime/Ipod might not play the file\n");
2295
2296 if (track->mode == MODE_MOV) {
2297 return mov_get_codec_tag(s, track);
2298 } else
2299 return validate_codec_tag(s->oformat->codec_tag, track->par->codec_tag,
2300 track->par->codec_id);
2301}
2302
2303/** Write uuid atom.
2304 * Needed to make file play in iPods running newest firmware
2305 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
2306 */
2308{
2309 avio_wb32(pb, 28);
2310 ffio_wfourcc(pb, "uuid");
2311 avio_wb32(pb, 0x6b6840f2);
2312 avio_wb32(pb, 0x5f244fc5);
2313 avio_wb32(pb, 0xba39a51b);
2314 avio_wb32(pb, 0xcf0323f3);
2315 avio_wb32(pb, 0x0);
2316 return 28;
2317}
2318
2319static const uint16_t fiel_data[] = {
2320 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
2321};
2322
2323static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
2324{
2325 unsigned mov_field_order = 0;
2326 if (field_order < FF_ARRAY_ELEMS(fiel_data))
2327 mov_field_order = fiel_data[field_order];
2328 else
2329 return 0;
2330 avio_wb32(pb, 10);
2331 ffio_wfourcc(pb, "fiel");
2332 avio_wb16(pb, mov_field_order);
2333 return 10;
2334}
2335
2337{
2338 MOVMuxContext *mov = s->priv_data;
2339 int ret = AVERROR_BUG;
2340 int64_t pos = avio_tell(pb);
2341 avio_wb32(pb, 0); /* size */
2342 avio_wl32(pb, track->tag); // store it byteswapped
2343 avio_wb32(pb, 0); /* Reserved */
2344 avio_wb16(pb, 0); /* Reserved */
2345 avio_wb16(pb, 1); /* Data-reference index */
2346
2347 if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
2348 mov_write_esds_tag(pb, track);
2349 else if (track->par->codec_id == AV_CODEC_ID_TTML) {
2350 switch (track->par->codec_tag) {
2351 case MOV_ISMV_TTML_TAG:
2352 // ISMV dfxp requires no extradata.
2353 break;
2354 case MOV_MP4_TTML_TAG:
2355 // As specified in 14496-30, XMLSubtitleSampleEntry
2356 // Namespace
2357 avio_put_str(pb, "http://www.w3.org/ns/ttml");
2358 // Empty schema_location
2359 avio_w8(pb, 0);
2360 // Empty auxiliary_mime_types
2361 avio_w8(pb, 0);
2362 break;
2363 default:
2365 "Unknown codec tag '%s' utilized for TTML stream with "
2366 "index %d (track id %d)!\n",
2367 av_fourcc2str(track->par->codec_tag), track->st->index,
2368 track->track_id);
2369 return AVERROR(EINVAL);
2370 }
2371 } else if (track->extradata_size[track->last_stsd_index])
2372 avio_write(pb, track->extradata[track->last_stsd_index], track->extradata_size[track->last_stsd_index]);
2373
2374 if (mov->write_btrt &&
2375 ((ret = mov_write_btrt_tag(pb, track)) < 0))
2376 return ret;
2377
2378 return update_size(pb, pos);
2379}
2380
2382{
2383 int8_t stereo_mode;
2384
2385 if (stereo_3d->flags != 0) {
2386 av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d flags %x. st3d not written.\n", stereo_3d->flags);
2387 return 0;
2388 }
2389
2390 switch (stereo_3d->type) {
2391 case AV_STEREO3D_2D:
2392 stereo_mode = 0;
2393 break;
2395 stereo_mode = 1;
2396 break;
2398 stereo_mode = 2;
2399 break;
2400 default:
2401 av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d type %s. st3d not written.\n", av_stereo3d_type_name(stereo_3d->type));
2402 return 0;
2403 }
2404 avio_wb32(pb, 13); /* size */
2405 ffio_wfourcc(pb, "st3d");
2406 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2407 avio_w8(pb, stereo_mode);
2408 return 13;
2409}
2410
2412{
2413 int64_t sv3d_pos, svhd_pos, proj_pos;
2414 const char* metadata_source = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : LIBAVFORMAT_IDENT;
2415
2416 if (spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
2417 spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
2418 spherical_mapping->projection != AV_SPHERICAL_CUBEMAP) {
2419 av_log(s, AV_LOG_WARNING, "Unsupported projection %d. sv3d not written.\n", spherical_mapping->projection);
2420 return 0;
2421 }
2422
2423 sv3d_pos = avio_tell(pb);
2424 avio_wb32(pb, 0); /* size */
2425 ffio_wfourcc(pb, "sv3d");
2426
2427 svhd_pos = avio_tell(pb);
2428 avio_wb32(pb, 0); /* size */
2429 ffio_wfourcc(pb, "svhd");
2430 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2431 avio_put_str(pb, metadata_source);
2432 update_size(pb, svhd_pos);
2433
2434 proj_pos = avio_tell(pb);
2435 avio_wb32(pb, 0); /* size */
2436 ffio_wfourcc(pb, "proj");
2437
2438 avio_wb32(pb, 24); /* size */
2439 ffio_wfourcc(pb, "prhd");
2440 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2441 avio_wb32(pb, spherical_mapping->yaw);
2442 avio_wb32(pb, spherical_mapping->pitch);
2443 avio_wb32(pb, spherical_mapping->roll);
2444
2445 switch (spherical_mapping->projection) {
2448 avio_wb32(pb, 28); /* size */
2449 ffio_wfourcc(pb, "equi");
2450 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2451 avio_wb32(pb, spherical_mapping->bound_top);
2452 avio_wb32(pb, spherical_mapping->bound_bottom);
2453 avio_wb32(pb, spherical_mapping->bound_left);
2454 avio_wb32(pb, spherical_mapping->bound_right);
2455 break;
2457 avio_wb32(pb, 20); /* size */
2458 ffio_wfourcc(pb, "cbmp");
2459 avio_wb32(pb, 0); /* version = 0 & flags = 0 */
2460 avio_wb32(pb, 0); /* layout */
2461 avio_wb32(pb, spherical_mapping->padding); /* padding */
2462 break;
2463 }
2464 update_size(pb, proj_pos);
2465
2466 return update_size(pb, sv3d_pos);
2467}
2468
2470{
2471 return av_rescale(q.num, b, q.den);
2472}
2473
2475 const AVStereo3D *stereo3d)
2476{
2477 if (!stereo3d->horizontal_field_of_view.num)
2478 return;
2479
2480 avio_wb32(pb, 12); /* size */
2481 ffio_wfourcc(pb, "hfov");
2483}
2484
2486 const AVSphericalMapping *spherical_mapping)
2487{
2488 avio_wb32(pb, 24); /* size */
2489 ffio_wfourcc(pb, "proj");
2490 avio_wb32(pb, 16); /* size */
2491 ffio_wfourcc(pb, "prji");
2492 avio_wb32(pb, 0); /* version + flags */
2493
2494 switch (spherical_mapping->projection) {
2496 ffio_wfourcc(pb, "rect");
2497 break;
2499 ffio_wfourcc(pb, "equi");
2500 break;
2502 ffio_wfourcc(pb, "hequ");
2503 break;
2505 ffio_wfourcc(pb, "fish");
2506 break;
2507 default:
2508 av_assert0(0);
2509 }
2510}
2511
2513 const AVStereo3D *stereo3d)
2514{
2515 int64_t pos = avio_tell(pb);
2516 int view = 0;
2517
2518 avio_wb32(pb, 0); /* size */
2519 ffio_wfourcc(pb, "eyes");
2520
2521 // stri is mandatory
2522 avio_wb32(pb, 13); /* size */
2523 ffio_wfourcc(pb, "stri");
2524 avio_wb32(pb, 0); /* version + flags */
2525 switch (stereo3d->view) {
2527 view |= 1 << 0;
2528 break;
2530 view |= 1 << 1;
2531 break;
2533 view |= (1 << 0) | (1 << 1);
2534 break;
2535 }
2536 view |= !!(stereo3d->flags & AV_STEREO3D_FLAG_INVERT) << 3;
2537 avio_w8(pb, view);
2538
2539 // hero is optional
2540 if (stereo3d->primary_eye != AV_PRIMARY_EYE_NONE) {
2541 avio_wb32(pb, 13); /* size */
2542 ffio_wfourcc(pb, "hero");
2543 avio_wb32(pb, 0); /* version + flags */
2544 avio_w8(pb, stereo3d->primary_eye);
2545 }
2546
2547 // it's not clear if cams is mandatory or optional
2548 if (stereo3d->baseline) {
2549 avio_wb32(pb, 24); /* size */
2550 ffio_wfourcc(pb, "cams");
2551 avio_wb32(pb, 16); /* size */
2552 ffio_wfourcc(pb, "blin");
2553 avio_wb32(pb, 0); /* version + flags */
2554 avio_wb32(pb, stereo3d->baseline);
2555 }
2556
2557 // it's not clear if cmfy is mandatory or optional
2558 if (stereo3d->horizontal_disparity_adjustment.num) {
2559 avio_wb32(pb, 24); /* size */
2560 ffio_wfourcc(pb, "cmfy");
2561 avio_wb32(pb, 16); /* size */
2562 ffio_wfourcc(pb, "dadj");
2563 avio_wb32(pb, 0); /* version + flags */
2565 }
2566
2567 return update_size(pb, pos);
2568}
2569
2571 const AVStereo3D *stereo3d,
2572 const AVSphericalMapping *spherical_mapping)
2573{
2574 int64_t pos;
2575
2576 if (spherical_mapping &&
2577 spherical_mapping->projection != AV_SPHERICAL_RECTILINEAR &&
2578 spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
2579 spherical_mapping->projection != AV_SPHERICAL_HALF_EQUIRECTANGULAR &&
2580 spherical_mapping->projection != AV_SPHERICAL_FISHEYE) {
2581 av_log(s, AV_LOG_WARNING, "Unsupported projection %d. proj not written.\n",
2582 spherical_mapping->projection);
2583 spherical_mapping = NULL;
2584 }
2585
2586 if (stereo3d && (stereo3d->type == AV_STEREO3D_2D ||
2587 (!(stereo3d->flags & AV_STEREO3D_FLAG_INVERT) &&
2588 stereo3d->view == AV_STEREO3D_VIEW_UNSPEC &&
2589 stereo3d->primary_eye == AV_PRIMARY_EYE_NONE &&
2590 !stereo3d->baseline &&
2591 !stereo3d->horizontal_disparity_adjustment.num))) {
2592 av_log(s, AV_LOG_WARNING, "Unsupported stereo 3d metadata. eyes not written.\n");
2593 stereo3d = NULL;
2594 }
2595
2596 if (!spherical_mapping && !stereo3d)
2597 return 0;
2598
2599 pos = avio_tell(pb);
2600 avio_wb32(pb, 0); /* size */
2601 ffio_wfourcc(pb, "vexu");
2602
2603 if (spherical_mapping)
2604 mov_write_vexu_proj_tag(s, pb, spherical_mapping);
2605
2606 if (stereo3d)
2607 mov_write_eyes_tag(s, pb, stereo3d);
2608
2609 return update_size(pb, pos);
2610}
2611
2613{
2614 uint8_t buf[ISOM_DVCC_DVVC_SIZE];
2615
2616 avio_wb32(pb, 32); /* size = 8 + 24 */
2617 if (dovi->dv_profile > 10)
2618 ffio_wfourcc(pb, "dvwC");
2619 else if (dovi->dv_profile > 7)
2620 ffio_wfourcc(pb, "dvvC");
2621 else
2622 ffio_wfourcc(pb, "dvcC");
2623
2624 ff_isom_put_dvcc_dvvc(s, buf, dovi);
2625 avio_write(pb, buf, sizeof(buf));
2626
2627 return 32; /* 8 + 24 */
2628}
2629
2631{
2632 avio_wb32(pb, 8 + sd->size);
2633 ffio_wfourcc(pb, "hvcE");
2634 avio_write(pb, sd->data, sd->size);
2635 return 8 + sd->size;
2636}
2637
2639 uint32_t top, uint32_t bottom,
2640 uint32_t left, uint32_t right)
2641{
2642 uint32_t cropped_width = track->par->width - left - right;
2643 uint32_t cropped_height = track->height - top - bottom;
2644 AVRational horizOff =
2645 av_sub_q((AVRational) { track->par->width - cropped_width, 2 },
2646 (AVRational) { left, 1 });
2647 AVRational vertOff =
2648 av_sub_q((AVRational) { track->height - cropped_height, 2 },
2649 (AVRational) { top, 1 });
2650
2651 avio_wb32(pb, 40);
2652 ffio_wfourcc(pb, "clap");
2653 avio_wb32(pb, cropped_width); /* apertureWidthN */
2654 avio_wb32(pb, 1); /* apertureWidthD */
2655 avio_wb32(pb, cropped_height); /* apertureHeightN */
2656 avio_wb32(pb, 1); /* apertureHeightD */
2657
2658 avio_wb32(pb, -horizOff.num);
2659 avio_wb32(pb, horizOff.den);
2660 avio_wb32(pb, -vertOff.num);
2661 avio_wb32(pb, vertOff.den);
2662
2663 return 40;
2664}
2665
2667{
2668 AVRational sar;
2669 av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
2670 track->par->sample_aspect_ratio.den, INT_MAX);
2671
2672 avio_wb32(pb, 16);
2673 ffio_wfourcc(pb, "pasp");
2674 avio_wb32(pb, sar.num);
2675 avio_wb32(pb, sar.den);
2676 return 16;
2677}
2678
2679static int mov_write_gama_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track, double gamma)
2680{
2681 uint32_t gama = 0;
2682 if (gamma <= 0.0)
2684 av_log(s, AV_LOG_DEBUG, "gamma value %g\n", gamma);
2685
2686 if (gamma > 1e-6) {
2687 gama = (uint32_t)lrint((double)(1<<16) * gamma);
2688 av_log(s, AV_LOG_DEBUG, "writing gama value %"PRId32"\n", gama);
2689
2690 av_assert0(track->mode == MODE_MOV);
2691 avio_wb32(pb, 12);
2692 ffio_wfourcc(pb, "gama");
2693 avio_wb32(pb, gama);
2694 return 12;
2695 } else {
2696 av_log(s, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
2697 }
2698 return 0;
2699}
2700
2701static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track, int prefer_icc)
2702{
2703 int64_t pos = avio_tell(pb);
2704
2705 // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
2706 // Ref (MP4): ISO/IEC 14496-12:2012
2707
2708 if (prefer_icc) {
2712
2713 if (sd) {
2714 avio_wb32(pb, 12 + sd->size);
2715 ffio_wfourcc(pb, "colr");
2716 ffio_wfourcc(pb, "prof");
2717 avio_write(pb, sd->data, sd->size);
2718 return 12 + sd->size;
2719 }
2720 else {
2721 av_log(NULL, AV_LOG_INFO, "no ICC profile found, will write nclx/nclc colour info instead\n");
2722 }
2723 }
2724
2725 /* We should only ever be called for MOV, MP4 and AVIF. */
2726 av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4 ||
2727 track->mode == MODE_AVIF);
2728
2729 avio_wb32(pb, 0); /* size */
2730 ffio_wfourcc(pb, "colr");
2731 if (track->mode == MODE_MP4 || track->mode == MODE_AVIF)
2732 ffio_wfourcc(pb, "nclx");
2733 else
2734 ffio_wfourcc(pb, "nclc");
2735 // Do not try to guess the color info if it is AVCOL_PRI_UNSPECIFIED.
2736 // e.g., Dolby Vision for Apple devices should be set to AVCOL_PRI_UNSPECIFIED. See
2737 // https://developer.apple.com/av-foundation/High-Dynamic-Range-Metadata-for-Apple-Devices.pdf
2738 avio_wb16(pb, track->par->color_primaries);
2739 avio_wb16(pb, track->par->color_trc);
2740 avio_wb16(pb, track->par->color_space);
2741 if (track->mode == MODE_MP4 || track->mode == MODE_AVIF) {
2742 int full_range = track->par->color_range == AVCOL_RANGE_JPEG;
2743 avio_w8(pb, full_range << 7);
2744 }
2745
2746 return update_size(pb, pos);
2747}
2748
2750{
2751 const AVPacketSideData *side_data;
2752 const AVContentLightMetadata *content_light_metadata;
2753
2757 if (!side_data) {
2758 return 0;
2759 }
2760 content_light_metadata = (const AVContentLightMetadata*)side_data->data;
2761
2762 avio_wb32(pb, 12); // size
2763 ffio_wfourcc(pb, "clli");
2764 avio_wb16(pb, content_light_metadata->MaxCLL);
2765 avio_wb16(pb, content_light_metadata->MaxFALL);
2766 return 12;
2767}
2768
2770{
2771 const int chroma_den = 50000;
2772 const int luma_den = 10000;
2773 const AVPacketSideData *side_data;
2775
2779 if (side_data)
2780 metadata = (const AVMasteringDisplayMetadata*)side_data->data;
2781 if (!metadata || !metadata->has_primaries || !metadata->has_luminance) {
2782 return 0;
2783 }
2784
2785 avio_wb32(pb, 32); // size
2786 ffio_wfourcc(pb, "mdcv");
2787 avio_wb16(pb, rescale_rational(metadata->display_primaries[1][0], chroma_den));
2788 avio_wb16(pb, rescale_rational(metadata->display_primaries[1][1], chroma_den));
2789 avio_wb16(pb, rescale_rational(metadata->display_primaries[2][0], chroma_den));
2790 avio_wb16(pb, rescale_rational(metadata->display_primaries[2][1], chroma_den));
2791 avio_wb16(pb, rescale_rational(metadata->display_primaries[0][0], chroma_den));
2792 avio_wb16(pb, rescale_rational(metadata->display_primaries[0][1], chroma_den));
2793 avio_wb16(pb, rescale_rational(metadata->white_point[0], chroma_den));
2794 avio_wb16(pb, rescale_rational(metadata->white_point[1], chroma_den));
2795 avio_wb32(pb, rescale_rational(metadata->max_luminance, luma_den));
2796 avio_wb32(pb, rescale_rational(metadata->min_luminance, luma_den));
2797 return 32;
2798}
2799
2801{
2802 const int illuminance_den = 10000;
2803 const int ambient_den = 50000;
2804 const AVPacketSideData *side_data;
2805 const AVAmbientViewingEnvironment *ambient;
2806
2807
2811
2812 if (!side_data)
2813 return 0;
2814
2815 ambient = (const AVAmbientViewingEnvironment*)side_data->data;
2816 if (!ambient || !ambient->ambient_illuminance.num)
2817 return 0;
2818
2819 avio_wb32(pb, 16); // size
2820 ffio_wfourcc(pb, "amve");
2821 avio_wb32(pb, rescale_rational(ambient->ambient_illuminance, illuminance_den));
2822 avio_wb16(pb, rescale_rational(ambient->ambient_light_x, ambient_den));
2823 avio_wb16(pb, rescale_rational(ambient->ambient_light_y, ambient_den));
2824 return 16;
2825}
2826
2827static void find_compressor(char * compressor_name, int len, MOVTrack *track)
2828{
2829 AVDictionaryEntry *encoder;
2830 int xdcam_res = (track->par->width == 1280 && track->par->height == 720)
2831 || (track->par->width == 1440 && track->par->height == 1080)
2832 || (track->par->width == 1920 && track->par->height == 1080);
2833
2834 if ((track->mode == MODE_AVIF ||
2835 track->mode == MODE_MOV ||
2836 track->mode == MODE_MP4) &&
2837 (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
2838 av_strlcpy(compressor_name, encoder->value, 32);
2839 } else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
2841 AVStream *st = track->st;
2842 int rate = defined_frame_rate(NULL, st);
2843 av_strlcatf(compressor_name, len, "XDCAM");
2844 if (track->par->format == AV_PIX_FMT_YUV422P) {
2845 av_strlcatf(compressor_name, len, " HD422");
2846 } else if(track->par->width == 1440) {
2847 av_strlcatf(compressor_name, len, " HD");
2848 } else
2849 av_strlcatf(compressor_name, len, " EX");
2850
2851 av_strlcatf(compressor_name, len, " %d%c", track->par->height, interlaced ? 'i' : 'p');
2852
2853 av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
2854 }
2855}
2856
2857#if CONFIG_AVIF_MUXER
2858static int mov_write_ccst_tag(AVIOContext *pb)
2859{
2860 int64_t pos = avio_tell(pb);
2861 // Write sane defaults:
2862 // all_ref_pics_intra = 0 : all samples can use any type of reference.
2863 // intra_pred_used = 1 : intra prediction may or may not be used.
2864 // max_ref_per_pic = 15 : reserved value to indicate that any number of
2865 // reference images can be used.
2866 uint8_t ccstValue = (0 << 7) | /* all_ref_pics_intra */
2867 (1 << 6) | /* intra_pred_used */
2868 (15 << 2); /* max_ref_per_pic */
2869 avio_wb32(pb, 0); /* size */
2870 ffio_wfourcc(pb, "ccst");
2871 avio_wb32(pb, 0); /* Version & flags */
2872 avio_w8(pb, ccstValue);
2873 avio_wb24(pb, 0); /* reserved */
2874 return update_size(pb, pos);
2875}
2876
2877static int mov_write_aux_tag(AVIOContext *pb, const char *aux_type)
2878{
2879 int64_t pos = avio_tell(pb);
2880 avio_wb32(pb, 0); /* size */
2881 ffio_wfourcc(pb, aux_type);
2882 avio_wb32(pb, 0); /* Version & flags */
2883 avio_write(pb, "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\0", 44);
2884 return update_size(pb, pos);
2885}
2886#endif
2887
2889{
2890 int ret = AVERROR_BUG;
2891 int64_t pos = avio_tell(pb);
2892 const AVPacketSideData *sd;
2893 char compressor_name[32] = { 0 };
2894 int avid = 0;
2895
2896 int uncompressed_ycbcr = ((track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVY422)
2897 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_YUYV422)
2898 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_VYU444)
2899 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVA)
2900 || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_V30XLE)
2901 || track->par->codec_id == AV_CODEC_ID_V210);
2902
2903 avio_wb32(pb, 0); /* size */
2904 if (mov->encryption_scheme != MOV_ENC_NONE) {
2905 ffio_wfourcc(pb, "encv");
2906 } else {
2907 avio_wl32(pb, track->tag); // store it byteswapped
2908 }
2909 avio_wb32(pb, 0); /* Reserved */
2910 avio_wb16(pb, 0); /* Reserved */
2911 avio_wb16(pb, 1); /* Data-reference index */
2912
2913 if (uncompressed_ycbcr) {
2914 avio_wb16(pb, 2); /* Codec stream version */
2915 } else {
2916 avio_wb16(pb, 0); /* Codec stream version */
2917 }
2918 avio_wb16(pb, 0); /* Codec stream revision (=0) */
2919 if (track->mode == MODE_MOV) {
2920 ffio_wfourcc(pb, "FFMP"); /* Vendor */
2921 if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO || uncompressed_ycbcr) {
2922 avio_wb32(pb, 0); /* Temporal Quality */
2923 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
2924 } else {
2925 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
2926 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
2927 }
2928 } else {
2929 ffio_fill(pb, 0, 3 * 4); /* Reserved */
2930 }
2931 avio_wb16(pb, track->par->width); /* Video width */
2932 avio_wb16(pb, track->height); /* Video height */
2933 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
2934 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
2935 avio_wb32(pb, 0); /* Data size (= 0) */
2936 avio_wb16(pb, 1); /* Frame count (= 1) */
2937
2938 find_compressor(compressor_name, 32, track);
2939 avio_w8(pb, strlen(compressor_name));
2940 avio_write(pb, compressor_name, 31);
2941
2942 if (track->mode == MODE_MOV && track->par->codec_id == AV_CODEC_ID_V210)
2943 avio_wb16(pb, 0x18);
2944 else if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
2945 avio_wb16(pb, track->par->bits_per_coded_sample |
2946 (track->par->format == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
2947 else
2948 avio_wb16(pb, 0x18); /* Reserved */
2949
2950 if (track->mode == MODE_MOV && track->par->format == AV_PIX_FMT_PAL8) {
2951 int pal_size, i;
2952 avio_wb16(pb, 0); /* Color table ID */
2953 avio_wb32(pb, 0); /* Color table seed */
2954 avio_wb16(pb, 0x8000); /* Color table flags */
2955 if (track->par->bits_per_coded_sample < 0 || track->par->bits_per_coded_sample > 8)
2956 return AVERROR(EINVAL);
2957 pal_size = 1 << track->par->bits_per_coded_sample;
2958 avio_wb16(pb, pal_size - 1); /* Color table size (zero-relative) */
2959 for (i = 0; i < pal_size; i++) {
2960 uint32_t rgb = track->palette[i];
2961 uint16_t r = (rgb >> 16) & 0xff;
2962 uint16_t g = (rgb >> 8) & 0xff;
2963 uint16_t b = rgb & 0xff;
2964 avio_wb16(pb, 0);
2965 avio_wb16(pb, (r << 8) | r);
2966 avio_wb16(pb, (g << 8) | g);
2967 avio_wb16(pb, (b << 8) | b);
2968 }
2969 } else
2970 avio_wb16(pb, 0xffff); /* Reserved */
2971
2972 if (track->tag == MKTAG('m','p','4','v'))
2973 mov_write_esds_tag(pb, track);
2974 else if (track->par->codec_id == AV_CODEC_ID_H263)
2976 else if (track->par->codec_id == AV_CODEC_ID_AVUI ||
2977 track->par->codec_id == AV_CODEC_ID_SVQ3) {
2978 mov_write_extradata_tag(pb, track);
2979 avio_wb32(pb, 0);
2980 } else if (track->par->codec_id == AV_CODEC_ID_DNXHD) {
2981 mov_write_avid_tag(pb, track);
2982 avid = 1;
2983 } else if (track->par->codec_id == AV_CODEC_ID_HEVC) {
2984 mov_write_hvcc_tag(mov->fc, pb, track);
2986 ret = mov_write_lhvc_tag(mov->fc, pb, track);
2987 if (ret < 0)
2988 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'lhvC' atom for multilayer stream.\n");
2989 }
2990 } else if (track->par->codec_id == AV_CODEC_ID_VVC)
2991 mov_write_vvcc_tag(pb, track);
2992 else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
2993 mov_write_avcc_tag(pb, track);
2994 if (track->mode == MODE_IPOD)
2996 }
2997 else if (track->par->codec_id ==AV_CODEC_ID_EVC) {
2998 mov_write_evcc_tag(pb, track);
2999 } else if (track->par->codec_id == AV_CODEC_ID_LCEVC) {
3000 mov_write_lvcc_tag(mov->fc, pb, track);
3001 } else if (track->par->codec_id ==AV_CODEC_ID_APV) {
3002 mov_write_apvc_tag(mov->fc, pb, track);
3003 } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
3004 mov_write_vpcc_tag(mov->fc, pb, track);
3005 } else if (track->par->codec_id == AV_CODEC_ID_AV1) {
3006 mov_write_av1c_tag(pb, track);
3007 } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->extradata_size[track->last_stsd_index] > 0)
3008 mov_write_dvc1_tag(pb, track);
3009 else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
3010 track->par->codec_id == AV_CODEC_ID_VP6A) {
3011 /* Don't write any potential extradata here - the cropping
3012 * is signalled via the normal width/height fields. */
3013 } else if (track->par->codec_id == AV_CODEC_ID_R10K) {
3014 if (track->par->codec_tag == MKTAG('R','1','0','k'))
3015 mov_write_dpxe_tag(pb, track);
3016 } else if (track->par->codec_id == AV_CODEC_ID_AVS3) {
3017 mov_write_av3c_tag(pb, track);
3018 } else if (track->extradata_size[track->last_stsd_index] > 0)
3019 mov_write_glbl_tag(pb, track);
3020
3021 if (track->par->codec_id != AV_CODEC_ID_H264 &&
3022 track->par->codec_id != AV_CODEC_ID_MPEG4 &&
3023 track->par->codec_id != AV_CODEC_ID_DNXHD) {
3024 int field_order = track->par->field_order;
3025
3026 if (field_order != AV_FIELD_UNKNOWN)
3027 mov_write_fiel_tag(pb, track, field_order);
3028 }
3029
3030 if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
3031 if (track->mode == MODE_MOV)
3032 mov_write_gama_tag(s, pb, track, mov->gamma);
3033 else
3034 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
3035 }
3036 if (track->mode == MODE_MOV || track->mode == MODE_MP4 || track->mode == MODE_AVIF) {
3037 int has_color_info = track->par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
3038 track->par->color_trc != AVCOL_TRC_UNSPECIFIED &&
3040 if (has_color_info || mov->flags & FF_MOV_FLAG_WRITE_COLR ||
3043 int prefer_icc = mov->flags & FF_MOV_FLAG_PREFER_ICC || !has_color_info;
3044 mov_write_colr_tag(pb, track, prefer_icc);
3045 }
3046 } else if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
3047 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4 or AVIF.\n");
3048 }
3049
3050 if (track->mode == MODE_MOV || track->mode == MODE_MP4) {
3051 mov_write_clli_tag(pb, track);
3052 mov_write_mdcv_tag(pb, track);
3053 mov_write_amve_tag(pb, track);
3054 }
3055
3056 if (track->mode == MODE_MP4) {
3060 const AVPacketSideData *spherical_mapping = av_packet_side_data_get(track->st->codecpar->coded_side_data,
3063 if (stereo_3d && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)
3064 mov_write_st3d_tag(s, pb, (AVStereo3D*)stereo_3d->data);
3065 else if (stereo_3d)
3066 av_log(s, AV_LOG_WARNING, "Writing supported 'st3d' metadata requires -strict unofficial.\n");
3067 if (spherical_mapping && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)
3068 mov_write_sv3d_tag(mov->fc, pb, (AVSphericalMapping*)spherical_mapping->data);
3069 else if (spherical_mapping)
3070 av_log(s, AV_LOG_WARNING, "Writing supported 'sv3d' metadata requires -strict unofficial.\n");
3071 }
3072
3073 if (track->mode == MODE_MOV || (track->mode == MODE_MP4 &&
3075 const AVStereo3D *stereo3d = NULL;
3076 const AVSphericalMapping *spherical_mapping = NULL;
3077
3081 if (sd)
3082 stereo3d = (AVStereo3D *)sd->data;
3083
3087 if (sd)
3088 spherical_mapping = (AVSphericalMapping *)sd->data;
3089
3090 if (stereo3d || spherical_mapping)
3091 mov_write_vexu_tag(s, pb, stereo3d, spherical_mapping);
3092 if (stereo3d)
3093 mov_write_hfov_tag(s, pb, stereo3d);
3094 }
3095
3096 if (track->mode == MODE_MP4) {
3100 if (dovi && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
3102 } else if (dovi) {
3103 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'dvcC'/'dvvC' box. Requires -strict unofficial.\n");
3104 }
3105
3109 if (hvce && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
3110 mov_write_hvce_tag(pb, hvce);
3111 } else if (hvce) {
3112 av_log(mov->fc, AV_LOG_WARNING, "Not writing 'hvcE' box. Requires -strict unofficial.\n");
3113 }
3114 }
3115
3116 if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num) {
3117 mov_write_pasp_tag(pb, track);
3118 }
3119
3123 if (sd && sd->size >= sizeof(uint32_t) * 4) {
3124 uint64_t top = AV_RL32(sd->data + 0);
3125 uint64_t bottom = AV_RL32(sd->data + 4);
3126 uint64_t left = AV_RL32(sd->data + 8);
3127 uint64_t right = AV_RL32(sd->data + 12);
3128
3129 if ((left + right) >= track->par->width ||
3130 (top + bottom) >= track->height) {
3131 av_log(s, AV_LOG_ERROR, "Invalid cropping dimensions in stream side data\n");
3132 return AVERROR(EINVAL);
3133 }
3134 if (top || bottom || left || right)
3135 mov_write_clap_tag(pb, track, top, bottom, left, right);
3136 } else if (uncompressed_ycbcr)
3137 mov_write_clap_tag(pb, track, 0, 0, 0, 0);
3138
3139 if (mov->encryption_scheme != MOV_ENC_NONE) {
3141 }
3142
3143 if (mov->write_btrt &&
3144 ((ret = mov_write_btrt_tag(pb, track)) < 0))
3145 return ret;
3146
3147 /* extra padding for avid stsd */
3148 /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
3149 if (avid)
3150 avio_wb32(pb, 0);
3151
3152#if CONFIG_AVIF_MUXER
3153 if (track->mode == MODE_AVIF) {
3154 mov_write_ccst_tag(pb);
3155 if (mov->nb_streams > 0 && track == &mov->tracks[1])
3156 mov_write_aux_tag(pb, "auxi");
3157 }
3158#endif
3159
3160 return update_size(pb, pos);
3161}
3162
3164{
3165 int64_t pos = avio_tell(pb);
3166 avio_wb32(pb, 0); /* size */
3167 ffio_wfourcc(pb, "rtp ");
3168 avio_wb32(pb, 0); /* Reserved */
3169 avio_wb16(pb, 0); /* Reserved */
3170 avio_wb16(pb, 1); /* Data-reference index */
3171
3172 avio_wb16(pb, 1); /* Hint track version */
3173 avio_wb16(pb, 1); /* Highest compatible version */
3174 avio_wb32(pb, track->max_packet_size); /* Max packet size */
3175
3176 avio_wb32(pb, 12); /* size */
3177 ffio_wfourcc(pb, "tims");
3178 avio_wb32(pb, track->timescale);
3179
3180 return update_size(pb, pos);
3181}
3182
3183static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
3184{
3185 uint64_t str_size =strlen(reel_name);
3186 int64_t pos = avio_tell(pb);
3187
3188 if (str_size >= UINT16_MAX){
3189 av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
3190 avio_wb16(pb, 0);
3191 return AVERROR(EINVAL);
3192 }
3193
3194 avio_wb32(pb, 0); /* size */
3195 ffio_wfourcc(pb, "name"); /* Data format */
3196 avio_wb16(pb, str_size); /* string size */
3197 avio_wb16(pb, track->language); /* langcode */
3198 avio_write(pb, reel_name, str_size); /* reel name */
3199 return update_size(pb,pos);
3200}
3201
3203{
3204 int64_t pos = avio_tell(pb);
3205#if 1
3206 int frame_duration;
3207 int nb_frames;
3209
3210 if (!track->st->avg_frame_rate.num || !track->st->avg_frame_rate.den) {
3211 av_log(NULL, AV_LOG_ERROR, "avg_frame_rate not set for tmcd track.\n");
3212 return AVERROR(EINVAL);
3213 } else {
3214 frame_duration = av_rescale(track->timescale, track->st->avg_frame_rate.den, track->st->avg_frame_rate.num);
3215 nb_frames = ROUNDED_DIV(track->st->avg_frame_rate.num, track->st->avg_frame_rate.den);
3216 }
3217
3218 if (nb_frames > 255) {
3219 av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
3220 return AVERROR(EINVAL);
3221 }
3222
3223 avio_wb32(pb, 0); /* size */
3224 ffio_wfourcc(pb, "tmcd"); /* Data format */
3225 avio_wb32(pb, 0); /* Reserved */
3226 avio_wb32(pb, 1); /* Data reference index */
3227 avio_wb32(pb, 0); /* Flags */
3228 avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
3229 avio_wb32(pb, track->timescale); /* Timescale */
3230 avio_wb32(pb, frame_duration); /* Frame duration */
3231 avio_w8(pb, nb_frames); /* Number of frames */
3232 avio_w8(pb, 0); /* Reserved */
3233
3234 t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
3235 if (t && utf8len(t->value) && track->mode != MODE_MP4)
3237 else
3238 avio_wb16(pb, 0); /* zero size */
3239#else
3240
3241 avio_wb32(pb, 0); /* size */
3242 ffio_wfourcc(pb, "tmcd"); /* Data format */
3243 avio_wb32(pb, 0); /* Reserved */
3244 avio_wb32(pb, 1); /* Data reference index */
3245 if (track->par->extradata_size)
3246 avio_write(pb, track->par->extradata, track->par->extradata_size);
3247#endif
3248 return update_size(pb, pos);
3249}
3250
3251static int mov_write_bin_data_tag(AVIOContext *pb, uint32_t tag)
3252{
3253 int64_t pos = avio_tell(pb);
3254 avio_wb32(pb, 0); /* size */
3255 avio_wl32(pb, tag);
3256 avio_wb32(pb, 0); /* Reserved */
3257 avio_wb16(pb, 0); /* Reserved */
3258 avio_wb16(pb, 1); /* Data-reference index */
3259 avio_wb32(pb, 0); /* Reserved */
3260 return update_size(pb, pos);
3261}
3262
3264{
3265 int64_t pos = avio_tell(pb);
3266 int ret = 0;
3267 avio_wb32(pb, 0); /* size */
3268 ffio_wfourcc(pb, "stsd");
3269 avio_wb32(pb, 0); /* version & flags */
3270 avio_wb32(pb, track->stsd_count);
3271
3272 int stsd_index_back = track->last_stsd_index;
3273 for (track->last_stsd_index = 0;
3274 track->last_stsd_index < track->stsd_count;
3275 track->last_stsd_index++) {
3276 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
3277 ret = mov_write_video_tag(s, pb, mov, track);
3278 else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
3279 ret = mov_write_audio_tag(s, pb, mov, track);
3280 else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
3281 ret = mov_write_subtitle_tag(s, pb, track);
3282 else if (track->par->codec_tag == MKTAG('r','t','p',' '))
3283 ret = mov_write_rtp_tag(pb, track);
3284 else if (track->par->codec_tag == MKTAG('t','m','c','d'))
3285 ret = mov_write_tmcd_tag(pb, track);
3286 else if (track->par->codec_tag == MKTAG('g','p','m','d'))
3287 ret = mov_write_bin_data_tag(pb, track->par->codec_tag);
3288 else if (track->tag == MOV_MP4_DJMD_TAG ||
3289 track->tag == MOV_MP4_DBGI_TAG)
3290 ret = mov_write_bin_data_tag(pb, track->tag);
3291
3292 if (ret < 0)
3293 return ret;
3294 }
3295
3296 track->last_stsd_index = stsd_index_back;
3297
3298 return update_size_and_version(pb, pos, track->entry_version);
3299}
3300
3302{
3303 MOVMuxContext *mov = s->priv_data;
3304 MOVCtts *ctts_entries;
3305 uint32_t entries = 0;
3306 uint32_t atom_size;
3307 int i;
3308
3309 ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
3310 if (!ctts_entries)
3311 return AVERROR(ENOMEM);
3312 ctts_entries[0].count = 1;
3313 ctts_entries[0].offset = track->cluster[0].cts;
3314 for (i = 1; i < track->entry; i++) {
3315 if (track->cluster[i].cts == ctts_entries[entries].offset) {
3316 ctts_entries[entries].count++; /* compress */
3317 } else {
3318 entries++;
3319 ctts_entries[entries].offset = track->cluster[i].cts;
3320 ctts_entries[entries].count = 1;
3321 }
3322 }
3323 entries++; /* last one */
3324 atom_size = 16 + (entries * 8);
3325 avio_wb32(pb, atom_size); /* size */
3326 ffio_wfourcc(pb, "ctts");
3328 avio_w8(pb, 1); /* version */
3329 else
3330 avio_w8(pb, 0); /* version */
3331 avio_wb24(pb, 0); /* flags */
3332 avio_wb32(pb, entries); /* entry count */
3333 for (i = 0; i < entries; i++) {
3334 avio_wb32(pb, ctts_entries[i].count);
3335 avio_wb32(pb, ctts_entries[i].offset);
3336 }
3337 av_free(ctts_entries);
3338 return atom_size;
3339}
3340
3341/* Time to sample atom */
3343{
3344 MOVStts *stts_entries = NULL;
3345 uint32_t entries = -1;
3346 uint32_t atom_size;
3347 int i;
3348
3349 if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
3350 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
3351 if (!stts_entries)
3352 return AVERROR(ENOMEM);
3353 stts_entries[0].count = track->sample_count;
3354 stts_entries[0].duration = 1;
3355 entries = 1;
3356 } else {
3357 if (track->entry) {
3358 stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
3359 if (!stts_entries)
3360 return AVERROR(ENOMEM);
3361 }
3362 for (i = 0; i < track->entry; i++) {
3363 int duration = get_cluster_duration(track, i);
3364#if CONFIG_IAMFENC
3365 if (track->iamf && track->par->codec_id == AV_CODEC_ID_OPUS)
3366 duration = av_rescale(duration, 48000, track->par->sample_rate);
3367#endif
3368 if (i && duration == stts_entries[entries].duration) {
3369 stts_entries[entries].count++; /* compress */
3370 } else {
3371 entries++;
3372 stts_entries[entries].duration = duration;
3373 stts_entries[entries].count = 1;
3374 }
3375 }
3376 entries++; /* last one */
3377 }
3378 atom_size = 16 + (entries * 8);
3379 avio_wb32(pb, atom_size); /* size */
3380 ffio_wfourcc(pb, "stts");
3381 avio_wb32(pb, 0); /* version & flags */
3382 avio_wb32(pb, entries); /* entry count */
3383 for (i = 0; i < entries; i++) {
3384 avio_wb32(pb, stts_entries[i].count);
3385 avio_wb32(pb, stts_entries[i].duration);
3386 }
3387 av_free(stts_entries);
3388 return atom_size;
3389}
3390
3392{
3393 avio_wb32(pb, 28); /* size */
3394 ffio_wfourcc(pb, "dref");
3395 avio_wb32(pb, 0); /* version & flags */
3396 avio_wb32(pb, 1); /* entry count */
3397
3398 avio_wb32(pb, 0xc); /* size */
3399 //FIXME add the alis and rsrc atom
3400 ffio_wfourcc(pb, "url ");
3401 avio_wb32(pb, 1); /* version & flags */
3402
3403 return 28;
3404}
3405
3407{
3408 struct sgpd_entry {
3409 int count;
3410 int16_t roll_distance;
3411 int group_description_index;
3412 };
3413
3414 struct sgpd_entry *sgpd_entries = NULL;
3415 int entries = -1;
3416 int group = 0;
3417 int i, j;
3418
3419 const int OPUS_SEEK_PREROLL_MS = 80;
3420 int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
3421 (AVRational){1, 1000},
3422 (AVRational){1, 48000});
3423
3424 if (!track->entry)
3425 return 0;
3426
3427 sgpd_entries = av_malloc_array(track->entry, sizeof(*sgpd_entries));
3428 if (!sgpd_entries)
3429 return AVERROR(ENOMEM);
3430
3432
3433 if (track->par->codec_id == AV_CODEC_ID_OPUS) {
3434 for (i = 0; i < track->entry; i++) {
3435 int roll_samples_remaining = roll_samples;
3436 int distance = 0;
3437 for (j = i - 1; j >= 0; j--) {
3438 roll_samples_remaining -= get_cluster_duration(track, j);
3439 distance++;
3440 if (roll_samples_remaining <= 0)
3441 break;
3442 }
3443 /* We don't have enough preceding samples to compute a valid
3444 roll_distance here, so this sample can't be independently
3445 decoded. */
3446 if (roll_samples_remaining > 0)
3447 distance = 0;
3448 /* Verify distance is a maximum of 32 (2.5ms) packets. */
3449 if (distance > 32) {
3450 av_freep(&sgpd_entries);
3451 return AVERROR_INVALIDDATA;
3452 }
3453 if (i && distance == sgpd_entries[entries].roll_distance) {
3454 sgpd_entries[entries].count++;
3455 } else {
3456 entries++;
3457 sgpd_entries[entries].count = 1;
3458 sgpd_entries[entries].roll_distance = distance;
3459 sgpd_entries[entries].group_description_index = distance ? ++group : 0;
3460 }
3461 }
3462 } else {
3463 entries++;
3464 sgpd_entries[entries].count = track->sample_count;
3465 sgpd_entries[entries].roll_distance = 1;
3466 sgpd_entries[entries].group_description_index = ++group;
3467 }
3468 entries++;
3469
3470 if (!group) {
3471 av_free(sgpd_entries);
3472 return 0;
3473 }
3474
3475 /* Write sgpd tag */
3476 avio_wb32(pb, 24 + (group * 2)); /* size */
3477 ffio_wfourcc(pb, "sgpd");
3478 avio_wb32(pb, 1 << 24); /* fullbox */
3479 ffio_wfourcc(pb, "roll");
3480 avio_wb32(pb, 2); /* default_length */
3481 avio_wb32(pb, group); /* entry_count */
3482 for (i = 0; i < entries; i++) {
3483 if (sgpd_entries[i].group_description_index) {
3484 avio_wb16(pb, -sgpd_entries[i].roll_distance); /* roll_distance */
3485 }
3486 }
3487
3488 /* Write sbgp tag */
3489 avio_wb32(pb, 20 + (entries * 8)); /* size */
3490 ffio_wfourcc(pb, "sbgp");
3491 avio_wb32(pb, 0); /* fullbox */
3492 ffio_wfourcc(pb, "roll");
3493 avio_wb32(pb, entries); /* entry_count */
3494 for (i = 0; i < entries; i++) {
3495 avio_wb32(pb, sgpd_entries[i].count); /* sample_count */
3496 avio_wb32(pb, sgpd_entries[i].group_description_index); /* group_description_index */
3497 }
3498
3499 av_free(sgpd_entries);
3500 return 0;
3501}
3502
3504{
3505 int64_t pos = avio_tell(pb);
3506 int ret = 0;
3507
3508 avio_wb32(pb, 0); /* size */
3509 ffio_wfourcc(pb, "stbl");
3510 if ((ret = mov_write_stsd_tag(s, pb, mov, track)) < 0)
3511 return ret;
3512 mov_write_stts_tag(pb, track);
3513 if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
3514 track->par->codec_id == AV_CODEC_ID_TRUEHD ||
3516 (track->par->codec_id == AV_CODEC_ID_AAC && track->par->profile == AV_PROFILE_AAC_USAC) ||
3517 track->par->codec_tag == MKTAG('r','t','p',' ')) &&
3518 track->has_keyframes && track->has_keyframes < track->entry)
3520 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && track->has_disposable && track->entry)
3521 mov_write_sdtp_tag(pb, track);
3522 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
3524 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
3525 track->flags & MOV_TRACK_CTTS && track->entry) {
3526
3527 if ((ret = mov_write_ctts_tag(s, pb, track)) < 0)
3528 return ret;
3529 }
3530 mov_write_stsc_tag(pb, track);
3531 mov_write_stsz_tag(pb, track);
3532 mov_write_stco_tag(pb, track);
3533 if (track->cenc.aes_ctr && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3534 ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, 0);
3535 }
3536 if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
3538 }
3539 return update_size(pb, pos);
3540}
3541
3543{
3544 int64_t pos = avio_tell(pb);
3545 avio_wb32(pb, 0); /* size */
3546 ffio_wfourcc(pb, "dinf");
3548 return update_size(pb, pos);
3549}
3550
3552{
3553 avio_wb32(pb, 12);
3554 ffio_wfourcc(pb, "nmhd");
3555 avio_wb32(pb, 0);
3556 return 12;
3557}
3558
3560{
3561 avio_wb32(pb, 12);
3562 ffio_wfourcc(pb, "sthd");
3563 avio_wb32(pb, 0);
3564 return 12;
3565}
3566
3568{
3569 int64_t pos = avio_tell(pb);
3570 const char *font = "Lucida Grande";
3571 avio_wb32(pb, 0); /* size */
3572 ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
3573 avio_wb32(pb, 0); /* version & flags */
3574 avio_wb16(pb, 0); /* text font */
3575 avio_wb16(pb, 0); /* text face */
3576 avio_wb16(pb, 12); /* text size */
3577 avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
3578 avio_wb16(pb, 0x0000); /* text color (red) */
3579 avio_wb16(pb, 0x0000); /* text color (green) */
3580 avio_wb16(pb, 0x0000); /* text color (blue) */
3581 avio_wb16(pb, 0xffff); /* background color (red) */
3582 avio_wb16(pb, 0xffff); /* background color (green) */
3583 avio_wb16(pb, 0xffff); /* background color (blue) */
3584 avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
3585 avio_write(pb, font, strlen(font)); /* font name */
3586 return update_size(pb, pos);
3587}
3588
3590{
3591 int64_t pos = avio_tell(pb);
3592 avio_wb32(pb, 0); /* size */
3593 ffio_wfourcc(pb, "gmhd");
3594 avio_wb32(pb, 0x18); /* gmin size */
3595 ffio_wfourcc(pb, "gmin");/* generic media info */
3596 avio_wb32(pb, 0); /* version & flags */
3597 avio_wb16(pb, 0x40); /* graphics mode = */
3598 avio_wb16(pb, 0x8000); /* opColor (r?) */
3599 avio_wb16(pb, 0x8000); /* opColor (g?) */
3600 avio_wb16(pb, 0x8000); /* opColor (b?) */
3601 avio_wb16(pb, 0); /* balance */
3602 avio_wb16(pb, 0); /* reserved */
3603
3604 /*
3605 * This special text atom is required for
3606 * Apple Quicktime chapters. The contents
3607 * don't appear to be documented, so the
3608 * bytes are copied verbatim.
3609 */
3610 if (track->tag != MKTAG('c','6','0','8')) {
3611 avio_wb32(pb, 0x2C); /* size */
3612 ffio_wfourcc(pb, "text");
3613 avio_wb16(pb, 0x01);
3614 avio_wb32(pb, 0x00);
3615 avio_wb32(pb, 0x00);
3616 avio_wb32(pb, 0x00);
3617 avio_wb32(pb, 0x01);
3618 avio_wb32(pb, 0x00);
3619 avio_wb32(pb, 0x00);
3620 avio_wb32(pb, 0x00);
3621 avio_wb32(pb, 0x00004000);
3622 avio_wb16(pb, 0x0000);
3623 }
3624
3625 if (track->par->codec_tag == MKTAG('t','m','c','d')) {
3626 int64_t tmcd_pos = avio_tell(pb);
3627 avio_wb32(pb, 0); /* size */
3628 ffio_wfourcc(pb, "tmcd");
3629 mov_write_tcmi_tag(pb, track);
3630 update_size(pb, tmcd_pos);
3631 } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
3632 int64_t gpmd_pos = avio_tell(pb);
3633 avio_wb32(pb, 0); /* size */
3634 ffio_wfourcc(pb, "gpmd");
3635 avio_wb32(pb, 0); /* version */
3636 update_size(pb, gpmd_pos);
3637 } else if (track->tag == MOV_MP4_DBGI_TAG) {
3638 avio_wb32(pb, 12);
3640 avio_wb32(pb, 0);
3641 } else if (track->tag == MOV_MP4_DJMD_TAG) {
3642 avio_wb32(pb, 8);
3644 }
3645 return update_size(pb, pos);
3646}
3647
3649{
3650 avio_wb32(pb, 16); /* size */
3651 ffio_wfourcc(pb, "smhd");
3652 avio_wb32(pb, 0); /* version & flags */
3653 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
3654 avio_wb16(pb, 0); /* reserved */
3655 return 16;
3656}
3657
3658static uint16_t mov_graphics_mode(MOVTrack *track)
3659{
3660 if (track->mode != MODE_MOV)
3661 return 0; /* ISO/IEC 14496-12 doesn't define any other modes */
3662
3663 switch (track->par->alpha_mode) {
3664 default: return MOV_GRAPHICS_MODE_COPY;
3667 }
3668}
3669
3671{
3672 avio_wb32(pb, 0x14); /* size (always 0x14) */
3673 ffio_wfourcc(pb, "vmhd");
3674 avio_wb32(pb, 0x01); /* version & flags */
3675 avio_wb16(pb, mov_graphics_mode(track));
3676 for (int i = 0; i < 3; i++)
3677 avio_wb16(pb, 0); /* opcolor */
3678 return 0x14;
3679}
3680
3681static int is_clcp_track(MOVTrack *track)
3682{
3683 return track->tag == MKTAG('c','7','0','8') ||
3684 track->tag == MKTAG('c','6','0','8');
3685}
3686
3688{
3689 MOVMuxContext *mov = s->priv_data;
3690 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
3691 int64_t pos = avio_tell(pb);
3692 size_t descr_len;
3693
3694 hdlr = "dhlr";
3695 hdlr_type = "url ";
3696 descr = "DataHandler";
3697
3698 if (track) {
3699 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
3700 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3701 if (track->mode == MODE_AVIF) {
3702 hdlr_type = (track == &mov->tracks[0]) ? "pict" : "auxv";
3703 descr = "PictureHandler";
3704 } else {
3705 hdlr_type = "vide";
3706 descr = "VideoHandler";
3707 }
3708 } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3709 hdlr_type = "soun";
3710 descr = "SoundHandler";
3711 } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3712 if (is_clcp_track(track)) {
3713 hdlr_type = "clcp";
3714 descr = "ClosedCaptionHandler";
3715 } else {
3716 if (track->tag == MKTAG('t','x','3','g')) {
3717 hdlr_type = "sbtl";
3718 } else if (track->tag == MKTAG('m','p','4','s')) {
3719 hdlr_type = "subp";
3720 } else if (track->tag == MOV_MP4_TTML_TAG) {
3721 hdlr_type = "subt";
3722 } else {
3723 hdlr_type = "text";
3724 }
3725 descr = "SubtitleHandler";
3726 }
3727 } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
3728 hdlr_type = "hint";
3729 descr = "HintHandler";
3730 } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
3731 hdlr_type = "tmcd";
3732 descr = "TimeCodeHandler";
3733 } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
3734 hdlr_type = "meta";
3735 descr = "GoPro MET"; // GoPro Metadata
3736 } else if (track->tag == MOV_MP4_DJMD_TAG) {
3737 hdlr_type = "meta";
3738 descr = "CAM meta";
3739 } else if (track->tag == MOV_MP4_DBGI_TAG) {
3740 hdlr_type = "meta";
3741 descr = "CAM dbgi";
3742 } else {
3744 "Unknown hdlr_type for %s, writing dummy values\n",
3745 av_fourcc2str(track->par->codec_tag));
3746 }
3747 if (track->st) {
3748 // hdlr.name is used by some players to identify the content title
3749 // of the track. So if an alternate handler description is
3750 // specified, use it.
3752 t = av_dict_get(track->st->metadata, "handler_name", NULL, 0);
3753 if (t && utf8len(t->value))
3754 descr = t->value;
3755 }
3756 }
3757
3758 if (mov->empty_hdlr_name) /* expressly allowed by QTFF and not prohibited in ISO 14496-12 8.4.3.3 */
3759 descr = "";
3760
3761 avio_wb32(pb, 0); /* size */
3762 ffio_wfourcc(pb, "hdlr");
3763 avio_wb32(pb, 0); /* Version & flags */
3764 avio_write(pb, hdlr, 4); /* handler */
3765 ffio_wfourcc(pb, hdlr_type); /* handler type */
3766 avio_wb32(pb, 0); /* reserved */
3767 avio_wb32(pb, 0); /* reserved */
3768 avio_wb32(pb, 0); /* reserved */
3769 descr_len = strlen(descr);
3770 if (!track || track->mode == MODE_MOV)
3771 avio_w8(pb, descr_len); /* pascal string */
3772 avio_write(pb, descr, descr_len); /* handler description */
3773 if (track && track->mode != MODE_MOV)
3774 avio_w8(pb, 0); /* c string */
3775 return update_size(pb, pos);
3776}
3777
3778#if CONFIG_AVIF_MUXER
3779static int mov_write_pitm_tag(AVIOContext *pb, int item_id)
3780{
3781 int64_t pos = avio_tell(pb);
3782 avio_wb32(pb, 0); /* size */
3783 ffio_wfourcc(pb, "pitm");
3784 avio_wb32(pb, 0); /* Version & flags */
3785 avio_wb16(pb, item_id); /* item_id */
3786 return update_size(pb, pos);
3787}
3788
3789static int mov_write_iloc_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3790{
3791 int64_t pos = avio_tell(pb);
3792 avio_wb32(pb, 0); /* size */
3793 ffio_wfourcc(pb, "iloc");
3794 avio_wb32(pb, 0); /* Version & flags */
3795 avio_w8(pb, (4 << 4) + 4); /* offset_size(4) and length_size(4) */
3796 avio_w8(pb, 0); /* base_offset_size(4) and reserved(4) */
3797 avio_wb16(pb, mov->nb_streams); /* item_count */
3798
3799 for (int i = 0; i < mov->nb_streams; i++) {
3800 avio_wb16(pb, i + 1); /* item_id */
3801 avio_wb16(pb, 0); /* data_reference_index */
3802 avio_wb16(pb, 1); /* extent_count */
3803 mov->avif_extent_pos[i] = avio_tell(pb);
3804 avio_wb32(pb, 0); /* extent_offset (written later) */
3805 // For animated AVIF, we simply write the first packet's size.
3806 avio_wb32(pb, mov->avif_extent_length[i]); /* extent_length */
3807 }
3808
3809 return update_size(pb, pos);
3810}
3811
3812static int mov_write_iinf_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3813{
3814 int64_t iinf_pos = avio_tell(pb);
3815 avio_wb32(pb, 0); /* size */
3816 ffio_wfourcc(pb, "iinf");
3817 avio_wb32(pb, 0); /* Version & flags */
3818 avio_wb16(pb, mov->nb_streams); /* entry_count */
3819
3820 av_assert2(mov->nb_streams <= 2);
3821 for (int i = 0; i < mov->nb_streams; i++) {
3822 int64_t infe_pos = avio_tell(pb);
3823 avio_wb32(pb, 0); /* size */
3824 ffio_wfourcc(pb, "infe");
3825 avio_w8(pb, 0x2); /* Version */
3826 avio_wb24(pb, !!i); /* flags (0x1 == hidden) */
3827 avio_wb16(pb, i + 1); /* item_id */
3828 avio_wb16(pb, 0); /* item_protection_index */
3829 avio_write(pb, "av01", 4); /* item_type */
3830 avio_write(pb, !i ? "Color\0" : "Alpha\0", 6); /* item_name */
3831 update_size(pb, infe_pos);
3832 }
3833
3834 return update_size(pb, iinf_pos);
3835}
3836
3837
3838static int mov_write_iref_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3839{
3840 int64_t auxl_pos;
3841 int64_t iref_pos = avio_tell(pb);
3842 avio_wb32(pb, 0); /* size */
3843 ffio_wfourcc(pb, "iref");
3844 avio_wb32(pb, 0); /* Version & flags */
3845
3846 auxl_pos = avio_tell(pb);
3847 avio_wb32(pb, 0); /* size */
3848 ffio_wfourcc(pb, "auxl");
3849 avio_wb16(pb, 2); /* from_item_ID */
3850 avio_wb16(pb, 1); /* reference_count */
3851 avio_wb16(pb, 1); /* to_item_ID */
3852 update_size(pb, auxl_pos);
3853
3854 return update_size(pb, iref_pos);
3855}
3856
3857static int mov_write_ispe_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s,
3858 int stream_index)
3859{
3860 int64_t pos = avio_tell(pb);
3861 avio_wb32(pb, 0); /* size */
3862 ffio_wfourcc(pb, "ispe");
3863 avio_wb32(pb, 0); /* Version & flags */
3864 avio_wb32(pb, s->streams[stream_index]->codecpar->width); /* image_width */
3865 avio_wb32(pb, s->streams[stream_index]->codecpar->height); /* image_height */
3866 return update_size(pb, pos);
3867}
3868
3869static int mov_write_pixi_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s,
3870 int stream_index)
3871{
3872 MOVTrack *trk = &mov->tracks[stream_index];
3873 int64_t pos = avio_tell(pb);
3874 const AVPixFmtDescriptor *pixdesc =
3875 av_pix_fmt_desc_get(s->streams[stream_index]->codecpar->format);
3876 int depth, nb_components;
3877 if (pixdesc->nb_components > 0) {
3878 depth = pixdesc->comp[0].depth;
3879 nb_components = pixdesc->nb_components;
3880 } else {
3883 trk->extradata_size[trk->last_stsd_index]);
3884 if (ret < 0)
3885 return ret;
3886 depth = seq.bitdepth;
3887 nb_components = seq.monochrome ? 1 : 3;
3888 }
3889 avio_wb32(pb, 0); /* size */
3890 ffio_wfourcc(pb, "pixi");
3891 avio_wb32(pb, 0); /* Version & flags */
3892 avio_w8(pb, nb_components); /* num_channels */
3893 for (int i = 0; i < nb_components; ++i)
3894 avio_w8(pb, depth); /* bits_per_channel */
3895 return update_size(pb, pos);
3896}
3897
3898static int mov_write_ipco_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3899{
3900 int64_t pos = avio_tell(pb);
3901 avio_wb32(pb, 0); /* size */
3902 ffio_wfourcc(pb, "ipco");
3903 for (int i = 0; i < mov->nb_streams; i++) {
3904 mov_write_ispe_tag(pb, mov, s, i);
3905 mov_write_pixi_tag(pb, mov, s, i);
3906 mov_write_av1c_tag(pb, &mov->tracks[i]);
3907 if (!i)
3908 mov_write_colr_tag(pb, &mov->tracks[0], 0);
3909 else
3910 mov_write_aux_tag(pb, "auxC");
3911 }
3912 return update_size(pb, pos);
3913}
3914
3915static int mov_write_ipma_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3916{
3917 int64_t pos = avio_tell(pb);
3918 avio_wb32(pb, 0); /* size */
3919 ffio_wfourcc(pb, "ipma");
3920 avio_wb32(pb, 0); /* Version & flags */
3921 avio_wb32(pb, mov->nb_streams); /* entry_count */
3922
3923 for (int i = 0, index = 1; i < mov->nb_streams; i++) {
3924 avio_wb16(pb, i + 1); /* item_ID */
3925 avio_w8(pb, 4); /* association_count */
3926
3927 // ispe association.
3928 avio_w8(pb, index++); /* essential and property_index */
3929 // pixi association.
3930 avio_w8(pb, index++); /* essential and property_index */
3931 // av1C association.
3932 avio_w8(pb, 0x80 | index++); /* essential and property_index */
3933 // colr/auxC association.
3934 avio_w8(pb, index++); /* essential and property_index */
3935 }
3936 return update_size(pb, pos);
3937}
3938
3939static int mov_write_iprp_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
3940{
3941 int64_t pos = avio_tell(pb);
3942 avio_wb32(pb, 0); /* size */
3943 ffio_wfourcc(pb, "iprp");
3944 mov_write_ipco_tag(pb, mov, s);
3945 mov_write_ipma_tag(pb, mov, s);
3946 return update_size(pb, pos);
3947}
3948#endif
3949
3951{
3952 /* This atom must be present, but leaving the values at zero
3953 * seems harmless. */
3954 avio_wb32(pb, 28); /* size */
3955 ffio_wfourcc(pb, "hmhd");
3956 avio_wb32(pb, 0); /* version, flags */
3957 avio_wb16(pb, 0); /* maxPDUsize */
3958 avio_wb16(pb, 0); /* avgPDUsize */
3959 avio_wb32(pb, 0); /* maxbitrate */
3960 avio_wb32(pb, 0); /* avgbitrate */
3961 avio_wb32(pb, 0); /* reserved */
3962 return 28;
3963}
3964
3966{
3967 int64_t pos = avio_tell(pb);
3968 int ret;
3969
3970 avio_wb32(pb, 0); /* size */
3971 ffio_wfourcc(pb, "minf");
3972 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
3973 mov_write_vmhd_tag(pb, track);
3974 else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
3976 else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3977 if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
3978 mov_write_gmhd_tag(pb, track);
3979 } else if (track->tag == MOV_MP4_TTML_TAG) {
3981 } else {
3983 }
3984 } else if (track->tag == MKTAG('r','t','p',' ')) {
3986 } else if (track->tag == MKTAG('t','m','c','d')) {
3987 if (track->mode != MODE_MOV)
3989 else
3990 mov_write_gmhd_tag(pb, track);
3991 } else if (track->tag == MKTAG('g','p','m','d') ||
3992 track->tag == MOV_MP4_DJMD_TAG ||
3993 track->tag == MOV_MP4_DBGI_TAG) {
3994 mov_write_gmhd_tag(pb, track);
3995 }
3996 if (track->mode == MODE_MOV) /* ISO 14496-12 8.4.3.1 specifies hdlr only within mdia or meta boxes */
3999 if ((ret = mov_write_stbl_tag(s, pb, mov, track)) < 0)
4000 return ret;
4001 return update_size(pb, pos);
4002}
4003
4004static void get_pts_range(MOVMuxContext *mov, MOVTrack *track,
4005 int64_t *start, int64_t *end, int elst, int mdhd)
4006{
4007 if (track->tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd && track->nb_src_track) {
4008 // tmcd tracks gets track_duration set in mov_write_moov_tag from
4009 // another track's duration, while the end_pts may be left at zero.
4010 // Calculate the pts duration for that track instead.
4011 get_pts_range(mov, &mov->tracks[*track->src_track], start, end, elst, mdhd);
4012 *start = av_rescale(*start, track->timescale,
4013 mov->tracks[*track->src_track].timescale);
4014 *end = av_rescale(*end, track->timescale,
4015 mov->tracks[*track->src_track].timescale);
4016 return;
4017 }
4018 if (!mdhd && track->end_pts != AV_NOPTS_VALUE &&
4019 track->start_dts != AV_NOPTS_VALUE &&
4020 track->start_cts != AV_NOPTS_VALUE) {
4021 *start = track->start_dts + track->start_cts;
4022 *end = elst ? track->elst_end_pts : track->end_pts;
4023 return;
4024 }
4025 *start = 0;
4026 *end = track->track_duration;
4027}
4028
4030{
4031 int64_t start, end;
4032 get_pts_range(mov, track, &start, &end, 0, 1);
4033 return end - start;
4034}
4035
4036// Calculate the actual duration of the track, after edits.
4037// If it starts with a pts < 0, that is removed by the edit list.
4038// If it starts with a pts > 0, the edit list adds a delay before that.
4039// Thus, with edit lists enabled, the post-edit output of the file is
4040// starting with pts=0.
4042{
4043 int64_t start, end;
4044 get_pts_range(mov, track, &start, &end, 0, 0);
4045 if (mov->use_editlist != 0)
4046 start = 0;
4047 return end - start;
4048}
4049
4051{
4052 int64_t start, end;
4053 get_pts_range(mov, track, &start, &end, 1, 0);
4054 return end - start;
4055}
4056
4058{
4059 if (track && track->mode == MODE_ISM)
4060 return 1;
4061 if (duration < INT32_MAX)
4062 return 0;
4063 return 1;
4064}
4065
4067 MOVTrack *track)
4068{
4070 int version = mov_mdhd_mvhd_tkhd_version(mov, track, duration);
4071
4072 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
4073 ffio_wfourcc(pb, "mdhd");
4074 avio_w8(pb, version);
4075 avio_wb24(pb, 0); /* flags */
4076 if (version == 1) {
4077 avio_wb64(pb, track->time);
4078 avio_wb64(pb, track->time);
4079 } else {
4080 avio_wb32(pb, track->time); /* creation time */
4081 avio_wb32(pb, track->time); /* modification time */
4082 }
4083 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
4084 if (!track->entry && mov->mode == MODE_ISM)
4085 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
4086 else if (!track->entry)
4087 (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
4088 else
4089 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration); /* duration */
4090 avio_wb16(pb, track->language); /* language */
4091 avio_wb16(pb, 0); /* reserved (quality) */
4092
4093 if (version != 0 && track->mode == MODE_MOV) {
4095 "FATAL error, file duration too long for timebase, this file will not be\n"
4096 "playable with QuickTime. Choose a different timebase with "
4097 "-video_track_timescale or a different container format\n");
4098 }
4099
4100 return 32;
4101}
4102
4104 MOVMuxContext *mov, MOVTrack *track)
4105{
4106 int64_t pos = avio_tell(pb);
4107 int ret;
4108
4109 avio_wb32(pb, 0); /* size */
4110 ffio_wfourcc(pb, "mdia");
4111 mov_write_mdhd_tag(pb, mov, track);
4112 mov_write_hdlr_tag(s, pb, track);
4113 if ((ret = mov_write_minf_tag(s, pb, mov, track)) < 0)
4114 return ret;
4115 return update_size(pb, pos);
4116}
4117
4118/* transformation matrix
4119 |a b u|
4120 |c d v|
4121 |tx ty w| */
4122static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
4123 int16_t d, int16_t tx, int16_t ty)
4124{
4125 avio_wb32(pb, a << 16); /* 16.16 format */
4126 avio_wb32(pb, b << 16); /* 16.16 format */
4127 avio_wb32(pb, 0); /* u in 2.30 format */
4128 avio_wb32(pb, c << 16); /* 16.16 format */
4129 avio_wb32(pb, d << 16); /* 16.16 format */
4130 avio_wb32(pb, 0); /* v in 2.30 format */
4131 avio_wb32(pb, tx << 16); /* 16.16 format */
4132 avio_wb32(pb, ty << 16); /* 16.16 format */
4133 avio_wb32(pb, 1 << 30); /* w in 2.30 format */
4134}
4135
4137 MOVTrack *track, AVStream *st)
4138{
4140 mov->movie_timescale, track->timescale,
4141 AV_ROUND_UP);
4142 int version;
4144 int group = 0;
4145
4146 uint32_t *display_matrix = NULL;
4147 int i;
4148
4149#if CONFIG_AVIF_MUXER
4150 if (mov->mode == MODE_AVIF)
4151 if (!mov->avif_loop_count)
4152 duration = INT64_MAX;
4153 else
4154 duration *= mov->avif_loop_count;
4155#endif
4156
4157 if (st) {
4158 const AVPacketSideData *sd;
4159 if (mov->per_stream_grouping)
4160 group = st->index;
4161 else
4162 group = st->codecpar->codec_type;
4163
4167 if (sd && sd->size == 9 * sizeof(*display_matrix))
4168 display_matrix = (uint32_t *)sd->data;
4169 }
4170
4171 if (track->flags & MOV_TRACK_ENABLED)
4173
4175
4176 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
4177 ffio_wfourcc(pb, "tkhd");
4178 avio_w8(pb, version);
4179 avio_wb24(pb, flags);
4180 if (version == 1) {
4181 avio_wb64(pb, track->time);
4182 avio_wb64(pb, track->time);
4183 } else {
4184 avio_wb32(pb, track->time); /* creation time */
4185 avio_wb32(pb, track->time); /* modification time */
4186 }
4187 avio_wb32(pb, track->track_id); /* track-id */
4188 avio_wb32(pb, 0); /* reserved */
4189 if (!track->entry && mov->mode == MODE_ISM)
4190 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
4191 else if (!track->entry)
4192 (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
4193 else
4194 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
4195
4196 avio_wb32(pb, 0); /* reserved */
4197 avio_wb32(pb, 0); /* reserved */
4198 avio_wb16(pb, 0); /* layer */
4199 avio_wb16(pb, group); /* alternate group) */
4200 /* Volume, only for audio */
4201 if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
4202 avio_wb16(pb, 0x0100);
4203 else
4204 avio_wb16(pb, 0);
4205 avio_wb16(pb, 0); /* reserved */
4206
4207 /* Matrix structure */
4208 if (display_matrix) {
4209 for (i = 0; i < 9; i++)
4210 avio_wb32(pb, display_matrix[i]);
4211 } else {
4212 write_matrix(pb, 1, 0, 0, 1, 0, 0);
4213 }
4214 /* Track width and height, for visual only */
4215 if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
4216 track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
4217 int64_t track_width_1616;
4218 if (track->mode == MODE_MOV || track->mode == MODE_AVIF) {
4219 track_width_1616 = track->par->width * 0x10000ULL;
4220 } else {
4221 track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
4222 track->par->width * 0x10000LL,
4224 if (!track_width_1616 ||
4225 track->height != track->par->height ||
4226 track_width_1616 > UINT32_MAX)
4227 track_width_1616 = track->par->width * 0x10000ULL;
4228 }
4229 if (track_width_1616 > UINT32_MAX) {
4230 av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");
4231 track_width_1616 = 0;
4232 }
4233 avio_wb32(pb, track_width_1616);
4234 if (track->height > 0xFFFF) {
4235 av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");
4236 avio_wb32(pb, 0);
4237 } else
4238 avio_wb32(pb, track->height * 0x10000U);
4239 } else {
4240 avio_wb32(pb, 0);
4241 avio_wb32(pb, 0);
4242 }
4243 return 0x5c;
4244}
4245
4247{
4249 track->par->sample_aspect_ratio.den);
4250
4251 int64_t pos = avio_tell(pb);
4252
4253 avio_wb32(pb, 0); /* size */
4254 ffio_wfourcc(pb, "tapt");
4255
4256 avio_wb32(pb, 20);
4257 ffio_wfourcc(pb, "clef");
4258 avio_wb32(pb, 0);
4259 avio_wb32(pb, width << 16);
4260 avio_wb32(pb, track->par->height << 16);
4261
4262 avio_wb32(pb, 20);
4263 ffio_wfourcc(pb, "prof");
4264 avio_wb32(pb, 0);
4265 avio_wb32(pb, width << 16);
4266 avio_wb32(pb, track->par->height << 16);
4267
4268 avio_wb32(pb, 20);
4269 ffio_wfourcc(pb, "enof");
4270 avio_wb32(pb, 0);
4271 avio_wb32(pb, track->par->width << 16);
4272 avio_wb32(pb, track->par->height << 16);
4273
4274 return update_size(pb, pos);
4275}
4276
4277// This box is written in the following cases:
4278// * Seems important for the psp playback. Without it the movie seems to hang.
4279// * Used for specifying the looping behavior of animated AVIF (as specified
4280// in Section 9.6 of the HEIF specification ISO/IEC 23008-12).
4282 MOVTrack *track)
4283{
4285 mov->movie_timescale, track->timescale,
4286 AV_ROUND_UP);
4287 int version = duration < INT32_MAX ? 0 : 1;
4288 int entry_size, entry_count, size;
4289 int64_t delay, start_ct = track->start_cts;
4290 int64_t start_dts = track->start_dts;
4291 int flags = 0;
4292
4293 if (track->entry) {
4294 if (start_dts != track->cluster[0].dts || (start_ct != track->cluster[0].cts && track->cluster[0].dts >= 0)) {
4295
4296 av_log(mov->fc, AV_LOG_DEBUG,
4297 "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
4298 track->cluster[0].dts, track->cluster[0].cts,
4299 start_dts, start_ct, track->track_id);
4300 start_dts = track->cluster[0].dts;
4301 start_ct = track->cluster[0].cts;
4302 }
4303 }
4304
4305 delay = av_rescale_rnd(start_dts + start_ct, mov->movie_timescale,
4306 track->timescale, AV_ROUND_DOWN);
4307
4308#if CONFIG_AVIF_MUXER
4309 if (mov->mode == MODE_AVIF) {
4310 delay = 0;
4311 // Section 9.6.3 of ISO/IEC 23008-12: flags specifies repetition of the
4312 // edit list as follows: (flags & 1) equal to 0 specifies that the edit
4313 // list is not repeated, while (flags & 1) equal to 1 specifies that the
4314 // edit list is repeated.
4315 flags = mov->avif_loop_count != 1;
4316 start_ct = 0;
4317 }
4318#endif
4319
4320 version |= delay < INT32_MAX ? 0 : 1;
4321
4322 entry_size = (version == 1) ? 20 : 12;
4323 entry_count = 1 + (delay > 0);
4324 size = 24 + entry_count * entry_size;
4325
4326 /* write the atom data */
4327 avio_wb32(pb, size);
4328 ffio_wfourcc(pb, "edts");
4329 avio_wb32(pb, size - 8);
4330 ffio_wfourcc(pb, "elst");
4331 avio_w8(pb, version);
4332 avio_wb24(pb, flags); /* flags */
4333
4334 avio_wb32(pb, entry_count);
4335 if (delay > 0) { /* add an empty edit to delay presentation */
4336 /* In the positive delay case, the delay includes the cts
4337 * offset, and the second edit list entry below trims out
4338 * the same amount from the actual content. This makes sure
4339 * that the offset last sample is included in the edit
4340 * list duration as well. */
4341 if (version == 1) {
4342 avio_wb64(pb, delay);
4343 avio_wb64(pb, -1);
4344 } else {
4345 avio_wb32(pb, delay);
4346 avio_wb32(pb, -1);
4347 }
4348 avio_wb32(pb, 0x00010000);
4349 } else if (mov->mode != MODE_AVIF) {
4350 /* Avoid accidentally ending up with start_ct = -1 which has got a
4351 * special meaning. Normally start_ct should end up positive or zero
4352 * here, but use FFMIN in case dts is a small positive integer
4353 * rounded to 0 when represented in movie timescale units. */
4354 av_assert0(av_rescale_rnd(start_dts, mov->movie_timescale, track->timescale, AV_ROUND_DOWN) <= 0);
4355 start_ct = -FFMIN(start_dts, 0);
4356
4357#if CONFIG_IAMFENC
4358 if (track->iamf && track->par->codec_id == AV_CODEC_ID_OPUS)
4359 start_ct = av_rescale(start_ct, 48000, track->par->sample_rate);
4360#endif
4361 /* Note, this delay is calculated from the pts of the first sample,
4362 * ensuring that we don't reduce the duration for cases with
4363 * dts<0 pts=0. */
4364 duration += delay;
4365 }
4366
4367 /* For fragmented files, we don't know the full length yet. Setting
4368 * duration to 0 allows us to only specify the offset, including
4369 * the rest of the content (from all future fragments) without specifying
4370 * an explicit duration.
4371 *
4372 * For hybrid_fragmented during mov_write_trailer (mov->moov_written != 0),
4373 * don't reset duration to zero.
4374 */
4375 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
4377 duration = 0;
4378
4379 /* duration */
4380 if (version == 1) {
4381 avio_wb64(pb, duration);
4382 avio_wb64(pb, start_ct);
4383 } else {
4384 avio_wb32(pb, duration);
4385 avio_wb32(pb, start_ct);
4386 }
4387 avio_wb32(pb, 0x00010000);
4388 return size;
4389}
4390
4392{
4393 int64_t pos = avio_tell(pb);
4394 avio_wb32(pb, 0); // size placeholder
4395 ffio_wfourcc(pb, "tref");
4396
4397 for (int i = 0; i < track->nb_tref_tags; i++) {
4398 MovTag *tag = &track->tref_tags[i];
4399 avio_wb32(pb, 8 + tag->nb_id * sizeof(*tag->id)); // size (subatom)
4400 avio_wl32(pb, tag->name);
4401 for (int j = 0; j < tag->nb_id; j++)
4402 avio_wb32(pb, tag->id[j]);
4403 }
4404 return update_size(pb, pos);
4405}
4406
4407// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
4409{
4410 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
4411 ffio_wfourcc(pb, "uuid");
4412 ffio_wfourcc(pb, "USMT");
4413 avio_wb32(pb, 0x21d24fce);
4414 avio_wb32(pb, 0xbb88695c);
4415 avio_wb32(pb, 0xfac9c740);
4416 avio_wb32(pb, 0x1c); // another size here!
4417 ffio_wfourcc(pb, "MTDT");
4418 avio_wb32(pb, 0x00010012);
4419 avio_wb32(pb, 0x0a);
4420 avio_wb32(pb, 0x55c40000);
4421 avio_wb32(pb, 0x1);
4422 avio_wb32(pb, 0x0);
4423 return 0x34;
4424}
4425
4427{
4428 AVFormatContext *ctx = track->rtp_ctx;
4429 char buf[1000] = "";
4430 int len;
4431
4432 av_assert0(track->nb_src_track);
4433 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], *track->src_track,
4434 NULL, NULL, 0, 0, ctx);
4435 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
4436 len = strlen(buf);
4437
4438 avio_wb32(pb, len + 24);
4439 ffio_wfourcc(pb, "udta");
4440 avio_wb32(pb, len + 16);
4441 ffio_wfourcc(pb, "hnti");
4442 avio_wb32(pb, len + 8);
4443 ffio_wfourcc(pb, "sdp ");
4444 avio_write(pb, buf, len);
4445 return len + 24;
4446}
4447
4449 const char *tag, const char *str)
4450{
4451 int64_t pos = avio_tell(pb);
4452 AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
4453 if (!t || !utf8len(t->value))
4454 return 0;
4455
4456 avio_wb32(pb, 0); /* size */
4457 ffio_wfourcc(pb, tag); /* type */
4458 avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
4459 return update_size(pb, pos);
4460}
4461
4462static int mov_write_track_kind(AVIOContext *pb, const char *scheme_uri,
4463 const char *value)
4464{
4465 int64_t pos = avio_tell(pb);
4466
4467 /* Box|FullBox basics */
4468 avio_wb32(pb, 0); /* size placeholder */
4469 ffio_wfourcc(pb, (const unsigned char *)"kind");
4470 avio_w8(pb, 0); /* version = 0 */
4471 avio_wb24(pb, 0); /* flags = 0 */
4472
4473 /* Required null-terminated scheme URI */
4474 avio_write(pb, (const unsigned char *)scheme_uri,
4475 strlen(scheme_uri));
4476 avio_w8(pb, 0);
4477
4478 /* Optional value string */
4479 if (value && value[0])
4480 avio_write(pb, (const unsigned char *)value,
4481 strlen(value));
4482
4483 avio_w8(pb, 0);
4484
4485 return update_size(pb, pos);
4486}
4487
4489{
4490 int ret = AVERROR_BUG;
4491
4492 for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
4494
4495 for (int j = 0; map.value_maps[j].disposition; j++) {
4496 const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
4497 if (!(st->disposition & value_map.disposition))
4498 continue;
4499
4500 if ((ret = mov_write_track_kind(pb, map.scheme_uri, value_map.value)) < 0)
4501 return ret;
4502 }
4503 }
4504
4505 return 0;
4506}
4507
4509 AVStream *st)
4510{
4511 AVIOContext *pb_buf;
4512 int ret, size;
4513 uint8_t *buf;
4514
4515 if (!st)
4516 return 0;
4517
4518 ret = avio_open_dyn_buf(&pb_buf);
4519 if (ret < 0)
4520 return ret;
4521
4522 if (mov->mode & (MODE_MP4|MODE_MOV))
4523 mov_write_track_metadata(pb_buf, st, "name", "title");
4524
4525 if (mov->mode & MODE_MP4) {
4526 if ((ret = mov_write_track_kinds(pb_buf, st)) < 0)
4527 goto end;
4528 }
4529
4530 if ((size = avio_get_dyn_buf(pb_buf, &buf)) > 0) {
4531 avio_wb32(pb, size + 8);
4532 ffio_wfourcc(pb, "udta");
4533 avio_write(pb, buf, size);
4534 }
4535end:
4536 ffio_free_dyn_buf(&pb_buf);
4537
4538 return ret;
4539}
4540
4542 MOVTrack *track, AVStream *st)
4543{
4544 int64_t pos = avio_tell(pb);
4545 int entry_backup = track->entry;
4546 int chunk_backup = track->chunkCount;
4547 int ret;
4548
4549 /* If we want to have an empty moov, but some samples already have been
4550 * buffered (delay_moov), pretend that no samples have been written yet. */
4551 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
4552 track->chunkCount = track->entry = 0;
4553
4554 avio_wb32(pb, 0); /* size */
4555 ffio_wfourcc(pb, "trak");
4556 mov_write_tkhd_tag(pb, mov, track, st);
4557
4558 av_assert2(mov->use_editlist >= 0);
4559
4560 if (track->start_dts != AV_NOPTS_VALUE) {
4561 if (mov->use_editlist)
4562 mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
4563 else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
4564 av_log(mov->fc, AV_LOG_WARNING,
4565 "Not writing any edit list even though one would have been required\n");
4566 }
4567
4568 if (mov->is_animated_avif)
4569 mov_write_edts_tag(pb, mov, track);
4570
4571 if (track->nb_tref_tags)
4572 mov_write_tref_tag(pb, track);
4573
4574 if ((ret = mov_write_mdia_tag(s, pb, mov, track)) < 0)
4575 return ret;
4576 if (track->mode == MODE_PSP)
4577 mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
4578 if (track->tag == MKTAG('r','t','p',' '))
4579 mov_write_udta_sdp(pb, track);
4580 if (track->mode == MODE_MOV) {
4581 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
4582 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
4583 if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
4584 mov_write_tapt_tag(pb, track);
4585 }
4586 }
4587 if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
4588 mov_write_tapt_tag(pb, track);
4589 }
4590 }
4591 mov_write_track_udta_tag(pb, mov, st);
4592 track->entry = entry_backup;
4593 track->chunkCount = chunk_backup;
4594 return update_size(pb, pos);
4595}
4596
4598{
4599 int i, has_audio = 0, has_video = 0;
4600 int64_t pos = avio_tell(pb);
4601 int audio_profile = mov->iods_audio_profile;
4602 int video_profile = mov->iods_video_profile;
4603 for (i = 0; i < mov->nb_tracks; i++) {
4604 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4605 has_audio |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_AUDIO;
4606 has_video |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_VIDEO;
4607 }
4608 }
4609 if (audio_profile < 0)
4610 audio_profile = 0xFF - has_audio;
4611 if (video_profile < 0)
4612 video_profile = 0xFF - has_video;
4613 avio_wb32(pb, 0x0); /* size */
4614 ffio_wfourcc(pb, "iods");
4615 avio_wb32(pb, 0); /* version & flags */
4616 put_descr(pb, 0x10, 7);
4617 avio_wb16(pb, 0x004f);
4618 avio_w8(pb, 0xff);
4619 avio_w8(pb, 0xff);
4620 avio_w8(pb, audio_profile);
4621 avio_w8(pb, video_profile);
4622 avio_w8(pb, 0xff);
4623 return update_size(pb, pos);
4624}
4625
4627{
4628 avio_wb32(pb, 0x20); /* size */
4629 ffio_wfourcc(pb, "trex");
4630 avio_wb32(pb, 0); /* version & flags */
4631 avio_wb32(pb, track->track_id); /* track ID */
4632 avio_wb32(pb, 1); /* default sample description index */
4633 avio_wb32(pb, 0); /* default sample duration */
4634 avio_wb32(pb, 0); /* default sample size */
4635 avio_wb32(pb, 0); /* default sample flags */
4636 return 0;
4637}
4638
4640{
4641 int64_t pos = avio_tell(pb);
4642 int i;
4643 avio_wb32(pb, 0x0); /* size */
4644 ffio_wfourcc(pb, "mvex");
4645 for (i = 0; i < mov->nb_tracks; i++)
4646 mov_write_trex_tag(pb, &mov->tracks[i]);
4647 return update_size(pb, pos);
4648}
4649
4651{
4652 int max_track_id = 1, i;
4653 int64_t max_track_len = 0;
4654 int version;
4655 int timescale;
4656
4657 for (i = 0; i < mov->nb_tracks; i++) {
4658 if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
4659 int64_t max_track_len_temp = av_rescale_rnd(
4660 calc_pts_duration(mov, &mov->tracks[i]),
4661 mov->movie_timescale,
4662 mov->tracks[i].timescale,
4663 AV_ROUND_UP);
4664 if (max_track_len < max_track_len_temp)
4665 max_track_len = max_track_len_temp;
4666 if (max_track_id < mov->tracks[i].track_id)
4667 max_track_id = mov->tracks[i].track_id;
4668 }
4669 }
4670 /* If using delay_moov, make sure the output is the same as if no
4671 * samples had been written yet. */
4672 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
4673 max_track_len = 0;
4674 max_track_id = 1;
4675 }
4676
4677 version = mov_mdhd_mvhd_tkhd_version(mov, NULL, max_track_len);
4678 avio_wb32(pb, version == 1 ? 120 : 108); /* size */
4679
4680 ffio_wfourcc(pb, "mvhd");
4681 avio_w8(pb, version);
4682 avio_wb24(pb, 0); /* flags */
4683 if (version == 1) {
4684 avio_wb64(pb, mov->time);
4685 avio_wb64(pb, mov->time);
4686 } else {
4687 avio_wb32(pb, mov->time); /* creation time */
4688 avio_wb32(pb, mov->time); /* modification time */
4689 }
4690
4691 timescale = mov->movie_timescale;
4692 if (mov->mode == MODE_AVIF && !timescale)
4693 timescale = mov->tracks[0].timescale;
4694
4695 avio_wb32(pb, timescale);
4696 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
4697
4698 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
4699 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
4700 ffio_fill(pb, 0, 2 + 2 * 4); /* reserved */
4701
4702 /* Matrix structure */
4703 write_matrix(pb, 1, 0, 0, 1, 0, 0);
4704
4705 avio_wb32(pb, 0); /* reserved (preview time) */
4706 avio_wb32(pb, 0); /* reserved (preview duration) */
4707 avio_wb32(pb, 0); /* reserved (poster time) */
4708 avio_wb32(pb, 0); /* reserved (selection time) */
4709 avio_wb32(pb, 0); /* reserved (selection duration) */
4710 avio_wb32(pb, 0); /* reserved (current time) */
4711 avio_wb32(pb, max_track_id + 1); /* Next track id */
4712 return 0x6c;
4713}
4714
4717{
4718 avio_wb32(pb, 33); /* size */
4719 ffio_wfourcc(pb, "hdlr");
4720 avio_wb32(pb, 0);
4721 avio_wb32(pb, 0);
4722 ffio_wfourcc(pb, "mdir");
4723 ffio_wfourcc(pb, "appl");
4724 avio_wb32(pb, 0);
4725 avio_wb32(pb, 0);
4726 avio_w8(pb, 0);
4727 return 33;
4728}
4729
4730/* helper function to write a data tag with the specified string as data */
4731static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
4732{
4733 size_t data_len = strlen(data);
4734 if (long_style) {
4735 int size = 16 + data_len;
4736 avio_wb32(pb, size); /* size */
4737 ffio_wfourcc(pb, "data");
4738 avio_wb32(pb, 1);
4739 avio_wb32(pb, 0);
4740 avio_write(pb, data, data_len);
4741 return size;
4742 } else {
4743 avio_wb16(pb, data_len); /* string length */
4744 if (!lang)
4745 lang = ff_mov_iso639_to_lang("und", 1);
4746 avio_wb16(pb, lang);
4747 avio_write(pb, data, data_len);
4748 return data_len + 4;
4749 }
4750}
4751
4752static int mov_write_string_tag(AVIOContext *pb, const char *name,
4753 const char *value, int lang, int long_style)
4754{
4755 int size = 0;
4756 if (value && value[0]) {
4757 int64_t pos = avio_tell(pb);
4758 avio_wb32(pb, 0); /* size */
4759 ffio_wfourcc(pb, name);
4760 mov_write_string_data_tag(pb, value, lang, long_style);
4761 size = update_size(pb, pos);
4762 }
4763 return size;
4764}
4765
4766static int mov_write_freeform_tag(AVIOContext *pb, const char *mean,
4767 const char *name, const char *data)
4768{
4769 if (!data || !data[0])
4770 return 0;
4771
4772 static const char stub_flags[4] = {0, 0, 0, 0};
4773
4774 int64_t entry_pos = avio_tell(pb);
4775 avio_wb32(pb, 0); /* size */
4776 ffio_wfourcc(pb, "----"); /* freeform */
4777
4778 size_t mean_len = strlen(mean);
4779 avio_wb32(pb, 12 + mean_len);
4780 ffio_wfourcc(pb, "mean");
4781 avio_write(pb, &stub_flags[0], sizeof(stub_flags));
4782 avio_write(pb, mean, mean_len);
4783
4784 size_t name_len = strlen(name);
4785 avio_wb32(pb, 12 + name_len);
4786 ffio_wfourcc(pb, "name");
4787 avio_write(pb, &stub_flags[0], sizeof(stub_flags));
4788 avio_write(pb, name, name_len);
4789
4791
4792 return update_size(pb, entry_pos);
4793}
4794
4796 const char *tag, int *lang)
4797{
4798 int l, len, len2;
4799 AVDictionaryEntry *t, *t2 = NULL;
4800 char tag2[16];
4801
4802 *lang = 0;
4803
4804 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
4805 return NULL;
4806
4807 len = strlen(t->key);
4808 snprintf(tag2, sizeof(tag2), "%s-", tag);
4809 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
4810 len2 = strlen(t2->key);
4811 if (len2 == len + 4 && !strcmp(t->value, t2->value)
4812 && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
4813 *lang = l;
4814 return t;
4815 }
4816 }
4817 return t;
4818}
4819
4821 const char *name, const char *tag,
4822 int long_style)
4823{
4824 int lang;
4826 if (!t)
4827 return 0;
4828 return mov_write_string_tag(pb, name, t->value, lang, long_style);
4829}
4830
4832 const char *mean, const char *name,
4833 const char *tag)
4834{
4835 AVDictionaryEntry *t = av_dict_get(s->metadata, tag, NULL, 0);
4836 if (!t)
4837 return 0;
4838 return mov_write_freeform_tag(pb, mean, name, t->value);
4839}
4840
4841/* iTunes bpm number */
4843{
4844 AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
4845 int size = 0, tmpo = t ? atoi(t->value) : 0;
4846 if (tmpo) {
4847 size = 26;
4848 avio_wb32(pb, size);
4849 ffio_wfourcc(pb, "tmpo");
4850 avio_wb32(pb, size-8); /* size */
4851 ffio_wfourcc(pb, "data");
4852 avio_wb32(pb, 0x15); //type specifier
4853 avio_wb32(pb, 0);
4854 avio_wb16(pb, tmpo); // data
4855 }
4856 return size;
4857}
4858
4859/* 3GPP TS 26.244 */
4861{
4862 int lang;
4863 int64_t pos = avio_tell(pb);
4864 double latitude, longitude, altitude;
4865 int32_t latitude_fix, longitude_fix, altitude_fix;
4866 AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
4867 const char *ptr, *place = "";
4868 char *end;
4869 static const char *astronomical_body = "earth";
4870 if (!t)
4871 return 0;
4872
4873 ptr = t->value;
4874 latitude = strtod(ptr, &end);
4875 if (end == ptr) {
4876 av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
4877 return 0;
4878 }
4879 ptr = end;
4880 longitude = strtod(ptr, &end);
4881 if (end == ptr) {
4882 av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
4883 return 0;
4884 }
4885 ptr = end;
4886 altitude = strtod(ptr, &end);
4887 /* If no altitude was present, the default 0 should be fine */
4888 if (*end == '/')
4889 place = end + 1;
4890
4891 latitude_fix = (int32_t) ((1 << 16) * latitude);
4892 longitude_fix = (int32_t) ((1 << 16) * longitude);
4893 altitude_fix = (int32_t) ((1 << 16) * altitude);
4894
4895 avio_wb32(pb, 0); /* size */
4896 ffio_wfourcc(pb, "loci"); /* type */
4897 avio_wb32(pb, 0); /* version + flags */
4898 avio_wb16(pb, lang);
4899 avio_write(pb, place, strlen(place) + 1);
4900 avio_w8(pb, 0); /* role of place (0 == shooting location, 1 == real location, 2 == fictional location) */
4901 avio_wb32(pb, longitude_fix);
4902 avio_wb32(pb, latitude_fix);
4903 avio_wb32(pb, altitude_fix);
4904 avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
4905 avio_w8(pb, 0); /* additional notes, null terminated string */
4906
4907 return update_size(pb, pos);
4908}
4909
4910/* iTunes track or disc number */
4912 AVFormatContext *s, int disc)
4913{
4914 AVDictionaryEntry *t = av_dict_get(s->metadata,
4915 disc ? "disc" : "track",
4916 NULL, 0);
4917 int size = 0, track = t ? atoi(t->value) : 0;
4918 if (track) {
4919 int tracks = 0;
4920 char *slash = strchr(t->value, '/');
4921 if (slash)
4922 tracks = atoi(slash + 1);
4923 avio_wb32(pb, 32); /* size */
4924 ffio_wfourcc(pb, disc ? "disk" : "trkn");
4925 avio_wb32(pb, 24); /* size */
4926 ffio_wfourcc(pb, "data");
4927 avio_wb32(pb, 0); // 8 bytes empty
4928 avio_wb32(pb, 0);
4929 avio_wb16(pb, 0); // empty
4930 avio_wb16(pb, track); // track / disc number
4931 avio_wb16(pb, tracks); // total track / disc number
4932 avio_wb16(pb, 0); // empty
4933 size = 32;
4934 }
4935 return size;
4936}
4937
4939 const char *name, const char *tag,
4940 int len)
4941{
4943 uint8_t num;
4944 int size = 24 + len;
4945
4946 if (len != 1 && len != 4)
4947 return -1;
4948
4949 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
4950 return 0;
4951 num = atoi(t->value);
4952
4953 avio_wb32(pb, size);
4954 ffio_wfourcc(pb, name);
4955 avio_wb32(pb, size - 8);
4956 ffio_wfourcc(pb, "data");
4957 avio_wb32(pb, 0x15);
4958 avio_wb32(pb, 0);
4959 if (len==4) avio_wb32(pb, num);
4960 else avio_w8 (pb, num);
4961
4962 return size;
4963}
4964
4966{
4967 MOVMuxContext *mov = s->priv_data;
4968 int64_t pos = 0;
4969
4970 for (int i = 0; i < mov->nb_streams; i++) {
4971 MOVTrack *trk = &mov->tracks[i];
4972
4973 if (!is_cover_image(trk->st) || trk->cover_image->size <= 0)
4974 continue;
4975
4976 if (!pos) {
4977 pos = avio_tell(pb);
4978 avio_wb32(pb, 0);
4979 ffio_wfourcc(pb, "covr");
4980 }
4981 avio_wb32(pb, 16 + trk->cover_image->size);
4982 ffio_wfourcc(pb, "data");
4983 avio_wb32(pb, trk->tag);
4984 avio_wb32(pb , 0);
4985 avio_write(pb, trk->cover_image->data, trk->cover_image->size);
4986 }
4987
4988 return pos ? update_size(pb, pos) : 0;
4989}
4990
4991/* iTunes meta data list */
4994{
4995 int64_t pos = avio_tell(pb);
4996 avio_wb32(pb, 0); /* size */
4997 ffio_wfourcc(pb, "ilst");
4998 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
4999 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
5000 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
5001 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
5002 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
5003 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
5004 if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
5005 if (!(s->flags & AVFMT_FLAG_BITEXACT))
5006 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
5007 }
5008 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
5009 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
5010 mov_write_string_metadata(s, pb, "cprt", "copyright", 1);
5011 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
5012 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
5013 mov_write_string_metadata(s, pb, "desc", "description",1);
5014 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
5015 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
5016 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
5017 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
5018 mov_write_string_metadata(s, pb, "keyw", "keywords" , 1);
5019 mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
5020 mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
5021 mov_write_int8_metadata (s, pb, "stik", "media_type",1);
5022 mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
5023 mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
5024 mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
5025 mov_write_covr(pb, s);
5026 mov_write_trkn_tag(pb, mov, s, 0); // track number
5027 mov_write_trkn_tag(pb, mov, s, 1); // disc number
5028 mov_write_tmpo_tag(pb, s);
5029 mov_write_custom_metadata(s, pb, "com.apple.iTunes", "DISCSUBTITLE", "disc_subtitle");
5030 return update_size(pb, pos);
5031}
5032
5035{
5036 avio_wb32(pb, 33); /* size */
5037 ffio_wfourcc(pb, "hdlr");
5038 avio_wb32(pb, 0);
5039 avio_wb32(pb, 0);
5040 ffio_wfourcc(pb, "mdta");
5041 avio_wb32(pb, 0);
5042 avio_wb32(pb, 0);
5043 avio_wb32(pb, 0);
5044 avio_w8(pb, 0);
5045 return 33;
5046}
5047
5050{
5051 const AVDictionaryEntry *t = NULL;
5052 int64_t pos = avio_tell(pb);
5053 int64_t curpos, entry_pos;
5054 int count = 0;
5055
5056 avio_wb32(pb, 0); /* size */
5057 ffio_wfourcc(pb, "keys");
5058 avio_wb32(pb, 0);
5059 entry_pos = avio_tell(pb);
5060 avio_wb32(pb, 0); /* entry count */
5061
5062 while (t = av_dict_iterate(s->metadata, t)) {
5063 size_t key_len = strlen(t->key);
5064 avio_wb32(pb, key_len + 8);
5065 ffio_wfourcc(pb, "mdta");
5066 avio_write(pb, t->key, key_len);
5067 count += 1;
5068 }
5069 curpos = avio_tell(pb);
5070 avio_seek(pb, entry_pos, SEEK_SET);
5071 avio_wb32(pb, count); // rewrite entry count
5072 avio_seek(pb, curpos, SEEK_SET);
5073
5074 return update_size(pb, pos);
5075}
5076
5079{
5080 const AVDictionaryEntry *t = NULL;
5081 int64_t pos = avio_tell(pb);
5082 int count = 1; /* keys are 1-index based */
5083
5084 avio_wb32(pb, 0); /* size */
5085 ffio_wfourcc(pb, "ilst");
5086
5087 while (t = av_dict_iterate(s->metadata, t)) {
5088 int64_t entry_pos = avio_tell(pb);
5089 avio_wb32(pb, 0); /* size */
5090 avio_wb32(pb, count); /* key */
5091 mov_write_string_data_tag(pb, t->value, 0, 1);
5092 update_size(pb, entry_pos);
5093 count += 1;
5094 }
5095 return update_size(pb, pos);
5096}
5097
5098/* meta data tags */
5101{
5102 int size = 0;
5103 int64_t pos = avio_tell(pb);
5104 avio_wb32(pb, 0); /* size */
5105 ffio_wfourcc(pb, "meta");
5106 avio_wb32(pb, 0);
5107 if (mov->flags & FF_MOV_FLAG_USE_MDTA) {
5108 mov_write_mdta_hdlr_tag(pb, mov, s);
5109 mov_write_mdta_keys_tag(pb, mov, s);
5110 mov_write_mdta_ilst_tag(pb, mov, s);
5111#if CONFIG_AVIF_MUXER
5112 } else if (mov->mode == MODE_AVIF) {
5113 mov_write_hdlr_tag(s, pb, &mov->tracks[0]);
5114 // We always write the primary item id as 1 since only one track is
5115 // supported for AVIF.
5116 mov_write_pitm_tag(pb, 1);
5117 mov_write_iloc_tag(pb, mov, s);
5118 mov_write_iinf_tag(pb, mov, s);
5119 if (mov->nb_streams > 1)
5120 mov_write_iref_tag(pb, mov, s);
5121 mov_write_iprp_tag(pb, mov, s);
5122#endif
5123 } else {
5124 /* iTunes metadata tag */
5125 mov_write_itunes_hdlr_tag(pb, mov, s);
5126 mov_write_ilst_tag(pb, mov, s);
5127 }
5128 size = update_size(pb, pos);
5129 return size;
5130}
5131
5133 const char *name, const char *key)
5134{
5135 int len;
5137
5138 if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
5139 return 0;
5140
5141 len = strlen(t->value);
5142 if (len > 0) {
5143 int size = len + 8;
5144 avio_wb32(pb, size);
5145 ffio_wfourcc(pb, name);
5146 avio_write(pb, t->value, len);
5147 return size;
5148 }
5149 return 0;
5150}
5151
5152static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
5153{
5154 int val;
5155 while (*b) {
5156 GET_UTF8(val, *b++, return -1;)
5157 avio_wb16(pb, val);
5158 }
5159 avio_wb16(pb, 0x00);
5160 return 0;
5161}
5162
5163static uint16_t language_code(const char *str)
5164{
5165 return (((str[0] - 0x60) & 0x1F) << 10) +
5166 (((str[1] - 0x60) & 0x1F) << 5) +
5167 (( str[2] - 0x60) & 0x1F);
5168}
5169
5171 const char *tag, const char *str)
5172{
5173 int64_t pos = avio_tell(pb);
5174 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
5175 if (!t || !utf8len(t->value))
5176 return 0;
5177 avio_wb32(pb, 0); /* size */
5178 ffio_wfourcc(pb, tag); /* type */
5179 avio_wb32(pb, 0); /* version + flags */
5180 if (!strcmp(tag, "yrrc"))
5181 avio_wb16(pb, atoi(t->value));
5182 else {
5183 avio_wb16(pb, language_code("eng")); /* language */
5184 avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
5185 if (!strcmp(tag, "albm") &&
5186 (t = av_dict_get(s->metadata, "track", NULL, 0)))
5187 avio_w8(pb, atoi(t->value));
5188 }
5189 return update_size(pb, pos);
5190}
5191
5193{
5194 int64_t pos = avio_tell(pb);
5195 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
5196
5197 avio_wb32(pb, 0); // size
5198 ffio_wfourcc(pb, "chpl");
5199 avio_wb32(pb, 0x01000000); // version + flags
5200 avio_wb32(pb, 0); // unknown
5201 avio_w8(pb, nb_chapters);
5202
5203 for (i = 0; i < nb_chapters; i++) {
5204 AVChapter *c = s->chapters[i];
5206 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
5207
5208 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
5209 int len = FFMIN(strlen(t->value), 255);
5210 while (len > 252 && (t->value[len] & 0xC0) == 0x80)
5211 len--;
5212 avio_w8(pb, len);
5213 avio_write(pb, t->value, len);
5214 } else
5215 avio_w8(pb, 0);
5216 }
5217 return update_size(pb, pos);
5218}
5219
5222{
5223 AVIOContext *pb_buf;
5224 int ret, size;
5225 uint8_t *buf;
5226
5227 ret = avio_open_dyn_buf(&pb_buf);
5228 if (ret < 0)
5229 return ret;
5230
5231 if (mov->mode & MODE_3GP) {
5232 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
5233 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
5234 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
5235 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
5236 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
5237 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
5238 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
5239 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
5240 mov_write_loci_tag(s, pb_buf);
5241 } 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
5242 mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
5243 mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
5244 mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
5245 mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
5246 /* mov_read_udta_string() already maps this atom back to "composer";
5247 * without it a mov to mov remux dropped the tag on the floor. */
5248 mov_write_string_metadata(s, pb_buf, "\251com", "composer", 0);
5249 mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
5250 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
5251 // currently ignored by mov.c
5252 mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
5253 // add support for libquicktime, this atom is also actually read by mov.c
5254 mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
5255 mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
5256 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
5257 mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
5258 mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
5259 mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
5260 mov_write_string_metadata(s, pb_buf, "\251key", "keywords", 0);
5261 mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
5262 } else {
5263 /* iTunes meta data */
5264 mov_write_meta_tag(pb_buf, mov, s);
5265 mov_write_loci_tag(s, pb_buf);
5266 }
5267
5268 if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
5269 mov_write_chpl_tag(pb_buf, s);
5270
5271 if ((size = avio_get_dyn_buf(pb_buf, &buf)) > 0) {
5272 avio_wb32(pb, size + 8);
5273 ffio_wfourcc(pb, "udta");
5274 avio_write(pb, buf, size);
5275 }
5276 ffio_free_dyn_buf(&pb_buf);
5277
5278 return 0;
5279}
5280
5282 const char *str, const char *lang, int type)
5283{
5284 int len = utf8len(str) + 1;
5285 if (len <= 0)
5286 return;
5287 avio_wb16(pb, len * 2 + 10); /* size */
5288 avio_wb32(pb, type); /* type */
5289 avio_wb16(pb, language_code(lang)); /* language */
5290 avio_wb16(pb, 0x01); /* ? */
5291 ascii_to_wc(pb, str);
5292}
5293
5295{
5296 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
5297 int64_t pos, pos2;
5298
5299 if (title) {
5300 pos = avio_tell(pb);
5301 avio_wb32(pb, 0); /* size placeholder*/
5302 ffio_wfourcc(pb, "uuid");
5303 ffio_wfourcc(pb, "USMT");
5304 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
5305 avio_wb32(pb, 0xbb88695c);
5306 avio_wb32(pb, 0xfac9c740);
5307
5308 pos2 = avio_tell(pb);
5309 avio_wb32(pb, 0); /* size placeholder*/
5310 ffio_wfourcc(pb, "MTDT");
5311 avio_wb16(pb, 4);
5312
5313 // ?
5314 avio_wb16(pb, 0x0C); /* size */
5315 avio_wb32(pb, 0x0B); /* type */
5316 avio_wb16(pb, language_code("und")); /* language */
5317 avio_wb16(pb, 0x0); /* ? */
5318 avio_wb16(pb, 0x021C); /* data */
5319
5320 if (!(s->flags & AVFMT_FLAG_BITEXACT))
5321 mov_write_psp_udta_tag(pb, LIBAVFORMAT_IDENT, "eng", 0x04);
5322 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
5323 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
5324
5325 update_size(pb, pos2);
5326 return update_size(pb, pos);
5327 }
5328
5329 return 0;
5330}
5331
5333{
5338 if (!sd)
5339 return 0;
5340
5342 for (AVEncryptionInitInfo *copy = info; copy; copy = copy->next) {
5343 int64_t pos;
5344
5345 if (!copy->data_size && !copy->num_key_ids)
5346 continue;
5347
5348 pos = avio_tell(pb);
5349 avio_wb32(pb, 0); /* size placeholder */
5350 ffio_wfourcc(pb, "pssh");
5351 avio_w8(pb, 1); /* version */
5352 avio_wb24(pb, 0);
5353 for (int i = 0; i < copy->system_id_size; i++)
5354 avio_w8(pb, copy->system_id[i]);
5355 avio_wb32(pb, copy->num_key_ids);
5356 for (int i = 0; i < copy->num_key_ids; i++)
5357 for (int j = 0; j < copy->key_id_size; j++)
5358 avio_w8(pb, copy->key_ids[i][j]);
5359 avio_wb32(pb, copy->data_size);
5360 avio_write(pb, copy->data, copy->data_size);
5361 update_size(pb, pos);
5362 }
5363
5365
5366 return 0;
5367}
5368
5369static void build_chunks(MOVTrack *trk)
5370{
5371 int i;
5372 MOVIentry *chunk = &trk->cluster[0];
5373 uint64_t chunkSize = chunk->size;
5374 chunk->chunkNum = 1;
5375 if (trk->chunkCount)
5376 return;
5377 trk->chunkCount = 1;
5378 for (i = 1; i<trk->entry; i++){
5379 if (chunk->pos + chunkSize == trk->cluster[i].pos &&
5380 chunk->stsd_index == trk->cluster[i].stsd_index &&
5381 chunkSize + trk->cluster[i].size < (1<<20)){
5382 chunkSize += trk->cluster[i].size;
5383 chunk->samples_in_chunk += trk->cluster[i].entries;
5384 } else {
5385 trk->cluster[i].chunkNum = chunk->chunkNum+1;
5386 chunk=&trk->cluster[i];
5387 chunkSize = chunk->size;
5388 trk->chunkCount++;
5389 }
5390 }
5391}
5392
5393/**
5394 * Assign track ids. If option "use_stream_ids_as_track_ids" is set,
5395 * the stream ids are used as track ids.
5396 *
5397 * This assumes mov->tracks and s->streams are in the same order and
5398 * there are no gaps in either of them (so mov->tracks[n] refers to
5399 * s->streams[n]).
5400 *
5401 * As an exception, there can be more entries in
5402 * s->streams than in mov->tracks, in which case new track ids are
5403 * generated (starting after the largest found stream id).
5404 */
5406{
5407 int i;
5408
5409 if (mov->track_ids_ok)
5410 return 0;
5411
5412 if (mov->use_stream_ids_as_track_ids) {
5413 int next_generated_track_id = 0;
5414 for (i = 0; i < mov->nb_streams; i++) {
5415 AVStream *st = mov->tracks[i].st;
5416 if (st->id > next_generated_track_id)
5417 next_generated_track_id = st->id;
5418 }
5419
5420 for (i = 0; i < mov->nb_tracks; i++) {
5421 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
5422 continue;
5423
5424 mov->tracks[i].track_id = i >= mov->nb_streams ? ++next_generated_track_id : mov->tracks[i].st->id;
5425 }
5426 } else {
5427 int last_track_id = 0;
5428 for (i = 0; i < mov->nb_tracks; i++) {
5429 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
5430 continue;
5431
5432 last_track_id =
5433 mov->tracks[i].track_id = (mov->tracks[i].st
5434 ? FFMAX(mov->tracks[i].st->index, last_track_id)
5435 : FFMAX(i, last_track_id)) + 1;
5436 }
5437 }
5438
5439 mov->track_ids_ok = 1;
5440
5441 return 0;
5442}
5443
5444static int mov_find_tref_id(MOVMuxContext *mov, const MovTag *tag, uint32_t id)
5445{
5446 for (int i = 0; i < tag->nb_id; i++) {
5447 if (tag->id[i] == id)
5448 return 1;
5449 }
5450 return 0;
5451}
5452
5453static int mov_add_tref_id(MOVMuxContext *mov, MovTag *tag, uint32_t id)
5454{
5455 int ret = mov_find_tref_id(mov, tag, id);
5456
5457 if (!ret) {
5458 uint32_t *tmp = av_realloc_array(tag->id, tag->nb_id + 1, sizeof(*tag->id));
5459 if (!tmp)
5460 return AVERROR(ENOMEM);
5461 tag->id = tmp;
5462 tag->id[tag->nb_id++] = id;
5463 }
5464
5465 return 0;
5466}
5467
5468static MovTag *mov_find_tref_tag(MOVMuxContext *mov, const MOVTrack *trk, uint32_t name)
5469{
5470 for (int i = 0; i < trk->nb_tref_tags; i++) {
5471 MovTag *entry = &trk->tref_tags[i];
5472
5473 if (entry->name == name)
5474 return entry;
5475 }
5476 return NULL;
5477}
5478
5480{
5481 MovTag *tag = mov_find_tref_tag(mov, trk, name);
5482
5483 if (!tag) {
5485 sizeof(*trk->tref_tags));
5486 if (!tmp)
5487 return NULL;
5488 trk->tref_tags = tmp;
5489 tag = &trk->tref_tags[trk->nb_tref_tags++];
5490 *tag = (MovTag){ .name = name };
5491 }
5492
5493 return tag;
5494}
5495
5498{
5499 int i;
5500 int64_t pos = avio_tell(pb);
5501 avio_wb32(pb, 0); /* size placeholder*/
5502 ffio_wfourcc(pb, "moov");
5503
5504 mov_setup_track_ids(mov, s);
5505
5506 for (i = 0; i < mov->nb_tracks; i++) {
5507 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
5508 continue;
5509
5510 mov->tracks[i].time = mov->time;
5511
5512 if (mov->tracks[i].entry)
5513 build_chunks(&mov->tracks[i]);
5514 }
5515
5516 if (mov->chapter_track)
5517 for (i = 0; i < mov->nb_streams; i++) {
5518 MovTag *tag = mov_add_tref_tag(mov, &mov->tracks[i], MKTAG('c','h','a','p'));
5519 if (!tag)
5520 return AVERROR(ENOMEM);
5521
5522 int ret = mov_add_tref_id(mov, tag, mov->tracks[mov->chapter_track].track_id);
5523 if (ret < 0)
5524 return ret;
5525 }
5526 for (i = 0; i < mov->nb_tracks; i++) {
5527 MOVTrack *track = &mov->tracks[i];
5528 if (track->tag == MKTAG('r','t','p',' ')) {
5529 MovTag *tag = mov_add_tref_tag(mov, track, MKTAG('h','i','n','t'));
5530 if (!tag)
5531 return AVERROR(ENOMEM);
5532
5533 av_assert0(track->nb_src_track);
5534 int ret = mov_add_tref_id(mov, tag, mov->tracks[*track->src_track].track_id);
5535 if (ret < 0)
5536 return ret;
5537 } else if (track->tag == MKTAG('l','v','c','1') && track->nb_src_track) {
5538 MovTag *tag = mov_add_tref_tag(mov, track, MKTAG('s','b','a','s'));
5539 if (!tag)
5540 return AVERROR(ENOMEM);
5541
5542 int ret = mov_add_tref_id(mov, tag, mov->tracks[*track->src_track].track_id);
5543 if (ret < 0)
5544 return ret;
5545 } else {
5546 if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
5550 if (sd && sd->size == sizeof(int)) {
5551 int *fallback = (int *)sd->data;
5552 if (*fallback >= 0 && *fallback < mov->nb_tracks) {
5553 MovTag *tag = mov_add_tref_tag(mov, track, MKTAG('f','a','l','l'));
5554 if (!tag)
5555 return AVERROR(ENOMEM);
5556
5557 int ret = mov_add_tref_id(mov, tag, mov->tracks[*fallback].track_id);
5558 if (ret < 0)
5559 return ret;
5560 }
5561 }
5562 }
5563 for (int j = 0; j < track->nb_src_track; j++) {
5564 int src_trk = track->src_track[j];
5565 MovTag *tag = mov_add_tref_tag(mov, &mov->tracks[src_trk], track->tag);
5566 if (!tag)
5567 return AVERROR(ENOMEM);
5568 int ret = mov_add_tref_id(mov, tag, track->track_id);
5569 if (ret < 0)
5570 return ret;
5571 }
5572 if (mov->nb_meta_tmcd && track->nb_src_track) {
5573 /* Derive the duration from the first source track, matching
5574 * the convention used by get_pts_range() and the rtp/lvc1
5575 * branches above. The source may use a different timescale. */
5576 int src_trk = track->src_track[0];
5577 track->track_duration = av_rescale(mov->tracks[src_trk].track_duration,
5578 track->timescale,
5579 mov->tracks[src_trk].timescale);
5580 }
5581 }
5582 }
5583
5584 mov_write_mvhd_tag(pb, mov);
5585 if (mov->mode != MODE_MOV && mov->mode != MODE_AVIF && !mov->iods_skip)
5586 mov_write_iods_tag(pb, mov);
5587 for (i = 0; i < mov->nb_tracks; i++) {
5588 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT ||
5589 mov->mode == MODE_AVIF) {
5590 int ret = mov_write_trak_tag(s, pb, mov, &(mov->tracks[i]), i < mov->nb_streams ? mov->tracks[i].st : NULL);
5591 if (ret < 0)
5592 return ret;
5593 }
5594 }
5595 /* Don't write mvex for hybrid_fragmented during mov_write_trailer
5596 * (mov->moov_written != 0)
5597 */
5598 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
5600 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
5601
5602 if (mov->mode == MODE_PSP)
5604 else if (mov->mode != MODE_AVIF)
5605 mov_write_udta_tag(pb, mov, s);
5606 for (i = 0; i < mov->nb_streams; i++)
5607 mov_write_pssh_tag(pb, mov->tracks[i].st);
5608
5609 return update_size(pb, pos);
5610}
5611
5612static void param_write_int(AVIOContext *pb, const char *name, int value)
5613{
5614 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
5615}
5616
5617static void param_write_string(AVIOContext *pb, const char *name, const char *value)
5618{
5619 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
5620}
5621
5622static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
5623{
5624 char buf[150];
5625 len = FFMIN(sizeof(buf) / 2 - 1, len);
5626 ff_data_to_hex(buf, value, len, 0);
5627 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
5628}
5629
5631{
5632 int64_t pos = avio_tell(pb);
5633 int i;
5634
5635 static const AVUUID uuid = {
5636 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
5637 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
5638 };
5639
5640 avio_wb32(pb, 0);
5641 ffio_wfourcc(pb, "uuid");
5642 avio_write(pb, uuid, AV_UUID_LEN);
5643 avio_wb32(pb, 0);
5644
5645 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
5646 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
5647 avio_printf(pb, "<head>\n");
5648 if (!(mov->fc->flags & AVFMT_FLAG_BITEXACT))
5649 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
5651 avio_printf(pb, "</head>\n");
5652 avio_printf(pb, "<body>\n");
5653 avio_printf(pb, "<switch>\n");
5654
5655 mov_setup_track_ids(mov, s);
5656
5657 for (i = 0; i < mov->nb_tracks; i++) {
5658 MOVTrack *track = &mov->tracks[i];
5659 struct mpeg4_bit_rate_values bit_rates =
5661 const char *type;
5662 int track_id = track->track_id;
5663 char track_name_buf[32] = { 0 };
5664
5665 AVStream *st = track->st;
5666 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
5667
5668 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && !is_cover_image(st)) {
5669 type = "video";
5670 } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
5671 type = "audio";
5672 } else {
5673 continue;
5674 }
5675
5676 avio_printf(pb, "<%s systemBitrate=\"%"PRIu32"\">\n", type,
5677 bit_rates.avg_bit_rate);
5678 param_write_int(pb, "systemBitrate", bit_rates.avg_bit_rate);
5679 param_write_int(pb, "trackID", track_id);
5680 param_write_string(pb, "systemLanguage", lang ? lang->value : "und");
5681
5682 /* Build track name piece by piece: */
5683 /* 1. track type */
5684 av_strlcat(track_name_buf, type, sizeof(track_name_buf));
5685 /* 2. track language, if available */
5686 if (lang)
5687 av_strlcatf(track_name_buf, sizeof(track_name_buf),
5688 "_%s", lang->value);
5689 /* 3. special type suffix */
5690 /* "_cc" = closed captions, "_ad" = audio_description */
5692 av_strlcat(track_name_buf, "_cc", sizeof(track_name_buf));
5694 av_strlcat(track_name_buf, "_ad", sizeof(track_name_buf));
5695
5696 param_write_string(pb, "trackName", track_name_buf);
5697
5698 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
5699 if (track->par->codec_id == AV_CODEC_ID_H264) {
5700 uint8_t *ptr;
5701 int size = track->extradata_size[track->last_stsd_index];
5703 &size)) {
5704 param_write_hex(pb, "CodecPrivateData",
5705 ptr ? ptr : track->extradata[track->last_stsd_index],
5706 size);
5707 av_free(ptr);
5708 }
5709 param_write_string(pb, "FourCC", "H264");
5710 } else if (track->par->codec_id == AV_CODEC_ID_VC1) {
5711 param_write_string(pb, "FourCC", "WVC1");
5712 param_write_hex(pb, "CodecPrivateData", track->extradata[track->last_stsd_index],
5713 track->extradata_size[track->last_stsd_index]);
5714 }
5715 param_write_int(pb, "MaxWidth", track->par->width);
5716 param_write_int(pb, "MaxHeight", track->par->height);
5717 param_write_int(pb, "DisplayWidth", track->par->width);
5718 param_write_int(pb, "DisplayHeight", track->par->height);
5719 } else {
5720 if (track->par->codec_id == AV_CODEC_ID_AAC) {
5721 switch (track->par->profile) {
5723 param_write_string(pb, "FourCC", "AACP");
5724 break;
5725 case AV_PROFILE_AAC_HE:
5726 param_write_string(pb, "FourCC", "AACH");
5727 break;
5728 default:
5729 param_write_string(pb, "FourCC", "AACL");
5730 }
5731 } else if (track->par->codec_id == AV_CODEC_ID_WMAPRO) {
5732 param_write_string(pb, "FourCC", "WMAP");
5733 }
5734 param_write_hex(pb, "CodecPrivateData", track->extradata[track->last_stsd_index],
5735 track->extradata_size[track->last_stsd_index]);
5737 track->par->codec_id));
5738 param_write_int(pb, "Channels", track->par->ch_layout.nb_channels);
5739 param_write_int(pb, "SamplingRate", track->tag == MKTAG('i','a','m','f') ?
5740 0 : track->par->sample_rate);
5741 param_write_int(pb, "BitsPerSample", 16);
5742 param_write_int(pb, "PacketSize", track->par->block_align ?
5743 track->par->block_align : 4);
5744 }
5745 avio_printf(pb, "</%s>\n", type);
5746 }
5747 avio_printf(pb, "</switch>\n");
5748 avio_printf(pb, "</body>\n");
5749 avio_printf(pb, "</smil>\n");
5750
5751 return update_size(pb, pos);
5752}
5753
5755{
5756 avio_wb32(pb, 16);
5757 ffio_wfourcc(pb, "mfhd");
5758 avio_wb32(pb, 0);
5759 avio_wb32(pb, mov->fragments);
5760 return 0;
5761}
5762
5768
5770 MOVTrack *track, int64_t moof_offset)
5771{
5772 int64_t pos = avio_tell(pb);
5775 if (!track->entry) {
5777 } else {
5779 }
5785 }
5786 /* CMAF requires all values to be explicit in tfhd atoms */
5787 if (mov->flags & FF_MOV_FLAG_CMAF)
5789
5790 /* Don't set a default sample size, the silverlight player refuses
5791 * to play files with that set. Don't set a default sample duration,
5792 * WMP freaks out if it is set. Don't set a base data offset, PIFF
5793 * file format says it MUST NOT be set. */
5794 if (track->mode == MODE_ISM)
5797
5798 avio_wb32(pb, 0); /* size placeholder */
5799 ffio_wfourcc(pb, "tfhd");
5800 avio_w8(pb, 0); /* version */
5801 avio_wb24(pb, flags);
5802
5803 avio_wb32(pb, track->track_id); /* track-id */
5805 avio_wb64(pb, moof_offset);
5806 if (flags & MOV_TFHD_STSD_ID) {
5807 avio_wb32(pb, 1);
5808 }
5810 track->default_duration = get_cluster_duration(track, 0);
5811 avio_wb32(pb, track->default_duration);
5812 }
5814 track->default_size = track->entry ? track->cluster[0].size : 1;
5815 avio_wb32(pb, track->default_size);
5816 } else
5817 track->default_size = -1;
5818
5820 /* Set the default flags based on the second sample, if available.
5821 * If the first sample is different, that can be signaled via a separate field. */
5822 if (track->entry > 1)
5823 track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
5824 else
5825 track->default_sample_flags =
5826 track->par->codec_type == AVMEDIA_TYPE_VIDEO ?
5829 avio_wb32(pb, track->default_sample_flags);
5830 }
5831
5832 return update_size(pb, pos);
5833}
5834
5836 MOVTrack *track, int moof_size,
5837 int first, int end)
5838{
5839 int64_t pos = avio_tell(pb);
5840 uint32_t flags = MOV_TRUN_DATA_OFFSET;
5841 int i;
5842
5843 for (i = first; i < end; i++) {
5844 if (get_cluster_duration(track, i) != track->default_duration)
5846 if (track->cluster[i].size != track->default_size)
5848 if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
5850 }
5851 if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > first &&
5852 get_sample_flags(track, &track->cluster[first]) != track->default_sample_flags)
5854 if (track->flags & MOV_TRACK_CTTS)
5856
5857 avio_wb32(pb, 0); /* size placeholder */
5858 ffio_wfourcc(pb, "trun");
5860 avio_w8(pb, 1); /* version */
5861 else
5862 avio_w8(pb, 0); /* version */
5863 avio_wb24(pb, flags);
5864
5865 avio_wb32(pb, end - first); /* sample count */
5868 !mov->first_trun)
5869 avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
5870 else
5871 avio_wb32(pb, moof_size + 8 + track->data_offset +
5872 track->cluster[first].pos); /* data offset */
5874 avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
5875
5876 for (i = first; i < end; i++) {
5878 avio_wb32(pb, get_cluster_duration(track, i));
5880 avio_wb32(pb, track->cluster[i].size);
5882 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
5884 avio_wb32(pb, track->cluster[i].cts);
5885 }
5886
5887 mov->first_trun = 0;
5888 return update_size(pb, pos);
5889}
5890
5892{
5893 int64_t pos = avio_tell(pb);
5894 static const uint8_t uuid[] = {
5895 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
5896 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
5897 };
5898
5899 avio_wb32(pb, 0); /* size placeholder */
5900 ffio_wfourcc(pb, "uuid");
5901 avio_write(pb, uuid, AV_UUID_LEN);
5902 avio_w8(pb, 1);
5903 avio_wb24(pb, 0);
5904 avio_wb64(pb, track->cluster[0].dts + track->cluster[0].cts);
5905 avio_wb64(pb, track->end_pts -
5906 (track->cluster[0].dts + track->cluster[0].cts));
5907
5908 return update_size(pb, pos);
5909}
5910
5912 MOVTrack *track, int entry)
5913{
5914 int n = track->nb_frag_info - 1 - entry, i;
5915 int size = 8 + 16 + 4 + 1 + 16*n;
5916 static const uint8_t uuid[] = {
5917 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
5918 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
5919 };
5920
5921 if (entry < 0)
5922 return 0;
5923
5924 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
5925 avio_wb32(pb, size);
5926 ffio_wfourcc(pb, "uuid");
5927 avio_write(pb, uuid, AV_UUID_LEN);
5928 avio_w8(pb, 1);
5929 avio_wb24(pb, 0);
5930 avio_w8(pb, n);
5931 for (i = 0; i < n; i++) {
5932 int index = entry + 1 + i;
5933 avio_wb64(pb, track->frag_info[index].time);
5934 avio_wb64(pb, track->frag_info[index].duration);
5935 }
5936 if (n < mov->ism_lookahead) {
5937 int free_size = 16 * (mov->ism_lookahead - n);
5938 avio_wb32(pb, free_size);
5939 ffio_wfourcc(pb, "free");
5940 ffio_fill(pb, 0, free_size - 8);
5941 }
5942
5943 return 0;
5944}
5945
5947 MOVTrack *track)
5948{
5949 int64_t pos = avio_tell(pb);
5950 int i;
5951 for (i = 0; i < mov->ism_lookahead; i++) {
5952 /* Update the tfrf tag for the last ism_lookahead fragments,
5953 * nb_frag_info - 1 is the next fragment to be written. */
5954 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
5955 }
5956 avio_seek(pb, pos, SEEK_SET);
5957 return 0;
5958}
5959
5960static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
5961 int size)
5962{
5963 int i;
5964 for (i = 0; i < mov->nb_tracks; i++) {
5965 MOVTrack *track = &mov->tracks[i];
5966 MOVFragmentInfo *info;
5967 if ((tracks >= 0 && i != tracks) || !track->entry)
5968 continue;
5969 track->nb_frag_info++;
5970 if (track->nb_frag_info >= track->frag_info_capacity) {
5971 unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
5972 if (av_reallocp_array(&track->frag_info,
5973 new_capacity,
5974 sizeof(*track->frag_info)))
5975 return AVERROR(ENOMEM);
5976 track->frag_info_capacity = new_capacity;
5977 }
5978 info = &track->frag_info[track->nb_frag_info - 1];
5979 info->offset = avio_tell(pb);
5980 info->size = size;
5981 // Try to recreate the original pts for the first packet
5982 // from the fields we have stored
5983 info->time = track->cluster[0].dts + track->cluster[0].cts;
5984 info->duration = track->end_pts -
5985 (track->cluster[0].dts + track->cluster[0].cts);
5986 // If the pts is less than zero, we will have trimmed
5987 // away parts of the media track using an edit list,
5988 // and the corresponding start presentation time is zero.
5989 if (info->time < 0) {
5990 info->duration += info->time;
5991 info->time = 0;
5992 }
5993 info->tfrf_offset = 0;
5994 mov_write_tfrf_tags(pb, mov, track);
5995 }
5996 return 0;
5997}
5998
5999static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
6000{
6001 int i;
6002 for (i = 0; i < mov->nb_tracks; i++) {
6003 MOVTrack *track = &mov->tracks[i];
6004 if ((tracks >= 0 && i != tracks) || !track->entry)
6005 continue;
6006 if (track->nb_frag_info > max) {
6007 memmove(track->frag_info, track->frag_info + (track->nb_frag_info - max), max * sizeof(*track->frag_info));
6008 track->nb_frag_info = max;
6009 }
6010 }
6011}
6012
6014{
6015 int64_t pos = avio_tell(pb);
6016
6017 avio_wb32(pb, 0); /* size */
6018 ffio_wfourcc(pb, "tfdt");
6019 avio_w8(pb, 1); /* version */
6020 avio_wb24(pb, 0);
6021 avio_wb64(pb, track->cluster[0].dts - track->start_dts);
6022 return update_size(pb, pos);
6023}
6024
6026 MOVTrack *track, int64_t moof_offset,
6027 int moof_size)
6028{
6029 int64_t pos = avio_tell(pb);
6030 int i, start = 0;
6031 avio_wb32(pb, 0); /* size placeholder */
6032 ffio_wfourcc(pb, "traf");
6033
6034 mov_write_tfhd_tag(pb, mov, track, moof_offset);
6035 if (mov->mode != MODE_ISM)
6036 mov_write_tfdt_tag(pb, track);
6037 for (i = 1; i < track->entry; i++) {
6038 if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
6039 mov_write_trun_tag(pb, mov, track, moof_size, start, i);
6040 start = i;
6041 }
6042 }
6043 mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
6044 if (mov->mode == MODE_ISM) {
6045 mov_write_tfxd_tag(pb, track);
6046
6047 if (mov->ism_lookahead) {
6048 int size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
6049
6050 if (track->nb_frag_info > 0) {
6051 MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
6052 if (!info->tfrf_offset)
6053 info->tfrf_offset = avio_tell(pb);
6054 }
6055 avio_wb32(pb, 8 + size);
6056 ffio_wfourcc(pb, "free");
6057 ffio_fill(pb, 0, size);
6058 }
6059 }
6060
6061 if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
6062 ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, moof_offset);
6063
6064 return update_size(pb, pos);
6065}
6066
6068 int tracks, int moof_size)
6069{
6070 int64_t pos = avio_tell(pb);
6071 int i;
6072
6073 avio_wb32(pb, 0); /* size placeholder */
6074 ffio_wfourcc(pb, "moof");
6075 mov->first_trun = 1;
6076
6077 mov_write_mfhd_tag(pb, mov);
6078 for (i = 0; i < mov->nb_tracks; i++) {
6079 MOVTrack *track = &mov->tracks[i];
6080 if (tracks >= 0 && i != tracks)
6081 continue;
6082 if (!track->entry)
6083 continue;
6084 if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
6085 mov_write_pssh_tag(pb, track->st);
6086 mov_write_traf_tag(pb, mov, track, pos, moof_size);
6087 }
6088
6089 return update_size(pb, pos);
6090}
6091
6093 MOVTrack *track, int ref_size, int total_sidx_size)
6094{
6095 int64_t pos = avio_tell(pb), offset_pos, end_pos;
6096 int64_t presentation_time, duration, offset;
6097 unsigned starts_with_SAP;
6098 int i, entries;
6099
6100 if (track->entry) {
6101 entries = 1;
6102 presentation_time = track->cluster[0].dts + track->cluster[0].cts;
6103 duration = track->end_pts -
6104 (track->cluster[0].dts + track->cluster[0].cts);
6105 starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
6106
6107 // pts<0 should be cut away using edts
6108 if (presentation_time < 0) {
6109 duration += presentation_time;
6110 presentation_time = 0;
6111 }
6112 } else {
6113 entries = track->nb_frag_info;
6114 if (entries <= 0)
6115 return 0;
6116 presentation_time = track->frag_info[0].time;
6117 }
6118
6119 avio_wb32(pb, 0); /* size */
6120 ffio_wfourcc(pb, "sidx");
6121 avio_w8(pb, 1); /* version */
6122 avio_wb24(pb, 0);
6123 avio_wb32(pb, track->track_id); /* reference_ID */
6124 avio_wb32(pb, track->timescale); /* timescale */
6125 avio_wb64(pb, presentation_time); /* earliest_presentation_time */
6126 offset_pos = avio_tell(pb);
6127 avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
6128 avio_wb16(pb, 0); /* reserved */
6129
6130 avio_wb16(pb, entries); /* reference_count */
6131 for (i = 0; i < entries; i++) {
6132 if (!track->entry) {
6133 if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
6134 av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
6135 }
6136 duration = track->frag_info[i].duration;
6137 ref_size = track->frag_info[i].size;
6138 starts_with_SAP = 1;
6139 }
6140 avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
6141 avio_wb32(pb, duration); /* subsegment_duration */
6142 avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
6143 }
6144
6145 end_pos = avio_tell(pb);
6146 offset = pos + total_sidx_size - end_pos;
6147 avio_seek(pb, offset_pos, SEEK_SET);
6148 avio_wb64(pb, offset);
6149 avio_seek(pb, end_pos, SEEK_SET);
6150 return update_size(pb, pos);
6151}
6152
6154 int tracks, int ref_size)
6155{
6156 int i, round, ret;
6157 AVIOContext *avio_buf;
6158 int total_size = 0;
6159 for (round = 0; round < 2; round++) {
6160 // First run one round to calculate the total size of all
6161 // sidx atoms.
6162 // This would be much simpler if we'd only write one sidx
6163 // atom, for the first track in the moof.
6164 if (round == 0) {
6165 if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
6166 return ret;
6167 } else {
6168 avio_buf = pb;
6169 }
6170 for (i = 0; i < mov->nb_tracks; i++) {
6171 MOVTrack *track = &mov->tracks[i];
6172 if (tracks >= 0 && i != tracks)
6173 continue;
6174 // When writing a sidx for the full file, entry is 0, but
6175 // we want to include all tracks. ref_size is 0 in this case,
6176 // since we read it from frag_info instead.
6177 if (!track->entry && ref_size > 0)
6178 continue;
6179 total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
6180 total_size);
6181 }
6182 if (round == 0) {
6183 total_size = ffio_close_null_buf(avio_buf);
6184 if (total_size < 0)
6185 return total_size;
6186 }
6187 }
6188 return 0;
6189}
6190
6191static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
6192{
6193 int64_t pos = avio_tell(pb), pts_us, ntp_ts;
6194 MOVTrack *first_track;
6195 int flags = 24;
6196
6197 /* PRFT should be associated with at most one track. So, choosing only the
6198 * first track. */
6199 if (tracks > 0)
6200 return 0;
6201 first_track = &(mov->tracks[0]);
6202
6203 if (!first_track->entry) {
6204 av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, no entries in the track\n");
6205 return 0;
6206 }
6207
6208 if (first_track->cluster[0].pts == AV_NOPTS_VALUE) {
6209 av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, first PTS is invalid\n");
6210 return 0;
6211 }
6212
6213 if (mov->write_prft == MOV_PRFT_SRC_WALLCLOCK) {
6214 if (first_track->cluster[0].prft.wallclock) {
6215 /* Round the NTP time to whole milliseconds. */
6216 ntp_ts = ff_get_formatted_ntp_time((first_track->cluster[0].prft.wallclock / 1000) * 1000 +
6218 flags = first_track->cluster[0].prft.flags;
6219 } else
6221 } else if (mov->write_prft == MOV_PRFT_SRC_PTS) {
6222 pts_us = av_rescale_q(first_track->cluster[0].pts,
6223 first_track->st->time_base, AV_TIME_BASE_Q);
6224 ntp_ts = ff_get_formatted_ntp_time(pts_us + NTP_OFFSET_US);
6225 } else {
6226 av_log(mov->fc, AV_LOG_WARNING, "Unsupported PRFT box configuration: %d\n",
6227 mov->write_prft);
6228 return 0;
6229 }
6230
6231 avio_wb32(pb, 0); // Size place holder
6232 ffio_wfourcc(pb, "prft"); // Type
6233 avio_w8(pb, 1); // Version
6234 avio_wb24(pb, flags); // Flags
6235 avio_wb32(pb, first_track->track_id); // reference track ID
6236 avio_wb64(pb, ntp_ts); // NTP time stamp
6237 avio_wb64(pb, first_track->cluster[0].pts); //media time
6238 return update_size(pb, pos);
6239}
6240
6241static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
6242 int64_t mdat_size)
6243{
6244 AVIOContext *avio_buf;
6245 int ret, moof_size;
6246
6247 if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
6248 return ret;
6249 mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
6250 moof_size = ffio_close_null_buf(avio_buf);
6251 if (moof_size < 0)
6252 return moof_size;
6253
6254 if (mov->flags & FF_MOV_FLAG_DASH &&
6256 mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
6257
6258 if (mov->write_prft > MOV_PRFT_NONE && mov->write_prft < MOV_PRFT_NB)
6259 mov_write_prft_tag(pb, mov, tracks);
6260
6261 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX ||
6262 !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) ||
6263 mov->ism_lookahead) {
6264 if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
6265 return ret;
6266 if (!(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) &&
6268 mov_prune_frag_info(mov, tracks, mov->ism_lookahead + 1);
6269 }
6270 }
6271
6272 return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
6273}
6274
6276{
6277 int64_t pos = avio_tell(pb);
6278 int i;
6279
6280 avio_wb32(pb, 0); /* size placeholder */
6281 ffio_wfourcc(pb, "tfra");
6282 avio_w8(pb, 1); /* version */
6283 avio_wb24(pb, 0);
6284
6285 avio_wb32(pb, track->track_id);
6286 avio_wb32(pb, 0); /* length of traf/trun/sample num */
6287 avio_wb32(pb, track->nb_frag_info);
6288 for (i = 0; i < track->nb_frag_info; i++) {
6289 avio_wb64(pb, track->frag_info[i].time);
6290 avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
6291 avio_w8(pb, 1); /* traf number */
6292 avio_w8(pb, 1); /* trun number */
6293 avio_w8(pb, 1); /* sample number */
6294 }
6295
6296 return update_size(pb, pos);
6297}
6298
6300{
6301 AVIOContext *mfra_pb;
6302 int i, ret, sz;
6303 uint8_t *buf;
6304
6305 ret = avio_open_dyn_buf(&mfra_pb);
6306 if (ret < 0)
6307 return ret;
6308
6309 avio_wb32(mfra_pb, 0); /* size placeholder */
6310 ffio_wfourcc(mfra_pb, "mfra");
6311 /* An empty mfra atom is enough to indicate to the publishing point that
6312 * the stream has ended. */
6313 if (mov->flags & FF_MOV_FLAG_ISML)
6314 goto done_mfra;
6315
6316 for (i = 0; i < mov->nb_tracks; i++) {
6317 MOVTrack *track = &mov->tracks[i];
6318 if (track->nb_frag_info)
6319 mov_write_tfra_tag(mfra_pb, track);
6320 }
6321
6322 avio_wb32(mfra_pb, 16);
6323 ffio_wfourcc(mfra_pb, "mfro");
6324 avio_wb32(mfra_pb, 0); /* version + flags */
6325 avio_wb32(mfra_pb, avio_tell(mfra_pb) + 4);
6326
6327done_mfra:
6328
6329 sz = update_size(mfra_pb, 0);
6330 ret = avio_get_dyn_buf(mfra_pb, &buf);
6331 avio_write(pb, buf, ret);
6332 ffio_free_dyn_buf(&mfra_pb);
6333
6334 return sz;
6335}
6336
6338{
6339 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
6340 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
6341
6342 mov->mdat_pos = avio_tell(pb);
6343 avio_wb32(pb, 0); /* size placeholder*/
6344 ffio_wfourcc(pb, "mdat");
6345 return 0;
6346}
6347
6349 int has_h264, int has_video, int write_minor)
6350{
6351 MOVMuxContext *mov = s->priv_data;
6352 int minor = 0x200;
6353
6354 if (mov->major_brand && strlen(mov->major_brand) >= 4)
6355 ffio_wfourcc(pb, mov->major_brand);
6356 else if (mov->mode == MODE_3GP) {
6357 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
6358 minor = has_h264 ? 0x100 : 0x200;
6359 } else if (mov->mode == MODE_AVIF) {
6360 ffio_wfourcc(pb, mov->is_animated_avif ? "avis" : "avif");
6361 minor = 0;
6362 } else if (mov->mode & MODE_3G2) {
6363 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
6364 minor = has_h264 ? 0x20000 : 0x10000;
6365 } else if (mov->mode == MODE_PSP)
6366 ffio_wfourcc(pb, "MSNV");
6367 else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_FRAGMENT &&
6369 ffio_wfourcc(pb, "iso6"); // Required when using signed CTS offsets in trun boxes
6370 else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
6371 ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
6372 else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
6373 ffio_wfourcc(pb, "iso4");
6374 else if (mov->mode == MODE_MP4)
6375 ffio_wfourcc(pb, "isom");
6376 else if (mov->mode == MODE_IPOD)
6377 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
6378 else if (mov->mode == MODE_ISM)
6379 ffio_wfourcc(pb, "isml");
6380 else if (mov->mode == MODE_F4V)
6381 ffio_wfourcc(pb, "f4v ");
6382 else
6383 ffio_wfourcc(pb, "qt ");
6384
6385 if (write_minor)
6386 avio_wb32(pb, minor);
6387}
6388
6390{
6391 MOVMuxContext *mov = s->priv_data;
6392 int64_t pos = avio_tell(pb);
6393 int has_h264 = 0, has_av1 = 0, has_video = 0, has_dolby = 0, has_id3 = 0;
6394 int has_iamf = 0;
6395
6396#if CONFIG_IAMFENC
6397 for (int i = 0; i < s->nb_stream_groups; i++) {
6398 const AVStreamGroup *stg = s->stream_groups[i];
6399
6402 has_iamf = 1;
6403 break;
6404 }
6405 }
6406#endif
6407 for (int i = 0; i < mov->nb_streams; i++) {
6408 AVStream *st = mov->tracks[i].st;
6409 if (is_cover_image(st))
6410 continue;
6412 has_video = 1;
6414 has_h264 = 1;
6415 if (st->codecpar->codec_id == AV_CODEC_ID_AV1)
6416 has_av1 = 1;
6417 if (st->codecpar->codec_id == AV_CODEC_ID_AC3 ||
6423 has_dolby = 1;
6425 has_id3 = 1;
6426 }
6427
6428 avio_wb32(pb, 0); /* size */
6429 ffio_wfourcc(pb, "ftyp");
6430
6431 // Write major brand
6432 mov_write_ftyp_tag_internal(pb, s, has_h264, has_video, 1);
6433 // Write the major brand as the first compatible brand as well
6434 mov_write_ftyp_tag_internal(pb, s, has_h264, has_video, 0);
6435
6436 // Write compatible brands, ensuring that we don't write the major brand as a
6437 // compatible brand a second time.
6438 if (mov->mode == MODE_ISM) {
6439 ffio_wfourcc(pb, "piff");
6440#if CONFIG_AVIF_MUXER
6441 } else if (mov->mode == MODE_AVIF) {
6442 const MOVTrack *trk = &mov->tracks[0];
6443 const AVPixFmtDescriptor *pix_fmt_desc =
6444 av_pix_fmt_desc_get(s->streams[0]->codecpar->format);
6445 int depth = 0, log2_chroma_w, log2_chroma_h;
6446
6447 if (pix_fmt_desc->nb_components > 0) {
6448 depth = pix_fmt_desc->comp[0].depth;
6449 log2_chroma_w = pix_fmt_desc->log2_chroma_w;
6450 log2_chroma_h = pix_fmt_desc->log2_chroma_h;
6451 } else {
6454 trk->extradata_size[trk->last_stsd_index]);
6455 if (!ret) {
6456 depth = seq.bitdepth;
6457 log2_chroma_w = seq.chroma_subsampling_x;
6458 log2_chroma_h = seq.chroma_subsampling_y;
6459 }
6460 }
6461 if (mov->is_animated_avif) {
6462 // For animated AVIF, major brand is "avis". Add "avif" as a
6463 // compatible brand.
6464 ffio_wfourcc(pb, "avif");
6465 ffio_wfourcc(pb, "msf1");
6466 ffio_wfourcc(pb, "iso8");
6467 }
6468 ffio_wfourcc(pb, "mif1");
6469 ffio_wfourcc(pb, "miaf");
6470 if (depth == 8 || depth == 10) {
6471 // MA1B and MA1A brands are based on AV1 profile. Short hand for
6472 // computing that is based on chroma subsampling type. 420 chroma
6473 // subsampling is MA1B. 444 chroma subsampling is MA1A.
6474 if (!log2_chroma_w && !log2_chroma_h) {
6475 // 444 chroma subsampling.
6476 ffio_wfourcc(pb, "MA1A");
6477 } else {
6478 // 420 chroma subsampling.
6479 ffio_wfourcc(pb, "MA1B");
6480 }
6481 }
6482#endif
6483 } else if (mov->mode != MODE_MOV) {
6484 // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
6485 // brand, if not already the major brand. This is compatible with users that
6486 // don't understand tfdt.
6487 if (mov->mode == MODE_MP4) {
6488 if (mov->flags & FF_MOV_FLAG_CMAF)
6489 ffio_wfourcc(pb, "cmfc");
6491 ffio_wfourcc(pb, "iso6");
6492 if (has_av1)
6493 ffio_wfourcc(pb, "av01");
6494 if (has_dolby)
6495 ffio_wfourcc(pb, "dby1");
6496 if (has_iamf)
6497 ffio_wfourcc(pb, "iamf");
6498 } else {
6499 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
6500 ffio_wfourcc(pb, "iso6");
6502 ffio_wfourcc(pb, "iso5");
6504 ffio_wfourcc(pb, "iso4");
6505 }
6506 // Brands prior to iso5 can't be signaled when using default-base-is-moof
6507 if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
6508 // write isom for mp4 only if it it's not the major brand already.
6510 ffio_wfourcc(pb, "isom");
6511 ffio_wfourcc(pb, "iso2");
6512 if (has_h264)
6513 ffio_wfourcc(pb, "avc1");
6514 }
6515 }
6516
6517 if (mov->mode == MODE_MP4)
6518 ffio_wfourcc(pb, "mp41");
6519
6521 ffio_wfourcc(pb, "dash");
6522
6523 if (has_id3)
6524 ffio_wfourcc(pb, "aid3");
6525
6526 return update_size(pb, pos);
6527}
6528
6530{
6531 AVStream *video_st = s->streams[0];
6532 AVCodecParameters *video_par = s->streams[0]->codecpar;
6533 AVCodecParameters *audio_par = s->streams[1]->codecpar;
6534 int audio_rate = audio_par->sample_rate;
6535 int64_t frame_rate = video_st->avg_frame_rate.den ?
6536 (video_st->avg_frame_rate.num * 0x10000LL) / video_st->avg_frame_rate.den :
6537 0;
6538 int audio_kbitrate = audio_par->bit_rate / 1000;
6539 int video_kbitrate = FFMIN(video_par->bit_rate / 1000, 800 - audio_kbitrate);
6540
6541 if (frame_rate < 0 || frame_rate > INT32_MAX) {
6542 av_log(s, AV_LOG_ERROR, "Frame rate %f outside supported range\n", frame_rate / (double)0x10000);
6543 return AVERROR(EINVAL);
6544 }
6545
6546 avio_wb32(pb, 0x94); /* size */
6547 ffio_wfourcc(pb, "uuid");
6548 ffio_wfourcc(pb, "PROF");
6549
6550 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
6551 avio_wb32(pb, 0xbb88695c);
6552 avio_wb32(pb, 0xfac9c740);
6553
6554 avio_wb32(pb, 0x0); /* ? */
6555 avio_wb32(pb, 0x3); /* 3 sections ? */
6556
6557 avio_wb32(pb, 0x14); /* size */
6558 ffio_wfourcc(pb, "FPRF");
6559 avio_wb32(pb, 0x0); /* ? */
6560 avio_wb32(pb, 0x0); /* ? */
6561 avio_wb32(pb, 0x0); /* ? */
6562
6563 avio_wb32(pb, 0x2c); /* size */
6564 ffio_wfourcc(pb, "APRF"); /* audio */
6565 avio_wb32(pb, 0x0);
6566 avio_wb32(pb, 0x2); /* TrackID */
6567 ffio_wfourcc(pb, "mp4a");
6568 avio_wb32(pb, 0x20f);
6569 avio_wb32(pb, 0x0);
6570 avio_wb32(pb, audio_kbitrate);
6571 avio_wb32(pb, audio_kbitrate);
6572 avio_wb32(pb, audio_rate);
6573 avio_wb32(pb, audio_par->ch_layout.nb_channels);
6574
6575 avio_wb32(pb, 0x34); /* size */
6576 ffio_wfourcc(pb, "VPRF"); /* video */
6577 avio_wb32(pb, 0x0);
6578 avio_wb32(pb, 0x1); /* TrackID */
6579 if (video_par->codec_id == AV_CODEC_ID_H264) {
6580 ffio_wfourcc(pb, "avc1");
6581 avio_wb16(pb, 0x014D);
6582 avio_wb16(pb, 0x0015);
6583 } else {
6584 ffio_wfourcc(pb, "mp4v");
6585 avio_wb16(pb, 0x0000);
6586 avio_wb16(pb, 0x0103);
6587 }
6588 avio_wb32(pb, 0x0);
6589 avio_wb32(pb, video_kbitrate);
6590 avio_wb32(pb, video_kbitrate);
6591 avio_wb32(pb, frame_rate);
6592 avio_wb32(pb, frame_rate);
6593 avio_wb16(pb, video_par->width);
6594 avio_wb16(pb, video_par->height);
6595 avio_wb32(pb, 0x010001); /* ? */
6596
6597 return 0;
6598}
6599
6601{
6602 MOVMuxContext *mov = s->priv_data;
6603 int i;
6604
6606 if (mov->mode == MODE_PSP) {
6607 int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
6608 for (i = 0; i < mov->nb_streams; i++) {
6609 AVStream *st = mov->tracks[i].st;
6610 if (is_cover_image(st))
6611 continue;
6613 video_streams_nb++;
6614 else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
6615 audio_streams_nb++;
6616 else
6617 other_streams_nb++;
6618 }
6619
6620 if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
6621 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
6622 return AVERROR(EINVAL);
6623 }
6624 return mov_write_uuidprof_tag(pb, s);
6625 }
6626 return 0;
6627}
6628
6630{
6631 uint32_t c = -1;
6632 int i, closed_gop = 0;
6633
6634 for (i = 0; i < pkt->size - 4; i++) {
6635 c = (c << 8) + pkt->data[i];
6636 if (c == 0x1b8) { // gop
6637 closed_gop = pkt->data[i + 4] >> 6 & 0x01;
6638 } else if (c == 0x100) { // pic
6639 int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
6640 if (!temp_ref || closed_gop) // I picture is not reordered
6642 else
6644 break;
6645 }
6646 }
6647 return 0;
6648}
6649
6651{
6652 const uint8_t *start, *next, *end = pkt->data + pkt->size;
6653 int seq = 0, entry = 0;
6654 int key = pkt->flags & AV_PKT_FLAG_KEY;
6655 start = find_next_marker(pkt->data, end);
6656 for (next = start; next < end; start = next) {
6657 next = find_next_marker(start + 4, end);
6658 switch (AV_RB32(start)) {
6659 case VC1_CODE_SEQHDR:
6660 seq = 1;
6661 break;
6663 entry = 1;
6664 break;
6665 case VC1_CODE_SLICE:
6666 trk->vc1_info.slices = 1;
6667 break;
6668 }
6669 }
6670 if (!trk->entry && trk->vc1_info.first_packet_seen)
6672 if (!trk->entry && !trk->vc1_info.first_frag_written) {
6673 /* First packet in first fragment */
6674 trk->vc1_info.first_packet_seq = seq;
6676 trk->vc1_info.first_packet_seen = 1;
6677 } else if ((seq && !trk->vc1_info.packet_seq) ||
6678 (entry && !trk->vc1_info.packet_entry)) {
6679 int i;
6680 for (i = 0; i < trk->entry; i++)
6681 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
6682 trk->has_keyframes = 0;
6683 if (seq)
6684 trk->vc1_info.packet_seq = 1;
6685 if (entry)
6686 trk->vc1_info.packet_entry = 1;
6687 if (!trk->vc1_info.first_frag_written) {
6688 /* First fragment */
6689 if ((!seq || trk->vc1_info.first_packet_seq) &&
6690 (!entry || trk->vc1_info.first_packet_entry)) {
6691 /* First packet had the same headers as this one, readd the
6692 * sync sample flag. */
6693 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
6694 trk->has_keyframes = 1;
6695 }
6696 }
6697 }
6698 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
6699 key = seq && entry;
6700 else if (trk->vc1_info.packet_seq)
6701 key = seq;
6702 else if (trk->vc1_info.packet_entry)
6703 key = entry;
6704 if (key) {
6705 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
6706 trk->has_keyframes++;
6707 }
6708}
6709
6711{
6712 int length;
6713
6714 if (pkt->size < 8)
6715 return;
6716
6717 length = (AV_RB16(pkt->data) & 0xFFF) * 2;
6718 if (length < 8 || length > pkt->size)
6719 return;
6720
6721 if (AV_RB32(pkt->data + 4) == 0xF8726FBA) {
6722 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
6723 trk->has_keyframes++;
6724 }
6725
6726 return;
6727}
6728
6730{
6731 MOVMuxContext *mov = s->priv_data;
6732 int ret, buf_size;
6733 uint8_t *buf;
6734 int i, offset;
6735
6736 if (!track->mdat_buf)
6737 return 0;
6738 if (!mov->mdat_buf) {
6739 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
6740 return ret;
6741 }
6742 buf_size = avio_get_dyn_buf(track->mdat_buf, &buf);
6743
6744 offset = avio_tell(mov->mdat_buf);
6745 avio_write(mov->mdat_buf, buf, buf_size);
6747
6748 for (i = track->entries_flushed; i < track->entry; i++)
6749 track->cluster[i].pos += offset;
6750 track->entries_flushed = track->entry;
6751 return 0;
6752}
6753
6755{
6756 MOVMuxContext *mov = s->priv_data;
6757 AVPacket *squashed_packet = mov->pkt;
6758 int ret = AVERROR_BUG;
6759
6760 switch (track->st->codecpar->codec_id) {
6761 case AV_CODEC_ID_TTML: {
6762 int had_packets = !!track->squashed_packet_queue.head;
6763
6764 if ((ret = ff_mov_generate_squashed_ttml_packet(s, track, squashed_packet)) < 0) {
6765 goto finish_squash;
6766 }
6767
6768 // We have generated a padding packet (no actual input packets in
6769 // queue) and its duration is zero. Skipping writing it.
6770 if (!had_packets && squashed_packet->duration == 0) {
6771 goto finish_squash;
6772 }
6773
6774 track->end_reliable = 1;
6775 break;
6776 }
6777 default:
6778 ret = AVERROR(EINVAL);
6779 goto finish_squash;
6780 }
6781
6782 squashed_packet->stream_index = track->st->index;
6783
6784 ret = mov_write_single_packet(s, squashed_packet);
6785
6786finish_squash:
6787 av_packet_unref(squashed_packet);
6788
6789 return ret;
6790}
6791
6793{
6794 MOVMuxContext *mov = s->priv_data;
6795
6796 for (int i = 0; i < mov->nb_streams; i++) {
6797 MOVTrack *track = &mov->tracks[i];
6798 int ret = AVERROR_BUG;
6799
6800 if (track->squash_fragment_samples_to_one && !track->entry) {
6801 if ((ret = mov_write_squashed_packet(s, track)) < 0) {
6803 "Failed to write squashed packet for %s stream with "
6804 "index %d and track id %d. Error: %s\n",
6806 track->st->index, track->track_id,
6807 av_err2str(ret));
6808 return ret;
6809 }
6810 }
6811 }
6812
6813 return 0;
6814}
6815
6817 int64_t ref_pos)
6818{
6819 int i;
6820 if (!track->entry)
6821 return 0;
6823 for (i = 0; i < track->entry; i++)
6824 track->cluster[i].pos += ref_pos + track->data_offset;
6825 if (track->cluster_written == 0) {
6826 // First flush. Chunking for this fragment may already have been
6827 // done, either if we didn't use empty_moov, or if we did use
6828 // delay_moov. In either case, reset chunking here.
6829 for (i = 0; i < track->entry; i++) {
6830 track->cluster[i].chunkNum = 0;
6831 track->cluster[i].samples_in_chunk = track->cluster[i].entries;
6832 }
6833 }
6835 track->entry_written + track->entry,
6836 sizeof(*track->cluster)))
6837 return AVERROR(ENOMEM);
6838 memcpy(&track->cluster_written[track->entry_written],
6839 track->cluster, track->entry * sizeof(*track->cluster));
6840 track->entry_written += track->entry;
6841 }
6842 track->entry = 0;
6843 track->entries_flushed = 0;
6844 track->end_reliable = 0;
6845 return 0;
6846}
6847
6848static int mov_flush_fragment(AVFormatContext *s, int force)
6849{
6850 MOVMuxContext *mov = s->priv_data;
6851 int i, first_track = -1;
6852 int64_t mdat_size = 0, mdat_start = 0;
6853 int ret;
6854 int has_video = 0, starts_with_key = 0, first_video_track = 1;
6855
6856 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
6857 return 0;
6858
6859 // Check if we have any tracks that require squashing.
6860 // In that case, we'll have to write the packet here.
6861 if ((ret = mov_write_squashed_packets(s)) < 0)
6862 return ret;
6863
6864 // Try to fill in the duration of the last packet in each stream
6865 // from queued packets in the interleave queues. If the flushing
6866 // of fragments was triggered automatically by an AVPacket, we
6867 // already have reliable info for the end of that track, but other
6868 // tracks may need to be filled in.
6869 for (i = 0; i < mov->nb_streams; i++) {
6870 MOVTrack *track = &mov->tracks[i];
6871 if (!track->end_reliable) {
6872 const AVPacket *pkt = ff_interleaved_peek(s, i);
6873 if (pkt) {
6874 int64_t offset, dts, pts;
6876 pts = pkt->pts + offset;
6877 dts = pkt->dts + offset;
6878 if (track->dts_shift != AV_NOPTS_VALUE)
6879 dts += track->dts_shift;
6880 track->track_duration = dts - track->start_dts;
6881 if (pts != AV_NOPTS_VALUE)
6882 track->end_pts = pts;
6883 else
6884 track->end_pts = dts;
6885 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
6886 track->elst_end_pts = track->end_pts;
6887 }
6888 }
6889 }
6890
6891 for (i = 0; i < mov->nb_tracks; i++) {
6892 MOVTrack *track = &mov->tracks[i];
6893 if (track->entry <= 1)
6894 continue;
6895 // Sample durations are calculated as the diff of dts values,
6896 // but for the last sample in a fragment, we don't know the dts
6897 // of the first sample in the next fragment, so we have to rely
6898 // on what was set as duration in the AVPacket. Not all callers
6899 // set this though, so we might want to replace it with an
6900 // estimate if it currently is zero.
6901 if (get_cluster_duration(track, track->entry - 1) != 0)
6902 continue;
6903 // Use the duration (i.e. dts diff) of the second last sample for
6904 // the last one. This is a wild guess (and fatal if it turns out
6905 // to be too long), but probably the best we can do - having a zero
6906 // duration is bad as well.
6907 track->track_duration += get_cluster_duration(track, track->entry - 2);
6908 track->end_pts += get_cluster_duration(track, track->entry - 2);
6909 if (!mov->missing_duration_warned) {
6911 "Estimating the duration of the last packet in a "
6912 "fragment, consider setting the duration field in "
6913 "AVPacket instead.\n");
6914 mov->missing_duration_warned = 1;
6915 }
6916 }
6917
6918 if (!mov->moov_written) {
6919 int64_t pos = avio_tell(s->pb);
6920 uint8_t *buf;
6921 int buf_size, moov_size;
6922
6923 for (i = 0; i < mov->nb_tracks; i++)
6924 if (!mov->tracks[i].entry && !is_cover_image(mov->tracks[i].st))
6925 break;
6926 /* Don't write the initial moov unless all tracks have data */
6927 if (i < mov->nb_tracks && !force)
6928 return 0;
6929
6930 moov_size = get_moov_size(s);
6931 for (i = 0; i < mov->nb_tracks; i++)
6932 mov->tracks[i].data_offset = pos + moov_size + 8;
6933
6935 if (mov->flags & FF_MOV_FLAG_DELAY_MOOV &&
6938 if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
6939 return ret;
6940
6941 if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
6942 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
6943 mov->reserved_header_pos = avio_tell(s->pb);
6945 mov->moov_written = 1;
6946 return 0;
6947 }
6948
6949 if (mov->mdat_buf) {
6950 buf_size = avio_get_dyn_buf(mov->mdat_buf, &buf);
6951 avio_wb32(s->pb, buf_size + 8);
6952 ffio_wfourcc(s->pb, "mdat");
6953 avio_write(s->pb, buf, buf_size);
6955 }
6956
6957 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
6958 mov->reserved_header_pos = avio_tell(s->pb);
6959
6960 mov->moov_written = 1;
6961 mov->mdat_size = 0;
6962 for (i = 0; i < mov->nb_tracks; i++)
6963 mov_finish_fragment(mov, &mov->tracks[i], 0);
6965 return 0;
6966 }
6967
6968 if (mov->frag_interleave) {
6969 for (i = 0; i < mov->nb_tracks; i++) {
6970 MOVTrack *track = &mov->tracks[i];
6971 int ret;
6972 if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
6973 return ret;
6974 }
6975
6976 if (!mov->mdat_buf)
6977 return 0;
6978 mdat_size = avio_tell(mov->mdat_buf);
6979 }
6980
6981 for (i = 0; i < mov->nb_tracks; i++) {
6982 MOVTrack *track = &mov->tracks[i];
6984 track->data_offset = 0;
6985 else
6986 track->data_offset = mdat_size;
6987 if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
6988 has_video = 1;
6989 if (first_video_track) {
6990 if (track->entry)
6991 starts_with_key = track->cluster[0].flags & MOV_SYNC_SAMPLE;
6992 first_video_track = 0;
6993 }
6994 }
6995 if (!track->entry)
6996 continue;
6997 if (track->mdat_buf)
6998 mdat_size += avio_tell(track->mdat_buf);
6999 if (first_track < 0)
7000 first_track = i;
7001 }
7002
7003 if (!mdat_size)
7004 return 0;
7005
7006 avio_write_marker(s->pb,
7007 av_rescale(mov->tracks[first_track].cluster[0].dts, AV_TIME_BASE, mov->tracks[first_track].timescale),
7008 (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);
7009
7010 for (i = first_track; i < mov->nb_tracks; i++) {
7011 MOVTrack *track = &mov->tracks[i];
7012 int buf_size, write_moof = 1, moof_tracks = -1;
7013 uint8_t *buf;
7014
7015 if (!track->entry)
7016 continue;
7017 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
7018 mdat_size = avio_tell(track->mdat_buf);
7019 moof_tracks = i;
7020 } else {
7021 write_moof = i == first_track;
7022 }
7023
7024 if (write_moof) {
7026
7027 mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
7028 mov->fragments++;
7029
7030 if (track->cenc.aes_ctr)
7031 ff_mov_cenc_flush(&track->cenc);
7032
7033 avio_wb32(s->pb, mdat_size + 8);
7034 ffio_wfourcc(s->pb, "mdat");
7035 mdat_start = avio_tell(s->pb);
7036 }
7037
7038 mov_finish_fragment(mov, &mov->tracks[i], mdat_start);
7039 if (!mov->frag_interleave) {
7040 if (!track->mdat_buf)
7041 continue;
7042 buf_size = avio_get_dyn_buf(track->mdat_buf, &buf);
7043 avio_write(s->pb, buf, buf_size);
7045 } else {
7046 if (!mov->mdat_buf)
7047 continue;
7048 buf_size = avio_get_dyn_buf(mov->mdat_buf, &buf);
7049 avio_write(s->pb, buf, buf_size);
7051 }
7052 }
7053
7054 mov->mdat_size = 0;
7055
7057 return 0;
7058}
7059
7061{
7062 MOVMuxContext *mov = s->priv_data;
7063 int had_moov = mov->moov_written;
7064 int ret = mov_flush_fragment(s, force);
7065 if (ret < 0)
7066 return ret;
7067 // If using delay_moov, the first flush only wrote the moov,
7068 // not the actual moof+mdat pair, thus flush once again.
7069 if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
7070 ret = mov_flush_fragment(s, force);
7071 return ret;
7072}
7073
7075{
7076 int64_t ref;
7077 uint64_t duration;
7078
7079 if (trk->entry) {
7080 ref = trk->cluster[trk->entry - 1].dts;
7081 } else if ( trk->start_dts != AV_NOPTS_VALUE
7082 && !trk->frag_discont) {
7083 ref = trk->start_dts + trk->track_duration;
7084 } else
7085 ref = pkt->dts; // Skip tests for the first packet
7086
7087 if (trk->dts_shift != AV_NOPTS_VALUE) {
7088 /* With negative CTS offsets we have set an offset to the DTS,
7089 * reverse this for the check. */
7090 ref -= trk->dts_shift;
7091 }
7092
7093 duration = pkt->dts - ref;
7094 if (pkt->dts < ref || duration >= INT_MAX) {
7095 av_log(s, AV_LOG_WARNING, "Packet duration: %"PRId64" / dts: %"PRId64" in stream %d is out of range\n",
7096 duration, pkt->dts, pkt->stream_index);
7097
7098 pkt->dts = ref + 1;
7099 pkt->pts = AV_NOPTS_VALUE;
7100 }
7101
7102 if (pkt->duration < 0 || pkt->duration > INT_MAX) {
7103 av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" in stream %d is invalid\n", pkt->duration, pkt->stream_index);
7104 return AVERROR(EINVAL);
7105 }
7106 return 0;
7107}
7108
7110 const AVPacket *pkt, AVRational src_time_base)
7111{
7112 MOVMuxContext *mov = s->priv_data;
7113 AVStream *st;
7114 MOVTrack *track;
7115 int64_t offset, dts, pts, candidate_duration;
7116
7117 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT) ||
7118 stream_index < 0 || stream_index >= s->nb_streams ||
7119 pkt->dts == AV_NOPTS_VALUE)
7120 return 0;
7121
7122 st = s->streams[stream_index];
7123 track = st->priv_data;
7124 if (!track->entry || track->start_dts == AV_NOPTS_VALUE)
7125 return 0;
7126
7127 if (ff_get_muxer_ts_offset(s, stream_index, &offset) < 0)
7128 return 0;
7129
7130 dts = av_rescale_q(pkt->dts, src_time_base, st->time_base) + offset;
7131 pts = pkt->pts == AV_NOPTS_VALUE
7133 : av_rescale_q(pkt->pts, src_time_base, st->time_base) + offset;
7134 if (track->dts_shift != AV_NOPTS_VALUE)
7135 dts += track->dts_shift;
7136
7137 candidate_duration = dts - track->start_dts;
7138 if (dts <= track->cluster[track->entry - 1].dts ||
7139 candidate_duration < 0 || candidate_duration >= track->track_duration)
7140 return 0;
7141
7142 track->track_duration = candidate_duration;
7143 track->end_pts = pts != AV_NOPTS_VALUE ? pts : dts;
7144 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
7145 track->elst_end_pts = track->end_pts;
7146 track->end_reliable = 1;
7147
7148 return 1;
7149}
7150
7152{
7153 MOVMuxContext *mov = s->priv_data;
7154 AVIOContext *pb = s->pb;
7155 MOVTrack *trk;
7156 AVCodecParameters *par;
7158 unsigned int samples_in_chunk = 0;
7160 int size = pkt->size, ret = 0, offset = 0;
7161 size_t prft_size;
7162 uint8_t *reformatted_data = NULL;
7163
7164 if (pkt->stream_index < s->nb_streams)
7165 trk = s->streams[pkt->stream_index]->priv_data;
7166 else // Timecode or chapter
7167 trk = &mov->tracks[pkt->stream_index];
7168 par = trk->par;
7169
7170 ret = check_pkt(s, trk, pkt);
7171 if (ret < 0)
7172 return ret;
7173
7174 if (pkt->pts != AV_NOPTS_VALUE &&
7175 (uint64_t)pkt->dts - pkt->pts != (int32_t)((uint64_t)pkt->dts - pkt->pts)) {
7176 av_log(s, AV_LOG_WARNING, "pts/dts pair unsupported\n");
7177 return AVERROR_PATCHWELCOME;
7178 }
7179
7180 if (mov->flags & FF_MOV_FLAG_FRAGMENT || mov->mode == MODE_AVIF) {
7181 int ret;
7182 if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
7183 if (mov->frag_interleave && mov->fragments > 0) {
7184 if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
7185 if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
7186 return ret;
7187 }
7188 }
7189
7190 if (!trk->mdat_buf) {
7191 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
7192 return ret;
7193 }
7194 pb = trk->mdat_buf;
7195 } else {
7196 if (!mov->mdat_buf) {
7197 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
7198 return ret;
7199 }
7200 pb = mov->mdat_buf;
7201 }
7202 }
7203
7204 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
7205 /* We must find out how many AMR blocks there are in one packet */
7206 static const uint16_t packed_size[16] =
7207 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
7208 int len = 0;
7209
7210 while (len < size && samples_in_chunk < 100) {
7211 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
7212 samples_in_chunk++;
7213 }
7214 if (samples_in_chunk > 1) {
7215 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
7216 return -1;
7217 }
7218 } else if (par->codec_id == AV_CODEC_ID_ADPCM_MS ||
7220 samples_in_chunk = trk->par->frame_size;
7221 } else if (trk->sample_size)
7222 samples_in_chunk = size / trk->sample_size;
7223 else
7224 samples_in_chunk = 1;
7225
7226 if (samples_in_chunk < 1) {
7227 av_log(s, AV_LOG_ERROR, "fatal error, input packet contains no samples\n");
7228 return AVERROR_PATCHWELCOME;
7229 }
7230
7231 /* copy extradata if it exists */
7232 if (trk->extradata_size[0] == 0 && par->extradata_size > 0 &&
7233 !TAG_IS_AVCI(trk->tag) &&
7234 (par->codec_id != AV_CODEC_ID_DNXHD)) {
7235 trk->extradata[0] = av_memdup(par->extradata, par->extradata_size);
7236 if (!trk->extradata[0]) {
7237 ret = AVERROR(ENOMEM);
7238 goto err;
7239 }
7240 trk->extradata_size[0] = par->extradata_size;
7241 }
7242
7243 if ((par->codec_id == AV_CODEC_ID_DNXHD ||
7244 par->codec_id == AV_CODEC_ID_H264 ||
7245 par->codec_id == AV_CODEC_ID_HEVC ||
7246 par->codec_id == AV_CODEC_ID_VVC ||
7247 par->codec_id == AV_CODEC_ID_VP9 ||
7248 par->codec_id == AV_CODEC_ID_EVC ||
7249 par->codec_id == AV_CODEC_ID_LCEVC ||
7250 par->codec_id == AV_CODEC_ID_TRUEHD) && !trk->extradata_size[0] &&
7251 !TAG_IS_AVCI(trk->tag)) {
7252 /* copy frame to create needed atoms */
7253 trk->extradata_size[0] = size;
7255 if (!trk->extradata[0]) {
7256 ret = AVERROR(ENOMEM);
7257 goto err;
7258 }
7259 memcpy(trk->extradata[0], pkt->data, size);
7260 memset(trk->extradata[0] + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
7261 }
7262
7263 const AVPacketSideData *sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems, AV_PKT_DATA_NEW_EXTRADATA);
7264 if (pkt->size && sd && sd->size > 0) {
7265 int i;
7266 for (i = 0; i < trk->stsd_count; i++) {
7267 if (trk->extradata_size[i] == sd->size && !memcmp(trk->extradata[i], sd->data, sd->size))
7268 break;
7269 }
7270
7271 if (i < trk->stsd_count)
7272 trk->last_stsd_index = i;
7273 else if (trk->stsd_count <= INT_MAX - 1) {
7274 int new_count = trk->stsd_count + 1;
7275 uint8_t **extradata = av_realloc_array(trk->extradata, new_count, sizeof(*trk->extradata));
7276 if (!extradata)
7277 return AVERROR(ENOMEM);
7278 trk->extradata = extradata;
7279
7280 int *extradata_size = av_realloc_array(trk->extradata_size, new_count, sizeof(*trk->extradata_size));
7281 if (!extradata_size)
7282 return AVERROR(ENOMEM);
7283 trk->extradata_size = extradata_size;
7284
7285 trk->extradata[trk->stsd_count] = av_memdup(sd->data, sd->size);
7286 if (!trk->extradata[trk->stsd_count])
7287 return AVERROR(ENOMEM);
7288
7289 trk->extradata_size[trk->stsd_count] = sd->size;
7290 trk->last_stsd_index = trk->stsd_count;
7291 trk->stsd_count = new_count;
7292 } else
7293 return AVERROR(ENOMEM);
7294 }
7295
7296 if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
7297 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
7298 if (!trk->st->nb_frames) {
7299 av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
7300 "use the audio bitstream filter 'aac_adtstoasc' to fix it "
7301 "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
7302 return -1;
7303 }
7304 av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
7305 }
7306 if (par->codec_id == AV_CODEC_ID_H264 && trk->extradata_size[trk->last_stsd_index] > 0 &&
7307 *(uint8_t *)trk->extradata[trk->last_stsd_index] != 1 && !TAG_IS_AVCI(trk->tag)) {
7308 /* from x264 or from bytestream H.264 */
7309 /* NAL reformatting needed */
7310 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7311 ret = ff_nal_parse_units_buf(pkt->data, &reformatted_data,
7312 &size);
7313 if (ret < 0)
7314 return ret;
7315 avio_write(pb, reformatted_data, size);
7316 } else {
7317 if (trk->cenc.aes_ctr) {
7318 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
7319 if (size < 0) {
7320 ret = size;
7321 goto err;
7322 }
7323 } else {
7324 size = ff_nal_parse_units(pb, pkt->data, pkt->size);
7325 }
7326 }
7327 } else if (par->codec_id == AV_CODEC_ID_HEVC && trk->extradata_size[trk->last_stsd_index] > 6 &&
7328 (AV_RB24(trk->extradata[trk->last_stsd_index]) == 1 || AV_RB32(trk->extradata[trk->last_stsd_index]) == 1)) {
7329 /* extradata is Annex B, assume the bitstream is too and convert it */
7330 int filter_ps = (trk->tag == MKTAG('h','v','c','1'));
7331 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7332 ret = ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data,
7333 &size, filter_ps, NULL);
7334 if (ret < 0)
7335 return ret;
7336 avio_write(pb, reformatted_data, size);
7337 } else {
7338 if (trk->cenc.aes_ctr) {
7339 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
7340 if (size < 0) {
7341 ret = size;
7342 goto err;
7343 }
7344 } else {
7345 size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, filter_ps, NULL);
7346 }
7347 }
7348 } else if (par->codec_id == AV_CODEC_ID_VVC && trk->extradata_size[trk->last_stsd_index] > 6 &&
7349 (AV_RB24(trk->extradata[trk->last_stsd_index]) == 1 || AV_RB32(trk->extradata[trk->last_stsd_index]) == 1)) {
7350 /* extradata is Annex B, assume the bitstream is too and convert it */
7351 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7352 ret = ff_vvc_annexb2mp4_buf(pkt->data, &reformatted_data,
7353 &size, 0, NULL);
7354 if (ret < 0)
7355 return ret;
7356 avio_write(pb, reformatted_data, size);
7357 } else {
7358 size = ff_vvc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
7359 }
7360 } else if (par->codec_id == AV_CODEC_ID_LCEVC && trk->extradata_size[trk->last_stsd_index] > 0 &&
7361 *(uint8_t *)trk->extradata[trk->last_stsd_index] != 1) {
7362 /* extradata is Annex B, assume the bitstream is too and convert it */
7363 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7364 ret = ff_nal_parse_units_buf(pkt->data, &reformatted_data, &size);
7365 if (ret < 0)
7366 return ret;
7367 avio_write(pb, reformatted_data, size);
7368 } else {
7369 if (trk->cenc.aes_ctr) {
7370 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
7371 if (size < 0) {
7372 ret = size;
7373 goto err;
7374 }
7375 } else {
7376 size = ff_nal_parse_units(pb, pkt->data, pkt->size);
7377 }
7378 }
7379 } else if (par->codec_id == AV_CODEC_ID_AV1 && !trk->cenc.aes_ctr) {
7380 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
7381 ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data,
7382 &size, &offset);
7383 if (ret < 0)
7384 return ret;
7385 avio_write(pb, reformatted_data, size);
7386 } else {
7387 size = ff_av1_filter_obus(pb, pkt->data, pkt->size);
7388 if (trk->mode == MODE_AVIF && !mov->avif_extent_length[pkt->stream_index]) {
7389 mov->avif_extent_length[pkt->stream_index] = size;
7390 }
7391 }
7392
7393 } else if (par->codec_id == AV_CODEC_ID_AC3 ||
7394 par->codec_id == AV_CODEC_ID_EAC3) {
7395 size = handle_eac3(mov, pkt, trk);
7396 if (size < 0)
7397 return size;
7398 else if (!size)
7399 goto end;
7400 avio_write(pb, pkt->data, size);
7401 } else if (par->codec_id == AV_CODEC_ID_EIA_608) {
7402 size = 8;
7403
7404 for (int i = 0; i < pkt->size; i += 3) {
7405 if (pkt->data[i] == 0xFC) {
7406 size += 2;
7407 }
7408 }
7409 avio_wb32(pb, size);
7410 ffio_wfourcc(pb, "cdat");
7411 for (int i = 0; i < pkt->size; i += 3) {
7412 if (pkt->data[i] == 0xFC) {
7413 avio_w8(pb, pkt->data[i + 1]);
7414 avio_w8(pb, pkt->data[i + 2]);
7415 }
7416 }
7417 } else if (par->codec_id == AV_CODEC_ID_APV) {
7418 ff_isom_parse_apvc(trk->apv, pkt, s);
7419 avio_wb32(s->pb, pkt->size);
7420 size += 4;
7421
7422 avio_write(s->pb, pkt->data, pkt->size);
7423 } else {
7424 if (trk->cenc.aes_ctr) {
7425 uint8_t *extradata = trk->extradata[trk->last_stsd_index];
7426 int extradata_size = trk->extradata_size[trk->last_stsd_index];
7427 if (par->codec_id == AV_CODEC_ID_H264 && extradata_size > 4) {
7428 int nal_size_length = (extradata[4] & 0x3) + 1;
7429 ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
7430 } else if(par->codec_id == AV_CODEC_ID_HEVC && extradata_size > 21) {
7431 int nal_size_length = (extradata[21] & 0x3) + 1;
7432 ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
7433 } else if(par->codec_id == AV_CODEC_ID_VVC) {
7435 } else if(par->codec_id == AV_CODEC_ID_AV1) {
7436 av_assert0(size == pkt->size);
7437 ret = ff_mov_cenc_av1_write_obus(s, &trk->cenc, pb, pkt);
7438 if (ret > 0) {
7439 size = ret;
7440 ret = 0;
7441 }
7442 } else {
7443 ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size);
7444 }
7445
7446 if (ret) {
7447 goto err;
7448 }
7449 } else {
7450 avio_write(pb, pkt->data, size);
7451 }
7452 }
7453
7454 if (trk->entry >= trk->cluster_capacity) {
7455 unsigned new_capacity = trk->entry + MOV_INDEX_CLUSTER_SIZE;
7456 void *cluster = av_realloc_array(trk->cluster, new_capacity, sizeof(*trk->cluster));
7457 if (!cluster) {
7458 ret = AVERROR(ENOMEM);
7459 goto err;
7460 }
7461 trk->cluster = cluster;
7462 trk->cluster_capacity = new_capacity;
7463 }
7464
7465 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
7466 trk->cluster[trk->entry].stsd_index = trk->last_stsd_index;
7467 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
7468 trk->cluster[trk->entry].chunkNum = 0;
7469 trk->cluster[trk->entry].size = size;
7470 trk->cluster[trk->entry].entries = samples_in_chunk;
7471 trk->cluster[trk->entry].dts = pkt->dts;
7472 trk->cluster[trk->entry].pts = pkt->pts;
7474 !trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
7475 if (!trk->frag_discont) {
7476 /* First packet of a new fragment. We already wrote the duration
7477 * of the last packet of the previous fragment based on track_duration,
7478 * which might not exactly match our dts. Therefore adjust the dts
7479 * of this packet to be what the previous packets duration implies. */
7480 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
7481 /* We also may have written the pts and the corresponding duration
7482 * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
7483 * the next fragment. This means the cts of the first sample must
7484 * be the same in all fragments, unless end_pts was updated by
7485 * the packet causing the fragment to be written. */
7486 if ((mov->flags & FF_MOV_FLAG_DASH &&
7488 mov->mode == MODE_ISM)
7489 pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
7490 } else {
7491 /* New fragment, but discontinuous from previous fragments.
7492 * Pretend the duration sum of the earlier fragments is
7493 * pkt->dts - trk->start_dts. */
7494 trk->end_pts = trk->elst_end_pts = AV_NOPTS_VALUE;
7495 trk->frag_discont = 0;
7496 }
7497 }
7498
7499 if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
7500 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
7501 /* Not using edit lists and shifting the first track to start from zero.
7502 * If the other streams start from a later timestamp, we won't be able
7503 * to signal the difference in starting time without an edit list.
7504 * Thus move the timestamp for this first sample to 0, increasing
7505 * its duration instead. */
7506 trk->cluster[trk->entry].dts = trk->start_dts = 0;
7507 }
7508 if (trk->start_dts == AV_NOPTS_VALUE) {
7509 trk->start_dts = pkt->dts;
7510 if (trk->frag_discont) {
7511 if (mov->use_editlist) {
7512 /* Pretend the whole stream started at pts=0, with earlier fragments
7513 * already written. If the stream started at pts=0, the duration sum
7514 * of earlier fragments would have been pkt->pts. */
7515 trk->start_dts = pkt->dts - pkt->pts;
7516 } else {
7517 /* Pretend the whole stream started at dts=0, with earlier fragments
7518 * already written, with a duration summing up to pkt->dts. */
7519 trk->start_dts = 0;
7520 }
7521 trk->frag_discont = 0;
7522 } else if (pkt->dts && mov->moov_written)
7524 "Track %d starts with a nonzero dts %"PRId64", while the moov "
7525 "already has been written. Set the delay_moov flag to handle "
7526 "this case.\n",
7527 pkt->stream_index, pkt->dts);
7528 }
7529
7530 sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems, AV_PKT_DATA_SKIP_SAMPLES);
7531 if (sd && sd->size >= 10 && trk->par->frame_size) {
7532 int coded_samples = av_get_audio_frame_duration2(trk->par, pkt->size);
7533 if (coded_samples <= 0)
7534 coded_samples = trk->par->frame_size;
7535 duration = FFMAX(av_rescale_q(coded_samples, (AVRational){ 1, trk->par->sample_rate },
7536 trk->st->time_base), pkt->duration);
7537 if (mov->use_editlist)
7538 pkt->duration = duration;
7539 duration -= av_rescale_q(AV_RL32(sd->data + 4), (AVRational){ 1, trk->par->sample_rate },
7540 trk->st->time_base);
7541 if (duration < 0)
7542 return AVERROR_INVALIDDATA;
7543 } else
7544 duration = pkt->duration;
7545
7546 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
7548
7549 if (pkt->pts == AV_NOPTS_VALUE) {
7550 av_log(s, AV_LOG_WARNING, "pts has no value\n");
7551 pkt->pts = pkt->dts;
7552 }
7553 if (pkt->dts != pkt->pts)
7554 trk->flags |= MOV_TRACK_CTTS;
7555 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
7556 trk->cluster[trk->entry].flags = 0;
7557 if (trk->start_cts == AV_NOPTS_VALUE || (pkt->dts <= 0 && trk->start_cts > pkt->pts - pkt->dts))
7558 trk->start_cts = pkt->pts - pkt->dts;
7559 if (trk->end_pts == AV_NOPTS_VALUE)
7560 trk->end_pts = trk->cluster[trk->entry].dts +
7561 trk->cluster[trk->entry].cts + duration;
7562 else
7563 trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
7564 trk->cluster[trk->entry].cts +
7565 duration);
7566 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
7567 trk->elst_end_pts = trk->end_pts;
7568
7569 if (par->codec_id == AV_CODEC_ID_VC1) {
7571 } else if (par->codec_id == AV_CODEC_ID_TRUEHD) {
7573 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
7574 if (mov->mode == MODE_MOV && par->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
7575 trk->entry > 0) { // force sync sample for the first key frame
7577 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
7578 trk->flags |= MOV_TRACK_STPS;
7579 } else {
7580 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
7581 }
7582 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
7583 trk->has_keyframes++;
7584 }
7585 if (pkt->flags & AV_PKT_FLAG_DISPOSABLE) {
7587 trk->has_disposable++;
7588 }
7589
7591 if (prft && prft_size == sizeof(AVProducerReferenceTime))
7592 memcpy(&trk->cluster[trk->entry].prft, prft, prft_size);
7593 else
7594 memset(&trk->cluster[trk->entry].prft, 0, sizeof(AVProducerReferenceTime));
7595
7596 trk->entry++;
7597 trk->sample_count += samples_in_chunk;
7598 mov->mdat_size += size;
7599
7600 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks)
7602 reformatted_data ? reformatted_data + offset
7603 : NULL, size);
7604
7605end:
7606err:
7607
7608 if (pkt->data != reformatted_data)
7609 av_free(reformatted_data);
7610 return ret;
7611}
7612
7614{
7615 MOVMuxContext *mov = s->priv_data;
7616 MOVTrack *trk = s->streams[pkt->stream_index]->priv_data;
7617 AVCodecParameters *par = trk->par;
7618 int64_t frag_duration = 0;
7619 int size = pkt->size;
7620
7621 int ret = check_pkt(s, trk, pkt);
7622 if (ret < 0)
7623 return ret;
7624
7625 if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
7626 for (int i = 0; i < mov->nb_streams; i++)
7627 mov->tracks[i].frag_discont = 1;
7629 }
7630
7632 if (trk->dts_shift == AV_NOPTS_VALUE)
7633 trk->dts_shift = pkt->pts - pkt->dts;
7634 pkt->dts += trk->dts_shift;
7635 }
7636
7637 if (trk->par->codec_id == AV_CODEC_ID_MP4ALS ||
7638 trk->par->codec_id == AV_CODEC_ID_AAC ||
7639 trk->par->codec_id == AV_CODEC_ID_AV1 ||
7640 trk->par->codec_id == AV_CODEC_ID_FLAC) {
7641 size_t side_size;
7642 uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
7643 /* Overwrite extradata only on flush packets or when no extradata was available during init */
7644 if (side_size > 0 && (!pkt->size || !trk->extradata_size[trk->last_stsd_index])) {
7645 void *newextra = av_malloc(side_size + AV_INPUT_BUFFER_PADDING_SIZE);
7646 if (!newextra)
7647 return AVERROR(ENOMEM);
7648 memset((uint8_t*)newextra + side_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
7649 memcpy(newextra, side, side_size);
7650 av_free(trk->extradata[trk->last_stsd_index]);
7651 trk->extradata[trk->last_stsd_index] = newextra;
7652 trk->extradata_size[trk->last_stsd_index] = side_size;
7653 }
7654 }
7655
7656 if (!pkt->size) {
7657 if (trk->start_dts == AV_NOPTS_VALUE && trk->frag_discont) {
7658 trk->start_dts = pkt->dts;
7659 if (pkt->pts != AV_NOPTS_VALUE)
7660 trk->start_cts = pkt->pts - pkt->dts;
7661 else
7662 trk->start_cts = 0;
7663 }
7664
7665 return 0; /* Discard 0 sized packets */
7666 }
7667
7668 if (trk->entry && pkt->stream_index < mov->nb_streams)
7669 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
7670 s->streams[pkt->stream_index]->time_base,
7672 if ((mov->max_fragment_duration &&
7673 frag_duration >= mov->max_fragment_duration) ||
7674 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
7677 trk->entry && pkt->flags & AV_PKT_FLAG_KEY) ||
7679 if (frag_duration >= mov->min_fragment_duration) {
7680 if (trk->entry) {
7681 // Set the duration of this track to line up with the next
7682 // sample in this track. This avoids relying on AVPacket
7683 // duration, but only helps for this particular track, not
7684 // for the other ones that are flushed at the same time.
7685 //
7686 // If we have trk->entry == 0, no fragment will be written
7687 // for this track, and we can't adjust the track end here.
7688 trk->track_duration = pkt->dts - trk->start_dts;
7689 if (pkt->pts != AV_NOPTS_VALUE)
7690 trk->end_pts = pkt->pts;
7691 else
7692 trk->end_pts = pkt->dts;
7693 if (!(pkt->flags & AV_PKT_FLAG_DISCARD))
7694 trk->elst_end_pts = trk->end_pts;
7695 trk->end_reliable = 1;
7696 }
7698 }
7699 }
7700
7701 return ff_mov_write_packet(s, pkt);
7702}
7703
7705 int stream_index,
7706 int64_t dts) {
7707 MOVMuxContext *mov = s->priv_data;
7708 AVPacket *end = mov->pkt;
7709 uint8_t data[2] = {0};
7710 int ret;
7711
7712 end->size = sizeof(data);
7713 end->data = data;
7714 end->pts = dts;
7715 end->dts = dts;
7716 end->duration = 0;
7717 end->stream_index = stream_index;
7718
7720 av_packet_unref(end);
7721
7722 return ret;
7723}
7724
7725#if CONFIG_IAMFENC
7726static int mov_build_iamf_packet(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt)
7727{
7728 uint8_t *data;
7729 int ret;
7730
7731 if (pkt->stream_index == trk->first_iamf_idx) {
7733 if (ret < 0)
7734 return ret;
7735 }
7736
7738 s->streams[pkt->stream_index]->id, pkt);
7739 if (ret < 0)
7740 return ret;
7741
7742 if (pkt->stream_index != trk->last_iamf_idx)
7743 return AVERROR(EAGAIN);
7744
7746 trk->iamf_buf = NULL;
7747 if (!ret) {
7748 if (pkt->size) {
7749 // Either all or none of the packets for a single
7750 // IA Sample may be empty.
7751 av_log(s, AV_LOG_ERROR, "Unexpected packet from "
7752 "stream #%d\n", pkt->stream_index);
7754 }
7755 av_free(data);
7756 return ret;
7757 }
7758
7759 av_buffer_unref(&pkt->buf);
7760 pkt->buf = av_buffer_create(data, ret, NULL, NULL, 0);
7761 if (!pkt->buf) {
7762 av_free(data);
7763 return AVERROR(ENOMEM);
7764 }
7765 pkt->data = data;
7766 pkt->size = ret;
7767 pkt->stream_index = trk->first_iamf_idx;
7768
7769 return avio_open_dyn_buf(&trk->iamf_buf);
7770}
7771#endif
7772
7774{
7775 int64_t pos = avio_tell(pb);
7776 const char *scheme_id_uri = "https://aomedia.org/emsg/ID3";
7777 const char *value = "";
7778
7779 av_assert0(st->time_base.num == 1);
7780
7784
7785 avio_wb32(pb, 0); /* size */
7786 ffio_wfourcc(pb, "emsg");
7787 avio_w8(pb, 1); /* version */
7788 avio_wb24(pb, 0);
7789 avio_wb32(pb, st->time_base.den); /* timescale */
7790 avio_wb64(pb, pkt->pts); /* presentation_time */
7791 avio_wb32(pb, 0xFFFFFFFFU); /* event_duration */
7792 avio_wb32(pb, 0); /* id */
7793 /* null terminated UTF8 strings */
7794 avio_write(pb, scheme_id_uri, strlen(scheme_id_uri) + 1);
7795 avio_write(pb, value, strlen(value) + 1);
7796 avio_write(pb, pkt->data, pkt->size);
7797
7798 return update_size(pb, pos);
7799}
7800
7802{
7803 MOVMuxContext *mov = s->priv_data;
7804 MOVTrack *trk;
7805
7806 if (!pkt) {
7808 return 1;
7809 }
7810
7811 if (s->streams[pkt->stream_index]->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
7812 mov_write_emsg_tag(s->pb, s->streams[pkt->stream_index], pkt);
7813 return 0;
7814 }
7815
7816 trk = s->streams[pkt->stream_index]->priv_data;
7817
7818#if CONFIG_IAMFENC
7819 if (trk->iamf) {
7820 int ret = mov_build_iamf_packet(s, trk, pkt);
7821 if (ret < 0) {
7822 if (ret == AVERROR(EAGAIN))
7823 return 0;
7824 av_log(s, AV_LOG_ERROR, "Error assembling an IAMF packet "
7825 "for stream #%d\n", trk->st->index);
7826 return ret;
7827 }
7828 }
7829#endif
7830
7831 if (is_cover_image(trk->st)) {
7832 int ret;
7833
7834 if (trk->st->nb_frames >= 1) {
7835 if (trk->st->nb_frames == 1)
7836 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
7837 " ignoring.\n", pkt->stream_index);
7838 return 0;
7839 }
7840
7841 if ((ret = av_packet_ref(trk->cover_image, pkt)) < 0)
7842 return ret;
7843
7844 return 0;
7845 } else {
7846 int i;
7847
7848 if (!pkt->size)
7849 return mov_write_single_packet(s, pkt); /* Passthrough. */
7850
7851 /*
7852 * Subtitles require special handling.
7853 *
7854 * 1) For full compliance, every track must have a sample at
7855 * dts == 0, which is rarely true for subtitles. So, as soon
7856 * as we see any packet with dts > 0, write an empty subtitle
7857 * at dts == 0 for any subtitle track with no samples in it.
7858 *
7859 * 2) For each subtitle track, check if the current packet's
7860 * dts is past the duration of the last subtitle sample. If
7861 * so, we now need to write an end sample for that subtitle.
7862 *
7863 * This must be done conditionally to allow for subtitles that
7864 * immediately replace each other, in which case an end sample
7865 * is not needed, and is, in fact, actively harmful.
7866 *
7867 * 3) See mov_write_trailer for how the final end sample is
7868 * handled.
7869 */
7870 for (i = 0; i < mov->nb_tracks; i++) {
7871 MOVTrack *trk = &mov->tracks[i];
7872 int ret;
7873
7874 if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
7875 trk->track_duration < pkt->dts &&
7876 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
7878 if (ret < 0) return ret;
7880 }
7881 }
7882
7884 /*
7885 * If the track has to have its samples squashed into one sample,
7886 * we just take it into the track's queue.
7887 * This will then be utilized as the samples get written in either
7888 * mov_flush_fragment or when the mux is finalized in
7889 * mov_write_trailer.
7890 */
7891 int ret = AVERROR_BUG;
7892
7893 if (pkt->pts == AV_NOPTS_VALUE) {
7895 "Packets without a valid presentation timestamp are "
7896 "not supported with packet squashing!\n");
7897 return AVERROR(EINVAL);
7898 }
7899
7900 /* The following will reset pkt and is only allowed to be used
7901 * because we return immediately. afterwards. */
7903 pkt, NULL, 0)) < 0) {
7904 return ret;
7905 }
7906
7907 return 0;
7908 }
7909
7910
7911 if (trk->mode == MODE_MOV && trk->par->codec_type == AVMEDIA_TYPE_VIDEO) {
7912 AVPacket *opkt = pkt;
7913 int reshuffle_ret, ret;
7914 if (trk->is_unaligned_qt_rgb) {
7915 int64_t bpc = trk->par->bits_per_coded_sample != 15 ? trk->par->bits_per_coded_sample : 16;
7916 int expected_stride = ((trk->par->width * bpc + 15) >> 4)*2;
7917 reshuffle_ret = ff_reshuffle_raw_rgb(s, &pkt, trk->par, expected_stride);
7918 if (reshuffle_ret < 0)
7919 return reshuffle_ret;
7920 } else
7921 reshuffle_ret = 0;
7922 if (trk->par->format == AV_PIX_FMT_PAL8 && !trk->pal_done) {
7923 ret = ff_get_packet_palette(s, opkt, reshuffle_ret, trk->palette);
7924 if (ret < 0)
7925 goto fail;
7926 if (ret)
7927 trk->pal_done++;
7928 } else if (trk->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
7929 (trk->par->format == AV_PIX_FMT_GRAY8 ||
7930 trk->par->format == AV_PIX_FMT_MONOBLACK)) {
7932 if (ret < 0)
7933 goto fail;
7934 for (i = 0; i < pkt->size; i++)
7935 pkt->data[i] = ~pkt->data[i];
7936 }
7937 if (reshuffle_ret) {
7939fail:
7940 if (reshuffle_ret)
7942 return ret;
7943 }
7944 }
7945
7946 return mov_write_single_packet(s, pkt);
7947 }
7948}
7949
7950// QuickTime chapters involve an additional text track with the chapter names
7951// as samples, and a tref pointing from the other tracks to the chapter one.
7953{
7954 static const uint8_t stub_header[] = {
7955 // TextSampleEntry
7956 0x00, 0x00, 0x00, 0x01, // displayFlags
7957 0x00, 0x00, // horizontal + vertical justification
7958 0x00, 0x00, 0x00, 0x00, // bgColourRed/Green/Blue/Alpha
7959 // BoxRecord
7960 0x00, 0x00, 0x00, 0x00, // defTextBoxTop/Left
7961 0x00, 0x00, 0x00, 0x00, // defTextBoxBottom/Right
7962 // StyleRecord
7963 0x00, 0x00, 0x00, 0x00, // startChar + endChar
7964 0x00, 0x01, // fontID
7965 0x00, 0x00, // fontStyleFlags + fontSize
7966 0x00, 0x00, 0x00, 0x00, // fgColourRed/Green/Blue/Alpha
7967 // FontTableBox
7968 0x00, 0x00, 0x00, 0x0D, // box size
7969 'f', 't', 'a', 'b', // box atom name
7970 0x00, 0x01, // entry count
7971 // FontRecord
7972 0x00, 0x01, // font ID
7973 0x00, // font name length
7974 };
7975 MOVMuxContext *mov = s->priv_data;
7976 MOVTrack *track = &mov->tracks[tracknum];
7977 AVPacket *pkt = mov->pkt;
7978 int i, len;
7979 int ret;
7980
7981 track->mode = mov->mode;
7982 track->tag = MKTAG('t','e','x','t');
7983 track->timescale = mov->movie_timescale;
7984 track->par = avcodec_parameters_alloc();
7985 if (!track->par)
7986 return AVERROR(ENOMEM);
7988 ret = ff_alloc_extradata(track->par, sizeof(stub_header));
7989 if (ret < 0)
7990 return ret;
7991 memcpy(track->par->extradata, stub_header, sizeof(stub_header));
7992
7993 if (track->extradata == NULL) {
7994 track->stsd_count = 1;
7995 track->extradata = av_calloc(1, sizeof(*track->extradata));
7996 track->extradata_size = av_calloc(1, sizeof(*track->extradata_size));
7997 if (!track->extradata || !track->extradata_size)
7998 return AVERROR(ENOMEM);
7999 }
8000
8001 track->extradata[0] = av_memdup(stub_header, sizeof(stub_header));
8002 if (!track->extradata[0])
8003 return AVERROR(ENOMEM);
8004 track->extradata_size[0] = sizeof(stub_header);
8005
8006 pkt->stream_index = tracknum;
8007 pkt->flags = AV_PKT_FLAG_KEY;
8008
8009 for (i = 0; i < s->nb_chapters; i++) {
8010 AVChapter *c = s->chapters[i];
8012
8013 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,mov->movie_timescale});
8014 pkt->pts = pkt->dts = av_rescale_q(c->start, c->time_base, (AVRational){1,mov->movie_timescale});
8015 pkt->duration = end - pkt->dts;
8016
8017 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
8018 static const char encd[12] = {
8019 0x00, 0x00, 0x00, 0x0C,
8020 'e', 'n', 'c', 'd',
8021 0x00, 0x00, 0x01, 0x00 };
8022 len = strlen(t->value);
8023 pkt->size = len + 2 + 12;
8024 pkt->data = av_malloc(pkt->size);
8025 if (!pkt->data) {
8027 return AVERROR(ENOMEM);
8028 }
8029 AV_WB16(pkt->data, len);
8030 memcpy(pkt->data + 2, t->value, len);
8031 memcpy(pkt->data + len + 2, encd, sizeof(encd));
8033 av_freep(&pkt->data);
8034 }
8035 }
8036
8037 av_packet_unref(mov->pkt);
8038
8039 return 0;
8040}
8041
8042
8043static int mov_check_timecode_track(AVFormatContext *s, AVTimecode *tc, AVStream *src_st, const char *tcstr)
8044{
8045 int ret;
8046
8047 /* compute the frame number */
8048 ret = av_timecode_init_from_string(tc, src_st->avg_frame_rate, tcstr, s);
8049 return ret;
8050}
8051
8052static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, AVTimecode tc)
8053{
8054 MOVMuxContext *mov = s->priv_data;
8055 MOVTrack *track = &mov->tracks[index];
8056 AVStream *src_st = mov->tracks[src_index].st;
8057 uint8_t data[4];
8058 AVPacket *pkt = mov->pkt;
8059 AVRational rate = src_st->avg_frame_rate;
8060 int ret;
8061
8062 /* tmcd track based on video stream */
8063 track->mode = mov->mode;
8064 track->tag = MKTAG('t','m','c','d');
8065 track->src_track = av_malloc(sizeof(*track->src_track));
8066 if (!track->src_track)
8067 return AVERROR(ENOMEM);
8068 *track->src_track = src_index;
8069 track->nb_src_track = 1;
8071 track->timescale = tc.fps * 1000;
8072 else if (tc.rate.den == 1001)
8073 track->timescale = tc.rate.num;
8074 else
8075 track->timescale = tc.fps;
8078
8079 /* set st to src_st for metadata access*/
8080 track->st = src_st;
8081
8082 /* encode context: tmcd data stream */
8083 track->par = avcodec_parameters_alloc();
8084 if (!track->par)
8085 return AVERROR(ENOMEM);
8087 track->par->codec_tag = track->tag;
8088 track->st->avg_frame_rate = rate;
8089
8090 /* the tmcd track just contains one packet with the frame number */
8091 pkt->data = data;
8092 pkt->stream_index = index;
8093 pkt->flags = AV_PKT_FLAG_KEY;
8094 pkt->pts = pkt->dts = av_rescale_q(tc.start, av_inv_q(rate), (AVRational){ 1, track->timescale });
8095 pkt->size = 4;
8096 AV_WB32(pkt->data, tc.start);
8099 return ret;
8100}
8101
8102/*
8103 * st->disposition controls the "enabled" flag in the tkhd tag.
8104 * QuickTime will not play a track if it is not enabled. So make sure
8105 * that one track of each type (audio, video, subtitle) is enabled.
8106 *
8107 * Subtitles are special. For audio and video, setting "enabled" also
8108 * makes the track "default" (i.e. it is rendered when played). For
8109 * subtitles, an "enabled" subtitle is not rendered by default, but
8110 * if no subtitle is enabled, the subtitle menu in QuickTime will be
8111 * empty!
8112 */
8114{
8115 MOVMuxContext *mov = s->priv_data;
8116 int i;
8117 int enabled[AVMEDIA_TYPE_NB];
8118 int first[AVMEDIA_TYPE_NB];
8119
8120 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
8121 enabled[i] = 0;
8122 first[i] = -1;
8123 }
8124
8125 for (i = 0; i < mov->nb_streams; i++) {
8126 AVStream *st = mov->tracks[i].st;
8127
8130 is_cover_image(st))
8131 continue;
8132
8133 if (first[st->codecpar->codec_type] < 0)
8134 first[st->codecpar->codec_type] = i;
8137 enabled[st->codecpar->codec_type]++;
8138 }
8139 }
8140
8141 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
8142 switch (i) {
8143 case AVMEDIA_TYPE_VIDEO:
8144 case AVMEDIA_TYPE_AUDIO:
8146 if (enabled[i] > 1)
8147 mov->per_stream_grouping = 1;
8148 if (!enabled[i] && first[i] >= 0)
8149 mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
8150 break;
8151 }
8152 }
8153}
8154
8156{
8157 MOVMuxContext *mov = s->priv_data;
8158
8159 for (int i = 0; i < s->nb_streams; i++)
8160 s->streams[i]->priv_data = NULL;
8161
8162 if (!mov->tracks)
8163 return;
8164
8165 if (mov->chapter_track) {
8167 }
8168
8169 for (int i = 0; i < mov->nb_tracks; i++) {
8170 MOVTrack *const track = &mov->tracks[i];
8171
8172 if (track->tag == MKTAG('r','t','p',' '))
8173 ff_mov_close_hinting(track);
8174 else if (track->tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
8175 av_freep(&track->par);
8176 av_freep(&track->cluster);
8177 av_freep(&track->cluster_written);
8178 av_freep(&track->frag_info);
8179 av_packet_free(&track->cover_image);
8180
8181 if (track->eac3_priv) {
8182 struct eac3_info *info = track->eac3_priv;
8183 av_packet_free(&info->pkt);
8184 av_freep(&track->eac3_priv);
8185 }
8186 for (int j = 0; j < track->stsd_count; j++)
8187 av_freep(&track->extradata[j]);
8188 av_freep(&track->extradata);
8189 av_freep(&track->extradata_size);
8190
8191 for (int i = 0; i < track->nb_tref_tags; i++)
8192 av_freep(&track->tref_tags[i].id);
8193 av_freep(&track->tref_tags);
8194 av_freep(&track->src_track);
8195
8196 ff_mov_cenc_free(&track->cenc);
8197 ffio_free_dyn_buf(&track->mdat_buf);
8198
8199#if CONFIG_IAMFENC
8200 ffio_free_dyn_buf(&track->iamf_buf);
8201 if (track->iamf)
8203 av_freep(&track->iamf);
8204#endif
8205 ff_isom_close_apvc(&track->apv);
8206
8208 }
8209
8210 av_freep(&mov->tracks);
8212}
8213
8214static uint32_t rgb_to_yuv(uint32_t rgb)
8215{
8216 uint8_t r, g, b;
8217 int y, cb, cr;
8218
8219 r = (rgb >> 16) & 0xFF;
8220 g = (rgb >> 8) & 0xFF;
8221 b = (rgb ) & 0xFF;
8222
8223 y = av_clip_uint8(( 16000 + 257 * r + 504 * g + 98 * b)/1000);
8224 cb = av_clip_uint8((128000 - 148 * r - 291 * g + 439 * b)/1000);
8225 cr = av_clip_uint8((128000 + 439 * r - 368 * g - 71 * b)/1000);
8226
8227 return (y << 16) | (cr << 8) | cb;
8228}
8229
8231 AVStream *st)
8232{
8233 int i, width = 720, height = 480;
8234 int have_palette = 0, have_size = 0;
8235 uint32_t palette[16];
8236 char *cur = st->codecpar->extradata;
8237
8238 while (cur && *cur) {
8239 if (strncmp("palette:", cur, 8) == 0) {
8240 int i, count;
8241 count = sscanf(cur + 8,
8242 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
8243 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
8244 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
8245 "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
8246 &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
8247 &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
8248 &palette[ 8], &palette[ 9], &palette[10], &palette[11],
8249 &palette[12], &palette[13], &palette[14], &palette[15]);
8250
8251 for (i = 0; i < count; i++) {
8253 }
8254 have_palette = 1;
8255 } else if (!strncmp("size:", cur, 5)) {
8256 sscanf(cur + 5, "%dx%d", &width, &height);
8257 have_size = 1;
8258 }
8259 if (have_palette && have_size)
8260 break;
8261 cur += strcspn(cur, "\n\r");
8262 cur += strspn(cur, "\n\r");
8263 }
8264 if (have_palette) {
8266 if (!track->extradata[track->last_stsd_index])
8267 return AVERROR(ENOMEM);
8268 for (i = 0; i < 16; i++) {
8269 AV_WB32(track->extradata[track->last_stsd_index] + i * 4, palette[i]);
8270 }
8271 memset(track->extradata[track->last_stsd_index] + 16*4, 0, AV_INPUT_BUFFER_PADDING_SIZE);
8272 track->extradata_size[track->last_stsd_index] = 16 * 4;
8273 }
8274 st->codecpar->width = width;
8275 st->codecpar->height = track->height = height;
8276
8277 return 0;
8278}
8279
8280#if CONFIG_IAMFENC
8281static int mov_init_iamf_track(AVFormatContext *s)
8282{
8283 MOVMuxContext *mov = s->priv_data;
8284 MOVTrack *track;
8285 IAMFContext *iamf;
8286 int first_iamf_idx = INT_MAX, last_iamf_idx = 0;
8287 int nb_audio_elements = 0, nb_mix_presentations = 0;
8288 int ret;
8289
8290 for (int i = 0; i < s->nb_stream_groups; i++) {
8291 const AVStreamGroup *stg = s->stream_groups[i];
8292
8294 nb_audio_elements++;
8296 nb_mix_presentations++;
8297 }
8298
8299 if (!nb_audio_elements && !nb_mix_presentations)
8300 return 0;
8301
8302 if (nb_audio_elements < 1 || nb_audio_elements > 2 || nb_mix_presentations < 1) {
8303 av_log(s, AV_LOG_ERROR, "There must be >= 1 and <= 2 IAMF_AUDIO_ELEMENT and at least "
8304 "one IAMF_MIX_PRESENTATION stream groups to write a IMAF track\n");
8305 return AVERROR(EINVAL);
8306 }
8307
8308 iamf = av_mallocz(sizeof(*iamf));
8309 if (!iamf)
8310 return AVERROR(ENOMEM);
8311
8312
8313 for (int i = 0; i < s->nb_stream_groups; i++) {
8314 const AVStreamGroup *stg = s->stream_groups[i];
8315 switch(stg->type) {
8317 for (int j = 0; j < stg->nb_entries; j++) {
8318 first_iamf_idx = FFMIN(stg->entries[j]->stream->index, first_iamf_idx);
8319 last_iamf_idx = FFMAX(stg->entries[j]->stream->index, last_iamf_idx);
8320 }
8321
8322 ret = ff_iamf_add_audio_element(iamf, stg, s);
8323 break;
8325 ret = ff_iamf_add_mix_presentation(iamf, stg, s);
8326 break;
8327 default:
8328 av_assert0(0);
8329 }
8330 if (ret < 0) {
8332 av_free(iamf);
8333 return ret;
8334 }
8335 }
8336
8337 track = &mov->tracks[first_iamf_idx];
8338 track->iamf = iamf;
8339 track->first_iamf_idx = first_iamf_idx;
8340 track->last_iamf_idx = last_iamf_idx;
8341 track->tag = MKTAG('i','a','m','f');
8342
8343 for (int i = 0; i < s->nb_stream_groups; i++) {
8344 AVStreamGroup *stg = s->stream_groups[i];
8346 continue;
8347 for (int j = 0; j < stg->nb_entries; j++)
8348 stg->entries[j]->stream->priv_data = track;
8349 }
8350
8351 ret = avio_open_dyn_buf(&track->iamf_buf);
8352 if (ret < 0)
8353 return ret;
8354
8355 return 0;
8356}
8357#endif
8358
8360{
8361 MOVMuxContext *mov = s->priv_data;
8362 int has_iamf = 0;
8363 int i, ret;
8364
8365 mov->fc = s;
8366 mov->pkt = ffformatcontext(s)->pkt;
8367
8368 /* Default mode == MP4 */
8369 mov->mode = MODE_MP4;
8370
8371#define IS_MODE(muxer, config) (CONFIG_ ## config ## _MUXER && !strcmp(#muxer, s->oformat->name))
8372 if (IS_MODE(3gp, TGP)) mov->mode = MODE_3GP;
8373 else if (IS_MODE(3g2, TG2)) mov->mode = MODE_3GP|MODE_3G2;
8374 else if (IS_MODE(mov, MOV)) mov->mode = MODE_MOV;
8375 else if (IS_MODE(psp, PSP)) mov->mode = MODE_PSP;
8376 else if (IS_MODE(ipod, IPOD)) mov->mode = MODE_IPOD;
8377 else if (IS_MODE(ismv, ISMV)) mov->mode = MODE_ISM;
8378 else if (IS_MODE(f4v, F4V)) mov->mode = MODE_F4V;
8379 else if (IS_MODE(avif, AVIF)) mov->mode = MODE_AVIF;
8380#undef IS_MODE
8381
8382 if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
8384
8385 if (mov->mode == MODE_AVIF)
8387
8388 /* Set the FRAGMENT flag if any of the fragmentation methods are
8389 * enabled. */
8390 if (mov->max_fragment_duration || mov->max_fragment_size ||
8396
8399 av_log(s, AV_LOG_ERROR, "Setting both hybrid_fragmented and faststart is not supported.\n");
8400 return AVERROR(EINVAL);
8401 }
8402
8403 /* Set other implicit flags immediately */
8406
8407 if (mov->mode == MODE_ISM)
8410 if (mov->flags & FF_MOV_FLAG_DASH)
8413 if (mov->flags & FF_MOV_FLAG_CMAF)
8416
8417 /* global_sidx is inserted in a second pass, shifting all fragments. Use
8418 * moof-relative addressing so that tfhd offsets remain valid after that
8419 * shift, unless absolute offsets are already avoided (omit_tfhd_offset,
8420 * ISMV) or no sidx is inserted (hybrid_fragmented). */
8421 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX &&
8422 mov->flags & FF_MOV_FLAG_FRAGMENT &&
8425 mov->mode != MODE_ISM)
8427
8428 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV && s->flags & AVFMT_FLAG_AUTO_BSF) {
8429 av_log(s, AV_LOG_VERBOSE, "Empty MOOV enabled; disabling automatic bitstream filtering\n");
8430 s->flags &= ~AVFMT_FLAG_AUTO_BSF;
8431 }
8432
8434 av_log(s, AV_LOG_WARNING, "Global SIDX enabled; Ignoring skip_sidx option\n");
8436 }
8437
8438 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
8439 mov->reserved_moov_size = -1;
8440 }
8441
8442 if (mov->use_editlist < 0) {
8443 mov->use_editlist = 1;
8444 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
8445 !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
8446 // If we can avoid needing an edit list by shifting the
8447 // tracks, prefer that over (trying to) write edit lists
8448 // in fragmented output.
8449 if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO ||
8450 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)
8451 mov->use_editlist = 0;
8452 }
8453 }
8454 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
8455 !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
8456 av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
8457
8458 if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist)
8460
8461 if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO &&
8463 s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO;
8464
8465 /* Clear the omit_tfhd_offset flag if default_base_moof is set;
8466 * if the latter is set that's enough and omit_tfhd_offset doesn't
8467 * add anything extra on top of that. */
8471
8472 if (mov->frag_interleave &&
8475 "Sample interleaving in fragments is mutually exclusive with "
8476 "omit_tfhd_offset and separate_moof\n");
8477 return AVERROR(EINVAL);
8478 }
8479
8480 /* Non-seekable output is ok if using fragmentation. If ism_lookahead
8481 * is enabled, we don't support non-seekable output at all. */
8482 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
8483 (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead ||
8484 mov->mode == MODE_AVIF)) {
8485 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
8486 return AVERROR(EINVAL);
8487 }
8488
8489#if CONFIG_AVIF_MUXER
8490 /* AVIF output must have at most two video streams (one for YUV and one for
8491 * alpha). */
8492 if (mov->mode == MODE_AVIF) {
8493 if (s->nb_streams > 2) {
8494 av_log(s, AV_LOG_ERROR, "AVIF output requires exactly one or two streams\n");
8495 return AVERROR(EINVAL);
8496 }
8497 if (s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
8498 (s->nb_streams > 1 && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)) {
8499 av_log(s, AV_LOG_ERROR, "AVIF output supports only video streams\n");
8500 return AVERROR(EINVAL);
8501 }
8502 if (!av_pix_fmt_desc_get(s->streams[0]->codecpar->format)) {
8503 av_log(s, AV_LOG_ERROR, "AVIF output requires a known pixel format\n");
8504 return AVERROR(EINVAL);
8505 }
8506 if (s->nb_streams > 1) {
8507 const AVPixFmtDescriptor *pixdesc =
8508 av_pix_fmt_desc_get(s->streams[1]->codecpar->format);
8509 if (!pixdesc || pixdesc->nb_components != 1) {
8510 av_log(s, AV_LOG_ERROR, "Second stream for AVIF (alpha) output must have exactly one plane\n");
8511 return AVERROR(EINVAL);
8512 }
8513 }
8514 s->streams[0]->disposition |= AV_DISPOSITION_DEFAULT;
8515 }
8516#endif
8517
8518 for (i = 0; i < s->nb_stream_groups; i++) {
8519 AVStreamGroup *stg = s->stream_groups[i];
8520
8521 for (int j = 0; j < stg->nb_entries; j++) {
8522 if (stg->entries[j]->type != AV_STREAM_GROUP_ENTRY_STREAM) {
8523 av_log(s, AV_LOG_ERROR, "Unsupported Stream Group entry #%d "
8524 "in Stream Group id %"PRId64"\n",
8525 i, stg->id);
8526 return AVERROR(EINVAL);
8527 }
8528 }
8529
8530 if (stg->type == AV_STREAM_GROUP_PARAMS_LCEVC) {
8531 if (stg->nb_entries != 2) {
8532 av_log(s, AV_LOG_ERROR, "Exactly two Streams are supported for Stream Groups of type LCEVC\n");
8533 return AVERROR(EINVAL);
8534 }
8536 if (lcevc->el_index > 1)
8537 return AVERROR(EINVAL);
8538 AVStream *st = stg->entries[lcevc->el_index]->stream;
8539 if (st->codecpar->codec_id != AV_CODEC_ID_LCEVC) {
8540 av_log(s, AV_LOG_ERROR, "Stream #%u is not an LCEVC stream\n", lcevc->el_index);
8541 return AVERROR(EINVAL);
8542 }
8543 }
8544
8545#if CONFIG_IAMFENC
8547 continue;
8548
8549 for (int j = 0; j < stg->nb_entries; j++) {
8550 AVStream *st = stg->entries[j]->stream;
8551
8552 if (st->priv_data) {
8553 av_log(s, AV_LOG_ERROR, "Stream %d is present in more than one Stream Group of type "
8554 "IAMF Audio Element\n", j);
8555 return AVERROR(EINVAL);
8556 }
8557 st->priv_data = st;
8558 }
8559 has_iamf = 1;
8560
8561 if (!mov->nb_tracks) // We support one track for the entire IAMF structure
8562 mov->nb_tracks++;
8563#endif
8564 }
8565
8566 for (i = 0; i < s->nb_streams; i++) {
8567 AVStream *st = s->streams[i];
8568 if (st->priv_data)
8569 continue;
8570 // Don't produce a track in the output file for timed ID3 streams.
8572 // Leave priv_data set to NULL for these AVStreams that don't
8573 // have a corresponding track.
8574 continue;
8575 }
8576 st->priv_data = st;
8577 mov->nb_tracks++;
8578 }
8579
8580 mov->nb_streams = mov->nb_tracks;
8581
8582 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
8583 mov->chapter_track = mov->nb_tracks++;
8584
8585 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
8586 for (i = 0; i < s->nb_streams; i++)
8587 if (rtp_hinting_needed(s->streams[i]))
8588 mov->nb_tracks++;
8589 }
8590
8591 if (mov->write_btrt < 0) {
8592 mov->write_btrt = mov->mode == MODE_MP4;
8593 }
8594
8595 if ( mov->write_tmcd == -1 && (mov->mode == MODE_MOV || mov->mode == MODE_MP4)
8596 || mov->write_tmcd == 1) {
8597 AVDictionaryEntry *global_tcr = av_dict_get(s->metadata, "timecode",
8598 NULL, 0);
8599
8600 /* +1 tmcd track for each video stream with a timecode */
8601 for (i = 0; i < s->nb_streams; i++) {
8602 AVStream *st = s->streams[i];
8603 AVDictionaryEntry *t = global_tcr;
8605 (t || (t=av_dict_get(st->metadata, "timecode", NULL, 0)))) {
8606 AVTimecode tc;
8607 ret = mov_check_timecode_track(s, &tc, st, t->value);
8608 if (ret >= 0)
8609 mov->nb_meta_tmcd++;
8610 }
8611 }
8612
8613 /* check if there is already a tmcd track to remux */
8614 if (mov->nb_meta_tmcd) {
8615 for (i = 0; i < s->nb_streams; i++) {
8616 AVStream *st = s->streams[i];
8617 if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
8618 av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
8619 "so timecode metadata are now ignored\n");
8620 mov->nb_meta_tmcd = 0;
8621 }
8622 }
8623 }
8624
8625 mov->nb_tracks += mov->nb_meta_tmcd;
8626 }
8627
8628 // Reserve an extra stream for chapters for the case where chapters
8629 // are written in the trailer
8630 mov->tracks = av_calloc(mov->nb_tracks + 1, sizeof(*mov->tracks));
8631 if (!mov->tracks)
8632 return AVERROR(ENOMEM);
8633
8634 for (i = 0; i < mov->nb_tracks; i++) {
8635 MOVTrack *track = &mov->tracks[i];
8636
8637 track->stsd_count = 1;
8638 track->extradata = av_calloc(track->stsd_count, sizeof(*track->extradata));
8639 track->extradata_size = av_calloc(track->stsd_count, sizeof(*track->extradata_size));
8640 if (!track->extradata || !track->extradata_size)
8641 return AVERROR(ENOMEM);
8642 }
8643
8644 if (mov->encryption_scheme_str != NULL && strcmp(mov->encryption_scheme_str, "none") != 0) {
8645 if (strcmp(mov->encryption_scheme_str, "cenc-aes-ctr") == 0) {
8647
8649 av_log(s, AV_LOG_ERROR, "Invalid encryption key len %d expected %d\n",
8651 return AVERROR(EINVAL);
8652 }
8653
8654 if (mov->encryption_kid_len != CENC_KID_SIZE) {
8655 av_log(s, AV_LOG_ERROR, "Invalid encryption kid len %d expected %d\n",
8657 return AVERROR(EINVAL);
8658 }
8659 } else {
8660 av_log(s, AV_LOG_ERROR, "unsupported encryption scheme %s\n",
8662 return AVERROR(EINVAL);
8663 }
8664 }
8665
8666#if CONFIG_IAMFENC
8667 ret = mov_init_iamf_track(s);
8668 if (ret < 0)
8669 return ret;
8670#endif
8671
8672 for (int j = 0, i = 0; j < s->nb_streams; j++) {
8673 AVStream *st = s->streams[j];
8674
8675 if (st != st->priv_data) {
8676 if (has_iamf)
8677 i += has_iamf--;
8678 continue;
8679 }
8680 st->priv_data = &mov->tracks[i++];
8681 }
8682
8683 AVRational movie_timescale = (AVRational) { 0, 1 };
8684 for (i = 0; i < s->nb_streams; i++) {
8685 AVStream *st= s->streams[i];
8686 MOVTrack *track = st->priv_data;
8687
8688 if (!track)
8689 continue;
8690
8691 if (!track->st) {
8692 track->st = st;
8693 track->par = st->codecpar;
8694 }
8695
8696 movie_timescale = av_gcd_q(movie_timescale, st->time_base, INT_MAX, (AVRational){1,0});
8697 }
8698 if (!movie_timescale.den)
8699 movie_timescale = MOV_TIMESCALE_Q;
8700
8701 if (!mov->movie_timescale)
8702 mov->movie_timescale = FFMAX(movie_timescale.den, MOV_TIMESCALE);
8703
8704 for (i = 0; i < s->nb_streams; i++) {
8705 AVStream *st= s->streams[i];
8706 MOVTrack *track = st->priv_data;
8707 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
8708
8709 if (!track)
8710 continue;
8711
8712 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
8713 if (track->language < 0)
8714 track->language = 32767; // Unspecified Macintosh language code
8715 track->mode = mov->mode;
8716 if (!track->tag)
8717 track->tag = mov_find_codec_tag(s, track);
8718 if (!track->tag) {
8719 av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
8720 "codec not currently supported in container\n",
8722 return AVERROR(EINVAL);
8723 }
8724 /* If hinting of this track is enabled by a later hint track,
8725 * this is updated. */
8726 track->hint_track = -1;
8727 track->start_dts = AV_NOPTS_VALUE;
8728 track->start_cts = AV_NOPTS_VALUE;
8729 track->end_pts = AV_NOPTS_VALUE;
8730 track->dts_shift = AV_NOPTS_VALUE;
8732 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
8733 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
8734 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
8735 if (st->codecpar->width != 720 || (st->codecpar->height != 608 && st->codecpar->height != 512)) {
8736 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
8737 return AVERROR(EINVAL);
8738 }
8739 track->height = track->tag >> 24 == 'n' ? 486 : 576;
8740 }
8741 if (mov->video_track_timescale) {
8742 track->timescale = mov->video_track_timescale;
8743 if (mov->mode == MODE_ISM && mov->video_track_timescale != 10000000)
8744 av_log(s, AV_LOG_WARNING, "Warning: some tools, like mp4split, assume a timescale of 10000000 for ISMV.\n");
8745 } else {
8746 track->timescale = st->time_base.den;
8747 while(track->timescale < 10000)
8748 track->timescale *= 2;
8749 }
8750 if (st->codecpar->width > 65535 || st->codecpar->height > 65535) {
8751 av_log(s, AV_LOG_ERROR, "Resolution %dx%d too large for mov/mp4\n", st->codecpar->width, st->codecpar->height);
8752 return AVERROR(EINVAL);
8753 }
8754 if (track->mode == MODE_MOV && track->timescale > 100000)
8756 "WARNING codec timebase is very high. If duration is too long,\n"
8757 "file may not be playable by quicktime. Specify a shorter timebase\n"
8758 "or choose different container.\n");
8759 if (track->mode == MODE_MOV &&
8760 track->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
8761 track->tag == MKTAG('r','a','w',' ')) {
8762 enum AVPixelFormat pix_fmt = track->par->format;
8763 if (pix_fmt == AV_PIX_FMT_NONE && track->par->bits_per_coded_sample == 1)
8765 track->is_unaligned_qt_rgb =
8772 }
8773 if (track->par->codec_id == AV_CODEC_ID_VP9 && track->mode != MODE_MP4) {
8774 av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
8775 return AVERROR(EINVAL);
8776 } else if (track->par->codec_id == AV_CODEC_ID_AV1 &&
8777 track->mode != MODE_MP4 && track->mode != MODE_AVIF) {
8778 av_log(s, AV_LOG_ERROR, "%s only supported in MP4 and AVIF.\n", avcodec_get_name(track->par->codec_id));
8779 return AVERROR(EINVAL);
8780 } else if (track->par->codec_id == AV_CODEC_ID_VP8) {
8781 /* altref frames handling is not defined in the spec as of version v1.0,
8782 * so just forbid muxing VP8 streams altogether until a new version does */
8783 av_log(s, AV_LOG_ERROR, "VP8 muxing is currently not supported.\n");
8784 return AVERROR_PATCHWELCOME;
8785 } else if (track->par->codec_id == AV_CODEC_ID_APV) {
8786 ret = ff_isom_init_apvc(&track->apv, s);
8787 if (ret < 0)
8788 return ret;
8789 }
8790 if (is_cover_image(st)) {
8791 track->cover_image = av_packet_alloc();
8792 if (!track->cover_image)
8793 return AVERROR(ENOMEM);
8794 }
8795 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
8796 track->timescale = st->codecpar->sample_rate;
8798 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
8799 track->audio_vbr = 1;
8800 }else if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_MS ||
8803 if (!st->codecpar->block_align) {
8804 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
8805 return AVERROR(EINVAL);
8806 }
8807 track->sample_size = st->codecpar->block_align;
8808 }else if (st->codecpar->frame_size > 1){ /* assume compressed audio */
8809 track->audio_vbr = 1;
8810 }else{
8811 track->sample_size = (av_get_bits_per_sample(st->codecpar->codec_id) >> 3) *
8813 }
8814 if (st->codecpar->codec_id == AV_CODEC_ID_ILBC ||
8816 track->audio_vbr = 1;
8817 }
8818 if (track->mode != MODE_MOV &&
8819 track->par->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
8820 if (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
8821 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not standard, to mux anyway set strict to -1\n",
8822 i, track->par->sample_rate);
8823 return AVERROR(EINVAL);
8824 } else {
8825 av_log(s, AV_LOG_WARNING, "track %d: muxing mp3 at %dhz is not standard in MP4\n",
8826 i, track->par->sample_rate);
8827 }
8828 }
8829 if (track->par->codec_id == AV_CODEC_ID_FLAC ||
8830 track->par->codec_id == AV_CODEC_ID_TRUEHD ||
8831 track->par->codec_id == AV_CODEC_ID_OPUS) {
8832 if (track->mode != MODE_MP4) {
8833 av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
8834 return AVERROR(EINVAL);
8835 }
8836 if (track->par->codec_id == AV_CODEC_ID_TRUEHD &&
8837 s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
8839 "%s in MP4 support is experimental, add "
8840 "'-strict %d' if you want to use it.\n",
8842 return AVERROR_EXPERIMENTAL;
8843 }
8844 }
8845 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
8846 track->timescale = st->time_base.den;
8847
8848 if (track->par->codec_id == AV_CODEC_ID_TTML) {
8849 /* 14496-30 requires us to use a single sample per fragment
8850 for TTML, for which we define a per-track flag.
8851
8852 We set the flag in case we are receiving TTML paragraphs
8853 from the input, in other words in case we are not doing
8854 stream copy. */
8857
8858 if (track->mode != MODE_ISM &&
8859 track->par->codec_tag == MOV_ISMV_TTML_TAG &&
8860 s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
8862 "ISMV style TTML support with the 'dfxp' tag in "
8863 "non-ISMV formats is not officially supported. Add "
8864 "'-strict unofficial' if you want to use it.\n");
8865 return AVERROR_EXPERIMENTAL;
8866 }
8867 }
8868 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
8869 track->timescale = st->time_base.den;
8870 if (track->tag == MKTAG('t','m','c','d') &&
8871 st->codecpar->extradata_size >= 8)
8872 track->timecode_flags = AV_RB32(st->codecpar->extradata + 4);
8873 } else {
8874 track->timescale = mov->movie_timescale;
8875 }
8876 if (!track->height)
8877 track->height = st->codecpar->height;
8878 /* The Protected Interoperable File Format (PIFF) standard, used by ISMV recommends but
8879 doesn't mandate a track timescale of 10,000,000. The muxer allows a custom timescale
8880 for video tracks, so if user-set, it isn't overwritten */
8881 if (mov->mode == MODE_ISM &&
8884 track->timescale = 10000000;
8885 }
8886
8887 avpriv_set_pts_info(st, 64, 1, track->timescale);
8888
8890 ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
8891 (track->par->codec_id == AV_CODEC_ID_H264 || track->par->codec_id == AV_CODEC_ID_HEVC ||
8892 track->par->codec_id == AV_CODEC_ID_VVC || track->par->codec_id == AV_CODEC_ID_AV1),
8893 track->par->codec_id, s->flags & AVFMT_FLAG_BITEXACT);
8894 if (ret)
8895 return ret;
8896 }
8897 }
8898
8899 for (i = 0; i < s->nb_stream_groups; i++) {
8900 AVStreamGroup *stg = s->stream_groups[i];
8901
8902 switch (stg->type) {
8905 AVStream *st = stg->entries[lcevc->el_index]->stream;
8906 MOVTrack *track = st->priv_data;
8907
8908 for (int j = 0; j < mov->nb_tracks; j++) {
8909 MOVTrack *trk = &mov->tracks[j];
8910
8911 if (trk->st == stg->entries[!lcevc->el_index]->stream) {
8912 track->src_track = av_malloc(sizeof(*track->src_track));
8913 if (!track->src_track)
8914 return AVERROR(ENOMEM);
8915 *track->src_track = j;
8916 track->nb_src_track = 1;
8917 break;
8918 }
8919 }
8920
8921 track->par->width = lcevc->width;
8922 track->par->height = track->height = lcevc->height;
8923 break;
8924 }
8926 const AVStreamGroupTREF *tref = stg->params.tref;
8927 MOVTrack *track;
8928
8929 if (tref->metadata_index >= stg->nb_entries)
8930 return AVERROR(EINVAL);
8931
8932 track = stg->entries[tref->metadata_index]->stream->priv_data;
8933 for (int j = 0; j < stg->nb_entries; j++) {
8934 const AVStream *st2 = stg->entries[j]->stream;
8935 int index = -1;
8936
8937 if (j == tref->metadata_index)
8938 continue;
8939
8940 for (int k = 0; k < mov->nb_tracks; k++) {
8941 if (mov->tracks[k].st != st2)
8942 continue;
8943 index = k;
8944 break;
8945 }
8946 if (index < 0)
8947 return AVERROR(EINVAL);
8948
8949 int *tmp = av_realloc(track->src_track,
8950 (track->nb_src_track + 1) * sizeof(*track->src_track));
8951 if (!tmp)
8952 return AVERROR(ENOMEM);
8953 track->src_track = tmp;
8954 track->src_track[track->nb_src_track++] = index;
8955 }
8956 break;
8957 }
8958 default:
8959 break;
8960 }
8961 }
8962
8963 /* fill src_track for tmcd tracks not covered by stream groups or
8964 * created by this muxer, retaining the old behavior of src_track
8965 * being arbitrarily 0. */
8966 for (i = 0; i < s->nb_streams; i++) {
8967 AVStream *st = s->streams[i];
8968 MOVTrack *track = st->priv_data;
8969 if (st->codecpar->codec_tag != MKTAG('t','m','c','d') ||
8970 track->nb_src_track)
8971 continue;
8972
8973 track->src_track = av_malloc(sizeof(*track->src_track));
8974 if (!track->src_track)
8975 return AVERROR(ENOMEM);
8976 *track->src_track = 0;
8977 track->nb_src_track = 1;
8978 }
8979
8981 return 0;
8982}
8983
8985{
8986 AVIOContext *pb = s->pb;
8987 MOVMuxContext *mov = s->priv_data;
8988 int ret, hint_track = 0, tmcd_track = 0, nb_tracks = mov->nb_streams;
8989
8990 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
8991 nb_tracks++;
8992
8993 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
8994 hint_track = nb_tracks;
8995 for (int i = 0; i < mov->nb_streams; i++) {
8996 if (rtp_hinting_needed(mov->tracks[i].st))
8997 nb_tracks++;
8998 }
8999 }
9000
9001 if (mov->nb_meta_tmcd)
9002 tmcd_track = nb_tracks;
9003
9004 for (int i = 0; i < mov->nb_streams; i++) {
9005 MOVTrack *track = &mov->tracks[i];
9006 AVStream *st = track->st;
9007
9008 /* copy extradata if it exists */
9009 if (st->codecpar->extradata_size) {
9012 else if (!TAG_IS_AVCI(track->tag) && st->codecpar->codec_id != AV_CODEC_ID_DNXHD) {
9014 track->extradata[track->last_stsd_index] =
9016 if (!track->extradata[track->last_stsd_index]) {
9017 return AVERROR(ENOMEM);
9018 }
9019 memcpy(track->extradata[track->last_stsd_index],
9020 st->codecpar->extradata, track->extradata_size[track->last_stsd_index]);
9021 memset(track->extradata[track->last_stsd_index] + track->extradata_size[track->last_stsd_index],
9023 }
9024 }
9025
9029 continue;
9030
9031 for (int j = 0; j < mov->nb_streams; j++) {
9032 AVStream *stj= mov->tracks[j].st;
9033 MOVTrack *trackj= &mov->tracks[j];
9034 if (j == i)
9035 continue;
9036
9037 if (stj->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
9038 (trackj->par->ch_layout.nb_channels != 1 ||
9041 )
9042 track->mono_as_fc = -1;
9043
9044 if (stj->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
9047 trackj->par->ch_layout.nb_channels == 1 && track->mono_as_fc >= 0
9048 )
9049 track->mono_as_fc++;
9050
9051 if (stj->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
9054 trackj->language != track->language ||
9055 trackj->tag != track->tag
9056 )
9057 continue;
9058 track->multichannel_as_mono++;
9059 }
9060 }
9061
9062 if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV) ||
9064 if ((ret = mov_write_identification(pb, s)) < 0)
9065 return ret;
9066 }
9067
9068 if (mov->reserved_moov_size){
9069 mov->reserved_header_pos = avio_tell(pb);
9070 if (mov->reserved_moov_size > 0)
9071 avio_skip(pb, mov->reserved_moov_size);
9072 }
9073
9074 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
9075 /* If no fragmentation options have been set, set a default. */
9076 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
9082 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
9083 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
9084 mov->mdat_pos = avio_tell(pb);
9085 // The free/wide header that later will be converted into an
9086 // mdat, covering the initial moov and all the fragments.
9087 avio_wb32(pb, 0);
9088 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
9089 // Write an ftyp atom, hidden in a free/wide. This is neither
9090 // exposed while the file is written, as fragmented, nor when the
9091 // file is finalized into non-fragmented form. However, this allows
9092 // accessing a pristine, sequential ftyp+moov init segment, even
9093 // after the file is finalized. It also allows dumping the whole
9094 // contents of the mdat box, to get the fragmented form of the
9095 // file.
9096 if ((ret = mov_write_identification(pb, s)) < 0)
9097 return ret;
9098 update_size(pb, mov->mdat_pos);
9099 }
9100 } else if (mov->mode != MODE_AVIF) {
9101 if (mov->flags & FF_MOV_FLAG_FASTSTART)
9102 mov->reserved_header_pos = avio_tell(pb);
9103 mov_write_mdat_tag(pb, mov);
9104 }
9105
9107 if (mov->time)
9108 mov->time += 0x7C25B080; // 1970 based -> 1904 based
9109
9110 if (mov->chapter_track)
9111 if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
9112 return ret;
9113
9114 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
9115 for (int i = 0; i < mov->nb_streams; i++) {
9116 if (rtp_hinting_needed(mov->tracks[i].st)) {
9117 if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
9118 return ret;
9119 hint_track++;
9120 }
9121 }
9122 }
9123
9124 if (mov->nb_meta_tmcd) {
9125 const AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata,
9126 "timecode", NULL, 0);
9127 /* Initialize the tmcd tracks */
9128 for (int i = 0; i < mov->nb_streams; i++) {
9129 AVStream *st = mov->tracks[i].st;
9130 t = global_tcr;
9131
9133 AVTimecode tc;
9134 if (!t)
9135 t = av_dict_get(st->metadata, "timecode", NULL, 0);
9136 if (!t)
9137 continue;
9138 if (mov_check_timecode_track(s, &tc, st, t->value) < 0)
9139 continue;
9140 if ((ret = mov_create_timecode_track(s, tmcd_track, i, tc)) < 0)
9141 return ret;
9142 tmcd_track++;
9143 }
9144 }
9145 }
9146
9147 avio_flush(pb);
9148
9149 if (mov->flags & FF_MOV_FLAG_ISML)
9150 mov_write_isml_manifest(pb, mov, s);
9151
9152 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
9153 !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
9154 if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
9155 return ret;
9156 mov->moov_written = 1;
9157 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
9158 mov->reserved_header_pos = avio_tell(pb);
9159 }
9160
9161 return 0;
9162}
9163
9165{
9166 int ret;
9167 AVIOContext *moov_buf;
9168 MOVMuxContext *mov = s->priv_data;
9169
9170 if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
9171 return ret;
9172 if ((ret = mov_write_moov_tag(moov_buf, mov, s)) < 0)
9173 return ret;
9174 return ffio_close_null_buf(moov_buf);
9175}
9176
9178{
9179 int ret;
9180 AVIOContext *buf;
9181 MOVMuxContext *mov = s->priv_data;
9182
9183 if ((ret = ffio_open_null_buf(&buf)) < 0)
9184 return ret;
9185 mov_write_sidx_tags(buf, mov, -1, 0);
9186 return ffio_close_null_buf(buf);
9187}
9188
9189/*
9190 * This function gets the moov size if moved to the top of the file: the chunk
9191 * offset table can switch between stco (32-bit entries) to co64 (64-bit
9192 * entries) when the moov is moved to the beginning, so the size of the moov
9193 * would change. It also updates the chunk offset tables.
9194 */
9196{
9197 int i, moov_size, moov_size2;
9198 MOVMuxContext *mov = s->priv_data;
9199
9200 moov_size = get_moov_size(s);
9201 if (moov_size < 0)
9202 return moov_size;
9203
9204 for (i = 0; i < mov->nb_tracks; i++)
9205 mov->tracks[i].data_offset += moov_size;
9206
9207 moov_size2 = get_moov_size(s);
9208 if (moov_size2 < 0)
9209 return moov_size2;
9210
9211 /* if the size changed, we just switched from stco to co64 and need to
9212 * update the offsets */
9213 if (moov_size2 != moov_size)
9214 for (i = 0; i < mov->nb_tracks; i++)
9215 mov->tracks[i].data_offset += moov_size2 - moov_size;
9216
9217 return moov_size2;
9218}
9219
9221{
9222 int i, sidx_size;
9223 MOVMuxContext *mov = s->priv_data;
9224
9225 sidx_size = get_sidx_size(s);
9226 if (sidx_size < 0)
9227 return sidx_size;
9228
9229 for (i = 0; i < mov->nb_tracks; i++)
9230 mov->tracks[i].data_offset += sidx_size;
9231
9232 return sidx_size;
9233}
9234
9236{
9237 int moov_size;
9238 MOVMuxContext *mov = s->priv_data;
9239
9240 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
9241 moov_size = compute_sidx_size(s);
9242 else
9243 moov_size = compute_moov_size(s);
9244 if (moov_size < 0)
9245 return moov_size;
9246
9247 return ff_format_shift_data(s, mov->reserved_header_pos, moov_size);
9248}
9249
9251{
9252 MOVMuxContext *mov = s->priv_data;
9253 AVIOContext *pb = s->pb;
9254
9255 /* Write size of mdat tag */
9256 if (mov->mdat_size + 8 <= UINT32_MAX) {
9257 avio_seek(pb, mov->mdat_pos, SEEK_SET);
9258 avio_wb32(pb, mov->mdat_size + 8);
9260 ffio_wfourcc(pb, "mdat"); // overwrite the original free/wide into a mdat
9261 } else {
9262 /* overwrite 'wide' placeholder atom */
9263 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
9264 /* special value: real atom size will be 64 bit value after
9265 * tag field */
9266 avio_wb32(pb, 1);
9267 ffio_wfourcc(pb, "mdat");
9268 avio_wb64(pb, mov->mdat_size + 16);
9269 }
9270}
9271
9273{
9274 MOVMuxContext *mov = s->priv_data;
9275 AVIOContext *pb = s->pb;
9276 int res = 0;
9277 int i;
9278 int64_t moov_pos;
9279
9280 /*
9281 * Before actually writing the trailer, make sure that there are no
9282 * dangling subtitles, that need a terminating sample.
9283 */
9284 for (i = 0; i < mov->nb_tracks; i++) {
9285 MOVTrack *trk = &mov->tracks[i];
9286 if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
9290 }
9291 }
9292
9293 //Also check for a buffered EAC3 packet
9294 for (i = 0; i < mov->nb_tracks; i++) {
9295 MOVTrack *trk = &mov->tracks[i];
9296 if (trk->par->codec_id != AV_CODEC_ID_EAC3)
9297 continue;
9298 struct eac3_info *info = trk->eac3_priv;
9299 if (!info || !info->pkt || !info->pkt->size)
9300 continue;
9301
9302 av_packet_move_ref(mov->pkt, info->pkt);
9303 info->eof = 1;
9304 res = mov_write_single_packet(s, mov->pkt);
9305 if (res < 0)
9306 return res;
9307
9308 av_packet_unref(mov->pkt);
9309 }
9310
9311 // Check if we have any tracks that require squashing.
9312 // In that case, we'll have to write the packet here.
9313 if ((res = mov_write_squashed_packets(s)) < 0)
9314 return res;
9315
9316 // If there were no chapters when the header was written, but there
9317 // are chapters now, write them in the trailer. This only works
9318 // when we are not doing fragments.
9319 if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
9320 if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
9321 mov->chapter_track = mov->nb_tracks++;
9322 if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
9323 return res;
9324 }
9325 }
9326
9327 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT) ||
9331 mov->mdat_size = avio_tell(pb) - mov->mdat_pos - 8;
9332 for (i = 0; i < mov->nb_tracks; i++) {
9333 MOVTrack *track = &mov->tracks[i];
9334 track->data_offset = 0;
9335 av_free(track->cluster);
9336 track->cluster = track->cluster_written;
9337 track->entry = track->entry_written;
9338 track->cluster_written = NULL;
9339 track->entry_written = 0;
9340 track->chunkCount = 0; // Force build_chunks to rebuild the list of chunks
9341 }
9342 // Clear the empty_moov flag, as we do want the moov to include
9343 // all the samples at this point.
9345 }
9346
9347 moov_pos = avio_tell(pb);
9348
9351
9352 avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_header_pos : moov_pos, SEEK_SET);
9353
9354 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
9355 av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
9356 res = shift_data(s);
9357 if (res < 0)
9358 return res;
9359 avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
9360 if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
9361 return res;
9362 } else if (mov->reserved_moov_size > 0) {
9363 int64_t size;
9364 if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
9365 return res;
9367 if (size < 8){
9368 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
9369 return AVERROR(EINVAL);
9370 }
9371 avio_wb32(pb, size);
9372 ffio_wfourcc(pb, "free");
9373 ffio_fill(pb, 0, size - 8);
9374 avio_seek(pb, moov_pos, SEEK_SET);
9375 } else {
9376 if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
9377 return res;
9378 }
9379
9381 // With hybrid fragmentation, only write the mdat size (hiding
9382 // the original moov and all the fragments within the mdat)
9383 // after we've successfully written the complete moov, to avoid
9384 // risk for an unreadable file if writing the final moov fails.
9386 }
9387
9388 res = 0;
9389 } else {
9391 for (i = 0; i < mov->nb_tracks; i++)
9392 mov->tracks[i].data_offset = 0;
9393 if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) {
9394 int64_t end;
9395 av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
9396 res = shift_data(s);
9397 if (res < 0)
9398 return res;
9399 end = avio_tell(pb);
9400 avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
9401 mov_write_sidx_tags(pb, mov, -1, 0);
9402 avio_seek(pb, end, SEEK_SET);
9403 }
9404 if (!(mov->flags & FF_MOV_FLAG_SKIP_TRAILER)) {
9406 res = mov_write_mfra_tag(pb, mov);
9407 if (res < 0)
9408 return res;
9409 }
9410 }
9411
9412 return res;
9413}
9414
9416 const AVPacket *pkt)
9417{
9418 int ret = 1;
9419
9420 if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
9421 if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
9422 ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
9423 } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
9424 ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
9425 }
9426
9427 return ret;
9428}
9429
9430#if CONFIG_AVIF_MUXER
9431static int avif_write_trailer(AVFormatContext *s)
9432{
9433 AVIOContext *pb = s->pb;
9434 MOVMuxContext *mov = s->priv_data;
9435 int64_t pos_backup, extent_offsets[2];
9436 uint8_t *buf;
9437 int buf_size, moov_size;
9438
9439 if (mov->moov_written) return 0;
9440
9441 mov->is_animated_avif = s->streams[0]->nb_frames > 1;
9442 if (mov->is_animated_avif && mov->nb_streams > 1) {
9443 // For animated avif with alpha channel, we need to write a tref tag
9444 // with type "auxl".
9445 MovTag *tag = mov_add_tref_tag(mov, &mov->tracks[1], MKTAG('a', 'u', 'x', 'l'));
9446 if (!tag)
9447 return AVERROR(ENOMEM);
9448
9449 int ret = mov_add_tref_id(mov, tag, 1);
9450 if (ret < 0)
9451 return ret;
9452 }
9454 mov_write_meta_tag(pb, mov, s);
9455
9456 moov_size = get_moov_size(s);
9457 for (int i = 0; i < mov->nb_tracks; i++)
9458 mov->tracks[i].data_offset = avio_tell(pb) + moov_size + 8;
9459
9460 if (mov->is_animated_avif) {
9461 int ret;
9462 if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
9463 return ret;
9464 }
9465
9466 buf_size = avio_get_dyn_buf(mov->mdat_buf, &buf);
9467 avio_wb32(pb, buf_size + 8);
9468 ffio_wfourcc(pb, "mdat");
9469
9470 // The offset for the YUV planes is the starting position of mdat.
9471 extent_offsets[0] = avio_tell(pb);
9472 // The offset for alpha plane is YUV offset + YUV size.
9473 extent_offsets[1] = extent_offsets[0] + mov->avif_extent_length[0];
9474
9475 avio_write(pb, buf, buf_size);
9476
9477 // write extent offsets.
9478 pos_backup = avio_tell(pb);
9479 for (int i = 0; i < mov->nb_streams; i++) {
9480 if (extent_offsets[i] != (uint32_t)extent_offsets[i]) {
9481 av_log(s, AV_LOG_ERROR, "extent offset does not fit in 32 bits\n");
9482 return AVERROR_INVALIDDATA;
9483 }
9484 avio_seek(pb, mov->avif_extent_pos[i], SEEK_SET);
9485 avio_wb32(pb, extent_offsets[i]); /* rewrite offset */
9486 }
9487 avio_seek(pb, pos_backup, SEEK_SET);
9488
9489 return 0;
9490}
9491#endif
9492
9493#if CONFIG_TGP_MUXER || CONFIG_TG2_MUXER
9494static const AVCodecTag codec_3gp_tags[] = {
9495 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
9496 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
9497 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
9498 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
9499 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
9500 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
9501 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
9502 { AV_CODEC_ID_NONE, 0 },
9503};
9504static const AVCodecTag *const codec_3gp_tags_list[] = { codec_3gp_tags, NULL };
9505#endif
9506
9507static const AVCodecTag codec_mp4_tags[] = {
9508 { AV_CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
9509 { AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
9510 { AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '3') },
9511 { AV_CODEC_ID_HEVC, MKTAG('h', 'e', 'v', '1') },
9512 { AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1') },
9513 { AV_CODEC_ID_HEVC, MKTAG('d', 'v', 'h', '1') },
9514 { AV_CODEC_ID_VVC, MKTAG('v', 'v', 'c', '1') },
9515 { AV_CODEC_ID_VVC, MKTAG('v', 'v', 'i', '1') },
9516 { AV_CODEC_ID_EVC, MKTAG('e', 'v', 'c', '1') },
9517 { AV_CODEC_ID_LCEVC, MKTAG('l', 'v', 'c', '1') },
9518 { AV_CODEC_ID_APV, MKTAG('a', 'p', 'v', '1') },
9519 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', '4', 'v') },
9520 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', '4', 'v') },
9521 { AV_CODEC_ID_MJPEG, MKTAG('m', 'p', '4', 'v') },
9522 { AV_CODEC_ID_PNG, MKTAG('m', 'p', '4', 'v') },
9523 { AV_CODEC_ID_JPEG2000, MKTAG('m', 'p', '4', 'v') },
9524 { AV_CODEC_ID_VC1, MKTAG('v', 'c', '-', '1') },
9525 { AV_CODEC_ID_DIRAC, MKTAG('d', 'r', 'a', 'c') },
9526 { AV_CODEC_ID_TSCC2, MKTAG('m', 'p', '4', 'v') },
9527 { AV_CODEC_ID_VP9, MKTAG('v', 'p', '0', '9') },
9528 { AV_CODEC_ID_AV1, MKTAG('a', 'v', '0', '1') },
9529 { AV_CODEC_ID_AV1, MKTAG('d', 'a', 'v', '1') },
9530 { AV_CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
9531 { AV_CODEC_ID_APPLE_APAC, MKTAG('a', 'p', 'a', 'c') },
9532 { AV_CODEC_ID_ALAC, MKTAG('a', 'l', 'a', 'c') },
9533 { AV_CODEC_ID_MP4ALS, MKTAG('m', 'p', '4', 'a') },
9534 { AV_CODEC_ID_MP3, MKTAG('m', 'p', '4', 'a') },
9535 { AV_CODEC_ID_MP2, MKTAG('m', 'p', '4', 'a') },
9536 { AV_CODEC_ID_AC3, MKTAG('a', 'c', '-', '3') },
9537 { AV_CODEC_ID_EAC3, MKTAG('e', 'c', '-', '3') },
9538 { AV_CODEC_ID_DTS, MKTAG('m', 'p', '4', 'a') },
9539 { AV_CODEC_ID_TRUEHD, MKTAG('m', 'l', 'p', 'a') },
9540 { AV_CODEC_ID_FLAC, MKTAG('f', 'L', 'a', 'C') },
9541 { AV_CODEC_ID_OPUS, MKTAG('O', 'p', 'u', 's') },
9542 { AV_CODEC_ID_VORBIS, MKTAG('m', 'p', '4', 'a') },
9543 { AV_CODEC_ID_QCELP, MKTAG('m', 'p', '4', 'a') },
9544 { AV_CODEC_ID_EVRC, MKTAG('m', 'p', '4', 'a') },
9545 { AV_CODEC_ID_DVD_SUBTITLE, MKTAG('m', 'p', '4', 's') },
9546 { AV_CODEC_ID_MOV_TEXT, MKTAG('t', 'x', '3', 'g') },
9547 { AV_CODEC_ID_BIN_DATA, MKTAG('g', 'p', 'm', 'd') },
9550 { AV_CODEC_ID_MPEGH_3D_AUDIO, MKTAG('m', 'h', 'm', '1') },
9553 { AV_CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
9554
9555 /* ISO/IEC 23003-5 integer formats */
9562 /* ISO/IEC 23003-5 floating-point formats */
9567
9568 { AV_CODEC_ID_AVS3, MKTAG('a', 'v', 's', '3') },
9569
9570 { AV_CODEC_ID_NONE, 0 },
9571};
9572#if CONFIG_MP4_MUXER || CONFIG_PSP_MUXER
9573static const AVCodecTag *const mp4_codec_tags_list[] = { codec_mp4_tags, NULL };
9574#endif
9575
9576static const AVCodecTag codec_ism_tags[] = {
9577 { AV_CODEC_ID_WMAPRO , MKTAG('w', 'm', 'a', ' ') },
9579 { AV_CODEC_ID_NONE , 0 },
9580};
9581
9582static const AVCodecTag codec_ipod_tags[] = {
9583 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
9584 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
9585 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
9586 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
9587 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
9588 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
9589 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
9590 { AV_CODEC_ID_NONE, 0 },
9591};
9592
9593static const AVCodecTag codec_f4v_tags[] = {
9594 { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
9595 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
9596 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
9597 { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
9598 { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
9599 { AV_CODEC_ID_NONE, 0 },
9600};
9601
9602#if CONFIG_AVIF_MUXER
9603
9604static const AVOption avif_options[] = {
9605 { "movie_timescale", "set movie timescale", offsetof(MOVMuxContext, movie_timescale), AV_OPT_TYPE_INT, {.i64 = MOV_TIMESCALE}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
9606 { "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 },
9607 { NULL },
9608};
9609static const AVCodecTag codec_avif_tags[] = {
9610 { AV_CODEC_ID_AV1, MKTAG('a','v','0','1') },
9611 { AV_CODEC_ID_NONE, 0 },
9612};
9613static const AVCodecTag *const codec_avif_tags_list[] = { codec_avif_tags, NULL };
9614
9615static const AVClass mov_avif_muxer_class = {
9616 .class_name = "avif muxer",
9617 .item_name = av_default_item_name,
9618 .option = avif_options,
9619 .version = LIBAVUTIL_VERSION_INT,
9620};
9621#endif
9622
9623#if CONFIG_MOV_MUXER
9625 .p.name = "mov",
9626 .p.long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
9627 .p.extensions = "mov",
9628 .priv_data_size = sizeof(MOVMuxContext),
9629 .p.audio_codec = AV_CODEC_ID_AAC,
9630 .p.video_codec = CONFIG_LIBX264_ENCODER ?
9632 .init = mov_init,
9636 .deinit = mov_free,
9638 .p.codec_tag = (const AVCodecTag* const []){
9640 },
9641 .check_bitstream = mov_check_bitstream,
9642 .p.priv_class = &mov_isobmff_muxer_class,
9643 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9644};
9645#endif
9646#if CONFIG_TGP_MUXER
9648 .p.name = "3gp",
9649 .p.long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
9650 .p.extensions = "3gp",
9651 .priv_data_size = sizeof(MOVMuxContext),
9652 .p.audio_codec = AV_CODEC_ID_AMR_NB,
9653 .p.video_codec = AV_CODEC_ID_H263,
9654 .init = mov_init,
9655 .write_header = mov_write_header,
9656 .write_packet = mov_write_packet,
9657 .write_trailer = mov_write_trailer,
9658 .deinit = mov_free,
9660 .p.codec_tag = codec_3gp_tags_list,
9661 .check_bitstream = mov_check_bitstream,
9662 .p.priv_class = &mov_isobmff_muxer_class,
9663 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9664};
9665#endif
9666#if CONFIG_MP4_MUXER
9668 .p.name = "mp4",
9669 .p.long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
9670 .p.mime_type = "video/mp4",
9671 .p.extensions = "mp4",
9672 .priv_data_size = sizeof(MOVMuxContext),
9673 .p.audio_codec = AV_CODEC_ID_AAC,
9674 .p.video_codec = CONFIG_LIBX264_ENCODER ?
9676 .init = mov_init,
9680 .deinit = mov_free,
9682 .p.codec_tag = mp4_codec_tags_list,
9684 .p.priv_class = &mov_isobmff_muxer_class,
9685 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9686};
9687#endif
9688#if CONFIG_PSP_MUXER
9690 .p.name = "psp",
9691 .p.long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
9692 .p.extensions = "mp4,psp",
9693 .priv_data_size = sizeof(MOVMuxContext),
9694 .p.audio_codec = AV_CODEC_ID_AAC,
9695 .p.video_codec = CONFIG_LIBX264_ENCODER ?
9697 .init = mov_init,
9701 .deinit = mov_free,
9703 .p.codec_tag = mp4_codec_tags_list,
9705 .p.priv_class = &mov_isobmff_muxer_class,
9706 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9707};
9708#endif
9709#if CONFIG_TG2_MUXER
9711 .p.name = "3g2",
9712 .p.long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
9713 .p.extensions = "3g2",
9714 .priv_data_size = sizeof(MOVMuxContext),
9715 .p.audio_codec = AV_CODEC_ID_AMR_NB,
9716 .p.video_codec = AV_CODEC_ID_H263,
9717 .init = mov_init,
9718 .write_header = mov_write_header,
9719 .write_packet = mov_write_packet,
9720 .write_trailer = mov_write_trailer,
9721 .deinit = mov_free,
9723 .p.codec_tag = codec_3gp_tags_list,
9724 .check_bitstream = mov_check_bitstream,
9725 .p.priv_class = &mov_isobmff_muxer_class,
9726 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9727};
9728#endif
9729#if CONFIG_IPOD_MUXER
9731 .p.name = "ipod",
9732 .p.long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
9733 .p.mime_type = "video/mp4",
9734 .p.extensions = "m4v,m4a,m4b",
9735 .priv_data_size = sizeof(MOVMuxContext),
9736 .p.audio_codec = AV_CODEC_ID_AAC,
9737 .p.video_codec = AV_CODEC_ID_H264,
9738 .init = mov_init,
9739 .write_header = mov_write_header,
9740 .write_packet = mov_write_packet,
9741 .write_trailer = mov_write_trailer,
9742 .deinit = mov_free,
9744 .p.codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
9745 .check_bitstream = mov_check_bitstream,
9746 .p.priv_class = &mov_isobmff_muxer_class,
9747 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9748};
9749#endif
9750#if CONFIG_ISMV_MUXER
9752 .p.name = "ismv",
9753 .p.long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
9754 .p.mime_type = "video/mp4",
9755 .p.extensions = "ismv,isma",
9756 .priv_data_size = sizeof(MOVMuxContext),
9757 .p.audio_codec = AV_CODEC_ID_AAC,
9758 .p.video_codec = AV_CODEC_ID_H264,
9759 .init = mov_init,
9760 .write_header = mov_write_header,
9761 .write_packet = mov_write_packet,
9762 .write_trailer = mov_write_trailer,
9763 .deinit = mov_free,
9765 .p.codec_tag = (const AVCodecTag* const []){
9767 .check_bitstream = mov_check_bitstream,
9768 .p.priv_class = &mov_isobmff_muxer_class,
9769 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9770};
9771#endif
9772#if CONFIG_F4V_MUXER
9774 .p.name = "f4v",
9775 .p.long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
9776 .p.mime_type = "application/f4v",
9777 .p.extensions = "f4v",
9778 .priv_data_size = sizeof(MOVMuxContext),
9779 .p.audio_codec = AV_CODEC_ID_AAC,
9780 .p.video_codec = AV_CODEC_ID_H264,
9781 .init = mov_init,
9782 .write_header = mov_write_header,
9783 .write_packet = mov_write_packet,
9784 .write_trailer = mov_write_trailer,
9785 .deinit = mov_free,
9786 .p.flags = AVFMT_GLOBALHEADER,
9787 .p.codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
9788 .check_bitstream = mov_check_bitstream,
9789 .p.priv_class = &mov_isobmff_muxer_class,
9790 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9791};
9792#endif
9793#if CONFIG_AVIF_MUXER
9795 .p.name = "avif",
9796 .p.long_name = NULL_IF_CONFIG_SMALL("AVIF"),
9797 .p.mime_type = "image/avif",
9798 .p.extensions = "avif",
9799 .priv_data_size = sizeof(MOVMuxContext),
9800 .p.video_codec = AV_CODEC_ID_AV1,
9801 .init = mov_init,
9802 .write_header = mov_write_header,
9803 .write_packet = mov_write_packet,
9804 .write_trailer = avif_write_trailer,
9805 .deinit = mov_free,
9806 .p.flags = AVFMT_GLOBALHEADER,
9807 .p.codec_tag = codec_avif_tags_list,
9808 .p.priv_class = &mov_avif_muxer_class,
9809 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
9810};
9811#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:397
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:464
@ AV_CODEC_ID_V210
Definition codec_id.h:177
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:612
@ 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:499
@ 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:615
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:333
@ AV_CODEC_ID_DVD_SUBTITLE
Definition codec_id.h:572
@ AV_CODEC_ID_TTML
Definition codec_id.h:596
@ AV_CODEC_ID_PCM_F32BE
Definition codec_id.h:353
@ AV_CODEC_ID_MP4ALS
Definition codec_id.h:504
@ 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:530
@ 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:380
@ 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:550
@ AV_CODEC_ID_PCM_F64LE
Definition codec_id.h:356
@ AV_CODEC_ID_ALAC
Definition codec_id.h:475
@ AV_CODEC_ID_EIA_608
Definition codec_id.h:582
@ AV_CODEC_ID_AMR_NB
Definition codec_id.h:440
@ AV_CODEC_ID_VP6A
Definition codec_id.h:156
@ AV_CODEC_ID_MP2
Definition codec_id.h:459
@ AV_CODEC_ID_DVVIDEO
Definition codec_id.h:74
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition codec_id.h:374
@ AV_CODEC_ID_DTS
Definition codec_id.h:463
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_BIN_DATA
Definition codec_id.h:613
@ AV_CODEC_ID_ADPCM_G726
Definition codec_id.h:385
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:503
@ AV_CODEC_ID_FLAC
Definition codec_id.h:471
@ 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:483
@ AV_CODEC_ID_AMR_WB
Definition codec_id.h:441
@ AV_CODEC_ID_AC3
Definition codec_id.h:462
@ AV_CODEC_ID_TSCC2
Definition codec_id.h:213
@ AV_CODEC_ID_QDM2
Definition codec_id.h:478
@ 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:375
@ 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:568
@ AV_CODEC_ID_AVUI
Definition codec_id.h:252
@ AV_CODEC_ID_ILBC
Definition codec_id.h:518
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:460
@ 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:519
@ 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:496
@ AV_CODEC_ID_MOV_TEXT
Definition codec_id.h:577
#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:236
const struct MP4TrackKindMapping ff_mov_track_kind_table[]
Definition isom.c:467
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_MP4_DBGI_TAG
Definition isom.h:511
#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_MP4_DJMD_TAG
Definition isom.h:510
#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
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
Definition utils.c:834
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:447
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:3263
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:1689
static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4626
static int get_moov_size(AVFormatContext *s)
Definition movenc.c:9164
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:6092
static const AVCodecTag codec_cover_image_tags[]
Definition movenc.c:2257
static int mov_write_header(AVFormatContext *s)
Definition movenc.c:8984
static int mov_write_sdtp_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:317
static const AVCodecTag codec_ism_tags[]
Definition movenc.c:9576
static MovTag * mov_add_tref_tag(MOVMuxContext *mov, MOVTrack *trk, uint32_t name)
Definition movenc.c:5479
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:4795
static int mov_write_stbl_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:3503
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:1729
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:7109
static uint16_t mov_graphics_mode(MOVTrack *track)
Definition movenc.c:3658
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
Definition movenc.c:4408
static int mov_write_evcc_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1766
static int mov_write_raw_metadata_tag(AVFormatContext *s, AVIOContext *pb, const char *name, const char *key)
Definition movenc.c:5132
static int mov_write_dfla_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:906
static int mov_write_dinf_tag(AVIOContext *pb)
Definition movenc.c:3542
static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1950
#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:4122
static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset, int moof_size)
Definition movenc.c:6025
uint32_t tag
Definition movenc.c:2144
static int mov_write_emsg_tag(AVIOContext *pb, AVStream *st, AVPacket *pkt)
Definition movenc.c:7773
static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:6729
static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:1997
static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4066
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition movenc.c:7952
static int mov_mdhd_mvhd_tkhd_version(MOVMuxContext *mov, MOVTrack *track, int64_t duration)
Definition movenc.c:4057
static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks, int size)
Definition movenc.c:5960
static int mov_write_mhac_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:989
static int shift_data(AVFormatContext *s)
Definition movenc.c:9235
static int mov_write_av3c_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1708
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:895
static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1085
unsigned bps
Definition movenc.c:2145
static int mov_write_gama_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track, double gamma)
Definition movenc.c:2679
static int mov_write_amve_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2800
static const AVCodecTag codec_ipod_tags[]
Definition movenc.c:9582
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:3559
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2666
static int compute_moov_size(AVFormatContext *s)
Definition movenc.c:9195
static int mov_write_loci_tag(AVFormatContext *s, AVIOContext *pb)
Definition movenc.c:4860
static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:6275
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition movenc.c:5152
static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov, int tracks, int ref_size)
Definition movenc.c:6153
static int mov_write_vexu_tag(AVFormatContext *s, AVIOContext *pb, const AVStereo3D *stereo3d, const AVSphericalMapping *spherical_mapping)
Definition movenc.c:2570
static MovTag * mov_find_tref_tag(MOVMuxContext *mov, const MOVTrack *trk, uint32_t name)
Definition movenc.c:5468
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:3406
static void mov_write_ftyp_tag_internal(AVIOContext *pb, AVFormatContext *s, int has_h264, int has_video, int write_minor)
Definition movenc.c:6348
static int mov_write_dmlp_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:960
static int mov_write_vvcc_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1796
static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3202
static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, AVTimecode tc)
Definition movenc.c:8052
static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track, AVStream *st)
Definition movenc.c:8230
static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
Definition movenc.c:6191
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1290
static void mov_write_mdat_size(AVFormatContext *s)
Definition movenc.c:9250
static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
Definition movenc.c:5763
static void enable_tracks(AVFormatContext *s)
Definition movenc.c:8113
static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:3965
static int mov_pcm_le_gt16(enum AVCodecID codec_id)
Definition movenc.c:867
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition movenc.c:5911
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1674
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:4965
static int mov_write_srat_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1454
static int mov_write_smhd_tag(AVIOContext *pb)
Definition movenc.c:3648
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:2638
static int mov_finish_fragment(MOVMuxContext *mov, MOVTrack *track, int64_t ref_pos)
Definition movenc.c:6816
static int mov_write_mdta_keys_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5048
static void find_compressor(char *compressor_name, int len, MOVTrack *track)
Definition movenc.c:2827
static void mov_parse_truehd_frame(AVPacket *pkt, MOVTrack *trk)
Definition movenc.c:6710
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition movenc.c:4136
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:2411
static int mov_get_h264_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2059
static int mov_get_dnxhd_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2167
static uint32_t rgb_to_yuv(uint32_t rgb)
Definition movenc.c:8214
static int mov_write_lhvc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1744
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:7060
static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks, int64_t mdat_size)
Definition movenc.c:6241
static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov, int tracks, int moof_size)
Definition movenc.c:6067
static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4281
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:4448
static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
Definition movenc.c:5622
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:4992
static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
Definition movenc.c:5999
static int mov_write_d263_tag(AVIOContext *pb)
Definition movenc.c:1651
static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:4842
static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int len)
Definition movenc.c:4938
static int mov_check_timecode_track(AVFormatContext *s, AVTimecode *tc, AVStream *src_st, const char *tcstr)
Definition movenc.c:8043
static int mov_write_ms_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:883
static uint16_t language_code(const char *str)
Definition movenc.c:5163
static int mov_write_custom_metadata(AVFormatContext *s, AVIOContext *pb, const char *mean, const char *name, const char *tag)
Definition movenc.c:4831
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4391
static void param_write_string(AVIOContext *pb, const char *name, const char *value)
Definition movenc.c:5617
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:812
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1832
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:4050
static int mov_add_tref_id(MOVMuxContext *mov, MovTag *tag, uint32_t id)
Definition movenc.c:5453
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5496
static int64_t calc_pts_duration(MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4041
static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1273
static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:5946
static int mov_write_bin_data_tag(AVIOContext *pb, uint32_t tag)
Definition movenc.c:3251
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:5192
static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:1466
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style)
Definition movenc.c:4752
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3163
static int mov_write_dref_tag(AVIOContext *pb)
Definition movenc.c:3391
static int mov_write_hdlr_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3687
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:4597
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, const char *tag, const char *str)
Definition movenc.c:5170
static int mov_find_tref_id(MOVMuxContext *mov, const MovTag *tag, uint32_t id)
Definition movenc.c:5444
static int mov_write_subtitle_end_packet(AVFormatContext *s, int stream_index, int64_t dts)
Definition movenc.c:7704
static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3589
static int mov_write_dvcc_dvvc_tag(AVFormatContext *s, AVIOContext *pb, AVDOVIDecoderConfigurationRecord *dovi)
Definition movenc.c:2612
static int mov_write_pssh_tag(AVIOContext *pb, AVStream *st)
Definition movenc.c:5332
static int mov_init(AVFormatContext *s)
Definition movenc.c:8359
static void param_write_int(AVIOContext *pb, const char *name, int value)
Definition movenc.c:5612
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int long_style)
Definition movenc.c:4820
static int mov_get_evc_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2121
static int mov_write_mdta_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5033
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:4650
static int mov_write_track_kind(AVIOContext *pb, const char *scheme_uri, const char *value)
Definition movenc.c:4462
static void mov_write_psp_udta_tag(AVIOContext *pb, const char *str, const char *lang, int type)
Definition movenc.c:5281
static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2284
static const uint16_t fiel_data[]
Definition movenc.c:2319
static int mov_write_apvc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1815
static int esds_has_decoder_specific_info(enum AVCodecID codec_id)
Definition movenc.c:798
static int mov_write_lvcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1783
static int mov_write_dops_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:926
static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3567
static int64_t rescale_rational(AVRational q, int b)
Definition movenc.c:2469
static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:5754
static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset)
Definition movenc.c:5769
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3342
static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:6299
static int mov_write_clli_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2749
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:6337
static int mov_write_chnl_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1386
static int mov_write_vmhd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3670
static int mov_write_pcmc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1428
static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1718
static int mov_write_video_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:2888
static int check_pkt(AVFormatContext *s, MOVTrack *trk, AVPacket *pkt)
Definition movenc.c:7074
static int mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:6529
static int mov_write_identification(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:6600
static int mov_write_ctts_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:3301
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s, int disc)
Definition movenc.c:4911
static int mov_write_wave_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1146
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4426
static int mov_write_mdia_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition movenc.c:4103
static const AVCodecTag codec_f4v_tags[]
Definition movenc.c:9593
static int is_clcp_track(MOVTrack *track)
Definition movenc.c:3681
static int mov_write_trak_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition movenc.c:4541
static int compute_sidx_size(AVFormatContext *s)
Definition movenc.c:9220
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:4715
static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
Definition movenc.c:1189
static int mov_write_track_kinds(AVIOContext *pb, AVStream *st)
Definition movenc.c:4488
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:6389
static int get_sidx_size(AVFormatContext *s)
Definition movenc.c:9177
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:1348
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5220
static int mov_pcm_be_gt16(enum AVCodecID codec_id)
Definition movenc.c:875
static int mov_write_trailer(AVFormatContext *s)
Definition movenc.c:9272
static int mov_write_btrt_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1367
static int rtp_hinting_needed(const AVStream *st)
Definition movenc.c:192
static int mov_write_nmhd_tag(AVIOContext *pb)
Definition movenc.c:3551
static int mov_get_lpcm_flags(enum AVCodecID codec_id)
Compute flags for 'lpcm' tag.
Definition movenc.c:1303
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2176
static int mov_write_squashed_packet(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:6754
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:4246
static int defined_frame_rate(AVFormatContext *s, AVStream *st)
Definition movenc.c:1988
static void mov_free(AVFormatContext *s)
Definition movenc.c:8155
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
Write uuid atom.
Definition movenc.c:2307
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:7151
static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:6013
static int mov_setup_track_ids(MOVMuxContext *mov, AVFormatContext *s)
Assign track ids.
Definition movenc.c:5405
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:4731
static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
Definition movenc.c:6650
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:5891
static int mov_flush_fragment(AVFormatContext *s, int force)
Definition movenc.c:6848
static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
Definition movenc.c:7613
static int mov_write_squashed_packets(AVFormatContext *s)
Definition movenc.c:6792
static int mov_write_eyes_tag(AVFormatContext *s, AVIOContext *pb, const AVStereo3D *stereo3d)
Definition movenc.c:2512
static unsigned int validate_codec_tag(const AVCodecTag *const *tags, unsigned int tag, int codec_id)
Definition movenc.c:2264
static void build_chunks(MOVTrack *trk)
Definition movenc.c:5369
static void get_pts_range(MOVMuxContext *mov, MOVTrack *track, int64_t *start, int64_t *end, int elst, int mdhd)
Definition movenc.c:4004
static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition movenc.c:4639
static void mov_write_vexu_proj_tag(AVFormatContext *s, AVIOContext *pb, const AVSphericalMapping *spherical_mapping)
Definition movenc.c:2485
static int mov_write_st3d_tag(AVFormatContext *s, AVIOContext *pb, AVStereo3D *stereo_3d)
Definition movenc.c:2381
static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2202
static int mov_write_av1c_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1663
static int mov_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition movenc.c:9415
static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
Definition movenc.c:3183
static int get_cluster_duration(MOVTrack *track, int cluster_idx)
Definition movenc.c:1328
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:4029
static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
Definition movenc.c:2323
static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track, int prefer_icc)
Definition movenc.c:2701
static int mov_write_mdta_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5077
static void mov_write_hfov_tag(AVFormatContext *s, AVIOContext *pb, const AVStereo3D *stereo3d)
Definition movenc.c:2474
static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVStream *st)
Definition movenc.c:4508
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition movenc.c:6629
static int mov_write_freeform_tag(AVIOContext *pb, const char *mean, const char *name, const char *data)
Definition movenc.c:4766
static int mov_write_SA3D_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:1027
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition movenc.c:7801
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5099
static int mov_write_mdcv_tag(AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2769
static const AVCodecTag codec_mp4_tags[]
Definition movenc.c:9507
static int mov_get_apv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:2131
static int mov_write_subtitle_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition movenc.c:2336
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
Definition movenc.c:5294
static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition movenc.c:5630
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition movenc.c:1963
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:5835
static int mov_write_hvce_tag(AVIOContext *pb, const AVPacketSideData *sd)
Definition movenc.c:2630
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:3950
#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 av_realloc(p, s)
Definition ops_static.c:54
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
#define OPUS_SEEK_PREROLL_MS
Definition opus.c:37
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:515
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