xref: /aosp_15_r20/external/v4l2_codec2/common/include/v4l2_codec2/common/EncodeHelpers.h (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
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_COMMON_HELPERS_H
6 #define ANDROID_V4L2_CODEC2_COMMON_HELPERS_H
7 
8 #include <C2Config.h>
9 #include <system/graphics.h>
10 #include <ui/Size.h>
11 
12 #include <v4l2_codec2/common/VideoPixelFormat.h>
13 
14 namespace android {
15 
16 // The encoder parameter set.
17 //  |mInputFormat| is the pixel format of the input frames.
18 //  |mInputVisibleSize| is the resolution of the input frames.
19 //  |mOutputProfile| is the codec profile of the encoded output stream.
20 //  |mInitialBitrate| is the initial bitrate of the encoded output stream, in bits per second.
21 //  |mInitialFramerate| is the initial requested framerate.
22 //  |mH264OutputLevel| is H264 level of encoded output stream.
23 //  |mStorageType| is the storage type of video frame provided on encode().
24 struct VideoEncoderAcceleratorConfig {
25     enum VideoFrameStorageType {
26         SHMEM = 0,
27         DMABUF = 1,
28     };
29 
30     VideoPixelFormat mInputFormat;
31     ui::Size mInputVisibleSize;
32     C2Config::profile_t mOutputProfile;
33     uint32_t mInitialBitrate;
34     uint32_t mInitialFramerate;
35     uint8_t mH264OutputLevel;
36     VideoFrameStorageType mStorageType;
37 };
38 
39 // Convert the specified C2Config level to a V4L2 level.
40 uint8_t c2LevelToV4L2Level(C2Config::level_t level);
41 
42 // Get the specified graphics block in YCbCr format.
43 android_ycbcr getGraphicBlockInfo(const C2ConstGraphicBlock& block);
44 
45 // Try to extract SPS and PPS NAL units from the specified H.264 |data| stream. If found the data
46 // will be copied (after resizing) into the provided |sps| and |pps| buffers. Returns whether
47 // extraction was successful.
48 bool extractSPSPPS(const uint8_t* data, size_t length, std::vector<uint8_t>* sps,
49                    std::vector<uint8_t>* pps);
50 
51 // When encoding a video the codec-specific data (CSD; e.g. SPS and PPS for H264 encoding) will be
52 // concatenated to the first encoded slice. This function extracts the CSD out of the bitstream and
53 // stores it into |csd|. Returns whether extracting CSD info was successful.
54 bool extractCSDInfo(std::unique_ptr<C2StreamInitDataInfo::output>* const csd, const uint8_t* data,
55                     size_t length);
56 
57 // Prepend the specified |sps| and |pps| NAL units (without start codes) to the H.264 |data| stream.
58 // The result is copied into |dst|. The provided |sps| and |pps| data will be updated if an SPS or
59 // PPS NAL unit is encountered. Returns the size of the new data, will be 0 if an error occurred.
60 size_t prependSPSPPSToIDR(const uint8_t* src, size_t srcSize, uint8_t* dst, size_t dstSize,
61                           std::vector<uint8_t>* sps, std::vector<uint8_t>* pps);
62 
63 }  // namespace android
64 
65 #endif  // ANDROID_V4L2_CODEC2_COMMON_HELPERS_H
66