Commit Graph
1336 Commits
Author SHA1 Message Date
James Almer b3d4ce20a4 avformat/lcevc: add a log context parameter to all functions
Printing to NULL should be avoided when possible.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-07-23 13:56:54 -03:00
James Almer b1327568b6 avformat/movenc: unify remainder samples signaling
When writing edit lists and the last packet has skip samples side data to
report discard padding, for codecs that have a fixed frame size it's best to
always use the full packet duration to calculate track duration (and thus
bitrate in some cases) and let the edit list be the sole source of triming
information.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-30 22:19:24 -03:00
Michael Niedermayer b553b810c4 avformat/movenc: free sgpd_entries on the roll-distance error path
Found-by: Ao Xijie
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2026-06-30 17:29:51 +00:00
James Almer 7ab12fd02a avformat/movenc: use frame_size for EAC3 when merging packets
Otherwise remainder calculations will not work correctly if the input packets
have smaller frame sizes.

Should fix issue #23600.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-29 14:00:25 -03:00
James Almer df9caa03b8 avformat/movenc: preserve packet side data when merging eac3 packets
If the last packet fed to the muxer has skip samples side data, if it were to be merged
into a single packet, said side data would be lost as is.
Copy it to the merged packet.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-29 12:38:12 -03:00
James Almer 2e6d5dbdc2 avformat/movenc: drain buffered EAC3 packets
handle_eac3() will buffer a packet if it doesn't reach the desired num_blocks
value, but if it was the last one to be passed to the muxer, it will never be
written.
Fix this by writing it during mov_write_trailer().

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-29 12:38:12 -03:00
James Almer dac8b3f367 avformat/movenc: read packet duration at the time it's needed
Reading it at the beginning of ff_mov_write_packet() means that any changes to it
that happen afterwards will not be taken into account for the remainer calculation.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-29 12:38:12 -03:00
James Almer 579edeabec avformat/movenc: look for remainder samples in packet side data
Sometimes the duration itself reflects the remainder samples by being set to a
value lower than the frame size, but in other cases it's done only through
a discard padding value in side data.
Take the latter into account when calculating durations.

Fixes issue #23532.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-23 20:07:25 -03:00
James Almer 661dd0ffd5 avformat/movenc: use track timescale when making up a tmcd track pts
Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-23 20:07:25 -03:00
James Almer 0559f92adc avformat/movenc: don't derive tmcd track durations if they were copied
This behavior only applies to tracks created by the muxer.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-23 20:07:25 -03:00
Michael Niedermayer 54849fe663 avcodec/avformat: Unavpriv avpriv_packet_list_*()
The avpriv_packet_list_put/get/free() functions and the PacketList type
were implemented in libavcodec and exported via the avpriv_ mechanism
solely so that libavformat (and decklink in libavdevice) could use them;
libavcodec itself has no users of them. Exporting them across the library
boundary has the usual drawbacks for shared builds (export/import overhead
and having to keep them around for ABI stability even once unneeded).

Move the implementation and the PacketList/PacketListEntry types to
libavformat and rename the functions to ff_packet_list_*(). libavformat is
the primary user and compiles the new packet_list.c directly; decklink,
the only libavdevice user, gets a private copy for shared builds via the
SHLIBOBJS scheme already used for reverse.o and ccfifo.o (static builds
resolve the symbols from libavformat).

AVPACKET_IS_EMPTY() and ff_side_data_set_prft() remain in
libavcodec/packet_internal.h as they are libavcodec-internal.
2026-06-23 19:15:57 +02:00
Andreas Rheinhardt 0438966d84 avcodec: Remove FF_API_V408_CODECID
Deprecated on 2024-10-12.
The output of the v410enc test changes because padding bits
are now filled with 1s instead of zeroes and because
bits_per_coded_sample changes to 30 (from 32), see
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22455#issuecomment-46213.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2026-06-23 19:13:06 +02:00
Michael Niedermayer c098b07035 avformat/movenc: initialize all tracks
Fixes: NULL pointer dereference

As a sideeffect this also sets the timescale correctly if an
intermediate matches MOV_TIMESCALE_Q, the previous code stoped
in that case as it could not distinguish it from the overflow detection

