• 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 <atomic>
25 #include <SimpleC2Component.h>
26 
27 #include "ih264_typedefs.h"
28 #include "ih264d.h"
29 
30 namespace android {
31 
32 #define ivdec_api_function              ih264d_api_function
33 #define ivdext_create_ip_t              ih264d_create_ip_t
34 #define ivdext_create_op_t              ih264d_create_op_t
35 #define ivdext_delete_ip_t              ih264d_delete_ip_t
36 #define ivdext_delete_op_t              ih264d_delete_op_t
37 #define ivdext_ctl_set_num_cores_ip_t   ih264d_ctl_set_num_cores_ip_t
38 #define ivdext_ctl_set_num_cores_op_t   ih264d_ctl_set_num_cores_op_t
39 #define ivdext_ctl_get_vui_params_ip_t  ih264d_ctl_get_vui_params_ip_t
40 #define ivdext_ctl_get_vui_params_op_t  ih264d_ctl_get_vui_params_op_t
41 #define ALIGN32(x)                      ((((x) + 31) >> 5) << 5)
42 #define MAX_NUM_CORES                   4
43 #define IVDEXT_CMD_CTL_SET_NUM_CORES    \
44         (IVD_CONTROL_API_COMMAND_TYPE_T)IH264D_CMD_CTL_SET_NUM_CORES
45 #define MIN(a, b)                       (((a) < (b)) ? (a) : (b))
46 #define GETTIME(a, b)                   gettimeofday(a, b);
47 #define TIME_DIFF(start, end, diff)     \
48     diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \
49             ((end).tv_usec - (start).tv_usec);
50 
51 #ifdef FILE_DUMP_ENABLE
52     #define INPUT_DUMP_PATH     "/sdcard/clips/avcd_input"
53     #define INPUT_DUMP_EXT      "h264"
54     #define GENERATE_FILE_NAMES() {                         \
55         GETTIME(&mTimeStart, NULL);                         \
56         strcpy(mInFile, "");                                \
57         sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH,  \
58                 mTimeStart.tv_sec, mTimeStart.tv_usec,      \
59                 INPUT_DUMP_EXT);                            \
60     }
61     #define CREATE_DUMP_FILE(m_filename) {                  \
62         FILE *fp = fopen(m_filename, "wb");                 \
63         if (fp != NULL) {                                   \
64             fclose(fp);                                     \
65         } else {                                            \
66             ALOGD("Could not open file %s", m_filename);    \
67         }                                                   \
68     }
69     #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset)\
70     {                                                       \
71         FILE *fp = fopen(m_filename, "ab");                 \
72         if (fp != NULL && m_buf != NULL && m_offset == 0) { \
73             int i;                                          \
74             i = fwrite(m_buf, 1, m_size, fp);               \
75             ALOGD("fwrite ret %d to write %d", i, m_size);  \
76             if (i != (int) m_size) {                        \
77                 ALOGD("Error in fwrite, returned %d", i);   \
78                 perror("Error in write to file");           \
79             }                                               \
80         } else if (fp == NULL) {                            \
81             ALOGD("Could not write to file %s", m_filename);\
82         }                                                   \
83         if (fp) {                                           \
84             fclose(fp);                                     \
85         }                                                   \
86     }
87 #else /* FILE_DUMP_ENABLE */
88     #define INPUT_DUMP_PATH
89     #define INPUT_DUMP_EXT
90     #define OUTPUT_DUMP_PATH
91     #define OUTPUT_DUMP_EXT
92     #define GENERATE_FILE_NAMES()
93     #define CREATE_DUMP_FILE(m_filename)
94     #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset)
95 #endif /* FILE_DUMP_ENABLE */
96 
97 
98 class C2SoftAvcDec : public SimpleC2Component {
99 public:
100     class IntfImpl;
101     C2SoftAvcDec(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
102     virtual ~C2SoftAvcDec();
103 
104     // From SimpleC2Component
105     c2_status_t onInit() override;
106     c2_status_t onStop() override;
107     void onReset() override;
108     void onRelease() override;
109     c2_status_t onFlush_sm() override;
110     void process(
111             const std::unique_ptr<C2Work> &work,
112             const std::shared_ptr<C2BlockPool> &pool) override;
113     c2_status_t drain(
114             uint32_t drainMode,
115             const std::shared_ptr<C2BlockPool> &pool) override;
116 
117 private:
118     status_t createDecoder();
119     status_t setNumCores();
120     status_t setParams(size_t stride, IVD_VIDEO_DECODE_MODE_T dec_mode);
121     void getVersion();
122     status_t initDecoder();
123     bool setDecodeArgs(ivd_video_decode_ip_t *ps_decode_ip,
124                        ivd_video_decode_op_t *ps_decode_op,
125                        C2ReadView *inBuffer,
126                        C2GraphicView *outBuffer,
127                        size_t inOffset,
128                        size_t inSize,
129                        uint32_t tsMarker);
130     bool getVuiParams();
131     c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool);
132     void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work);
133     status_t setFlushMode();
134     c2_status_t drainInternal(
135             uint32_t drainMode,
136             const std::shared_ptr<C2BlockPool> &pool,
137             const std::unique_ptr<C2Work> &work);
138     status_t resetDecoder();
139     void resetPlugin();
140     status_t deleteDecoder();
141 
142     std::shared_ptr<IntfImpl> mIntf;
143 
144     // TODO:This is not the right place for this enum. These should
145     // be part of c2-vndk so that they can be accessed by all video plugins
146     // until then, make them feel at home
147     enum {
148         kNotSupported,
149         kPreferBitstream,
150         kPreferContainer,
151     };
152 
153     iv_obj_t *mDecHandle;
154     std::shared_ptr<C2GraphicBlock> mOutBlock;
155     uint8_t *mOutBufferFlush;
156 
157     size_t mNumCores;
158     IV_COLOR_FORMAT_T mIvColorFormat;
159     uint32_t mOutputDelay;
160     uint32_t mWidth;
161     uint32_t mHeight;
162     uint32_t mStride;
163     bool mSignalledOutputEos;
164     bool mSignalledError;
165     bool mHeaderDecoded;
166     std::atomic_uint64_t mOutIndex;
167     // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid
168     // converting them to C2 values for each frame
169     struct VuiColorAspects {
170         uint8_t primaries;
171         uint8_t transfer;
172         uint8_t coeffs;
173         uint8_t fullRange;
174 
175         // default color aspects
VuiColorAspectsVuiColorAspects176         VuiColorAspects()
177             : primaries(2), transfer(2), coeffs(2), fullRange(0) { }
178 
179         bool operator==(const VuiColorAspects &o) {
180             return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs
181                     && fullRange == o.fullRange;
182         }
183     } mBitstreamColorAspects;
184 
185     // profile
186     struct timeval mTimeStart;
187     struct timeval mTimeEnd;
188 #ifdef FILE_DUMP_ENABLE
189     char mInFile[200];
190 #endif /* FILE_DUMP_ENABLE */
191 
192     C2_DO_NOT_COPY(C2SoftAvcDec);
193 };
194 
195 }  // namespace android
196 
197 #endif  // ANDROID_C2_SOFT_AVC_DEC_H_
198