]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tile.c
Merge remote-tracking branch 'cus/stable'
[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 int 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 0;
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     return 0;
113 }
114
115 static int 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     return 0;
130 }
131
132 static void draw_blank_frame(AVFilterContext *ctx, AVFilterBufferRef *out_buf)
133 {
134     TileContext *tile    = ctx->priv;
135     AVFilterLink *inlink  = ctx->inputs[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                       out_buf->data, 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     AVFilterBufferRef *out_buf = outlink->out_buf;
149
150     outlink->out_buf = NULL;
151     ff_start_frame(outlink, out_buf);
152     while (tile->current < tile->w * tile->h)
153         draw_blank_frame(ctx, out_buf);
154     ff_draw_slice(outlink, 0, out_buf->video->h, 1);
155     ff_end_frame(outlink);
156     tile->current = 0;
157 }
158
159 static int end_frame(AVFilterLink *inlink)
160 {
161     AVFilterContext *ctx  = inlink->dst;
162     TileContext *tile    = ctx->priv;
163
164     avfilter_unref_bufferp(&inlink->cur_buf);
165     if (++tile->current == tile->w * tile->h)
166         end_last_frame(ctx);
167     return 0;
168 }
169
170 static int request_frame(AVFilterLink *outlink)
171 {
172     AVFilterContext *ctx = outlink->src;
173     TileContext *tile   = ctx->priv;
174     AVFilterLink *inlink = ctx->inputs[0];
175     int r;
176
177     while (1) {
178         r = ff_request_frame(inlink);
179         if (r < 0) {
180             if (r == AVERROR_EOF && tile->current)
181                 end_last_frame(ctx);
182             else
183                 return r;
184             break;
185         }
186         if (!tile->current) /* done */
187             break;
188     }
189     return 0;
190 }
191
192
193 AVFilter avfilter_vf_tile = {
194     .name          = "tile",
195     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
196     .init          = init,
197     .query_formats = query_formats,
198     .priv_size     = sizeof(TileContext),
199     .inputs = (const AVFilterPad[]) {
200         { .name        = "default",
201           .type        = AVMEDIA_TYPE_VIDEO,
202           .start_frame = start_frame,
203           .draw_slice  = draw_slice,
204           .end_frame   = end_frame,
205           .min_perms   = AV_PERM_READ, },
206         { .name = NULL }
207     },
208     .outputs = (const AVFilterPad[]) {
209         { .name          = "default",
210           .type          = AVMEDIA_TYPE_VIDEO,
211           .config_props  = config_props,
212           .request_frame = request_frame },
213         { .name = NULL }
214     },
215 };