annotate dct.c @ 11497:6962a149f0a4 libavcodec

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