vp9_alloccommon.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
  18. const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
  19. const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
  20. cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
  21. cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
  22. cm->mi_stride = calc_mi_size(cm->mi_cols);
  23. cm->mb_cols = (cm->mi_cols + 1) >> 1;
  24. cm->mb_rows = (cm->mi_rows + 1) >> 1;
  25. cm->MBs = cm->mb_rows * cm->mb_cols;
  26. }
  27. static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
  28. int i;
  29. for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
  30. cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
  31. if (cm->seg_map_array[i] == NULL) return 1;
  32. }
  33. cm->seg_map_alloc_size = seg_map_size;
  34. // Init the index.
  35. cm->seg_map_idx = 0;
  36. cm->prev_seg_map_idx = 1;
  37. cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
  38. cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
  39. return 0;
  40. }
  41. static void free_seg_map(VP9_COMMON *cm) {
  42. int i;
  43. for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
  44. vpx_free(cm->seg_map_array[i]);
  45. cm->seg_map_array[i] = NULL;
  46. }
  47. cm->current_frame_seg_map = NULL;
  48. cm->last_frame_seg_map = NULL;
  49. }
  50. void vp9_free_ref_frame_buffers(BufferPool *pool) {
  51. int i;
  52. for (i = 0; i < FRAME_BUFFERS; ++i) {
  53. if (pool->frame_bufs[i].ref_count > 0 &&
  54. pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
  55. pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
  56. pool->frame_bufs[i].ref_count = 0;
  57. }
  58. vpx_free(pool->frame_bufs[i].mvs);
  59. pool->frame_bufs[i].mvs = NULL;
  60. vpx_free_frame_buffer(&pool->frame_bufs[i].buf);
  61. }
  62. }
  63. void vp9_free_postproc_buffers(VP9_COMMON *cm) {
  64. #if CONFIG_VP9_POSTPROC
  65. vpx_free_frame_buffer(&cm->post_proc_buffer);
  66. vpx_free_frame_buffer(&cm->post_proc_buffer_int);
  67. vpx_free(cm->postproc_state.limits);
  68. cm->postproc_state.limits = NULL;
  69. vpx_free(cm->postproc_state.generated_noise);
  70. cm->postproc_state.generated_noise = NULL;
  71. #else
  72. (void)cm;
  73. #endif
  74. }
  75. void vp9_free_context_buffers(VP9_COMMON *cm) {
  76. cm->free_mi(cm);
  77. free_seg_map(cm);
  78. vpx_free(cm->above_context);
  79. cm->above_context = NULL;
  80. vpx_free(cm->above_seg_context);
  81. cm->above_seg_context = NULL;
  82. vpx_free(cm->lf.lfm);
  83. cm->lf.lfm = NULL;
  84. }
  85. int vp9_alloc_loop_filter(VP9_COMMON *cm) {
  86. vpx_free(cm->lf.lfm);
  87. // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region. The
  88. // stride and rows are rounded up / truncated to a multiple of 8.
  89. cm->lf.lfm_stride = (cm->mi_cols + (MI_BLOCK_SIZE - 1)) >> 3;
  90. cm->lf.lfm = (LOOP_FILTER_MASK *)vpx_calloc(
  91. ((cm->mi_rows + (MI_BLOCK_SIZE - 1)) >> 3) * cm->lf.lfm_stride,
  92. sizeof(*cm->lf.lfm));
  93. if (!cm->lf.lfm) return 1;
  94. return 0;
  95. }
  96. int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
  97. int new_mi_size;
  98. vp9_set_mb_mi(cm, width, height);
  99. new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
  100. if (cm->mi_alloc_size < new_mi_size) {
  101. cm->free_mi(cm);
  102. if (cm->alloc_mi(cm, new_mi_size)) goto fail;
  103. }
  104. if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
  105. // Create the segmentation map structure and set to 0.
  106. free_seg_map(cm);
  107. if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
  108. }
  109. if (cm->above_context_alloc_cols < cm->mi_cols) {
  110. vpx_free(cm->above_context);
  111. cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
  112. 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
  113. sizeof(*cm->above_context));
  114. if (!cm->above_context) goto fail;
  115. vpx_free(cm->above_seg_context);
  116. cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
  117. mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
  118. if (!cm->above_seg_context) goto fail;
  119. cm->above_context_alloc_cols = cm->mi_cols;
  120. }
  121. if (vp9_alloc_loop_filter(cm)) goto fail;
  122. return 0;
  123. fail:
  124. // clear the mi_* values to force a realloc on resync
  125. vp9_set_mb_mi(cm, 0, 0);
  126. vp9_free_context_buffers(cm);
  127. return 1;
  128. }
  129. void vp9_remove_common(VP9_COMMON *cm) {
  130. #if CONFIG_VP9_POSTPROC
  131. vp9_free_postproc_buffers(cm);
  132. #endif
  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)
  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. }