1 // Copyright 2019 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/internal_frame_buffer_list.h"
16
17 #include <cassert>
18 #include <cstdint>
19 #include <memory>
20 #include <new>
21 #include <utility>
22
23 #include "src/utils/common.h"
24
25 namespace libgav1 {
26 extern "C" {
27
OnInternalFrameBufferSizeChanged(void * callback_private_data,int bitdepth,Libgav1ImageFormat image_format,int width,int height,int left_border,int right_border,int top_border,int bottom_border,int stride_alignment)28 Libgav1StatusCode OnInternalFrameBufferSizeChanged(
29 void* callback_private_data, int bitdepth, Libgav1ImageFormat image_format,
30 int width, int height, int left_border, int right_border, int top_border,
31 int bottom_border, int stride_alignment) {
32 auto* buffer_list =
33 static_cast<InternalFrameBufferList*>(callback_private_data);
34 return buffer_list->OnFrameBufferSizeChanged(
35 bitdepth, image_format, width, height, left_border, right_border,
36 top_border, bottom_border, stride_alignment);
37 }
38
GetInternalFrameBuffer(void * callback_private_data,int bitdepth,Libgav1ImageFormat image_format,int width,int height,int left_border,int right_border,int top_border,int bottom_border,int stride_alignment,Libgav1FrameBuffer * frame_buffer)39 Libgav1StatusCode GetInternalFrameBuffer(
40 void* callback_private_data, int bitdepth, Libgav1ImageFormat image_format,
41 int width, int height, int left_border, int right_border, int top_border,
42 int bottom_border, int stride_alignment, Libgav1FrameBuffer* frame_buffer) {
43 auto* buffer_list =
44 static_cast<InternalFrameBufferList*>(callback_private_data);
45 return buffer_list->GetFrameBuffer(
46 bitdepth, image_format, width, height, left_border, right_border,
47 top_border, bottom_border, stride_alignment, frame_buffer);
48 }
49
ReleaseInternalFrameBuffer(void * callback_private_data,void * buffer_private_data)50 void ReleaseInternalFrameBuffer(void* callback_private_data,
51 void* buffer_private_data) {
52 auto* buffer_list =
53 static_cast<InternalFrameBufferList*>(callback_private_data);
54 buffer_list->ReleaseFrameBuffer(buffer_private_data);
55 }
56
57 } // extern "C"
58
OnFrameBufferSizeChanged(int,Libgav1ImageFormat,int,int,int,int,int,int,int)59 StatusCode InternalFrameBufferList::OnFrameBufferSizeChanged(
60 int /*bitdepth*/, Libgav1ImageFormat /*image_format*/, int /*width*/,
61 int /*height*/, int /*left_border*/, int /*right_border*/,
62 int /*top_border*/, int /*bottom_border*/, int /*stride_alignment*/) {
63 return kStatusOk;
64 }
65
GetFrameBuffer(int bitdepth,Libgav1ImageFormat image_format,int width,int height,int left_border,int right_border,int top_border,int bottom_border,int stride_alignment,Libgav1FrameBuffer * frame_buffer)66 StatusCode InternalFrameBufferList::GetFrameBuffer(
67 int bitdepth, Libgav1ImageFormat image_format, int width, int height,
68 int left_border, int right_border, int top_border, int bottom_border,
69 int stride_alignment, Libgav1FrameBuffer* frame_buffer) {
70 FrameBufferInfo info;
71 StatusCode status = ComputeFrameBufferInfo(
72 bitdepth, image_format, width, height, left_border, right_border,
73 top_border, bottom_border, stride_alignment, &info);
74 if (status != kStatusOk) return status;
75
76 if (info.uv_buffer_size > SIZE_MAX / 2 ||
77 info.y_buffer_size > SIZE_MAX - 2 * info.uv_buffer_size) {
78 return kStatusInvalidArgument;
79 }
80 const size_t min_size = info.y_buffer_size + 2 * info.uv_buffer_size;
81
82 Buffer* buffer = nullptr;
83 for (auto& buffer_ptr : buffers_) {
84 if (!buffer_ptr->in_use) {
85 buffer = buffer_ptr.get();
86 break;
87 }
88 }
89 if (buffer == nullptr) {
90 std::unique_ptr<Buffer> new_buffer(new (std::nothrow) Buffer);
91 if (new_buffer == nullptr || !buffers_.push_back(std::move(new_buffer))) {
92 return kStatusOutOfMemory;
93 }
94 buffer = buffers_.back().get();
95 }
96
97 if (buffer->size < min_size) {
98 std::unique_ptr<uint8_t[], MallocDeleter> new_data(
99 static_cast<uint8_t*>(malloc(min_size)));
100 if (new_data == nullptr) return kStatusOutOfMemory;
101 buffer->data = std::move(new_data);
102 buffer->size = min_size;
103 }
104
105 uint8_t* const y_buffer = buffer->data.get();
106 uint8_t* const u_buffer =
107 (info.uv_buffer_size == 0) ? nullptr : y_buffer + info.y_buffer_size;
108 uint8_t* const v_buffer =
109 (info.uv_buffer_size == 0) ? nullptr : u_buffer + info.uv_buffer_size;
110 status = Libgav1SetFrameBuffer(&info, y_buffer, u_buffer, v_buffer, buffer,
111 frame_buffer);
112 if (status != kStatusOk) return status;
113 buffer->in_use = true;
114 return kStatusOk;
115 }
116
ReleaseFrameBuffer(void * buffer_private_data)117 void InternalFrameBufferList::ReleaseFrameBuffer(void* buffer_private_data) {
118 auto* const buffer = static_cast<Buffer*>(buffer_private_data);
119 buffer->in_use = false;
120 }
121
122 } // namespace libgav1
123