• 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::V2_0::CodecCompCapability caps,OMX_VIDEO_CODINGTYPE codingType)26     HEncoder(OHOS::HDI::Codec::V2_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     void UpdateFormatFromSurfaceBuffer() override;
50     int32_t AllocInBufsForDynamicSurfaceBuf();
51     int32_t SubmitAllBuffersOwnedByUs() override;
52     int32_t SubmitOutputBuffersToOmxNode() override;
53     bool ReadyToStart() override;
54 
55     // input buffer circulation
56     void OnGetBufferFromSurface() override;
57     bool GetOneBufferFromSurface();
58     void FindAllIdleSlotAndSubmit();
59     void SubmitOneBuffer(BufferInfo& info);
60     void OnOMXEmptyBufferDone(uint32_t bufferId, BufferOperationMode mode) override;
61     int32_t WrapSurfaceBufferIntoOmxBuffer(std::shared_ptr<OHOS::HDI::Codec::V2_0::OmxCodecBuffer>& omxBuffer,
62         const sptr<SurfaceBuffer>& surfaceBuffer, int64_t pts, uint32_t flag);
63     void OnSignalEndOfInputStream(const MsgInfo &msg) override;
64     void OnQueueInputBuffer(const MsgInfo &msg, BufferOperationMode mode) override;
65 
66     // stop/release
67     void EraseBufferFromPool(OMX_DIRTYPE portIndex, size_t i) override;
68     void OnEnterUninitializedState() override;
69 
70 private:
71     class EncoderBuffersConsumerListener : public IBufferConsumerListener {
72     public:
EncoderBuffersConsumerListener(HEncoder * codec)73         explicit EncoderBuffersConsumerListener(HEncoder *codec) : codec_(codec) {}
74         void OnBufferAvailable() override;
75     private:
76         HEncoder* codec_;
77     };
78 
79 private:
80     sptr<Surface> inputSurface_;
81     uint32_t inBufferCnt_ = 0;
82     static constexpr uint32_t THIRTY_MILLISECONDS_IN_US = 30'000;
83     static constexpr uint32_t SURFACE_MODE_CONSUMER_USAGE = BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_ENCODER;
84     static constexpr uint64_t BUFFER_MODE_REQUEST_USAGE =
85         SURFACE_MODE_CONSUMER_USAGE | BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_MMZ_CACHE;
86 
87     struct InSurfaceBufferEntry {
88         sptr<SurfaceBuffer> buffer;
89         sptr<SyncFence> fence;
90         int64_t timestamp;
91         OHOS::Rect damage;
92     };
93     std::list<InSurfaceBufferEntry> avaliableBuffers_;
94 };
95 } // namespace OHOS::MediaAVCodec
96 #endif // HCODEC_HENCODER_H
97