2 * Copyright (c) 2020 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/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/lfg.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/random_seed.h"
35 typedef struct ShufflePixelsContext {
56 int (*shuffle_pixels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
57 } ShufflePixelsContext;
59 static int query_formats(AVFilterContext *ctx)
61 static const enum AVPixelFormat pix_fmts[] = {
62 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
63 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
64 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
65 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
66 AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GBRAP,
67 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10,
68 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
71 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
73 return AVERROR(ENOMEM);
74 return ff_set_common_formats(ctx, fmts_list);
77 static void make_horizontal_map(AVFilterContext *ctx)
79 ShufflePixelsContext *s = ctx->priv;
80 const int nb_blocks = s->nb_blocks;
82 uint8_t *used = s->used;
83 int32_t *map = s->map;
85 for (int x = 0; x < s->planewidth[0];) {
86 int rand = av_lfg_get(c) % nb_blocks;
88 if (used[rand] == 0) {
92 width = FFMIN(s->block_w, s->planewidth[0] - x);
93 map[rand * s->block_w] = x;
95 width = FFMIN(s->block_w, s->planewidth[0] - rand * s->block_w);
96 map[x] = rand * s->block_w;
101 for (int i = 1; i < width; i++) {
102 map[rand * s->block_w + i] = map[rand * s->block_w] + i;
105 for (int i = 1; i < width; i++) {
106 map[x + i] = map[x] + i;
115 static void make_vertical_map(AVFilterContext *ctx)
117 ShufflePixelsContext *s = ctx->priv;
118 const int nb_blocks = s->nb_blocks;
120 uint8_t *used = s->used;
121 int32_t *map = s->map;
123 for (int y = 0; y < s->planeheight[0];) {
124 int rand = av_lfg_get(c) % nb_blocks;
126 if (used[rand] == 0) {
130 height = FFMIN(s->block_h, s->planeheight[0] - y);
131 map[rand * s->block_h] = y;
133 height = FFMIN(s->block_h, s->planeheight[0] - rand * s->block_h);
134 map[y] = rand * s->block_h;
139 for (int i = 1; i < height; i++) {
140 map[rand * s->block_h + i] = map[rand * s->block_h] + i;
143 for (int i = 1; i < height; i++) {
144 map[y + i] = map[y] + i;
153 static void make_block_map(AVFilterContext *ctx)
155 ShufflePixelsContext *s = ctx->priv;
156 const int nb_blocks = s->nb_blocks;
157 int nb_blocks_w = s->planewidth[0] / s->block_w;
159 uint8_t *used = s->used;
160 int32_t *map = s->map;
162 for (int i = 0; i < nb_blocks;) {
163 int rand = av_lfg_get(c) % nb_blocks;
165 if (used[rand] == 0) {
166 int yin = i / nb_blocks_w;
167 int xin = i % nb_blocks_w;
168 int in = yin * s->block_h * s->planewidth[0] + xin * s->block_w;
169 int yout = rand / nb_blocks_w;
170 int xout = rand % nb_blocks_w;
171 int out = yout * s->block_h * s->planewidth[0] + xout * s->block_w;
181 for (int y = 0; y < s->block_h; y++) {
182 for (int x = 0; x < s->block_w; x++) {
183 map[out + y * s->planewidth[0] + x] = map[out] + x + y * s->planewidth[0];
187 for (int y = 0; y < s->block_h; y++) {
188 for (int x = 0; x < s->block_w; x++) {
189 map[in + y * s->planewidth[0] + x] = map[in] + x + y * s->planewidth[0];
199 typedef struct ThreadData {
204 #define SHUFFLE_HORIZONTAL(name, type) \
205 static int shuffle_horizontal## name(AVFilterContext *ctx, void *arg, \
206 int jobnr, int nb_jobs) \
208 ShufflePixelsContext *s = ctx->priv; \
209 ThreadData *td = arg; \
210 AVFrame *in = td->in; \
211 AVFrame *out = td->out; \
213 for (int p = 0; p < s->nb_planes; p++) { \
214 const int slice_start = (s->planeheight[p] * jobnr) / nb_jobs; \
215 const int slice_end = (s->planeheight[p] * (jobnr+1)) / nb_jobs; \
216 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
217 const type *src = (const type *)(in->data[p] + \
218 slice_start * in->linesize[p]); \
219 const int32_t *map = s->map; \
221 for (int y = slice_start; y < slice_end; y++) { \
222 for (int x = 0; x < s->planewidth[p]; x++) { \
223 dst[x] = src[map[x]]; \
226 dst += out->linesize[p] / sizeof(type); \
227 src += in->linesize[p] / sizeof(type); \
234 SHUFFLE_HORIZONTAL(8, uint8_t)
235 SHUFFLE_HORIZONTAL(16, uint16_t)
237 #define SHUFFLE_VERTICAL(name, type) \
238 static int shuffle_vertical## name(AVFilterContext *ctx, void *arg, \
239 int jobnr, int nb_jobs) \
241 ShufflePixelsContext *s = ctx->priv; \
242 ThreadData *td = arg; \
243 AVFrame *in = td->in; \
244 AVFrame *out = td->out; \
246 for (int p = 0; p < s->nb_planes; p++) { \
247 const int slice_start = (s->planeheight[p] * jobnr) / nb_jobs; \
248 const int slice_end = (s->planeheight[p] * (jobnr+1)) / nb_jobs; \
249 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
250 const int32_t *map = s->map; \
252 for (int y = slice_start; y < slice_end; y++) { \
253 const type *src = (const type *)(in->data[p] + \
254 map[y] * in->linesize[p]); \
256 memcpy(dst, src, s->linesize[p]); \
257 dst += out->linesize[p] / sizeof(type); \
264 SHUFFLE_VERTICAL(8, uint8_t)
265 SHUFFLE_VERTICAL(16, uint16_t)
267 #define SHUFFLE_BLOCK(name, type) \
268 static int shuffle_block## name(AVFilterContext *ctx, void *arg, \
269 int jobnr, int nb_jobs) \
271 ShufflePixelsContext *s = ctx->priv; \
272 ThreadData *td = arg; \
273 AVFrame *in = td->in; \
274 AVFrame *out = td->out; \
276 for (int p = 0; p < s->nb_planes; p++) { \
277 const int slice_start = (s->planeheight[p] * jobnr) / nb_jobs; \
278 const int slice_end = (s->planeheight[p] * (jobnr+1)) / nb_jobs; \
279 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
280 const type *src = (const type *)in->data[p]; \
281 const int32_t *map = s->map + slice_start * s->planewidth[p]; \
283 for (int y = slice_start; y < slice_end; y++) { \
284 for (int x = 0; x < s->planewidth[p]; x++) { \
285 int ymap = map[x] / s->planewidth[p]; \
286 int xmap = map[x] % s->planewidth[p]; \
288 dst[x] = src[xmap + ymap * in->linesize[p] / sizeof(type)]; \
291 dst += out->linesize[p] / sizeof(type); \
292 map += s->planewidth[p]; \
299 SHUFFLE_BLOCK(8, uint8_t)
300 SHUFFLE_BLOCK(16, uint16_t)
302 static int config_output(AVFilterLink *outlink)
304 AVFilterContext *ctx = outlink->src;
305 ShufflePixelsContext *s = ctx->priv;
306 AVFilterLink *inlink = ctx->inputs[0];
307 const AVPixFmtDescriptor *desc;
311 s->seed = av_get_random_seed();
312 av_lfg_init(&s->c, s->seed);
314 desc = av_pix_fmt_desc_get(outlink->format);
317 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
318 s->depth = desc->comp[0].depth;
320 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
323 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
324 s->planewidth[0] = s->planewidth[3] = inlink->w;
326 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
327 s->planeheight[0] = s->planeheight[3] = inlink->h;
329 s->map = av_calloc(inlink->w * inlink->h, sizeof(*s->map));
331 return AVERROR(ENOMEM);
335 s->shuffle_pixels = s->depth <= 8 ? shuffle_horizontal8 : shuffle_horizontal16;
336 s->nb_blocks = (s->planewidth[0] + s->block_w - 1) / s->block_w;
339 s->shuffle_pixels = s->depth <= 8 ? shuffle_vertical8 : shuffle_vertical16;
340 s->nb_blocks = (s->planeheight[0] + s->block_h - 1) / s->block_h;
343 s->shuffle_pixels = s->depth <= 8 ? shuffle_block8 : shuffle_block16;
344 s->nb_blocks = (s->planeheight[0] / s->block_h) *
345 (s->planewidth[0] / s->block_w);
351 s->used = av_calloc(s->nb_blocks, sizeof(*s->used));
353 return AVERROR(ENOMEM);
357 make_horizontal_map(ctx);
360 make_vertical_map(ctx);
372 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
374 AVFilterContext *ctx = inlink->dst;
375 ShufflePixelsContext *s = ctx->priv;
376 AVFrame *out = ff_get_video_buffer(ctx->outputs[0], in->width, in->height);
380 ret = av_frame_copy_props(out, in);
388 ctx->internal->execute(ctx, s->shuffle_pixels, &td, NULL, FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
391 return ff_filter_frame(ctx->outputs[0], out);
397 static av_cold void uninit(AVFilterContext *ctx)
399 ShufflePixelsContext *s = ctx->priv;
405 #define OFFSET(x) offsetof(ShufflePixelsContext, x)
406 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
407 static const AVOption shufflepixels_options[] = {
408 { "direction", "set shuffle direction", OFFSET(direction), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "dir" },
409 { "d", "set shuffle direction", OFFSET(direction), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "dir" },
410 { "forward", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "dir" },
411 { "inverse", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "dir" },
412 { "mode", "set shuffle mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mode" },
413 { "m", "set shuffle mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mode" },
414 { "horizontal", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
415 { "vertical", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
416 { "block", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode" },
417 { "width", "set block width", OFFSET(block_w), AV_OPT_TYPE_INT, {.i64=10}, 1, 8000, FLAGS },
418 { "w", "set block width", OFFSET(block_w), AV_OPT_TYPE_INT, {.i64=10}, 1, 8000, FLAGS },
419 { "height", "set block height", OFFSET(block_h), AV_OPT_TYPE_INT, {.i64=10}, 1, 8000, FLAGS },
420 { "h", "set block height", OFFSET(block_h), AV_OPT_TYPE_INT, {.i64=10}, 1, 8000, FLAGS },
421 { "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT_MAX, FLAGS },
422 { "s", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT_MAX, FLAGS },
426 AVFILTER_DEFINE_CLASS(shufflepixels);
428 static const AVFilterPad shufflepixels_inputs[] = {
431 .type = AVMEDIA_TYPE_VIDEO,
432 .filter_frame = filter_frame,
437 static const AVFilterPad shufflepixels_outputs[] = {
440 .type = AVMEDIA_TYPE_VIDEO,
441 .config_props = config_output,
446 const AVFilter ff_vf_shufflepixels = {
447 .name = "shufflepixels",
448 .description = NULL_IF_CONFIG_SMALL("Shuffle video pixels."),
449 .priv_size = sizeof(ShufflePixelsContext),
450 .priv_class = &shufflepixels_class,
451 .query_formats = query_formats,
453 .inputs = shufflepixels_inputs,
454 .outputs = shufflepixels_outputs,
455 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,