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