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

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

20
static void clear_mi_border(const VP9_COMMON *cm, MODE_INFO *mi) {
John Koleszar's avatar
John Koleszar committed
21
22
  int i;

23
  // Top border row
24
  vpx_memset(mi, 0, sizeof(*mi) * cm->mi_stride);
John Koleszar's avatar
John Koleszar committed
25

26
27
  // Left border column
  for (i = 1; i < cm->mi_rows + 1; ++i)
28
    vpx_memset(&mi[i * cm->mi_stride], 0, sizeof(*mi));
29
}
30

Adrian Grange's avatar
Adrian Grange committed
31
32
33
34
void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
  const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
  const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);

James Zern's avatar
James Zern committed
35
36
  cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
  cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
37
  cm->mi_stride = cm->mi_cols + MI_BLOCK_SIZE;
38
39
40
41

  cm->mb_cols = (cm->mi_cols + 1) >> 1;
  cm->mb_rows = (cm->mi_rows + 1) >> 1;
  cm->MBs = cm->mb_rows * cm->mb_cols;
42
43
44
}

static void setup_mi(VP9_COMMON *cm) {
45
46
47
48
  cm->mi = cm->mip + cm->mi_stride + 1;
  cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
  cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
49

50
  vpx_memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
51

52
53
  vpx_memset(cm->mi_grid_base, 0, cm->mi_stride * (cm->mi_rows + 1) *
                                      sizeof(*cm->mi_grid_base));
54

55
  clear_mi_border(cm, cm->prev_mip);
56
57
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
58
static int alloc_mi(VP9_COMMON *cm, int mi_size) {
59
60
61
62
63
64
65
  int i;

  for (i = 0; i < 2; ++i) {
    cm->mip_array[i] =
        (MODE_INFO *)vpx_calloc(mi_size, sizeof(*cm->mip));
    if (cm->mip_array[i] == NULL)
      return 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
66

67
68
69
70
71
    cm->mi_grid_base_array[i] =
        (MODE_INFO **)vpx_calloc(mi_size, sizeof(*cm->mi_grid_base));
    if (cm->mi_grid_base_array[i] == NULL)
      return 1;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
72

73
74
75
  // Init the index.
  cm->mi_idx = 0;
  cm->prev_mi_idx = 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
76

77
78
79
80
  cm->mip = cm->mip_array[cm->mi_idx];
  cm->prev_mip = cm->mip_array[cm->prev_mi_idx];
  cm->mi_grid_base = cm->mi_grid_base_array[cm->mi_idx];
  cm->prev_mi_grid_base = cm->mi_grid_base_array[cm->prev_mi_idx];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
81
82
83
84
85

  return 0;
}

static void free_mi(VP9_COMMON *cm) {
86
87
88
89
90
91
92
93
  int i;

  for (i = 0; i < 2; ++i) {
    vpx_free(cm->mip_array[i]);
    cm->mip_array[i] = NULL;
    vpx_free(cm->mi_grid_base_array[i]);
    cm->mi_grid_base_array[i] = NULL;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
94
95
96
97
98
99
100

  cm->mip = NULL;
  cm->prev_mip = NULL;
  cm->mi_grid_base = NULL;
  cm->prev_mi_grid_base = NULL;
}

Adrian Grange's avatar
Adrian Grange committed
101
void vp9_free_ref_frame_buffers(VP9_COMMON *cm) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
102
103
104
105
106
107
108
109
110
111
112
113
114
  int i;

  for (i = 0; i < FRAME_BUFFERS; ++i) {
    vp9_free_frame_buffer(&cm->frame_bufs[i].buf);

    if (cm->frame_bufs[i].ref_count > 0 &&
        cm->frame_bufs[i].raw_frame_buffer.data != NULL) {
      cm->release_fb_cb(cm->cb_priv, &cm->frame_bufs[i].raw_frame_buffer);
      cm->frame_bufs[i].ref_count = 0;
    }
  }

  vp9_free_frame_buffer(&cm->post_proc_buffer);
115
}
Dmitry Kovalev's avatar
Dmitry Kovalev committed
116

117
void vp9_free_context_buffers(VP9_COMMON *cm) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
118
119
120
121
  free_mi(cm);

  vpx_free(cm->last_frame_seg_map);
  cm->last_frame_seg_map = NULL;
122

123
124
125
  vpx_free(cm->above_context);
  cm->above_context = NULL;

126
127
  vpx_free(cm->above_seg_context);
  cm->above_seg_context = NULL;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
128
129
}

Adrian Grange's avatar
Adrian Grange committed
130
131
int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
  vp9_free_context_buffers(cm);
132

Adrian Grange's avatar
Adrian Grange committed
133
134
  vp9_set_mb_mi(cm, width, height);
  if (alloc_mi(cm, cm->mi_stride * (cm->mi_rows + MI_BLOCK_SIZE))) goto fail;
135

136
  cm->last_frame_seg_map = (uint8_t *)vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
Adrian Grange's avatar
Adrian Grange committed
137
  if (!cm->last_frame_seg_map) goto fail;
138

Adrian Grange's avatar
Adrian Grange committed
139
140
141
142
  cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
      2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
      sizeof(*cm->above_context));
  if (!cm->above_context) goto fail;
