Commit Graph

55 Commits

Author SHA1 Message Date
Andreas Rheinhardt 790f793844 avutil/common: Don't auto-include mem.h
There are lots of files that don't need it: The number of object
files that actually need it went down from 2011 to 884 here.

Keep it for external users in order to not cause breakages.

Also improve the other headers a bit while just at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-03-31 00:08:43 +01:00
Anton Khirnov 1e7d2007c3 all: use designated initializers for AVOption.unit
Makes it robust against adding fields before it, which will be useful in
following commits.

Majority of the patch generated by the following Coccinelle script:

@@
typedef AVOption;
identifier arr_name;
initializer list il;
initializer list[8] il1;
expression tail;
@@
AVOption arr_name[] = { il, { il1,
- tail
+ .unit = tail
}, ...  };

with some manual changes, as the script:
* has trouble with options defined inside macros
* sometimes does not handle options under an #else branch
* sometimes swallows whitespace
2024-02-14 14:53:41 +01:00
Andreas Rheinhardt 19af142d45 avfilter/internal: Don't include formats.h
internal.h doesn't rely on it; instead include it directly
in every user that needs it (a filter needing it is basically
equivalent to it using FILTER_QUERY_FUNC, i.e. a majority of
filters doesn't need it).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-07 09:21:13 +02:00
Andreas Rheinhardt 6d15643173 avfilter/internal: Don't include video.h
internal.h does not depend on video.h (and should not depend on it)
and therefore should not include video.h at all; instead all users
of video.h should include it directly.

Doing so also avoids unnecessary video.h inclusions in files that
don't need it, like most audio filters.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-07 09:21:13 +02:00
Clément Bœsch 11926c4bd4 avfilter/palette{gen,use}: misc for-loop cosmetics 2023-01-03 17:18:56 +01:00
Clément Bœsch c9084cd0e2 avfilter/palettegen: use lowbias32 for color hashing
1.12x faster overall in palettegen on my machine.
2023-01-03 17:18:55 +01:00
Clément Bœsch 051926868c avfilter/palette{gen,use}: update Copyright after recent changes 2023-01-03 17:18:55 +01:00
Clément Bœsch 3a4a6c7815 avfilter/palettegen: use libc qsort
Now that the sort function is deterministic, we can rely on the libc
sorting function.
2023-01-03 17:18:55 +01:00
Clément Bœsch 8057e1a618 avfilter/palettegen: make refs order deterministic
Currently, in case of equality on the first color channel, the order of
the ref colors is defined by the hashing function. This commit makes the
sorting deterministic and improve the hierarchical ordering.
2023-01-03 17:18:55 +01:00
Clément Bœsch 2441e53c0a avfilter/palettegen: add a warning about supporting only sRGB 2023-01-03 17:18:55 +01:00
Clément Bœsch 9026d92c13 avfilter/palettegen: base box split decision on a perceptual model
Similar to the change in paletteuse, we rely on a perceptual model to
decide how and where to split the box.
2023-01-03 17:18:55 +01:00
Clément Bœsch 724f52b906 avfilter/palettegen: switch to signed arithmetic
This prevents mixed sign arithmetic (typically because we have signed
color channel differences), which has nasty side effects in C.
2023-01-03 17:18:55 +01:00
Clément Bœsch 67ce6ad9a2 avfilter/palettegen: rename local variable box_weight to weight
This variable is used only for the running weight (used to reach the
target median). The places where we actually need the box weight are
changed to use box->weight.
2023-01-03 17:18:55 +01:00
Clément Bœsch 61d334d8c3 avfilter/palettegen: misc cosmetics 2023-01-03 17:18:55 +01:00
Clément Bœsch efb0a6f6ce avfilter/palettegen: compute average color within compute_box_stats() 2023-01-03 17:18:55 +01:00
Clément Bœsch dafd43b78d avfilter/palettegen: change cut score from ∑e² to max e²
This is following the results from personal research¹.

¹: https://github.com/ubitux/research/tree/main/color-quantization#results
2023-01-03 17:18:55 +01:00
Clément Bœsch 187f5e7f90 avfilter/palettegen: rename variance to cut_score
"Variance" wasn't exactly the correct word; "cut score" is more
agnostic, which will be useful when changing the algorithm in the next
commit.
2023-01-03 17:18:55 +01:00
Clément Bœsch e49fc1a6ef avfilter/palettegen: always compute the box variance
The variance computation is simple enough now (since we can use the axis
squared errors) that it doesn't need to have a complex lazy computation
logic.
2023-01-03 17:18:55 +01:00
Clément Bœsch 9e5f494d26 avfilter/palettegen: use box->major_axis without intermediate variable 2023-01-03 17:18:55 +01:00
Clément Bœsch 86bced7c65 avfilter/palettegen: define the best axis to cut using the squared error
This is following the results from personal research¹.

¹: https://github.com/ubitux/research/tree/main/color-quantization#results
2023-01-03 17:18:55 +01:00
Clément Bœsch 21f7814238 avfilter/palettegen: move box stats computation to a dedicated function 2023-01-03 17:18:55 +01:00
Clément Bœsch 320fbf5f24 avfilter/palette{gen,use}: revert support palettes with alpha
This reverts commit dea673d0d5.

This change cannot work for several reasons, the most obvious ones are:

- the alpha is being part of the scoring of the color difference, even
  though we can not interpret the alpha as part of the perception of the
  color (we don't even know if it's premultiplied or postmultiplied)
- the colors are averaged with their alpha value which simply cannot
  work

The command proposed in the original thread of the patch actually
produces a completely broken file:

    ffmpeg -y -loglevel verbose -i fate-suite/apng/o_sample.png -filter_complex "split[split1][split2];[split1]palettegen=max_colors=254:use_alpha=1[pal1];[split2][pal1]paletteuse=use_alpha=1" -frames:v 1 out.png

We can see that many color pixels are off, but more importantly some
colors have a random alpha value: https://imgur.com/eFQ2UK7

I don't see any easy fix for this unfortunately, the approach appears to
be flawed by design.
2023-01-03 17:18:55 +01:00
Clément Bœsch cad9d7fc85 avfilter/palettegen: allow a minimum of 2 colors 2023-01-03 17:18:55 +01:00
Yiyuan GUO 2201c91fbf avfilter/vf_palettegen: add protection against potential divide by zero
In libavfilter/vf_palettegen.c, the function get_avg_color requires
that box->len greater than zero to avoid dividing by zero. However,
the call sequence filter_frame -> get_palette_frame -> get_avg_color
may not satisfy this precondition. Fixes #9222.

Signed-off-by: Yiyuan GUO <yguoaz@gmail.com>
2022-03-14 17:55:00 +01:00
Soft Works 73fe19f09c avfilter/vf_palettegen: cosmetic changes
Signed-off-by: softworkz <softworkz@hotmail.com>
2021-10-13 18:52:14 +02:00
Soft Works dea673d0d5 avfilter/vf_palette(gen|use): support palettes with alpha 2021-10-13 18:52:14 +02:00
Andreas Rheinhardt b4f5201967 avfilter: Replace query_formats callback with union of list and callback
If one looks at the many query_formats callbacks in existence,
one will immediately recognize that there is one type of default
callback for video and a slightly different default callback for
audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);"
for video with a filter-specific pix_fmts list. For audio, it is
the same with a filter-specific sample_fmts list together with
ff_set_common_all_samplerates() and ff_set_common_all_channel_counts().

