encodeintra.c 10.3 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 12
 */


#include "vpx_ports/config.h"
John Koleszar's avatar
John Koleszar committed
13
#include "vp8/common/idct.h"
John Koleszar's avatar
John Koleszar committed
14
#include "quantize.h"
John Koleszar's avatar
John Koleszar committed
15 16
#include "vp8/common/reconintra.h"
#include "vp8/common/reconintra4x4.h"
John Koleszar's avatar
John Koleszar committed
17
#include "encodemb.h"
John Koleszar's avatar
John Koleszar committed
18
#include "vp8/common/invtrans.h"
19
#include "vp8/common/recon.h"
John Koleszar's avatar
John Koleszar committed
20
#include "dct.h"
John Koleszar's avatar
John Koleszar committed
21
#include "vp8/common/g_common.h"
John Koleszar's avatar
John Koleszar committed
22 23 24 25 26 27 28 29
#include "encodeintra.h"


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

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

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

    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
50
    }
John Koleszar's avatar
John Koleszar committed
51
  }
Tero Rintaluoma's avatar
Tero Rintaluoma committed
52

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

John Koleszar's avatar
John Koleszar committed
55
  return intra_pred_var;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
56
}
57 58

void vp8_encode_intra4x4block(const VP8_ENCODER_RTCD *rtcd,
John Koleszar's avatar
John Koleszar committed
59 60 61
                              MACROBLOCK *x, int ib) {
  BLOCKD *b = &x->e_mbd.block[ib];
  BLOCK *be = &x->block[ib];
62

Jingning Han's avatar
Jingning Han committed
63 64 65 66 67 68
#if CONFIG_HYBRIDTRANSFORM
    int QIndex = x->q_index;
    int active_ht = (QIndex < ACTIVE_HT);
#endif


69
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
70
  if (b->bmi.as_mode.second == (B_PREDICTION_MODE)(B_DC_PRED - 1)) {
71
#endif
72 73
    RECON_INVOKE(&rtcd->common->recon, intra4x4_predict)
    (b, b->bmi.as_mode.first, b->predictor);
74
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
75
  } else {
76 77
    RECON_INVOKE(&rtcd->common->recon, comp_intra4x4_predict)
    (b, b->bmi.as_mode.first, b->bmi.as_mode.second, b->predictor);
John Koleszar's avatar
John Koleszar committed
78
  }
79
#endif
John Koleszar's avatar
John Koleszar committed
80

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

Jingning Han's avatar
Jingning Han committed
83
#if CONFIG_HYBRIDTRANSFORM
84 85 86 87
  if (active_ht) {
    b->bmi.as_mode.test = b->bmi.as_mode.first;
    txfm_map(b, b->bmi.as_mode.first);
    vp8_fht_c(be->src_diff, be->coeff, 32, b->bmi.as_mode.tx_type, 4);
88
    vp8_ht_quantize_b_4x4(be, b);
89
    vp8_ihtllm_c(b->dqcoeff, b->diff, 32, b->bmi.as_mode.tx_type, 4);
90 91
  } else {
    x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32) ;
92 93
    x->quantize_b_4x4(be, b) ;
    vp8_inverse_transform_b_4x4(IF_RTCD(&rtcd->common->idct), b, 32) ;
94
  }
Jingning Han's avatar
Jingning Han committed
95
#else
96
  x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
97 98
  x->quantize_b_4x4(be, b);
  vp8_inverse_transform_b_4x4(IF_RTCD(&rtcd->common->idct), b, 32);
Jingning Han's avatar
Jingning Han committed
99
#endif
John Koleszar's avatar
John Koleszar committed
100

101
  RECON_INVOKE(&rtcd->common->recon, recon)(b->predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
102 103
}

