FFmpeg
vf_dejudder.c
Go to the documentation of this file.
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 "filters.h"
56 
57 typedef struct DejudderContext {
58  const AVClass *class;
60  int i1, i2, i3, i4;
63 
64  /* options */
65  int cycle;
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  FilterLink *outl = ff_filter_link(outlink);
82  AVFilterContext *ctx = outlink->src;
83  DejudderContext *s = ctx->priv;
84  AVFilterLink *inlink = outlink->src->inputs[0];
86 
87  outlink->time_base = av_mul_q(inlink->time_base, av_make_q(1, 2 * s->cycle));
88  outl->frame_rate = av_mul_q(inl->frame_rate, av_make_q(2 * s->cycle, 1));
89 
90  av_log(ctx, AV_LOG_VERBOSE, "cycle:%d\n", s->cycle);
91 
92  return 0;
93 }
94 
96 {
97  DejudderContext *s = ctx->priv;
98 
99  s->ringbuff = av_calloc(s->cycle + 2, sizeof(*s->ringbuff));
100  if (!s->ringbuff)
101  return AVERROR(ENOMEM);
102 
103  s->new_pts = 0;
104  s->i1 = 0;
105  s->i2 = 1;
106  s->i3 = 2;
107  s->i4 = 3;
108  s->start_count = s->cycle + 2;
109 
110  return 0;
111 }
112 
114 {
115  DejudderContext *s = ctx->priv;
116 
117  av_freep(&(s->ringbuff));
118 }
119 
121 {
122  int k;
123  AVFilterContext *ctx = inlink->dst;
124  AVFilterLink *outlink = ctx->outputs[0];
125  DejudderContext *s = ctx->priv;
126  int64_t *judbuff = s->ringbuff;
127  int64_t next_pts = frame->pts;
128  int64_t offset;
129 
130  if (next_pts == AV_NOPTS_VALUE)
131  return ff_filter_frame(outlink, frame);
132 
133  if (s->start_count) {
134  s->start_count--;
135  s->new_pts = next_pts * 2 * s->cycle;
136  } else {
137  if (next_pts < judbuff[s->i2]) {
138  offset = next_pts + judbuff[s->i3] - judbuff[s->i4] - judbuff[s->i1];
139  for (k = 0; k < s->cycle + 2; k++)
140  judbuff[k] += offset;
141  }
142  s->new_pts += (s->cycle - 1) * (judbuff[s->i3] - judbuff[s->i1])
143  + (s->cycle + 1) * (next_pts - judbuff[s->i4]);
144  }
145 
146  judbuff[s->i2] = next_pts;
147  s->i1 = s->i2;
148  s->i2 = s->i3;
149  s->i3 = s->i4;
150  s->i4 = (s->i4 + 1) % (s->cycle + 2);
151 
152  frame->pts = s->new_pts;
153 
154  for (k = 0; k < s->cycle + 2; k++)
155  av_log(ctx, AV_LOG_DEBUG, "%"PRId64"\t", judbuff[k]);
156  av_log(ctx, AV_LOG_DEBUG, "next=%"PRId64", new=%"PRId64"\n", next_pts, frame->pts);
157 
158  return ff_filter_frame(outlink, frame);
159 }
160 
161 static const AVFilterPad dejudder_inputs[] = {
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_VIDEO,
165  .filter_frame = filter_frame,
166  },
167 };
168 
169 static const AVFilterPad dejudder_outputs[] = {
170  {
171  .name = "default",
172  .type = AVMEDIA_TYPE_VIDEO,
173  .config_props = config_out_props,
174  },
175 };
176 
178  .name = "dejudder",
179  .description = NULL_IF_CONFIG_SMALL("Remove judder produced by pullup."),
180  .priv_size = sizeof(DejudderContext),
181  .priv_class = &dejudder_class,
184  .init = dejudder_init,
185  .uninit = dejudder_uninit,
186 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
DejudderContext::cycle
int cycle
Definition: vf_dejudder.c:65
dejudder_inputs
static const AVFilterPad dejudder_inputs[]
Definition: vf_dejudder.c:161
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
dejudder_outputs
static const AVFilterPad dejudder_outputs[]
Definition: vf_dejudder.c:169
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVOption
AVOption.
Definition: opt.h:429
DejudderContext::i3
int i3
Definition: vf_dejudder.c:60
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
dejudder_init
static av_cold int dejudder_init(AVFilterContext *ctx)
Definition: vf_dejudder.c:95
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_dejudder.c:120
filters.h
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
DejudderContext::ringbuff
int64_t * ringbuff
Definition: vf_dejudder.c:59
dejudder_uninit
static av_cold void dejudder_uninit(AVFilterContext *ctx)
Definition: vf_dejudder.c:113
DejudderContext::i4
int i4
Definition: vf_dejudder.c:60
dejudder_options
static const AVOption dejudder_options[]
Definition: vf_dejudder.c:71
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:465
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
DejudderContext
Definition: vf_dejudder.c:57
DejudderContext::i1
int i1
Definition: vf_dejudder.c:60
DejudderContext::i2
int i2
Definition: vf_dejudder.c:60
ff_vf_dejudder
const AVFilter ff_vf_dejudder
Definition: vf_dejudder.c:177
FLAGS
#define FLAGS
Definition: vf_dejudder.c:69
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
AVFilter
Filter definition.
Definition: avfilter.h:201
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(dejudder)
DejudderContext::start_count
int start_count
Definition: vf_dejudder.c:62
OFFSET
#define OFFSET(x)
Definition: vf_dejudder.c:68
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
config_out_props
static int config_out_props(AVFilterLink *outlink)
Definition: vf_dejudder.c:79
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
DejudderContext::new_pts
int64_t new_pts
Definition: vf_dejudder.c:61