FFmpeg
Loading...
Searching...
No Matches
vf_tile.c
Go to the documentation of this file.
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
35typedef 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;
48 uint8_t rgba_color[4];
50
51#define OFFSET(x) offsetof(TileContext, x)
52#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53
54static 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
72
74{
75 TileContext *tile = ctx->priv;
76
77 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 if (tile->padding) {
84 if ((tile->w - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding) ||
85 (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 if (tile->nb_frames == 0) {
93 tile->nb_frames = tile->w * tile->h;
94 } 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 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 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 tile->current = tile->init_padding;
109 }
110
111 return 0;
112}
113
115 AVFilterFormatsConfig **cfg_in,
116 AVFilterFormatsConfig **cfg_out)
117{
118 return ff_set_common_formats2(ctx, cfg_in, cfg_out,
120}
121
122static int config_props(AVFilterLink *outlink)
123{
124 AVFilterContext *ctx = outlink->src;
125 TileContext *tile = ctx->priv;
126 AVFilterLink *inlink = ctx->inputs[0];
127 FilterLink *il = ff_filter_link(inlink);
128 FilterLink *ol = ff_filter_link(outlink);
129 const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
130 const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
131 int ret;
132
133 if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
134 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
135 tile->w, inlink->w);
136 return AVERROR(EINVAL);
137 }
138 if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
139 av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
140 tile->h, inlink->h);
141 return AVERROR(EINVAL);
142 }
143 outlink->w = tile->w * inlink->w + total_margin_w;
144 outlink->h = tile->h * inlink->h + total_margin_h;
145 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
146 ol->frame_rate = av_mul_q(il->frame_rate, av_make_q(1, tile->nb_frames - tile->overlap));
147 ret = ff_draw_init_from_link(&tile->draw, inlink, 0);
148 if (ret < 0) {
149 av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
150 return ret;
151 }
152 ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
153
154 return 0;
155}
156
157static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
158{
159 TileContext *tile = ctx->priv;
160 AVFilterLink *inlink = ctx->inputs[0];
161 const unsigned tx = current % tile->w;
162 const unsigned ty = current / tile->w;
163
164 *x = tile->margin + (inlink->w + tile->padding) * tx;
165 *y = tile->margin + (inlink->h + tile->padding) * ty;
166}
167
169{
170 TileContext *tile = ctx->priv;
171 AVFilterLink *inlink = ctx->inputs[0];
172 unsigned x0, y0;
173
174 get_tile_pos(ctx, &x0, &y0, tile->current);
175 ff_fill_rectangle(&tile->draw, &tile->blank,
176 out_buf->data, out_buf->linesize,
177 x0, y0, inlink->w, inlink->h);
178 tile->current++;
179}
180
182{
183 TileContext *tile = ctx->priv;
184 AVFilterLink *outlink = ctx->outputs[0];
185 AVFrame *out_buf = tile->out_ref;
186 int ret;
187
188 while (tile->current < tile->nb_frames)
189 draw_blank_frame(ctx, out_buf);
190 tile->current = tile->overlap;
191 if (tile->current) {
192 av_frame_free(&tile->prev_out_ref);
193 tile->prev_out_ref = av_frame_clone(out_buf);
194 }
195 ret = ff_filter_frame(outlink, out_buf);
196 tile->out_ref = NULL;
197 return ret;
198}
199
200/* Note: direct rendering is not possible since there is no guarantee that
201 * buffers are fed to filter_frame in the order they were obtained from
202 * get_buffer (think B-frames). */
203
204static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
205{
206 AVFilterContext *ctx = inlink->dst;
207 TileContext *tile = ctx->priv;
208 AVFilterLink *outlink = ctx->outputs[0];
209 unsigned x0, y0;
210
211 if (!tile->out_ref) {
212 tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
213 if (!tile->out_ref) {
214 av_frame_free(&picref);
215 return AVERROR(ENOMEM);
216 }
217 av_frame_copy_props(tile->out_ref, picref);
218 tile->out_ref->width = outlink->w;
219 tile->out_ref->height = outlink->h;
220
221 /* fill surface once for margin/padding */
222 if (tile->margin || tile->padding || tile->init_padding)
223 ff_fill_rectangle(&tile->draw, &tile->blank,
224 tile->out_ref->data,
225 tile->out_ref->linesize,
226 0, 0, outlink->w, outlink->h);
227 tile->init_padding = 0;
228 }
229
230 if (tile->prev_out_ref) {
231 unsigned x1, y1, i;
232
233 for (i = tile->nb_frames - tile->overlap; i < tile->nb_frames; i++) {
234 get_tile_pos(ctx, &x1, &y1, i);
235 get_tile_pos(ctx, &x0, &y0, i - (tile->nb_frames - tile->overlap));
237 tile->out_ref->data, tile->out_ref->linesize,
238 tile->prev_out_ref->data, tile->prev_out_ref->linesize,
239 x0, y0, x1, y1, inlink->w, inlink->h);
240
241 }
242 }
243
244 get_tile_pos(ctx, &x0, &y0, tile->current);
246 tile->out_ref->data, tile->out_ref->linesize,
247 picref->data, picref->linesize,
248 x0, y0, 0, 0, inlink->w, inlink->h);
249
250 av_frame_free(&picref);
251 if (++tile->current == tile->nb_frames)
252 return end_last_frame(ctx);
253
254 return 0;
255}
256
257static int request_frame(AVFilterLink *outlink)
258{
259 AVFilterContext *ctx = outlink->src;
260 TileContext *tile = ctx->priv;
261 AVFilterLink *inlink = ctx->inputs[0];
262 int r;
263
264 r = ff_request_frame(inlink);
265 if (r == AVERROR_EOF && tile->current && tile->out_ref)
267 return r;
268}
269
271{
272 TileContext *tile = ctx->priv;
273
274 av_frame_free(&tile->out_ref);
275 av_frame_free(&tile->prev_out_ref);
276}
277
278static const AVFilterPad tile_inputs[] = {
279 {
280 .name = "default",
281 .type = AVMEDIA_TYPE_VIDEO,
282 .filter_frame = filter_frame,
283 },
284};
285
286static const AVFilterPad tile_outputs[] = {
287 {
288 .name = "default",
289 .type = AVMEDIA_TYPE_VIDEO,
290 .config_props = config_props,
291 .request_frame = request_frame,
292 },
293};
294
296 .p.name = "tile",
297 .p.description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
298 .p.priv_class = &tile_class,
299 .init = init,
300 .uninit = uninit,
301 .priv_size = sizeof(TileContext),
305};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
const FFFilter ff_vf_tile
Definition vf_tile.c:295
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition avfilter.c:483
Main libavfilter public API header.
static int FUNC tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
void ff_copy_rectangle2(FFDrawContext *draw, uint8_t *dst[], int dst_linesize[], uint8_t *src[], int src_linesize[], int dst_x, int dst_y, int src_x, int src_y, int w, int h)
Copy a rectangle from an image to another.
Definition drawutils.c:236
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition drawutils.c:179
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition drawutils.c:670
void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_x, int dst_y, int w, int h)
Fill a rectangle with an uniform color.
Definition drawutils.c:258
int ff_draw_init_from_link(FFDrawContext *draw, const AVFilterLink *link, unsigned flags)
Init a draw context, taking the format, colorspace and range from the given filter link.
Definition drawutils.c:168
misc drawing utilities
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @111144215057303131116103221376075045141373005341 current
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition opt.h:322
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
misc image utilities
#define r
Definition input.c:42
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
uint8_t w
Definition llvidencdsp.c:39
AVOptions.
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
unsigned h
Definition vf_tile.c:37
unsigned nb_frames
Definition vf_tile.c:43
FFDrawContext draw
Definition vf_tile.c:44
unsigned w
Definition vf_tile.c:37
AVFrame * prev_out_ref
Definition vf_tile.c:47
unsigned padding
Definition vf_tile.c:39
uint8_t rgba_color[4]
Definition vf_tile.c:48
unsigned init_padding
Definition vf_tile.c:41
unsigned margin
Definition vf_tile.c:38
unsigned current
Definition vf_tile.c:42
unsigned overlap
Definition vf_tile.c:40
AVFrame * out_ref
Definition vf_tile.c:46
FFDrawColor blank
Definition vf_tile.c:45
#define av_log(a,...)
static int config_props(AVFilterLink *outlink)
Definition vf_tile.c:122
static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
Definition vf_tile.c:157
static const AVOption tile_options[]
Definition vf_tile.c:54
static int end_last_frame(AVFilterContext *ctx)
Definition vf_tile.c:181
static int request_frame(AVFilterLink *outlink)
Definition vf_tile.c:257
static const AVFilterPad tile_inputs[]
Definition vf_tile.c:278
static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
Definition vf_tile.c:168
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_tile.c:114
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_tile.c:270
#define OFFSET(x)
Definition vf_tile.c:51
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
Definition vf_tile.c:204
static const AVFilterPad tile_outputs[]
Definition vf_tile.c:286
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89