143

Adrian Grange's avatar
Adrian Grange committed
144
145
146
  cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
      mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
  if (!cm->above_seg_context) goto fail;
147

148
149
150
  return 0;

 fail:
151
  vp9_free_context_buffers(cm);
152
153
154
  return 1;
}

155
156
157
158
159
160
161
162
163
164
165
166
static void init_frame_bufs(VP9_COMMON *cm) {
  int i;

  cm->new_fb_idx = FRAME_BUFFERS - 1;
  cm->frame_bufs[cm->new_fb_idx].ref_count = 1;

  for (i = 0; i < REF_FRAMES; ++i) {
    cm->ref_frame_map[i] = i;
    cm->frame_bufs[i].ref_count = 1;
  }
}

Adrian Grange's avatar
Adrian Grange committed
167
int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) {
168
  int i;
169
170
  const int ss_x = cm->subsampling_x;
  const int ss_y = cm->subsampling_y;
John Koleszar's avatar
John Koleszar committed
171

Adrian Grange's avatar
Adrian Grange committed
172
  vp9_free_ref_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
173

174
  for (i = 0; i < FRAME_BUFFERS; ++i) {
175
176
177
    cm->frame_bufs[i].ref_count = 0;
    if (vp9_alloc_frame_buffer(&cm->frame_bufs[i].buf, width, height,
                               ss_x, ss_y, VP9_ENC_BORDER_IN_PIXELS) < 0)
178
      goto fail;
John Koleszar's avatar
John Koleszar committed
179
  }
John Koleszar's avatar
John Koleszar committed
180

181
  init_frame_bufs(cm);
John Koleszar's avatar
John Koleszar committed
182

183
#if CONFIG_INTERNAL_STATS || CONFIG_VP9_POSTPROC
184
  if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
185
                             VP9_ENC_BORDER_IN_PIXELS) < 0)
186
    goto fail;
187
#endif
188

189
190
191
  return 0;

 fail:
Adrian Grange's avatar
Adrian Grange committed
192
  vp9_free_ref_frame_buffers(cm);
193
  return 1;
John Koleszar's avatar
John Koleszar committed
194
}
195

196
void vp9_remove_common(VP9_COMMON *cm) {
Adrian Grange's avatar
Adrian Grange committed
197
  vp9_free_ref_frame_buffers(cm);
198
  vp9_free_context_buffers(cm);
199
  vp9_free_internal_frame_buffers(&cm->int_frame_buffers);
John Koleszar's avatar
John Koleszar committed
200
}
201

Adrian Grange's avatar
Adrian Grange committed
202
void vp9_init_context_buffers(VP9_COMMON *cm) {
203
  setup_mi(cm);
204
205
  if (cm->last_frame_seg_map)
    vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
206
}
207
208

void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
209
210
211
212
213
  // Swap indices.
  const int tmp = cm->mi_idx;
  cm->mi_idx = cm->prev_mi_idx;
  cm->prev_mi_idx = tmp;

214
  // Current mip will be the prev_mip for the next frame.
215
216
217
218
  cm->mip = cm->mip_array[cm->mi_idx];
  cm->prev_mip = cm->mip_array[cm->prev_mi_idx];
  cm->mi_grid_base = cm->mi_grid_base_array[cm->mi_idx];
  cm->prev_mi_grid_base = cm->mi_grid_base_array[cm->prev_mi_idx];
219
220

  // Update the upper left visible macroblock ptrs.
221
222
223
224
  cm->mi = cm->mip + cm->mi_stride + 1;
  cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
  cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
225
}