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