summaryrefslogtreecommitdiff
path: root/vp8/decoder/dequantize.c
blob: 73859b0d759f5b957a45c2b41a9ff44441b4ee9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */


#include "vpx_ports/config.h"
#include "dequantize.h"
#include "vp8/common/idct.h"
#include "vpx_mem/vpx_mem.h"
#include "onyxd_int.h"

extern void vp8_short_idct4x4llm_c(short *input, short *output, int pitch);
extern void vp8_short_idct4x4llm_1_c(short *input, short *output, int pitch);
extern void vp8_short_idct8x8_c(short *input, short *output, int pitch);
extern void vp8_short_idct8x8_1_c(short *input, short *output, int pitch);

#if CONFIG_LOSSLESS
extern void vp8_short_inv_walsh4x4_x8_c(short *input, short *output, int pitch);
extern void vp8_short_inv_walsh4x4_1_x8_c(short *input, short *output, int pitch);
#endif

#ifdef DEC_DEBUG
extern int dec_debug;
#endif

void vp8_dequantize_b_c(BLOCKD *d) {

  int i;
  short *DQ  = d->dqcoeff;
  short *Q   = d->qcoeff;
  short *DQC = d->dequant;

  for (i = 0; i < 16; i++) {
    DQ[i] = Q[i] * DQC[i];
  }
}

void vp8_dequant_idct_add_c(short *input, short *dq, unsigned char *pred,
                            unsigned char *dest, int pitch, int stride) {
  short output[16];
  short *diff_ptr = output;
  int r, c;
  int i;

  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }

  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);

  vpx_memset(input, 0, 32);

  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];

      if (a < 0)
        a = 0;

      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
    }

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
}

void vp8_dequant_dc_idct_add_c(short *input, short *dq, unsigned char *pred,
                               unsigned char *dest, int pitch, int stride,
                               int Dc) {
  int i;
  short output[16];
  short *diff_ptr = output;
  int r, c;

  input[0] = (short)Dc;

  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }

  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);

  vpx_memset(input, 0, 32);

  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];

      if (a < 0)
        a = 0;

      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
    }

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
}

#if CONFIG_LOSSLESS
void vp8_dequant_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
                                     unsigned char *dest, int pitch, int stride) {
  short output[16];
  short *diff_ptr = output;
  int r, c;
  int i;

  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }

  vp8_short_inv_walsh4x4_x8_c(input, output, 4 << 1);

  vpx_memset(input, 0, 32);

  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];

      if (a < 0)
        a = 0;

      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
    }

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
}

void vp8_dequant_dc_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
                                        unsigned char *dest, int pitch, int stride,
                                        int Dc) {
  int i;
  short output[16];
  short *diff_ptr = output;
  int r, c;

  input[0] = (short)Dc;

  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }

  vp8_short_inv_walsh4x4_x8_c(input, output, 4 << 1);
  vpx_memset(input, 0, 32);

  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];

      if (a < 0)
        a = 0;

      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
    }

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
}
#endif

void vp8_dequantize_b_2x2_c(BLOCKD *d) {
  int i;
  short *DQ  = d->dqcoeff;
  short *Q   = d->qcoeff;
  short *DQC = d->dequant;

  for (i = 0; i < 16; i++) {
    DQ[i] = (short)((Q[i] * DQC[i]));
  }
#ifdef DEC_DEBUG
  if (dec_debug) {
    int j;
    printf("Dequantize 2x2\n");
    for (j = 0; j < 16; j++) printf("%d ", Q[j]);
    printf("\n");
    for (j = 0; j < 16; j++) printf("%d ", DQ[j]);
    printf("\n");
  }
#endif
}

