• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "internal.h"
22 
23 typedef struct EPXContext {
24     const AVClass *class;
25 
26     int n;
27 
28     int (*epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
29 } EPXContext;
30 
31 typedef struct ThreadData {
32     AVFrame *in, *out;
33 } ThreadData;
34 
35 #define OFFSET(x) offsetof(EPXContext, x)
36 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
37 static const AVOption epx_options[] = {
38     { "n", "set scale factor", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, .flags = FLAGS },
39     { NULL }
40 };
41 
42 AVFILTER_DEFINE_CLASS(epx);
43 
epx2_slice(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)44 static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
45 {
46     ThreadData *td = arg;
47     const AVFrame *in = td->in;
48     AVFrame *out = td->out;
49     const int slice_start = (in->height *  jobnr   ) / nb_jobs;
50     const int slice_end   = (in->height * (jobnr+1)) / nb_jobs;
51 
52     for (int p = 0; p < 1; p++) {
53         const int width = in->width;
54         const int height = in->height;
55         const int src_linesize = in->linesize[p] / 4;
56         const int dst_linesize = out->linesize[p] / 4;
57         const uint32_t *src = (const uint32_t *)in->data[p];
58         uint32_t *dst = (uint32_t *)out->data[p];
59         const uint32_t *src_line[3];
60 
61         src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
62         src_line[1] = src + src_linesize * slice_start;
63         src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
64 
65         for (int y = slice_start; y < slice_end; y++) {
66             uint32_t *dst_line[2];
67 
68             dst_line[0] = dst + dst_linesize*2*y;
69             dst_line[1] = dst + dst_linesize*(2*y+1);
70 
71             for (int x = 0; x < width; x++) {
72                 uint32_t E0, E1, E2, E3;
73                 uint32_t B, D, E, F, H;
74 
75                 B = src_line[0][x];
76                 D = src_line[1][FFMAX(x-1, 0)];
77                 E = src_line[1][x];
78                 F = src_line[1][FFMIN(x+1, width - 1)];
79                 H = src_line[2][x];
80 
81                 if (B != H && D != F) {
82                     E0 = D == B ? D : E;
83                     E1 = B == F ? F : E;
84                     E2 = D == H ? D : E;
85                     E3 = H == F ? F : E;
86                 } else {
87                     E0 = E;
88                     E1 = E;
89                     E2 = E;
90                     E3 = E;
91                 }
92 
93                 dst_line[0][x*2]   = E0;
94                 dst_line[0][x*2+1] = E1;
95                 dst_line[1][x*2]   = E2;
96                 dst_line[1][x*2+1] = E3;
97             }
98 
99             src_line[0] = src_line[1];
100             src_line[1] = src_line[2];
101             src_line[2] = src_line[1];
102 
103             if (y < height - 2)
104                 src_line[2] += src_linesize;
105         }
106     }
107 
108     return 0;
109 }
110 
epx3_slice(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)111 static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
112 {
113     ThreadData *td = arg;
114     const AVFrame *in = td->in;
115     AVFrame *out = td->out;
116     const int slice_start = (in->height *  jobnr   ) / nb_jobs;
117     const int slice_end   = (in->height * (jobnr+1)) / nb_jobs;
118 
119     for (int p = 0; p < 1; p++) {
120         const int width = in->width;
121         const int height = in->height;
122         const int src_linesize = in->linesize[p] / 4;
123         const int dst_linesize = out->linesize[p] / 4;
124         const uint32_t *src = (const uint32_t *)in->data[p];
125         uint32_t *dst = (uint32_t *)out->data[p];
126         const uint32_t *src_line[3];
127 
128         src_line[0] = src + src_linesize * FFMAX(slice_start - 1, 0);
129         src_line[1] = src + src_linesize * slice_start;
130         src_line[2] = src + src_linesize * FFMIN(slice_start + 1, height-1);
131 
132         for (int y = slice_start; y < slice_end; y++) {
133             uint32_t *dst_line[3];
134 
135             dst_line[0] = dst + dst_linesize*3*y;
136             dst_line[1] = dst + dst_linesize*(3*y+1);
137             dst_line[2] = dst + dst_linesize*(3*y+2);
138 
139             for (int x = 0; x < width; x++) {
140                 uint32_t E0, E1, E2, E3, E4, E5, E6, E7, E8;
141                 uint32_t A, B, C, D, E, F, G, H, I;
142 
143                 A = src_line[0][FFMAX(x-1, 0)];
144                 B = src_line[0][x];
145                 C = src_line[0][FFMIN(x+1, width - 1)];
146                 D = src_line[1][FFMAX(x-1, 0)];
147                 E = src_line[1][x];
148                 F = src_line[1][FFMIN(x+1, width - 1)];
149                 G = src_line[2][FFMAX(x-1, 0)];
150                 H = src_line[2][x];
151                 I = src_line[2][FFMIN(x+1, width - 1)];
152 
153                 if (B != H && D != F) {
154                     E0 = D == B ? D : E;
155                     E1 = (D == B && E != C) || (B == F && E != A) ? B : E;
156                     E2 = B == F ? F : E;
157                     E3 = (D == B && E != G) || (D == H && E != A) ? D : E;
158                     E4 = E;
159                     E5 = (B == F && E != I) || (H == F && E != C) ? F : E;
160                     E6 = D == H ? D : E;
161                     E7 = (D == H && E != I) || (H == F && E != G) ? H : E;
162                     E8 = H == F ? F : E;
163                 } else {
164                     E0 = E;
165                     E1 = E;
166                     E2 = E;
167                     E3 = E;
168                     E4 = E;
169                     E5 = E;
170                     E6 = E;
171                     E7 = E;
172                     E8 = E;
173                 }
174 
175                 dst_line[0][x*3]   = E0;
176                 dst_line[0][x*3+1] = E1;
177                 dst_line[0][x*3+2] = E2;
178                 dst_line[1][x*3]   = E3;
179                 dst_line[1][x*3+1] = E4;
180                 dst_line[1][x*3+2] = E5;
181                 dst_line[2][x*3]   = E6;
182                 dst_line[2][x*3+1] = E7;
183                 dst_line[2][x*3+2] = E8;
184             }
185 
186             src_line[0] = src_line[1];
187             src_line[1] = src_line[2];
188             src_line[2] = src_line[1];
189 
190             if (y < height - 2)
191                 src_line[2] += src_linesize;
192         }
193     }
194 
195     return 0;
196 }
197 
config_output(AVFilterLink * outlink)198 static int config_output(AVFilterLink *outlink)
199 {
200     AVFilterContext *ctx = outlink->src;
201     EPXContext *s = ctx->priv;
202     AVFilterLink *inlink = ctx->inputs[0];
203     const AVPixFmtDescriptor *desc;
204 
205     desc = av_pix_fmt_desc_get(outlink->format);
206     if (!desc)
207         return AVERROR_BUG;
208 
209     outlink->w = inlink->w * s->n;
210     outlink->h = inlink->h * s->n;
211 
212     switch (s->n) {
213     case 2:
214         s->epx_slice = epx2_slice;
215         break;
216     case 3:
217         s->epx_slice = epx3_slice;
218         break;
219     }
220 
221     return 0;
222 }
223 
224 static const enum AVPixelFormat pix_fmts[] = {
225     AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
226     AV_PIX_FMT_NONE,
227 };
228 
filter_frame(AVFilterLink * inlink,AVFrame * in)229 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
230 {
231     AVFilterContext *ctx = inlink->dst;
232     AVFilterLink *outlink = ctx->outputs[0];
233     EPXContext *s = ctx->priv;
234     ThreadData td;
235 
236     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
237     if (!out) {
238         av_frame_free(&in);
239         return AVERROR(ENOMEM);
240     }
241 
242     av_frame_copy_props(out, in);
243 
244     td.in = in, td.out = out;
245     ff_filter_execute(ctx, s->epx_slice, &td, NULL,
246                       FFMIN(inlink->h, ff_filter_get_nb_threads(ctx)));
247 
248     av_frame_free(&in);
249     return ff_filter_frame(outlink, out);
250 }
251 
252 static const AVFilterPad inputs[] = {
253     {
254         .name         = "default",
255         .type         = AVMEDIA_TYPE_VIDEO,
256         .filter_frame = filter_frame,
257     },
258 };
259 
260 static const AVFilterPad outputs[] = {
261     {
262         .name         = "default",
263         .type         = AVMEDIA_TYPE_VIDEO,
264         .config_props = config_output,
265     },
266 };
267 
268 const AVFilter ff_vf_epx = {
269     .name          = "epx",
270     .description   = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."),
271     FILTER_INPUTS(inputs),
272     FILTER_OUTPUTS(outputs),
273     FILTER_PIXFMTS_ARRAY(pix_fmts),
274     .priv_size     = sizeof(EPXContext),
275     .priv_class    = &epx_class,
276     .flags         = AVFILTER_FLAG_SLICE_THREADS,
277 };
278