Commit Graph

12155 Commits

Author SHA1 Message Date
Niklas Haas
81c1004891 avfilter/af_afade: shorten crossfade on too short inputs
This behavior is currently completely broken, leading to an abrupt end of the
first audio stream. I want to generalize this filter to multiple inputs, but
having too short input files will always represent a significant problem.

I considered a few approaches for how to handle this more gracefully, but
most of them come with their own problems; in particular when a short input
is sandwiched between two longer ones; or when there is a sequence of short
inputs. In the end, it's simplest to just shorten the crossfade window.

I also considered (and tested) padding the input with silence, but this also
has its own aesthetic implications and strange edge cases.

See-Also: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20388
2025-09-04 23:12:34 +00:00
Niklas Haas
2e0885a091 avfilter/af_afade: don't leak memory on error
This first frame was never cleaned up if the second input fails.
2025-09-04 23:12:34 +00:00
Niklas Haas
386264ae16 avfilter/af_afade: generalize pass_crossfade() signature
Prerequisite to an upcoming refactor.
2025-09-04 23:12:34 +00:00
Andreas Rheinhardt
2451e06f19 all: Use "" instead of <> to include internal headers
Reviewed-by: Niklas Haas <ffmpeg@haasn.dev>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-09-04 22:20:58 +02:00
Niklas Haas
a700f0f72d avfilter/vf_zscale: simplify and fix alpha handling
There's no reason to use a completely separate graph just to process the
alpha plane in isolation - zimg supports native alpha handling as part of the
main image.

Fixes several issues with this filter when adding or removing alpha planes,
and also adds support for scaling premultiplied alpha (which reduces artefacts
near the image borders).
2025-09-02 17:06:25 +02:00
Niklas Haas
c7adeb75ac avfilter/vf_scale: require straight alpha for alpha blending
Fortunately, we only care if this flag is set - otherwise, this filter is
alpha mode agnostic (since it is purely scaling, etc).

That said, there is an argument to be made that we should prefer premul
alpha when scaling, because scaling in straight alpha will leak garbage
pixels; but I think that would be a too backwards-incompatible change to
be worth thinking about at this time.
2025-09-02 17:06:25 +02:00
Niklas Haas
446a25b70f avfilter: add alpha_mode consistency assertion 2025-09-02 17:06:25 +02:00
Niklas Haas
39631c99d4 avfilter/drawutils: add alpha mode support
This allows drawing colors onto images with premultiplied alpha, by first
premultiplying the specified RGBA values.
2025-09-02 17:06:25 +02:00
Niklas Haas
a90e8f1c1d avfilter: use ff_draw_init_from_link() where possible 2025-09-02 17:06:25 +02:00
Niklas Haas
eddbdc8b7c avfilter/drawutils: add ff_draw_init_from_link()
The overwhelming majority of references to ff_draw_init2() just set the
colorspace properties from a filter link. This wrapper allows us to update
all such referencen automatically.
2025-09-02 17:06:25 +02:00
Niklas Haas
d05484757a avfilter/vf_fade: set correct alpha mode requirement
Fading to black in non-alpha mode works for either straight or premultiplied
alpha, but fading to a color does not.
2025-09-02 17:06:25 +02:00
Niklas Haas
ba8aa0e7b3 avfilter/x86/vf_overlay: simplify function signature
No reason to pass all the variables again, if we're already passing the
context.
2025-09-02 17:06:25 +02:00
Niklas Haas
59bb51a8f2 avfilter/vf_overlay: support premultiplied base layers
Conceptually, these are pretty simple to handle, since they are basically
equivalent to the case of alpha being absent, since the only thing the
destination alpha plane is used for is unpremultiplyng straight alpha.

For planar formats, the total number of cases doesn't change in principle,
since before this patch we have:

- alpha present, overlay straight
- alpha present, overlay premultiplied
- alpha absent, overlay straight
- alpha absent, overlay premultiplied

And now we have:

- main straight, overlay straight
- main straight, overlay premultiplied
- main premultiplied, overlay straight
- main premultiplied, overlay premultiplied

That said, we do gain some cases now that we used to (incorrectly) ignore,
like premultiplied yuva420p10.

Notably, we can skip defining separate functions for the case of main alpha
being absent, since that can be a single cheap branch inside the function
itself, on whether or not to also process the alpha plane. Otherwise, as long
as we treat "alpha absent" as "main premultiplied", the per-plane logic will
skip touching the nonexistent alpha plane.

