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 "avsharedmemorybase.h" 22 #include "format.h" 23 #include "surface.h" 24 #include "surface_memory.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 const int32_t VIDEO_ALIGN_SIZE = 16; 34 struct ScalePara { 35 int32_t srcWidth = 0; 36 int32_t srcHeight = 0; 37 AVPixelFormat srcFfFmt = AVPixelFormat::AV_PIX_FMT_NONE; 38 int32_t dstWidth = 0; 39 int32_t dstHeight = 0; 40 AVPixelFormat dstFfFmt = AVPixelFormat::AV_PIX_FMT_RGBA; 41 int32_t align = VIDEO_ALIGN_SIZE; 42 }; 43 struct Scale { 44 public: 45 int32_t Init(const ScalePara &scalePara, uint8_t **dstData, int32_t *dstLineSize); 46 int32_t Convert(uint8_t **srcData, const int32_t *srcLineSize, uint8_t **dstData, int32_t *dstLineSize); 47 48 private: 49 ScalePara scalePara_; 50 std::shared_ptr<SwsContext> swsCtx_ = nullptr; 51 }; 52 GraphicPixelFormat TranslateSurfaceFormat(const VideoPixelFormat &surfaceFormat); 53 VideoPixelFormat ConvertPixelFormatFromFFmpeg(int32_t ffmpegPixelFormat); 54 AVPixelFormat ConvertPixelFormatToFFmpeg(VideoPixelFormat pixelFormat); 55 GraphicTransformType TranslateSurfaceRotation(const VideoRotation &rotation); 56 int32_t ConvertVideoFrame(std::shared_ptr<Scale> *scale, std::shared_ptr<AVFrame> frame, uint8_t **dstData, 57 int32_t *dstLineSize, AVPixelFormat dstPixFmt); 58 int32_t WriteSurfaceData(const std::shared_ptr<SurfaceMemory> &surfaceMemory, uint8_t **scaleData, 59 int32_t *scaleLineSize, const Format &format); 60 int32_t WriteBufferData(const std::shared_ptr<AVSharedMemoryBase> &bufferMemory, uint8_t **scaleData, 61 int32_t *scaleLineSize, const Format &format); 62 63 std::string AVStrError(int errnum); 64 bool IsYuvFormat(VideoPixelFormat &format); 65 bool IsRgbFormat(VideoPixelFormat &format); 66 } // namespace Codec 67 } // namespace MediaAVCodec 68 } // namespace OHOS 69 #endif