FFmpeg  2.1.1
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  uint8_t rgba_color[4];
45 } TileContext;
46 
47 #define REASONABLE_SIZE 1024
48 
49 #define OFFSET(x) offsetof(TileContext, x)
50 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 
52 static const AVOption tile_options[] = {
53  { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
54  {.str = "6x5"}, 0, 0, FLAGS },
55  { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
56  AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
57  { "margin", "set outer border margin in pixels", OFFSET(margin),
58  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
59  { "padding", "set inner border thickness in pixels", OFFSET(padding),
60  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
61  { "color", "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
62  { NULL }
63 };
64 
66 
67 static av_cold int init(AVFilterContext *ctx)
68 {
69  TileContext *tile = ctx->priv;
70 
71  if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
72  av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
73  tile->w, tile->h);
74  return AVERROR(EINVAL);
75  }
76 
77  if (tile->nb_frames == 0) {
78  tile->nb_frames = tile->w * tile->h;
79  } else if (tile->nb_frames > tile->w * tile->h) {
80  av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
81  tile->w, tile->h, tile->w * tile->h);
82  return AVERROR(EINVAL);
83  }
84 
85  return 0;
86 }
87 
89 {
91  return 0;
92 }
93 
94 static int config_props(AVFilterLink *outlink)
95 {
96  AVFilterContext *ctx = outlink->src;
97  TileContext *tile = ctx->priv;
98  AVFilterLink *inlink = ctx->inputs[0];
99  const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
100  const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
101 
102  if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
103  av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
104  tile->w, inlink->w);
105  return AVERROR(EINVAL);
106  }
107  if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
108  av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
109  tile->h, inlink->h);
110  return AVERROR(EINVAL);
111  }
112  outlink->w = tile->w * inlink->w + total_margin_w;
113  outlink->h = tile->h * inlink->h + total_margin_h;
114  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
115  outlink->frame_rate = av_mul_q(inlink->frame_rate,
116  (AVRational){ 1, tile->nb_frames });
117  ff_draw_init(&tile->draw, inlink->format, 0);
118  ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
119 
120  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
121 
122  return 0;
123 }
124 
125 static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
126 {
127  TileContext *tile = ctx->priv;
128  AVFilterLink *inlink = ctx->inputs[0];
129  const unsigned tx = tile->current % tile->w;
130  const unsigned ty = tile->current / tile->w;
131 
132  *x = tile->margin + (inlink->w + tile->padding) * tx;
133  *y = tile->margin + (inlink->h + tile->padding) * ty;
134 }
135 
136 static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
137 {
138  TileContext *tile = ctx->priv;
139  AVFilterLink *inlink = ctx->inputs[0];
140  unsigned x0, y0;
141 
142  get_current_tile_pos(ctx, &x0, &y0);
143  ff_fill_rectangle(&tile->draw, &tile->blank,
144  out_buf->data, out_buf->linesize,
145  x0, y0, inlink->w, inlink->h);
146  tile->current++;
147 }
149 {
150  TileContext *tile = ctx->priv;
151  AVFilterLink *outlink = ctx->outputs[0];
152  AVFrame *out_buf = tile->out_ref;
153  int ret;
154 
155  while (tile->current < tile->nb_frames)
156  draw_blank_frame(ctx, out_buf);
157  ret = ff_filter_frame(outlink, out_buf);
158  tile->current = 0;
159  return ret;
160 }
161 
162 /* Note: direct rendering is not possible since there is no guarantee that
163  * buffers are fed to filter_frame in the order they were obtained from
164  * get_buffer (think B-frames). */
165 
166 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
167 {
168  AVFilterContext *ctx = inlink->dst;
169  TileContext *tile = ctx->priv;
170  AVFilterLink *outlink = ctx->outputs[0];
171  unsigned x0, y0;
172 
173  if (!tile->current) {
174  tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
175  if (!tile->out_ref) {
176  av_frame_free(&picref);
177  return AVERROR(ENOMEM);
178  }
179  av_frame_copy_props(tile->out_ref, picref);
180  tile->out_ref->width = outlink->w;
181  tile->out_ref->height = outlink->h;
182 
183  /* fill surface once for margin/padding */
184  if (tile->margin || tile->padding)
185  ff_fill_rectangle(&tile->draw, &tile->blank,
186  tile->out_ref->data,
187  tile->out_ref->linesize,
188  0, 0, outlink->w, outlink->h);
189  }
190 
191  get_current_tile_pos(ctx, &x0, &y0);
192  ff_copy_rectangle2(&tile->draw,
193  tile->out_ref->data, tile->out_ref->linesize,
194  picref->data, picref->linesize,
195  x0, y0, 0, 0, inlink->w, inlink->h);
196 
197  av_frame_free(&picref);
198  if (++tile->current == tile->nb_frames)
199  return end_last_frame(ctx);
200 
201  return 0;
202 }
203 
204 static int request_frame(AVFilterLink *outlink)
205 {
206  AVFilterContext *ctx = outlink->src;
207  TileContext *tile = ctx->priv;
208  AVFilterLink *inlink = ctx->inputs[0];
209  int r;
210 
211  r = ff_request_frame(inlink);
212  if (r == AVERROR_EOF && tile->current)
213  r = end_last_frame(ctx);
214  return r;
215 }
216 
217 static const AVFilterPad tile_inputs[] = {
218  {
219  .name = "default",
220  .type = AVMEDIA_TYPE_VIDEO,
221  .filter_frame = filter_frame,
222  },
223  { NULL }
224 };
225 
226 static const AVFilterPad tile_outputs[] = {
227  {
228  .name = "default",
229  .type = AVMEDIA_TYPE_VIDEO,
230  .config_props = config_props,
231  .request_frame = request_frame,
232  },
233  { NULL }
234 };
235 
237  .name = "tile",
238  .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
239  .init = init,
240  .query_formats = query_formats,
241  .priv_size = sizeof(TileContext),
242  .inputs = tile_inputs,
243  .outputs = tile_outputs,
244  .priv_class = &tile_class,
245 };
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:518
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
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:231
AVOption.
Definition: opt.h:253
static int request_frame(AVFilterLink *outlink)
Definition: vf_tile.c:204
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
unsigned padding
Definition: vf_tile.c:38
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define REASONABLE_SIZE
Definition: vf_tile.c:47
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
Definition: vf_tile.c:166
#define av_cold
Definition: avcodec.h:653
const char * name
Pad name.
Definition: internal.h:66
unsigned nb_frames
Definition: vf_tile.c:40
static int end_last_frame(AVFilterContext *ctx)
Definition: vf_tile.c:148
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
uint8_t
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only &quot;metadata&quot; fields from src to dst.
Definition: frame.c:446
static int config_props(AVFilterLink *outlink)
Definition: vf_tile.c:94
AVRational av_mul_q(AVRational b, AVRational c) av_const
Multiply two rationals.
Definition: rational.c:79
static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
Definition: vf_tile.c:125
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:531
static const AVFilterPad tile_inputs[]
Definition: vf_tile.c:217
A filter pad used for either input or output.
Definition: internal.h:60
int width
width and height of the video frame
Definition: frame.h:145
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
Frame requests may need to loop in order to be fulfilled.
Definition: internal.h:347
static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
Definition: vf_tile.c:136
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
const char * r
Definition: vf_curves.c:103
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:191
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
unsigned current
Definition: vf_tile.c:39
static int query_formats(AVFilterContext *ctx)
Definition: vf_tile.c:88
float y
ret
Definition: avfilter.c:961
static const AVFilterPad tile_outputs[]
Definition: vf_tile.c:226
static av_cold int init(AVFilterContext *ctx)
Definition: vf_tile.c:67
Main libavfilter public API header.
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
misc drawing utilities
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:464
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
AVFilter avfilter_vf_tile
Definition: vf_tile.c:236
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:635
rational number numerator/denominator
Definition: rational.h:43
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Init a draw context.
Definition: drawutils.c:149
AVFrame * out_ref
Definition: vf_tile.c:43
offset must point to two consecutive integers
Definition: opt.h:230
unsigned h
Definition: vf_tile.c:36
#define AVERROR_EOF
unsigned w
Definition: vf_tile.c:36
#define FLAGS
Definition: vf_tile.c:50
unsigned margin
Definition: vf_tile.c:37
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
FFDrawColor blank
Definition: vf_tile.c:42
static const AVOption tile_options[]
Definition: vf_tile.c:52
#define AVERROR(e)
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:253
An instance of a filter.
Definition: avfilter.h:627
int height
Definition: frame.h:145
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:335
internal API functions
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
#define OFFSET(x)
Definition: vf_tile.c:49
FFDrawContext draw
Definition: vf_tile.c:41