• 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_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 VDecNdkInnerFuzzSample : public NoCopyable {
73 public:
74     VDecNdkInnerFuzzSample() = default;
75     ~VDecNdkInnerFuzzSample();
76 
77     int64_t GetSystemTimeUs();
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 ReleaseOutputBuffer(uint32_t index);
88     int32_t SetCallback();
89     void CreateSurface();
90     int32_t InputFuncFUZZ(const uint8_t *data, size_t size);
91     int32_t SetOutputSurface();
92 
93     const char *outDir = "/data/test/media/VDecTest.yuv";
94     const char *outDir2 = "/data/test/media/VDecTest2.yuv";
95     uint32_t defaultWidth = 1920;
96     uint32_t defaultHeight = 1080;
97     double defaultFrameRate = 30.0;
98     uint32_t maxSurfNum = 2;
99     bool sfOutput = false;
100     bool autoSwitchSurface = false;
101     int32_t switchSurfaceFlag = 0;
102     bool isP3Full = false;
103     int32_t DEFAULT_FORMAT = static_cast<int32_t>(VideoPixelFormat::NV12);
104     int32_t defaultColorspace = 0;
105 
106     uint32_t errCount = 0;
107     std::atomic<bool> isRunning_ { false };
108     NativeWindow *nativeWindow[2] = {};
109     std::shared_ptr<AVCodecVideoDecoder> vdec_;
110 
111 private:
112     std::shared_ptr<VDecInnerSignal> signal_;
113     std::shared_ptr<VDecInnerCallback> cb_;
114     sptr<Surface> cs[2] = {};
115     sptr<Surface> ps[2] = {};
116 };
117 } // namespace MediaAVCodec
118 } // namespace OHOS
119 
120 #endif // VIDEODEC_INNER_SAMPLE_H
121