Skip to content

Commit

Permalink
Align strides to be divisible by 16 when resizing/caching textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Spacetech committed Jun 26, 2022
1 parent 5776c97 commit fa93cb0
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions xbmc/pictures/Picture.cpp
Expand Up @@ -161,24 +161,30 @@ bool CPicture::ResizeTexture(const std::string &image, uint8_t *pixels, uint32_t
// create a buffer large enough for the resulting image
GetScale(width, height, dest_width, dest_height);

uint8_t *buffer = new uint8_t[dest_width * dest_height * sizeof(uint32_t)];
// Let's align so that stride is always divisible by 16, and then add some 32 bytes more on top
// See: https://github.com/FFmpeg/FFmpeg/blob/75638fe9402f70645bdde4d95672fa640a327300/libswscale/tests/swscale.c#L157
uint32_t dest_width_aligned = ((dest_width + 15) & ~0x0f);
uint32_t stride = dest_width_aligned * sizeof(uint32_t);

uint32_t* buffer = new uint32_t[dest_width_aligned * dest_height + 4];
if (buffer == NULL)
{
result = NULL;
result_size = 0;
return false;
}

if (!ScaleImage(pixels, width, height, pitch, AV_PIX_FMT_BGRA, buffer, dest_width, dest_height,
dest_width * sizeof(uint32_t), AV_PIX_FMT_BGRA, scalingAlgorithm))
if (!ScaleImage(pixels, width, height, pitch, AV_PIX_FMT_BGRA, (uint8_t*)buffer, dest_width,
dest_height, stride, AV_PIX_FMT_BGRA, scalingAlgorithm))
{
delete[] buffer;
result = NULL;
result_size = 0;
return false;
}

bool success = GetThumbnailFromSurface(buffer, dest_width, dest_height, dest_width * sizeof(uint32_t), image, result, result_size);
bool success = GetThumbnailFromSurface((unsigned char*)buffer, dest_width, dest_height, stride,
image, result, result_size);
delete[] buffer;

if (!success)
Expand Down Expand Up @@ -239,15 +245,22 @@ bool CPicture::CacheTexture(uint8_t *pixels, uint32_t width, uint32_t height, ui

// create a buffer large enough for the resulting image
GetScale(width, height, dest_width, dest_height);
uint32_t *buffer = new uint32_t[dest_width * dest_height];

// Let's align so that stride is always divisible by 16, and then add some 32 bytes more on top
// See: https://github.com/FFmpeg/FFmpeg/blob/75638fe9402f70645bdde4d95672fa640a327300/libswscale/tests/swscale.c#L157
uint32_t dest_width_aligned = ((dest_width + 15) & ~0x0f);
uint32_t stride = dest_width_aligned * sizeof(uint32_t);

uint32_t* buffer = new uint32_t[dest_width_aligned * dest_height + 4];
if (buffer)
{
if (ScaleImage(pixels, width, height, pitch, AV_PIX_FMT_BGRA, (uint8_t*)buffer, dest_width,
dest_height, dest_width * 4, AV_PIX_FMT_BGRA, scalingAlgorithm))
dest_height, stride, AV_PIX_FMT_BGRA, scalingAlgorithm))
{
if (!orientation || OrientateImage(buffer, dest_width, dest_height, orientation))
{
success = CreateThumbnailFromSurface((unsigned char*)buffer, dest_width, dest_height, dest_width * 4, dest);
success = CreateThumbnailFromSurface((unsigned char*)buffer, dest_width, dest_height,
stride, dest);
}
}
delete[] buffer;
Expand Down

0 comments on commit fa93cb0

Please sign in to comment.