xref: /aosp_15_r20/external/libgav1/src/gav1/decoder.h (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1*09537850SAkhilesh Sanikop /*
2*09537850SAkhilesh Sanikop  * Copyright 2019 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_DECODER_H_
18*09537850SAkhilesh Sanikop #define LIBGAV1_SRC_GAV1_DECODER_H_
19*09537850SAkhilesh Sanikop 
20*09537850SAkhilesh Sanikop #if defined(__cplusplus)
21*09537850SAkhilesh Sanikop #include <cstddef>
22*09537850SAkhilesh Sanikop #include <cstdint>
23*09537850SAkhilesh Sanikop #include <memory>
24*09537850SAkhilesh Sanikop #else
25*09537850SAkhilesh Sanikop #include <stddef.h>
26*09537850SAkhilesh Sanikop #include <stdint.h>
27*09537850SAkhilesh Sanikop #endif  // defined(__cplusplus)
28*09537850SAkhilesh Sanikop 
29*09537850SAkhilesh Sanikop // IWYU pragma: begin_exports
30*09537850SAkhilesh Sanikop #include "gav1/decoder_buffer.h"
31*09537850SAkhilesh Sanikop #include "gav1/decoder_settings.h"
32*09537850SAkhilesh Sanikop #include "gav1/frame_buffer.h"
33*09537850SAkhilesh Sanikop #include "gav1/status_code.h"
34*09537850SAkhilesh Sanikop #include "gav1/symbol_visibility.h"
35*09537850SAkhilesh Sanikop #include "gav1/version.h"
36*09537850SAkhilesh Sanikop // IWYU pragma: end_exports
37*09537850SAkhilesh Sanikop 
38*09537850SAkhilesh Sanikop #if defined(__cplusplus)
39*09537850SAkhilesh Sanikop extern "C" {
40*09537850SAkhilesh Sanikop #endif
41*09537850SAkhilesh Sanikop 
42*09537850SAkhilesh Sanikop struct Libgav1Decoder;
43*09537850SAkhilesh Sanikop typedef struct Libgav1Decoder Libgav1Decoder;
44*09537850SAkhilesh Sanikop 
45*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC Libgav1StatusCode Libgav1DecoderCreate(
46*09537850SAkhilesh Sanikop     const Libgav1DecoderSettings* settings, Libgav1Decoder** decoder_out);
47*09537850SAkhilesh Sanikop 
48*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC void Libgav1DecoderDestroy(Libgav1Decoder* decoder);
49*09537850SAkhilesh Sanikop 
50*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC Libgav1StatusCode Libgav1DecoderEnqueueFrame(
51*09537850SAkhilesh Sanikop     Libgav1Decoder* decoder, const uint8_t* data, size_t size,
52*09537850SAkhilesh Sanikop     int64_t user_private_data, void* buffer_private_data);
53*09537850SAkhilesh Sanikop 
54*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC Libgav1StatusCode Libgav1DecoderDequeueFrame(
55*09537850SAkhilesh Sanikop     Libgav1Decoder* decoder, const Libgav1DecoderBuffer** out_ptr);
56*09537850SAkhilesh Sanikop 
57*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC Libgav1StatusCode
58*09537850SAkhilesh Sanikop Libgav1DecoderSignalEOS(Libgav1Decoder* decoder);
59*09537850SAkhilesh Sanikop 
60*09537850SAkhilesh Sanikop LIBGAV1_PUBLIC int Libgav1DecoderGetMaxBitdepth(void);
61*09537850SAkhilesh Sanikop 
62*09537850SAkhilesh Sanikop #if defined(__cplusplus)
63*09537850SAkhilesh Sanikop }  // extern "C"
64*09537850SAkhilesh Sanikop 
65*09537850SAkhilesh Sanikop namespace libgav1 {
66*09537850SAkhilesh Sanikop 
67*09537850SAkhilesh Sanikop // Forward declaration.
68*09537850SAkhilesh Sanikop class DecoderImpl;
69*09537850SAkhilesh Sanikop 
70*09537850SAkhilesh Sanikop class LIBGAV1_PUBLIC Decoder {
71*09537850SAkhilesh Sanikop  public:
72*09537850SAkhilesh Sanikop   Decoder();
73*09537850SAkhilesh Sanikop   ~Decoder();
74*09537850SAkhilesh Sanikop 
75*09537850SAkhilesh Sanikop   // Init must be called exactly once per instance. Subsequent calls will do
76*09537850SAkhilesh Sanikop   // nothing. If |settings| is nullptr, the decoder will be initialized with
77*09537850SAkhilesh Sanikop   // default settings. Returns kStatusOk on success, an error status otherwise.
78*09537850SAkhilesh Sanikop   StatusCode Init(const DecoderSettings* settings);
79*09537850SAkhilesh Sanikop 
80*09537850SAkhilesh Sanikop   // Enqueues a compressed frame to be decoded.
81*09537850SAkhilesh Sanikop   //
82*09537850SAkhilesh Sanikop   // This function returns:
83*09537850SAkhilesh Sanikop   //   * kStatusOk on success
84*09537850SAkhilesh Sanikop   //   * kStatusTryAgain if the decoder queue is full
85*09537850SAkhilesh Sanikop   //   * an error status otherwise.
86*09537850SAkhilesh Sanikop   //
87*09537850SAkhilesh Sanikop   // |user_private_data| may be used to associate application specific private
88*09537850SAkhilesh Sanikop   // data with the compressed frame. It will be copied to the user_private_data
89*09537850SAkhilesh Sanikop   // field of the DecoderBuffer returned by the corresponding |DequeueFrame()|
90*09537850SAkhilesh Sanikop   // call.
91*09537850SAkhilesh Sanikop   //
92*09537850SAkhilesh Sanikop   // NOTE: |EnqueueFrame()| does not copy the data. Therefore, after a
93*09537850SAkhilesh Sanikop   // successful |EnqueueFrame()| call, the caller must keep the |data| buffer
94*09537850SAkhilesh Sanikop   // alive until:
95*09537850SAkhilesh Sanikop   // 1) If |settings_.release_input_buffer| is not nullptr, then |data| buffer
96*09537850SAkhilesh Sanikop   // must be kept alive until release_input_buffer is called with the
97*09537850SAkhilesh Sanikop   // |buffer_private_data| passed into this EnqueueFrame call.
98*09537850SAkhilesh Sanikop   // 2) If |settings_.release_input_buffer| is nullptr, then |data| buffer must
99*09537850SAkhilesh Sanikop   // be kept alive until the corresponding DequeueFrame() call is completed.
100*09537850SAkhilesh Sanikop   //
101*09537850SAkhilesh Sanikop   // If the call to |EnqueueFrame()| is not successful, then libgav1 will not
102*09537850SAkhilesh Sanikop   // hold any references to the |data| buffer. |settings_.release_input_buffer|
103*09537850SAkhilesh Sanikop   // callback will not be called in that case.
104*09537850SAkhilesh Sanikop   StatusCode EnqueueFrame(const uint8_t* data, size_t size,
105*09537850SAkhilesh Sanikop                           int64_t user_private_data, void* buffer_private_data);
106*09537850SAkhilesh Sanikop 
107*09537850SAkhilesh Sanikop   // Dequeues a decompressed frame. If there are enqueued compressed frames,
108*09537850SAkhilesh Sanikop   // decodes one and sets |*out_ptr| to the last displayable frame in the
109*09537850SAkhilesh Sanikop   // compressed frame. If there are no displayable frames available, sets
110*09537850SAkhilesh Sanikop   // |*out_ptr| to nullptr.
111*09537850SAkhilesh Sanikop   //
112*09537850SAkhilesh Sanikop   // Returns kStatusOk on success. Returns kStatusNothingToDequeue if there are
113*09537850SAkhilesh Sanikop   // no enqueued frames (in this case out_ptr will always be set to nullptr).
114*09537850SAkhilesh Sanikop   // Returns one of the other error statuses if there is an error.
115*09537850SAkhilesh Sanikop   //
116*09537850SAkhilesh Sanikop   // If |settings_.blocking_dequeue| is false and the decoder is operating in
117*09537850SAkhilesh Sanikop   // frame parallel mode (|settings_.frame_parallel| is true and the video
118*09537850SAkhilesh Sanikop   // stream passes the decoder's heuristics for enabling frame parallel mode),
119*09537850SAkhilesh Sanikop   // then this call will return kStatusTryAgain if an enqueued frame is not yet
120*09537850SAkhilesh Sanikop   // decoded (it is a non blocking call in this case). In all other cases, this
121*09537850SAkhilesh Sanikop   // call will block until an enqueued frame has been decoded.
122*09537850SAkhilesh Sanikop   StatusCode DequeueFrame(const DecoderBuffer** out_ptr);
123*09537850SAkhilesh Sanikop 
124*09537850SAkhilesh Sanikop   // Signals the end of stream.
125*09537850SAkhilesh Sanikop   //
126*09537850SAkhilesh Sanikop   // In non-frame-parallel mode, this function will release all the frames held
127*09537850SAkhilesh Sanikop   // by the decoder. If the frame buffers were allocated by libgav1, then the
128*09537850SAkhilesh Sanikop   // pointer obtained by the prior DequeueFrame call will no longer be valid. If
129*09537850SAkhilesh Sanikop   // the frame buffers were allocated by the application, then any references
130*09537850SAkhilesh Sanikop   // that libgav1 is holding on to will be released.
131*09537850SAkhilesh Sanikop   //
132*09537850SAkhilesh Sanikop   // Once this function returns successfully, the decoder state will be reset
133*09537850SAkhilesh Sanikop   // and the decoder is ready to start decoding a new coded video sequence.
134*09537850SAkhilesh Sanikop   StatusCode SignalEOS();
135*09537850SAkhilesh Sanikop 
136*09537850SAkhilesh Sanikop   // Returns the maximum bitdepth that is supported by this decoder.
137*09537850SAkhilesh Sanikop   static int GetMaxBitdepth();
138*09537850SAkhilesh Sanikop 
139*09537850SAkhilesh Sanikop  private:
140*09537850SAkhilesh Sanikop   DecoderSettings settings_;
141*09537850SAkhilesh Sanikop   // The object is initialized if and only if impl_ != nullptr.
142*09537850SAkhilesh Sanikop   std::unique_ptr<DecoderImpl> impl_;
143*09537850SAkhilesh Sanikop };
144*09537850SAkhilesh Sanikop 
145*09537850SAkhilesh Sanikop }  // namespace libgav1
146*09537850SAkhilesh Sanikop #endif  // defined(__cplusplus)
147*09537850SAkhilesh Sanikop 
148*09537850SAkhilesh Sanikop #endif  // LIBGAV1_SRC_GAV1_DECODER_H_
149