• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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_AVC_DEC_H_
18 #define ANDROID_C2_SOFT_AVC_DEC_H_
19 
20 #include <sys/time.h>
21 
22 #include <media/stagefright/foundation/ColorUtils.h>
23 
24 #include "MediaH264Decoder.h"
25 #include <SimpleC2Component.h>
26 #include <atomic>
27 #include <map>
28 
29 namespace android {
30 
31 #define ALIGN2(x) ((((x) + 1) >> 1) << 1)
32 #define ALIGN8(x) ((((x) + 7) >> 3) << 3)
33 #define ALIGN16(x) ((((x) + 15) >> 4) << 4)
34 #define ALIGN32(x) ((((x) + 31) >> 5) << 5)
35 #define MAX_NUM_CORES 4
36 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
37 #define GETTIME(a, b) gettimeofday(a, b);
38 #define TIME_DIFF(start, end, diff)                                            \
39     diff = (((end).tv_sec - (start).tv_sec) * 1000000) +                       \
40            ((end).tv_usec - (start).tv_usec);
41 
42 class C2GoldfishAvcDec : public SimpleC2Component {
43   public:
44     class IntfImpl;
45     C2GoldfishAvcDec(const char *name, c2_node_id_t id,
46                      const std::shared_ptr<IntfImpl> &intfImpl);
47     virtual ~C2GoldfishAvcDec();
48 
49     // From SimpleC2Component
50     c2_status_t onInit() override;
51     c2_status_t onStop() override;
52     void onReset() override;
53     void onRelease() override;
54     c2_status_t onFlush_sm() override;
55     void process(const std::unique_ptr<C2Work> &work,
56                  const std::shared_ptr<C2BlockPool> &pool) override;
57     c2_status_t drain(uint32_t drainMode,
58                       const std::shared_ptr<C2BlockPool> &pool) override;
59 
60   private:
61     std::unique_ptr<MediaH264Decoder> mContext;
62     bool mEnableAndroidNativeBuffers{true};
63 
64     void checkMode(const std::shared_ptr<C2BlockPool> &pool);
65     //    status_t createDecoder();
66     status_t createDecoder();
67     status_t setParams(size_t stride);
68     status_t initDecoder();
69     bool setDecodeArgs(C2ReadView *inBuffer, C2GraphicView *outBuffer,
70                        size_t inOffset, size_t inSize, uint32_t tsMarker);
71     c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool);
72     void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work);
73     status_t setFlushMode();
74     c2_status_t drainInternal(uint32_t drainMode,
75                               const std::shared_ptr<C2BlockPool> &pool,
76                               const std::unique_ptr<C2Work> &work);
77     status_t resetDecoder();
78     void resetPlugin();
79     void deleteContext();
80 
81     std::shared_ptr<IntfImpl> mIntf;
82 
83     void removePts(uint64_t pts);
84     void insertPts(uint32_t work_index, uint64_t pts);
85     uint64_t getWorkIndex(uint64_t pts);
86 
87     // there are same pts matching to different work indices
88     // this happen during csd0/csd1 switching
89     std::map<uint64_t, uint64_t> mOldPts2Index;
90     std::map<uint64_t, uint64_t> mPts2Index;
91     std::map<uint64_t, uint64_t> mIndex2Pts;
92     uint64_t  mPts {0};
93 
94     // TODO:This is not the right place for this enum. These should
95     // be part of c2-vndk so that they can be accessed by all video plugins
96     // until then, make them feel at home
97     enum {
98         kNotSupported,
99         kPreferBitstream,
100         kPreferContainer,
101     };
102 
103     std::shared_ptr<C2GraphicBlock> mOutBlock;
104     uint8_t *mOutBufferFlush;
105 
106     int mHostColorBufferId{-1};
107 
108     void getVuiParams(h264_image_t &img);
109     void copyImageData(h264_image_t &img);
110 
111     h264_image_t mImg{};
112     uint32_t mConsumedBytes{0};
113     uint8_t *mInPBuffer{nullptr};
114     uint32_t mInPBufferSize;
115     uint32_t mInTsMarker;
116 
117     // size_t mNumCores;
118     // uint32_t mOutputDelay;
119     uint32_t mWidth;
120     uint32_t mHeight;
121     uint32_t mStride;
122     bool mSignalledOutputEos;
123     bool mSignalledError;
124     bool mHeaderDecoded;
125     std::atomic_uint64_t mOutIndex;
126     // Color aspects. These are ISO values and are meant to detect changes in
127     // aspects to avoid converting them to C2 values for each frame
128     struct VuiColorAspects {
129         uint8_t primaries;
130         uint8_t transfer;
131         uint8_t coeffs;
132         uint8_t fullRange;
133 
134         // default color aspects
VuiColorAspectsVuiColorAspects135         VuiColorAspects()
136             : primaries(2), transfer(2), coeffs(2), fullRange(0) {}
137 
138         bool operator==(const VuiColorAspects &o) {
139             return primaries == o.primaries && transfer == o.transfer &&
140                    coeffs == o.coeffs && fullRange == o.fullRange;
141         }
142     } mBitstreamColorAspects;
143 
144     // profile
145     struct timeval mTimeStart;
146     struct timeval mTimeEnd;
147 #ifdef FILE_DUMP_ENABLE
148     char mInFile[200];
149 #endif /* FILE_DUMP_ENABLE */
150 
151     std::vector<uint8_t> mCsd0;
152     std::vector<uint8_t> mCsd1;
153     void decodeHeaderAfterFlush();
154 
155     C2_DO_NOT_COPY(C2GoldfishAvcDec);
156 };
157 
158 } // namespace android
159 
160 #endif // ANDROID_C2_SOFT_AVC_DEC_H_
161