1da853ecaSopenharmony_ci/*
2da853ecaSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3da853ecaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4da853ecaSopenharmony_ci * you may not use this file except in compliance with the License.
5da853ecaSopenharmony_ci * You may obtain a copy of the License at
6da853ecaSopenharmony_ci *
7da853ecaSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8da853ecaSopenharmony_ci *
9da853ecaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10da853ecaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11da853ecaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12da853ecaSopenharmony_ci * See the License for the specific language governing permissions and
13da853ecaSopenharmony_ci * limitations under the License.
14da853ecaSopenharmony_ci */
15da853ecaSopenharmony_ci
16da853ecaSopenharmony_ci#ifndef VIDEODEC_SAMPLE_H
17da853ecaSopenharmony_ci#define VIDEODEC_SAMPLE_H
18da853ecaSopenharmony_ci
19da853ecaSopenharmony_ci#include <atomic>
20da853ecaSopenharmony_ci#include <cstdio>
21da853ecaSopenharmony_ci#include <fstream>
22da853ecaSopenharmony_ci#include <iostream>
23da853ecaSopenharmony_ci#include <mutex>
24da853ecaSopenharmony_ci#include <queue>
25da853ecaSopenharmony_ci#include <string>
26da853ecaSopenharmony_ci#include <thread>
27da853ecaSopenharmony_ci#include <unistd.h>
28da853ecaSopenharmony_ci#include <unordered_map>
29da853ecaSopenharmony_ci#include "native_avcodec_videodecoder.h"
30da853ecaSopenharmony_ci#include "native_averrors.h"
31da853ecaSopenharmony_ci#include "native_avformat.h"
32da853ecaSopenharmony_ci#include "native_avmemory.h"
33da853ecaSopenharmony_ci#include "nocopyable.h"
34da853ecaSopenharmony_ci#include "securec.h"
35da853ecaSopenharmony_ci#include "surface/window.h"
36da853ecaSopenharmony_ci#include "iconsumer_surface.h"
37da853ecaSopenharmony_cinamespace OHOS {
38da853ecaSopenharmony_cinamespace Media {
39da853ecaSopenharmony_ciclass VDecSignal {
40da853ecaSopenharmony_cipublic:
41da853ecaSopenharmony_ci    std::mutex inMutex_;
42da853ecaSopenharmony_ci    std::mutex outMutex_;
43da853ecaSopenharmony_ci    std::condition_variable inCond_;
44da853ecaSopenharmony_ci    std::condition_variable outCond_;
45da853ecaSopenharmony_ci    std::queue<uint32_t> inIdxQueue_;
46da853ecaSopenharmony_ci    std::queue<uint32_t> outIdxQueue_;
47da853ecaSopenharmony_ci    std::queue<OH_AVCodecBufferAttr> attrQueue_;
48da853ecaSopenharmony_ci    std::queue<OH_AVMemory *> inBufferQueue_;
49da853ecaSopenharmony_ci    std::queue<OH_AVMemory *> outBufferQueue_;
50da853ecaSopenharmony_ci};
51da853ecaSopenharmony_ci
52da853ecaSopenharmony_ciclass VDecFuzzSample : public NoCopyable {
53da853ecaSopenharmony_cipublic:
54da853ecaSopenharmony_ci    VDecFuzzSample() = default;
55da853ecaSopenharmony_ci    ~VDecFuzzSample();
56da853ecaSopenharmony_ci    int32_t RunVideoDec();
57da853ecaSopenharmony_ci    const char *inpDir = "/data/test/media/1920_1080_10_30Mb.h264";
58da853ecaSopenharmony_ci    const char *outDir = "/data/test/media/VDecTest.yuv";
59da853ecaSopenharmony_ci    uint32_t defaultWidth = 1920;
60da853ecaSopenharmony_ci    uint32_t defaultHeight = 1080;
61da853ecaSopenharmony_ci    uint32_t defaultFrameRate = 30;
62da853ecaSopenharmony_ci    uint32_t defaultRotation = 0;
63da853ecaSopenharmony_ci    uint32_t defaultPixelFormat = AV_PIXEL_FORMAT_NV12;
64da853ecaSopenharmony_ci    uint32_t frameCount_ = 0;
65da853ecaSopenharmony_ci    int32_t Start();
66da853ecaSopenharmony_ci    int32_t Stop();
67da853ecaSopenharmony_ci    int32_t Flush();
68da853ecaSopenharmony_ci    int32_t Reset();
69da853ecaSopenharmony_ci    void SetEOS(uint32_t index);
70da853ecaSopenharmony_ci    uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVMemory *buffer);
71da853ecaSopenharmony_ci    void CopyStartCode(uint8_t *frameBuffer, uint32_t bufferSize, OH_AVCodecBufferAttr &attr);
72da853ecaSopenharmony_ci    int32_t ReadData(uint32_t index, OH_AVMemory *buffer);
73da853ecaSopenharmony_ci    void WaitForEOS();
74da853ecaSopenharmony_ci    int32_t ConfigureVideoDecoder();
75da853ecaSopenharmony_ci    int32_t StartVideoDecoder();
76da853ecaSopenharmony_ci    int64_t GetSystemTimeUs();
77da853ecaSopenharmony_ci    int32_t CreateVideoDecoder();
78da853ecaSopenharmony_ci    int32_t SetVideoDecoderCallback();
79da853ecaSopenharmony_ci    int32_t Release();
80da853ecaSopenharmony_ci    void SetParameter(int32_t data);
81da853ecaSopenharmony_ci    void InputFuncAVCC();
82da853ecaSopenharmony_ci    OH_AVErrCode InputFuncFUZZ(const uint8_t *data, size_t size);
83da853ecaSopenharmony_ci    void ReleaseSignal();
84da853ecaSopenharmony_ci    void ReleaseInFile();
85da853ecaSopenharmony_ci    void StopInLoop();
86da853ecaSopenharmony_ci    VDecSignal *signal_;
87da853ecaSopenharmony_ci    uint32_t errCount = 0;
88da853ecaSopenharmony_ci    bool isSurfMode = false;
89da853ecaSopenharmony_ci    OH_AVCodec *vdec_;
90da853ecaSopenharmony_ci    OHNativeWindow *nativeWindow = nullptr;
91da853ecaSopenharmony_ci    sptr<Surface> cs = nullptr;
92da853ecaSopenharmony_ci    sptr<Surface> ps = nullptr;
93da853ecaSopenharmony_ciprivate:
94da853ecaSopenharmony_ci    std::atomic<bool> isRunning_ { false };
95da853ecaSopenharmony_ci    std::unique_ptr<std::ifstream> inFile_;
96da853ecaSopenharmony_ci    std::unique_ptr<std::thread> inputLoop_;
97da853ecaSopenharmony_ci    std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_;
98da853ecaSopenharmony_ci    OH_AVCodecAsyncCallback cb_;
99da853ecaSopenharmony_ci    int64_t timeStamp_ { 0 };
100da853ecaSopenharmony_ci    int64_t lastRenderedTimeUs_ { 0 };
101da853ecaSopenharmony_ci    bool isFirstFrame_ = true;
102da853ecaSopenharmony_ci};
103da853ecaSopenharmony_ci} // namespace Media
104da853ecaSopenharmony_ci} // namespace OHOS
105da853ecaSopenharmony_ci
106da853ecaSopenharmony_civoid VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData);
107da853ecaSopenharmony_civoid VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData);
108da853ecaSopenharmony_civoid VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData);
109da853ecaSopenharmony_civoid VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
110da853ecaSopenharmony_ci                         void *userData);
111da853ecaSopenharmony_ci#endif // VIDEODEC_SAMPLE_H
112