• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <util/C2InterfaceHelper.h>
24 
25 #include <media/stagefright/foundation/ColorUtils.h>
26 
27 #include "iv_datatypedef.h"
28 #include "iv.h"
29 #include "ivd.h"
30 
31 namespace android {
32 
33 #define ivdec_api_function              impeg2d_api_function
34 #define ivdext_init_ip_t                impeg2d_init_ip_t
35 #define ivdext_init_op_t                impeg2d_init_op_t
36 #define ivdext_fill_mem_rec_ip_t        impeg2d_fill_mem_rec_ip_t
37 #define ivdext_fill_mem_rec_op_t        impeg2d_fill_mem_rec_op_t
38 #define ivdext_ctl_set_num_cores_ip_t   impeg2d_ctl_set_num_cores_ip_t
39 #define ivdext_ctl_set_num_cores_op_t   impeg2d_ctl_set_num_cores_op_t
40 #define ivdext_ctl_get_seq_info_ip_t    impeg2d_ctl_get_seq_info_ip_t
41 #define ivdext_ctl_get_seq_info_op_t    impeg2d_ctl_get_seq_info_op_t
42 #define ALIGN128(x)                     ((((x) + 127) >> 7) << 7)
43 #define MAX_NUM_CORES                   4
44 #define IVDEXT_CMD_CTL_SET_NUM_CORES    \
45         (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_SET_NUM_CORES
46 #define MIN(a, b)                       (((a) < (b)) ? (a) : (b))
47 
48 #ifdef FILE_DUMP_ENABLE
49     #define INPUT_DUMP_PATH     "/sdcard/clips/mpeg2d_input"
50     #define INPUT_DUMP_EXT      "m2v"
51     #define GENERATE_FILE_NAMES() {                         \
52         nsecs_t now = systemTime();                         \
53         sprintf(mInFile, "%s_%" PRId64 ".%s",               \
54                 INPUT_DUMP_PATH, now,                       \
55                 INPUT_DUMP_EXT);                            \
56     }
57     #define CREATE_DUMP_FILE(m_filename) {                  \
58         FILE *fp = fopen(m_filename, "wb");                 \
59         if (fp != NULL) {                                   \
60             fclose(fp);                                     \
61         } else {                                            \
62             ALOGD("Could not open file %s", m_filename);    \
63         }                                                   \
64     }
65     #define DUMP_TO_FILE(m_filename, m_buf, m_size)         \
66     {                                                       \
67         FILE *fp = fopen(m_filename, "ab");                 \
68         if (fp != NULL && m_buf != NULL) {                  \
69             uint32_t i;                                     \
70             i = fwrite(m_buf, 1, m_size, fp);               \
71             ALOGD("fwrite ret %d to write %d", i, m_size);  \
72             if (i != (uint32_t)m_size) {                    \
73                 ALOGD("Error in fwrite, returned %d", i);   \
74                 perror("Error in write to file");           \
75             }                                               \
76             fclose(fp);                                     \
77         } else {                                            \
78             ALOGD("Could not write to file %s", m_filename);\
79         }                                                   \
80     }
81 #else /* FILE_DUMP_ENABLE */
82     #define INPUT_DUMP_PATH
83     #define INPUT_DUMP_EXT
84     #define OUTPUT_DUMP_PATH
85     #define OUTPUT_DUMP_EXT
86     #define GENERATE_FILE_NAMES()
87     #define CREATE_DUMP_FILE(m_filename)
88     #define DUMP_TO_FILE(m_filename, m_buf, m_size)
89 #endif /* FILE_DUMP_ENABLE */
90 
91 struct C2SoftMpeg2Dec : public SimpleC2Component {
92     class IntfImpl;
93 
94     C2SoftMpeg2Dec(const char* name, c2_node_id_t id,
95                    const std::shared_ptr<IntfImpl>& intfImpl);
96     C2SoftMpeg2Dec(const char* name, c2_node_id_t id,
97                    const std::shared_ptr<C2ReflectorHelper>& helper);
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 = nullptr;
151     iv_mem_rec_t *mMemRecords = nullptr;
152     size_t mNumMemRecords = 0;
153     std::shared_ptr<C2GraphicBlock> mOutBlock;
154     uint8_t *mOutBufferDrain = nullptr;
155 
156     size_t mNumCores = 1;
157     IV_COLOR_FORMAT_T mIvColorformat = IV_YUV_420P;
158 
159     uint32_t mWidth = 320;
160     uint32_t mHeight = 240;
161     uint32_t mStride = 0;
162     bool mSignalledOutputEos = false;
163     bool mSignalledError = false;
164     bool mKeepThreadsActive = false;
165     std::atomic_uint64_t mOutIndex = 0;
166 
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
VuiColorAspectsC2SoftMpeg2Dec::VuiColorAspects176         VuiColorAspects()
177             : primaries(2), transfer(2), coeffs(2), fullRange(0) { }
178 
179         bool operator==(const VuiColorAspects &o) const {
180             return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs
181                     && fullRange == o.fullRange;
182         }
183     } mBitstreamColorAspects;
184 
185     // profile
186     nsecs_t mTimeStart = 0;
187     nsecs_t mTimeEnd = 0;
188 #ifdef FILE_DUMP_ENABLE
189     char mInFile[200];
190 #endif /* FILE_DUMP_ENABLE */
191 
192     C2_DO_NOT_COPY(C2SoftMpeg2Dec);
193 };
194 
195 }  // namespace android
196 
197 #endif  // ANDROID_C2_SOFT_MPEG2_DEC_H_
198