• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "filters.h"
27 
28 typedef struct UntileContext {
29     const AVClass *class;
30     unsigned w, h;
31     unsigned current;
32     unsigned nb_frames;
33     AVFrame *frame;
34     const AVPixFmtDescriptor *desc;
35     int64_t dpts, pts;
36     int max_step[4];
37 } UntileContext;
38 
39 #define OFFSET(x) offsetof(UntileContext, x)
40 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
41 
42 static const AVOption untile_options[] = {
43     { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
44         {.str = "6x5"}, 0, 0, FLAGS },
45     { NULL }
46 };
47 
48 AVFILTER_DEFINE_CLASS(untile);
49 
init(AVFilterContext * ctx)50 static av_cold int init(AVFilterContext *ctx)
51 {
52     UntileContext *s = ctx->priv;
53 
54     if (s->w > UINT_MAX / s->h) {
55         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
56                s->w, s->h);
57         return AVERROR(EINVAL);
58     }
59     s->nb_frames = s->w * s->h;
60     return 0;
61 }
62 
query_formats(AVFilterContext * ctx)63 static int query_formats(AVFilterContext *ctx)
64 {
65     int reject_flags = AV_PIX_FMT_FLAG_HWACCEL   |
66                        AV_PIX_FMT_FLAG_BITSTREAM |
67                        FF_PIX_FMT_FLAG_SW_FLAT_SUB;
68 
69     return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
70 }
71 
config_output(AVFilterLink * outlink)72 static int config_output(AVFilterLink *outlink)
73 {
74     AVFilterContext *ctx = outlink->src;
75     UntileContext *s = ctx->priv;
76     AVFilterLink *inlink = ctx->inputs[0];
77     AVRational dt;
78 
79     s->desc = av_pix_fmt_desc_get(outlink->format);
80     if (inlink->w % (s->w << s->desc->log2_chroma_w) ||
81         inlink->h % (s->h << s->desc->log2_chroma_h)) {
82         av_log(ctx, AV_LOG_ERROR,
83                "Input resolution %ux%u not multiple of layout %ux%u.\n",
84                inlink->w, inlink->h, s->w, s->h);
85         return AVERROR(EINVAL);
86     }
87     outlink->w = inlink->w / s->w;
88     outlink->h = inlink->h / s->h;
89     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
90     outlink->frame_rate = av_mul_q(inlink->frame_rate, av_make_q(s->nb_frames, 1));
91     if (outlink->frame_rate.num)
92         dt = av_inv_q(outlink->frame_rate);
93     else
94         dt = av_mul_q(inlink->time_base, av_make_q(1, s->nb_frames));
95     outlink->time_base = av_gcd_q(inlink->time_base, dt, AV_TIME_BASE / 2, AV_TIME_BASE_Q);
96     s->dpts = av_rescale_q(1, dt, outlink->time_base);
97     av_log(ctx, AV_LOG_VERBOSE, "frame interval: %"PRId64"*%d/%d\n",
98            s->dpts, dt.num, dt.den);
99     av_image_fill_max_pixsteps(s->max_step, NULL, s->desc);
100     return 0;
101 }
102 
activate(AVFilterContext * ctx)103 static int activate(AVFilterContext *ctx)
104 {
105     UntileContext *s = ctx->priv;
106     AVFilterLink *inlink = ctx->inputs[0];
107     AVFilterLink *outlink = ctx->outputs[0];
108     AVFrame *out;
109     int i, x, y, ret;
110 
111     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
112     if (!s->frame) {
113         ret = ff_inlink_consume_frame(inlink, &s->frame);
114         if (ret < 0)
115             return ret;
116         if (ret)
117             s->pts = av_rescale_q(s->frame->pts, inlink->time_base, outlink->time_base);
118     }
119     if (s->frame) {
120         if (s->current == s->nb_frames - 1) {
121             out = s->frame;
122             s->frame = NULL;
123         } else {
124             out = av_frame_clone(s->frame);
125             if (!out)
126                 return AVERROR(ENOMEM);
127         }
128         x = outlink->w * (s->current % s->w);
129         y = outlink->h * (s->current / s->w);
130         out->width = outlink->w;
131         out->height = outlink->h;
132         out->data[0] += y * out->linesize[0];
133         out->data[0] += x * s->max_step[0];
134         if (!(s->desc->flags & AV_PIX_FMT_FLAG_PAL)) {
135             for (i = 1; i < 3; i ++) {
136                 if (out->data[i]) {
137                     out->data[i] += (y >> s->desc->log2_chroma_h) * out->linesize[i];
138                     out->data[i] += (x >> s->desc->log2_chroma_w) * s->max_step[i];
139                 }
140             }
141         }
142         if (out->data[3]) {
143             out->data[3] += y * out->linesize[3];
144             out->data[3] += x * s->max_step[3];
145         }
146         out->pts = s->pts;
147         s->pts += s->dpts;
148         if (++s->current == s->nb_frames)
149             s->current = 0;
150         return ff_filter_frame(outlink, out);
151     }
152     FF_FILTER_FORWARD_STATUS(inlink, outlink);
153     FF_FILTER_FORWARD_WANTED(outlink, inlink);
154     return FFERROR_NOT_READY;
155 
156 }
157 
uninit(AVFilterContext * ctx)158 static av_cold void uninit(AVFilterContext *ctx)
159 {
160     UntileContext *s = ctx->priv;
161 
162     av_frame_free(&s->frame);
163 }
164 
165 static const AVFilterPad untile_inputs[] = {
166     {
167         .name         = "default",
168         .type         = AVMEDIA_TYPE_VIDEO,
169     },
170 };
171 
172 static const AVFilterPad untile_outputs[] = {
173     {
174         .name          = "default",
175         .type          = AVMEDIA_TYPE_VIDEO,
176         .config_props  = config_output,
177     },
178 };
179 
180 const AVFilter ff_vf_untile = {
181     .name          = "untile",
182     .description   = NULL_IF_CONFIG_SMALL("Untile a frame into a sequence of frames."),
183     .init          = init,
184     .uninit        = uninit,
185     .activate      = activate,
186     .priv_size     = sizeof(UntileContext),
187     FILTER_INPUTS(untile_inputs),
188     FILTER_OUTPUTS(untile_outputs),
189     FILTER_QUERY_FUNC(query_formats),
190     .priv_class    = &untile_class,
191 };
192