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 #ifndef ANDROID_V4L2_CODEC2_COMPONENTS_V4L2_DECODE_INTERFACE_H
6 #define ANDROID_V4L2_CODEC2_COMPONENTS_V4L2_DECODE_INTERFACE_H
7 
8 #include <memory>
9 #include <string>
10 
11 #include <C2Config.h>
12 #include <ui/Size.h>
13 #include <util/C2InterfaceHelper.h>
14 
15 #include <v4l2_codec2/common/VideoTypes.h>
16 
17 namespace android {
18 
19 class V4L2DecodeInterface : public C2InterfaceHelper {
20 public:
21     V4L2DecodeInterface(const std::string& name, const std::shared_ptr<C2ReflectorHelper>& helper);
22     V4L2DecodeInterface(const V4L2DecodeInterface&) = delete;
23     V4L2DecodeInterface& operator=(const V4L2DecodeInterface&) = delete;
24     ~V4L2DecodeInterface() = default;
25 
26     // interfaces for the client component.
status()27     c2_status_t status() const { return mInitStatus; }
getBlockPoolId()28     C2BlockPool::local_id_t getBlockPoolId() const { return mOutputBlockPoolIds->m.values[0]; }
getVideoCodec()29     std::optional<VideoCodec> getVideoCodec() const { return mVideoCodec; }
30 
31     static uint32_t getOutputDelay(VideoCodec codec);
32 
33     size_t getInputBufferSize() const;
34     c2_status_t queryColorAspects(
35             std::shared_ptr<C2StreamColorAspectsInfo::output>* targetColorAspects);
36 
37 private:
38     // Configurable parameter setters.
39     static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input>& info);
40     static C2R SizeSetter(bool mayBlock, C2P<C2StreamPictureSizeInfo::output>& videoSize);
41     static C2R MaxInputBufferSizeCalculator(bool mayBlock,
42                                             C2P<C2StreamMaxBufferSizeInfo::input>& me,
43                                             const C2P<C2StreamPictureSizeInfo::output>& size);
44 
45     template <typename T>
46     static C2R DefaultColorAspectsSetter(bool mayBlock, C2P<T>& def);
47 
48     static C2R MergedColorAspectsSetter(bool mayBlock,
49                                         C2P<C2StreamColorAspectsInfo::output>& merged,
50                                         const C2P<C2StreamColorAspectsTuning::output>& def,
51                                         const C2P<C2StreamColorAspectsInfo::input>& coded);
52 
53     // The kind of the component; should be C2Component::KIND_DECODER.
54     std::shared_ptr<C2ComponentKindSetting> mKind;
55     // The input format kind; should be C2FormatCompressed.
56     std::shared_ptr<C2StreamBufferTypeSetting::input> mInputFormat;
57     // The memory usage flag of input buffer; should be BufferUsage::VIDEO_DECODER.
58     std::shared_ptr<C2StreamUsageTuning::input> mInputMemoryUsage;
59     // The output format kind; should be C2FormatVideo.
60     std::shared_ptr<C2StreamBufferTypeSetting::output> mOutputFormat;
61     // The MIME type of input port.
62     std::shared_ptr<C2PortMediaTypeSetting::input> mInputMediaType;
63     // The MIME type of output port; should be MEDIA_MIMETYPE_VIDEO_RAW.
64     std::shared_ptr<C2PortMediaTypeSetting::output> mOutputMediaType;
65     // The number of additional output frames that might need to be generated before an output
66     // buffer can be released by the component; only used for H264 because H264 may reorder the
67     // output frames.
68     std::shared_ptr<C2PortDelayTuning::output> mOutputDelay;
69     // The input codec profile and level. For now configuring this parameter is useless since
70     // the component always uses fixed codec profile to initialize accelerator. It is only used
71     // for the client to query supported profile and level values.
72     // TODO: use configured profile/level to initialize accelerator.
73     std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
74     // Decoded video size for output.
75     std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
76     // Maximum size of one input buffer.
77     std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
78     // The suggested usage of input buffer allocator ID.
79     std::shared_ptr<C2PortAllocatorsTuning::input> mInputAllocatorIds;
80     // The suggested usage of output buffer allocator ID.
81     std::shared_ptr<C2PortAllocatorsTuning::output> mOutputAllocatorIds;
82     // The suggested usage of output buffer allocator ID with surface.
83     std::shared_ptr<C2PortSurfaceAllocatorTuning::output> mOutputSurfaceAllocatorId;
84     // Component uses this ID to fetch corresponding output block pool from platform.
85     std::shared_ptr<C2PortBlockPoolsTuning::output> mOutputBlockPoolIds;
86     // The color aspects parsed from input bitstream. This parameter should be configured by
87     // component while decoding.
88     std::shared_ptr<C2StreamColorAspectsInfo::input> mCodedColorAspects;
89     // The default color aspects specified by requested output format. This parameter should be
90     // configured by client.
91     std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
92     // The combined color aspects by |mCodedColorAspects| and |mDefaultColorAspects|, and the
93     // former has higher priority. This parameter is used for component to provide color aspects
94     // as C2Info in decoded output buffers.
95     std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects;
96 
97     c2_status_t mInitStatus;
98     std::optional<VideoCodec> mVideoCodec;
99 };
100 
101 }  // namespace android
102 
103 #endif  // ANDROID_V4L2_CODEC2_COMPONENTS_V4L2_DECODE_INTERFACE_H
104