| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 40 | typedef struct BWDIFContext { | ||
| 41 | YADIFContext yadif; | ||
| 42 | BWDIFDSPContext dsp; | ||
| 43 | } BWDIFContext; | ||
| 44 | |||
| 45 | typedef struct ThreadData { | ||
| 46 | AVFrame *frame; | ||
| 47 | int plane; | ||
| 48 | int w, h; | ||
| 49 | int parity; | ||
| 50 | int tff; | ||
| 51 | } ThreadData; | ||
| 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 | ||
| 55 | 6642 | static inline int job_start(const int jobnr, const int nb_jobs, const int h) | |
| 56 | { | ||
| 57 |
2/2✓ Branch 0 taken 6273 times.
✓ Branch 1 taken 369 times.
|
6642 | return jobnr >= nb_jobs ? h : (ff_slice_pos(h, jobnr, nb_jobs)) & ~3; |
| 58 | } | ||
| 59 | |||
| 60 | 3321 | static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 61 | { | ||
| 62 | 3321 | BWDIFContext *s = ctx->priv; | |
| 63 | 3321 | YADIFContext *yadif = &s->yadif; | |
| 64 | 3321 | ThreadData *td = arg; | |
| 65 | 3321 | int linesize = yadif->cur->linesize[td->plane]; | |
| 66 | 3321 | int clip_max = (1 << (yadif->csp->comp[td->plane].depth)) - 1; | |
| 67 | 3321 | int df = (yadif->csp->comp[td->plane].depth + 7) / 8; | |
| 68 | 3321 | int refs = linesize / df; | |
| 69 | 3321 | int slice_start = job_start(jobnr, nb_jobs, td->h); | |
| 70 | 3321 | int slice_end = job_start(jobnr + 1, nb_jobs, td->h); | |
| 71 | int y; | ||
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 141696 times.
✓ Branch 1 taken 3321 times.
|
145017 | for (y = slice_start; y < slice_end; y++) { |
| 74 |
2/2✓ Branch 0 taken 70848 times.
✓ Branch 1 taken 70848 times.
|
141696 | if ((y ^ td->parity) & 1) { |
| 75 | 70848 | uint8_t *prev = &yadif->prev->data[td->plane][y * linesize]; | |
| 76 | 70848 | uint8_t *cur = &yadif->cur ->data[td->plane][y * linesize]; | |
| 77 | 70848 | uint8_t *next = &yadif->next->data[td->plane][y * linesize]; | |
| 78 | 70848 | uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]]; | |
| 79 |
2/2✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 69120 times.
|
70848 | if (yadif->current_field == YADIF_FIELD_END) { |
| 80 |
3/4✓ Branch 0 taken 1719 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1728 times.
|
3456 | s->dsp.filter_intra(dst, cur, td->w, (y + 1) < td->h ? refs : -refs, |
| 81 | y > 0 ? -refs : refs, | ||
| 82 |
2/2✓ Branch 0 taken 1719 times.
✓ Branch 1 taken 9 times.
|
1728 | (y + 3) < td->h ? 3 * refs : -refs, |
| 83 | y > 2 ? -3 * refs : refs, | ||
| 84 |
2/2✓ Branch 0 taken 1710 times.
✓ Branch 1 taken 18 times.
|
1728 | td->parity ^ td->tff, clip_max); |
| 85 |
4/4✓ Branch 0 taken 68400 times.
✓ Branch 1 taken 720 times.
✓ Branch 2 taken 720 times.
✓ Branch 3 taken 67680 times.
|
69120 | } else if ((y < 4) || ((y + 5) > td->h)) { |
| 86 |
2/2✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 360 times.
|
5400 | s->dsp.filter_edge(dst, prev, cur, next, td->w, |
| 87 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 1350 times.
|
1440 | (y + 1) < td->h ? refs : -refs, |
| 88 | y > 0 ? -refs : refs, | ||
| 89 | 1440 | refs << 1, -(refs << 1), | |
| 90 |
2/2✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 270 times.
|
1440 | td->parity ^ td->tff, clip_max, |
| 91 |
2/2✓ Branch 0 taken 720 times.
✓ Branch 1 taken 360 times.
|
1080 | (y < 2) || ((y + 3) > td->h) ? 0 : 1); |
| 92 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 67680 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
67680 | } 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 | 67680 | s->dsp.filter_line(dst, prev, cur, next, td->w, | |
| 99 | 67680 | refs, -refs, refs << 1, -(refs << 1), | |
| 100 | 67680 | 3 * refs, -3 * refs, refs << 2, -(refs << 2), | |
| 101 | 67680 | td->parity ^ td->tff, clip_max); | |
| 102 | } | ||
| 103 | } else { | ||
| 104 | 70848 | memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]], | |
| 105 | 70848 | &yadif->cur->data[td->plane][y * linesize], td->w * df); | |
| 106 | } | ||
| 107 | } | ||
| 108 | 3321 | return 0; | |
| 109 | } | ||
| 110 | |||
| 111 | 123 | static void filter(AVFilterContext *ctx, AVFrame *dstpic, | |
| 112 | int parity, int tff) | ||
| 113 | { | ||
| 114 | 123 | BWDIFContext *bwdif = ctx->priv; | |
| 115 | 123 | YADIFContext *yadif = &bwdif->yadif; | |
| 116 | 123 | ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff }; | |
| 117 | int i; | ||
| 118 | |||
| 119 |
2/2✓ Branch 0 taken 369 times.
✓ Branch 1 taken 123 times.
|
492 | for (i = 0; i < yadif->csp->nb_components; i++) { |
| 120 | 369 | int w = dstpic->width; | |
| 121 | 369 | int h = dstpic->height; | |
| 122 | |||
| 123 |
4/4✓ Branch 0 taken 246 times.
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 123 times.
✓ Branch 3 taken 123 times.
|
369 | if (i == 1 || i == 2) { |
| 124 | 246 | w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w); | |
| 125 | 246 | h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h); | |
| 126 | } | ||
| 127 | |||
| 128 | 369 | td.w = w; | |
| 129 | 369 | td.h = h; | |
| 130 | 369 | td.plane = i; | |
| 131 | |||
| 132 | 369 | ff_filter_execute(ctx, filter_slice, &td, NULL, | |
| 133 |
1/2✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
|
369 | FFMIN((h+3)/4, ff_filter_get_nb_threads(ctx))); |
| 134 | } | ||
| 135 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 120 times.
|
123 | if (yadif->current_field == YADIF_FIELD_END) { |
| 136 | 3 | yadif->current_field = YADIF_FIELD_NORMAL; | |
| 137 | } | ||
| 138 | 123 | } | |
| 139 | |||
| 140 | static const enum AVPixelFormat pix_fmts[] = { | ||
| 141 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV420P, | ||
| 142 | AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, | ||
| 143 | AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, | ||
| 144 | AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, | ||
| 145 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, | ||
| 146 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, | ||
| 147 | AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, | ||
| 148 | AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, | ||
| 149 | AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, | ||
| 150 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, | ||
| 151 | AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, | ||
| 152 | AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, | ||
| 153 | AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, | ||
| 154 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, | ||
| 155 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, | ||
| 156 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16, | ||
| 157 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16, | ||
| 158 | AV_PIX_FMT_NONE | ||
| 159 | }; | ||
| 160 | |||
| 161 | 3 | static int config_props(AVFilterLink *link) | |
| 162 | { | ||
| 163 | 3 | AVFilterContext *ctx = link->src; | |
| 164 | 3 | BWDIFContext *s = link->src->priv; | |
| 165 | 3 | YADIFContext *yadif = &s->yadif; | |
| 166 | int ret; | ||
| 167 | |||
| 168 | 3 | ret = ff_yadif_config_output_common(link); | |
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
| 170 | ✗ | return AVERROR(EINVAL); | |
| 171 | |||
| 172 | 3 | yadif->csp = av_pix_fmt_desc_get(link->format); | |
| 173 | 3 | yadif->filter = filter; | |
| 174 | |||
| 175 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | 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 | 3 | ff_bwdif_init_filter_line(&s->dsp, yadif->csp->comp[0].depth); | |
| 181 | |||
| 182 | 3 | 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 | |||
| 191 | static 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 | |||
| 208 | AVFILTER_DEFINE_CLASS(bwdif); | ||
| 209 | |||
| 210 | static const AVFilterPad avfilter_vf_bwdif_inputs[] = { | ||
| 211 | { | ||
| 212 | .name = "default", | ||
| 213 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 214 | .filter_frame = ff_yadif_filter_frame, | ||
| 215 | }, | ||
| 216 | }; | ||
| 217 | |||
| 218 | static const AVFilterPad avfilter_vf_bwdif_outputs[] = { | ||
| 219 | { | ||
| 220 | .name = "default", | ||
| 221 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 222 | .request_frame = ff_yadif_request_frame, | ||
| 223 | .config_props = config_props, | ||
| 224 | }, | ||
| 225 | }; | ||
| 226 | |||
| 227 | const FFFilter ff_vf_bwdif = { | ||
| 228 | .p.name = "bwdif", | ||
| 229 | .p.description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."), | ||
| 230 | .p.priv_class = &bwdif_class, | ||
| 231 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, | ||
| 232 | .priv_size = sizeof(BWDIFContext), | ||
| 233 | .uninit = ff_yadif_uninit, | ||
| 234 | FILTER_INPUTS(avfilter_vf_bwdif_inputs), | ||
| 235 | FILTER_OUTPUTS(avfilter_vf_bwdif_outputs), | ||
| 236 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
| 237 | }; | ||
| 238 |