timefilter.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 feedback2_factor;
  30. double feedback3_factor;
  31. double clock_period;
  32. int count;
  33. };
  34. TimeFilter * ff_timefilter_new(double clock_period, double feedback2_factor, double feedback3_factor)
  35. {
  36. TimeFilter *self = av_mallocz(sizeof(TimeFilter));
  37. self->clock_period = clock_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->count = 0;
  49. }
  50. double ff_timefilter_update(TimeFilter *self, double system_time, double period)
  51. {
  52. self->count++;
  53. if (self->count==1) {
  54. /// init loop
  55. self->cycle_time = system_time;
  56. } else {
  57. double loop_error;
  58. self->cycle_time += self->clock_period * period;
  59. /// calculate loop error
  60. loop_error = system_time - self->cycle_time;
  61. /// update loop
  62. self->cycle_time += FFMAX(self->feedback2_factor, 1.0/(self->count)) * loop_error;
  63. self->clock_period += self->feedback3_factor * loop_error / period;
  64. }
  65. return self->cycle_time;
  66. }
  67. #ifdef TEST
  68. #undef rand
  69. int main(void)
  70. {
  71. double n0,n1;
  72. #define SAMPLES 1000
  73. double ideal[SAMPLES];
  74. double samples[SAMPLES];
  75. #if 1
  76. for(n0= 0; n0<40; n0=2*n0+1){
  77. for(n1= 0; n1<10; n1=2*n1+1){
  78. #else
  79. {{
  80. n0=7;
  81. n1=1;
  82. #endif
  83. double best_error= 1000000000;
  84. double bestpar0=1;
  85. double bestpar1=0.001;
  86. int better, i;
  87. srandom(123);
  88. for(i=0; i<SAMPLES; i++){
  89. ideal[i] = 10 + i + n1*i/(1000);
  90. samples[i]= ideal[i] + n0*(rand()-RAND_MAX/2)/(RAND_MAX*10LL);
  91. }
  92. do{
  93. double par0, par1;
  94. better=0;
  95. for(par0= bestpar0*0.8; par0<=bestpar0*1.21; par0+=bestpar0*0.05){
  96. for(par1= bestpar1*0.8; par1<=bestpar1*1.21; par1+=bestpar1*0.05){
  97. double error=0;
  98. TimeFilter *tf= ff_timefilter_new(1, par0, par1);
  99. for(i=0; i<SAMPLES; i++){
  100. double filtered;
  101. filtered= ff_timefilter_update(tf, samples[i], 1);
  102. error += (filtered - ideal[i]) * (filtered - ideal[i]);
  103. }
  104. ff_timefilter_destroy(tf);
  105. if(error < best_error){
  106. best_error= error;
  107. bestpar0= par0;
  108. bestpar1= par1;
  109. better=1;
  110. }
  111. }
  112. }
  113. }while(better);
  114. #if 0
  115. double lastfil=9;
  116. TimeFilter *tf= ff_timefilter_new(1, bestpar0, bestpar1);
  117. for(i=0; i<SAMPLES; i++){
  118. double filtered;
  119. filtered= ff_timefilter_update(tf, samples[i], 1);
  120. printf("%f %f %f %f\n", i - samples[i] + 10, filtered - samples[i], samples[FFMAX(i, 1)] - samples[FFMAX(i-1, 0)], filtered - lastfil);
  121. lastfil= filtered;
  122. }
  123. ff_timefilter_destroy(tf);
  124. #else
  125. printf(" [%f %f %f]", bestpar0, bestpar1, best_error);
  126. #endif
  127. }
  128. printf("\n");
  129. }
  130. return 0;
  131. }
  132. #endif