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     c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool);
129     void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work);
130     status_t setFlushMode();
131     c2_status_t drainInternal(
132             uint32_t drainMode,
133             const std::shared_ptr<C2BlockPool> &pool,
134             const std::unique_ptr<C2Work> &work);
135     status_t resetDecoder();
136     void resetPlugin();
137     status_t deleteDecoder();
138     status_t reInitDecoder();
139 
140     // TODO:This is not the right place for this enum. These should
141     // be part of c2-vndk so that they can be accessed by all video plugins
142     // until then, make them feel at home
143     enum {
144         kNotSupported,
145         kPreferBitstream,
146         kPreferContainer,
147     };
148 
149     std::shared_ptr<IntfImpl> mIntf;
150     iv_obj_t *mDecHandle;
151     iv_mem_rec_t *mMemRecords;
152     size_t mNumMemRecords;
153     std::shared_ptr<C2GraphicBlock> mOutBlock;
154     uint8_t *mOutBufferDrain;
155 
156     size_t mNumCores;
157     IV_COLOR_FORMAT_T mIvColorformat;
158 
159     uint32_t mWidth;
160     uint32_t mHeight;
161     uint32_t mStride;
162     bool mSignalledOutputEos;
163     bool mSignalledError;
164 
165     // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid
166     // converting them to C2 values for each frame
167     struct VuiColorAspects {
168         uint8_t primaries;
169         uint8_t transfer;
170         uint8_t coeffs;
171         uint8_t fullRange;
172 
173         // default color aspects
VuiColorAspectsC2SoftMpeg2Dec::VuiColorAspects174         VuiColorAspects()
175             : primaries(2), transfer(2), coeffs(2), fullRange(0) { }
176 
177         bool operator==(const VuiColorAspects &o) {
178             return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs
179                     && fullRange == o.fullRange;
180         }
181     } mBitstreamColorAspects;
182 
183     // profile
184     struct timeval mTimeStart;
185     struct timeval mTimeEnd;
186 #ifdef FILE_DUMP_ENABLE
187     char mInFile[200];
188 #endif /* FILE_DUMP_ENABLE */
189 
190     C2_DO_NOT_COPY(C2SoftMpeg2Dec);
191 };
192 
193 }  // namespace android
194 
195 #endif  // ANDROID_C2_SOFT_MPEG2_DEC_H_
196