FFmpeg
Loading...
Searching...
No Matches
vf_yadif.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3 * 2010 James Darnley <james.darnley@gmail.com>
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
22#include "libavutil/common.h"
23#include "libavutil/pixdesc.h"
24#include "avfilter.h"
25#include "filters.h"
26#include "yadif.h"
27
28typedef struct ThreadData {
29 AVFrame *frame;
30 int plane;
31 int w, h;
32 int parity;
33 int tff;
35
36#define CHECK(j)\
37 { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
38 + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
39 + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
40 if (score < spatial_score) {\
41 spatial_score= score;\
42 spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
43
44/* The is_not_edge argument here controls when the code will enter a branch
45 * which reads up to and including x-3 and x+3. */
46
47#define FILTER(start, end, is_not_edge) \
48 for (x = start; x < end; x++) { \
49 int c = cur[mrefs]; \
50 int d = (prev2[0] + next2[0])>>1; \
51 int e = cur[prefs]; \
52 int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
53 int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
54 int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
55 int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
56 int spatial_pred = (c+e) >> 1; \
57 \
58 if (is_not_edge) {\
59 int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
60 + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
61 CHECK(-1) CHECK(-2) }} }} \
62 CHECK( 1) CHECK( 2) }} }} \
63 }\
64 \
65 if (!(mode&2)) { \
66 int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
67 int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
68 int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
69 int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
70 \
71 diff = FFMAX3(diff, min, -max); \
72 } \
73 \
74 if (spatial_pred > d + diff) \
75 spatial_pred = d + diff; \
76 else if (spatial_pred < d - diff) \
77 spatial_pred = d - diff; \
78 \
79 dst[0] = spatial_pred; \
80 \
81 dst++; \
82 cur++; \
83 prev++; \
84 next++; \
85 prev2++; \
86 next2++; \
87 }
88
89static void filter_line_c(void *dst1,
90 void *prev1, void *cur1, void *next1,
91 int w, int prefs, int mrefs, int parity, int mode)
92{
93 uint8_t *dst = dst1;
94 uint8_t *prev = prev1;
95 uint8_t *cur = cur1;
96 uint8_t *next = next1;
97 int x;
98 uint8_t *prev2 = parity ? prev : cur ;
99 uint8_t *next2 = parity ? cur : next;
100
101 /* The function is called with the pointers already pointing to data[3] and
102 * with 6 subtracted from the width. This allows the FILTER macro to be
103 * called so that it processes all the pixels normally. A constant value of
104 * true for is_not_edge lets the compiler ignore the if statement. */
105 FILTER(0, w, 1)
106}
107
108#define MAX_ALIGN 8
109static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
110 int w, int prefs, int mrefs, int parity, int mode)
111{
112 uint8_t *dst = dst1;
113 uint8_t *prev = prev1;
114 uint8_t *cur = cur1;
115 uint8_t *next = next1;
116 int x;
117 uint8_t *prev2 = parity ? prev : cur ;
118 uint8_t *next2 = parity ? cur : next;
119
120 const int edge = MAX_ALIGN - 1;
121 int offset = FFMAX(w - edge, 3);
122
123 /* Only edge pixels need to be processed here. A constant value of false
124 * for is_not_edge should let the compiler ignore the whole branch. */
125 FILTER(0, FFMIN(3, w), 0)
126
127 dst = (uint8_t*)dst1 + offset;
128 prev = (uint8_t*)prev1 + offset;
129 cur = (uint8_t*)cur1 + offset;
130 next = (uint8_t*)next1 + offset;
131 prev2 = (uint8_t*)(parity ? prev : cur);
132 next2 = (uint8_t*)(parity ? cur : next);
133
134 FILTER(offset, w - 3, 1)
135 offset = FFMAX(offset, w - 3);
136 FILTER(offset, w, 0)
137}
138
139
140static void filter_line_c_16bit(void *dst1,
141 void *prev1, void *cur1, void *next1,
142 int w, int prefs, int mrefs, int parity,
143 int mode)
144{
145 uint16_t *dst = dst1;
146 uint16_t *prev = prev1;
147 uint16_t *cur = cur1;
148 uint16_t *next = next1;
149 int x;
150 uint16_t *prev2 = parity ? prev : cur ;
151 uint16_t *next2 = parity ? cur : next;
152 mrefs /= 2;
153 prefs /= 2;
154
155 FILTER(0, w, 1)
156}
157
158static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
159 int w, int prefs, int mrefs, int parity, int mode)
160{
161 uint16_t *dst = dst1;
162 uint16_t *prev = prev1;
163 uint16_t *cur = cur1;
164 uint16_t *next = next1;
165 int x;
166 uint16_t *prev2 = parity ? prev : cur ;
167 uint16_t *next2 = parity ? cur : next;
168
169 const int edge = MAX_ALIGN / 2 - 1;
170 int offset = FFMAX(w - edge, 3);
171
172 mrefs /= 2;
173 prefs /= 2;
174
175 FILTER(0, FFMIN(3, w), 0)
176
177 dst = (uint16_t*)dst1 + offset;
178 prev = (uint16_t*)prev1 + offset;
179 cur = (uint16_t*)cur1 + offset;
180 next = (uint16_t*)next1 + offset;
181 prev2 = (uint16_t*)(parity ? prev : cur);
182 next2 = (uint16_t*)(parity ? cur : next);
183
184 FILTER(offset, w - 3, 1)
185 offset = FFMAX(offset, w - 3);
186 FILTER(offset, w, 0)
187}
188
189static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
190{
191 YADIFContext *s = ctx->priv;
192 ThreadData *td = arg;
193 int refs = s->cur->linesize[td->plane];
194 int df = (s->csp->comp[td->plane].depth + 7) / 8;
195 int pix_3 = 3 * df;
196 int slice_start = ff_slice_pos(td->h, jobnr, nb_jobs);
197 int slice_end = ff_slice_pos(td->h, jobnr + 1, nb_jobs);
198 int y;
199 int edge = 3 + MAX_ALIGN / df - 1;
200
201 /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
202 * we need to call the c variant which avoids this for border pixels
203 */
204 for (y = slice_start; y < slice_end; y++) {
205 if ((y ^ td->parity) & 1) {
206 uint8_t *prev = &s->prev->data[td->plane][y * refs];
207 uint8_t *cur = &s->cur ->data[td->plane][y * refs];
208 uint8_t *next = &s->next->data[td->plane][y * refs];
209 uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
210 int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
211 s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
212 next + pix_3, td->w - edge,
213 y + 1 < td->h ? refs : -refs,
214 y ? -refs : refs,
215 td->parity ^ td->tff, mode);
216 s->filter_edges(dst, prev, cur, next, td->w,
217 y + 1 < td->h ? refs : -refs,
218 y ? -refs : refs,
219 td->parity ^ td->tff, mode);
220 } else {
221 memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
222 &s->cur->data[td->plane][y * refs], td->w * df);
223 }
224 }
225 return 0;
226}
227
228static void filter(AVFilterContext *ctx, AVFrame *dstpic,
229 int parity, int tff)
230{
231 YADIFContext *yadif = ctx->priv;
232 ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
233 int i;
234
235 for (i = 0; i < yadif->csp->nb_components; i++) {
236 int w = dstpic->width;
237 int h = dstpic->height;
238
239 if (i == 1 || i == 2) {
240 w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
241 h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
242 }
243
244
245 td.w = w;
246 td.h = h;
247 td.plane = i;
248
251 }
252}
253
271
272static int config_output(AVFilterLink *outlink)
273{
274 AVFilterContext *ctx = outlink->src;
275 YADIFContext *s = ctx->priv;
276 int ret;
277
278 ret = ff_yadif_config_output_common(outlink);
279 if (ret < 0)
280 return ret;
281
282 s->csp = av_pix_fmt_desc_get(outlink->format);
283 s->filter = filter;
284 if (s->csp->comp[0].depth > 8) {
285 s->filter_line = filter_line_c_16bit;
286 s->filter_edges = filter_edges_16bit;
287 } else {
288 s->filter_line = filter_line_c;
289 s->filter_edges = filter_edges;
290 }
291
292#if ARCH_X86 && HAVE_X86ASM
294#endif
295
296 return 0;
297}
298
299
300static const AVClass yadif_class = {
301 .class_name = "yadif",
302 .item_name = av_default_item_name,
303 .option = ff_yadif_options,
304 .version = LIBAVUTIL_VERSION_INT,
305 .category = AV_CLASS_CATEGORY_FILTER,
306};
307
309 {
310 .name = "default",
311 .type = AVMEDIA_TYPE_VIDEO,
312 .filter_frame = ff_yadif_filter_frame,
313 },
314};
315
317 {
318 .name = "default",
319 .type = AVMEDIA_TYPE_VIDEO,
320 .request_frame = ff_yadif_request_frame,
321 .config_props = config_output,
322 },
323};
324
326 .p.name = "yadif",
327 .p.description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
328 .p.priv_class = &yadif_class,
330 .priv_size = sizeof(YADIFContext),
335};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFFilter ff_vf_yadif
Definition vf_yadif.c:325
static AVFormatContext * ctx
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
static void filter_line_c_16bit(void *dst1, const void *prev1, const void *cur1, const void *next1, int w, int prefs, int mrefs, int prefs2, int mrefs2, int prefs3, int mrefs3, int prefs4, int mrefs4, int parity, int clip_max)
Definition bwdifdsp.c:173
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
unsigned offset
Definition libaomenc.c:763
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
uint8_t w
Definition llvidencdsp.c:39
@ AV_CLASS_CATEGORY_FILTER
Definition log.h:36
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition pixdesc.h:71
Used for passing data between threads.
Definition dsddec.c:71
int parity
Definition vf_bwdif.c:49
AVFrame * frame
Definition dsddec.c:72
int plane
Definition vf_blend.c:61
const AVPixFmtDescriptor * csp
Definition yadif.h:76
Definition swscale.c:71
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
mcdeint parity
Definition vf_mcdeint.c:293
#define df(A, B)
Definition vf_xbr.c:91
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_yadif.c:189
static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1, int w, int prefs, int mrefs, int parity, int mode)
Definition vf_yadif.c:158
#define MAX_ALIGN
Definition vf_yadif.c:108
static void filter_line_c_16bit(void *dst1, void *prev1, void *cur1, void *next1, int w, int prefs, int mrefs, int parity, int mode)
Definition vf_yadif.c:140
static void filter_line_c(void *dst1, void *prev1, void *cur1, void *next1, int w, int prefs, int mrefs, int parity, int mode)
Definition vf_yadif.c:89
#define FILTER(start, end, is_not_edge)
Definition vf_yadif.c:47
static const AVFilterPad avfilter_vf_yadif_outputs[]
Definition vf_yadif.c:316
static int config_output(AVFilterLink *outlink)
Definition vf_yadif.c:272
static const AVFilterPad avfilter_vf_yadif_inputs[]
Definition vf_yadif.c:308
static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1, int w, int prefs, int mrefs, int parity, int mode)
Definition vf_yadif.c:109
static const AVClass yadif_class
Definition vf_yadif.c:300
av_cold void ff_yadif_init_x86(YADIFContext *yadif)
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844
int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
int ff_yadif_request_frame(AVFilterLink *link)
const AVOption ff_yadif_options[]
void ff_yadif_uninit(AVFilterContext *ctx)
int ff_yadif_config_output_common(AVFilterLink *outlink)