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