FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_shuffleplanes.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 48 56 85.7%
Functions: 3 3 100.0%
Branches: 37 40 92.5%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/avstring.h"
20 #include "libavutil/common.h"
21 #include "libavutil/internal.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/pixfmt.h"
25
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "video.h"
29
30 typedef struct ShufflePlanesContext {
31 const AVClass *class;
32
33 /* number of planes in the selected pixel format */
34 int planes;
35
36 /* mapping indices */
37 int map[4];
38
39 /* set to 1 if some plane is used more than once, so we need to make a copy */
40 int copy;
41 } ShufflePlanesContext;
42
43 2 static int query_formats(AVFilterContext *ctx)
44 {
45 2 AVFilterFormats *formats = NULL;
46 2 ShufflePlanesContext *s = ctx->priv;
47 int fmt, ret, i;
48
49
2/2
✓ Branch 1 taken 444 times.
✓ Branch 2 taken 2 times.
446 for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
50 444 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
51 444 int planes = av_pix_fmt_count_planes(fmt);
52
53
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 2 times.
444 if (!(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
54
2/2
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 28 times.
442 !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
55
2/2
✓ Branch 0 taken 1282 times.
✓ Branch 1 taken 227 times.
1509 for (i = 0; i < 4; i++) {
56
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 1167 times.
1282 if (s->map[i] >= planes)
57 115 break;
58
59
4/4
✓ Branch 0 taken 987 times.
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 183 times.
✓ Branch 3 taken 804 times.
1167 if ((desc->log2_chroma_h || desc->log2_chroma_w) &&
60
10/10
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 121 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 193 times.
✓ Branch 4 taken 314 times.
✓ Branch 5 taken 49 times.
✓ Branch 6 taken 49 times.
✓ Branch 7 taken 265 times.
✓ Branch 8 taken 72 times.
✓ Branch 9 taken 291 times.
363 (i == 1 || i == 2) != (s->map[i] == 1 || s->map[i] == 2))
61 72 break;
62 }
63
64
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 227 times.
414 if (i != 4)
65 187 continue;
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
227 if ((ret = ff_add_format(&formats, fmt)) < 0) {
67 return ret;
68 }
69 }
70 }
71
72 2 return ff_set_common_formats(ctx, formats);
73 }
74
75 2 static av_cold int shuffleplanes_config_input(AVFilterLink *inlink)
76 {
77 2 AVFilterContext *ctx = inlink->dst;
78 2 ShufflePlanesContext *s = ctx->priv;
79 2 int used[4] = { 0 };
80 int i;
81
82 2 s->copy = 0;
83 2 s->planes = av_pix_fmt_count_planes(inlink->format);
84
85
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 for (i = 0; i < s->planes; i++) {
86
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (used[s->map[i]])
87 3 s->copy = 1;
88 7 used[s->map[i]]++;
89 }
90
91 2 return 0;
92 }
93
94 100 static int shuffleplanes_filter_frame(AVFilterLink *inlink, AVFrame *frame)
95 {
96 100 AVFilterContext *ctx = inlink->dst;
97 100 ShufflePlanesContext *s = ctx->priv;
98 100 uint8_t *shuffled_data[4] = { NULL };
99 100 int shuffled_linesize[4] = { 0 };
100 int i, ret;
101
102
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 100 times.
450 for (i = 0; i < s->planes; i++) {
103 350 shuffled_data[i] = frame->data[s->map[i]];
104 350 shuffled_linesize[i] = frame->linesize[s->map[i]];
105 }
106 100 memcpy(frame->data, shuffled_data, sizeof(shuffled_data));
107 100 memcpy(frame->linesize, shuffled_linesize, sizeof(shuffled_linesize));
108
109
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 50 times.
100 if (s->copy) {
110 50 AVFrame *copy = ff_get_video_buffer(ctx->outputs[0], frame->width, frame->height);
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (!copy) {
113 ret = AVERROR(ENOMEM);
114 goto fail;
115 }
116
117 50 av_frame_copy(copy, frame);
118
119 50 ret = av_frame_copy_props(copy, frame);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0) {
121 av_frame_free(&copy);
122 goto fail;
123 }
124
125 50 av_frame_free(&frame);
126 50 frame = copy;
127 }
128
129 100 return ff_filter_frame(ctx->outputs[0], frame);
130 fail:
131 av_frame_free(&frame);
132 return ret;
133 }
134
135 #define OFFSET(x) offsetof(ShufflePlanesContext, x)
136 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
137 static const AVOption shuffleplanes_options[] = {
138 { "map0", "Index of the input plane to be used as the first output plane ", OFFSET(map[0]), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
139 { "map1", "Index of the input plane to be used as the second output plane ", OFFSET(map[1]), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 3, FLAGS },
140 { "map2", "Index of the input plane to be used as the third output plane ", OFFSET(map[2]), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, 3, FLAGS },
141 { "map3", "Index of the input plane to be used as the fourth output plane ", OFFSET(map[3]), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 3, FLAGS },
142 { NULL },
143 };
144
145 AVFILTER_DEFINE_CLASS(shuffleplanes);
146
147 static const AVFilterPad shuffleplanes_inputs[] = {
148 {
149 .name = "default",
150 .type = AVMEDIA_TYPE_VIDEO,
151 .config_props = shuffleplanes_config_input,
152 .filter_frame = shuffleplanes_filter_frame,
153 },
154 };
155
156 static const AVFilterPad shuffleplanes_outputs[] = {
157 {
158 .name = "default",
159 .type = AVMEDIA_TYPE_VIDEO,
160 },
161 };
162
163 const AVFilter ff_vf_shuffleplanes = {
164 .name = "shuffleplanes",
165 .description = NULL_IF_CONFIG_SMALL("Shuffle video planes."),
166 .priv_size = sizeof(ShufflePlanesContext),
167 .priv_class = &shuffleplanes_class,
168 FILTER_INPUTS(shuffleplanes_inputs),
169 FILTER_OUTPUTS(shuffleplanes_outputs),
170 FILTER_QUERY_FUNC(query_formats),
171 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
172 };
173