John Koleszar's avatar
John Koleszar committed
104 105
void vp8_encode_intra4x4mby(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *mb) {
  int i;
John Koleszar's avatar
John Koleszar committed
106

Adrian Grange's avatar
Adrian Grange committed
107
#if 0
Paul Wilkins's avatar
Paul Wilkins committed
108
  MACROBLOCKD *xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
109
  // Intra modes requiring top-right MB reconstructed data have been disabled
Paul Wilkins's avatar
Paul Wilkins committed
110
  vp8_intra_prediction_down_copy(xd);
Adrian Grange's avatar
Adrian Grange committed
111
#endif
John Koleszar's avatar
John Koleszar committed
112

John Koleszar's avatar
John Koleszar committed
113 114 115
  for (i = 0; i < 16; i++)
    vp8_encode_intra4x4block(rtcd, mb, i);
  return;
John Koleszar's avatar
John Koleszar committed
116 117
}

John Koleszar's avatar
John Koleszar committed
118 119
void vp8_encode_intra16x16mby(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  BLOCK *b = &x->block[0];
John Koleszar's avatar
John Koleszar committed
120

John Koleszar's avatar
John Koleszar committed
121
  int tx_type = x->e_mbd.mode_info_context->mbmi.txfm_size;
122 123 124
#if CONFIG_HYBRIDTRANSFORM16X16
  TX_TYPE txfm_type = x->e_mbd.mode_info_context->bmi[0].as_mode.tx_type;
#endif
Paul Wilkins's avatar
Paul Wilkins committed
125

126
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
127
  if (x->e_mbd.mode_info_context->mbmi.second_mode == (MB_PREDICTION_MODE)(DC_PRED - 1))
128
#endif
129
    RECON_INVOKE(&rtcd->common->recon, build_intra_predictors_mby)(&x->e_mbd);
130
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
131
  else
132
    RECON_INVOKE(&rtcd->common->recon, build_comp_intra_predictors_mby)(&x->e_mbd);
133
#endif
John Koleszar's avatar
John Koleszar committed
134

John Koleszar's avatar
John Koleszar committed
135
  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, *(b->base_src), x->e_mbd.predictor, b->src_stride);
Paul Wilkins's avatar
Paul Wilkins committed
136

Daniel Kang's avatar
Daniel Kang committed
137
  if (tx_type == TX_16X16)
138 139 140 141 142 143 144 145 146 147 148 149
#if CONFIG_HYBRIDTRANSFORM16X16
  {
    if ((x->e_mbd.mode_info_context->mbmi.mode < I8X8_PRED) &&
        (x->q_index < ACTIVE_HT16)) {
      BLOCKD  *bd = &x->e_mbd.block[0];
      txfm_map(bd, pred_mode_conv(x->e_mbd.mode_info_context->mbmi.mode));
      txfm_type = bd->bmi.as_mode.tx_type;
      vp8_fht_c(b->src_diff, b->coeff, 32, txfm_type, 16);
    } else
      vp8_transform_intra_mby_16x16(x);
  }
#else
Daniel Kang's avatar
Daniel Kang committed
150
    vp8_transform_intra_mby_16x16(x);
151
#endif
152
  else if (tx_type == TX_8X8)
John Koleszar's avatar
John Koleszar committed
153 154
    vp8_transform_intra_mby_8x8(x);
  else
155
    vp8_transform_intra_mby_4x4(x);
John Koleszar's avatar
John Koleszar committed
156

Daniel Kang's avatar
Daniel Kang committed
157 158
  if (tx_type == TX_16X16)
    vp8_quantize_mby_16x16(x);
159
  else if (tx_type == TX_8X8)
John Koleszar's avatar
John Koleszar committed
160 161
    vp8_quantize_mby_8x8(x);
  else
162
    vp8_quantize_mby_4x4(x);
John Koleszar's avatar
John Koleszar committed
163

John Koleszar's avatar
John Koleszar committed
164
  if (x->optimize) {
Daniel Kang's avatar
Daniel Kang committed
165 166
    if (tx_type == TX_16X16)
      vp8_optimize_mby_16x16(x, rtcd);
167
    else if (tx_type == TX_8X8)
John Koleszar's avatar
John Koleszar committed
168
      vp8_optimize_mby_8x8(x, rtcd);
169
    else
170
      vp8_optimize_mby_4x4(x, rtcd);
John Koleszar's avatar
John Koleszar committed
171 172
  }

Daniel Kang's avatar
Daniel Kang committed
173
  if (tx_type == TX_16X16)
