Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame^] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #include "./aom_config.h" |
| 13 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
| 15 | #include "av1/common/alloccommon.h" |
| 16 | #include "av1/common/blockd.h" |
| 17 | #include "av1/common/entropymode.h" |
| 18 | #include "av1/common/entropymv.h" |
| 19 | #include "av1/common/onyxc_int.h" |
| 20 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 21 | void av1_set_mb_mi(AV1_COMMON *cm, int width, int height) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 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 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 34 | static int alloc_seg_map(AV1_COMMON *cm, int seg_map_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 35 | int i; |
| 36 | |
| 37 | for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 38 | cm->seg_map_array[i] = (uint8_t *)aom_calloc(seg_map_size, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 39 | if (cm->seg_map_array[i] == NULL) return 1; |
| 40 | } |
| 41 | cm->seg_map_alloc_size = seg_map_size; |
| 42 | |
| 43 | // Init the index. |
| 44 | cm->seg_map_idx = 0; |
| 45 | cm->prev_seg_map_idx = 1; |
| 46 | |
| 47 | cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx]; |
| 48 | if (!cm->frame_parallel_decode) |
| 49 | cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx]; |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 54 | static void free_seg_map(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 58 | aom_free(cm->seg_map_array[i]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 59 | cm->seg_map_array[i] = NULL; |
| 60 | } |
| 61 | |
| 62 | cm->current_frame_seg_map = NULL; |
| 63 | |
| 64 | if (!cm->frame_parallel_decode) { |
| 65 | cm->last_frame_seg_map = NULL; |
| 66 | } |
| 67 | } |
| 68 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 69 | void av1_free_ref_frame_buffers(BufferPool *pool) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 70 | int i; |
| 71 | |
| 72 | for (i = 0; i < FRAME_BUFFERS; ++i) { |
| 73 | if (pool->frame_bufs[i].ref_count > 0 && |
| 74 | pool->frame_bufs[i].raw_frame_buffer.data != NULL) { |
| 75 | pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer); |
| 76 | pool->frame_bufs[i].ref_count = 0; |
| 77 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 78 | aom_free(pool->frame_bufs[i].mvs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 79 | pool->frame_bufs[i].mvs = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 80 | aom_free_frame_buffer(&pool->frame_bufs[i].buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | #if CONFIG_LOOP_RESTORATION |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 85 | void av1_free_restoration_buffers(AV1_COMMON *cm) { |
| 86 | aom_free(cm->rst_info.bilateral_level); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 87 | cm->rst_info.bilateral_level = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 88 | aom_free(cm->rst_info.vfilter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 89 | cm->rst_info.vfilter = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 90 | aom_free(cm->rst_info.hfilter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 91 | cm->rst_info.hfilter = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 92 | aom_free(cm->rst_info.wiener_level); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 93 | cm->rst_info.wiener_level = NULL; |
| 94 | } |
| 95 | #endif // CONFIG_LOOP_RESTORATION |
| 96 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 97 | void av1_free_context_buffers(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 98 | int i; |
| 99 | cm->free_mi(cm); |
| 100 | free_seg_map(cm); |
| 101 | for (i = 0; i < MAX_MB_PLANE; i++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 102 | aom_free(cm->above_context[i]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 103 | cm->above_context[i] = NULL; |
| 104 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 105 | aom_free(cm->above_seg_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 106 | cm->above_seg_context = NULL; |
| 107 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 108 | aom_free(cm->above_txfm_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 109 | cm->above_txfm_context = NULL; |
| 110 | #endif |
| 111 | } |
| 112 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 113 | int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 114 | int new_mi_size; |
| 115 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 116 | av1_set_mb_mi(cm, width, height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 117 | new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows); |
| 118 | if (cm->mi_alloc_size < new_mi_size) { |
| 119 | cm->free_mi(cm); |
| 120 | if (cm->alloc_mi(cm, new_mi_size)) goto fail; |
| 121 | } |
| 122 | |
| 123 | if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) { |
| 124 | // Create the segmentation map structure and set to 0. |
| 125 | free_seg_map(cm); |
| 126 | if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail; |
| 127 | } |
| 128 | |
| 129 | if (cm->above_context_alloc_cols < cm->mi_cols) { |
| 130 | // TODO(geza.lore): These are bigger than they need to be. |
| 131 | // cm->tile_width would be enough but it complicates indexing a |
| 132 | // little elsewhere. |
| 133 | const int aligned_mi_cols = |
| 134 | ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2); |
| 135 | int i; |
| 136 | |
| 137 | for (i = 0; i < MAX_MB_PLANE; i++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 138 | aom_free(cm->above_context[i]); |
| 139 | cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 140 | 2 * aligned_mi_cols, sizeof(*cm->above_context[0])); |
| 141 | if (!cm->above_context[i]) goto fail; |
| 142 | } |
| 143 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 144 | aom_free(cm->above_seg_context); |
| 145 | cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 146 | aligned_mi_cols, sizeof(*cm->above_seg_context)); |
| 147 | if (!cm->above_seg_context) goto fail; |
| 148 | |
| 149 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 150 | aom_free(cm->above_txfm_context); |
| 151 | cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 152 | aligned_mi_cols, sizeof(*cm->above_txfm_context)); |
| 153 | if (!cm->above_txfm_context) goto fail; |
| 154 | #endif |
| 155 | |
| 156 | cm->above_context_alloc_cols = aligned_mi_cols; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | |
| 161 | fail: |
| 162 | // clear the mi_* values to force a realloc on resync |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 163 | av1_set_mb_mi(cm, 0, 0); |
| 164 | av1_free_context_buffers(cm); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 168 | void av1_remove_common(AV1_COMMON *cm) { |
| 169 | av1_free_context_buffers(cm); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 170 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 171 | aom_free(cm->fc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 172 | cm->fc = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 173 | aom_free(cm->frame_contexts); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 174 | cm->frame_contexts = NULL; |
| 175 | } |
| 176 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 177 | void av1_init_context_buffers(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 178 | cm->setup_mi(cm); |
| 179 | if (cm->last_frame_seg_map && !cm->frame_parallel_decode) |
| 180 | memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols); |
| 181 | } |
| 182 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 183 | void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 184 | // Swap indices. |
| 185 | const int tmp = cm->seg_map_idx; |
| 186 | cm->seg_map_idx = cm->prev_seg_map_idx; |
| 187 | cm->prev_seg_map_idx = tmp; |
| 188 | |
| 189 | cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx]; |
| 190 | cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx]; |
| 191 | } |