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.

151 lines
4.9KB

  1. /*
  2. * Delay Locked Loop based time filter
  3. * Copyright (c) 2009 Samalyse
  4. * Copyright (c) 2009 Michael Niedermayer
  5. * Author: Olivier Guilyardi <olivier samalyse com>
  6. * Michael Niedermayer <michaelni gmx at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/common.h"
  25. #include "libavutil/mem.h"
  26. #include "timefilter.h"
  27. struct TimeFilter {
  28. // Delay Locked Loop data. These variables refer to mathematical
  29. // concepts described in: http://www.kokkinizita.net/papers/usingdll.pdf
  30. double cycle_time;
  31. double feedback2_factor;
  32. double feedback3_factor;
  33. double clock_period;
  34. int count;
  35. };
  36. TimeFilter *ff_timefilter_new(double clock_period,
  37. double feedback2_factor,
  38. double feedback3_factor)
  39. {
  40. TimeFilter *self = av_mallocz(sizeof(TimeFilter));
  41. if (!self)
  42. return NULL;
  43. self->clock_period = clock_period;
  44. self->feedback2_factor = feedback2_factor;
  45. self->feedback3_factor = feedback3_factor;
  46. return self;
  47. }
  48. void ff_timefilter_destroy(TimeFilter *self)
  49. {
  50. av_freep(&self);
  51. }
  52. void ff_timefilter_reset(TimeFilter *self)
  53. {
  54. self->count = 0;
  55. }
  56. double ff_timefilter_update(TimeFilter *self, double system_time, double period)
  57. {
  58. self->count++;
  59. if (self->count == 1) {
  60. self->cycle_time = system_time;
  61. } else {
  62. double loop_error;
  63. self->cycle_time += self->clock_period * period;
  64. loop_error = system_time - self->cycle_time;
  65. self->cycle_time += FFMAX(self->feedback2_factor, 1.0 / self->count) * loop_error;
  66. self->clock_period += self->feedback3_factor * loop_error / period;
  67. }
  68. return self->cycle_time;
  69. }
  70. #ifdef TEST
  71. #include "libavutil/lfg.h"
  72. #define LFG_MAX ((1LL << 32) - 1)
  73. int main(void)
  74. {
  75. AVLFG prng;
  76. double n0, n1;
  77. #define SAMPLES 1000
  78. double ideal[SAMPLES];
  79. double samples[SAMPLES];
  80. for (n0 = 0; n0 < 40; n0 = 2 * n0 + 1) {
  81. for (n1 = 0; n1 < 10; n1 = 2 * n1 + 1) {
  82. double best_error = 1000000000;
  83. double bestpar0 = 1;
  84. double bestpar1 = 0.001;
  85. int better, i;
  86. av_lfg_init(&prng, 123);
  87. for (i = 0; i < SAMPLES; i++) {
  88. ideal[i] = 10 + i + n1 * i / (1000);
  89. samples[i] = ideal[i] + n0 * (av_lfg_get(&prng) - LFG_MAX / 2) / (LFG_MAX * 10LL);
  90. }
  91. do {
  92. double par0, par1;
  93. better = 0;
  94. for (par0 = bestpar0 * 0.8; par0 <= bestpar0 * 1.21; par0 += bestpar0 * 0.05) {
  95. for (par1 = bestpar1 * 0.8; par1 <= bestpar1 * 1.21; par1 += bestpar1 * 0.05) {
  96. double error = 0;
  97. TimeFilter *tf = ff_timefilter_new(1, par0, par1);
  98. if (!tf) {
  99. printf("Could not allocate memory for timefilter.\n");
  100. exit(1);
  101. }
  102. for (i = 0; i < SAMPLES; i++) {
  103. double filtered;
  104. filtered = ff_timefilter_update(tf, samples[i], 1);
  105. error += (filtered - ideal[i]) * (filtered - ideal[i]);
  106. }
  107. ff_timefilter_destroy(tf);
  108. if (error < best_error) {
  109. best_error = error;
  110. bestpar0 = par0;
  111. bestpar1 = par1;
  112. better = 1;
  113. }
  114. }
  115. }
  116. } while (better);
  117. #if 0
  118. double lastfil = 9;
  119. TimeFilter *tf = ff_timefilter_new(1, bestpar0, bestpar1);
  120. for (i = 0; i < SAMPLES; i++) {
  121. double filtered;
  122. filtered = ff_timefilter_update(tf, samples[i], 1);
  123. printf("%f %f %f %f\n", i - samples[i] + 10, filtered - samples[i],
  124. samples[FFMAX(i, 1)] - samples[FFMAX(i - 1, 0)], filtered - lastfil);
  125. lastfil = filtered;
  126. }
  127. ff_timefilter_destroy(tf);
  128. #else
  129. printf(" [%f %f %9f]", bestpar0, bestpar1, best_error);
  130. #endif
  131. }
  132. printf("\n");
  133. }
  134. return 0;
  135. }
  136. #endif