Skip to content

Commit

Permalink
Add support for frame sizes not divisible by 8
Browse files Browse the repository at this point in the history
It was over optimised anyway

Fixes matanui159#5
  • Loading branch information
Joshua Minter committed Jul 25, 2020
1 parent 20d9659 commit 0dae21e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ void rsConfigLoad(RSConfig *config) {

// Relative config
configLoadFile(config, ".");

if (config->width % 8 != 0 || config->height % 8 != 0) {
rsError("Only frame sizes divisable by 2 are supported");
}
}

void rsConfigDestroy(RSConfig *config) {
Expand Down
33 changes: 7 additions & 26 deletions src/util/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@
#define FRAME_GET(frame, x, y) \
((frame)->data + (y) * (frame)->strideY + (x) * (frame)->strideX)

#define FRAME_REPEAT4(init, cond, next, body) \
for (init; cond;) { \
do \
body while (false); \
next; \
do \
body while (false); \
next; \
do \
body while (false); \
next; \
do \
body while (false); \
next; \
}

static void frameDestroy(RSFrame *frame) {
rsMemoryDestroy(frame->data);
}
Expand All @@ -60,27 +44,24 @@ void rsFrameDestroy(RSFrame *frame) {

void rsFrameConvertI420(const RSFrame *restrict frame, RSFrame *restrict yFrame,
RSFrame *restrict uFrame, RSFrame *restrict vFrame) {
if (frame->width % 8 != 0 || frame->height % 8 != 0) {
rsError(
"Due to optimization reasons, only frame sizes divisable by 8 are supported");
}
// We can assume frame is divisible by 2 due to check done in `config.c`

FRAME_REPEAT4(size_t y = 0, y < frame->height, y += 2, {
for(size_t y = 0; y < frame->height; y += 2) {
uint16_t uRow[frame->width];
uint16_t vRow[frame->width];

// Copy the first Y-row and add together the top half of the U and V pixels
FRAME_REPEAT4(size_t x = 0, x < frame->width, x += 2, {
for(size_t x = 0; x < frame->width; x += 2) {
const uint8_t *restrict leftPixel = FRAME_GET(frame, x, y);
const uint8_t *restrict rightPixel = FRAME_GET(frame, x + 1, y);
*FRAME_GET(yFrame, x, y) = leftPixel[0];
*FRAME_GET(yFrame, x + 1, y) = rightPixel[0];
uRow[x >> 1] = (uint16_t)(leftPixel[1] + rightPixel[1]);
vRow[x >> 1] = (uint16_t)(leftPixel[2] + rightPixel[2]);
})
}

// Copy the second Y-row and calculate the full U and V pixels
FRAME_REPEAT4(size_t x = 0, x < frame->width, x += 2, {
for(size_t x = 0; x < frame->width; x += 2) {
const uint8_t *restrict leftPixel = FRAME_GET(frame, x, y + 1);
const uint8_t *restrict rightPixel = FRAME_GET(frame, x + 1, y + 1);
*FRAME_GET(yFrame, x, y + 1) = leftPixel[0];
Expand All @@ -89,6 +70,6 @@ void rsFrameConvertI420(const RSFrame *restrict frame, RSFrame *restrict yFrame,
uint8_t vPixel = (uint8_t)((vRow[x >> 1] + leftPixel[2] + rightPixel[2]) >> 2);
*FRAME_GET(uFrame, x >> 1, y >> 1) = uPixel;
*FRAME_GET(vFrame, x >> 1, y >> 1) = vPixel;
})
})
}
}
}

0 comments on commit 0dae21e

Please sign in to comment.