encodeintra.c 9.8 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"
John Koleszar's avatar
John Koleszar committed
19
#include "dct.h"
John Koleszar's avatar
John Koleszar committed
20
#include "vp8/common/g_common.h"
John Koleszar's avatar
John Koleszar committed
21
#include "encodeintra.h"
22
#include "vpx_rtcd.h"
John Koleszar's avatar
John Koleszar committed
23
24
25
26
27
28
29


#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
32
33
#if CONFIG_HYBRIDTRANSFORM
extern void vp8_ht_quantize_b(BLOCK *b, BLOCKD *d);
#endif
Tero Rintaluoma's avatar
Tero Rintaluoma committed
34

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

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

    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
54
    }
John Koleszar's avatar
John Koleszar committed
55
  }
Tero Rintaluoma's avatar
Tero Rintaluoma committed
56

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

John Koleszar's avatar
John Koleszar committed
59
  return intra_pred_var;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
60
}
61
62

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

Jingning Han's avatar
Jingning Han committed
67
68
69
70
71
72
#if CONFIG_HYBRIDTRANSFORM
    int QIndex = x->q_index;
    int active_ht = (QIndex < ACTIVE_HT);
#endif


73
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
74
  if (b->bmi.as_mode.second == (B_PREDICTION_MODE)(B_DC_PRED - 1)) {
75
#endif
76
    vp8_intra4x4_predict(b, b->bmi.as_mode.first, b->predictor);
77
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
78
  } else {
79
80
    vp8_comp_intra4x4_predict(b, b->bmi.as_mode.first, b->bmi.as_mode.second,
                              b->predictor);
John Koleszar's avatar
John Koleszar committed
81
  }
82
#endif
John Koleszar's avatar
John Koleszar committed
83

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

Jingning Han's avatar
Jingning Han committed
86
#if CONFIG_HYBRIDTRANSFORM
87
88
89
90
91
92
93
94
95
96
97
  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);
    vp8_ht_quantize_b(be, b);
    vp8_inverse_htransform_b(IF_RTCD(&rtcd->common->idct), b, 32) ;
  } else {
    x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32) ;
    x->quantize_b(be, b) ;
    vp8_inverse_transform_b(IF_RTCD(&rtcd->common->idct), b, 32) ;
  }
Jingning Han's avatar
Jingning Han committed
98
#else
99
100
101
  x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
  x->quantize_b(be, b);
  vp8_inverse_transform_b(IF_RTCD(&rtcd->common->idct), b, 32);
Jingning Han's avatar
Jingning Han committed
102
#endif
John Koleszar's avatar
John Koleszar committed
103

104
  vp8_recon_b(b->predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
105
106
}

