Merge "Fix high bit depth in vp10 codebase"
[platform/upstream/libvpx.git] / vp10 / common / vp9_alloccommon.c
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 "vp10/common/vp9_alloccommon.h"
15 #include "vp10/common/vp9_blockd.h"
16 #include "vp10/common/vp9_entropymode.h"
17 #include "vp10/common/vp9_entropymv.h"
18 #include "vp10/common/vp9_onyxc_int.h"
19 #include "vp10/common/vp9_systemdependent.h"
20
21 void vp10_set_mb_mi(VP9_COMMON *cm, int width, int height) {
22   const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
23   const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
24
25   cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
26   cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
27   cm->mi_stride = calc_mi_size(cm->mi_cols);
28
29   cm->mb_cols = (cm->mi_cols + 1) >> 1;
30   cm->mb_rows = (cm->mi_rows + 1) >> 1;
31   cm->MBs = cm->mb_rows * cm->mb_cols;
32 }
33
34 static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
35   int i;
36
37   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
38     cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
39     if (cm->seg_map_array[i] == NULL)
40       return 1;
41   }
42   cm->seg_map_alloc_size = seg_map_size;
43
44   // Init the index.
45   cm->seg_map_idx = 0;
46   cm->prev_seg_map_idx = 1;
47
48   cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
49   if (!cm->frame_parallel_decode)
50     cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
51
52   return 0;
53 }
54
55 static void free_seg_map(VP9_COMMON *cm) {
56   int i;
57
58   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
59     vpx_free(cm->seg_map_array[i]);
60     cm->seg_map_array[i] = NULL;
61   }
62
63   cm->current_frame_seg_map = NULL;
64
65   if (!cm->frame_parallel_decode) {
66     cm->last_frame_seg_map = NULL;
67   }
68 }
69
70 void vp10_free_ref_frame_buffers(BufferPool *pool) {
71   int i;
72
73   for (i = 0; i < FRAME_BUFFERS; ++i) {
74     if (pool->frame_bufs[i].ref_count > 0 &&
75         pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
76       pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
77       pool->frame_bufs[i].ref_count = 0;
78     }
79     vpx_free(pool->frame_bufs[i].mvs);
80     pool->frame_bufs[i].mvs = NULL;
81     vp9_free_frame_buffer(&pool->frame_bufs[i].buf);
82   }
83 }
84
85 void vp10_free_postproc_buffers(VP9_COMMON *cm) {
86 #if CONFIG_VP9_POSTPROC
87   vp9_free_frame_buffer(&cm->post_proc_buffer);
88   vp9_free_frame_buffer(&cm->post_proc_buffer_int);
89 #else
90   (void)cm;
91 #endif
92 }
93
94 void vp10_free_context_buffers(VP9_COMMON *cm) {
95   cm->free_mi(cm);
96   free_seg_map(cm);
97   vpx_free(cm->above_context);
98   cm->above_context = NULL;
99   vpx_free(cm->above_seg_context);
100   cm->above_seg_context = NULL;
101 }
102
103 int vp10_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
104   int new_mi_size;
105
106   vp10_set_mb_mi(cm, width, height);
107   new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
108   if (cm->mi_alloc_size < new_mi_size) {
109     cm->free_mi(cm);
110     if (cm->alloc_mi(cm, new_mi_size))
111       goto fail;
112   }
113
114   if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
115     // Create the segmentation map structure and set to 0.
116     free_seg_map(cm);
117     if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
118       goto fail;
119   }
120
121   if (cm->above_context_alloc_cols < cm->mi_cols) {
122     vpx_free(cm->above_context);
123     cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
124         2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
125         sizeof(*cm->above_context));
126     if (!cm->above_context) goto fail;
127
128     vpx_free(cm->above_seg_context);
129     cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
130         mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
131     if (!cm->above_seg_context) goto fail;
132     cm->above_context_alloc_cols = cm->mi_cols;
133   }
134
135   return 0;
136
137  fail:
138   vp10_free_context_buffers(cm);
139   return 1;
140 }
141
142 void vp10_remove_common(VP9_COMMON *cm) {
143   vp10_free_context_buffers(cm);
144
145   vpx_free(cm->fc);
146   cm->fc = NULL;
147   vpx_free(cm->frame_contexts);
148   cm->frame_contexts = NULL;
149 }
150
151 void vp10_init_context_buffers(VP9_COMMON *cm) {
152   cm->setup_mi(cm);
153   if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
154     memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
155 }
156
157 void vp10_swap_current_and_last_seg_map(VP9_COMMON *cm) {
158   // Swap indices.
159   const int tmp = cm->seg_map_idx;
160   cm->seg_map_idx = cm->prev_seg_map_idx;
161   cm->prev_seg_map_idx = tmp;
162
163   cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
164   cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
165 }