~ubuntu-branches/debian/sid/v4l-utils/sid

« back to all changes in this revision

Viewing changes to lib/libv4lconvert/jidctflt.c

  • Committer: Bazaar Package Importer
  • Author(s): Gregor Jasny
  • Date: 2010-05-07 20:48:34 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100507204834-ga01cxhz3fekk47r
Tags: 0.8.0-1
* New upstream version
* Switch to 3.0 (quilt) source format
* Re-enable pristine-tar
* Split utils package into command line and the Qt based qv4l2
  (Closes: #576422)
* Update upstream URL

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
 * implementation, accuracy is lost due to imprecise representation of the
70
70
 * scaled quantization values.  However, that problem does not arise if
71
71
 * we use floating point arithmetic.
72
 
 */
 
72
*/
73
73
 
74
74
#include <stdint.h>
75
75
#include "tinyjpeg-internal.h"
76
76
 
77
77
#define FAST_FLOAT float
78
78
#define DCTSIZE    8
79
 
#define DCTSIZE2   (DCTSIZE*DCTSIZE)
 
79
#define DCTSIZE2   (DCTSIZE * DCTSIZE)
80
80
 
81
 
#define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))
 
81
#define DEQUANTIZE(coef, quantval)  (((FAST_FLOAT) (coef)) * (quantval))
82
82
 
83
83
#if defined(__GNUC__) && (defined(__i686__) || defined(__x86_64__))
84
84
 
85
85
static inline unsigned char descale_and_clamp(int x, int shift)
86
86
{
87
 
  __asm__ (
88
 
      "add %3,%1\n"
89
 
      "\tsar %2,%1\n"
90
 
      "\tsub $-128,%1\n"
91
 
      "\tcmovl %5,%1\n" /* Use the sub to compare to 0 */
92
 
      "\tcmpl %4,%1\n"
93
 
      "\tcmovg %4,%1\n"
94
 
      : "=r"(x)
95
 
      : "0"(x), "Ic"((unsigned char)shift), "ir"(1U<<(shift-1)), "r" (0xff), "r" (0)
96
 
      );
97
 
  return x;
 
87
        __asm__ (
 
88
                "add %3,%1\n"
 
89
                "\tsar %2,%1\n"
 
90
                "\tsub $-128,%1\n"
 
91
                "\tcmovl %5,%1\n"       /* Use the sub to compare to 0 */
 
92
                "\tcmpl %4,%1\n"
 
93
                "\tcmovg %4,%1\n"
 
94
                : "=r"(x)
 
95
                : "0"(x), "Ic"((unsigned char)shift), "ir" (1U << (shift - 1)), "r" (0xff), "r" (0)
 
96
                );
 
97
        return x;
98
98
}
99
99
 
100
100
#else
101
101
static inline unsigned char descale_and_clamp(int x, int shift)
102
102
{
103
 
  x += (1UL<<(shift-1));
104
 
  if (x<0)
105
 
    x = (x >> shift) | ((~(0UL)) << (32-(shift)));
106
 
  else
107
 
    x >>= shift;
108
 
  x += 128;
109
 
  if (x>255)
110
 
    return 255;
111
 
  else if (x<0)
112
 
    return 0;
113
 
  else
114
 
    return x;
 
103
        x += 1UL << (shift - 1);
 
104
        if (x < 0)
 
105
                x = (x >> shift) | ((~(0UL)) << (32 - (shift)));
 
106
        else
 
107
                x >>= shift;
 
108
        x += 128;
 
109
        if (x > 255)
 
110
                return 255;
 
111
        if (x < 0)
 
112
                return 0;
 
113
        return x;
115
114
}
116
115
#endif
117
116
 
119
118
 * Perform dequantization and inverse DCT on one block of coefficients.
120
119
 */
121
120
 
122
 
void
123
 
tinyjpeg_idct_float (struct component *compptr, uint8_t *output_buf, int stride)
 
121
void tinyjpeg_idct_float(struct component *compptr, uint8_t *output_buf, int stride)
124
122
{
125
 
  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
126
 
  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
127
 
  FAST_FLOAT z5, z10, z11, z12, z13;
128
 
  int16_t *inptr;
129
 
  FAST_FLOAT *quantptr;
130
 
  FAST_FLOAT *wsptr;
131
 
  uint8_t *outptr;
132
 
  int ctr;
133
 
  FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
134
 
 
135
 
  /* Pass 1: process columns from input, store into work array. */
136
 
 
137
 
  inptr = compptr->DCT;
138
 
  quantptr = compptr->Q_table;
139
 
  wsptr = workspace;
140
 
  for (ctr = DCTSIZE; ctr > 0; ctr--) {
141
 
    /* Due to quantization, we will usually find that many of the input
142
 
     * coefficients are zero, especially the AC terms.  We can exploit this
143
 
     * by short-circuiting the IDCT calculation for any column in which all
144
 
     * the AC terms are zero.  In that case each output is equal to the
145
 
     * DC coefficient (with scale factor as needed).
146
 
     * With typical images and quantization tables, half or more of the
147
 
     * column DCT calculations can be simplified this way.
148
 
     */
149
 
 
150
 
    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
151
 
        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
152
 
        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
153
 
        inptr[DCTSIZE*7] == 0) {
154
 
      /* AC terms all zero */
155
 
      FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
156
 
 
157
 
      wsptr[DCTSIZE*0] = dcval;
158
 
      wsptr[DCTSIZE*1] = dcval;
159
 
      wsptr[DCTSIZE*2] = dcval;
160
 
      wsptr[DCTSIZE*3] = dcval;
161
 
      wsptr[DCTSIZE*4] = dcval;
162
 
      wsptr[DCTSIZE*5] = dcval;
163
 
      wsptr[DCTSIZE*6] = dcval;
164
 
      wsptr[DCTSIZE*7] = dcval;
165
 
 
166
 
      inptr++;                  /* advance pointers to next column */
167
 
      quantptr++;
168
 
      wsptr++;
169
 
      continue;
170
 
    }
171
 
 
172
 
    /* Even part */
173
 
 
174
 
    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
175
 
    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
176
 
    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
177
 
    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
178
 
 
179
 
    tmp10 = tmp0 + tmp2;        /* phase 3 */
180
 
    tmp11 = tmp0 - tmp2;
181
 
 
182
 
    tmp13 = tmp1 + tmp3;        /* phases 5-3 */
183
 
    tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
184
 
 
185
 
    tmp0 = tmp10 + tmp13;       /* phase 2 */
186
 
    tmp3 = tmp10 - tmp13;
187
 
    tmp1 = tmp11 + tmp12;
188
 
    tmp2 = tmp11 - tmp12;
189
 
 
190
 
    /* Odd part */
191
 
 
192
 
    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
193
 
    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
194
 
    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
195
 
    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
196
 
 
197
 
    z13 = tmp6 + tmp5;          /* phase 6 */
198
 
    z10 = tmp6 - tmp5;
199
 
    z11 = tmp4 + tmp7;
200
 
    z12 = tmp4 - tmp7;
201
 
 
202
 
    tmp7 = z11 + z13;           /* phase 5 */
203
 
    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
204
 
 
205
 
    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
206
 
    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
207
 
    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
208
 
 
209
 
    tmp6 = tmp12 - tmp7;        /* phase 2 */
210
 
    tmp5 = tmp11 - tmp6;
211
 
    tmp4 = tmp10 + tmp5;
212
 
 
213
 
    wsptr[DCTSIZE*0] = tmp0 + tmp7;
214
 
    wsptr[DCTSIZE*7] = tmp0 - tmp7;
215
 
    wsptr[DCTSIZE*1] = tmp1 + tmp6;
216
 
    wsptr[DCTSIZE*6] = tmp1 - tmp6;
217
 
    wsptr[DCTSIZE*2] = tmp2 + tmp5;
218
 
    wsptr[DCTSIZE*5] = tmp2 - tmp5;
219
 
    wsptr[DCTSIZE*4] = tmp3 + tmp4;
220
 
    wsptr[DCTSIZE*3] = tmp3 - tmp4;
221
 
 
222
 
    inptr++;                    /* advance pointers to next column */
223
 
    quantptr++;
224
 
    wsptr++;
225
 
  }
226
 
 
227
 
  /* Pass 2: process rows from work array, store into output array. */
228
 
  /* Note that we must descale the results by a factor of 8 == 2**3. */
229
 
 
230
 
  wsptr = workspace;
231
 
  outptr = output_buf;
232
 
  for (ctr = 0; ctr < DCTSIZE; ctr++) {
233
 
    /* Rows of zeroes can be exploited in the same way as we did with columns.
234
 
     * However, the column calculation has created many nonzero AC terms, so
235
 
     * the simplification applies less often (typically 5% to 10% of the time).
236
 
     * And testing floats for zero is relatively expensive, so we don't bother.
237
 
     */
238
 
 
239
 
    /* Even part */
240
 
 
241
 
    tmp10 = wsptr[0] + wsptr[4];
242
 
    tmp11 = wsptr[0] - wsptr[4];
243
 
 
244
 
    tmp13 = wsptr[2] + wsptr[6];
245
 
    tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
246
 
 
247
 
    tmp0 = tmp10 + tmp13;
248
 
    tmp3 = tmp10 - tmp13;
249
 
    tmp1 = tmp11 + tmp12;
250
 
    tmp2 = tmp11 - tmp12;
251
 
 
252
 
    /* Odd part */
253
 
 
254
 
    z13 = wsptr[5] + wsptr[3];
255
 
    z10 = wsptr[5] - wsptr[3];
256
 
    z11 = wsptr[1] + wsptr[7];
257
 
    z12 = wsptr[1] - wsptr[7];
258
 
 
259
 
    tmp7 = z11 + z13;
260
 
    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
261
 
 
262
 
    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
263
 
    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
264
 
    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
265
 
 
266
 
    tmp6 = tmp12 - tmp7;
267
 
    tmp5 = tmp11 - tmp6;
268
 
    tmp4 = tmp10 + tmp5;
269
 
 
270
 
    /* Final output stage: scale down by a factor of 8 and range-limit */
271
 
 
272
 
    outptr[0] = descale_and_clamp((int)(tmp0 + tmp7), 3);
273
 
    outptr[7] = descale_and_clamp((int)(tmp0 - tmp7), 3);
274
 
    outptr[1] = descale_and_clamp((int)(tmp1 + tmp6), 3);
275
 
    outptr[6] = descale_and_clamp((int)(tmp1 - tmp6), 3);
276
 
    outptr[2] = descale_and_clamp((int)(tmp2 + tmp5), 3);
277
 
    outptr[5] = descale_and_clamp((int)(tmp2 - tmp5), 3);
278
 
    outptr[4] = descale_and_clamp((int)(tmp3 + tmp4), 3);
279
 
    outptr[3] = descale_and_clamp((int)(tmp3 - tmp4), 3);
280
 
 
281
 
 
282
 
    wsptr += DCTSIZE;           /* advance pointer to next row */
283
 
    outptr += stride;
284
 
  }
 
123
        FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
 
124
        FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
 
125
        FAST_FLOAT z5, z10, z11, z12, z13;
 
126
        int16_t *inptr;
 
127
        FAST_FLOAT *quantptr;
 
128
        FAST_FLOAT *wsptr;
 
129
        uint8_t *outptr;
 
130
        int ctr;
 
131
        FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
 
132
 
 
133
        /* Pass 1: process columns from input, store into work array. */
 
134
 
 
135
        inptr = compptr->DCT;
 
136
        quantptr = compptr->Q_table;
 
137
        wsptr = workspace;
 
138
        for (ctr = DCTSIZE; ctr > 0; ctr--) {
 
139
                /* Due to quantization, we will usually find that many of the input
 
140
                 * coefficients are zero, especially the AC terms.  We can exploit this
 
141
                 * by short-circuiting the IDCT calculation for any column in which all
 
142
                 * the AC terms are zero.  In that case each output is equal to the
 
143
                 * DC coefficient (with scale factor as needed).
 
144
                 * With typical images and quantization tables, half or more of the
 
145
                 * column DCT calculations can be simplified this way.
 
146
                 */
 
147
 
 
148
                if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
 
149
                                inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
 
150
                                inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
 
151
                                inptr[DCTSIZE*7] == 0) {
 
152
                        /* AC terms all zero */
 
153
                        FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
 
154
 
 
155
                        wsptr[DCTSIZE*0] = dcval;
 
156
                        wsptr[DCTSIZE*1] = dcval;
 
157
                        wsptr[DCTSIZE*2] = dcval;
 
158
                        wsptr[DCTSIZE*3] = dcval;
 
159
                        wsptr[DCTSIZE*4] = dcval;
 
160
                        wsptr[DCTSIZE*5] = dcval;
 
161
                        wsptr[DCTSIZE*6] = dcval;
 
162
                        wsptr[DCTSIZE*7] = dcval;
 
163
 
 
164
                        inptr++;                        /* advance pointers to next column */
 
165
                        quantptr++;
 
166
                        wsptr++;
 
167
                        continue;
 
168
                }
 
169
 
 
170
                /* Even part */
 
171
 
 
172
                tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
 
173
                tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
 
174
                tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
 
175
                tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
 
176
 
 
177
                tmp10 = tmp0 + tmp2;    /* phase 3 */
 
178
                tmp11 = tmp0 - tmp2;
 
179
 
 
180
                tmp13 = tmp1 + tmp3;    /* phases 5-3 */
 
181
                tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
 
182
 
 
183
                tmp0 = tmp10 + tmp13;   /* phase 2 */
 
184
                tmp3 = tmp10 - tmp13;
 
185
                tmp1 = tmp11 + tmp12;
 
186
                tmp2 = tmp11 - tmp12;
 
187
 
 
188
                /* Odd part */
 
189
 
 
190
                tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
 
191
                tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
 
192
                tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
 
193
                tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
 
194
 
 
195
                z13 = tmp6 + tmp5;              /* phase 6 */
 
196
                z10 = tmp6 - tmp5;
 
197
                z11 = tmp4 + tmp7;
 
198
                z12 = tmp4 - tmp7;
 
199
 
 
200
                tmp7 = z11 + z13;               /* phase 5 */
 
201
                tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
 
202
 
 
203
                z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
 
204
                tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
 
205
                tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
 
206
 
 
207
                tmp6 = tmp12 - tmp7;    /* phase 2 */
 
208
                tmp5 = tmp11 - tmp6;
 
209
                tmp4 = tmp10 + tmp5;
 
210
 
 
211
                wsptr[DCTSIZE*0] = tmp0 + tmp7;
 
212
                wsptr[DCTSIZE*7] = tmp0 - tmp7;
 
213
                wsptr[DCTSIZE*1] = tmp1 + tmp6;
 
214
                wsptr[DCTSIZE*6] = tmp1 - tmp6;
 
215
                wsptr[DCTSIZE*2] = tmp2 + tmp5;
 
216
                wsptr[DCTSIZE*5] = tmp2 - tmp5;
 
217
                wsptr[DCTSIZE*4] = tmp3 + tmp4;
 
218
                wsptr[DCTSIZE*3] = tmp3 - tmp4;
 
219
 
 
220
                inptr++;                        /* advance pointers to next column */
 
221
                quantptr++;
 
222
                wsptr++;
 
223
        }
 
