ffmpeg-devel
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
August 2026
- 42 participants
- 340 discussions
27 Sep '26
This patch is based mostly on the one by Victor Luchits for 6.1, which
was in turn based on my patch for 3.3.2. This patch is for the current
repository version, 8.1. I've made a few minor changes to the code to
make it about ten percent faster than the original code.
The patch allows -b:v to control the target bit rate for the
compressed video. The tolerance for the video stream can be set using
-bt:v, but the encoder will allow lower bit rate frames through when
the frame being encoded is too simple to generate the desired bit
rate. We could pad the stream to force it to the target bit rate, but
that is currently considered not necessary. We only need the bit rate
to not exceed the desired rate (within the tolerance) to avoid issues
with devices like single or double speed CDROMs. If not specified, the
tolerance defaults to five percent of the set bit rate.
The patch also adds keyframe detection on the decoding of the video stream.
It had been suggested when Vic posted the first patch that something
like binary search be applied to increase the speed of encoding. I did
that - it made the encoding slower. This makes sense since 1) the
relationship between lambda and the bit rate changes with the content
of the frame, and 2) the relationship between lambda and the bit rate
is a non-linear function. Binary search is only faster if the range to
be searched has equal probability of the target being at any point
within the range. This is not the case for the RoQ encoder. As lambda
increases, the bit rate drops at a slower rate. If you modify the
binary search to account for this, the algorithm becomes more and more
like the one currently used. The patch here is approximately
twenty-five to thirty percent faster than the best binary search
routine I could manage on a range of different encoded videos, be it
live action, anime, or computer generated video.
I welcome all suggestions and criticisms. I'd love nothing better than
to improve the performance of the encoder, if possible.
Signed-off-by: Joe Fenton <jlfenton65(a)gmail.com>
2
1
22 Sep '26
Commit 88d80cb975 (avcodec/put_bits: Make bit buffers 64-bit, 2020-07-18)
introduced a 64-bit bit buffer, reducing the number of times it must be
flushed. However, it is only enabled for x86-64, even though AArch64
also supports efficient 64-bit operations. Enable it on AArch64 as well
to improve performance.
Benchmarking on various formats shows consistently positive results
comparable to the x86-64 improvements:
dnxhd 84.13 fps -> 85.04 fps (+1.1%)
ffv1 88.77 fps -> 91.36 fps (+2.9%)
huffyuv 286.93 fps -> 310.34 fps (+8.2%)
jpegls 28.99 fps -> 29.86 fps (+3.0%)
magicyuv 118.76 fps -> 124.18 fps (+4.6%)
mjpeg 213.71 fps -> 217.53 fps (+1.8%)
mpeg1video 311.31 fps -> 315.19 fps (+1.2%)
mpeg2video 310.19 fps -> 313.93 fps (+1.2%)
mpeg4 286.53 fps -> 290.95 fps (+1.5%)
prores 82.57 fps -> 85.53 fps (+3.6%)
prores_ks 106.31 fps -> 109.24 fps (+2.8%)
rv20 223.64 fps -> 226.45 fps (+1.3%)
utvideo 119.47 fps -> 123.72 fps (+3.6%)
Benchmark details: Transcoding the first 720 frames of Big Buck Bunny
at 1080p on an M3 MacBook Air running macOS 15.7.9, compiled with
(Homebrew) clang 22.1.8. The benchmarks were single-threaded, with ten
runs per format, the two highest and lowest results were discarded.
Signed-off-by: Dominik Loidolt <dominik.loidolt(a)univie.ac.at>
---
I have only benchmarked this on my MacBook. I do not have access to
other AArch64 hardware, so performance on other AArch64 implementations
remains untested. Naively I would guess they benefit from the wider bit
buffer too.
It may also make sense to benchmark other 64-bit platforms with the
wider bit buffer (as well as 32-bit platforms?).
Dominik
P.S. This is my first FFmpeg patch, so please let me know if I missed
anything.
libavcodec/put_bits.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h
index 98c0d7e51f..eea249a01f 100644
--- a/libavcodec/put_bits.h
+++ b/libavcodec/put_bits.h
@@ -34,7 +34,7 @@
#include "libavutil/avassert.h"
#include "libavutil/common.h"
-#if ARCH_X86_64
+#if ARCH_X86_64 || ARCH_AARCH64
// TODO: Benchmark and optionally enable on other 64-bit architectures.
typedef uint64_t BitBuf;
#define AV_WBBUF AV_WB64
base-commit: 44095007d00f908dbdeaadaccc50d7bdeff36a37
--
2.55.0
1
1
[FFmpeg-devel][PATCH RESEND] libavutil: Don't require __STDC_CONSTANT_MACROS on C++11
by Clément Péron 09 Sep '26
by Clément Péron 09 Sep '26
09 Sep '26
Prior to C++11, some of the C99 macros in stdint.h were not defined
unless __STDC_CONSTANT_MACROS was defined before including the header.
C++11 updated this so that the C99 macros are unconditionally defined -
__STDC_CONSTANT_MACROS is no longer required.
Update the conditional on the error message in common.h so that it does
not trigger when using a C++11 or later compiler.
---
libavutil/common.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/libavutil/common.h b/libavutil/common.h
index b9fbcc4d60..f90de59157 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -26,7 +26,11 @@
#ifndef AVUTIL_COMMON_H
#define AVUTIL_COMMON_H
-#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) &&
!defined(UINT64_C)
+/*
+ * In C++ prior to C++11, some of the C99 defines in stdint.h are
omitted unless
+ * you request to enable them by setting __STDC_CONSTANT_MACROS
+ */
+#if defined(__cplusplus) && __cplusplus < 201103L &&
!defined(__STDC_CONSTANT_MACROS) && !defined(UINT64_C)
#error missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS
#endif
--
2.29.2
1
1
I’ve developed a set of patches that enable FFmpeg to be optionally given a single
JSON containing all commands and parameters.
This…
* makes building FFMPEG commands programmatically easier and more reliable
* avoids difficulties that come with very long FFMPEG command-line strings
* avoids the requirement to escape slashes and other common characters
(particularly useful when passing urls or presigned urls)
* makes it easier to save and modify ffmpeg “recipes” (taking a template,
modifying one or more parameters, saving as a new ffmpeg command)
The only slight caveat is that filter complexes must still be defined as a string.
I believe that this design is independent of any particular command-line parameters, and so it shouldn’t break when
commands or parameters are added or changed (so, it shouldn’t be a maintenance headache).
There is also a json_cmd_gen command that converts a FFMPEG command-line string into the equivalent JSON,
and a json_cmd_print command that converts a FFMPEG JSON into the equivalent command-line string.
Add -json_cmd option that reads ffmpeg parameters from a structured
JSON file. The JSON format supports global options, input/output file
specifications with per-file options, and loopback decoder definitions.
The implementation includes a self-contained JSON parser (no external
dependencies) and an argv builder that translates JSON key-value pairs
into the equivalent command-line arguments.
Option values support strings, booleans (true for flags, false to skip),
numbers, arrays (for repeated options like -map), and null (skip).
Keys starting with "/" use FFmpeg's file-loading syntax.
Signed-off-by: Tom Vaughan
---
fftools/Makefile | 1 +
fftools/ffmpeg.c | 14 +
fftools/ffmpeg_json.c | 831 ++++++++++++++++++++++++++++++++++++++++++
fftools/ffmpeg_json.h | 76 ++++
fftools/ffmpeg_opt.c | 12 +
5 files changed, 934 insertions(+)
create mode 100644 fftools/ffmpeg_json.c
create mode 100644 fftools/ffmpeg_json.h
diff --git a/fftools/Makefile b/fftools/Makefile
index 01b16fa8f4..e6eea056eb 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -17,6 +17,7 @@ OBJS-ffmpeg += \
fftools/ffmpeg_enc.o \
fftools/ffmpeg_filter.o \
fftools/ffmpeg_hw.o \
+ fftools/ffmpeg_json.o \
fftools/ffmpeg_mux.o \
fftools/ffmpeg_mux_init.o \
fftools/ffmpeg_opt.o \
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index b394243f59..5dcd641700 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -82,6 +82,7 @@
#include "compat/android/binder.h"
#endif
#include "ffmpeg.h"
+#include "ffmpeg_json.h"
#include "ffmpeg_sched.h"
#include "ffmpeg_utils.h"
#include "graph/graphprint.h"
@@ -984,6 +985,8 @@ int main(int argc, char **argv)
int ret;
BenchmarkTimeStamps ti;
+ int json_argc = 0;
+ char **json_argv = NULL;
init_dynload();
@@ -999,6 +1002,15 @@ int main(int argc, char **argv)
show_banner(argc, argv, options);
+ /* Handle -json_cmd <file>: parse JSON file into argc/argv */
+ if (argc == 3 && !strcmp(argv[1], "-json_cmd")) {
+ ret = ffmpeg_json_parse_file(argv[2], &json_argc, &json_argv);
+ if (ret < 0)
+ goto finish;
+ argc = json_argc;
+ argv = json_argv;
+ }
+
sch = sch_alloc();
if (!sch) {
ret = AVERROR(ENOMEM);
@@ -1051,6 +1063,8 @@ finish:
sch_free(&sch);
+ ffmpeg_json_free_argv(json_argc, &json_argv);
+
av_log(NULL, AV_LOG_VERBOSE, "\n");
av_log(NULL, AV_LOG_VERBOSE, "Exiting with exit code %d\n", ret);
diff --git a/fftools/ffmpeg_json.c b/fftools/ffmpeg_json.c
new file mode 100644
index 0000000000..3fdc1884e5
--- /dev/null
+++ b/fftools/ffmpeg_json.c
@@ -0,0 +1,831 @@
+/*
+ * JSON command file support for FFmpeg
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/avutil.h"
+#include "libavutil/error.h"
+#include "libavutil/log.h"
+#include "libavutil/mem.h"
+
+#include "cmdutils.h"
+#include "ffmpeg.h"
+#include "ffmpeg_json.h"
+
+/* ------------------------------------------------------------------ */
+/* Minimal JSON parser */
+/* ------------------------------------------------------------------ */
+
+/** JSON value type tag. */
+typedef enum {
+ JSON_NULL,
+ JSON_BOOL,
+ JSON_NUMBER,
+ JSON_STRING,
+ JSON_ARRAY,
+ JSON_OBJECT,
+} JsonType;
+
+typedef struct JsonValue JsonValue;
+
+/** Single key/value pair in a JSON object. */
+typedef struct JsonObjectEntry {
+ char *key; /**< property name */
+ JsonValue *value; /**< property value */
+} JsonObjectEntry;
+
+/** Parsed JSON value node. */
+struct JsonValue {
+ JsonType type; /**< discriminator tag */
+ union {
+ int bool_val; /**< JSON_BOOL */
+ double num_val; /**< JSON_NUMBER */
+ char *str_val; /**< JSON_STRING */
+ struct { /**< JSON_ARRAY */
+ JsonValue **items; /**< array elements */
+ int nb_items; /**< element count */
+ } arr;
+ struct { /**< JSON_OBJECT */
+ JsonObjectEntry *entries; /**< key/value pairs */
+ int nb_entries; /**< pair count */
+ } obj;
+ } u;
+};
+
+/* Forward declarations */
+static JsonValue *json_parse_value(const char **p);
+static void json_free(JsonValue *v);
+
+static void skip_whitespace(const char **p)
+{
+ while (**p == ' ' || **p == '\t' || **p == '\n' || **p == '\r')
+ (*p)++;
+}
+
+static JsonValue *json_alloc(JsonType type)
+{
+ JsonValue *v = av_mallocz(sizeof(*v));
+ if (v)
+ v->type = type;
+ return v;
+}
+
+/**
+ * Parse a JSON string literal.
+ * The opening '"' must be the current character at @p *p.
+ *
+ * @param p pointer to the current parse position (advanced past
+ * the closing quote on success)
+ * @return heap-allocated string, or NULL on error
+ */
+static char *json_parse_string_raw(const char **p)
+{
+ const char *s;
+ char *out, *dst;
+ size_t len;
+
+ if (**p != '"')
+ return NULL;
+ (*p)++; /* skip opening quote */
+ s = *p;
+
+ /* First pass: find the closing quote and measure output length */
+ len = 0;
+ while (*s && *s != '"') {
+ if (*s == '\\') {
+ s++;
+ if (!*s)
+ return NULL;
+ if (*s == 'u') {
+ /* \uXXXX – simplified: just store as-is in UTF-8 later */
+ if (!s[1] || !s[2] || !s[3] || !s[4])
+ return NULL;
+ s += 4;
+ len += 4; /* conservative upper bound for UTF-8 */
+ }
+ }
+ len++;
+ s++;
+ }
+ if (*s != '"')
+ return NULL;
+
+ out = av_malloc(len + 1);
+ if (!out)
+ return NULL;
+
+ dst = out;
+ while (**p != '"') {
+ if (**p == '\\') {
+ (*p)++;
+ switch (**p) {
+ case '"': *dst++ = '"'; break;
+ case '\\': *dst++ = '\\'; break;
+ case '/': *dst++ = '/'; break;
+ case 'b': *dst++ = '\b'; break;
+ case 'f': *dst++ = '\f'; break;
+ case 'n': *dst++ = '\n'; break;
+ case 'r': *dst++ = '\r'; break;
+ case 't': *dst++ = '\t'; break;
+ case 'u': {
+ /* Simplified \uXXXX: decode to UTF-8 */
+ unsigned cp = 0;
+ int i;
+ for (i = 0; i < 4; i++) {
+ (*p)++;
+ cp <<= 4;
+ if (**p >= '0' && **p <= '9') cp |= **p - '0';
+ else if (**p >= 'a' && **p <= 'f') cp |= **p - 'a' + 10;
+ else if (**p >= 'A' && **p <= 'F') cp |= **p - 'A' + 10;
+ else { av_free(out); return NULL; }
+ }
+ if (cp < 0x80) {
+ *dst++ = (char)cp;
+ } else if (cp < 0x800) {
+ *dst++ = (char)(0xC0 | (cp >> 6));
+ *dst++ = (char)(0x80 | (cp & 0x3F));
+ } else {
+ *dst++ = (char)(0xE0 | (cp >> 12));
+ *dst++ = (char)(0x80 | ((cp >> 6) & 0x3F));
+ *dst++ = (char)(0x80 | (cp & 0x3F));
+ }
+ break;
+ }
+ default:
+ av_free(out);
+ return NULL;
+ }
+ } else {
+ *dst++ = **p;
+ }
+ (*p)++;
+ }
+ *dst = '\0';
+ (*p)++; /* skip closing quote */
+ return out;
+}
+
+/** Parse a JSON string and wrap it in a JsonValue. */
+static JsonValue *json_parse_string(const char **p)
+{
+ JsonValue *v;
+ char *s = json_parse_string_raw(p);
+ if (!s)
+ return NULL;
+ v = json_alloc(JSON_STRING);
+ if (!v) {
+ av_free(s);
+ return NULL;
+ }
+ v->u.str_val = s;
+ return v;
+}
+
+/** Parse a JSON number literal and return it as a JsonValue. */
+static JsonValue *json_parse_number(const char **p)
+{
+ JsonValue *v;
+ char *end;
+ double d;
+
+ d = strtod(*p, &end);
+ if (end == *p)
+ return NULL;
+ v = json_alloc(JSON_NUMBER);
+ if (!v)
+ return NULL;
+ v->u.num_val = d;
+ *p = end;
+ return v;
+}
+
+/** Parse a JSON array and return it as a JsonValue. */
+static JsonValue *json_parse_array(const char **p)
+{
+ JsonValue *v = json_alloc(JSON_ARRAY);
+ if (!v)
+ return NULL;
+
+ (*p)++; /* skip '[' */
+ skip_whitespace(p);
+
+ if (**p == ']') {
+ (*p)++;
+ return v;
+ }
+
+ for (;;) {
+ JsonValue *item;
+ JsonValue **new_items;
+
+ skip_whitespace(p);
+ item = json_parse_value(p);
+ if (!item)
+ goto fail;
+
+ new_items = av_realloc_array(v->u.arr.items,
+ v->u.arr.nb_items + 1,
+ sizeof(*v->u.arr.items));
+ if (!new_items) {
+ json_free(item);
+ goto fail;
+ }
+ v->u.arr.items = new_items;
+ v->u.arr.items[v->u.arr.nb_items++] = item;
+
+ skip_whitespace(p);
+ if (**p == ']') {
+ (*p)++;
+ return v;
+ }
+ if (**p != ',')
+ goto fail;
+ (*p)++;
+ }
+
+fail:
+ json_free(v);
+ return NULL;
+}
+
+/** Parse a JSON object and return it as a JsonValue. */
+static JsonValue *json_parse_object(const char **p)
+{
+ JsonValue *v = json_alloc(JSON_OBJECT);
+ if (!v)
+ return NULL;
+
+ (*p)++; /* skip '{' */
+ skip_whitespace(p);
+
+ if (**p == '}') {
+ (*p)++;
+ return v;
+ }
+
+ for (;;) {
+ char *key;
+ JsonValue *val;
+ JsonObjectEntry *new_entries;
+
+ skip_whitespace(p);
+ key = json_parse_string_raw(p);
+ if (!key)
+ goto fail;
+
+ skip_whitespace(p);
+ if (**p != ':') {
+ av_free(key);
+ goto fail;
+ }
+ (*p)++;
+
+ skip_whitespace(p);
+ val = json_parse_value(p);
+ if (!val) {
+ av_free(key);
+ goto fail;
+ }
+
+ new_entries = av_realloc_array(v->u.obj.entries,
+ v->u.obj.nb_entries + 1,
+ sizeof(*v->u.obj.entries));
+ if (!new_entries) {
+ av_free(key);
+ json_free(val);
+ goto fail;
+ }
+ v->u.obj.entries = new_entries;
+ v->u.obj.entries[v->u.obj.nb_entries].key = key;
+ v->u.obj.entries[v->u.obj.nb_entries].value = val;
+ v->u.obj.nb_entries++;
+
+ skip_whitespace(p);
+ if (**p == '}') {
+ (*p)++;
+ return v;
+ }
+ if (**p != ',')
+ goto fail;
+ (*p)++;
+ }
+
+fail:
+ json_free(v);
+ return NULL;
+}
+
+/** Dispatch to the appropriate parser based on the next character. */
+static JsonValue *json_parse_value(const char **p)
+{
+ skip_whitespace(p);
+
+ switch (**p) {
+ case '"':
+ return json_parse_string(p);
+ case '{':
+ return json_parse_object(p);
+ case '[':
+ return json_parse_array(p);
+ case 't':
+ if (strncmp(*p, "true", 4) == 0) {
+ JsonValue *v = json_alloc(JSON_BOOL);
+ if (v)
+ v->u.bool_val = 1;
+ *p += 4;
+ return v;
+ }
+ return NULL;
+ case 'f':
+ if (strncmp(*p, "false", 5) == 0) {
+ JsonValue *v = json_alloc(JSON_BOOL);
+ if (v)
+ v->u.bool_val = 0;
+ *p += 5;
+ return v;
+ }
+ return NULL;
+ case 'n':
+ if (strncmp(*p, "null", 4) == 0) {
+ JsonValue *v = json_alloc(JSON_NULL);
+ *p += 4;
+ return v;
+ }
+ return NULL;
+ default:
+ if (**p == '-' || (**p >= '0' && **p <= '9'))
+ return json_parse_number(p);
+ return NULL;
+ }
+}
+
+/** Recursively free a parsed JSON value tree. */
+static void json_free(JsonValue *v)
+{
+ int i;
+ if (!v)
+ return;
+ switch (v->type) {
+ case JSON_STRING:
+ av_free(v->u.str_val);
+ break;
+ case JSON_ARRAY:
+ for (i = 0; i < v->u.arr.nb_items; i++)
+ json_free(v->u.arr.items[i]);
+ av_free(v->u.arr.items);
+ break;
+ case JSON_OBJECT:
+ for (i = 0; i < v->u.obj.nb_entries; i++) {
+ av_free(v->u.obj.entries[i].key);
+ json_free(v->u.obj.entries[i].value);
+ }
+ av_free(v->u.obj.entries);
+ break;
+ default:
+ break;
+ }
+ av_free(v);
+}
+
+/** Look up a key in a JSON object, returning the value or NULL. */
+static JsonValue *json_object_get(const JsonValue *obj, const char *key)
+{
+ int i;
+ if (!obj || obj->type != JSON_OBJECT)
+ return NULL;
+ for (i = 0; i < obj->u.obj.nb_entries; i++) {
+ if (strcmp(obj->u.obj.entries[i].key, key) == 0)
+ return obj->u.obj.entries[i].value;
+ }
+ return NULL;
+}
+
+/* ------------------------------------------------------------------ */
+/* argv builder */
+/* ------------------------------------------------------------------ */
+
+/** Growable argument vector used while building argc/argv. */
+typedef struct ArgvBuilder {
+ char **args; /**< argument strings */
+ int nb_args; /**< current argument count */
+ int capacity; /**< allocated slots in @p args */
+} ArgvBuilder;
+
+/** Append a single argument string to the builder. */
+static int argv_add(ArgvBuilder *b, const char *arg)
+{
+ char **new_args;
+
+ if (b->nb_args >= b->capacity) {
+ int new_cap = b->capacity ? b->capacity * 2 : 32;
+ new_args = av_realloc_array(b->args, new_cap, sizeof(*b->args));
+ if (!new_args)
+ return AVERROR(ENOMEM);
+ b->args = new_args;
+ b->capacity = new_cap;
+ }
+ b->args[b->nb_args] = av_strdup(arg);
+ if (!b->args[b->nb_args])
+ return AVERROR(ENOMEM);
+ b->nb_args++;
+ return 0;
+}
+
+/** Free all strings and reset the builder to empty. */
+static void argv_builder_free(ArgvBuilder *b)
+{
+ int i;
+ for (i = 0; i < b->nb_args; i++)
+ av_free(b->args[i]);
+ av_free(b->args);
+ memset(b, 0, sizeof(*b));
+}
+
+/**
+ * Add a JSON value as a CLI argument string to the argv builder.
+ * Handles strings of any length (no fixed buffer for JSON_STRING).
+ */
+static int argv_add_json_value(ArgvBuilder *b, const JsonValue *v)
+{
+ char buf[64];
+ switch (v->type) {
+ case JSON_STRING:
+ return argv_add(b, v->u.str_val);
+ case JSON_NUMBER:
+ /* Print integers without decimal point where possible */
+ if (v->u.num_val == (int64_t)v->u.num_val)
+ snprintf(buf, sizeof(buf), "%" PRId64, (int64_t)v->u.num_val);
+ else
+ snprintf(buf, sizeof(buf), "%g", v->u.num_val);
+ return argv_add(b, buf);
+ case JSON_BOOL:
+ snprintf(buf, sizeof(buf), "%d", v->u.bool_val);
+ return argv_add(b, buf);
+ default:
+ return AVERROR(EINVAL);
+ }
+}
+
+/**
+ * Build an option key string for the argv builder.
+ *
+ * Keys starting with "/" use FFmpeg's file-loading syntax: -/option filename
+ * reads the option value from a file. In JSON this is represented as:
+ * "/filter_complex": "graph.txt" => -/filter_complex graph.txt
+ *
+ * All other keys are emitted as -key.
+ */
+static int argv_add_option_key(ArgvBuilder *b, const char *key)
+{
+ char buf[256];
+ if (key[0] == '/') {
+ /* File-loading syntax: -/option */
+ snprintf(buf, sizeof(buf), "-%s", key);
+ } else {
+ snprintf(buf, sizeof(buf), "-%s", key);
+ }
+ return argv_add(b, buf);
+}
+
+/**
+ * Emit the options from an object into the argv builder.
+ *
+ * Each key becomes "-key" and each value becomes the argument:
+ * "c:v": "libx264" => -c:v libx264
+ * "y": true => -y
+ * "an": false => (skipped; use "an": true to disable audio)
+ * "map": ["0:v","0:a"] => -map 0:v -map 0:a
+ * "/filter_complex": "f.txt" => -/filter_complex f.txt (load from file)
+ */
+static int emit_options(ArgvBuilder *b, const JsonValue *opts)
+{
+ int i, j, ret;
+
+ if (!opts)
+ return 0;
+ if (opts->type != JSON_OBJECT) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: \"options\" must be an object.\n");
+ return AVERROR(EINVAL);
+ }
+
+ for (i = 0; i < opts->u.obj.nb_entries; i++) {
+ const char *key = opts->u.obj.entries[i].key;
+ const JsonValue *val = opts->u.obj.entries[i].value;
+
+ if (val->type == JSON_BOOL) {
+ if (val->u.bool_val) {
+ /* -flag */
+ ret = argv_add_option_key(b, key);
+ if (ret < 0) return ret;
+ }
+ /* false booleans are intentionally skipped (no-op) */
+ continue;
+ }
+
+ if (val->type == JSON_ARRAY) {
+ /* Repeated option: -key elem0 -key elem1 ... */
+ for (j = 0; j < val->u.arr.nb_items; j++) {
+ ret = argv_add_option_key(b, key);
+ if (ret < 0) return ret;
+
+ ret = argv_add_json_value(b, val->u.arr.items[j]);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: unsupported type in array for option \"%s\".\n", key);
+ return ret;
+ }
+ }
+ continue;
+ }
+
+ if (val->type == JSON_NULL) {
+ continue;
+ }
+
+ /* Scalar: -key value */
+ ret = argv_add_option_key(b, key);
+ if (ret < 0) return ret;
+
+ ret = argv_add_json_value(b, val);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: unsupported value type for option \"%s\".\n", key);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+/* ------------------------------------------------------------------ */
+/* Public API */
+/* ------------------------------------------------------------------ */
+
+/** @sa ffmpeg_json_parse_file() in ffmpeg_json.h for full documentation. */
+int ffmpeg_json_parse_file(const char *filename, int *out_argc, char ***out_argv)
+{
+ FILE *f = NULL;
+ long fsize;
+ char *data = NULL;
+ const char *p;
+ JsonValue *root = NULL;
+ JsonValue *global_opts, *inputs, *outputs, *decoders;
+ ArgvBuilder b = { 0 };
+ int ret, i;
+
+ *out_argc = 0;
+ *out_argv = NULL;
+
+ /* Read file contents */
+ f = fopen(filename, "rb");
+ if (!f) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: cannot open file '%s': %s\n", filename, strerror(errno));
+ return AVERROR(errno);
+ }
+
+ if (fseek(f, 0, SEEK_END) < 0) {
+ ret = AVERROR(errno);
+ goto fail;
+ }
+ fsize = ftell(f);
+ if (fsize < 0 || fsize > 100 * 1024 * 1024) { /* 100 MB sanity limit */
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: file '%s' is too large or unreadable.\n", filename);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ if (fseek(f, 0, SEEK_SET) < 0) {
+ ret = AVERROR(errno);
+ goto fail;
+ }
+
+ data = av_malloc(fsize + 1);
+ if (!data) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ if (fread(data, 1, fsize, f) != (size_t)fsize) {
+ av_log(NULL, AV_LOG_ERROR, "JSON: failed to read '%s'.\n", filename);
+ ret = AVERROR(EIO);
+ goto fail;
+ }
+ data[fsize] = '\0';
+ fclose(f);
+ f = NULL;
+
+ /* Parse JSON */
+ p = data;
+ root = json_parse_value(&p);
+ if (!root || root->type != JSON_OBJECT) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: failed to parse '%s' – root must be an object.\n", filename);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ /* Verify there are no trailing non-whitespace characters */
+ skip_whitespace(&p);
+ if (*p != '\0') {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: trailing content after root object in '%s'.\n", filename);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ /* Build argv: start with program name */
+ ret = argv_add(&b, "ffmpeg");
+ if (ret < 0) goto fail;
+
+ /* 1. Global options */
+ global_opts = json_object_get(root, "global_options");
+ if (global_opts) {
+ ret = emit_options(&b, global_opts);
+ if (ret < 0) goto fail;
+ }
+
+ /* 2. Input files: options then -i url */
+ inputs = json_object_get(root, "inputs");
+ if (inputs) {
+ if (inputs->type != JSON_ARRAY) {
+ av_log(NULL, AV_LOG_ERROR, "JSON: \"inputs\" must be an array.\n");
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ for (i = 0; i < inputs->u.arr.nb_items; i++) {
+ JsonValue *input = inputs->u.arr.items[i];
+ JsonValue *url, *opts;
+
+ if (input->type != JSON_OBJECT) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: each input must be an object.\n");
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ url = json_object_get(input, "url");
+ if (!url || url->type != JSON_STRING) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: input %d missing \"url\" string.\n", i);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ /* Emit input options before -i */
+ opts = json_object_get(input, "options");
+ ret = emit_options(&b, opts);
+ if (ret < 0) goto fail;
+
+ ret = argv_add(&b, "-i");
+ if (ret < 0) goto fail;
+ ret = argv_add(&b, url->u.str_val);
+ if (ret < 0) goto fail;
+ }
+ }
+
+ /* 3. Output files: options then url */
+ outputs = json_object_get(root, "outputs");
+ if (outputs) {
+ if (outputs->type != JSON_ARRAY) {
+ av_log(NULL, AV_LOG_ERROR, "JSON: \"outputs\" must be an array.\n");
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ for (i = 0; i < outputs->u.arr.nb_items; i++) {
+ JsonValue *output = outputs->u.arr.items[i];
+ JsonValue *url, *opts;
+
+ if (output->type != JSON_OBJECT) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: each output must be an object.\n");
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ url = json_object_get(output, "url");
+ if (!url || url->type != JSON_STRING) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: output %d missing \"url\" string.\n", i);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ /* Emit output options before the output url */
+ opts = json_object_get(output, "options");
+ ret = emit_options(&b, opts);
+ if (ret < 0) goto fail;
+
+ ret = argv_add(&b, url->u.str_val);
+ if (ret < 0) goto fail;
+ }
+ }
+
+ /* 4. Loopback decoders: options then -dec name */
+ decoders = json_object_get(root, "decoders");
+ if (decoders) {
+ if (decoders->type != JSON_ARRAY) {
+ av_log(NULL, AV_LOG_ERROR, "JSON: \"decoders\" must be an array.\n");
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ for (i = 0; i < decoders->u.arr.nb_items; i++) {
+ JsonValue *decoder = decoders->u.arr.items[i];
+ JsonValue *name, *opts;
+
+ if (decoder->type != JSON_OBJECT) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: each decoder must be an object.\n");
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ name = json_object_get(decoder, "name");
+ if (!name || name->type != JSON_STRING) {
+ av_log(NULL, AV_LOG_ERROR,
+ "JSON: decoder %d missing \"name\" string.\n", i);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ /* Emit decoder options before -dec */
+ opts = json_object_get(decoder, "options");
+ ret = emit_options(&b, opts);
+ if (ret < 0) goto fail;
+
+ ret = argv_add(&b, "-dec");
+ if (ret < 0) goto fail;
+ ret = argv_add(&b, name->u.str_val);
+ if (ret < 0) goto fail;
+ }
+ }
+
+ /* Add NULL sentinel for execv-style usage */
+ if (b.nb_args >= b.capacity) {
+ char **new_args = av_realloc_array(b.args, b.nb_args + 1, sizeof(*b.args));
+ if (!new_args) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ b.args = new_args;
+ }
+ b.args[b.nb_args] = NULL;
+
+ *out_argc = b.nb_args;
+ *out_argv = b.args;
+
+ /* Log the constructed command line */
+ {
+ int k;
+ av_log(NULL, AV_LOG_INFO, "JSON: constructed command line:\n ");
+ for (k = 0; k < b.nb_args; k++)
+ av_log(NULL, AV_LOG_INFO, " %s", b.args[k]);
+ av_log(NULL, AV_LOG_INFO, "\n");
+ }
+
+ json_free(root);
+ av_free(data);
+ return 0;
+
+fail:
+ if (f)
+ fclose(f);
+ json_free(root);
+ av_free(data);
+ argv_builder_free(&b);
+ return ret;
+}
+
+/** @sa ffmpeg_json_free_argv() in ffmpeg_json.h for full documentation. */
+void ffmpeg_json_free_argv(int argc, char ***argv)
+{
+ int i;
+ if (!argv || !*argv)
+ return;
+ for (i = 0; i < argc; i++)
+ av_free((*argv)[i]);
+ av_free(*argv);
+ *argv = NULL;
+}
diff --git a/fftools/ffmpeg_json.h b/fftools/ffmpeg_json.h
new file mode 100644
index 0000000000..d509f142d7
--- /dev/null
+++ b/fftools/ffmpeg_json.h
@@ -0,0 +1,76 @@
+/*
+ * JSON command file support for FFmpeg
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef FFTOOLS_FFMPEG_JSON_H
+#define FFTOOLS_FFMPEG_JSON_H
+
+/**
+ * Parse a JSON command file and convert it to an argc/argv pair
+ * compatible with ffmpeg_parse_options().
+ *
+ * The JSON file should have the following structure:
+ * {
+ * "global_options": { "option_name": "value", ... },
+ * "inputs": [
+ * { "url": "input_file", "options": { "option_name": "value", ... } },
+ * ...
+ * ],
+ * "outputs": [
+ * { "url": "output_file", "options": { "option_name": "value", ... } },
+ * ...
+ * ],
+ * "decoders": [
+ * { "name": "decoder_name", "options": { ... } },
+ * ...
+ * ]
+ * }
+ *
+ * Option values can be:
+ * - string: added as -option value (no length limit)
+ * - true: added as -option (flag)
+ * - false: skipped (no-op); to negate a boolean, use the "no"-prefixed
+ * name with true, e.g. "noautorotate": true
+ * - number: converted to string and added as -option value
+ * - array: option is repeated for each element, e.g. "map": ["0:v","0:a"]
+ * becomes -map 0:v -map 0:a
+ * - null: skipped
+ *
+ * Keys starting with "/" use FFmpeg's file-loading syntax:
+ * "/filter_complex": "graph.txt" => -/filter_complex graph.txt
+ *
+ * All fields except the root object are optional.
+ *
+ * @param filename Path to the JSON file
+ * @param[out] argc Number of arguments in the constructed argv
+ * @param[out] argv Constructed argument vector (caller must free with
+ * ffmpeg_json_free_argv())
+ * @return 0 on success, negative AVERROR code on failure
+ */
+int ffmpeg_json_parse_file(const char *filename, int *argc, char ***argv);
+
+/**
+ * Free an argv array previously allocated by ffmpeg_json_parse_file().
+ *
+ * @param argc Number of arguments
+ * @param argv Pointer to the argument vector (set to NULL on return)
+ */
+void ffmpeg_json_free_argv(int argc, char ***argv);
+
+#endif /* FFTOOLS_FFMPEG_JSON_H */
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 48e6816c19..abdc81988c 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1610,6 +1610,15 @@ static int opt_adrift_threshold(void *optctx, const char *opt, const char *arg)
}
#endif
+static int opt_json_cmd(void *optctx, const char *opt, const char *arg)
+{
+ av_log(NULL, AV_LOG_ERROR,
+ "-json_cmd requires exactly one argument: the path to a JSON file\n"
+ "containing ffmpeg commands and parameters. No other options are allowed.\n"
+ "Usage: ffmpeg -json_cmd <file.json>\n");
+ return AVERROR(EINVAL);
+}
+
static const char *const alt_channel_layout[] = { "ch_layout", NULL};
static const char *const alt_codec[] = { "c", "acodec", "vcodec", "scodec", "dcodec", NULL };
static const char *const alt_filter[] = { "af", "vf", NULL };
@@ -1622,6 +1631,9 @@ static const char *const alt_tag[] = { "atag", "vtag", "stag", NULL }
const OptionDef options[] = {
/* main options */
CMDUTILS_COMMON_OPTIONS
+ { "json_cmd", OPT_TYPE_FUNC, OPT_FUNC_ARG | OPT_EXIT,
+ { .func_arg = opt_json_cmd },
+ "read options from a JSON command file", "file" },
{ "f", OPT_TYPE_STRING, OPT_OFFSET | OPT_INPUT | OPT_OUTPUT,
{ .off = OFFSET(format) },
"force container format (auto-detected otherwise)", "fmt" },
--
2.49.0
3
9
In order to enable our Ampere-Server to host the Netint Quadra VPU card,
I had to buy it a new OCP3 Network Card, to free up the only working
PCIe slot.
I bought the cheapest cheapest model I could find, which was a
refurbished Lenovo one:
> https://www.serverschmiede.com/en/lenovo-mellanox-connectx6-lx-2x-10-25gb-s…
There was also an Intel X710 option, which would have been 10€ cheaper,
but a mlx5 driven NIC is usually much nicer and friendlier for
virtualization than those old Intel ones. So the 10€ extra seemed a good
deal.
The total cost of the order thus was 208€ for the card, plus 11.07€
shipping, for a total of 219.07€.
Timo
3
3
31 Aug '26
PR #24340 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24340
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24340.patch
From 85b9bc8110e73c5e701a68b02aaedfcfe84dd344 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 22:36:03 +0200
Subject: [PATCH 1/2] avcodec/h264_mvpred: Optimize always-false checks away
data_partitioning is only used with CAVLC, not CABAC.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/h264_mvpred.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libavcodec/h264_mvpred.h b/libavcodec/h264_mvpred.h
index 1d5f22417e..bcc058b1e4 100644
--- a/libavcodec/h264_mvpred.h
+++ b/libavcodec/h264_mvpred.h
@@ -676,8 +676,8 @@ static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int m
int nnz_mask = -1;
int nnz_excluded = 64; // 64: unavailable, 0: present but empty
- if (sl->data_partitioning && h->ps.pps->constrained_intra_pred &&
- IS_INTRA(mb_type)) {
+ if (!CABAC(h) && sl->data_partitioning &&
+ h->ps.pps->constrained_intra_pred && IS_INTRA(mb_type)) {
nnz_mask = IS_INTRA(-1);
if (h->workaround_bugs & FF_BUG_H264_DP_NNZ)
nnz_excluded = 0;
@@ -696,7 +696,7 @@ static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int m
}
} else {
uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 :
- top_type ? nnz_excluded * 0x01010101u : 0x40404040;
+ !CABAC(h) && top_type ? nnz_excluded * 0x01010101u : 0x40404040;
AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
@@ -723,7 +723,7 @@ static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int m
}
} else {
int empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 :
- left_type[LEFT(i)] ? nnz_excluded : 64;
+ !CABAC(h) && left_type[LEFT(i)] ? nnz_excluded : 64;
nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
--
2.52.0
From 1fb3d21cf94a303f6d7b36ddd2abd18fe74a3329 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 23:41:34 +0200
Subject: [PATCH 2/2] avcodec/h264dec: Put CABAC and data_partitioing stuff
into union
data_partitioning and CABAC are incompatible, so one can put
data_partitioning stuff into a union with the CABAC context/state.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/h264dec.h | 32 +++++++++++++++++++-------------
1 file changed, 19 insertions(+), 13 deletions(-)
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
index 7bce897021..3b05605423 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -180,14 +180,7 @@ typedef struct H264SliceContext {
GetBitContext gb;
ERContext *er;
- /* Data partitioning: residual comes from gb_dpb (intra) or gb_dpc (inter),
- * chosen per macroblock. Values not pointers: this struct is memcpy'd. */
- GetBitContext gb_dpb;
- GetBitContext gb_dpc;
int data_partitioning;
- int dpb_available;
- int dpc_available;
- unsigned slice_id;
int slice_num;
int slice_type;
@@ -321,12 +314,25 @@ typedef struct H264SliceContext {
uint8_t (*mvd_table[2])[2];
- /**
- * Cabac
- */
- CABACContext cabac;
- uint8_t cabac_state[1024];
- int cabac_init_idc;
+ union {
+ struct {
+ /**
+ * Cabac
+ */
+ CABACContext cabac;
+ uint8_t cabac_state[1024];
+ int cabac_init_idc;
+ };
+ struct {
+ /* Data partitioning: residual comes from gb_dpb (intra) or gb_dpc (inter),
+ * chosen per macroblock. Values not pointers: this struct is memcpy'd. */
+ GetBitContext gb_dpb;
+ GetBitContext gb_dpc;
+ int dpb_available;
+ int dpc_available;
+ unsigned slice_id;
+ };
+ };
MMCO mmco[H264_MAX_MMCO_COUNT];
int nb_mmco;
--
2.52.0
1
0
[PR] forgejo/inject-pr-samples: properly flush and close injected sample before hashing (PR #24339)
by Timo Rothenpieler 31 Aug '26
by Timo Rothenpieler 31 Aug '26
31 Aug '26
PR #24339 opened by Timo Rothenpieler (BtbN)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24339
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24339.patch
Without explicitly flushing (which a close does), the last write, if small enough, might not have hit the disk yet.
So the digest() function would miss reading it, resulting in a wrong hash.
Closing it is generally better, since it not only flushes it, but also closes the handle, avoid a rename of a file with an open handle.
From c0516ee7966f4ecd845a6ca9bc8062f737031a3e Mon Sep 17 00:00:00 2001
From: Timo Rothenpieler <timo(a)rothenpieler.org>
Date: Mon, 31 Aug 2026 19:17:47 +0200
Subject: [PATCH] forgejo/inject-pr-samples: properly flush and close injected
sample before hashing
---
.forgejo/inject-pr-samples.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/.forgejo/inject-pr-samples.py b/.forgejo/inject-pr-samples.py
index 3f50067751..0c328279e0 100755
--- a/.forgejo/inject-pr-samples.py
+++ b/.forgejo/inject-pr-samples.py
@@ -109,6 +109,7 @@ def download(url, dst):
with urllib.request.urlopen(url) as r:
while chunk := r.read(1 << 16):
tmp.write(chunk)
+ tmp.close()
if dst.exists() and digest(dst) != digest(tmp_path):
raise ValueError(f"already exists with different content: {dst}")
tmp_path.rename(dst)
--
2.52.0
1
0
31 Aug '26
PR #24338 opened by AYOUB NABIL BOUBAGRAT (ayoubnabil)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24338
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24338.patch
samples_per_block is used as a divisor when seeking. reject zero during header parsing to prevent a division by zero on malformed BRSTM, BFSTM, and BCSTM files. add a FATE regression test.
From ac528c0cef785024f1b9f575b2f04e6e26556ef3 Mon Sep 17 00:00:00 2001
From: Ayoub Nabil Boubagrat <237098474+ayoubnabil(a)users.noreply.github.com>
Date: Mon, 31 Aug 2026 19:07:19 +0200
Subject: [PATCH] avformat/brstm: reject zero samples per block
Signed-off-by: Ayoub Nabil Boubagrat <237098474+ayoubnabil(a)users.noreply.github.com>
---
libavformat/brstm.c | 2 ++
libavformat/tests/seek.c | 10 ++++++++++
tests/fate/libavformat.mak | 11 +++++++++++
3 files changed, 23 insertions(+)
diff --git a/libavformat/brstm.c b/libavformat/brstm.c
index 3fe19fff72..1a7c3a9100 100644
--- a/libavformat/brstm.c
+++ b/libavformat/brstm.c
@@ -253,6 +253,8 @@ static int read_header(AVFormatContext *s)
return AVERROR_INVALIDDATA;
b->samples_per_block = read32(s);
+ if (!b->samples_per_block)
+ return AVERROR_INVALIDDATA;
b->last_block_used_bytes = read32(s);
b->last_block_samples = read32(s);
b->last_block_size = read32(s);
diff --git a/libavformat/tests/seek.c b/libavformat/tests/seek.c
index 61219e8086..0f2b18330d 100644
--- a/libavformat/tests/seek.c
+++ b/libavformat/tests/seek.c
@@ -67,6 +67,7 @@ int main(int argc, char **argv)
int frame_count = 1;
int duration = 4;
int seekfirst_stream = -1;
+ int expect_open_invalid = 0;
for(i=2; i<argc; i+=2){
if (!strcmp(argv[i], "-seekforw")){
@@ -84,6 +85,8 @@ int main(int argc, char **argv)
if (atoi(argv[i+1])) {
ic->flags |= AVFMT_FLAG_FAST_SEEK;
}
+ } else if(!strcmp(argv[i], "-expect_open_invalid")) {
+ expect_open_invalid = atoi(argv[i+1]);
} else if(argv[i][0] == '-' && argv[i+1]) {
av_dict_set(&format_opts, argv[i] + 1, argv[i+1], 0);
} else {
@@ -105,9 +108,16 @@ int main(int argc, char **argv)
ret = avformat_open_input(&ic, filename, NULL, &format_opts);
av_dict_free(&format_opts);
if (ret < 0) {
+ if (expect_open_invalid && ret == AVERROR_INVALIDDATA)
+ return 0;
fprintf(stderr, "cannot open %s\n", filename);
return 1;
}
+ if (expect_open_invalid) {
+ av_seek_frame(ic, 0, 1, 0);
+ avformat_close_input(&ic);
+ return 1;
+ }
ret = avformat_find_stream_info(ic, NULL);
if (ret < 0) {
diff --git a/tests/fate/libavformat.mak b/tests/fate/libavformat.mak
index f306e19245..962ec28b01 100644
--- a/tests/fate/libavformat.mak
+++ b/tests/fate/libavformat.mak
@@ -65,6 +65,17 @@ fate-seek_utils: libavformat/tests/seek_utils$(EXESUF)
fate-seek_utils: CMD = run libavformat/tests/seek_utils$(EXESUF)
fate-seek_utils: CMP = null
+# 103-byte BRSTM with samples_per_block set to zero
+BRSTM_ZERO_SAMPLES_1 = UlNUTf7/AQAAAABnAA5IRUFEAAAAUAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAQAfQAAA
+BRSTM_ZERO_SAMPLES_2 = AAAAAAAAAAEAAABmAAAAAQAAAAEAAAAAAAAAAQAAAAEAAAABAAAAAERBVEEAAAAJAA==
+BRSTM_DATA_URI = data:application/octet-stream;base64,
+BRSTM_ZERO_SAMPLES_URI = $(BRSTM_DATA_URI)$(BRSTM_ZERO_SAMPLES_1)$(BRSTM_ZERO_SAMPLES_2)
+FATE_LIBAVFORMAT-$(call ALLYES, BRSTM_DEMUXER DATA_PROTOCOL) += fate-brstm-zero-samples-per-block
+fate-brstm-zero-samples-per-block: libavformat/tests/seek$(EXESUF)
+fate-brstm-zero-samples-per-block: CMD = run libavformat/tests/seek$(EXESUF) \
+ "$(BRSTM_ZERO_SAMPLES_URI)" -expect_open_invalid 1
+fate-brstm-zero-samples-per-block: CMP = null
+
FATE_LIBAVFORMAT += $(FATE_LIBAVFORMAT-yes)
FATE-$(CONFIG_AVFORMAT) += $(FATE_LIBAVFORMAT)
fate-libavformat: $(FATE_LIBAVFORMAT)
--
2.52.0
1
0
[PR] fftools/thread_queue: add per-stream FIFO queue accounting and condition vars (PR #24337)
by Niklas Haas 31 Aug '26
by Niklas Haas 31 Aug '26
31 Aug '26
PR #24337 opened by Niklas Haas (haasn)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24337
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24337.patch
ThreadQueue as written was very poorly optimized for the case of multiple streams. Not only was it possible for one stream to just completely fill the FIFO, depriving other streams a chance to write anything at all, but it also had two other inefficiencies:
1. The writers were only woken up when the FIFO was completely empty, leading to a ping/pong behavior; possibly intended as a mitigation for..
2. Any wake on the single shared condition variable immediately woke up every single writer, leading to most of them just going right back to sleep (thundering herd problem)
This series addresses all three, and then dramatically reduces the number of buffered packets - which due to the lack of inter-thread scheduling on filter graph inputs actually translated directly to a number of buffered *frames*. Verified empirically that this allows much lower settings of -filter_buffured_frames (for multi-input, shared demuxer scenarios).
From f94ba2aa48d4a9f37f7e699647cc4ae3e866a064 Mon Sep 17 00:00:00 2001
From: Niklas Haas <git(a)haasn.dev>
Date: Mon, 31 Aug 2026 16:21:57 +0200
Subject: [PATCH 1/5] fftools/ffmpeg_opt: remove stale -thread_queue_size input
option
The rewrite in d119ae2fd82a494d9430ff4d4fc262961a68c598 silently turned
this option into a no-op on the input side. The documentation wording is also
still very much out of date, as FFmpeg is now always threaded.
We could hook this up to the demuxer queue size again, but given that the
caveats no longer apply, and this option is a bit pointless outside of
debugging, I have opted to just remove the corresponding section and mark
it as output-only.
Sponsored-by: nxtedition AB
Signed-off-by: Niklas Haas <git(a)haasn.dev>
---
doc/ffmpeg.texi | 12 +++---------
fftools/ffmpeg_opt.c | 4 ++--
2 files changed, 5 insertions(+), 11 deletions(-)
diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 1cbb1ccbdd..2cb26a3e8f 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -2789,15 +2789,9 @@ to the @option{-ss} option is considered an actual timestamp, and is not
offset by the start time of the file. This matters only for files which do
not start from timestamp 0, such as transport streams.
-@item -thread_queue_size @var{size} (@emph{input/output})
-For input, this option sets the maximum number of queued packets when reading
-from the file or device. With low latency / high rate live streams, packets may
-be discarded if they are not read in a timely manner; setting this value can
-force ffmpeg to use a separate input thread and read packets as soon as they
-arrive. By default ffmpeg only does this if multiple inputs are specified.
-
-For output, this option specified the maximum number of packets that may be
-queued to each muxing thread.
+@item -thread_queue_size @var{size} (@emph{output})
+This option specifies the maximum number of packets that may be queued to each
+muxing thread.
@item -sdp_file @var{file} (@emph{global})
Print sdp information for an output stream to @var{file}.
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index fdbc0fdc13..f93be37558 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1802,9 +1802,9 @@ const OptionDef options[] = {
{ "disposition", OPT_TYPE_STRING, OPT_PERSTREAM | OPT_OUTPUT | OPT_EXPERT,
{ .off = OFFSET(disposition) },
"disposition", "" },
- { "thread_queue_size", OPT_TYPE_INT, OPT_OFFSET | OPT_EXPERT | OPT_INPUT | OPT_OUTPUT,
+ { "thread_queue_size", OPT_TYPE_INT, OPT_OFFSET | OPT_EXPERT | OPT_OUTPUT,
{ .off = OFFSET(thread_queue_size) },
- "set the maximum number of queued packets from the demuxer" },
+ "set the maximum number of queued packets on the muxer" },
{ "find_stream_info", OPT_TYPE_BOOL, OPT_INPUT | OPT_EXPERT | OPT_OFFSET,
{ .off = OFFSET(find_stream_info) },
"read and decode the streams to fill missing information with heuristics" },
--
2.52.0
From 57d98ef0964e7231edc611efecd59c0786aebe74 Mon Sep 17 00:00:00 2001
From: Niklas Haas <git(a)haasn.dev>
Date: Mon, 31 Aug 2026 16:29:04 +0200
Subject: [PATCH 2/5] fftools/thread_queue: apply queue size per stream
This preserves FIFO properties, but allows a queue with multiple streams
(e.g. a filter graph with many inputs) to have each stream contend for only
its own space, rather than fighting with possibly arbitrarily many other
streams for space inside a single 2-element frame queue.
This does slightly change the semantics of the -thread_queue_size option,
but this is already marked as an expert/debug option and was partially
broken for several years, so I consider the risk of downstream annoyance
to be rather minimal. (Especially as the new behavior results in a larger
queue, which only wastes memory, rather than a smaller queue, which may
starve)
Sponsored-by: nxtedition AB
Signed-off-by: Niklas Haas <git(a)haasn.dev>
---
doc/ffmpeg.texi | 4 ++--
fftools/ffmpeg_opt.c | 2 +-
fftools/thread_queue.c | 24 +++++++++++++++++++++---
fftools/thread_queue.h | 2 +-
4 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 2cb26a3e8f..593f4a9968 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -2790,8 +2790,8 @@ offset by the start time of the file. This matters only for files which do
not start from timestamp 0, such as transport streams.
@item -thread_queue_size @var{size} (@emph{output})
-This option specifies the maximum number of packets that may be queued to each
-muxing thread.
+This option specifies the maximum number of packets per stream that may be
+queued to each muxing thread.
@item -sdp_file @var{file} (@emph{global})
Print sdp information for an output stream to @var{file}.
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index f93be37558..1611784ff4 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1804,7 +1804,7 @@ const OptionDef options[] = {
"disposition", "" },
{ "thread_queue_size", OPT_TYPE_INT, OPT_OFFSET | OPT_EXPERT | OPT_OUTPUT,
{ .off = OFFSET(thread_queue_size) },
- "set the maximum number of queued packets on the muxer" },
+ "set the maximum number of queued packets per stream on the muxer" },
{ "find_stream_info", OPT_TYPE_BOOL, OPT_INPUT | OPT_EXPERT | OPT_OFFSET,
{ .off = OFFSET(find_stream_info) },
"read and decode the streams to fill missing information with heuristics" },
diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index 9fa8a46cac..64f06ebfb0 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -41,11 +41,13 @@ struct ThreadQueue {
int choked;
int *finished;
unsigned int nb_streams;
+ size_t queue_size;
enum ThreadQueueType type;
AVContainerFifo *fifo;
AVFifo *fifo_stream_index;
+ size_t *stream_count;
pthread_mutex_t lock;
pthread_cond_t cond;
@@ -60,6 +62,7 @@ void tq_free(ThreadQueue **ptq)
av_container_fifo_free(&tq->fifo);
av_fifo_freep2(&tq->fifo_stream_index);
+ av_freep(&tq->stream_count);
av_freep(&tq->finished);
@@ -96,7 +99,7 @@ ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
if (!tq->finished)
goto fail;
tq->nb_streams = nb_streams;
-
+ tq->queue_size = queue_size;
tq->type = type;
tq->fifo = (type == THREAD_QUEUE_FRAMES) ?
@@ -104,16 +107,29 @@ ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
if (!tq->fifo)
goto fail;
- tq->fifo_stream_index = av_fifo_alloc2(queue_size, sizeof(unsigned), 0);
+ av_assert0(queue_size);
+ if (nb_streams > SIZE_MAX / queue_size)
+ goto fail; // treat like OOM
+
+ tq->fifo_stream_index = av_fifo_alloc2(queue_size * nb_streams, sizeof(unsigned), 0);
if (!tq->fifo_stream_index)
goto fail;
+ tq->stream_count = av_calloc(nb_streams, sizeof(*tq->stream_count));
+ if (!tq->stream_count)
+ goto fail;
+
return tq;
fail:
tq_free(&tq);
return NULL;
}
+static int can_write(ThreadQueue *tq, unsigned int stream_idx)
+{
+ return tq->stream_count[stream_idx] < tq->queue_size;
+}
+
int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
{
int *finished;
@@ -129,7 +145,7 @@ int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
goto finish;
}
- while (!(*finished & FINISHED_RECV) && !av_fifo_can_write(tq->fifo_stream_index))
+ while (!(*finished & FINISHED_RECV) && !can_write(tq, stream_idx))
pthread_cond_wait(&tq->cond, &tq->lock);
if (*finished & FINISHED_RECV) {
@@ -144,6 +160,7 @@ int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
if (ret < 0)
goto finish;
+ tq->stream_count[stream_idx]++;
pthread_cond_broadcast(&tq->cond);
}
@@ -167,6 +184,7 @@ static int receive_locked(ThreadQueue *tq, int *stream_idx,
ret = av_fifo_read(tq->fifo_stream_index, &idx, 1);
av_assert0(ret >= 0);
+ tq->stream_count[idx]--;
if (tq->finished[idx] & FINISHED_RECV) {
(tq->type == THREAD_QUEUE_FRAMES) ?
av_frame_unref(data) : av_packet_unref(data);
diff --git a/fftools/thread_queue.h b/fftools/thread_queue.h
index 756545df44..ba54af0947 100644
--- a/fftools/thread_queue.h
+++ b/fftools/thread_queue.h
@@ -40,7 +40,7 @@ typedef struct ThreadQueue ThreadQueue;
* @param nb_streams number of streams for which a distinct EOF state is
* maintained
* @param queue_size number of items that can be stored in the queue without
- * blocking
+ * blocking (per stream)
*/
ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
enum ThreadQueueType type);
--
2.52.0
From 1f789e86cadb7d32502f2cc099bfd7aec6c064e4 Mon Sep 17 00:00:00 2001
From: Niklas Haas <git(a)haasn.dev>
Date: Mon, 31 Aug 2026 17:26:33 +0200
Subject: [PATCH 3/5] fftools/thread_queue: split apart cond var per index
Instead of multiple producers and consumers all waiting on a single cond
var, resulting in a thundering herd problem, use a separate cond var for
signalling the "queue full" and "queue empty" conditions.
Since the previous commit splits up the universal limit into sublimits per
stream, we can actually go a step further and only wake up the writer whose
stream we just read from.
In practice, this will result in a nice round-robin behavior, without
spending countless context switches waking up idle decoders who will
immediately go back to sleep.
Results in 67x less sys time and 11x fewer context switches in a synthetic
test where 16 streams are feeding into a single filter graph.
Sponsored-by: nxtedition AB
Signed-off-by: Niklas Haas <git(a)haasn.dev>
---
fftools/thread_queue.c | 47 +++++++++++++++++++++++++++---------------
1 file changed, 30 insertions(+), 17 deletions(-)
diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index 64f06ebfb0..ed70ce582a 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -50,7 +50,9 @@ struct ThreadQueue {
size_t *stream_count;
pthread_mutex_t lock;
- pthread_cond_t cond;
+ pthread_cond_t cond_read;
+ pthread_cond_t *cond_write;
+ unsigned int nb_cond_write;
};
void tq_free(ThreadQueue **ptq)
@@ -66,7 +68,11 @@ void tq_free(ThreadQueue **ptq)
av_freep(&tq->finished);
- pthread_cond_destroy(&tq->cond);
+ for (unsigned i = 0; i < tq->nb_cond_write; i++)
+ pthread_cond_destroy(&tq->cond_write[i]);
+ av_freep(&tq->cond_write);
+
+ pthread_cond_destroy(&tq->cond_read);
pthread_mutex_destroy(&tq->lock);
av_freep(ptq);
@@ -82,7 +88,7 @@ ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
if (!tq)
return NULL;
- ret = pthread_cond_init(&tq->cond, NULL);
+ ret = pthread_cond_init(&tq->cond_read, NULL);
if (ret) {
av_freep(&tq);
return NULL;
@@ -90,11 +96,20 @@ ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
ret = pthread_mutex_init(&tq->lock, NULL);
if (ret) {
- pthread_cond_destroy(&tq->cond);
+ pthread_cond_destroy(&tq->cond_read);
av_freep(&tq);
return NULL;
}
+ tq->cond_write = av_calloc(nb_streams, sizeof(*tq->cond_write));
+ if (!tq->cond_write)
+ goto fail;
+ for (tq->nb_cond_write = 0; tq->nb_cond_write < nb_streams; tq->nb_cond_write++) {
+ ret = pthread_cond_init(&tq->cond_write[tq->nb_cond_write], NULL);
+ if (ret)
+ goto fail;
+ }
+
tq->finished = av_calloc(nb_streams, sizeof(*tq->finished));
if (!tq->finished)
goto fail;
@@ -146,7 +161,7 @@ int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
}
while (!(*finished & FINISHED_RECV) && !can_write(tq, stream_idx))
- pthread_cond_wait(&tq->cond, &tq->lock);
+ pthread_cond_wait(&tq->cond_write[stream_idx], &tq->lock);
if (*finished & FINISHED_RECV) {
ret = AVERROR_EOF;
@@ -161,7 +176,7 @@ int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
goto finish;
tq->stream_count[stream_idx]++;
- pthread_cond_broadcast(&tq->cond);
+ pthread_cond_broadcast(&tq->cond_read); // signal downstream
}
finish:
@@ -184,7 +199,11 @@ static int receive_locked(ThreadQueue *tq, int *stream_idx,
ret = av_fifo_read(tq->fifo_stream_index, &idx, 1);
av_assert0(ret >= 0);
- tq->stream_count[idx]--;
+
+ // signal upstream if the fifo is empty
+ if (--tq->stream_count[idx] == 0)
+ pthread_cond_broadcast(&tq->cond_write[idx]);
+
if (tq->finished[idx] & FINISHED_RECV) {
(tq->type == THREAD_QUEUE_FRAMES) ?
av_frame_unref(data) : av_packet_unref(data);
@@ -221,16 +240,10 @@ int tq_receive(ThreadQueue *tq, int *stream_idx, void *data, int flags)
pthread_mutex_lock(&tq->lock);
while (1) {
- size_t can_read = av_container_fifo_can_read(tq->fifo);
-
ret = receive_locked(tq, stream_idx, data);
- // signal other threads if the fifo state changed
- if (can_read != av_container_fifo_can_read(tq->fifo))
- pthread_cond_broadcast(&tq->cond);
-
if (ret == AVERROR(EAGAIN) && !(flags & THREAD_QUEUE_FLAG_NO_BLOCK)) {
- pthread_cond_wait(&tq->cond, &tq->lock);
+ pthread_cond_wait(&tq->cond_read, &tq->lock);
continue;
}
@@ -253,7 +266,7 @@ void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
* an EOF and recv-finished flag will be set */
tq->finished[stream_idx] |= FINISHED_SEND;
tq->choked = 0;
- pthread_cond_broadcast(&tq->cond);
+ pthread_cond_broadcast(&tq->cond_read);
pthread_mutex_unlock(&tq->lock);
}
@@ -268,7 +281,7 @@ void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
* next time the producer thread tries to send for this stream, it will
* get an EOF and send-finished flag will be set */
tq->finished[stream_idx] |= FINISHED_RECV;
- pthread_cond_broadcast(&tq->cond);
+ pthread_cond_broadcast(&tq->cond_write[stream_idx]);
pthread_mutex_unlock(&tq->lock);
}
@@ -280,7 +293,7 @@ void tq_choke(ThreadQueue *tq, int choked)
int prev_choked = tq->choked;
tq->choked = choked;
if (choked != prev_choked)
- pthread_cond_broadcast(&tq->cond);
+ pthread_cond_broadcast(&tq->cond_read);
pthread_mutex_unlock(&tq->lock);
}
--
2.52.0
From 926fe29776561314853c45e426fc94932e8cc6a0 Mon Sep 17 00:00:00 2001
From: Niklas Haas <git(a)haasn.dev>
Date: Mon, 31 Aug 2026 17:41:04 +0200
Subject: [PATCH 4/5] fftools/thread_queue: unblock writers as soon as FIFO is
no longer full
With the thundering herd problem resolved, there's no more reason to
wait until the FIFO is empty before unblocking the writers.
In practice, this means that in a downstream-throttled scenario, we will
always wake up exactly the decoder that we just read from.
Sponsored-by: nxtedition AB
Signed-off-by: Niklas Haas <git(a)haasn.dev>
---
fftools/thread_queue.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index ed70ce582a..af112810c6 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -200,8 +200,8 @@ static int receive_locked(ThreadQueue *tq, int *stream_idx,
ret = av_fifo_read(tq->fifo_stream_index, &idx, 1);
av_assert0(ret >= 0);
- // signal upstream if the fifo is empty
- if (--tq->stream_count[idx] == 0)
+ // signal upstream if the fifo is no longer full
+ if (tq->stream_count[idx]-- == tq->queue_size)
pthread_cond_broadcast(&tq->cond_write[idx]);
if (tq->finished[idx] & FINISHED_RECV) {
--
2.52.0
From 71010824db7ccaa87b6678b4f7e043317f7ceeea Mon Sep 17 00:00:00 2001
From: Niklas Haas <git(a)haasn.dev>
Date: Mon, 31 Aug 2026 18:19:37 +0200
Subject: [PATCH 5/5] fftools/ffmpeg_sched: drop packet thread queue size
The previous series of commits makes this limit apply per-stream, instead
of per-packet, so there's no real reason to have it set to such a high
limit anymore.
Buffering 8 packets is really the same as buffering 8 frames, if the
decoders are faster than the encoder (usually the case) and immediately
drain it into a shared filter graph while that filter graph is stuck
waiting for a different input (from the same demuxer).
I also doubt the methodology of e0da916b8 still holds up; a lot of moving
parts have changed since then. I was not able to reproduce any speed
difference at all acress any number of packets buffered; even reducing this
to 1 gave me no measurable downside. The only scenario where I can measure
a difference is if the fifo size is smaller than the number of streams,
but that is now impossible.
I'm tempted to just lower this all the way to 1, but I kept 2 for the time
being as a hedge against any pathological edge cases.
In the worst case, users can still use -thread_queue_size to increase
this limit for testing / performance tuning.
Sponsored-by: nxtedition AB
Signed-off-by: Niklas Haas <git(a)haasn.dev>
---
fftools/ffmpeg_sched.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fftools/ffmpeg_sched.h b/fftools/ffmpeg_sched.h
index 2cf3034437..952e56e7b5 100644
--- a/fftools/ffmpeg_sched.h
+++ b/fftools/ffmpeg_sched.h
@@ -254,7 +254,7 @@ int sch_add_mux(Scheduler *sch, SchThreadFunc func, int (*init)(void *),
* Default size of a packet thread queue. For muxing this can be overridden by
* the thread_queue_size option as passed to a call to sch_add_mux().
*/
-#define DEFAULT_PACKET_THREAD_QUEUE_SIZE 8
+#define DEFAULT_PACKET_THREAD_QUEUE_SIZE 2
/**
* Default size of a frame thread queue.
--
2.52.0
1
0
PR #24335 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24335
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24335.patch
Also add a checkasm test for compute_mantissa_size and constify the ac3dsp API.
From 871c13a38d610cf47bf9a4b0c28e7d749fc2d711 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 17:22:51 +0200
Subject: [PATCH 1/8] avcodec/ac3dsp: Constify
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/aarch64/ac3dsp_init_aarch64.c | 2 +-
libavcodec/ac3dsp.c | 6 +++---
libavcodec/ac3dsp.h | 6 +++---
libavcodec/arm/ac3dsp_init_arm.c | 4 ++--
libavcodec/mips/ac3dsp_mips.c | 2 +-
libavcodec/riscv/ac3dsp_init.c | 4 ++--
libavcodec/x86/ac3dsp.asm | 4 ++--
libavcodec/x86/ac3dsp_init.c | 6 +++---
tests/checkasm/ac3dsp.c | 2 +-
9 files changed, 18 insertions(+), 18 deletions(-)
diff --git a/libavcodec/aarch64/ac3dsp_init_aarch64.c b/libavcodec/aarch64/ac3dsp_init_aarch64.c
index e367353e11..8edfe911ac 100644
--- a/libavcodec/aarch64/ac3dsp_init_aarch64.c
+++ b/libavcodec/aarch64/ac3dsp_init_aarch64.c
@@ -26,7 +26,7 @@
#include "config.h"
void ff_ac3_exponent_min_neon(uint8_t *exp, int num_reuse_blocks, int nb_coefs);
-void ff_ac3_extract_exponents_neon(uint8_t *exp, int32_t *coef, int nb_coefs);
+void ff_ac3_extract_exponents_neon(uint8_t *exp, const int32_t *coef, int nb_coefs);
void ff_float_to_fixed24_neon(int32_t *dst, const float *src, size_t len);
void ff_ac3_sum_square_butterfly_int32_neon(int64_t sum[4],
const int32_t *coef0,
diff --git a/libavcodec/ac3dsp.c b/libavcodec/ac3dsp.c
index a4a28c8672..090287e42d 100644
--- a/libavcodec/ac3dsp.c
+++ b/libavcodec/ac3dsp.c
@@ -97,7 +97,7 @@ static void ac3_bit_alloc_calc_bap_c(int16_t *mask, int16_t *psd,
} while (end > band_end);
}
-static void ac3_update_bap_counts_c(uint16_t mant_cnt[16], uint8_t *bap,
+static void ac3_update_bap_counts_c(uint16_t mant_cnt[16], const uint8_t bap[],
int len)
{
while (len-- > 0)
@@ -108,7 +108,7 @@ DECLARE_ALIGNED(16, const uint16_t, ff_ac3_bap_bits)[16] = {
0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
};
-static int ac3_compute_mantissa_size_c(uint16_t mant_cnt[6][16])
+static int ac3_compute_mantissa_size_c(const uint16_t mant_cnt[6][16])
{
int blk, bap;
int bits = 0;
@@ -128,7 +128,7 @@ static int ac3_compute_mantissa_size_c(uint16_t mant_cnt[6][16])
return bits;
}
-static void ac3_extract_exponents_c(uint8_t *exp, int32_t *coef, int nb_coefs)
+static void ac3_extract_exponents_c(uint8_t *exp, const int32_t *coef, int nb_coefs)
{
int i;
diff --git a/libavcodec/ac3dsp.h b/libavcodec/ac3dsp.h
index b1b2bced8f..084876cb05 100644
--- a/libavcodec/ac3dsp.h
+++ b/libavcodec/ac3dsp.h
@@ -82,7 +82,7 @@ typedef struct AC3DSPContext {
* @param[in] bap array of bap, pointing to start coef bin
* @param[in] len number of elements to process
*/
- void (*update_bap_counts)(uint16_t mant_cnt[16], uint8_t *bap, int len);
+ void (*update_bap_counts)(uint16_t mant_cnt[16], const uint8_t bap[], int len);
/**
* Calculate the number of bits needed to encode a set of mantissas.
@@ -90,9 +90,9 @@ typedef struct AC3DSPContext {
* @param[in] mant_cnt bap counts for all blocks
* @return mantissa bit count
*/
- int (*compute_mantissa_size)(uint16_t mant_cnt[6][16]);
+ int (*compute_mantissa_size)(const uint16_t mant_cnt[6][16]);
- void (*extract_exponents)(uint8_t *exp, int32_t *coef, int nb_coefs);
+ void (*extract_exponents)(uint8_t *exp, const int32_t *coef, int nb_coefs);
void (*sum_square_butterfly_int32)(int64_t sum[4], const int32_t *coef0,
const int32_t *coef1, int len);
diff --git a/libavcodec/arm/ac3dsp_init_arm.c b/libavcodec/arm/ac3dsp_init_arm.c
index ae989069c9..36e2efb968 100644
--- a/libavcodec/arm/ac3dsp_init_arm.c
+++ b/libavcodec/arm/ac3dsp_init_arm.c
@@ -27,7 +27,7 @@
void ff_ac3_exponent_min_neon(uint8_t *exp, int num_reuse_blocks, int nb_coefs);
void ff_float_to_fixed24_neon(int32_t *dst, const float *src, size_t len);
-void ff_ac3_extract_exponents_neon(uint8_t *exp, int32_t *coef, int nb_coefs);
+void ff_ac3_extract_exponents_neon(uint8_t *exp, const int32_t *coef, int nb_coefs);
void ff_ac3_sum_square_butterfly_int32_neon(int64_t sum[4],
const int32_t *coef0,
const int32_t *coef1,
@@ -42,7 +42,7 @@ void ff_ac3_bit_alloc_calc_bap_armv6(int16_t *mask, int16_t *psd,
int snr_offset, int floor,
const uint8_t *bap_tab, uint8_t *bap);
-void ff_ac3_update_bap_counts_arm(uint16_t mant_cnt[16], uint8_t *bap, int len);
+void ff_ac3_update_bap_counts_arm(uint16_t mant_cnt[16], const uint8_t bap[], int len);
av_cold void ff_ac3dsp_init_arm(AC3DSPContext *c)
{
diff --git a/libavcodec/mips/ac3dsp_mips.c b/libavcodec/mips/ac3dsp_mips.c
index cc49ba3888..5d15468c28 100644
--- a/libavcodec/mips/ac3dsp_mips.c
+++ b/libavcodec/mips/ac3dsp_mips.c
@@ -142,7 +142,7 @@ static void ac3_bit_alloc_calc_bap_mips(int16_t *mask, int16_t *psd,
} while (end > band_end);
}
-static void ac3_update_bap_counts_mips(uint16_t mant_cnt[16], uint8_t *bap,
+static void ac3_update_bap_counts_mips(uint16_t mant_cnt[16], const uint8_t bap[],
int len)
{
void *temp0, *temp2, *temp4, *temp5, *temp6, *temp7;
diff --git a/libavcodec/riscv/ac3dsp_init.c b/libavcodec/riscv/ac3dsp_init.c
index f68a592839..22b1a5f962 100644
--- a/libavcodec/riscv/ac3dsp_init.c
+++ b/libavcodec/riscv/ac3dsp_init.c
@@ -28,8 +28,8 @@
void ff_ac3_exponent_min_rvb(uint8_t *exp, int, int);
void ff_ac3_exponent_min_rvv(uint8_t *exp, int, int);
-void ff_extract_exponents_rvb(uint8_t *exp, int32_t *coef, int nb_coefs);
-void ff_extract_exponents_rvvb(uint8_t *exp, int32_t *coef, int nb_coefs);
+void ff_extract_exponents_rvb(uint8_t *exp, const int32_t *coef, int nb_coefs);
+void ff_extract_exponents_rvvb(uint8_t *exp, const int32_t *coef, int nb_coefs);
void ff_float_to_fixed24_rvv(int32_t *dst, const float *src, size_t len);
void ff_sum_square_butterfly_int32_rvv(int64_t *, const int32_t *,
const int32_t *, int);
diff --git a/libavcodec/x86/ac3dsp.asm b/libavcodec/x86/ac3dsp.asm
index 5b16af99c5..5d07c5f04c 100644
--- a/libavcodec/x86/ac3dsp.asm
+++ b/libavcodec/x86/ac3dsp.asm
@@ -153,7 +153,7 @@ cglobal float_to_fixed24, 3, 3, 5, dst, src, len
RET
;------------------------------------------------------------------------------
-; int ff_ac3_compute_mantissa_size(uint16_t mant_cnt[6][16])
+; int ff_ac3_compute_mantissa_size(const uint16_t mant_cnt[6][16])
;------------------------------------------------------------------------------
%macro PHADDD4 2 ; xmm src, xmm tmp
@@ -201,7 +201,7 @@ cglobal ac3_compute_mantissa_size, 1, 2, 4, mant_cnt, sum
RET
;------------------------------------------------------------------------------
-; void ff_ac3_extract_exponents(uint8_t *exp, int32_t *coef, int nb_coefs)
+; void ff_ac3_extract_exponents(uint8_t *exp, const int32_t *coef, int nb_coefs)
;------------------------------------------------------------------------------
%macro PABSD 1-2 ; src/dst, unused
diff --git a/libavcodec/x86/ac3dsp_init.c b/libavcodec/x86/ac3dsp_init.c
index b8d25f72a5..5e14cefcff 100644
--- a/libavcodec/x86/ac3dsp_init.c
+++ b/libavcodec/x86/ac3dsp_init.c
@@ -28,10 +28,10 @@ void ff_ac3_exponent_min_sse2 (uint8_t *exp, int num_reuse_blocks, int nb_coefs
void ff_float_to_fixed24_sse2 (int32_t *dst, const float *src, size_t len);
void ff_float_to_fixed24_avx (int32_t *dst, const float *src, size_t len);
-int ff_ac3_compute_mantissa_size_sse2(uint16_t mant_cnt[6][16]);
+int ff_ac3_compute_mantissa_size_sse2(const uint16_t mant_cnt[6][16]);
-void ff_ac3_extract_exponents_sse2 (uint8_t *exp, int32_t *coef, int nb_coefs);
-void ff_ac3_extract_exponents_ssse3(uint8_t *exp, int32_t *coef, int nb_coefs);
+void ff_ac3_extract_exponents_sse2 (uint8_t *exp, const int32_t *coef, int nb_coefs);
+void ff_ac3_extract_exponents_ssse3(uint8_t *exp, const int32_t *coef, int nb_coefs);
av_cold void ff_ac3dsp_init_x86(AC3DSPContext *c)
{
diff --git a/tests/checkasm/ac3dsp.c b/tests/checkasm/ac3dsp.c
index fa2fe17276..f49a5b2281 100644
--- a/tests/checkasm/ac3dsp.c
+++ b/tests/checkasm/ac3dsp.c
@@ -95,7 +95,7 @@ static void check_ac3_extract_exponents(AC3DSPContext *c) {
LOCAL_ALIGNED_16(uint8_t, v2, [MAX_EXPS]);
int n;
- declare_func(void, uint8_t *, int32_t *, int);
+ declare_func(void, uint8_t *, const int32_t *, int);
for (n = 512; n <= MAX_EXPS; n += 256) {
if (check_func(c->extract_exponents, "ac3_extract_exponents_n%d", n)) {
--
2.52.0
From 8f2dc4a623d1ddc5da84abe9b46227b7fad174d2 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 17:25:12 +0200
Subject: [PATCH 2/8] tests/checkasm/ac3dsp: Add test for compute_mantissa_size
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
tests/checkasm/ac3dsp.c | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/tests/checkasm/ac3dsp.c b/tests/checkasm/ac3dsp.c
index f49a5b2281..75a2b1d7ac 100644
--- a/tests/checkasm/ac3dsp.c
+++ b/tests/checkasm/ac3dsp.c
@@ -22,9 +22,9 @@
#include <stdint.h>
#include <string.h>
-#include "libavutil/mem.h"
#include "libavutil/mem_internal.h"
+#include "libavcodec/ac3defs.h"
#include "libavcodec/ac3dsp.h"
#include "checkasm.h"
@@ -139,6 +139,29 @@ static void check_float_to_fixed24(AC3DSPContext *c) {
report("float_to_fixed24");
}
+static void check_compute_mantissa_size(AC3DSPContext *const c)
+{
+ declare_func(int, const uint16_t mant_cnt[6][16]);
+
+ if (!check_func(c->compute_mantissa_size, "compute_mantissa_size"))
+ return;
+
+ DECLARE_ALIGNED_16(uint16_t, mant_cnt)[AC3_MAX_BLOCKS][16];
+
+ // The maximum of a single value is 3*60 (max_bandwith_code) + 73 + 2
+ checkasm_randomize_mask16((uint16_t*)mant_cnt, AC3_MAX_BLOCKS*16, 0xFF);
+
+ int size_ref = call_ref(mant_cnt);
+ int size_new = call_new(mant_cnt);
+
+ if (size_ref != size_new)
+ fail();
+
+ bench_new(mant_cnt);
+
+ report("compute_mantissa_size");
+}
+
static void check_ac3_sum_square_butterfly_int32(AC3DSPContext *c) {
#define ELEMS 240
LOCAL_ALIGNED_16(int32_t, lt, [ELEMS]);
@@ -198,6 +221,7 @@ void checkasm_check_ac3dsp(void)
check_ac3_exponent_min(&c);
check_ac3_extract_exponents(&c);
check_float_to_fixed24(&c);
+ check_compute_mantissa_size(&c);
check_ac3_sum_square_butterfly_int32(&c);
check_ac3_sum_square_butterfly_float(&c);
}
--
2.52.0
From 724cb2233960029f9e491298776482c31302f5e9 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 17:35:38 +0200
Subject: [PATCH 3/8] avcodec/x86/ac3dsp: Avoid unnecessary horizontal addition
Old benchmarks:
compute_mantissa_size_c: 112.1
compute_mantissa_size_sse2: 6.5 (17.29x)
New benchmarks:
compute_mantissa_size_c: 111.6
compute_mantissa_size_sse2: 5.5 (20.35x)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/x86/ac3dsp.asm | 28 +++++++++++++---------------
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/libavcodec/x86/ac3dsp.asm b/libavcodec/x86/ac3dsp.asm
index 5d07c5f04c..51874bc5f5 100644
--- a/libavcodec/x86/ac3dsp.asm
+++ b/libavcodec/x86/ac3dsp.asm
@@ -164,40 +164,38 @@ cglobal float_to_fixed24, 3, 3, 5, dst, src, len
%endmacro
INIT_XMM sse2
-cglobal ac3_compute_mantissa_size, 1, 2, 4, mant_cnt, sum
+cglobal ac3_compute_mantissa_size, 1, 1, 5, mant_cnt
movdqa m0, [mant_cntq ]
movdqa m1, [mant_cntq+ 1*16]
+ movdqa m4, [pw_bap_mul1]
paddw m0, [mant_cntq+ 2*16]
paddw m1, [mant_cntq+ 3*16]
+ movhpd m2, [mant_cntq +2]
paddw m0, [mant_cntq+ 4*16]
paddw m1, [mant_cntq+ 5*16]
+ movlpd m2, [mant_cntq+1*32+2]
paddw m0, [mant_cntq+ 6*16]
paddw m1, [mant_cntq+ 7*16]
paddw m0, [mant_cntq+ 8*16]
+ pmulhuw m2, m4
paddw m1, [mant_cntq+ 9*16]
paddw m0, [mant_cntq+10*16]
paddw m1, [mant_cntq+11*16]
pmaddwd m0, [ac3_bap_bits ]
pmaddwd m1, [ac3_bap_bits+16]
paddd m0, m1
- PHADDD4 m0, m1
- movd sumd, m0
- movdqa m3, [pw_bap_mul1]
- movhpd m0, [mant_cntq +2]
- movlpd m0, [mant_cntq+1*32+2]
movhpd m1, [mant_cntq+2*32+2]
movlpd m1, [mant_cntq+3*32+2]
- movhpd m2, [mant_cntq+4*32+2]
- movlpd m2, [mant_cntq+5*32+2]
- pmulhuw m0, m3
- pmulhuw m1, m3
- pmulhuw m2, m3
- paddusw m0, m1
- paddusw m0, m2
- pmaddwd m0, [pw_bap_mul2]
+ movhpd m3, [mant_cntq+4*32+2]
+ movlpd m3, [mant_cntq+5*32+2]
+ pmulhuw m1, m4
+ pmulhuw m3, m4
+ paddusw m1, m2
+ paddusw m1, m3
+ pmaddwd m1, [pw_bap_mul2]
+ paddd m0, m1
PHADDD4 m0, m1
movd eax, m0
- add eax, sumd
RET
;------------------------------------------------------------------------------
--
2.52.0
From 25d8f08529265c2ba436f5f5917ed23b9f848c1d Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 17:46:32 +0200
Subject: [PATCH 4/8] avcodec/x86/ac3dsp: Use movq instead of movlpd
Avoids a dependency on dst.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/x86/ac3dsp.asm | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/libavcodec/x86/ac3dsp.asm b/libavcodec/x86/ac3dsp.asm
index 51874bc5f5..9acf595131 100644
--- a/libavcodec/x86/ac3dsp.asm
+++ b/libavcodec/x86/ac3dsp.asm
@@ -170,10 +170,10 @@ cglobal ac3_compute_mantissa_size, 1, 1, 5, mant_cnt
movdqa m4, [pw_bap_mul1]
paddw m0, [mant_cntq+ 2*16]
paddw m1, [mant_cntq+ 3*16]
- movhpd m2, [mant_cntq +2]
+ movq m2, [mant_cntq +2]
paddw m0, [mant_cntq+ 4*16]
paddw m1, [mant_cntq+ 5*16]
- movlpd m2, [mant_cntq+1*32+2]
+ movhpd m2, [mant_cntq+1*32+2]
paddw m0, [mant_cntq+ 6*16]
paddw m1, [mant_cntq+ 7*16]
paddw m0, [mant_cntq+ 8*16]
@@ -184,10 +184,10 @@ cglobal ac3_compute_mantissa_size, 1, 1, 5, mant_cnt
pmaddwd m0, [ac3_bap_bits ]
pmaddwd m1, [ac3_bap_bits+16]
paddd m0, m1
- movhpd m1, [mant_cntq+2*32+2]
- movlpd m1, [mant_cntq+3*32+2]
- movhpd m3, [mant_cntq+4*32+2]
- movlpd m3, [mant_cntq+5*32+2]
+ movq m1, [mant_cntq+2*32+2]
+ movhpd m1, [mant_cntq+3*32+2]
+ movq m3, [mant_cntq+4*32+2]
+ movhpd m3, [mant_cntq+5*32+2]
pmulhuw m1, m4
pmulhuw m3, m4
paddusw m1, m2
--
2.52.0
From 1697c6868f0a5c5a3d4dc13e2ab5d9018235aa8c Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 17:48:12 +0200
Subject: [PATCH 5/8] avcodec/x86/ac3dsp: Don't use saturated addition
unnecessarily
These numbers always fit into 16bit.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/x86/ac3dsp.asm | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libavcodec/x86/ac3dsp.asm b/libavcodec/x86/ac3dsp.asm
index 9acf595131..90ead9814b 100644
--- a/libavcodec/x86/ac3dsp.asm
+++ b/libavcodec/x86/ac3dsp.asm
@@ -190,8 +190,8 @@ cglobal ac3_compute_mantissa_size, 1, 1, 5, mant_cnt
movhpd m3, [mant_cntq+5*32+2]
pmulhuw m1, m4
pmulhuw m3, m4
- paddusw m1, m2
- paddusw m1, m3
+ paddw m1, m2
+ paddw m1, m3
pmaddwd m1, [pw_bap_mul2]
paddd m0, m1
PHADDD4 m0, m1
--
2.52.0
From 2bbebeb58f13793000ff45ac9ce97d7f470fef9d Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 17:50:13 +0200
Subject: [PATCH 6/8] avcodec/x86/ac3dsp: Don't duplicate HADDD
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/x86/ac3dsp.asm | 9 +--------
1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/libavcodec/x86/ac3dsp.asm b/libavcodec/x86/ac3dsp.asm
index 90ead9814b..deb2a71a26 100644
--- a/libavcodec/x86/ac3dsp.asm
+++ b/libavcodec/x86/ac3dsp.asm
@@ -156,13 +156,6 @@ cglobal float_to_fixed24, 3, 3, 5, dst, src, len
; int ff_ac3_compute_mantissa_size(const uint16_t mant_cnt[6][16])
;------------------------------------------------------------------------------
-%macro PHADDD4 2 ; xmm src, xmm tmp
- movhlps %2, %1
- paddd %1, %2
- pshufd %2, %1, 0x1
- paddd %1, %2
-%endmacro
-
INIT_XMM sse2
cglobal ac3_compute_mantissa_size, 1, 1, 5, mant_cnt
movdqa m0, [mant_cntq ]
@@ -194,7 +187,7 @@ cglobal ac3_compute_mantissa_size, 1, 1, 5, mant_cnt
paddw m1, m3
pmaddwd m1, [pw_bap_mul2]
paddd m0, m1
- PHADDD4 m0, m1
+ HADDD m0, m1
movd eax, m0
RET
--
2.52.0
From 0a75042d2abe91ef1ee568118b399c9b50da99f6 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 17:58:01 +0200
Subject: [PATCH 7/8] avcodec/x86/ac3dsp: Use memory src operand when possible
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/x86/ac3dsp.asm | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/libavcodec/x86/ac3dsp.asm b/libavcodec/x86/ac3dsp.asm
index deb2a71a26..9822f35465 100644
--- a/libavcodec/x86/ac3dsp.asm
+++ b/libavcodec/x86/ac3dsp.asm
@@ -215,10 +215,14 @@ cglobal ac3_extract_exponents, 3, 3, 4, exp, coef, len
mova m2, [pd_1]
mova m3, [pd_151]
.loop:
+%if cpuflag(ssse3)
+ pabsd m0, [coefq+4*lenq]
+%else
; move 4 32-bit coefs to xmm0
mova m0, [coefq+4*lenq]
; absolute value
PABSD m0, m1
+%endif
; convert to float and extract exponents
pslld m0, 1
por m0, m2
--
2.52.0
From 3453390682c98db251f05a2d5756d17b1887ea93 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
Date: Mon, 31 Aug 2026 18:05:27 +0200
Subject: [PATCH 8/8] avcodec/x86/ac3dsp: Avoid reg-reg mov when forcing VEX
encoding
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt(a)outlook.com>
---
libavcodec/x86/ac3dsp.asm | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/libavcodec/x86/ac3dsp.asm b/libavcodec/x86/ac3dsp.asm
index 9822f35465..702ea1579d 100644
--- a/libavcodec/x86/ac3dsp.asm
+++ b/libavcodec/x86/ac3dsp.asm
@@ -228,8 +228,7 @@ cglobal ac3_extract_exponents, 3, 3, 4, exp, coef, len
por m0, m2
cvtdq2ps m1, m0
psrld m1, 23
- mova m0, m3
- psubd m0, m1
+ psubd m0, m3, m1
; move the lowest byte in each of 4 dwords to the low dword
; NOTE: We cannot just extract the low bytes with pshufb because the dword
; result for 16777215 is -1 due to float inaccuracy. Using packuswb
--
2.52.0
1
0