This commit allows to remove the boilerplate query_formats callbacks
by replacing said callback with a union consisting the old callback
and pointers for pixel and sample format arrays. For the not uncommon
case in which these lists only contain a single entry (besides the
sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also
added to the union to store them directly in the AVFilter,
thereby avoiding a relocation.

The state of said union will be contained in a new, dedicated AVFilter
field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t
in order to create a hole for this new field; this is no problem, as
the maximum of all the nb_inputs is four; for nb_outputs it is only
two).

The state's default value coincides with the earlier default of
query_formats being unset, namely that the filter accepts all formats
(and also sample rates and channel counts/layouts for audio)
provided that these properties agree coincide for all inputs and
outputs.

By using different union members for audio and video filters
the type-unsafety of using the same functions for audio and video
lists will furthermore be more confined to formats.c than before.

When the new fields are used, they will also avoid allocations:
Currently something nearly equivalent to ff_default_query_formats()
is called after every successful call to a query_formats callback;
yet in the common case that the newly allocated AVFilterFormats
are not used at all (namely if there are no free links) these newly
allocated AVFilterFormats are freed again without ever being used.
Filters no longer using the callback will not exhibit this any more.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-10-05 17:48:25 +02:00
Andreas Rheinhardt 8be701d9f7 avfilter/avfilter: Add numbers of (in|out)pads directly to AVFilter
Up until now, an AVFilter's lists of input and output AVFilterPads
were terminated by a sentinel and the only way to get the length
of these lists was by using avfilter_pad_count(). This has two
drawbacks: first, sizeof(AVFilterPad) is not negligible
(i.e. 64B on 64bit systems); second, getting the size involves
a function call instead of just reading the data.

