dct.c 5.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * (I)DCT Transforms
 * Copyright (c) 2009 Peter Ross <pross@xvid.org>
 * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
 * Copyright (c) 2010 Vitor Sessak
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
25
 * @file
26 27 28 29 30
 * (Inverse) Discrete Cosine Transforms. These are also known as the
 * type II and type III DCTs respectively.
 */

#include <math.h>
31
#include <string.h>
32

33
#include "libavutil/mathematics.h"
34
#include "dct.h"
35
#include "dct32.h"
36

37 38
/* sin((M_PI * x / (2 * n)) */
#define SIN(s, n, x) (s->costab[(n) - (x)])
39

40 41
/* cos((M_PI * x / (2 * n)) */
#define COS(s, n, x) (s->costab[x])
42

43
static void dst_calc_I_c(DCTContext *ctx, FFTSample *data)
44 45 46 47 48
{
    int n = 1 << ctx->nbits;
    int i;

    data[0] = 0;
49 50 51 52 53 54 55 56 57
    for (i = 1; i < n / 2; i++) {
        float tmp1   = data[i    ];
        float tmp2   = data[n - i];
        float s      = SIN(ctx, n, 2 * i);

        s           *= tmp1 + tmp2;
        tmp1         = (tmp1 - tmp2) * 0.5f;
        data[i]      = s + tmp1;
        data[n - i]  = s - tmp1;
58 59
    }

60
    data[n / 2] *= 2;
61
    ctx->rdft.rdft_calc(&ctx->rdft, data);
62 63 64

    data[0] *= 0.5f;

65 66 67
    for (i = 1; i < n - 2; i += 2) {
        data[i + 1] +=  data[i - 1];
        data[i]      = -data[i + 2];
68 69
    }

70
    data[n - 1] = 0;
71 72
}

73
static void dct_calc_I_c(DCTContext *ctx, FFTSample *data)
74 75 76 77 78
{
    int n = 1 << ctx->nbits;
    int i;
    float next = -0.5f * (data[0] - data[n]);

79 80
    for (i = 0; i < n / 2; i++) {
        float tmp1 = data[i];
81
        float tmp2 = data[n - i];
82 83
        float s    = SIN(ctx, n, 2 * i);
        float c    = COS(ctx, n, 2 * i);
84 85 86 87 88 89

        c *= tmp1 - tmp2;
        s *= tmp1 - tmp2;

        next += c;

90 91
        tmp1        = (tmp1 + tmp2) * 0.5f;
        data[i]     = tmp1 - s;
92 93 94
        data[n - i] = tmp1 + s;
    }

95
    ctx->rdft.rdft_calc(&ctx->rdft, data);
96 97 98
    data[n] = data[1];
    data[1] = next;

99
    for (i = 3; i <= n; i += 2)
100 101 102
        data[i] = data[i - 2] - data[i];
}

103
static void dct_calc_III_c(DCTContext *ctx, FFTSample *data)
104 105 106 107
{
    int n = 1 << ctx->nbits;
    int i;

108
    float next  = data[n - 1];
Vitor Sessak's avatar
Vitor Sessak committed
109
    float inv_n = 1.0f / n;
110

Vitor Sessak's avatar
Vitor Sessak committed
111
    for (i = n - 2; i >= 2; i -= 2) {
112
        float val1 = data[i];
Vitor Sessak's avatar
Vitor Sessak committed
113
        float val2 = data[i - 1] - data[i + 1];
114 115
        float c    = COS(ctx, n, i);
        float s    = SIN(ctx, n, i);
116

117
        data[i]     = c * val1 + s * val2;
Vitor Sessak's avatar
Vitor Sessak committed
118 119
        data[i + 1] = s * val1 - c * val2;
    }
120

Vitor Sessak's avatar
Vitor Sessak committed
121
    data[1] = 2 * next;
122

123
    ctx->rdft.rdft_calc(&ctx->rdft, data);
124

Vitor Sessak's avatar
Vitor Sessak committed
125
    for (i = 0; i < n / 2; i++) {
126
        float tmp1 = data[i]         * inv_n;
Vitor Sessak's avatar
Vitor Sessak committed
127
        float tmp2 = data[n - i - 1] * inv_n;
128
        float csc  = ctx->csc2[i] * (tmp1 - tmp2);
129

130 131 132
        tmp1            += tmp2;
        data[i]          = tmp1 + csc;
        data[n - i - 1]  = tmp1 - csc;
Vitor Sessak's avatar
Vitor Sessak committed
133
    }
134 135
}

