vp9_alloccommon.c 5.76 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"
John Koleszar's avatar
John Koleszar committed
13
#include "vpx_mem/vpx_mem.h"
14

15
#include "vp9/common/vp9_blockd.h"
16
17
#include "vp9/common/vp9_entropymode.h"
#include "vp9/common/vp9_entropymv.h"
18
19
#include "vp9/common/vp9_findnearmv.h"
#include "vp9/common/vp9_onyxc_int.h"
20
#include "vp9/common/vp9_systemdependent.h"
John Koleszar's avatar
John Koleszar committed
21

22
23
void vp9_update_mode_info_border(VP9_COMMON *cm, MODE_INFO *mi) {
  const int stride = cm->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 < cm->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 *cm, MODE_INFO *mi) {
John Koleszar's avatar
John Koleszar committed
35
36
37
  int i, j;

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

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

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

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

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

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

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

71
static void set_mb_mi(VP9_COMMON *cm, int aligned_width, int aligned_height) {
72
73
  cm->mb_cols = (aligned_width + 8) >> 4;
  cm->mb_rows = (aligned_height + 8) >> 4;
74
75
76
77
  cm->MBs = cm->mb_rows * cm->mb_cols;

  cm->mi_cols = aligned_width >> LOG2_MI_SIZE;
  cm->mi_rows = aligned_height >> LOG2_MI_SIZE;
78
  cm->mode_info_stride = cm->mi_cols + MI_BLOCK_SIZE;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}

static void setup_mi(VP9_COMMON *cm) {
  cm->mi = cm->mip + cm->mode_info_stride + 1;
  cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;

  vpx_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);
}

95
int vp9_alloc_frame_buffers(VP9_COMMON *oci, int width, int height) {
96
  int i, mi_cols;
97

98
99
  const int aligned_width = ALIGN_POWER_OF_TWO(width, LOG2_MI_SIZE);
  const int aligned_height = ALIGN_POWER_OF_TWO(height, LOG2_MI_SIZE);
100
101
  const int ss_x = oci->subsampling_x;
  const int ss_y = oci->subsampling_y;
102
  int mi_size;
John Koleszar's avatar
John Koleszar committed
103

Dmitry Kovalev's avatar
Dmitry Kovalev committed
104
  vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
105

John Koleszar's avatar
John Koleszar committed
106
107
  for (i = 0; i < NUM_YV12_BUFFERS; i++) {
    oci->fb_idx_ref_cnt[i] = 0;
108
109
110
    if (vp9_alloc_frame_buffer(&oci->yv12_fb[i], width, height, ss_x, ss_y,
                               VP9BORDERINPIXELS) < 0)
      goto fail;
John Koleszar's avatar
John Koleszar committed
111
  }
John Koleszar's avatar
John Koleszar committed
112

113
114
115
  oci->new_fb_idx = NUM_YV12_BUFFERS - 1;
  oci->fb_idx_ref_cnt[oci->new_fb_idx] = 1;

116
  for (i = 0; i < ALLOWED_REFS_PER_FRAME; i++)
117
    oci->active_ref_idx[i] = i;
John Koleszar's avatar
John Koleszar committed
118

119
120
121
122
  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
123

124
125
126
  if (vp9_alloc_frame_buffer(&oci->temp_scale_frame, width, 16, ss_x, ss_y,
                             VP9BORDERINPIXELS) < 0)
    goto fail;
John Koleszar's avatar
John Koleszar committed
127

128
129
130
131
132
  if (vp9_alloc_frame_buffer(&oci->post_proc_buffer, width, height, ss_x, ss_y,
                             VP9BORDERINPIXELS) < 0)
    goto fail;

  set_mb_mi(oci, aligned_width, aligned_height);
John Koleszar's avatar
John Koleszar committed
133

134
  // Allocation
135
136
137
  mi_size = oci->mode_info_stride * (oci->mi_rows + MI_BLOCK_SIZE);

  oci->mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
138
139
  if (!oci->mip)
    goto fail;
Paul Wilkins's avatar
Paul Wilkins committed
140

141
  oci->prev_mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
142
143
  if (!oci->prev_mip)
    goto fail;
144

145
  setup_mi(oci);
John Koleszar's avatar
John Koleszar committed
146

147
148
  // FIXME(jkoleszar): allocate subsampled arrays for U/V once subsampling
  // information is exposed at this level
149
  mi_cols = mi_cols_aligned_to_sb(oci->mi_cols);
150

Ronald S. Bultje's avatar
Ronald S. Bultje committed
151
152
  // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
  // block where mi unit size is 8x8.
153
154
155
# if CONFIG_ALPHA
  oci->above_context[0] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * 8 * mi_cols, 1);
#else
156
  oci->above_context[0] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * 6 * mi_cols, 1);
157
#endif
158
159
160
  if (!oci->above_context[0])
    goto fail;

161
162
163
  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
164

165
166
167
  oci->above_seg_context = vpx_calloc(sizeof(PARTITION_CONTEXT) * mi_cols, 1);
  if (!oci->above_seg_context)
    goto fail;
168

John Koleszar's avatar
John Koleszar committed
169
  return 0;
170
171
172
173

 fail:
  vp9_free_frame_buffers(oci);
  return 1;
John Koleszar's avatar
John Koleszar committed
174
}
175

176
void vp9_create_common(VP9_COMMON *oci) {
177
  vp9_machine_specific_config(oci);
John Koleszar's avatar
John Koleszar committed
178

179
  vp9_init_mbmode_probs(oci);
John Koleszar's avatar
John Koleszar committed
180
181
182
183
184

  oci->txfm_mode = ONLY_4X4;
  oci->comp_pred_mode = HYBRID_PREDICTION;
  oci->clr_type = REG_YUV;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
185
  // Initialize reference frame sign bias structure to defaults
John Koleszar's avatar
John Koleszar committed
186
  vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
John Koleszar's avatar
John Koleszar committed
187
188
}

189
void vp9_remove_common(VP9_COMMON *oci) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
190
  vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
191
192
}

193
194
195
196
void vp9_initialize_common() {
  vp9_coef_tree_initialize();
  vp9_entropy_mode_init();
  vp9_entropy_mv_init();
John Koleszar's avatar
John Koleszar committed
197
}
198
199

void vp9_update_frame_size(VP9_COMMON *cm) {
200
201
  const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, LOG2_MI_SIZE);
  const int aligned_height = ALIGN_POWER_OF_TWO(cm->height, LOG2_MI_SIZE);
202

203
204
  set_mb_mi(cm, aligned_width, aligned_height);
  setup_mi(cm);
205
}