John Koleszar's avatar
John Koleszar committed
107
108
void vp8_encode_intra4x4mby(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *mb) {
  int i;
John Koleszar's avatar
John Koleszar committed
109

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

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

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

John Koleszar's avatar
John Koleszar committed
124
  int tx_type = x->e_mbd.mode_info_context->mbmi.txfm_size;
125
126
127
#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
128

129
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
130
  if (x->e_mbd.mode_info_context->mbmi.second_mode == (MB_PREDICTION_MODE)(DC_PRED - 1))
131
#endif
132
    vp8_build_intra_predictors_mby(&x->e_mbd);
133
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
134
  else
135
    vp8_build_comp_intra_predictors_mby(&x->e_mbd);
136
#endif
John Koleszar's avatar
John Koleszar committed
137

John Koleszar's avatar
John Koleszar committed
138
  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
139

Daniel Kang's avatar
Daniel Kang committed
140
  if (tx_type == TX_16X16)
141
142
143
144
145
146
147
148
149
150
151
152
#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
153
    vp8_transform_intra_mby_16x16(x);
154
#endif
155
  else if (tx_type == TX_8X8)
John Koleszar's avatar
John Koleszar committed
156
157
158
    vp8_transform_intra_mby_8x8(x);
  else
    vp8_transform_intra_mby(x);
John Koleszar's avatar
John Koleszar committed
159

Daniel Kang's avatar
Daniel Kang committed
160
161
  if (tx_type == TX_16X16)
    vp8_quantize_mby_16x16(x);
162
  else if (tx_type == TX_8X8)
John Koleszar's avatar
John Koleszar committed
163
164
165
    vp8_quantize_mby_8x8(x);
  else
    vp8_quantize_mby(x);
John Koleszar's avatar
John Koleszar committed
166

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

Daniel Kang's avatar
Daniel Kang committed
176
  if (tx_type == TX_16X16)
177
178
179
180
181
182
183
184
185
186
#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
187
    vp8_inverse_transform_mby_16x16(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
188
#endif
189
  else if (tx_type == TX_8X8)
John Koleszar's avatar
John Koleszar committed
190
191
192
    vp8_inverse_transform_mby_8x8(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  else
    vp8_inverse_transform_mby(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
193

194
  vp8_recon_mby(&x->e_mbd);
John Koleszar's avatar
John Koleszar committed
195
196
197

}

John Koleszar's avatar
John Koleszar committed
198
199
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
200
  if (tx_type == TX_16X16) tx_type = TX_8X8; // 16x16 for U and V should default to 8x8 behavior.
201
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
202
  if (x->e_mbd.mode_info_context->mbmi.second_uv_mode == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
Paul Wilkins's avatar
Paul Wilkins committed
203
#endif
204
    vp8_build_intra_predictors_mbuv(&x->e_mbd);
205
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
206
  } else {
207
    vp8_build_comp_intra_predictors_mbuv(&x->e_mbd);
John Koleszar's avatar
John Koleszar committed
208
  }
209
#endif
John Koleszar's avatar
John Koleszar committed
210

John Koleszar's avatar
John Koleszar committed
211
212
213
214
215
  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
    vp8_transform_mbuv(x);
John Koleszar's avatar
John Koleszar committed
216

John Koleszar's avatar
John Koleszar committed
217
218
219
220
  if (tx_type == TX_8X8)
    vp8_quantize_mbuv_8x8(x);
  else
    vp8_quantize_mbuv(x);
221

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

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

234
  vp8_recon_intra_mbuv(&x->e_mbd);
John Koleszar's avatar
John Koleszar committed
235
}
Yaowu Xu's avatar
Yaowu Xu committed
236
237

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

245
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
246
  if (b->bmi.as_mode.second == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
247
#endif
248
    vp8_intra8x8_predict(b, b->bmi.as_mode.first, b->predictor);
249
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
250
  } else {
251
252
    vp8_comp_intra8x8_predict(b, b->bmi.as_mode.first, b->bmi.as_mode.second,
                              b->predictor);
John Koleszar's avatar
John Koleszar committed
253
  }
254
#endif
Yaowu Xu's avatar
Yaowu Xu committed
255

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

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

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

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

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

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

308
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
309
  if (second == -1) {
310
#endif
311
    vp8_intra_uv4x4_predict(b, mode, b->predictor);
312
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
313
  } else {
314
    vp8_comp_intra_uv4x4_predict(b, mode, second, b->predictor);
John Koleszar's avatar
John Koleszar committed
315
  }
316
#endif
Yaowu Xu's avatar
Yaowu Xu committed
317

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

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

John Koleszar's avatar
John Koleszar committed
322
  x->quantize_b(be, b);
Yaowu Xu's avatar
Yaowu Xu committed
323

John Koleszar's avatar
John Koleszar committed
324
  vp8_inverse_transform_b(IF_RTCD(&rtcd->common->idct), b, 16);
Yaowu Xu's avatar
Yaowu Xu committed
325

326
  vp8_recon_uv_b_c(b->predictor,b->diff, *(b->base_dst) + b->dst, b->dst_stride);
Yaowu Xu's avatar
Yaowu Xu committed
327
328
329
330
}



John Koleszar's avatar
John Koleszar committed
331
332
333
334
335
336
337
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;
338
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
339
    second = b->bmi.as_mode.second;
340
#else
John Koleszar's avatar
John Koleszar committed
341
    second = -1;
342
#endif
John Koleszar's avatar
John Koleszar committed
343
344
345
346
347
    /*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
348
}