1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or bpied warranties, including, but not limited to, the bpied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #if !defined CUDA_DISABLER
45 #include "opencv2/gpu/device/common.hpp"
46 #include "opencv2/gpu/device/vec_traits.hpp"
48 namespace cv { namespace gpu { namespace device
50 namespace video_encoding
52 __device__ __forceinline__ void rgbtoy(const uchar b, const uchar g, const uchar r, uchar& y)
54 y = static_cast<uchar>(((int)(30 * r) + (int)(59 * g) + (int)(11 * b)) / 100);
57 __device__ __forceinline__ void rgbtoyuv(const uchar b, const uchar g, const uchar r, uchar& y, uchar& u, uchar& v)
60 u = static_cast<uchar>(((int)(-17 * r) - (int)(33 * g) + (int)(50 * b) + 12800) / 100);
61 v = static_cast<uchar>(((int)(50 * r) - (int)(42 * g) - (int)(8 * b) + 12800) / 100);
64 __global__ void Gray_to_YV12(const PtrStepSzb src, PtrStepb dst)
66 const int x = (blockIdx.x * blockDim.x + threadIdx.x) * 2;
67 const int y = (blockIdx.y * blockDim.y + threadIdx.y) * 2;
69 if (x + 1 >= src.cols || y + 1 >= src.rows)
72 // get pointers to the data
73 const size_t planeSize = src.rows * dst.step;
74 PtrStepb y_plane(dst.data, dst.step);
75 PtrStepb u_plane(y_plane.data + planeSize, dst.step / 2);
76 PtrStepb v_plane(u_plane.data + (planeSize / 4), dst.step / 2);
79 uchar y_val, u_val, v_val;
82 rgbtoy(pix, pix, pix, y_val);
83 y_plane(y, x) = y_val;
86 rgbtoy(pix, pix, pix, y_val);
87 y_plane(y, x + 1) = y_val;
90 rgbtoy(pix, pix, pix, y_val);
91 y_plane(y + 1, x) = y_val;
93 pix = src(y + 1, x + 1);
94 rgbtoyuv(pix, pix, pix, y_val, u_val, v_val);
95 y_plane(y + 1, x + 1) = y_val;
96 u_plane(y / 2, x / 2) = u_val;
97 v_plane(y / 2, x / 2) = v_val;
100 template <typename T>
101 __global__ void BGR_to_YV12(const PtrStepSz<T> src, PtrStepb dst)
103 const int x = (blockIdx.x * blockDim.x + threadIdx.x) * 2;
104 const int y = (blockIdx.y * blockDim.y + threadIdx.y) * 2;
106 if (x + 1 >= src.cols || y + 1 >= src.rows)
109 // get pointers to the data
110 const size_t planeSize = src.rows * dst.step;
111 PtrStepb y_plane(dst.data, dst.step);
112 PtrStepb u_plane(y_plane.data + planeSize, dst.step / 2);
113 PtrStepb v_plane(u_plane.data + (planeSize / 4), dst.step / 2);
116 uchar y_val, u_val, v_val;
119 rgbtoy(pix.z, pix.y, pix.x, y_val);
120 y_plane(y, x) = y_val;
123 rgbtoy(pix.z, pix.y, pix.x, y_val);
124 y_plane(y, x + 1) = y_val;
127 rgbtoy(pix.z, pix.y, pix.x, y_val);
128 y_plane(y + 1, x) = y_val;
130 pix = src(y + 1, x + 1);
131 rgbtoyuv(pix.z, pix.y, pix.x, y_val, u_val, v_val);
132 y_plane(y + 1, x + 1) = y_val;
133 u_plane(y / 2, x / 2) = u_val;
134 v_plane(y / 2, x / 2) = v_val;
137 void Gray_to_YV12_caller(const PtrStepSzb src, PtrStepb dst)
140 dim3 grid(divUp(src.cols, block.x * 2), divUp(src.rows, block.y * 2));
142 Gray_to_YV12<<<grid, block>>>(src, dst);
143 cudaSafeCall( cudaGetLastError() );
145 cudaSafeCall( cudaDeviceSynchronize() );
148 void BGR_to_YV12_caller(const PtrStepSzb src, PtrStepb dst)
150 typedef typename TypeVec<uchar, cn>::vec_type src_t;
153 dim3 grid(divUp(src.cols, block.x * 2), divUp(src.rows, block.y * 2));
155 BGR_to_YV12<<<grid, block>>>(static_cast< PtrStepSz<src_t> >(src), dst);
156 cudaSafeCall( cudaGetLastError() );
158 cudaSafeCall( cudaDeviceSynchronize() );
161 void YV12_gpu(const PtrStepSzb src, int cn, PtrStepSzb dst)
163 typedef void (*func_t)(const PtrStepSzb src, PtrStepb dst);
165 static const func_t funcs[] =
167 0, Gray_to_YV12_caller, 0, BGR_to_YV12_caller<3>, BGR_to_YV12_caller<4>
175 #endif /* CUDA_DISABLER */