blob: 6d09694ad32d38417b4d3fde5ed72292e17726da [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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
Rakesh Kumard91bc482019-02-18 10:42:41 +053020#include <atomic>
Ray Essick24754942022-04-16 09:50:35 -070021#include <inttypes.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080022#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
30namespace 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
Harish Mahendrakare403bf72021-10-21 10:30:35 -070041#define ALIGN128(x) ((((x) + 127) >> 7) << 7)
Pawin Vongmasa36653902018-11-15 00:10:25 -080042#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))
Pawin Vongmasa36653902018-11-15 00:10:25 -080046
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() { \
Ray Essick24754942022-04-16 09:50:35 -070051 nsecs_t now = systemTime(); \
Greg Kaiser6ffbad12022-04-19 06:38:25 -070052 sprintf(mInFile, "%s_%" PRId64 ".%s", \
Ray Essick24754942022-04-16 09:50:35 -070053 INPUT_DUMP_PATH, now, \
Pawin Vongmasa36653902018-11-15 00:10:25 -080054 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
90struct 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;
Suyog Pawar67e08fb2024-11-14 11:31:42 +0530147 iv_obj_t *mDecHandle = nullptr;
148 iv_mem_rec_t *mMemRecords = nullptr;
149 size_t mNumMemRecords = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800150 std::shared_ptr<C2GraphicBlock> mOutBlock;
Suyog Pawar67e08fb2024-11-14 11:31:42 +0530151 uint8_t *mOutBufferDrain = nullptr;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800152
Suyog Pawar67e08fb2024-11-14 11:31:42 +0530153 size_t mNumCores = 1;
154 IV_COLOR_FORMAT_T mIvColorformat = IV_YUV_420P;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800155
Suyog Pawar67e08fb2024-11-14 11:31:42 +0530156 uint32_t mWidth = 320;
157 uint32_t mHeight = 240;
158 uint32_t mStride = 0;
159 bool mSignalledOutputEos = false;
160 bool mSignalledError = false;
161 bool mKeepThreadsActive = false;
162 std::atomic_uint64_t mOutIndex = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800163
164 // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid
165 // converting them to C2 values for each frame
166 struct VuiColorAspects {
167 uint8_t primaries;
168 uint8_t transfer;
169 uint8_t coeffs;
170 uint8_t fullRange;
171
172 // default color aspects
173 VuiColorAspects()
174 : primaries(2), transfer(2), coeffs(2), fullRange(0) { }
175
Satish Yalla892344a2024-06-11 01:36:45 +0000176 bool operator==(const VuiColorAspects &o) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800177 return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs
178 && fullRange == o.fullRange;
179 }
180 } mBitstreamColorAspects;
181
182 // profile
Ray Essick24754942022-04-16 09:50:35 -0700183 nsecs_t mTimeStart = 0;
184 nsecs_t mTimeEnd = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800185#ifdef FILE_DUMP_ENABLE
186 char mInFile[200];
187#endif /* FILE_DUMP_ENABLE */
188
189 C2_DO_NOT_COPY(C2SoftMpeg2Dec);
190};
191
192} // namespace android
193
194#endif // ANDROID_C2_SOFT_MPEG2_DEC_H_