FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_vflip.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 27 50 54.0%
Branches: 18 26 69.2%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2007 Bobby Bingham
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 * video vertical flip filter
24 */
25
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "internal.h"
31 #include "video.h"
32
33 typedef struct FlipContext {
34 const AVClass *class;
35 int vsub; ///< vertical chroma subsampling
36 int bayer;
37 } FlipContext;
38
39 static const AVOption vflip_options[] = {
40 { NULL }
41 };
42
43 AVFILTER_DEFINE_CLASS(vflip);
44
45 187 static int config_input(AVFilterLink *link)
46 {
47 187 FlipContext *flip = link->dst->priv;
48 187 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
49
50 187 flip->vsub = desc->log2_chroma_h;
51 187 flip->bayer = !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
52
53 187 return 0;
54 }
55
56 187 static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
57 {
58 187 FlipContext *flip = link->dst->priv;
59 AVFrame *frame;
60 int i;
61
62 187 frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
187 if (!frame)
64 return NULL;
65
66
2/2
✓ Branch 0 taken 748 times.
✓ Branch 1 taken 187 times.
935 for (i = 0; i < 4; i ++) {
67
4/4
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 187 times.
✓ Branch 2 taken 187 times.
✓ Branch 3 taken 374 times.
748 int vsub = i == 1 || i == 2 ? flip->vsub : 0;
68 748 int height = AV_CEIL_RSHIFT(h, vsub);
69
70
2/2
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 305 times.
748 if (frame->data[i]) {
71 443 frame->data[i] += (height - 1) * frame->linesize[i];
72 443 frame->linesize[i] = -frame->linesize[i];
73 }
74 }
75
76 187 return frame;
77 }
78
79 static int flip_bayer(AVFilterLink *link, AVFrame *in)
80 {
81 AVFilterContext *ctx = link->dst;
82 AVFilterLink *outlink = ctx->outputs[0];
83 AVFrame *out;
84 uint8_t *inrow = in->data[0], *outrow;
85 int i, width = outlink->w << (av_pix_fmt_desc_get(link->format)->comp[0].step > 1);
86 if (outlink->h & 1) {
87 av_log(ctx, AV_LOG_ERROR, "Bayer vertical flip needs even height\n");
88 return AVERROR_INVALIDDATA;
89 }
90
91 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
92 if (!out) {
93 av_frame_free(&in);
94 return AVERROR(ENOMEM);
95 }
96 av_frame_copy_props(out, in);
97 outrow = out->data[0] + out->linesize[0] * (outlink->h - 2);
98 for (i = 0; i < outlink->h >> 1; i++) {
99 memcpy(outrow, inrow, width);
100 memcpy(outrow + out->linesize[0], inrow + in->linesize[0], width);
101 inrow += 2 * in->linesize[0];
102 outrow -= 2 * out->linesize[0];
103 }
104 av_frame_free(&in);
105 return ff_filter_frame(outlink, out);
106 }
107
108 223 static int filter_frame(AVFilterLink *link, AVFrame *frame)
109 {
110 223 FlipContext *flip = link->dst->priv;
111 int i;
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 223 times.
223 if (flip->bayer)
114 return flip_bayer(link, frame);
115
116
2/2
✓ Branch 0 taken 892 times.
✓ Branch 1 taken 223 times.
1115 for (i = 0; i < 4; i ++) {
117
4/4
✓ Branch 0 taken 669 times.
✓ Branch 1 taken 223 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 446 times.
892 int vsub = i == 1 || i == 2 ? flip->vsub : 0;
118 892 int height = AV_CEIL_RSHIFT(link->h, vsub);
119
120
2/2
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 341 times.
892 if (frame->data[i]) {
121 551 frame->data[i] += (height - 1) * frame->linesize[i];
122 551 frame->linesize[i] = -frame->linesize[i];
123 }
124 }
125
126 223 return ff_filter_frame(link->dst->outputs[0], frame);
127 }
128 static const AVFilterPad avfilter_vf_vflip_inputs[] = {
129 {
130 .name = "default",
131 .type = AVMEDIA_TYPE_VIDEO,
132 .get_buffer.video = get_video_buffer,
133 .filter_frame = filter_frame,
134 .config_props = config_input,
135 },
136 };
137
138 static const AVFilterPad avfilter_vf_vflip_outputs[] = {
139 {
140 .name = "default",
141 .type = AVMEDIA_TYPE_VIDEO,
142 },
143 };
144
145 const AVFilter ff_vf_vflip = {
146 .name = "vflip",
147 .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
148 .priv_size = sizeof(FlipContext),
149 .priv_class = &vflip_class,
150 FILTER_INPUTS(avfilter_vf_vflip_inputs),
151 FILTER_OUTPUTS(avfilter_vf_vflip_outputs),
152 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
153 };
154