vp9_alloccommon.c 5.9 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void vp9_update_mode_info_in_image(VP9_COMMON *cm, MODE_INFO *mi) {
  int i, j;

  // For each in image mode_info element set the in image flag to 1
  for (i = 0; i < cm->mi_rows; i++) {
    MODE_INFO *ptr = mi;
    for (j = 0; j < cm->mi_cols; j++) {
      ptr->mbmi.in_image = 1;
      ptr++;  // Next element in the row
    }

    // Step over border element at start of next row
    mi += cm->mode_info_stride;
  }
}

50
void vp9_free_frame_buffers(VP9_COMMON *cm) {
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(&cm->yv12_fb[i]);
55

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

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

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

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

James Zern's avatar
James Zern committed
77
78
  cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
  cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
79
  cm->mode_info_stride = cm->mi_cols + MI_BLOCK_SIZE;
80
81
82
83
84
85
86
87
88
}

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

89
  vp9_update_mode_info_border(cm, cm->mip);
90
91
  vp9_update_mode_info_in_image(cm, cm->mi);

92
  vp9_update_mode_info_border(cm, cm->prev_mip);
93
  vp9_update_mode_info_in_image(cm, cm->prev_mi);
94
95
}

96
int vp9_alloc_frame_buffers(VP9_COMMON *cm, int width, int height) {
97
  int i, mi_cols;
98

James Zern's avatar
James Zern committed
99
100
  const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
  const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
101
102
  const int ss_x = cm->subsampling_x;
  const int ss_y = cm->subsampling_y;
103
  int mi_size;
John Koleszar's avatar
John Koleszar committed
104

105
  vp9_free_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
106

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

114
115
  cm->new_fb_idx = NUM_YV12_BUFFERS - 1;
  cm->fb_idx_ref_cnt[cm->new_fb_idx] = 1;
116

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

120
  for (i = 0; i < NUM_REF_FRAMES; i++) {
121
122
    cm->ref_frame_map[i] = i;
    cm->fb_idx_ref_cnt[i] = 1;
123
  }
John Koleszar's avatar
John Koleszar committed
124

125
  if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
126
127
128
                             VP9BORDERINPIXELS) < 0)
    goto fail;

129
  set_mb_mi(cm, aligned_width, aligned_height);
John Koleszar's avatar
John Koleszar committed
130

131
  // Allocation
132
  mi_size = cm->mode_info_stride * (cm->mi_rows + MI_BLOCK_SIZE);
133

134
135
  cm->mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
  if (!cm->mip)
136
    goto fail;
Paul Wilkins's avatar
Paul Wilkins committed
137

138
139
  cm->prev_mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
  if (!cm->prev_mip)
140
    goto fail;
141

142
  setup_mi(cm);
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(cm->mi_cols);
147

Ronald S. Bultje's avatar
Ronald S. Bultje committed
148
149
  // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
  // block where mi unit size is 8x8.
150
  cm->above_context[0] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * MAX_MB_PLANE *
151
                                         (2 * mi_cols), 1);
152
  if (!cm->above_context[0])
153
154
    goto fail;

155
156
  cm->above_seg_context = vpx_calloc(sizeof(PARTITION_CONTEXT) * mi_cols, 1);
  if (!cm->above_seg_context)
157
    goto fail;
158

159
  // Create the segmentation map structure and set to 0.
160
161
  cm->last_frame_seg_map = vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
  if (!cm->last_frame_seg_map)
162
163
    goto fail;

John Koleszar's avatar
John Koleszar committed
164
  return 0;
165
166

 fail:
167
  vp9_free_frame_buffers(cm);
168
  return 1;
John Koleszar's avatar
John Koleszar committed
169
}
170

171
172
void vp9_create_common(VP9_COMMON *cm) {
  vp9_machine_specific_config(cm);
John Koleszar's avatar
John Koleszar committed
173

174
  vp9_init_mbmode_probs(cm);
John Koleszar's avatar
John Koleszar committed
175

176
177
  cm->tx_mode = ONLY_4X4;
  cm->comp_pred_mode = HYBRID_PREDICTION;
John Koleszar's avatar
John Koleszar committed
178

Dmitry Kovalev's avatar
Dmitry Kovalev committed
179
  // Initialize reference frame sign bias structure to defaults
180
  vpx_memset(cm->ref_frame_sign_bias, 0, sizeof(cm->ref_frame_sign_bias));
John Koleszar's avatar
John Koleszar committed
181
182
}

183
184
void vp9_remove_common(VP9_COMMON *cm) {
  vp9_free_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
185
186
}

187
188
189
190
void vp9_initialize_common() {
  vp9_coef_tree_initialize();
  vp9_entropy_mode_init();
  vp9_entropy_mv_init();
John Koleszar's avatar
John Koleszar committed
191
}
192
193

void vp9_update_frame_size(VP9_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
194
  int i, mi_cols;
James Zern's avatar
James Zern committed
195
196
  const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, MI_SIZE_LOG2);
  const int aligned_height = ALIGN_POWER_OF_TWO(cm->height, MI_SIZE_LOG2);
197

198
199
  set_mb_mi(cm, aligned_width, aligned_height);
  setup_mi(cm);
John Koleszar's avatar
John Koleszar committed
200
201
202
203
204

  mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  for (i = 1; i < MAX_MB_PLANE; i++)
    cm->above_context[i] =
        cm->above_context[0] + i * sizeof(ENTROPY_CONTEXT) * 2 * mi_cols;
205
206
207
208

  // Initialize the previous frame segment map to 0.
  if (cm->last_frame_seg_map)
    vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
209
}