1726 Commits

Author SHA1 Message Date
Kacper Michajłow
3cc10b5ff6 fftools/cmdutils: use strcpy directly, the length is computed already
There is no need to scan for NULL, if we inject it ourselves.

Fixes: warning: 'strncat' specified bound 10 equals source length [-Wstringop-overflow=]
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-12-03 23:45:44 +00:00
Zhao Zhili
540aacf759 fftools/ffmpeg: add force key frame by scdet metadata support
For example:

./ffmpeg -hwaccel videotoolbox \
	-i input.mp4 -c:a copy \
	-vf scdet=threshold=10 \
	-c:v h264_videotoolbox \
	-force_key_frames scd_metadata \
	-g 1000 -t 30 output.mp4
2025-12-02 03:03:55 +00:00
Niklas Haas
38a5fcc02c fftools/ffmpeg_filter: close all no-longer needed inputs
Currently, the thread loop of ffmpeg_filter essentially works like this:

while (1) {
    frame, idx = get_from_decoder();
    err = send_to_filter_graph(frame);
    if (err) { // i.e. EOF
        close_input(idx);
        continue;
    }

    while (filtered_frame = get_filtered_frame())
        send_to_encoder(filtered_frame);
}

The exact details are not 100% correct since the actual control flow is a bit
more complicated as a result of the scheduler, but this is the general flow.

Notably, this leaves the possibility of leaving a no-longer-needed input
permanently open if the filter graph starts producing infinite frames (during
the second loop) *after* it finishes reading from an input, e.g. in a filter
graph like -af atrim,apad.

This patch avoids this issue by always querying the status of all filter graph
inputs and explicitly closing any that were closed downstream; after each round
of reading output frames. As a result, information about the filtergraph being
closed can now propagate back upstream, even if the filter is no longer
requesting any input frames (i.e. input_idx == fg->nb_inputs).

Fixes: https://trac.ffmpeg.org/ticket/11061
See-Also: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20457#issuecomment-6208
2025-11-26 13:15:16 +00:00
Ayose
efd6e85abb fftools/tf_mermaid: close subgraph header when there are no inputs.
Ensure that the fragment to close the header (`</div>\"]`) is written when the
function `mermaid_print_section_header` is called only once, which happens when
the filtergraph has no inputs.
2025-11-24 20:01:40 +00:00
Ayose
5717c5c1e1 fftools/tf_mermaid: logic to complete subgraphs in a reusable function.
`subgraph` blocks can be closed in different places. The logic to complete the
header is moved to a function, so it can be reused later.
2025-11-24 20:01:40 +00:00
Dmitrii Okunev
eccbbd528e fftools: Fix MediaCodec on Android15+
On Android15+ MediaCodec HAL backend was switched from HIDL to AIDL.
As a result, MediaCodec operations started to hang, see:

    https://trac.ffmpeg.org/ticket/11363
    https://github.com/termux/termux-packages/issues/21264
    https://issuetracker.google.com/issues/382831999

To fix that it is necessary to initialize binder thread pool.

Signed-off-by: Dmitrii Okunev <xaionaro@dx.center>
2025-11-23 12:53:43 +00:00
Romain Beauxis
c75ada5040 ffplay: print new metadata 2025-11-22 18:38:40 +00:00
Niklas Haas
133a0bcb13 fftools/ffmpeg_sched: prevent demuxers from getting stuck
When the furthest-behind stream is being fed by a demuxer that is also
feeding packets to a choked filter graph, we need to unchoke that filter
graph to prevent the demuxer from getting stuck trying to write packets to
the choked filter graph.

This situation can also apply recursively - if the demuxer is also writing
to a filtergraph that is also reading from a choked demuxer, there is a
similar deadlock.

