vp9_alloccommon.c 6.86 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_config.h"
13
#include "vp9/common/vp9_blockd.h"
John Koleszar's avatar
John Koleszar committed
14
#include "vpx_mem/vpx_mem.h"
15
16
17
18
19
#include "vp9/common/vp9_onyxc_int.h"
#include "vp9/common/vp9_findnearmv.h"
#include "vp9/common/vp9_entropymode.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_systemdependent.h"
John Koleszar's avatar
John Koleszar committed
20
21


Dmitry Kovalev's avatar
Dmitry Kovalev committed
22
23
void vp9_update_mode_info_border(VP9_COMMON *cpi, MODE_INFO *mi) {
  const int stride = cpi->mode_info_stride;
John Koleszar's avatar
John Koleszar committed
24
25
26
  int i;

  // Clear down top border row
Dmitry Kovalev's avatar
Dmitry Kovalev committed
27
  vpx_memset(mi, 0, sizeof(MODE_INFO) * stride);
John Koleszar's avatar
John Koleszar committed
28
29

  // Clear left border column
30
  for (i = 1; i < cpi->mi_rows + 1; i++)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
31
    vpx_memset(&mi[i * stride], 0, sizeof(MODE_INFO));
32
}
33

34
void vp9_update_mode_info_in_image(VP9_COMMON *cpi, MODE_INFO *mi) {
John Koleszar's avatar
John Koleszar committed
35
  int i, j;
36
  MODE_INFO *ptr;
John Koleszar's avatar
John Koleszar committed
37
38

  // For each in image mode_info element set the in image flag to 1
39
  for (i = 0; i < cpi->mi_rows; i++) {
40
    ptr = mi;
41
    for (j = 0; j < cpi->mi_cols; j++) {
42
43
      ptr->mbmi.mb_in_image = 1;
      ptr++;  // Next element in the row
John Koleszar's avatar
John Koleszar committed
44
    }
John Koleszar's avatar
John Koleszar committed
45

46
47
    // Step over border element at start of next row
    mi += cpi->mode_info_stride;
John Koleszar's avatar
John Koleszar committed
48
  }
John Koleszar's avatar
John Koleszar committed
49
}
50

Dmitry Kovalev's avatar
Dmitry Kovalev committed
51
void vp9_free_frame_buffers(VP9_COMMON *oci) {
John Koleszar's avatar
John Koleszar committed
52
  int i;
53

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

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

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

64
65
66
  vpx_free(oci->above_context[0]);
  for (i = 0; i < MAX_MB_PLANE; i++)
    oci->above_context[i] = 0;
John Koleszar's avatar
John Koleszar committed
67
68
  oci->mip = 0;
  oci->prev_mip = 0;
69
  oci->above_seg_context = 0;
John Koleszar's avatar
John Koleszar committed
70
71
}

72
int vp9_alloc_frame_buffers(VP9_COMMON *oci, int width, int height) {
73
  int i, mi_cols;
74

Dmitry Kovalev's avatar
Dmitry Kovalev committed
75
76
77
  // Our internal buffers are always multiples of 16
  const int aligned_width = multiple16(width);
  const int aligned_height = multiple16(height);
John Koleszar's avatar
John Koleszar committed
78

Dmitry Kovalev's avatar
Dmitry Kovalev committed
79
  vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
80

John Koleszar's avatar
John Koleszar committed
81
82
  for (i = 0; i < NUM_YV12_BUFFERS; i++) {
    oci->fb_idx_ref_cnt[i] = 0;
83
84
85
    if (vp9_alloc_frame_buffer(&oci->yv12_fb[i], width, height,
                               oci->subsampling_x, oci->subsampling_y,
                               VP9BORDERINPIXELS) < 0) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
86
      vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
87
      return 1;
John Koleszar's avatar
John Koleszar committed
88
    }
John Koleszar's avatar
John Koleszar committed
89
  }
John Koleszar's avatar
John Koleszar committed
90

91
92
93
  oci->new_fb_idx = NUM_YV12_BUFFERS - 1;
  oci->fb_idx_ref_cnt[oci->new_fb_idx] = 1;

94
  for (i = 0; i < ALLOWED_REFS_PER_FRAME; i++)
95
    oci->active_ref_idx[i] = i;
John Koleszar's avatar
John Koleszar committed
96

97
98
99
100
  for (i = 0; i < NUM_REF_FRAMES; i++) {
    oci->ref_frame_map[i] = i;
    oci->fb_idx_ref_cnt[i] = 1;
  }
John Koleszar's avatar
John Koleszar committed
101

102
103
104
  if (vp9_alloc_frame_buffer(&oci->temp_scale_frame, width, 16,
                             oci->subsampling_x, oci->subsampling_y,
                             VP9BORDERINPIXELS) < 0) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
105
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
106
107
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
108

109
110
111
  if (vp9_alloc_frame_buffer(&oci->post_proc_buffer, width, height,
                             oci->subsampling_x, oci->subsampling_y,
                             VP9BORDERINPIXELS) < 0) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
112
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
113
114
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
115

John Koleszar's avatar
John Koleszar committed
116
  oci->mb_rows = aligned_height >> 4;
117
  oci->mi_rows = aligned_height >> LOG2_MI_SIZE;
John Koleszar's avatar
John Koleszar committed
118
  oci->mb_cols = aligned_width >> 4;
