timefilter.c 4.94 KB
Newer Older
1 2 3
/*
 * Delay Locked Loop based time filter
 * 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 12 13 14
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
15
 * Libav is distributed in the hope that it will be useful,
16 17 18 19 20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 23 24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

25
#include "libavutil/common.h"
26
#include "libavutil/mem.h"
27 28 29 30
#include "config.h"
#include "timefilter.h"

struct TimeFilter {
31 32
    // Delay Locked Loop data. These variables refer to mathematical
    // concepts described in: http://www.kokkinizita.net/papers/usingdll.pdf
33 34 35
    double cycle_time;
    double feedback2_factor;
    double feedback3_factor;
36
    double clock_period;
37
    int count;
38 39
};

40 41 42
TimeFilter *ff_timefilter_new(double clock_period,
                              double feedback2_factor,
                              double feedback3_factor)
43
{
44 45 46 47 48
    TimeFilter *self = av_mallocz(sizeof(TimeFilter));

    if (!self)
        return NULL;

49 50 51
    self->clock_period     = clock_period;
    self->feedback2_factor = feedback2_factor;
    self->feedback3_factor = feedback3_factor;
52 53 54 55 56 57 58 59 60 61
    return self;
}

void ff_timefilter_destroy(TimeFilter *self)
{
    av_freep(&self);
}

void ff_timefilter_reset(TimeFilter *self)
{
62
    self->count = 0;
63 64
}

65
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
66
{
67
    self->count++;
68 69
    if (self->count == 1) {
        self->cycle_time = system_time;
70
    } else {
71
        double loop_error;
72 73
        self->cycle_time += self->clock_period * period;
        loop_error = system_time - self->cycle_time;
74

75
        self->cycle_time   += FFMAX(self->feedback2_factor, 1.0 / self->count) * loop_error;
76
        self->clock_period += self->feedback3_factor * loop_error / period;
77 78 79
    }
    return self->cycle_time;
}
Michael Niedermayer's avatar
Michael Niedermayer committed
80 81

#ifdef TEST
82 83 84
#include "libavutil/lfg.h"
#define LFG_MAX ((1LL << 32) - 1)

85 86
int main(void)
{
87
    AVLFG prng;
88
    double n0, n1;
Michael Niedermayer's avatar
Michael Niedermayer committed
89 90 91
#define SAMPLES 1000
    double ideal[SAMPLES];
    double samples[SAMPLES];
92 93 94 95 96
    for (n0 = 0; n0 < 40; n0 = 2 * n0 + 1) {
        for (n1 = 0; n1 < 10; n1 = 2 * n1 + 1) {
            double best_error = 1000000000;
            double bestpar0   = 1;
            double bestpar1   = 0.001;
Michael Niedermayer's avatar
Michael Niedermayer committed
97 98
            int better, i;

99
            av_lfg_init(&prng, 123);
100 101 102
            for (i = 0; i < SAMPLES; i++) {
                ideal[i]   = 10 + i + n1 * i / (1000);
                samples[i] = ideal[i] + n0 * (av_lfg_get(&prng) - LFG_MAX / 2) / (LFG_MAX * 10LL);
Michael Niedermayer's avatar
Michael Niedermayer committed
103 104
            }

105
            do {
Michael Niedermayer's avatar
Michael Niedermayer committed
106
                double par0, par1;
107 108 109 110 111
                better = 0;
                for (par0 = bestpar0 * 0.8; par0 <= bestpar0 * 1.21; par0 += bestpar0 * 0.05) {
                    for (par1 = bestpar1 * 0.8; par1 <= bestpar1 * 1.21; par1 += bestpar1 * 0.05) {
                        double error   = 0;
                        TimeFilter *tf = ff_timefilter_new(1, par0, par1);
112 113 114 115
                        if (!tf) {
                            printf("Could not allocate memory for timefilter.\n");
                            exit(1);
                        }
116
                        for (i = 0; i < SAMPLES; i++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
117
                            double filtered;
118 119
                            filtered = ff_timefilter_update(tf, samples[i], 1);
                            error   += (filtered - ideal[i]) * (filtered - ideal[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
120 121
                        }
                        ff_timefilter_destroy(tf);
122 123 124 125 126
                        if (error < best_error) {
                            best_error = error;
                            bestpar0   = par0;
                            bestpar1   = par1;
                            better     = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
127 128 129
                        }
                    }
                }
130
            } while (better);
131
#if 0
132 133 134
            double lastfil = 9;
            TimeFilter *tf = ff_timefilter_new(1, bestpar0, bestpar1);
            for (i = 0; i < SAMPLES; i++) {
135
                double filtered;
136 137 138 139
                filtered = ff_timefilter_update(tf, samples[i], 1);
                printf("%f %f %f %f\n", i - samples[i] + 10, filtered - samples[i],
                       samples[FFMAX(i, 1)] - samples[FFMAX(i - 1, 0)], filtered - lastfil);
                lastfil = filtered;
140 141 142
            }
            ff_timefilter_destroy(tf);
#else
143
            printf(" [%f %f %9f]", bestpar0, bestpar1, best_error);
144
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
145 146 147
        }
        printf("\n");
    }
148
    return 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
149 150
}
#endif