blob: 4b1b2a58f28635edd0d76e0e5f2dec4dd71641d7 [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
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000046// Conversion between the RawVideoType and the LibYuv videoType.
47// TODO(wu): Consolidate types into one type throughout WebRtc.
48VideoType RawVideoTypeToCommonVideoVideoType(RawVideoType type);
49
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000050// Supported rotation
51// Direction of rotation - clockwise.
52enum VideoRotationMode {
53 kRotateNone = 0,
54 kRotate90 = 90,
55 kRotate180 = 180,
56 kRotate270 = 270,
57};
58
mikhal@webrtc.orgc7ecc112012-09-28 16:07:10 +000059// Align integer values.
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000060// Input:
mikhal@webrtc.orgc7ecc112012-09-28 16:07:10 +000061// - value : Input value to be aligned.
62// - alignment : Alignment basis (power of 2).
63// Return value: An aligned form of the input value.
64int AlignInt(int value, int alignment);
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000065
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000066// Calculate the required buffer size.
67// Input:
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000068// - 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.org2cdb2d32011-11-28 18:09:41 +000073int CalcBufferSize(VideoType type, int width, int height);
74
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000075// 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.
82int 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.
91int ExtractBuffer(const I420VideoFrame& input_frame,
92 int size, uint8_t* buffer);
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +000093// Convert To I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000094// 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.org2f4ff892012-09-24 21:09:54 +000098// - src_width : src width in pixels.
99// - src_height : src height in pixels.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000100// - sample_size : Required only for the parsing of MJPG (set to 0 else).
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000101// - rotate : Rotation mode of output image.
102// Output:
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +0000103// - dst_frame : Reference to a destination frame.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000104// Return value: 0 if OK, < 0 otherwise.
105
106int 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.orge2642492011-12-28 21:21:40 +0000111 VideoRotationMode rotation,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000112 I420VideoFrame* dst_frame);
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000113
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +0000114// Convert From I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000115// Input:
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000116// - src_frame : Reference to a source frame.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000117// - dst_video_type : Type of output video.
118// - dst_sample_size : Required only for the parsing of MJPG.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000119// - dst_frame : Pointer to a destination frame.
120// Return value: 0 if OK, < 0 otherwise.
mikhal@webrtc.org1e033e12012-10-01 20:09:32 +0000121// It is assumed that source and destination have equal height.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000122int ConvertFromI420(const I420VideoFrame& src_frame,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000123 VideoType dst_video_type, int dst_sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000124 uint8_t* dst_frame);
125// ConvertFrom YV12.
126// Interface - same as above.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000127int ConvertFromYV12(const I420VideoFrame& src_frame,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000128 VideoType dst_video_type, int dst_sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000129 uint8_t* dst_frame);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000130
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +0000131// The following list describes designated conversion functions which
132// are not covered by the previous general functions.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000133// Input and output descriptions mostly match the above descriptions, and are
134// therefore omitted.
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000135int ConvertRGB24ToARGB(const uint8_t* src_frame,
136 uint8_t* dst_frame,
137 int width, int height,
138 int dst_stride);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000139int ConvertNV12ToRGB565(const uint8_t* src_frame,
140 uint8_t* dst_frame,
141 int width, int height);
142
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000143// Mirror functions
144// The following 2 functions perform mirroring on a given image
145// (LeftRight/UpDown).
146// Input:
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000147// - 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.org23381312012-09-27 15:36:15 +0000150// It is assumed that src and dst frames have equal dimensions.
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000151int MirrorI420LeftRight(const I420VideoFrame* src_frame,
152 I420VideoFrame* dst_frame);
153int MirrorI420UpDown(const I420VideoFrame* src_frame,
154 I420VideoFrame* dst_frame);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000155
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000156// Compute PSNR for an I420 frame (all planes).
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000157double I420PSNR(const I420VideoFrame* ref_frame,
158 const I420VideoFrame* test_frame);
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000159// Compute SSIM for an I420 frame (all planes).
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000160double I420SSIM(const I420VideoFrame* ref_frame,
161 const I420VideoFrame* test_frame);
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000162
163// TODO(mikhal): Remove these functions and keep only the above functionality.
164// Compute PSNR for an I420 buffer (all planes).
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000165double I420PSNR(const uint8_t* ref_frame,
166 const uint8_t* test_frame,
167 int width, int height);
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000168// Compute SSIM for an I420 buffer (all planes).
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000169double I420SSIM(const uint8_t* ref_frame,
170 const uint8_t* test_frame,
171 int width, int height);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000172}
173
andrew@webrtc.orgc1354bd2012-07-27 18:21:16 +0000174#endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_