UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
cuda_pix_conv.cu
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <cuda_runtime.h>
5#include <time.h>
6
7 __global__
8void kern_RGBtoRGBA(unsigned char *dst,
9 size_t dstPitch,
10 unsigned char *src,
11 size_t srcPitch,
12 size_t width,
13 size_t height)
14{
15 const int x = (blockIdx.x * blockDim.x) + threadIdx.x;
16 const int y = (blockIdx.y * blockDim.y) + threadIdx.y;
17
18 if(x >= width)
19 return;
20
21 if(y >= height)
22 return;
23
24 uchar3 *src_px = (uchar3 *) (src + y * srcPitch) + x;
25 uchar4 *dst_px = (uchar4 *) (dst + y * dstPitch) + x;
26
27 const uchar3 px = *src_px;
28
29 *dst_px = make_uchar4(px.x, px.y, px.z, 0);
30}
31
32 __global__
33void kern_RGBAtoRGB(unsigned char *dst,
34 size_t dstPitch,
35 unsigned char *src,
36 size_t srcPitch,
37 size_t width,
38 size_t height)
39{
40 const int x = (blockIdx.x * blockDim.x) + threadIdx.x;
41 const int y = (blockIdx.y * blockDim.y) + threadIdx.y;
42
43 if(x >= width)
44 return;
45
46 if(y >= height)
47 return;
48
49 uchar4 *src_px = (uchar4 *) (src + y * srcPitch) + x;
50 uchar3 *dst_px = (uchar3 *) (dst + y * dstPitch) + x;
51
52 const uchar4 px = *src_px;
53
54 *dst_px = make_uchar3(px.x, px.y, px.z);
55}
56
57#define max(a, b) (((a) > (b))? (a): (b))
58#define min(a, b) (((a) < (b))? (a): (b))
59
60__global__
61void kern_UYVYtoRGBA(unsigned char *dst,
62 size_t dstPitch,
63 unsigned char *src,
64 size_t srcPitch,
65 size_t width,
66 size_t height)
67{
68 const int x = (blockIdx.x * blockDim.x) + threadIdx.x;
69 const int y = (blockIdx.y * blockDim.y) + threadIdx.y;
70
71 if(x >= width)
72 return;
73
74 if(y >= height)
75 return;
76
77 const int uyvy_block = x / 2;
78
79 uchar4 *src_px = (uchar4 *) (src + y * srcPitch) + uyvy_block;
80 uchar4 *dst_px = (uchar4 *) (dst + y * dstPitch) + x;
81
82 const uchar4 block = *src_px;
83
84 int u = block.x;
85 int v = block.z;
86 int luma = (x % 2) ? block.w : block.y;
87
88 int r = min(max(1.164f*(luma - 16) + 1.793f*(v - 128), 0), 255);
89 int g = min(max(1.164f*(luma - 16) - 0.534f*(v - 128) - 0.213f*(u - 128), 0), 255);
90 int b = min(max(1.164f*(luma - 16) + 2.115f*(u - 128), 0), 255);
91
92 *dst_px = make_uchar4(r, g, b, 0);
93}
94
95__global__
96void kern_RGBAtoUYVY(unsigned char *dst,
97 size_t dstPitch,
98 unsigned char *src,
99 size_t srcPitch,
100 size_t width,
101 size_t height)
102{
103 const int x = (blockIdx.x * blockDim.x) + threadIdx.x;
104 const int y = (blockIdx.y * blockDim.y) + threadIdx.y;
105
106 const int uyvy_block = x;
107 const int src_x = x * 2;
108
109 if(src_x + 1 >= width)
110 return;
111
112 if(y >= height)
113 return;
114
115 uchar4 *src_px_1 = (uchar4 *) (src + y * srcPitch) + src_x;
116 uchar4 *src_px_2 = (uchar4 *) (src + y * srcPitch) + src_x + 1;
117 uchar4 *dst_px = (uchar4 *) (dst + y * dstPitch) + uyvy_block;
118
119 int y1 = 11993 * src_px_1->x + 40239 * src_px_1->y + 4063 * src_px_1->z + (1<<20);
120 int u = -6619 * src_px_1->x -22151 * src_px_1->y + 28770 * src_px_1->z;
121 int v = 28770 * src_px_1->x - 26149 * src_px_1->y - 2621 * src_px_1->z;
122
123 int y2 = 11993 * src_px_2->x + 40239 * src_px_2->y + 4063 * src_px_2->z + (1<<20);
124 u += -6619 * src_px_2->x -22151 * src_px_2->y + 28770 * src_px_2->z;
125 v += 28770 * src_px_2->x - 26149 * src_px_2->y - 2621 * src_px_2->z;
126
127 u = u / 2 + (1<<23);
128 v = v / 2 + (1<<23);
129
130 *dst_px = make_uchar4(min(max(u, 0), (1<<24)-1) >> 16,
131 min(max(y1, 0), (1<<24)-1) >> 16,
132 min(max(v, 0), (1<<24)-1) >> 16,
133 min(max(y2, 0), (1<<24)-1) >> 16);
134}
135
136
137void cuda_RGB_to_RGBA(unsigned char *dst,
138 size_t dstPitch,
139 unsigned char *src,
140 size_t srcPitch,
141 size_t width,
142 size_t height,
143 CUstream_st *stream){
144
145 dim3 blockSize(32,32);
146 dim3 numBlocks((width + blockSize.x - 1) / blockSize.x,
147 (height + blockSize.y - 1) / blockSize.y);
148
149 kern_RGBtoRGBA<<<numBlocks, blockSize, 0, stream>>>(dst, dstPitch, src, srcPitch, width, height);
150}
151
152void cuda_RGBA_to_RGB(unsigned char *dst,
153 size_t dstPitch,
154 unsigned char *src,
155 size_t srcPitch,
156 size_t width,
157 size_t height,
158 CUstream_st *stream){
159
160 dim3 blockSize(32,32);
161 dim3 numBlocks((width + blockSize.x - 1) / blockSize.x,
162 (height + blockSize.y - 1) / blockSize.y);
163
164 kern_RGBAtoRGB<<<numBlocks, blockSize, 0, stream>>>(dst, dstPitch, src, srcPitch, width, height);
165}
166
167void cuda_RGBA_to_UYVY(unsigned char *dst,
168 size_t dstPitch,
169 unsigned char *src,
170 size_t srcPitch,
171 size_t width,
172 size_t height,
173 CUstream_st *stream){
174
175 dim3 blockSize(32,32);
176 dim3 numBlocks((width / 2 + blockSize.x - 1) / blockSize.x,
177 (height + blockSize.y - 1) / blockSize.y);
178
179 kern_RGBAtoUYVY<<<numBlocks, blockSize, 0, stream>>>(dst, dstPitch, src, srcPitch, width, height);
180}
181
182void cuda_UYVY_to_RGBA(unsigned char *dst,
183 size_t dstPitch,
184 unsigned char *src,
185 size_t srcPitch,
186 size_t width,
187 size_t height,
188 CUstream_st *stream){
189
190 dim3 blockSize(32,32);
191 dim3 numBlocks((width + blockSize.x - 1) / blockSize.x,
192 (height + blockSize.y - 1) / blockSize.y);
193
194 kern_UYVYtoRGBA<<<numBlocks, blockSize, 0, stream>>>(dst, dstPitch, src, srcPitch, width, height);
195}
__global__ void kern_RGBAtoUYVY(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height)
void cuda_RGBA_to_UYVY(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height, CUstream_st *stream)
__global__ void kern_RGBAtoRGB(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height)
void cuda_UYVY_to_RGBA(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height, CUstream_st *stream)
void cuda_RGBA_to_RGB(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height, CUstream_st *stream)
#define min(a, b)
__global__ void kern_RGBtoRGBA(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height)
void cuda_RGB_to_RGBA(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height, CUstream_st *stream)
__global__ void kern_UYVYtoRGBA(unsigned char *dst, size_t dstPitch, unsigned char *src, size_t srcPitch, size_t width, size_t height)
#define max(a, b)