1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef OHOS_HDI_CODEC_IMAGE_V2_1_CODEC_HEIF_DECODE_HELPER
17 #define OHOS_HDI_CODEC_IMAGE_V2_1_CODEC_HEIF_DECODE_HELPER
18
19 #include <vector>
20 #include "command_parser.h"
21 #include "v2_1/icodec_image.h"
22 #include "v1_2/display_composer_type.h"
23 #include "v1_2/display_buffer_type.h"
24 #include "v1_2/include/idisplay_buffer.h"
25 #include "v2_0/cm_color_space.h"
26
27 template <typename T>
Pod2Vec(const T & t)28 std::vector<uint8_t> Pod2Vec(const T& t)
29 {
30 const uint8_t* begin = reinterpret_cast<const uint8_t*>(&t);
31 const uint8_t* end = begin + sizeof(T);
32 return std::vector<uint8_t>(begin, end);
33 }
34
35 namespace OHOS::VDI::HEIF {
36 class HeifDecoderHelper {
37 public:
HeifDecoderHelper(const CommandOpt & opt)38 explicit HeifDecoderHelper(const CommandOpt& opt) : decodeOpt_(opt) {}
39 ~HeifDecoderHelper() = default;
40 void DoDecode();
41 private:
42 bool IsHeifHardwareDecodeSupported(OHOS::sptr<OHOS::HDI::Codec::Image::V2_1::ICodecImage>& hdiHeifDecoder);
43 bool GetOutputFormat();
44 bool ReadInput(std::vector<OHOS::sptr<OHOS::Ashmem>>& inputs);
45 bool AllocateOutputBuffer(OHOS::sptr<OHOS::HDI::Base::NativeBuffer>& output);
46 void DumpOutput(OHOS::sptr<OHOS::HDI::Base::NativeBuffer>& output);
47 void GetSampleSize();
48 void GetMetaDataInfo(OHOS::HDI::Display::Graphic::Common::V2_0::CM_ColorSpaceInfo& colorSpaceInfo);
49
50 private:
51 class InputParser {
52 public:
InputParser(const std::string & inputPath)53 explicit InputParser(const std::string& inputPath) : source_(inputPath) {}
54 ~InputParser() = default;
55 bool ParseGridInfo(OHOS::HDI::Codec::Image::V2_1::GridInfo& gridInfo);
56 bool ReadInput(std::vector<OHOS::sptr<OHOS::Ashmem>>& inputs);
57 private:
58 void FindXpsAndIFrameFile();
59 static void SplitString(const std::string& src, char sep, std::vector<std::string>& vec);
60 static std::string JoinPath(const std::string& base, const std::string& append);
61 static bool ReadFileToAshmem(const std::string& filePath, std::vector<OHOS::sptr<OHOS::Ashmem>>& inputs);
62 static int ExtractIFrameNum(const std::string& filePath);
63
64 static constexpr char MAIN_SEP = '_';
65 static constexpr size_t MIN_MAIN_SEG_CNT = 2;
66 static constexpr size_t MAX_MAIN_SEG_CNT = 4;
67 static constexpr char SUB_SEP = 'x';
68 static constexpr size_t SUB_SEG_CNT = 2;
69 static constexpr char NO_GRID_INDICATOR[] = "nogrid";
70 static constexpr char XPS_INDICATOR[] = "_hevc_xps";
71 static constexpr char I_FRAME_INDICATOR[] = "_hevc_I";
72 enum MainSeg {
73 DISPLAY_SIZE = 0,
74 GRID_FLAG,
75 TILE_SIZE,
76 GRID_SIZE
77 };
78 enum SubSeg {
79 HORIZONTAL = 0,
80 VERTICAL
81 };
82
83 std::string source_;
84 std::string xpsFile_;
85 std::vector<std::string> iFrameFile_;
86 };
87 struct BufferFormatInfo {
88 OHOS::HDI::Display::Composer::V1_2::PixelFormat format = OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_BUTT;
89 std::string desc = "unknow";
90 };
91 private:
92 static constexpr int MAX_PATH_LEN = 256;
93 static constexpr char DUMP_PATH[] = "/data/log/imagecodecdump";
94 CommandOpt decodeOpt_;
95 BufferFormatInfo outputFormat_;
96 OHOS::HDI::Codec::Image::V2_1::CodecHeifDecInfo decInfo_;
97 };
98 } // OHOS::VDI::HEIF
99 #endif // OHOS_HDI_CODEC_IMAGE_V2_1_CODEC_HEIF_DECODE_HELPER