vp9_encodeintra.c 7.61 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
  vp9_intra4x4_predict(&x->e_mbd, b, b->bmi.as_mode.first, b->predictor);
56
  vp9_subtract_b(be, b, 16);
John Koleszar's avatar
John Koleszar committed
57

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

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

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

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

82
void vp9_encode_intra16x16mby(VP9_COMMON *const cm, MACROBLOCK *x) {
83
  MACROBLOCKD *xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
84
  BLOCK *b = &x->block[0];
85
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
86

87
  vp9_build_intra_predictors_mby(xd);
John Koleszar's avatar
John Koleszar committed
88

89
  vp9_subtract_mby(x->src_diff, *(b->base_src), xd->predictor, b->src_stride);
Paul Wilkins's avatar
Paul Wilkins committed
90

Dmitry Kovalev's avatar
Dmitry Kovalev committed
91
92
93
94
95
  switch (tx_size) {
    case TX_16X16:
      vp9_transform_mby_16x16(x);
      vp9_quantize_mby_16x16(x);
      if (x->optimize)
96
        vp9_optimize_mby_16x16(cm, x);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
97
98
99
100
101
102
      vp9_inverse_transform_mby_16x16(xd);
      break;
    case TX_8X8:
      vp9_transform_mby_8x8(x);
      vp9_quantize_mby_8x8(x);
      if (x->optimize)
103
        vp9_optimize_mby_8x8(cm, x);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
104
105
106
107
108
109
      vp9_inverse_transform_mby_8x8(xd);
      break;
    default:
      vp9_transform_mby_4x4(x);
      vp9_quantize_mby_4x4(x);
      if (x->optimize)
110
        vp9_optimize_mby_4x4(cm, x);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
111
112
      vp9_inverse_transform_mby_4x4(xd);
      break;
John Koleszar's avatar
John Koleszar committed
113
114
  }

115
  vp9_recon_mby(xd);
John Koleszar's avatar
John Koleszar committed
116
117
}

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

122
  vp9_build_intra_predictors_mbuv(xd);
John Koleszar's avatar
John Koleszar committed
123

124
  vp9_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer,
Jim Bankoski's avatar
Jim Bankoski committed
125
126
                    xd->predictor, x->src.uv_stride);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
127
128
129
130
131
  switch (tx_size) {
    case TX_4X4:
      vp9_transform_mbuv_4x4(x);
      vp9_quantize_mbuv_4x4(x);
      if (x->optimize)
132
        vp9_optimize_mbuv_4x4(cm, x);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
133
134
135
136
137
138
      vp9_inverse_transform_mbuv_4x4(xd);
      break;
    default:  // 16x16 or 8x8
      vp9_transform_mbuv_8x8(x);
      vp9_quantize_mbuv_8x8(x);
      if (x->optimize)
139
        vp9_optimize_mbuv_8x8(cm, x);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
140
141
142
      vp9_inverse_transform_mbuv_8x8(xd);
      break;
    }
John Koleszar's avatar
John Koleszar committed
143

144
  vp9_recon_intra_mbuv(xd);
John Koleszar's avatar
John Koleszar committed
145
}
Yaowu Xu's avatar
Yaowu Xu committed
146

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

155
  vp9_intra8x8_predict(xd, b, b->bmi.as_mode.first, b->predictor);
156
157
  // generate residual blocks
  vp9_subtract_4b_c(be, b, 16);
Yaowu Xu's avatar
Yaowu Xu committed
158

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

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

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

  // reconstruct submacroblock
  for (i = 0; i < 4; i++) {
    b = &xd->block[ib + iblock[i]];
209
    vp9_recon_b_c(b->predictor, b->diff, *(b->base_dst) + b->dst,
210
211
                  b->dst_stride);
  }
Yaowu Xu's avatar
Yaowu Xu committed
212
213
}

214
void vp9_encode_intra8x8mby(MACROBLOCK *x) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
215
  int i;
John Koleszar's avatar
John Koleszar committed
216

Dmitry Kovalev's avatar
Dmitry Kovalev committed
217
218
  for (i = 0; i < 4; i++)
    vp9_encode_intra8x8(x, vp9_i8x8_block[i]);
Yaowu Xu's avatar
Yaowu Xu committed
219
220
}

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

229
  assert(ib >= 16 && ib < 24);
230
  vp9_intra_uv4x4_predict(&x->e_mbd, b, mode, b->predictor);
Yaowu Xu's avatar
Yaowu Xu committed
231

232
  vp9_subtract_b(be, b, 8);
Yaowu Xu's avatar
Yaowu Xu committed
233

Yaowu Xu's avatar
Yaowu Xu committed
234
  x->fwd_txm4x4(be->src_diff, be->coeff, 16);
John Koleszar's avatar
John Koleszar committed
235
236
  x->quantize_b_4x4(x, ib, 16);
  vp9_inverse_transform_b_4x4(&x->e_mbd, xd->plane[plane].eobs[block],
237
                              dqcoeff, b->diff, 16);
Yaowu Xu's avatar
Yaowu Xu committed
238

239
240
  vp9_recon_uv_b_c(b->predictor, b->diff, *(b->base_dst) + b->dst,
                   b->dst_stride);
Yaowu Xu's avatar
Yaowu Xu committed
241
242
}

243
void vp9_encode_intra8x8mbuv(MACROBLOCK *x) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
244
  int i;
245

John Koleszar's avatar
John Koleszar committed
246
  for (i = 0; i < 4; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
247
248
249
250
251
    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
252
  }
Yaowu Xu's avatar
Yaowu Xu committed
253
}