119
  oci->mi_cols = aligned_width >> LOG2_MI_SIZE;
John Koleszar's avatar
John Koleszar committed
120
  oci->MBs = oci->mb_rows * oci->mb_cols;
121
122
123
  oci->mode_info_stride = oci->mi_cols + 1;
  oci->mip = vpx_calloc(oci->mode_info_stride * (oci->mi_rows + 1),
                        sizeof(MODE_INFO));
John Koleszar's avatar
John Koleszar committed
124

John Koleszar's avatar
John Koleszar committed
125
  if (!oci->mip) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
126
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
127
128
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
129

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

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

134
135
  oci->prev_mip = vpx_calloc(oci->mode_info_stride * (oci->mi_rows + 1),
                             sizeof(MODE_INFO));
136

John Koleszar's avatar
John Koleszar committed
137
  if (!oci->prev_mip) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
138
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
139
140
    return 1;
  }
141

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

144
145
  // FIXME(jkoleszar): allocate subsampled arrays for U/V once subsampling
  // information is exposed at this level
146
  mi_cols = mi_cols_aligned_to_sb(oci);
147
148
149
150
# if CONFIG_ALPHA
  // TODO(jkoleszar): Why is this * 2?
  oci->above_context[0] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * 8 * mi_cols, 1);
#else
151
  oci->above_context[0] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * 6 * mi_cols, 1);
152
#endif
153
  if (!oci->above_context[0]) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
154
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
155
156
    return 1;
  }
157
158
159
  for (i = 1; i < MAX_MB_PLANE; i++)
    oci->above_context[i] =
        oci->above_context[0] + i * sizeof(ENTROPY_CONTEXT) * 2 * mi_cols;
John Koleszar's avatar
John Koleszar committed
160

161
  oci->above_seg_context =
162
    vpx_calloc(sizeof(PARTITION_CONTEXT) * mi_cols, 1);
163
164
165
166
167
168
169
170
171

  if (!oci->above_seg_context) {
    vp9_free_frame_buffers(oci);
    return 1;
  }

  vp9_update_mode_info_border(oci, oci->mip);
  vp9_update_mode_info_in_image(oci, oci->mi);

John Koleszar's avatar
John Koleszar committed
172
  return 0;
John Koleszar's avatar
John Koleszar committed
173
}
174

175
void vp9_setup_version(VP9_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
176
177
178
179
180
181
182
183
184
  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
185
    case 0:
John Koleszar's avatar
John Koleszar committed
186
187
188
      cm->no_lpf = 0;
      cm->use_bilinear_mc_filter = 0;
      break;
John Koleszar's avatar
John Koleszar committed
189
    case 1:
John Koleszar's avatar
John Koleszar committed
190
191
192
      cm->no_lpf = 0;
      cm->use_bilinear_mc_filter = 1;
      break;
John Koleszar's avatar
John Koleszar committed
193
    case 2:
Paul Wilkins's avatar
Paul Wilkins committed
194
    case 3:
John Koleszar's avatar
John Koleszar committed
195
196
197
198
      cm->no_lpf = 1;
      cm->use_bilinear_mc_filter = 1;
      break;
  }
John Koleszar's avatar
John Koleszar committed
199
}
200
void vp9_create_common(VP9_COMMON *oci) {
201
  vp9_machine_specific_config(oci);
John Koleszar's avatar
John Koleszar committed
202

203
  vp9_init_mbmode_probs(oci);
John Koleszar's avatar
John Koleszar committed
204
205
206
207
208
209
210

  oci->txfm_mode = ONLY_4X4;
  oci->comp_pred_mode = HYBRID_PREDICTION;
  oci->no_lpf = 0;
  oci->use_bilinear_mc_filter = 0;
  oci->clr_type = REG_YUV;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
211
  // Initialize reference frame sign bias structure to defaults
John Koleszar's avatar
John Koleszar committed
212
  vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
John Koleszar's avatar
John Koleszar committed
213
214
}

215
void vp9_remove_common(VP9_COMMON *oci) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
216
  vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
217
218
}

219
220
221
222
void vp9_initialize_common() {
  vp9_coef_tree_initialize();
  vp9_entropy_mode_init();
  vp9_entropy_mv_init();
John Koleszar's avatar
John Koleszar committed
223
}
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248


void vp9_update_frame_size(VP9_COMMON *cm) {
  const int aligned_width = multiple16(cm->width);
  const int aligned_height = multiple16(cm->height);

  cm->mb_rows = aligned_height >> 4;
  cm->mb_cols = aligned_width >> 4;

  cm->mi_rows = aligned_height >> LOG2_MI_SIZE;
  cm->mi_cols = aligned_width >> LOG2_MI_SIZE;

  cm->MBs = cm->mb_rows * cm->mb_cols;
  cm->mode_info_stride = cm->mi_cols + 1;
  cm->mi = cm->mip + cm->mode_info_stride + 1;
  cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;

  memset(cm->mip, 0,
         cm->mode_info_stride * (cm->mi_rows + 1) * sizeof(MODE_INFO));
  vp9_update_mode_info_border(cm, cm->mip);
  vp9_update_mode_info_in_image(cm, cm->mi);

  vp9_update_mode_info_border(cm, cm->prev_mip);
  vp9_update_mode_info_in_image(cm, cm->prev_mi);
}