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 | |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 20 | #include "common_types.h" // RawVideoTypes. |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 21 | #include "common_video/interface/i420_video_frame.h" |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 22 | #include "typedefs.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 26 | // Supported video types. |
| 27 | enum VideoType { |
| 28 | kUnknown, |
| 29 | kI420, |
| 30 | kIYUV, |
| 31 | kRGB24, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 32 | kABGR, |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 33 | kARGB, |
| 34 | kARGB4444, |
| 35 | kRGB565, |
| 36 | kARGB1555, |
| 37 | kYUY2, |
| 38 | kYV12, |
| 39 | kUYVY, |
| 40 | kMJPG, |
| 41 | kNV21, |
| 42 | kNV12, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 43 | kBGRA, |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 46 | // Conversion between the RawVideoType and the LibYuv videoType. |
| 47 | // TODO(wu): Consolidate types into one type throughout WebRtc. |
| 48 | VideoType RawVideoTypeToCommonVideoVideoType(RawVideoType type); |
| 49 | |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 50 | // Supported rotation |
| 51 | // Direction of rotation - clockwise. |
| 52 | enum VideoRotationMode { |
| 53 | kRotateNone = 0, |
| 54 | kRotate90 = 90, |
| 55 | kRotate180 = 180, |
| 56 | kRotate270 = 270, |
| 57 | }; |
| 58 | |
mikhal@webrtc.org | c7ecc11 | 2012-09-28 16:07:10 +0000 | [diff] [blame] | 59 | // Align integer values. |
mikhal@webrtc.org | 1a26588 | 2012-09-21 15:37:06 +0000 | [diff] [blame] | 60 | // Input: |
mikhal@webrtc.org | c7ecc11 | 2012-09-28 16:07:10 +0000 | [diff] [blame] | 61 | // - value : Input value to be aligned. |
| 62 | // - alignment : Alignment basis (power of 2). |
| 63 | // Return value: An aligned form of the input value. |
| 64 | int AlignInt(int value, int alignment); |
mikhal@webrtc.org | 1a26588 | 2012-09-21 15:37:06 +0000 | [diff] [blame] | 65 | |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 66 | // Calculate the required buffer size. |
| 67 | // Input: |
mikhal@webrtc.org | 1a26588 | 2012-09-21 15:37:06 +0000 | [diff] [blame] | 68 | // - type :The type of the designated video frame. |
| 69 | // - width :frame width in pixels. |
| 70 | // - height :frame height in pixels. |
| 71 | // Return value: :The required size in bytes to accommodate the specified |
| 72 | // video frame or -1 in case of an error . |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 73 | int CalcBufferSize(VideoType type, int width, int height); |
| 74 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 75 | // TODO(mikhal): Add unit test for these two functions and determine location. |
| 76 | // Print I420VideoFrame to file |
| 77 | // Input: |
| 78 | // - frame : Reference to video frame. |
| 79 | // - file : pointer to file object. It is assumed that the file is |
| 80 | // already open for writing. |
| 81 | // Return value: 0 if OK, < 0 otherwise. |
| 82 | int PrintI420VideoFrame(const I420VideoFrame& frame, FILE* file); |
| 83 | |
| 84 | // Extract buffer from I420VideoFrame (consecutive planes, no stride) |
| 85 | // Input: |
| 86 | // - frame : Reference to video frame. |
| 87 | // - size : pointer to the size of the allocated buffer. If size is |
| 88 | // insufficient, an error will be returned. |
| 89 | // - buffer : Pointer to buffer |
| 90 | // Return value: length of buffer if OK, < 0 otherwise. |
| 91 | int ExtractBuffer(const I420VideoFrame& input_frame, |
| 92 | int size, uint8_t* buffer); |
mikhal@webrtc.org | a2026ba | 2012-01-05 18:19:32 +0000 | [diff] [blame] | 93 | // Convert To I420 |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 94 | // Input: |
| 95 | // - src_video_type : Type of input video. |
| 96 | // - src_frame : Pointer to a source frame. |
| 97 | // - 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] | 98 | // - src_width : src width in pixels. |
| 99 | // - src_height : src height in pixels. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 100 | // - 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] | 101 | // - rotate : Rotation mode of output image. |
| 102 | // Output: |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 103 | // - dst_frame : Reference to a destination frame. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 104 | // Return value: 0 if OK, < 0 otherwise. |
| 105 | |
| 106 | int ConvertToI420(VideoType src_video_type, |
| 107 | const uint8_t* src_frame, |
| 108 | int crop_x, int crop_y, |
| 109 | int src_width, int src_height, |
| 110 | int sample_size, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 111 | VideoRotationMode rotation, |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 112 | I420VideoFrame* dst_frame); |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 113 | |
mikhal@webrtc.org | a2026ba | 2012-01-05 18:19:32 +0000 | [diff] [blame] | 114 | // Convert From I420 |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 115 | // Input: |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 116 | // - src_frame : Reference to a source frame. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 117 | // - dst_video_type : Type of output video. |
| 118 | // - dst_sample_size : Required only for the parsing of MJPG. |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 119 | // - dst_frame : Pointer to a destination frame. |
| 120 | // Return value: 0 if OK, < 0 otherwise. |
mikhal@webrtc.org | 1e033e1 | 2012-10-01 20:09:32 +0000 | [diff] [blame] | 121 | // It is assumed that source and destination have equal height. |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 122 | int ConvertFromI420(const I420VideoFrame& src_frame, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 123 | VideoType dst_video_type, int dst_sample_size, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 124 | uint8_t* dst_frame); |
| 125 | // ConvertFrom YV12. |
| 126 | // Interface - same as above. |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 127 | int ConvertFromYV12(const I420VideoFrame& src_frame, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 128 | VideoType dst_video_type, int dst_sample_size, |
mikhal@webrtc.org | e264249 | 2011-12-28 21:21:40 +0000 | [diff] [blame] | 129 | uint8_t* dst_frame); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 130 | |
mikhal@webrtc.org | a2026ba | 2012-01-05 18:19:32 +0000 | [diff] [blame] | 131 | // The following list describes designated conversion functions which |
| 132 | // are not covered by the previous general functions. |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 133 | // Input and output descriptions mostly match the above descriptions, and are |
| 134 | // therefore omitted. |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 135 | int ConvertRGB24ToARGB(const uint8_t* src_frame, |
| 136 | uint8_t* dst_frame, |
| 137 | int width, int height, |
| 138 | int dst_stride); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 139 | int ConvertNV12ToRGB565(const uint8_t* src_frame, |
| 140 | uint8_t* dst_frame, |
| 141 | int width, int height); |
| 142 | |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 143 | // Mirror functions |
| 144 | // The following 2 functions perform mirroring on a given image |
| 145 | // (LeftRight/UpDown). |
| 146 | // Input: |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 147 | // - src_frame : Pointer to a source frame. |
| 148 | // - dst_frame : Pointer to a destination frame. |
| 149 | // Return value: 0 if OK, < 0 otherwise. |
mikhal@webrtc.org | 2338131 | 2012-09-27 15:36:15 +0000 | [diff] [blame] | 150 | // It is assumed that src and dst frames have equal dimensions. |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 151 | int MirrorI420LeftRight(const I420VideoFrame* src_frame, |
| 152 | I420VideoFrame* dst_frame); |
| 153 | int MirrorI420UpDown(const I420VideoFrame* src_frame, |
| 154 | I420VideoFrame* dst_frame); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 155 | |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 156 | // Compute PSNR for an I420 frame (all planes). |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 157 | double I420PSNR(const I420VideoFrame* ref_frame, |
| 158 | const I420VideoFrame* test_frame); |
mikhal@webrtc.org | 3f9a721 | 2012-10-04 17:22:32 +0000 | [diff] [blame] | 159 | // Compute SSIM for an I420 frame (all planes). |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame^] | 160 | double I420SSIM(const I420VideoFrame* ref_frame, |
| 161 | const I420VideoFrame* test_frame); |
mikhal@webrtc.org | 3f9a721 | 2012-10-04 17:22:32 +0000 | [diff] [blame] | 162 | |
| 163 | // TODO(mikhal): Remove these functions and keep only the above functionality. |
| 164 | // Compute PSNR for an I420 buffer (all planes). |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 165 | double I420PSNR(const uint8_t* ref_frame, |
| 166 | const uint8_t* test_frame, |
| 167 | int width, int height); |
mikhal@webrtc.org | 3f9a721 | 2012-10-04 17:22:32 +0000 | [diff] [blame] | 168 | // Compute SSIM for an I420 buffer (all planes). |
mikhal@webrtc.org | 6f7fbc7 | 2011-12-20 17:38:28 +0000 | [diff] [blame] | 169 | double I420SSIM(const uint8_t* ref_frame, |
| 170 | const uint8_t* test_frame, |
| 171 | int width, int height); |
mikhal@webrtc.org | 2cdb2d3 | 2011-11-28 18:09:41 +0000 | [diff] [blame] | 172 | } |
| 173 | |
andrew@webrtc.org | c1354bd | 2012-07-27 18:21:16 +0000 | [diff] [blame] | 174 | #endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_ |