• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
119 
vp9_alloc_context_buffers(VP9_COMMON * cm,int width,int height)120 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
121   int new_mi_size;
122 
123   vp9_set_mb_mi(cm, width, height);
124   new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
125   if (cm->mi_alloc_size < new_mi_size) {
126     cm->free_mi(cm);
127     if (cm->alloc_mi(cm, new_mi_size))
128       goto fail;
129   }
130 
131   if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
132     // Create the segmentation map structure and set to 0.
133     free_seg_map(cm);
134     if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
135       goto fail;
136   }
137 
138   if (cm->above_context_alloc_cols < cm->mi_cols) {
139     vpx_free(cm->above_context);
140     cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
141         2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
142         sizeof(*cm->above_context));
143     if (!cm->above_context) goto fail;
144 
145     vpx_free(cm->above_seg_context);
146     cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
147         mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
148     if (!cm->above_seg_context) goto fail;
149     cm->above_context_alloc_cols = cm->mi_cols;
150   }
151 
152   return 0;
153 
154  fail:
155   vp9_free_context_buffers(cm);
156   return 1;
157 }
158 
vp9_remove_common(VP9_COMMON * cm)159 void vp9_remove_common(VP9_COMMON *cm) {
160   vp9_free_context_buffers(cm);
161 
162   vpx_free(cm->fc);
163   cm->fc = NULL;
164   vpx_free(cm->frame_contexts);
165   cm->frame_contexts = NULL;
166 }
167 
vp9_init_context_buffers(VP9_COMMON * cm)168 void vp9_init_context_buffers(VP9_COMMON *cm) {
169   cm->setup_mi(cm);
170   if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
171     memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
172 }
173 
vp9_swap_current_and_last_seg_map(VP9_COMMON * cm)174 void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
175   // Swap indices.
176   const int tmp = cm->seg_map_idx;
177   cm->seg_map_idx = cm->prev_seg_map_idx;
178   cm->prev_seg_map_idx = tmp;
179 
180   cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
181   cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
182 }
183