vp9_alloccommon.c 6.29 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// TODO(hkuang): Don't need to lock the whole pool after implementing atomic
// frame reference count.
void lock_buffer_pool(BufferPool *const pool) {
#if CONFIG_MULTITHREAD
  pthread_mutex_lock(&pool->pool_mutex);
#else
  (void)pool;
#endif
}

void unlock_buffer_pool(BufferPool *const pool) {
#if CONFIG_MULTITHREAD
  pthread_mutex_unlock(&pool->pool_mutex);
#else
  (void)pool;
#endif
}

Adrian Grange's avatar
Adrian Grange committed
38 39 40 41
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
42 43
  cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
  cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
44
  cm->mi_stride = calc_mi_size(cm->mi_cols);
45 46 47 48

  cm->mb_cols = (cm->mi_cols + 1) >> 1;
  cm->mb_rows = (cm->mi_rows + 1) >> 1;
  cm->MBs = cm->mb_rows * cm->mb_cols;
49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
  int i;

  for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
    cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
    if (cm->seg_map_array[i] == NULL)
      return 1;
  }

  // Init the index.
  cm->seg_map_idx = 0;
  cm->prev_seg_map_idx = 1;

  cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
  if (!cm->frame_parallel_decode)
    cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];

  return 0;
}

static void free_seg_map(VP9_COMMON *cm) {
  int i;

  for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
    vpx_free(cm->seg_map_array[i]);
    cm->seg_map_array[i] = NULL;
  }

  cm->current_frame_seg_map = NULL;

  if (!cm->frame_parallel_decode) {
    cm->last_frame_seg_map = NULL;
  }
}

Adrian Grange's avatar
Adrian Grange committed
86
void vp9_free_ref_frame_buffers(VP9_COMMON *cm) {
87
  BufferPool *const pool = cm->buffer_pool;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
88 89 90
  int i;

  for (i = 0; i < FRAME_BUFFERS; ++i) {
91 92 93 94
    if (pool->frame_bufs[i].ref_count > 0 &&
        pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
      pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
      pool->frame_bufs[i].ref_count = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
95
    }
96 97 98
    vpx_free(pool->frame_bufs[i].mvs);
    pool->frame_bufs[i].mvs = NULL;
    vp9_free_frame_buffer(&pool->frame_bufs[i].buf);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
99 100
  }

101
#if CONFIG_VP9_POSTPROC
Dmitry Kovalev's avatar
Dmitry Kovalev committed
102
  vp9_free_frame_buffer(&cm->post_proc_buffer);
103
  vp9_free_frame_buffer(&cm->post_proc_buffer_int);
104
#endif
105
}
Dmitry Kovalev's avatar
Dmitry Kovalev committed
106

107
void vp9_free_context_buffers(VP9_COMMON *cm) {
108
  cm->free_mi(cm);
109
  free_seg_map(cm);
110 111
  vpx_free(cm->above_context);
  cm->above_context = NULL;
112 113
  vpx_free(cm->above_seg_context);
  cm->above_seg_context = NULL;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
114 115
}

Adrian Grange's avatar
Adrian Grange committed
116 117
int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
  vp9_free_context_buffers(cm);
118

Adrian Grange's avatar
Adrian Grange committed
119
  vp9_set_mb_mi(cm, width, height);
120
  if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows)))
121
    goto fail;
122

123 124 125 126
  // Create the segmentation map structure and set to 0.
  free_seg_map(cm);
  if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
    goto fail;
127

Adrian Grange's avatar
Adrian Grange committed
128 129 130 131
  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;
132

Adrian Grange's avatar
Adrian Grange committed
133 134 135
  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;
136

137 138 139
  return 0;

 fail:
140
  vp9_free_context_buffers(cm);
141 142 143
  return 1;
}

144
static void init_frame_bufs(VP9_COMMON *cm) {
145
  BufferPool *const pool = cm->buffer_pool;
146 147 148
  int i;

  cm->new_fb_idx = FRAME_BUFFERS - 1;
149
  pool->frame_bufs[cm->new_fb_idx].ref_count = 1;
150 151 152

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

Adrian Grange's avatar
Adrian Grange committed
157
int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) {
158
  int i;
159 160
  const int ss_x = cm->subsampling_x;
  const int ss_y = cm->subsampling_y;
John Koleszar's avatar
John Koleszar committed
161

Adrian Grange's avatar
Adrian Grange committed
162
  vp9_free_ref_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
163

164
  for (i = 0; i < FRAME_BUFFERS; ++i) {
165 166 167
    BufferPool *const pool = cm->buffer_pool;
    pool->frame_bufs[i].ref_count = 0;
    if (vp9_alloc_frame_buffer(&pool->frame_bufs[i].buf, width, height,
168 169 170 171
                               ss_x, ss_y,
#if CONFIG_VP9_HIGHBITDEPTH
                               cm->use_highbitdepth,
#endif
172 173
                               VP9_ENC_BORDER_IN_PIXELS,
                               cm->byte_alignment) < 0)
174
      goto fail;
175 176
    if (pool->frame_bufs[i].mvs == NULL) {
      pool->frame_bufs[i].mvs =
177
          (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
178 179
                               sizeof(*pool->frame_bufs[i].mvs));
      if (pool->frame_bufs[i].mvs == NULL)
180 181
        goto fail;

182 183
      pool->frame_bufs[i].mi_rows = cm->mi_rows;
      pool->frame_bufs[i].mi_cols = cm->mi_cols;
184
    }
John Koleszar's avatar
John Koleszar committed
185
  }
John Koleszar's avatar
John Koleszar committed
186

187
  init_frame_bufs(cm);
John Koleszar's avatar
John Koleszar committed
188

189
#if CONFIG_VP9_POSTPROC
190
  if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
191 192 193
#if CONFIG_VP9_HIGHBITDEPTH
                             cm->use_highbitdepth,
#endif
194 195
                             VP9_ENC_BORDER_IN_PIXELS,
                             cm->byte_alignment) < 0)
196
    goto fail;
197
#endif
198

199 200 201
  return 0;

 fail:
Adrian Grange's avatar
Adrian Grange committed
202
  vp9_free_ref_frame_buffers(cm);
203
  return 1;
John Koleszar's avatar
John Koleszar committed
204
}
205

206
void vp9_remove_common(VP9_COMMON *cm) {
Adrian Grange's avatar
Adrian Grange committed
207
  vp9_free_ref_frame_buffers(cm);
208
  vp9_free_context_buffers(cm);
209 210 211 212 213

  vpx_free(cm->fc);
  cm->fc = NULL;
  vpx_free(cm->frame_contexts);
  cm->frame_contexts = NULL;
John Koleszar's avatar
John Koleszar committed
214
}
215

Adrian Grange's avatar
Adrian Grange committed
216
void vp9_init_context_buffers(VP9_COMMON *cm) {
217
  cm->setup_mi(cm);
218
  if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
219
    vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
220
}
221 222 223 224 225 226 227 228 229 230

void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
  // Swap indices.
  const int tmp = cm->seg_map_idx;
  cm->seg_map_idx = cm->prev_seg_map_idx;
  cm->prev_seg_map_idx = tmp;

  cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
  cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
}