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