webrtc_libyuv.h (5824B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 /* 12 * WebRTC's wrapper to libyuv. 13 */ 14 15 #ifndef COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_ 16 #define COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_ 17 18 #include <stdint.h> 19 #include <stdio.h> 20 21 #include <vector> 22 23 #include "api/scoped_refptr.h" 24 #include "api/video/video_frame.h" 25 #include "api/video/video_frame_buffer.h" 26 #include "rtc_base/system/rtc_export.h" 27 28 namespace webrtc { 29 30 enum class VideoType { 31 kUnknown, 32 kI420, 33 kIYUV, 34 kRGB24, 35 kBGR24, 36 kARGB, 37 kABGR, 38 kARGB4444, 39 kRGB565, 40 kARGB1555, 41 kYUY2, 42 kYV12, 43 kUYVY, 44 kMJPEG, 45 kNV21, 46 kBGRA, 47 kNV12, 48 }; 49 50 // This is the max PSNR value our algorithms can return. 51 const double kPerfectPSNR = 48.0f; 52 53 // Calculate the required buffer size. 54 // Input: 55 // - type :The type of the designated video frame. 56 // - width :frame width in pixels. 57 // - height :frame height in pixels. 58 // Return value: :The required size in bytes to accommodate the specified 59 // video frame. 60 size_t CalcBufferSize(VideoType type, int width, int height); 61 62 // Extract buffer from VideoFrame or I420BufferInterface (consecutive 63 // planes, no stride) 64 // Input: 65 // - frame : Reference to video frame. 66 // - size : pointer to the size of the allocated buffer. If size is 67 // insufficient, an error will be returned. 68 // - buffer : Pointer to buffer 69 // Return value: length of buffer if OK, < 0 otherwise. 70 int ExtractBuffer(const scoped_refptr<I420BufferInterface>& input_frame, 71 size_t size, 72 uint8_t* buffer); 73 int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer); 74 // Convert From I420 75 // Input: 76 // - src_frame : Reference to a source frame. 77 // - dst_video_type : Type of output video. 78 // - dst_sample_size : Required only for the parsing of MJPG. 79 // - dst_frame : Pointer to a destination frame. 80 // Return value: 0 if OK, < 0 otherwise. 81 // It is assumed that source and destination have equal height. 82 int ConvertFromI420(const VideoFrame& src_frame, 83 VideoType dst_video_type, 84 int dst_sample_size, 85 uint8_t* dst_frame); 86 87 scoped_refptr<I420BufferInterface> ScaleVideoFrameBuffer( 88 const I420BufferInterface& source, 89 int dst_width, 90 int dst_height); 91 92 double I420SSE(const I420BufferInterface& ref_buffer, 93 const I420BufferInterface& test_buffer); 94 95 // Compute PSNR for an I420 frame (all planes). 96 // Returns the PSNR in decibel, to a maximum of kPerfectPSNR. 97 double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame); 98 double I420PSNR(const I420BufferInterface& ref_buffer, 99 const I420BufferInterface& test_buffer); 100 101 // Computes the weighted PSNR-YUV for an I420 buffer. 102 // 103 // For the definition and motivation, see 104 // J. Ohm, G. J. Sullivan, H. Schwarz, T. K. Tan and T. Wiegand, 105 // "Comparison of the Coding Efficiency of Video Coding Standards—Including 106 // High Efficiency Video Coding (HEVC)," in IEEE Transactions on Circuits and 107 // Systems for Video Technology, vol. 22, no. 12, pp. 1669-1684, Dec. 2012 108 // doi: 10.1109/TCSVT.2012.2221192. 109 // 110 // Returns the PSNR-YUV in decibel, to a maximum of kPerfectPSNR. 111 double I420WeightedPSNR(const I420BufferInterface& ref_buffer, 112 const I420BufferInterface& test_buffer); 113 114 // Compute SSIM for an I420 frame (all planes). 115 double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame); 116 double I420SSIM(const I420BufferInterface& ref_buffer, 117 const I420BufferInterface& test_buffer); 118 119 // Helper function for scaling NV12 to NV12. 120 // If the `src_width` and `src_height` matches the `dst_width` and `dst_height`, 121 // then `tmp_buffer` is not used. In other cases, the minimum size of 122 // `tmp_buffer` should be: 123 // (src_width/2) * (src_height/2) * 2 + (dst_width/2) * (dst_height/2) * 2 124 void NV12Scale(uint8_t* tmp_buffer, 125 const uint8_t* src_y, 126 int src_stride_y, 127 const uint8_t* src_uv, 128 int src_stride_uv, 129 int src_width, 130 int src_height, 131 uint8_t* dst_y, 132 int dst_stride_y, 133 uint8_t* dst_uv, 134 int dst_stride_uv, 135 int dst_width, 136 int dst_height); 137 138 // Helper class for directly converting and scaling NV12 to I420. The Y-plane 139 // will be scaled directly to the I420 destination, which makes this faster 140 // than separate NV12->I420 + I420->I420 scaling. 141 class RTC_EXPORT NV12ToI420Scaler { 142 public: 143 NV12ToI420Scaler(); 144 ~NV12ToI420Scaler(); 145 void NV12ToI420Scale(const uint8_t* src_y, 146 int src_stride_y, 147 const uint8_t* src_uv, 148 int src_stride_uv, 149 int src_width, 150 int src_height, 151 uint8_t* dst_y, 152 int dst_stride_y, 153 uint8_t* dst_u, 154 int dst_stride_u, 155 uint8_t* dst_v, 156 int dst_stride_v, 157 int dst_width, 158 int dst_height); 159 160 private: 161 std::vector<uint8_t> tmp_uv_planes_; 162 }; 163 164 // Convert VideoType to libyuv FourCC type 165 int ConvertVideoType(VideoType video_type); 166 167 } // namespace webrtc 168 169 #endif // COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_