2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * 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.
11 #include "./vpx_config.h"
12 #include "vpx_mem/vpx_mem.h"
14 #include "vp9/common/vp9_blockd.h"
15 #include "vp9/common/vp9_entropymode.h"
16 #include "vp9/common/vp9_entropymv.h"
17 #include "vp9/common/vp9_onyxc_int.h"
18 #include "vp9/common/vp9_systemdependent.h"
20 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
21 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
22 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
24 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
25 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
26 cm->mi_stride = calc_mi_size(cm->mi_cols);
28 cm->mb_cols = (cm->mi_cols + 1) >> 1;
29 cm->mb_rows = (cm->mi_rows + 1) >> 1;
30 cm->MBs = cm->mb_rows * cm->mb_cols;
33 void vp9_free_ref_frame_buffers(VP9_COMMON *cm) {
36 for (i = 0; i < FRAME_BUFFERS; ++i) {
37 if (cm->frame_bufs[i].ref_count > 0 &&
38 cm->frame_bufs[i].raw_frame_buffer.data != NULL) {
39 cm->release_fb_cb(cm->cb_priv, &cm->frame_bufs[i].raw_frame_buffer);
40 cm->frame_bufs[i].ref_count = 0;
42 vpx_free(cm->frame_bufs[i].mvs);
43 cm->frame_bufs[i].mvs = NULL;
44 vp9_free_frame_buffer(&cm->frame_bufs[i].buf);
47 #if CONFIG_VP9_POSTPROC
48 vp9_free_frame_buffer(&cm->post_proc_buffer);
49 vp9_free_frame_buffer(&cm->post_proc_buffer_int);
53 void vp9_free_context_buffers(VP9_COMMON *cm) {
55 vpx_free(cm->last_frame_seg_map);
56 cm->last_frame_seg_map = NULL;
57 vpx_free(cm->above_context);
58 cm->above_context = NULL;
59 vpx_free(cm->above_seg_context);
60 cm->above_seg_context = NULL;
63 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
64 vp9_free_context_buffers(cm);
66 vp9_set_mb_mi(cm, width, height);
67 if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows)))
70 cm->last_frame_seg_map = (uint8_t *)vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
71 if (!cm->last_frame_seg_map) goto fail;
73 cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
74 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
75 sizeof(*cm->above_context));
76 if (!cm->above_context) goto fail;
78 cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
79 mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
80 if (!cm->above_seg_context) goto fail;
85 vp9_free_context_buffers(cm);
89 static void init_frame_bufs(VP9_COMMON *cm) {
92 cm->new_fb_idx = FRAME_BUFFERS - 1;
93 cm->frame_bufs[cm->new_fb_idx].ref_count = 1;
95 for (i = 0; i < REF_FRAMES; ++i) {
96 cm->ref_frame_map[i] = i;
97 cm->frame_bufs[i].ref_count = 1;
101 int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) {
103 const int ss_x = cm->subsampling_x;
104 const int ss_y = cm->subsampling_y;
106 vp9_free_ref_frame_buffers(cm);
108 for (i = 0; i < FRAME_BUFFERS; ++i) {
109 cm->frame_bufs[i].ref_count = 0;
110 if (vp9_alloc_frame_buffer(&cm->frame_bufs[i].buf, width, height,
112 #if CONFIG_VP9_HIGHBITDEPTH
113 cm->use_highbitdepth,
115 VP9_ENC_BORDER_IN_PIXELS,
116 cm->byte_alignment) < 0)
118 if (cm->frame_bufs[i].mvs == NULL) {
119 cm->frame_bufs[i].mvs =
120 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
121 sizeof(*cm->frame_bufs[i].mvs));
122 if (cm->frame_bufs[i].mvs == NULL)
125 cm->frame_bufs[i].mi_rows = cm->mi_rows;
126 cm->frame_bufs[i].mi_cols = cm->mi_cols;
132 #if CONFIG_VP9_POSTPROC
133 if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
134 #if CONFIG_VP9_HIGHBITDEPTH
135 cm->use_highbitdepth,
137 VP9_ENC_BORDER_IN_PIXELS,
138 cm->byte_alignment) < 0)
145 vp9_free_ref_frame_buffers(cm);
149 void vp9_remove_common(VP9_COMMON *cm) {
150 vp9_free_ref_frame_buffers(cm);
151 vp9_free_context_buffers(cm);
152 vp9_free_internal_frame_buffers(&cm->int_frame_buffers);
156 vpx_free(cm->frame_contexts);
157 cm->frame_contexts = NULL;
160 void vp9_init_context_buffers(VP9_COMMON *cm) {
162 if (cm->last_frame_seg_map)
163 vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);