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 INNER_DEMUXER_PARSER_SAMPLE_H
17 #define INNER_DEMUXER_PARSER_SAMPLE_H
18 
19 #include <iostream>
20 #include <map>
21 #include "avdemuxer.h"
22 #include "avsource.h"
23 #include "nlohmann/json.hpp"
24 
25 struct JsonFrameLayerInfo {
26     int32_t frameId;
27     int64_t dts;
28     int32_t layer;
29     bool discardable;
30 };
31 
32 struct JsonGopInfo {
33     int32_t gopId;
34     int32_t gopSize;
35     int32_t startFrameId;
36     uint32_t layerCount = 0;
37     std::map<uint8_t, uint32_t> layerFrameNum;
38 };
39 
40 namespace OHOS {
41 namespace MediaAVCodec {
42 using AVBuffer = OHOS::Media::AVBuffer;
43 using AVAllocator = OHOS::Media::AVAllocator;
44 using AVAllocatorFactory = OHOS::Media::AVAllocatorFactory;
45 using MemoryFlag = OHOS::Media::MemoryFlag;
46 using FormatDataType = OHOS::Media::FormatDataType;
47 
48 enum struct MP4Scene : uint32_t {
49     ONE_I_FRAME_AVC = 0,
50     ALL_I_FRAME_AVC = 1,
51     IPB_FRAME_AVC,
52     SDTP_FRAME_AVC,
53     OPENGOP_FRAME_AVC,
54     LTR_FRAME_AVC,
55     TWO_LAYER_FRAME_AVC,
56     THREE_LAYER_FRAME_AVC,
57     FOUR_LAYER_FRAME_AVC,
58     ONE_I_FRAME_HEVC,
59     ALL_I_FRAME_HEVC,
60     IPB_FRAME_HEVC,
61     SDTP_FRAME_HEVC,
62     LTR_FRAME_HEVC,
63     TWO_LAYER_FRAME_HEVC,
64     THREE_LAYER_FRAME_HEVC,
65     FOUR_LAYER_FRAME_HEVC,
66     HDR_1_HEVC,
67     HDR_2_HEVC,
68     SDTP_EXTENDED_HEVC
69 };
70 
71 enum struct WorkPts : uint32_t {
72     START_PTS = 0,
73     END_PTS = 1,
74     RANDOM_PTS,
75     SPECIFIED_PTS
76 };
77 
78 class InnerDemuxerParserSample {
79 public:
80     InnerDemuxerParserSample(const std::string &filePath);
81     ~InnerDemuxerParserSample();
82     size_t GetFileSize(const std::string& filePath);
83     void InitParameter(MP4Scene scene);
84     bool RunSeekScene(WorkPts workPts);
85     bool RunSpeedScene(WorkPts workPts);
86 
87     std::shared_ptr<AVDemuxer> demuxer_ = nullptr;
88     std::shared_ptr<OHOS::Media::AVBuffer> avBuffer;
89     int64_t specified_pts = 0;
90 private:
91     void InitMP4Scene(MP4Scene scene);
92     void InitAVCScene(MP4Scene scene);
93     void InitHEVCScene(MP4Scene scene);
94     bool CheckGopLayerResult(GopLayerInfo &info, int32_t gopId);
95     bool CheckFrameLayerResult(FrameLayerInfo &info, int64_t dts, bool speedScene);
96     uint32_t GetGopIdFromFrameId(int32_t frameId);
97     int64_t GetPtsFromWorkPts(WorkPts workPts);
98     std::shared_ptr<AVSource> avsource_ = nullptr;
99     Format source_format_;
100     Format track_format_;
101     int32_t fd;
102     int32_t trackCount;
103     int64_t duration;
104     int32_t videoTrackIdx;
105     int64_t usleepTime = 300000;
106 
107     nlohmann::json gopJson_;
108     nlohmann::json frameLayerJson_;
109     std::vector<JsonFrameLayerInfo> frameVec_;
110     std::vector<JsonGopInfo> gopVec_;
111     std::map<int64_t, JsonFrameLayerInfo> frameMap_;
112     std::map<int32_t, JsonGopInfo> frameGopMap_;
113 };
114 }
115 }
116 #endif
117