@@ -282,6 +282,21 @@ static void BuildMatrix_Yuv2Rgb_core(double Kr, double Kb, int int_arith_shift,
282282
283283}
284284
285+ bool GetKrKb (int matrix, double & Kr, double & Kb)
286+ {
287+ switch (matrix) {
288+ case AVS_MATRIX_BT470_BG:
289+ case AVS_MATRIX_ST170_M: Kr = 0.299 ; Kb = 0.114 ; return true ;
290+ case AVS_MATRIX_BT709: Kr = 0.2126 ; Kb = 0.0722 ; return true ;
291+ case AVS_MATRIX_BT2020_NCL:
292+ case AVS_MATRIX_BT2020_CL: Kr = 0.2627 ; Kb = 0.0593 ; return true ;
293+ case AVS_MATRIX_BT470_M: Kr = 0.3 ; Kb = 0.11 ; return true ;
294+ case AVS_MATRIX_ST240_M: Kr = 0.212 ; Kb = 0.087 ; return true ;
295+ case AVS_MATRIX_AVERAGE: Kr = 1.0 / 3 ; Kb = 1.0 / 3 ; return true ;
296+ default : return false ;
297+ }
298+ }
299+
285300bool do_BuildMatrix_Rgb2Yuv (int _Matrix, int _ColorRange, int _ColorRange_Out, int int_arith_shift, int bits_per_pixel, ConversionMatrix& matrix)
286301{
287302 if (_ColorRange != ColorRange_e::AVS_RANGE_FULL && _ColorRange != ColorRange_e::AVS_RANGE_LIMITED)
@@ -292,48 +307,20 @@ bool do_BuildMatrix_Rgb2Yuv(int _Matrix, int _ColorRange, int _ColorRange_Out, i
292307 const bool is_full_s = _ColorRange == ColorRange_e::AVS_RANGE_FULL;
293308 const bool is_full_d = _ColorRange_Out == ColorRange_e::AVS_RANGE_FULL;
294309
295- if (_Matrix == Matrix_e::AVS_MATRIX_BT470_BG || _Matrix == Matrix_e::AVS_MATRIX_ST170_M) { // 601
296- /*
297- Y'= 0.299*R' + 0.587*G' + 0.114*B'
298- Cb=-0.169*R' - 0.331*G' + 0.500*B'
299- Cr= 0.500*R' - 0.419*G' - 0.081*B'
300- */
301- BuildMatrix_Rgb2Yuv_core (0.299 , /* 0.587 */ 0.114 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
302- }
303- else if (_Matrix == Matrix_e::AVS_MATRIX_BT709) {
304- /*
305- Y'= 0.2126*R' + 0.7152*G' + 0.0722*B'
306- Cb=-0.1145*R' - 0.3855*G' + 0.5000*B'
307- Cr= 0.5000*R' - 0.4542*G' - 0.0458*B'
308- */
309- BuildMatrix_Rgb2Yuv_core (0.2126 , /* 0.7152 */ 0.0722 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
310- }
311- else if (_Matrix == Matrix_e::AVS_MATRIX_AVERAGE) { // non-standard!
312- BuildMatrix_Rgb2Yuv_core (1.0 / 3 , /* 1.0/3 */ 1.0 / 3 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
313- }
314- else if (_Matrix == Matrix_e::AVS_MATRIX_BT2020_CL || _Matrix == Matrix_e::AVS_MATRIX_BT2020_NCL) {
315- BuildMatrix_Rgb2Yuv_core (0.2627 , /* 0.6780 */ 0.0593 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
316- }
317- else if (_Matrix == Matrix_e::AVS_MATRIX_BT470_M) {
318- BuildMatrix_Rgb2Yuv_core (0.3 , /* 0.59 */ 0.11 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
310+ // Special cases not handled by GetKrKb
311+ if (_Matrix == Matrix_e::AVS_MATRIX_RGB) {
312+ // copies Green to Y and sets UV to 0
313+ BuildMatrix_Rgb2Yuv_core (0.0 , 0.0 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
314+ return true ;
319315 }
320- else if (_Matrix == Matrix_e::AVS_MATRIX_ST240_M) {
321- BuildMatrix_Rgb2Yuv_core (0.212 , /* 0.701 */ 0.087 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
322- }
323- else if (_Matrix == Matrix_e::AVS_MATRIX_RGB) { // copies Green to Y and sets UV to 0
324- BuildMatrix_Rgb2Yuv_core (0.0 , /* */ 0.0 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
325- }
326- else if (_Matrix == Matrix_e::AVS_MATRIX_ICTCP) {
327- // not supported REC_2100_LMS
328- return false ;
329- }
330- else if (_Matrix == Matrix_e::AVS_MATRIX_YCGCO) {
331- // not supported
332- return false ;
333- }
334- else {
316+ if (_Matrix == Matrix_e::AVS_MATRIX_ICTCP || _Matrix == Matrix_e::AVS_MATRIX_YCGCO)
317+ return false ; // not supported
318+
319+ double Kr, Kb;
320+ if (!GetKrKb (_Matrix, Kr, Kb))
335321 return false ;
336- }
322+
323+ BuildMatrix_Rgb2Yuv_core (Kr, Kb, int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
337324 return true ;
338325}
339326
@@ -347,37 +334,18 @@ bool do_BuildMatrix_Yuv2Rgb(int _Matrix, int _ColorRange, int _ColorRange_Out, i
347334 const bool is_full_s = _ColorRange == ColorRange_e::AVS_RANGE_FULL;
348335 const bool is_full_d = _ColorRange_Out == ColorRange_e::AVS_RANGE_FULL;
349336
350- if (_Matrix == Matrix_e::AVS_MATRIX_BT470_BG || _Matrix == Matrix_e::AVS_MATRIX_ST170_M) { // 601
351- BuildMatrix_Yuv2Rgb_core (0.299 , /* 0.587 */ 0.114 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
352- }
353- else if (_Matrix == Matrix_e::AVS_MATRIX_BT709) {
354- BuildMatrix_Yuv2Rgb_core (0.2126 , /* 0.7152 */ 0.0722 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
355- }
356- else if (_Matrix == Matrix_e::AVS_MATRIX_AVERAGE) { // non-standard!
357- BuildMatrix_Yuv2Rgb_core (1.0 / 3 , /* 1.0/3 */ 1.0 / 3 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
358- }
359- else if (_Matrix == Matrix_e::AVS_MATRIX_BT2020_CL || _Matrix == Matrix_e::AVS_MATRIX_BT2020_NCL) {
360- BuildMatrix_Yuv2Rgb_core (0.2627 , /* 0.6780 */ 0.0593 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
361- }
362- else if (_Matrix == Matrix_e::AVS_MATRIX_BT470_M) {
363- BuildMatrix_Yuv2Rgb_core (0.3 , /* 0.59 */ 0.11 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
364- }
365- else if (_Matrix == Matrix_e::AVS_MATRIX_ST240_M) {
366- BuildMatrix_Yuv2Rgb_core (0.212 , /* 0.701 */ 0.087 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
367- }
368- else if (_Matrix == Matrix_e::AVS_MATRIX_RGB) {
369- BuildMatrix_Yuv2Rgb_core (0.0 , /* */ 0.0 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
337+ // Special cases not handled by GetKrKb
338+ if (_Matrix == Matrix_e::AVS_MATRIX_RGB) {
339+ BuildMatrix_Yuv2Rgb_core (0.0 , 0.0 , int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
340+ return true ;
370341 }
371- else if (_Matrix == Matrix_e::AVS_MATRIX_ICTCP) {
372- // not supported REC_2100_LMS
373- return false ;
374- }
375- else if (_Matrix == Matrix_e::AVS_MATRIX_YCGCO) {
376- // not supported
377- return false ;
378- }
379- else {
342+ if (_Matrix == Matrix_e::AVS_MATRIX_ICTCP || _Matrix == Matrix_e::AVS_MATRIX_YCGCO)
343+ return false ; // not supported
344+
345+ double Kr, Kb;
346+ if (!GetKrKb (_Matrix, Kr, Kb))
380347 return false ;
381- }
348+
349+ BuildMatrix_Yuv2Rgb_core (Kr, Kb, int_arith_shift, is_full_s, is_full_d, bits_per_pixel, matrix);
382350 return true ;
383351}
0 commit comments