• 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 HCODEC_HENCODER_H
17 #define HCODEC_HENCODER_H
18 
19 #include "hcodec.h"
20 #include "codec_omx_ext.h"
21 #include "sync_fence.h"
22 
23 namespace OHOS::MediaAVCodec {
24 class HEncoder : public HCodec {
25 public:
HEncoder(OHOS::HDI::Codec::V1_0::CodecCompCapability caps,OMX_VIDEO_CODINGTYPE codingType)26     HEncoder(OHOS::HDI::Codec::V1_0::CodecCompCapability caps, OMX_VIDEO_CODINGTYPE codingType)
27         : HCodec(caps, codingType, true) {}
28 
29 private:
30     // configure
31     int32_t OnConfigure(const Format &format) override;
32     int32_t SetupPort(const Format &format, std::optional<double> frameRate);
33     void CalcInputBufSize(PortInfo& info, VideoPixelFormat pixelFmt);
34     int32_t UpdateInPortFormat() override;
35     int32_t UpdateOutPortFormat() override;
36     int32_t ConfigureOutputBitrate(const Format &format);
37     static std::optional<uint32_t> GetBitRateFromUser(const Format &format);
38     int32_t SetupAVCEncoderParameters(const Format &format, std::optional<double> frameRate);
39     void SetAvcFields(OMX_VIDEO_PARAM_AVCTYPE& avcType, int32_t iFrameInterval, double frameRate);
40     int32_t SetupHEVCEncoderParameters(const Format &format, std::optional<double> frameRate);
41     int32_t SetColorAspects(const Format &format);
42     int32_t OnSetParameters(const Format &format) override;
43     sptr<Surface> OnCreateInputSurface() override;
44     int32_t OnSetInputSurface(sptr<Surface> &inputSurface) override;
45     int32_t RequestIDRFrame() override;
46 
47     // start
48     int32_t AllocateBuffersOnPort(OMX_DIRTYPE portIndex) override;
49     int32_t AllocInBufsForDynamicSurfaceBuf();
50     std::shared_ptr<OHOS::HDI::Codec::V1_0::OmxCodecBuffer> AllocOmxBufferOfDynamicType();
51     int32_t SubmitAllBuffersOwnedByUs() override;
52     int32_t SubmitOutputBuffersToOmxNode() override;
53     bool ReadyToStart() override;
54 
55     // input buffer circulation
56     void OnGetBufferFromSurface() override;
57     void FindAllIdleSlotAndSubmit();
58     void SubmitOneBuffer(BufferInfo& info);
59     void OnOMXEmptyBufferDone(uint32_t bufferId, BufferOperationMode mode) override;
60     int32_t WrapSurfaceBufferIntoOmxBuffer(std::shared_ptr<OHOS::HDI::Codec::V1_0::OmxCodecBuffer>& omxBuffer,
61         const sptr<SurfaceBuffer>& surfaceBuffer, int64_t pts);
62     void OnSignalEndOfInputStream(const MsgInfo &msg) override;
63     void OnQueueInputBuffer(const MsgInfo &msg, BufferOperationMode mode) override;
64 
65     // stop/release
66     void EraseBufferFromPool(OMX_DIRTYPE portIndex, size_t i) override;
67 
68 private:
69     class EncoderBuffersConsumerListener : public IBufferConsumerListener {
70     public:
EncoderBuffersConsumerListener(HEncoder * codec)71         explicit EncoderBuffersConsumerListener(HEncoder *codec) : codec_(codec) {}
72         void OnBufferAvailable();
73     private:
74         HEncoder* codec_;
75     };
76 
77 private:
78     sptr<Surface> inputSurface_;
79     BufferType inputBufferType_ = BufferType::DYNAMIC_SURFACE_BUFFER;
80     uint32_t inBufferCnt_ = 0;
81     static constexpr uint32_t THIRTY_MILLISECONDS_IN_US = 30'000;
82     struct InSurfaceBufferEntry {
83         sptr<SurfaceBuffer> buffer;
84         sptr<SyncFence> fence;
85         int64_t timestamp;
86         OHOS::Rect damage;
87     };
88     std::list<InSurfaceBufferEntry> avaliableBuffers;
89 };
90 } // namespace OHOS::MediaAVCodec
91 #endif // HCODEC_HENCODER_H
92