FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_il.c
Date: 2024-04-23 06:12:56
Exec Total Coverage
Lines: 42 55 76.4%
Functions: 4 4 100.0%
Branches: 9 18 50.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * (de)interleave fields filter
25 */
26
27 #include "libavutil/opt.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34
35 enum FilterMode {
36 MODE_NONE,
37 MODE_INTERLEAVE,
38 MODE_DEINTERLEAVE
39 };
40
41 typedef struct IlContext {
42 const AVClass *class;
43 int luma_mode, chroma_mode, alpha_mode; ///<FilterMode
44 int luma_swap, chroma_swap, alpha_swap;
45 int nb_planes;
46 int linesize[4], chroma_height;
47 int has_alpha;
48 } IlContext;
49
50 #define OFFSET(x) offsetof(IlContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
52
53 static const AVOption il_options[] = {
54 {"luma_mode", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "luma_mode"},
55 {"l", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "luma_mode"},
56 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, .unit = "luma_mode"},
57 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
58 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
59 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
60 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
61 {"chroma_mode", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "chroma_mode"},
62 {"c", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "chroma_mode"},
63 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, .unit = "chroma_mode"},
64 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
65 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
66 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
67 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
68 {"alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "alpha_mode"},
69 {"a", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "alpha_mode"},
70 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, .unit = "alpha_mode"},
71 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
72 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
73 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
74 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
75 {"luma_swap", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
76 {"ls", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
77 {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
78 {"cs", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
79 {"alpha_swap", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
80 {"as", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
81 {NULL}
82 };
83
84 AVFILTER_DEFINE_CLASS(il);
85
86 184 static int query_formats(AVFilterContext *ctx)
87 {
88 184 int reject_flags = AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL;
89
90 184 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
91 }
92
93 183 static int config_input(AVFilterLink *inlink)
94 {
95 183 IlContext *s = inlink->dst->priv;
96 183 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
97 int ret;
98
99 183 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
100
101 183 s->has_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 183 times.
183 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
103 return ret;
104
105 183 s->chroma_height = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
106
107 183 return 0;
108 }
109
110 443 static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
111 int dst_linesize, int src_linesize,
112 enum FilterMode mode, int swap)
113 {
114 443 const int a = swap;
115 443 const int b = 1 - a;
116 443 const int m = h >> 1;
117 int y;
118
119
1/4
✓ Branch 0 taken 443 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
443 switch (mode) {
120 443 case MODE_DEINTERLEAVE:
121
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 443 times.
59843 for (y = 0; y < m; y++) {
122 59400 memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
123 59400 memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
124 }
125 443 break;
126 case MODE_NONE:
127 for (y = 0; y < m; y++) {
128 memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
129 memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
130 }
131 break;
132 case MODE_INTERLEAVE:
133 for (y = 0; y < m; y++) {
134 memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
135 memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
136 }
137 break;
138 }
139 443 }
140
141 183 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
142 {
143 183 IlContext *s = inlink->dst->priv;
144 183 AVFilterLink *outlink = inlink->dst->outputs[0];
145 AVFrame *out;
146 int comp;
147
148 183 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (!out) {
150 av_frame_free(&inpicref);
151 return AVERROR(ENOMEM);
152 }
153 183 av_frame_copy_props(out, inpicref);
154
155 183 interleave(out->data[0], inpicref->data[0],
156 s->linesize[0], inlink->h,
157 183 out->linesize[0], inpicref->linesize[0],
158 183 s->luma_mode, s->luma_swap);
159
160
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 183 times.
394 for (comp = 1; comp < (s->nb_planes - s->has_alpha); comp++) {
161 211 interleave(out->data[comp], inpicref->data[comp],
162 s->linesize[comp], s->chroma_height,
163 211 out->linesize[comp], inpicref->linesize[comp],
164 211 s->chroma_mode, s->chroma_swap);
165 }
166
167
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 134 times.
183 if (s->has_alpha) {
168 49 comp = s->nb_planes - 1;
169 49 interleave(out->data[comp], inpicref->data[comp],
170 s->linesize[comp], inlink->h,
171 49 out->linesize[comp], inpicref->linesize[comp],
172 49 s->alpha_mode, s->alpha_swap);
173 }
174
175 183 av_frame_free(&inpicref);
176 183 return ff_filter_frame(outlink, out);
177 }
178
179 static const AVFilterPad inputs[] = {
180 {
181 .name = "default",
182 .type = AVMEDIA_TYPE_VIDEO,
183 .filter_frame = filter_frame,
184 .config_props = config_input,
185 },
186 };
187
188 const AVFilter ff_vf_il = {
189 .name = "il",
190 .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
191 .priv_size = sizeof(IlContext),
192 FILTER_INPUTS(inputs),
193 FILTER_OUTPUTS(ff_video_default_filterpad),
194 FILTER_QUERY_FUNC(query_formats),
195 .priv_class = &il_class,
196 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
197 .process_command = ff_filter_process_command,
198 };
199