alloccommon.c 5.7 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 14 15 16 17
#include "blockd.h"
#include "vpx_mem/vpx_mem.h"
#include "onyxc_int.h"
#include "findnearmv.h"
#include "entropymode.h"
18
#include "entropymv.h"
John Koleszar's avatar
John Koleszar committed
19 20 21 22 23
#include "systemdependent.h"


extern  void vp8_init_scan_order_mask();

24
void vp9_update_mode_info_border(VP8_COMMON *cpi, MODE_INFO *mi_base) {
John Koleszar's avatar
John Koleszar committed
25 26 27 28 29 30 31 32 33 34
  int stride = cpi->mode_info_stride;
  int i;

  // Clear down top border row
  vpx_memset(mi_base, 0, sizeof(MODE_INFO) * cpi->mode_info_stride);

  // Clear left border column
  for (i = 1; i < cpi->mb_rows + 1; i++) {
    vpx_memset(&mi_base[i * stride], 0, sizeof(MODE_INFO));
  }
35
}
36 37

void vp9_update_mode_info_in_image(VP8_COMMON *cpi, MODE_INFO *mi) {
John Koleszar's avatar
John Koleszar committed
38 39 40 41 42 43 44
  int i, j;

  // For each in image mode_info element set the in image flag to 1
  for (i = 0; i < cpi->mb_rows; i++) {
    for (j = 0; j < cpi->mb_cols; j++) {
      mi->mbmi.mb_in_image = 1;
      mi++;   // Next element in the row
John Koleszar's avatar
John Koleszar committed
45
    }
John Koleszar's avatar
John Koleszar committed
46 47 48

    mi++;       // Step over border element at start of next row
  }
John Koleszar's avatar
John Koleszar committed
49
}
50

51
void vp9_de_alloc_frame_buffers(VP8_COMMON *oci) {
John Koleszar's avatar
John Koleszar committed
52
  int i;
53

John Koleszar's avatar
John Koleszar committed
54 55
  for (i = 0; i < NUM_YV12_BUFFERS; i++)
    vp8_yv12_de_alloc_frame_buffer(&oci->yv12_fb[i]);
56

John Koleszar's avatar
John Koleszar committed
57 58
  vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
  vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
John Koleszar's avatar
John Koleszar committed
59

John Koleszar's avatar
John Koleszar committed
60 61 62
  vpx_free(oci->above_context);
  vpx_free(oci->mip);
  vpx_free(oci->prev_mip);
John Koleszar's avatar
John Koleszar committed
63

John Koleszar's avatar
John Koleszar committed
64 65 66
  oci->above_context = 0;
  oci->mip = 0;
  oci->prev_mip = 0;
John Koleszar's avatar
John Koleszar committed
67 68 69

}

70
int vp9_alloc_frame_buffers(VP8_COMMON *oci, int width, int height) {
John Koleszar's avatar
John Koleszar committed
71
  int i;
72

73
  vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
74

John Koleszar's avatar
John Koleszar committed
75 76 77
  /* our internal buffers are always multiples of 16 */
  if ((width & 0xf) != 0)
    width += 16 - (width & 0xf);
John Koleszar's avatar
John Koleszar committed
78

John Koleszar's avatar
John Koleszar committed
79 80
  if ((height & 0xf) != 0)
    height += 16 - (height & 0xf);
John Koleszar's avatar
John Koleszar committed
81 82


John Koleszar's avatar
John Koleszar committed
83 84 85 86
  for (i = 0; i < NUM_YV12_BUFFERS; i++) {
    oci->fb_idx_ref_cnt[i] = 0;
    oci->yv12_fb[i].flags = 0;
    if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i], width, height, VP8BORDERINPIXELS) < 0) {
87
      vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
88
      return 1;
John Koleszar's avatar
John Koleszar committed
89
    }
John Koleszar's avatar
John Koleszar committed
90
  }
John Koleszar's avatar
John Koleszar committed
91

John Koleszar's avatar
John Koleszar committed
92 93 94 95
  oci->new_fb_idx = 0;
  oci->lst_fb_idx = 1;
  oci->gld_fb_idx = 2;
  oci->alt_fb_idx = 3;
John Koleszar's avatar
John Koleszar committed
96

John Koleszar's avatar
John Koleszar committed
97 98 99 100
  oci->fb_idx_ref_cnt[0] = 1;
  oci->fb_idx_ref_cnt[1] = 1;
  oci->fb_idx_ref_cnt[2] = 1;
  oci->fb_idx_ref_cnt[3] = 1;
John Koleszar's avatar
John Koleszar committed
101