Solve all such deadlocks by just brute-force recursively unchoking all
nodes that can somehow prevent this demuxer from writing packets. This
should normally not result in any change in behavior, unless audio/video
streams are badly desynchronized, in which case it may result in extra
memory usage from the too-far-ahead stream buffering packets inside the
muxer. (But this is, of course, preferable to a deadlock)

Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/20611
2025-11-10 09:55:40 +00:00
Kacper Michajłow
08d327e92c fftools/ffmpeg: suppress unused variable warning, but using it
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-11-05 19:04:00 +01:00
James Almer
492310ec6e fftools/ffmpeg_demux: fix memory leak when creating tile grid filtergraphs
If av_packet_side_data_to_frame() fails, graph_str will leak.

Fixes CID 1668266.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-11-01 12:41:23 -03:00
James Almer
acd2cfe2eb fftools/ffmpeg: make fg_create{_simple,} clear the input string pointer
The graph string is either freed or attached to the filtergraph, so it's best to
not leave a dangling pointer with the caller.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-11-01 12:41:23 -03:00
James Almer
5ff8395e78 fftools/ffmpeg_demux: create a filtegraph to merge HEIF tiles automatically
Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
433d18a1d9 fftools/ffmpeg_filter: handle metadata from stream group in relevant the filtergraphs
Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
dec3cc0138 fftools/ffmpeg_filter: allow binding unlabeled filtergraphs
Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
e7cf188bb6 fftools/ffmpeg_filter: reindent after the previous commit
Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
9dcd25b7cd fftools/ffmpeg_filter: allow removing filtergraphs that contain unbound outputs
Actual practical implementation of the previous commit.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
cce85642c9 fftools/ffmpeg_sched: add a function to remove a filtergraph from the scheduler
For the purpose of merging streams in a stream group, a filtergraph can't be
created once we know it will be used. Therefore, allow filtergraphs to be
removed from the scheduler after being added.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
c751ad2c36 fftools/ffmpeg_mux_init: allow creating streams from filtergraphs created out of stream groups
Several formats are being designed where more than one independent video or
audio stream within a container are part of what should be a single combined
output. This is the case for HEIF (Defining a grid where the decoded output
of several separate video streams are to be placed to generate a single output
image) and IAMF (Defining audio streams where channels are present in separate
coded stream).

AVStreamGroup was designed and implemented in libavformat to convey this
information, but the actual merging is left to the caller.
This change allows the FFmpeg CLI to take said information, parse it, and
create filtergraphs to merge the streams, making the combined output be usable
automatically further in the process.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
99ec0752d7 fftools/ffmpeg_demux: add InputStreamGroup to store stream groups
Preparatory work for upcoming changes. Make ffmpeg keep track of stream groups
internally.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
ba0dc3d49e fftools/ffmpeg_opt: add helpers to match stream groups
Will be used to check for specifiers that match a given stream group and not
a stream within one.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
7b18beb477 fftools: pass global side data through using a separate array
This keeps global and per frame side data clearly separated, and actually
propagates the former as it comes out from the buffersink filter.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
James Almer
7ac1b410e1 fftools/ffmpeg_filter: fix passing certain parameters to inputs bound to a filtergraph output
Certain parameters, like calculated framerate, are unavailable when connecting
the output of a filtergraph to the input of another.

This fixes command lines like

ffmpeg -lavfi "testsrc=rate=1:duration=1[out0]" -filter_complex "[out0]null[out1]" -map [out1] -y out.png

Signed-off-by: James Almer <jamrial@gmail.com>
2025-10-30 11:02:01 -03:00
caifan3
91512f0856 fftools/cmdutils: Rename file_read to read_file_to_string to avoid symbol collision with NuttX
The function name 'file_read' is too generic and conflicts with a function
of the same name in the NuttX kernel. Since NuttX links kernel and userspace
into a single binary, this causes a symbol collision when building FFmpeg tools.

