FFmpeg
vf_epx.c
Go to the documentation of this file.
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 
43 
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 - 1)
104  src_line[2] += src_linesize;
105  }
106  }
107 
108  return 0;
109 }
110 
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 - 1)
191  src_line[2] += src_linesize;
192  }
193  }
194 
195  return 0;
196 }
197 
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[] = {
227 };
228 
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 
243 
244  td.in = in, td.out = out;
245  ff_filter_execute(ctx, s->epx_slice, &td, NULL,
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 
269  .name = "epx",
270  .description = NULL_IF_CONFIG_SMALL("Scale the input using EPX algorithm."),
274  .priv_size = sizeof(EPXContext),
275  .priv_class = &epx_class,
277 };
E4
#define E4(a, b)
Definition: indeo3data.h:251
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:98
td
#define td
Definition: regdef.h:70
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2564
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_epx.c:229
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:171
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
pixdesc.h
AVFrame::width
int width
Definition: frame.h:361
AVOption
AVOption.
Definition: opt.h:247
F
#define F(x)
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
D
D(D(float, sse)
Definition: rematrix_init.c:29
epx3_slice
static int epx3_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_epx.c:111
A
#define A(x)
Definition: vp56_arith.h:28
epx_options
static const AVOption epx_options[]
Definition: vf_epx.c:37
outputs
static const AVFilterPad outputs[]
Definition: vf_epx.c:260
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_epx.c:224
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
C
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
Definition: writing_filters.txt:58
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
E1
#define E1(x)
Definition: mem_internal.h:103
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2041
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
E
#define E
Definition: avdct.c:32
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
arg
const char * arg
Definition: jacosubdec.c:67
ff_vf_epx
const AVFilter ff_vf_epx
Definition: vf_epx.c:268
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
EPXContext
Definition: vf_epx.c:23
src
#define src
Definition: vp8dsp.c:255
OFFSET
#define OFFSET(x)
Definition: vf_epx.c:35
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
EPXContext::n
int n
Definition: vf_epx.c:26
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
height
#define height
H
#define H
Definition: pixlet.c:39
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_epx.c:198
internal.h
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(epx)
epx2_slice
static int epx2_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_epx.c:44
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:803
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
E2
#define E2(a, b)
Expand a pair of delta values (a,b) into two/four delta entries.
Definition: indeo3data.h:250
AVFilter
Filter definition.
Definition: avfilter.h:149
inputs
static const AVFilterPad inputs[]
Definition: vf_epx.c:252
G
#define G
Definition: huffyuvdsp.h:33
B
#define B
Definition: huffyuvdsp.h:32
AVFrame::height
int height
Definition: frame.h:361
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:121
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
FLAGS
#define FLAGS
Definition: vf_epx.c:36
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
EPXContext::epx_slice
int(* epx_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_epx.c:28
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:143
int
int
Definition: ffmpeg_filter.c:156