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

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

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

  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

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#if CONFIG_HYBRIDTRANSFORM8X8
void vp8_ht_dequant_idct_add_8x8_c(TX_TYPE tx_type, short *input, short *dq,
                                   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];
  }

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

  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;
    }
120
121
122
123
    // 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;
124
125
126
127
  }
}
#endif

Johann's avatar
Johann committed
128
void vp8_dequant_idct_add_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
129
130
131
132
133
                            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
134

John Koleszar's avatar
John Koleszar committed
135
136
137
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Yaowu Xu's avatar
Yaowu Xu committed
138

John Koleszar's avatar
John Koleszar committed
139
140
  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);
141

John Koleszar's avatar
John Koleszar committed
142
  vpx_memset(input, 0, 32);
143

John Koleszar's avatar
John Koleszar committed
144
145
146
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
147

John Koleszar's avatar
John Koleszar committed
148
149
      if (a < 0)
        a = 0;
150

John Koleszar's avatar
John Koleszar committed
151
152
      if (a > 255)
        a = 255;
153

John Koleszar's avatar
John Koleszar committed
154
      dest[c] = (unsigned char) a;
155
    }
John Koleszar's avatar
John Koleszar committed
156
157
158
159
160

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

Johann's avatar
Johann committed
163
164
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
165
166
167
168
169
                               int Dc) {
  int i;
  short output[16];
  short *diff_ptr = output;
  int r, c;
John Koleszar's avatar
John Koleszar committed
170

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

John Koleszar's avatar
John Koleszar committed
173
174
175
  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Yaowu Xu's avatar
Yaowu Xu committed
176

John Koleszar's avatar
John Koleszar committed
177
178
  /* the idct halves ( >> 1) the pitch */
  vp8_short_idct4x4llm_c(input, output, 4 << 1);
179

John Koleszar's avatar
John Koleszar committed
180
  vpx_memset(input, 0, 32);
181

John Koleszar's avatar
John Koleszar committed
182
183
184
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      int a = diff_ptr[c] + pred[c];
185

John Koleszar's avatar
John Koleszar committed
186
187
      if (a < 0)
        a = 0;
188

John Koleszar's avatar
John Koleszar committed
189
190
191
192
      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
193
    }
John Koleszar's avatar
John Koleszar committed
194
195
196
197
198

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

Hui Su's avatar
Hui Su committed
201
202
#if CONFIG_LOSSLESS
void vp8_dequant_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
203
204
205
206
207
                                     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
208

John Koleszar's avatar
John Koleszar committed
209
210
211
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Hui Su's avatar
Hui Su committed
212

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

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

John Koleszar's avatar
John Koleszar committed
217
218
219
  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
220

John Koleszar's avatar
John Koleszar committed
221
222
      if (a < 0)
        a = 0;
Hui Su's avatar
Hui Su committed
223

John Koleszar's avatar
John Koleszar committed
224
225
      if (a > 255)
        a = 255;
Hui Su's avatar
Hui Su committed
226

John Koleszar's avatar
John Koleszar committed
227
      dest[c] = (unsigned char) a;
Hui Su's avatar
Hui Su committed
228
    }
John Koleszar's avatar
John Koleszar committed
229
230
231
232
233

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
Hui Su's avatar
Hui Su committed
234
235
236
}

void vp8_dequant_dc_idct_add_lossless_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
237
238
239
240
241
242
                                        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
243

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

John Koleszar's avatar
John Koleszar committed
246
247
248
  for (i = 1; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Hui Su's avatar
Hui Su committed
249

John Koleszar's avatar
John Koleszar committed
250
251
  vp8_short_inv_walsh4x4_x8_c(input, output, 4 << 1);
  vpx_memset(input, 0, 32);
Hui Su's avatar
Hui Su committed
252

John Koleszar's avatar
John Koleszar committed
253
254
255
  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
256

John Koleszar's avatar
John Koleszar committed
257
258
      if (a < 0)
        a = 0;
Hui Su's avatar
Hui Su committed
259

John Koleszar's avatar
John Koleszar committed
260
261
262
263
      if (a > 255)
        a = 255;

      dest[c] = (unsigned char) a;
Hui Su's avatar
Hui Su committed
264
    }
John Koleszar's avatar
John Koleszar committed
265
266
267
268
269

    dest += stride;
    diff_ptr += 4;
    pred += pitch;
  }
Hui Su's avatar
Hui Su committed
270
271
272
}
#endif

John Koleszar's avatar
John Koleszar committed
273
274
275
276
277
void vp8_dequantize_b_2x2_c(BLOCKD *d) {
  int i;
  short *DQ  = d->dqcoeff;
  short *Q   = d->qcoeff;
  short *DQC = d->dequant;
278

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

void vp8_dequant_idct_add_8x8_c(short *input, short *dq, unsigned char *pred,
John Koleszar's avatar
John Koleszar committed
295
296
297
298
299
300
301
                                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;
302
303

#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
304
305
306
307
308
309
  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");
310
    }
John Koleszar's avatar
John Koleszar committed
311
  }
312
#endif
313

John Koleszar's avatar
John Koleszar committed
314
  input[0] = input[0] * dq[0];
315

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

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

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

John Koleszar's avatar
John Koleszar committed
346
347
348
349
  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];
350

John Koleszar's avatar
John Koleszar committed
351
352
        if (a < 0)
          a = 0;
353

John Koleszar's avatar
John Koleszar committed
354
355
        if (a > 255)
          a = 255;
356

John Koleszar's avatar
John Koleszar committed
357
        dest[c] = (unsigned char) a;
358
      }
John Koleszar's avatar
John Koleszar committed
359
360
361
362
363
364
365
366
367

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

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

#ifdef DEC_DEBUG
John Koleszar's avatar
John Koleszar committed
410
411
412
413
414
415
  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");
416
    }
John Koleszar's avatar
John Koleszar committed
417
  }
418
419
#endif

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

John Koleszar's avatar
John Koleszar committed
434
435
436
437
  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];
438

John Koleszar's avatar
John Koleszar committed
439
440
        if (a < 0)
          a = 0;
441

John Koleszar's avatar
John Koleszar committed
442
443
        if (a > 255)
          a = 255;
444

John Koleszar's avatar
John Koleszar committed
445
        dest[c] = (unsigned char) a;
446
      }
John Koleszar's avatar
John Koleszar committed
447
448
449
450
451
452
453
454
455

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

Daniel Kang's avatar
Daniel Kang committed
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
506
#if CONFIG_TX16X16
void vp8_dequant_idct_add_16x16_c(short *input, short *dq, 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];

  // 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;
  }
}
#endif