• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/avassert.h"
23 #include "libavutil/cpu.h"
24 #include "libavutil/common.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/imgutils.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31 #include "yadif.h"
32 
33 typedef struct ThreadData {
34     AVFrame *frame;
35     int plane;
36     int w, h;
37     int parity;
38     int tff;
39 } ThreadData;
40 
41 #define CHECK(j)\
42     {   int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
43                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
44                   + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
45         if (score < spatial_score) {\
46             spatial_score= score;\
47             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
48 
49 /* The is_not_edge argument here controls when the code will enter a branch
50  * which reads up to and including x-3 and x+3. */
51 
52 #define FILTER(start, end, is_not_edge) \
53     for (x = start;  x < end; x++) { \
54         int c = cur[mrefs]; \
55         int d = (prev2[0] + next2[0])>>1; \
56         int e = cur[prefs]; \
57         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
58         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
59         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
60         int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
61         int spatial_pred = (c+e) >> 1; \
62  \
63         if (is_not_edge) {\
64             int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
65                               + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
66             CHECK(-1) CHECK(-2) }} }} \
67             CHECK( 1) CHECK( 2) }} }} \
68         }\
69  \
70         if (!(mode&2)) { \
71             int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
72             int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
73             int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
74             int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
75  \
76             diff = FFMAX3(diff, min, -max); \
77         } \
78  \
79         if (spatial_pred > d + diff) \
80            spatial_pred = d + diff; \
81         else if (spatial_pred < d - diff) \
82            spatial_pred = d - diff; \
83  \
84         dst[0] = spatial_pred; \
85  \
86         dst++; \
87         cur++; \
88         prev++; \
89         next++; \
90         prev2++; \
91         next2++; \
92     }
93 
filter_line_c(void * dst1,void * prev1,void * cur1,void * next1,int w,int prefs,int mrefs,int parity,int mode)94 static void filter_line_c(void *dst1,
95                           void *prev1, void *cur1, void *next1,
96                           int w, int prefs, int mrefs, int parity, int mode)
97 {
98     uint8_t *dst  = dst1;
99     uint8_t *prev = prev1;
100     uint8_t *cur  = cur1;
101     uint8_t *next = next1;
102     int x;
103     uint8_t *prev2 = parity ? prev : cur ;
104     uint8_t *next2 = parity ? cur  : next;
105 
106     /* The function is called with the pointers already pointing to data[3] and
107      * with 6 subtracted from the width.  This allows the FILTER macro to be
108      * called so that it processes all the pixels normally.  A constant value of
109      * true for is_not_edge lets the compiler ignore the if statement. */
110     FILTER(0, w, 1)
111 }
112 
113 #define MAX_ALIGN 8
filter_edges(void * dst1,void * prev1,void * cur1,void * next1,int w,int prefs,int mrefs,int parity,int mode)114 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
115                          int w, int prefs, int mrefs, int parity, int mode)
116 {
117     uint8_t *dst  = dst1;
118     uint8_t *prev = prev1;
119     uint8_t *cur  = cur1;
120     uint8_t *next = next1;
121     int x;
122     uint8_t *prev2 = parity ? prev : cur ;
123     uint8_t *next2 = parity ? cur  : next;
124 
125     const int edge = MAX_ALIGN - 1;
126     int offset = FFMAX(w - edge, 3);
127 
128     /* Only edge pixels need to be processed here.  A constant value of false
129      * for is_not_edge should let the compiler ignore the whole branch. */
130     FILTER(0, FFMIN(3, w), 0)
131 
132     dst  = (uint8_t*)dst1  + offset;
133     prev = (uint8_t*)prev1 + offset;
134     cur  = (uint8_t*)cur1  + offset;
135     next = (uint8_t*)next1 + offset;
136     prev2 = (uint8_t*)(parity ? prev : cur);
137     next2 = (uint8_t*)(parity ? cur  : next);
138 
139     FILTER(offset, w - 3, 1)
140     offset = FFMAX(offset, w - 3);
141     FILTER(offset, w, 0)
142 }
143 
144 
filter_line_c_16bit(void * dst1,void * prev1,void * cur1,void * next1,int w,int prefs,int mrefs,int parity,int mode)145 static void filter_line_c_16bit(void *dst1,
146                                 void *prev1, void *cur1, void *next1,
147                                 int w, int prefs, int mrefs, int parity,
148                                 int mode)
149 {
150     uint16_t *dst  = dst1;
151     uint16_t *prev = prev1;
152     uint16_t *cur  = cur1;
153     uint16_t *next = next1;
154     int x;
155     uint16_t *prev2 = parity ? prev : cur ;
156     uint16_t *next2 = parity ? cur  : next;
157     mrefs /= 2;
158     prefs /= 2;
159 
160     FILTER(0, w, 1)
161 }
162 
filter_edges_16bit(void * dst1,void * prev1,void * cur1,void * next1,int w,int prefs,int mrefs,int parity,int mode)163 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
164                                int w, int prefs, int mrefs, int parity, int mode)
165 {
166     uint16_t *dst  = dst1;
167     uint16_t *prev = prev1;
168     uint16_t *cur  = cur1;
169     uint16_t *next = next1;
170     int x;
171     uint16_t *prev2 = parity ? prev : cur ;
172     uint16_t *next2 = parity ? cur  : next;
173 
174     const int edge = MAX_ALIGN / 2 - 1;
175     int offset = FFMAX(w - edge, 3);
176 
177     mrefs /= 2;
178     prefs /= 2;
179 
180     FILTER(0,  FFMIN(3, w), 0)
181 
182     dst   = (uint16_t*)dst1  + offset;
183     prev  = (uint16_t*)prev1 + offset;
184     cur   = (uint16_t*)cur1  + offset;
185     next  = (uint16_t*)next1 + offset;
186     prev2 = (uint16_t*)(parity ? prev : cur);
187     next2 = (uint16_t*)(parity ? cur  : next);
188 
189     FILTER(offset, w - 3, 1)
190     offset = FFMAX(offset, w - 3);
191     FILTER(offset, w, 0)
192 }
193 
filter_slice(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)194 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
195 {
196     YADIFContext *s = ctx->priv;
197     ThreadData *td  = arg;
198     int refs = s->cur->linesize[td->plane];
199     int df = (s->csp->comp[td->plane].depth + 7) / 8;
200     int pix_3 = 3 * df;
201     int slice_start = (td->h *  jobnr   ) / nb_jobs;
202     int slice_end   = (td->h * (jobnr+1)) / nb_jobs;
203     int y;
204     int edge = 3 + MAX_ALIGN / df - 1;
205 
206     /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
207      * we need to call the c variant which avoids this for border pixels
208      */
209     for (y = slice_start; y < slice_end; y++) {
210         if ((y ^ td->parity) & 1) {
211             uint8_t *prev = &s->prev->data[td->plane][y * refs];
212             uint8_t *cur  = &s->cur ->data[td->plane][y * refs];
213             uint8_t *next = &s->next->data[td->plane][y * refs];
214             uint8_t *dst  = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
215             int     mode  = y == 1 || y + 2 == td->h ? 2 : s->mode;
216             s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
217                            next + pix_3, td->w - edge,
218                            y + 1 < td->h ? refs : -refs,
219                            y ? -refs : refs,
220                            td->parity ^ td->tff, mode);
221             s->filter_edges(dst, prev, cur, next, td->w,
222                             y + 1 < td->h ? refs : -refs,
223                             y ? -refs : refs,
224                             td->parity ^ td->tff, mode);
225         } else {
226             memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
227                    &s->cur->data[td->plane][y * refs], td->w * df);
228         }
229     }
230     return 0;
231 }
232 
filter(AVFilterContext * ctx,AVFrame * dstpic,int parity,int tff)233 static void filter(AVFilterContext *ctx, AVFrame *dstpic,
234                    int parity, int tff)
235 {
236     YADIFContext *yadif = ctx->priv;
237     ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
238     int i;
239 
240     for (i = 0; i < yadif->csp->nb_components; i++) {
241         int w = dstpic->width;
242         int h = dstpic->height;
243 
244         if (i == 1 || i == 2) {
245             w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
246             h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
247         }
248 
249 
250         td.w       = w;
251         td.h       = h;
252         td.plane   = i;
253 
254         ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
255     }
256 
257     emms_c();
258 }
259 
uninit(AVFilterContext * ctx)260 static av_cold void uninit(AVFilterContext *ctx)
261 {
262     YADIFContext *yadif = ctx->priv;
263 
264     av_frame_free(&yadif->prev);
265     av_frame_free(&yadif->cur );
266     av_frame_free(&yadif->next);
267 }
268 
query_formats(AVFilterContext * ctx)269 static int query_formats(AVFilterContext *ctx)
270 {
271     static const enum AVPixelFormat pix_fmts[] = {
272         AV_PIX_FMT_YUV420P,   AV_PIX_FMT_YUV422P,   AV_PIX_FMT_YUV444P,
273         AV_PIX_FMT_YUV410P,   AV_PIX_FMT_YUV411P,   AV_PIX_FMT_YUV440P,
274         AV_PIX_FMT_GRAY8,     AV_PIX_FMT_GRAY16,
275         AV_PIX_FMT_YUVJ420P,  AV_PIX_FMT_YUVJ422P,  AV_PIX_FMT_YUVJ444P,
276         AV_PIX_FMT_YUVJ440P,
277         AV_PIX_FMT_YUV420P9,  AV_PIX_FMT_YUV422P9,  AV_PIX_FMT_YUV444P9,
278         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
279         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
280         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
281         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
282         AV_PIX_FMT_YUVA420P,  AV_PIX_FMT_YUVA422P,  AV_PIX_FMT_YUVA444P,
283         AV_PIX_FMT_GBRP,      AV_PIX_FMT_GBRP9,     AV_PIX_FMT_GBRP10,
284         AV_PIX_FMT_GBRP12,    AV_PIX_FMT_GBRP14,    AV_PIX_FMT_GBRP16,
285         AV_PIX_FMT_GBRAP,
286         AV_PIX_FMT_NONE
287     };
288 
289     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
290     if (!fmts_list)
291         return AVERROR(ENOMEM);
292     return ff_set_common_formats(ctx, fmts_list);
293 }
294 
config_output(AVFilterLink * outlink)295 static int config_output(AVFilterLink *outlink)
296 {
297     AVFilterContext *ctx = outlink->src;
298     YADIFContext *s = ctx->priv;
299 
300     outlink->time_base.num = ctx->inputs[0]->time_base.num;
301     outlink->time_base.den = ctx->inputs[0]->time_base.den * 2;
302     outlink->w             = ctx->inputs[0]->w;
303     outlink->h             = ctx->inputs[0]->h;
304 
305     if(s->mode & 1)
306         outlink->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
307                                     (AVRational){2, 1});
308 
309     if (outlink->w < 3 || outlink->h < 3) {
310         av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
311         return AVERROR(EINVAL);
312     }
313 
314     s->csp = av_pix_fmt_desc_get(outlink->format);
315     s->filter = filter;
316     if (s->csp->comp[0].depth > 8) {
317         s->filter_line  = filter_line_c_16bit;
318         s->filter_edges = filter_edges_16bit;
319     } else {
320         s->filter_line  = filter_line_c;
321         s->filter_edges = filter_edges;
322     }
323 
324     if (ARCH_X86)
325         ff_yadif_init_x86(s);
326 
327     return 0;
328 }
329 
330 
331 static const AVClass yadif_class = {
332     .class_name = "yadif",
333     .item_name  = av_default_item_name,
334     .option     = ff_yadif_options,
335     .version    = LIBAVUTIL_VERSION_INT,
336     .category   = AV_CLASS_CATEGORY_FILTER,
337 };
338 
339 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
340     {
341         .name          = "default",
342         .type          = AVMEDIA_TYPE_VIDEO,
343         .filter_frame  = ff_yadif_filter_frame,
344     },
345     { NULL }
346 };
347 
348 static const AVFilterPad avfilter_vf_yadif_outputs[] = {
349     {
350         .name          = "default",
351         .type          = AVMEDIA_TYPE_VIDEO,
352         .request_frame = ff_yadif_request_frame,
353         .config_props  = config_output,
354     },
355     { NULL }
356 };
357 
358 AVFilter ff_vf_yadif = {
359     .name          = "yadif",
360     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
361     .priv_size     = sizeof(YADIFContext),
362     .priv_class    = &yadif_class,
363     .uninit        = uninit,
364     .query_formats = query_formats,
365     .inputs        = avfilter_vf_yadif_inputs,
366     .outputs       = avfilter_vf_yadif_outputs,
367     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
368 };
369