mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 3ebff4c | 2012-05-04 17:07:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 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 | /* |
andrew@webrtc.org | c1354bd | 2012-07-27 18:21:16 +0000 | [diff] [blame] | 12 | * WebRTC's wrapper to libyuv. |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
andrew@webrtc.org | c1354bd | 2012-07-27 18:21:16 +0000 | [diff] [blame] | 15 | #ifndef WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_ |
| 16 | #define WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_ |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 17 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
magjed | 1a7ef1f | 2016-09-17 02:39:03 -0700 | [diff] [blame] | 19 | #include <vector> |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 20 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 21 | #include "webrtc/api/video/video_frame.h" |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 22 | #include "webrtc/common_types.h" // VideoTypes. |
pbos@webrtc.org | c69ae69 | 2013-06-04 09:02:37 +0000 | [diff] [blame] | 23 | #include "webrtc/typedefs.h" |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 27 | class I420Buffer; |
| 28 | |
phoglund@webrtc.org | 273ccad | 2012-11-29 10:08:16 +0000 | [diff] [blame] | 29 | // This is the max PSNR value our algorithms can return. |
phoglund@webrtc.org | 5b689ef | 2012-12-13 10:15:06 +0000 | [diff] [blame] | 30 | const double kPerfectPSNR = 48.0f; |
phoglund@webrtc.org | 273ccad | 2012-11-29 10:08:16 +0000 | [diff] [blame] | 31 | |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 32 | // TODO(nisse): Some downstream apps call CalcBufferSize with |
| 33 | // ::webrtc::kI420 as the first argument. Delete after they are updated. |
| 34 | const VideoType kI420 = VideoType::kI420; |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 35 | |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 36 | // Calculate the required buffer size. |
| 37 | // Input: |
mikhal@webrtc.org | 1a26588 | 2012-09-21 15:37:06 +0000 | [diff] [blame] | 38 | // - type :The type of the designated video frame. |
| 39 | // - width :frame width in pixels. |
| 40 | // - height :frame height in pixels. |
| 41 | // Return value: :The required size in bytes to accommodate the specified |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 42 | // video frame. |
| 43 | size_t CalcBufferSize(VideoType type, int width, int height); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 44 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 45 | // TODO(mikhal): Add unit test for these two functions and determine location. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 46 | // Print VideoFrame to file |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 47 | // Input: |
| 48 | // - frame : Reference to video frame. |
| 49 | // - file : pointer to file object. It is assumed that the file is |
| 50 | // already open for writing. |
| 51 | // Return value: 0 if OK, < 0 otherwise. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 52 | int PrintVideoFrame(const VideoFrame& frame, FILE* file); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 53 | int PrintVideoFrame(const I420BufferInterface& frame, FILE* file); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 54 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 55 | // Extract buffer from VideoFrame or I420BufferInterface (consecutive |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 56 | // planes, no stride) |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 57 | // Input: |
| 58 | // - frame : Reference to video frame. |
| 59 | // - size : pointer to the size of the allocated buffer. If size is |
| 60 | // insufficient, an error will be returned. |
| 61 | // - buffer : Pointer to buffer |
| 62 | // Return value: length of buffer if OK, < 0 otherwise. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 63 | int ExtractBuffer(const rtc::scoped_refptr<I420BufferInterface>& input_frame, |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 64 | size_t size, |
| 65 | uint8_t* buffer); |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 66 | int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer); |
mikhal@webrtc.org | a2026ba | 2012-01-05 18:19:32 +0000 | [diff] [blame] | 67 | // Convert To I420 |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 68 | // Input: |
| 69 | // - src_video_type : Type of input video. |
| 70 | // - src_frame : Pointer to a source frame. |
| 71 | // - crop_x/crop_y : Starting positions for cropping (0 for no crop). |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 72 | // - src_width : src width in pixels. |
| 73 | // - src_height : src height in pixels. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 74 | // - sample_size : Required only for the parsing of MJPG (set to 0 else). |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 75 | // - rotate : Rotation mode of output image. |
| 76 | // Output: |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 77 | // - dst_buffer : Reference to a destination frame buffer. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 78 | // Return value: 0 if OK, < 0 otherwise. |
| 79 | |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 80 | // TODO(nisse): Delete this wrapper, and let users call libyuv directly. Most |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 81 | // calls pass |src_video_type| == kI420, and should use libyuv::I420Copy. Also |
| 82 | // remember to delete the I420Buffer forward declaration above. The only |
| 83 | // exception at the time of this writing is VideoCaptureImpl::IncomingFrame, |
| 84 | // which still needs libyuv::ConvertToI420. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 85 | int ConvertToI420(VideoType src_video_type, |
| 86 | const uint8_t* src_frame, |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 87 | int crop_x, |
| 88 | int crop_y, |
| 89 | int src_width, |
| 90 | int src_height, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 91 | size_t sample_size, |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 92 | VideoRotation rotation, |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 93 | I420Buffer* dst_buffer); |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 94 | |
mikhal@webrtc.org | a2026ba | 2012-01-05 18:19:32 +0000 | [diff] [blame] | 95 | // Convert From I420 |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 96 | // Input: |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 97 | // - src_frame : Reference to a source frame. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 98 | // - dst_video_type : Type of output video. |
| 99 | // - dst_sample_size : Required only for the parsing of MJPG. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 100 | // - dst_frame : Pointer to a destination frame. |
| 101 | // Return value: 0 if OK, < 0 otherwise. |
mikhal@webrtc.org | 1e033e1 | 2012-10-01 20:09:32 +0000 | [diff] [blame] | 102 | // It is assumed that source and destination have equal height. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 103 | int ConvertFromI420(const VideoFrame& src_frame, |
| 104 | VideoType dst_video_type, |
| 105 | int dst_sample_size, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 106 | uint8_t* dst_frame); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 107 | |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 108 | // Compute PSNR for an I420 frame (all planes). |
phoglund@webrtc.org | 273ccad | 2012-11-29 10:08:16 +0000 | [diff] [blame] | 109 | // Returns the PSNR in decibel, to a maximum of kInfinitePSNR. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 110 | double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 111 | double I420PSNR(const I420BufferInterface& ref_buffer, |
| 112 | const I420BufferInterface& test_buffer); |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 113 | |
mikhal@webrtc.org | 3f9a721 | 2012-10-04 17:22:32 +0000 | [diff] [blame] | 114 | // Compute SSIM for an I420 frame (all planes). |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 115 | double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 116 | double I420SSIM(const I420BufferInterface& ref_buffer, |
| 117 | const I420BufferInterface& test_buffer); |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 118 | |
magjed | 5a87245 | 2016-10-20 03:34:29 -0700 | [diff] [blame] | 119 | // Helper function for scaling NV12 to NV12. |
Anders Carlsson | bfe45c2 | 2017-06-19 16:46:31 +0200 | [diff] [blame^] | 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, |
magjed | 5a87245 | 2016-10-20 03:34:29 -0700 | [diff] [blame] | 125 | const uint8_t* src_y, int src_stride_y, |
| 126 | const uint8_t* src_uv, int src_stride_uv, |
| 127 | int src_width, int src_height, |
| 128 | uint8_t* dst_y, int dst_stride_y, |
| 129 | uint8_t* dst_uv, int dst_stride_uv, |
| 130 | int dst_width, int dst_height); |
| 131 | |
magjed | 1a7ef1f | 2016-09-17 02:39:03 -0700 | [diff] [blame] | 132 | // Helper class for directly converting and scaling NV12 to I420. The Y-plane |
| 133 | // will be scaled directly to the I420 destination, which makes this faster |
| 134 | // than separate NV12->I420 + I420->I420 scaling. |
| 135 | class NV12ToI420Scaler { |
| 136 | public: |
magjed | 3149e09 | 2017-05-08 05:32:05 -0700 | [diff] [blame] | 137 | NV12ToI420Scaler(); |
| 138 | ~NV12ToI420Scaler(); |
magjed | 1a7ef1f | 2016-09-17 02:39:03 -0700 | [diff] [blame] | 139 | void NV12ToI420Scale(const uint8_t* src_y, int src_stride_y, |
| 140 | const uint8_t* src_uv, int src_stride_uv, |
| 141 | int src_width, int src_height, |
| 142 | uint8_t* dst_y, int dst_stride_y, |
| 143 | uint8_t* dst_u, int dst_stride_u, |
| 144 | uint8_t* dst_v, int dst_stride_v, |
| 145 | int dst_width, int dst_height); |
| 146 | private: |
| 147 | std::vector<uint8_t> tmp_uv_planes_; |
| 148 | }; |
| 149 | |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 150 | } // namespace webrtc |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 151 | |
andrew@webrtc.org | c1354bd | 2012-07-27 18:21:16 +0000 | [diff] [blame] | 152 | #endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_ |