blob: d66736fb248ea958de646c559ffd55c75d4f3fda [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
pbos@webrtc.orgc69ae692013-06-04 09:02:37 +000020#include "webrtc/common_types.h" // RawVideoTypes.
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000021#include "webrtc/common_video/rotation.h"
pbos@webrtc.orgc69ae692013-06-04 09:02:37 +000022#include "webrtc/typedefs.h"
Thiago Farina9bfe3da2015-04-10 12:52:13 +020023#include "webrtc/video_frame.h"
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000024
25namespace webrtc {
26
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000027// Supported video types.
28enum VideoType {
29 kUnknown,
30 kI420,
31 kIYUV,
32 kRGB24,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000033 kABGR,
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000034 kARGB,
35 kARGB4444,
36 kRGB565,
37 kARGB1555,
38 kYUY2,
39 kYV12,
40 kUYVY,
41 kMJPG,
42 kNV21,
43 kNV12,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000044 kBGRA,
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000045};
46
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +000047// This is the max PSNR value our algorithms can return.
phoglund@webrtc.org5b689ef2012-12-13 10:15:06 +000048const double kPerfectPSNR = 48.0f;
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +000049
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000050// Conversion between the RawVideoType and the LibYuv videoType.
51// TODO(wu): Consolidate types into one type throughout WebRtc.
52VideoType RawVideoTypeToCommonVideoVideoType(RawVideoType type);
53
mikhal@webrtc.orgc7ecc112012-09-28 16:07:10 +000054// Align integer values.
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000055// Input:
mikhal@webrtc.orgc7ecc112012-09-28 16:07:10 +000056// - value : Input value to be aligned.
57// - alignment : Alignment basis (power of 2).
58// Return value: An aligned form of the input value.
59int AlignInt(int value, int alignment);
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000060
mikhal@webrtc.org91a03402012-10-30 19:19:32 +000061// 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.
68void Calc16ByteAlignedStride(int width, int* stride_y, int* stride_uv);
69
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000070// Calculate the required buffer size.
71// Input:
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000072// - 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.org4591fbd2014-11-20 22:28:14 +000076// video frame.
77size_t CalcBufferSize(VideoType type, int width, int height);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000078
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000079// TODO(mikhal): Add unit test for these two functions and determine location.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070080// Print VideoFrame to file
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000081// 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-Sanchez47650702015-05-29 17:21:40 -070086int PrintVideoFrame(const VideoFrame& frame, FILE* file);
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000087
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070088// Extract buffer from VideoFrame (consecutive planes, no stride)
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000089// 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-Sanchez47650702015-05-29 17:21:40 -070095int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer);
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +000096// Convert To I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000097// 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.org2f4ff892012-09-24 21:09:54 +0000101// - src_width : src width in pixels.
102// - src_height : src height in pixels.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000103// - sample_size : Required only for the parsing of MJPG (set to 0 else).
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000104// - rotate : Rotation mode of output image.
105// Output:
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +0000106// - dst_frame : Reference to a destination frame.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000107// Return value: 0 if OK, < 0 otherwise.
108
109int ConvertToI420(VideoType src_video_type,
110 const uint8_t* src_frame,
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000111 int crop_x,
112 int crop_y,
113 int src_width,
114 int src_height,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000115 size_t sample_size,
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000116 VideoRotation rotation,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700117 VideoFrame* dst_frame);
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000118
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +0000119// Convert From I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000120// Input:
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000121// - src_frame : Reference to a source frame.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000122// - dst_video_type : Type of output video.
123// - dst_sample_size : Required only for the parsing of MJPG.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000124// - dst_frame : Pointer to a destination frame.
125// Return value: 0 if OK, < 0 otherwise.
mikhal@webrtc.org1e033e12012-10-01 20:09:32 +0000126// It is assumed that source and destination have equal height.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700127int ConvertFromI420(const VideoFrame& src_frame,
128 VideoType dst_video_type,
129 int dst_sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000130 uint8_t* dst_frame);
131// ConvertFrom YV12.
132// Interface - same as above.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700133int ConvertFromYV12(const VideoFrame& src_frame,
134 VideoType dst_video_type,
135 int dst_sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000136 uint8_t* dst_frame);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000137
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +0000138// The following list describes designated conversion functions which
139// are not covered by the previous general functions.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000140// Input and output descriptions mostly match the above descriptions, and are
141// therefore omitted.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000142int ConvertRGB24ToARGB(const uint8_t* src_frame,
143 uint8_t* dst_frame,
144 int width, int height,
145 int dst_stride);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000146int ConvertNV12ToRGB565(const uint8_t* src_frame,
147 uint8_t* dst_frame,
148 int width, int height);
149
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000150// Compute PSNR for an I420 frame (all planes).
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +0000151// Returns the PSNR in decibel, to a maximum of kInfinitePSNR.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700152double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame);
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000153// Compute SSIM for an I420 frame (all planes).
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700154double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame);
jbauch0f2e9392015-12-10 03:11:42 -0800155
156} // namespace webrtc
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000157
andrew@webrtc.orgc1354bd2012-07-27 18:21:16 +0000158#endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_