FFmpeg: libavfilter/vf_identity.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Calculate the Identity between two input videos.
24 */
25
26 #include "config_components.h"
27
38
56 int jobnr, int nb_jobs);
59
60 #define OFFSET(x) offsetof(IdentityContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62
64 {
65 unsigned score = 0;
66
67 for (int j = 0; j < outw; j++)
68 score += main_line[j] == ref_line[j];
69
70 return score;
71 }
72
74 {
75 const uint16_t *main_line = (const uint16_t *)mmain_line;
76 const uint16_t *ref_line = (const uint16_t *)rref_line;
77 unsigned score = 0;
78
79 for (int j = 0; j < outw; j++)
80 score += main_line[j] == ref_line[j];
81
82 return score;
83 }
84
95
96 static
98 int jobnr, int nb_jobs)
99 {
102 uint64_t *score =
td->score[jobnr];
103
104 for (
int c = 0;
c <
td->nb_components;
c++) {
105 const int outw =
td->planewidth[
c];
106 const int outh =
td->planeheight[
c];
107 const int slice_start = (outh * jobnr) / nb_jobs;
108 const int slice_end = (outh * (jobnr+1)) / nb_jobs;
109 const int ref_linesize =
td->ref_linesize[
c];
110 const int main_linesize =
td->main_linesize[
c];
111 const uint8_t *main_line =
td->main_data[
c] + main_linesize * slice_start;
112 const uint8_t *ref_line =
td->ref_data[
c] + ref_linesize * slice_start;
113 uint64_t m = 0;
114
115 s->sad(main_line, main_linesize, ref_line, ref_linesize,
117
119 }
120
121 return 0;
122 }
123
124 static
126 int jobnr, int nb_jobs)
127 {
130 uint64_t *score =
td->score[jobnr];
131
132 for (
int c = 0;
c <
td->nb_components;
c++) {
133 const int outw =
td->planewidth[
c];
134 const int outh =
td->planeheight[
c];
135 const int slice_start = (outh * jobnr) / nb_jobs;
136 const int slice_end = (outh * (jobnr+1)) / nb_jobs;
137 const int ref_linesize =
td->ref_linesize[
c];
138 const int main_linesize =
td->main_linesize[
c];
139 const uint8_t *main_line =
td->main_data[
c] + main_linesize * slice_start;
140 const uint8_t *ref_line =
td->ref_data[
c] + ref_linesize * slice_start;
141 uint64_t m = 0;
142
144 m +=
s->filter_line(main_line, ref_line, outw);
145 ref_line += ref_linesize;
146 main_line += main_linesize;
147 }
149 }
150
151 return 0;
152 }
153
156 {
160 char key2[128];
161 snprintf(key2,
sizeof(key2),
"lavfi.%s.%s%s%c",
164 } else {
165 char key2[128];
166 snprintf(key2,
sizeof(key2),
"lavfi.%s.%s%s",
167 ctx->filter->name,
ctx->filter->name,
key);
169 }
170 }
171
173 {
177 double comp_score[4], score = 0.;
178 uint64_t comp_sum[4] = { 0 };
182
186 if (
ctx->is_disabled || !
ref)
188 metadata = &
master->metadata;
189
190 td.nb_components =
s->nb_components;
191 td.score =
s->scores;
192 for (
int c = 0;
c <
s->nb_components;
c++) {
196 td.ref_linesize[
c] =
ref->linesize[
c];
197 td.planewidth[
c] =
s->planewidth[
c];
198 td.planeheight[
c] =
s->planeheight[
c];
199 }
200
202 FFMIN(
s->planeheight[1],
s->nb_threads));
203
204 for (
int j = 0; j <
s->nb_threads; j++) {
205 for (
int c = 0;
c <
s->nb_components;
c++)
206 comp_sum[
c] +=
s->scores[j][
c];
207 }
208
209 for (
int c = 0;
c <
s->nb_components;
c++)
210 comp_score[
c] = comp_sum[
c] / ((
double)
s->planewidth[
c] *
s->planeheight[
c]);
211
212 for (
int c = 0;
c <
s->nb_components &&
s->is_msad;
c++)
213 comp_score[
c] /= (
double)
s->max[
c];
214
215 for (
int c = 0;
c <
s->nb_components;
c++)
216 score += comp_score[
c];
217 score /=
s->nb_components;
218
219 s->min_score =
FFMIN(
s->min_score, score);
220 s->max_score =
FFMAX(
s->max_score, score);
221
223
224 for (
int j = 0; j <
s->nb_components; j++)
225 s->score_comp[j] += comp_score[j];
227
228 for (
int j = 0; j <
s->nb_components; j++) {
229 int c =
s->is_rgb ?
s->rgba_map[j] : j;
231 }
233
235 }
236
238 {
240
242
243 return 0;
244 }
245
248 #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
249 #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
250 #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
259 };
260
262 {
266
268 s->nb_components =
desc->nb_components;
269 if (
ctx->inputs[0]->w !=
ctx->inputs[1]->w ||
270 ctx->inputs[0]->h !=
ctx->inputs[1]->h) {
273 }
274
276 s->comps[0] =
s->is_rgb ?
'R' :
'Y' ;
277 s->comps[1] =
s->is_rgb ?
'G' :
'U' ;
278 s->comps[2] =
s->is_rgb ?
'B' :
'V' ;
280
282 s->planeheight[0] =
s->planeheight[3] =
inlink->h;
284 s->planewidth[0] =
s->planewidth[3] =
inlink->w;
285
286 s->scores =
av_calloc(
s->nb_threads,
sizeof(*
s->scores));
289
290 for (
int t = 0; t <
s->nb_threads; t++) {
291 s->scores[t] =
av_calloc(
s->nb_components,
sizeof(*
s->scores[0]));
294 }
295
298
299 s->max[0] = (1 <<
desc->comp[0].depth) - 1;
300 s->max[1] = (1 <<
desc->comp[1].depth) - 1;
301 s->max[2] = (1 <<
desc->comp[2].depth) - 1;
302 s->max[3] = (1 <<
desc->comp[3].depth) - 1;
303
304 s->is_msad = !strcmp(
ctx->filter->name,
"msad");
307
311
312 return 0;
313 }
314
316 {
321
325 outlink->
w = mainlink->
w;
326 outlink->
h = mainlink->
h;
332
334
337 av_log(
ctx,
AV_LOG_WARNING,
"not matching timebases found between first input: %d/%d and second input %d/%d, results may be incorrect!\n",
339 ctx->inputs[1]->time_base.num,
ctx->inputs[1]->time_base.den);
340
341 return 0;
342 }
343
345 {
348 }
349
351 {
353
354 if (
s->nb_frames > 0) {
355 char buf[256];
356
357 buf[0] = 0;
358 for (
int j = 0; j <
s->nb_components; j++) {
359 int c =
s->is_rgb ?
s->rgba_map[j] : j;
360 av_strlcatf(buf,
sizeof(buf),
" %c:%f",
s->comps[j],
s->score_comp[
c] /
s->nb_frames);
361 }
362
365 buf,
366 s->score /
s->nb_frames,
369 }
370
372 for (
int t = 0; t <
s->nb_threads &&
s->scores; t++)
375 }
376
378 {
381 },{
382 .name = "reference",
385 },
386 };
387
389 {
393 },
394 };
395
398 };
399
400 #if CONFIG_IDENTITY_FILTER
401
402 #define identity_options options
404
408 .preinit = identity_framesync_preinit,
413 .priv_class = &identity_class,
420 };
421
422 #endif /* CONFIG_IDENTITY_FILTER */
423
424 #if CONFIG_MSAD_FILTER
425
426 #define msad_options options
428
432 .preinit = msad_framesync_preinit,
437 .priv_class = &msad_class,
444 };
445
446 #endif /* CONFIG_MSAD_FILTER */
#define AV_PIX_FMT_GBRAP16
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel format.
const AVFilter ff_vf_identity
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
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
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 FILTER_PIXFMTS_ARRAY(array)
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
static int config_input_ref(AVFilterLink *inlink)
This structure describes decoded (raw) audio or video data.
static int do_identity(FFFrameSync *fs)
#define FRAMESYNC_DEFINE_CLASS(name, context, field)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
const char * name
Filter name.
A link between two filters.
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
#define AV_PIX_FMT_GBRP14
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
static void set_meta(AVFilterContext *ctx, AVDictionary **metadata, const char *key, char comp, float d)
unsigned(* filter_line)(const uint8_t *buf, const uint8_t *ref, int w)
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GRAY16
A filter pad used for either input or output.
static unsigned identity_line_16bit(const uint8_t *mmain_line, const uint8_t *rref_line, int outw)
@ 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...
#define AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP12
#define AV_CEIL_RSHIFT(a, b)
static const AVFilterPad identity_outputs[]
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable; if left to 0/0,...
#define AV_PIX_FMT_GRAY14
#define FILTER_INPUTS(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...
#define AV_PIX_FMT_GRAY10
ff_scene_sad_fn ff_scene_sad_get_fn(int depth)
#define AV_PIX_FMT_GBRP16
Describe the class of an AVClass context structure.
int(* filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define fs(width, name, subs,...)
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static int compute_images_identity(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
static enum AVPixelFormat pix_fmts[]
static av_cold void uninit(AVFilterContext *ctx)
const AVFilter ff_vf_msad
void(* ff_scene_sad_fn)(SCENE_SAD_PARAMS)
AVFilterContext * src
source filter
#define AV_LOG_INFO
Standard information.
static unsigned identity_line_8bit(const uint8_t *main_line, const uint8_t *ref_line, int outw)
#define i(width, name, range_min, range_max)
static const AVOption options[]
int w
agreed upon image width
#define AV_PIX_FMT_GBRP12
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Used for passing data between threads.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
static int activate(AVFilterContext *ctx)
static av_cold int init(AVFilterContext *ctx)
int h
agreed upon image height
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
static int ref[MAX_W *MAX_W]
static const AVFilterPad identity_inputs[]
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
#define FILTER_OUTPUTS(array)
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
static int compute_images_msad(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
#define AV_PIX_FMT_GRAY12
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
const uint8_t * ref_data[4]
static int config_output(AVFilterLink *outlink)
const uint8_t * main_data[4]
Generated on Tue Feb 28 2023 21:33:56 for FFmpeg by
doxygen
1.8.17