Skip to content

Commit

Permalink
Add RGBA4444 format support
Browse files Browse the repository at this point in the history
  • Loading branch information
xerpi committed Sep 27, 2020
1 parent 6015b7d commit fe2c265
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/format_conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
void r8g8b8a8_to_yuy2(const unsigned char *rgba, unsigned char *yuy2, int in_stride, int width, int height);
void r5g6b5_to_yuy2(const unsigned char *rgb, unsigned char *yuy2, int in_stride, int width, int height);
void r5g5b5a1_to_yuy2(const unsigned char *rgba, unsigned char *yuy2, int in_stride, int width, int height);
void r4g4b4a4_to_yuy2(const unsigned char *rgba, unsigned char *yuy2, int in_stride, int width, int height);

#endif
36 changes: 36 additions & 0 deletions src/format_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,39 @@ void r5g5b5a1_to_yuy2(const unsigned char *rgba, unsigned char *yuy2, int in_str
}
}
}

void r4g4b4a4_to_yuy2(const unsigned char *rgba, unsigned char *yuy2, int in_stride, int width, int height)
{
int i, j;

for (i = 0; i < height; i++) {
for (j = 0; j < width; j+=2) {
const unsigned short *rgbap = (unsigned short *)&rgba[2 * (j + i * in_stride)];
unsigned char *yuy2p = &yuy2[2 * (j + i * width)];

unsigned short p0 = rgbap[0];
unsigned short p1 = rgbap[1];

unsigned char p0_r = (p0 & 0xF) << 4,
p0_g = ((p0 >> 4) & 0xF) << 4,
p0_b = ((p0 >> 8) & 0xF) << 4;

unsigned char p1_r = (p1 & 0xF) << 4,
p1_g = ((p1 >> 4) & 0xF) << 4,
p1_b = ((p1 >> 8) & 0xF) << 4;

unsigned char sub_r = AVERAGE(p0_r, p1_r);
unsigned char sub_g = AVERAGE(p0_g, p1_g);
unsigned char sub_b = AVERAGE(p0_b, p1_b);
unsigned char y0 = RGB2Y(p0_r, p0_g, p0_b);
unsigned char y1 = RGB2Y(p1_r, p1_g, p1_b);
unsigned char u = RGB2U(sub_r, sub_g, sub_b);
unsigned char v = RGB2V(sub_r, sub_g, sub_b);

yuy2p[0] = y0;
yuy2p[1] = u;
yuy2p[2] = y1;
yuy2p[3] = v;
}
}
}
17 changes: 14 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ int convert_and_send_frame_yuy2(int fid, void *fbaddr, int fbstride, int fbpixel
static struct UsbbdDeviceRequest req;
static const int eof = 1;
unsigned int event;
unsigned int t0, t1, t2, t3;
int ret;

req = (struct UsbbdDeviceRequest){
Expand All @@ -439,31 +440,41 @@ int convert_and_send_frame_yuy2(int fid, void *fbaddr, int fbstride, int fbpixel
if (eof)
tx_buf[1] |= UVC_STREAM_EOF;

t0 = sceKernelGetSystemTimeLow();

if (fbpixelformat == PSP_DISPLAY_PIXEL_FORMAT_8888)
r8g8b8a8_to_yuy2(fbaddr, &tx_buf[UVC_PAYLOAD_HEADER_SIZE], fbstride, 480, 272);
else if (fbpixelformat == PSP_DISPLAY_PIXEL_FORMAT_565)
r5g6b5_to_yuy2(fbaddr, &tx_buf[UVC_PAYLOAD_HEADER_SIZE], fbstride, 480, 272);
else if (fbpixelformat == PSP_DISPLAY_PIXEL_FORMAT_5551)
r5g5b5a1_to_yuy2(fbaddr, &tx_buf[UVC_PAYLOAD_HEADER_SIZE], fbstride, 480, 272);
//else if (fbpixelformat == PSP_DISPLAY_PIXEL_FORMAT_4444)
// r4g4b4a4_to_yuy2(fbaddr, &tx_buf[UVC_PAYLOAD_HEADER_SIZE], fbstride, 480, 272);
else if (fbpixelformat == PSP_DISPLAY_PIXEL_FORMAT_4444)
r4g4b4a4_to_yuy2(fbaddr, &tx_buf[UVC_PAYLOAD_HEADER_SIZE], fbstride, 480, 272);

sceKernelDcacheWritebackRange(tx_buf, sizeof(tx_buf));

t1 = sceKernelGetSystemTimeLow();

LOG("Sending frame...\n");

t2 = sceKernelGetSystemTimeLow();

ret = sceUsbbdReqSend(&req);
if (ret < 0)
return ret;

ret = sceKernelWaitEventFlagCB(uvc_frame_req_evflag, EVENT_STOP_STREAM | EVENT_FRAME_SENT,
PSP_EVENT_WAITOR | PSP_EVENT_WAITCLEAR, &event, NULL);

t3 = sceKernelGetSystemTimeLow();

if (event & EVENT_STOP_STREAM)
LOG("Received stream stop!\n");
else if (event & EVENT_FRAME_SENT)
LOG("Frame sent!\n");

LOG("CSC: %dus, USB send: %dus\n", t1 - t0, t3 - t2);

return ret;
}

Expand Down Expand Up @@ -494,7 +505,7 @@ static int send_frame(void)
void *fbaddr;
int fbwidth;
int fbstride;
int fbpixelformat;
int fbpixelformat = 0;

//fbwidth = 480;
//ret = sceDisplayGetFrameBuf(&fbaddr, &fbstride, &fbpixelformat, PSP_DISPLAY_SETBUF_IMMEDIATE);
Expand Down

0 comments on commit fe2c265

Please sign in to comment.