136
static void dct_calc_II_c(DCTContext *ctx, FFTSample *data)
137 138 139
{
    int n = 1 << ctx->nbits;
    int i;
Vitor Sessak's avatar
Vitor Sessak committed
140
    float next;
141

142 143
    for (i = 0; i < n / 2; i++) {
        float tmp1 = data[i];
Vitor Sessak's avatar
Vitor Sessak committed
144
        float tmp2 = data[n - i - 1];
145
        float s    = SIN(ctx, n, 2 * i + 1);
146

147 148
        s    *= tmp1 - tmp2;
        tmp1  = (tmp1 + tmp2) * 0.5f;
149

150
        data[i]     = tmp1 + s;
Vitor Sessak's avatar
Vitor Sessak committed
151 152
        data[n-i-1] = tmp1 - s;
    }
153

154
    ctx->rdft.rdft_calc(&ctx->rdft, data);
155

156
    next     = data[1] * 0.5;
Vitor Sessak's avatar
Vitor Sessak committed
157
    data[1] *= -1;
158

Vitor Sessak's avatar
Vitor Sessak committed
159 160 161
    for (i = n - 2; i >= 0; i -= 2) {
        float inr = data[i    ];
        float ini = data[i + 1];
162 163
        float c   = COS(ctx, n, i);
        float s   = SIN(ctx, n, i);
164

165 166
        data[i]     = c * inr + s * ini;
        data[i + 1] = next;
167

168
        next += s * inr - c * ini;
Vitor Sessak's avatar
Vitor Sessak committed
169
    }
170 171
}

172 173 174
static void dct32_func(DCTContext *ctx, FFTSample *data)
{
    ctx->dct32(data, data);
175 176
}

177
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
178 179 180 181
{
    int n = 1 << nbits;
    int i;

182 183
    memset(s, 0, sizeof(*s));

184 185
    s->nbits   = nbits;
    s->inverse = inverse;
186

187 188 189
    if (inverse == DCT_II && nbits == 5) {
        s->dct_calc = dct32_func;
    } else {
190
        ff_init_ff_cos_tabs(nbits + 2);
191

192 193
        s->costab = ff_cos_tabs[nbits + 2];
        s->csc2   = av_malloc(n / 2 * sizeof(FFTSample));
194

Mans Rullgard's avatar
Mans Rullgard committed
195 196 197 198
        if (ff_rdft_init(&s->rdft, nbits, inverse == DCT_III) < 0) {
            av_free(s->csc2);
            return -1;
        }
199

200 201
        for (i = 0; i < n / 2; i++)
            s->csc2[i] = 0.5 / sin((M_PI / (2 * n) * (2 * i + 1)));
202

203
        switch (inverse) {
204 205 206 207
        case DCT_I  : s->dct_calc = dct_calc_I_c;   break;
        case DCT_II : s->dct_calc = dct_calc_II_c;  break;
        case DCT_III: s->dct_calc = dct_calc_III_c; break;
        case DST_I  : s->dct_calc = dst_calc_I_c;   break;
Mans Rullgard's avatar
Mans Rullgard committed
208
        }
209
    }
210

211
    s->dct32 = ff_dct32_float;
212 213
    if (ARCH_X86)
        ff_dct_init_x86(s);
214

215 216 217
    return 0;
}

218 219 220 221 222
av_cold void ff_dct_end(DCTContext *s)
{
    ff_rdft_end(&s->rdft);
    av_free(s->csc2);
}