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 FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_PIXEL_YUV_UTILS_H 17 #define FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_PIXEL_YUV_UTILS_H 18 19 #include <cstdlib> 20 #include <cstdio> 21 #include <string> 22 #include "image_type.h" 23 #include "iosfwd" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 #include "libavfilter/avfilter.h" 29 #include "libavutil/avstring.h" 30 #include "libavfilter/buffersrc.h" 31 #include "libavfilter/buffersink.h" 32 #include "libavutil/frame.h" 33 #include "libavutil/imgutils.h" 34 #include "libavutil/pixfmt.h" 35 #include "libswscale/swscale.h" 36 #ifdef __cplusplus 37 } 38 #endif 39 40 namespace OHOS { 41 namespace Media { 42 43 struct XYaxis { 44 float xAxis = 0; 45 float yAxis = 0; 46 }; 47 48 struct PixelSize { 49 int32_t srcW = 0; 50 int32_t srcH = 0; 51 int32_t dstW = 0; 52 int32_t dstH = 0; 53 }; 54 55 struct YuvImageInfo { 56 AVPixelFormat format = AVPixelFormat::AV_PIX_FMT_NONE; 57 int32_t width; 58 int32_t height; 59 PixelFormat yuvFormat; 60 YUVDataInfo yuvDataInfo; 61 uint32_t pixelsSize = 0; 62 }; 63 64 struct YuvPixelsP010Translate { 65 uint16_t *srcPixels; 66 uint16_t *dstPixels; 67 }; 68 69 class PixelYuvUtils { 70 public: 71 static int32_t YuvConvertOption(const AntiAliasingOption &option); 72 static bool WriteYuvConvert(const void *srcPixels, const ImageInfo &srcInfo, void *dstPixels, 73 const Position &dstPos, const YUVDataInfo &yuvDataInfo); 74 static bool ReadYuvConvert(const void *srcPixels, const Position &srcPos, YuvImageInfo &srcInfo, 75 void *dstPixels, const ImageInfo &dstInfo); 76 static void SetTranslateDataDefault(uint8_t *srcPixels, int32_t width, int32_t height, PixelFormat format, 77 YUVStrideInfo &dstStrides); 78 static uint8_t GetYuv420Y(uint32_t x, uint32_t y, YUVDataInfo &info, const uint8_t *in); 79 static uint8_t GetYuv420U(uint32_t x, uint32_t y, YUVDataInfo &info, PixelFormat format, 80 const uint8_t *in); 81 static uint8_t GetYuv420V(uint32_t x, uint32_t y, YUVDataInfo &info, PixelFormat format, 82 const uint8_t *in); 83 static AVPixelFormat ConvertFormat(const PixelFormat &pixelFormat); 84 static bool BGRAToYuv420(const uint8_t *src, YuvImageInfo &srcInfo, uint8_t *dst, YuvImageInfo &dstInfo); 85 static bool Yuv420ToBGRA(const uint8_t *in, YuvImageInfo &srcInfo, uint8_t *out, 86 YuvImageInfo &dstInfo); 87 static bool Yuv420ToARGB(const uint8_t *in, YuvImageInfo &srcInfo, uint8_t *out, YuvImageInfo &dstInfo); 88 static bool YuvTranslate(const uint8_t *srcPixels, YUVDataInfo &yuvInfo, uint8_t *dstPixels, XYaxis &xyAxis, 89 ImageInfo &info, YUVStrideInfo &dstStrides); 90 static bool Yuv420WritePixels(const YUVDataInfo &yuvInfo, uint8_t *srcPixels, ImageInfo &info, 91 const uint32_t &color); 92 static bool YuvWritePixel(uint8_t *srcPixels, const YUVDataInfo &yuvDataInfo, const PixelFormat &format, 93 const Position &pos, const uint32_t &color); 94 static bool YuvCrop(uint8_t *srcData, YuvImageInfo &srcInfo, uint8_t *dstData, const Rect &rect, 95 YUVStrideInfo &dstStrides); 96 static bool YuvRotate(uint8_t *srcData, YuvImageInfo &srcInfo, 97 uint8_t *dstData, YuvImageInfo &dstInfo, int32_t degrees); 98 static bool YuvFlip(uint8_t *srcData, YuvImageInfo &srcInfo, uint8_t *dstData, bool xAxis); 99 static bool YuvReversal(uint8_t *srcData, YuvImageInfo &srcInfo, uint8_t *dstData, YuvImageInfo &dstInfo); 100 static int32_t YuvScale(uint8_t *srcPixels, YuvImageInfo &srcInfo, 101 uint8_t *dstPixels, YuvImageInfo &dstInfo, int32_t module); 102 static void Yuv420SPTranslate(const uint8_t *srcPixels, YUVDataInfo &yuvInfo, 103 uint8_t *dstPixels, XYaxis &xyAxis, ImageInfo &info, YUVStrideInfo &strides); 104 static bool CheckWidthAndHeightMult(const int32_t &width, const int32_t &height, const int32_t &multiples); 105 106 /** 107 * @brief Check the axis is legal, about the overflow and not finite and compare with the max dimension, 108 * if the axis is legal, return true, otherwise return false 109 * @param xAxis The x axis it is the offset of the width 110 * @param yAxis The y axis it is the offset of the height 111 * @param info The image info it is the image info of the source image 112 * @return true if the axis is legal, false otherwise 113 */ 114 static bool IsLegalAxis(float xAxis, float yAxis, ImageInfo &info); 115 }; 116 } // namespace Media 117 } // namespace OHOS 118 119 #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_PIXEL_YUV_UTILS_H 120