dequantize.c 9.13 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10 11 12 13
 */


#include "vpx_ports/config.h"
#include "dequantize.h"
John Koleszar's avatar
John Koleszar committed
14
#include "vp8/common/idct.h"
John Koleszar's avatar
John Koleszar committed
15
#include "vpx_mem/vpx_mem.h"
16
#include "onyxd_int.h"
John Koleszar's avatar
John Koleszar committed
17

John Koleszar's avatar
John Koleszar committed
18
extern void vp8_short_idct4x4llm_c(short *input, short *output, int pitch);
John Koleszar's avatar
John Koleszar committed
19
extern void vp8_short_idct4x4llm_1_c(short *input, short *output, int pitch);
20 21
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);
John Koleszar's avatar
John Koleszar committed
22

Hui Su's avatar
Hui Su committed
23 24 25 26 27
#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

28 29 30
#ifdef DEC_DEBUG
extern int dec_debug;
#endif
John Koleszar's avatar
John Koleszar committed
31

John Koleszar's avatar
John Koleszar committed
32
void vp8_dequantize_b_c(BLOCKD *d) {
33

John Koleszar's avatar
John Koleszar committed
34 35 36 37
  int i;
  short *DQ  = d->dqcoeff;
  short *Q   = d->qcoeff;
  short *DQC = d->dequant;
John Koleszar's avatar
John Koleszar committed
38

John Koleszar's avatar
John Koleszar committed
39 40 41
  for (i = 0; i < 16; i++) {
    DQ[i] = Q[i] * DQC[i];
  }
John Koleszar's avatar
John Koleszar committed
42 43
}

Jingning Han's avatar
Jingning Han committed
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

#if CONFIG_HYBRIDTRANSFORM
void vp8_ht_dequant_idct_add_c(TX_TYPE tx_type, 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_iht4x4llm_c( input, output, 4 << 1, tx_type );

  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

Johann's avatar
Johann committed
82
void vp8_dequant_idct_add_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
83 84 85 86 87
                            unsigned char *dest, int pitch, int stride) {
  short output[16];
  short *diff_ptr = output;
  int r, c;
  int i;
John Koleszar's avatar
John Koleszar committed
88

John Koleszar's avatar
John Koleszar committed
89 90 91
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Yaowu Xu's avatar
Yaowu Xu committed
92

John Koleszar's avatar
John Koleszar committed
93 94
  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);
95

John Koleszar's avatar
John Koleszar committed
96
  vpx_memset(input, 0, 32);
97

John Koleszar's avatar
John Koleszar committed
98 99 100
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
101

John Koleszar's avatar
John Koleszar committed
102 103
      if (a < 0)
        a = 0;
104

John Koleszar's avatar
John Koleszar committed
105 106
      if (a > 255)
        a = 255;
107

John Koleszar's avatar
John Koleszar committed
108
      dest[c] = (unsigned char) a;
109
    }
John Koleszar's avatar
John Koleszar committed
110 111 112 113 114

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
John Koleszar's avatar
John Koleszar committed
115 116
}

Johann's avatar
Johann committed
117 118
void vp8_dequant_dc_idct_add_c(short *input, short *dq, unsigned char *pred,
                               unsigned char *dest, int pitch, int stride,
John Koleszar's avatar
John Koleszar committed
119 120 121 122 123
                               int Dc) {
  int i;
  short output[16];
  short *diff_ptr = output;
  int r, c;
John Koleszar's avatar
John Koleszar committed
124

John Koleszar's avatar
John Koleszar committed
125
  input[0] = (short)Dc;
Yaowu Xu's avatar
Yaowu Xu committed
126

John Koleszar's avatar
John Koleszar committed
127 128 129
  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Yaowu Xu's avatar
Yaowu Xu committed
130

John Koleszar's avatar
John Koleszar committed
131 132
  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);
133

John Koleszar's avatar
John Koleszar committed
134
  vpx_memset(input, 0, 32);
135

John Koleszar's avatar
John Koleszar committed
136 137 138
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
139

John Koleszar's avatar
John Koleszar committed
140 141
      if (a < 0)
        a = 0;
142

John Koleszar's avatar
John Koleszar committed
143 144 145 146
      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
147
    }
John Koleszar's avatar
John Koleszar committed
148 149 150 151 152

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
John Koleszar's avatar
John Koleszar committed
153
}
154

Hui Su's avatar
Hui Su committed
155 156
#if CONFIG_LOSSLESS
void vp8_dequant_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
157 158 159 160 161
                                     unsigned char *dest, int pitch, int stride) {
  short output[16];
  short *diff_ptr = output;
  int r, c;
  int i;
Hui Su's avatar
Hui Su committed
162

John Koleszar's avatar
John Koleszar committed
163 164 165
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Hui Su's avatar
Hui Su committed
166

John Koleszar's avatar
John Koleszar committed
167
  vp8_short_inv_walsh4x4_x8_c(input, output, 4 << 1);
Hui Su's avatar
Hui Su committed
168

John Koleszar's avatar
John Koleszar committed
169
  vpx_memset(input, 0, 32);
Hui Su's avatar
Hui Su committed
170

John Koleszar's avatar
John Koleszar committed
171 172 173
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
Hui Su's avatar
Hui Su committed
174

John Koleszar's avatar
John Koleszar committed
175 176
      if (a < 0)
        a = 0;
Hui Su's avatar
Hui Su committed
177

John Koleszar's avatar
John Koleszar committed
178 179
      if (a > 255)
        a = 255;
Hui Su's avatar
Hui Su committed
180

John Koleszar's avatar
John Koleszar committed
181
      dest[c] = (unsigned char) a;
Hui Su's avatar
Hui Su committed
182
    }
