2 * Copyright (c) 2020 Nicolas George
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
28 typedef struct UntileContext {
34 const AVPixFmtDescriptor *desc;
39 #define OFFSET(x) offsetof(UntileContext, x)
40 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
42 static const AVOption untile_options[] = {
43 { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
44 {.str = "6x5"}, 0, 0, FLAGS },
48 AVFILTER_DEFINE_CLASS(untile);
50 static av_cold int init(AVFilterContext *ctx)
52 UntileContext *s = ctx->priv;
54 if (s->w > UINT_MAX / s->h) {
55 av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
57 return AVERROR(EINVAL);
59 s->nb_frames = s->w * s->h;
63 static int query_formats(AVFilterContext *ctx)
65 AVFilterFormats *formats = NULL;
68 ret = ff_formats_pixdesc_filter(&formats, 0,
69 AV_PIX_FMT_FLAG_HWACCEL |
70 AV_PIX_FMT_FLAG_BITSTREAM |
71 FF_PIX_FMT_FLAG_SW_FLAT_SUB);
74 return ff_set_common_formats(ctx, formats);
77 static int config_output(AVFilterLink *outlink)
79 AVFilterContext *ctx = outlink->src;
80 UntileContext *s = ctx->priv;
81 AVFilterLink *inlink = ctx->inputs[0];
84 s->desc = av_pix_fmt_desc_get(outlink->format);
85 if (inlink->w % (s->w << s->desc->log2_chroma_w) ||
86 inlink->h % (s->h << s->desc->log2_chroma_h)) {
87 av_log(ctx, AV_LOG_ERROR,
88 "Input resolution %ux%u not multiple of layout %ux%u.\n",
89 inlink->w, inlink->h, s->w, s->h);
90 return AVERROR(EINVAL);
92 outlink->w = inlink->w / s->w;
93 outlink->h = inlink->h / s->h;
94 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
95 outlink->frame_rate = av_mul_q(inlink->frame_rate, av_make_q(s->nb_frames, 1));
96 if (outlink->frame_rate.num)
97 dt = av_inv_q(outlink->frame_rate);
99 dt = av_mul_q(inlink->time_base, av_make_q(1, s->nb_frames));
100 outlink->time_base = av_gcd_q(inlink->time_base, dt, AV_TIME_BASE / 2, AV_TIME_BASE_Q);
101 s->dpts = av_rescale_q(1, dt, outlink->time_base);
102 av_log(ctx, AV_LOG_VERBOSE, "frame interval: %"PRId64"*%d/%d\n",
103 s->dpts, dt.num, dt.den);
104 av_image_fill_max_pixsteps(s->max_step, NULL, s->desc);
108 static int activate(AVFilterContext *ctx)
110 UntileContext *s = ctx->priv;
111 AVFilterLink *inlink = ctx->inputs[0];
112 AVFilterLink *outlink = ctx->outputs[0];
116 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
118 ret = ff_inlink_consume_frame(inlink, &s->frame);
122 s->pts = av_rescale_q(s->frame->pts, inlink->time_base, outlink->time_base);
125 if (s->current == s->nb_frames - 1) {
129 out = av_frame_clone(s->frame);
131 return AVERROR(ENOMEM);
133 x = outlink->w * (s->current % s->w);
134 y = outlink->h * (s->current / s->w);
135 out->width = outlink->w;
136 out->height = outlink->h;
137 out->data[0] += y * out->linesize[0];
138 out->data[0] += x * s->max_step[0];
139 if (!(s->desc->flags & AV_PIX_FMT_FLAG_PAL || s->desc->flags & FF_PSEUDOPAL)) {
140 for (i = 1; i < 3; i ++) {
142 out->data[i] += (y >> s->desc->log2_chroma_w) * out->linesize[i];
143 out->data[i] += (x >> s->desc->log2_chroma_h) * s->max_step[i];
148 out->data[3] += y * out->linesize[3];
149 out->data[3] += x * s->max_step[3];
153 if (++s->current == s->nb_frames)
155 return ff_filter_frame(outlink, out);
157 FF_FILTER_FORWARD_STATUS(inlink, outlink);
158 FF_FILTER_FORWARD_WANTED(outlink, inlink);
159 return FFERROR_NOT_READY;
163 static av_cold void uninit(AVFilterContext *ctx)
165 UntileContext *s = ctx->priv;
167 av_frame_free(&s->frame);
170 static const AVFilterPad untile_inputs[] = {
173 .type = AVMEDIA_TYPE_VIDEO,
178 static const AVFilterPad untile_outputs[] = {
181 .type = AVMEDIA_TYPE_VIDEO,
182 .config_props = config_output,
187 AVFilter ff_vf_untile = {
189 .description = NULL_IF_CONFIG_SMALL("Untile a frame into a sequence of frames."),
192 .query_formats = query_formats,
193 .activate = activate,
194 .priv_size = sizeof(UntileContext),
195 .inputs = untile_inputs,
196 .outputs = untile_outputs,
197 .priv_class = &untile_class,