| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include "libavutil/opt.h" | ||
| 20 | #include "libavutil/pixdesc.h" | ||
| 21 | |||
| 22 | #include "filters.h" | ||
| 23 | #include "video.h" | ||
| 24 | |||
| 25 | typedef struct EPXContext { | ||
| 26 | const AVClass *class; | ||
| 27 | |||
| 28 | int n; | ||
| 29 | |||
| 30 | int (*epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); | ||
| 31 | } EPXContext; | ||
| 32 | |||
| 33 | typedef struct ThreadData { | ||
| 34 | AVFrame *in, *out; | ||
| 35 | } ThreadData; | ||
| 36 | |||
| 37 | #define OFFSET(x) offsetof(EPXContext, x) | ||
| 38 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
| 39 | static const AVOption epx_options[] = { | ||
| 40 | { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, .flags = FLAGS }, | ||
| 41 | { NULL } | ||
| 42 | }; | ||
| 43 | |||
| 44 | AVFILTER_DEFINE_CLASS(epx); | ||
| 45 | |||
| 46 | 18 | static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 47 | { | ||
| 48 | 18 | ThreadData *td = arg; | |
| 49 | 18 | const AVFrame *in = td->in; | |
| 50 | 18 | AVFrame *out = td->out; | |
| 51 | 18 | const int slice_start = (in->height * jobnr ) / nb_jobs; | |
| 52 | 18 | const int slice_end = (in->height * (jobnr+1)) / nb_jobs; | |
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | for (int p = 0; p < 1; p++) { |
| 55 | 18 | const int width = in->width; | |
| 56 | 18 | const int height = in->height; | |
| 57 | 18 | const int src_linesize = in->linesize[p] / 4; | |
| 58 | 18 | const int dst_linesize = out->linesize[p] / 4; | |
| 59 | 18 | const uint32_t *src = (const uint32_t *)in->data[p]; | |
| 60 | 18 | uint32_t *dst = (uint32_t *)out->data[p]; | |
| 61 | const uint32_t *src_line[3]; | ||
| 62 | |||
| 63 | 18 | src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0); | |
| 64 | 18 | src_line[1] = src + src_linesize * slice_start; | |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1); |
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 518 times.
✓ Branch 1 taken 18 times.
|
536 | for (int y = slice_start; y < slice_end; y++) { |
| 68 | uint32_t *dst_line[2]; | ||
| 69 | |||
| 70 | 518 | dst_line[0] = dst + dst_linesize*2*y; | |
| 71 | 518 | dst_line[1] = dst + dst_linesize*(2*y+1); | |
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 98938 times.
✓ Branch 1 taken 518 times.
|
99456 | for (int x = 0; x < width; x++) { |
| 74 | uint32_t E0, E1, E2, E3; | ||
| 75 | uint32_t B, D, E, F, H; | ||
| 76 | |||
| 77 | 98938 | B = src_line[0][x]; | |
| 78 |
2/2✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
|
98938 | D = src_line[1][FFMAX(x-1, 0)]; |
| 79 | 98938 | E = src_line[1][x]; | |
| 80 |
2/2✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
|
98938 | F = src_line[1][FFMIN(x+1, width - 1)]; |
| 81 | 98938 | H = src_line[2][x]; | |
| 82 | |||
| 83 |
4/4✓ Branch 0 taken 38981 times.
✓ Branch 1 taken 59957 times.
✓ Branch 2 taken 23048 times.
✓ Branch 3 taken 15933 times.
|
98938 | if (B != H && D != F) { |
| 84 |
2/2✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
|
23048 | E0 = D == B ? D : E; |
| 85 |
2/2✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
|
23048 | E1 = B == F ? F : E; |
| 86 |
2/2✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
|
23048 | E2 = D == H ? D : E; |
| 87 |
2/2✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 15399 times.
|
23048 | E3 = H == F ? F : E; |
| 88 | } else { | ||
| 89 | 75890 | E0 = E; | |
| 90 | 75890 | E1 = E; | |
| 91 | 75890 | E2 = E; | |
| 92 | 75890 | E3 = E; | |
| 93 | } | ||
| 94 | |||
| 95 | 98938 | dst_line[0][x*2] = E0; | |
| 96 | 98938 | dst_line[0][x*2+1] = E1; | |
| 97 | 98938 | dst_line[1][x*2] = E2; | |
| 98 | 98938 | dst_line[1][x*2+1] = E3; | |
| 99 | } | ||
| 100 | |||
| 101 | 518 | src_line[0] = src_line[1]; | |
| 102 | 518 | src_line[1] = src_line[2]; | |
| 103 | 518 | src_line[2] = src_line[1]; | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 514 times.
✓ Branch 1 taken 4 times.
|
518 | if (y < height - 2) |
| 106 | 514 | src_line[2] += src_linesize; | |
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | 18 | return 0; | |
| 111 | } | ||
| 112 | |||
| 113 | 18 | static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 114 | { | ||
| 115 | 18 | ThreadData *td = arg; | |
| 116 | 18 | const AVFrame *in = td->in; | |
| 117 | 18 | AVFrame *out = td->out; | |
| 118 | 18 | const int slice_start = (in->height * jobnr ) / nb_jobs; | |
| 119 | 18 | const int slice_end = (in->height * (jobnr+1)) / nb_jobs; | |
| 120 | |||
| 121 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | for (int p = 0; p < 1; p++) { |
| 122 | 18 | const int width = in->width; | |
| 123 | 18 | const int height = in->height; | |
| 124 | 18 | const int src_linesize = in->linesize[p] / 4; | |
| 125 | 18 | const int dst_linesize = out->linesize[p] / 4; | |
| 126 | 18 | const uint32_t *src = (const uint32_t *)in->data[p]; | |
| 127 | 18 | uint32_t *dst = (uint32_t *)out->data[p]; | |
| 128 | const uint32_t *src_line[3]; | ||
| 129 | |||
| 130 | 18 | src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0); | |
| 131 | 18 | src_line[1] = src + src_linesize * slice_start; | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1); |
| 133 | |||
| 134 |
2/2✓ Branch 0 taken 518 times.
✓ Branch 1 taken 18 times.
|
536 | for (int y = slice_start; y < slice_end; y++) { |
| 135 | uint32_t *dst_line[3]; | ||
| 136 | |||
| 137 | 518 | dst_line[0] = dst + dst_linesize*3*y; | |
| 138 | 518 | dst_line[1] = dst + dst_linesize*(3*y+1); | |
| 139 | 518 | dst_line[2] = dst + dst_linesize*(3*y+2); | |
| 140 | |||
| 141 |
2/2✓ Branch 0 taken 98938 times.
✓ Branch 1 taken 518 times.
|
99456 | for (int x = 0; x < width; x++) { |
| 142 | uint32_t E0, E1, E2, E3, E4, E5, E6, E7, E8; | ||
| 143 | uint32_t A, B, C, D, E, F, G, H, I; | ||
| 144 | |||
| 145 |
2/2✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
|
98938 | A = src_line[0][FFMAX(x-1, 0)]; |
| 146 | 98938 | B = src_line[0][x]; | |
| 147 |
2/2✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
|
98938 | C = src_line[0][FFMIN(x+1, width - 1)]; |
| 148 |
2/2✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
|
98938 | D = src_line[1][FFMAX(x-1, 0)]; |
| 149 | 98938 | E = src_line[1][x]; | |
| 150 |
2/2✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
|
98938 | F = src_line[1][FFMIN(x+1, width - 1)]; |
| 151 |
2/2✓ Branch 0 taken 97902 times.
✓ Branch 1 taken 1036 times.
|
98938 | G = src_line[2][FFMAX(x-1, 0)]; |
| 152 | 98938 | H = src_line[2][x]; | |
| 153 |
2/2✓ Branch 0 taken 518 times.
✓ Branch 1 taken 98420 times.
|
98938 | I = src_line[2][FFMIN(x+1, width - 1)]; |
| 154 | |||
| 155 |
4/4✓ Branch 0 taken 38981 times.
✓ Branch 1 taken 59957 times.
✓ Branch 2 taken 23048 times.
✓ Branch 3 taken 15933 times.
|
98938 | if (B != H && D != F) { |
| 156 |
2/2✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
|
23048 | E0 = D == B ? D : E; |
| 157 |
8/8✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
✓ Branch 2 taken 4088 times.
✓ Branch 3 taken 3555 times.
✓ Branch 4 taken 6676 times.
✓ Branch 5 taken 12817 times.
✓ Branch 6 taken 3280 times.
✓ Branch 7 taken 3396 times.
|
23048 | E1 = (D == B && E != C) || (B == F && E != A) ? B : E; |
| 158 |
2/2✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
|
23048 | E2 = B == F ? F : E; |
| 159 |
8/8✓ Branch 0 taken 7643 times.
✓ Branch 1 taken 15405 times.
✓ Branch 2 taken 4026 times.
✓ Branch 3 taken 3617 times.
✓ Branch 4 taken 6524 times.
✓ Branch 5 taken 12907 times.
✓ Branch 6 taken 3140 times.
✓ Branch 7 taken 3384 times.
|
23048 | E3 = (D == B && E != G) || (D == H && E != A) ? D : E; |
| 160 | 23048 | E4 = E; | |
| 161 |
8/8✓ Branch 0 taken 6676 times.
✓ Branch 1 taken 16372 times.
✓ Branch 2 taken 3412 times.
✓ Branch 3 taken 3264 times.
✓ Branch 4 taken 7649 times.
✓ Branch 5 taken 12135 times.
✓ Branch 6 taken 3610 times.
✓ Branch 7 taken 4039 times.
|
23048 | E5 = (B == F && E != I) || (H == F && E != C) ? F : E; |
| 162 |
2/2✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
|
23048 | E6 = D == H ? D : E; |
| 163 |
8/8✓ Branch 0 taken 6524 times.
✓ Branch 1 taken 16524 times.
✓ Branch 2 taken 3296 times.
✓ Branch 3 taken 3228 times.
✓ Branch 4 taken 7649 times.
✓ Branch 5 taken 12171 times.
✓ Branch 6 taken 3594 times.
✓ Branch 7 taken 4055 times.
|
23048 | E7 = (D == H && E != I) || (H == F && E != G) ? H : E; |
| 164 |
2/2✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 15399 times.
|
23048 | E8 = H == F ? F : E; |
| 165 | } else { | ||
| 166 | 75890 | E0 = E; | |
| 167 | 75890 | E1 = E; | |
| 168 | 75890 | E2 = E; | |
| 169 | 75890 | E3 = E; | |
| 170 | 75890 | E4 = E; | |
| 171 | 75890 | E5 = E; | |
| 172 | 75890 | E6 = E; | |
| 173 | 75890 | E7 = E; | |
| 174 | 75890 | E8 = E; | |
| 175 | } | ||
| 176 | |||
| 177 | 98938 | dst_line[0][x*3] = E0; | |
| 178 | 98938 | dst_line[0][x*3+1] = E1; | |
| 179 | 98938 | dst_line[0][x*3+2] = E2; | |
| 180 | 98938 | dst_line[1][x*3] = E3; | |
| 181 | 98938 | dst_line[1][x*3+1] = E4; | |
| 182 | 98938 | dst_line[1][x*3+2] = E5; | |
| 183 | 98938 | dst_line[2][x*3] = E6; | |
| 184 | 98938 | dst_line[2][x*3+1] = E7; | |
| 185 | 98938 | dst_line[2][x*3+2] = E8; | |
| 186 | } | ||
| 187 | |||
| 188 | 518 | src_line[0] = src_line[1]; | |
| 189 | 518 | src_line[1] = src_line[2]; | |
| 190 | 518 | src_line[2] = src_line[1]; | |
| 191 | |||
| 192 |
2/2✓ Branch 0 taken 514 times.
✓ Branch 1 taken 4 times.
|
518 | if (y < height - 2) |
| 193 | 514 | src_line[2] += src_linesize; | |
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | 18 | return 0; | |
| 198 | } | ||
| 199 | |||
| 200 | 4 | static int config_output(AVFilterLink *outlink) | |
| 201 | { | ||
| 202 | 4 | AVFilterContext *ctx = outlink->src; | |
| 203 | 4 | EPXContext *s = ctx->priv; | |
| 204 | 4 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 205 | const AVPixFmtDescriptor *desc; | ||
| 206 | |||
| 207 | 4 | desc = av_pix_fmt_desc_get(outlink->format); | |
| 208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!desc) |
| 209 | ✗ | return AVERROR_BUG; | |
| 210 | |||
| 211 | 4 | outlink->w = inlink->w * s->n; | |
| 212 | 4 | outlink->h = inlink->h * s->n; | |
| 213 | |||
| 214 |
2/3✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | switch (s->n) { |
| 215 | 2 | case 2: | |
| 216 | 2 | s->epx_slice = epx2_slice; | |
| 217 | 2 | break; | |
| 218 | 2 | case 3: | |
| 219 | 2 | s->epx_slice = epx3_slice; | |
| 220 | 2 | break; | |
| 221 | } | ||
| 222 | |||
| 223 | 4 | return 0; | |
| 224 | } | ||
| 225 | |||
| 226 | static const enum AVPixelFormat pix_fmts[] = { | ||
| 227 | AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, | ||
| 228 | AV_PIX_FMT_NONE, | ||
| 229 | }; | ||
| 230 | |||
| 231 | 4 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 232 | { | ||
| 233 | 4 | AVFilterContext *ctx = inlink->dst; | |
| 234 | 4 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 235 | 4 | EPXContext *s = ctx->priv; | |
| 236 | ThreadData td; | ||
| 237 | |||
| 238 | 4 | AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!out) { |
| 240 | ✗ | av_frame_free(&in); | |
| 241 | ✗ | return AVERROR(ENOMEM); | |
| 242 | } | ||
| 243 | |||
| 244 | 4 | av_frame_copy_props(out, in); | |
| 245 | |||
| 246 | 4 | td.in = in, td.out = out; | |
| 247 | 4 | ff_filter_execute(ctx, s->epx_slice, &td, NULL, | |
| 248 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | FFMIN(inlink->h, ff_filter_get_nb_threads(ctx))); |
| 249 | |||
| 250 | 4 | av_frame_free(&in); | |
| 251 | 4 | return ff_filter_frame(outlink, out); | |
| 252 | } | ||
| 253 | |||
| 254 | static const AVFilterPad inputs[] = { | ||
| 255 | { | ||
| 256 | .name = "default", | ||
| 257 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 258 | .filter_frame = filter_frame, | ||
| 259 | }, | ||
| 260 | }; | ||
| 261 | |||
| 262 | static const AVFilterPad outputs[] = { | ||
| 263 | { | ||
| 264 | .name = "default", | ||
| 265 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 266 | .config_props = config_output, | ||
| 267 | }, | ||
| 268 | }; | ||
| 269 | |||
| 270 | const FFFilter ff_vf_epx = { | ||
| 271 | .p.name = "epx", | ||
| 272 | .p.description = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."), | ||
| 273 | .p.priv_class = &epx_class, | ||
| 274 | .p.flags = AVFILTER_FLAG_SLICE_THREADS, | ||
| 275 | FILTER_INPUTS(inputs), | ||
| 276 | FILTER_OUTPUTS(outputs), | ||
| 277 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
| 278 | .priv_size = sizeof(EPXContext), | ||
| 279 | }; | ||
| 280 |