068c3c1b6832fcaa406bb0938ac41bd2ec749bef
[ffmpeg.git] / libavfilter / vf_chromashift.c
1 /*
2  * Copyright (c) 2018 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "libavutil/avstring.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "framesync.h"
32 #include "video.h"
33
34 typedef struct ChromaShiftContext {
35     const AVClass *class;
36     int cbh, cbv;
37     int crh, crv;
38     int rh, rv;
39     int gh, gv;
40     int bh, bv;
41     int ah, av;
42     int edge;
43
44     int nb_planes;
45     int depth;
46     int height[4];
47     int width[4];
48     int linesize[4];
49
50     AVFrame *in;
51
52     int is_rgbashift;
53     int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
54 } ChromaShiftContext;
55
56 static int query_formats(AVFilterContext *ctx)
57 {
58     static const enum AVPixelFormat yuv_pix_fmts[] = {
59         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
60         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
61         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
62         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
63         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV440P10,
64         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
65         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
66         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
67         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
68         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
69         AV_PIX_FMT_NONE
70     };
71     static const enum AVPixelFormat rgb_pix_fmts[] = {
72         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP9,
73         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
74         AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
75         AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
76         AV_PIX_FMT_NONE
77     };
78     const enum AVPixelFormat *pix_fmts;
79
80     if (!strcmp(ctx->filter->name, "rgbashift"))
81         pix_fmts = rgb_pix_fmts;
82     else
83         pix_fmts = yuv_pix_fmts;
84
85     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
86     if (!fmts_list)
87         return AVERROR(ENOMEM);
88     return ff_set_common_formats(ctx, fmts_list);
89 }
90
91 #define DEFINE_SMEAR(depth, type, div)                                                    \
92 static int smear_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)  \
93 {                                                                                         \
94     ChromaShiftContext *s = ctx->priv;                                                    \
95     AVFrame *in = s->in;                                                                  \
96     AVFrame *out = arg;                                                                   \
97     const int sulinesize = in->linesize[1] / div;                                         \
98     const int svlinesize = in->linesize[2] / div;                                         \
99     const int ulinesize = out->linesize[1] / div;                                         \
100     const int vlinesize = out->linesize[2] / div;                                         \
101     const int cbh = s->cbh;                                                               \
102     const int cbv = s->cbv;                                                               \
103     const int crh = s->crh;                                                               \
104     const int crv = s->crv;                                                               \
105     const int h = s->height[1];                                                           \
106     const int w = s->width[1];                                                            \
107     const int slice_start = (h * jobnr) / nb_jobs;                                        \
108     const int slice_end = (h * (jobnr+1)) / nb_jobs;                                      \
109     const type *su = (const type *)in->data[1];                                           \
110     const type *sv = (const type *)in->data[2];                                           \
111     type *du = (type *)out->data[1] + slice_start * ulinesize;                            \
112     type *dv = (type *)out->data[2] + slice_start * vlinesize;                            \
113                                                                                           \
114     for (int y = slice_start; y < slice_end; y++) {                                       \
115         const int duy = av_clip(y - cbv, 0, h-1) * sulinesize;                            \
116         const int dvy = av_clip(y - crv, 0, h-1) * svlinesize;                            \
117                                                                                           \
118         for (int x = 0; x < w; x++) {                                                     \
119             du[x] = su[av_clip(x - cbh, 0, w - 1) + duy];                                 \
120             dv[x] = sv[av_clip(x - crh, 0, w - 1) + dvy];                                 \
121         }                                                                                 \
122                                                                                           \
123         du += ulinesize;                                                                  \
124         dv += vlinesize;                                                                  \
125     }                                                                                     \
126                                                                                           \
127     return 0;                                                                             \
128 }
129
130 DEFINE_SMEAR(8, uint8_t, 1)
131 DEFINE_SMEAR(16, uint16_t, 2)
132
133 #define DEFINE_WRAP(depth, type, div)                                                     \
134 static int wrap_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)   \
135 {                                                                                         \
136     ChromaShiftContext *s = ctx->priv;                                                    \
137     AVFrame *in = s->in;                                                                  \
138     AVFrame *out = arg;                                                                   \
139     const int sulinesize = in->linesize[1] / div;                                         \
140     const int svlinesize = in->linesize[2] / div;                                         \
141     const int ulinesize = out->linesize[1] / div;                                         \
142     const int vlinesize = out->linesize[2] / div;                                         \
143     const int cbh = s->cbh;                                                               \
144     const int cbv = s->cbv;                                                               \
145     const int crh = s->crh;                                                               \
146     const int crv = s->crv;                                                               \
147     const int h = s->height[1];                                                           \
148     const int w = s->width[1];                                                            \
149     const int slice_start = (h * jobnr) / nb_jobs;                                        \
150     const int slice_end = (h * (jobnr+1)) / nb_jobs;                                      \
151     const type *su = (const type *)in->data[1];                                           \
152     const type *sv = (const type *)in->data[2];                                           \
153     type *du = (type *)out->data[1] + slice_start * ulinesize;                            \
154     type *dv = (type *)out->data[2] + slice_start * vlinesize;                            \
155                                                                                           \
156     for (int y = slice_start; y < slice_end; y++) {                                       \
157         int uy = (y - cbv) % h;                                                           \
158         int vy = (y - crv) % h;                                                           \
159                                                                                           \
160         if (uy < 0)                                                                       \
161             uy += h;                                                                      \
162         if (vy < 0)                                                                       \
163             vy += h;                                                                      \
164                                                                                           \
165         for (int x = 0; x < w; x++) {                                                     \
166             int ux = (x - cbh) % w;                                                       \
167             int vx = (x - crh) % w;                                                       \
168                                                                                           \
169             if (ux < 0)                                                                   \
170                 ux += w;                                                                  \
171             if (vx < 0)                                                                   \
172                 vx += w;                                                                  \
173                                                                                           \
174             du[x] = su[ux + uy * sulinesize];                                             \
175             dv[x] = sv[vx + vy * svlinesize];                                             \
176         }                                                                                 \
177                                                                                           \
178         du += ulinesize;                                                                  \
179         dv += vlinesize;                                                                  \
180     }                                                                                     \
181                                                                                           \
182     return 0;                                                                             \
183 }
184
185 DEFINE_WRAP(8, uint8_t, 1)
186 DEFINE_WRAP(16, uint16_t, 2)
187
188 #define DEFINE_RGBASMEAR(depth, type, div)                                                    \
189 static int rgbasmear_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)  \
190 {                                                                                         \
191     ChromaShiftContext *s = ctx->priv;                                                    \
192     AVFrame *in = s->in;                                                                  \
193     AVFrame *out = arg;                                                                   \
194     const int srlinesize = in->linesize[2] / div;                                         \
195     const int sglinesize = in->linesize[0] / div;                                         \
196     const int sblinesize = in->linesize[1] / div;                                         \
197     const int salinesize = in->linesize[3] / div;                                         \
198     const int rlinesize = out->linesize[2] / div;                                         \
199     const int glinesize = out->linesize[0] / div;                                         \
200     const int blinesize = out->linesize[1] / div;                                         \
201     const int alinesize = out->linesize[3] / div;                                         \
202     const int rh = s->rh;                                                                 \
203     const int rv = s->rv;                                                                 \
204     const int gh = s->gh;                                                                 \
205     const int gv = s->gv;                                                                 \
206     const int bh = s->bh;                                                                 \
207     const int bv = s->bv;                                                                 \
208     const int ah = s->ah;                                                                 \
209     const int av = s->av;                                                                 \
210     const int h = s->height[1];                                                           \
211     const int w = s->width[1];                                                            \
212     const int slice_start = (h * jobnr) / nb_jobs;                                        \
213     const int slice_end = (h * (jobnr+1)) / nb_jobs;                                      \
214     const type *sr = (const type *)in->data[2];                                           \
215     const type *sg = (const type *)in->data[0];                                           \
216     const type *sb = (const type *)in->data[1];                                           \
217     const type *sa = (const type *)in->data[3];                                           \
218     type *dr = (type *)out->data[2] + slice_start * rlinesize;                            \
219     type *dg = (type *)out->data[0] + slice_start * glinesize;                            \
220     type *db = (type *)out->data[1] + slice_start * blinesize;                            \
221     type *da = (type *)out->data[3] + slice_start * alinesize;                            \
222                                                                                           \
223     for (int y = slice_start; y < slice_end; y++) {                                       \
224         const int ry = av_clip(y - rv, 0, h-1) * srlinesize;                              \
225         const int gy = av_clip(y - gv, 0, h-1) * sglinesize;                              \
226         const int by = av_clip(y - bv, 0, h-1) * sblinesize;                              \
227         int ay;                                                                           \
228                                                                                           \
229         for (int x = 0; x < w; x++) {                                                     \
230             dr[x] = sr[av_clip(x - rh, 0, w - 1) + ry];                                   \
231             dg[x] = sg[av_clip(x - gh, 0, w - 1) + gy];                                   \
232             db[x] = sb[av_clip(x - bh, 0, w - 1) + by];                                   \
233         }                                                                                 \
234                                                                                           \
235         dr += rlinesize;                                                                  \
236         dg += glinesize;                                                                  \
237         db += blinesize;                                                                  \
238                                                                                           \
239         if (s->nb_planes < 4)                                                             \
240             continue;                                                                     \
241         ay = av_clip(y - av, 0, h-1) * salinesize;                                        \
242         for (int x = 0; x < w; x++) {                                                     \
243             da[x] = sa[av_clip(x - ah, 0, w - 1) + ay];                                   \
244         }                                                                                 \
245                                                                                           \
246         da += alinesize;                                                                  \
247     }                                                                                     \
248                                                                                           \
249     return 0;                                                                             \
250 }
251
252 DEFINE_RGBASMEAR(8, uint8_t, 1)
253 DEFINE_RGBASMEAR(16, uint16_t, 2)
254
255 #define DEFINE_RGBAWRAP(depth, type, div)                                                     \
256 static int rgbawrap_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)   \
257 {                                                                                         \
258     ChromaShiftContext *s = ctx->priv;                                                    \
259     AVFrame *in = s->in;                                                                  \
260     AVFrame *out = arg;                                                                   \
261     const int srlinesize = in->linesize[2] / div;                                         \
262     const int sglinesize = in->linesize[0] / div;                                         \
263     const int sblinesize = in->linesize[1] / div;                                         \
264     const int salinesize = in->linesize[3] / div;                                         \
265     const int rlinesize = out->linesize[2] / div;                                         \
266     const int glinesize = out->linesize[0] / div;                                         \
267     const int blinesize = out->linesize[1] / div;                                         \
268     const int alinesize = out->linesize[3] / div;                                         \
269     const int rh = s->rh;                                                                 \
270     const int rv = s->rv;                                                                 \
271     const int gh = s->gh;                                                                 \
272     const int gv = s->gv;                                                                 \
273     const int bh = s->bh;                                                                 \
274     const int bv = s->bv;                                                                 \
275     const int ah = s->ah;                                                                 \
276     const int av = s->av;                                                                 \
277     const int h = s->height[1];                                                           \
278     const int w = s->width[1];                                                            \
279     const int slice_start = (h * jobnr) / nb_jobs;                                        \
280     const int slice_end = (h * (jobnr+1)) / nb_jobs;                                      \
281     const type *sr = (const type *)in->data[2];                                           \
282     const type *sg = (const type *)in->data[0];                                           \
283     const type *sb = (const type *)in->data[1];                                           \
284     const type *sa = (const type *)in->data[3];                                           \
285     type *dr = (type *)out->data[2] + slice_start * rlinesize;                            \
286     type *dg = (type *)out->data[0] + slice_start * glinesize;                            \
287     type *db = (type *)out->data[1] + slice_start * blinesize;                            \
288     type *da = (type *)out->data[3] + slice_start * alinesize;                            \
289                                                                                           \
290     for (int y = slice_start; y < slice_end; y++) {                                       \
291         int ry = (y - rv) % h;                                                            \
292         int gy = (y - gv) % h;                                                            \
293         int by = (y - bv) % h;                                                            \
294                                                                                           \
295         if (ry < 0)                                                                       \
296             ry += h;                                                                      \
297         if (gy < 0)                                                                       \
298             gy += h;                                                                      \
299         if (by < 0)                                                                       \
300             by += h;                                                                      \
301                                                                                           \
302         for (int x = 0; x < w; x++) {                                                     \
303             int rx = (x - rh) % w;                                                        \
304             int gx = (x - gh) % w;                                                        \
305             int bx = (x - bh) % w;                                                        \
306                                                                                           \
307             if (rx < 0)                                                                   \
308                 rx += w;                                                                  \
309             if (gx < 0)                                                                   \
310                 gx += w;                                                                  \
311             if (bx < 0)                                                                   \
312                 bx += w;                                                                  \
313             dr[x] = sr[rx + ry * srlinesize];                                             \
314             dg[x] = sg[gx + gy * sglinesize];                                             \
315             db[x] = sb[bx + by * sblinesize];                                             \
316         }                                                                                 \
317                                                                                           \
318         dr += rlinesize;                                                                  \
319         dg += glinesize;                                                                  \
320         db += blinesize;                                                                  \
321                                                                                           \
322         if (s->nb_planes < 4)                                                             \
323             continue;                                                                     \
324         for (int x = 0; x < w; x++) {                                                     \
325             int ax = (x - ah) % w;                                                        \
326             int ay = (x - av) % h;                                                        \
327                                                                                           \
328             if (ax < 0)                                                                   \
329                 ax += w;                                                                  \
330             if (ay < 0)                                                                   \
331                 ay += h;                                                                  \
332             da[x] = sa[ax + ay * salinesize];                                             \
333         }                                                                                 \
334                                                                                           \
335         da += alinesize;                                                                  \
336     }                                                                                     \
337                                                                                           \
338     return 0;                                                                             \
339 }
340
341 DEFINE_RGBAWRAP(8, uint8_t, 1)
342 DEFINE_RGBAWRAP(16, uint16_t, 2)
343
344 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
345 {
346     AVFilterContext *ctx = inlink->dst;
347     AVFilterLink *outlink = ctx->outputs[0];
348     ChromaShiftContext *s = ctx->priv;
349     AVFrame *out;
350
351     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
352     if (!out) {
353         av_frame_free(&in);
354         return AVERROR(ENOMEM);
355     }
356     av_frame_copy_props(out, in);
357
358     s->in = in;
359     if (!s->is_rgbashift) {
360         av_image_copy_plane(out->data[0] + out->linesize[0],
361                             out->linesize[0],
362                             in->data[0], in->linesize[0],
363                             s->linesize[0], s->height[0]);
364     }
365     ctx->internal->execute(ctx, s->filter_slice, out, NULL,
366                            FFMIN3(s->height[1],
367                                   s->height[2],
368                                   ff_filter_get_nb_threads(ctx)));
369     s->in = NULL;
370     av_frame_free(&in);
371     return ff_filter_frame(outlink, out);
372 }
373
374 static int config_input(AVFilterLink *inlink)
375 {
376     AVFilterContext *ctx = inlink->dst;
377     ChromaShiftContext *s = ctx->priv;
378     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
379
380     s->is_rgbashift = !strcmp(ctx->filter->name, "rgbashift");
381     s->depth = desc->comp[0].depth;
382     s->nb_planes = desc->nb_components;
383     if (s->is_rgbashift) {
384         if (s->edge)
385             s->filter_slice = s->depth > 8 ? rgbawrap_slice16 : rgbawrap_slice8;
386         else
387             s->filter_slice = s->depth > 8 ? rgbasmear_slice16 : rgbasmear_slice8;
388     } else {
389         if (s->edge)
390             s->filter_slice = s->depth > 8 ? wrap_slice16 : wrap_slice8;
391         else
392             s->filter_slice = s->depth > 8 ? smear_slice16 : smear_slice8;
393     }
394     s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
395     s->height[0] = s->height[3] = inlink->h;
396     s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
397     s->width[0] = s->width[3] = inlink->w;
398
399     return av_image_fill_linesizes(s->linesize, inlink->format, inlink->w);
400 }
401
402 #define OFFSET(x) offsetof(ChromaShiftContext, x)
403 #define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
404
405 static const AVOption chromashift_options[] = {
406     { "cbh", "shift chroma-blue horizontally", OFFSET(cbh),  AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
407     { "cbv", "shift chroma-blue vertically",   OFFSET(cbv),  AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
408     { "crh", "shift chroma-red horizontally",  OFFSET(crh),  AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
409     { "crv", "shift chroma-red vertically",    OFFSET(crv),  AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
410     { "edge", "set edge operation",            OFFSET(edge), AV_OPT_TYPE_INT,   {.i64=0},    0,   1, .flags = VF, "edge" },
411     { "smear",                              0,            0, AV_OPT_TYPE_CONST, {.i64=0},    0,   0, .flags = VF, "edge" },
412     { "wrap",                               0,            0, AV_OPT_TYPE_CONST, {.i64=1},    0,   0, .flags = VF, "edge" },
413     { NULL },
414 };
415
416 static const AVFilterPad inputs[] = {
417     {
418         .name         = "default",
419         .type         = AVMEDIA_TYPE_VIDEO,
420         .filter_frame = filter_frame,
421         .config_props = config_input,
422     },
423     { NULL }
424 };
425
426 static const AVFilterPad outputs[] = {
427     {
428         .name = "default",
429         .type = AVMEDIA_TYPE_VIDEO,
430     },
431     { NULL }
432 };
433
434 AVFILTER_DEFINE_CLASS(chromashift);
435
436 AVFilter ff_vf_chromashift = {
437     .name          = "chromashift",
438     .description   = NULL_IF_CONFIG_SMALL("Shift chroma."),
439     .priv_size     = sizeof(ChromaShiftContext),
440     .priv_class    = &chromashift_class,
441     .query_formats = query_formats,
442     .outputs       = outputs,
443     .inputs        = inputs,
444     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
445 };
446
447 static const AVOption rgbashift_options[] = {
448     { "rh", "shift red horizontally",   OFFSET(rh),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
449     { "rv", "shift red vertically",     OFFSET(rv),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
450     { "gh", "shift green horizontally", OFFSET(gh),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
451     { "gv", "shift green vertically",   OFFSET(gv),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
452     { "bh", "shift blue horizontally",  OFFSET(bh),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
453     { "bv", "shift blue vertically",    OFFSET(bv),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
454     { "ah", "shift alpha horizontally", OFFSET(ah),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
455     { "av", "shift alpha vertically",   OFFSET(av),   AV_OPT_TYPE_INT,   {.i64=0}, -255, 255, .flags = VF },
456     { "edge", "set edge operation",     OFFSET(edge), AV_OPT_TYPE_INT,   {.i64=0},    0,   1, .flags = VF, "edge" },
457     { "smear",                          0,         0, AV_OPT_TYPE_CONST, {.i64=0},    0,   0, .flags = VF, "edge" },
458     { "wrap",                           0,         0, AV_OPT_TYPE_CONST, {.i64=1},    0,   0, .flags = VF, "edge" },
459     { NULL },
460 };
461
462 AVFILTER_DEFINE_CLASS(rgbashift);
463
464 AVFilter ff_vf_rgbashift = {
465     .name          = "rgbashift",
466     .description   = NULL_IF_CONFIG_SMALL("Shift RGBA."),
467     .priv_size     = sizeof(ChromaShiftContext),
468     .priv_class    = &rgbashift_class,
469     .query_formats = query_formats,
470     .outputs       = outputs,
471     .inputs        = inputs,
472     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
473 };