~ubuntu-branches/ubuntu/karmic/emesene/karmic

« back to all changes in this revision

Viewing changes to libmimic/fdct_quant.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-08-24 10:58:29 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090824105829-rlawufckorou1exz
Tags: 1.5-1ubuntu1
* Merge from debian unstable, remaining changes (LP: #417371):
  - Add check_notifications.patch to check if the current notification
    system supports actions
  - Drop python-dcop suggests, not in the archive anymore
* Drop debian/patches/currentSong_plugin_support_amarok2.patch:
  merged upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005  Ole André Vadla Ravnås <oleavr@gmail.com>
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 */
 
17
 
 
18
#include "mimic-private.h"
 
19
 
 
20
extern guchar _col_zag[64];
 
21
 
 
22
void _fdct_quant_block(MimCtx *ctx, gint *block, const guchar *src,
 
23
                       gint stride, gboolean is_chrom, gint num_coeffs)
 
24
{
 
25
    gint sum1, sum2, sum3, sum4;
 
26
    gint diff1, diff2, diff3, diff4;
 
27
    gint ex1, ex2, ex3, ex4, ex5;
 
28
    gint i, j;
 
29
    const guchar *p1;
 
30
    gint *iptr;
 
31
 
 
32
    /*
 
33
     * Forward DCT, first pass (horizontal).
 
34
     */
 
35
    p1 = src;
 
36
    iptr = block;
 
37
 
 
38
    for (i = 0; i < 8; i++) {
 
39
        sum1 = p1[0] + p1[7];
 
40
        sum2 = p1[1] + p1[6];
 
41
        sum3 = p1[2] + p1[5];
 
42
        sum4 = p1[3] + p1[4];
 
43
 
 
44
        diff1 = p1[0] - p1[7];
 
45
        diff2 = p1[1] - p1[6];
 
46
        diff3 = p1[2] - p1[5];
 
47
        diff4 = p1[3] - p1[4];
 
48
 
 
49
        ex1 = ((diff1 + diff4) *  851) - (diff1 *  282);
 
50
        ex2 = ((diff2 + diff3) * 1004) - (diff2 *  804);
 
51
        ex3 = ((diff2 + diff3) * 1004) - (diff3 * 1204);
 
52
        ex4 = ((diff1 + diff4) *  851) - (diff4 * 1420);
 
53
 
 
54
        iptr[0] = sum1 + sum2 + sum3 + sum4;
 
55
        iptr[2] = (((sum1 - sum4) * 1337) + ((sum2 - sum3) * 554)) >> 10;
 
56
        iptr[4] = sum1 - sum2 - sum3 + sum4;
 
57
 
 
58
        iptr[1] = (ex1 + ex2 + ex3 + ex4) >> 10;
 
59
        iptr[3] = ((ex4 - ex2) * 181) >> 17;
 
60
        iptr[5] = ((ex1 - ex3) * 181) >> 17;
 
61
 
 
62
        p1 += stride;
 
63
        iptr += 8;
 
64
    }
 
65
 
 
66
    p1 = src;
 
67
    iptr = block;
 
68
 
 
69
    /*
 
70
     * Forward DCT, first pass (vertical).
 
71
     * 
 
72
     * This is only known to be correct for i == 0, though it seems to be ...
 
73
     */
 
74
    for (i = 0; i < 6; i++) {
 
75
        sum1 = iptr[ 0 + i] + iptr[56 + i];
 
76
        sum2 = iptr[ 8 + i] + iptr[48 + i];
 
77
        sum3 = iptr[16 + i] + iptr[40 + i];
 
78
        sum4 = iptr[24 + i] + iptr[32 + i];
 
79
 
 
80
        diff1 = iptr[ 0 + i] - iptr[56 + i];
 
81
        diff2 = iptr[ 8 + i] - iptr[48 + i];
 
82
        diff3 = iptr[16 + i] - iptr[40 + i];
 
83
        diff4 = iptr[24 + i] - iptr[32 + i];
 
84
 
 
85
        ex1 = ((diff1 + diff4) *  851) - (diff1 *  282);
 
86
        ex2 = ((diff2 + diff3) * 1004) - (diff2 *  804);
 
87
        ex3 = ((diff2 + diff3) * 1004) - (diff3 * 1204);
 
88
        ex4 = ((diff1 + diff4) *  851) - (diff4 * 1420);
 
89
 
 
90
        ex5 = (sum1 + sum2 - sum3 - sum4) * 554;
 
91
 
 
92
        for (j = 0; j < 7 - i; j++) {
 
93
            switch (j) {
 
94
 
 
95
                case 0:
 
96
                    iptr[ 0 + i] = (16 + sum1 + sum2 + sum3 + sum4) >> 5;
 
97
                    break;
 
98
 
 
99
                case 1:
 
100
                    iptr[ 8 + i] = (16384 + ex1 + ex2 + ex3 + ex4) >> 15;
 
101
                    break;
 
102
 
 
103
                case 2:
 
104
                    iptr[16 + i] = (16384 + ((sum1 - sum4) * 783) + ex5) >> 15;
 
105
                    break;
 
106
 
 
107
                case 3:
 
108
                    iptr[24 + i] = (8192 + (((ex4 - ex2) >> 8) * 181)) >> 14;
 
109
                    break;
 
110
 
 
111
                case 4:
 
112
                    iptr[32 + i] = (16 + sum1 - sum2 - sum3 + sum4) >>  5;
 
113
                    break;
 
114
 
 
115
                case 5:
 
116
                    iptr[40 + i] = (8192 + (((ex1 - ex3) >> 8) * 181)) >> 14;
 
117
                    break;
 
118
 
 
119
                case 6:
 
120
                    iptr[48 + i] = (16384 - ((sum2 - sum3) * 1891) + ex5) >> 15;
 
121
                    break;
 
122
            }
 
123
        }
 
124
    }
 
125
 
 
126
    /*
 
127
     * Quantize.
 
128
     */
 
129
    block[0] /= 2;
 
130
    block[8] /= 4;
 
131
    block[1] /= 4;
 
132
    block[6] = 0;
 
133
 
 
134
    if (num_coeffs > 3) {
 
135
 
 
136
        gdouble s = (10000 - ctx->quality) * 10.0 * (gfloat) 9.9999997e-5;
 
137
 
 
138
        if (s > 10.0)
 
139
            s = 10.0;
 
140
        else if (is_chrom != 0 && s < 1.0)
 
141
            s = 1.0;
 
142
        else if (s < 2.0)
 
143
            s = 2.0;
 
144
 
 
145
        s = 1.0 / s;
 
146
 
 
147
        for (i = 3; i < num_coeffs; i++) {
 
148
 
 
149
            gdouble coeff, r;
 
150
 
 
151
            coeff = block[_col_zag[i]] * s;
 
152
            r = coeff - (gint) coeff;
 
153
 
 
154
            if (r >= 0.6)
 
155
                block[_col_zag[i]] = (gint) (coeff + 1.0);
 
156
            else if (r <= -0.6)
 
157
                block[_col_zag[i]] = (gint) (coeff - 1.0);
 
158
            else
 
159
                block[_col_zag[i]] = (gint) coeff;
 
160
 
 
161
            if (block[_col_zag[i]] > 120)
 
162
                block[_col_zag[i]] = 120;
 
163
            else if (block[_col_zag[i]] < -120)
 
164
                block[_col_zag[i]] = -120;
 
165
        }
 
166
    }
 
167
 
 
168
    if (block[8] > 120)
 
169
        block[8] = 120;
 
170
    else if (block[8] < -120)
 
171
        block[8] = -120;
 
172
 
 
173
    if (block[1] > 120)
 
174
        block[1] = 120;
 
175
    else if (block[1] < -120)
 
176
        block[1] = -120;
 
177
 
 
178
    for (i = num_coeffs; i < 64; i++)
 
179
        block[_col_zag[i]] = 0;
 
180
}
 
181