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 CODEC_UTILS_H 17 #define CODEC_UTILS_H 18 19 #include "av_common.h" 20 #include "avcodec_errors.h" 21 #include "buffer/avbuffer.h" 22 #include "fsurface_memory.h" 23 #include "meta/format.h" 24 #include "surface.h" 25 extern "C" { 26 #include "libavcodec/avcodec.h" 27 #include "libavutil/imgutils.h" 28 #include "libswscale/swscale.h" 29 }; 30 namespace OHOS { 31 namespace MediaAVCodec { 32 namespace Codec { 33 using AVMemory = Media::AVMemory; 34 using Format = Media::Format; 35 const int32_t VIDEO_ALIGN_SIZE = 16; 36 struct ScalePara { 37 int32_t srcWidth = 0; 38 int32_t srcHeight = 0; 39 AVPixelFormat srcFfFmt = AVPixelFormat::AV_PIX_FMT_NONE; 40 int32_t dstWidth = 0; 41 int32_t dstHeight = 0; 42 AVPixelFormat dstFfFmt = AVPixelFormat::AV_PIX_FMT_RGBA; 43 int32_t align = VIDEO_ALIGN_SIZE; 44 }; 45 struct Scale { 46 public: 47 int32_t Init(const ScalePara &scalePara, uint8_t **dstData, int32_t *dstLineSize); 48 int32_t Convert(uint8_t **srcData, const int32_t *srcLineSize, uint8_t **dstData, int32_t *dstLineSize); 49 50 private: 51 ScalePara scalePara_; 52 std::shared_ptr<SwsContext> swsCtx_ = nullptr; 53 }; 54 struct SurfaceInfo { 55 uint32_t surfaceStride = 0; 56 int32_t surfaceFence = 0; 57 uint8_t **scaleData = nullptr; 58 int32_t *scaleLineSize = nullptr; 59 }; 60 GraphicPixelFormat TranslateSurfaceFormat(const VideoPixelFormat &surfaceFormat); 61 VideoPixelFormat ConvertPixelFormatFromFFmpeg(int32_t ffmpegPixelFormat); 62 AVPixelFormat ConvertPixelFormatToFFmpeg(VideoPixelFormat pixelFormat); 63 GraphicTransformType TranslateSurfaceRotation(const VideoRotation &rotation); 64 int32_t ConvertVideoFrame(std::shared_ptr<Scale> *scale, std::shared_ptr<AVFrame> frame, uint8_t **dstData, 65 int32_t *dstLineSize, AVPixelFormat dstPixFmt); 66 int32_t WriteSurfaceData(const std::shared_ptr<AVMemory> &memory, struct SurfaceInfo &surfaceInfo, 67 const Format &format); 68 int32_t WriteBufferData(const std::shared_ptr<AVMemory> &memory, uint8_t **scaleData, int32_t *scaleLineSize, 69 const Format &format); 70 71 std::string AVStrError(int errnum); 72 bool IsYuvFormat(VideoPixelFormat &format); 73 bool IsRgbFormat(VideoPixelFormat &format); 74 } // namespace Codec 75 } // namespace MediaAVCodec 76 } // namespace OHOS 77 #endif