]> 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 #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 ff_start_frame(outlink, outlink->out_buf);
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)
133 {
134     TileContext *tile    = ctx->priv;
135     AVFilterLink *inlink  = ctx->inputs[0];
136     AVFilterLink *outlink = ctx->outputs[0];
137     unsigned x0 = inlink->w * (tile->current % tile->w);
138     unsigned y0 = inlink->h * (tile->current / tile->w);
139
140     ff_fill_rectangle(&tile->draw, &tile->blank,
141                       outlink->out_buf->data, outlink->out_buf->linesize,
142                       x0, y0, inlink->w, inlink->h);
143     tile->current++;
144 }
145 static void end_last_frame(AVFilterContext *ctx)
146 {
147     TileContext *tile    = ctx->priv;
148     AVFilterLink *outlink = ctx->outputs[0];
149
150     while (tile->current < tile->w * tile->h)
151         draw_blank_frame(ctx);
152     ff_draw_slice(outlink, 0, outlink->out_buf->video->h, 1);
153     ff_end_frame(outlink);
154     tile->current = 0;
155 }
156
157 static int end_frame(AVFilterLink *inlink)
158 {
159     AVFilterContext *ctx  = inlink->dst;
160     TileContext *tile    = ctx->priv;
161
162     avfilter_unref_bufferp(&inlink->cur_buf);
163     if (++tile->current == tile->w * tile->h)
164         end_last_frame(ctx);
165     return 0;
166 }
167
168 static int request_frame(AVFilterLink *outlink)
169 {
170     AVFilterContext *ctx = outlink->src;
171     TileContext *tile   = ctx->priv;
172     AVFilterLink *inlink = ctx->inputs[0];
173     int r;
174
175     while (1) {
176         r = ff_request_frame(inlink);
177         if (r < 0) {
178             if (r == AVERROR_EOF && tile->current)
179                 end_last_frame(ctx);
180             else
181                 return r;
182             break;
183         }
184         if (!tile->current) /* done */
185             break;
186     }
187     return 0;
188 }
189
190
191 AVFilter avfilter_vf_tile = {
192     .name          = "tile",
193     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
194     .init          = init,
195     .query_formats = query_formats,
196     .priv_size     = sizeof(TileContext),
197     .inputs = (const AVFilterPad[]) {
198         { .name        = "default",
199           .type        = AVMEDIA_TYPE_VIDEO,
200           .start_frame = start_frame,
201           .draw_slice  = draw_slice,
202           .end_frame   = end_frame,
203           .min_perms   = AV_PERM_READ, },
204         { .name = NULL }
205     },
206     .outputs = (const AVFilterPad[]) {
207         { .name          = "default",
208           .type          = AVMEDIA_TYPE_VIDEO,
209           .config_props  = config_props,
210           .request_frame = request_frame },
211         { .name = NULL }
212     },
213 };