The only format that actually needs extra cases is packed rgb, but that's only
two additional cases in total.

Also arguably simplifies the assignment logic massively.
2025-09-02 17:06:25 +02:00
Niklas Haas
6d6bbdaab0 avfilter/vf_overlay: rename variables for clarity
`is_straight`, `alpha_mode` etc. are more consistently named to refer to
either the main image, or the overlay.
2025-09-02 17:06:25 +02:00
Niklas Haas
6f3eddbedd avfilter/vf_overlay: configure alpha mode on the link
And use the link-tagged value instead of the hard-coded parameter.
2025-09-02 17:06:25 +02:00
Niklas Haas
1c8ca56a88 avfilter/vf_libplacebo: add an alpha_mode setting
Chooses the desired output alpha mode. Note that this depends on
an upstream version of libplacebo new enough to respect the corresponding
AVFrame field in pl_map_avframe_ex.
2025-09-02 17:06:25 +02:00
Niklas Haas
dc02514f6b avfilter/vf_setparams: add alpha_mode parameter 2025-09-02 17:06:25 +02:00
Niklas Haas
e6ab7fe582 avfilter/vf_scale: set correct alpha mode after format change
While vf_scale cannot directly convert between premultiplied and straight
alpha, the effective tagging can still change as a result of a change in
the pixel format (i.e. adding or removing an alpha channel).
2025-09-02 17:06:25 +02:00
Niklas Haas
a3d518a57c avfilter/vf_extractplanes: require straight alpha input 2025-09-02 17:06:25 +02:00
Niklas Haas
ab1bc440f7 avfilter/vf_alphamerge: configure correct alpha mode 2025-09-02 17:06:25 +02:00
Niklas Haas
2e2ca4e740 avfilter/vf_showinfo: print alpha mode when relevant 2025-09-02 17:06:25 +02:00
Niklas Haas
2b1ef029eb avfilter/vf_format: add alpha_modes parameter 2025-09-02 17:06:25 +02:00
Niklas Haas
de297ae2fc avfilter/buffersrc: add alpha_mode parameter 2025-09-02 17:06:25 +02:00
Niklas Haas
0fbd90d78f avfilter/buffersink: add support for alpha modes 2025-09-02 17:06:25 +02:00
Niklas Haas
fb3a4f6180 avfilter/formats: use vf_premultiply_dynamic for alpha mode autoconversion 2025-09-02 17:06:09 +02:00
Niklas Haas
c39a67e64f avfilter/vf_premultiply: add dynamic variant of this filter
We need a filter that can premultiply and unpremultiply the alpha channel
dynamically, on demand, in response to the negotiated alpha mode, analogous
to how vf_scale operates. Introduce a new filter "vf_premultiply_dynamic"
that accomplishes this.
2025-09-02 17:06:09 +02:00
Niklas Haas
6bac13d20a avfilter/vf_premultiply: signal correct alpha mode
Set the output link according to the chosen direction, and the input link
to its opposite (unless the input is not in-place, in which case there is no
alpha channel).

Also update the alpha mode on the output frame, since it may differ.
2025-09-02 17:06:09 +02:00
Niklas Haas
eea99a77ed avfilter: add link negotiation for AVAlphaMode 2025-09-02 17:06:08 +02:00
Niklas Haas
8b375b2ffd avfilter/avfiltergraph: allow different conversion filters per merger
Instead of hard-coding the assumption that all video properties are handled
by vf_scale, generalize the struct slightly to allow different conversion
filters for each property being merged. The avfiltergraph code creates a list
of conversion filters needed and inserts all of them at once.

Because a conversion filter might itself e.g. not support all formats,
it's possible that we need to go through the whole process of negotiating
formats multiple times, and keep adding conversion filters until all of them
are settled. Do this by simply jumping back to the beginning of the loop
to ensure that the `convertor_count` field remains contiguous.
2025-09-02 17:05:51 +02:00
Niklas Haas
dbcdcaede8 avfilter/formats: fix param name on ff_set_common_color_spaces*
Purely cosmetic.
2025-09-02 17:05:51 +02:00
Zsolt Vadasz
173988aa0c libavfilter/vf_minterpolate: Use local variable in array
Signed-off-by: Zsolt Vadasz <zsolt_vadasz@protonmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-09-02 15:56:23 +02:00
Zsolt Vadasz
5d7e4b7496 libavfilter/vf_minterpolate: Hoist hot loop in set_frame_data
Signed-off-by: Zsolt Vadasz <zsolt_vadasz@protonmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-09-02 15:56:02 +02:00
Niklas Haas
ef856ef93e avfilter/vf_libplacebo: also output a frame during input gaps
In constant FPS mode, it's possible for there no be *no* input active at
the current timestamp, even though there would be active inputs later in
the timeline (nb_active > 0). This would previously hit the AVERROR_BUG case.

