Skip to content

Commit b1f16b6

Browse files
committed
pgssubdec: fix subpicture output colorspace and range
Functionality used before didn't widen the values from limited to full range. Additionally, now the decoder uses BT.709 where it should be used according to the video resolution. Default for not yet set colorimetry is BT.709 due to most observed HDMV content being HD. BT.709 coefficients were gathered from the first two parts of BT.709 to BT.2020 conversion guide in ARIB STD-B62 (Pt. 1, Chapter 6.2.2). Based on a patch by Carl Eugen Hoyos.
1 parent 3eafbbe commit b1f16b6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

libavcodec/pgssubdec.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef struct PGSSubContext {
9696
PGSSubPalettes palettes;
9797
PGSSubObjects objects;
9898
int forced_subs_only;
99+
int hdtv;
99100
} PGSSubContext;
100101

101102
static void flush_cache(AVCodecContext *avctx)
@@ -136,6 +137,9 @@ static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
136137

137138
static av_cold int init_decoder(AVCodecContext *avctx)
138139
{
140+
PGSSubContext *ctx = avctx->priv_data;
141+
ctx->hdtv = -1;
142+
139143
avctx->pix_fmt = AV_PIX_FMT_PAL8;
140144

141145
return 0;
@@ -354,8 +358,14 @@ static int parse_palette_segment(AVCodecContext *avctx,
354358
cb = bytestream_get_byte(&buf);
355359
alpha = bytestream_get_byte(&buf);
356360

357-
YUV_TO_RGB1(cb, cr);
358-
YUV_TO_RGB2(r, g, b, y);
361+
/* Default to HDTV (-1 or 1) */
362+
if (ctx->hdtv) {
363+
YUV_TO_RGB1_CCIR_BT709(cb, cr);
364+
} else {
365+
YUV_TO_RGB1_CCIR(cb, cr);
366+
}
367+
368+
YUV_TO_RGB2_CCIR(r, g, b, y);
359369

360370
ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
361371

@@ -388,6 +398,12 @@ static int parse_presentation_segment(AVCodecContext *avctx,
388398
int w = bytestream_get_be16(&buf);
389399
int h = bytestream_get_be16(&buf);
390400

401+
// Set colorimetry according to resolution if not yet set
402+
if (h > 576)
403+
ctx->hdtv = 1;
404+
else
405+
ctx->hdtv = 0;
406+
391407
ctx->presentation.pts = pts;
392408

393409
ff_dlog(avctx, "Video Dimensions %dx%d\n",

libavutil/colorspace.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
4242
}
4343

44+
#define YUV_TO_RGB1_CCIR_BT709(cb1, cr1)\
45+
{\
46+
cb = (cb1) - 128;\
47+
cr = (cr1) - 128;\
48+
r_add = FIX(1.5747*255.0/224.0) * cr + ONE_HALF;\
49+
g_add = - FIX(0.1873*255.0/224.0) * cb - FIX(0.4682*255.0/224.0) * cr + \
50+
ONE_HALF;\
51+
b_add = FIX(1.8556*255.0/224.0) * cb + ONE_HALF;\
52+
}
53+
4454
#define YUV_TO_RGB2_CCIR(r, g, b, y1)\
4555
{\
4656
y = ((y1) - 16) * FIX(255.0/219.0);\

0 commit comments

Comments
 (0)