Signed-off-by: caifan3 <caifan3@xiaomi.com>
2025-10-28 02:48:05 +00:00
Kacper Michajłow
20f59bfc6b fftools/ffmpeg: use enum VideoSyncMethod instead of int
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-10-25 01:01:14 +02:00
Gyan Doshi
535d4047d3 ffmpeg: unbreak max_error_rate application
The calculation of decode error rate neglected to cast
its operands to float, thus always leading to a value of 0.
2025-10-21 13:22:08 +00:00
zhanghongyuan
0bc54cddb1 fftools/opt_common: add long-form license option
Add "license" as a long-form command line option alongside the existing
"L" short option for showing license information. This maintains
consistent option naming patterns with other commands that provide both
short and long forms (help/?/help, etc.) and improves command line
usability by providing more descriptive option names.
2025-10-12 03:26:21 +00:00
Niklas Haas
fd1fd5850d fftools/ffmpeg_sched: unchoke upstream nodes on recv-closed filter inputs
This allows upstream filters to observe EOF on their corresponding outputs
and terminate early, which is particularly useful for decoders and demuxers
that may then gracefully release their resources.

Prevents "leaking" memory for previously used, but now unused, filter inputs.
2025-09-30 13:16:59 +02:00
Niklas Haas
d43fd5b332 fftools/ffmpeg_sched: close stream when sch_filter_send receives EOF
THis is currently done by sch_demux_send() (via demux_stream_send_to_dst()),
sch_enc_send() (via enc_send_to_dst()), and sch_dec_send() (via
dec_send_to_dst()), but not by sch_filter_send().

Implement the same queue-closing logic for them. The main benefit here is that
this will allow them to mark downstream inputs as send-done (in addition
to received-done), which is useful for a following commit.
2025-09-30 13:16:59 +02:00
Niklas Haas
56d9ca69d7 fftools/ffmpeg_dec: free decoder ctx after EOF
The codec context is no longer used after the decoder thread exits, but
still idly sticks around until program exit, wasting memory.
2025-09-30 13:16:59 +02:00
Niklas Haas
9d0b88feb1 fftools/ffmpeg_sched: forward demuxer choke status to dst queues
Cut off a choked demuxer's output codec/filter queues, effectively preventing
them from processing packets while the demuxer is choked. Avoids downstream
nodes from piling up extra input that a demuxer shouldn't currently be
sending.

The main benefit of this is to avoid queuing up excess packets that don't want
to be decoded yet, reducing memory consumption for idle inputs by preventing
them from being read earlier than needed.
2025-09-30 13:16:59 +02:00
Niklas Haas
59a847a237 fftools/thread_queue: allow choking thread queues directly
Currently, when a demuxer thread is choked, it will avoid queuing more
packets, but any packets already present on the thread queue will still be
processed.

This can be quite wasteful if the choke is due to e.g. decoder not being
needed yet, such as in a filter graph involving concatenation-style filters.
Adding the ability to propagate the choke status to the thread queue directly
allows downstream decoders and filter graphs to avoid unnecessary work and
buffering.

Reduces the effective latency between scheduler updates and changes in the
thread workfload.
2025-09-30 13:16:59 +02:00
Niklas Haas
5f4cbb5617 fftools/ffmpeg_sched: choke inputs during filtergraph configuration
Currently, while the filter graph is being initially created, the scheduler
continues demuxing frames on the last input that happened to be active before
the filter graph was complete.

This can lead to an excess number of decoded frames "piling" up on this input,
regardless of whether or not it will actually be requested by the configured
filter graph. Suspending the filter graph during this initialization phase
reduces the amount of wasted memory.
2025-09-30 13:16:59 +02:00
Niklas Haas
23f1f094f8 fftools/ffmpeg_sched: get rid of src_sched
This field is just saving (typically) a single pointer indirection; and IMO
makes the logic and graph relations unnecessarily complicated. I am also
considering adding choking logic to decoders and encoders as well, which this
field would get in the way of.

