dequantize.c 11.8 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

32
void vp9_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
void vp9_ht_dequant_idct_add_c(TX_TYPE tx_type, short *input, short *dq,
Jingning Han's avatar
Jingning Han committed
46 47 48 49 50 51 52 53 54 55 56
                               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];
  }

57
  vp8_ihtllm_c(input, output, 4 << 1, tx_type, 4);
Jingning Han's avatar
Jingning Han committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  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;
  }
}

80
void vp9_ht_dequant_idct_add_8x8_c(TX_TYPE tx_type, short *input, short *dq,
81 82 83 84 85 86 87 88 89 90 91 92 93 94
                                   unsigned char *pred, unsigned char *dest,
                                   int pitch, int stride) {
  short output[64];
  short *diff_ptr = output;
  int b, r, c;
  int i;
  unsigned char *origdest = dest;
  unsigned char *origpred = pred;

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

95
  vp8_ihtllm_c(input, output, 16, tx_type, 8);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

  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;
    }
117 118 119 120
    // shift buffer pointers to next 4x4 block in the submacroblock
    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;
121 122 123
  }
}

124
void vp9_dequant_idct_add_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
125 126 127 128 129
                            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
130

John Koleszar's avatar
John Koleszar committed
131 132 133
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Yaowu Xu's avatar
Yaowu Xu committed
134

John Koleszar's avatar
John Koleszar committed
135 136
  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);
137

John Koleszar's avatar
John Koleszar committed
138
  vpx_memset(input, 0, 32);
139

John Koleszar's avatar
John Koleszar committed
140 141 142
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
143

John Koleszar's avatar
John Koleszar committed
144 145
      if (a < 0)
        a = 0;
146

John Koleszar's avatar
John Koleszar committed
147 148
      if (a > 255)
        a = 255;
149

John Koleszar's avatar
John Koleszar committed
150
      dest[c] = (unsigned char) a;
151
    }
John Koleszar's avatar
John Koleszar committed
152 153 154 155 156

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

159
void vp9_dequant_dc_idct_add_c(short *input, short *dq, unsigned char *pred,
Johann's avatar
Johann committed
160
                               unsigned char *dest, int pitch, int stride,
John Koleszar's avatar
John Koleszar committed
161 162 163 164 165
                               int Dc) {
  int i;
  short output[16];
  short *diff_ptr = output;
  int r, c;
John Koleszar's avatar
John Koleszar committed
166

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

John Koleszar's avatar
John Koleszar committed
169 170 171
  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Yaowu Xu's avatar
Yaowu Xu committed
172

John Koleszar's avatar
John Koleszar committed
173 174
  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);
175

John Koleszar's avatar
John Koleszar committed
176
  vpx_memset(input, 0, 32);
177

John Koleszar's avatar
John Koleszar committed
178 179 180
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
181

John Koleszar's avatar
John Koleszar committed
182 183
      if (a < 0)
        a = 0;
184

John Koleszar's avatar
John Koleszar committed
185 186 187 188
      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
189
    }
John Koleszar's avatar
John Koleszar committed
190 191 192 193 194

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

Hui Su's avatar
Hui Su committed
197
#if CONFIG_LOSSLESS
198
void vp9_dequant_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
199 200 201 202 203
                                     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
204

John Koleszar's avatar
John Koleszar committed
205 206 207
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Hui Su's avatar
Hui Su committed
208

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

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

John Koleszar's avatar
John Koleszar committed
213 214 215
  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
216

John Koleszar's avatar
John Koleszar committed
217 218
      if (a < 0)
        a = 0;
Hui Su's avatar
Hui Su committed
219

John Koleszar's avatar
John Koleszar committed
220 221
      if (a > 255)
        a = 255;
Hui Su's avatar
Hui Su committed
222

John Koleszar's avatar
John Koleszar committed
223
      dest[c] = (unsigned char) a;
Hui Su's avatar
Hui Su committed
224
    }
John Koleszar's avatar
John Koleszar committed
225 226 227 228 229

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
Hui Su's avatar
Hui Su committed
230 231
}

232
void vp9_dequant_dc_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
233 234 235 236 237 238
                                        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
239

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

John Koleszar's avatar
John Koleszar committed
242 243 244
  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Hui Su's avatar
Hui Su committed
245

John Koleszar's avatar
John Koleszar committed
246 247
  vp8_short_inv_walsh4x4_x8_c(input, output, 4 << 1);
  vpx_memset(input, 0, 32);
Hui Su's avatar
Hui Su committed
248

John Koleszar's avatar
John Koleszar committed
249 250 251
  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
252

John Koleszar's avatar
John Koleszar committed
253 254
      if (a < 0)
        a = 0;
Hui Su's avatar
Hui Su committed
255

John Koleszar's avatar
John Koleszar committed
256 257 258 259
      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
Hui Su's avatar
Hui Su committed
260
    }
John Koleszar's avatar
John Koleszar committed
261 262 263 264 265

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
Hui Su's avatar
Hui Su committed
266 267 268
}
#endif

269
void vp9_dequantize_b_2x2_c(BLOCKD *d) {
John Koleszar's avatar
John Koleszar committed
270 271 272 273
  int i;
  short *DQ  = d->dqcoeff;
  short *Q   = d->qcoeff;
  short *DQC = d->dequant;
274

John Koleszar's avatar
John Koleszar committed
275 276 277
  for (i = 0; i < 16; i++) {
    DQ[i] = (short)((Q[i] * DQC[i]));
  }
278
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
279 280 281 282 283 284 285 286
  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");
  }
