blob: 52014567fe9efe27764fe1d85cba6795e4cdc39d [file] [log] [blame]
Richard Xief2932a02023-10-20 17:37:57 +00001/*
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
32//#define FILE_DUMP_ENABLE 1
33#define DUMP_FILE_PATH "/data/local/tmp/dump"
34#define INPUT_DATA_DUMP_EXT "av1"
35#define INPUT_SIZE_DUMP_EXT "size"
36#define OUTPUT_YUV_DUMP_EXT "yuv"
37
38namespace android {
39
40struct C2SoftDav1dDec : public SimpleC2Component {
41 class IntfImpl;
42
43 C2SoftDav1dDec(const char* name, c2_node_id_t id, const std::shared_ptr<IntfImpl>& intfImpl);
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 std::deque<Dav1dPicture> mDecodedPictures;
65
66 // configurations used by component in process
67 // (TODO: keep this in intf but make them internal only)
68 std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormatInfo;
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
91 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) {
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(Dav1dPicture* picture, const std::unique_ptr<C2Work>& work);
108 void getHDR10PlusInfoData(Dav1dPicture* picture, const std::unique_ptr<C2Work>& work);
109 void getVuiParams(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 // Sets |work->result| and mSignalledError. Returns false.
114 void setError(const std::unique_ptr<C2Work>& work, c2_status_t error);
115 bool allocTmpFrameBuffer(size_t size);
116 bool outputBuffer(const std::shared_ptr<C2BlockPool>& pool,
117 const std::unique_ptr<C2Work>& work);
118
119 c2_status_t drainInternal(uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool,
120 const std::unique_ptr<C2Work>& work);
121
122 void flushDav1d();
123
124#ifdef FILE_DUMP_ENABLE
125 char mInDataFileName[256];
126 char mInSizeFileName[256];
127 char mDav1dOutYuvFileName[256];
128
129 FILE* mInDataFile = nullptr;
130 FILE* mInSizeFile = nullptr;
131 FILE* mDav1dOutYuvFile = nullptr;
132
133 void writeDav1dOutYuvFile(const Dav1dPicture& p);
134
135 int num_frames_to_dump = 0;
136#endif
137
138 C2_DO_NOT_COPY(C2SoftDav1dDec);
139};
140
141} // namespace android
142
143#endif // ANDROID_C2_SOFT_DAV1D_DEC_H_