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_MPEG2_DEC_H_
18 #define ANDROID_C2_SOFT_MPEG2_DEC_H_
19 
20 #include <SimpleC2Component.h>
21 
22 #include <media/stagefright/foundation/ColorUtils.h>
23 
24 #include "iv_datatypedef.h"
25 #include "iv.h"
26 #include "ivd.h"
27 
28 namespace android {
29 
30 #define ivdec_api_function              impeg2d_api_function
31 #define ivdext_init_ip_t                impeg2d_init_ip_t
32 #define ivdext_init_op_t                impeg2d_init_op_t
33 #define ivdext_fill_mem_rec_ip_t        impeg2d_fill_mem_rec_ip_t
34 #define ivdext_fill_mem_rec_op_t        impeg2d_fill_mem_rec_op_t
35 #define ivdext_ctl_set_num_cores_ip_t   impeg2d_ctl_set_num_cores_ip_t
36 #define ivdext_ctl_set_num_cores_op_t   impeg2d_ctl_set_num_cores_op_t
37 #define ivdext_ctl_get_seq_info_ip_t    impeg2d_ctl_get_seq_info_ip_t
38 #define ivdext_ctl_get_seq_info_op_t    impeg2d_ctl_get_seq_info_op_t
39 #define ALIGN64(x)                      ((((x) + 63) >> 6) << 6)
40 #define MAX_NUM_CORES                   4
41 #define IVDEXT_CMD_CTL_SET_NUM_CORES    \
42         (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_SET_NUM_CORES
43 #define MIN(a, b)                       (((a) < (b)) ? (a) : (b))
44 #define GETTIME(a, b)                   gettimeofday(a, b);
45 #define TIME_DIFF(start, end, diff)     \
46     diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \
47             ((end).tv_usec - (start).tv_usec);
48 
49 #ifdef FILE_DUMP_ENABLE
50     #define INPUT_DUMP_PATH     "/sdcard/clips/mpeg2d_input"
51     #define INPUT_DUMP_EXT      "m2v"
52     #define GENERATE_FILE_NAMES() {                         \
53         GETTIME(&mTimeStart, NULL);                         \
54         strcpy(mInFile, "");                                \
55         sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH,  \
56                 mTimeStart.tv_sec, mTimeStart.tv_usec,      \
57                 INPUT_DUMP_EXT);                            \
58     }
59     #define CREATE_DUMP_FILE(m_filename) {                  \
60         FILE *fp = fopen(m_filename, "wb");                 \
61         if (fp != NULL) {                                   \
62             fclose(fp);                                     \
63         } else {                                            \
64             ALOGD("Could not open file %s", m_filename);    \
65         }                                                   \
66     }
67     #define DUMP_TO_FILE(m_filename, m_buf, m_size)         \
68     {                                                       \
69         FILE *fp = fopen(m_filename, "ab");                 \
70         if (fp != NULL && m_buf != NULL) {                  \
71             uint32_t i;                                     \
72             i = fwrite(m_buf, 1, m_size, fp);               \
73             ALOGD("fwrite ret %d to write %d", i, m_size);  \
74             if (i != (uint32_t)m_size) {                    \
75                 ALOGD("Error in fwrite, returned %d", i);   \
76                 perror("Error in write to file");           \
77             }                                               \
78             fclose(fp);                                     \
79         } else {                                            \
80             ALOGD("Could not write to file %s", m_filename);\
81         }                                                   \
82     }
83 #else /* FILE_DUMP_ENABLE */
84     #define INPUT_DUMP_PATH
85     #define INPUT_DUMP_EXT
86     #define OUTPUT_DUMP_PATH
87     #define OUTPUT_DUMP_EXT
88     #define GENERATE_FILE_NAMES()
89     #define CREATE_DUMP_FILE(m_filename)
90     #define DUMP_TO_FILE(m_filename, m_buf, m_size)
91 #endif /* FILE_DUMP_ENABLE */
92 
93 struct C2SoftMpeg2Dec : public SimpleC2Component {
94     class IntfImpl;
95 
96     C2SoftMpeg2Dec(const char* name, c2_node_id_t id,
97                    const std::shared_ptr<IntfImpl>& intfImpl);
98     virtual ~C2SoftMpeg2Dec();
99 
100     // From SimpleC2Component
101     c2_status_t onInit() override;
102     c2_status_t onStop() override;
103     void onReset() override;
104     void onRelease() override;
105     c2_status_t onFlush_sm() override;
106     void process(
107             const std::unique_ptr<C2Work> &work,
108             const std::shared_ptr<C2BlockPool> &pool) override;
109     c2_status_t drain(
110             uint32_t drainMode,
111             const std::shared_ptr<C2BlockPool> &pool) override;
112  private:
113     status_t getNumMemRecords();
114     status_t fillMemRecords();
115     status_t createDecoder();
116     status_t setNumCores();
117     status_t setParams(size_t stride);
118     status_t getVersion();
119     status_t initDecoder();
120     bool setDecodeArgs(ivd_video_decode_ip_t *ps_decode_ip,
121                        ivd_video_decode_op_t *ps_decode_op,
122                        C2ReadView *inBuffer,
123                        C2GraphicView *outBuffer,
124                        size_t inOffset,
125                        size_t inSize,
126                        uint32_t tsMarker);
127     bool getSeqInfo();
128     // TODO:This is not the right place for colorAspects functions. These should
129     // be part of c2-vndk so that they can be accessed by all video plugins
130     // until then, make them feel at home
131     bool colorAspectsDiffer(const ColorAspects &a, const ColorAspects &b);
132     void updateFinalColorAspects(
133             const ColorAspects &otherAspects, const ColorAspects &preferredAspects);
134     status_t handleColorAspectsChange();
135     c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool);
136     void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work);
137     status_t setFlushMode();
138     c2_status_t drainInternal(
139             uint32_t drainMode,
140             const std::shared_ptr<C2BlockPool> &pool,
141             const std::unique_ptr<C2Work> &work);
142     status_t resetDecoder();
143     void resetPlugin();
144     status_t deleteDecoder();
145     status_t reInitDecoder();
146 
147     // TODO:This is not the right place for this enum. These should
148     // be part of c2-vndk so that they can be accessed by all video plugins
149     // until then, make them feel at home
150     enum {
151         kNotSupported,
152         kPreferBitstream,
153         kPreferContainer,
154     };
155 
156     std::shared_ptr<IntfImpl> mIntf;
157     iv_obj_t *mDecHandle;
158     iv_mem_rec_t *mMemRecords;
159     size_t mNumMemRecords;
160     std::shared_ptr<C2GraphicBlock> mOutBlock;
161     uint8_t *mOutBufferDrain;
162 
163     size_t mNumCores;
164     IV_COLOR_FORMAT_T mIvColorformat;
165 
166     uint32_t mWidth;
167     uint32_t mHeight;
168     uint32_t mStride;
169     bool mSignalledOutputEos;
170     bool mSignalledError;
171 
172     // ColorAspects
173     Mutex mColorAspectsLock;
174     int mPreference;
175     ColorAspects mDefaultColorAspects;
176     ColorAspects mBitstreamColorAspects;
177     ColorAspects mFinalColorAspects;
178     bool mUpdateColorAspects;
179 
180     // profile
181     struct timeval mTimeStart;
182     struct timeval mTimeEnd;
183 #ifdef FILE_DUMP_ENABLE
184     char mInFile[200];
185 #endif /* FILE_DUMP_ENABLE */
186 
187     C2_DO_NOT_COPY(C2SoftMpeg2Dec);
188 };
189 
190 }  // namespace android
191 
192 #endif  // ANDROID_C2_SOFT_MPEG2_DEC_H_
193