1 /*
2  * Copyright (C) 2024 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 HDRCODEC_SAMPLE_H
17 #define HDRCODEC_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_encoder.h"
31 #include "nocopyable.h"
32 #include "buffer/avsharedmemory.h"
33 #include "meta/format.h"
34 #include "avcodec_errors.h"
35 #include "surface/window.h"
36 #include "media_description.h"
37 #include "av_common.h"
38 #include "avcodec_common.h"
39 #include "external_window.h"
40 #include "avcodec_video_decoder.h"
41 
42 namespace OHOS {
43 namespace MediaAVCodec {
44 class InnerSignal {
45 public:
46     std::mutex inMutex_;
47     std::mutex outMutex_;
48     std::condition_variable inCond_;
49     std::condition_variable outCond_;
50     std::queue<uint32_t> inIdxQueue_;
51     std::queue<uint32_t> outIdxQueue_;
52     std::queue<std::shared_ptr<AVSharedMemory>> inBufferQueue_;
53     std::queue<std::shared_ptr<AVSharedMemory>> outBufferQueue_;
54 };
55 
56 class HdrEncInnerCallback : public AVCodecCallback, public NoCopyable {
57 public:
58     explicit HdrEncInnerCallback(std::shared_ptr<InnerSignal> signal);
59     ~HdrEncInnerCallback() = default;
60 
61     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
62     void OnOutputFormatChanged(const Format& format) override;
63     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
64     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
65         std::shared_ptr<AVSharedMemory> buffer) override;
66 
67 private:
68     std::shared_ptr<InnerSignal> encInnersignal_;
69 };
70 
71 class HdrDecInnerCallback : public AVCodecCallback, public NoCopyable {
72 public:
73     explicit HdrDecInnerCallback(std::shared_ptr<InnerSignal> signal);
74     ~HdrDecInnerCallback() = default;
75 
76     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
77     void OnOutputFormatChanged(const Format& format) override;
78     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
79     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
80         std::shared_ptr<AVSharedMemory> buffer) override;
81 
82 private:
83     std::shared_ptr<InnerSignal> decInnersignal_;
84 };
85 
86 class HDRCodecInnderNdkSample : public NoCopyable {
87 public:
88     HDRCodecInnderNdkSample() = default;
89     ~HDRCodecInnderNdkSample();
90     int32_t CreateCodec();
91     int32_t Configure();
92     int32_t Start();
93     void WaitForEos();
94     void ReleaseInFile();
95     void StopInloop();
96     void InputFunc();
97     void FlushBuffer();
98     void SwitchInputFile();
99     int32_t ReConfigure();
100     int32_t RepeatCall();
101     int32_t DecSetCallback();
102     int32_t EncSetCallback();
103     int32_t RepeatCallStartFlush();
104     int32_t RepeatCallStartStop();
105     int32_t RepeatCallStartFlushStop();
106     void Release();
107     int32_t SendData(std::shared_ptr<AVCodecVideoDecoder> codec,
108         uint32_t index, std::shared_ptr<AVSharedMemory> buffer);
109     const char *INP_DIR = "/data/test/media/1920_1080_10_30Mb.h264";
110     bool needEncode = false;
111     bool needTransCode = false;
112     uint32_t DEFAULT_WIDTH = 3840;
113     uint32_t DEFAULT_HEIGHT = 2160;
114     double DEFAULT_FRAME_RATE = 30.0;
115 
116     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
117     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
118     uint32_t REPEAT_START_FLUSH_STOP_BEFORE_EOS = 0;
119     uint32_t frameCount_ = 0;
120     uint32_t repeat_time = 0;
121     uint32_t frameCountDec = 0;
122     uint32_t frameCountEnc = 0;
123     int32_t DEFAULT_PROFILE = HEVC_PROFILE_MAIN_10;
124     bool needOutputFile;
125     std::unique_ptr<std::thread> inputLoop_;
126     std::shared_ptr<AVCodecVideoDecoder> vdec_;
127     std::shared_ptr<AVCodecVideoEncoder> venc_;
128     uint32_t errorCount = 0;
129 private:
130     OHNativeWindow *window;
131     std::shared_ptr<HdrEncInnerCallback> encCb_;
132     std::shared_ptr<HdrDecInnerCallback> decCb_;
133     std::shared_ptr<InnerSignal> signal_;
134 };
135 } // namespace Media
136 } // namespace OHOS
137 
138 #endif
139