vp9_encodeintra.c 6.25 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
15
#include "vp9/common/vp9_reconintra.h"
#include "vp9/common/vp9_reconintra4x4.h"
16
#include "vp9/encoder/vp9_encodemb.h"
17
#include "vp9/common/vp9_invtrans.h"
18
#include "vp9/encoder/vp9_encodeintra.h"
John Koleszar's avatar
John Koleszar committed
19

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

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

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

39
  intra_pred_var = vp9_get_mb_ss(x->src_diff);
Tero Rintaluoma's avatar
Tero Rintaluoma committed
40

John Koleszar's avatar
John Koleszar committed
41
  return intra_pred_var;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
42
}
43

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

49
50
51
52
#if CONFIG_NEWBINTRAMODES
  b->bmi.as_mode.context = vp9_find_bpred_context(b);
#endif

53
  vp9_intra4x4_predict(b, b->bmi.as_mode.first, b->predictor);
54
  vp9_subtract_b(be, b, 16);
John Koleszar's avatar
John Koleszar committed
55

56
  tx_type = get_tx_type_4x4(&x->e_mbd, b);
Deb Mukherjee's avatar
Deb Mukherjee committed
57
  if (tx_type != DCT_DCT) {
58
    vp9_fht(be->src_diff, 32, be->coeff, tx_type, 4);
59
    vp9_ht_quantize_b_4x4(be, b, tx_type);
Scott LaVarnway's avatar
Scott LaVarnway committed
60
    vp9_ihtllm(b->dqcoeff, b->diff, 32, tx_type, 4, b->eob);
61
  } else {
62
    x->vp9_short_fdct4x4(be->src_diff, be->coeff, 32);
63
    x->quantize_b_4x4(be, b) ;
64
    vp9_inverse_transform_b_4x4(&x->e_mbd, ib, 32);
65
  }
John Koleszar's avatar
John Koleszar committed
66

67
  vp9_recon_b(b->predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
68
69
}

70
void vp9_encode_intra4x4mby(MACROBLOCK *mb) {
John Koleszar's avatar
John Koleszar committed
71
  int i;
John Koleszar's avatar
John Koleszar committed
72

John Koleszar's avatar
John Koleszar committed
73
  for (i = 0; i < 16; i++)
74
    vp9_encode_intra4x4block(mb, i);
John Koleszar's avatar
John Koleszar committed
75
  return;
John Koleszar's avatar
John Koleszar committed
76
77
}

78
void vp9_encode_intra16x16mby(MACROBLOCK *x) {
79
  MACROBLOCKD *xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
80
  BLOCK *b = &x->block[0];
81
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
82

83
  vp9_build_intra_predictors_mby(xd);
John Koleszar's avatar
John Koleszar committed
84

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

87
  if (tx_size == TX_16X16) {
88
89
90
91
92
    vp9_transform_mby_16x16(x);
    vp9_quantize_mby_16x16(x);
    if (x->optimize)
      vp9_optimize_mby_16x16(x);
    vp9_inverse_transform_mby_16x16(xd);
93
  } else if (tx_size == TX_8X8) {
94
95
    vp9_transform_mby_8x8(x);
    vp9_quantize_mby_8x8(x);
96
    if (x->optimize)
97
98
      vp9_optimize_mby_8x8(x);
    vp9_inverse_transform_mby_8x8(xd);
99
  } else {
100
101
    vp9_transform_mby_4x4(x);
    vp9_quantize_mby_4x4(x);
102
    if (x->optimize)
103
104
      vp9_optimize_mby_4x4(x);
    vp9_inverse_transform_mby_4x4(xd);
John Koleszar's avatar
John Koleszar committed
105
106
  }

107
  vp9_recon_mby(xd);
John Koleszar's avatar
John Koleszar committed
108
109
}

110
void vp9_encode_intra16x16mbuv(MACROBLOCK *x) {
111
112
113
  MACROBLOCKD *xd = &x->e_mbd;
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;

114
  vp9_build_intra_predictors_mbuv(xd);
John Koleszar's avatar
John Koleszar committed
115

116
  vp9_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer,
Jim Bankoski's avatar
Jim Bankoski committed
117
118
                    xd->predictor, x->src.uv_stride);

119
  if (tx_size == TX_4X4) {
120
121
    vp9_transform_mbuv_4x4(x);
    vp9_quantize_mbuv_4x4(x);
122
    if (x->optimize)
123
124
      vp9_optimize_mbuv_4x4(x);
    vp9_inverse_transform_mbuv_4x4(xd);
125
  } else /* 16x16 or 8x8 */ {
126
127
    vp9_transform_mbuv_8x8(x);
    vp9_quantize_mbuv_8x8(x);
128
    if (x->optimize)
129
130
      vp9_optimize_mbuv_8x8(x);
    vp9_inverse_transform_mbuv_8x8(xd);
John Koleszar's avatar
John Koleszar committed
131
132
  }

133
  vp9_recon_intra_mbuv(xd);
John Koleszar's avatar
John Koleszar committed
134
}
Yaowu Xu's avatar
Yaowu Xu committed
135

