• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 FCODEC_H
17 #define FCODEC_H
18 
19 #include <atomic>
20 #include <list>
21 #include <map>
22 #include <shared_mutex>
23 #include <tuple>
24 #include <vector>
25 #include <optional>
26 #include "av_common.h"
27 #include "avcodec_common.h"
28 #include "avcodec_info.h"
29 #include "block_queue.h"
30 #include "codec_utils.h"
31 #include "codecbase.h"
32 #include "media_description.h"
33 #include "fsurface_memory.h"
34 #include "task_thread.h"
35 
36 namespace OHOS {
37 namespace MediaAVCodec {
38 namespace Codec {
39 using AVBuffer = Media::AVBuffer;
40 using AVAllocator = Media::AVAllocator;
41 using AVAllocatorFactory = Media::AVAllocatorFactory;
42 using MemoryFlag = Media::MemoryFlag;
43 class FCodec : public CodecBase {
44 public:
45     explicit FCodec(const std::string &name);
46     ~FCodec() override;
47     int32_t Configure(const Format &format) override;
48     int32_t Start() override;
49     int32_t Stop() override;
50     int32_t Flush() override;
51     int32_t Reset() override;
52     int32_t Release() override;
53     int32_t SetParameter(const Format &format) override;
54     int32_t GetOutputFormat(Format &format) override;
55 
56     int32_t QueueInputBuffer(uint32_t index) override;
57     int32_t ReleaseOutputBuffer(uint32_t index) override;
58     int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) override;
59     int32_t SetOutputSurface(sptr<Surface> surface) override;
60     int32_t RenderOutputBuffer(uint32_t index) override;
61     static int32_t GetCodecCapability(std::vector<CapabilityData> &capaArray);
62 
63     struct FBuffer {
64     public:
65         FBuffer() = default;
66         ~FBuffer() = default;
67 
68         enum class Owner {
69             OWNED_BY_US,
70             OWNED_BY_CODEC,
71             OWNED_BY_USER,
72             OWNED_BY_SURFACE,
73         };
74 
75         std::shared_ptr<AVBuffer> avBuffer_ = nullptr;
76         std::shared_ptr<FSurfaceMemory> sMemory_ = nullptr;
77         std::atomic<Owner> owner_ = Owner::OWNED_BY_US;
78         int32_t width_ = 0;
79         int32_t height_ = 0;
80     };
81 
82 private:
83     int32_t Init();
84 
85     enum struct State : int32_t {
86         Uninitialized,
87         Initialized,
88         Configured,
89         Stopping,
90         Running,
91         Flushed,
92         Flushing,
93         EOS,
94         Error,
95     };
96     bool IsActive() const;
97     void ResetContext(bool isFlush = false);
98     void CalculateBufferSize();
99     int32_t AllocateBuffers();
100     void InitBuffers();
101     void ResetBuffers();
102     void ResetData();
103     void ReleaseBuffers();
104     void StopThread();
105     void ReleaseResource();
106     int32_t UpdateBuffers(uint32_t index, int32_t bufferSize, uint32_t bufferType);
107     int32_t UpdateSurfaceMemory(uint32_t index);
108     void SendFrame();
109     void ReceiveFrame();
110     void RenderFrame();
111     void ConfigureSurface(const Format &format, const std::string_view &formatKey, uint32_t FORMAT_TYPE);
112     void ConfigureDefaultVal(const Format &format, const std::string_view &formatKey, int32_t minVal = 0,
113                              int32_t maxVal = INT_MAX);
114     int32_t ConfigureContext(const Format &format);
115     void FramePostProcess(std::shared_ptr<FBuffer> &frameBuffer, uint32_t index, int32_t status, int ret);
116     int32_t AllocateInputBuffer(int32_t bufferCnt, int32_t inBufferSize);
117     int32_t AllocateOutputBuffer(int32_t bufferCnt, int32_t outBufferSize);
118     int32_t FillFrameBuffer(const std::shared_ptr<FBuffer> &frameBuffer);
119     int32_t CheckFormatChange(uint32_t index, int width, int height);
120     void SetSurfaceParameter(const Format &format, const std::string_view &formatKey, uint32_t FORMAT_TYPE);
121     int32_t FlushSurfaceMemory(std::shared_ptr<FSurfaceMemory> &surfaceMemory, int64_t pts);
122 
123     std::string codecName_;
124     std::atomic<State> state_ = State::Uninitialized;
125     Format format_;
126     int32_t width_ = 0;
127     int32_t height_ = 0;
128     int32_t inputBufferSize_ = 0;
129     int32_t outputBufferSize_ = 0;
130     // INIT
131     std::shared_ptr<AVCodec> avCodec_ = nullptr;
132     // Config
133     std::shared_ptr<AVCodecContext> avCodecContext_ = nullptr;
134     // Start
135     std::shared_ptr<AVPacket> avPacket_ = nullptr;
136     std::shared_ptr<AVFrame> cachedFrame_ = nullptr;
137     // Receive frame
138     uint8_t *scaleData_[AV_NUM_DATA_POINTERS];
139     int32_t scaleLineSize_[AV_NUM_DATA_POINTERS];
140     std::shared_ptr<Scale> scale_ = nullptr;
141     bool isConverted_ = false;
142     bool isOutBufSetted_ = false;
143     VideoPixelFormat outputPixelFmt_ = VideoPixelFormat::UNKNOWN;
144     // Running
145     std::vector<std::shared_ptr<FBuffer>> buffers_[2];
146     std::shared_ptr<AVBuffer> outAVBuffer4Surface_ = nullptr;
147     std::shared_ptr<BlockQueue<uint32_t>> inputAvailQue_;
148     std::shared_ptr<BlockQueue<uint32_t>> codecAvailQue_;
149     std::shared_ptr<BlockQueue<uint32_t>> renderAvailQue_;
150     std::optional<uint32_t> synIndex_ = std::nullopt;
151     sptr<Surface> surface_ = nullptr;
152     std::shared_ptr<TaskThread> sendTask_ = nullptr;
153     std::shared_ptr<TaskThread> receiveTask_ = nullptr;
154     std::shared_ptr<TaskThread> renderTask_ = nullptr;
155     std::mutex inputMutex_;
156     std::mutex outputMutex_;
157     std::mutex sendMutex_;
158     std::mutex recvMutex_;
159     std::mutex syncMutex_;
160     std::mutex surfaceMutex_;
161     std::condition_variable sendCv_;
162     std::condition_variable recvCv_;
163     std::shared_ptr<MediaCodecCallback> callback_;
164     std::atomic<bool> isSendWait_ = false;
165     std::atomic<bool> isSendEos_ = false;
166     std::atomic<bool> isBufferAllocated_ = false;
167 };
168 } // namespace Codec
169 } // namespace MediaAVCodec
170 } // namespace OHOS
171 #endif // FCODEC_H