vp9_alloccommon.c 5.95 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++) {
Paul Wilkins's avatar
Paul Wilkins committed
41
      ptr->mbmi.in_image = 1;
42
      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
  vp9_free_frame_buffer(&oci->post_proc_buffer);
John Koleszar's avatar
John Koleszar committed
57

John Koleszar's avatar
John Koleszar committed
58
59
  vpx_free(oci->mip);
  vpx_free(oci->prev_mip);
60
  vpx_free(oci->above_seg_context);
61
  vpx_free(oci->last_frame_seg_map);
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;
69
  oci->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
89
90
91
92
93
94
95
}

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

96
int vp9_alloc_frame_buffers(VP9_COMMON *oci, 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 = oci->subsampling_x;
  const int ss_y = oci->subsampling_y;
103
  int mi_size;
John Koleszar's avatar
John Koleszar committed
104

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

John Koleszar's avatar
John Koleszar committed
107
108
  for (i = 0; i < NUM_YV12_BUFFERS; i++) {
    oci->fb_idx_ref_cnt[i] = 0;
109
110
111
    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
112
  }
John Koleszar's avatar
John Koleszar committed
113

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

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

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

125
126
127
128
129
  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
130

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

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

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

142
  setup_mi(oci);
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->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
151
  oci->above_context[0] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * MAX_MB_PLANE *
                                         (2 * mi_cols), 1);
152
153
154
155
156
157
  if (!oci->above_context[0])
    goto fail;

  oci->above_seg_context = vpx_calloc(sizeof(PARTITION_CONTEXT) * mi_cols, 1);
  if (!oci->above_seg_context)
    goto fail;
158

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

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

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

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

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

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

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

183
void vp9_remove_common(VP9_COMMON *oci) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
184
  vp9_free_frame_buffers(oci);
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
}