encodeintra.c 8.81 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_ports/config.h"
John Koleszar's avatar
John Koleszar committed
12
#include "vp8/common/idct.h"
John Koleszar's avatar
John Koleszar committed
13
#include "quantize.h"
John Koleszar's avatar
John Koleszar committed
14 15
#include "vp8/common/reconintra.h"
#include "vp8/common/reconintra4x4.h"
John Koleszar's avatar
John Koleszar committed
16
#include "encodemb.h"
John Koleszar's avatar
John Koleszar committed
17
#include "vp8/common/invtrans.h"
John Koleszar's avatar
John Koleszar committed
18
#include "dct.h"
John Koleszar's avatar
John Koleszar committed
19
#include "vp8/common/g_common.h"
John Koleszar's avatar
John Koleszar committed
20
#include "encodeintra.h"
21
#include "vpx_rtcd.h"
John Koleszar's avatar
John Koleszar committed
22 23 24 25 26 27

#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x) (x)
#else
#define IF_RTCD(x) NULL
#endif
28

Jingning Han's avatar
Jingning Han committed
29
int vp8_encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_16x16_pred) {
John Koleszar's avatar
John Koleszar committed
30 31
  int i;
  int intra_pred_var = 0;
Paul Wilkins's avatar
Paul Wilkins committed
32
  MB_MODE_INFO * mbmi = &x->e_mbd.mode_info_context->mbmi;
John Koleszar's avatar
John Koleszar committed
33
  (void) cpi;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
34

John Koleszar's avatar
John Koleszar committed
35
  if (use_16x16_pred) {
Paul Wilkins's avatar
Paul Wilkins committed
36
    mbmi->mode = DC_PRED;
37
#if CONFIG_COMP_INTRA_PRED
Paul Wilkins's avatar
Paul Wilkins committed
38
    mbmi->second_mode = (MB_PREDICTION_MODE)(DC_PRED - 1);
39
#endif
Paul Wilkins's avatar
Paul Wilkins committed
40 41
    mbmi->uv_mode = DC_PRED;
    mbmi->ref_frame = INTRA_FRAME;
John Koleszar's avatar
John Koleszar committed
42 43 44 45 46 47

    vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
  } else {
    for (i = 0; i < 16; i++) {
      x->e_mbd.block[i].bmi.as_mode.first = B_DC_PRED;
      vp8_encode_intra4x4block(IF_RTCD(&cpi->rtcd), x, i);
Tero Rintaluoma's avatar
Tero Rintaluoma committed
48
    }
John Koleszar's avatar
John Koleszar committed
49
  }
Tero Rintaluoma's avatar
Tero Rintaluoma committed
50

John Koleszar's avatar
John Koleszar committed
51
  intra_pred_var = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff);
Tero Rintaluoma's avatar
Tero Rintaluoma committed
52

John Koleszar's avatar
John Koleszar committed
53
  return intra_pred_var;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
54
}
55 56

void vp8_encode_intra4x4block(const VP8_ENCODER_RTCD *rtcd,
John Koleszar's avatar
John Koleszar committed
57 58 59
                              MACROBLOCK *x, int ib) {
  BLOCKD *b = &x->e_mbd.block[ib];
  BLOCK *be = &x->block[ib];
Deb Mukherjee's avatar
Deb Mukherjee committed
60 61 62
#if CONFIG_HYBRIDTRANSFORM
  TX_TYPE tx_type;
#endif
63

64
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
65
  if (b->bmi.as_mode.second == (B_PREDICTION_MODE)(B_DC_PRED - 1)) {
66
#endif
67
    vp8_intra4x4_predict(b, b->bmi.as_mode.first, b->predictor);
68
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
69
  } else {
70 71
    vp8_comp_intra4x4_predict(b, b->bmi.as_mode.first, b->bmi.as_mode.second,
                              b->predictor);
John Koleszar's avatar
John Koleszar committed
72
  }
73
#endif
John Koleszar's avatar
John Koleszar committed
74

John Koleszar's avatar
John Koleszar committed
75
  ENCODEMB_INVOKE(&rtcd->encodemb, subb)(be, b, 16);
John Koleszar's avatar
John Koleszar committed
76

Jingning Han's avatar
Jingning Han committed
77
#if CONFIG_HYBRIDTRANSFORM
Deb Mukherjee's avatar
Deb Mukherjee committed
78 79 80 81 82
  tx_type = get_tx_type(&x->e_mbd, b);
  if (tx_type != DCT_DCT) {
    vp8_fht_c(be->src_diff, be->coeff, 32, tx_type, 4);
    vp8_ht_quantize_b_4x4(be, b, tx_type);
    vp8_ihtllm_c(b->dqcoeff, b->diff, 32, tx_type, 4);
83 84 85
  } else
#endif
  {
86
    x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32) ;
87 88
    x->quantize_b_4x4(be, b) ;
    vp8_inverse_transform_b_4x4(IF_RTCD(&rtcd->common->idct), b, 32) ;
89
  }
