1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include "./vpx_config.h"
12 #include "vpx_mem/vpx_mem.h"
13 
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"
19 
clear_mi_border(const VP9_COMMON * cm,MODE_INFO * mi)20 static void clear_mi_border(const VP9_COMMON *cm, MODE_INFO *mi) {
21   int i;
22 
23   // Top border row
24   vpx_memset(mi, 0, sizeof(*mi) * cm->mi_stride);
25 
26   // Left border column
27   for (i = 1; i < cm->mi_rows + 1; ++i)
28     vpx_memset(&mi[i * cm->mi_stride], 0, sizeof(*mi));
29 }
30 
vp9_set_mb_mi(VP9_COMMON * cm,int width,int height)31 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
32   const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
33   const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
34 
35   cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
36   cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
37   cm->mi_stride = cm->mi_cols + MI_BLOCK_SIZE;
38 
39   cm->mb_cols = (cm->mi_cols + 1) >> 1;
40   cm->mb_rows = (cm->mi_rows + 1) >> 1;
41   cm->MBs = cm->mb_rows * cm->mb_cols;
42 }
43 
setup_mi(VP9_COMMON * cm)44 static void setup_mi(VP9_COMMON *cm) {
45   cm->mi = cm->mip + cm->mi_stride + 1;
46   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
47   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
48   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   vpx_memset(cm->mi_grid_base, 0, cm->mi_stride * (cm->mi_rows + 1) *
53                                       sizeof(*cm->mi_grid_base));
54 
55   clear_mi_border(cm, cm->prev_mip);
56 }
57 
alloc_mi(VP9_COMMON * cm,int mi_size)58 static int alloc_mi(VP9_COMMON *cm, int mi_size) {
59   int i;
60 
61   for (i = 0; i < 2; ++i) {
62     cm->mip_array[i] =
63         (MODE_INFO *)vpx_calloc(mi_size, sizeof(*cm->mip));
64     if (cm->mip_array[i] == NULL)
65       return 1;
66 
67     cm->mi_grid_base_array[i] =
68         (MODE_INFO **)vpx_calloc(mi_size, sizeof(*cm->mi_grid_base));
69     if (cm->mi_grid_base_array[i] == NULL)
70       return 1;
71   }
72 
73   // Init the index.
74   cm->mi_idx = 0;
75   cm->prev_mi_idx = 1;
76 
77   cm->mip = cm->mip_array[cm->mi_idx];
78   cm->prev_mip = cm->mip_array[cm->prev_mi_idx];
79   cm->mi_grid_base = cm->mi_grid_base_array[cm->mi_idx];
80   cm->prev_mi_grid_base = cm->mi_grid_base_array[cm->prev_mi_idx];
81 
82   return 0;
83 }
84 
free_mi(VP9_COMMON * cm)85 static void free_mi(VP9_COMMON *cm) {
86   int i;
87 
88   for (i = 0; i < 2; ++i) {
89     vpx_free(cm->mip_array[i]);
90     cm->mip_array[i] = NULL;
91     vpx_free(cm->mi_grid_base_array[i]);
92     cm->mi_grid_base_array[i] = NULL;
93   }
94 
95   cm->mip = NULL;
96   cm->prev_mip = NULL;
97   cm->mi_grid_base = NULL;
98   cm->prev_mi_grid_base = NULL;
99 }
100 
vp9_free_ref_frame_buffers(VP9_COMMON * cm)101 void vp9_free_ref_frame_buffers(VP9_COMMON *cm) {
102   int i;
103 
104   for (i = 0; i < FRAME_BUFFERS; ++i) {
105     vp9_free_frame_buffer(&cm->frame_bufs[i].buf);
106 
107     if (cm->frame_bufs[i].ref_count > 0 &&
108         cm->frame_bufs[i].raw_frame_buffer.data != NULL) {
109       cm->release_fb_cb(cm->cb_priv, &cm->frame_bufs[i].raw_frame_buffer);
110       cm->frame_bufs[i].ref_count = 0;
111     }
112   }
113 
114   vp9_free_frame_buffer(&cm->post_proc_buffer);
115 }
116 
vp9_free_context_buffers(VP9_COMMON * cm)117 void vp9_free_context_buffers(VP9_COMMON *cm) {
118   free_mi(cm);
119 
120   vpx_free(cm->last_frame_seg_map);
121   cm->last_frame_seg_map = NULL;
122 
123   vpx_free(cm->above_context);
124   cm->above_context = NULL;
125 
126   vpx_free(cm->above_seg_context);
127   cm->above_seg_context = NULL;
128 }
129 
vp9_alloc_context_buffers(VP9_COMMON * cm,int width,int height)130 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
131   vp9_free_context_buffers(cm);
132 
133   vp9_set_mb_mi(cm, width, height);
134   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);
137   if (!cm->last_frame_seg_map) goto fail;
138 
139   cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
140       2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
141       sizeof(*cm->above_context));
142   if (!cm->above_context) goto fail;
143 
144   cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
145       mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
146   if (!cm->above_seg_context) goto fail;
147 
148   return 0;
149 
150  fail:
151   vp9_free_context_buffers(cm);
152   return 1;
153 }
154 
init_frame_bufs(VP9_COMMON * cm)155 static void init_frame_bufs(VP9_COMMON *cm) {
156   int i;
157 
158   cm->new_fb_idx = FRAME_BUFFERS - 1;
159   cm->frame_bufs[cm->new_fb_idx].ref_count = 1;
160 
161   for (i = 0; i < REF_FRAMES; ++i) {
162     cm->ref_frame_map[i] = i;
163     cm->frame_bufs[i].ref_count = 1;
164   }
165 }
166 
vp9_alloc_ref_frame_buffers(VP9_COMMON * cm,int width,int height)167 int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) {
168   int i;
169   const int ss_x = cm->subsampling_x;
170   const int ss_y = cm->subsampling_y;
171 
172   vp9_free_ref_frame_buffers(cm);
173 
174   for (i = 0; i < FRAME_BUFFERS; ++i) {
175     cm->frame_bufs[i].ref_count = 0;
176     if (vp9_alloc_frame_buffer(&cm->frame_bufs[i].buf, width, height,
177                                ss_x, ss_y, VP9_ENC_BORDER_IN_PIXELS) < 0)
178       goto fail;
179   }
180 
181   init_frame_bufs(cm);
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   return 0;
190 
191  fail:
192   vp9_free_ref_frame_buffers(cm);
193   return 1;
194 }
195 
vp9_remove_common(VP9_COMMON * cm)196 void vp9_remove_common(VP9_COMMON *cm) {
197   vp9_free_ref_frame_buffers(cm);
198   vp9_free_context_buffers(cm);
199   vp9_free_internal_frame_buffers(&cm->int_frame_buffers);
200 }
201 
vp9_init_context_buffers(VP9_COMMON * cm)202 void vp9_init_context_buffers(VP9_COMMON *cm) {
203   setup_mi(cm);
204   if (cm->last_frame_seg_map)
205     vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
206 }
207 
vp9_swap_mi_and_prev_mi(VP9_COMMON * cm)208 void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
209   // Swap indices.
210   const int tmp = cm->mi_idx;
211   cm->mi_idx = cm->prev_mi_idx;
212   cm->prev_mi_idx = tmp;
213 
214   // Current mip will be the prev_mip for the next frame.
215   cm->mip = cm->mip_array[cm->mi_idx];
216   cm->prev_mip = cm->mip_array[cm->prev_mi_idx];
217   cm->mi_grid_base = cm->mi_grid_base_array[cm->mi_idx];
218   cm->prev_mi_grid_base = cm->mi_grid_base_array[cm->prev_mi_idx];
219 
220   // Update the upper left visible macroblock ptrs.
221   cm->mi = cm->mip + cm->mi_stride + 1;
222   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
223   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
224   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
225 }
226