2 * Copyright (c) 2018 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
29 typedef struct ChromaShiftContext
{
48 int (*filter_slice
[2])(AVFilterContext
*ctx
, void *arg
, int jobnr
, int nb_jobs
);
51 #define DEFINE_SMEAR(depth, type, div) \
52 static int smear_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
54 ChromaShiftContext *s = ctx->priv; \
55 AVFrame *in = s->in; \
57 const int sulinesize = in->linesize[1] / div; \
58 const int svlinesize = in->linesize[2] / div; \
59 const int ulinesize = out->linesize[1] / div; \
60 const int vlinesize = out->linesize[2] / div; \
61 const int cbh = s->cbh; \
62 const int cbv = s->cbv; \
63 const int crh = s->crh; \
64 const int crv = s->crv; \
65 const int h = s->height[1]; \
66 const int w = s->width[1]; \
67 const int slice_start = (h * jobnr) / nb_jobs; \
68 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
69 const type *su = (const type *)in->data[1]; \
70 const type *sv = (const type *)in->data[2]; \
71 type *du = (type *)out->data[1] + slice_start * ulinesize; \
72 type *dv = (type *)out->data[2] + slice_start * vlinesize; \
74 for (int y = slice_start; y < slice_end; y++) { \
75 const int duy = av_clip(y - cbv, 0, h-1) * sulinesize; \
76 const int dvy = av_clip(y - crv, 0, h-1) * svlinesize; \
78 for (int x = 0; x < w; x++) { \
79 du[x] = su[av_clip(x - cbh, 0, w - 1) + duy]; \
80 dv[x] = sv[av_clip(x - crh, 0, w - 1) + dvy]; \
90 DEFINE_SMEAR(8, uint8_t, 1)
91 DEFINE_SMEAR(16, uint16_t, 2)
93 #define DEFINE_WRAP(depth, type, div) \
94 static int wrap_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
96 ChromaShiftContext *s = ctx->priv; \
97 AVFrame *in = s->in; \
99 const int sulinesize = in->linesize[1] / div; \
100 const int svlinesize = in->linesize[2] / div; \
101 const int ulinesize = out->linesize[1] / div; \
102 const int vlinesize = out->linesize[2] / div; \
103 const int cbh = s->cbh; \
104 const int cbv = s->cbv; \
105 const int crh = s->crh; \
106 const int crv = s->crv; \
107 const int h = s->height[1]; \
108 const int w = s->width[1]; \
109 const int slice_start = (h * jobnr) / nb_jobs; \
110 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
111 const type *su = (const type *)in->data[1]; \
112 const type *sv = (const type *)in->data[2]; \
113 type *du = (type *)out->data[1] + slice_start * ulinesize; \
114 type *dv = (type *)out->data[2] + slice_start * vlinesize; \
116 for (int y = slice_start; y < slice_end; y++) { \
117 int uy = (y - cbv) % h; \
118 int vy = (y - crv) % h; \
125 for (int x = 0; x < w; x++) { \
126 int ux = (x - cbh) % w; \
127 int vx = (x - crh) % w; \
134 du[x] = su[ux + uy * sulinesize]; \
135 dv[x] = sv[vx + vy * svlinesize]; \
145 DEFINE_WRAP(8, uint8_t, 1)
146 DEFINE_WRAP(16, uint16_t, 2)
148 #define DEFINE_RGBASMEAR(depth, type, div) \
149 static int rgbasmear_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
151 ChromaShiftContext *s = ctx->priv; \
152 AVFrame *in = s->in; \
153 AVFrame *out = arg; \
154 const int srlinesize = in->linesize[2] / div; \
155 const int sglinesize = in->linesize[0] / div; \
156 const int sblinesize = in->linesize[1] / div; \
157 const int salinesize = in->linesize[3] / div; \
158 const int rlinesize = out->linesize[2] / div; \
159 const int glinesize = out->linesize[0] / div; \
160 const int blinesize = out->linesize[1] / div; \
161 const int alinesize = out->linesize[3] / div; \
162 const int rh = s->rh; \
163 const int rv = s->rv; \
164 const int gh = s->gh; \
165 const int gv = s->gv; \
166 const int bh = s->bh; \
167 const int bv = s->bv; \
168 const int ah = s->ah; \
169 const int av = s->av; \
170 const int h = s->height[1]; \
171 const int w = s->width[1]; \
172 const int slice_start = (h * jobnr) / nb_jobs; \
173 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
174 const type *sr = (const type *)in->data[2]; \
175 const type *sg = (const type *)in->data[0]; \
176 const type *sb = (const type *)in->data[1]; \
177 const type *sa = (const type *)in->data[3]; \
178 type *dr = (type *)out->data[2] + slice_start * rlinesize; \
179 type *dg = (type *)out->data[0] + slice_start * glinesize; \
180 type *db = (type *)out->data[1] + slice_start * blinesize; \
181 type *da = (type *)out->data[3] + slice_start * alinesize; \
183 for (int y = slice_start; y < slice_end; y++) { \
184 const int ry = av_clip(y - rv, 0, h-1) * srlinesize; \
185 const int gy = av_clip(y - gv, 0, h-1) * sglinesize; \
186 const int by = av_clip(y - bv, 0, h-1) * sblinesize; \
189 for (int x = 0; x < w; x++) { \
190 dr[x] = sr[av_clip(x - rh, 0, w - 1) + ry]; \
191 dg[x] = sg[av_clip(x - gh, 0, w - 1) + gy]; \
192 db[x] = sb[av_clip(x - bh, 0, w - 1) + by]; \
199 if (s->nb_planes < 4) \
201 ay = av_clip(y - av, 0, h-1) * salinesize; \
202 for (int x = 0; x < w; x++) { \
203 da[x] = sa[av_clip(x - ah, 0, w - 1) + ay]; \
212 DEFINE_RGBASMEAR(8, uint8_t, 1)
213 DEFINE_RGBASMEAR(16, uint16_t, 2)
215 #define DEFINE_RGBAWRAP(depth, type, div) \
216 static int rgbawrap_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
218 ChromaShiftContext *s = ctx->priv; \
219 AVFrame *in = s->in; \
220 AVFrame *out = arg; \
221 const int srlinesize = in->linesize[2] / div; \
222 const int sglinesize = in->linesize[0] / div; \
223 const int sblinesize = in->linesize[1] / div; \
224 const int salinesize = in->linesize[3] / div; \
225 const int rlinesize = out->linesize[2] / div; \
226 const int glinesize = out->linesize[0] / div; \
227 const int blinesize = out->linesize[1] / div; \
228 const int alinesize = out->linesize[3] / div; \
229 const int rh = s->rh; \
230 const int rv = s->rv; \
231 const int gh = s->gh; \
232 const int gv = s->gv; \
233 const int bh = s->bh; \
234 const int bv = s->bv; \
235 const int ah = s->ah; \
236 const int av = s->av; \
237 const int h = s->height[1]; \
238 const int w = s->width[1]; \
239 const int slice_start = (h * jobnr) / nb_jobs; \
240 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
241 const type *sr = (const type *)in->data[2]; \
242 const type *sg = (const type *)in->data[0]; \
243 const type *sb = (const type *)in->data[1]; \
244 const type *sa = (const type *)in->data[3]; \
245 type *dr = (type *)out->data[2] + slice_start * rlinesize; \
246 type *dg = (type *)out->data[0] + slice_start * glinesize; \
247 type *db = (type *)out->data[1] + slice_start * blinesize; \
248 type *da = (type *)out->data[3] + slice_start * alinesize; \
250 for (int y = slice_start; y < slice_end; y++) { \
251 int ry = (y - rv) % h; \
252 int gy = (y - gv) % h; \
253 int by = (y - bv) % h; \
262 for (int x = 0; x < w; x++) { \
263 int rx = (x - rh) % w; \
264 int gx = (x - gh) % w; \
265 int bx = (x - bh) % w; \
273 dr[x] = sr[rx + ry * srlinesize]; \
274 dg[x] = sg[gx + gy * sglinesize]; \
275 db[x] = sb[bx + by * sblinesize]; \
282 if (s->nb_planes < 4) \
284 for (int x = 0; x < w; x++) { \
285 int ax = (x - ah) % w; \
286 int ay = (x - av) % h; \
292 da[x] = sa[ax + ay * salinesize]; \
301 DEFINE_RGBAWRAP(8, uint8_t, 1)
302 DEFINE_RGBAWRAP(16, uint16_t, 2)
304 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*in
)
306 AVFilterContext
*ctx
= inlink
->dst
;
307 AVFilterLink
*outlink
= ctx
->outputs
[0];
308 ChromaShiftContext
*s
= ctx
->priv
;
311 out
= ff_get_video_buffer(outlink
, outlink
->w
, outlink
->h
);
314 return AVERROR(ENOMEM
);
316 av_frame_copy_props(out
, in
);
319 if (!s
->is_rgbashift
) {
320 av_image_copy_plane(out
->data
[0],
322 in
->data
[0], in
->linesize
[0],
323 s
->linesize
[0], s
->height
[0]);
325 ff_filter_execute(ctx
, s
->filter_slice
[s
->edge
], out
, NULL
,
328 ff_filter_get_nb_threads(ctx
)));
331 return ff_filter_frame(outlink
, out
);
334 static int config_input(AVFilterLink
*inlink
)
336 AVFilterContext
*ctx
= inlink
->dst
;
337 ChromaShiftContext
*s
= ctx
->priv
;
338 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(inlink
->format
);
340 s
->is_rgbashift
= !strcmp(ctx
->filter
->name
, "rgbashift");
341 s
->depth
= desc
->comp
[0].depth
;
342 s
->nb_planes
= desc
->nb_components
;
343 if (s
->is_rgbashift
) {
344 s
->filter_slice
[1] = s
->depth
> 8 ? rgbawrap_slice16
: rgbawrap_slice8
;
345 s
->filter_slice
[0] = s
->depth
> 8 ? rgbasmear_slice16
: rgbasmear_slice8
;
347 s
->filter_slice
[1] = s
->depth
> 8 ? wrap_slice16
: wrap_slice8
;
348 s
->filter_slice
[0] = s
->depth
> 8 ? smear_slice16
: smear_slice8
;
350 s
->height
[1] = s
->height
[2] = AV_CEIL_RSHIFT(inlink
->h
, desc
->log2_chroma_h
);
351 s
->height
[0] = s
->height
[3] = inlink
->h
;
352 s
->width
[1] = s
->width
[2] = AV_CEIL_RSHIFT(inlink
->w
, desc
->log2_chroma_w
);
353 s
->width
[0] = s
->width
[3] = inlink
->w
;
355 return av_image_fill_linesizes(s
->linesize
, inlink
->format
, inlink
->w
);
358 #define OFFSET(x) offsetof(ChromaShiftContext, x)
359 #define VFR AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
361 static const AVOption chromashift_options
[] = {
362 { "cbh", "shift chroma-blue horizontally", OFFSET(cbh
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
363 { "cbv", "shift chroma-blue vertically", OFFSET(cbv
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
364 { "crh", "shift chroma-red horizontally", OFFSET(crh
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
365 { "crv", "shift chroma-red vertically", OFFSET(crv
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
366 { "edge", "set edge operation", OFFSET(edge
), AV_OPT_TYPE_INT
, {.i64
=0}, 0, 1, .flags
= VFR
, .unit
= "edge" },
367 { "smear", 0, 0, AV_OPT_TYPE_CONST
, {.i64
=0}, 0, 0, .flags
= VFR
, .unit
= "edge" },
368 { "wrap", 0, 0, AV_OPT_TYPE_CONST
, {.i64
=1}, 0, 0, .flags
= VFR
, .unit
= "edge" },
372 static const AVFilterPad inputs
[] = {
375 .type
= AVMEDIA_TYPE_VIDEO
,
376 .filter_frame
= filter_frame
,
377 .config_props
= config_input
,
381 static const enum AVPixelFormat yuv_pix_fmts
[] = {
382 AV_PIX_FMT_YUVA444P
, AV_PIX_FMT_YUVA422P
, AV_PIX_FMT_YUVA420P
,
383 AV_PIX_FMT_YUVJ444P
, AV_PIX_FMT_YUVJ440P
, AV_PIX_FMT_YUVJ422P
,AV_PIX_FMT_YUVJ420P
, AV_PIX_FMT_YUVJ411P
,
384 AV_PIX_FMT_YUV444P
, AV_PIX_FMT_YUV440P
, AV_PIX_FMT_YUV422P
, AV_PIX_FMT_YUV420P
, AV_PIX_FMT_YUV411P
, AV_PIX_FMT_YUV410P
,
385 AV_PIX_FMT_YUV420P9
, AV_PIX_FMT_YUV422P9
, AV_PIX_FMT_YUV444P9
,
386 AV_PIX_FMT_YUV420P10
, AV_PIX_FMT_YUV422P10
, AV_PIX_FMT_YUV444P10
, AV_PIX_FMT_YUV440P10
,
387 AV_PIX_FMT_YUVA420P10
, AV_PIX_FMT_YUVA422P10
, AV_PIX_FMT_YUVA444P10
,
388 AV_PIX_FMT_YUV420P12
, AV_PIX_FMT_YUV422P12
, AV_PIX_FMT_YUV444P12
, AV_PIX_FMT_YUV440P12
,
389 AV_PIX_FMT_YUVA422P12
, AV_PIX_FMT_YUVA444P12
,
390 AV_PIX_FMT_YUV444P14
, AV_PIX_FMT_YUV422P14
, AV_PIX_FMT_YUV420P14
,
391 AV_PIX_FMT_YUV420P16
, AV_PIX_FMT_YUV422P16
, AV_PIX_FMT_YUV444P16
,
392 AV_PIX_FMT_YUVA420P16
, AV_PIX_FMT_YUVA422P16
, AV_PIX_FMT_YUVA444P16
,
396 AVFILTER_DEFINE_CLASS(chromashift
);
398 const FFFilter ff_vf_chromashift
= {
399 .p
.name
= "chromashift",
400 .p
.description
= NULL_IF_CONFIG_SMALL("Shift chroma."),
401 .p
.priv_class
= &chromashift_class
,
402 .p
.flags
= AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
| AVFILTER_FLAG_SLICE_THREADS
,
403 .priv_size
= sizeof(ChromaShiftContext
),
404 FILTER_OUTPUTS(ff_video_default_filterpad
),
405 FILTER_INPUTS(inputs
),
406 FILTER_PIXFMTS_ARRAY(yuv_pix_fmts
),
407 .process_command
= ff_filter_process_command
,
410 static const enum AVPixelFormat rgb_pix_fmts
[] = {
411 AV_PIX_FMT_GBRP
, AV_PIX_FMT_GBRAP
, AV_PIX_FMT_GBRP9
,
412 AV_PIX_FMT_GBRP10
, AV_PIX_FMT_GBRP12
,
413 AV_PIX_FMT_GBRP14
, AV_PIX_FMT_GBRP16
,
414 AV_PIX_FMT_GBRAP10
, AV_PIX_FMT_GBRAP12
, AV_PIX_FMT_GBRAP16
,
418 static const AVOption rgbashift_options
[] = {
419 { "rh", "shift red horizontally", OFFSET(rh
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
420 { "rv", "shift red vertically", OFFSET(rv
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
421 { "gh", "shift green horizontally", OFFSET(gh
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
422 { "gv", "shift green vertically", OFFSET(gv
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
423 { "bh", "shift blue horizontally", OFFSET(bh
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
424 { "bv", "shift blue vertically", OFFSET(bv
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
425 { "ah", "shift alpha horizontally", OFFSET(ah
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
426 { "av", "shift alpha vertically", OFFSET(av
), AV_OPT_TYPE_INT
, {.i64
=0}, -255, 255, .flags
= VFR
},
427 { "edge", "set edge operation", OFFSET(edge
), AV_OPT_TYPE_INT
, {.i64
=0}, 0, 1, .flags
= VFR
, .unit
= "edge" },
428 { "smear", 0, 0, AV_OPT_TYPE_CONST
, {.i64
=0}, 0, 0, .flags
= VFR
, .unit
= "edge" },
429 { "wrap", 0, 0, AV_OPT_TYPE_CONST
, {.i64
=1}, 0, 0, .flags
= VFR
, .unit
= "edge" },
433 AVFILTER_DEFINE_CLASS(rgbashift
);
435 const FFFilter ff_vf_rgbashift
= {
436 .p
.name
= "rgbashift",
437 .p
.description
= NULL_IF_CONFIG_SMALL("Shift RGBA."),
438 .p
.priv_class
= &rgbashift_class
,
439 .p
.flags
= AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
| AVFILTER_FLAG_SLICE_THREADS
,
440 .priv_size
= sizeof(ChromaShiftContext
),
441 FILTER_OUTPUTS(ff_video_default_filterpad
),
442 FILTER_INPUTS(inputs
),
443 FILTER_PIXFMTS_ARRAY(rgb_pix_fmts
),
444 .process_command
= ff_filter_process_command
,