FFmpeg
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 
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;
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 
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 {
117 }
118 
119 static int config_props(AVFilterLink *outlink)
120 {
121  AVFilterContext *ctx = outlink->src;
122  TileContext *tile = ctx->priv;
123  AVFilterLink *inlink = ctx->inputs[0];
125  FilterLink *ol = ff_filter_link(outlink);
126  const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
127  const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
128 
129  if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
130  av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
131  tile->w, inlink->w);
132  return AVERROR(EINVAL);
133  }
134  if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
135  av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
136  tile->h, inlink->h);
137  return AVERROR(EINVAL);
138  }
139  outlink->w = tile->w * inlink->w + total_margin_w;
140  outlink->h = tile->h * inlink->h + total_margin_h;
141  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
142  ol->frame_rate = av_mul_q(il->frame_rate, av_make_q(1, tile->nb_frames - tile->overlap));
143  ff_draw_init2(&tile->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
144  ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
145 
146  return 0;
147 }
148 
149 static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
150 {
151  TileContext *tile = ctx->priv;
152  AVFilterLink *inlink = ctx->inputs[0];
153  const unsigned tx = current % tile->w;
154  const unsigned ty = current / tile->w;
155 
156  *x = tile->margin + (inlink->w + tile->padding) * tx;
157  *y = tile->margin + (inlink->h + tile->padding) * ty;
158 }
159 
161 {
162  TileContext *tile = ctx->priv;
163  AVFilterLink *inlink = ctx->inputs[0];
164  unsigned x0, y0;
165 
166  get_tile_pos(ctx, &x0, &y0, tile->current);
167  ff_fill_rectangle(&tile->draw, &tile->blank,
168  out_buf->data, out_buf->linesize,
169  x0, y0, inlink->w, inlink->h);
170  tile->current++;
171 }
172 
174 {
175  TileContext *tile = ctx->priv;
176  AVFilterLink *outlink = ctx->outputs[0];
177  AVFrame *out_buf = tile->out_ref;
178  int ret;
179 
180  while (tile->current < tile->nb_frames)
181  draw_blank_frame(ctx, out_buf);
182  tile->current = tile->overlap;
183  if (tile->current) {
184  av_frame_free(&tile->prev_out_ref);
185  tile->prev_out_ref = av_frame_clone(out_buf);
186  }
187  ret = ff_filter_frame(outlink, out_buf);
188  tile->out_ref = NULL;
189  return ret;
190 }
191 
192 /* Note: direct rendering is not possible since there is no guarantee that
193  * buffers are fed to filter_frame in the order they were obtained from
194  * get_buffer (think B-frames). */
195 
197 {
198  AVFilterContext *ctx = inlink->dst;
199  TileContext *tile = ctx->priv;
200  AVFilterLink *outlink = ctx->outputs[0];
201  unsigned x0, y0;
202 
203  if (!tile->out_ref) {
204  tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
205  if (!tile->out_ref) {
206  av_frame_free(&picref);
207  return AVERROR(ENOMEM);
208  }
209  av_frame_copy_props(tile->out_ref, picref);
210  tile->out_ref->width = outlink->w;
211  tile->out_ref->height = outlink->h;
212 
213  /* fill surface once for margin/padding */
214  if (tile->margin || tile->padding || tile->init_padding)
215  ff_fill_rectangle(&tile->draw, &tile->blank,
216  tile->out_ref->data,
217  tile->out_ref->linesize,
218  0, 0, outlink->w, outlink->h);
219  tile->init_padding = 0;
220  }
221 
222  if (tile->prev_out_ref) {
223  unsigned x1, y1, i;
224 
225  for (i = tile->nb_frames - tile->overlap; i < tile->nb_frames; i++) {
226  get_tile_pos(ctx, &x1, &y1, i);
227  get_tile_pos(ctx, &x0, &y0, i - (tile->nb_frames - tile->overlap));
228  ff_copy_rectangle2(&tile->draw,
229  tile->out_ref->data, tile->out_ref->linesize,
230  tile->prev_out_ref->data, tile->prev_out_ref->linesize,
231  x0, y0, x1, y1, inlink->w, inlink->h);
232 
233  }
234  }
235 
236  get_tile_pos(ctx, &x0, &y0, tile->current);
237  ff_copy_rectangle2(&tile->draw,
238  tile->out_ref->data, tile->out_ref->linesize,
239  picref->data, picref->linesize,
240  x0, y0, 0, 0, inlink->w, inlink->h);
241 
242  av_frame_free(&picref);
243  if (++tile->current == tile->nb_frames)
244  return end_last_frame(ctx);
245 
246  return 0;
247 }
248 
249 static int request_frame(AVFilterLink *outlink)
250 {
251  AVFilterContext *ctx = outlink->src;
252  TileContext *tile = ctx->priv;
253  AVFilterLink *inlink = ctx->inputs[0];
254  int r;
255 
257  if (r == AVERROR_EOF && tile->current && tile->out_ref)
258  r = end_last_frame(ctx);
259  return r;
260 }
261 
263 {
264  TileContext *tile = ctx->priv;
265 
266  av_frame_free(&tile->out_ref);
267  av_frame_free(&tile->prev_out_ref);
268 }
269 
270 static const AVFilterPad tile_inputs[] = {
271  {
272  .name = "default",
273  .type = AVMEDIA_TYPE_VIDEO,
274  .filter_frame = filter_frame,
275  },
276 };
277 
278 static const AVFilterPad tile_outputs[] = {
279  {
280  .name = "default",
281  .type = AVMEDIA_TYPE_VIDEO,
282  .config_props = config_props,
283  .request_frame = request_frame,
284  },
285 };
286 
288  .name = "tile",
289  .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
290  .init = init,
291  .uninit = uninit,
292  .priv_size = sizeof(TileContext),
296  .priv_class = &tile_class,
297 };
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
Definition: vf_tile.c:196
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_tile.c:114
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
FFDrawColor
Definition: drawutils.h:50
r
const char * r
Definition: vf_curves.c:127
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
tile_outputs
static const AVFilterPad tile_outputs[]
Definition: vf_tile.c:278
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1023
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
TileContext::out_ref
AVFrame * out_ref
Definition: vf_tile.c:46
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_tile.c:262
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
AVFrame::width
int width
Definition: frame.h:461
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:429
config_props
static int config_props(AVFilterLink *outlink)
Definition: vf_tile.c:119
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:472
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
formats.h
TileContext::init_padding
unsigned init_padding
Definition: vf_tile.c:41
TileContext::current
unsigned current
Definition: vf_tile.c:42
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:867
TileContext::prev_out_ref
AVFrame * prev_out_ref
Definition: vf_tile.c:47
ff_copy_rectangle2
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:210
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:597
TileContext::rgba_color
uint8_t rgba_color[4]
Definition: vf_tile.c:48
ff_vf_tile
const AVFilter ff_vf_tile
Definition: vf_tile.c:287
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
TileContext::blank
FFDrawColor blank
Definition: vf_tile.c:45
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:713
tile_options
static const AVOption tile_options[]
Definition: vf_tile.c:54
get_tile_pos
static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
Definition: vf_tile.c:149
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition: opt.h:323
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition: opt.h:303
OFFSET
#define OFFSET(x)
Definition: vf_tile.c:51
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
tile_inputs
static const AVFilterPad tile_inputs[]
Definition: vf_tile.c:270
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_draw_init2
int ff_draw_init2(FFDrawContext *draw, enum AVPixelFormat format, enum AVColorSpace csp, enum AVColorRange range, unsigned flags)
Init a draw context.
Definition: drawutils.c:81
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
ff_fill_rectangle
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:232
TileContext::w
unsigned w
Definition: vf_tile.c:37
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: vf_tile.c:249
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_draw_supported_pixel_formats
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:648
TileContext::draw
FFDrawContext draw
Definition: vf_tile.c:44
TileContext::nb_frames
unsigned nb_frames
Definition: vf_tile.c:43
FFDrawContext
Definition: drawutils.h:35
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_tile.c:73
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
TileContext::overlap
unsigned overlap
Definition: vf_tile.c:40
end_last_frame
static int end_last_frame(AVFilterContext *ctx)
Definition: vf_tile.c:173
ff_draw_color
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:157
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
TileContext::padding
unsigned padding
Definition: vf_tile.c:39
AVFrame::height
int height
Definition: frame.h:461
TileContext::h
unsigned h
Definition: vf_tile.c:37
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
TileContext::margin
unsigned margin
Definition: vf_tile.c:38
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
draw_blank_frame
static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
Definition: vf_tile.c:160
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(tile)
imgutils.h
AVFrame::linesize
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:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
TileContext
Definition: vf_tile.c:35
drawutils.h
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:236
FLAGS
#define FLAGS
Definition: vf_tile.c:52