vp9_idct_blk.c 9.8 KB
Newer Older
Fritz Koenig's avatar
Fritz Koenig committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
Fritz Koenig's avatar
Fritz Koenig committed
3 4 5 6 7 8 9 10
 *
 *  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.
 */

11
#include "vp9_rtcd.h"
12
#include "vp9/common/vp9_blockd.h"
13
#include "vp9/decoder/vp9_idct_blk.h"
Johann's avatar
Johann committed
14

15 16
void vp9_idct_add_y_block_c(int16_t *q, uint8_t *dst, int stride,
                            MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
17 18 19 20
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
21
      vp9_idct_add(q, dst, stride, xd->plane[0].eobs[i * 4  + j]);
John Koleszar's avatar
John Koleszar committed
22 23
      q   += 16;
      dst += 4;
Fritz Koenig's avatar
Fritz Koenig committed
24
    }
John Koleszar's avatar
John Koleszar committed
25 26 27

    dst += 4 * stride - 16;
  }
Fritz Koenig's avatar
Fritz Koenig committed
28 29
}

30 31
void vp9_idct_add_uv_block_c(int16_t *q, uint8_t *dst, int stride,
                             uint16_t *eobs) {
John Koleszar's avatar
John Koleszar committed
32 33 34 35
  int i, j;

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
36
      vp9_idct_add(q, dst, stride, eobs[i * 2 + j]);
37 38
      q   += 16;
      dst += 4;
Fritz Koenig's avatar
Fritz Koenig committed
39
    }
John Koleszar's avatar
John Koleszar committed
40

41
    dst += 4 * stride - 8;
John Koleszar's avatar
John Koleszar committed
42
  }
Fritz Koenig's avatar
Fritz Koenig committed
43
}
44

45 46
void vp9_idct_add_y_block_8x8_c(int16_t *q, uint8_t *dst, int stride,
                                MACROBLOCKD *xd) {
47
  uint8_t *origdest = dst;
48

49 50 51 52 53 54
  vp9_idct_add_8x8_c(q, dst, stride, xd->plane[0].eobs[0]);
  vp9_idct_add_8x8_c(&q[64], origdest + 8, stride, xd->plane[0].eobs[4]);
  vp9_idct_add_8x8_c(&q[128], origdest + 8 * stride, stride,
                     xd->plane[0].eobs[8]);
  vp9_idct_add_8x8_c(&q[192], origdest + 8 * stride + 8, stride,
                     xd->plane[0].eobs[12]);
55 56
}

57 58
void vp9_idct_add_y_block_lossless_c(int16_t *q, uint8_t *dst, int stride,
                                     MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
59 60 61 62
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
63
      vp9_idct_add_lossless_c(q, dst, stride, xd->plane[0].eobs[i * 4 + j]);
John Koleszar's avatar
John Koleszar committed
64 65
      q   += 16;
      dst += 4;
Hui Su's avatar
Hui Su committed
66
    }
John Koleszar's avatar
John Koleszar committed
67 68 69

    dst += 4 * stride - 16;
  }
Hui Su's avatar
Hui Su committed
70 71
}

72 73
void vp9_idct_add_uv_block_lossless_c(int16_t *q, uint8_t *dst, int stride,
                                      uint16_t *eobs) {
John Koleszar's avatar
John Koleszar committed
74 75 76 77
  int i, j;

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
78
      vp9_idct_add_lossless_c(q, dst, stride, eobs[i * 2 + j]);
79 80
      q   += 16;
      dst += 4;
Hui Su's avatar
Hui Su committed
81
    }
John Koleszar's avatar
John Koleszar committed
82

83
    dst += 4 * stride - 8;
John Koleszar's avatar
John Koleszar committed
84
  }
