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