FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_tinterlace.c
Date: 2022-12-06 04:36:11
Exec Total Coverage
Lines: 215 271 79.3%
Functions: 9 9 100.0%
Branches: 105 150 70.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com>
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2010 Baptiste Coudurier
5 * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with FFmpeg if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 /**
25 * @file
26 * temporal field interlace filter, ported from MPlayer/libmpcodecs
27 */
28
29 #include "libavutil/opt.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/avassert.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "tinterlace.h"
35
36 #define OFFSET(x) offsetof(TInterlaceContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
38
39 static const AVOption tinterlace_options[] = {
40 {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
41 {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
42 {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
43 {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
44 {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
45 {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
46 {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
47 {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
48 {"mergex2", "merge fields keeping same frame rate", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
49
50 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
51 {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
52 {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
53 {"complex_filter", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
54 {"cvlpf", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
55 {"exact_tb", "force a timebase which can represent timestamps exactly", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_EXACT_TB}, INT_MIN, INT_MAX, FLAGS, "flags" },
56 {"bypass_il", "bypass already interlaced frames", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_BYPASS_IL}, INT_MIN, INT_MAX, FLAGS, "flags" },
57
58 {NULL}
59 };
60
61 AVFILTER_DEFINE_CLASS(tinterlace);
62
63 static const AVOption interlace_options[] = {
64 { "scan", "scanning mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_TFF}, 0, 1, FLAGS, "mode"},
65 { "tff", "top field first", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
66 { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_BFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
67 { "lowpass", "set vertical low-pass filter", OFFSET(lowpass), AV_OPT_TYPE_INT, {.i64 = VLPF_LIN}, 0, 2, FLAGS, "lowpass" },
68 { "off", "disable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_OFF}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
69 { "linear", "linear vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_LIN}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
70 { "complex", "complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_CMP}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
71
72 { NULL }
73 };
74
75 AVFILTER_DEFINE_CLASS(interlace);
76
77 #define FULL_SCALE_YUVJ_FORMATS \
78 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
79
80 static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
81 FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
82 };
83
84 static const AVRational standard_tbs[] = {
85 {1, 25},
86 {1, 30},
87 {1001, 30000},
88 };
89
90 static const enum AVPixelFormat pix_fmts[] = {
91 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
92 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
93 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
94 AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE,
95 AV_PIX_FMT_YUV440P10LE, AV_PIX_FMT_YUV444P10LE,
96 AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE,
97 AV_PIX_FMT_YUV440P12LE, AV_PIX_FMT_YUV444P12LE,
98 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
99 AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE,
100 AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
101 AV_PIX_FMT_NONE
102 };
103
104 39312 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
105 ptrdiff_t mref, ptrdiff_t pref, int clip_max)
106 {
107 39312 const uint8_t *srcp_above = srcp + mref;
108 39312 const uint8_t *srcp_below = srcp + pref;
109 int i;
110
2/2
✓ Branch 0 taken 10657152 times.
✓ Branch 1 taken 39312 times.
10696464 for (i = 0; i < width; i++) {
111 // this calculation is an integer representation of
112 // '0.5 * current + 0.25 * above + 0.25 * below'
113 // '1 +' is for rounding.
114 10657152 dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
115 }
116 39312 }
117
118 8928 static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
119 ptrdiff_t mref, ptrdiff_t pref, int clip_max)
120 {
121 8928 uint16_t *dstp = (uint16_t *)dst8;
122 8928 const uint16_t *srcp = (const uint16_t *)src8;
123 8928 const uint16_t *srcp_above = srcp + mref / 2;
124 8928 const uint16_t *srcp_below = srcp + pref / 2;
125 int i, src_x;
126
2/2
✓ Branch 0 taken 2686464 times.
✓ Branch 1 taken 8928 times.
2695392 for (i = 0; i < width; i++) {
127 // this calculation is an integer representation of
128 // '0.5 * current + 0.25 * above + 0.25 * below'
129 // '1 +' is for rounding.
130 2686464 src_x = av_le2ne16(srcp[i]) << 1;
131 2686464 dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
132 + av_le2ne16(srcp_below[i])) >> 2);
133 }
134 8928 }
135
136 24912 static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
137 ptrdiff_t mref, ptrdiff_t pref, int clip_max)
138 {
139 24912 const uint8_t *srcp_above = srcp + mref;
140 24912 const uint8_t *srcp_below = srcp + pref;
141 24912 const uint8_t *srcp_above2 = srcp + mref * 2;
142 24912 const uint8_t *srcp_below2 = srcp + pref * 2;
143 int i, src_x, src_ab;
144
2/2
✓ Branch 0 taken 6855552 times.
✓ Branch 1 taken 24912 times.
6880464 for (i = 0; i < width; i++) {
145 // this calculation is an integer representation of
146 // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
147 // '4 +' is for rounding.
148 6855552 src_x = srcp[i] << 1;
149 6855552 src_ab = srcp_above[i] + srcp_below[i];
150 6855552 dstp[i] = av_clip_uint8((4 + ((srcp[i] + src_x + src_ab) << 1)
151 6855552 - srcp_above2[i] - srcp_below2[i]) >> 3);
152 // Prevent over-sharpening:
153 // dst must not exceed src when the average of above and below
154 // is less than src. And the other way around.
155
2/2
✓ Branch 0 taken 2774592 times.
✓ Branch 1 taken 4080960 times.
6855552 if (src_ab > src_x) {
156
2/2
✓ Branch 0 taken 384549 times.
✓ Branch 1 taken 2390043 times.
2774592 if (dstp[i] < srcp[i])
157 384549 dstp[i] = srcp[i];
158
2/2
✓ Branch 0 taken 487484 times.
✓ Branch 1 taken 3593476 times.
4080960 } else if (dstp[i] > srcp[i])
159 487484 dstp[i] = srcp[i];
160 }
161 24912 }
162
163 8928 static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
164 ptrdiff_t mref, ptrdiff_t pref, int clip_max)
165 {
166 8928 uint16_t *dstp = (uint16_t *)dst8;
167 8928 const uint16_t *srcp = (const uint16_t *)src8;
168 8928 const uint16_t *srcp_above = srcp + mref / 2;
169 8928 const uint16_t *srcp_below = srcp + pref / 2;
170 8928 const uint16_t *srcp_above2 = srcp + mref;
171 8928 const uint16_t *srcp_below2 = srcp + pref;
172 int i, dst_le, src_le, src_x, src_ab;
173
2/2
✓ Branch 0 taken 2686464 times.
✓ Branch 1 taken 8928 times.
2695392 for (i = 0; i < width; i++) {
174 // this calculation is an integer representation of
175 // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
176 // '4 +' is for rounding.
177 2686464 src_le = av_le2ne16(srcp[i]);
178 2686464 src_x = src_le << 1;
179 2686464 src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
180 2686464 dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
181 2686464 - av_le2ne16(srcp_above2[i])
182 2686464 - av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
183 // Prevent over-sharpening:
184 // dst must not exceed src when the average of above and below
185 // is less than src. And the other way around.
186
2/2
✓ Branch 0 taken 1072111 times.
✓ Branch 1 taken 1614353 times.
2686464 if (src_ab > src_x) {
187
2/2
✓ Branch 0 taken 270609 times.
✓ Branch 1 taken 801502 times.
1072111 if (dst_le < src_le)
188 270609 dstp[i] = av_le2ne16(src_le);
189 else
190 801502 dstp[i] = av_le2ne16(dst_le);
191
2/2
✓ Branch 0 taken 306777 times.
✓ Branch 1 taken 1307576 times.
1614353 } else if (dst_le > src_le) {
192 306777 dstp[i] = av_le2ne16(src_le);
193 } else
194 1307576 dstp[i] = av_le2ne16(dst_le);
195 }
196 8928 }
197
198 117 static av_cold void uninit(AVFilterContext *ctx)
199 {
200 117 TInterlaceContext *tinterlace = ctx->priv;
201
202 117 av_frame_free(&tinterlace->cur );
203 117 av_frame_free(&tinterlace->next);
204 117 av_freep(&tinterlace->black_data[0]);
205 117 }
206
207 113 static int config_out_props(AVFilterLink *outlink)
208 {
209 113 AVFilterContext *ctx = outlink->src;
210 113 AVFilterLink *inlink = outlink->src->inputs[0];
211 113 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
212 113 TInterlaceContext *tinterlace = ctx->priv;
213 int i;
214
215 113 tinterlace->vsub = desc->log2_chroma_h;
216 113 outlink->w = inlink->w;
217
3/4
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 63 times.
88 outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2?
218
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 25 times.
201 inlink->h*2 : inlink->h;
219
5/6
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 25 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 63 times.
113 if (tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2)
220 50 outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio,
221 av_make_q(2, 1));
222
223
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 88 times.
113 if (tinterlace->mode == MODE_PAD) {
224 25 uint8_t black[4] = { 0, 0, 0, 16 };
225 int ret;
226 25 ff_draw_init(&tinterlace->draw, outlink->format, 0);
227 25 ff_draw_color(&tinterlace->draw, &tinterlace->color, black);
228
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 21 times.
25 if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
229 4 tinterlace->color.comp[0].u8[0] = 0;
230 25 ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
231 25 outlink->w, outlink->h, outlink->format, 16);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
233 return ret;
234
235 25 ff_fill_rectangle(&tinterlace->draw, &tinterlace->color, tinterlace->black_data,
236 25 tinterlace->black_linesize, 0, 0, outlink->w, outlink->h);
237 }
238
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 60 times.
113 if (tinterlace->flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 && !(tinterlace->mode == MODE_INTERLEAVE_TOP
240 || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
241 av_log(ctx, AV_LOG_WARNING, "low_pass_filter flags ignored with mode %d\n",
242 tinterlace->mode);
243 tinterlace->flags &= ~(TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF);
244 }
245 113 tinterlace->preout_time_base = inlink->time_base;
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 if (tinterlace->mode == MODE_INTERLACEX2) {
247 tinterlace->preout_time_base.den *= 2;
248 outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
249 outlink->time_base = av_mul_q(inlink->time_base , (AVRational){1,2});
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 } else if (tinterlace->mode == MODE_MERGEX2) {
251 outlink->frame_rate = inlink->frame_rate;
252 outlink->time_base = inlink->time_base;
253
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 25 times.
113 } else if (tinterlace->mode != MODE_PAD) {
254 88 outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
255 88 outlink->time_base = av_mul_q(inlink->time_base , (AVRational){2,1});
256 }
257
258
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 113 times.
452 for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 339 times.
339 if (!av_cmp_q(standard_tbs[i], outlink->time_base))
260 break;
261 }
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 if (i == FF_ARRAY_ELEMS(standard_tbs) ||
263 (tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
264 113 outlink->time_base = tinterlace->preout_time_base;
265
266 113 tinterlace->csp = av_pix_fmt_desc_get(outlink->format);
267
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 87 times.
113 if (tinterlace->flags & TINTERLACE_FLAG_CVLPF) {
268
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 15 times.
26 if (tinterlace->csp->comp[0].depth > 8)
269 11 tinterlace->lowpass_line = lowpass_line_complex_c_16;
270 else
271 15 tinterlace->lowpass_line = lowpass_line_complex_c;
272 #if ARCH_X86
273 26 ff_tinterlace_init_x86(tinterlace);
274 #endif
275
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 60 times.
87 } else if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
276
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
27 if (tinterlace->csp->comp[0].depth > 8)
277 11 tinterlace->lowpass_line = lowpass_line_c_16;
278 else
279 16 tinterlace->lowpass_line = lowpass_line_c;
280 #if ARCH_X86
281 27 ff_tinterlace_init_x86(tinterlace);
282 #endif
283 }
284
285 113 av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n", tinterlace->mode,
286
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 26 times.
113 (tinterlace->flags & TINTERLACE_FLAG_CVLPF) ? "complex" :
287
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 60 times.
87 (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "linear" : "off",
288 inlink->h, outlink->h);
289
290 113 return 0;
291 }
292
293 #define FIELD_UPPER 0
294 #define FIELD_LOWER 1
295 #define FIELD_UPPER_AND_LOWER 2
296
297 /**
298 * Copy picture field from src to dst.
299 *
300 * @param src_field copy from upper, lower field or both
301 * @param interleave leave a padding line between each copied line
302 * @param dst_field copy to upper or lower field,
303 * only meaningful when interleave is selected
304 * @param flags context flags
305 */
306 static inline
307 370 void copy_picture_field(TInterlaceContext *tinterlace,
308 uint8_t *dst[4], int dst_linesize[4],
309 const uint8_t *src[4], int src_linesize[4],
310 enum AVPixelFormat format, int w, int src_h,
311 int src_field, int interleave, int dst_field,
312 int flags)
313 {
314 370 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
315 370 int hsub = desc->log2_chroma_w;
316 370 int plane, vsub = desc->log2_chroma_h;
317
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 270 times.
370 int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
318 int h;
319
320
2/2
✓ Branch 0 taken 1142 times.
✓ Branch 1 taken 370 times.
1512 for (plane = 0; plane < desc->nb_components; plane++) {
321
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 362 times.
✓ Branch 2 taken 362 times.
✓ Branch 3 taken 418 times.
1142 int lines = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(src_h, vsub) : src_h;
322
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 362 times.
✓ Branch 2 taken 362 times.
✓ Branch 3 taken 418 times.
1142 int cols = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT( w, hsub) : w;
323 1142 uint8_t *dstp = dst[plane];
324 1142 const uint8_t *srcp = src[plane];
325 1142 int srcp_linesize = src_linesize[plane] * k;
326
1/2
✓ Branch 0 taken 1142 times.
✗ Branch 1 not taken.
1142 int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
327 1142 int clip_max = (1 << tinterlace->csp->comp[plane].depth) - 1;
328
329 1142 lines = (lines + (src_field == FIELD_UPPER)) / k;
330
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 729 times.
1142 if (src_field == FIELD_LOWER)
331 413 srcp += src_linesize[plane];
332
3/4
✓ Branch 0 taken 1142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 571 times.
✓ Branch 3 taken 571 times.
1142 if (interleave && dst_field == FIELD_LOWER)
333 571 dstp += dst_linesize[plane];
334 // Low-pass filtering is required when creating an interlaced destination from
335 // a progressive source which contains high-frequency vertical detail.
336 // Filtering will reduce interlace 'twitter' and Moire patterning.
337
2/2
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 376 times.
1142 if (flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)) {
338 766 int x = !!(flags & TINTERLACE_FLAG_CVLPF);
339
2/2
✓ Branch 0 taken 82080 times.
✓ Branch 1 taken 766 times.
82846 for (h = lines; h > 0; h--) {
340 82080 ptrdiff_t pref = src_linesize[plane];
341 82080 ptrdiff_t mref = -pref;
342
2/2
✓ Branch 0 taken 1074 times.
✓ Branch 1 taken 81006 times.
82080 if (h >= (lines - x)) mref = 0; // there is no line above
343
2/2
✓ Branch 0 taken 1074 times.
✓ Branch 1 taken 79932 times.
81006 else if (h <= (1 + x)) pref = 0; // there is no line below
344
345 82080 tinterlace->lowpass_line(dstp, cols, srcp, mref, pref, clip_max);
346 82080 dstp += dstp_linesize;
347 82080 srcp += srcp_linesize;
348 }
349 } else {
350
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 232 times.
376 if (tinterlace->csp->comp[plane].depth > 8)
351 144 cols *= 2;
352 376 av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);
353 }
354 }
355 370 }
356
357 370 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
358 {
359 370 AVFilterContext *ctx = inlink->dst;
360 370 AVFilterLink *outlink = ctx->outputs[0];
361 370 TInterlaceContext *tinterlace = ctx->priv;
362 AVFrame *cur, *next, *out;
363 int field, tff, ret;
364
365 370 av_frame_free(&tinterlace->cur);
366 370 tinterlace->cur = tinterlace->next;
367 370 tinterlace->next = picref;
368
369 370 cur = tinterlace->cur;
370 370 next = tinterlace->next;
371 /* we need at least two frames */
372
2/2
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 185 times.
370 if (!tinterlace->cur)
373 185 return 0;
374
375
3/6
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 135 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
185 switch (tinterlace->mode) {
376 25 case MODE_MERGEX2: /* move the odd frame into the upper field of the new image, even into
377 * the lower field, generating a double-height video at same framerate */
378 case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
379 * the lower field, generating a double-height video at half framerate */
380 25 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!out)
382 return AVERROR(ENOMEM);
383 25 av_frame_copy_props(out, cur);
384 25 out->height = outlink->h;
385 25 out->interlaced_frame = 1;
386 25 out->top_field_first = 1;
387 25 out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
388
389 /* write odd frame lines into the upper field of the new frame */
390 25 copy_picture_field(tinterlace, out->data, out->linesize,
391 25 (const uint8_t **)cur->data, cur->linesize,
392 25 inlink->format, inlink->w, inlink->h,
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_LOWER : FIELD_UPPER : FIELD_UPPER, tinterlace->flags);
394 /* write even frame lines into the lower field of the new frame */
395 25 copy_picture_field(tinterlace, out->data, out->linesize,
396 25 (const uint8_t **)next->data, next->linesize,
397 25 inlink->format, inlink->w, inlink->h,
398
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25 FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_UPPER : FIELD_LOWER : FIELD_LOWER, tinterlace->flags);
399
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (tinterlace->mode != MODE_MERGEX2)
400 25 av_frame_free(&tinterlace->next);
401 25 break;
402
403 case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
404 case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
405 out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
406 if (!out)
407 return AVERROR(ENOMEM);
408 av_frame_free(&tinterlace->next);
409 break;
410
411 25 case MODE_PAD: /* expand each frame to double height, but pad alternate
412 * lines with black; framerate unchanged */
413 25 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!out)
415 return AVERROR(ENOMEM);
416 25 av_frame_copy_props(out, cur);
417 25 out->height = outlink->h;
418 25 out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
419
420 25 field = (1 + outlink->frame_count_in) & 1 ? FIELD_UPPER : FIELD_LOWER;
421 /* copy upper and lower fields */
422 25 copy_picture_field(tinterlace, out->data, out->linesize,
423 25 (const uint8_t **)cur->data, cur->linesize,
424 25 inlink->format, inlink->w, inlink->h,
425 FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
426 /* pad with black the other field */
427 25 copy_picture_field(tinterlace, out->data, out->linesize,
428 25 (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
429 25 inlink->format, inlink->w, inlink->h,
430 FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
431 25 break;
432
433 /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
434 * halving the frame rate and preserving image height */
435 135 case MODE_INTERLEAVE_TOP: /* top field first */
436 case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
437
3/4
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 75 times.
135 if ((tinterlace->flags & TINTERLACE_FLAG_BYPASS_IL) && cur->interlaced_frame) {
438 av_log(ctx, AV_LOG_WARNING,
439 "video is already interlaced, adjusting framerate only\n");
440 out = av_frame_clone(cur);
441 if (!out)
442 return AVERROR(ENOMEM);
443 out->pts /= 2; // adjust pts to new framerate
444 ret = ff_filter_frame(outlink, out);
445 return ret;
446 }
447 135 tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
448 135 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (!out)
450 return AVERROR(ENOMEM);
451 135 av_frame_copy_props(out, cur);
452 135 out->interlaced_frame = 1;
453 135 out->top_field_first = tff;
454
455 /* copy upper/lower field from cur */
456 135 copy_picture_field(tinterlace, out->data, out->linesize,
457 135 (const uint8_t **)cur->data, cur->linesize,
458 135 inlink->format, inlink->w, inlink->h,
459 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
460 tinterlace->flags);
461 /* copy lower/upper field from next */
462 135 copy_picture_field(tinterlace, out->data, out->linesize,
463 135 (const uint8_t **)next->data, next->linesize,
464 135 inlink->format, inlink->w, inlink->h,
465 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
466 tinterlace->flags);
467 135 av_frame_free(&tinterlace->next);
468 135 break;
469 case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
470 /* output current frame first */
471 out = av_frame_clone(cur);
472 if (!out)
473 return AVERROR(ENOMEM);
474 out->interlaced_frame = 1;
475 if (cur->pts != AV_NOPTS_VALUE)
476 out->pts = cur->pts*2;
477
478 out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
479 if ((ret = ff_filter_frame(outlink, out)) < 0)
480 return ret;
481
482 /* output mix of current and next frame */
483 tff = next->top_field_first;
484 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
485 if (!out)
486 return AVERROR(ENOMEM);
487 av_frame_copy_props(out, next);
488 out->interlaced_frame = 1;
489 out->top_field_first = !tff;
490
491 if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
492 out->pts = cur->pts + next->pts;
493 else
494 out->pts = AV_NOPTS_VALUE;
495 /* write current frame second field lines into the second field of the new frame */
496 copy_picture_field(tinterlace, out->data, out->linesize,
497 (const uint8_t **)cur->data, cur->linesize,
498 inlink->format, inlink->w, inlink->h,
499 tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
500 tinterlace->flags);
501 /* write next frame first field lines into the first field of the new frame */
502 copy_picture_field(tinterlace, out->data, out->linesize,
503 (const uint8_t **)next->data, next->linesize,
504 inlink->format, inlink->w, inlink->h,
505 tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
506 tinterlace->flags);
507 break;
508 default:
509 av_assert0(0);
510 }
511
512 185 out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
513 185 out->duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
514 185 ret = ff_filter_frame(outlink, out);
515
516 185 return ret;
517 }
518
519 3 static int init_interlace(AVFilterContext *ctx)
520 {
521 3 TInterlaceContext *tinterlace = ctx->priv;
522
523
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (tinterlace->mode <= MODE_BFF)
524 3 tinterlace->mode += MODE_INTERLEAVE_TOP;
525
526 3 tinterlace->flags |= TINTERLACE_FLAG_BYPASS_IL;
527
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (tinterlace->lowpass == VLPF_LIN)
528 2 tinterlace->flags |= TINTERLACE_FLAG_VLPF;
529
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (tinterlace->lowpass == VLPF_CMP)
530 1 tinterlace->flags |= TINTERLACE_FLAG_CVLPF;
531
532 3 return 0;
533 }
534
535 static const AVFilterPad tinterlace_inputs[] = {
536 {
537 .name = "default",
538 .type = AVMEDIA_TYPE_VIDEO,
539 .filter_frame = filter_frame,
540 },
541 };
542
543 static const AVFilterPad tinterlace_outputs[] = {
544 {
545 .name = "default",
546 .type = AVMEDIA_TYPE_VIDEO,
547 .config_props = config_out_props,
548 },
549 };
550
551 const AVFilter ff_vf_tinterlace = {
552 .name = "tinterlace",
553 .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
554 .priv_size = sizeof(TInterlaceContext),
555 .uninit = uninit,
556 FILTER_INPUTS(tinterlace_inputs),
557 FILTER_OUTPUTS(tinterlace_outputs),
558 FILTER_PIXFMTS_ARRAY(pix_fmts),
559 .priv_class = &tinterlace_class,
560 };
561
562
563 const AVFilter ff_vf_interlace = {
564 .name = "interlace",
565 .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
566 .priv_size = sizeof(TInterlaceContext),
567 .init = init_interlace,
568 .uninit = uninit,
569 FILTER_INPUTS(tinterlace_inputs),
570 FILTER_OUTPUTS(tinterlace_outputs),
571 FILTER_PIXFMTS_ARRAY(pix_fmts),
572 .priv_class = &interlace_class,
573 };
574