Regression since: 51f5f60443

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2026-06-18 12:04:39 +00:00
James Almer 51f5f60443 avformat/movenc: use a common denominator across all tracks as movie timescale
The default of 1000 may result in off by 1 errors when rescaling certain
durations, as is the case of fate-gaplessenc-itunes-to-ipod-aac, so lets
try to prevent that by using a global timescale every track can agree
with whenever possible.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-16 09:18:24 -03:00
James Almer cf0244aa38 avformat/movenc: avoid negative cts offsets when using an edit list with CMAF output
Fixes issue #23417.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-06-10 18:14:42 +00:00
James Almer f778a7e241 avformat: deprecate AVStreamGroupLCEVC
It's been replaced with AVStreamGroupLayeredVideo, which is functionally the
same while generic enough to be shared with other kinds of layered video
implementations.

Signed-off-by: James Almer <jamrial@gmail.com>
2026-05-31 03:43:29 +00:00
Romain Beauxis 199e49d9b6 avformat/movenc: write hvcE box for Dolby Vision enhancement layer
When AV_PKT_DATA_HEVC_CONF is present on a MODE_MP4 HEVC
track, write it as an hvcE box alongside hvcC and dvcC. Like dvcC,
writing requires -strict unofficial.
2026-05-30 17:37:28 +00:00
Tim Walker c591ddd453 avformat/movenc: don't write a 'chan' atom with an incorrect channel bitmap or description. 2026-05-28 03:22:29 +00:00
Zhao Zhili 78a0309735 avformat/movenc: don't overwrite track_duration on every src_track
When a tmcd or similar metadata track references multiple source
tracks, the per-iteration assignment overwrote track->track_duration
on each loop step, leaving it set to whatever the last source happened
to be.
2026-05-25 02:31:09 +00:00
Zhao Zhili a0def8f915 avformat/movenc: skip the metadata stream itself when populating tref
A tref stream group lists the metadata stream (e.g. tmcd) alongside the
streams it references. The init loop iterated over every stream in the
group and added it to the metadata track's src_track array, which made
the tmcd track reference itself.

Regression since 4444a755.

Signed-off-by: Zhao Zhili <quinkblack@foxmail.com>
2026-05-24 21:14:31 +00:00
Tau Gärtli 504c135f8c libavformat: Add generic disc_subtitle tag mapping for MOV/MP4
The mapping is taken from Picard's [1] and taglib's [2] documentation.

[1]: https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#disc-subtitle
[2]: https://taglib.org/api/p_propertymapping.html
2026-05-23 16:07:57 +02:00
Damiano Galassi 1aefacfda4 avformat/movenc: fix mov_create_dvd_sub_decoder_specific_info
It has been broken since 3064487.
2026-05-20 13:53:37 +00:00
Tim Walker 9e71ea2d60 avformat/movenc: write stsd v2 atom when nb_channels > 2
Required by the QuickTime File Format specification.
2026-05-20 12:31:15 +00:00
Tim Walker 6181195d1f avformat/movenc: use sample_rate to determine audio stsd version.
timescale should be equal sample_rate, but the latter is
the actual source field used when writing the stsd atom
since 3b5d4428ac
2026-05-20 12:31:15 +00:00
James Almer 4444a75590 avformat/movenc: support writing more than one entry per tref tag
Signed-off-by: James Almer <jamrial@gmail.com>
2026-05-17 11:16:56 -03:00
jiangjie 2f4ad2497e avformat/movenc: fix dynamic buffer leaks on error paths
In mov_write_iacb_tag(), the dynamic buffer dyn_bc was leaked when
ff_iamf_write_descriptors() failed.

In mov_write_track_udta_tag(), the dynamic buffer pb_buf was leaked
when mov_write_track_kinds() failed, as the error path returned
directly instead of going through cleanup.

