vp9_alloccommon.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "vp9/common/vp9_systemdependent.h"
  18. // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
  19. // frame reference count.
  20. void lock_buffer_pool(BufferPool *const pool) {
  21. #if CONFIG_MULTITHREAD
  22. pthread_mutex_lock(&pool->pool_mutex);
  23. #else
  24. (void)pool;
  25. #endif
  26. }
  27. void unlock_buffer_pool(BufferPool *const pool) {
  28. #if CONFIG_MULTITHREAD
  29. pthread_mutex_unlock(&pool->pool_mutex);
  30. #else
  31. (void)pool;
  32. #endif
  33. }
  34. void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
  35. const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
  36. const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
  37. cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
  38. cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
  39. cm->mi_stride = calc_mi_size(cm->mi_cols);
  40. cm->mb_cols = (cm->mi_cols + 1) >> 1;
  41. cm->mb_rows = (cm->mi_rows + 1) >> 1;
  42. cm->MBs = cm->mb_rows * cm->mb_cols;
  43. }
  44. static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
  45. int i;
  46. for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
  47. cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
  48. if (cm->seg_map_array[i] == NULL)
  49. return 1;
  50. }
  51. cm->seg_map_alloc_size = seg_map_size;
  52. // Init the index.
  53. cm->seg_map_idx = 0;
  54. cm->prev_seg_map_idx = 1;
  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. return 0;
  59. }
  60. static void free_seg_map(VP9_COMMON *cm) {
  61. int i;
  62. for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
  63. vpx_free(cm->seg_map_array[i]);
  64. cm->seg_map_array[i] = NULL;
  65. }
  66. cm->current_frame_seg_map = NULL;
  67. if (!cm->frame_parallel_decode) {
  68. cm->last_frame_seg_map = NULL;
  69. }
  70. }
  71. void vp9_free_ref_frame_buffers(BufferPool *pool) {
  72. int i;
  73. for (i = 0; i < FRAME_BUFFERS; ++i) {
  74. if (pool->frame_bufs[i].ref_count > 0 &&
  75. pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
  76. pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
  77. pool->frame_bufs[i].ref_count = 0;
  78. }
  79. vpx_free(pool->frame_bufs[i].mvs);
  80. pool->frame_bufs[i].mvs = NULL;
  81. vp9_free_frame_buffer(&pool->frame_bufs[i].buf);
  82. }
  83. }
  84. void vp9_free_postproc_buffers(VP9_COMMON *cm) {
  85. #if CONFIG_VP9_POSTPROC
  86. vp9_free_frame_buffer(&cm->post_proc_buffer);
  87. vp9_free_frame_buffer(&cm->post_proc_buffer_int);
  88. #else
  89. (void)cm;
  90. #endif
  91. }
  92. void vp9_free_context_buffers(VP9_COMMON *cm) {
  93. cm->free_mi(cm);
  94. free_seg_map(cm);
  95. vpx_free(cm->above_context);
  96. cm->above_context = NULL;
  97. vpx_free(cm->above_seg_context);
  98. cm->above_seg_context = NULL;
  99. }
  100. int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
  101. int new_mi_size;
  102. vp9_set_mb_mi(cm, width, height);
  103. new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
  104. if (cm->mi_alloc_size < new_mi_size) {
  105. cm->free_mi(cm);
  106. if (cm->alloc_mi(cm, new_mi_size))
  107. goto fail;
  108. }
  109. if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
  110. // Create the segmentation map structure and set to 0.
  111. free_seg_map(cm);
  112. if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
  113. goto fail;
  114. }
  115. if (cm->above_context_alloc_cols < cm->mi_cols) {
  116. vpx_free(cm->above_context);
  117. cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
  118. 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
  119. sizeof(*cm->above_context));
  120. if (!cm->above_context) goto fail;
  121. vpx_free(cm->above_seg_context);
  122. cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
  123. mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
  124. if (!cm->above_seg_context) goto fail;
  125. cm->above_context_alloc_cols = cm->mi_cols;
  126. }
  127. return 0;
  128. fail:
  129. vp9_free_context_buffers(cm);
  130. return 1;
  131. }
  132. void vp9_remove_common(VP9_COMMON *cm) {
  133. vp9_free_context_buffers(cm);
  134. vpx_free(cm->fc);
  135. cm->fc = NULL;
  136. vpx_free(cm->frame_contexts);
  137. cm->frame_contexts = NULL;
  138. }
  139. void vp9_init_context_buffers(VP9_COMMON *cm) {
  140. cm->setup_mi(cm);
  141. if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
  142. memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
  143. }
  144. void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
  145. // Swap indices.
  146. const int tmp = cm->seg_map_idx;
  147. cm->seg_map_idx = cm->prev_seg_map_idx;
  148. cm->prev_seg_map_idx = tmp;
  149. cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
  150. cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
  151. }