John Koleszar's avatar
John Koleszar committed
90

91
  vp8_recon_b(b->predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
92 93
}

John Koleszar's avatar
John Koleszar committed
94 95
void vp8_encode_intra4x4mby(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *mb) {
  int i;
John Koleszar's avatar
John Koleszar committed
96

John Koleszar's avatar
John Koleszar committed
97 98 99
  for (i = 0; i < 16; i++)
    vp8_encode_intra4x4block(rtcd, mb, i);
  return;
John Koleszar's avatar
John Koleszar committed
100 101
}

John Koleszar's avatar
John Koleszar committed
102
void vp8_encode_intra16x16mby(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
103
  MACROBLOCKD *xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
104
  BLOCK *b = &x->block[0];
105
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
106
#if CONFIG_HYBRIDTRANSFORM16X16
Deb Mukherjee's avatar
Deb Mukherjee committed
107
  TX_TYPE tx_type;
108
#endif
Paul Wilkins's avatar
Paul Wilkins committed
109

110
#if CONFIG_COMP_INTRA_PRED
111
  if (xd->mode_info_context->mbmi.second_mode == (MB_PREDICTION_MODE)(DC_PRED - 1))
112
#endif
113
    vp8_build_intra_predictors_mby(xd);
114
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
115
  else
116
    vp8_build_comp_intra_predictors_mby(xd);
117
#endif
John Koleszar's avatar
John Koleszar committed
118

119 120
  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, *(b->base_src),
                                           xd->predictor, b->src_stride);
Paul Wilkins's avatar
Paul Wilkins committed
121

122
  if (tx_size == TX_16X16) {
123
#if CONFIG_HYBRIDTRANSFORM16X16
Deb Mukherjee's avatar
Deb Mukherjee committed
124 125 126 127
    BLOCKD  *bd = &xd->block[0];
    tx_type = get_tx_type(xd, bd);
    if (tx_type != DCT_DCT) {
      vp8_fht_c(b->src_diff, b->coeff, 32, tx_type, 16);
128 129 130
      vp8_quantize_mby_16x16(x);
      if (x->optimize)
        vp8_optimize_mby_16x16(x, rtcd);
Deb Mukherjee's avatar
Deb Mukherjee committed
131
      vp8_ihtllm_c(bd->dqcoeff, bd->diff, 32, tx_type, 16);
132 133
    } else
#endif
134 135 136 137 138 139 140 141
    {
      vp8_transform_mby_16x16(x);
      vp8_quantize_mby_16x16(x);
      if (x->optimize)
        vp8_optimize_mby_16x16(x, rtcd);
      vp8_inverse_transform_mby_16x16(IF_RTCD(&rtcd->common->idct), xd);
    }
  } else if (tx_size == TX_8X8) {
142
    vp8_transform_mby_8x8(x);
John Koleszar's avatar
John Koleszar committed
143
    vp8_quantize_mby_8x8(x);
144
    if (x->optimize)
John Koleszar's avatar
John Koleszar committed
145
      vp8_optimize_mby_8x8(x, rtcd);
146 147 148 149 150
    vp8_inverse_transform_mby_8x8(IF_RTCD(&rtcd->common->idct), xd);
  } else {
    vp8_transform_mby_4x4(x);
    vp8_quantize_mby_4x4(x);
    if (x->optimize)
151
      vp8_optimize_mby_4x4(x, rtcd);
152
    vp8_inverse_transform_mby_4x4(IF_RTCD(&rtcd->common->idct), xd);
John Koleszar's avatar
John Koleszar committed
153 154
  }

