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