vp9_alloccommon.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "./vpx_config.h"
  11. #include "vpx_mem/vpx_mem.h"
  12. #include "vp9/common/vp9_alloccommon.h"
  13. #include "vp9/common/vp9_blockd.h"
  14. #include "vp9/common/vp9_entropymode.h"
  15. #include "vp9/common/vp9_entropymv.h"
  16. #include "vp9/common/vp9_onyxc_int.h"
  17. // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
  18. // frame reference count.
  19. void lock_buffer_pool(BufferPool *const pool) {
  20. #if CONFIG_MULTITHREAD
  21. pthread_mutex_lock(&pool->pool_mutex);
  22. #else
  23. (void)pool;
  24. #endif
  25. }
  26. void unlock_buffer_pool(BufferPool *const pool) {
  27. #if CONFIG_MULTITHREAD
  28. pthread_mutex_unlock(&pool->pool_mutex);
  29. #else
  30. (void)pool;
  31. #endif
  32. }
  33. void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
  34. const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
  35. const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
  36. cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
  37. cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
  38. cm->mi_stride = calc_mi_size(cm->mi_cols);
  39. cm->mb_cols = (cm->mi_cols + 1) >> 1;
  40. cm->mb_rows = (cm->mi_rows + 1) >> 1;
  41. cm->MBs = cm->mb_rows * cm->mb_cols;
  42. }
  43. static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
  44. int i;
  45. for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
  46. cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
  47. if (cm->seg_map_array[i] == NULL)
  48. return 1;
  49. }
  50. cm->seg_map_alloc_size = seg_map_size;
  51. // Init the index.
  52. cm->seg_map_idx = 0;
  53. cm->prev_seg_map_idx = 1;
  54. cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
  55. if (!cm->frame_parallel_decode)
  56. cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
  57. return 0;
  58. }
  59. static void free_seg_map(VP9_COMMON *cm) {
  60. int i;
  61. for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
  62. vpx_free(cm->seg_map_array[i]);
  63. cm->seg_map_array[i] = NULL;
  64. }
  65. cm->current_frame_seg_map = NULL;
  66. if (!cm->frame_parallel_decode) {
  67. cm->last_frame_seg_map = NULL;
  68. }
  69. }
  70. void vp9_free_ref_frame_buffers(BufferPool *pool) {
  71. int i;
  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. }
  78. vpx_free(pool->frame_bufs[i].mvs);
  79. pool->frame_bufs[i].mvs = NULL;
  80. vpx_free_frame_buffer(&pool->frame_bufs[i].buf);
  81. }
  82. }
  83. void vp9_free_postproc_buffers(VP9_COMMON *cm) {
  84. #if CONFIG_VP9_POSTPROC
  85. vpx_free_frame_buffer(&cm->post_proc_buffer);
  86. vpx_free_frame_buffer(&cm->post_proc_buffer_int);
  87. #else
  88. (void)cm;
  89. #endif
  90. }
  91. void vp9_free_context_buffers(VP9_COMMON *cm) {
  92. cm->free_mi(cm);
  93. free_seg_map(cm);
  94. vpx_free(cm->above_context);
  95. cm->above_context = NULL;
  96. vpx_free(cm->above_seg_context);
  97. cm->above_seg_context = NULL;
  98. vpx_free(cm->lf.lfm);
  99. cm->lf.lfm = NULL;
  100. }
  101. int vp9_alloc_loop_filter(VP9_COMMON *cm) {
  102. vpx_free(cm->lf.lfm);
  103. // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region. The
  104. // stride and rows are rounded up / truncated to a multiple of 8.
  105. cm->lf.lfm_stride = (cm->mi_cols + (MI_BLOCK_SIZE - 1)) >> 3;
  106. cm->lf.lfm = (LOOP_FILTER_MASK *)vpx_calloc(
  107. ((cm->mi_rows + (MI_BLOCK_SIZE - 1)) >> 3) * cm->lf.lfm_stride,
  108. sizeof(*cm->lf.lfm));
  109. if (!cm->lf.lfm)
  110. return 1;
  111. return 0;
  112. }
  113. int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
  114. int new_mi_size;
  115. vp9_set_mb_mi(cm, width, height);
  116. new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
  117. if (cm->mi_alloc_size < new_mi_size) {
  118. cm->free_mi(cm);
  119. if (cm->alloc_mi(cm, new_mi_size))
  120. goto fail;
  121. }
  122. if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
  123. // Create the segmentation map structure and set to 0.
  124. free_seg_map(cm);
  125. if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
  126. goto fail;
  127. }
  128. if (cm->above_context_alloc_cols < cm->mi_cols) {
  129. vpx_free(cm->above_context);
  130. cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
  131. 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
  132. sizeof(*cm->above_context));
  133. if (!cm->above_context) goto fail;
  134. vpx_free(cm->above_seg_context);
  135. cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
  136. mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
  137. if (!cm->above_seg_context) goto fail;
  138. cm->above_context_alloc_cols = cm->mi_cols;
  139. }
  140. if (vp9_alloc_loop_filter(cm))
  141. goto fail;
  142. return 0;
  143. fail:
  144. vp9_free_context_buffers(cm);
  145. return 1;
  146. }
  147. void vp9_remove_common(VP9_COMMON *cm) {
  148. vp9_free_context_buffers(cm);
  149. vpx_free(cm->fc);
  150. cm->fc = NULL;
  151. vpx_free(cm->frame_contexts);
  152. cm->frame_contexts = NULL;
  153. }
  154. void vp9_init_context_buffers(VP9_COMMON *cm) {
  155. cm->setup_mi(cm);
  156. if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
  157. memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
  158. }
  159. void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
  160. // Swap indices.
  161. const int tmp = cm->seg_map_idx;
  162. cm->seg_map_idx = cm->prev_seg_map_idx;
  163. cm->prev_seg_map_idx = tmp;
  164. cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
  165. cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
  166. }