Skip to content

Commit

Permalink
Fix YUV conversion for full color range
Browse files Browse the repository at this point in the history
Take the color range (full vs limited) into account to render the
picture.

Note that with the current version of SDL, it has no impact with the SDL
opengl render driver.

Fixes #4756 <#4756>
Refs <#4756 (comment)>
Refs libusb/#9311 <libsdl-org/SDL#9311>

Suggested-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
  • Loading branch information
rom1v committed Mar 30, 2024
1 parent 1c3801a commit db55edb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/src/display.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "display.h"

#include <assert.h>
#include <libavutil/pixfmt.h>

#include "util/log.h"

Expand Down Expand Up @@ -65,6 +66,7 @@ sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps) {
display->texture = NULL;
display->pending.flags = 0;
display->pending.frame = NULL;
display->has_frame = false;

return true;
}
Expand Down Expand Up @@ -196,9 +198,25 @@ sc_display_set_texture_size(struct sc_display *display, struct sc_size size) {
return SC_DISPLAY_RESULT_OK;
}

static SDL_YUV_CONVERSION_MODE
sc_display_to_sdl_color_range(enum AVColorRange color_range) {
return color_range == AVCOL_RANGE_JPEG ? SDL_YUV_CONVERSION_JPEG
: SDL_YUV_CONVERSION_AUTOMATIC;
}

static bool
sc_display_update_texture_internal(struct sc_display *display,
const AVFrame *frame) {
if (!display->has_frame) {
// First frame
display->has_frame = true;

// Configure YUV color range conversion
SDL_YUV_CONVERSION_MODE sdl_color_range =
sc_display_to_sdl_color_range(frame->color_range);
SDL_SetYUVConversionMode(sdl_color_range);
}

int ret = SDL_UpdateYUVTexture(display->texture, NULL,
frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1],
Expand Down
2 changes: 2 additions & 0 deletions app/src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct sc_display {
struct sc_size size;
AVFrame *frame;
} pending;

bool has_frame;
};

enum sc_display_result {
Expand Down

0 comments on commit db55edb

Please sign in to comment.