1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 //#define LOG_NDEBUG 0
6 #define LOG_TAG "V4L2DecodeInterface"
7
8 #include <v4l2_codec2/components/V4L2DecodeInterface.h>
9
10 #include <C2PlatformSupport.h>
11 #include <SimpleC2Interface.h>
12 #include <android/hardware/graphics/common/1.0/types.h>
13 #include <log/log.h>
14 #include <media/stagefright/foundation/MediaDefs.h>
15
16 #include <v4l2_codec2/common/V4L2ComponentCommon.h>
17 #include <v4l2_codec2/plugin_store/V4L2AllocatorId.h>
18 #include <v4l2_device.h>
19
20 namespace android {
21 namespace {
22
23 constexpr size_t k1080pArea = 1920 * 1088;
24 constexpr size_t k4KArea = 3840 * 2160;
25 // Input bitstream buffer size for up to 1080p streams.
26 constexpr size_t kInputBufferSizeFor1080p = 1024 * 1024; // 1MB
27 // Input bitstream buffer size for up to 4k streams.
28 constexpr size_t kInputBufferSizeFor4K = 4 * kInputBufferSizeFor1080p;
29
getCodecFromComponentName(const std::string & name)30 std::optional<VideoCodec> getCodecFromComponentName(const std::string& name) {
31 if (name == V4L2ComponentName::kH264Decoder || name == V4L2ComponentName::kH264SecureDecoder)
32 return VideoCodec::H264;
33 if (name == V4L2ComponentName::kVP8Decoder || name == V4L2ComponentName::kVP8SecureDecoder)
34 return VideoCodec::VP8;
35 if (name == V4L2ComponentName::kVP9Decoder || name == V4L2ComponentName::kVP9SecureDecoder)
36 return VideoCodec::VP9;
37
38 ALOGE("Unknown name: %s", name.c_str());
39 return std::nullopt;
40 }
41
calculateInputBufferSize(size_t area)42 size_t calculateInputBufferSize(size_t area) {
43 if (area > k4KArea) {
44 ALOGW("Input buffer size for video size (%zu) larger than 4K (%zu) might be too small.",
45 area, k4KArea);
46 }
47
48 // Enlarge the input buffer for 4k video
49 if (area > k1080pArea) return kInputBufferSizeFor4K;
50 return kInputBufferSizeFor1080p;
51 }
52
getOutputDelay(VideoCodec codec)53 uint32_t getOutputDelay(VideoCodec codec) {
54 switch (codec) {
55 case VideoCodec::H264:
56 // Due to frame reordering an H264 decoder might need multiple additional input frames to be
57 // queued before being able to output the associated decoded buffers. We need to tell the
58 // codec2 framework that it should not stop queuing new work items until the maximum number
59 // of frame reordering is reached, to avoid stalling the decoder.
60 return 16;
61 case VideoCodec::VP8:
62 return 0;
63 case VideoCodec::VP9:
64 return 0;
65 }
66 }
67
68 } // namespace
69
70 // static
ProfileLevelSetter(bool,C2P<C2StreamProfileLevelInfo::input> & info)71 C2R V4L2DecodeInterface::ProfileLevelSetter(bool /* mayBlock */,
72 C2P<C2StreamProfileLevelInfo::input>& info) {
73 return info.F(info.v.profile)
74 .validatePossible(info.v.profile)
75 .plus(info.F(info.v.level).validatePossible(info.v.level));
76 }
77
78 // static
SizeSetter(bool,C2P<C2StreamPictureSizeInfo::output> & videoSize)79 C2R V4L2DecodeInterface::SizeSetter(bool /* mayBlock */,
80 C2P<C2StreamPictureSizeInfo::output>& videoSize) {
81 return videoSize.F(videoSize.v.width)
82 .validatePossible(videoSize.v.width)
83 .plus(videoSize.F(videoSize.v.height).validatePossible(videoSize.v.height));
84 }
85
86 // static
87 template <typename T>
DefaultColorAspectsSetter(bool,C2P<T> & def)88 C2R V4L2DecodeInterface::DefaultColorAspectsSetter(bool /* mayBlock */, C2P<T>& def) {
89 if (def.v.range > C2Color::RANGE_OTHER) {
90 def.set().range = C2Color::RANGE_OTHER;
91 }
92 if (def.v.primaries > C2Color::PRIMARIES_OTHER) {
93 def.set().primaries = C2Color::PRIMARIES_OTHER;
94 }
95 if (def.v.transfer > C2Color::TRANSFER_OTHER) {
96 def.set().transfer = C2Color::TRANSFER_OTHER;
97 }
98 if (def.v.matrix > C2Color::MATRIX_OTHER) {
99 def.set().matrix = C2Color::MATRIX_OTHER;
100 }
101 return C2R::Ok();
102 }
103
104 // static
MergedColorAspectsSetter(bool,C2P<C2StreamColorAspectsInfo::output> & merged,const C2P<C2StreamColorAspectsTuning::output> & def,const C2P<C2StreamColorAspectsInfo::input> & coded)105 C2R V4L2DecodeInterface::MergedColorAspectsSetter(
106 bool /* mayBlock */, C2P<C2StreamColorAspectsInfo::output>& merged,
107 const C2P<C2StreamColorAspectsTuning::output>& def,
108 const C2P<C2StreamColorAspectsInfo::input>& coded) {
109 // Take coded values for all specified fields, and default values for unspecified ones.
110 merged.set().range = coded.v.range == RANGE_UNSPECIFIED ? def.v.range : coded.v.range;
111 merged.set().primaries =
112 coded.v.primaries == PRIMARIES_UNSPECIFIED ? def.v.primaries : coded.v.primaries;
113 merged.set().transfer =
114 coded.v.transfer == TRANSFER_UNSPECIFIED ? def.v.transfer : coded.v.transfer;
115 merged.set().matrix = coded.v.matrix == MATRIX_UNSPECIFIED ? def.v.matrix : coded.v.matrix;
116 return C2R::Ok();
117 }
118
119 // static
MaxInputBufferSizeCalculator(bool,C2P<C2StreamMaxBufferSizeInfo::input> & me,const C2P<C2StreamPictureSizeInfo::output> & size)120 C2R V4L2DecodeInterface::MaxInputBufferSizeCalculator(
121 bool /* mayBlock */, C2P<C2StreamMaxBufferSizeInfo::input>& me,
122 const C2P<C2StreamPictureSizeInfo::output>& size) {
123 me.set().value = calculateInputBufferSize(size.v.width * size.v.height);
124 return C2R::Ok();
125 }
126
V4L2DecodeInterface(const std::string & name,const std::shared_ptr<C2ReflectorHelper> & helper)127 V4L2DecodeInterface::V4L2DecodeInterface(const std::string& name,
128 const std::shared_ptr<C2ReflectorHelper>& helper)
129 : C2InterfaceHelper(helper), mInitStatus(C2_OK) {
130 ALOGV("%s(%s)", __func__, name.c_str());
131
132 setDerivedInstance(this);
133
134 mVideoCodec = getCodecFromComponentName(name);
135 if (!mVideoCodec) {
136 ALOGE("Invalid component name: %s", name.c_str());
137 mInitStatus = C2_BAD_VALUE;
138 return;
139 }
140
141 std::string inputMime;
142 switch (*mVideoCodec) {
143 case VideoCodec::H264:
144 inputMime = MEDIA_MIMETYPE_VIDEO_AVC;
145 addParameter(
146 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
147 .withDefault(new C2StreamProfileLevelInfo::input(
148 0u, C2Config::PROFILE_AVC_MAIN, C2Config::LEVEL_AVC_4))
149 .withFields(
150 {C2F(mProfileLevel, profile)
151 .oneOf({C2Config::PROFILE_AVC_BASELINE,
152 C2Config::PROFILE_AVC_CONSTRAINED_BASELINE,
153 C2Config::PROFILE_AVC_MAIN,
154 C2Config::PROFILE_AVC_HIGH,
155 C2Config::PROFILE_AVC_CONSTRAINED_HIGH}),
156 C2F(mProfileLevel, level)
157 .oneOf({C2Config::LEVEL_AVC_1, C2Config::LEVEL_AVC_1B,
158 C2Config::LEVEL_AVC_1_1, C2Config::LEVEL_AVC_1_2,
159 C2Config::LEVEL_AVC_1_3, C2Config::LEVEL_AVC_2,
160 C2Config::LEVEL_AVC_2_1, C2Config::LEVEL_AVC_2_2,
161 C2Config::LEVEL_AVC_3, C2Config::LEVEL_AVC_3_1,
162 C2Config::LEVEL_AVC_3_2, C2Config::LEVEL_AVC_4,
163 C2Config::LEVEL_AVC_4_1, C2Config::LEVEL_AVC_4_2,
164 C2Config::LEVEL_AVC_5, C2Config::LEVEL_AVC_5_1,
165 C2Config::LEVEL_AVC_5_2})})
166 .withSetter(ProfileLevelSetter)
167 .build());
168 break;
169
170 case VideoCodec::VP8:
171 inputMime = MEDIA_MIMETYPE_VIDEO_VP8;
172 addParameter(DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
173 .withConstValue(new C2StreamProfileLevelInfo::input(
174 0u, C2Config::PROFILE_UNUSED, C2Config::LEVEL_UNUSED))
175 .build());
176 break;
177
178 case VideoCodec::VP9:
179 inputMime = MEDIA_MIMETYPE_VIDEO_VP9;
180 addParameter(
181 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
182 .withDefault(new C2StreamProfileLevelInfo::input(
183 0u, C2Config::PROFILE_VP9_0, C2Config::LEVEL_VP9_5))
184 .withFields({C2F(mProfileLevel, profile).oneOf({C2Config::PROFILE_VP9_0}),
185 C2F(mProfileLevel, level)
186 .oneOf({C2Config::LEVEL_VP9_1, C2Config::LEVEL_VP9_1_1,
187 C2Config::LEVEL_VP9_2, C2Config::LEVEL_VP9_2_1,
188 C2Config::LEVEL_VP9_3, C2Config::LEVEL_VP9_3_1,
189 C2Config::LEVEL_VP9_4, C2Config::LEVEL_VP9_4_1,
190 C2Config::LEVEL_VP9_5})})
191 .withSetter(ProfileLevelSetter)
192 .build());
193 break;
194 }
195
196 addParameter(
197 DefineParam(mInputFormat, C2_PARAMKEY_INPUT_STREAM_BUFFER_TYPE)
198 .withConstValue(new C2StreamBufferTypeSetting::input(0u, C2BufferData::LINEAR))
199 .build());
200 addParameter(
201 DefineParam(mInputMemoryUsage, C2_PARAMKEY_INPUT_STREAM_USAGE)
202 .withConstValue(new C2StreamUsageTuning::input(
203 0u, static_cast<uint64_t>(android::hardware::graphics::common::V1_0::
204 BufferUsage::VIDEO_DECODER)))
205 .build());
206
207 addParameter(DefineParam(mOutputFormat, C2_PARAMKEY_OUTPUT_STREAM_BUFFER_TYPE)
208 .withConstValue(
209 new C2StreamBufferTypeSetting::output(0u, C2BufferData::GRAPHIC))
210 .build());
211 addParameter(
212 DefineParam(mOutputDelay, C2_PARAMKEY_OUTPUT_DELAY)
213 .withConstValue(new C2PortDelayTuning::output(getOutputDelay(*mVideoCodec)))
214 .build());
215
216 addParameter(DefineParam(mInputMediaType, C2_PARAMKEY_INPUT_MEDIA_TYPE)
217 .withConstValue(AllocSharedString<C2PortMediaTypeSetting::input>(
218 inputMime.c_str()))
219 .build());
220
221 addParameter(DefineParam(mOutputMediaType, C2_PARAMKEY_OUTPUT_MEDIA_TYPE)
222 .withConstValue(AllocSharedString<C2PortMediaTypeSetting::output>(
223 MEDIA_MIMETYPE_VIDEO_RAW))
224 .build());
225
226 // Note(b/165826281): The check is not used at Android framework currently.
227 // In order to fasten the bootup time, we use the maximum supported size instead of querying the
228 // capability from the V4L2 device.
229 addParameter(DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
230 .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240))
231 .withFields({
232 C2F(mSize, width).inRange(16, 4096, 16),
233 C2F(mSize, height).inRange(16, 4096, 16),
234 })
235 .withSetter(SizeSetter)
236 .build());
237
238 addParameter(
239 DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
240 .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, kInputBufferSizeFor1080p))
241 .withFields({
242 C2F(mMaxInputSize, value).any(),
243 })
244 .calculatedAs(MaxInputBufferSizeCalculator, mSize)
245 .build());
246
247 bool secureMode = name.find(".secure") != std::string::npos;
248 const C2Allocator::id_t inputAllocators[] = {secureMode ? V4L2AllocatorId::SECURE_LINEAR
249 : C2PlatformAllocatorStore::BLOB};
250
251 const C2Allocator::id_t outputAllocators[] = {V4L2AllocatorId::V4L2_BUFFERPOOL};
252 const C2Allocator::id_t surfaceAllocator =
253 secureMode ? V4L2AllocatorId::SECURE_GRAPHIC : V4L2AllocatorId::V4L2_BUFFERQUEUE;
254 const C2BlockPool::local_id_t outputBlockPools[] = {C2BlockPool::BASIC_GRAPHIC};
255
256 addParameter(
257 DefineParam(mInputAllocatorIds, C2_PARAMKEY_INPUT_ALLOCATORS)
258 .withConstValue(C2PortAllocatorsTuning::input::AllocShared(inputAllocators))
259 .build());
260
261 addParameter(
262 DefineParam(mOutputAllocatorIds, C2_PARAMKEY_OUTPUT_ALLOCATORS)
263 .withConstValue(C2PortAllocatorsTuning::output::AllocShared(outputAllocators))
264 .build());
265
266 addParameter(DefineParam(mOutputSurfaceAllocatorId, C2_PARAMKEY_OUTPUT_SURFACE_ALLOCATOR)
267 .withConstValue(new C2PortSurfaceAllocatorTuning::output(surfaceAllocator))
268 .build());
269
270 addParameter(
271 DefineParam(mOutputBlockPoolIds, C2_PARAMKEY_OUTPUT_BLOCK_POOLS)
272 .withDefault(C2PortBlockPoolsTuning::output::AllocShared(outputBlockPools))
273 .withFields({C2F(mOutputBlockPoolIds, m.values[0]).any(),
274 C2F(mOutputBlockPoolIds, m.values).inRange(0, 1)})
275 .withSetter(Setter<C2PortBlockPoolsTuning::output>::NonStrictValuesWithNoDeps)
276 .build());
277
278 addParameter(
279 DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
280 .withDefault(new C2StreamColorAspectsTuning::output(
281 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
282 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
283 .withFields(
284 {C2F(mDefaultColorAspects, range)
285 .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
286 C2F(mDefaultColorAspects, primaries)
287 .inRange(C2Color::PRIMARIES_UNSPECIFIED,
288 C2Color::PRIMARIES_OTHER),
289 C2F(mDefaultColorAspects, transfer)
290 .inRange(C2Color::TRANSFER_UNSPECIFIED,
291 C2Color::TRANSFER_OTHER),
292 C2F(mDefaultColorAspects, matrix)
293 .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)})
294 .withSetter(DefaultColorAspectsSetter)
295 .build());
296
297 addParameter(
298 DefineParam(mCodedColorAspects, C2_PARAMKEY_VUI_COLOR_ASPECTS)
299 .withDefault(new C2StreamColorAspectsInfo::input(
300 0u, C2Color::RANGE_LIMITED, C2Color::PRIMARIES_UNSPECIFIED,
301 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
302 .withFields(
303 {C2F(mCodedColorAspects, range)
304 .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
305 C2F(mCodedColorAspects, primaries)
306 .inRange(C2Color::PRIMARIES_UNSPECIFIED,
307 C2Color::PRIMARIES_OTHER),
308 C2F(mCodedColorAspects, transfer)
309 .inRange(C2Color::TRANSFER_UNSPECIFIED,
310 C2Color::TRANSFER_OTHER),
311 C2F(mCodedColorAspects, matrix)
312 .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)})
313 .withSetter(DefaultColorAspectsSetter)
314 .build());
315
316 addParameter(
317 DefineParam(mColorAspects, C2_PARAMKEY_COLOR_ASPECTS)
318 .withDefault(new C2StreamColorAspectsInfo::output(
319 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
320 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
321 .withFields(
322 {C2F(mColorAspects, range)
323 .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
324 C2F(mColorAspects, primaries)
325 .inRange(C2Color::PRIMARIES_UNSPECIFIED,
326 C2Color::PRIMARIES_OTHER),
327 C2F(mColorAspects, transfer)
328 .inRange(C2Color::TRANSFER_UNSPECIFIED,
329 C2Color::TRANSFER_OTHER),
330 C2F(mColorAspects, matrix)
331 .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)})
332 .withSetter(MergedColorAspectsSetter, mDefaultColorAspects, mCodedColorAspects)
333 .build());
334 }
335
getInputBufferSize() const336 size_t V4L2DecodeInterface::getInputBufferSize() const {
337 return calculateInputBufferSize(getMaxSize().GetArea());
338 }
339
queryColorAspects(std::shared_ptr<C2StreamColorAspectsInfo::output> * targetColorAspects)340 c2_status_t V4L2DecodeInterface::queryColorAspects(
341 std::shared_ptr<C2StreamColorAspectsInfo::output>* targetColorAspects) {
342 std::unique_ptr<C2StreamColorAspectsInfo::output> colorAspects =
343 std::make_unique<C2StreamColorAspectsInfo::output>(
344 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
345 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED);
346 c2_status_t status = query({colorAspects.get()}, {}, C2_DONT_BLOCK, nullptr);
347 if (status == C2_OK) {
348 *targetColorAspects = std::move(colorAspects);
349 }
350 return status;
351 }
352
353 } // namespace android
354