]> granicus.if.org Git - libvpx/blob - vp9/common/vp9_alloccommon.c
Revert "Revert "Add support for setting byte alignment.""
[libvpx] / vp9 / 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 "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
20 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
21   const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
22   const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
23
24   cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
25   cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
26   cm->mi_stride = calc_mi_size(cm->mi_cols);
27
28   cm->mb_cols = (cm->mi_cols + 1) >> 1;
29   cm->mb_rows = (cm->mi_rows + 1) >> 1;
30   cm->MBs = cm->mb_rows * cm->mb_cols;
31 }
32
33 void vp9_free_ref_frame_buffers(VP9_COMMON *cm) {
34   int i;
35
36   for (i = 0; i < FRAME_BUFFERS; ++i) {
37     if (cm->frame_bufs[i].ref_count > 0 &&
38         cm->frame_bufs[i].raw_frame_buffer.data != NULL) {
39       cm->release_fb_cb(cm->cb_priv, &cm->frame_bufs[i].raw_frame_buffer);
40       cm->frame_bufs[i].ref_count = 0;
41     }
42     vpx_free(cm->frame_bufs[i].mvs);
43     cm->frame_bufs[i].mvs = NULL;
44     vp9_free_frame_buffer(&cm->frame_bufs[i].buf);
45   }
46
47 #if CONFIG_VP9_POSTPROC
48   vp9_free_frame_buffer(&cm->post_proc_buffer);
49   vp9_free_frame_buffer(&cm->post_proc_buffer_int);
50 #endif
51 }
52
53 void vp9_free_context_buffers(VP9_COMMON *cm) {
54   cm->free_mi(cm);
55   vpx_free(cm->last_frame_seg_map);
56   cm->last_frame_seg_map = NULL;
57   vpx_free(cm->above_context);
58   cm->above_context = NULL;
59   vpx_free(cm->above_seg_context);
60   cm->above_seg_context = NULL;
61 }
62
63 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
64   vp9_free_context_buffers(cm);
65
66   vp9_set_mb_mi(cm, width, height);
67   if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows)))
68     goto fail;
69
70   cm->last_frame_seg_map = (uint8_t *)vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
71   if (!cm->last_frame_seg_map) goto fail;
72
73   cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
74       2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
75       sizeof(*cm->above_context));
76   if (!cm->above_context) goto fail;
77
78   cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
79       mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
80   if (!cm->above_seg_context) goto fail;
81
82   return 0;
83
84  fail:
85   vp9_free_context_buffers(cm);
86   return 1;
87 }
88
89 static void init_frame_bufs(VP9_COMMON *cm) {
90   int i;
91
92   cm->new_fb_idx = FRAME_BUFFERS - 1;
93   cm->frame_bufs[cm->new_fb_idx].ref_count = 1;
94
95   for (i = 0; i < REF_FRAMES; ++i) {
96     cm->ref_frame_map[i] = i;
97     cm->frame_bufs[i].ref_count = 1;
98   }
99 }
100
101 int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) {
102   int i;
103   const int ss_x = cm->subsampling_x;
104   const int ss_y = cm->subsampling_y;
105
106   vp9_free_ref_frame_buffers(cm);
107
108   for (i = 0; i < FRAME_BUFFERS; ++i) {
109     cm->frame_bufs[i].ref_count = 0;
110     if (vp9_alloc_frame_buffer(&cm->frame_bufs[i].buf, width, height,
111                                ss_x, ss_y,
112 #if CONFIG_VP9_HIGHBITDEPTH
113                                cm->use_highbitdepth,
114 #endif
115                                VP9_ENC_BORDER_IN_PIXELS,
116                                cm->byte_alignment) < 0)
117       goto fail;
118     if (cm->frame_bufs[i].mvs == NULL) {
119       cm->frame_bufs[i].mvs =
120           (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
121                                sizeof(*cm->frame_bufs[i].mvs));
122       if (cm->frame_bufs[i].mvs == NULL)
123         goto fail;
124
125       cm->frame_bufs[i].mi_rows = cm->mi_rows;
126       cm->frame_bufs[i].mi_cols = cm->mi_cols;
127     }
128   }
129
130   init_frame_bufs(cm);
131
132 #if CONFIG_VP9_POSTPROC
133   if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
134 #if CONFIG_VP9_HIGHBITDEPTH
135                              cm->use_highbitdepth,
136 #endif
137                              VP9_ENC_BORDER_IN_PIXELS,
138                              cm->byte_alignment) < 0)
139     goto fail;
140 #endif
141
142   return 0;
143
144  fail:
145   vp9_free_ref_frame_buffers(cm);
146   return 1;
147 }
148
149 void vp9_remove_common(VP9_COMMON *cm) {
150   vp9_free_ref_frame_buffers(cm);
151   vp9_free_context_buffers(cm);
152   vp9_free_internal_frame_buffers(&cm->int_frame_buffers);
153
154   vpx_free(cm->fc);
155   cm->fc = NULL;
156   vpx_free(cm->frame_contexts);
157   cm->frame_contexts = NULL;
158 }
159
160 void vp9_init_context_buffers(VP9_COMMON *cm) {
161   cm->setup_mi(cm);
162   if (cm->last_frame_seg_map)
163     vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
164 }