blob: 13e58d2ae4866aa73a4874e900096409b7bba9a3 [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>
magjed1a7ef1f2016-09-17 02:39:03 -070019#include <vector>
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000020
nisseaf916892017-01-10 07:44:26 -080021#include "webrtc/api/video/video_frame.h"
nisseeb44b392017-04-28 07:18:05 -070022#include "webrtc/common_types.h" // VideoTypes.
pbos@webrtc.orgc69ae692013-06-04 09:02:37 +000023#include "webrtc/typedefs.h"
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000024
25namespace webrtc {
26
nisseaf916892017-01-10 07:44:26 -080027class I420Buffer;
28
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +000029// This is the max PSNR value our algorithms can return.
phoglund@webrtc.org5b689ef2012-12-13 10:15:06 +000030const double kPerfectPSNR = 48.0f;
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +000031
nisseeb44b392017-04-28 07:18:05 -070032// TODO(nisse): Some downstream apps call CalcBufferSize with
33// ::webrtc::kI420 as the first argument. Delete after they are updated.
34const VideoType kI420 = VideoType::kI420;
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000035
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000036// Calculate the required buffer size.
37// Input:
mikhal@webrtc.org1a265882012-09-21 15:37:06 +000038// - type :The type of the designated video frame.
39// - width :frame width in pixels.
40// - height :frame height in pixels.
41// Return value: :The required size in bytes to accommodate the specified
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000042// video frame.
43size_t CalcBufferSize(VideoType type, int width, int height);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +000044
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000045// TODO(mikhal): Add unit test for these two functions and determine location.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070046// Print VideoFrame to file
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000047// Input:
48// - frame : Reference to video frame.
49// - file : pointer to file object. It is assumed that the file is
50// already open for writing.
51// Return value: 0 if OK, < 0 otherwise.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070052int PrintVideoFrame(const VideoFrame& frame, FILE* file);
magjed3f075492017-06-01 10:02:26 -070053int PrintVideoFrame(const I420BufferInterface& frame, FILE* file);
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000054
magjed3f075492017-06-01 10:02:26 -070055// Extract buffer from VideoFrame or I420BufferInterface (consecutive
Niels Möller718a7632016-06-13 13:06:01 +020056// planes, no stride)
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000057// Input:
58// - frame : Reference to video frame.
59// - size : pointer to the size of the allocated buffer. If size is
60// insufficient, an error will be returned.
61// - buffer : Pointer to buffer
62// Return value: length of buffer if OK, < 0 otherwise.
magjed3f075492017-06-01 10:02:26 -070063int ExtractBuffer(const rtc::scoped_refptr<I420BufferInterface>& input_frame,
Niels Möller718a7632016-06-13 13:06:01 +020064 size_t size,
65 uint8_t* buffer);
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070066int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer);
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +000067// Convert To I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000068// Input:
69// - src_video_type : Type of input video.
70// - src_frame : Pointer to a source frame.
71// - crop_x/crop_y : Starting positions for cropping (0 for no crop).
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +000072// - src_width : src width in pixels.
73// - src_height : src height in pixels.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000074// - sample_size : Required only for the parsing of MJPG (set to 0 else).
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000075// - rotate : Rotation mode of output image.
76// Output:
nisse64ec8f82016-09-27 00:17:25 -070077// - dst_buffer : Reference to a destination frame buffer.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000078// Return value: 0 if OK, < 0 otherwise.
79
nisse64ec8f82016-09-27 00:17:25 -070080// TODO(nisse): Delete this wrapper, and let users call libyuv directly. Most
nisseaf916892017-01-10 07:44:26 -080081// calls pass |src_video_type| == kI420, and should use libyuv::I420Copy. Also
82// remember to delete the I420Buffer forward declaration above. The only
83// exception at the time of this writing is VideoCaptureImpl::IncomingFrame,
84// which still needs libyuv::ConvertToI420.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000085int ConvertToI420(VideoType src_video_type,
86 const uint8_t* src_frame,
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000087 int crop_x,
88 int crop_y,
89 int src_width,
90 int src_height,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000091 size_t sample_size,
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000092 VideoRotation rotation,
nisse64ec8f82016-09-27 00:17:25 -070093 I420Buffer* dst_buffer);
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000094
mikhal@webrtc.orga2026ba2012-01-05 18:19:32 +000095// Convert From I420
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000096// Input:
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000097// - src_frame : Reference to a source frame.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +000098// - dst_video_type : Type of output video.
99// - dst_sample_size : Required only for the parsing of MJPG.
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000100// - dst_frame : Pointer to a destination frame.
101// Return value: 0 if OK, < 0 otherwise.
mikhal@webrtc.org1e033e12012-10-01 20:09:32 +0000102// It is assumed that source and destination have equal height.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700103int ConvertFromI420(const VideoFrame& src_frame,
104 VideoType dst_video_type,
105 int dst_sample_size,
mikhal@webrtc.orge2642492011-12-28 21:21:40 +0000106 uint8_t* dst_frame);
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000107
mikhal@webrtc.org6f7fbc72011-12-20 17:38:28 +0000108// Compute PSNR for an I420 frame (all planes).
phoglund@webrtc.org273ccad2012-11-29 10:08:16 +0000109// Returns the PSNR in decibel, to a maximum of kInfinitePSNR.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700110double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame);
magjed3f075492017-06-01 10:02:26 -0700111double I420PSNR(const I420BufferInterface& ref_buffer,
112 const I420BufferInterface& test_buffer);
nisse1996e3f2016-09-19 00:34:46 -0700113
mikhal@webrtc.org3f9a7212012-10-04 17:22:32 +0000114// Compute SSIM for an I420 frame (all planes).
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700115double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame);
magjed3f075492017-06-01 10:02:26 -0700116double I420SSIM(const I420BufferInterface& ref_buffer,
117 const I420BufferInterface& test_buffer);
jbauch0f2e9392015-12-10 03:11:42 -0800118
magjed5a872452016-10-20 03:34:29 -0700119// Helper function for scaling NV12 to NV12.
Anders Carlssonbfe45c22017-06-19 16:46:31 +0200120// If the |src_width| and |src_height| matches the |dst_width| and |dst_height|,
121// then |tmp_buffer| is not used. In other cases, the minimum size of
122// |tmp_buffer| should be:
123// (src_width/2) * (src_height/2) * 2 + (dst_width/2) * (dst_height/2) * 2
124void NV12Scale(uint8_t* tmp_buffer,
magjed5a872452016-10-20 03:34:29 -0700125 const uint8_t* src_y, int src_stride_y,
126 const uint8_t* src_uv, int src_stride_uv,
127 int src_width, int src_height,
128 uint8_t* dst_y, int dst_stride_y,
129 uint8_t* dst_uv, int dst_stride_uv,
130 int dst_width, int dst_height);
131
magjed1a7ef1f2016-09-17 02:39:03 -0700132// Helper class for directly converting and scaling NV12 to I420. The Y-plane
133// will be scaled directly to the I420 destination, which makes this faster
134// than separate NV12->I420 + I420->I420 scaling.
135class NV12ToI420Scaler {
136 public:
magjed3149e092017-05-08 05:32:05 -0700137 NV12ToI420Scaler();
138 ~NV12ToI420Scaler();
magjed1a7ef1f2016-09-17 02:39:03 -0700139 void NV12ToI420Scale(const uint8_t* src_y, int src_stride_y,
140 const uint8_t* src_uv, int src_stride_uv,
141 int src_width, int src_height,
142 uint8_t* dst_y, int dst_stride_y,
143 uint8_t* dst_u, int dst_stride_u,
144 uint8_t* dst_v, int dst_stride_v,
145 int dst_width, int dst_height);
146 private:
147 std::vector<uint8_t> tmp_uv_planes_;
148};
149
jbauch0f2e9392015-12-10 03:11:42 -0800150} // namespace webrtc
mikhal@webrtc.org2cdb2d32011-11-28 18:09:41 +0000151
andrew@webrtc.orgc1354bd2012-07-27 18:21:16 +0000152#endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_