1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  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.
9 */
10
11#include "vpx_config.h"
12#include "vp8_rtcd.h"
13#include "./vpx_dsp_rtcd.h"
14#include "vp8/encoder/quantize.h"
15#include "vp8/common/reconintra.h"
16#include "vp8/common/reconintra4x4.h"
17#include "encodemb.h"
18#include "vp8/common/invtrans.h"
19#include "encodeintra.h"
20
21int vp8_encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred) {
22  int i;
23  int intra_pred_var = 0;
24  (void)cpi;
25
26  if (use_dc_pred) {
27    x->e_mbd.mode_info_context->mbmi.mode = DC_PRED;
28    x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
29    x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
30
31    vp8_encode_intra16x16mby(x);
32
33    vp8_inverse_transform_mby(&x->e_mbd);
34  } else {
35    for (i = 0; i < 16; ++i) {
36      x->e_mbd.block[i].bmi.as_mode = B_DC_PRED;
37      vp8_encode_intra4x4block(x, i);
38    }
39  }
40
41  intra_pred_var = vpx_get_mb_ss(x->src_diff);
42
43  return intra_pred_var;
44}
45
46void vp8_encode_intra4x4block(MACROBLOCK *x, int ib) {
47  BLOCKD *b = &x->e_mbd.block[ib];
48  BLOCK *be = &x->block[ib];
49  int dst_stride = x->e_mbd.dst.y_stride;
50  unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
51  unsigned char *Above = dst - dst_stride;
52  unsigned char *yleft = dst - 1;
53  unsigned char top_left = Above[-1];
54
55  vp8_intra4x4_predict(Above, yleft, dst_stride, b->bmi.as_mode, b->predictor,
56                       16, top_left);
57
58  vp8_subtract_b(be, b, 16);
59
60  x->short_fdct4x4(be->src_diff, be->coeff, 32);
61
62  x->quantize_b(be, b);
63
64  if (*b->eob > 1) {
65    vp8_short_idct4x4llm(b->dqcoeff, b->predictor, 16, dst, dst_stride);
66  } else {
67    vp8_dc_only_idct_add(b->dqcoeff[0], b->predictor, 16, dst, dst_stride);
68  }
69}
70
71void vp8_encode_intra4x4mby(MACROBLOCK *mb) {
72  int i;
73
74  MACROBLOCKD *xd = &mb->e_mbd;
75  intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
76
77  for (i = 0; i < 16; ++i) vp8_encode_intra4x4block(mb, i);
78  return;
79}
80
81void vp8_encode_intra16x16mby(MACROBLOCK *x) {
82  BLOCK *b = &x->block[0];
83  MACROBLOCKD *xd = &x->e_mbd;
84
85  vp8_build_intra_predictors_mby_s(xd, xd->dst.y_buffer - xd->dst.y_stride,
86                                   xd->dst.y_buffer - 1, xd->dst.y_stride,
87                                   xd->dst.y_buffer, xd->dst.y_stride);
88
89  vp8_subtract_mby(x->src_diff, *(b->base_src), b->src_stride, xd->dst.y_buffer,
90                   xd->dst.y_stride);
91
92  vp8_transform_intra_mby(x);
93
94  vp8_quantize_mby(x);
95
96  if (x->optimize) vp8_optimize_mby(x);
97}
98
99void vp8_encode_intra16x16mbuv(MACROBLOCK *x) {
100  MACROBLOCKD *xd = &x->e_mbd;
101
102  vp8_build_intra_predictors_mbuv_s(xd, xd->dst.u_buffer - xd->dst.uv_stride,
103                                    xd->dst.v_buffer - xd->dst.uv_stride,
104                                    xd->dst.u_buffer - 1, xd->dst.v_buffer - 1,
105                                    xd->dst.uv_stride, xd->dst.u_buffer,
106                                    xd->dst.v_buffer, xd->dst.uv_stride);
107
108  vp8_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer,
109                    x->src.uv_stride, xd->dst.u_buffer, xd->dst.v_buffer,
110                    xd->dst.uv_stride);
111
112  vp8_transform_mbuv(x);
113
114  vp8_quantize_mbuv(x);
115
116  if (x->optimize) vp8_optimize_mbuv(x);
117}
118