FFmpeg
Loading...
Searching...
No Matches
vf_bwdif.c
Go to the documentation of this file.
1/*
2 * BobWeaver Deinterlacing Filter
3 * Copyright (C) 2016 Thomas Mundt <loudmax@yahoo.de>
4 *
5 * Based on YADIF (Yet Another Deinterlacing Filter)
6 * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
7 * 2010 James Darnley <james.darnley@gmail.com>
8 *
9 * With use of Weston 3 Field Deinterlacing Filter algorithm
10 * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
11 * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
12 * Based on the process described by Martin Weston for BBC R&D
13 *
14 * This file is part of FFmpeg.
15 *
16 * FFmpeg is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * FFmpeg is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with FFmpeg; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31#include "libavutil/common.h"
32#include "libavutil/opt.h"
33#include "libavutil/pixdesc.h"
34#include "avfilter.h"
35#include "bwdifdsp.h"
36#include "ccfifo.h"
37#include "filters.h"
38#include "yadif.h"
39
44
45typedef struct ThreadData {
47 int plane;
48 int w, h;
49 int parity;
50 int tff;
52
53// Round job start line down to multiple of 4 so that if filter_line3 exists
54// and the frame is a multiple of 4 high then filter_line will never be called
55static inline int job_start(const int jobnr, const int nb_jobs, const int h)
56{
57 return jobnr >= nb_jobs ? h : (ff_slice_pos(h, jobnr, nb_jobs)) & ~3;
58}
59
60static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
61{
62 BWDIFContext *s = ctx->priv;
63 YADIFContext *yadif = &s->yadif;
64 ThreadData *td = arg;
65 int linesize = yadif->cur->linesize[td->plane];
66 int clip_max = (1 << (yadif->csp->comp[td->plane].depth)) - 1;
67 int df = (yadif->csp->comp[td->plane].depth + 7) / 8;
68 int refs = linesize / df;
69 int slice_start = job_start(jobnr, nb_jobs, td->h);
70 int slice_end = job_start(jobnr + 1, nb_jobs, td->h);
71 int y;
72
73 for (y = slice_start; y < slice_end; y++) {
74 if ((y ^ td->parity) & 1) {
75 uint8_t *prev = &yadif->prev->data[td->plane][y * linesize];
76 uint8_t *cur = &yadif->cur ->data[td->plane][y * linesize];
77 uint8_t *next = &yadif->next->data[td->plane][y * linesize];
78 uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
79 if (yadif->current_field == YADIF_FIELD_END) {
80 s->dsp.filter_intra(dst, cur, td->w, (y + 1) < td->h ? refs : -refs,
81 y > 0 ? -refs : refs,
82 (y + 3) < td->h ? 3 * refs : -refs,
83 y > 2 ? -3 * refs : refs,
84 td->parity ^ td->tff, clip_max);
85 } else if ((y < 4) || ((y + 5) > td->h)) {
86 s->dsp.filter_edge(dst, prev, cur, next, td->w,
87 (y + 1) < td->h ? refs : -refs,
88 y > 0 ? -refs : refs,
89 refs << 1, -(refs << 1),
90 td->parity ^ td->tff, clip_max,
91 (y < 2) || ((y + 3) > td->h) ? 0 : 1);
92 } else if (s->dsp.filter_line3 && y + 2 < slice_end && y + 6 < td->h) {
93 s->dsp.filter_line3(dst, td->frame->linesize[td->plane],
94 prev, cur, next, linesize, td->w,
95 td->parity ^ td->tff, clip_max);
96 y += 2;
97 } else {
98 s->dsp.filter_line(dst, prev, cur, next, td->w,
99 refs, -refs, refs << 1, -(refs << 1),
100 3 * refs, -3 * refs, refs << 2, -(refs << 2),
101 td->parity ^ td->tff, clip_max);
102 }
103 } else {
104 memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
105 &yadif->cur->data[td->plane][y * linesize], td->w * df);
106 }
107 }
108 return 0;
109}
110
111static void filter(AVFilterContext *ctx, AVFrame *dstpic,
112 int parity, int tff)
113{
114 BWDIFContext *bwdif = ctx->priv;
115 YADIFContext *yadif = &bwdif->yadif;
116 ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
117 int i;
118
119 for (i = 0; i < yadif->csp->nb_components; i++) {
120 int w = dstpic->width;
121 int h = dstpic->height;
122
123 if (i == 1 || i == 2) {
124 w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
125 h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
126 }
127
128 td.w = w;
129 td.h = h;
130 td.plane = i;
131
134 }
135 if (yadif->current_field == YADIF_FIELD_END) {
137 }
138}
139
160
161static int config_props(AVFilterLink *link)
162{
163 AVFilterContext *ctx = link->src;
164 BWDIFContext *s = link->src->priv;
165 YADIFContext *yadif = &s->yadif;
166 int ret;
167
169 if (ret < 0)
170 return AVERROR(EINVAL);
171
172 yadif->csp = av_pix_fmt_desc_get(link->format);
173 yadif->filter = filter;
174
175 if (AV_CEIL_RSHIFT(link->w, yadif->csp->log2_chroma_w) < 3 || AV_CEIL_RSHIFT(link->h, yadif->csp->log2_chroma_h) < 4) {
176 av_log(ctx, AV_LOG_ERROR, "Video with planes less than 3 columns or 4 lines is not supported\n");
177 return AVERROR(EINVAL);
178 }
179
180 ff_bwdif_init_filter_line(&s->dsp, yadif->csp->comp[0].depth);
181
182 return 0;
183}
184
185
186#define OFFSET(x) offsetof(YADIFContext, x)
187#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
188
189#define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u }
190
191static const AVOption bwdif_options[] = {
192 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FIELD}, 0, 1, FLAGS, .unit = "mode"},
193 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
194 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
195
196 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" },
197 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
198 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
199 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
200
201 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" },
202 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
203 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
204
205 { NULL }
206};
207
209
211 {
212 .name = "default",
213 .type = AVMEDIA_TYPE_VIDEO,
214 .filter_frame = ff_yadif_filter_frame,
215 },
216};
217
219 {
220 .name = "default",
221 .type = AVMEDIA_TYPE_VIDEO,
222 .request_frame = ff_yadif_request_frame,
223 .config_props = config_props,
224 },
225};
226
228 .p.name = "bwdif",
229 .p.description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
230 .p.priv_class = &bwdif_class,
232 .priv_size = sizeof(BWDIFContext),
237};
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_bwdif
Definition vf_bwdif.c:227
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.
av_cold void ff_bwdif_init_filter_line(BWDIFDSPContext *s, int bit_depth)
Definition bwdifdsp.c:208
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
CC FIFO Buffer.
#define FLAGS
Definition cmdutils.c:598
common internal and external API header
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static AVFrame * frame
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#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
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
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 AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_bwdif.c:60
static int job_start(const int jobnr, const int nb_jobs, const int h)
Definition vf_bwdif.c:55
static const AVFilterPad avfilter_vf_bwdif_outputs[]
Definition vf_bwdif.c:218
#define CONST(name, help, val, u)
Definition vf_bwdif.c:189
static const AVOption bwdif_options[]
Definition vf_bwdif.c:191
static const AVFilterPad avfilter_vf_bwdif_inputs[]
Definition vf_bwdif.c:210
#define OFFSET(x)
Definition vf_bwdif.c:186
static int config_props(AVFilterLink *link)
Definition vf_bwdif.c:161
#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
#define FFMIN(a, b)
Definition macros.h:49
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
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_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#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_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#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_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ 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_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#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
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
int depth
Number of bits in the component.
Definition pixdesc.h:57
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
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
AVOption.
Definition opt.h:428
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
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
YADIFContext yadif
Definition vf_bwdif.c:41
BWDIFDSPContext dsp
Definition vf_bwdif.c:42
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
AVFrame * prev
Definition yadif.h:62
AVFrame * cur
Definition yadif.h:60
AVFrame * next
Definition yadif.h:61
void(* filter)(AVFilterContext *ctx, AVFrame *dstpic, int parity, int tff)
Definition yadif.h:65
int current_field
YADIFCurrentField.
Definition yadif.h:88
const AVPixFmtDescriptor * csp
Definition yadif.h:76
Definition swscale.c:71
#define av_log(a,...)
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 slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844
@ YADIF_DEINT_INTERLACED
only deinterlace frames marked as interlaced
Definition yadif.h:42
@ YADIF_DEINT_ALL
deinterlace all frames
Definition yadif.h:41
@ YADIF_PARITY_TFF
top field first
Definition yadif.h:35
@ YADIF_PARITY_BFF
bottom field first
Definition yadif.h:36
@ YADIF_PARITY_AUTO
auto detection
Definition yadif.h:37
@ YADIF_FIELD_NORMAL
A normal field in the middle of a sequence.
Definition yadif.h:48
@ YADIF_FIELD_END
The first or last field in a sequence.
Definition yadif.h:47
int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
@ YADIF_MODE_SEND_FIELD
send 1 frame for each field
Definition yadif.h:29
@ YADIF_MODE_SEND_FRAME
send 1 frame for each frame
Definition yadif.h:28
int ff_yadif_request_frame(AVFilterLink *link)
void ff_yadif_uninit(AVFilterContext *ctx)
int ff_yadif_config_output_common(AVFilterLink *outlink)