1 /*
2  * Copyright (c) 2024 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 applicable 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 HEIF_HW_DECODER_DEMO_H
17 #define HEIF_HW_DECODER_DEMO_H
18 
19 #include <vector>
20 #include "command_parser.h"
21 #include "hardware/heif_hw_decoder.h"
22 
23 namespace OHOS::ImagePlugin {
24 class HeifHwDecoderFlow {
25 public:
26     HeifHwDecoderFlow() = default;
27     ~HeifHwDecoderFlow();
28     bool Run(const CommandOpt& opt);
29 private:
30     class InputParser {
31     public:
InputParser(const std::string & inputPath)32         explicit InputParser(const std::string& inputPath) : source_(inputPath) {}
33         ~InputParser() = default;
34         bool ParseGridInfo(GridInfo& gridInfo);
35         bool ReadInput(std::vector<std::vector<uint8_t>>& inputs);
36     private:
37         void FindXpsAndIFrameFile();
38         static void SplitString(const std::string& src, char sep, std::vector<std::string>& vec);
39         static std::string JoinPath(const std::string& base, const std::string& append);
40         static bool ReadFileToVec(const std::string& filePath, std::vector<std::vector<uint8_t>>& inputs);
41         static int ExtractIFrameNum(const std::string& filePath);
42 
43         static constexpr char MAIN_SEP = '_';
44         static constexpr size_t MIN_MAIN_SEG_CNT = 2;
45         static constexpr size_t MAX_MAIN_SEG_CNT = 4;
46         static constexpr char SUB_SEP = 'x';
47         static constexpr size_t SUB_SEG_CNT = 2;
48         static constexpr char NO_GRID_INDICATOR[] = "nogrid";
49         static constexpr char XPS_INDICATOR[] = "_hevc_xps";
50         static constexpr char I_FRAME_INDICATOR[] = "_hevc_I";
51         enum MainSeg {
52             DISPLAY_SIZE = 0,
53             GRID_FLAG,
54             TILE_SIZE,
55             GRID_SIZE
56         };
57         enum SubSeg {
58             HORIZONTAL = 0,
59             VERTICAL
60         };
61 
62         std::string source_;
63         std::string xpsFile_;
64         std::vector<std::string> iFrameFile_;
65     };
66 private:
67     bool PrepareInput(const std::string& inputPath);
68     bool AllocOutput(UserPixelFormat userPixelFormat);
69     bool DoDecode();
70 private:
71     GridInfo gridInfo_;
72     std::vector<std::vector<uint8_t>> input_;
73     sptr<SurfaceBuffer> output_;
74     HeifHardwareDecoder hwDecoder_;
75 };
76 } // OHOS::ImagePlugin
77 
78 #endif // HEIF_HW_DECODER_DEMO_H
79