1 /*
2  * Copyright (C) 2023 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_DAV1D_DEC_H_
18 #define ANDROID_C2_SOFT_DAV1D_DEC_H_
19 
20 #include <inttypes.h>
21 
22 #include <memory>
23 
24 #include <media/stagefright/foundation/ColorUtils.h>
25 
26 #include <C2Config.h>
27 #include <SimpleC2Component.h>
28 
29 #include <dav1d/dav1d.h>
30 #include <deque>
31 #include <C2SoftDav1dDump.h>
32 
33 //#define FILE_DUMP_ENABLE 1
34 
35 namespace android {
36 
37 struct C2SoftDav1dDec : public SimpleC2Component {
38     class IntfImpl;
39 
40     C2SoftDav1dDec(const char* name, c2_node_id_t id, const std::shared_ptr<IntfImpl>& intfImpl);
41     ~C2SoftDav1dDec();
42 
43     // Begin SimpleC2Component overrides.
44     c2_status_t onInit() override;
45     c2_status_t onStop() override;
46     void onReset() override;
47     void onRelease() override;
48     c2_status_t onFlush_sm() override;
49     void process(const std::unique_ptr<C2Work>& work,
50                  const std::shared_ptr<C2BlockPool>& pool) override;
51     c2_status_t drain(uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool) override;
52     // End SimpleC2Component overrides.
53 
54   private:
55     std::shared_ptr<IntfImpl> mIntf;
56 
57     int mInputBufferIndex = 0;
58     int mOutputBufferIndex = 0;
59 
60     Dav1dContext* mDav1dCtx = nullptr;
61 
62     // configurations used by component in process
63     // (TODO: keep this in intf but make them internal only)
64     std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormatInfo;
65     std::shared_ptr<C2PortActualDelayTuning::output> mActualOutputDelayInfo;
66 
67     uint32_t mHalPixelFormat;
68     uint32_t mWidth;
69     uint32_t mHeight;
70     bool mSignalledOutputEos;
71     bool mSignalledError;
72     // Used during 10-bit I444/I422 to 10-bit P010 & 8-bit I420 conversions.
73     std::unique_ptr<uint16_t[]> mTmpFrameBuffer;
74     size_t mTmpFrameBufferSize = 0;
75 
76     C2StreamHdrStaticMetadataInfo::output mHdrStaticMetadataInfo;
77     std::unique_ptr<C2StreamHdr10PlusInfo::output> mHdr10PlusInfo = nullptr;
78 
79     // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid
80     // converting them to C2 values for each frame
81     struct VuiColorAspects {
82         uint8_t primaries;
83         uint8_t transfer;
84         uint8_t coeffs;
85         uint8_t fullRange;
86 
87         // default color aspects
VuiColorAspectsC2SoftDav1dDec::VuiColorAspects88         VuiColorAspects()
89             : primaries(C2Color::PRIMARIES_UNSPECIFIED),
90               transfer(C2Color::TRANSFER_UNSPECIFIED),
91               coeffs(C2Color::MATRIX_UNSPECIFIED),
92               fullRange(C2Color::RANGE_UNSPECIFIED) {}
93 
94         bool operator==(const VuiColorAspects& o) {
95             return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs &&
96                    fullRange == o.fullRange;
97         }
98     } mBitstreamColorAspects;
99 
100     nsecs_t mTimeStart = 0;  // Time at the start of decode()
101     nsecs_t mTimeEnd = 0;    // Time at the end of decode()
102 
103     bool initDecoder();
104     void getHDRStaticParams(const Dav1dPicture* picture, const std::unique_ptr<C2Work>& work);
105     void getHDR10PlusInfoData(const Dav1dPicture* picture, const std::unique_ptr<C2Work>& work);
106     void getVuiParams(const Dav1dPicture* picture);
107     void destroyDecoder();
108     void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work,
109                     const std::shared_ptr<C2GraphicBlock>& block,
110                     const Dav1dPicture &img);
111     // Sets |work->result| and mSignalledError. Returns false.
112     void setError(const std::unique_ptr<C2Work>& work, c2_status_t error);
113     bool allocTmpFrameBuffer(size_t size);
114     bool outputBuffer(const std::shared_ptr<C2BlockPool>& pool,
115                       const std::unique_ptr<C2Work>& work);
116 
117     c2_status_t drainInternal(uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool,
118                               const std::unique_ptr<C2Work>& work);
119 
120     void flushDav1d();
121 
122 #ifdef FILE_DUMP_ENABLE
123     C2SoftDav1dDump mC2SoftDav1dDump;
124 #endif
125 
126     C2_DO_NOT_COPY(C2SoftDav1dDec);
127 };
128 
129 }  // namespace android
130 
131 #endif  // ANDROID_C2_SOFT_DAV1D_DEC_H_
132