vp9_encodeintra.c 8.44 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
#include "./vpx_config.h"
12
#include "vp9_rtcd.h"
13
#include "vp9/encoder/vp9_quantize.h"
14
#include "vp9/common/vp9_reconintra.h"
15
#include "vp9/encoder/vp9_encodemb.h"
16
#include "vp9/common/vp9_invtrans.h"
17
#include "vp9/encoder/vp9_encodeintra.h"
John Koleszar's avatar
John Koleszar committed
18

19 20
static void encode_intra4x4block(MACROBLOCK *x, int ib);

21
int vp9_encode_intra(VP9_COMP *cpi, MACROBLOCK *x, int use_16x16_pred) {
Paul Wilkins's avatar
Paul Wilkins committed
22
  MB_MODE_INFO * mbmi = &x->e_mbd.mode_info_context->mbmi;
John Koleszar's avatar
John Koleszar committed
23
  (void) cpi;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
24

John Koleszar's avatar
John Koleszar committed
25
  if (use_16x16_pred) {
Paul Wilkins's avatar
Paul Wilkins committed
26 27 28
    mbmi->mode = DC_PRED;
    mbmi->uv_mode = DC_PRED;
    mbmi->ref_frame = INTRA_FRAME;
John Koleszar's avatar
John Koleszar committed
29

30
    vp9_encode_intra16x16mby(&cpi->common, x);
John Koleszar's avatar
John Koleszar committed
31
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
32 33
    int i;

John Koleszar's avatar
John Koleszar committed
34 35
    for (i = 0; i < 16; i++) {
      x->e_mbd.block[i].bmi.as_mode.first = B_DC_PRED;
36
      encode_intra4x4block(x, i);
Tero Rintaluoma's avatar
Tero Rintaluoma committed
37
    }
John Koleszar's avatar
John Koleszar committed
38
  }
Tero Rintaluoma's avatar
Tero Rintaluoma committed
39

Dmitry Kovalev's avatar
Dmitry Kovalev committed
40
  return vp9_get_mb_ss(x->src_diff);
Tero Rintaluoma's avatar
Tero Rintaluoma committed
41
}
42

