vp9_dequantize.c 10.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
#include "vp9_rtcd.h"
13
#include "vp9/decoder/vp9_dequantize.h"
John Koleszar's avatar
John Koleszar committed
14
#include "vpx_mem/vpx_mem.h"
15
#include "vp9/decoder/vp9_onyxd_int.h"
16
#include "vp9/common/vp9_common.h"
Yunqing Wang's avatar
Yunqing Wang committed
17 18
static void add_residual(const int16_t *diff, const uint8_t *pred, int pitch,
                         uint8_t *dest, int stride, int width, int height) {
Yunqing Wang's avatar
Yunqing Wang committed
19 20 21 22
  int r, c;

  for (r = 0; r < height; r++) {
    for (c = 0; c < width; c++) {
23
      dest[c] = clip_pixel(diff[c] + pred[c]);
Yunqing Wang's avatar
Yunqing Wang committed
24 25 26 27 28 29 30 31
    }

    dest += stride;
    diff += width;
    pred += pitch;
  }
}

Yunqing Wang's avatar
Yunqing Wang committed
32 33 34 35 36 37 38
static void add_constant_residual(const int16_t diff, const uint8_t *pred,
                                  int pitch, uint8_t *dest, int stride,
                                  int width, int height) {
  int r, c;

  for (r = 0; r < height; r++) {
    for (c = 0; c < width; c++) {
39
      dest[c] = clip_pixel(diff + pred[c]);
Yunqing Wang's avatar
Yunqing Wang committed
40 41 42 43 44 45 46
    }

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

47
void vp9_dequantize_b_c(BLOCKD *d) {
48

John Koleszar's avatar
John Koleszar committed
49
  int i;
Yunqing Wang's avatar
Yunqing Wang committed
50
  int16_t *DQ  = d->dqcoeff;
51 52
  const int16_t *Q   = d->qcoeff;
  const int16_t *DQC = d->dequant;
John Koleszar's avatar
John Koleszar committed
53

John Koleszar's avatar
John Koleszar committed
54 55 56
  for (i = 0; i < 16; i++) {
    DQ[i] = Q[i] * DQC[i];
  }
John Koleszar's avatar
John Koleszar committed
57 58
}

Jingning Han's avatar
Jingning Han committed
59

60 61
void vp9_ht_dequant_idct_add_c(TX_TYPE tx_type, int16_t *input,
                               const int16_t *dq,
Yunqing Wang's avatar
Yunqing Wang committed
62
                               uint8_t *pred, uint8_t *dest,
Scott LaVarnway's avatar
Scott LaVarnway committed
63
                               int pitch, int stride, uint16_t eobs) {
Yunqing Wang's avatar
Yunqing Wang committed
64 65
  int16_t output[16];
  int16_t *diff_ptr = output;
Jingning Han's avatar
Jingning Han committed
66 67 68 69 70 71
  int i;

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

Scott LaVarnway's avatar
Scott LaVarnway committed
72
  vp9_ihtllm(input, output, 4 << 1, tx_type, 4, eobs);
Jingning Han's avatar
Jingning Han committed
73 74 75

  vpx_memset(input, 0, 32);

Yunqing Wang's avatar
Yunqing Wang committed
76
  add_residual(diff_ptr, pred, pitch, dest, stride, 4, 4);
Jingning Han's avatar
Jingning Han committed
77 78
}

79 80
void vp9_ht_dequant_idct_add_8x8_c(TX_TYPE tx_type, int16_t *input,
                                   const int16_t *dq,
Yunqing Wang's avatar
Yunqing Wang committed
81
                                   uint8_t *pred, uint8_t *dest,
Scott LaVarnway's avatar
Scott LaVarnway committed
82
                                   int pitch, int stride, uint16_t eobs) {
Yunqing Wang's avatar
Yunqing Wang committed
83 84
  int16_t output[64];
  int16_t *diff_ptr = output;
85
  int i;
Scott LaVarnway's avatar
Scott LaVarnway committed
86 87 88 89 90 91 92 93
  if (eobs == 0) {
    /* All 0 DCT coefficient */
    vp9_copy_mem8x8(pred, pitch, dest, stride);
  } else if (eobs > 0) {
    input[0] = dq[0] * input[0];
    for (i = 1; i < 64; i++) {
      input[i] = dq[1] * input[i];
    }
94

95 96 97
#if CONFIG_INTHT
    vp9_short_iht8x8(input, output, tx_type, 16);
#else
Scott LaVarnway's avatar
Scott LaVarnway committed
98
    vp9_ihtllm(input, output, 16, tx_type, 8, eobs);
99
#endif
Scott LaVarnway's avatar
Scott LaVarnway committed
100
    vpx_memset(input, 0, 128);
101

Scott LaVarnway's avatar
Scott LaVarnway committed
102 103
    add_residual(diff_ptr, pred, pitch, dest, stride, 8, 8);
  }
104 105
}

106
void vp9_dequant_idct_add_c(int16_t *input, const int16_t *dq, uint8_t *pred,
Yunqing Wang's avatar
Yunqing Wang committed
107 108 109
                            uint8_t *dest, int pitch, int stride) {
  int16_t output[16];
  int16_t *diff_ptr = output;
John Koleszar's avatar
John Koleszar committed
110
  int i;
John Koleszar's avatar
John Koleszar committed
111

John Koleszar's avatar
John Koleszar committed
112 113 114
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Yaowu Xu's avatar
Yaowu Xu committed
115

John Koleszar's avatar
John Koleszar committed
116
  /* the idct halves ( >> 1) the pitch */
117
  vp9_short_idct4x4llm_c(input, output, 4 << 1);
118

John Koleszar's avatar
John Koleszar committed
119
  vpx_memset(input, 0, 32);
120

Yunqing Wang's avatar
Yunqing Wang committed
121
  add_residual(diff_ptr, pred, pitch, dest, stride, 4, 4);
John Koleszar's avatar
John Koleszar committed
122 123
}

124 125
void vp9_dequant_dc_idct_add_c(int16_t *input, const int16_t *dq, uint8_t *pred,
                               uint8_t *dest, int pitch, int stride, int Dc) {
John Koleszar's avatar
John Koleszar committed
126
  int i;
Yunqing Wang's avatar
Yunqing Wang committed
127 128
  int16_t output[16];
  int16_t *diff_ptr = output;
John Koleszar's avatar
John Koleszar committed
129

Yunqing Wang's avatar
Yunqing Wang committed
130
  input[0] = (int16_t)Dc;
Yaowu Xu's avatar
Yaowu Xu committed
131

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

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

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

Yunqing Wang's avatar
Yunqing Wang committed
141
  add_residual(diff_ptr, pred, pitch, dest, stride, 4, 4);
John Koleszar's avatar
John Koleszar committed
142
}
143

Hui Su's avatar
Hui Su committed
144
#if CONFIG_LOSSLESS
145
void vp9_dequant_idct_add_lossless_c(int16_t *input, const int16_t *dq,
Yunqing Wang's avatar
Yunqing Wang committed
146
                                     uint8_t *pred, uint8_t *dest,
147
                                     int pitch, int stride) {
Yunqing Wang's avatar
Yunqing Wang committed
148 149
  int16_t output[16];
  int16_t *diff_ptr = output;
John Koleszar's avatar
John Koleszar committed
150
  int i;
Hui Su's avatar
Hui Su committed
151

John Koleszar's avatar
John Koleszar committed
152 153 154
  for (i = 0; i < 16; i++) {
    input[i] = dq[i] * input[i];
  }
Hui Su's avatar
Hui Su committed
155

156
  vp9_short_inv_walsh4x4_x8_c(input, output, 4 << 1);
Hui Su's avatar
Hui Su committed
157

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

Yunqing Wang's avatar
Yunqing Wang committed
160
  add_residual(diff_ptr, pred, pitch, dest, stride, 4, 4);
Hui Su's avatar
Hui Su committed
161 162
}

163
void vp9_dequant_dc_idct_add_lossless_c(int16_t *input, const int16_t *dq,
Yunqing Wang's avatar
Yunqing Wang committed
164 165
                                        uint8_t *pred,
                                        uint8_t *dest,
166
                                        int pitch, int stride, int dc) {
John Koleszar's avatar
John Koleszar committed
167
  int i;
Yunqing Wang's avatar
Yunqing Wang committed
168 169
  int16_t output[16];
  int16_t *diff_ptr = output;
Hui Su's avatar
Hui Su committed
170

Yunqing Wang's avatar
Yunqing Wang committed
171
  input[0] = (int16_t)dc;
Hui Su's avatar
Hui Su committed
172

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

177
  vp9_short_inv_walsh4x4_x8_c(input, output, 4 << 1);
John Koleszar's avatar
John Koleszar committed
178
  vpx_memset(input, 0, 32);
Hui Su's avatar
Hui Su committed
179

Yunqing Wang's avatar
Yunqing Wang committed
180
  add_residual(diff_ptr, pred, pitch, dest, stride, 4, 4);
Hui Su's avatar
Hui Su committed
181 182 183
}
#endif

184
void vp9_dequantize_b_2x2_c(BLOCKD *d) {
John Koleszar's avatar
John Koleszar committed
185
  int i;
Yunqing Wang's avatar
Yunqing Wang committed
186
  int16_t *DQ  = d->dqcoeff;
187 188
  const int16_t *Q   = d->qcoeff;
  const int16_t *DQC = d->dequant;
189

John Koleszar's avatar
John Koleszar committed
190
  for (i = 0; i < 16; i++) {
Yunqing Wang's avatar
Yunqing Wang committed
191
    DQ[i] = (int16_t)((Q[i] * DQC[i]));
John Koleszar's avatar
John Koleszar committed
192
  }
193 194
}

195 196
void vp9_dequant_idct_add_8x8_c(int16_t *input, const int16_t *dq,
                                uint8_t *pred, uint8_t *dest, int pitch,
197
                                int stride, int dc, int eob) {
Yunqing Wang's avatar
Yunqing Wang committed
198 199
  int16_t output[64];
  int16_t *diff_ptr = output;
John Koleszar's avatar
John Koleszar committed
200
  int i;
201

Yunqing Wang's avatar
Yunqing Wang committed
202 203 204 205 206
  /* If dc is 1, then input[0] is the reconstructed value, do not need
   * dequantization. Also, when dc is 1, dc is counted in eobs, namely eobs >=1.
   */
  if (!dc)
    input[0] *= dq[0];
207

Yunqing Wang's avatar
Yunqing Wang committed
208 209 210 211 212
  /* The calculation can be simplified if there are not many non-zero dct
   * coefficients. Use eobs to decide what to do.
   * TODO(yunqingwang): "eobs = 1" case is also handled in vp9_short_idct8x8_c.
   * Combine that with code here.
   */
213
  if (eob == 0) {
Yunqing Wang's avatar
Yunqing Wang committed
214 215
    /* All 0 DCT coefficient */
    vp9_copy_mem8x8(pred, pitch, dest, stride);
216
  } else if (eob == 1) {
Yunqing Wang's avatar
Yunqing Wang committed
217
    /* DC only DCT coefficient. */
Yaowu Xu's avatar
Yaowu Xu committed
218
    int16_t in = input[0];
Yunqing Wang's avatar
Yunqing Wang committed
219 220 221
    int16_t out;
    /* Note: the idct1 will need to be modified accordingly whenever
     * vp9_short_idct8x8_c() is modified. */
Yaowu Xu's avatar
Yaowu Xu committed
222
    vp9_short_idct1_8x8_c(&in, &out);
Yunqing Wang's avatar
Yunqing Wang committed
223
    input[0] = 0;
224

Yunqing Wang's avatar
Yunqing Wang committed
225
    add_constant_residual(out, pred, pitch, dest, stride, 8, 8);
226
  } else if (eob <= 10) {
Yunqing Wang's avatar
Yunqing Wang committed
227 228 229 230 231 232 233 234 235
    input[1] = input[1] * dq[1];
    input[2] = input[2] * dq[1];
    input[3] = input[3] * dq[1];
    input[8] = input[8] * dq[1];
    input[9] = input[9] * dq[1];
    input[10] = input[10] * dq[1];
    input[16] = input[16] * dq[1];
    input[17] = input[17] * dq[1];
    input[24] = input[24] * dq[1];
John Koleszar's avatar
John Koleszar committed
236

Yunqing Wang's avatar
Yunqing Wang committed
237
    vp9_short_idct10_8x8_c(input, output, 16);
238

Yunqing Wang's avatar
Yunqing Wang committed
239 240 241 242
    input[0] = input[1] = input[2] = input[3] = 0;
    input[8] = input[9] = input[10] = 0;
    input[16] = input[17] = 0;
    input[24] = 0;
John Koleszar's avatar
John Koleszar committed
243

Yunqing Wang's avatar
Yunqing Wang committed
244 245 246 247 248
    add_residual(diff_ptr, pred, pitch, dest, stride, 8, 8);
  } else {
    // recover quantizer for 4 4x4 blocks
    for (i = 1; i < 64; i++) {
      input[i] = input[i] * dq[1];
249
    }
Yunqing Wang's avatar
Yunqing Wang committed
250 251
    // the idct halves ( >> 1) the pitch
    vp9_short_idct8x8_c(input, output, 16);
252

Yunqing Wang's avatar
Yunqing Wang committed
253
    vpx_memset(input, 0, 128);
254

Yunqing Wang's avatar
Yunqing Wang committed
255
    add_residual(diff_ptr, pred, pitch, dest, stride, 8, 8);
256

Yunqing Wang's avatar
Yunqing Wang committed
257
  }
258 259
}

Yunqing Wang's avatar
Yunqing Wang committed
260
void vp9_ht_dequant_idct_add_16x16_c(TX_TYPE tx_type, int16_t *input,
261
                                     const int16_t *dq, uint8_t *pred,
Scott LaVarnway's avatar
Scott LaVarnway committed
262 263
                                     uint8_t *dest, int pitch, int stride,
                                     uint16_t eobs) {
Yunqing Wang's avatar
Yunqing Wang committed
264 265
  int16_t output[256];
  int16_t *diff_ptr = output;
Yunqing Wang's avatar
Yunqing Wang committed
266
  int i;
Scott LaVarnway's avatar
Scott LaVarnway committed
267 268 269 270 271
  if (eobs == 0) {
    /* All 0 DCT coefficient */
    vp9_copy_mem16x16(pred, pitch, dest, stride);
  } else if (eobs > 0) {
    input[0]= input[0] * dq[0];
272

Scott LaVarnway's avatar
Scott LaVarnway committed
273 274 275
    // recover quantizer for 4 4x4 blocks
    for (i = 1; i < 256; i++)
      input[i] = input[i] * dq[1];
276

Scott LaVarnway's avatar
Scott LaVarnway committed
277 278
    // inverse hybrid transform
    vp9_ihtllm(input, output, 32, tx_type, 16, eobs);
279

Scott LaVarnway's avatar
Scott LaVarnway committed
280 281
    // the idct halves ( >> 1) the pitch
    // vp9_short_idct16x16_c(input, output, 32);
282

Scott LaVarnway's avatar
Scott LaVarnway committed
283
    vpx_memset(input, 0, 512);
284

Scott LaVarnway's avatar
Scott LaVarnway committed
285 286
    add_residual(diff_ptr, pred, pitch, dest, stride, 16, 16);
  }
287 288
}

289 290
void vp9_dequant_idct_add_16x16_c(int16_t *input, const int16_t *dq,
                                  uint8_t *pred, uint8_t *dest, int pitch,
291
                                  int stride, int eob) {
Yunqing Wang's avatar
Yunqing Wang committed
292 293
  int16_t output[256];
  int16_t *diff_ptr = output;
Yunqing Wang's avatar
Yunqing Wang committed
294
  int i;
Daniel Kang's avatar
Daniel Kang committed
295

Yunqing Wang's avatar
Yunqing Wang committed
296 297
  /* The calculation can be simplified if there are not many non-zero dct
   * coefficients. Use eobs to separate different cases. */
298
  if (eob == 0) {
Yunqing Wang's avatar
Yunqing Wang committed
299 300
    /* All 0 DCT coefficient */
    vp9_copy_mem16x16(pred, pitch, dest, stride);
301
  } else if (eob == 1) {
Yunqing Wang's avatar
Yunqing Wang committed
302
    /* DC only DCT coefficient. */
Yaowu Xu's avatar
Yaowu Xu committed
303
    int16_t in = input[0] * dq[0];
Yunqing Wang's avatar
Yunqing Wang committed
304
    int16_t out;
Yunqing Wang's avatar
Yunqing Wang committed
305 306
    /* Note: the idct1 will need to be modified accordingly whenever
     * vp9_short_idct16x16_c() is modified. */
Yaowu Xu's avatar
Yaowu Xu committed
307
    vp9_short_idct1_16x16_c(&in, &out);
Yunqing Wang's avatar
Yunqing Wang committed
308
    input[0] = 0;
Daniel Kang's avatar
Daniel Kang committed
309

Yunqing Wang's avatar
Yunqing Wang committed
310
    add_constant_residual(out, pred, pitch, dest, stride, 16, 16);
311
  } else if (eob <= 10) {
Yunqing Wang's avatar
Yunqing Wang committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    input[0]= input[0] * dq[0];
    input[1] = input[1] * dq[1];
    input[2] = input[2] * dq[1];
    input[3] = input[3] * dq[1];
    input[16] = input[16] * dq[1];
    input[17] = input[17] * dq[1];
    input[18] = input[18] * dq[1];
    input[32] = input[32] * dq[1];
    input[33] = input[33] * dq[1];
    input[48] = input[48] * dq[1];

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

    input[0] = input[1] = input[2] = input[3] = 0;
    input[16] = input[17] = input[18] = 0;
    input[32] = input[33] = 0;
    input[48] = 0;

Yunqing Wang's avatar
Yunqing Wang committed
331
    add_residual(diff_ptr, pred, pitch, dest, stride, 16, 16);
Yunqing Wang's avatar
Yunqing Wang committed
332 333 334 335 336 337 338 339 340 341 342 343
  } else {
    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
    vp9_short_idct16x16_c(input, output, 32);

    vpx_memset(input, 0, 512);

Yunqing Wang's avatar
Yunqing Wang committed
344
    add_residual(diff_ptr, pred, pitch, dest, stride, 16, 16);
Daniel Kang's avatar
Daniel Kang committed
345 346
  }
}
347

348 349 350
void vp9_dequant_idct_add_32x32_c(int16_t *input, const int16_t *dq,
                                  uint8_t *pred, uint8_t *dest, int pitch,
                                  int stride, int eob) {
351
  int16_t output[1024];
352 353
  int i;

354
  if (eob) {
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    input[0] = input[0] * dq[0] / 2;
#if !CONFIG_DWTDCTHYBRID
    if (eob == 1) {
      vp9_short_idct1_32x32_c(input, output);
      add_constant_residual(output[0], pred, pitch, dest, stride, 32, 32);
      input[0] = 0;
    } else {
#endif
      for (i = 1; i < 1024; i++)
        input[i] = input[i] * dq[1] / 2;
      vp9_short_idct32x32_c(input, output, 64);
      vpx_memset(input, 0, 2048);
      add_residual(output, pred, pitch, dest, stride, 32, 32);
#if !CONFIG_DWTDCTHYBRID
    }
#endif
371
  }
372 373
}

374 375 376
void vp9_dequant_idct_add_uv_block_16x16_c(int16_t *q, const int16_t *dq,
                                           uint8_t *dstu,
                                           uint8_t *dstv,
377
                                           int stride,
378
                                           uint16_t *eobs) {
379 380 381 382
  vp9_dequant_idct_add_16x16_c(q, dq, dstu, dstu, stride, stride, eobs[0]);
  vp9_dequant_idct_add_16x16_c(q + 256, dq,
                               dstv, dstv, stride, stride, eobs[4]);
}