John Koleszar's avatar
John Koleszar committed
102
  if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame,   width, 16, VP8BORDERINPIXELS) < 0) {
103
    vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
104 105
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
106

John Koleszar's avatar
John Koleszar committed
107
  if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height, VP8BORDERINPIXELS) < 0) {
108
    vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
109 110
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
111

John Koleszar's avatar
John Koleszar committed
112 113 114 115 116
  oci->mb_rows = height >> 4;
  oci->mb_cols = width >> 4;
  oci->MBs = oci->mb_rows * oci->mb_cols;
  oci->mode_info_stride = oci->mb_cols + 1;
  oci->mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
John Koleszar's avatar
John Koleszar committed
117

John Koleszar's avatar
John Koleszar committed
118
  if (!oci->mip) {
119
    vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
120 121
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
122

John Koleszar's avatar
John Koleszar committed
123
  oci->mi = oci->mip + oci->mode_info_stride + 1;
John Koleszar's avatar
John Koleszar committed
124

John Koleszar's avatar
John Koleszar committed
125
  /* allocate memory for last frame MODE_INFO array */
Paul Wilkins's avatar
Paul Wilkins committed
126

John Koleszar's avatar
John Koleszar committed
127
  oci->prev_mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
128

John Koleszar's avatar
John Koleszar committed
129
  if (!oci->prev_mip) {
130
    vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
131 132
    return 1;
  }
133

John Koleszar's avatar
John Koleszar committed
134
  oci->prev_mi = oci->prev_mip + oci->mode_info_stride + 1;
John Koleszar's avatar
John Koleszar committed
135

John Koleszar's avatar
John Koleszar committed
136
  oci->above_context = vpx_calloc(sizeof(ENTROPY_CONTEXT_PLANES) * oci->mb_cols, 1);
John Koleszar's avatar
John Koleszar committed
137

John Koleszar's avatar
John Koleszar committed
138
  if (!oci->above_context) {
139
    vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
140 141
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
142

143 144
  vp9_update_mode_info_border(oci, oci->mip);
  vp9_update_mode_info_in_image(oci, oci->mi);
John Koleszar's avatar
John Koleszar committed
145

John Koleszar's avatar
John Koleszar committed
146
  return 0;
John Koleszar's avatar
John Koleszar committed
147
}
148
void vp9_setup_version(VP8_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
149 150 151 152 153 154 155 156 157
  if (cm->version & 0x4) {
    if (!CONFIG_EXPERIMENTAL)
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
                         "Bitstream was created by an experimental "
                         "encoder");
    cm->experimental = 1;
  }

  switch (cm->version & 0x3) {
John Koleszar's avatar
John Koleszar committed
158
    case 0:
John Koleszar's avatar
John Koleszar committed
159 160 161 162 163
      cm->no_lpf = 0;
      cm->filter_type = NORMAL_LOOPFILTER;
      cm->use_bilinear_mc_filter = 0;
      cm->full_pixel = 0;
      break;
John Koleszar's avatar
John Koleszar committed
164
    case 1:
John Koleszar's avatar
John Koleszar committed
165 166 167 168 169
      cm->no_lpf = 0;
      cm->filter_type = SIMPLE_LOOPFILTER;
      cm->use_bilinear_mc_filter = 1;
      cm->full_pixel = 0;
      break;
John Koleszar's avatar
John Koleszar committed
170
    case 2:
Paul Wilkins's avatar
Paul Wilkins committed
171
    case 3:
John Koleszar's avatar
John Koleszar committed
172 173 174 175 176 177 178 179 180 181 182 183 184
      cm->no_lpf = 1;
      cm->filter_type = NORMAL_LOOPFILTER;
      cm->use_bilinear_mc_filter = 1;
      cm->full_pixel = 0;
      break;
      // Full pel only code deprecated in experimental code base
      // case 3:
      //    cm->no_lpf = 1;
      //    cm->filter_type = SIMPLE_LOOPFILTER;
      //    cm->use_bilinear_mc_filter = 1;
      //    cm->full_pixel = 1;
      //    break;
  }
John Koleszar's avatar
John Koleszar committed
185
}
186 187
void vp9_create_common(VP8_COMMON *oci) {
  vp9_machine_specific_config(oci);
John Koleszar's avatar
John Koleszar committed
188

189
  vp9_init_mbmode_probs(oci);
John Koleszar's avatar
John Koleszar committed
190

191
  vp9_default_bmode_probs(oci->fc.bmode_prob);
John Koleszar's avatar
John Koleszar committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

  oci->txfm_mode = ONLY_4X4;
  oci->mb_no_coeff_skip = 1;
  oci->comp_pred_mode = HYBRID_PREDICTION;
  oci->no_lpf = 0;
  oci->filter_type = NORMAL_LOOPFILTER;
  oci->use_bilinear_mc_filter = 0;
  oci->full_pixel = 0;
  oci->clr_type = REG_YUV;
  oci->clamp_type = RECON_CLAMP_REQUIRED;

  /* Initialise reference frame sign bias structure to defaults */
  vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));

  /* Default disable buffer to buffer copying */
  oci->copy_buffer_to_gf = 0;
  oci->copy_buffer_to_arf = 0;
  oci->kf_ymode_probs_update = 0;
John Koleszar's avatar
John Koleszar committed
210 211
}

212 213
void vp9_remove_common(VP8_COMMON *oci) {
  vp9_de_alloc_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
214 215
}

216 217
void vp9_initialize_common() {
  vp9_coef_tree_initialize();
John Koleszar's avatar
John Koleszar committed
218

219
  vp9_entropy_mode_init();
John Koleszar's avatar
John Koleszar committed
220

221
  vp9_entropy_mv_init();
John Koleszar's avatar
John Koleszar committed
222
}