• 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 <SimpleC2Component.h>
25 
26 #include "ih264_typedefs.h"
27 #include "iv.h"
28 #include "ivd.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 ALIGN64(x)                      ((((x) + 63) >> 6) << 6)
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);
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     // TODO:This is not the right place for colorAspects functions. These should
132     // be part of c2-vndk so that they can be accessed by all video plugins
133     // until then, make them feel at home
134     bool colorAspectsDiffer(const ColorAspects &a, const ColorAspects &b);
135     void updateFinalColorAspects(
136             const ColorAspects &otherAspects, const ColorAspects &preferredAspects);
137     status_t handleColorAspectsChange();
138     c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool);
139     void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work);
140     status_t setFlushMode();
141     c2_status_t drainInternal(
142             uint32_t drainMode,
143             const std::shared_ptr<C2BlockPool> &pool,
144             const std::unique_ptr<C2Work> &work);
145     status_t resetDecoder();
146     void resetPlugin();
147     status_t deleteDecoder();
148 
149     std::shared_ptr<IntfImpl> mIntf;
150 
151     // TODO:This is not the right place for this enum. These should
152     // be part of c2-vndk so that they can be accessed by all video plugins
153     // until then, make them feel at home
154     enum {
155         kNotSupported,
156         kPreferBitstream,
157         kPreferContainer,
158     };
159 
160     iv_obj_t *mDecHandle;
161     std::shared_ptr<C2GraphicBlock> mOutBlock;
162     uint8_t *mOutBufferFlush;
163 
164     size_t mNumCores;
165     IV_COLOR_FORMAT_T mIvColorFormat;
166 
167     uint32_t mWidth;
168     uint32_t mHeight;
169     uint32_t mStride;
170     bool mSignalledOutputEos;
171     bool mSignalledError;
172 
173     // ColorAspects
174     Mutex mColorAspectsLock;
175     int mPreference;
176     ColorAspects mDefaultColorAspects;
177     ColorAspects mBitstreamColorAspects;
178     ColorAspects mFinalColorAspects;
179     bool mUpdateColorAspects;
180 
181     // profile
182     struct timeval mTimeStart;
183     struct timeval mTimeEnd;
184 #ifdef FILE_DUMP_ENABLE
185     char mInFile[200];
186 #endif /* FILE_DUMP_ENABLE */
187 
188     C2_DO_NOT_COPY(C2SoftAvcDec);
189 };
190 
191 }  // namespace android
192 
193 #endif  // ANDROID_C2_SOFT_AVC_DEC_H_
194