void vp8_dequant_idct_add_8x8_c(short *input, short *dq, unsigned char *pred,
                                unsigned char *dest, int pitch, int stride) { // , MACROBLOCKD *xd, short blk_idx
  short output[64];
  short *diff_ptr = output;
  int r, c, b;
  int i;
  unsigned char *origdest = dest;
  unsigned char *origpred = pred;

#ifdef DEC_DEBUG
  if (dec_debug) {
    int j;
    printf("Input 8x8\n");
    for (j = 0; j < 64; j++) {
      printf("%d ", input[j]);
      if (j % 8 == 7) printf("\n");
    }
  }
#endif

  input[0] = input[0] * dq[0];

  // recover quantizer for 4 4x4 blocks
  for (i = 1; i < 64; i++) {
    input[i] = input[i] * dq[1];
  }
#ifdef DEC_DEBUG
  if (dec_debug) {
    int j;
    printf("Input DQ 8x8\n");
    for (j = 0; j < 64; j++) {
      printf("%d ", input[j]);
      if (j % 8 == 7) printf("\n");
    }
  }
#endif

  // the idct halves ( >> 1) the pitch
  vp8_short_idct8x8_c(input, output, 16);
#ifdef DEC_DEBUG
  if (dec_debug) {
    int j;
    printf("Output 8x8\n");
    for (j = 0; j < 64; j++) {
      printf("%d ", output[j]);
      if (j % 8 == 7) printf("\n");
    }
  }
#endif

  vpx_memset(input, 0, 128);// test what should i put here

  for (b = 0; b < 4; b++) {
    for (r = 0; r < 4; r++) {
      for (c = 0; c < 4; c++) {
        int a = diff_ptr[c] + pred[c];

        if (a < 0)
          a = 0;

        if (a > 255)
          a = 255;

        dest[c] = (unsigned char) a;
      }

      dest += stride;
      diff_ptr += 8;
      pred += pitch;
    }
    diff_ptr = output + (b + 1) / 2 * 4 * 8 + (b + 1) % 2 * 4;
    dest = origdest + (b + 1) / 2 * 4 * stride + (b + 1) % 2 * 4;
    pred = origpred + (b + 1) / 2 * 4 * pitch + (b + 1) % 2 * 4;
  }
#ifdef DEC_DEBUG
  if (dec_debug) {
    int k, j;
    printf("Final 8x8\n");
    for (j = 0; j < 8; j++) {
      for (k = 0; k < 8; k++) {
        printf("%d ", origdest[k]);
      }
      printf("\n");
      origdest += stride;
    }
  }
#endif
}

void vp8_dequant_dc_idct_add_8x8_c(short *input, short *dq, unsigned char *pred,
                                   unsigned char *dest, int pitch, int stride,
                                   int Dc) { // Dc for 1st order T in some rear case
  short output[64];
  short *diff_ptr = output;
  int r, c, b;
  int i;
  unsigned char *origdest = dest;
  unsigned char *origpred = pred;

  input[0] = (short)Dc;// Dc is the reconstructed value, do not need dequantization
  // dc value is recovered after dequantization, since dc need not quantization
#ifdef DEC_DEBUG
  if (dec_debug) {
    int j;
    printf("Input 8x8\n");
    for (j = 0; j < 64; j++) {
      printf("%d ", input[j]);
      if (j % 8 == 7) printf("\n");
    }
  }
#endif
  for (i = 1; i < 64; i++) {
    input[i] = input[i] * dq[1];
  }

#ifdef DEC_DEBUG
  if (dec_debug) {
    int j;
    printf("Input DQ 8x8\n");
    for (j = 0; j < 64; j++) {
      printf("%d ", input[j]);
      if (j % 8 == 7) printf("\n");
    }
  }
#endif

  // the idct halves ( >> 1) the pitch
  vp8_short_idct8x8_c(input, output, 16);
#ifdef DEC_DEBUG
  if (dec_debug) {
    int j;
    printf("Output 8x8\n");
    for (j = 0; j < 64; j++) {
      printf("%d ", output[j]);
      if (j % 8 == 7) printf("\n");
    }
  }
#endif
  vpx_memset(input, 0, 128);

  for (b = 0; b < 4; b++) {
    for (r = 0; r < 4; r++) {
      for (c = 0; c < 4; c++) {
        int a = diff_ptr[c] + pred[c];

        if (a < 0)
          a = 0;

        if (a > 255)
          a = 255;

        dest[c] = (unsigned char) a;
      }

      dest += stride;
      diff_ptr += 8;
      pred += pitch;
    }
    diff_ptr = output + (b + 1) / 2 * 4 * 8 + (b + 1) % 2 * 4;
    dest = origdest + (b + 1) / 2 * 4 * stride + (b + 1) % 2 * 4;
    pred = origpred + (b + 1) / 2 * 4 * pitch + (b + 1) % 2 * 4;
  }
#ifdef DEC_DEBUG
  if (dec_debug) {
    int k, j;
    printf("Final 8x8\n");
    for (j = 0; j < 8; j++) {
      for (k = 0; k < 8; k++) {
        printf("%d ", origdest[k]);
      }
      printf("\n");
      origdest += stride;
    }
  }
#endif
}