]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tile.c
vf_tile: set frame_rate.
[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     outlink->frame_rate = av_mul_q(inlink->frame_rate,
84                                    (AVRational){ 1, tile->w * tile->h });
85     ff_draw_init(&tile->draw, inlink->format, 0);
86     /* TODO make the color an option, or find an unified way of choosing it */
87     ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
88
89     return 0;
90 }
91
92 /* Note: direct rendering is not possible since there is no guarantee that
93  * buffers are fed to start_frame in the order they were obtained from
94  * get_buffer (think B-frames). */
95
96 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
97 {
98     AVFilterContext *ctx  = inlink->dst;
99     TileContext *tile    = ctx->priv;
100     AVFilterLink *outlink = ctx->outputs[0];
101
102     if (tile->current)
103         return;
104     outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
105                                                  outlink->w, outlink->h);
106     avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
107     outlink->out_buf->video->w = outlink->w;
108     outlink->out_buf->video->h = outlink->h;
109     avfilter_start_frame(outlink, outlink->out_buf);
110 }
111
112 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
113 {
114     AVFilterContext *ctx  = inlink->dst;
115     TileContext *tile    = ctx->priv;
116     AVFilterLink *outlink = ctx->outputs[0];
117     unsigned x0 = inlink->w * (tile->current % tile->w);
118     unsigned y0 = inlink->h * (tile->current / tile->w);
119
120     ff_copy_rectangle2(&tile->draw,
121                        outlink->out_buf->data, outlink->out_buf->linesize,
122                        inlink ->cur_buf->data, inlink ->cur_buf->linesize,
123                        x0, y0 + y, 0, y, inlink->cur_buf->video->w, h);
124     /* TODO if tile->w == 1 && slice_dir is always 1, we could draw_slice
125      * immediately. */
126 }
127
128 static void draw_blank_frame(AVFilterContext *ctx)
129 {
130     TileContext *tile    = ctx->priv;
131     AVFilterLink *inlink  = ctx->inputs[0];
132     AVFilterLink *outlink = ctx->outputs[0];
133     unsigned x0 = inlink->w * (tile->current % tile->w);
134     unsigned y0 = inlink->h * (tile->current / tile->w);
135
136     ff_fill_rectangle(&tile->draw, &tile->blank,
137                       outlink->out_buf->data, outlink->out_buf->linesize,
138                       x0, y0, inlink->w, inlink->h);
139     tile->current++;
140 }
141 static void end_last_frame(AVFilterContext *ctx)
142 {
143     TileContext *tile    = ctx->priv;
144     AVFilterLink *outlink = ctx->outputs[0];
145
146     while (tile->current < tile->w * tile->h)
147         draw_blank_frame(ctx);
148     avfilter_draw_slice(outlink, 0, outlink->out_buf->video->h, 1);
149     avfilter_end_frame(outlink);
150     tile->current = 0;
151 }
152
153 static void end_frame(AVFilterLink *inlink)
154 {
155     AVFilterContext *ctx  = inlink->dst;
156     TileContext *tile    = ctx->priv;
157
158     avfilter_unref_buffer(inlink->cur_buf);
159     if (++tile->current == tile->w * tile->h)
160         end_last_frame(ctx);
161 }
162
163 static int request_frame(AVFilterLink *outlink)
164 {
165     AVFilterContext *ctx = outlink->src;
166     TileContext *tile   = ctx->priv;
167     AVFilterLink *inlink = ctx->inputs[0];
168     int r;
169
170     while (1) {
171         r = avfilter_request_frame(inlink);
172         if (r < 0) {
173             if (r == AVERROR_EOF && tile->current)
174                 end_last_frame(ctx);
175             else
176                 return r;
177             break;
178         }
179         if (!tile->current) /* done */
180             break;
181     }
182     return 0;
183 }
184
185
186 AVFilter avfilter_vf_tile = {
187     .name          = "tile",
188     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
189     .init          = init,
190     .query_formats = query_formats,
191     .priv_size     = sizeof(TileContext),
192     .inputs = (const AVFilterPad[]) {
193         { .name        = "default",
194           .type        = AVMEDIA_TYPE_VIDEO,
195           .start_frame = start_frame,
196           .draw_slice  = draw_slice,
197           .end_frame   = end_frame,
198           .min_perms   = AV_PERM_READ, },
199         { .name = NULL }
200     },
201     .outputs = (const AVFilterPad[]) {
202         { .name          = "default",
203           .type          = AVMEDIA_TYPE_VIDEO,
204           .config_props  = config_props,
205           .request_frame = request_frame },
206         { .name = NULL }
207     },
208 };