Statistics
| Branch: | Revision:

ffmpeg / libavformat / timefilter.c @ 494065ca

History | View | Annotate | Download (2.29 KB)

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 integrator2_state;
35
    int count;
36
};
37

    
38
TimeFilter * ff_timefilter_new(double feedback2_factor, double feedback3_factor)
39
{
40
    TimeFilter *self        = av_mallocz(sizeof(TimeFilter));
41
    self->integrator2_state = 1.0;
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->cycle_time = 0;
55
    self->count      = 0;
56
}
57

    
58
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
59
{
60
    self->count++;
61
    if (!self->cycle_time) {
62
        /// init loop
63
        self->cycle_time        = system_time;
64
    } else {
65
        double loop_error;
66
        self->cycle_time+= self->integrator2_state * period;
67
        /// calculate loop error
68
        loop_error = system_time - self->cycle_time;
69

    
70
        /// update loop
71
        self->cycle_time        += FFMAX(self->feedback2_factor, 1.0/(self->count)) * loop_error;
72
        self->integrator2_state += self->feedback3_factor * loop_error / period;
73
    }
74
    return self->cycle_time;
75
}