• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   kARGB,
36   kRGB565,
37   kYUY2,
38   kYV12,
39   kUYVY,
40   kMJPEG,
41   kBGRA,
42   kNV12,
43 };
44 
45 // This is the max PSNR value our algorithms can return.
46 const double kPerfectPSNR = 48.0f;
47 
48 // Calculate the required buffer size.
49 // Input:
50 //   - type         :The type of the designated video frame.
51 //   - width        :frame width in pixels.
52 //   - height       :frame height in pixels.
53 // Return value:    :The required size in bytes to accommodate the specified
54 //                   video frame.
55 size_t CalcBufferSize(VideoType type, int width, int height);
56 
57 // Extract buffer from VideoFrame or I420BufferInterface (consecutive
58 // planes, no stride)
59 // Input:
60 //   - frame       : Reference to video frame.
61 //   - size        : pointer to the size of the allocated buffer. If size is
62 //                   insufficient, an error will be returned.
63 //   - buffer      : Pointer to buffer
64 // Return value: length of buffer if OK, < 0 otherwise.
65 int ExtractBuffer(const rtc::scoped_refptr<I420BufferInterface>& input_frame,
66                   size_t size,
67                   uint8_t* buffer);
68 int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer);
69 // Convert From I420
70 // Input:
71 //   - src_frame        : Reference to a source frame.
72 //   - dst_video_type   : Type of output video.
73 //   - dst_sample_size  : Required only for the parsing of MJPG.
74 //   - dst_frame        : Pointer to a destination frame.
75 // Return value: 0 if OK, < 0 otherwise.
76 // It is assumed that source and destination have equal height.
77 int ConvertFromI420(const VideoFrame& src_frame,
78                     VideoType dst_video_type,
79                     int dst_sample_size,
80                     uint8_t* dst_frame);
81 
82 rtc::scoped_refptr<I420BufferInterface> ScaleVideoFrameBuffer(
83     const I420BufferInterface& source,
84     int dst_width,
85     int dst_height);
86 
87 double I420SSE(const I420BufferInterface& ref_buffer,
88                const I420BufferInterface& test_buffer);
89 
90 // Compute PSNR for an I420 frame (all planes).
91 // Returns the PSNR in decibel, to a maximum of kPerfectPSNR.
92 double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame);
93 double I420PSNR(const I420BufferInterface& ref_buffer,
94                 const I420BufferInterface& test_buffer);
95 
96 // Computes the weighted PSNR-YUV for an I420 buffer.
97 //
98 // For the definition and motivation, see
99 // J. Ohm, G. J. Sullivan, H. Schwarz, T. K. Tan and T. Wiegand,
100 // "Comparison of the Coding Efficiency of Video Coding Standards—Including
101 // High Efficiency Video Coding (HEVC)," in IEEE Transactions on Circuits and
102 // Systems for Video Technology, vol. 22, no. 12, pp. 1669-1684, Dec. 2012
103 // doi: 10.1109/TCSVT.2012.2221192.
104 //
105 // Returns the PSNR-YUV in decibel, to a maximum of kPerfectPSNR.
106 double I420WeightedPSNR(const I420BufferInterface& ref_buffer,
107                         const I420BufferInterface& test_buffer);
108 
109 // Compute SSIM for an I420 frame (all planes).
110 double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame);
111 double I420SSIM(const I420BufferInterface& ref_buffer,
112                 const I420BufferInterface& test_buffer);
113 
114 // Helper function for scaling NV12 to NV12.
115 // If the `src_width` and `src_height` matches the `dst_width` and `dst_height`,
116 // then `tmp_buffer` is not used. In other cases, the minimum size of
117 // `tmp_buffer` should be:
118 //   (src_width/2) * (src_height/2) * 2 + (dst_width/2) * (dst_height/2) * 2
119 void NV12Scale(uint8_t* tmp_buffer,
120                const uint8_t* src_y,
121                int src_stride_y,
122                const uint8_t* src_uv,
123                int src_stride_uv,
124                int src_width,
125                int src_height,
126                uint8_t* dst_y,
127                int dst_stride_y,
128                uint8_t* dst_uv,
129                int dst_stride_uv,
130                int dst_width,
131                int dst_height);
132 
133 // Helper class for directly converting and scaling NV12 to I420. The Y-plane
134 // will be scaled directly to the I420 destination, which makes this faster
135 // than separate NV12->I420 + I420->I420 scaling.
136 class RTC_EXPORT NV12ToI420Scaler {
137  public:
138   NV12ToI420Scaler();
139   ~NV12ToI420Scaler();
140   void NV12ToI420Scale(const uint8_t* src_y,
141                        int src_stride_y,
142                        const uint8_t* src_uv,
143                        int src_stride_uv,
144                        int src_width,
145                        int src_height,
146                        uint8_t* dst_y,
147                        int dst_stride_y,
148                        uint8_t* dst_u,
149                        int dst_stride_u,
150                        uint8_t* dst_v,
151                        int dst_stride_v,
152                        int dst_width,
153                        int dst_height);
154 
155  private:
156   std::vector<uint8_t> tmp_uv_planes_;
157 };
158 
159 // Convert VideoType to libyuv FourCC type
160 int ConvertVideoType(VideoType video_type);
161 
162 }  // namespace webrtc
163 
164 #endif  // COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_
165