This commit therefore changes this. The sentinels are removed and new
private fields nb_inputs and nb_outputs are added to AVFilter that
contain the number of elements of the respective AVFilterPad array.

Given that AVFilter.(in|out)puts are the only arrays of zero-terminated
AVFilterPads an API user has access to (AVFilterContext.(in|out)put_pads
are not zero-terminated and they already have a size field) the argument
to avfilter_pad_count() is always one of these lists, so it just has to
find the filter the list belongs to and read said number. This is slower
than before, but a replacement function that just reads the internal numbers
that users are expected to switch to will be added soon; and furthermore,
avfilter_pad_count() is probably never called in hot loops anyway.

This saves about 49KiB from the binary; notice that these sentinels are
not in .bss despite being zeroed: they are in .data.rel.ro due to the
non-sentinels.

Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-08-20 12:53:58 +02:00
Paul B Mahol 124eec942a avfilter/vf_palettegen: fix integer overflow 2021-08-19 01:29:11 +02:00
Andreas Rheinhardt a04ad248a0 avfilter: Constify all AVFilters
This is possible now that the next-API is gone.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 11:48:05 -03:00
Nicolas George 2f76476549 lavfi: regroup formats lists in a single structure.
It will allow to refernce it as a whole without clunky macros.

Most of the changes have been automatically made with sed:

sed -i '
  s/-> *in_formats/->incfg.formats/g;
  s/-> *out_formats/->outcfg.formats/g;
  s/-> *in_channel_layouts/->incfg.channel_layouts/g;
  s/-> *out_channel_layouts/->outcfg.channel_layouts/g;
  s/-> *in_samplerates/->incfg.samplerates/g;
  s/-> *out_samplerates/->outcfg.samplerates/g;
  ' src/libavfilter/*(.)