43
static void encode_intra4x4block(MACROBLOCK *x, int ib) {
John Koleszar's avatar
John Koleszar committed
44 45
  BLOCKD *b = &x->e_mbd.block[ib];
  BLOCK *be = &x->block[ib];
46
  MACROBLOCKD * const xd = &x->e_mbd;
Deb Mukherjee's avatar
Deb Mukherjee committed
47
  TX_TYPE tx_type;
48

49 50
  assert(ib < 16);

51
#if CONFIG_NEWBINTRAMODES
52
  b->bmi.as_mode.context = vp9_find_bpred_context(&x->e_mbd, b);
53 54
#endif

55 56
  vp9_intra4x4_predict(&x->e_mbd, b, b->bmi.as_mode.first,
                       *(b->base_dst) + b->dst, b->dst_stride);
57
  vp9_subtract_b(be, b, 16);
John Koleszar's avatar
John Koleszar committed
58

59
  tx_type = get_tx_type_4x4(&x->e_mbd, ib);
Deb Mukherjee's avatar
Deb Mukherjee committed
60
  if (tx_type != DCT_DCT) {
61
    vp9_short_fht4x4(be->src_diff, be->coeff, 16, tx_type);
62
    vp9_ht_quantize_b_4x4(x, ib, tx_type);
63 64
    vp9_short_iht4x4(BLOCK_OFFSET(xd->plane[0].dqcoeff, ib, 16),
                     b->diff, 16, tx_type);
65
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
66
    x->fwd_txm4x4(be->src_diff, be->coeff, 32);
John Koleszar's avatar
John Koleszar committed
67 68
    x->quantize_b_4x4(x, ib, 16);
    vp9_inverse_transform_b_4x4(&x->e_mbd, xd->plane[0].eobs[ib],
69 70
                                BLOCK_OFFSET(xd->plane[0].dqcoeff, ib, 16),
                                b->diff, 32);
71
  }
John Koleszar's avatar
John Koleszar committed
72

73 74
  vp9_recon_b(*(b->base_dst) + b->dst, b->diff,
              *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
75 76
}

77
void vp9_encode_intra4x4mby(MACROBLOCK *mb) {
John Koleszar's avatar
John Koleszar committed
78
  int i;
John Koleszar's avatar
John Koleszar committed
79

John Koleszar's avatar
John Koleszar committed
80
  for (i = 0; i < 16; i++)
81
    encode_intra4x4block(mb, i);
John Koleszar's avatar
John Koleszar committed
82 83
}

84
void vp9_encode_intra16x16mby(VP9_COMMON *const cm, MACROBLOCK *x) {
85 86
  MACROBLOCKD *xd = &x->e_mbd;
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
87

88 89 90 91 92
  vp9_build_intra_predictors_sby_s(xd, BLOCK_SIZE_MB16X16);
  vp9_subtract_sby_s_c(x->src_diff,
                       x->src.y_buffer, x->src.y_stride,
                       xd->dst.y_buffer, xd->dst.y_stride,
                       BLOCK_SIZE_MB16X16);
Paul Wilkins's avatar
Paul Wilkins committed
93

Dmitry Kovalev's avatar
Dmitry Kovalev committed
94 95
  switch (tx_size) {
    case TX_16X16:
96 97
      vp9_transform_sby_16x16(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sby_16x16(x, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
98
      if (x->optimize)
99 100
        vp9_optimize_sby_16x16(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sby_16x16(xd, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
101 102
      break;
    case TX_8X8:
103 104
      vp9_transform_sby_8x8(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sby_8x8(x, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
105
      if (x->optimize)
106 107
        vp9_optimize_sby_8x8(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sby_8x8(xd, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
108 109
      break;
    default:
110 111
      vp9_transform_sby_4x4(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sby_4x4(x, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
112
      if (x->optimize)
113 114
        vp9_optimize_sby_4x4(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sby_4x4(xd, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
115
      break;
John Koleszar's avatar
John Koleszar committed
116 117
  }

118
  vp9_recon_mby(xd);
John Koleszar's avatar
John Koleszar committed
119 120
}

121
void vp9_encode_intra16x16mbuv(VP9_COMMON *const cm, MACROBLOCK *x) {
122 123 124
  MACROBLOCKD *xd = &x->e_mbd;
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;

125 126 127 128 129
  vp9_build_intra_predictors_sbuv_s(xd, BLOCK_SIZE_MB16X16);
  vp9_subtract_sbuv_s_c(x->src_diff,
                        x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
                        xd->dst.u_buffer, xd->dst.v_buffer, xd->dst.uv_stride,
                        BLOCK_SIZE_MB16X16);
Jim Bankoski's avatar
Jim Bankoski committed
130

Dmitry Kovalev's avatar
Dmitry Kovalev committed
131 132
  switch (tx_size) {
    case TX_4X4:
133 134
      vp9_transform_sbuv_4x4(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sbuv_4x4(x, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
135
      if (x->optimize)
136 137
        vp9_optimize_sbuv_4x4(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sbuv_4x4(xd, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
138 139
      break;
    default:  // 16x16 or 8x8
140 141
      vp9_transform_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
142
      if (x->optimize)
143 144
        vp9_optimize_sbuv_8x8(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sbuv_8x8(xd, BLOCK_SIZE_MB16X16);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
145 146
      break;
    }
John Koleszar's avatar
John Koleszar committed
147

148
  vp9_recon_intra_mbuv(xd);
John Koleszar's avatar
John Koleszar committed
149
}
Yaowu Xu's avatar
Yaowu Xu committed
150

151
void vp9_encode_intra8x8(MACROBLOCK *x, int ib) {
152 153
  MACROBLOCKD *xd = &x->e_mbd;
  BLOCKD *b = &xd->block[ib];
John Koleszar's avatar
John Koleszar committed
154 155 156
  BLOCK *be = &x->block[ib];
  const int iblock[4] = {0, 1, 4, 5};
  int i;
Deb Mukherjee's avatar
Deb Mukherjee committed
157
  TX_TYPE tx_type;
Yaowu Xu's avatar
Yaowu Xu committed
158

159 160
  vp9_intra8x8_predict(xd, b, b->bmi.as_mode.first,
                       *(b->base_dst) + b->dst, b->dst_stride);
161 162
  // generate residual blocks
  vp9_subtract_4b_c(be, b, 16);
Yaowu Xu's avatar
Yaowu Xu committed
163

164
  if (xd->mode_info_context->mbmi.txfm_size == TX_8X8) {
165
    int idx = (ib & 0x02) ? (ib + 2) : ib;
166
    int16_t * const dqcoeff = BLOCK_OFFSET(xd->plane[0].dqcoeff, idx, 16);
167

168
    assert(idx < 16);
169
    tx_type = get_tx_type_8x8(xd, ib);
Deb Mukherjee's avatar
Deb Mukherjee committed
170
    if (tx_type != DCT_DCT) {
171
      vp9_short_fht8x8(be->src_diff, (x->block + idx)->coeff, 16, tx_type);
John Koleszar's avatar
John Koleszar committed
172
      x->quantize_b_8x8(x, idx, tx_type, 16);
173
      vp9_short_iht8x8(dqcoeff, xd->block[ib].diff,
174
                            16, tx_type);
Deb Mukherjee's avatar
Deb Mukherjee committed
175
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
176
      x->fwd_txm8x8(be->src_diff, (x->block + idx)->coeff, 32);
John Koleszar's avatar
John Koleszar committed
177
      x->quantize_b_8x8(x, idx, DCT_DCT, 16);
178
      vp9_short_idct8x8(dqcoeff, xd->block[ib].diff, 32);
Deb Mukherjee's avatar
Deb Mukherjee committed
179
    }
180
  } else {
181
    for (i = 0; i < 4; i++) {
182 183 184 185
      int idx = ib + iblock[i];
      int16_t * const dqcoeff = BLOCK_OFFSET(xd->plane[0].dqcoeff, idx, 16);

      assert(idx < 16);
186
      b = &xd->block[ib + iblock[i]];
187
      be = &x->block[ib + iblock[i]];
188
      tx_type = get_tx_type_4x4(xd, ib + iblock[i]);
189
      if (tx_type != DCT_DCT) {
190
        vp9_short_fht4x4(be->src_diff, be->coeff, 16, tx_type);
191
        vp9_ht_quantize_b_4x4(x, ib + iblock[i], tx_type);
192
        vp9_short_iht4x4(dqcoeff, b->diff, 16, tx_type);
193 194
      } else if (!(i & 1) &&
                 get_tx_type_4x4(xd, ib + iblock[i] + 1) == DCT_DCT) {
Yaowu Xu's avatar
Yaowu Xu committed
195
        x->fwd_txm8x4(be->src_diff, be->coeff, 32);
John Koleszar's avatar
John Koleszar committed
196 197
        x->quantize_b_4x4_pair(x, ib + iblock[i], ib + iblock[i] + 1, 16);
        vp9_inverse_transform_b_4x4(xd, xd->plane[0].eobs[ib + iblock[i]],
198
                                    dqcoeff, b->diff, 32);
John Koleszar's avatar
John Koleszar committed
199
        vp9_inverse_transform_b_4x4(xd, xd->plane[0].eobs[ib + iblock[i] + 1],
200
                                    dqcoeff + 16, (b + 1)->diff, 32);
201
        i++;
202
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
203
        x->fwd_txm4x4(be->src_diff, be->coeff, 32);
John Koleszar's avatar
John Koleszar committed
204 205
        x->quantize_b_4x4(x, ib + iblock[i], 16);
        vp9_inverse_transform_b_4x4(xd, xd->plane[0].eobs[ib + iblock[i]],
206
                                    dqcoeff, b->diff, 32);
207
      }
208 209
    }
  }
210 211 212 213

  // reconstruct submacroblock
  for (i = 0; i < 4; i++) {
    b = &xd->block[ib + iblock[i]];
214
    vp9_recon_b_c(*(b->base_dst) + b->dst, b->diff, *(b->base_dst) + b->dst,
215 216
                  b->dst_stride);
  }
Yaowu Xu's avatar
Yaowu Xu committed
217 218
}

219
void vp9_encode_intra8x8mby(MACROBLOCK *x) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
220
  int i;
John Koleszar's avatar
John Koleszar committed
221

Dmitry Kovalev's avatar
Dmitry Kovalev committed
222 223
  for (i = 0; i < 4; i++)
    vp9_encode_intra8x8(x, vp9_i8x8_block[i]);
Yaowu Xu's avatar
Yaowu Xu committed
224 225
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
226
static void encode_intra_uv4x4(MACROBLOCK *x, int ib, int mode) {
227
  MACROBLOCKD * const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
228 229
  BLOCKD *b = &x->e_mbd.block[ib];
  BLOCK *be = &x->block[ib];
230
  int16_t * const dqcoeff = MB_SUBBLOCK_FIELD(xd, dqcoeff, ib);
John Koleszar's avatar
John Koleszar committed
231 232
  const int plane = ib < 20 ? 1 : 2;
  const int block = ib < 20 ? ib - 16 : ib - 20;
Yaowu Xu's avatar
Yaowu Xu committed
233

234
  assert(ib >= 16 && ib < 24);
235 236
  vp9_intra_uv4x4_predict(&x->e_mbd, b, mode,
                          *(b->base_dst) + b->dst, b->dst_stride);
Yaowu Xu's avatar
Yaowu Xu committed
237

238
  vp9_subtract_b(be, b, 8);
Yaowu Xu's avatar
Yaowu Xu committed
239

Yaowu Xu's avatar
Yaowu Xu committed
240
  x->fwd_txm4x4(be->src_diff, be->coeff, 16);
John Koleszar's avatar
John Koleszar committed
241 242
  x->quantize_b_4x4(x, ib, 16);
  vp9_inverse_transform_b_4x4(&x->e_mbd, xd->plane[plane].eobs[block],
243
                              dqcoeff, b->diff, 16);
Yaowu Xu's avatar
Yaowu Xu committed
244

245
  vp9_recon_uv_b_c(*(b->base_dst) + b->dst, b->diff, *(b->base_dst) + b->dst,
246
                   b->dst_stride);
Yaowu Xu's avatar
Yaowu Xu committed
247 248
}

249
void vp9_encode_intra8x8mbuv(MACROBLOCK *x) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
250
  int i;
251

John Koleszar's avatar
John Koleszar committed
252
  for (i = 0; i < 4; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
253 254 255 256 257
    BLOCKD *b = &x->e_mbd.block[vp9_i8x8_block[i]];
    int mode = b->bmi.as_mode.first;

    encode_intra_uv4x4(x, i + 16, mode);  // u
    encode_intra_uv4x4(x, i + 20, mode);  // v
John Koleszar's avatar
John Koleszar committed
258
  }
Yaowu Xu's avatar
Yaowu Xu committed
259
}