codec: vpx: do proper color mapping
authorFrancois Cartegnie <fcvlcdev@free.fr>
Wed, 8 Aug 2018 15:26:32 +0000 (17:26 +0200)
committerFrancois Cartegnie <fcvlcdev@free.fr>
Wed, 8 Aug 2018 15:26:32 +0000 (17:26 +0200)
modules/codec/vpx.c

index 0d0eae0..7c111e6 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <vpx/vpx_decoder.h>
 #include <vpx/vp8dx.h>
+#include <vpx/vpx_image.h>
 
 #ifdef ENABLE_SOUT
 # include <vpx/vpx_encoder.h>
@@ -143,6 +144,41 @@ static const struct
     { VLC_CODEC_I444_16L, VPX_IMG_FMT_I44416, 16, 0 },
 };
 
+struct video_color
+{
+    video_color_primaries_t primaries;
+    video_transfer_func_t transfer;
+    video_color_space_t space;
+};
+
+const struct video_color vpx_color_mapping_table[] =
+{
+    [VPX_CS_UNKNOWN]   =  { COLOR_PRIMARIES_UNDEF,
+                            TRANSFER_FUNC_UNDEF,
+                            COLOR_SPACE_UNDEF },
+    [VPX_CS_BT_601]    =  { COLOR_PRIMARIES_BT601_525,
+                            TRANSFER_FUNC_BT709,
+                            COLOR_SPACE_BT601 },
+    [VPX_CS_BT_709]    =  { COLOR_PRIMARIES_BT709,
+                            TRANSFER_FUNC_BT709,
+                            COLOR_SPACE_BT709 },
+    [VPX_CS_SMPTE_170] =  { COLOR_PRIMARIES_SMTPE_170,
+                            TRANSFER_FUNC_BT709,
+                            COLOR_SPACE_BT601 },
+    [VPX_CS_SMPTE_240] =  { COLOR_PRIMARIES_SMTPE_240,
+                            TRANSFER_FUNC_SMPTE_240,
+                            COLOR_SPACE_UNDEF },
+    [VPX_CS_BT_2020]   =  { COLOR_PRIMARIES_BT2020,
+                            TRANSFER_FUNC_BT2020,
+                            COLOR_SPACE_BT2020 },
+    [VPX_CS_RESERVED]  =  { COLOR_PRIMARIES_UNDEF,
+                            TRANSFER_FUNC_UNDEF,
+                            COLOR_SPACE_UNDEF },
+    [VPX_CS_SRGB]      =  { COLOR_PRIMARIES_SRGB,
+                            TRANSFER_FUNC_SRGB,
+                            COLOR_SPACE_UNDEF },
+};
+
 static vlc_fourcc_t FindVlcChroma( struct vpx_image *img )
 {
     uint8_t hack = (img->fmt & VPX_IMG_FMT_I444) && (img->cs == VPX_CS_SRGB);
@@ -227,24 +263,13 @@ static int Decode(decoder_t *dec, block_t *block)
         dec->fmt_out.video.i_sar_den = 1;
     }
 
-    v->b_color_range_full = img->range == VPX_CR_FULL_RANGE;
-
-    switch( img->cs )
+    if(dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF &&
+       img->cs >= 0 && img->cs < ARRAY_SIZE(vpx_color_mapping_table))
     {
-        case VPX_CS_SRGB:
-        case VPX_CS_BT_709:
-            v->space = COLOR_SPACE_BT709;
-            break;
-        case VPX_CS_BT_601:
-        case VPX_CS_SMPTE_170:
-        case VPX_CS_SMPTE_240:
-            v->space = COLOR_SPACE_BT601;
-            break;
-        case VPX_CS_BT_2020:
-            v->space = COLOR_SPACE_BT2020;
-            break;
-        default:
-            break;
+        v->primaries = vpx_color_mapping_table[img->cs].primaries;
+        v->transfer = vpx_color_mapping_table[img->cs].transfer;
+        v->space = vpx_color_mapping_table[img->cs].space;
+        v->b_color_range_full = img->range == VPX_CR_FULL_RANGE;
     }
 
     dec->fmt_out.video.projection_mode = dec->fmt_in.video.projection_mode;