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_alloccommon.h"
15 #include "vp9/common/vp9_blockd.h"
16 #include "vp9/common/vp9_entropymode.h"
17 #include "vp9/common/vp9_entropymv.h"
18 #include "vp9/common/vp9_onyxc_int.h"
19 
20 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
21 // frame reference count.
lock_buffer_pool(BufferPool * const pool)22 void lock_buffer_pool(BufferPool *const pool) {
23 #if CONFIG_MULTITHREAD
24   pthread_mutex_lock(&pool->pool_mutex);
25 #else
26   (void)pool;
27 #endif
28 }
29 
unlock_buffer_pool(BufferPool * const pool)30 void unlock_buffer_pool(BufferPool *const pool) {
31 #if CONFIG_MULTITHREAD
32   pthread_mutex_unlock(&pool->pool_mutex);
33 #else
34   (void)pool;
35 #endif
36 }
37 
vp9_set_mb_mi(VP9_COMMON * cm,int width,int height)38 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
39   const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
40   const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
41 
42   cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
43   cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
44   cm->mi_stride = calc_mi_size(cm->mi_cols);
45 
46   cm->mb_cols = (cm->mi_cols + 1) >> 1;
47   cm->mb_rows = (cm->mi_rows + 1) >> 1;
48   cm->MBs = cm->mb_rows * cm->mb_cols;
49 }
50 
alloc_seg_map(VP9_COMMON * cm,int seg_map_size)51 static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
52   int i;
53 
54   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
55     cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
56     if (cm->seg_map_array[i] == NULL)
57       return 1;
58   }
59   cm->seg_map_alloc_size = seg_map_size;
60 
61   // Init the index.
62   cm->seg_map_idx = 0;
63   cm->prev_seg_map_idx = 1;
64 
65   cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
66   if (!cm->frame_parallel_decode)
67     cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
68 
69   return 0;
70 }
71 
free_seg_map(VP9_COMMON * cm)72 static void free_seg_map(VP9_COMMON *cm) {
73   int i;
74 
75   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
76     vpx_free(cm->seg_map_array[i]);
77     cm->seg_map_array[i] = NULL;
78   }
79 
80   cm->current_frame_seg_map = NULL;
81 
82   if (!cm->frame_parallel_decode) {
83     cm->last_frame_seg_map = NULL;
84   }
85 }
86 
vp9_free_ref_frame_buffers(BufferPool * pool)87 void vp9_free_ref_frame_buffers(BufferPool *pool) {
88   int i;
89 
90   for (i = 0; i < FRAME_BUFFERS; ++i) {
91     if (pool->frame_bufs[i].ref_count > 0 &&
92         pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
93       pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
94       pool->frame_bufs[i].ref_count = 0;
95     }
96     vpx_free(pool->frame_bufs[i].mvs);
97     pool->frame_bufs[i].mvs = NULL;
98     vpx_free_frame_buffer(&pool->frame_bufs[i].buf);
99   }
100 }
101 
vp9_free_postproc_buffers(VP9_COMMON * cm)102 void vp9_free_postproc_buffers(VP9_COMMON *cm) {
103 #if CONFIG_VP9_POSTPROC
104   vpx_free_frame_buffer(&cm->post_proc_buffer);
105   vpx_free_frame_buffer(&cm->post_proc_buffer_int);
106 #else
107   (void)cm;
108 #endif
109 }
110 
vp9_free_context_buffers(VP9_COMMON * cm)111 void vp9_free_context_buffers(VP9_COMMON *cm) {
112   cm->free_mi(cm);
113   free_seg_map(cm);
114   vpx_free(cm->above_context);
115   cm->above_context = NULL;
116   vpx_free(cm->above_seg_context);
117   cm->above_seg_context = NULL;
118   vpx_free(cm->lf.lfm);
119   cm->lf.lfm = NULL;
120 }
121 
122 
vp9_alloc_loop_filter(VP9_COMMON * cm)123 int vp9_alloc_loop_filter(VP9_COMMON *cm) {
124   vpx_free(cm->lf.lfm);
125   // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region.  The
126   // stride and rows are rounded up / truncated to a multiple of 8.
127   cm->lf.lfm_stride = (cm->mi_cols + (MI_BLOCK_SIZE - 1)) >> 3;
128   cm->lf.lfm = (LOOP_FILTER_MASK *)vpx_calloc(
129       ((cm->mi_rows + (MI_BLOCK_SIZE - 1)) >> 3) * cm->lf.lfm_stride,
130       sizeof(*cm->lf.lfm));
131   if (!cm->lf.lfm)
132     return 1;
133   return 0;
134 }
135 
vp9_alloc_context_buffers(VP9_COMMON * cm,int width,int height)136 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
137   int new_mi_size;
138 
139   vp9_set_mb_mi(cm, width, height);
140   new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
141   if (cm->mi_alloc_size < new_mi_size) {
142     cm->free_mi(cm);
143     if (cm->alloc_mi(cm, new_mi_size))
144       goto fail;
145   }
146 
147   if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
148     // Create the segmentation map structure and set to 0.
149     free_seg_map(cm);
150     if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
151       goto fail;
152   }
153 
154   if (cm->above_context_alloc_cols < cm->mi_cols) {
155     vpx_free(cm->above_context);
156     cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
157         2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
158         sizeof(*cm->above_context));
159     if (!cm->above_context) goto fail;
160 
161     vpx_free(cm->above_seg_context);
162     cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
163         mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
164     if (!cm->above_seg_context) goto fail;
165     cm->above_context_alloc_cols = cm->mi_cols;
166   }
167 
168   if (vp9_alloc_loop_filter(cm))
169     goto fail;
170 
171   return 0;
172 
173  fail:
174   vp9_free_context_buffers(cm);
175   return 1;
176 }
177 
vp9_remove_common(VP9_COMMON * cm)178 void vp9_remove_common(VP9_COMMON *cm) {
179   vp9_free_context_buffers(cm);
180 
181   vpx_free(cm->fc);
182   cm->fc = NULL;
183   vpx_free(cm->frame_contexts);
184   cm->frame_contexts = NULL;
185 }
186 
vp9_init_context_buffers(VP9_COMMON * cm)187 void vp9_init_context_buffers(VP9_COMMON *cm) {
188   cm->setup_mi(cm);
189   if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
190     memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
191 }
192 
vp9_swap_current_and_last_seg_map(VP9_COMMON * cm)193 void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
194   // Swap indices.
195   const int tmp = cm->seg_map_idx;
196   cm->seg_map_idx = cm->prev_seg_map_idx;
197   cm->prev_seg_map_idx = tmp;
198 
199   cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
200   cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
201 }
202