Fix both by ensuring ffio_free_dyn_buf() is called on all error paths.
2026-05-09 19:27:17 +08:00
James Almer 711b1a52bd avformat/movenc: check if a packet is to be discarded when calculating edit list durations
Demuxers like mov will export packets not meant for presentation (e.g. because
an edit list doesn't include them) by flagging them as discard, but the mov
muxer completely ignored this, resulting in output edit lists considering every
packet.

Fixes issue #22552

Signed-off-by: James Almer <jamrial@gmail.com>
2026-03-21 23:35:39 -03:00
Zhao Zhili cc866fb5e9 avformat/movenc: fix loop variable shadowing in LCEVC stream group init
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2026-03-16 13:25:59 +00:00
James Almer 0878ae59f9 avformat/movenc: add support for LCEVC track muxing
Signed-off-by: James Almer <jamrial@gmail.com>
2026-03-14 20:50:21 -03:00
Anthony Bajoua 627da1111c libavformat/movenc: Uses dynamic buffers for fragmented chunks 2026-02-04 18:45:15 -08:00
Jun Zhao 6c878f8b82 lavf/movenc: fix missing padding for AV1 extradata
The extradata allocated in mov_write_single_packet() for AV1 was missing
the required AV_INPUT_BUFFER_PADDING_SIZE padding bytes. This could lead
to out-of-bounds reads when the extradata is parsed by bitstream readers.

Replace av_memdup() with av_malloc() + memset() + memcpy() to ensure
proper padding is present and zeroed.

Reproduced with:
./ffmpeg -y -f lavfi -i "testsrc=duration=1:size=320x240:rate=30" -c:v libaom-av1 -cpu-used 8 -crf 50 test-av1.mp4

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-01-14 23:56:39 +00:00
Marton Balint c80d3b35ae avformat/aviobuf: return error for ffio_close_null_buf() if written bytes exceed INT_MAX
Also check return value where it is used.

Signed-off-by: Marton Balint <cus@passwd.hu>
2026-01-12 00:47:20 +01:00
Andreas Rheinhardt e0845ec2cf avformat/movenc: Fix leak of IAMFContext on error
Forgotten in 5b87869c09.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-12-04 16:15:09 +00:00
James Almer 6f4a3be9dc avformat/movenc: add support for writing srat box
Signed-off-by: James Almer <jamrial@gmail.com>
2025-11-24 02:59:02 +00:00
Martin Storsjö 8313dc1120 movenc: Make hybrid_fragmented retain the fragmented form headers
This makes the final file truly hybrid: Externally the file
is a regular, non-fragmented file, but internally, the fragmented
form also exists un-overwritten.

To make any use of that, first, the fragments need to be muxed in
a position independent form, i.e. with empty_moov+default_base_moof
(or the dash or cmaf meta-flags).

Making use of the fragmented form when the file is finalized is
not entirely obvious though. One can dump the contents of the
single mdat box, and get the fragmented form. (This is a neat
trick, but not something that anybody really is expected to
want to do.)

The main expected use case is accessing fragments in the form of
byte range segments, for e.g. HLS.

Previously, the start of the file would look like this:

- ftyp
- free
- moov
 - (moov contents)

After finalizing the file, it would look like this:

- ftyp
- free
- mdat (previously moov)
 - (moov contents)

In this form, the size and type of the original moov box were
overwritten, and the original moov contents is just leftover
as unused data in the mdat box.

To avoid this issue, the start of the file now looks like this:

- ftyp
- free
- free
 - ftyp
- moov
 - (moov contents)

The second, hidden ftyp box inside mdat, would normally never be
seen.

After finalizing, the difference is that the mdat box now is
extended to cover the ftyp and the whole moov including its header
(and all the following fragments).

I.e., the start of the file looks like this:

- ftyp
- free
- mdat
 - ftyp
 - moov
  - (moov contents)

This allows accessing the "ftyp+moov" pair sequentially as such,
with a byte range - this range is untouched when finalizing,
producing the same ftyp+moov pair both while writing, when the
file is fragmented, and after finalizing, when the file is
transformed to non-fragmented externally.

Note; the sequential two "free+free" boxes may look slightly
silly; it could be tempting to make the second one an mdat
from the get-go. However, some players of fragmented mp4 (in
particular, Apple's HLS player) bail out if the initialization
segment contains an mdat box - therefore, use a free box.

It could also be possible to use just one single free box with
8 bytes of padding at the start - but that would require more
changes to the finalization logic.

For a segmenting user of the muxer, the only unclarity is how
to determine the right byte range for the internal ftyp+moov
pair. Currently, this requires parsing the muxer output and skip
past anything up to the start of the non-empty free box.
2025-11-04 10:23:31 +00:00
Anthony Bajoua 68152978b5 libavformat/movenc: Output ftyp atom with hybrid_fragmented+delay_moov 2025-10-27 09:06:06 +00:00
Kacper Michajłow a2b47ccfbf avcodec/{png,mov}enc: use EOTF gamma approximation for gAMA chunk
This is how images encoded with specific transfer function should be
viewed. Image viewers that doesn't support named trc metadata, will
fallback to simple gAMA value and both of those cases should produce the
same image apperance for the viewer.

Fixes: https://github.com/mpv-player/mpv/issues/13438

Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-10-24 22:24:54 +00:00
Baptiste Coudurier 0b0cb7cd6c lavf/movenc: improve AVdh atom generation for DNxHD/DNxHR 2025-10-24 00:16:21 +00:00
Martin Storsjö e442128944 movenc: Make sure to flush the delayed moov atom for hybrid fragmented
If using the delay_moov flag in combination with hybrid_fragment
(which is a potentially problematic combination otherwise - the
ftyp box does end up hidden in the end), then we need to flush
twice to get both the moov box and the first fragment, if the
file is finished before the first fragment is completed.
2025-10-21 08:38:32 +00:00
Martin Storsjö 27f5561885 movenc: Fix sample clustering for hybrid_fragmented+delay_moov
If samples were available when the moov was written, chunking
for those samples has been done already, which has to be reset
here.

This is the case when not using empty_moov, when the moov box
describes the first fragment - this case was accounted for already.
But if using the delay_moov flag, then those samples also were
available when writing the moov, so chunking for them has already
been done in this case as well.

Therefore, always reset chunking here (it should be harmless to
always do it), and update the comment to clarify the cases
involved here.
2025-10-21 08:38:32 +00:00
Martin Storsjö 36896af64a movenc: Make the hybrid_fragmented mode more robust
Write the moov tag at the end first, before overwriting the mdat size
at the start of the file.

In case writing the final moov box fails (e.g. due to being out
of disk), we haven't broken the initial moov box yet.

Thus if writing stops between these steps, we could end up with
a file with two moov boxes - which arguably is more feasible to
recover from, than from a file with no moov boxes at all.
2025-10-16 18:58:54 +00:00
Zhao Zhili cd4b01707d Revert "avformat/movenc: sidx earliest_presentation_time is applied after editlist"
This reverts commit 301141b576.

cluster[0].dts, pts and frag_info[0].time are already in presentation
timeline, so they shouldn't be shift by start_pts.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2025-10-16 11:22:37 +08:00
James Almer 6d8732f397 avformat/movenc: clear subsample information on fragment flush
Don't keep around information from a previous traf atom.

Fixes issue #20492.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-25 23:18:08 -03:00
Jan Ekström af35a13f6d avformat/movenc: add support for fragmented TTML muxing
Attempts to base the fragmentation timing on other streams
as most receivers expect media fragments to be more or less
aligned.

Currently does not support fragmentation on subtitle track
only, as the subtitle packet queue timings would have to be
checked in addition to the current fragmentation timing logic.

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
2025-09-16 16:29:48 +03:00
Zhao Zhili 9a34ddc345 avformat/movenc: remove mvex from final mp4 in hybrid_fragmented mode
In hybrid_fragmented mode, the first moov at start should contain
mvex tag, since it's fmp4. When writing the moov at the second time,
it's not fmp4 any more, so mvex should be skipped.
2025-09-01 12:41:38 +00:00
Damiano Galassi b2f5bc7fd3 movenc: ensure chapters track extradata is not null and populated
fix a regression introduced in 4eca8df,
writing chapters failed if chapters where
not available before mov_write_header().
2025-08-24 13:31:53 +00:00
Timo Rothenpieler 262d41c804 all: fix typos found by codespell 2025-08-03 13:48:47 +02:00
James Almer 9aed7383bd avformat/movenc: look for the Intra profile flag before assuming a stream is AVC-Intra
Fixes issue #20018.

Note that Codec Descriptors are not written for all these ai** codec tags, and no
considerations were ever made to ensure parameter sets are present in muxed packets.
Their usage may result in unplayable files if parameter sets are only available
in extradata (Default behavior for x264 encoding when combined with this muxer).

Signed-off-by: James Almer <jamrial@gmail.com>
2025-08-01 14:53:17 +00:00
James Almer 4eca8df8b8 avformat/movenc: support writing more than one STSD entry per track
If a packet contains new extradata within a side data entry, save for
specific cases it means the coded stream changed. If ignored, upon
demuxing every packet from then onwards may be undecodable.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-07-30 16:48:14 -03:00
James Almer 306448756b avformat/movenc: allow storing more than one extradata buffer per track
This is in preparation for a following change

Signed-off-by: James Almer <jamrial@gmail.com>
2025-07-30 16:48:14 -03:00