blob: 1bea4f1852714bab74bb30849f7171d56cd3d96c [file] [log] [blame]
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +00001/*
leozwang@webrtc.org3ebff4c2012-05-04 17:07:30 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +00003 *
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.orgc1354bd2012-07-27 18:21:16 +000012 * WebRTC's wrapper to libyuv.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000013 */
14
andrew@webrtc.orgc1354bd2012-07-27 18:21:16 +000015#ifndef WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_
16#define WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000017
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000018#include <stdio.h>
19
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000020#include "common_types.h" // RawVideoTypes.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000021#include "common_video/interface/i420_video_frame.h"
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000022#include "typedefs.h"
23
24namespace webrtc {
25
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000026// Supported video types.
27enum VideoType {
28 kUnknown,
29 kI420,
30 kIYUV,
31 kRGB24,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000032 kABGR,
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000033 kARGB,
34 kARGB4444,
35 kRGB565,
36 kARGB1555,
37 kYUY2,
38 kYV12,
39 kUYVY,
40 kMJPG,
41 kNV21,
42 kNV12,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000043 kBGRA,
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000044};
45
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +000046// This is the max PSNR value our algorithms can return.
phoglund@webrtc.org5b689ef2012-12-13 10:15:06 +000047const double kPerfectPSNR = 48.0f;
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +000048
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000049// Conversion between the RawVideoType and the LibYuv videoType.
50// TODO(wu): Consolidate types into one type throughout WebRtc.
51VideoType RawVideoTypeToCommonVideoVideoType(RawVideoType type);
52
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000053// Supported rotation
54// Direction of rotation - clockwise.
55enum VideoRotationMode {
56 kRotateNone = 0,
57 kRotate90 = 90,
58 kRotate180 = 180,
59 kRotate270 = 270,
60};
61
mikhal@webrtc.orgc7ecc112012-09-28 16:07:10 +000062// Align integer values.
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000063// Input:
mikhal@webrtc.orgc7ecc112012-09-28 16:07:10 +000064// - value : Input value to be aligned.
65// - alignment : Alignment basis (power of 2).
66// Return value: An aligned form of the input value.
67int AlignInt(int value, int alignment);
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000068
mikhal@webrtc.org91a03402012-10-30 19:19:32 +000069// Align stride values for I420 Video frames.
70// Input:
71// - width : Image width.
72// - stride_y : Pointer to the stride of the y plane.
73// - stride_uv: Pointer to the stride of the u and v planes (setting identical
74// values for both).
75// Setting 16 byte alignment.
76void Calc16ByteAlignedStride(int width, int* stride_y, int* stride_uv);
77
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000078// Calculate the required buffer size.
79// Input:
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000080// - type :The type of the designated video frame.
81// - width :frame width in pixels.
82// - height :frame height in pixels.
83// Return value: :The required size in bytes to accommodate the specified
84// video frame or -1 in case of an error .
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000085int CalcBufferSize(VideoType type, int width, int height);
86
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000087// TODO(mikhal): Add unit test for these two functions and determine location.
88// Print I420VideoFrame to file
89// Input:
90// - frame : Reference to video frame.
91// - file : pointer to file object. It is assumed that the file is
92// already open for writing.
93// Return value: 0 if OK, < 0 otherwise.
94int PrintI420VideoFrame(const I420VideoFrame& frame, FILE* file);
95
96// Extract buffer from I420VideoFrame (consecutive planes, no stride)
97// Input:
98// - frame : Reference to video frame.
99// - size : pointer to the size of the allocated buffer. If size is
100// insufficient, an error will be returned.
101// - buffer : Pointer to buffer
102// Return value: length of buffer if OK, < 0 otherwise.
103int ExtractBuffer(const I420VideoFrame& input_frame,
104 int size, uint8_t* buffer);
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +0000105// Convert To I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000106// Input:
107// - src_video_type : Type of input video.
108// - src_frame : Pointer to a source frame.
109// - crop_x/crop_y : Starting positions for cropping (0 for no crop).
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +0000110// - src_width : src width in pixels.
111// - src_height : src height in pixels.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000112// - sample_size : Required only for the parsing of MJPG (set to 0 else).
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000113// - rotate : Rotation mode of output image.
114// Output:
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +0000115// - dst_frame : Reference to a destination frame.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000116// Return value: 0 if OK, < 0 otherwise.
117
118int ConvertToI420(VideoType src_video_type,
119 const uint8_t* src_frame,
120 int crop_x, int crop_y,
121 int src_width, int src_height,
122 int sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000123 VideoRotationMode rotation,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000124 I420VideoFrame* dst_frame);
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000125
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +0000126// Convert From I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000127// Input:
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000128// - src_frame : Reference to a source frame.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000129// - dst_video_type : Type of output video.
130// - dst_sample_size : Required only for the parsing of MJPG.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000131// - dst_frame : Pointer to a destination frame.
132// Return value: 0 if OK, < 0 otherwise.
mikhal@webrtc.org1e033e12012-10-01 20:09:32 +0000133// It is assumed that source and destination have equal height.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000134int ConvertFromI420(const I420VideoFrame& src_frame,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000135 VideoType dst_video_type, int dst_sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000136 uint8_t* dst_frame);
137// ConvertFrom YV12.
138// Interface - same as above.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000139int ConvertFromYV12(const I420VideoFrame& src_frame,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000140 VideoType dst_video_type, int dst_sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000141 uint8_t* dst_frame);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000142
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +0000143// The following list describes designated conversion functions which
144// are not covered by the previous general functions.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000145// Input and output descriptions mostly match the above descriptions, and are
146// therefore omitted.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000147int ConvertRGB24ToARGB(const uint8_t* src_frame,
148 uint8_t* dst_frame,
149 int width, int height,
150 int dst_stride);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000151int ConvertNV12ToRGB565(const uint8_t* src_frame,
152 uint8_t* dst_frame,
153 int width, int height);
154
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000155// Mirror functions
156// The following 2 functions perform mirroring on a given image
157// (LeftRight/UpDown).
158// Input:
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000159// - src_frame : Pointer to a source frame.
160// - dst_frame : Pointer to a destination frame.
161// Return value: 0 if OK, < 0 otherwise.
mikhal@webrtc.org23381312012-09-27 15:36:15 +0000162// It is assumed that src and dst frames have equal dimensions.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000163int MirrorI420LeftRight(const I420VideoFrame* src_frame,
164 I420VideoFrame* dst_frame);
165int MirrorI420UpDown(const I420VideoFrame* src_frame,
166 I420VideoFrame* dst_frame);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000167
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000168// Compute PSNR for an I420 frame (all planes).
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +0000169// Returns the PSNR in decibel, to a maximum of kInfinitePSNR.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000170double I420PSNR(const I420VideoFrame* ref_frame,
171 const I420VideoFrame* test_frame);
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000172// Compute SSIM for an I420 frame (all planes).
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000173double I420SSIM(const I420VideoFrame* ref_frame,
174 const I420VideoFrame* test_frame);
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000175
176// TODO(mikhal): Remove these functions and keep only the above functionality.
177// Compute PSNR for an I420 buffer (all planes).
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +0000178// Returns the PSNR in decibel, to a maximum of kInfinitePSNR.
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000179double I420PSNR(const uint8_t* ref_frame,
180 const uint8_t* test_frame,
181 int width, int height);
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000182// Compute SSIM for an I420 buffer (all planes).
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000183double I420SSIM(const uint8_t* ref_frame,
184 const uint8_t* test_frame,
185 int width, int height);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000186}
187
andrew@webrtc.orgc1354bd2012-07-27 18:21:16 +0000188#endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_