155
  vp8_recon_mby(xd);
John Koleszar's avatar
John Koleszar committed
156 157
}

John Koleszar's avatar
John Koleszar committed
158
void vp8_encode_intra16x16mbuv(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
159 160 161
  MACROBLOCKD *xd = &x->e_mbd;
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;

162
#if CONFIG_COMP_INTRA_PRED
163
  if (xd->mode_info_context->mbmi.second_uv_mode == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
Paul Wilkins's avatar
Paul Wilkins committed
164
#endif
165
    vp8_build_intra_predictors_mbuv(xd);
166
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
167
  } else {
168
    vp8_build_comp_intra_predictors_mbuv(xd);
John Koleszar's avatar
John Koleszar committed
169
  }
170
#endif
John Koleszar's avatar
John Koleszar committed
171

172 173 174 175
  ENCODEMB_INVOKE(&rtcd->encodemb, submbuv)(x->src_diff,
                                            x->src.u_buffer, x->src.v_buffer,
                                            xd->predictor, x->src.uv_stride);
  if (tx_size == TX_4X4) {
176 177
    vp8_transform_mbuv_4x4(x);
    vp8_quantize_mbuv_4x4(x);
178
    if (x->optimize)
179
      vp8_optimize_mbuv_4x4(x, rtcd);
180 181 182 183 184 185 186
    vp8_inverse_transform_mbuv_4x4(IF_RTCD(&rtcd->common->idct), xd);
  } else /* 16x16 or 8x8 */ {
    vp8_transform_mbuv_8x8(x);
    vp8_quantize_mbuv_8x8(x);
    if (x->optimize)
      vp8_optimize_mbuv_8x8(x, rtcd);
    vp8_inverse_transform_mbuv_8x8(IF_RTCD(&rtcd->common->idct), xd);
John Koleszar's avatar
John Koleszar committed
187 188
  }

189
  vp8_recon_intra_mbuv(xd);
John Koleszar's avatar
John Koleszar committed
190
}
Yaowu Xu's avatar
Yaowu Xu committed
191 192

