2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/opt.h"
20 #include "libavutil/avassert.h"
21 #include "libavutil/pixdesc.h"
24 typedef struct EPXContext {
29 int (*epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
32 typedef struct ThreadData {
36 #define OFFSET(x) offsetof(EPXContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
38 static const AVOption epx_options[] = {
39 { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, .flags = FLAGS },
43 AVFILTER_DEFINE_CLASS(epx);
45 static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
48 const AVFrame *in = td->in;
49 AVFrame *out = td->out;
50 const int slice_start = (in->height * jobnr ) / nb_jobs;
51 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
53 for (int p = 0; p < 1; p++) {
54 const int width = in->width;
55 const int height = in->height;
56 const int src_linesize = in->linesize[p] / 4;
57 const int dst_linesize = out->linesize[p] / 4;
58 const uint32_t *src = (const uint32_t *)in->data[p];
59 uint32_t *dst = (uint32_t *)out->data[p];
60 const uint32_t *src_line[3];
62 src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
63 src_line[1] = src + src_linesize * slice_start;
64 src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
66 for (int y = slice_start; y < slice_end; y++) {
67 uint32_t *dst_line[2];
69 dst_line[0] = dst + dst_linesize*2*y;
70 dst_line[1] = dst + dst_linesize*(2*y+1);
72 for (int x = 0; x < width; x++) {
73 uint32_t E0, E1, E2, E3;
74 uint32_t B, D, E, F, H;
77 D = src_line[1][FFMAX(x-1, 0)];
79 F = src_line[1][FFMIN(x+1, width - 1)];
82 if (B != H && D != F) {
94 dst_line[0][x*2] = E0;
95 dst_line[0][x*2+1] = E1;
96 dst_line[1][x*2] = E2;
97 dst_line[1][x*2+1] = E3;
100 src_line[0] = src_line[1];
101 src_line[1] = src_line[2];
102 src_line[2] = src_line[1];
105 src_line[2] += src_linesize;
112 static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
114 ThreadData *td = arg;
115 const AVFrame *in = td->in;
116 AVFrame *out = td->out;
117 const int slice_start = (in->height * jobnr ) / nb_jobs;
118 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
120 for (int p = 0; p < 1; p++) {
121 const int width = in->width;
122 const int height = in->height;
123 const int src_linesize = in->linesize[p] / 4;
124 const int dst_linesize = out->linesize[p] / 4;
125 const uint32_t *src = (const uint32_t *)in->data[p];
126 uint32_t *dst = (uint32_t *)out->data[p];
127 const uint32_t *src_line[3];
129 src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
130 src_line[1] = src + src_linesize * slice_start;
131 src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
133 for (int y = slice_start; y < slice_end; y++) {
134 uint32_t *dst_line[3];
136 dst_line[0] = dst + dst_linesize*3*y;
137 dst_line[1] = dst + dst_linesize*(3*y+1);
138 dst_line[2] = dst + dst_linesize*(3*y+2);
140 for (int x = 0; x < width; x++) {
141 uint32_t E0, E1, E2, E3, E4, E5, E6, E7, E8;
142 uint32_t A, B, C, D, E, F, G, H, I;
144 A = src_line[0][FFMAX(x-1, 0)];
146 C = src_line[0][FFMIN(x+1, width - 1)];
147 D = src_line[1][FFMAX(x-1, 0)];
149 F = src_line[1][FFMIN(x+1, width - 1)];
150 G = src_line[2][FFMAX(x-1, 0)];
152 I = src_line[2][FFMIN(x+1, width - 1)];
154 if (B != H && D != F) {
156 E1 = (D == B && E != C) || (B == F && E != A) ? B : E;
158 E3 = (D == B && E != G) || (D == H && E != A) ? D : E;
160 E5 = (B == F && E != I) || (H == F && E != C) ? F : E;
162 E7 = (D == H && E != I) || (H == F && E != G) ? H : E;
176 dst_line[0][x*3] = E0;
177 dst_line[0][x*3+1] = E1;
178 dst_line[0][x*3+2] = E2;
179 dst_line[1][x*3] = E3;
180 dst_line[1][x*3+1] = E4;
181 dst_line[1][x*3+2] = E5;
182 dst_line[2][x*3] = E6;
183 dst_line[2][x*3+1] = E7;
184 dst_line[2][x*3+2] = E8;
187 src_line[0] = src_line[1];
188 src_line[1] = src_line[2];
189 src_line[2] = src_line[1];
192 src_line[2] += src_linesize;
199 static int config_output(AVFilterLink *outlink)
201 AVFilterContext *ctx = outlink->src;
202 EPXContext *s = ctx->priv;
203 AVFilterLink *inlink = ctx->inputs[0];
204 const AVPixFmtDescriptor *desc;
206 desc = av_pix_fmt_desc_get(outlink->format);
210 outlink->w = inlink->w * s->n;
211 outlink->h = inlink->h * s->n;
215 s->epx_slice = epx2_slice;
218 s->epx_slice = epx3_slice;
225 static int query_formats(AVFilterContext *ctx)
227 static const enum AVPixelFormat pix_fmts[] = {
228 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
232 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
234 return AVERROR(ENOMEM);
235 return ff_set_common_formats(ctx, fmts_list);
238 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
240 AVFilterContext *ctx = inlink->dst;
241 AVFilterLink *outlink = ctx->outputs[0];
242 EPXContext *s = ctx->priv;
245 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
248 return AVERROR(ENOMEM);
251 av_frame_copy_props(out, in);
253 td.in = in, td.out = out;
254 ctx->internal->execute(ctx, s->epx_slice, &td, NULL, FFMIN(inlink->h, ff_filter_get_nb_threads(ctx)));
257 return ff_filter_frame(outlink, out);
260 static const AVFilterPad inputs[] = {
263 .type = AVMEDIA_TYPE_VIDEO,
264 .filter_frame = filter_frame,
269 static const AVFilterPad outputs[] = {
272 .type = AVMEDIA_TYPE_VIDEO,
273 .config_props = config_output,
278 AVFilter ff_vf_epx = {
280 .description = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."),
283 .query_formats = query_formats,
284 .priv_size = sizeof(EPXContext),
285 .priv_class = &epx_class,
286 .flags = AVFILTER_FLAG_SLICE_THREADS,