FFmpeg: libavfilter/vf_decimate.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2012 Fredrik Mellbin
3 * Copyright (c) 2013 Clément Bœsch
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
27
29 #define INPUT_CLEANSRC 1
30
35 };
36
39 struct qitem *
queue;
///< window of cycle frames and the associated data diff
40 int fid;
///< current frame id in the queue
41 int filled;
///< 1 if the queue is filled, 0 otherwise
44 int got_frame[2];
///< frame request flag for each input stream
48 uint32_t
eof;
///< bitmask for end of stream
57
58 /* options */
69
70 #define OFFSET(x) offsetof(DecimateContext, x)
71 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
72
74 {
"cycle",
"set the number of frame from which one will be dropped",
OFFSET(cycle),
AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25,
FLAGS },
77 {
"blockx",
"set the size of the x-axis blocks used during metric calculations",
OFFSET(blockx),
AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9,
FLAGS },
78 {
"blocky",
"set the size of the y-axis blocks used during metric calculations",
OFFSET(blocky),
AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9,
FLAGS },
79 {
"ppsrc",
"mark main input as a pre-processed input and activate clean source input stream",
OFFSET(ppsrc),
AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,
FLAGS },
81 {
"mixed",
"set whether or not the input only partially contains content to be decimated",
OFFSET(mixed),
AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,
FLAGS },
83 };
84
86
89 {
93
94 memset(bdiffs, 0, dm->
bdiffsize *
sizeof(*bdiffs));
95
96 for (plane = 0; plane < (dm->
chroma && f1->
data[2] ? 3 : 1); plane++) {
97 int x, y, xl;
98 const int linesize1 = f1->
linesize[plane];
99 const int linesize2 = f2->
linesize[plane];
100 const uint8_t *f1p = f1->
data[plane];
101 const uint8_t *f2p = f2->
data[plane];
104 int hblockx = dm->
blockx / 2;
105 int hblocky = dm->
blocky / 2;
106
107 if (plane) {
108 hblockx >>= dm->
hsub;
109 hblocky >>= dm->
vsub;
110 }
111
112 for (y = 0; y <
height; y++) {
113 int ydest = y / hblocky;
114 int xdest = 0;
115
116 #define CALC_DIFF(nbits) do { \
117 for (x = 0; x < width; x += hblockx) { \
118 int64_t acc = 0; \
119 int m = FFMIN(width, x + hblockx); \
120 for (xl = x; xl < m; xl++) \
121 acc += abs(((const uint##nbits##_t *)f1p)[xl] - \
122 ((const uint##nbits##_t *)f2p)[xl]); \
123 bdiffs[ydest * dm->nxblocks + xdest] += acc; \
124 xdest++; \
125 } \
126 } while (0)
129
130 f1p += linesize1;
131 f2p += linesize2;
132 }
133 }
134
136 for (j = 0; j < dm->
nxblocks - 1; j++) {
140 + bdiffs[(
i + 1) * dm->
nxblocks + j + 1];
143 }
144 }
145
150 }
151
153 {
154 int scpos = -1, duppos = -1;
155 int drop = INT_MIN,
i, lowest = 0,
ret;
160
161 /* update frames queue(s) */
165 } else {
168 }
170 return 0;
172
175
176 if (in) {
177 /* update frame metrics */
179 if (!prv) {
182 } else {
184 }
186 return 0;
190
191 /* we have a complete cycle, select the frame to drop */
192 lowest = 0;
198 }
200 duppos = lowest;
201
202 if (dm->
mixed && duppos < 0) {
203 drop = -1; // no drop if mixed content + no frame in cycle below threshold
204 } else {
205 drop = scpos >= 0 && duppos < 0 ? scpos : lowest;
206 }
207 }
208
209 /* metrics debug */
215 i == scpos ?
" sc" :
"",
216 i == duppos ?
" dup" :
"",
217 i == lowest ?
" lowest" :
"",
218 i == drop ?
" [DROP]" :
"");
219 }
220 }
221
222 /* push all frames except the drop */
229 } else {
234
239 continue;
241 }
242
250 break;
251 }
252 }
253
255 }
256
258 {
263
265
271 }
280 }
285 return 0;
293 return 0;
294 }
301 return 0;
302 }
303 }
304
313 }
314 return 0;
315 }
316
318 {
323 };
325
328
330 pad.
name =
"clean_src";
334 }
335
340 }
341
344
345 return 0;
346 }
347
349 {
352
358 }
363 }
365 }
366
368 #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
369 #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
370 #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
379 };
380
382 {
389 int max_value;
393
397 max_value = (1 << dm->
depth) - 1;
408
411
416 }
417
418 if (!fps.
num || !fps.
den) {
420 "current rate of %d/%d is invalid\n", fps.
num, fps.
den);
422 }
423
428 } else {
433 }
438 } else {
441 }
442 return 0;
443 }
444
446 {
450 },
451 };
452
454 .
p.
name =
"decimate",
456 .p.priv_class = &decimate_class,
464 };
AVPixelFormat
Pixel format.
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int64_t start_pts
base for output timestamps
#define FILTER_PIXFMTS_ARRAY(array)
struct qitem * queue
window of cycle frames and the associated data diff
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AVERROR_EOF
End of file.
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
static enum AVPixelFormat pix_fmts[]
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
int depth
Number of bits in the component.
#define AV_LOG_VERBOSE
Detailed information.
int vsub
chroma subsampling values
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
int got_frame[2]
frame request flag for each input stream
const char * name
Filter name.
A link between two filters.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Link properties exposed to filter code, but not external callers.
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
int ff_append_inpad(AVFilterContext *f, AVFilterPad *p)
Append a new input/output pad to the filter's list of such pads.
#define AV_PIX_FMT_GRAY16
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
static int activate(AVFilterContext *ctx)
static void calc_diffs(const DecimateContext *dm, struct qitem *q, const AVFrame *f1, const AVFrame *f2)
A filter pad used for either input or output.
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
#define AV_CEIL_RSHIFT(a, b)
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
const FFFilter ff_vf_decimate
static const AVOption decimate_options[]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_PIX_FMT_GRAY14
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
#define FILTER_OUTPUTS(array)
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
int64_t last_pts
last output timestamp
#define AV_PIX_FMT_GRAY10
int av_log_get_level(void)
Get the current log level.
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
#define AV_PIX_FMT_YUV440P10
uint32_t eof
bitmask for end of stream
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
static FilterLink * ff_filter_link(AVFilterLink *link)
int(* config_props)(AVFilterLink *link)
Link configuration callback.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define AV_NOPTS_VALUE
Undefined timestamp value.
AVFilterContext * src
source filter
static av_cold void decimate_uninit(AVFilterContext *ctx)
AVFrame ** clean_src
frame queue for the clean source
#define i(width, name, range_min, range_max)
#define FF_INLINK_IDX(link)
Find the index of a link.
AVRational av_gcd_q(AVRational a, AVRational b, int max_den, AVRational def)
Return the best rational so that a and b are multiple of it.
int w
agreed upon image width
#define AV_TIME_BASE
Internal time base represented as integer.
#define av_malloc_array(a, b)
static av_cold int decimate_init(AVFilterContext *ctx)
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
int h
agreed upon image height
@ AV_OPT_TYPE_INT
Underlying C type is int.
AVFILTER_DEFINE_CLASS(decimate)
int fid
current frame id in the queue
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
static int config_output(AVFilterLink *outlink)
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
AVFilter p
The public AVFilter.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
#define AV_PIX_FMT_YUV440P12
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable.
#define AV_PIX_FMT_GRAY12
int filled
1 if the queue is filled, 0 otherwise
AVFrame * last
last frame from the previous queue
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
static const AVFilterPad decimate_outputs[]
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
int64_t last_duration
last output duration
Generated on Tue Nov 18 2025 19:23:10 for FFmpeg by
doxygen
1.8.17