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> |
| 19 | |
pbos@webrtc.org | c69ae69 | 2013-06-04 09:02:37 +0000 | [diff] [blame] | 20 | #include "webrtc/common_types.h" // RawVideoTypes. |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 21 | #include "webrtc/common_video/rotation.h" |
pbos@webrtc.org | c69ae69 | 2013-06-04 09:02:37 +0000 | [diff] [blame] | 22 | #include "webrtc/typedefs.h" |
Thiago Farina | 9bfe3da | 2015-04-10 12:52:13 +0200 | [diff] [blame] | 23 | #include "webrtc/video_frame.h" |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 27 | // Supported video types. |
| 28 | enum VideoType { |
| 29 | kUnknown, |
| 30 | kI420, |
| 31 | kIYUV, |
| 32 | kRGB24, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 33 | kABGR, |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 34 | kARGB, |
| 35 | kARGB4444, |
| 36 | kRGB565, |
| 37 | kARGB1555, |
| 38 | kYUY2, |
| 39 | kYV12, |
| 40 | kUYVY, |
| 41 | kMJPG, |
| 42 | kNV21, |
| 43 | kNV12, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 44 | kBGRA, |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
phoglund@webrtc.org | 273ccad | 2012-11-29 10:08:16 +0000 | [diff] [blame] | 47 | // This is the max PSNR value our algorithms can return. |
phoglund@webrtc.org | 5b689ef | 2012-12-13 10:15:06 +0000 | [diff] [blame] | 48 | const double kPerfectPSNR = 48.0f; |
phoglund@webrtc.org | 273ccad | 2012-11-29 10:08:16 +0000 | [diff] [blame] | 49 | |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 50 | // Conversion between the RawVideoType and the LibYuv videoType. |
| 51 | // TODO(wu): Consolidate types into one type throughout WebRtc. |
| 52 | VideoType RawVideoTypeToCommonVideoVideoType(RawVideoType type); |
| 53 | |
mikhal@webrtc.org | c7ecc11 | 2012-09-28 16:07:10 +0000 | [diff] [blame] | 54 | // Align integer values. |
mikhal@webrtc.org | 1a26588 | 2012-09-21 15:37:06 +0000 | [diff] [blame] | 55 | // Input: |
mikhal@webrtc.org | c7ecc11 | 2012-09-28 16:07:10 +0000 | [diff] [blame] | 56 | // - value : Input value to be aligned. |
| 57 | // - alignment : Alignment basis (power of 2). |
| 58 | // Return value: An aligned form of the input value. |
| 59 | int AlignInt(int value, int alignment); |
mikhal@webrtc.org | 1a26588 | 2012-09-21 15:37:06 +0000 | [diff] [blame] | 60 | |
mikhal@webrtc.org | 91a0340 | 2012-10-30 19:19:32 +0000 | [diff] [blame] | 61 | // Align stride values for I420 Video frames. |
| 62 | // Input: |
| 63 | // - width : Image width. |
| 64 | // - stride_y : Pointer to the stride of the y plane. |
| 65 | // - stride_uv: Pointer to the stride of the u and v planes (setting identical |
| 66 | // values for both). |
| 67 | // Setting 16 byte alignment. |
| 68 | void Calc16ByteAlignedStride(int width, int* stride_y, int* stride_uv); |
| 69 | |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 70 | // Calculate the required buffer size. |
| 71 | // Input: |
mikhal@webrtc.org | 1a26588 | 2012-09-21 15:37:06 +0000 | [diff] [blame] | 72 | // - type :The type of the designated video frame. |
| 73 | // - width :frame width in pixels. |
| 74 | // - height :frame height in pixels. |
| 75 | // Return value: :The required size in bytes to accommodate the specified |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 76 | // video frame. |
| 77 | size_t CalcBufferSize(VideoType type, int width, int height); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 78 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 79 | // 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] | 80 | // Print VideoFrame to file |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 81 | // Input: |
| 82 | // - frame : Reference to video frame. |
| 83 | // - file : pointer to file object. It is assumed that the file is |
| 84 | // already open for writing. |
| 85 | // Return value: 0 if OK, < 0 otherwise. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 86 | int PrintVideoFrame(const VideoFrame& frame, FILE* file); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 87 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 88 | // Extract buffer from VideoFrame (consecutive planes, no stride) |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 89 | // Input: |
| 90 | // - frame : Reference to video frame. |
| 91 | // - size : pointer to the size of the allocated buffer. If size is |
| 92 | // insufficient, an error will be returned. |
| 93 | // - buffer : Pointer to buffer |
| 94 | // Return value: length of buffer if OK, < 0 otherwise. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 95 | 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] | 96 | // Convert To I420 |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 97 | // Input: |
| 98 | // - src_video_type : Type of input video. |
| 99 | // - src_frame : Pointer to a source frame. |
| 100 | // - 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] | 101 | // - src_width : src width in pixels. |
| 102 | // - src_height : src height in pixels. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 103 | // - 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] | 104 | // - rotate : Rotation mode of output image. |
| 105 | // Output: |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 106 | // - dst_frame : Reference to a destination frame. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 107 | // Return value: 0 if OK, < 0 otherwise. |
| 108 | |
| 109 | int ConvertToI420(VideoType src_video_type, |
| 110 | const uint8_t* src_frame, |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 111 | int crop_x, |
| 112 | int crop_y, |
| 113 | int src_width, |
| 114 | int src_height, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 115 | size_t sample_size, |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 116 | VideoRotation rotation, |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 117 | VideoFrame* dst_frame); |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 118 | |
mikhal@webrtc.org | a2026ba | 2012-01-05 18:19:32 +0000 | [diff] [blame] | 119 | // Convert From I420 |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 120 | // Input: |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 121 | // - src_frame : Reference to a source frame. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 122 | // - dst_video_type : Type of output video. |
| 123 | // - dst_sample_size : Required only for the parsing of MJPG. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 124 | // - dst_frame : Pointer to a destination frame. |
| 125 | // Return value: 0 if OK, < 0 otherwise. |
mikhal@webrtc.org | 1e033e1 | 2012-10-01 20:09:32 +0000 | [diff] [blame] | 126 | // It is assumed that source and destination have equal height. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 127 | int ConvertFromI420(const VideoFrame& src_frame, |
| 128 | VideoType dst_video_type, |
| 129 | int dst_sample_size, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 130 | uint8_t* dst_frame); |
| 131 | // ConvertFrom YV12. |
| 132 | // Interface - same as above. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 133 | int ConvertFromYV12(const VideoFrame& src_frame, |
| 134 | VideoType dst_video_type, |
| 135 | int dst_sample_size, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 136 | uint8_t* dst_frame); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 137 | |
mikhal@webrtc.org | a2026ba | 2012-01-05 18:19:32 +0000 | [diff] [blame] | 138 | // The following list describes designated conversion functions which |
| 139 | // are not covered by the previous general functions. |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 140 | // Input and output descriptions mostly match the above descriptions, and are |
| 141 | // therefore omitted. |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 142 | int ConvertRGB24ToARGB(const uint8_t* src_frame, |
| 143 | uint8_t* dst_frame, |
| 144 | int width, int height, |
| 145 | int dst_stride); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 146 | int ConvertNV12ToRGB565(const uint8_t* src_frame, |
| 147 | uint8_t* dst_frame, |
| 148 | int width, int height); |
| 149 | |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 150 | // Compute PSNR for an I420 frame (all planes). |
phoglund@webrtc.org | 273ccad | 2012-11-29 10:08:16 +0000 | [diff] [blame] | 151 | // Returns the PSNR in decibel, to a maximum of kInfinitePSNR. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 152 | double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame); |
mikhal@webrtc.org | 3f9a721 | 2012-10-04 17:22:32 +0000 | [diff] [blame] | 153 | // Compute SSIM for an I420 frame (all planes). |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 154 | double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame); |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame^] | 155 | |
| 156 | } // namespace webrtc |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 157 | |
andrew@webrtc.org | c1354bd | 2012-07-27 18:21:16 +0000 | [diff] [blame] | 158 | #endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_ |