blob: ed27493f32dd82a50c927428508c39892adb3ed7 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 2017 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_AVC_DEC_H_
18#define ANDROID_C2_SOFT_AVC_DEC_H_
19
20#include <sys/time.h>
21
22#include <media/stagefright/foundation/ColorUtils.h>
23
Rakesh Kumar4ec85ae2019-02-13 00:50:07 +053024#include <atomic>
Pawin Vongmasa36653902018-11-15 00:10:25 -080025#include <SimpleC2Component.h>
26
27#include "ih264_typedefs.h"
28#include "iv.h"
29#include "ivd.h"
30
31namespace android {
32
33#define ivdec_api_function ih264d_api_function
34#define ivdext_create_ip_t ih264d_create_ip_t
35#define ivdext_create_op_t ih264d_create_op_t
36#define ivdext_delete_ip_t ih264d_delete_ip_t
37#define ivdext_delete_op_t ih264d_delete_op_t
38#define ivdext_ctl_set_num_cores_ip_t ih264d_ctl_set_num_cores_ip_t
39#define ivdext_ctl_set_num_cores_op_t ih264d_ctl_set_num_cores_op_t
40#define ivdext_ctl_get_vui_params_ip_t ih264d_ctl_get_vui_params_ip_t
41#define ivdext_ctl_get_vui_params_op_t ih264d_ctl_get_vui_params_op_t
42#define ALIGN64(x) ((((x) + 63) >> 6) << 6)
Marcus Huang28af1152019-10-18 15:51:35 +080043#define ALIGN128(x) ((((x) + 127) >> 7) << 7)
Pawin Vongmasa36653902018-11-15 00:10:25 -080044#define MAX_NUM_CORES 4
45#define IVDEXT_CMD_CTL_SET_NUM_CORES \
46 (IVD_CONTROL_API_COMMAND_TYPE_T)IH264D_CMD_CTL_SET_NUM_CORES
47#define MIN(a, b) (((a) < (b)) ? (a) : (b))
48#define GETTIME(a, b) gettimeofday(a, b);
49#define TIME_DIFF(start, end, diff) \
50 diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \
51 ((end).tv_usec - (start).tv_usec);
52
53#ifdef FILE_DUMP_ENABLE
54 #define INPUT_DUMP_PATH "/sdcard/clips/avcd_input"
55 #define INPUT_DUMP_EXT "h264"
56 #define GENERATE_FILE_NAMES() { \
57 GETTIME(&mTimeStart, NULL); \
58 strcpy(mInFile, ""); \
59 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, \
60 mTimeStart.tv_sec, mTimeStart.tv_usec, \
61 INPUT_DUMP_EXT); \
62 }
63 #define CREATE_DUMP_FILE(m_filename) { \
64 FILE *fp = fopen(m_filename, "wb"); \
65 if (fp != NULL) { \
66 fclose(fp); \
67 } else { \
68 ALOGD("Could not open file %s", m_filename); \
69 } \
70 }
71 #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset)\
72 { \
73 FILE *fp = fopen(m_filename, "ab"); \
74 if (fp != NULL && m_buf != NULL && m_offset == 0) { \
75 int i; \
76 i = fwrite(m_buf, 1, m_size, fp); \
77 ALOGD("fwrite ret %d to write %d", i, m_size); \
78 if (i != (int) m_size) { \
79 ALOGD("Error in fwrite, returned %d", i); \
80 perror("Error in write to file"); \
81 } \
82 } else if (fp == NULL) { \
83 ALOGD("Could not write to file %s", m_filename);\
84 } \
85 if (fp) { \
86 fclose(fp); \
87 } \
88 }
89#else /* FILE_DUMP_ENABLE */
90 #define INPUT_DUMP_PATH
91 #define INPUT_DUMP_EXT
92 #define OUTPUT_DUMP_PATH
93 #define OUTPUT_DUMP_EXT
94 #define GENERATE_FILE_NAMES()
95 #define CREATE_DUMP_FILE(m_filename)
96 #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset)
97#endif /* FILE_DUMP_ENABLE */
98
99
100class C2SoftAvcDec : public SimpleC2Component {
101public:
102 class IntfImpl;
103 C2SoftAvcDec(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
104 virtual ~C2SoftAvcDec();
105
106 // From SimpleC2Component
107 c2_status_t onInit() override;
108 c2_status_t onStop() override;
109 void onReset() override;
110 void onRelease() override;
111 c2_status_t onFlush_sm() override;
112 void process(
113 const std::unique_ptr<C2Work> &work,
114 const std::shared_ptr<C2BlockPool> &pool) override;
115 c2_status_t drain(
116 uint32_t drainMode,
117 const std::shared_ptr<C2BlockPool> &pool) override;
118
119private:
120 status_t createDecoder();
121 status_t setNumCores();
122 status_t setParams(size_t stride, IVD_VIDEO_DECODE_MODE_T dec_mode);
123 void getVersion();
124 status_t initDecoder();
125 bool setDecodeArgs(ivd_video_decode_ip_t *ps_decode_ip,
126 ivd_video_decode_op_t *ps_decode_op,
127 C2ReadView *inBuffer,
128 C2GraphicView *outBuffer,
129 size_t inOffset,
130 size_t inSize,
131 uint32_t tsMarker);
132 bool getVuiParams();
133 c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool);
134 void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work);
135 status_t setFlushMode();
136 c2_status_t drainInternal(
137 uint32_t drainMode,
138 const std::shared_ptr<C2BlockPool> &pool,
139 const std::unique_ptr<C2Work> &work);
140 status_t resetDecoder();
141 void resetPlugin();
142 status_t deleteDecoder();
143
144 std::shared_ptr<IntfImpl> mIntf;
145
146 // TODO:This is not the right place for this enum. These should
147 // be part of c2-vndk so that they can be accessed by all video plugins
148 // until then, make them feel at home
149 enum {
150 kNotSupported,
151 kPreferBitstream,
152 kPreferContainer,
153 };
154
155 iv_obj_t *mDecHandle;
156 std::shared_ptr<C2GraphicBlock> mOutBlock;
157 uint8_t *mOutBufferFlush;
158
159 size_t mNumCores;
160 IV_COLOR_FORMAT_T mIvColorFormat;
Harish Mahendrakar343be8a2019-08-01 12:38:58 -0700161 uint32_t mOutputDelay;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800162 uint32_t mWidth;
163 uint32_t mHeight;
164 uint32_t mStride;
165 bool mSignalledOutputEos;
166 bool mSignalledError;
167 bool mHeaderDecoded;
Rakesh Kumar4ec85ae2019-02-13 00:50:07 +0530168 std::atomic_uint64_t mOutIndex;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800169 // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid
170 // converting them to C2 values for each frame
171 struct VuiColorAspects {
172 uint8_t primaries;
173 uint8_t transfer;
174 uint8_t coeffs;
175 uint8_t fullRange;
176
177 // default color aspects
178 VuiColorAspects()
179 : primaries(2), transfer(2), coeffs(2), fullRange(0) { }
180
181 bool operator==(const VuiColorAspects &o) {
182 return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs
183 && fullRange == o.fullRange;
184 }
185 } mBitstreamColorAspects;
186
187 // profile
188 struct timeval mTimeStart;
189 struct timeval mTimeEnd;
190#ifdef FILE_DUMP_ENABLE
191 char mInFile[200];
192#endif /* FILE_DUMP_ENABLE */
193
194 C2_DO_NOT_COPY(C2SoftAvcDec);
195};
196
197} // namespace android
198
199#endif // ANDROID_C2_SOFT_AVC_DEC_H_