FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_untile.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 65 75 86.7%
Functions: 5 5 100.0%
Branches: 25 36 69.4%

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