With this change, we can instead output an empty frame.
2025-09-02 09:55:31 +00:00
Niklas Haas
68c083c43f avfilter/vf_libplacebo: refactor status handling
Instead of having a single s->status field to track the most recently
EOF'd stream, track the number of active streams directly and only query
the most recent status at the end.

The reason this worked before is because we implicitly relied on the
assumption that `ok` would always be true until all streams are EOF, but
because it's possible to have gaps in the timeline when mixing multiple
streams, this is not always the case in principle.

In practice, this fixes a bug where the filter would early-exit (EOF)
too soon, when any input reached EOF and there is a gap in the timeline.
2025-09-02 09:55:31 +00:00
Niklas Haas
fc5a6a3b39 avfilter/vf_libplacebo: allow rendering empty frames
When using libplacebo to composite multiple streams with complex PTS
values, it's possible for there to be a "hole" in the output stream; in
particular in constant FPS mode. This commit refactors output_frame() to
allow it to handle the case of there being no active input.
2025-09-02 09:55:31 +00:00
Zhao Zhili
eb14d45824 avfilter/vf_colordetect: add aarch64 asm
| rpi5 gcc 12  | m1 clang -fno-vectorize | m1 clang
---------------------------------------------------------------------------
alpha_8_full_c:        | 32159.2 ( 1.00x) | 135.8 ( 1.00x) |  26.4 ( 1.00x)
alpha_8_full_neon:     |  1266.0 (25.40x) |   8.0 (17.03x) |   8.4 ( 3.15x)
alpha_8_limited_c:     | 37561.9 ( 1.00x) | 169.1 ( 1.00x) |  47.7 ( 1.00x)
alpha_8_limited_neon:  |  3967.0 ( 9.47x) |  12.5 (13.53x) |  13.3 ( 3.59x)
alpha_16_full_c:       | 15867.9 ( 1.00x) |  64.5 ( 1.00x) |  13.7 ( 1.00x)
alpha_16_full_neon:    |  1256.9 (12.62x) |   7.9 ( 8.15x) |   8.3 ( 1.64x)
alpha_16_limited_c:    | 16723.7 ( 1.00x) |  88.7 ( 1.00x) | 103.3 ( 1.00x)
alpha_16_limited_neon: |  4031.3 ( 4.15x) |  12.5 ( 7.08x) |  13.2 ( 7.86x)
range_8_c:             | 21819.7 ( 1.00x) | 120.0 ( 1.00x) |   9.4 ( 1.00x)
range_8_neon:          |  1148.3 (19.00x) |   4.3 (27.60x) |   4.8 ( 1.97x)
range_16_c:            | 10757.1 ( 1.00x) |  45.7 ( 1.00x) |   7.9 ( 1.00x)
range_16_neon:         |  1141.5 ( 9.42x) |   4.4 (10.38x) |   4.6 ( 1.72x)
2025-09-01 15:35:16 +00:00
Niklas Haas
f07c12d806 avfilter/x86/vf_colordetect: fix alpha detect tail handling
This wrapping logic still considered any nonzero return from the ASM function
to be the overall result, but this is not true since the addition of
FF_ALPHA_TRANSPARENT.

Fix it by only early returning if FF_ALPHA_STRAIGHT is detected.

Fixes: 9b8b78a815
See-Also: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20301#issuecomment-4802
2025-09-01 15:33:43 +00:00
Vittorio Palmisano
73d411c399 avfilter/af_whisper: fix srt file format
The SRT file format requires commas in the time string, not periods.
2025-08-30 14:30:51 +00:00
James Almer
fa23202cc7 configure: bump LCEVCdec minimum required version to 4.0.0
The project introduced API breaking changes to some of their public functions,
and given the library is relatively new, just bump the minimum supported
version instead of adding ifdeffery to the source files.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-08-25 10:43:23 -03:00
Robert Nagy
1b62f9d3ae avfilter/vf_scale: add more color_matrix aliases
Add more color_matrix aliases to make vf_scale consistent with e.g. vf_setparams aliases.
2025-08-25 08:12:37 +00:00
Niklas Haas
6825f6f1e2 avfilter/vf_libplacebo: add bwdif option
See-Also: https://code.videolan.org/videolan/libplacebo/-/merge_requests/737
2025-08-21 15:34:03 +00:00
Niklas Haas
8e0de5d811 avfilter/setpts: also preserve duration when not stripping fps
It makes sense to treat the presence of a frame duration and the presence
of frame rate metadata identically - because both convey effectively the same
amount of information.

