]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tile.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_tile.c
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/pixdesc.h"
27 #include "avfilter.h"
28 #include "drawutils.h"
29
30 typedef struct {
31     unsigned w, h;
32     unsigned current;
33     FFDrawContext draw;
34     FFDrawColor blank;
35 } TileContext;
36
37 #define REASONABLE_SIZE 1024
38
39 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
40 {
41     TileContext *tile = ctx->priv;
42     int r;
43     char dummy;
44
45     if (!args)
46         args = "6x5";
47     r = sscanf(args, "%ux%u%c", &tile->w, &tile->h, &dummy);
48     if (r != 2 || !tile->w || !tile->h)
49         return AVERROR(EINVAL);
50     if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
51         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
52                tile->w, tile->h);
53         return AVERROR(EINVAL);
54     }
55     return 0;
56 }
57
58 static int query_formats(AVFilterContext *ctx)
59 {
60     avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
61     return 0;
62 }
63
64 static int config_props(AVFilterLink *outlink)
65 {
66     AVFilterContext *ctx = outlink->src;
67     TileContext *tile   = ctx->priv;
68     AVFilterLink *inlink = ctx->inputs[0];
69
70     if (inlink->w > INT_MAX / tile->w) {
71         av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
72                tile->w, inlink->w);
73         return AVERROR(EINVAL);
74     }
75     if (inlink->h > INT_MAX / tile->h) {
76         av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
77                tile->h, inlink->h);
78         return AVERROR(EINVAL);
79     }
80     outlink->w = tile->w * inlink->w;
81     outlink->h = tile->h * inlink->h;
82     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
83     ff_draw_init(&tile->draw, inlink->format, 0);
84     /* TODO make the color an option, or find an unified way of choosing it */
85     ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
86
87     return 0;
88 }
89
90 /* Note: direct rendering is not possible since there is no guarantee that
91  * buffers are fed to start_frame in the order they were obtained from
92  * get_buffer (think B-frames). */
93
94 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
95 {
96     AVFilterContext *ctx  = inlink->dst;
97     TileContext *tile    = ctx->priv;
98     AVFilterLink *outlink = ctx->outputs[0];
99
100     if (tile->current)
101         return;
102     outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
103                                                  outlink->w, outlink->h);
104     avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
105     outlink->out_buf->video->w = outlink->w;
106     outlink->out_buf->video->h = outlink->h;
107     avfilter_start_frame(outlink, outlink->out_buf);
108 }
109
110 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
111 {
112     AVFilterContext *ctx  = inlink->dst;
113     TileContext *tile    = ctx->priv;
114     AVFilterLink *outlink = ctx->outputs[0];
115     unsigned x0 = inlink->w * (tile->current % tile->w);
116     unsigned y0 = inlink->h * (tile->current / tile->w);
117
118     ff_copy_rectangle2(&tile->draw,
119                        outlink->out_buf->data, outlink->out_buf->linesize,
120                        inlink ->cur_buf->data, inlink ->cur_buf->linesize,
121                        x0, y0 + y, 0, y, inlink->cur_buf->video->w, h);
122     /* TODO if tile->w == 1 && slice_dir is always 1, we could draw_slice
123      * immediately. */
124 }
125
126 static void draw_blank_frame(AVFilterContext *ctx)
127 {
128     TileContext *tile    = ctx->priv;
129     AVFilterLink *inlink  = ctx->inputs[0];
130     AVFilterLink *outlink = ctx->outputs[0];
131     unsigned x0 = inlink->w * (tile->current % tile->w);
132     unsigned y0 = inlink->h * (tile->current / tile->w);
133
134     ff_fill_rectangle(&tile->draw, &tile->blank,
135                       outlink->out_buf->data, outlink->out_buf->linesize,
136                       x0, y0, inlink->w, inlink->h);
137     tile->current++;
138 }
139 static void end_last_frame(AVFilterContext *ctx)
140 {
141     TileContext *tile    = ctx->priv;
142     AVFilterLink *outlink = ctx->outputs[0];
143
144     while (tile->current < tile->w * tile->h)
145         draw_blank_frame(ctx);
146     avfilter_draw_slice(outlink, 0, outlink->out_buf->video->h, 1);
147     avfilter_end_frame(outlink);
148     tile->current = 0;
149 }
150
151 static void end_frame(AVFilterLink *inlink)
152 {
153     AVFilterContext *ctx  = inlink->dst;
154     TileContext *tile    = ctx->priv;
155
156     avfilter_unref_buffer(inlink->cur_buf);
157     if (++tile->current == tile->w * tile->h)
158         end_last_frame(ctx);
159 }
160
161 static int request_frame(AVFilterLink *outlink)
162 {
163     AVFilterContext *ctx = outlink->src;
164     TileContext *tile   = ctx->priv;
165     AVFilterLink *inlink = ctx->inputs[0];
166     int r;
167
168     while (1) {
169         r = avfilter_request_frame(inlink);
170         if (r < 0) {
171             if (r == AVERROR_EOF && tile->current)
172                 end_last_frame(ctx);
173             else
174                 return r;
175             break;
176         }
177         if (!tile->current) /* done */
178             break;
179     }
180     return 0;
181 }
182
183
184 AVFilter avfilter_vf_tile = {
185     .name          = "tile",
186     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
187     .init          = init,
188     .query_formats = query_formats,
189     .priv_size     = sizeof(TileContext),
190     .inputs = (const AVFilterPad[]) {
191         { .name        = "default",
192           .type        = AVMEDIA_TYPE_VIDEO,
193           .start_frame = start_frame,
194           .draw_slice  = draw_slice,
195           .end_frame   = end_frame,
196           .min_perms   = AV_PERM_READ, },
197         { .name = NULL }
198     },
199     .outputs = (const AVFilterPad[]) {
200         { .name          = "default",
201           .type          = AVMEDIA_TYPE_VIDEO,
202           .config_props  = config_props,
203           .request_frame = request_frame },
204         { .name = NULL }
205     },
206 };