void vp8_encode_intra8x8(const VP8_ENCODER_RTCD *rtcd,
John Koleszar's avatar
John Koleszar committed
193
                         MACROBLOCK *x, int ib) {
194 195
  MACROBLOCKD *xd = &x->e_mbd;
  BLOCKD *b = &xd->block[ib];
John Koleszar's avatar
John Koleszar committed
196 197 198
  BLOCK *be = &x->block[ib];
  const int iblock[4] = {0, 1, 4, 5};
  int i;
Deb Mukherjee's avatar
Deb Mukherjee committed
199 200 201
#if CONFIG_HYBRIDTRANSFORM8X8
  TX_TYPE tx_type;
#endif
Yaowu Xu's avatar
Yaowu Xu committed
202

203
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
204
  if (b->bmi.as_mode.second == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
205
#endif
206
    vp8_intra8x8_predict(b, b->bmi.as_mode.first, b->predictor);
207
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
208
  } else {
209 210
    vp8_comp_intra8x8_predict(b, b->bmi.as_mode.first, b->bmi.as_mode.second,
                              b->predictor);
John Koleszar's avatar
John Koleszar committed
211
  }
212
#endif
Yaowu Xu's avatar
Yaowu Xu committed
213

214
  if (xd->mode_info_context->mbmi.txfm_size == TX_8X8) {
215 216 217 218
    int idx = (ib & 0x02) ? (ib + 2) : ib;

    // generate residual blocks
    vp8_subtract_4b_c(be, b, 16);
219

220
#if CONFIG_HYBRIDTRANSFORM8X8
Deb Mukherjee's avatar
Deb Mukherjee committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234
    tx_type = get_tx_type(xd, &xd->block[idx]);
    if (tx_type != DCT_DCT) {
      vp8_fht_c(be->src_diff, (x->block + idx)->coeff, 32,
                tx_type, 8);
      x->quantize_b_8x8(x->block + idx, xd->block + idx);
      vp8_ihtllm_c(xd->block[idx].dqcoeff, xd->block[ib].diff, 32,
                   tx_type, 8);
    } else {
#endif
      x->vp8_short_fdct8x8(be->src_diff, (x->block + idx)->coeff, 32);
      x->quantize_b_8x8(x->block + idx, xd->block + idx);
      vp8_idct_idct8(xd->block[idx].dqcoeff, xd->block[ib].diff, 32);
#if CONFIG_HYBRIDTRANSFORM8X8
    }
235
#endif
236
  } else {
237 238
    for (i = 0; i < 4; i++) {
      b = &xd->block[ib + iblock[i]];
239 240 241
      be = &x->block[ib + iblock[i]];
      ENCODEMB_INVOKE(&rtcd->encodemb, subb)(be, b, 16);
      x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
242 243
      x->quantize_b_4x4(be, b);
      vp8_inverse_transform_b_4x4(IF_RTCD(&rtcd->common->idct), b, 32);
244 245
    }
  }
246 247 248 249 250 251 252

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

John Koleszar's avatar
John Koleszar committed
255 256 257 258 259 260 261
void vp8_encode_intra8x8mby(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  int i, ib;

  for (i = 0; i < 4; i++) {
    ib = vp8_i8x8_block[i];
    vp8_encode_intra8x8(rtcd, x, ib);
  }
Yaowu Xu's avatar
Yaowu Xu committed
262 263 264
}

void vp8_encode_intra_uv4x4(const VP8_ENCODER_RTCD *rtcd,
John Koleszar's avatar
John Koleszar committed
265 266 267 268
                            MACROBLOCK *x, int ib,
                            int mode, int second) {
  BLOCKD *b = &x->e_mbd.block[ib];
  BLOCK *be = &x->block[ib];
Yaowu Xu's avatar
Yaowu Xu committed
269

270
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
271
  if (second == -1) {
272
#endif
273
    vp8_intra_uv4x4_predict(b, mode, b->predictor);
274
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
275
  } else {
276
    vp8_comp_intra_uv4x4_predict(b, mode, second, b->predictor);
John Koleszar's avatar
John Koleszar committed
277
  }
278
#endif
Yaowu Xu's avatar
Yaowu Xu committed
279

John Koleszar's avatar
John Koleszar committed
280
  ENCODEMB_INVOKE(&rtcd->encodemb, subb)(be, b, 8);
Yaowu Xu's avatar
Yaowu Xu committed
281

John Koleszar's avatar
John Koleszar committed
282
  x->vp8_short_fdct4x4(be->src_diff, be->coeff, 16);
283 284
  x->quantize_b_4x4(be, b);
  vp8_inverse_transform_b_4x4(IF_RTCD(&rtcd->common->idct), b, 16);
Yaowu Xu's avatar
Yaowu Xu committed
285

286
  vp8_recon_uv_b_c(b->predictor,b->diff, *(b->base_dst) + b->dst, b->dst_stride);
Yaowu Xu's avatar
Yaowu Xu committed
287 288
}

John Koleszar's avatar
John Koleszar committed
289 290 291
void vp8_encode_intra8x8mbuv(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  int i, ib, mode, second;
  BLOCKD *b;
292

John Koleszar's avatar
John Koleszar committed
293 294 295 296
  for (i = 0; i < 4; i++) {
    ib = vp8_i8x8_block[i];
    b = &x->e_mbd.block[ib];
    mode = b->bmi.as_mode.first;
297
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
298
    second = b->bmi.as_mode.second;
299
#else
John Koleszar's avatar
John Koleszar committed
300
    second = -1;
301
#endif
John Koleszar's avatar
John Koleszar committed
302 303 304 305 306
    /*u */
    vp8_encode_intra_uv4x4(rtcd, x, i + 16, mode, second);
    /*v */
    vp8_encode_intra_uv4x4(rtcd, x, i + 20, mode, second);
  }
Yaowu Xu's avatar
Yaowu Xu committed
307
}