FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_fieldorder.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 50 60 83.3%
Functions: 3 3 100.0%
Branches: 33 44 75.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2011 Mark Himsley
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 field order filter, heavily influenced by vf_pad.c
24 */
25
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34
35 typedef struct FieldOrderContext {
36 const AVClass *class;
37 int dst_tff; ///< output bff/tff
38 int line_size[4]; ///< bytes of pixel data per line for each plane
39 } FieldOrderContext;
40
41 143 static int query_formats(AVFilterContext *ctx)
42 {
43 143 const AVPixFmtDescriptor *desc = NULL;
44 AVFilterFormats *formats;
45 int ret;
46
47 /** accept any input pixel format that is not hardware accelerated, not
48 * a bitstream format, and does not have vertically sub-sampled chroma */
49 143 formats = NULL;
50
2/2
✓ Branch 1 taken 31746 times.
✓ Branch 2 taken 143 times.
31889 while ((desc = av_pix_fmt_desc_next(desc))) {
51 31746 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
52
2/2
✓ Branch 0 taken 29744 times.
✓ Branch 1 taken 2002 times.
31746 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
53
2/2
✓ Branch 0 taken 29601 times.
✓ Branch 1 taken 143 times.
29744 desc->flags & AV_PIX_FMT_FLAG_PAL ||
54
2/2
✓ Branch 0 taken 28886 times.
✓ Branch 1 taken 715 times.
29601 desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
55
4/6
✓ Branch 0 taken 28886 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24024 times.
✓ Branch 3 taken 4862 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24024 times.
52910 desc->nb_components && !desc->log2_chroma_h &&
56 24024 (ret = ff_add_format(&formats, pix_fmt)) < 0)
57 return ret;
58 }
59 143 return ff_set_common_formats(ctx, formats);
60 }
61
62 142 static int config_input(AVFilterLink *inlink)
63 {
64 142 AVFilterContext *ctx = inlink->dst;
65 142 FieldOrderContext *s = ctx->priv;
66
67 142 return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
68 }
69
70 166 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
71 {
72 166 AVFilterContext *ctx = inlink->dst;
73 166 FieldOrderContext *s = ctx->priv;
74 166 AVFilterLink *outlink = ctx->outputs[0];
75 int h, plane, src_line_step, dst_line_step, line_size, line;
76 uint8_t *dst, *src;
77 AVFrame *out;
78
79
1/2
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
166 if (!frame->interlaced_frame ||
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 frame->top_field_first == s->dst_tff) {
81 av_log(ctx, AV_LOG_VERBOSE,
82 "Skipping %s.\n",
83 frame->interlaced_frame ?
84 "frame with same field order" : "progressive frame");
85 return ff_filter_frame(outlink, frame);
86 }
87
88
1/2
✓ Branch 1 taken 166 times.
✗ Branch 2 not taken.
166 if (av_frame_is_writable(frame)) {
89 166 out = frame;
90 } else {
91 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
92 if (!out) {
93 av_frame_free(&frame);
94 return AVERROR(ENOMEM);
95 }
96 av_frame_copy_props(out, frame);
97 }
98
99 166 av_log(ctx, AV_LOG_TRACE,
100 "picture will move %s one line\n",
101
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 25 times.
166 s->dst_tff ? "up" : "down");
102 166 h = frame->height;
103
5/6
✓ Branch 0 taken 475 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 336 times.
✓ Branch 3 taken 139 times.
✓ Branch 4 taken 336 times.
✗ Branch 5 not taken.
502 for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
104 336 dst_line_step = out->linesize[plane] * (h > 2);
105 336 src_line_step = frame->linesize[plane] * (h > 2);
106 336 line_size = s->line_size[plane];
107 336 dst = out->data[plane];
108 336 src = frame->data[plane];
109
2/2
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 25 times.
336 if (s->dst_tff) {
110 /** Move every line up one line, working from
111 * the top to the bottom of the frame.
112 * The original top line is lost.
113 * The new last line is created as a copy of the
114 * penultimate line from that field. */
115
2/2
✓ Branch 0 taken 89568 times.
✓ Branch 1 taken 311 times.
89879 for (line = 0; line < h; line++) {
116
2/2
✓ Branch 0 taken 89257 times.
✓ Branch 1 taken 311 times.
89568 if (1 + line < frame->height) {
117 89257 memcpy(dst, src + src_line_step, line_size);
118 } else {
119 311 memcpy(dst, src - 2 * src_line_step, line_size);
120 }
121 89568 dst += dst_line_step;
122 89568 src += src_line_step;
123 }
124 } else {
125 /** Move every line down one line, working from
126 * the bottom to the top of the frame.
127 * The original bottom line is lost.
128 * The new first line is created as a copy of the
129 * second line from that field. */
130 25 dst += (h - 1) * dst_line_step;
131 25 src += (h - 1) * src_line_step;
132
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 25 times.
7225 for (line = h - 1; line >= 0 ; line--) {
133
2/2
✓ Branch 0 taken 7175 times.
✓ Branch 1 taken 25 times.
7200 if (line > 0) {
134 7175 memcpy(dst, src - src_line_step, line_size);
135 } else {
136 25 memcpy(dst, src + 2 * src_line_step, line_size);
137 }
138 7200 dst -= dst_line_step;
139 7200 src -= src_line_step;
140 }
141 }
142 }
143 166 out->top_field_first = s->dst_tff;
144
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 if (frame != out)
146 av_frame_free(&frame);
147 166 return ff_filter_frame(outlink, out);
148 }
149
150 #define OFFSET(x) offsetof(FieldOrderContext, x)
151 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
152
153 static const AVOption fieldorder_options[] = {
154 { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
155 { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
156 { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
157 { NULL }
158 };
159
160 AVFILTER_DEFINE_CLASS(fieldorder);
161
162 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
163 {
164 .name = "default",
165 .type = AVMEDIA_TYPE_VIDEO,
166 .config_props = config_input,
167 .filter_frame = filter_frame,
168 },
169 };
170
171 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
172 {
173 .name = "default",
174 .type = AVMEDIA_TYPE_VIDEO,
175 },
176 };
177
178 const AVFilter ff_vf_fieldorder = {
179 .name = "fieldorder",
180 .description = NULL_IF_CONFIG_SMALL("Set the field order."),
181 .priv_size = sizeof(FieldOrderContext),
182 .priv_class = &fieldorder_class,
183 FILTER_INPUTS(avfilter_vf_fieldorder_inputs),
184 FILTER_OUTPUTS(avfilter_vf_fieldorder_outputs),
185 FILTER_QUERY_FUNC(query_formats),
186 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
187 };
188