166f3657fSopenharmony_ci/*
266f3657fSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
366f3657fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
466f3657fSopenharmony_ci * you may not use this file except in compliance with the License.
566f3657fSopenharmony_ci * You may obtain a copy of the License at
666f3657fSopenharmony_ci *
766f3657fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
866f3657fSopenharmony_ci *
966f3657fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1066f3657fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1166f3657fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1266f3657fSopenharmony_ci * See the License for the specific language governing permissions and
1366f3657fSopenharmony_ci * limitations under the License.
1466f3657fSopenharmony_ci */
1566f3657fSopenharmony_ci
1666f3657fSopenharmony_ci#ifndef AVCODEC_VDEC_DEMO_H
1766f3657fSopenharmony_ci#define AVCODEC_VDEC_DEMO_H
1866f3657fSopenharmony_ci
1966f3657fSopenharmony_ci#include <atomic>
2066f3657fSopenharmony_ci#include <fstream>
2166f3657fSopenharmony_ci#include <queue>
2266f3657fSopenharmony_ci#include <string>
2366f3657fSopenharmony_ci#include <thread>
2466f3657fSopenharmony_ci
2566f3657fSopenharmony_ci#include "avcodec_video_decoder.h"
2666f3657fSopenharmony_ci#include "nocopyable.h"
2766f3657fSopenharmony_ci
2866f3657fSopenharmony_cinamespace OHOS {
2966f3657fSopenharmony_cinamespace MediaAVCodec {
3066f3657fSopenharmony_ciclass VDecSignal {
3166f3657fSopenharmony_cipublic:
3266f3657fSopenharmony_ci    std::mutex inMutex_;
3366f3657fSopenharmony_ci    std::mutex outMutex_;
3466f3657fSopenharmony_ci    std::condition_variable inCond_;
3566f3657fSopenharmony_ci    std::condition_variable outCond_;
3666f3657fSopenharmony_ci    std::queue<uint32_t> inQueue_;
3766f3657fSopenharmony_ci    std::queue<uint32_t> outQueue_;
3866f3657fSopenharmony_ci    std::queue<std::shared_ptr<Media::AVSharedMemory>> availableInputBufferQueue_;
3966f3657fSopenharmony_ci};
4066f3657fSopenharmony_ci
4166f3657fSopenharmony_ciclass VDecDemoCallback : public AVCodecCallback, public NoCopyable {
4266f3657fSopenharmony_cipublic:
4366f3657fSopenharmony_ci    explicit VDecDemoCallback(std::shared_ptr<VDecSignal> signal) : signal_(signal) {};
4466f3657fSopenharmony_ci    virtual ~VDecDemoCallback() = default;
4566f3657fSopenharmony_ci
4666f3657fSopenharmony_ci    void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
4766f3657fSopenharmony_ci    void OnOutputFormatChanged(const Media::Format &format) override;
4866f3657fSopenharmony_ci    void OnInputBufferAvailable(uint32_t index, std::shared_ptr<Media::AVSharedMemory> buffer) override;
4966f3657fSopenharmony_ci    void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
5066f3657fSopenharmony_ci                                 std::shared_ptr<Media::AVSharedMemory> buffer) override;
5166f3657fSopenharmony_ci
5266f3657fSopenharmony_ciprivate:
5366f3657fSopenharmony_ci    std::shared_ptr<VDecSignal> signal_;
5466f3657fSopenharmony_ci};
5566f3657fSopenharmony_ci
5666f3657fSopenharmony_ciclass VDecDemo : public NoCopyable {
5766f3657fSopenharmony_cipublic:
5866f3657fSopenharmony_ci    VDecDemo() = default;
5966f3657fSopenharmony_ci    virtual ~VDecDemo() = default;
6066f3657fSopenharmony_ci    void RunCase();
6166f3657fSopenharmony_ci    void SetOutputSurface(sptr<Surface> surface);
6266f3657fSopenharmony_ci    void SetWindowSize(uint32_t width, uint32_t height);
6366f3657fSopenharmony_ci
6466f3657fSopenharmony_ciprivate:
6566f3657fSopenharmony_ci    int32_t CreateVdec();
6666f3657fSopenharmony_ci    int32_t Configure(const Media::Format &format);
6766f3657fSopenharmony_ci    int32_t Prepare();
6866f3657fSopenharmony_ci    int32_t Start();
6966f3657fSopenharmony_ci    int32_t Stop();
7066f3657fSopenharmony_ci    int32_t Flush();
7166f3657fSopenharmony_ci    int32_t Reset();
7266f3657fSopenharmony_ci    int32_t Release();
7366f3657fSopenharmony_ci    int32_t SetSurface();
7466f3657fSopenharmony_ci    const int32_t *GetFrameLen();
7566f3657fSopenharmony_ci    void InputFunc();
7666f3657fSopenharmony_ci    void OutputFunc();
7766f3657fSopenharmony_ci    void CheckCodecType();
7866f3657fSopenharmony_ci
7966f3657fSopenharmony_ci    std::atomic<bool> isRunning_ = false;
8066f3657fSopenharmony_ci    sptr<Surface> surface_ = nullptr;
8166f3657fSopenharmony_ci    uint32_t width_ = 0;
8266f3657fSopenharmony_ci    uint32_t height_ = 0;
8366f3657fSopenharmony_ci    std::unique_ptr<std::ifstream> testFile_;
8466f3657fSopenharmony_ci    std::unique_ptr<std::thread> inputLoop_;
8566f3657fSopenharmony_ci    std::unique_ptr<std::thread> outputLoop_;
8666f3657fSopenharmony_ci    std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> vdec_;
8766f3657fSopenharmony_ci    std::shared_ptr<VDecSignal> signal_;
8866f3657fSopenharmony_ci    std::shared_ptr<VDecDemoCallback> cb_;
8966f3657fSopenharmony_ci    bool isFirstFrame_ = true;
9066f3657fSopenharmony_ci    bool isW = true;
9166f3657fSopenharmony_ci    int64_t timeStamp_ = 0;
9266f3657fSopenharmony_ci    uint32_t frameCount_ = 0;
9366f3657fSopenharmony_ci    uint32_t defaultFrameCount_ = 0;
9466f3657fSopenharmony_ci};
9566f3657fSopenharmony_ci} // namespace MediaAVCodec
9666f3657fSopenharmony_ci} // namespace OHOS
9766f3657fSopenharmony_ci#endif // AVCODEC_VDEC_DEMO_H