136
void vp9_encode_intra8x8(MACROBLOCK *x, int ib) {
137
138
  MACROBLOCKD *xd = &x->e_mbd;
  BLOCKD *b = &xd->block[ib];
John Koleszar's avatar
John Koleszar committed
139
140
141
  BLOCK *be = &x->block[ib];
  const int iblock[4] = {0, 1, 4, 5};
  int i;
Deb Mukherjee's avatar
Deb Mukherjee committed
142
  TX_TYPE tx_type;
Yaowu Xu's avatar
Yaowu Xu committed
143

144
  vp9_intra8x8_predict(b, b->bmi.as_mode.first, b->predictor);
145
146
  // generate residual blocks
  vp9_subtract_4b_c(be, b, 16);
Yaowu Xu's avatar
Yaowu Xu committed
147

148
  if (xd->mode_info_context->mbmi.txfm_size == TX_8X8) {
149
150
    int idx = (ib & 0x02) ? (ib + 2) : ib;

151
    tx_type = get_tx_type_8x8(xd, &xd->block[ib]);
Deb Mukherjee's avatar
Deb Mukherjee committed
152
    if (tx_type != DCT_DCT) {
153
      vp9_fht(be->src_diff, 32, (x->block + idx)->coeff,
Deb Mukherjee's avatar
Deb Mukherjee committed
154
155
                tx_type, 8);
      x->quantize_b_8x8(x->block + idx, xd->block + idx);
Jim Bankoski's avatar
Jim Bankoski committed
156
      vp9_ihtllm(xd->block[idx].dqcoeff, xd->block[ib].diff, 32,
Scott LaVarnway's avatar
Scott LaVarnway committed
157
                   tx_type, 8, xd->block[idx].eob);
Deb Mukherjee's avatar
Deb Mukherjee committed
158
    } else {
159
      x->vp9_short_fdct8x8(be->src_diff, (x->block + idx)->coeff, 32);
Deb Mukherjee's avatar
Deb Mukherjee committed
160
      x->quantize_b_8x8(x->block + idx, xd->block + idx);
161
      vp9_short_idct8x8(xd->block[idx].dqcoeff, xd->block[ib].diff, 32);
Deb Mukherjee's avatar
Deb Mukherjee committed
162
    }
163
  } else {
164
165
    for (i = 0; i < 4; i++) {
      b = &xd->block[ib + iblock[i]];
166
      be = &x->block[ib + iblock[i]];
167
168
169
170
      tx_type = get_tx_type_4x4(xd, b);
      if (tx_type != DCT_DCT) {
        vp9_fht_c(be->src_diff, 32, be->coeff, tx_type, 4);
        vp9_ht_quantize_b_4x4(be, b, tx_type);
Scott LaVarnway's avatar
Scott LaVarnway committed
171
        vp9_ihtllm(b->dqcoeff, b->diff, 32, tx_type, 4, b->eob);
172
173
174
175
176
      } else {
        x->vp9_short_fdct4x4(be->src_diff, be->coeff, 32);
        x->quantize_b_4x4(be, b);
        vp9_inverse_transform_b_4x4(xd, ib + iblock[i], 32);
      }
177
178
    }
  }
179
180
181
182

  // reconstruct submacroblock
  for (i = 0; i < 4; i++) {
    b = &xd->block[ib + iblock[i]];
183
    vp9_recon_b_c(b->predictor, b->diff, *(b->base_dst) + b->dst,
184
185
                  b->dst_stride);
  }
Yaowu Xu's avatar
Yaowu Xu committed
186
187
}

188
void vp9_encode_intra8x8mby(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
189
190
191
  int i, ib;

  for (i = 0; i < 4; i++) {
192
    ib = vp9_i8x8_block[i];
193
    vp9_encode_intra8x8(x, ib);
John Koleszar's avatar
John Koleszar committed
194
  }
Yaowu Xu's avatar
Yaowu Xu committed
195
196
}

197
198
static void encode_intra_uv4x4(MACROBLOCK *x, int ib,
                               int mode) {
John Koleszar's avatar
John Koleszar committed
199
200
  BLOCKD *b = &x->e_mbd.block[ib];
  BLOCK *be = &x->block[ib];
Yaowu Xu's avatar
Yaowu Xu committed
201

202
  vp9_intra_uv4x4_predict(b, mode, b->predictor);
Yaowu Xu's avatar
Yaowu Xu committed
203

204
  vp9_subtract_b(be, b, 8);
Yaowu Xu's avatar
Yaowu Xu committed
205

206
  x->vp9_short_fdct4x4(be->src_diff, be->coeff, 16);
207
  x->quantize_b_4x4(be, b);
208
  vp9_inverse_transform_b_4x4(&x->e_mbd, ib, 16);
Yaowu Xu's avatar
Yaowu Xu committed
209

210
211
  vp9_recon_uv_b_c(b->predictor, b->diff, *(b->base_dst) + b->dst,
                   b->dst_stride);
Yaowu Xu's avatar
Yaowu Xu committed
212
213
}

214
void vp9_encode_intra8x8mbuv(MACROBLOCK *x) {
215
  int i, ib, mode;
John Koleszar's avatar
John Koleszar committed
216
  BLOCKD *b;
217

John Koleszar's avatar
John Koleszar committed
218
  for (i = 0; i < 4; i++) {
219
    ib = vp9_i8x8_block[i];
John Koleszar's avatar
John Koleszar committed
220
221
    b = &x->e_mbd.block[ib];
    mode = b->bmi.as_mode.first;
222

John Koleszar's avatar
John Koleszar committed
223
    /*u */
224
    encode_intra_uv4x4(x, i + 16, mode);
John Koleszar's avatar
John Koleszar committed
225
    /*v */
226
    encode_intra_uv4x4(x, i + 20, mode);
John Koleszar's avatar
John Koleszar committed
227
  }
Yaowu Xu's avatar
Yaowu Xu committed
228
}