2 * Copyright (c) 2007 Bobby Bingham
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * video slicing filter
28 #include "libavutil/pixdesc.h"
31 int h; ///< output slice height
32 int vshift; ///< vertical chroma subsampling shift
33 uint32_t lcg_state; ///< LCG state used to compute random slice height
34 int use_random_h; ///< enable the use of random slice height values
37 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
39 SliceContext *slice = ctx->priv;
43 if (!strcmp(args, "random")) {
44 slice->use_random_h = 1;
46 sscanf(args, "%d", &slice->h);
52 static int config_props(AVFilterLink *link)
54 SliceContext *slice = link->dst->priv;
56 slice->vshift = av_pix_fmt_descriptors[link->format].log2_chroma_h;
61 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
63 SliceContext *slice = link->dst->priv;
65 if (slice->use_random_h) {
66 slice->lcg_state = slice->lcg_state * 1664525 + 1013904223;
67 slice->h = 8 + (uint64_t)slice->lcg_state * 25 / UINT32_MAX;
70 /* ensure that slices play nice with chroma subsampling, and enforce
71 * a reasonable minimum size for the slices */
72 slice->h = FFMAX(8, slice->h & (-1 << slice->vshift));
74 av_log(link->dst, AV_LOG_DEBUG, "h:%d\n", slice->h);
76 avfilter_start_frame(link->dst->outputs[0], picref);
79 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
81 SliceContext *slice = link->dst->priv;
85 for (y2 = y; y2 + slice->h <= y + h; y2 += slice->h)
86 avfilter_draw_slice(link->dst->outputs[0], y2, slice->h, slice_dir);
89 avfilter_draw_slice(link->dst->outputs[0], y2, y + h - y2, slice_dir);
90 } else if (slice_dir == -1) {
91 for (y2 = y + h; y2 - slice->h >= y; y2 -= slice->h)
92 avfilter_draw_slice(link->dst->outputs[0], y2 - slice->h, slice->h, slice_dir);
95 avfilter_draw_slice(link->dst->outputs[0], y, y2 - y, slice_dir);
99 AVFilter avfilter_vf_slicify = {
101 .description = NULL_IF_CONFIG_SMALL("Pass the images of input video on to next video filter as multiple slices."),
105 .priv_size = sizeof(SliceContext),
107 .inputs = (AVFilterPad[]) {{ .name = "default",
108 .type = AVMEDIA_TYPE_VIDEO,
109 .get_video_buffer = ff_null_get_video_buffer,
110 .start_frame = start_frame,
111 .draw_slice = draw_slice,
112 .config_props = config_props,
113 .end_frame = ff_null_end_frame, },
115 .outputs = (AVFilterPad[]) {{ .name = "default",
116 .type = AVMEDIA_TYPE_VIDEO, },