1*09537850SAkhilesh Sanikop /*
2*09537850SAkhilesh Sanikop * Copyright 2020 The libgav1 Authors
3*09537850SAkhilesh Sanikop *
4*09537850SAkhilesh Sanikop * Licensed under the Apache License, Version 2.0 (the "License");
5*09537850SAkhilesh Sanikop * you may not use this file except in compliance with the License.
6*09537850SAkhilesh Sanikop * You may obtain a copy of the License at
7*09537850SAkhilesh Sanikop *
8*09537850SAkhilesh Sanikop * http://www.apache.org/licenses/LICENSE-2.0
9*09537850SAkhilesh Sanikop *
10*09537850SAkhilesh Sanikop * Unless required by applicable law or agreed to in writing, software
11*09537850SAkhilesh Sanikop * distributed under the License is distributed on an "AS IS" BASIS,
12*09537850SAkhilesh Sanikop * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*09537850SAkhilesh Sanikop * See the License for the specific language governing permissions and
14*09537850SAkhilesh Sanikop * limitations under the License.
15*09537850SAkhilesh Sanikop */
16*09537850SAkhilesh Sanikop
17*09537850SAkhilesh Sanikop #ifndef LIBGAV1_SRC_GAV1_FRAME_BUFFER_H_
18*09537850SAkhilesh Sanikop #define LIBGAV1_SRC_GAV1_FRAME_BUFFER_H_
19*09537850SAkhilesh Sanikop
20*09537850SAkhilesh Sanikop // All the declarations in this file are part of the public ABI. This file may
21*09537850SAkhilesh Sanikop // be included by both C and C++ files.
22*09537850SAkhilesh Sanikop
23*09537850SAkhilesh Sanikop #if defined(__cplusplus)
24*09537850SAkhilesh Sanikop #include <cstddef>
25*09537850SAkhilesh Sanikop #include <cstdint>
26*09537850SAkhilesh Sanikop #else
27*09537850SAkhilesh Sanikop #include <stddef.h>
28*09537850SAkhilesh Sanikop #include <stdint.h>
29*09537850SAkhilesh Sanikop #endif // defined(__cplusplus)
30*09537850SAkhilesh Sanikop
31*09537850SAkhilesh Sanikop #include "gav1/decoder_buffer.h"
32*09537850SAkhilesh Sanikop #include "gav1/status_code.h"
33*09537850SAkhilesh Sanikop #include "gav1/symbol_visibility.h"
34*09537850SAkhilesh Sanikop
35*09537850SAkhilesh Sanikop // The callback functions use the C linkage conventions.
36*09537850SAkhilesh Sanikop #if defined(__cplusplus)
37*09537850SAkhilesh Sanikop extern "C" {
38*09537850SAkhilesh Sanikop #endif
39*09537850SAkhilesh Sanikop
40*09537850SAkhilesh Sanikop // This structure represents an allocated frame buffer.
41*09537850SAkhilesh Sanikop typedef struct Libgav1FrameBuffer {
42*09537850SAkhilesh Sanikop // In the |plane| and |stride| arrays, the elements at indexes 0, 1, and 2
43*09537850SAkhilesh Sanikop // are for the Y, U, and V planes, respectively.
44*09537850SAkhilesh Sanikop uint8_t* plane[3]; // Pointers to the frame (excluding the borders) in the
45*09537850SAkhilesh Sanikop // data buffers.
46*09537850SAkhilesh Sanikop int stride[3]; // Row strides in bytes.
47*09537850SAkhilesh Sanikop void* private_data; // Frame buffer's private data. Available for use by the
48*09537850SAkhilesh Sanikop // release frame buffer callback. Also copied to the
49*09537850SAkhilesh Sanikop // |buffer_private_data| field of DecoderBuffer for use
50*09537850SAkhilesh Sanikop // by the consumer of a DecoderBuffer.
51*09537850SAkhilesh Sanikop } Libgav1FrameBuffer;
52*09537850SAkhilesh Sanikop
53*09537850SAkhilesh Sanikop // This callback is invoked by the decoder to provide information on the
54*09537850SAkhilesh Sanikop // subsequent frames in the video, until the next invocation of this callback
55*09537850SAkhilesh Sanikop // or the end of the video.
56*09537850SAkhilesh Sanikop //
57*09537850SAkhilesh Sanikop // |width| and |height| are the maximum frame width and height in pixels.
58*09537850SAkhilesh Sanikop // |left_border|, |right_border|, |top_border|, and |bottom_border| are the
59*09537850SAkhilesh Sanikop // maximum left, right, top, and bottom border sizes in pixels.
60*09537850SAkhilesh Sanikop // |stride_alignment| specifies the alignment of the row stride in bytes.
61*09537850SAkhilesh Sanikop //
62*09537850SAkhilesh Sanikop // Returns kLibgav1StatusOk on success, an error status on failure.
63*09537850SAkhilesh Sanikop //
64*09537850SAkhilesh Sanikop // NOTE: This callback may be omitted if the information is not useful to the
65*09537850SAkhilesh Sanikop // application.
66*09537850SAkhilesh Sanikop typedef Libgav1StatusCode (*Libgav1FrameBufferSizeChangedCallback)(
67*09537850SAkhilesh Sanikop void* callback_private_data, int bitdepth, Libgav1ImageFormat image_format,
68*09537850SAkhilesh Sanikop int width, int height, int left_border, int right_border, int top_border,
69*09537850SAkhilesh Sanikop int bottom_border, int stride_alignment);
70*09537850SAkhilesh Sanikop
71*09537850SAkhilesh Sanikop // This callback is invoked by the decoder to allocate a frame buffer, which
72*09537850SAkhilesh Sanikop // consists of three data buffers, for the Y, U, and V planes, respectively.
73*09537850SAkhilesh Sanikop //
74*09537850SAkhilesh Sanikop // The callback must set |frame_buffer->plane[i]| to point to the data buffers
75*09537850SAkhilesh Sanikop // of the planes, and set |frame_buffer->stride[i]| to the row strides of the
76*09537850SAkhilesh Sanikop // planes. If |image_format| is kLibgav1ImageFormatMonochrome400, the callback
77*09537850SAkhilesh Sanikop // should set |frame_buffer->plane[1]| and |frame_buffer->plane[2]| to a null
78*09537850SAkhilesh Sanikop // pointer and set |frame_buffer->stride[1]| and |frame_buffer->stride[2]| to
79*09537850SAkhilesh Sanikop // 0. The callback may set |frame_buffer->private_data| to a value that will
80*09537850SAkhilesh Sanikop // be useful to the release frame buffer callback and the consumer of a
81*09537850SAkhilesh Sanikop // DecoderBuffer.
82*09537850SAkhilesh Sanikop //
83*09537850SAkhilesh Sanikop // Returns kLibgav1StatusOk on success, an error status on failure.
84*09537850SAkhilesh Sanikop
85*09537850SAkhilesh Sanikop // |width| and |height| are the frame width and height in pixels.
86*09537850SAkhilesh Sanikop // |left_border|, |right_border|, |top_border|, and |bottom_border| are the
87*09537850SAkhilesh Sanikop // left, right, top, and bottom border sizes in pixels. |stride_alignment|
88*09537850SAkhilesh Sanikop // specifies the alignment of the row stride in bytes.
89*09537850SAkhilesh Sanikop typedef Libgav1StatusCode (*Libgav1GetFrameBufferCallback)(
90*09537850SAkhilesh Sanikop void* callback_private_data, int bitdepth, Libgav1ImageFormat image_format,
91*09537850SAkhilesh Sanikop int width, int height, int left_border, int right_border, int top_border,
92*09537850SAkhilesh Sanikop int bottom_border, int stride_alignment, Libgav1FrameBuffer* frame_buffer);
93*09537850SAkhilesh Sanikop
94*09537850SAkhilesh Sanikop // After a frame buffer is allocated, the decoder starts to write decoded video
95*09537850SAkhilesh Sanikop // to the frame buffer. When the frame buffer is ready for consumption, it is
96*09537850SAkhilesh Sanikop // made available to the application in a Decoder::DequeueFrame() call.
97*09537850SAkhilesh Sanikop // Afterwards, the decoder may continue to use the frame buffer in read-only
98*09537850SAkhilesh Sanikop // mode. When the decoder is finished using the frame buffer, it notifies the
99*09537850SAkhilesh Sanikop // application by calling the Libgav1ReleaseFrameBufferCallback.
100*09537850SAkhilesh Sanikop
101*09537850SAkhilesh Sanikop // This callback is invoked by the decoder to release a frame buffer.
102*09537850SAkhilesh Sanikop typedef void (*Libgav1ReleaseFrameBufferCallback)(void* callback_private_data,
103*09537850SAkhilesh Sanikop void* buffer_private_data);
104*09537850SAkhilesh Sanikop
105*09537850SAkhilesh Sanikop // Libgav1ComputeFrameBufferInfo() and Libgav1SetFrameBuffer() are intended to
106*09537850SAkhilesh Sanikop // help clients implement frame buffer callbacks using memory buffers. First,
107*09537850SAkhilesh Sanikop // call Libgav1ComputeFrameBufferInfo(). If it succeeds, allocate y_buffer of
108*09537850SAkhilesh Sanikop // size info.y_buffer_size and allocate u_buffer and v_buffer, both of size
109*09537850SAkhilesh Sanikop // info.uv_buffer_size. Finally, pass y_buffer, u_buffer, v_buffer, and
110*09537850SAkhilesh Sanikop // buffer_private_data to Libgav1SetFrameBuffer().
111*09537850SAkhilesh Sanikop
112*09537850SAkhilesh Sanikop // This structure contains information useful for allocating memory for a frame
113*09537850SAkhilesh Sanikop // buffer.
114*09537850SAkhilesh Sanikop typedef struct Libgav1FrameBufferInfo {
115*09537850SAkhilesh Sanikop size_t y_buffer_size; // Size in bytes of the Y buffer.
116*09537850SAkhilesh Sanikop size_t uv_buffer_size; // Size in bytes of the U or V buffer.
117*09537850SAkhilesh Sanikop
118*09537850SAkhilesh Sanikop // The following fields are consumed by Libgav1SetFrameBuffer(). Do not use
119*09537850SAkhilesh Sanikop // them directly.
120*09537850SAkhilesh Sanikop int y_stride; // Row stride in bytes of the Y buffer.
121*09537850SAkhilesh Sanikop int uv_stride; // Row stride in bytes of the U or V buffer.
122*09537850SAkhilesh Sanikop size_t y_plane_offset; // Offset in bytes of the frame (excluding the
123*09537850SAkhilesh Sanikop // borders) in the Y buffer.
124*09537850SAkhilesh Sanikop size_t uv_plane_offset; // Offset in bytes of the frame (excluding the
125*09537850SAkhilesh Sanikop // borders) in the U or V buffer.
126*09537850SAkhilesh Sanikop int stride_alignment; // The stride_alignment argument passed to
127*09537850SAkhilesh Sanikop // Libgav1ComputeFrameBufferInfo().
128*09537850SAkhilesh Sanikop } Libgav1FrameBufferInfo;
129*09537850SAkhilesh Sanikop
130*09537850SAkhilesh Sanikop // Computes the information useful for allocating memory for a frame buffer.
131*09537850SAkhilesh Sanikop // On success, stores the output in |info|.
132*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC Libgav1StatusCode Libgav1ComputeFrameBufferInfo(
133*09537850SAkhilesh Sanikop int bitdepth, Libgav1ImageFormat image_format, int width, int height,
134*09537850SAkhilesh Sanikop int left_border, int right_border, int top_border, int bottom_border,
135*09537850SAkhilesh Sanikop int stride_alignment, Libgav1FrameBufferInfo* info);
136*09537850SAkhilesh Sanikop
137*09537850SAkhilesh Sanikop // Sets the |frame_buffer| struct.
138*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC Libgav1StatusCode Libgav1SetFrameBuffer(
139*09537850SAkhilesh Sanikop const Libgav1FrameBufferInfo* info, uint8_t* y_buffer, uint8_t* u_buffer,
140*09537850SAkhilesh Sanikop uint8_t* v_buffer, void* buffer_private_data,
141*09537850SAkhilesh Sanikop Libgav1FrameBuffer* frame_buffer);
142*09537850SAkhilesh Sanikop
143*09537850SAkhilesh Sanikop #if defined(__cplusplus)
144*09537850SAkhilesh Sanikop } // extern "C"
145*09537850SAkhilesh Sanikop
146*09537850SAkhilesh Sanikop // Declare type aliases for C++.
147*09537850SAkhilesh Sanikop namespace libgav1 {
148*09537850SAkhilesh Sanikop
149*09537850SAkhilesh Sanikop using FrameBuffer = Libgav1FrameBuffer;
150*09537850SAkhilesh Sanikop using FrameBufferSizeChangedCallback = Libgav1FrameBufferSizeChangedCallback;
151*09537850SAkhilesh Sanikop using GetFrameBufferCallback = Libgav1GetFrameBufferCallback;
152*09537850SAkhilesh Sanikop using ReleaseFrameBufferCallback = Libgav1ReleaseFrameBufferCallback;
153*09537850SAkhilesh Sanikop using FrameBufferInfo = Libgav1FrameBufferInfo;
154*09537850SAkhilesh Sanikop
ComputeFrameBufferInfo(int bitdepth,ImageFormat image_format,int width,int height,int left_border,int right_border,int top_border,int bottom_border,int stride_alignment,FrameBufferInfo * info)155*09537850SAkhilesh Sanikop inline StatusCode ComputeFrameBufferInfo(int bitdepth, ImageFormat image_format,
156*09537850SAkhilesh Sanikop int width, int height, int left_border,
157*09537850SAkhilesh Sanikop int right_border, int top_border,
158*09537850SAkhilesh Sanikop int bottom_border,
159*09537850SAkhilesh Sanikop int stride_alignment,
160*09537850SAkhilesh Sanikop FrameBufferInfo* info) {
161*09537850SAkhilesh Sanikop return Libgav1ComputeFrameBufferInfo(bitdepth, image_format, width, height,
162*09537850SAkhilesh Sanikop left_border, right_border, top_border,
163*09537850SAkhilesh Sanikop bottom_border, stride_alignment, info);
164*09537850SAkhilesh Sanikop }
165*09537850SAkhilesh Sanikop
SetFrameBuffer(const FrameBufferInfo * info,uint8_t * y_buffer,uint8_t * u_buffer,uint8_t * v_buffer,void * buffer_private_data,FrameBuffer * frame_buffer)166*09537850SAkhilesh Sanikop inline StatusCode SetFrameBuffer(const FrameBufferInfo* info, uint8_t* y_buffer,
167*09537850SAkhilesh Sanikop uint8_t* u_buffer, uint8_t* v_buffer,
168*09537850SAkhilesh Sanikop void* buffer_private_data,
169*09537850SAkhilesh Sanikop FrameBuffer* frame_buffer) {
170*09537850SAkhilesh Sanikop return Libgav1SetFrameBuffer(info, y_buffer, u_buffer, v_buffer,
171*09537850SAkhilesh Sanikop buffer_private_data, frame_buffer);
172*09537850SAkhilesh Sanikop }
173*09537850SAkhilesh Sanikop
174*09537850SAkhilesh Sanikop } // namespace libgav1
175*09537850SAkhilesh Sanikop #endif // defined(__cplusplus)
176*09537850SAkhilesh Sanikop
177*09537850SAkhilesh Sanikop #endif // LIBGAV1_SRC_GAV1_FRAME_BUFFER_H_
178