224
 
 
225
        /* Pass 2: process rows from work array, store into output array. */
 
226
        /* Note that we must descale the results by a factor of 8 == 2**3. */
 
227
 
 
228
        wsptr = workspace;
 
229
        outptr = output_buf;
 
230
        for (ctr = 0; ctr < DCTSIZE; ctr++) {
 
231
                /* Rows of zeroes can be exploited in the same way as we did with columns.
 
232
                 * However, the column calculation has created many nonzero AC terms, so
 
233
                 * the simplification applies less often (typically 5% to 10% of the time).
 
234
                 * And testing floats for zero is relatively expensive, so we don't bother.
 
235
                 */
 
236
 
 
237
                /* Even part */
 
238
 
 
239
                tmp10 = wsptr[0] + wsptr[4];
 
240
                tmp11 = wsptr[0] - wsptr[4];
 
241
 
 
242
                tmp13 = wsptr[2] + wsptr[6];
 
243
                tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
 
244
 
 
245
                tmp0 = tmp10 + tmp13;
 
246
                tmp3 = tmp10 - tmp13;
 
247
                tmp1 = tmp11 + tmp12;
 
248
                tmp2 = tmp11 - tmp12;
 
249
 
 
250
                /* Odd part */
 
251
 
 
252
                z13 = wsptr[5] + wsptr[3];
 
253
                z10 = wsptr[5] - wsptr[3];
 
254
                z11 = wsptr[1] + wsptr[7];
 
255
                z12 = wsptr[1] - wsptr[7];
 
256
 
 
257
                tmp7 = z11 + z13;
 
258
                tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
 
259
 
 
260
                z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
 
261
                tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
 
262
                tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
 
263
 
 
264
                tmp6 = tmp12 - tmp7;
 
265
                tmp5 = tmp11 - tmp6;
 
266
                tmp4 = tmp10 + tmp5;
 
267
 
 
268
                /* Final output stage: scale down by a factor of 8 and range-limit */
 
269
 
 
270
                outptr[0] = descale_and_clamp((int)(tmp0 + tmp7), 3);
 
271
                outptr[7] = descale_and_clamp((int)(tmp0 - tmp7), 3);
 
272
                outptr[1] = descale_and_clamp((int)(tmp1 + tmp6), 3);
 
273
                outptr[6] = descale_and_clamp((int)(tmp1 - tmp6), 3);
 
274
                outptr[2] = descale_and_clamp((int)(tmp2 + tmp5), 3);
 
275
                outptr[5] = descale_and_clamp((int)(tmp2 - tmp5), 3);
 
276
                outptr[4] = descale_and_clamp((int)(tmp3 + tmp4), 3);
 
277
                outptr[3] = descale_and_clamp((int)(tmp3 - tmp4), 3);
 
278
 
 
279
 
 
280
                wsptr += DCTSIZE;               /* advance pointer to next row */
 
281
                outptr += stride;
 
282
        }
285
283
}
286
284