2020-09-08 14:02:40 +02:00
Paul B Mahol d64cbd4fda remove CHAR_MIN/CHAR_MAX usage
It is not needed at all.
2020-03-17 22:46:36 +01:00
Tomas Härdin abc5ac3cf5 palettegen: Fill with last color, not black
If we fill with black then the generated palette will have one color more
than what the user requested. This also resulted in unwanted black specks in
the output of paletteuse, especially when generating small palettes.
2019-02-19 21:29:03 +01:00
Carl Eugen Hoyos 8b43039fe1 lavfi/palettegen: Fix compilation after 1da8c4e.
Found-by: James Almer
2017-10-29 02:35:36 +02:00
Carl Eugen Hoyos 1da8c4ec85 lavfi/palettegen: Allow setting the background colour. 2017-10-29 01:45:43 +02:00
Paul B Mahol ed93ed5ee3 avfilter: don't anonymously typedef structs
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2017-05-13 11:39:28 +02:00
Clément Bœsch 549045254c Fix all -Wformat warnings raised by DJGPP 2017-03-29 14:49:29 +02:00
Michael Niedermayer e740e9c798 avfilter/vf_palettegen: Fix leak and simplify code
Fixes CID1270818

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-01-21 22:40:14 +01:00
Paul B Mahol 93ae68d62a avfilter/vf_palettegen: add mode for generating palette for each input frame
Signed-off-by: Paul B Mahol <onemda@gmail.com>
2016-09-07 15:56:13 +02:00
Ganesh Ajjanagadde 92e483f8ed all: use FFDIFFSIGN to resolve possible undefined behavior in comparators
FFDIFFSIGN was created explicitly for this purpose, since the common
return a - b idiom is unsafe regarding overflow on signed integers. It
optimizes to branchless code on common compilers.

FFDIFFSIGN also has the subjective benefit of being easier to read due
to lack of ternary operators.

Tested with FATE.

Things not covered by this are unsigned integers, for which overflows
are well defined, and also places where overflow is clearly impossible,
e.g an instance where the a - b was being done on 24 bit values.

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Reviewed-by: Clément Bœsch <u@pkh.me>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>
2015-11-03 16:28:30 -05:00
Ganesh Ajjanagadde 6aaac24d72 avfilter/all: propagate errors of functions from avfilter/formats
Many of the functions from avfilter/formats can return errors, usually AVERROR(ENOMEM).
This propagates the return values.

All of these were found by using av_warn_unused_result, demonstrating its utility.

Tested with FATE. I am least sure of the changes to avfilter/filtergraph,
since I don't know what/how reduce_format is intended to behave and how it should
react to errors.

Fixes: CID 1325680, 1325679, 1325678.

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Previous version Reviewed-by: Nicolas George <george@nsup.org>
Previous version Reviewed-by: Clément Bœsch <u@pkh.me>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>
2015-10-14 10:04:01 -04:00
Nicolas George 44f660e7e7 lavfi: remove FF_LINK_FLAG_REQUEST_LOOP.
It has no longer any effect.
2015-09-20 19:02:33 +02:00
Clément Bœsch 81e52c6df9 avfilter/palettegen: use AV_OPT_TYPE_BOOL for reserve_transparent option 2015-09-09 00:34:56 +02:00
Ronald S. Bultje 229843aa35 Replace av_dlog with ff_dlog.
ff_dlog checks compilability, and is non-public. av_dlog is deprecated
and no longer exists if FF_API_DLOG=0.
2015-08-18 10:24:01 -04:00
Clément Bœsch 84da9339c2 avfilter/palettegen: make sure at least one frame was sent to the filter
Fix FPE.
2015-03-15 19:09:20 +01:00
Clément Bœsch 3e0ae19f86 avfilter/palettegen: fix frame mem leak 2015-02-27 14:18:53 +01:00
Clément Bœsch 321de03492 avfilter/palettegen: use AV_QSORT()
This makes the sorting of the colors along an axis (r, g or b)
predictible, and thus testable under FATE. The performance is not really
an issue here since the function is called only once at the end and will
need to sort very small number of entries, so an alternative would be to
make the sorting functions (see DECLARE_CMP_FUNC()) fallback on another
axis in case of equality. This approach was actually simpler.

I don't know if there is any advantage in using a multidimensional sort,
but it will affect the final palette one way or another.
2015-02-26 14:20:02 +01:00
Clément Bœsch 30f4e9d3d9 avfilter/palettegen: consistently use the same frame for dimensions 2015-02-26 14:20:02 +01:00
Clément Bœsch 4216968f85 avfilter/palettegen: use a logging context for the dupped color warning 2015-02-26 14:20:02 +01:00
Clément Bœsch ab093bdeef avfilter/palettegen: export color quantization ratio 2015-02-26 14:20:02 +01:00