174 175 176 177 178 179 180 181 182 183
#if CONFIG_HYBRIDTRANSFORM16X16
  {
    if ((x->e_mbd.mode_info_context->mbmi.mode < I8X8_PRED) &&
        (x->q_index < ACTIVE_HT16)) {
      BLOCKD *bd = &x->e_mbd.block[0];
      vp8_ihtllm_c(bd->dqcoeff, bd->diff, 32, txfm_type, 16);
    } else
      vp8_inverse_transform_mby_16x16(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  }
#else
Daniel Kang's avatar
Daniel Kang committed
184
    vp8_inverse_transform_mby_16x16(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
185
#endif
186
  else if (tx_type == TX_8X8)
John Koleszar's avatar
John Koleszar committed
187 188
    vp8_inverse_transform_mby_8x8(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  else
189
    vp8_inverse_transform_mby_4x4(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
190

191 192
  RECON_INVOKE(&rtcd->common->recon, recon_mby)
      (IF_RTCD(&rtcd->common->recon), &x->e_mbd);
John Koleszar's avatar
John Koleszar committed
193 194 195

}

John Koleszar's avatar
John Koleszar committed
196 197
void vp8_encode_intra16x16mbuv(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  int tx_type = x->e_mbd.mode_info_context->mbmi.txfm_size;
Daniel Kang's avatar
Daniel Kang committed
198
  if (tx_type == TX_16X16) tx_type = TX_8X8; // 16x16 for U and V should default to 8x8 behavior.
199
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
200
  if (x->e_mbd.mode_info_context->mbmi.second_uv_mode == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
Paul Wilkins's avatar
Paul Wilkins committed
201
#endif
202
    RECON_INVOKE(&rtcd->common->recon, build_intra_predictors_mbuv)(&x->e_mbd);
203
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
204
  } else {
205
    RECON_INVOKE(&rtcd->common->recon, build_comp_intra_predictors_mbuv)(&x->e_mbd);
John Koleszar's avatar
John Koleszar committed
206
  }
207
#endif
John Koleszar's avatar
John Koleszar committed
208

John Koleszar's avatar
John Koleszar committed
209 210 211 212
  ENCODEMB_INVOKE(&rtcd->encodemb, submbuv)(x->src_diff, x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);
  if (tx_type == TX_8X8)
    vp8_transform_mbuv_8x8(x);
  else
213
    vp8_transform_mbuv_4x4(x);
John Koleszar's avatar
John Koleszar committed
214

John Koleszar's avatar
John Koleszar committed
215 216 217
  if (tx_type == TX_8X8)
    vp8_quantize_mbuv_8x8(x);
  else
218
    vp8_quantize_mbuv_4x4(x);
219

John Koleszar's avatar
John Koleszar committed
220 221 222
  if (x->optimize) {
    if (tx_type == TX_8X8)
      vp8_optimize_mbuv_8x8(x, rtcd);
223
    else
224
      vp8_optimize_mbuv_4x4(x, rtcd);
John Koleszar's avatar
John Koleszar committed
225 226 227 228 229
  }

  if (tx_type == TX_8X8)
    vp8_inverse_transform_mbuv_8x8(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  else
230
    vp8_inverse_transform_mbuv_4x4(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
John Koleszar's avatar
John Koleszar committed
231

232
  vp8_recon_intra_mbuv(IF_RTCD(&rtcd->common->recon), &x->e_mbd);
John Koleszar's avatar
John Koleszar committed
233
}
Yaowu Xu's avatar
Yaowu Xu committed
234 235

void vp8_encode_intra8x8(const VP8_ENCODER_RTCD *rtcd,
John Koleszar's avatar
John Koleszar committed
236
                         MACROBLOCK *x, int ib) {
237 238
  MACROBLOCKD *xd = &x->e_mbd;
  BLOCKD *b = &xd->block[ib];
John Koleszar's avatar
John Koleszar committed
239 240 241
  BLOCK *be = &x->block[ib];
  const int iblock[4] = {0, 1, 4, 5};
  int i;
Yaowu Xu's avatar
Yaowu Xu committed
242

243
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
244
  if (b->bmi.as_mode.second == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
245
#endif
246 247
    RECON_INVOKE(&rtcd->common->recon, intra8x8_predict)
    (b, b->bmi.as_mode.first, b->predictor);
248
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
249
  } else {
250 251
    RECON_INVOKE(&rtcd->common->recon, comp_intra8x8_predict)
    (b, b->bmi.as_mode.first, b->bmi.as_mode.second, b->predictor);
John Koleszar's avatar
John Koleszar committed
252
  }
253
#endif
Yaowu Xu's avatar
Yaowu Xu committed
254

255
  if (x->e_mbd.mode_info_context->mbmi.txfm_size == TX_8X8) {
256 257 258 259
    int idx = (ib & 0x02) ? (ib + 2) : ib;

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

261
#if CONFIG_HYBRIDTRANSFORM8X8
262
    txfm_map(b, pred_mode_conv(b->bmi.as_mode.first));
263 264
    vp8_fht_c(be->src_diff, (x->block + idx)->coeff, 32,
              b->bmi.as_mode.tx_type, 8);
265
    x->quantize_b_8x8(x->block + idx, xd->block + idx);
266 267
    vp8_ihtllm_c(xd->block[idx].dqcoeff, xd->block[ib].diff, 32,
                 b->bmi.as_mode.tx_type, 8);
268 269 270 271 272
#else
    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);
#endif
273
  } else {
274 275
    for (i = 0; i < 4; i++) {
      b = &xd->block[ib + iblock[i]];
276 277 278
      be = &x->block[ib + iblock[i]];
      ENCODEMB_INVOKE(&rtcd->encodemb, subb)(be, b, 16);
      x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
279 280
      x->quantize_b_4x4(be, b);
      vp8_inverse_transform_b_4x4(IF_RTCD(&rtcd->common->idct), b, 32);
281 282
    }
  }
283 284 285 286 287 288 289

  // 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
290 291
}

John Koleszar's avatar
John Koleszar committed
292 293 294 295 296 297 298
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
299 300 301
}

void vp8_encode_intra_uv4x4(const VP8_ENCODER_RTCD *rtcd,
John Koleszar's avatar
John Koleszar committed
302 303 304 305
                            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
306

307
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
308
  if (second == -1) {
309
#endif
310 311
    RECON_INVOKE(&rtcd->common->recon, intra_uv4x4_predict)
    (b, mode, b->predictor);
312
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
313
  } else {
314 315
    RECON_INVOKE(&rtcd->common->recon, comp_intra_uv4x4_predict)
    (b, mode, second, b->predictor);
John Koleszar's avatar
John Koleszar committed
316
  }
317
#endif
Yaowu Xu's avatar
Yaowu Xu committed
318

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

John Koleszar's avatar
John Koleszar committed
321
  x->vp8_short_fdct4x4(be->src_diff, be->coeff, 16);
Yaowu Xu's avatar
Yaowu Xu committed
322

323
  x->quantize_b_4x4(be, b);
Yaowu Xu's avatar
Yaowu Xu committed
324

325
  vp8_inverse_transform_b_4x4(IF_RTCD(&rtcd->common->idct), b, 16);
Yaowu Xu's avatar
Yaowu Xu committed
326

327 328
  RECON_INVOKE(&rtcd->common->recon, recon_uv)(b->predictor,
                                               b->diff, *(b->base_dst) + b->dst, b->dst_stride);
Yaowu Xu's avatar
Yaowu Xu committed
329 330 331 332
}



John Koleszar's avatar
John Koleszar committed
333 334 335 336 337 338 339
void vp8_encode_intra8x8mbuv(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  int i, ib, mode, second;
  BLOCKD *b;
  for (i = 0; i < 4; i++) {
    ib = vp8_i8x8_block[i];
    b = &x->e_mbd.block[ib];
    mode = b->bmi.as_mode.first;
340
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
341
    second = b->bmi.as_mode.second;
342
#else
John Koleszar's avatar
John Koleszar committed
343
    second = -1;
344
#endif
John Koleszar's avatar
John Koleszar committed
345 346 347 348 349
    /*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
350
}