Commit | Line | Data |
---|---|---|
bc37ec92 SS |
1 | /* |
2 | * copyright (c) 2007 Bobby Bingham | |
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 | /** | |
22 | * @file libavfilter/vf_slicify.c | |
23 | * video slicing filter | |
24 | */ | |
25 | ||
26 | #include "avfilter.h" | |
b4b66456 | 27 | #include "libavutil/pixdesc.h" |
bc37ec92 SS |
28 | |
29 | typedef struct { | |
30 | int h; ///< output slice height | |
31 | int vshift; ///< vertical chroma subsampling shift | |
ec7ab610 SS |
32 | uint32_t lcg_state; ///< LCG state used to compute random slice height |
33 | int use_random_h; ///< enable the use of random slice height values | |
bc37ec92 SS |
34 | } SliceContext; |
35 | ||
36 | static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) | |
37 | { | |
38 | SliceContext *slice = ctx->priv; | |
39 | ||
40 | slice->h = 16; | |
ec7ab610 SS |
41 | if (args) { |
42 | if (!strcmp(args, "random")) { | |
43 | slice->use_random_h = 1; | |
44 | } else { | |
45 | sscanf(args, "%d", &slice->h); | |
46 | } | |
47 | } | |
bc37ec92 SS |
48 | return 0; |
49 | } | |
50 | ||
51 | static int config_props(AVFilterLink *link) | |
52 | { | |
53 | SliceContext *slice = link->dst->priv; | |
bc37ec92 | 54 | |
b4b66456 | 55 | slice->vshift = av_pix_fmt_descriptors[link->format].log2_chroma_h; |
bc37ec92 | 56 | |
bc37ec92 SS |
57 | return 0; |
58 | } | |
59 | ||
60 | static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms, | |
61 | int w, int h) | |
62 | { | |
63 | return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h); | |
64 | } | |
65 | ||
66 | static void start_frame(AVFilterLink *link, AVFilterPicRef *picref) | |
67 | { | |
ec7ab610 SS |
68 | SliceContext *slice = link->dst->priv; |
69 | ||
70 | if (slice->use_random_h) { | |
71 | slice->lcg_state = slice->lcg_state * 1664525 + 1013904223; | |
72 | slice->h = 8 + (uint64_t)slice->lcg_state * 25 / UINT32_MAX; | |
73 | } | |
74 | ||
75 | /* ensure that slices play nice with chroma subsampling, and enforce | |
76 | * a reasonable minimum size for the slices */ | |
77 | slice->h = FFMAX(8, slice->h & (-1 << slice->vshift)); | |
78 | ||
79 | av_log(link->dst, AV_LOG_DEBUG, "h:%d\n", slice->h); | |
80 | ||
bc37ec92 SS |
81 | avfilter_start_frame(link->dst->outputs[0], picref); |
82 | } | |
83 | ||
84 | static void end_frame(AVFilterLink *link) | |
85 | { | |
86 | avfilter_end_frame(link->dst->outputs[0]); | |
87 | } | |
88 | ||
a13a5437 | 89 | static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir) |
bc37ec92 SS |
90 | { |
91 | SliceContext *slice = link->dst->priv; | |
92 | int y2; | |
93 | ||
bf972d5e | 94 | if (slice_dir == 1) { |
3a1acfd7 SS |
95 | for (y2 = y; y2 + slice->h <= y + h; y2 += slice->h) |
96 | avfilter_draw_slice(link->dst->outputs[0], y2, slice->h, slice_dir); | |
bc37ec92 | 97 | |
3a1acfd7 SS |
98 | if (y2 < y + h) |
99 | avfilter_draw_slice(link->dst->outputs[0], y2, y + h - y2, slice_dir); | |
bf972d5e SS |
100 | } else if (slice_dir == -1) { |
101 | for (y2 = y + h; y2 - slice->h >= y; y2 -= slice->h) | |
102 | avfilter_draw_slice(link->dst->outputs[0], y2 - slice->h, slice->h, slice_dir); | |
103 | ||
104 | if (y2 > y) | |
105 | avfilter_draw_slice(link->dst->outputs[0], y, y2 - y, slice_dir); | |
106 | } | |
bc37ec92 SS |
107 | } |
108 | ||
109 | AVFilter avfilter_vf_slicify = { | |
110 | .name = "slicify", | |
111 | .description = "Pass the images of input video on to next video filter as multiple slices.", | |
112 | ||
113 | .init = init, | |
114 | ||
115 | .priv_size = sizeof(SliceContext), | |
116 | ||
117 | .inputs = (AVFilterPad[]) {{ .name = "default", | |
118 | .type = CODEC_TYPE_VIDEO, | |
119 | .get_video_buffer = get_video_buffer, | |
120 | .start_frame = start_frame, | |
121 | .draw_slice = draw_slice, | |
122 | .config_props = config_props, | |
123 | .end_frame = end_frame, }, | |
124 | { .name = NULL}}, | |
125 | .outputs = (AVFilterPad[]) {{ .name = "default", | |
126 | .type = CODEC_TYPE_VIDEO, }, | |
127 | { .name = NULL}}, | |
128 | }; |