vf_dejudder.c 5.6 KB

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