| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2012 Jeremy Tran | ||
| 3 | * Copyright (c) 2004 Tobias Diedrich | ||
| 4 | * Copyright (c) 2003 Donald A. Graft | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU General Public License as published by | ||
| 10 | * the Free Software Foundation; either version 2 of the License, or | ||
| 11 | * (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU General Public License along | ||
| 19 | * with FFmpeg; if not, write to the Free Software Foundation, Inc., | ||
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * Kernel Deinterlacer | ||
| 26 | * Ported from MPlayer libmpcodecs/vf_kerndeint.c. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "libavutil/imgutils.h" | ||
| 30 | #include "libavutil/intreadwrite.h" | ||
| 31 | #include "libavutil/mem.h" | ||
| 32 | #include "libavutil/opt.h" | ||
| 33 | #include "libavutil/pixdesc.h" | ||
| 34 | |||
| 35 | #include "avfilter.h" | ||
| 36 | #include "filters.h" | ||
| 37 | #include "video.h" | ||
| 38 | |||
| 39 | typedef struct KerndeintContext { | ||
| 40 | const AVClass *class; | ||
| 41 | int frame; ///< frame count, starting from 0 | ||
| 42 | int thresh, map, order, sharp, twoway; | ||
| 43 | int vsub; | ||
| 44 | int is_packed_rgb; | ||
| 45 | uint8_t *tmp_data [4]; ///< temporary plane data buffer | ||
| 46 | int tmp_linesize[4]; ///< temporary plane byte linesize | ||
| 47 | int tmp_bwidth [4]; ///< temporary plane byte width | ||
| 48 | } KerndeintContext; | ||
| 49 | |||
| 50 | #define OFFSET(x) offsetof(KerndeintContext, x) | ||
| 51 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 52 | static const AVOption kerndeint_options[] = { | ||
| 53 | { "thresh", "set the threshold", OFFSET(thresh), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS }, | ||
| 54 | { "map", "set the map", OFFSET(map), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, | ||
| 55 | { "order", "set the order", OFFSET(order), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, | ||
| 56 | { "sharp", "set sharpening", OFFSET(sharp), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, | ||
| 57 | { "twoway", "set twoway", OFFSET(twoway), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, | ||
| 58 | { NULL } | ||
| 59 | }; | ||
| 60 | |||
| 61 | AVFILTER_DEFINE_CLASS(kerndeint); | ||
| 62 | |||
| 63 | 21 | static av_cold void uninit(AVFilterContext *ctx) | |
| 64 | { | ||
| 65 | 21 | KerndeintContext *kerndeint = ctx->priv; | |
| 66 | |||
| 67 | 21 | av_freep(&kerndeint->tmp_data[0]); | |
| 68 | 21 | } | |
| 69 | |||
| 70 | static const enum AVPixelFormat pix_fmts[] = { | ||
| 71 | AV_PIX_FMT_YUV420P, | ||
| 72 | AV_PIX_FMT_YUYV422, | ||
| 73 | AV_PIX_FMT_ARGB, AV_PIX_FMT_0RGB, | ||
| 74 | AV_PIX_FMT_ABGR, AV_PIX_FMT_0BGR, | ||
| 75 | AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB0, | ||
| 76 | AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0, | ||
| 77 | AV_PIX_FMT_NONE | ||
| 78 | }; | ||
| 79 | |||
| 80 | 10 | static int config_props(AVFilterLink *inlink) | |
| 81 | { | ||
| 82 | 10 | KerndeintContext *kerndeint = inlink->dst->priv; | |
| 83 | 10 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 84 | int ret; | ||
| 85 | |||
| 86 | 10 | kerndeint->is_packed_rgb = av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_RGB; | |
| 87 | 10 | kerndeint->vsub = desc->log2_chroma_h; | |
| 88 | |||
| 89 | 10 | ret = av_image_alloc(kerndeint->tmp_data, kerndeint->tmp_linesize, | |
| 90 | 10 | inlink->w, inlink->h, inlink->format, 16); | |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (ret < 0) |
| 92 | ✗ | return ret; | |
| 93 | 10 | memset(kerndeint->tmp_data[0], 0, ret); | |
| 94 | |||
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret = av_image_fill_linesizes(kerndeint->tmp_bwidth, inlink->format, inlink->w)) < 0) |
| 96 | ✗ | return ret; | |
| 97 | |||
| 98 | 10 | return 0; | |
| 99 | } | ||
| 100 | |||
| 101 | 10 | static int filter_frame(AVFilterLink *inlink, AVFrame *inpic) | |
| 102 | { | ||
| 103 | 10 | KerndeintContext *kerndeint = inlink->dst->priv; | |
| 104 | 10 | AVFilterLink *outlink = inlink->dst->outputs[0]; | |
| 105 | AVFrame *outpic; | ||
| 106 | const uint8_t *prvp; ///< Previous field's pixel line number n | ||
| 107 | const uint8_t *prvpp; ///< Previous field's pixel line number (n - 1) | ||
| 108 | const uint8_t *prvpn; ///< Previous field's pixel line number (n + 1) | ||
| 109 | const uint8_t *prvppp; ///< Previous field's pixel line number (n - 2) | ||
| 110 | const uint8_t *prvpnn; ///< Previous field's pixel line number (n + 2) | ||
| 111 | const uint8_t *prvp4p; ///< Previous field's pixel line number (n - 4) | ||
| 112 | const uint8_t *prvp4n; ///< Previous field's pixel line number (n + 4) | ||
| 113 | |||
| 114 | const uint8_t *srcp; ///< Current field's pixel line number n | ||
| 115 | const uint8_t *srcpp; ///< Current field's pixel line number (n - 1) | ||
| 116 | const uint8_t *srcpn; ///< Current field's pixel line number (n + 1) | ||
| 117 | const uint8_t *srcppp; ///< Current field's pixel line number (n - 2) | ||
| 118 | const uint8_t *srcpnn; ///< Current field's pixel line number (n + 2) | ||
| 119 | const uint8_t *srcp3p; ///< Current field's pixel line number (n - 3) | ||
| 120 | const uint8_t *srcp3n; ///< Current field's pixel line number (n + 3) | ||
| 121 | const uint8_t *srcp4p; ///< Current field's pixel line number (n - 4) | ||
| 122 | const uint8_t *srcp4n; ///< Current field's pixel line number (n + 4) | ||
| 123 | |||
| 124 | uint8_t *dstp, *dstp_saved; | ||
| 125 | const uint8_t *srcp_saved; | ||
| 126 | |||
| 127 | int src_linesize, psrc_linesize, dst_linesize, bwidth; | ||
| 128 | 10 | int x, y, plane, val, hi, lo, g, h, n = kerndeint->frame++; | |
| 129 | double valf; | ||
| 130 | |||
| 131 | 10 | const int thresh = kerndeint->thresh; | |
| 132 | 10 | const int order = kerndeint->order; | |
| 133 | 10 | const int map = kerndeint->map; | |
| 134 | 10 | const int sharp = kerndeint->sharp; | |
| 135 | 10 | const int twoway = kerndeint->twoway; | |
| 136 | |||
| 137 | 10 | const int is_packed_rgb = kerndeint->is_packed_rgb; | |
| 138 | |||
| 139 | 10 | outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!outpic) { |
| 141 | ✗ | av_frame_free(&inpic); | |
| 142 | ✗ | return AVERROR(ENOMEM); | |
| 143 | } | ||
| 144 | 10 | av_frame_copy_props(outpic, inpic); | |
| 145 | 10 | outpic->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
| 146 | |||
| 147 |
4/6✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
22 | for (plane = 0; plane < 4 && inpic->data[plane] && inpic->linesize[plane]; plane++) { |
| 148 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | h = plane == 0 ? inlink->h : AV_CEIL_RSHIFT(inlink->h, kerndeint->vsub); |
| 149 | 12 | bwidth = kerndeint->tmp_bwidth[plane]; | |
| 150 | |||
| 151 | 12 | srcp_saved = inpic->data[plane]; | |
| 152 | 12 | src_linesize = inpic->linesize[plane]; | |
| 153 | 12 | psrc_linesize = kerndeint->tmp_linesize[plane]; | |
| 154 | 12 | dstp_saved = outpic->data[plane]; | |
| 155 | 12 | dst_linesize = outpic->linesize[plane]; | |
| 156 | 12 | srcp = srcp_saved + (1 - order) * src_linesize; | |
| 157 | 12 | dstp = dstp_saved + (1 - order) * dst_linesize; | |
| 158 | |||
| 159 |
2/2✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 12 times.
|
1596 | for (y = 0; y < h; y += 2) { |
| 160 | 1584 | memcpy(dstp, srcp, bwidth); | |
| 161 | 1584 | srcp += 2 * src_linesize; | |
| 162 | 1584 | dstp += 2 * dst_linesize; | |
| 163 | } | ||
| 164 | |||
| 165 | // Copy through the lines that will be missed below. | ||
| 166 | 12 | memcpy(dstp_saved + order * dst_linesize, srcp_saved + (1 - order) * src_linesize, bwidth); | |
| 167 | 12 | memcpy(dstp_saved + (2 + order ) * dst_linesize, srcp_saved + (3 - order) * src_linesize, bwidth); | |
| 168 | 12 | memcpy(dstp_saved + (h - 2 + order) * dst_linesize, srcp_saved + (h - 1 - order) * src_linesize, bwidth); | |
| 169 | 12 | memcpy(dstp_saved + (h - 4 + order) * dst_linesize, srcp_saved + (h - 3 - order) * src_linesize, bwidth); | |
| 170 | |||
| 171 | /* For the other field choose adaptively between using the previous field | ||
| 172 | or the interpolant from the current field. */ | ||
| 173 | 12 | prvp = kerndeint->tmp_data[plane] + 5 * psrc_linesize - (1 - order) * psrc_linesize; | |
| 174 | 12 | prvpp = prvp - psrc_linesize; | |
| 175 | 12 | prvppp = prvp - 2 * psrc_linesize; | |
| 176 | 12 | prvp4p = prvp - 4 * psrc_linesize; | |
| 177 | 12 | prvpn = prvp + psrc_linesize; | |
| 178 | 12 | prvpnn = prvp + 2 * psrc_linesize; | |
| 179 | 12 | prvp4n = prvp + 4 * psrc_linesize; | |
| 180 | |||
| 181 | 12 | srcp = srcp_saved + 5 * src_linesize - (1 - order) * src_linesize; | |
| 182 | 12 | srcpp = srcp - src_linesize; | |
| 183 | 12 | srcppp = srcp - 2 * src_linesize; | |
| 184 | 12 | srcp3p = srcp - 3 * src_linesize; | |
| 185 | 12 | srcp4p = srcp - 4 * src_linesize; | |
| 186 | |||
| 187 | 12 | srcpn = srcp + src_linesize; | |
| 188 | 12 | srcpnn = srcp + 2 * src_linesize; | |
| 189 | 12 | srcp3n = srcp + 3 * src_linesize; | |
| 190 | 12 | srcp4n = srcp + 4 * src_linesize; | |
| 191 | |||
| 192 | 12 | dstp = dstp_saved + 5 * dst_linesize - (1 - order) * dst_linesize; | |
| 193 | |||
| 194 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 12 times.
|
1548 | for (y = 5 - (1 - order); y <= h - 5 - (1 - order); y += 2) { |
| 195 |
2/2✓ Branch 0 taken 1748736 times.
✓ Branch 1 taken 1536 times.
|
1750272 | for (x = 0; x < bwidth; x++) { |
| 196 |
2/4✓ Branch 0 taken 1748736 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1748736 times.
|
1748736 | if (thresh == 0 || n == 0 || |
| 197 | ✗ | (abs((int)prvp[x] - (int)srcp[x]) > thresh) || | |
| 198 | ✗ | (abs((int)prvpp[x] - (int)srcpp[x]) > thresh) || | |
| 199 | ✗ | (abs((int)prvpn[x] - (int)srcpn[x]) > thresh)) { | |
| 200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1748736 times.
|
1748736 | if (map) { |
| 201 | ✗ | g = x & ~3; | |
| 202 | |||
| 203 | ✗ | if (is_packed_rgb) { | |
| 204 | ✗ | AV_WB32(dstp + g, 0xffffffff); | |
| 205 | ✗ | x = g + 3; | |
| 206 | ✗ | } else if (inlink->format == AV_PIX_FMT_YUYV422) { | |
| 207 | // y <- 235, u <- 128, y <- 235, v <- 128 | ||
| 208 | ✗ | AV_WB32(dstp + g, 0xeb80eb80); | |
| 209 | ✗ | x = g + 3; | |
| 210 | } else { | ||
| 211 | ✗ | dstp[x] = plane == 0 ? 235 : 128; | |
| 212 | } | ||
| 213 | } else { | ||
| 214 |
2/2✓ Branch 0 taken 1576960 times.
✓ Branch 1 taken 171776 times.
|
1748736 | if (is_packed_rgb) { |
| 215 | 1576960 | hi = 255; | |
| 216 | 1576960 | lo = 0; | |
| 217 |
2/2✓ Branch 0 taken 98560 times.
✓ Branch 1 taken 73216 times.
|
171776 | } else if (inlink->format == AV_PIX_FMT_YUYV422) { |
| 218 |
2/2✓ Branch 0 taken 49280 times.
✓ Branch 1 taken 49280 times.
|
98560 | hi = x & 1 ? 240 : 235; |
| 219 | 98560 | lo = 16; | |
| 220 | } else { | ||
| 221 |
2/2✓ Branch 0 taken 49280 times.
✓ Branch 1 taken 23936 times.
|
73216 | hi = plane == 0 ? 235 : 240; |
| 222 | 73216 | lo = 16; | |
| 223 | } | ||
| 224 | |||
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1748736 times.
|
1748736 | if (sharp) { |
| 226 | ✗ | if (twoway) { | |
| 227 | ✗ | valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x]) | |
| 228 | ✗ | + 0.170 * ((int)srcp[x] + (int)prvp[x]) | |
| 229 | ✗ | - 0.116 * ((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x]) | |
| 230 | ✗ | - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x]) | |
| 231 | ✗ | + 0.031 * ((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]); | |
| 232 | } else { | ||
| 233 | ✗ | valf = + 0.526 * ((int)srcpp[x] + (int)srcpn[x]) | |
| 234 | ✗ | + 0.170 * ((int)prvp[x]) | |
| 235 | ✗ | - 0.116 * ((int)prvppp[x] + (int)prvpnn[x]) | |
| 236 | ✗ | - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x]) | |
| 237 | ✗ | + 0.031 * ((int)prvp4p[x] + (int)prvp4p[x]); | |
| 238 | } | ||
| 239 | ✗ | dstp[x] = av_clip(valf, lo, hi); | |
| 240 | } else { | ||
| 241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1748736 times.
|
1748736 | if (twoway) { |
| 242 | ✗ | val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)srcp[x] + (int)prvp[x]) | |
| 243 | ✗ | - (int)(srcppp[x]) - (int)(srcpnn[x]) | |
| 244 | ✗ | - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4; | |
| 245 | } else { | ||
| 246 | 1748736 | val = (8 * ((int)srcpp[x] + (int)srcpn[x]) + 2 * ((int)prvp[x]) | |
| 247 | 1748736 | - (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4; | |
| 248 | } | ||
| 249 | 1748736 | dstp[x] = av_clip(val, lo, hi); | |
| 250 | } | ||
| 251 | } | ||
| 252 | } else { | ||
| 253 | ✗ | dstp[x] = srcp[x]; | |
| 254 | } | ||
| 255 | } | ||
| 256 | 1536 | prvp += 2 * psrc_linesize; | |
| 257 | 1536 | prvpp += 2 * psrc_linesize; | |
| 258 | 1536 | prvppp += 2 * psrc_linesize; | |
| 259 | 1536 | prvpn += 2 * psrc_linesize; | |
| 260 | 1536 | prvpnn += 2 * psrc_linesize; | |
| 261 | 1536 | prvp4p += 2 * psrc_linesize; | |
| 262 | 1536 | prvp4n += 2 * psrc_linesize; | |
| 263 | 1536 | srcp += 2 * src_linesize; | |
| 264 | 1536 | srcpp += 2 * src_linesize; | |
| 265 | 1536 | srcppp += 2 * src_linesize; | |
| 266 | 1536 | srcp3p += 2 * src_linesize; | |
| 267 | 1536 | srcp4p += 2 * src_linesize; | |
| 268 | 1536 | srcpn += 2 * src_linesize; | |
| 269 | 1536 | srcpnn += 2 * src_linesize; | |
| 270 | 1536 | srcp3n += 2 * src_linesize; | |
| 271 | 1536 | srcp4n += 2 * src_linesize; | |
| 272 | 1536 | dstp += 2 * dst_linesize; | |
| 273 | } | ||
| 274 | |||
| 275 | 12 | srcp = inpic->data[plane]; | |
| 276 | 12 | dstp = kerndeint->tmp_data[plane]; | |
| 277 | 12 | av_image_copy_plane(dstp, psrc_linesize, srcp, src_linesize, bwidth, h); | |
| 278 | } | ||
| 279 | |||
| 280 | 10 | av_frame_free(&inpic); | |
| 281 | 10 | return ff_filter_frame(outlink, outpic); | |
| 282 | } | ||
| 283 | |||
| 284 | static const AVFilterPad kerndeint_inputs[] = { | ||
| 285 | { | ||
| 286 | .name = "default", | ||
| 287 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 288 | .filter_frame = filter_frame, | ||
| 289 | .config_props = config_props, | ||
| 290 | }, | ||
| 291 | }; | ||
| 292 | |||
| 293 | |||
| 294 | const FFFilter ff_vf_kerndeint = { | ||
| 295 | .p.name = "kerndeint", | ||
| 296 | .p.description = NULL_IF_CONFIG_SMALL("Apply kernel deinterlacing to the input."), | ||
| 297 | .p.priv_class = &kerndeint_class, | ||
| 298 | .priv_size = sizeof(KerndeintContext), | ||
| 299 | .uninit = uninit, | ||
| 300 | FILTER_INPUTS(kerndeint_inputs), | ||
| 301 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 302 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
| 303 | }; | ||
| 304 |