vp9_frame_buffers.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 =
  19. (InternalFrameBuffer *)vpx_calloc(list->num_internal_frame_buffers,
  20. sizeof(*list->int_fb));
  21. return (list->int_fb == NULL);
  22. }
  23. void vp9_free_internal_frame_buffers(InternalFrameBufferList *list) {
  24. int i;
  25. assert(list != NULL);
  26. for (i = 0; i < list->num_internal_frame_buffers; ++i) {
  27. vpx_free(list->int_fb[i].data);
  28. list->int_fb[i].data = NULL;
  29. }
  30. vpx_free(list->int_fb);
  31. list->int_fb = NULL;
  32. }
  33. int vp9_get_frame_buffer(void *cb_priv, size_t min_size,
  34. vpx_codec_frame_buffer_t *fb) {
  35. int i;
  36. InternalFrameBufferList *const int_fb_list =
  37. (InternalFrameBufferList *)cb_priv;
  38. if (int_fb_list == NULL)
  39. return -1;
  40. // Find a free frame buffer.
  41. for (i = 0; i < int_fb_list->num_internal_frame_buffers; ++i) {
  42. if (!int_fb_list->int_fb[i].in_use)
  43. break;
  44. }
  45. if (i == int_fb_list->num_internal_frame_buffers)
  46. return -1;
  47. if (int_fb_list->int_fb[i].size < min_size) {
  48. int_fb_list->int_fb[i].data =
  49. (uint8_t *)vpx_realloc(int_fb_list->int_fb[i].data, min_size);
  50. if (!int_fb_list->int_fb[i].data)
  51. return -1;
  52. // This memset is needed for fixing valgrind error from C loop filter
  53. // due to access uninitialized memory in frame border. It could be
  54. // removed if border is totally removed.
  55. memset(int_fb_list->int_fb[i].data, 0, min_size);
  56. int_fb_list->int_fb[i].size = min_size;
  57. }
  58. fb->data = int_fb_list->int_fb[i].data;
  59. fb->size = int_fb_list->int_fb[i].size;
  60. int_fb_list->int_fb[i].in_use = 1;
  61. // Set the frame buffer's private data to point at the internal frame buffer.
  62. fb->priv = &int_fb_list->int_fb[i];
  63. return 0;
  64. }
  65. int vp9_release_frame_buffer(void *cb_priv, vpx_codec_frame_buffer_t *fb) {
  66. InternalFrameBuffer *const int_fb = (InternalFrameBuffer *)fb->priv;
  67. (void)cb_priv;
  68. if (int_fb)
  69. int_fb->in_use = 0;
  70. return 0;
  71. }