1/* 2 * Copyright (c) 2014 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 "vp9/encoder/vp9_context_tree.h" 12#include "vp9/encoder/vp9_encoder.h" 13 14static const BLOCK_SIZE square[] = { 15 BLOCK_8X8, 16 BLOCK_16X16, 17 BLOCK_32X32, 18 BLOCK_64X64, 19}; 20 21static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk, 22 PICK_MODE_CONTEXT *ctx) { 23 const int num_blk = (num_4x4_blk < 4 ? 4 : num_4x4_blk); 24 const int num_pix = num_blk << 4; 25 int i, k; 26 ctx->num_4x4_blk = num_blk; 27 28 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, 29 vpx_calloc(num_4x4_blk, sizeof(uint8_t))); 30 for (i = 0; i < MAX_MB_PLANE; ++i) { 31 for (k = 0; k < 3; ++k) { 32 CHECK_MEM_ERROR(cm, ctx->coeff[i][k], 33 vpx_memalign(16, num_pix * sizeof(*ctx->coeff[i][k]))); 34 CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k], 35 vpx_memalign(16, num_pix * sizeof(*ctx->qcoeff[i][k]))); 36 CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k], 37 vpx_memalign(16, num_pix * sizeof(*ctx->dqcoeff[i][k]))); 38 CHECK_MEM_ERROR(cm, ctx->eobs[i][k], 39 vpx_memalign(16, num_pix * sizeof(*ctx->eobs[i][k]))); 40 ctx->coeff_pbuf[i][k] = ctx->coeff[i][k]; 41 ctx->qcoeff_pbuf[i][k] = ctx->qcoeff[i][k]; 42 ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k]; 43 ctx->eobs_pbuf[i][k] = ctx->eobs[i][k]; 44 } 45 } 46} 47 48static void free_mode_context(PICK_MODE_CONTEXT *ctx) { 49 int i, k; 50 vpx_free(ctx->zcoeff_blk); 51 ctx->zcoeff_blk = 0; 52 for (i = 0; i < MAX_MB_PLANE; ++i) { 53 for (k = 0; k < 3; ++k) { 54 vpx_free(ctx->coeff[i][k]); 55 ctx->coeff[i][k] = 0; 56 vpx_free(ctx->qcoeff[i][k]); 57 ctx->qcoeff[i][k] = 0; 58 vpx_free(ctx->dqcoeff[i][k]); 59 ctx->dqcoeff[i][k] = 0; 60 vpx_free(ctx->eobs[i][k]); 61 ctx->eobs[i][k] = 0; 62 } 63 } 64} 65 66static void alloc_tree_contexts(VP9_COMMON *cm, PC_TREE *tree, 67 int num_4x4_blk) { 68 alloc_mode_context(cm, num_4x4_blk, &tree->none); 69 alloc_mode_context(cm, num_4x4_blk/2, &tree->horizontal[0]); 70 alloc_mode_context(cm, num_4x4_blk/2, &tree->vertical[0]); 71 72 /* TODO(Jbb): for 4x8 and 8x4 these allocated values are not used. 73 * Figure out a better way to do this. */ 74 alloc_mode_context(cm, num_4x4_blk/2, &tree->horizontal[1]); 75 alloc_mode_context(cm, num_4x4_blk/2, &tree->vertical[1]); 76} 77 78static void free_tree_contexts(PC_TREE *tree) { 79 free_mode_context(&tree->none); 80 free_mode_context(&tree->horizontal[0]); 81 free_mode_context(&tree->horizontal[1]); 82 free_mode_context(&tree->vertical[0]); 83 free_mode_context(&tree->vertical[1]); 84} 85 86// This function sets up a tree of contexts such that at each square 87// partition level. There are contexts for none, horizontal, vertical, and 88// split. Along with a block_size value and a selected block_size which 89// represents the state of our search. 90void vp9_setup_pc_tree(VP9_COMMON *cm, VP9_COMP *cpi) { 91 int i, j; 92 const int leaf_nodes = 64; 93 const int tree_nodes = 64 + 16 + 4 + 1; 94 int pc_tree_index = 0; 95 PC_TREE *this_pc; 96 PICK_MODE_CONTEXT *this_leaf; 97 int square_index = 1; 98 int nodes; 99 100 vpx_free(cpi->leaf_tree); 101 CHECK_MEM_ERROR(cm, cpi->leaf_tree, vpx_calloc(leaf_nodes, 102 sizeof(*cpi->leaf_tree))); 103 vpx_free(cpi->pc_tree); 104 CHECK_MEM_ERROR(cm, cpi->pc_tree, vpx_calloc(tree_nodes, 105 sizeof(*cpi->pc_tree))); 106 107 this_pc = &cpi->pc_tree[0]; 108 this_leaf = &cpi->leaf_tree[0]; 109 110 // 4x4 blocks smaller than 8x8 but in the same 8x8 block share the same 111 // context so we only need to allocate 1 for each 8x8 block. 112 for (i = 0; i < leaf_nodes; ++i) 113 alloc_mode_context(cm, 1, &cpi->leaf_tree[i]); 114 115 // Sets up all the leaf nodes in the tree. 116 for (pc_tree_index = 0; pc_tree_index < leaf_nodes; ++pc_tree_index) { 117 PC_TREE *const tree = &cpi->pc_tree[pc_tree_index]; 118 tree->block_size = square[0]; 119 alloc_tree_contexts(cm, tree, 4); 120 tree->leaf_split[0] = this_leaf++; 121 for (j = 1; j < 4; j++) 122 tree->leaf_split[j] = tree->leaf_split[0]; 123 } 124 125 // Each node has 4 leaf nodes, fill each block_size level of the tree 126 // from leafs to the root. 127 for (nodes = 16; nodes > 0; nodes >>= 2) { 128 for (i = 0; i < nodes; ++i) { 129 PC_TREE *const tree = &cpi->pc_tree[pc_tree_index]; 130 alloc_tree_contexts(cm, tree, 4 << (2 * square_index)); 131 tree->block_size = square[square_index]; 132 for (j = 0; j < 4; j++) 133 tree->split[j] = this_pc++; 134 ++pc_tree_index; 135 } 136 ++square_index; 137 } 138 cpi->pc_root = &cpi->pc_tree[tree_nodes - 1]; 139 cpi->pc_root[0].none.best_mode_index = 2; 140} 141 142void vp9_free_pc_tree(VP9_COMP *cpi) { 143 const int tree_nodes = 64 + 16 + 4 + 1; 144 int i; 145 146 // Set up all 4x4 mode contexts 147 for (i = 0; i < 64; ++i) 148 free_mode_context(&cpi->leaf_tree[i]); 149 150 // Sets up all the leaf nodes in the tree. 151 for (i = 0; i < tree_nodes; ++i) 152 free_tree_contexts(&cpi->pc_tree[i]); 153 154 vpx_free(cpi->pc_tree); 155 cpi->pc_tree = NULL; 156 vpx_free(cpi->leaf_tree); 157 cpi->leaf_tree = NULL; 158} 159