Hui Su's avatar
Hui Su committed
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
static void add_residual(const int16_t *diff, uint8_t *dest, int stride,
                         int width, int height) {
  int r, c;

  for (r = 0; r < height; r++) {
    for (c = 0; c < width; c++)
      dest[c] = clip_pixel(diff[c] + dest[c]);

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

void vp9_add_residual_4x4_c(const int16_t *diff, uint8_t *dest, int stride) {
  add_residual(diff, dest, stride, 4, 4);
}

void vp9_add_residual_8x8_c(const int16_t *diff, uint8_t *dest, int stride) {
  add_residual(diff, dest, stride, 8, 8);
}

void vp9_add_residual_16x16_c(const int16_t *diff, uint8_t *dest, int stride) {
  add_residual(diff, dest, stride, 16, 16);
}

void vp9_add_residual_32x32_c(const int16_t *diff, uint8_t *dest, int stride) {
  add_residual(diff, dest, stride, 32, 32);
}

static void add_constant_residual(const int16_t diff, uint8_t *dest, int stride,
                                  int width, int height) {
  int r, c;

  for (r = 0; r < height; r++) {
    for (c = 0; c < width; c++)
      dest[c] = clip_pixel(diff + dest[c]);

    dest += stride;
  }
}

void vp9_add_constant_residual_8x8_c(const int16_t diff, uint8_t *dest,
                                     int stride) {
  add_constant_residual(diff, dest, stride, 8, 8);
}

void vp9_add_constant_residual_16x16_c(const int16_t diff, uint8_t *dest,
                                       int stride) {
  add_constant_residual(diff, dest, stride, 16, 16);
}

void vp9_add_constant_residual_32x32_c(const int16_t diff,  uint8_t *dest,
                                       int stride) {
  add_constant_residual(diff, dest, stride, 32, 32);
}

void vp9_iht_add_c(TX_TYPE tx_type, int16_t *input, uint8_t *dest, int stride,
                   int eob) {
  if (tx_type == DCT_DCT) {
    vp9_idct_add(input, dest, stride, eob);
  } else {
    DECLARE_ALIGNED_ARRAY(16, int16_t, output, 16);

    vp9_short_iht4x4(input, output, 4, tx_type);
    vpx_memset(input, 0, 32);
    vp9_add_residual_4x4(output, dest, stride);
  }
}

void vp9_iht_add_8x8_c(TX_TYPE tx_type, int16_t *input, uint8_t *dest,
                       int stride, int eob) {
  if (tx_type == DCT_DCT) {
    vp9_idct_add_8x8(input, dest, stride, eob);
  } else {
    if (eob > 0) {
      DECLARE_ALIGNED_ARRAY(16, int16_t, output, 64);

      vp9_short_iht8x8(input, output, 8, tx_type);
      vpx_memset(input, 0, 128);
      vp9_add_residual_8x8(output, dest, stride);
    }
  }
}

void vp9_idct_add_c(int16_t *input, uint8_t *dest, int stride, int eob) {
  DECLARE_ALIGNED_ARRAY(16, int16_t, output, 16);

  if (eob > 1) {
    // the idct halves ( >> 1) the pitch
    vp9_short_idct4x4(input, output, 4 << 1);
    vpx_memset(input, 0, 32);
    vp9_add_residual_4x4(output, dest, stride);
  } else {
    vp9_dc_only_idct_add(input[0], dest, dest, stride, stride);
    ((int *)input)[0] = 0;
  }
}

void vp9_dc_idct_add_c(int16_t *input, uint8_t *dest, int stride, int dc) {
  DECLARE_ALIGNED_ARRAY(16, int16_t, output, 16);

  input[0] = dc;

  // the idct halves ( >> 1) the pitch
  vp9_short_idct4x4(input, output, 4 << 1);
  vpx_memset(input, 0, 32);
  vp9_add_residual_4x4(output, dest, stride);
}

void vp9_idct_add_lossless_c(int16_t *input, uint8_t *dest, int stride,
                             int eob) {
  DECLARE_ALIGNED_ARRAY(16, int16_t, output, 16);

  if (eob > 1) {
    vp9_short_iwalsh4x4_c(input, output, 4 << 1);
    vpx_memset(input, 0, 32);
    vp9_add_residual_4x4(output, dest, stride);
  } else {
    vp9_dc_only_inv_walsh_add(input[0], dest, dest, stride, stride);
    ((int *)input)[0] = 0;
  }
}

void vp9_dc_idct_add_lossless_c(int16_t *input, uint8_t *dest,
                                int stride, int dc) {
  DECLARE_ALIGNED_ARRAY(16, int16_t, output, 16);

  input[0] = dc;
  vp9_short_iwalsh4x4_c(input, output, 4 << 1);
  vpx_memset(input, 0, 32);
  vp9_add_residual_4x4(output, dest, stride);
}

void vp9_idct_add_8x8_c(int16_t *input, uint8_t *dest, int stride, int eob) {
  DECLARE_ALIGNED_ARRAY(16, int16_t, output, 64);

  // 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.

  // 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.
  if (eob) {
    if (eob == 1) {
      // DC only DCT coefficient
      int16_t in = input[0];
      int16_t out;

      // Note: the idct1 will need to be modified accordingly whenever
      // vp9_short_idct8x8_c() is modified.
      vp9_short_idct1_8x8_c(&in, &out);
      input[0] = 0;

      vp9_add_constant_residual_8x8(out, dest, stride);
#if !CONFIG_SCATTERSCAN
    } else if (eob <= 10) {
      vp9_short_idct10_8x8(input, output, 16);

      input[0] = input[1] = input[2] = input[3] = 0;
      input[8] = input[9] = input[10] = 0;
      input[16] = input[17] = 0;
      input[24] = 0;

      vp9_add_residual_8x8(output, dest, stride);
#endif
    } else {
      // the idct halves ( >> 1) the pitch
      vp9_short_idct8x8(input, output, 8 << 1);
      vpx_memset(input, 0, 128);
      vp9_add_residual_8x8(output, dest, stride);
    }
  }
}

void vp9_iht_add_16x16_c(TX_TYPE tx_type, int16_t *input, uint8_t *dest,
                         int stride, int eob) {
  if (tx_type == DCT_DCT) {
    vp9_idct_add_16x16(input, dest, stride, eob);
  } else {
    DECLARE_ALIGNED_ARRAY(16, int16_t, output, 256);

    if (eob > 0) {
      vp9_short_iht16x16(input, output, 16, tx_type);
      vpx_memset(input, 0, 512);
      vp9_add_residual_16x16(output, dest, stride);
    }
  }
}

void vp9_idct_add_16x16_c(int16_t *input, uint8_t *dest, int stride, int eob) {
  DECLARE_ALIGNED_ARRAY(16, int16_t, output, 256);

  /* The calculation can be simplified if there are not many non-zero dct
   * coefficients. Use eobs to separate different cases. */
  if (eob) {
    if (eob == 1) {
      /* DC only DCT coefficient. */
      int16_t in = input[0];
      int16_t out;
      /* Note: the idct1 will need to be modified accordingly whenever
       * vp9_short_idct16x16() is modified. */
      vp9_short_idct1_16x16_c(&in, &out);
      input[0] = 0;

      vp9_add_constant_residual_16x16(out, dest, stride);
#if !CONFIG_SCATTERSCAN
    } else if (eob <= 10) {
      // the idct halves ( >> 1) the pitch
      vp9_short_idct10_16x16(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;

      vp9_add_residual_16x16(output, dest, stride);
#endif
    } else {
      // the idct halves ( >> 1) the pitch
      vp9_short_idct16x16(input, output, 16 << 1);
      vpx_memset(input, 0, 512);
      vp9_add_residual_16x16(output, dest, stride);
    }
  }
}

void vp9_idct_add_32x32_c(int16_t *input, uint8_t *dest, int stride, int eob) {
  DECLARE_ALIGNED_ARRAY(16, int16_t, output, 1024);

  if (eob) {
    if (eob == 1) {
      vp9_short_idct1_32x32(input, output);
      vp9_add_constant_residual_32x32(output[0], dest, stride);
      input[0] = 0;
#if !CONFIG_SCATTERSCAN
    } else if (eob <= 10) {
      // the idct halves ( >> 1) the pitch
      vp9_short_idct10_32x32(input, output, 64);

      input[0] = input[1] = input[2] = input[3] = 0;
      input[32] = input[33] = input[34] = 0;
      input[64] = input[65] = 0;
      input[96] = 0;

      vp9_add_residual_32x32(output, dest, stride);
#endif
    } else {
      vp9_short_idct32x32(input, output, 64);
      vpx_memset(input, 0, 2048);
      vp9_add_residual_32x32(output, dest, stride);
    }
  }
}