You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4KB

  1. /*
  2. * Delay Locked Loop based time filter
  3. * Copyright (c) 2009 Samalyse
  4. * Author: Olivier Guilyardi <olivier samalyse com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "avformat.h"
  24. #include "timefilter.h"
  25. struct TimeFilter {
  26. /// Delay Locked Loop data. These variables refer to mathematical
  27. /// concepts described in: http://www.kokkinizita.net/papers/usingdll.pdf
  28. double cycle_time;
  29. double next_cycle_time;
  30. double feedback2_factor;
  31. double feedback3_factor;
  32. double integrator2_state;
  33. };
  34. TimeFilter * ff_timefilter_new(double period, double feedback2_factor, double feedback3_factor)
  35. {
  36. TimeFilter *self = av_mallocz(sizeof(TimeFilter));
  37. self->integrator2_state = period;
  38. self->feedback2_factor = feedback2_factor;
  39. self->feedback3_factor = feedback3_factor;
  40. return self;
  41. }
  42. void ff_timefilter_destroy(TimeFilter *self)
  43. {
  44. av_freep(&self);
  45. }
  46. void ff_timefilter_reset(TimeFilter *self)
  47. {
  48. self->cycle_time = 0;
  49. }
  50. void ff_timefilter_update(TimeFilter *self, double system_time)
  51. {
  52. if (!self->cycle_time) {
  53. /// init loop
  54. self->cycle_time = system_time;
  55. self->next_cycle_time = self->cycle_time + self->integrator2_state;
  56. } else {
  57. /// calculate loop error
  58. double loop_error = system_time - self->next_cycle_time;
  59. /// update loop
  60. self->cycle_time = self->next_cycle_time;
  61. self->next_cycle_time += self->feedback2_factor * loop_error + self->integrator2_state;
  62. self->integrator2_state += self->feedback3_factor * loop_error;
  63. }
  64. }
  65. double ff_timefilter_read(TimeFilter *self)
  66. {
  67. return self->cycle_time;
  68. }