• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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_SAMPLE_H
17 #define VIDEODEC_SAMPLE_H
18 
19 #include <atomic>
20 #include <cstdio>
21 #include <fstream>
22 #include <iostream>
23 #include <mutex>
24 #include <queue>
25 #include <string>
26 #include <thread>
27 #include <unistd.h>
28 #include <unordered_map>
29 #include "native_avcodec_videodecoder.h"
30 #include "native_averrors.h"
31 #include "native_avformat.h"
32 #include "native_avmemory.h"
33 #include "nocopyable.h"
34 #include "securec.h"
35 #include "window.h"
36 #include "iconsumer_surface.h"
37 namespace OHOS {
38 namespace Media {
39 class VDecSignal {
40 public:
41     std::mutex inMutex_;
42     std::mutex outMutex_;
43     std::condition_variable inCond_;
44     std::condition_variable outCond_;
45     std::queue<uint32_t> inIdxQueue_;
46     std::queue<uint32_t> outIdxQueue_;
47     std::queue<OH_AVCodecBufferAttr> attrQueue_;
48     std::queue<OH_AVMemory *> inBufferQueue_;
49     std::queue<OH_AVMemory *> outBufferQueue_;
50 };
51 
52 class VDecFuzzSample : public NoCopyable {
53 public:
54     VDecFuzzSample() = default;
55     ~VDecFuzzSample();
56     int32_t RunVideoDec(std::string codeName = "");
57     const char *inpDir = "/data/test/media/mpeg4_main@level4_1920x1080_30.m4v";
58     const char *outDir = "/data/test/media/VDecTest.yuv";
59     uint32_t defaultWidth = 1920;
60     uint32_t defaultHeight = 1080;
61     uint32_t defaultFrameRate = 30;
62     uint32_t defaultRotation = 0;
63 
64     uint32_t defaultPixelFormat = 1;
65     uint32_t frameCount_ = 0;
66     const char *fileSourcesha256[64] = {"27", "6D", "A2", "D4", "18", "21", "A5", "CD", "50", "F6", "DD", "CA", "46",
67                                         "32", "C3", "FE", "58", "FC", "BC", "51", "FD", "70", "C7", "D4", "E7", "4D",
68                                         "5C", "76", "E7", "71", "8A", "B3", "C0", "51", "84", "0A", "FA", "AF", "FA",
69                                         "DC", "7B", "C5", "26", "D1", "9A", "CA", "00", "DE", "FC", "C8", "4E", "34",
70                                         "C5", "9A", "43", "59", "85", "DC", "AC", "97", "A3", "FB", "23", "51"};
71 
72     int32_t Start();
73     int32_t Stop();
74     int32_t Flush();
75     int32_t Reset();
76     void SetEOS(uint32_t index);
77     uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVMemory *buffer);
78     int32_t ReadData(uint32_t index, OH_AVMemory *buffer);
79     void WaitForEOS();
80     int32_t ConfigureVideoDecoder();
81     int32_t StartVideoDecoder();
82     int64_t GetSystemTimeUs();
83     int32_t CreateVideoDecoder(std::string codeName);
84     int32_t SetVideoDecoderCallback();
85     int32_t Release();
86     int32_t SetParameter(OH_AVFormat *format);
87     void GetBufferSize();
88     void PtrStep(uint32_t &bufferSize, unsigned char **pBuffer, uint32_t size);
89     void PtrStepExtraRead(uint32_t &bufferSize, unsigned char **pBuffer);
90     void OutputFunc();
91     void WriteOutputFrame(uint32_t index, OH_AVMemory *buffer, OH_AVCodecBufferAttr attr, FILE *outFile);
92     void InputFuncAVCC();
93     OH_AVErrCode InputFuncFUZZ(const uint8_t *data, size_t size);
94     void ReleaseSignal();
95     void ReleaseInFile();
96     void StopInloop();
97     VDecSignal *signal_;
98     uint32_t errCount = 0;
99     uint32_t outCount = 0;
100     int64_t outTimeArray[2000] = {};
101     bool sleepOnFPS = false;
102     bool repeatRun = false;
103     bool isSurfMode = false;
104     bool setParameters = false;
105     OH_AVCodec *vdec_;
106     OHNativeWindow *nativeWindow = nullptr;
107     sptr<Surface> cs = nullptr;
108     sptr<Surface> ps = nullptr;
109     std::atomic<bool> isRunning_ { false };
110 private:
111     std::unique_ptr<std::ifstream> inFile_;
112     std::unique_ptr<std::thread> inputLoop_;
113     std::unique_ptr<uint8_t []> prereadBuffer_ = nullptr;
114     std::unique_ptr<std::vector<uint8_t>> mpegUnit_ = nullptr;
115     std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_;
116     std::unordered_map<uint32_t, OH_AVMemory *> outBufferMap_;
117     OH_AVCodecAsyncCallback cb_;
118     int64_t timeStamp_ { 0 };
119     int64_t lastRenderedTimeUs_ { 0 };
120     bool isFirstFrame_ = true;
121     bool finishLastPush = false;
122     uint32_t pPrereadBuffer_ = 0;
123     uint32_t prereadBufferSize_ = 0;
124 };
125 } // namespace Media
126 } // namespace OHOS
127 
128 void VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData);
129 void VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData);
130 void VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData);
131 void VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
132                          void *userData);
133 #endif // VIDEODEC_SAMPLE_H
134