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/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "drawutils.h"
30 #include "formats.h"
31 #include "video.h"
32 #include "internal.h"
33 
34 typedef struct {
35  const AVClass *class;
36  unsigned w, h;
37  unsigned margin;
38  unsigned padding;
39  unsigned current;
40  unsigned nb_frames;
44 } TileContext;
45 
46 #define REASONABLE_SIZE 1024
47 
48 #define OFFSET(x) offsetof(TileContext, x)
49 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 
51 static const AVOption tile_options[] = {
52  { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
53  {.str = "6x5"}, 0, 0, FLAGS },
54  { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
55  AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
56  { "margin", "set outer border margin in pixels", OFFSET(margin),
57  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
58  { "padding", "set inner border thickness in pixels", OFFSET(padding),
59  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
60  {NULL},
61 };
62 
64 
65 static av_cold int init(AVFilterContext *ctx)
66 {
67  TileContext *tile = ctx->priv;
68 
69  if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
70  av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
71  tile->w, tile->h);
72  return AVERROR(EINVAL);
73  }
74 
75  if (tile->nb_frames == 0) {
76  tile->nb_frames = tile->w * tile->h;
77  } else if (tile->nb_frames > tile->w * tile->h) {
78  av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
79  tile->w, tile->h, tile->w * tile->h);
80  return AVERROR(EINVAL);
81  }
82 
83  return 0;
84 }
85 
87 {
89  return 0;
90 }
91 
92 static int config_props(AVFilterLink *outlink)
93 {
94  AVFilterContext *ctx = outlink->src;
95  TileContext *tile = ctx->priv;
96  AVFilterLink *inlink = ctx->inputs[0];
97  const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
98  const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
99 
100  if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
101  av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
102  tile->w, inlink->w);
103  return AVERROR(EINVAL);
104  }
105  if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
106  av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
107  tile->h, inlink->h);
108  return AVERROR(EINVAL);
109  }
110  outlink->w = tile->w * inlink->w + total_margin_w;
111  outlink->h = tile->h * inlink->h + total_margin_h;
112  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
113  outlink->frame_rate = av_mul_q(inlink->frame_rate,
114  (AVRational){ 1, tile->nb_frames });
115  ff_draw_init(&tile->draw, inlink->format, 0);
116  /* TODO make the color an option, or find an unified way of choosing it */
117  ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
118 
119  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
120 
121  return 0;
122 }
123 
124 static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
125 {
126  TileContext *tile = ctx->priv;
127  AVFilterLink *inlink = ctx->inputs[0];
128  const unsigned tx = tile->current % tile->w;
129  const unsigned ty = tile->current / tile->w;
130 
131  *x = tile->margin + (inlink->w + tile->padding) * tx;
132  *y = tile->margin + (inlink->h + tile->padding) * ty;
133 }
134 
135 static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
136 {
137  TileContext *tile = ctx->priv;
138  AVFilterLink *inlink = ctx->inputs[0];
139  unsigned x0, y0;
140 
141  get_current_tile_pos(ctx, &x0, &y0);
142  ff_fill_rectangle(&tile->draw, &tile->blank,
143  out_buf->data, out_buf->linesize,
144  x0, y0, inlink->w, inlink->h);
145  tile->current++;
146 }
148 {
149  TileContext *tile = ctx->priv;
150  AVFilterLink *outlink = ctx->outputs[0];
151  AVFrame *out_buf = tile->out_ref;
152  int ret;
153 
154  while (tile->current < tile->nb_frames)
155  draw_blank_frame(ctx, out_buf);
156  ret = ff_filter_frame(outlink, out_buf);
157  tile->current = 0;
158  return ret;
159 }
160 
161 /* Note: direct rendering is not possible since there is no guarantee that
162  * buffers are fed to filter_frame in the order they were obtained from
163  * get_buffer (think B-frames). */
164 
165 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
166 {
167  AVFilterContext *ctx = inlink->dst;
168  TileContext *tile = ctx->priv;
169  AVFilterLink *outlink = ctx->outputs[0];
170  unsigned x0, y0;
171 
172  if (!tile->current) {
173  tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
174  if (!tile->out_ref)
175  return AVERROR(ENOMEM);
176  av_frame_copy_props(tile->out_ref, picref);
177  tile->out_ref->width = outlink->w;
178  tile->out_ref->height = outlink->h;
179 
180  /* fill surface once for margin/padding */
181  if (tile->margin || tile->padding)
182  ff_fill_rectangle(&tile->draw, &tile->blank,
183  tile->out_ref->data,
184  tile->out_ref->linesize,
185  0, 0, outlink->w, outlink->h);
186  }
187 
188  get_current_tile_pos(ctx, &x0, &y0);
189  ff_copy_rectangle2(&tile->draw,
190  tile->out_ref->data, tile->out_ref->linesize,
191  picref->data, picref->linesize,
192  x0, y0, 0, 0, inlink->w, inlink->h);
193 
194  av_frame_free(&picref);
195  if (++tile->current == tile->nb_frames)
196  return end_last_frame(ctx);
197 
198  return 0;
199 }
200 
201 static int request_frame(AVFilterLink *outlink)
202 {
203  AVFilterContext *ctx = outlink->src;
204  TileContext *tile = ctx->priv;
205  AVFilterLink *inlink = ctx->inputs[0];
206  int r;
207 
208  r = ff_request_frame(inlink);
209  if (r == AVERROR_EOF && tile->current)
210  r = end_last_frame(ctx);
211  return r;
212 }
213 
214 static const AVFilterPad tile_inputs[] = {
215  {
216  .name = "default",
217  .type = AVMEDIA_TYPE_VIDEO,
218  .filter_frame = filter_frame,
219  },
220  { NULL }
221 };
222 
223 static const AVFilterPad tile_outputs[] = {
224  {
225  .name = "default",
226  .type = AVMEDIA_TYPE_VIDEO,
227  .config_props = config_props,
228  .request_frame = request_frame,
229  },
230  { NULL }
231 };
232 
234  .name = "tile",
235  .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
236  .init = init,
237  .query_formats = query_formats,
238  .priv_size = sizeof(TileContext),
239  .inputs = tile_inputs,
240  .outputs = tile_outputs,
241  .priv_class = &tile_class,
242 };
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:501
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
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:214
AVOption.
Definition: opt.h:251
static int request_frame(AVFilterLink *outlink)
Definition: vf_tile.c:201
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
unsigned padding
Definition: vf_tile.c:38
#define REASONABLE_SIZE
Definition: vf_tile.c:46
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
Definition: vf_tile.c:165
output residual component w
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
unsigned nb_frames
Definition: vf_tile.c:40
static int end_last_frame(AVFilterContext *ctx)
Definition: vf_tile.c:147
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
static int config_props(AVFilterLink *outlink)
Definition: vf_tile.c:92
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
#define AVERROR_EOF
End of file.
Definition: error.h:55
static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
Definition: vf_tile.c:124
void 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:545
static const AVFilterPad tile_inputs[]
Definition: vf_tile.c:214
A filter pad used for either input or output.
Discrete Time axis x
int width
width and height of the video frame
Definition: frame.h:122
static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
Definition: vf_tile.c:135
AVFILTER_DEFINE_CLASS(tile)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const char * r
Definition: vf_curves.c:94
void * priv
private data for use by the filter
Definition: avfilter.h:545
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:179
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
unsigned current
Definition: vf_tile.c:39
static int query_formats(AVFilterContext *ctx)
Definition: vf_tile.c:86
ret
Definition: avfilter.c:821
static const AVFilterPad tile_outputs[]
Definition: vf_tile.c:223
static av_cold int init(AVFilterContext *ctx)
Definition: vf_tile.c:65
NULL
Definition: eval.c:55
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
AVFilter avfilter_vf_tile
Definition: vf_tile.c:233
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:437
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Init a draw context.
Definition: drawutils.c:135
AVFrame * out_ref
Definition: vf_tile.c:43
offset must point to two consecutive integers
Definition: opt.h:230
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
unsigned h
Definition: vf_tile.c:36
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
unsigned w
Definition: vf_tile.c:36
#define FLAGS
Definition: vf_tile.c:49
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
function y
Definition: D.m:1
unsigned margin
Definition: vf_tile.c:37
FFDrawColor blank
Definition: vf_tile.c:42
static const AVOption tile_options[]
Definition: vf_tile.c:51
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:236
An instance of a filter.
Definition: avfilter.h:524
int height
Definition: frame.h:122
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
internal API functions
Frame requests may need to loop in order to be fulfilled.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
#define OFFSET(x)
Definition: vf_tile.c:48
FFDrawContext draw
Definition: vf_tile.c:41