]> git.sesse.net Git - ffmpeg/blob - libavformat/timefilter.c
Add '#undef rand' to fix test program build.
[ffmpeg] / libavformat / timefilter.c
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
23
24 #include "config.h"
25 #include "avformat.h"
26 #include "timefilter.h"
27
28 struct TimeFilter {
29     /// Delay Locked Loop data. These variables refer to mathematical
30     /// concepts described in: http://www.kokkinizita.net/papers/usingdll.pdf
31     double cycle_time;
32     double feedback2_factor;
33     double feedback3_factor;
34     double clock_period;
35     int count;
36 };
37
38 TimeFilter * ff_timefilter_new(double clock_period, double feedback2_factor, double feedback3_factor)
39 {
40     TimeFilter *self        = av_mallocz(sizeof(TimeFilter));
41     self->clock_period      = clock_period;
42     self->feedback2_factor  = feedback2_factor;
43     self->feedback3_factor  = feedback3_factor;
44     return self;
45 }
46
47 void ff_timefilter_destroy(TimeFilter *self)
48 {
49     av_freep(&self);
50 }
51
52 void ff_timefilter_reset(TimeFilter *self)
53 {
54     self->count      = 0;
55 }
56
57 double ff_timefilter_update(TimeFilter *self, double system_time, double period)
58 {
59     self->count++;
60     if (self->count==1) {
61         /// init loop
62         self->cycle_time    = system_time;
63     } else {
64         double loop_error;
65         self->cycle_time   += self->clock_period * period;
66         /// calculate loop error
67         loop_error          = system_time - self->cycle_time;
68
69         /// update loop
70         self->cycle_time   += FFMAX(self->feedback2_factor, 1.0/(self->count)) * loop_error;
71         self->clock_period += self->feedback3_factor * loop_error / period;
72     }
73     return self->cycle_time;
74 }
75
76 #ifdef TEST
77 #undef rand
78 int main(void)
79 {
80     double n0,n1;
81 #define SAMPLES 1000
82     double ideal[SAMPLES];
83     double samples[SAMPLES];
84     for(n0= 0; n0<40; n0=2*n0+1){
85         for(n1= 0; n1<10; n1=2*n1+1){
86             double best_error= 1000000000;
87             double bestpar0=1;
88             double bestpar1=0.001;
89             int better, i;
90
91             srandom(123);
92             for(i=0; i<SAMPLES; i++){
93                 ideal[i]  = 10 + i + n1*i/(1000);
94                 samples[i]= ideal[i] + n0*(rand()-RAND_MAX/2)/(RAND_MAX*10LL);
95             }
96
97             do{
98                 double par0, par1;
99                 better=0;
100                 for(par0= bestpar0*0.8; par0<=bestpar0*1.21; par0+=bestpar0*0.05){
101                     for(par1= bestpar1*0.8; par1<=bestpar1*1.21; par1+=bestpar1*0.05){
102                         double error=0;
103                         TimeFilter *tf= ff_timefilter_new(1, par0, par1);
104                         for(i=0; i<SAMPLES; i++){
105                             double filtered;
106                             filtered=  ff_timefilter_update(tf, samples[i], 1);
107                             error += (filtered - ideal[i]) * (filtered - ideal[i]);
108                         }
109                         ff_timefilter_destroy(tf);
110                         if(error < best_error){
111                             best_error= error;
112                             bestpar0= par0;
113                             bestpar1= par1;
114                             better=1;
115                         }
116                     }
117                 }
118             }while(better);
119             printf(" [%f %f %f]", bestpar0, bestpar1, best_error);
120         }
121         printf("\n");
122     }
123     return 0;
124 }
125 #endif