| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved | ||
| 3 | * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D | ||
| 4 | * Based on the process described by Martin Weston for BBC R&D | ||
| 5 | * Author of FFmpeg filter: Mark Himsley for BBC Broadcast Systems Development | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include "libavutil/common.h" | ||
| 25 | #include "libavutil/imgutils.h" | ||
| 26 | #include "libavutil/mem.h" | ||
| 27 | #include "libavutil/opt.h" | ||
| 28 | #include "libavutil/pixdesc.h" | ||
| 29 | #include "avfilter.h" | ||
| 30 | #include "filters.h" | ||
| 31 | #include "video.h" | ||
| 32 | #include "w3fdif.h" | ||
| 33 | |||
| 34 | typedef struct W3FDIFContext { | ||
| 35 | const AVClass *class; | ||
| 36 | int filter; ///< 0 is simple, 1 is more complex | ||
| 37 | int mode; ///< 0 is frame, 1 is field | ||
| 38 | int parity; ///< frame field parity | ||
| 39 | int deint; ///< which frames to deinterlace | ||
| 40 | int linesize[4]; ///< bytes of pixel data per line for each plane | ||
| 41 | int planeheight[4]; ///< height of each plane | ||
| 42 | int field; ///< which field are we on, 0 or 1 | ||
| 43 | int eof; | ||
| 44 | int nb_planes; | ||
| 45 | AVFrame *prev, *cur, *next; ///< previous, current, next frames | ||
| 46 | int32_t **work_line; ///< lines we are calculating | ||
| 47 | int nb_threads; | ||
| 48 | int max; | ||
| 49 | |||
| 50 | W3FDIFDSPContext dsp; | ||
| 51 | } W3FDIFContext; | ||
| 52 | |||
| 53 | #define OFFSET(x) offsetof(W3FDIFContext, x) | ||
| 54 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
| 55 | #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, .unit = u } | ||
| 56 | |||
| 57 | static const AVOption w3fdif_options[] = { | ||
| 58 | { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, .unit = "filter" }, | ||
| 59 | CONST("simple", NULL, 0, "filter"), | ||
| 60 | CONST("complex", NULL, 1, "filter"), | ||
| 61 | { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, .unit = "mode"}, | ||
| 62 | CONST("frame", "send one frame for each frame", 0, "mode"), | ||
| 63 | CONST("field", "send one frame for each field", 1, "mode"), | ||
| 64 | { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, .unit = "parity" }, | ||
| 65 | CONST("tff", "assume top field first", 0, "parity"), | ||
| 66 | CONST("bff", "assume bottom field first", 1, "parity"), | ||
| 67 | CONST("auto", "auto detect parity", -1, "parity"), | ||
| 68 | { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "deint" }, | ||
| 69 | CONST("all", "deinterlace all frames", 0, "deint"), | ||
| 70 | CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"), | ||
| 71 | { NULL } | ||
| 72 | }; | ||
| 73 | |||
| 74 | AVFILTER_DEFINE_CLASS(w3fdif); | ||
| 75 | |||
| 76 | static const enum AVPixelFormat pix_fmts[] = { | ||
| 77 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, | ||
| 78 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, | ||
| 79 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, | ||
| 80 | AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, | ||
| 81 | AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, | ||
| 82 | AV_PIX_FMT_YUVJ411P, | ||
| 83 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, | ||
| 84 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, | ||
| 85 | AV_PIX_FMT_GRAY8, | ||
| 86 | AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, | ||
| 87 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, | ||
| 88 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, | ||
| 89 | AV_PIX_FMT_YUV440P10, | ||
| 90 | AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, | ||
| 91 | AV_PIX_FMT_YUV440P12, | ||
| 92 | AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, | ||
| 93 | AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, | ||
| 94 | AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, | ||
| 95 | AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, | ||
| 96 | AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, | ||
| 97 | AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, | ||
| 98 | AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, | ||
| 99 | AV_PIX_FMT_NONE | ||
| 100 | }; | ||
| 101 | |||
| 102 | 18432 | static void filter_simple_low(int32_t *work_line, | |
| 103 | uint8_t *in_lines_cur[2], | ||
| 104 | const int16_t *coef, int linesize) | ||
| 105 | { | ||
| 106 | int i; | ||
| 107 | |||
| 108 |
2/2✓ Branch 0 taken 9953280 times.
✓ Branch 1 taken 18432 times.
|
9971712 | for (i = 0; i < linesize; i++) { |
| 109 | 9953280 | *work_line = *in_lines_cur[0]++ * coef[0]; | |
| 110 | 9953280 | *work_line++ += *in_lines_cur[1]++ * coef[1]; | |
| 111 | } | ||
| 112 | 18432 | } | |
| 113 | |||
| 114 | 18432 | static void filter_complex_low(int32_t *work_line, | |
| 115 | uint8_t *in_lines_cur[4], | ||
| 116 | const int16_t *coef, int linesize) | ||
| 117 | { | ||
| 118 | int i; | ||
| 119 | |||
| 120 |
2/2✓ Branch 0 taken 9953280 times.
✓ Branch 1 taken 18432 times.
|
9971712 | for (i = 0; i < linesize; i++) { |
| 121 | 9953280 | *work_line = *in_lines_cur[0]++ * coef[0]; | |
| 122 | 9953280 | *work_line += *in_lines_cur[1]++ * coef[1]; | |
| 123 | 9953280 | *work_line += *in_lines_cur[2]++ * coef[2]; | |
| 124 | 9953280 | *work_line++ += *in_lines_cur[3]++ * coef[3]; | |
| 125 | } | ||
| 126 | 18432 | } | |
| 127 | |||
| 128 | 18432 | static void filter_simple_high(int32_t *work_line, | |
| 129 | uint8_t *in_lines_cur[3], | ||
| 130 | uint8_t *in_lines_adj[3], | ||
| 131 | const int16_t *coef, int linesize) | ||
| 132 | { | ||
| 133 | int i; | ||
| 134 | |||
| 135 |
2/2✓ Branch 0 taken 9953280 times.
✓ Branch 1 taken 18432 times.
|
9971712 | for (i = 0; i < linesize; i++) { |
| 136 | 9953280 | *work_line += *in_lines_cur[0]++ * coef[0]; | |
| 137 | 9953280 | *work_line += *in_lines_adj[0]++ * coef[0]; | |
| 138 | 9953280 | *work_line += *in_lines_cur[1]++ * coef[1]; | |
| 139 | 9953280 | *work_line += *in_lines_adj[1]++ * coef[1]; | |
| 140 | 9953280 | *work_line += *in_lines_cur[2]++ * coef[2]; | |
| 141 | 9953280 | *work_line++ += *in_lines_adj[2]++ * coef[2]; | |
| 142 | } | ||
| 143 | 18432 | } | |
| 144 | |||
| 145 | 18432 | static void filter_complex_high(int32_t *work_line, | |
| 146 | uint8_t *in_lines_cur[5], | ||
| 147 | uint8_t *in_lines_adj[5], | ||
| 148 | const int16_t *coef, int linesize) | ||
| 149 | { | ||
| 150 | int i; | ||
| 151 | |||
| 152 |
2/2✓ Branch 0 taken 9953280 times.
✓ Branch 1 taken 18432 times.
|
9971712 | for (i = 0; i < linesize; i++) { |
| 153 | 9953280 | *work_line += *in_lines_cur[0]++ * coef[0]; | |
| 154 | 9953280 | *work_line += *in_lines_adj[0]++ * coef[0]; | |
| 155 | 9953280 | *work_line += *in_lines_cur[1]++ * coef[1]; | |
| 156 | 9953280 | *work_line += *in_lines_adj[1]++ * coef[1]; | |
| 157 | 9953280 | *work_line += *in_lines_cur[2]++ * coef[2]; | |
| 158 | 9953280 | *work_line += *in_lines_adj[2]++ * coef[2]; | |
| 159 | 9953280 | *work_line += *in_lines_cur[3]++ * coef[3]; | |
| 160 | 9953280 | *work_line += *in_lines_adj[3]++ * coef[3]; | |
| 161 | 9953280 | *work_line += *in_lines_cur[4]++ * coef[4]; | |
| 162 | 9953280 | *work_line++ += *in_lines_adj[4]++ * coef[4]; | |
| 163 | } | ||
| 164 | 18432 | } | |
| 165 | |||
| 166 | 36864 | static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize, int max) | |
| 167 | { | ||
| 168 | int j; | ||
| 169 | |||
| 170 |
2/2✓ Branch 0 taken 19906560 times.
✓ Branch 1 taken 36864 times.
|
19943424 | for (j = 0; j < linesize; j++, out_pixel++, work_pixel++) |
| 171 | 19906560 | *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15; | |
| 172 | 36864 | } | |
| 173 | |||
| 174 | ✗ | static void filter16_simple_low(int32_t *work_line, | |
| 175 | uint8_t *in_lines_cur8[2], | ||
| 176 | const int16_t *coef, int linesize) | ||
| 177 | { | ||
| 178 | ✗ | uint16_t *in_lines_cur[2] = { (uint16_t *)in_lines_cur8[0], (uint16_t *)in_lines_cur8[1] }; | |
| 179 | int i; | ||
| 180 | |||
| 181 | ✗ | linesize /= 2; | |
| 182 | ✗ | for (i = 0; i < linesize; i++) { | |
| 183 | ✗ | *work_line = *in_lines_cur[0]++ * coef[0]; | |
| 184 | ✗ | *work_line++ += *in_lines_cur[1]++ * coef[1]; | |
| 185 | } | ||
| 186 | ✗ | } | |
| 187 | |||
| 188 | ✗ | static void filter16_complex_low(int32_t *work_line, | |
| 189 | uint8_t *in_lines_cur8[4], | ||
| 190 | const int16_t *coef, int linesize) | ||
| 191 | { | ||
| 192 | ✗ | uint16_t *in_lines_cur[4] = { (uint16_t *)in_lines_cur8[0], | |
| 193 | (uint16_t *)in_lines_cur8[1], | ||
| 194 | (uint16_t *)in_lines_cur8[2], | ||
| 195 | (uint16_t *)in_lines_cur8[3] }; | ||
| 196 | int i; | ||
| 197 | |||
| 198 | ✗ | linesize /= 2; | |
| 199 | ✗ | for (i = 0; i < linesize; i++) { | |
| 200 | ✗ | *work_line = *in_lines_cur[0]++ * coef[0]; | |
| 201 | ✗ | *work_line += *in_lines_cur[1]++ * coef[1]; | |
| 202 | ✗ | *work_line += *in_lines_cur[2]++ * coef[2]; | |
| 203 | ✗ | *work_line++ += *in_lines_cur[3]++ * coef[3]; | |
| 204 | } | ||
| 205 | ✗ | } | |
| 206 | |||
| 207 | ✗ | static void filter16_simple_high(int32_t *work_line, | |
| 208 | uint8_t *in_lines_cur8[3], | ||
| 209 | uint8_t *in_lines_adj8[3], | ||
| 210 | const int16_t *coef, int linesize) | ||
| 211 | { | ||
| 212 | ✗ | uint16_t *in_lines_cur[3] = { (uint16_t *)in_lines_cur8[0], | |
| 213 | (uint16_t *)in_lines_cur8[1], | ||
| 214 | (uint16_t *)in_lines_cur8[2] }; | ||
| 215 | ✗ | uint16_t *in_lines_adj[3] = { (uint16_t *)in_lines_adj8[0], | |
| 216 | (uint16_t *)in_lines_adj8[1], | ||
| 217 | (uint16_t *)in_lines_adj8[2] }; | ||
| 218 | int i; | ||
| 219 | |||
| 220 | ✗ | linesize /= 2; | |
| 221 | ✗ | for (i = 0; i < linesize; i++) { | |
| 222 | ✗ | *work_line += *in_lines_cur[0]++ * coef[0]; | |
| 223 | ✗ | *work_line += *in_lines_adj[0]++ * coef[0]; | |
| 224 | ✗ | *work_line += *in_lines_cur[1]++ * coef[1]; | |
| 225 | ✗ | *work_line += *in_lines_adj[1]++ * coef[1]; | |
| 226 | ✗ | *work_line += *in_lines_cur[2]++ * coef[2]; | |
| 227 | ✗ | *work_line++ += *in_lines_adj[2]++ * coef[2]; | |
| 228 | } | ||
| 229 | ✗ | } | |
| 230 | |||
| 231 | ✗ | static void filter16_complex_high(int32_t *work_line, | |
| 232 | uint8_t *in_lines_cur8[5], | ||
| 233 | uint8_t *in_lines_adj8[5], | ||
| 234 | const int16_t *coef, int linesize) | ||
| 235 | { | ||
| 236 | ✗ | uint16_t *in_lines_cur[5] = { (uint16_t *)in_lines_cur8[0], | |
| 237 | (uint16_t *)in_lines_cur8[1], | ||
| 238 | (uint16_t *)in_lines_cur8[2], | ||
| 239 | (uint16_t *)in_lines_cur8[3], | ||
| 240 | (uint16_t *)in_lines_cur8[4] }; | ||
| 241 | ✗ | uint16_t *in_lines_adj[5] = { (uint16_t *)in_lines_adj8[0], | |
| 242 | (uint16_t *)in_lines_adj8[1], | ||
| 243 | (uint16_t *)in_lines_adj8[2], | ||
| 244 | (uint16_t *)in_lines_adj8[3], | ||
| 245 | (uint16_t *)in_lines_adj8[4] }; | ||
| 246 | int i; | ||
| 247 | |||
| 248 | ✗ | linesize /= 2; | |
| 249 | ✗ | for (i = 0; i < linesize; i++) { | |
| 250 | ✗ | *work_line += *in_lines_cur[0]++ * coef[0]; | |
| 251 | ✗ | *work_line += *in_lines_adj[0]++ * coef[0]; | |
| 252 | ✗ | *work_line += *in_lines_cur[1]++ * coef[1]; | |
| 253 | ✗ | *work_line += *in_lines_adj[1]++ * coef[1]; | |
| 254 | ✗ | *work_line += *in_lines_cur[2]++ * coef[2]; | |
| 255 | ✗ | *work_line += *in_lines_adj[2]++ * coef[2]; | |
| 256 | ✗ | *work_line += *in_lines_cur[3]++ * coef[3]; | |
| 257 | ✗ | *work_line += *in_lines_adj[3]++ * coef[3]; | |
| 258 | ✗ | *work_line += *in_lines_cur[4]++ * coef[4]; | |
| 259 | ✗ | *work_line++ += *in_lines_adj[4]++ * coef[4]; | |
| 260 | } | ||
| 261 | ✗ | } | |
| 262 | |||
| 263 | ✗ | static void filter16_scale(uint8_t *out_pixel8, const int32_t *work_pixel, int linesize, int max) | |
| 264 | { | ||
| 265 | ✗ | uint16_t *out_pixel = (uint16_t *)out_pixel8; | |
| 266 | int j; | ||
| 267 | |||
| 268 | ✗ | linesize /= 2; | |
| 269 | ✗ | for (j = 0; j < linesize; j++, out_pixel++, work_pixel++) | |
| 270 | ✗ | *out_pixel = av_clip(*work_pixel, 0, max) >> 15; | |
| 271 | ✗ | } | |
| 272 | |||
| 273 | 2 | static int config_input(AVFilterLink *inlink) | |
| 274 | { | ||
| 275 | 2 | AVFilterContext *ctx = inlink->dst; | |
| 276 | 2 | W3FDIFContext *s = ctx->priv; | |
| 277 | 2 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 278 | int ret, i, depth, nb_threads; | ||
| 279 | |||
| 280 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) |
| 281 | ✗ | return ret; | |
| 282 | |||
| 283 | 2 | s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); | |
| 284 | 2 | s->planeheight[0] = s->planeheight[3] = inlink->h; | |
| 285 | |||
| 286 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (inlink->h < 3) { |
| 287 | ✗ | av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n"); | |
| 288 | ✗ | return AVERROR(EINVAL); | |
| 289 | } | ||
| 290 | |||
| 291 | 2 | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
| 292 | 2 | nb_threads = ff_filter_get_nb_threads(ctx); | |
| 293 | 2 | s->work_line = av_calloc(nb_threads, sizeof(*s->work_line)); | |
| 294 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->work_line) |
| 295 | ✗ | return AVERROR(ENOMEM); | |
| 296 | 2 | s->nb_threads = nb_threads; | |
| 297 | |||
| 298 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | for (i = 0; i < s->nb_threads; i++) { |
| 299 | 18 | s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0])); | |
| 300 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (!s->work_line[i]) |
| 301 | ✗ | return AVERROR(ENOMEM); | |
| 302 | } | ||
| 303 | |||
| 304 | 2 | depth = desc->comp[0].depth; | |
| 305 | 2 | s->max = ((1 << depth) - 1) * 256 * 128; | |
| 306 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (depth <= 8) { |
| 307 | 2 | s->dsp.filter_simple_low = filter_simple_low; | |
| 308 | 2 | s->dsp.filter_complex_low = filter_complex_low; | |
| 309 | 2 | s->dsp.filter_simple_high = filter_simple_high; | |
| 310 | 2 | s->dsp.filter_complex_high = filter_complex_high; | |
| 311 | 2 | s->dsp.filter_scale = filter_scale; | |
| 312 | } else { | ||
| 313 | ✗ | s->dsp.filter_simple_low = filter16_simple_low; | |
| 314 | ✗ | s->dsp.filter_complex_low = filter16_complex_low; | |
| 315 | ✗ | s->dsp.filter_simple_high = filter16_simple_high; | |
| 316 | ✗ | s->dsp.filter_complex_high = filter16_complex_high; | |
| 317 | ✗ | s->dsp.filter_scale = filter16_scale; | |
| 318 | } | ||
| 319 | |||
| 320 | #if ARCH_X86 | ||
| 321 | 2 | ff_w3fdif_init_x86(&s->dsp, depth); | |
| 322 | #endif | ||
| 323 | |||
| 324 | 2 | return 0; | |
| 325 | } | ||
| 326 | |||
| 327 | 2 | static int config_output(AVFilterLink *outlink) | |
| 328 | { | ||
| 329 | 2 | AVFilterContext *ctx = outlink->src; | |
| 330 | 2 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 331 | 2 | FilterLink *il = ff_filter_link(inlink); | |
| 332 | 2 | FilterLink *ol = ff_filter_link(outlink); | |
| 333 | 2 | W3FDIFContext *s = ctx->priv; | |
| 334 | |||
| 335 | 2 | outlink->time_base = av_mul_q(inlink->time_base, (AVRational){1, 2}); | |
| 336 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (s->mode) |
| 337 | 2 | ol->frame_rate = av_mul_q(il->frame_rate, (AVRational){2, 1}); | |
| 338 | |||
| 339 | 2 | return 0; | |
| 340 | } | ||
| 341 | |||
| 342 | /* | ||
| 343 | * Filter coefficients from PH-2071, scaled by 256 * 128. | ||
| 344 | * Each set of coefficients has a set for low-frequencies and high-frequencies. | ||
| 345 | * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex. | ||
| 346 | * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd. | ||
| 347 | * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies | ||
| 348 | * and high-frequencies for simple and more-complex mode. | ||
| 349 | */ | ||
| 350 | static const int8_t n_coef_lf[2] = { 2, 4 }; | ||
| 351 | static const int16_t coef_lf[2][4] = {{ 16384, 16384, 0, 0}, | ||
| 352 | { -852, 17236, 17236, -852}}; | ||
| 353 | static const int8_t n_coef_hf[2] = { 3, 5 }; | ||
| 354 | static const int16_t coef_hf[2][5] = {{ -2048, 4096, -2048, 0, 0}, | ||
| 355 | { 1016, -3801, 5570, -3801, 1016}}; | ||
| 356 | |||
| 357 | typedef struct ThreadData { | ||
| 358 | AVFrame *out, *cur, *adj; | ||
| 359 | } ThreadData; | ||
| 360 | |||
| 361 | 1728 | static int deinterlace_plane_slice(AVFilterContext *ctx, void *arg, | |
| 362 | int jobnr, int nb_jobs, int plane) | ||
| 363 | { | ||
| 364 | 1728 | W3FDIFContext *s = ctx->priv; | |
| 365 | 1728 | ThreadData *td = arg; | |
| 366 | 1728 | AVFrame *out = td->out; | |
| 367 | 1728 | AVFrame *cur = td->cur; | |
| 368 | 1728 | AVFrame *adj = td->adj; | |
| 369 | 1728 | const int filter = s->filter; | |
| 370 | uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5]; | ||
| 371 | uint8_t *out_line, *out_pixel; | ||
| 372 | int32_t *work_line, *work_pixel; | ||
| 373 | 1728 | uint8_t *cur_data = cur->data[plane]; | |
| 374 | 1728 | uint8_t *adj_data = adj->data[plane]; | |
| 375 | 1728 | uint8_t *dst_data = out->data[plane]; | |
| 376 | 1728 | const int linesize = s->linesize[plane]; | |
| 377 | 1728 | const int height = s->planeheight[plane]; | |
| 378 | 1728 | const int cur_line_stride = cur->linesize[plane]; | |
| 379 | 1728 | const int adj_line_stride = adj->linesize[plane]; | |
| 380 | 1728 | const int dst_line_stride = out->linesize[plane]; | |
| 381 | 1728 | const int start = (height * jobnr) / nb_jobs; | |
| 382 | 1728 | const int end = (height * (jobnr+1)) / nb_jobs; | |
| 383 | 1728 | const int max = s->max; | |
| 384 | 1728 | const int interlaced = !!(cur->flags & AV_FRAME_FLAG_INTERLACED); | |
| 385 |
3/6✓ Branch 0 taken 1728 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1728 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1728 times.
|
1728 | const int tff = s->field == (s->parity == -1 ? interlaced ? !!(cur->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 1 : |
| 386 | ✗ | s->parity ^ 1); | |
| 387 | int j, y_in, y_out; | ||
| 388 | |||
| 389 | /* copy unchanged the lines of the field */ | ||
| 390 | 1728 | y_out = start + (tff ^ (start & 1)); | |
| 391 | |||
| 392 | 1728 | in_line = cur_data + (y_out * cur_line_stride); | |
| 393 | 1728 | out_line = dst_data + (y_out * dst_line_stride); | |
| 394 | |||
| 395 |
2/2✓ Branch 0 taken 36864 times.
✓ Branch 1 taken 1728 times.
|
38592 | while (y_out < end) { |
| 396 | 36864 | memcpy(out_line, in_line, linesize); | |
| 397 | 36864 | y_out += 2; | |
| 398 | 36864 | in_line += cur_line_stride * 2; | |
| 399 | 36864 | out_line += dst_line_stride * 2; | |
| 400 | } | ||
| 401 | |||
| 402 | /* interpolate other lines of the field */ | ||
| 403 | 1728 | y_out = start + ((!tff) ^ (start & 1)); | |
| 404 | |||
| 405 | 1728 | out_line = dst_data + (y_out * dst_line_stride); | |
| 406 | |||
| 407 |
2/2✓ Branch 0 taken 36864 times.
✓ Branch 1 taken 1728 times.
|
38592 | while (y_out < end) { |
| 408 | /* get low vertical frequencies from current field */ | ||
| 409 |
2/2✓ Branch 0 taken 110592 times.
✓ Branch 1 taken 36864 times.
|
147456 | for (j = 0; j < n_coef_lf[filter]; j++) { |
| 410 | 110592 | y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter]; | |
| 411 | |||
| 412 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 110592 times.
|
110880 | while (y_in < 0) |
| 413 | 288 | y_in += 2; | |
| 414 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 110592 times.
|
110880 | while (y_in >= height) |
| 415 | 288 | y_in -= 2; | |
| 416 | |||
| 417 | 110592 | in_lines_cur[j] = cur_data + (y_in * cur_line_stride); | |
| 418 | } | ||
| 419 | |||
| 420 | 36864 | work_line = s->work_line[jobnr]; | |
| 421 |
2/3✓ Branch 0 taken 18432 times.
✓ Branch 1 taken 18432 times.
✗ Branch 2 not taken.
|
36864 | switch (n_coef_lf[filter]) { |
| 422 | 18432 | case 2: | |
| 423 | 18432 | s->dsp.filter_simple_low(work_line, in_lines_cur, | |
| 424 | 18432 | coef_lf[filter], linesize); | |
| 425 | 18432 | break; | |
| 426 | 18432 | case 4: | |
| 427 | 18432 | s->dsp.filter_complex_low(work_line, in_lines_cur, | |
| 428 | 18432 | coef_lf[filter], linesize); | |
| 429 | } | ||
| 430 | |||
| 431 | /* get high vertical frequencies from adjacent fields */ | ||
| 432 |
2/2✓ Branch 0 taken 147456 times.
✓ Branch 1 taken 36864 times.
|
184320 | for (j = 0; j < n_coef_hf[filter]; j++) { |
| 433 | 147456 | y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter]; | |
| 434 | |||
| 435 |
2/2✓ Branch 0 taken 480 times.
✓ Branch 1 taken 147456 times.
|
147936 | while (y_in < 0) |
| 436 | 480 | y_in += 2; | |
| 437 |
2/2✓ Branch 0 taken 480 times.
✓ Branch 1 taken 147456 times.
|
147936 | while (y_in >= height) |
| 438 | 480 | y_in -= 2; | |
| 439 | |||
| 440 | 147456 | in_lines_cur[j] = cur_data + (y_in * cur_line_stride); | |
| 441 | 147456 | in_lines_adj[j] = adj_data + (y_in * adj_line_stride); | |
| 442 | } | ||
| 443 | |||
| 444 | 36864 | work_line = s->work_line[jobnr]; | |
| 445 |
2/3✓ Branch 0 taken 18432 times.
✓ Branch 1 taken 18432 times.
✗ Branch 2 not taken.
|
36864 | switch (n_coef_hf[filter]) { |
| 446 | 18432 | case 3: | |
| 447 | 18432 | s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj, | |
| 448 | 18432 | coef_hf[filter], linesize); | |
| 449 | 18432 | break; | |
| 450 | 18432 | case 5: | |
| 451 | 18432 | s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj, | |
| 452 | 18432 | coef_hf[filter], linesize); | |
| 453 | } | ||
| 454 | |||
| 455 | /* save scaled result to the output frame, scaling down by 256 * 128 */ | ||
| 456 | 36864 | work_pixel = s->work_line[jobnr]; | |
| 457 | 36864 | out_pixel = out_line; | |
| 458 | |||
| 459 | 36864 | s->dsp.filter_scale(out_pixel, work_pixel, linesize, max); | |
| 460 | |||
| 461 | /* move on to next line */ | ||
| 462 | 36864 | y_out += 2; | |
| 463 | 36864 | out_line += dst_line_stride * 2; | |
| 464 | } | ||
| 465 | |||
| 466 | 1728 | return 0; | |
| 467 | } | ||
| 468 | |||
| 469 | 576 | static int deinterlace_slice(AVFilterContext *ctx, void *arg, | |
| 470 | int jobnr, int nb_jobs) | ||
| 471 | { | ||
| 472 | 576 | W3FDIFContext *s = ctx->priv; | |
| 473 | |||
| 474 |
2/2✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 576 times.
|
2304 | for (int p = 0; p < s->nb_planes; p++) |
| 475 | 1728 | deinterlace_plane_slice(ctx, arg, jobnr, nb_jobs, p); | |
| 476 | |||
| 477 | 576 | return 0; | |
| 478 | } | ||
| 479 | |||
| 480 | 64 | static int filter(AVFilterContext *ctx, int is_second) | |
| 481 | { | ||
| 482 | 64 | W3FDIFContext *s = ctx->priv; | |
| 483 | 64 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 484 | AVFrame *out, *adj; | ||
| 485 | ThreadData td; | ||
| 486 | |||
| 487 | 64 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 488 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (!out) |
| 489 | ✗ | return AVERROR(ENOMEM); | |
| 490 | 64 | av_frame_copy_props(out, s->cur); | |
| 491 | 64 | out->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
| 492 | |||
| 493 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 32 times.
|
64 | if (!is_second) { |
| 494 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (out->pts != AV_NOPTS_VALUE) |
| 495 | 32 | out->pts *= 2; | |
| 496 | } else { | ||
| 497 | 32 | int64_t cur_pts = s->cur->pts; | |
| 498 | 32 | int64_t next_pts = s->next->pts; | |
| 499 | |||
| 500 |
2/4✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
32 | if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) { |
| 501 | 32 | out->pts = cur_pts + next_pts; | |
| 502 | } else { | ||
| 503 | ✗ | out->pts = AV_NOPTS_VALUE; | |
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 32 times.
|
64 | adj = s->field ? s->next : s->prev; |
| 508 | 64 | td.out = out; td.cur = s->cur; td.adj = adj; | |
| 509 | 64 | ff_filter_execute(ctx, deinterlace_slice, &td, NULL, | |
| 510 | 64 | FFMIN(s->planeheight[1], s->nb_threads)); | |
| 511 | |||
| 512 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | if (s->mode) |
| 513 | 64 | s->field = !s->field; | |
| 514 | |||
| 515 | 64 | return ff_filter_frame(outlink, out); | |
| 516 | } | ||
| 517 | |||
| 518 | 34 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
| 519 | { | ||
| 520 | 34 | AVFilterContext *ctx = inlink->dst; | |
| 521 | 34 | W3FDIFContext *s = ctx->priv; | |
| 522 | int ret; | ||
| 523 | |||
| 524 | 34 | av_frame_free(&s->prev); | |
| 525 | 34 | s->prev = s->cur; | |
| 526 | 34 | s->cur = s->next; | |
| 527 | 34 | s->next = frame; | |
| 528 | |||
| 529 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 32 times.
|
34 | if (!s->cur) { |
| 530 | 2 | s->cur = av_frame_clone(s->next); | |
| 531 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->cur) |
| 532 | ✗ | return AVERROR(ENOMEM); | |
| 533 | } | ||
| 534 | |||
| 535 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 32 times.
|
34 | if (!s->prev) |
| 536 | 2 | return 0; | |
| 537 | |||
| 538 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 32 times.
|
32 | if ((s->deint && !(s->cur->flags & AV_FRAME_FLAG_INTERLACED)) || ctx->is_disabled) { |
| 539 | ✗ | AVFrame *out = av_frame_clone(s->cur); | |
| 540 | ✗ | if (!out) | |
| 541 | ✗ | return AVERROR(ENOMEM); | |
| 542 | |||
| 543 | ✗ | av_frame_free(&s->prev); | |
| 544 | ✗ | if (out->pts != AV_NOPTS_VALUE) | |
| 545 | ✗ | out->pts *= 2; | |
| 546 | ✗ | return ff_filter_frame(ctx->outputs[0], out); | |
| 547 | } | ||
| 548 | |||
| 549 | 32 | ret = filter(ctx, 0); | |
| 550 |
2/4✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
|
32 | if (ret < 0 || s->mode == 0) |
| 551 | ✗ | return ret; | |
| 552 | |||
| 553 | 32 | return filter(ctx, 1); | |
| 554 | } | ||
| 555 | |||
| 556 | 32 | static int request_frame(AVFilterLink *outlink) | |
| 557 | { | ||
| 558 | 32 | AVFilterContext *ctx = outlink->src; | |
| 559 | 32 | W3FDIFContext *s = ctx->priv; | |
| 560 | int ret; | ||
| 561 | |||
| 562 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (s->eof) |
| 563 | ✗ | return AVERROR_EOF; | |
| 564 | |||
| 565 | 32 | ret = ff_request_frame(ctx->inputs[0]); | |
| 566 | |||
| 567 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
32 | if (ret == AVERROR_EOF && s->cur) { |
| 568 | ✗ | AVFrame *next = av_frame_clone(s->next); | |
| 569 | ✗ | if (!next) | |
| 570 | ✗ | return AVERROR(ENOMEM); | |
| 571 | ✗ | next->pts = s->next->pts * 2 - s->cur->pts; | |
| 572 | ✗ | filter_frame(ctx->inputs[0], next); | |
| 573 | ✗ | s->eof = 1; | |
| 574 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | } else if (ret < 0) { |
| 575 | ✗ | return ret; | |
| 576 | } | ||
| 577 | |||
| 578 | 32 | return 0; | |
| 579 | } | ||
| 580 | |||
| 581 | 4 | static av_cold void uninit(AVFilterContext *ctx) | |
| 582 | { | ||
| 583 | 4 | W3FDIFContext *s = ctx->priv; | |
| 584 | int i; | ||
| 585 | |||
| 586 | 4 | av_frame_free(&s->prev); | |
| 587 | 4 | av_frame_free(&s->cur ); | |
| 588 | 4 | av_frame_free(&s->next); | |
| 589 | |||
| 590 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
|
22 | for (i = 0; i < s->nb_threads; i++) |
| 591 | 18 | av_freep(&s->work_line[i]); | |
| 592 | |||
| 593 | 4 | av_freep(&s->work_line); | |
| 594 | 4 | } | |
| 595 | |||
| 596 | static const AVFilterPad w3fdif_inputs[] = { | ||
| 597 | { | ||
| 598 | .name = "default", | ||
| 599 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 600 | .filter_frame = filter_frame, | ||
| 601 | .config_props = config_input, | ||
| 602 | }, | ||
| 603 | }; | ||
| 604 | |||
| 605 | static const AVFilterPad w3fdif_outputs[] = { | ||
| 606 | { | ||
| 607 | .name = "default", | ||
| 608 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 609 | .config_props = config_output, | ||
| 610 | .request_frame = request_frame, | ||
| 611 | }, | ||
| 612 | }; | ||
| 613 | |||
| 614 | const FFFilter ff_vf_w3fdif = { | ||
| 615 | .p.name = "w3fdif", | ||
| 616 | .p.description = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."), | ||
| 617 | .p.priv_class = &w3fdif_class, | ||
| 618 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, | ||
| 619 | .priv_size = sizeof(W3FDIFContext), | ||
| 620 | .uninit = uninit, | ||
| 621 | FILTER_INPUTS(w3fdif_inputs), | ||
| 622 | FILTER_OUTPUTS(w3fdif_outputs), | ||
| 623 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
| 624 | .process_command = ff_filter_process_command, | ||
| 625 | }; | ||
| 626 |