John Koleszar's avatar
John Koleszar committed
183 184 185 186 187

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
Hui Su's avatar
Hui Su committed
188 189 190
}

void vp8_dequant_dc_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
191 192 193 194 195 196
                                        unsigned char *dest, int pitch, int stride,
                                        int Dc) {
  int i;
  short output[16];
  short *diff_ptr = output;
  int r, c;
Hui Su's avatar
Hui Su committed
197

John Koleszar's avatar
John Koleszar committed
198
  input[0] = (short)Dc;
Hui Su's avatar
Hui Su committed
199

John Koleszar's avatar
John Koleszar committed
200 201 202
  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Hui Su's avatar
Hui Su committed
203

John Koleszar's avatar
John Koleszar committed
204 205
  vp8_short_inv_walsh4x4_x8_c(input, output, 4 << 1);
  vpx_memset(input, 0, 32);
Hui Su's avatar
Hui Su committed
206

John Koleszar's avatar
John Koleszar committed
207 208 209
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
Hui Su's avatar
Hui Su committed
210

John Koleszar's avatar
John Koleszar committed
211 212
      if (a < 0)
        a = 0;
Hui Su's avatar
Hui Su committed
213

John Koleszar's avatar
John Koleszar committed
214 215 216 217
      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
Hui Su's avatar
Hui Su committed
218
    }
John Koleszar's avatar
John Koleszar committed
219 220 221 222 223

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
Hui Su's avatar
Hui Su committed
224 225 226
}
#endif

John Koleszar's avatar
John Koleszar committed
227 228 229 230 231
void vp8_dequantize_b_2x2_c(BLOCKD *d) {
  int i;
  short *DQ  = d->dqcoeff;
  short *Q   = d->qcoeff;
  short *DQC = d->dequant;
232

John Koleszar's avatar
John Koleszar committed
233 234 235
  for (i = 0; i < 16; i++) {
    DQ[i] = (short)((Q[i] * DQC[i]));
  }
236
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
237 238 239 240 241 242 243 244
  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");
  }
245 246 247 248
#endif
}

void vp8_dequant_idct_add_8x8_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
249 250 251 252 253 254 255
                                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;
256 257

#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
258 259 260 261 262 263
  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");
264
    }
John Koleszar's avatar
John Koleszar committed
265
  }
266
#endif
267

John Koleszar's avatar
John Koleszar committed
268
  input[0] = input[0] * dq[0];
269

John Koleszar's avatar
John Koleszar committed
270 271 272 273
  // recover quantizer for 4 4x4 blocks
  for (i = 1; i < 64; i++) {
    input[i] = input[i] * dq[1];
  }
274
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
275 276 277 278 279 280
  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");
281
    }
John Koleszar's avatar
John Koleszar committed
282
  }
283 284
#endif

John Koleszar's avatar
John Koleszar committed
285 286
  // the idct halves ( >> 1) the pitch
  vp8_short_idct8x8_c(input, output, 16);
287
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
288 289 290 291 292 293
  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");
294
    }
John Koleszar's avatar
John Koleszar committed
295
  }
296 297
#endif

John Koleszar's avatar
John Koleszar committed
298
  vpx_memset(input, 0, 128);// test what should i put here
299

John Koleszar's avatar
John Koleszar committed
300 301 302 303
  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];
304

John Koleszar's avatar
John Koleszar committed
305 306
        if (a < 0)
          a = 0;
307

John Koleszar's avatar
John Koleszar committed
308 309
        if (a > 255)
          a = 255;
310

John Koleszar's avatar
John Koleszar committed
311
        dest[c] = (unsigned char) a;
312
      }
John Koleszar's avatar
John Koleszar committed
313 314 315 316 317 318 319 320 321

      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;
  }
322
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
323 324 325 326 327 328
  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]);
329
      }
John Koleszar's avatar
John Koleszar committed
330 331
      printf("\n");
      origdest += stride;
332
    }
John Koleszar's avatar
John Koleszar committed
333
  }
334 335 336 337
#endif
}

void vp8_dequant_dc_idct_add_8x8_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
338 339 340 341 342 343 344 345 346 347 348
                                   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
349
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
350 351 352 353 354 355
  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");
356
    }
John Koleszar's avatar
John Koleszar committed
357
  }
358
#endif
John Koleszar's avatar
John Koleszar committed
359 360 361
  for (i = 1; i < 64; i++) {
    input[i] = input[i] * dq[1];
  }
362 363

#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
364 365 366 367 368 369
  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");
370
    }
John Koleszar's avatar
John Koleszar committed
371
  }
372 373
#endif

John Koleszar's avatar
John Koleszar committed
374 375
  // the idct halves ( >> 1) the pitch
  vp8_short_idct8x8_c(input, output, 16);
376
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
377 378 379 380 381 382
  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");
383
    }
John Koleszar's avatar
John Koleszar committed
384
  }
385
#endif
John Koleszar's avatar
John Koleszar committed
386
  vpx_memset(input, 0, 128);
387

John Koleszar's avatar
John Koleszar committed
388 389 390 391
  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];
392

John Koleszar's avatar
John Koleszar committed
393 394
        if (a < 0)
          a = 0;
395

John Koleszar's avatar
John Koleszar committed
396 397
        if (a > 255)
          a = 255;
398

John Koleszar's avatar
John Koleszar committed
399
        dest[c] = (unsigned char) a;
400
      }
John Koleszar's avatar
John Koleszar committed
401 402 403 404 405 406 407 408 409

      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;
  }
410
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
411 412 413 414 415 416
  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]);
417
      }
John Koleszar's avatar
John Koleszar committed
418 419
      printf("\n");
      origdest += stride;
420
    }
John Koleszar's avatar
John Koleszar committed
421
  }
422 423 424
#endif
}