Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 2 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 5 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 6 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 7 | * was not distributed with this source code in the LICENSE file, you can |
| 8 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 9 | * Media Patent License 1.0 was not distributed with this source code in the |
| 10 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 13 | #include "./aom_config.h" |
| 14 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 15 | |
| 16 | #include "av1/common/alloccommon.h" |
| 17 | #include "av1/common/blockd.h" |
| 18 | #include "av1/common/entropymode.h" |
| 19 | #include "av1/common/entropymv.h" |
| 20 | #include "av1/common/onyxc_int.h" |
| 21 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 22 | void av1_set_mb_mi(AV1_COMMON *cm, int width, int height) { |
Jingning Han | 944b805 | 2017-03-31 08:57:30 -0700 | [diff] [blame] | 23 | // TODO(jingning): Fine tune the loop filter operations and bring this |
| 24 | // back to integer multiple of 4 for cb4x4. |
| 25 | const int aligned_width = ALIGN_POWER_OF_TWO(width, 3); |
| 26 | const int aligned_height = ALIGN_POWER_OF_TWO(height, 3); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 27 | |
| 28 | cm->mi_cols = aligned_width >> MI_SIZE_LOG2; |
| 29 | cm->mi_rows = aligned_height >> MI_SIZE_LOG2; |
| 30 | cm->mi_stride = calc_mi_size(cm->mi_cols); |
| 31 | |
Jingning Han | 91ad0e8 | 2016-12-07 17:59:32 -0800 | [diff] [blame] | 32 | #if CONFIG_CB4X4 |
| 33 | cm->mb_cols = (cm->mi_cols + 2) >> 2; |
| 34 | cm->mb_rows = (cm->mi_rows + 2) >> 2; |
| 35 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 36 | cm->mb_cols = (cm->mi_cols + 1) >> 1; |
| 37 | cm->mb_rows = (cm->mi_rows + 1) >> 1; |
Jingning Han | 91ad0e8 | 2016-12-07 17:59:32 -0800 | [diff] [blame] | 38 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 39 | cm->MBs = cm->mb_rows * cm->mb_cols; |
| 40 | } |
| 41 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 42 | static int alloc_seg_map(AV1_COMMON *cm, int seg_map_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 43 | int i; |
| 44 | |
| 45 | for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 46 | 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] | 47 | if (cm->seg_map_array[i] == NULL) return 1; |
| 48 | } |
| 49 | cm->seg_map_alloc_size = seg_map_size; |
| 50 | |
| 51 | // Init the index. |
| 52 | cm->seg_map_idx = 0; |
| 53 | cm->prev_seg_map_idx = 1; |
| 54 | |
| 55 | cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx]; |
| 56 | if (!cm->frame_parallel_decode) |
| 57 | cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx]; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 62 | static void free_seg_map(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 63 | int i; |
| 64 | |
| 65 | for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 66 | aom_free(cm->seg_map_array[i]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 67 | cm->seg_map_array[i] = NULL; |
| 68 | } |
| 69 | |
| 70 | cm->current_frame_seg_map = NULL; |
| 71 | |
| 72 | if (!cm->frame_parallel_decode) { |
| 73 | cm->last_frame_seg_map = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 77 | void av1_free_ref_frame_buffers(BufferPool *pool) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < FRAME_BUFFERS; ++i) { |
| 81 | if (pool->frame_bufs[i].ref_count > 0 && |
| 82 | pool->frame_bufs[i].raw_frame_buffer.data != NULL) { |
| 83 | pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer); |
| 84 | pool->frame_bufs[i].ref_count = 0; |
| 85 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 86 | aom_free(pool->frame_bufs[i].mvs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 87 | pool->frame_bufs[i].mvs = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 88 | aom_free_frame_buffer(&pool->frame_bufs[i].buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | #if CONFIG_LOOP_RESTORATION |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 93 | // Assumes cm->rst_info[p].restoration_tilesize is already initialized |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 94 | void av1_alloc_restoration_buffers(AV1_COMMON *cm) { |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 95 | int p; |
David Barker | befcc42 | 2017-01-31 09:42:10 +0000 | [diff] [blame] | 96 | av1_alloc_restoration_struct(cm, &cm->rst_info[0], cm->width, cm->height); |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 97 | for (p = 1; p < MAX_MB_PLANE; ++p) |
Debargha Mukherjee | 1a0ae84 | 2017-01-26 16:45:22 -0800 | [diff] [blame] | 98 | av1_alloc_restoration_struct( |
David Barker | befcc42 | 2017-01-31 09:42:10 +0000 | [diff] [blame] | 99 | cm, &cm->rst_info[p], ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x), |
Debargha Mukherjee | 1a0ae84 | 2017-01-26 16:45:22 -0800 | [diff] [blame] | 100 | ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y)); |
Alex Converse | 7f094f1 | 2017-02-23 17:29:40 -0800 | [diff] [blame] | 101 | aom_free(cm->rst_internal.tmpbuf); |
| 102 | CHECK_MEM_ERROR(cm, cm->rst_internal.tmpbuf, |
Yaowu Xu | bcf25cd | 2017-03-10 10:27:22 -0800 | [diff] [blame] | 103 | (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE)); |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 106 | void av1_free_restoration_buffers(AV1_COMMON *cm) { |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 107 | int p; |
| 108 | for (p = 0; p < MAX_MB_PLANE; ++p) |
| 109 | av1_free_restoration_struct(&cm->rst_info[p]); |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 110 | aom_free(cm->rst_internal.tmpbuf); |
| 111 | cm->rst_internal.tmpbuf = NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 112 | } |
| 113 | #endif // CONFIG_LOOP_RESTORATION |
| 114 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 115 | void av1_free_context_buffers(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 116 | int i; |
| 117 | cm->free_mi(cm); |
| 118 | free_seg_map(cm); |
| 119 | for (i = 0; i < MAX_MB_PLANE; i++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 120 | aom_free(cm->above_context[i]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 121 | cm->above_context[i] = NULL; |
| 122 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 123 | aom_free(cm->above_seg_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 124 | cm->above_seg_context = NULL; |
| 125 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 126 | aom_free(cm->above_txfm_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 127 | cm->above_txfm_context = NULL; |
| 128 | #endif |
| 129 | } |
| 130 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 131 | int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 132 | int new_mi_size; |
| 133 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 134 | av1_set_mb_mi(cm, width, height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 135 | new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows); |
| 136 | if (cm->mi_alloc_size < new_mi_size) { |
| 137 | cm->free_mi(cm); |
| 138 | if (cm->alloc_mi(cm, new_mi_size)) goto fail; |
| 139 | } |
| 140 | |
| 141 | if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) { |
| 142 | // Create the segmentation map structure and set to 0. |
| 143 | free_seg_map(cm); |
| 144 | if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail; |
| 145 | } |
| 146 | |
| 147 | if (cm->above_context_alloc_cols < cm->mi_cols) { |
| 148 | // TODO(geza.lore): These are bigger than they need to be. |
| 149 | // cm->tile_width would be enough but it complicates indexing a |
| 150 | // little elsewhere. |
| 151 | const int aligned_mi_cols = |
| 152 | ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2); |
| 153 | int i; |
| 154 | |
| 155 | for (i = 0; i < MAX_MB_PLANE; i++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 156 | aom_free(cm->above_context[i]); |
| 157 | cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc( |
Timothy B. Terriberry | 5e81643 | 2017-05-05 13:58:32 -0700 | [diff] [blame^] | 158 | aligned_mi_cols << (MI_SIZE_LOG2 - tx_size_wide_log2[0]), |
| 159 | sizeof(*cm->above_context[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 160 | if (!cm->above_context[i]) goto fail; |
| 161 | } |
| 162 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 163 | aom_free(cm->above_seg_context); |
| 164 | cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 165 | aligned_mi_cols, sizeof(*cm->above_seg_context)); |
| 166 | if (!cm->above_seg_context) goto fail; |
| 167 | |
| 168 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 169 | aom_free(cm->above_txfm_context); |
| 170 | cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 171 | aligned_mi_cols, sizeof(*cm->above_txfm_context)); |
| 172 | if (!cm->above_txfm_context) goto fail; |
| 173 | #endif |
| 174 | |
| 175 | cm->above_context_alloc_cols = aligned_mi_cols; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | fail: |
| 181 | // clear the mi_* values to force a realloc on resync |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 182 | av1_set_mb_mi(cm, 0, 0); |
| 183 | av1_free_context_buffers(cm); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 184 | return 1; |
| 185 | } |
| 186 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 187 | void av1_remove_common(AV1_COMMON *cm) { |
| 188 | av1_free_context_buffers(cm); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 189 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 190 | aom_free(cm->fc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 191 | cm->fc = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 192 | aom_free(cm->frame_contexts); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 193 | cm->frame_contexts = NULL; |
| 194 | } |
| 195 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 196 | void av1_init_context_buffers(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 197 | cm->setup_mi(cm); |
| 198 | if (cm->last_frame_seg_map && !cm->frame_parallel_decode) |
| 199 | memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols); |
| 200 | } |
| 201 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 202 | void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 203 | // Swap indices. |
| 204 | const int tmp = cm->seg_map_idx; |
| 205 | cm->seg_map_idx = cm->prev_seg_map_idx; |
| 206 | cm->prev_seg_map_idx = tmp; |
| 207 | |
| 208 | cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx]; |
| 209 | cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx]; |
| 210 | } |