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) return 1;
57   }
58   cm->seg_map_alloc_size = seg_map_size;
59 
60   // Init the index.
61   cm->seg_map_idx = 0;
62   cm->prev_seg_map_idx = 1;
63 
64   cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
65   if (!cm->frame_parallel_decode)
66     cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
67 
68   return 0;
69 }
70 
free_seg_map(VP9_COMMON * cm)71 static void free_seg_map(VP9_COMMON *cm) {
72   int i;
73 
74   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
75     vpx_free(cm->seg_map_array[i]);
76     cm->seg_map_array[i] = NULL;
77   }
78 
79   cm->current_frame_seg_map = NULL;
80 
81   if (!cm->frame_parallel_decode) {
82     cm->last_frame_seg_map = NULL;
83   }
84 }
85 
vp9_free_ref_frame_buffers(BufferPool * pool)86 void vp9_free_ref_frame_buffers(BufferPool *pool) {
87   int i;
88 
89   for (i = 0; i < FRAME_BUFFERS; ++i) {
90     if (pool->frame_bufs[i].ref_count > 0 &&
91         pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
92       pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
93       pool->frame_bufs[i].ref_count = 0;
94     }
95     vpx_free(pool->frame_bufs[i].mvs);
96     pool->frame_bufs[i].mvs = NULL;
97     vpx_free_frame_buffer(&pool->frame_bufs[i].buf);
98   }
99 }
100 
vp9_free_postproc_buffers(VP9_COMMON * cm)101 void vp9_free_postproc_buffers(VP9_COMMON *cm) {
102 #if CONFIG_VP9_POSTPROC
103   vpx_free_frame_buffer(&cm->post_proc_buffer);
104   vpx_free_frame_buffer(&cm->post_proc_buffer_int);
105   vpx_free(cm->postproc_state.limits);
106   cm->postproc_state.limits = NULL;
107   vpx_free(cm->postproc_state.generated_noise);
108   cm->postproc_state.generated_noise = NULL;
109 #else
110   (void)cm;
111 #endif
112 }
113 
vp9_free_context_buffers(VP9_COMMON * cm)114 void vp9_free_context_buffers(VP9_COMMON *cm) {
115   cm->free_mi(cm);
116   free_seg_map(cm);
117   vpx_free(cm->above_context);
118   cm->above_context = NULL;
119   vpx_free(cm->above_seg_context);
120   cm->above_seg_context = NULL;
121   vpx_free(cm->lf.lfm);
122   cm->lf.lfm = NULL;
123 }
124 
vp9_alloc_loop_filter(VP9_COMMON * cm)125 int vp9_alloc_loop_filter(VP9_COMMON *cm) {
126   vpx_free(cm->lf.lfm);
127   // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region.  The
128   // stride and rows are rounded up / truncated to a multiple of 8.
129   cm->lf.lfm_stride = (cm->mi_cols + (MI_BLOCK_SIZE - 1)) >> 3;
130   cm->lf.lfm = (LOOP_FILTER_MASK *)vpx_calloc(
131       ((cm->mi_rows + (MI_BLOCK_SIZE - 1)) >> 3) * cm->lf.lfm_stride,
132       sizeof(*cm->lf.lfm));
133   if (!cm->lf.lfm) return 1;
134   return 0;
135 }
136 
vp9_alloc_context_buffers(VP9_COMMON * cm,int width,int height)137 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
138   int new_mi_size;
139 
140   vp9_set_mb_mi(cm, width, height);
141   new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
142   if (cm->mi_alloc_size < new_mi_size) {
143     cm->free_mi(cm);
144     if (cm->alloc_mi(cm, new_mi_size)) 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)) goto fail;
151   }
152 
153   if (cm->above_context_alloc_cols < cm->mi_cols) {
154     vpx_free(cm->above_context);
155     cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
156         2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
157         sizeof(*cm->above_context));
158     if (!cm->above_context) goto fail;
159 
160     vpx_free(cm->above_seg_context);
161     cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
162         mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
163     if (!cm->above_seg_context) goto fail;
164     cm->above_context_alloc_cols = cm->mi_cols;
165   }
166 
167   if (vp9_alloc_loop_filter(cm)) goto fail;
168 
169   return 0;
170 
171 fail:
172   // clear the mi_* values to force a realloc on resync
173   vp9_set_mb_mi(cm, 0, 0);
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