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 WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_
16 #define WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_
17 
18 #include <stdio.h>
19 
20 #include "webrtc/common_types.h"  // RawVideoTypes.
21 #include "webrtc/common_video/rotation.h"
22 #include "webrtc/typedefs.h"
23 #include "webrtc/video_frame.h"
24 
25 namespace webrtc {
26 
27 // Supported video types.
28 enum VideoType {
29   kUnknown,
30   kI420,
31   kIYUV,
32   kRGB24,
33   kABGR,
34   kARGB,
35   kARGB4444,
36   kRGB565,
37   kARGB1555,
38   kYUY2,
39   kYV12,
40   kUYVY,
41   kMJPG,
42   kNV21,
43   kNV12,
44   kBGRA,
45 };
46 
47 // This is the max PSNR value our algorithms can return.
48 const double kPerfectPSNR = 48.0f;
49 
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 
54 // Align integer values.
55 // Input:
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);
60 
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 
70 // Calculate the required buffer size.
71 // Input:
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
76 //                   video frame.
77 size_t CalcBufferSize(VideoType type, int width, int height);
78 
79 // TODO(mikhal): Add unit test for these two functions and determine location.
80 // Print VideoFrame to file
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.
86 int PrintVideoFrame(const VideoFrame& frame, FILE* file);
87 
88 // Extract buffer from VideoFrame (consecutive planes, no stride)
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.
95 int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer);
96 // Convert To I420
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).
101 //   - src_width        : src width in pixels.
102 //   - src_height       : src height in pixels.
103 //   - sample_size      : Required only for the parsing of MJPG (set to 0 else).
104 //   - rotate           : Rotation mode of output image.
105 // Output:
106 //   - dst_frame        : Reference to a destination frame.
107 // Return value: 0 if OK, < 0 otherwise.
108 
109 int ConvertToI420(VideoType src_video_type,
110                   const uint8_t* src_frame,
111                   int crop_x,
112                   int crop_y,
113                   int src_width,
114                   int src_height,
115                   size_t sample_size,
116                   VideoRotation rotation,
117                   VideoFrame* dst_frame);
118 
119 // Convert From I420
120 // Input:
121 //   - src_frame        : Reference to a source frame.
122 //   - dst_video_type   : Type of output video.
123 //   - dst_sample_size  : Required only for the parsing of MJPG.
124 //   - dst_frame        : Pointer to a destination frame.
125 // Return value: 0 if OK, < 0 otherwise.
126 // It is assumed that source and destination have equal height.
127 int ConvertFromI420(const VideoFrame& src_frame,
128                     VideoType dst_video_type,
129                     int dst_sample_size,
130                     uint8_t* dst_frame);
131 // ConvertFrom YV12.
132 // Interface - same as above.
133 int ConvertFromYV12(const VideoFrame& src_frame,
134                     VideoType dst_video_type,
135                     int dst_sample_size,
136                     uint8_t* dst_frame);
137 
138 // The following list describes designated conversion functions which
139 // are not covered by the previous general functions.
140 // Input and output descriptions mostly match the above descriptions, and are
141 // therefore omitted.
142 int ConvertRGB24ToARGB(const uint8_t* src_frame,
143                        uint8_t* dst_frame,
144                        int width, int height,
145                        int dst_stride);
146 int ConvertNV12ToRGB565(const uint8_t* src_frame,
147                         uint8_t* dst_frame,
148                         int width, int height);
149 
150 // Compute PSNR for an I420 frame (all planes).
151 // Returns the PSNR in decibel, to a maximum of kInfinitePSNR.
152 double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame);
153 // Compute SSIM for an I420 frame (all planes).
154 double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame);
155 
156 }  // namespace webrtc
157 
158 #endif  // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_
159