Apart from the unchoking logic in unchoke_for_input(), the only other place
that uses this field is the (cold) function check_acyclic(), which can be
served just as well with a simple function to do the graph traversal there.
2025-09-30 13:16:59 +02:00
Niklas Haas
fd4b5b24ce fftools/ffmpeg_sched: lower default frame queue size
I tested this extensively under different conditions and could not come up
with any scenario where using a larger queue size was actually beneficial.
Moreover, having such a large default queue is very wasteful especially
for larger frame sizes; and can in the worst case lead to an extra ~50% memory
footprint per input (with the default 16 threads), regardless of whether that
input is currently in use or not.

My methodology was to add logging in the event of a queue underrun/overrun,
and then observe and then observe the frequency of such events in practice,
as well as the impact on performance. I came up with an example filter graph
involving decoding, filtering and encoding with several input files and
various changes to move the bottleneck around.

I found that, in all configurations I tested, with all thread counts and
bottlenecks, using a queue size of 2 frames yielded practically identical
performance to a queue size of 8 frames. I was only able to consistently
measure a slowdown when restricting the queue to a single frame, where the
underruns ended up making up almost 1.1% of frame events in the worst case.

A summary of my test log follows:

= Bottleneck in decoder =

ffmpeg -i A -i B -i C -filter_complex "concat=n=3" -f null -

== 16 threads ==

=== Queue statistics (dec -> filtergraph) ===
- 8 frames = 91355 underruns, 1 overrun
- 4 frames = 91381 underruns, 2 overruns
- 2 frames = 91326 underruns, 21 overruns
- 1 frame  = 91284 underruns, 102 overruns

=== Time elapsed ===
- 8 frames = 14.37s
- 4 frames = 14.28s
- 2 frames = 14.27s
- 1 frame  = 14.35s

== 1 thread ==

=== Queue statistics (dec -> filtergraph) ===
- 8 frames = 91801 underruns, 0 overruns
- 4 frames = 91929 underruns, 1 overrun
- 2 frames = 91854 underruns, 7 overruns
- 1 frame  = 91745 underrons, 83 overruns

=== Time elapsed ===
- 8 frames = 39.51s
- 4 frames = 39.94s
- 2 frames = 39.91s
- 1 frame  = 41.69s

= Bottleneck in filter graph: =

ffmpeg -i A -i B -i C -filter_complex "concat=n=3,scale=3840x2160" -f null -

== 16 threads ==

=== Queue statistics (dec -> filtergraph) ===
- 8 frames =  277 underruns, 84673 overruns
- 4 frames =  640 underruns, 86523 overruns
- 2 frames =  850 underruns, 88751 overruns
- 1 frame  = 1028 underruns, 89957 overruns

=== Time elapsed ===
- 8 frames = 26.35s
- 4 frames = 26.31s
- 2 frames = 26.38s
- 1 frame  = 26.55s

== 1 thread ==

=== Queue statistics (dec -> filtergraph) ===
- 8 frames = 29746 underruns, 57033 overruns
- 4 frames = 29940 underruns, 58948 overruns
- 2 frames = 30160 underruns, 60185 overruns
- 1 frame  = 30259 underruns, 61126 overruns

=== Time elapsed ===
- 8 frames = 52.08s
- 4 frames = 52.49s
- 2 frames = 52.25s
- 1 frame  = 52.69s

= Bottleneck in encoder: =

ffmpeg -i A -i B -i C -filter_complex "concat=n=3" -c:v libx264 -preset veryfast -f null -

== 1 thread ==

== Queue statistics (filtergraph -> enc) ==
- 8 frames = 26763 underruns, 63535 overruns
- 4 frames = 26863 underruns, 63810 overruns
- 2 frames = 27243 underruns, 63839 overruns
- 1 frame  = 27670 underruns, 63953 overruns

