comparison dct.c @ 10944:0985f1f7ab72 libavcodec

Floating point discrete cosine transform
author vitor
date Wed, 20 Jan 2010 00:39:47 +0000
parents
children 4b3da727d832
comparison
equal deleted inserted replaced
10943:3a723e8dcd26 10944:0985f1f7ab72
1 /*
2 * (I)DCT Transforms
3 * Copyright (c) 2009 Peter Ross <pross@xvid.org>
4 * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
5 * Copyright (c) 2010 Vitor Sessak
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file libavcodec/dct.c
26 * (Inverse) Discrete Cosine Transforms. These are also known as the
27 * type II and type III DCTs respectively.
28 */
29
30 #include <math.h>
31 #include "dsputil.h"
32
33 av_cold int ff_dct_init(DCTContext *s, int nbits, int inverse)
34 {
35 int n = 1 << nbits;
36 int i;
37
38 s->nbits = nbits;
39 s->inverse = inverse;
40
41 ff_init_ff_cos_tabs(nbits+2);
42
43 s->costab = ff_cos_tabs[nbits+2];
44
45 s->csc2 = av_malloc(n/2 * sizeof(FFTSample));
46
47 if (ff_rdft_init(&s->rdft, nbits, inverse) < 0) {
48 av_free(s->csc2);
49 return -1;
50 }
51
52 for (i = 0; i < n/2; i++)
53 s->csc2[i] = 0.5 / sin((M_PI / (2*n) * (2*i + 1)));
54
55 return 0;
56 }
57
58 /* sin((M_PI * x / (2*n)) */
59 #define SIN(s,n,x) (s->costab[(n) - (x)])
60
61 /* cos((M_PI * x / (2*n)) */
62 #define COS(s,n,x) (s->costab[x])
63
64 static void ff_dct_calc_c(DCTContext *ctx, FFTSample *data)
65 {
66 int n = 1 << ctx->nbits;
67 int i;
68
69 if (ctx->inverse) {
70 float next = data[n - 1];
71 float inv_n = 1.0f / n;
72
73 for (i = n - 2; i >= 2; i -= 2) {
74 float val1 = data[i ];
75 float val2 = data[i - 1] - data[i + 1];
76 float c = COS(ctx, n, i);
77 float s = SIN(ctx, n, i);
78
79 data[i ] = c * val1 + s * val2;
80 data[i + 1] = s * val1 - c * val2;
81 }
82
83 data[1] = 2 * next;
84
85 ff_rdft_calc(&ctx->rdft, data);
86
87 for (i = 0; i < n / 2; i++) {
88 float tmp1 = data[i ] * inv_n;
89 float tmp2 = data[n - i - 1] * inv_n;
90 float csc = ctx->csc2[i] * (tmp1 - tmp2);
91
92 tmp1 += tmp2;
93 data[i ] = tmp1 + csc;
94 data[n - i - 1] = tmp1 - csc;
95 }
96 } else {
97 float next;
98 for (i=0; i < n/2; i++) {
99 float tmp1 = data[i ];
100 float tmp2 = data[n - i - 1];
101 float s = SIN(ctx, n, 2*i + 1);
102
103 s *= tmp1 - tmp2;
104 tmp1 = (tmp1 + tmp2) * 0.5f;
105
106 data[i ] = tmp1 + s;
107 data[n-i-1] = tmp1 - s;
108 }
109
110 ff_rdft_calc(&ctx->rdft, data);
111
112 next = data[1] * 0.5;
113 data[1] *= -1;
114
115 for (i = n - 2; i >= 0; i -= 2) {
116 float inr = data[i ];
117 float ini = data[i + 1];
118 float c = COS(ctx, n, i);
119 float s = SIN(ctx, n, i);
120
121 data[i ] = c * inr + s * ini;
122
123 data[i+1] = next;
124
125 next += s * inr - c * ini;
126 }
127 }
128 }
129
130 void ff_dct_calc(DCTContext *s, FFTSample *data)
131 {
132 ff_dct_calc_c(s, data);
133 }
134
135 av_cold void ff_dct_end(DCTContext *s)
136 {
137 ff_rdft_end(&s->rdft);
138 av_free(s->csc2);
139 }