• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 HCODEC_TESTER_COMMON_H
17 #define HCODEC_TESTER_COMMON_H
18 
19 #include <fstream>
20 #include <mutex>
21 #include <condition_variable>
22 #include <memory>
23 #include "surface.h"
24 #include "wm/window.h"  // foundation/window/window_manager/interfaces/innerkits/
25 #include "native_avbuffer.h" // foundation/multimedia/media_foundation/interface/kits/c
26 #include "buffer/avbuffer.h" // foundation/multimedia/media_foundation/interface/inner_api
27 #include "native_avcodec_base.h" // foundation/multimedia/av_codec/interfaces/kits/c/
28 #include "command_parser.h"
29 #include "start_code_detector.h"
30 #include "test_utils.h"
31 
32 namespace OHOS::MediaAVCodec {
33 struct Span {
34     uint8_t* va;
35     size_t capacity;
36 };
37 
38 struct ImgBuf : Span {
39     GraphicPixelFormat fmt;
40     uint32_t dispW;
41     uint32_t dispH;
42     uint32_t byteStride;
43 };
44 
45 struct BufInfo : ImgBuf {
46     uint32_t idx;
47     OH_AVCodecBufferAttr attr;
48     OH_AVMemory* mem = nullptr;
49     OH_AVBuffer* cavbuf = nullptr;
50     std::shared_ptr<Media::AVBuffer> avbuf;
51     sptr<SurfaceBuffer> surfaceBuf;
52 };
53 
54 struct TesterCommon {
55     static bool Run(const CommandOpt &opt);
56     bool RunOnce();
57 
58 protected:
59     static std::shared_ptr<TesterCommon> Create(const CommandOpt &opt);
TesterCommonTesterCommon60     explicit TesterCommon(const CommandOpt &opt) : opt_(opt) {}
61     virtual ~TesterCommon() = default;
62     static int64_t GetNowUs();
63     virtual bool Create() = 0;
64     virtual bool SetCallback() = 0;
65     virtual bool GetInputFormat() = 0;
66     virtual bool GetOutputFormat() = 0;
67     virtual bool Start() = 0;
68     void EncoderInputLoop();
69     void DecoderInputLoop();
70     void OutputLoop();
71     void BeforeQueueInput(OH_AVCodecBufferAttr& attr);
72     void AfterGotOutput(const OH_AVCodecBufferAttr& attr);
73     virtual bool WaitForInput(BufInfo& buf) = 0;
74     virtual bool WaitForOutput(BufInfo& buf) = 0;
75     virtual bool ReturnInput(const BufInfo& buf) = 0;
76     virtual bool ReturnOutput(uint32_t idx) = 0;
77     virtual bool Flush() = 0;
78     virtual void ClearAllBuffer() = 0;
79     virtual bool Stop() = 0;
80     virtual bool Release() = 0;
81 
82     const CommandOpt opt_;
83     std::ifstream ifs_;
84 
85     std::mutex inputMtx_;
86     std::condition_variable inputCond_;
87     uint32_t currInputCnt_ = 0;
88 
89     std::mutex outputMtx_;
90     std::condition_variable outputCond_;
91 
92     uint64_t inTotalCnt_ = 0;
93     int64_t firstInTime_ = 0;
94     double inFps_ = 0;
95     uint64_t outTotalCnt_ = 0;
96     int64_t firstOutTime_ = 0;
97     uint64_t totalCost_ = 0;
98 
99     // encoder only
100     bool RunEncoder();
101     virtual bool ConfigureEncoder() = 0;
102     virtual sptr<Surface> CreateInputSurface() = 0;
103     virtual bool NotifyEos() = 0;
104     virtual bool RequestIDR() = 0;
105     virtual std::optional<uint32_t> GetInputStride() = 0;
106     static bool SurfaceBufferToBufferInfo(BufInfo& buf, sptr<SurfaceBuffer> surfaceBuffer);
107     static bool NativeBufferToBufferInfo(BufInfo& buf, OH_NativeBuffer* nativeBuffer);
108     bool WaitForInputSurfaceBuffer(BufInfo& buf);
109     bool ReturnInputSurfaceBuffer(BufInfo& buf);
110     uint32_t ReadOneFrame(ImgBuf& dstImg);
111     uint32_t ReadOneFrameYUV420P(ImgBuf& dstImg);
112     uint32_t ReadOneFrameYUV420SP(ImgBuf& dstImg);
113     uint32_t ReadOneFrameRGBA(ImgBuf& dstImg);
114     sptr<Surface> producerSurface_;
115     GraphicPixelFormat displayFmt_;
116     static constexpr uint32_t BYTES_PER_PIXEL_RBGA = 4;
117     static constexpr uint32_t SAMPLE_RATIO = 2;
118 
119     // decoder only
120     class Listener : public IBufferConsumerListener {
121     public:
ListenerTesterCommon122         explicit Listener(TesterCommon *test) : tester_(test) {}
123         void OnBufferAvailable() override;
124     private:
125         TesterCommon *tester_;
126     };
127 
128     bool RunDecoder();
129     bool InitDemuxer();
130     sptr<Surface> CreateSurfaceFromWindow();
131     sptr<Surface> CreateSurfaceNormal();
132     virtual bool SetOutputSurface(sptr<Surface> &surface) = 0;
133     void PrepareSeek();
134     bool SeekIfNecessary(); // false means quit loop
135     virtual bool ConfigureDecoder() = 0;
136     int GetNextSample(const Span &dstSpan, size_t &sampleIdx, bool &isCsd); // return filledLen
137     sptr<Surface> surface_; // consumer
138     sptr<OHOS::Rosen::Window> window_;
139     std::shared_ptr<StartCodeDetector> demuxer_;
140     size_t totalSampleCnt_ = 0;
141     size_t currSampleIdx_ = 0;
142     std::list<std::pair<size_t, size_t>> userSeekPos_; // seek from which index to which index
143 
144     static bool RunDecEnc(const CommandOpt &decOpt);
145     void SaveVivid(int64_t pts);
146     void CheckVivid(const BufInfo& buf);
147     static std::mutex vividMtx_;
148     static std::unordered_map<int64_t, std::vector<uint8_t>> vividMap_;
149 };
150 } // namespace OHOS::MediaAVCodec
151 #endif