1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_C2_SOFT_GAV1_DEC_H_
18 #define ANDROID_C2_SOFT_GAV1_DEC_H_
19 
20 #include <inttypes.h>
21 
22 #include <memory>
23 
24 #include <media/stagefright/foundation/ColorUtils.h>
25 
26 #include <SimpleC2Component.h>
27 #include <C2Config.h>
28 #include <gav1/decoder.h>
29 #include <gav1/decoder_settings.h>
30 
31 namespace android {
32 
33 struct C2SoftGav1Dec : public SimpleC2Component {
34   class IntfImpl;
35 
36   C2SoftGav1Dec(const char* name, c2_node_id_t id,
37                 const std::shared_ptr<IntfImpl>& intfImpl);
38   ~C2SoftGav1Dec();
39 
40   // Begin SimpleC2Component overrides.
41   c2_status_t onInit() override;
42   c2_status_t onStop() override;
43   void onReset() override;
44   void onRelease() override;
45   c2_status_t onFlush_sm() override;
46   void process(const std::unique_ptr<C2Work>& work,
47                const std::shared_ptr<C2BlockPool>& pool) override;
48   c2_status_t drain(uint32_t drainMode,
49                     const std::shared_ptr<C2BlockPool>& pool) override;
50   // End SimpleC2Component overrides.
51 
52  private:
53   std::shared_ptr<IntfImpl> mIntf;
54   std::unique_ptr<libgav1::Decoder> mCodecCtx;
55 
56   // configurations used by component in process
57   // (TODO: keep this in intf but make them internal only)
58   std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormatInfo;
59 
60   uint32_t mHalPixelFormat;
61   uint32_t mWidth;
62   uint32_t mHeight;
63   bool mSignalledOutputEos;
64   bool mSignalledError;
65   // Used during 10-bit I444/I422 to 10-bit P010 & 8-bit I420 conversions.
66   std::unique_ptr<uint16_t[]> mTmpFrameBuffer;
67   size_t mTmpFrameBufferSize = 0;
68 
69   C2StreamHdrStaticMetadataInfo::output mHdrStaticMetadataInfo;
70   std::unique_ptr<C2StreamHdr10PlusInfo::output> mHdr10PlusInfo = nullptr;
71 
72   // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid
73   // converting them to C2 values for each frame
74   struct VuiColorAspects {
75       uint8_t primaries;
76       uint8_t transfer;
77       uint8_t coeffs;
78       uint8_t fullRange;
79 
80       // default color aspects
VuiColorAspectsC2SoftGav1Dec::VuiColorAspects81       VuiColorAspects()
82           : primaries(C2Color::PRIMARIES_UNSPECIFIED),
83             transfer(C2Color::TRANSFER_UNSPECIFIED),
84             coeffs(C2Color::MATRIX_UNSPECIFIED),
85             fullRange(C2Color::RANGE_UNSPECIFIED) { }
86 
87       bool operator==(const VuiColorAspects &o) {
88           return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs
89                   && fullRange == o.fullRange;
90       }
91   } mBitstreamColorAspects;
92 
93   nsecs_t mTimeStart = 0;  // Time at the start of decode()
94   nsecs_t mTimeEnd = 0;    // Time at the end of decode()
95 
96   bool initDecoder();
97   void getHDRStaticParams(const libgav1::DecoderBuffer *buffer,
98                   const std::unique_ptr<C2Work> &work);
99   void getHDR10PlusInfoData(const libgav1::DecoderBuffer *buffer,
100                   const std::unique_ptr<C2Work> &work);
101   void getVuiParams(const libgav1::DecoderBuffer *buffer);
102   void destroyDecoder();
103   void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work,
104                   const std::shared_ptr<C2GraphicBlock>& block);
105   // Sets |work->result| and mSignalledError. Returns false.
106   void setError(const std::unique_ptr<C2Work> &work, c2_status_t error);
107   bool allocTmpFrameBuffer(size_t size);
108   bool fillMonochromeRow(int value);
109   bool outputBuffer(const std::shared_ptr<C2BlockPool>& pool,
110                     const std::unique_ptr<C2Work>& work);
111   c2_status_t drainInternal(uint32_t drainMode,
112                             const std::shared_ptr<C2BlockPool>& pool,
113                             const std::unique_ptr<C2Work>& work);
114 
115   C2_DO_NOT_COPY(C2SoftGav1Dec);
116 };
117 
118 }  // namespace android
119 
120 #endif  // ANDROID_C2_SOFT_GAV1_DEC_H_
121