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 BUFFER_CONVERTER_H 17 #define BUFFER_CONVERTER_H 18 #include <functional> 19 #include <shared_mutex> 20 #include "avcodec_dfx_component.h" 21 #include "avcodec_info.h" 22 #include "buffer/avbuffer.h" 23 #include "buffer/avsharedmemorybase.h" 24 #include "meta/format.h" 25 26 namespace OHOS { 27 namespace MediaAVCodec { 28 using AVBuffer = OHOS::Media::AVBuffer; 29 class CodecClient; 30 31 class BufferConverter : public AVCodecDfxComponent { 32 public: 33 BufferConverter(); 34 ~BufferConverter() = default; 35 static std::shared_ptr<BufferConverter> Create(AVCodecType type); 36 bool Init(AVCodecType type); 37 int32_t ReadFromBuffer(std::shared_ptr<AVBuffer> &buffer, std::shared_ptr<AVSharedMemory> &memory); 38 int32_t WriteToBuffer(std::shared_ptr<AVBuffer> &buffer, std::shared_ptr<AVSharedMemory> &memory); 39 40 void NeedToResetFormatOnce(); 41 void GetFormat(Format &format); 42 void SetFormat(const Format &format); 43 void SetInputBufferFormat(std::shared_ptr<AVBuffer> &buffer); 44 void SetOutputBufferFormat(std::shared_ptr<AVBuffer> &buffer); 45 typedef struct AVCodecRect { 46 int32_t wStride = 0; 47 int32_t hStride = 0; 48 } AVCodecRect; 49 50 private: 51 void SetPixFormat(const VideoPixelFormat pixelFormat); 52 void SetWidth(const int32_t width); 53 void SetHeight(const int32_t height); 54 void SetWidthStride(const int32_t wStride); 55 void SetHeightStride(const int32_t hStride); 56 57 bool SetBufferFormat(std::shared_ptr<AVBuffer> &buffer); 58 bool SetRectValue(const int32_t width, const int32_t height, const int32_t wStride, const int32_t hStride); 59 int32_t GetSliceHeightFromSurfaceBuffer(sptr<SurfaceBuffer> &surfaceBuffer) const; 60 61 static int32_t CalculateUserStride(const int32_t widthHeight); 62 std::function<int32_t(uint8_t *, uint8_t *, AVCodecRect *, int32_t)> func_; 63 AVCodecRect rect_; 64 AVCodecRect hwRect_; 65 AVCodecRect usrRect_; 66 bool isEncoder_ = false; 67 bool isSharedMemory_ = false; 68 bool needResetFormat_ = true; 69 std::shared_mutex mutex_; 70 }; 71 } // namespace MediaAVCodec 72 } // namespace OHOS 73 #endif // BUFFER_CONVERTER_H 74