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