FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_tile.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 85 120 70.8%
Functions: 8 9 88.9%
Branches: 20 46 43.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Nicolas George
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
23 * tile video filter
24 */
25
26 #include "libavutil/imgutils.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "drawutils.h"
31 #include "filters.h"
32 #include "formats.h"
33 #include "video.h"
34
35 typedef struct TileContext {
36 const AVClass *class;
37 unsigned w, h;
38 unsigned margin;
39 unsigned padding;
40 unsigned overlap;
41 unsigned init_padding;
42 unsigned current;
43 unsigned nb_frames;
44 FFDrawContext draw;
45 FFDrawColor blank;
46 AVFrame *out_ref;
47 AVFrame *prev_out_ref;
48 uint8_t rgba_color[4];
49 } TileContext;
50
51 #define OFFSET(x) offsetof(TileContext, x)
52 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53
54 static const AVOption tile_options[] = {
55 { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
56 {.str = "6x5"}, 0, 0, FLAGS },
57 { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
58 AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
59 { "margin", "set outer border margin in pixels", OFFSET(margin),
60 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
61 { "padding", "set inner border thickness in pixels", OFFSET(padding),
62 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
63 { "color", "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
64 { "overlap", "set how many frames to overlap for each render", OFFSET(overlap),
65 AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
66 { "init_padding", "set how many frames to initially pad", OFFSET(init_padding),
67 AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
68 { NULL }
69 };
70
71 AVFILTER_DEFINE_CLASS(tile);
72
73 2 static av_cold int init(AVFilterContext *ctx)
74 {
75 2 TileContext *tile = ctx->priv;
76
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (tile->w > UINT_MAX / tile->h) {
78 av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
79 tile->w, tile->h);
80 return AVERROR(EINVAL);
81 }
82
83
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (tile->padding) {
84
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if ((tile->w - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding) ||
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 (tile->h - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding)) {
86 av_log(ctx, AV_LOG_ERROR, "Combination of Tile size %ux%u, padding %d and margin %d overflows.\n",
87 tile->w, tile->h, tile->padding, tile->margin);
88 return AVERROR(EINVAL);
89 }
90 }
91
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (tile->nb_frames == 0) {
93 tile->nb_frames = tile->w * tile->h;
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (tile->nb_frames > tile->w * tile->h) {
95 av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
96 tile->w, tile->h, tile->w * tile->h);
97 return AVERROR(EINVAL);
98 }
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (tile->overlap >= tile->nb_frames) {
101 av_log(ctx, AV_LOG_WARNING, "overlap must be less than %d\n", tile->nb_frames);
102 tile->overlap = tile->nb_frames - 1;
103 }
104
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (tile->init_padding >= tile->nb_frames) {
106 av_log(ctx, AV_LOG_WARNING, "init_padding must be less than %d\n", tile->nb_frames);
107 } else {
108 2 tile->current = tile->init_padding;
109 }
110
111 2 return 0;
112 }
113
114 1 static int query_formats(const AVFilterContext *ctx,
115 AVFilterFormatsConfig **cfg_in,
116 AVFilterFormatsConfig **cfg_out)
117 {
118 1 return ff_set_common_formats2(ctx, cfg_in, cfg_out,
119 ff_draw_supported_pixel_formats(0));
120 }
121
122 1 static int config_props(AVFilterLink *outlink)
123 {
124 1 AVFilterContext *ctx = outlink->src;
125 1 TileContext *tile = ctx->priv;
126 1 AVFilterLink *inlink = ctx->inputs[0];
127 1 FilterLink *il = ff_filter_link(inlink);
128 1 FilterLink *ol = ff_filter_link(outlink);
129 1 const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
130 1 const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
131
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
133 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
134 tile->w, inlink->w);
135 return AVERROR(EINVAL);
136 }
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
138 av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
139 tile->h, inlink->h);
140 return AVERROR(EINVAL);
141 }
142 1 outlink->w = tile->w * inlink->w + total_margin_w;
143 1 outlink->h = tile->h * inlink->h + total_margin_h;
144 1 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
145 1 ol->frame_rate = av_mul_q(il->frame_rate, av_make_q(1, tile->nb_frames - tile->overlap));
146 1 ff_draw_init2(&tile->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
147 1 ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
148
149 1 return 0;
150 }
151
152 25 static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
153 {
154 25 TileContext *tile = ctx->priv;
155 25 AVFilterLink *inlink = ctx->inputs[0];
156 25 const unsigned tx = current % tile->w;
157 25 const unsigned ty = current / tile->w;
158
159 25 *x = tile->margin + (inlink->w + tile->padding) * tx;
160 25 *y = tile->margin + (inlink->h + tile->padding) * ty;
161 25 }
162
163 static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
164 {
165 TileContext *tile = ctx->priv;
166 AVFilterLink *inlink = ctx->inputs[0];
167 unsigned x0, y0;
168
169 get_tile_pos(ctx, &x0, &y0, tile->current);
170 ff_fill_rectangle(&tile->draw, &tile->blank,
171 out_buf->data, out_buf->linesize,
172 x0, y0, inlink->w, inlink->h);
173 tile->current++;
174 }
175
176 5 static int end_last_frame(AVFilterContext *ctx)
177 {
178 5 TileContext *tile = ctx->priv;
179 5 AVFilterLink *outlink = ctx->outputs[0];
180 5 AVFrame *out_buf = tile->out_ref;
181 int ret;
182
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 while (tile->current < tile->nb_frames)
184 draw_blank_frame(ctx, out_buf);
185 5 tile->current = tile->overlap;
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (tile->current) {
187 av_frame_free(&tile->prev_out_ref);
188 tile->prev_out_ref = av_frame_clone(out_buf);
189 }
190 5 ret = ff_filter_frame(outlink, out_buf);
191 5 tile->out_ref = NULL;
192 5 return ret;
193 }
194
195 /* Note: direct rendering is not possible since there is no guarantee that
196 * buffers are fed to filter_frame in the order they were obtained from
197 * get_buffer (think B-frames). */
198
199 25 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
200 {
201 25 AVFilterContext *ctx = inlink->dst;
202 25 TileContext *tile = ctx->priv;
203 25 AVFilterLink *outlink = ctx->outputs[0];
204 unsigned x0, y0;
205
206
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
25 if (!tile->out_ref) {
207 5 tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!tile->out_ref) {
209 av_frame_free(&picref);
210 return AVERROR(ENOMEM);
211 }
212 5 av_frame_copy_props(tile->out_ref, picref);
213 5 tile->out_ref->width = outlink->w;
214 5 tile->out_ref->height = outlink->h;
215
216 /* fill surface once for margin/padding */
217
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5 if (tile->margin || tile->padding || tile->init_padding)
218 5 ff_fill_rectangle(&tile->draw, &tile->blank,
219 5 tile->out_ref->data,
220 5 tile->out_ref->linesize,
221 0, 0, outlink->w, outlink->h);
222 5 tile->init_padding = 0;
223 }
224
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (tile->prev_out_ref) {
226 unsigned x1, y1, i;
227
228 for (i = tile->nb_frames - tile->overlap; i < tile->nb_frames; i++) {
229 get_tile_pos(ctx, &x1, &y1, i);
230 get_tile_pos(ctx, &x0, &y0, i - (tile->nb_frames - tile->overlap));
231 ff_copy_rectangle2(&tile->draw,
232 tile->out_ref->data, tile->out_ref->linesize,
233 tile->prev_out_ref->data, tile->prev_out_ref->linesize,
234 x0, y0, x1, y1, inlink->w, inlink->h);
235
236 }
237 }
238
239 25 get_tile_pos(ctx, &x0, &y0, tile->current);
240 25 ff_copy_rectangle2(&tile->draw,
241 25 tile->out_ref->data, tile->out_ref->linesize,
242 25 picref->data, picref->linesize,
243 x0, y0, 0, 0, inlink->w, inlink->h);
244
245 25 av_frame_free(&picref);
246
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
25 if (++tile->current == tile->nb_frames)
247 5 return end_last_frame(ctx);
248
249 20 return 0;
250 }
251
252 24 static int request_frame(AVFilterLink *outlink)
253 {
254 24 AVFilterContext *ctx = outlink->src;
255 24 TileContext *tile = ctx->priv;
256 24 AVFilterLink *inlink = ctx->inputs[0];
257 int r;
258
259 24 r = ff_request_frame(inlink);
260
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
24 if (r == AVERROR_EOF && tile->current && tile->out_ref)
261 r = end_last_frame(ctx);
262 24 return r;
263 }
264
265 2 static av_cold void uninit(AVFilterContext *ctx)
266 {
267 2 TileContext *tile = ctx->priv;
268
269 2 av_frame_free(&tile->out_ref);
270 2 av_frame_free(&tile->prev_out_ref);
271 2 }
272
273 static const AVFilterPad tile_inputs[] = {
274 {
275 .name = "default",
276 .type = AVMEDIA_TYPE_VIDEO,
277 .filter_frame = filter_frame,
278 },
279 };
280
281 static const AVFilterPad tile_outputs[] = {
282 {
283 .name = "default",
284 .type = AVMEDIA_TYPE_VIDEO,
285 .config_props = config_props,
286 .request_frame = request_frame,
287 },
288 };
289
290 const AVFilter ff_vf_tile = {
291 .name = "tile",
292 .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
293 .init = init,
294 .uninit = uninit,
295 .priv_size = sizeof(TileContext),
296 FILTER_INPUTS(tile_inputs),
297 FILTER_OUTPUTS(tile_outputs),
298 FILTER_QUERY_FUNC2(query_formats),
299 .priv_class = &tile_class,
300 };
301