vp9_frame_buffers.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <assert.h>
  11. #include "vp9/common/vp9_frame_buffers.h"
  12. #include "vpx_mem/vpx_mem.h"
  13. int vp9_alloc_internal_frame_buffers(InternalFrameBufferList *list) {
  14. assert(list != NULL);
  15. vp9_free_internal_frame_buffers(list);
  16. list->num_internal_frame_buffers =
  17. VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
  18. list->int_fb = (InternalFrameBuffer *)vpx_calloc(
  19. list->num_internal_frame_buffers, sizeof(*list->int_fb));
  20. return (list->int_fb == NULL);
  21. }
  22. void vp9_free_internal_frame_buffers(InternalFrameBufferList *list) {
  23. int i;
  24. assert(list != NULL);
  25. for (i = 0; i < list->num_internal_frame_buffers; ++i) {
  26. vpx_free(list->int_fb[i].data);
  27. list->int_fb[i].data = NULL;
  28. }
  29. vpx_free(list->int_fb);
  30. list->int_fb = NULL;
  31. }
  32. int vp9_get_frame_buffer(void *cb_priv, size_t min_size,
  33. vpx_codec_frame_buffer_t *fb) {
  34. int i;
  35. InternalFrameBufferList *const int_fb_list =
  36. (InternalFrameBufferList *)cb_priv;
  37. if (int_fb_list == NULL) return -1;
  38. // Find a free frame buffer.
  39. for (i = 0; i < int_fb_list->num_internal_frame_buffers; ++i) {
  40. if (!int_fb_list->int_fb[i].in_use) break;
  41. }
  42. if (i == int_fb_list->num_internal_frame_buffers) return -1;
  43. if (int_fb_list->int_fb[i].size < min_size) {
  44. vpx_free(int_fb_list->int_fb[i].data);
  45. // The data must be zeroed to fix a valgrind error from the C loop filter
  46. // due to access uninitialized memory in frame border. It could be
  47. // skipped if border were totally removed.
  48. int_fb_list->int_fb[i].data = (uint8_t *)vpx_calloc(1, min_size);
  49. if (!int_fb_list->int_fb[i].data) return -1;
  50. int_fb_list->int_fb[i].size = min_size;
  51. }
  52. fb->data = int_fb_list->int_fb[i].data;
  53. fb->size = int_fb_list->int_fb[i].size;
  54. int_fb_list->int_fb[i].in_use = 1;
  55. // Set the frame buffer's private data to point at the internal frame buffer.
  56. fb->priv = &int_fb_list->int_fb[i];
  57. return 0;
  58. }
  59. int vp9_release_frame_buffer(void *cb_priv, vpx_codec_frame_buffer_t *fb) {
  60. InternalFrameBuffer *const int_fb = (InternalFrameBuffer *)fb->priv;
  61. (void)cb_priv;
  62. if (int_fb) int_fb->in_use = 0;
  63. return 0;
  64. }