Skip to content

Commit

Permalink
Remove useless code from the libyuv wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov authored May 25, 2024
1 parent b93eb49 commit ca64bd1
Showing 1 changed file with 148 additions and 62 deletions.
210 changes: 148 additions & 62 deletions pkg/encoder/yuv/libyuv/libyuv.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package libyuv contains the wrapper for: https://chromium.googlesource.com/libyuv/libyuv.
// Libs are downloaded from: https://packages.macports.org/libyuv/.
// MacOS libs are from: https://packages.macports.org/libyuv/.
package libyuv

/*
Expand All @@ -12,6 +12,7 @@ package libyuv
#cgo darwin,arm64 LDFLAGS: -lyuv_darwin_arm64 -ljpeg -lstdc++
#include <stdint.h> // for uintptr_t and C99 types
#include <stdlib.h>
#if !defined(LIBYUV_API)
#define LIBYUV_API
Expand All @@ -23,6 +24,40 @@ package libyuv
#define LIBYUV_VERSION 1874 // darwin static libs version
#endif // INCLUDE_LIBYUV_VERSION_H_
// Supported rotation.
typedef enum RotationMode {
kRotate0 = 0, // No rotation.
kRotate90 = 90, // Rotate 90 degrees clockwise.
kRotate180 = 180, // Rotate 180 degrees.
kRotate270 = 270, // Rotate 270 degrees clockwise.
} RotationModeEnum;
// RGB16 (RGBP fourcc) little endian to I420.
LIBYUV_API
int RGB565ToI420(const uint8_t* src_rgb565, int src_stride_rgb565, uint8_t* dst_y, int dst_stride_y,
uint8_t* dst_u, int dst_stride_u, uint8_t* dst_v, int dst_stride_v, int width, int height);
// Rotate I420 frame.
LIBYUV_API
int I420Rotate(const uint8_t* src_y, int src_stride_y, const uint8_t* src_u, int src_stride_u,
const uint8_t* src_v, int src_stride_v, uint8_t* dst_y, int dst_stride_y, uint8_t* dst_u,
int dst_stride_u, uint8_t* dst_v, int dst_stride_v, int width, int height, enum RotationMode mode);
// RGB15 (RGBO fourcc) little endian to I420.
LIBYUV_API
int ARGB1555ToI420(const uint8_t* src_argb1555, int src_stride_argb1555, uint8_t* dst_y, int dst_stride_y,
uint8_t* dst_u, int dst_stride_u, uint8_t* dst_v, int dst_stride_v, int width, int height);
// ABGR little endian (rgba in memory) to I420.
LIBYUV_API
int ABGRToI420(const uint8_t* src_abgr, int src_stride_abgr, uint8_t* dst_y, int dst_stride_y, uint8_t* dst_u,
int dst_stride_u, uint8_t* dst_v, int dst_stride_v, int width, int height);
// ARGB little endian (bgra in memory) to I420.
LIBYUV_API
int ARGBToI420(const uint8_t* src_argb, int src_stride_argb, uint8_t* dst_y, int dst_stride_y, uint8_t* dst_u,
int dst_stride_u, uint8_t* dst_v, int dst_stride_v, int width, int height);
#ifdef __cplusplus
namespace libyuv {
extern "C" {
Expand All @@ -40,57 +75,95 @@ enum FourCC {
FOURCC_ANY = -1,
};
typedef enum RotationMode {
kRotate0 = 0, // No rotation.
kRotate90 = 90, // Rotate 90 degrees clockwise.
kRotate180 = 180, // Rotate 180 degrees.
kRotate270 = 270, // Rotate 270 degrees clockwise.
} RotationModeEnum;
inline void ConvertToI420Custom(const uint8_t* sample,
uint8_t* dst_y,
int dst_stride_y,
uint8_t* dst_u,
int dst_stride_u,
uint8_t* dst_v,
int dst_stride_v,
int src_width,
int src_height,
int crop_width,
int crop_height,
uint32_t fourcc) {
const int stride = src_width << 1;
LIBYUV_API
int ConvertToI420(const uint8_t* sample,
size_t sample_size,
uint8_t* dst_y,
int dst_stride_y,
uint8_t* dst_u,
int dst_stride_u,
uint8_t* dst_v,
int dst_stride_v,
int crop_x,
int crop_y,
int src_width,
int src_height,
int crop_width,
int crop_height,
enum RotationMode rotation,
uint32_t fourcc);
switch (fourcc) {
case FOURCC_RGBP:
RGB565ToI420(sample, stride, dst_y, dst_stride_y, dst_u,
dst_stride_u, dst_v, dst_stride_v, crop_width, crop_height);
break;
case FOURCC_RGBO:
ARGB1555ToI420(sample, stride, dst_y, dst_stride_y, dst_u,
dst_stride_u, dst_v, dst_stride_v, crop_width, crop_height);
break;
case FOURCC_ARGB:
ARGBToI420(sample, stride << 1, dst_y, dst_stride_y, dst_u,
dst_stride_u, dst_v, dst_stride_v, crop_width, crop_height);
break;
case FOURCC_ABGR:
ABGRToI420(sample, stride << 1, dst_y, dst_stride_y, dst_u,
dst_stride_u, dst_v, dst_stride_v, crop_width, crop_height);
break;
}
}
void rotateI420(const uint8_t* sample,
uint8_t* dst_y,
int dst_stride_y,
uint8_t* dst_u,
int dst_stride_u,
uint8_t* dst_v,
int dst_stride_v,
int src_width,
int src_height,
int crop_width,
int crop_height,
enum RotationMode rotation,
uint32_t fourcc) {
uint8_t* tmp_y = dst_y;
uint8_t* tmp_u = dst_u;
uint8_t* tmp_v = dst_v;
int tmp_y_stride = dst_stride_y;
int tmp_u_stride = dst_stride_u;
int tmp_v_stride = dst_stride_v;
uint8_t* rotate_buffer = NULL;
int y_size = crop_width * crop_height;
int uv_size = y_size >> 1;
rotate_buffer = (uint8_t*)malloc(y_size + y_size);
if (!rotate_buffer) {
return;
}
dst_y = rotate_buffer;
dst_u = dst_y + y_size;
dst_v = dst_u + uv_size;
dst_stride_y = crop_width;
dst_stride_u = dst_stride_v = crop_width >> 1;
ConvertToI420Custom(sample, dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v,
src_width, src_height, crop_width, crop_height, fourcc);
I420Rotate(dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
dst_stride_v, tmp_y, tmp_y_stride, tmp_u, tmp_u_stride,
tmp_v, tmp_v_stride, crop_width, crop_height, rotation);
free(rotate_buffer);
}
// Supported filtering.
typedef enum FilterMode {
kFilterNone = 0, // Point sample; Fastest.
kFilterLinear = 1, // Filter horizontally only.
kFilterBilinear = 2, // Faster than box, but lower quality scaling down.
kFilterBox = 3 // Highest quality.
kFilterNone = 0, // Point sample; Fastest.
kFilterLinear = 1, // Filter horizontally only.
kFilterBilinear = 2, // Faster than box, but lower quality scaling down.
kFilterBox = 3 // Highest quality.
} FilterModeEnum;
LIBYUV_API
int I420Scale(const uint8_t *src_y,
int src_stride_y,
const uint8_t *src_u,
int src_stride_u,
const uint8_t *src_v,
int src_stride_v,
int src_width,
int src_height,
uint8_t *dst_y,
int dst_stride_y,
uint8_t *dst_u,
int dst_stride_u,
uint8_t *dst_v,
int dst_stride_v,
int dst_width,
int dst_height,
enum FilterMode filtering);
int I420Scale(const uint8_t *src_y, int src_stride_y, const uint8_t *src_u, int src_stride_u,
const uint8_t *src_v, int src_stride_v, int src_width, int src_height, uint8_t *dst_y,
int dst_stride_y, uint8_t *dst_u, int dst_stride_u, uint8_t *dst_v, int dst_stride_v,
int dst_width, int dst_height, enum FilterMode filtering);
#ifdef __cplusplus
} // extern "C"
Expand All @@ -113,23 +186,36 @@ func Y420(src []byte, dst []byte, _, h, stride int, dw, dh int, rot uint, pix ui
yStride := dw
cStride := cw

C.ConvertToI420(
(*C.uchar)(&src[0]),
C.size_t(0),
(*C.uchar)(&dst[0]),
C.int(yStride),
(*C.uchar)(&dst[i0]),
C.int(cStride),
(*C.uchar)(&dst[i1]),
C.int(cStride),
C.int(0),
C.int(0),
C.int(stride),
C.int(h),
C.int(cx),
C.int(cy),
C.enum_RotationMode(rot),
C.uint32_t(pix))
if rot == 0 {
C.ConvertToI420Custom(
(*C.uchar)(&src[0]),
(*C.uchar)(&dst[0]),
C.int(yStride),
(*C.uchar)(&dst[i0]),
C.int(cStride),
(*C.uchar)(&dst[i1]),
C.int(cStride),
C.int(stride),
C.int(h),
C.int(cx),
C.int(cy),
C.uint32_t(pix))
} else {
C.rotateI420(
(*C.uchar)(&src[0]),
(*C.uchar)(&dst[0]),
C.int(yStride),
(*C.uchar)(&dst[i0]),
C.int(cStride),
(*C.uchar)(&dst[i1]),
C.int(cStride),
C.int(stride),
C.int(h),
C.int(cx),
C.int(cy),
C.enum_RotationMode(rot),
C.uint32_t(pix))
}
}

func Y420Scale(src []byte, dst []byte, w, h int, dw, dh int) {
Expand Down

0 comments on commit ca64bd1

Please sign in to comment.