vp9_alloccommon.c 5.61 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
  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);
John Koleszar's avatar
John Koleszar committed
61

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

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

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

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

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

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

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

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

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

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

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

123
124
125
126
127
  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
128

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

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

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

140
  setup_mi(oci);
John Koleszar's avatar
John Koleszar committed
141

142
143
  // FIXME(jkoleszar): allocate subsampled arrays for U/V once subsampling
  // information is exposed at this level
144
  mi_cols = mi_cols_aligned_to_sb(oci->mi_cols);
145

Ronald S. Bultje's avatar
Ronald S. Bultje committed
146
147
  // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
  // block where mi unit size is 8x8.
148
149
150
# if CONFIG_ALPHA
  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
154
155
156
157
158
  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;
159

John Koleszar's avatar
John Koleszar committed
160
  return 0;
161
162
163
164

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

167
void vp9_create_common(VP9_COMMON *oci) {
168
  vp9_machine_specific_config(oci);
John Koleszar's avatar
John Koleszar committed
169

170
  vp9_init_mbmode_probs(oci);
John Koleszar's avatar
John Koleszar committed
171

172
  oci->tx_mode = ONLY_4X4;
John Koleszar's avatar
John Koleszar committed
173
174
  oci->comp_pred_mode = HYBRID_PREDICTION;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
175
  // Initialize reference frame sign bias structure to defaults
John Koleszar's avatar
John Koleszar committed
176
  vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
John Koleszar's avatar
John Koleszar committed
177
178
}

179
void vp9_remove_common(VP9_COMMON *oci) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
180
  vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
181
182
}

183
184
185
186
void vp9_initialize_common() {
  vp9_coef_tree_initialize();
  vp9_entropy_mode_init();
  vp9_entropy_mv_init();
John Koleszar's avatar
John Koleszar committed
187
}
188
189

void vp9_update_frame_size(VP9_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
190
  int i, mi_cols;
191
192
  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);
193

194
195
  set_mb_mi(cm, aligned_width, aligned_height);
  setup_mi(cm);
John Koleszar's avatar
John Koleszar committed
196
197
198
199
200

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