== Time elapsed ==
- 8 frames = 89.45s
- 4 frames = 89.04s
- 2 frames = 89.24s
- 1 frame  = 90.26s
2025-09-30 13:16:59 +02:00
Niklas Haas
15407cf90b fftools/ffmpeg_sched: relax queue size assertion
The code in the decoder just cares about allocating enough extra hw frames
to cover the size of the queue; but there's no reason we actually *have* to
use this many. We can safely relax the assertion to a <= check.
2025-09-30 13:16:59 +02:00
Kacper Michajłow
d6cb0d2c2b ALL: move av_unused to conform with standard requirement
This is required placement by standard [[maybe_unused]] attribute, works
the same for __attribute__((unused)).

Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-09-26 16:15:46 +00:00
zhanghongyuan
b5e0d9f648 fftools/opt_common: add separator line to show_filters output
Add a separator line to show_filters() function output to maintain
consistent formatting with other show functions like show_codecs,
show_formats_devices, etc. This provides uniform formatting across
all command-line output functions for better readability and
parsing by external tools.
2025-09-22 02:47:40 +00:00
Marvin Scholz
2a1d5dd720 fftools: use fopen_utf8 to open FFREPORT
Should fix #11240
2025-09-10 11:38:43 +00:00
James Almer
05b6b30ee1 fftools/ffplay: set the alphamodes buffersink array option instead of the binary one
And add the missing goto fail. This should ensure the alpha mode is set and remove
bogus warnings printed by ffplay.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-08 18:38:16 -03: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
2032820e90 fftools/ffprobe: add AVFrame.alpha_mode support 2025-09-02 17:06:25 +02:00
Niklas Haas
4f99026c9f fftools/ffplay: add alpha_mode support
SDL seems to hard-code straight alpha compositing.
2025-09-02 17:06:25 +02:00
Niklas Haas
f07573f496 fftools/ffmpeg: add alpha_mode support
The implementation exactly mirrors the existing plumbing for color_ranges
and color_spaces.
2025-09-02 17:06:25 +02:00
Patrick Wang
d7173e982e fftools/ffmpeg: fix gracefully shutdown
d119ae2fd8 removed the loop-breaking condition
received_sigterm.
Thus, signals no longer gracefully shutdown ffmpeg.

Fixes: #10834

Signed-off-by: Patrick Wang <mail6543210@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-09-02 15:56:27 +02:00
James Almer
e62f9a071b ffprobe: print EXIF packet side data size
Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-01 12:07:14 -03:00
James Almer
70a6ad6c74 fftools/ffmpeg_demux: ensure the display_rotation option is honored
If requested, it should have priotity over any coded value.
Fixes ticket #11649.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-08-26 11:44:09 -03:00
Leo Izen
5d4f873ff3 fftools/ffprobe: print EXIF side data size
We don't need to print the tags here because they're added as dict
elements to AVFrame->metadata and are printed elsewhere with ffprobe
-show_frames.

Signed-off-by: Leo Izen <leo.izen@gmail.com>
2025-08-19 11:26:48 -04:00
Andreas Hartmann
11a89bfe7b ffmpeg_opt: Parse regular options in ffpreset files
instead of only AV-specific options. The previous code assumed that any
option not defining the codec in an `ffpreset` file is an AVOption. This
for example prevented the use of options defined in `OptionDef[]`, like
`-pix_fmt`, as part of preset files, requiring users to type these out
every time.

Closes: #1530
Signed-off-by: Andreas Hartmann <hartan@7x.de>
2025-08-18 13:54:30 +00:00
Michael Niedermayer
cdbb5f1b93 fftools/ffmpeg_mux_init: Use 64bit for score computation in map_auto_video()
Fixes: signed integer overflow: 10 * 1952737655 cannot be represented in type 'int'
Fixes: PoC_avi_demux

Found-by: 2ourc3 (Salim LARGO)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-08-14 00:36:29 +00:00
softworkz
1ad97b82f3 fftools/resources: Update .gitignore
Signed-off-by: softworkz <softworkz@hotmail.com>
2025-08-13 17:39:49 +00:00