1 /*
2  * Copyright (C) 2018 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_HEVC_DEC_H_
18 #define ANDROID_C2_SOFT_HEVC_DEC_H_
19 
20 #include <media/stagefright/foundation/ColorUtils.h>
21 
22 #include <atomic>
23 #include <SimpleC2Component.h>
24 
25 #include "ihevc_typedefs.h"
26 #include "iv.h"
27 #include "ivd.h"
28 
29 namespace android {
30 
31 #define ivdec_api_function              ihevcd_cxa_api_function
32 #define ivdext_create_ip_t              ihevcd_cxa_create_ip_t
33 #define ivdext_create_op_t              ihevcd_cxa_create_op_t
34 #define ivdext_delete_ip_t              ihevcd_cxa_delete_ip_t
35 #define ivdext_delete_op_t              ihevcd_cxa_delete_op_t
36 #define ivdext_ctl_set_num_cores_ip_t   ihevcd_cxa_ctl_set_num_cores_ip_t
37 #define ivdext_ctl_set_num_cores_op_t   ihevcd_cxa_ctl_set_num_cores_op_t
38 #define ivdext_ctl_get_vui_params_ip_t  ihevcd_cxa_ctl_get_vui_params_ip_t
39 #define ivdext_ctl_get_vui_params_op_t  ihevcd_cxa_ctl_get_vui_params_op_t
40 #define ALIGN64(x)                      ((((x) + 63) >> 6) << 6)
41 #define MAX_NUM_CORES                   4
42 #define IVDEXT_CMD_CTL_SET_NUM_CORES    \
43         (IVD_CONTROL_API_COMMAND_TYPE_T)IHEVCD_CXA_CMD_CTL_SET_NUM_CORES
44 #define MIN(a, b)                       (((a) < (b)) ? (a) : (b))
45 #define GETTIME(a, b)                   gettimeofday(a, b);
46 #define TIME_DIFF(start, end, diff)     \
47     diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \
48             ((end).tv_usec - (start).tv_usec);
49 
50 
51 struct C2SoftHevcDec : public SimpleC2Component {
52     class IntfImpl;
53 
54     C2SoftHevcDec(const char* name, c2_node_id_t id,
55                   const std::shared_ptr<IntfImpl>& intfImpl);
56     virtual ~C2SoftHevcDec();
57 
58     // From SimpleC2Component
59     c2_status_t onInit() override;
60     c2_status_t onStop() override;
61     void onReset() override;
62     void onRelease() override;
63     c2_status_t onFlush_sm() override;
64     void process(
65             const std::unique_ptr<C2Work> &work,
66             const std::shared_ptr<C2BlockPool> &pool) override;
67     c2_status_t drain(
68             uint32_t drainMode,
69             const std::shared_ptr<C2BlockPool> &pool) override;
70  private:
71     status_t createDecoder();
72     status_t setNumCores();
73     status_t setParams(size_t stride, IVD_VIDEO_DECODE_MODE_T dec_mode);
74     status_t getVersion();
75     status_t initDecoder();
76     bool setDecodeArgs(ivd_video_decode_ip_t *ps_decode_ip,
77                        ivd_video_decode_op_t *ps_decode_op,
78                        C2ReadView *inBuffer,
79                        C2GraphicView *outBuffer,
80                        size_t inOffset,
81                        size_t inSize,
82                        uint32_t tsMarker);
83     bool getVuiParams();
84     // TODO:This is not the right place for colorAspects functions. These should
85     // be part of c2-vndk so that they can be accessed by all video plugins
86     // until then, make them feel at home
87     bool colorAspectsDiffer(const ColorAspects &a, const ColorAspects &b);
88     void updateFinalColorAspects(
89             const ColorAspects &otherAspects, const ColorAspects &preferredAspects);
90     status_t handleColorAspectsChange();
91     c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool);
92     void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work);
93     status_t setFlushMode();
94     c2_status_t drainInternal(
95             uint32_t drainMode,
96             const std::shared_ptr<C2BlockPool> &pool,
97             const std::unique_ptr<C2Work> &work);
98     status_t resetDecoder();
99     void resetPlugin();
100     status_t deleteDecoder();
101 
102     // TODO:This is not the right place for this enum. These should
103     // be part of c2-vndk so that they can be accessed by all video plugins
104     // until then, make them feel at home
105     enum {
106         kNotSupported,
107         kPreferBitstream,
108         kPreferContainer,
109     };
110 
111     std::shared_ptr<IntfImpl> mIntf;
112     iv_obj_t *mDecHandle;
113     std::shared_ptr<C2GraphicBlock> mOutBlock;
114     uint8_t *mOutBufferFlush;
115 
116     size_t mNumCores;
117     IV_COLOR_FORMAT_T mIvColorformat;
118 
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 
127     // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid
128     // converting them to C2 values for each frame
129     struct VuiColorAspects {
130         uint8_t primaries;
131         uint8_t transfer;
132         uint8_t coeffs;
133         uint8_t fullRange;
134 
135         // default color aspects
VuiColorAspectsC2SoftHevcDec::VuiColorAspects136         VuiColorAspects()
137             : primaries(2), transfer(2), coeffs(2), fullRange(0) { }
138 
139         bool operator==(const VuiColorAspects &o) {
140             return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs
141                     && fullRange == o.fullRange;
142         }
143     } mBitstreamColorAspects;
144 
145     // profile
146     struct timeval mTimeStart;
147     struct timeval mTimeEnd;
148 
149     C2_DO_NOT_COPY(C2SoftHevcDec);
150 };
151 
152 }  // namespace android
153 
154 #endif  // ANDROID_C2_SOFT_HEVC_DEC_H_
155