FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_dejudder.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 51 0.0%
Functions: 0 4 0.0%
Branches: 0 12 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2014 Nicholas Robbins
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 * remove judder in video stream
24 *
25 * Algorithm:
26 * - If the old packets had PTS of old_pts[i]. Replace these with new
27 * value based on the running average of the last n=cycle frames. So
28 *
29 * new_pts[i] = Sum(k=i-n+1, i, old_pts[k])/n
30 * + (old_pts[i]-old_pts[i-n])*(n-1)/2n
31 *
32 * For any repeating pattern of length n of judder this will produce
33 * an even progression of PTS's.
34 *
35 * - In order to avoid calculating this sum ever frame, a running tally
36 * is maintained in ctx->new_pts. Each frame the new term at the start
37 * of the sum is added, the one and the end is removed, and the offset
38 * terms (second line in formula above) are recalculated.
39 *
40 * - To aid in this a ringbuffer of the last n-2 PTS's is maintained in
41 * ctx->ringbuff. With the indices of the first two and last two entries
42 * stored in i1, i2, i3, & i4.
43 *
44 * - To ensure that the new PTS's are integers, time_base is divided
45 * by 2n. This removes the division in the new_pts calculation.
46 *
47 * - frame_rate is also multiplied by 2n to allow the frames to fall
48 * where they may in what may now be a VFR output. This produces more
49 * even output then setting frame_rate=1/0 in practice.
50 */
51
52 #include "libavutil/mem.h"
53 #include "libavutil/opt.h"
54 #include "avfilter.h"
55 #include "internal.h"
56
57 typedef struct DejudderContext {
58 const AVClass *class;
59 int64_t *ringbuff;
60 int i1, i2, i3, i4;
61 int64_t new_pts;
62 int start_count;
63
64 /* options */
65 int cycle;
66 } DejudderContext;
67
68 #define OFFSET(x) offsetof(DejudderContext, x)
69 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
70
71 static const AVOption dejudder_options[] = {
72 {"cycle", "set the length of the cycle to use for dejuddering",
73 OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 4}, 2, 240, .flags = FLAGS},
74 {NULL}
75 };
76
77 AVFILTER_DEFINE_CLASS(dejudder);
78
79 static int config_out_props(AVFilterLink *outlink)
80 {
81 AVFilterContext *ctx = outlink->src;
82 DejudderContext *s = ctx->priv;
83 AVFilterLink *inlink = outlink->src->inputs[0];
84
85 outlink->time_base = av_mul_q(inlink->time_base, av_make_q(1, 2 * s->cycle));
86 outlink->frame_rate = av_mul_q(inlink->frame_rate, av_make_q(2 * s->cycle, 1));
87
88 av_log(ctx, AV_LOG_VERBOSE, "cycle:%d\n", s->cycle);
89
90 return 0;
91 }
92
93 static av_cold int dejudder_init(AVFilterContext *ctx)
94 {
95 DejudderContext *s = ctx->priv;
96
97 s->ringbuff = av_calloc(s->cycle + 2, sizeof(*s->ringbuff));
98 if (!s->ringbuff)
99 return AVERROR(ENOMEM);
100
101 s->new_pts = 0;
102 s->i1 = 0;
103 s->i2 = 1;
104 s->i3 = 2;
105 s->i4 = 3;
106 s->start_count = s->cycle + 2;
107
108 return 0;
109 }
110
111 static av_cold void dejudder_uninit(AVFilterContext *ctx)
112 {
113 DejudderContext *s = ctx->priv;
114
115 av_freep(&(s->ringbuff));
116 }
117
118 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
119 {
120 int k;
121 AVFilterContext *ctx = inlink->dst;
122 AVFilterLink *outlink = ctx->outputs[0];
123 DejudderContext *s = ctx->priv;
124 int64_t *judbuff = s->ringbuff;
125 int64_t next_pts = frame->pts;
126 int64_t offset;
127
128 if (next_pts == AV_NOPTS_VALUE)
129 return ff_filter_frame(outlink, frame);
130
131 if (s->start_count) {
132 s->start_count--;
133 s->new_pts = next_pts * 2 * s->cycle;
134 } else {
135 if (next_pts < judbuff[s->i2]) {
136 offset = next_pts + judbuff[s->i3] - judbuff[s->i4] - judbuff[s->i1];
137 for (k = 0; k < s->cycle + 2; k++)
138 judbuff[k] += offset;
139 }
140 s->new_pts += (s->cycle - 1) * (judbuff[s->i3] - judbuff[s->i1])
141 + (s->cycle + 1) * (next_pts - judbuff[s->i4]);
142 }
143
144 judbuff[s->i2] = next_pts;
145 s->i1 = s->i2;
146 s->i2 = s->i3;
147 s->i3 = s->i4;
148 s->i4 = (s->i4 + 1) % (s->cycle + 2);
149
150 frame->pts = s->new_pts;
151
152 for (k = 0; k < s->cycle + 2; k++)
153 av_log(ctx, AV_LOG_DEBUG, "%"PRId64"\t", judbuff[k]);
154 av_log(ctx, AV_LOG_DEBUG, "next=%"PRId64", new=%"PRId64"\n", next_pts, frame->pts);
155
156 return ff_filter_frame(outlink, frame);
157 }
158
159 static const AVFilterPad dejudder_inputs[] = {
160 {
161 .name = "default",
162 .type = AVMEDIA_TYPE_VIDEO,
163 .filter_frame = filter_frame,
164 },
165 };
166
167 static const AVFilterPad dejudder_outputs[] = {
168 {
169 .name = "default",
170 .type = AVMEDIA_TYPE_VIDEO,
171 .config_props = config_out_props,
172 },
173 };
174
175 const AVFilter ff_vf_dejudder = {
176 .name = "dejudder",
177 .description = NULL_IF_CONFIG_SMALL("Remove judder produced by pullup."),
178 .priv_size = sizeof(DejudderContext),
179 .priv_class = &dejudder_class,
180 FILTER_INPUTS(dejudder_inputs),
181 FILTER_OUTPUTS(dejudder_outputs),
182 .init = dejudder_init,
183 .uninit = dejudder_uninit,
184 };
185