-
Notifications
You must be signed in to change notification settings - Fork 5
/
convert_yuv.go
371 lines (339 loc) · 9.27 KB
/
convert_yuv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package blurry
/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo darwin LDFLAGS: -L${SRCDIR}/lib -lruntime_osx -ldl -lm
#cgo darwin LDFLAGS: -lconvert_from_yuv_420_osx
#cgo darwin LDFLAGS: -lconvert_from_yuv_444_osx
#cgo darwin LDFLAGS: -lconvert_to_yuv_420_osx
#cgo darwin LDFLAGS: -lconvert_to_yuv_444_osx
#cgo linux LDFLAGS: -L${SRCDIR}/lib -lruntime_linux -ldl -lm
#cgo linux LDFLAGS: -lconvert_from_yuv_420_linux
#cgo linux LDFLAGS: -lconvert_from_yuv_444_linux
#cgo linux LDFLAGS: -lconvert_to_yuv_420_linux
#cgo linux LDFLAGS: -lconvert_to_yuv_444_linux
#include <stdlib.h>
#ifdef __APPLE__
#include "libconvert_from_yuv_420_osx.h"
#include "libconvert_from_yuv_444_osx.h"
#include "libconvert_to_yuv_420_osx.h"
#include "libconvert_to_yuv_444_osx.h"
#elif __linux__
#include "libconvert_from_yuv_420_linux.h"
#include "libconvert_from_yuv_444_linux.h"
#include "libconvert_to_yuv_420_linux.h"
#include "libconvert_to_yuv_444_linux.h"
#endif
#include "buffer.h"
int call_convert_from_yuv(
unsigned char chrs,
halide_buffer_t *in_y_buf,
halide_buffer_t *in_u_buf,
halide_buffer_t *in_v_buf,
int32_t width, int32_t height,
halide_buffer_t *out_buf
) {
if(1 == chrs) {
return convert_from_yuv_420(in_y_buf, in_u_buf, in_v_buf, width, height, out_buf);
}
if(2 == chrs) {
return convert_from_yuv_444(in_y_buf, in_u_buf, in_v_buf, width, height, out_buf);
}
return 1;
}
halide_buffer_t *create_yuv_y_plane(
unsigned char chrs,
unsigned char *src, int32_t stride,
int32_t width, int32_t height
) {
return create_yuv_plane_buffer(src, stride, width, height);
}
halide_buffer_t *create_yuv_uv_plane(
unsigned char chrs,
unsigned char *src, int32_t stride,
int32_t width, int32_t height
) {
if(1 == chrs) {
return create_yuv_plane_buffer(src, stride, width / 2, height / 2);
}
if(2 == chrs) {
return create_yuv_plane_buffer(src, stride, width, height);
}
return NULL;
}
int libconvert_from_yuv(
unsigned char *src_y,
unsigned char *src_u,
unsigned char *src_v,
int32_t y_stride,
int32_t u_stride,
int32_t v_stride,
int32_t width, int32_t height,
unsigned char chrs,
unsigned char *out
) {
halide_buffer_t *in_y_buf = create_yuv_y_plane(chrs, src_y, y_stride, width, height);
if(in_y_buf == NULL){
return 1;
}
halide_buffer_t *in_u_buf = create_yuv_uv_plane(chrs, src_u, u_stride, width, height);
if(in_u_buf == NULL){
free_buf(in_y_buf);
return 1;
}
halide_buffer_t *in_v_buf = create_yuv_uv_plane(chrs, src_v, v_stride, width, height);
if(in_v_buf == NULL){
free_buf(in_y_buf);
free_buf(in_u_buf);
return 1;
}
halide_buffer_t *out_rgba_buf = create_rgba_buffer(out, width, height);
if(out_rgba_buf == NULL){
free_buf(in_y_buf);
free_buf(in_u_buf);
free_buf(in_v_buf);
return 1;
}
int ret = call_convert_from_yuv(chrs, in_y_buf, in_u_buf, in_v_buf, width, height, out_rgba_buf);
free_buf(in_y_buf);
free_buf(in_u_buf);
free_buf(in_v_buf);
free_buf(out_rgba_buf);
return ret;
}
int libconvert_to_yuv420(
unsigned char *src,
int32_t width, int32_t height,
unsigned char *out_y,
unsigned char *out_u,
unsigned char *out_v
) {
halide_buffer_t *in_rgba_buf = create_rgba_buffer(src, width, height);
if(in_rgba_buf == NULL){
return 1;
}
halide_buffer_t *out_y_buf = create_uint8_array_buffer(out_y, width, height);
if(out_y_buf == NULL){
free_buf(in_rgba_buf);
return 1;
}
halide_buffer_t *out_u_buf = create_uint8_array_buffer(out_u, width / 2, height / 2);
if(out_u_buf == NULL){
free_buf(in_rgba_buf);
free_buf(out_y_buf);
return 1;
}
halide_buffer_t *out_v_buf = create_uint8_array_buffer(out_v, width / 2, height / 2);
if(out_v_buf == NULL){
free_buf(in_rgba_buf);
free_buf(out_y_buf);
free_buf(out_u_buf);
return 1;
}
int ret = convert_to_yuv_420(in_rgba_buf, width, height, out_y_buf, out_u_buf, out_v_buf);
free_buf(in_rgba_buf);
free_buf(out_y_buf);
free_buf(out_u_buf);
free_buf(out_v_buf);
return ret;
}
int libconvert_to_yuv444(
unsigned char *src,
int32_t width, int32_t height,
unsigned char *out_y,
unsigned char *out_u,
unsigned char *out_v
) {
halide_buffer_t *in_rgba_buf = create_rgba_buffer(src, width, height);
if(in_rgba_buf == NULL){
return 1;
}
halide_buffer_t *out_y_buf = create_uint8_array_buffer(out_y, width, height);
if(out_y_buf == NULL){
free_buf(in_rgba_buf);
return 1;
}
halide_buffer_t *out_u_buf = create_uint8_array_buffer(out_u, width, height);
if(out_u_buf == NULL){
free_buf(in_rgba_buf);
free_buf(out_y_buf);
return 1;
}
halide_buffer_t *out_v_buf = create_uint8_array_buffer(out_v, width, height);
if(out_v_buf == NULL){
free_buf(in_rgba_buf);
free_buf(out_y_buf);
free_buf(out_u_buf);
return 1;
}
int ret = convert_to_yuv_444(in_rgba_buf, width, height, out_y_buf, out_u_buf, out_v_buf);
free_buf(in_rgba_buf);
free_buf(out_y_buf);
free_buf(out_u_buf);
free_buf(out_v_buf);
return ret;
}
*/
import "C"
import (
"errors"
"image"
)
var (
ErrConvertFromYUV = errors.New("convert_from cgo call error")
ErrConvertToYUV = errors.New("convert_to cgo call error")
ErrYUVSubsampleRateMustI420 = errors.New("image.YCbCr.SubsampleRatio must be 420")
ErrYUVSubsampleRateMustI444 = errors.New("image.YCbCr.SubsampleRatio must be 444")
)
func ConvertFromYUV(img *image.YCbCr, chrs ChromaSubsampling) (*image.RGBA, error) {
width, height := whYCbCr(img)
out := GetRGBA(width, height)
ret := C.libconvert_from_yuv(
(*C.uchar)(&img.Y[0]),
(*C.uchar)(&img.Cb[0]),
(*C.uchar)(&img.Cr[0]),
C.int(img.YStride),
C.int(img.CStride),
C.int(img.CStride),
C.int(width),
C.int(height),
C.uchar(chrs),
(*C.uchar)(&out.Pix[0]),
)
if int(ret) != 0 {
return nil, ErrConvertFromYUV
}
return out, nil
}
func ConvertFromYUV420(img *image.YCbCr) (*image.RGBA, error) {
if img.SubsampleRatio != image.YCbCrSubsampleRatio420 {
return nil, ErrYUVSubsampleRateMustI420
}
return ConvertFromYUV(img, ChromaSubsampling420)
}
func ConvertFromYUV444(img *image.YCbCr) (*image.RGBA, error) {
if img.SubsampleRatio != image.YCbCrSubsampleRatio444 {
return nil, ErrYUVSubsampleRateMustI444
}
return ConvertFromYUV(img, ChromaSubsampling444)
}
func ConvertFromYUV420Plane(y, u, v []byte, y_stride, u_stride, v_stride int, width, height int) (*image.RGBA, error) {
img := &image.YCbCr{
Y: y,
Cb: u,
Cr: v,
Rect: image.Rect(0, 0, width, height),
YStride: y_stride,
CStride: u_stride,
SubsampleRatio: image.YCbCrSubsampleRatio420,
}
return ConvertFromYUV(img, ChromaSubsampling420)
}
func ConvertFromYUV444Plane(y, u, v []byte, y_stride, u_stride, v_stride int, width, height int) (*image.RGBA, error) {
img := &image.YCbCr{
Y: y,
Cb: u,
Cr: v,
Rect: image.Rect(0, 0, width, height),
YStride: y_stride,
CStride: u_stride,
SubsampleRatio: image.YCbCrSubsampleRatio444,
}
return ConvertFromYUV(img, ChromaSubsampling444)
}
func ConvertToYUV420(img *image.RGBA) (*image.YCbCr, error) {
width, height := wh(img)
uvWidth := width / 2
uvHeight := height / 2
ySize := width * height
uSize := uvWidth * uvHeight
vSize := uvWidth * uvHeight
//
// data layout
// +-----------------+ width
// | Y Y Y Y Y Y Y Y |
// | Y Y Y Y Y Y Y Y |
// | Y Y Y Y Y Y Y Y |
// | Y Y Y Y Y Y Y Y |
// +---------+-------+ height
// | U U U U | 0 0 0 |
// | U U U U | 0 0 0 |
// +---------+-------+ uvHeight
// | V V V V | 0 0 0 |
// | V V V V | 0 0 0 |
// +-----------------+
// uvWidth
//
yBuf := GetByteBuf(ySize)
uBuf := GetByteBuf(uSize)
vBuf := GetByteBuf(vSize)
ret := C.libconvert_to_yuv420(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
(*C.uchar)(&yBuf[0]),
(*C.uchar)(&uBuf[0]),
(*C.uchar)(&vBuf[0]),
)
if int(ret) != 0 {
return nil, ErrConvertToYUV
}
out := &image.YCbCr{
Y: yBuf,
Cb: uBuf,
Cr: vBuf,
YStride: width,
CStride: uvWidth,
SubsampleRatio: image.YCbCrSubsampleRatio420,
Rect: image.Rect(0, 0, width, height),
}
return out, nil
}
func ConvertToYUV444(img *image.RGBA) (*image.YCbCr, error) {
width, height := wh(img)
ySize := width * height
uSize := width * height
vSize := width * height
//
// data layout
// +-----------------+ width
// | Y Y Y Y Y Y Y Y |
// | Y Y Y Y Y Y Y Y |
// | Y Y Y Y Y Y Y Y |
// | Y Y Y Y Y Y Y Y |
// +---------+-------+ height
// | U U U U U U U U |
// | U U U U U U U U |
// | U U U U U U U U |
// | U U U U U U U U |
// +---------+-------+ height
// | V V V V V V V V |
// | V V V V V V V V |
// | V V V V V V V V |
// | V V V V V V V V |
// +-----------------+ height
//
yBuf := GetByteBuf(ySize)
uBuf := GetByteBuf(uSize)
vBuf := GetByteBuf(vSize)
ret := C.libconvert_to_yuv444(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
(*C.uchar)(&yBuf[0]),
(*C.uchar)(&uBuf[0]),
(*C.uchar)(&vBuf[0]),
)
if int(ret) != 0 {
return nil, ErrConvertToYUV
}
out := &image.YCbCr{
Y: yBuf,
Cb: uBuf,
Cr: vBuf,
YStride: width,
CStride: width,
SubsampleRatio: image.YCbCrSubsampleRatio444,
Rect: image.Rect(0, 0, width, height),
}
return out, nil
}