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.

123 lines
3.9KB

  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. main(){
  69. double n0,n1;
  70. #define SAMPLES 1000
  71. double ideal[SAMPLES];
  72. double samples[SAMPLES];
  73. for(n0= 0; n0<40; n0=2*n0+1){
  74. for(n1= 0; n1<10; n1=2*n1+1){
  75. double best_error= 1000000000;
  76. double bestpar0=1;
  77. double bestpar1=0.001;
  78. int better, i;
  79. srandom(123);
  80. for(i=0; i<SAMPLES; i++){
  81. ideal[i] = 10 + i + n1*i/(1000);
  82. samples[i]= ideal[i] + n0*(rand()-RAND_MAX/2)/(RAND_MAX*10LL);
  83. }
  84. do{
  85. double par0, par1;
  86. better=0;
  87. for(par0= bestpar0*0.8; par0<=bestpar0*1.21; par0+=bestpar0*0.05){
  88. for(par1= bestpar1*0.8; par1<=bestpar1*1.21; par1+=bestpar1*0.05){
  89. double error=0;
  90. TimeFilter *tf= ff_timefilter_new(1, par0, par1);
  91. for(i=0; i<SAMPLES; i++){
  92. double filtered;
  93. filtered= ff_timefilter_update(tf, samples[i], 1);
  94. error += (filtered - ideal[i]) * (filtered - ideal[i]);
  95. }
  96. ff_timefilter_destroy(tf);
  97. if(error < best_error){
  98. best_error= error;
  99. bestpar0= par0;
  100. bestpar1= par1;
  101. better=1;
  102. }
  103. }
  104. }
  105. }while(better);
  106. printf(" [%f %f %f]", bestpar0, bestpar1, best_error);
  107. }
  108. printf("\n");
  109. }
  110. }
  111. #endif