287 288 289
#endif
}

290
void vp9_dequant_idct_add_8x8_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
291 292 293 294 295 296 297
                                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;
298 299

#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
300 301 302 303 304 305
  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");
306
    }
John Koleszar's avatar
John Koleszar committed
307
  }
308
#endif
309

John Koleszar's avatar
John Koleszar committed
310
  input[0] = input[0] * dq[0];
311

John Koleszar's avatar
John Koleszar committed
312 313 314 315
  // recover quantizer for 4 4x4 blocks
  for (i = 1; i < 64; i++) {
    input[i] = input[i] * dq[1];
  }
316
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
317 318 319 320 321 322
  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");
323
    }
John Koleszar's avatar
John Koleszar committed
324
  }
325 326
#endif

John Koleszar's avatar
John Koleszar committed
327 328
  // the idct halves ( >> 1) the pitch
  vp8_short_idct8x8_c(input, output, 16);
329
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
330 331 332 333 334 335
  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");
336
    }
John Koleszar's avatar
John Koleszar committed
337
  }
338 339
#endif

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

John Koleszar's avatar
John Koleszar committed
342 343 344 345
  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];
346

John Koleszar's avatar
John Koleszar committed
347 348
        if (a < 0)
          a = 0;
349

John Koleszar's avatar
John Koleszar committed
350 351
        if (a > 255)
          a = 255;
352

John Koleszar's avatar
John Koleszar committed
353
        dest[c] = (unsigned char) a;
354
      }
John Koleszar's avatar
John Koleszar committed
355 356 357 358 359 360 361 362 363

      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;
  }
364
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
365 366 367 368 369 370
  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]);
371
      }
John Koleszar's avatar
John Koleszar committed
372 373
      printf("\n");
      origdest += stride;
374
    }
John Koleszar's avatar
John Koleszar committed
375
  }
376 377 378
#endif
}

379
void vp9_dequant_dc_idct_add_8x8_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
380 381 382 383 384 385 386 387 388 389 390
                                   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
391
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
392 393 394 395 396 397
  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");
398
    }
John Koleszar's avatar
John Koleszar committed
399
  }
400
#endif
John Koleszar's avatar
John Koleszar committed
401 402 403
  for (i = 1; i < 64; i++) {
    input[i] = input[i] * dq[1];
  }
404 405

#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
406 407 408 409 410 411
  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");
412
    }
John Koleszar's avatar
John Koleszar committed
413
  }
414 415
#endif

John Koleszar's avatar
John Koleszar committed
416 417
  // the idct halves ( >> 1) the pitch
  vp8_short_idct8x8_c(input, output, 16);
418
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
419 420 421 422 423 424
  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");
425
    }
John Koleszar's avatar
John Koleszar committed
426
  }
427
#endif
John Koleszar's avatar
John Koleszar committed
428
  vpx_memset(input, 0, 128);
429

John Koleszar's avatar
John Koleszar committed
430 431 432 433
  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];
434

John Koleszar's avatar
John Koleszar committed
435 436
        if (a < 0)
          a = 0;
437

John Koleszar's avatar
John Koleszar committed
438 439
        if (a > 255)
          a = 255;
440

John Koleszar's avatar
John Koleszar committed
441
        dest[c] = (unsigned char) a;
442
      }
John Koleszar's avatar
John Koleszar committed
443 444 445 446 447 448 449 450 451

      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;
  }
452
#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
453 454 455 456 457 458
  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]);
459
      }
John Koleszar's avatar
John Koleszar committed
460 461
      printf("\n");
      origdest += stride;
462
    }
John Koleszar's avatar
John Koleszar committed
463
  }
464 465 466
#endif
}

467
void vp9_ht_dequant_idct_add_16x16_c(TX_TYPE tx_type, short *input, short *dq,
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
                                     unsigned char *pred, unsigned char *dest,
                                     int pitch, int stride) {
  short output[256];
  short *diff_ptr = output;
  int r, c, i;

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

  // recover quantizer for 4 4x4 blocks
  for (i = 1; i < 256; i++)
    input[i] = input[i] * dq[1];

  // inverse hybrid transform
  vp8_ihtllm_c(input, output, 32, tx_type, 16);

  // the idct halves ( >> 1) the pitch
  // vp8_short_idct16x16_c(input, output, 32);

  vpx_memset(input, 0, 512);

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

      if (a < 0)
        a = 0;
      else if (a > 255)
        a = 255;

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

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

506
void vp9_dequant_idct_add_16x16_c(short *input, short *dq, unsigned char *pred,
Daniel Kang's avatar
Daniel Kang committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
                                  unsigned char *dest, int pitch, int stride) {
  short output[256];
  short *diff_ptr = output;
  int r, c, i;

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

  // recover quantizer for 4 4x4 blocks
  for (i = 1; i < 256; i++)
    input[i] = input[i] * dq[1];

  // the idct halves ( >> 1) the pitch
  vp8_short_idct16x16_c(input, output, 32);

  vpx_memset(input, 0, 512);

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

      if (a < 0)
        a = 0;
      else if (a > 255)
        a = 255;

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

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