2
0

vp9_context_tree.c 5.2 KB

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