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 VIDEODEC_INNER_SAMPLE_H
17 #define VIDEODEC_INNER_SAMPLE_H
18 
19 #include <iostream>
20 #include <cstdio>
21 #include <unistd.h>
22 #include <atomic>
23 #include <fstream>
24 #include <thread>
25 #include <mutex>
26 #include <queue>
27 #include <string>
28 #include <unordered_map>
29 #include "securec.h"
30 #include "avcodec_video_decoder.h"
31 #include "nocopyable.h"
32 #include "buffer/avsharedmemory.h"
33 #include "meta/format.h"
34 #include "avcodec_errors.h"
35 #include "media_description.h"
36 #include "av_common.h"
37 #include "avcodec_common.h"
38 #include "surface/window.h"
39 
40 namespace OHOS {
41 namespace MediaAVCodec {
42 class VDecInnerSignal {
43 public:
44     std::mutex inMutex_;
45     std::mutex outMutex_;
46     std::condition_variable inCond_;
47     std::condition_variable outCond_;
48     std::queue<uint32_t> inIdxQueue_;
49     std::queue<uint32_t> outIdxQueue_;
50     std::queue<AVCodecBufferInfo> infoQueue_;
51     std::queue<AVCodecBufferFlag> flagQueue_;
52     std::queue<std::shared_ptr<AVSharedMemory>> inBufferQueue_;
53     std::queue<std::shared_ptr<AVSharedMemory>> outBufferQueue_;
54 };
55 
56 class VDecInnerCallback : public AVCodecCallback, public NoCopyable {
57 public:
58     explicit VDecInnerCallback(std::shared_ptr<VDecInnerSignal> signal);
59     ~VDecInnerCallback() = default;
60 
61     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
62     void OnOutputFormatChanged(const Format& format) override;
63     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
64     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
65         std::shared_ptr<AVSharedMemory> buffer) override;
66 
67 private:
68     std::shared_ptr<VDecInnerSignal> innersignal_;
69 };
70 
71 class VDecNdkInnerSample : public NoCopyable {
72 public:
73     VDecNdkInnerSample() = default;
74     ~VDecNdkInnerSample();
75 
76     int64_t GetSystemTimeUs();
77     int32_t CreateByMime(const std::string &mime);
78     int32_t CreateByName(const std::string &name);
79     int32_t Configure();
80     int32_t Prepare();
81     int32_t Start();
82     int32_t Stop();
83     int32_t Flush();
84     int32_t Reset();
85     int32_t Release();
86     int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag);
87     int32_t GetOutputFormat(Format &format);
88     int32_t ReleaseOutputBuffer(uint32_t index);
89     int32_t SetParameter(const Format &format);
90     int32_t SetCallback();
91 
92     int32_t StartVideoDecoder();
93     int32_t RunVideoDecoder(const std::string &codeName);
94     int32_t PushData(std::shared_ptr<AVSharedMemory> buffer, uint32_t index);
95     int32_t SendData(uint32_t bufferSize, uint32_t index, std::shared_ptr<AVSharedMemory> buffer);
96     int32_t StateEOS();
97     void RepeatStartBeforeEOS();
98     void SetEOS(uint32_t index);
99     void WaitForEOS();
100     void OpenFileFail();
101     void InputFunc();
102     void OutputFunc();
103     void ProcessOutputData(std::shared_ptr<AVSharedMemory> buffer, uint32_t index);
104     void FlushBuffer();
105     void StopInloop();
106     void StopOutloop();
107     void ReleaseInFile();
108     bool MdCompare(unsigned char *buffer, int len, const char *source[]);
109 
110     const char *INP_DIR = "/data/test/media/1920_1080_10_30Mb.h264";
111     const char *OUT_DIR = "/data/test/media/VDecTest.yuv";
112     uint32_t DEFAULT_WIDTH = 1920;
113     uint32_t DEFAULT_HEIGHT = 1080;
114     uint32_t DEFAULT_BITRATE = 10000000;
115     double DEFAULT_FRAME_RATE = 30.0;
116     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
117     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
118     bool SF_OUTPUT = false;
119     bool BEFORE_EOS_INPUT = false;              // 0800 测试用例
120     bool BEFORE_EOS_INPUT_INPUT = false;        // 0900 测试用例
121     bool AFTER_EOS_DESTORY_CODEC = true;        // 1000 测试用例 结束不销毁codec
122 
123     uint32_t errCount = 0;
124     uint32_t outCount = 0;
125     uint32_t frameCount = 0;
126     bool sleepOnFPS = false;
127     bool repeatRun = false;
128     bool enableRandomEos = false;
129     const char *fileSourcesha256[64] = {"27", "6D", "A2", "D4", "18", "21", "A5", "CD", "50", "F6", "DD", "CA", "46",
130                                         "32", "C3", "FE", "58", "FC", "BC", "51", "FD", "70", "C7", "D4", "E7", "4D",
131                                         "5C", "76", "E7", "71", "8A", "B3", "C0", "51", "84", "0A", "FA", "AF", "FA",
132                                         "DC", "7B", "C5", "26", "D1", "9A", "CA", "00", "DE", "FC", "C8", "4E", "34",
133                                         "C5", "9A", "43", "59", "85", "DC", "AC", "97", "A3", "FB", "23", "51"};
134 
135 private:
136     std::atomic<bool> isRunning_ { false };
137     std::unique_ptr<std::ifstream> inFile_;
138     std::unique_ptr<std::thread> inputLoop_;
139     std::unique_ptr<std::thread> outputLoop_;
140     std::shared_ptr<AVCodecVideoDecoder> vdec_;
141     std::shared_ptr<VDecInnerSignal> signal_;
142     std::shared_ptr<VDecInnerCallback> cb_;
143 };
144 } // namespace MediaAVCodec
145 } // namespace OHOS
146 
147 #endif // VIDEODEC_INNER_SAMPLE_H
148