In f121d95 and fa110c3 respectively, this information was stripped by default,
originally to work-around bugs when changing the PTS information of a stream
being fed to some encoders. (See https://trac.ffmpeg.org/ticket/10886)

Later, commit 959b799c restored the ability to preserve the frame rate
medatata via the `strip_fps` option, but this option did not extend to also
include the frame duration.

This commit resolves the scenario by making `frame_rate` and `duration`
handled in a consistent manner, so that the frame rate information is
generally preserved unless explicitly stripped by the user.

While it does regress the exact invocation presented in the trac ticket unless
using `strip_fps=yes`, I consider this an acceptable trade-off, especially in
light of the fact that the `fps` filter also exists and is arguably the better
tool for the task at hand.
2025-08-21 14:55:12 +02:00
Zhao Zhili
2796ce20d9 avfilter/vf_colordetect: Reorder to avoid null pointer check
Follow the general practice to assign the C implementation to DSP,
then the SIMD-optimized version.
2025-08-21 11:01:35 +08:00
Lynne
451e6bed43 lavfi/bwdif_vulkan: fix typo in temp_diff assignment
Thanks to Niklas Haas for pointing this out.
2025-08-20 22:06:21 +09:00
Niklas Haas
9b8b78a815 avfilter/vf_colordetect: detect fully opaque alpha planes
It can be useful to know if the alpha plane consists of fully opaque
pixels or not, in which case it can e.g. safely be stripped.

This only requires a very minor modification to the AVX2 routines, adding
an extra AND on the read alpha value with the reference alpha value, and a
single extra cheap test per line.

detect_alpha_8_full_c:                                2849.1 ( 1.00x)
detect_alpha_8_full_avx2:                              260.3 (10.95x)
detect_alpha_8_full_avx512icl:                         130.2 (21.87x)
detect_alpha_8_limited_c:                             8349.2 ( 1.00x)
detect_alpha_8_limited_avx2:                           756.6 (11.04x)
detect_alpha_8_limited_avx512icl:                      364.2 (22.93x)
detect_alpha_16_full_c:                               1652.8 ( 1.00x)
detect_alpha_16_full_avx2:                             236.5 ( 6.99x)
detect_alpha_16_full_avx512icl:                        134.6 (12.28x)
detect_alpha_16_limited_c:                            5263.1 ( 1.00x)
detect_alpha_16_limited_avx2:                          797.4 ( 6.60x)
detect_alpha_16_limited_avx512icl:                     400.3 (13.15x)
2025-08-18 18:50:00 +00:00
Niklas Haas
ae3c5ac2c1 avfilter/vf_colordetect: remove extra safety margin on premul check
This safety margin was motivated by the fact that vf_premultiply sometimes
produces such illegally high values, but this has since been fixed by
603334a043, so there's no more reason to have this safety margin, at
least for our own code. (Of course, other sources may also produce such
broken files, but we shouldn't work around that - garbage in, garbage out.)

See-Also: 603334a043
2025-08-18 18:50:00 +00:00
Niklas Haas
c96ccd78fc avfilter/vf_colordetect: rename p, q, k variables for clarity
Purely cosmetic.

Motivated in part because I want to depend on the assumption that P
represents the maximum alpha channel value.
2025-08-18 18:50:00 +00:00
Niklas Haas
34ad857d2b avfilter/vf_colordetect: slightly change detect_alpha() signature
Basically cosmetic.

I want to expand this to detect more than a single property about the alpha
channel at the same time; so we first need a way for this function to
return a more complex result.

Move the enum AlphaMode to the header and formally generalize the return
signature a bit to allow returning more than just one value.
2025-08-18 18:50:00 +00:00
Gyan Doshi
7df9271272 avfilter/whisper: correct option formatting 2025-08-17 00:58:03 +00:00
Zhao Zhili
1d06e8ddcd avfilter/af_whisper: fix broken output for multibyte character
text + 1 can break a multibyte character, e.g., Chinese in UTF-8.
There is no space at the beginning in this case.
2025-08-16 13:58:14 +00:00