[FFmpeg-devel] libavcodec/libopenh264enc: Save FFmpeg colorspace info in openh264 video files.

Submitted by Gregory J. Wolfe on Dec. 14, 2016, 7:47 p.m.

Details

Message ID 1481744869-8220-1-git-send-email-gregory.wolfe@kodakalaris.com
State New
Headers show

Commit Message

Gregory J. Wolfe Dec. 14, 2016, 7:47 p.m.
As of version 1.6, libopenh264 saves (in the output video file)
information about the color primaries, transfer characteristics,
and color matrix used when the video pixel data was created.
This patch sets the required libopenh264 data structures using
the FFmpeg colorspace information so that video players will
know how to properly decode video files created using FFmpeg
and libopenh264.

Signed-off-by: Gregory J. Wolfe <gregory.wolfe@kodakalaris.com>
---
 libavcodec/libopenh264enc.c | 61 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

Patch hide | download patch | download mbox

diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c
index e84de27..d8a7ea3 100644
--- a/libavcodec/libopenh264enc.c
+++ b/libavcodec/libopenh264enc.c
@@ -205,6 +205,67 @@  FF_ENABLE_DEPRECATION_WARNINGS
         }
     }
 
+#if OPENH264_VER_AT_LEAST(1, 6)
+    // set video signal type information
+    param.sSpatialLayers[0].bVideoSignalTypePresent = true;
+    param.sSpatialLayers[0].uiVideoFormat = VF_UNDEF; // default; choices are VF_: COMPONENT, PAL, NTSC, SECAM, MAC, UNDEF
+    param.sSpatialLayers[0].bFullRange = avctx->color_range == AVCOL_RANGE_JPEG;
+    param.sSpatialLayers[0].bColorDescriptionPresent = true;
+    // These switches are intended to filter out all but the values supported by libopenh264.
+    // An unsupported value causes the associated quantity to be set to "unspecified" and a
+    // warning message to be issued.
+    switch (avctx->color_primaries) {
+    case AVCOL_PRI_BT709:        param.sSpatialLayers[0].uiColorPrimaries          = CP_BT709;         break;
+    case AVCOL_PRI_UNSPECIFIED:  param.sSpatialLayers[0].uiColorPrimaries          = CP_UNDEF;         break;
+    case AVCOL_PRI_BT470M:       param.sSpatialLayers[0].uiColorPrimaries          = CP_BT470M;        break;
+    case AVCOL_PRI_BT470BG:      param.sSpatialLayers[0].uiColorPrimaries          = CP_BT470BG;       break;
+    case AVCOL_PRI_SMPTE170M:    param.sSpatialLayers[0].uiColorPrimaries          = CP_SMPTE170M;     break;
+    case AVCOL_PRI_SMPTE240M:    param.sSpatialLayers[0].uiColorPrimaries          = CP_SMPTE240M;     break;
+    case AVCOL_PRI_FILM:         param.sSpatialLayers[0].uiColorPrimaries          = CP_FILM;          break;
+    case AVCOL_PRI_BT2020:       param.sSpatialLayers[0].uiColorPrimaries          = CP_BT2020;        break;
+    default:                     param.sSpatialLayers[0].uiColorPrimaries          = CP_UNDEF;
+        av_log(avctx, AV_LOG_WARNING, "Unsupported color primaries value %d was specified;" 
+            " color primaries value has been set to \"unspecified\"\n", avctx->color_primaries);
+        break;
+    }
+    switch (avctx->color_trc) {
+    case AVCOL_TRC_BT709:        param.sSpatialLayers[0].uiTransferCharacteristics = TRC_BT709;        break;
+    case AVCOL_TRC_UNSPECIFIED:  param.sSpatialLayers[0].uiTransferCharacteristics = TRC_UNDEF;        break;
+    case AVCOL_TRC_GAMMA22:      param.sSpatialLayers[0].uiTransferCharacteristics = TRC_BT470M;       break;
+    case AVCOL_TRC_GAMMA28:      param.sSpatialLayers[0].uiTransferCharacteristics = TRC_BT470BG;      break;
+    case AVCOL_TRC_SMPTE170M:    param.sSpatialLayers[0].uiTransferCharacteristics = TRC_SMPTE170M;    break;
+    case AVCOL_TRC_SMPTE240M:    param.sSpatialLayers[0].uiTransferCharacteristics = TRC_SMPTE240M;    break;
+    case AVCOL_TRC_LINEAR:       param.sSpatialLayers[0].uiTransferCharacteristics = TRC_LINEAR;       break;
+    case AVCOL_TRC_LOG:          param.sSpatialLayers[0].uiTransferCharacteristics = TRC_LOG100;       break;
+    case AVCOL_TRC_LOG_SQRT:     param.sSpatialLayers[0].uiTransferCharacteristics = TRC_LOG316;       break;
+    case AVCOL_TRC_IEC61966_2_4: param.sSpatialLayers[0].uiTransferCharacteristics = TRC_IEC61966_2_4; break;
+    case AVCOL_TRC_BT1361_ECG:   param.sSpatialLayers[0].uiTransferCharacteristics = TRC_BT1361E;      break;
+    case AVCOL_TRC_IEC61966_2_1: param.sSpatialLayers[0].uiTransferCharacteristics = TRC_IEC61966_2_1; break;
+    case AVCOL_TRC_BT2020_10:    param.sSpatialLayers[0].uiTransferCharacteristics = TRC_BT2020_10;    break;
+    case AVCOL_TRC_BT2020_12:    param.sSpatialLayers[0].uiTransferCharacteristics = TRC_BT2020_12;    break;
+    default:                     param.sSpatialLayers[0].uiTransferCharacteristics = TRC_UNDEF;
+        av_log(avctx, AV_LOG_WARNING, "Unsupported transfer characteristics value %d was specified;" 
+            " transfer characteristics value has been set to \"unspecified\"\n", avctx->color_trc);
+        break;
+    }
+    switch (avctx->colorspace) {
+    case AVCOL_SPC_RGB:          param.sSpatialLayers[0].uiColorMatrix             = CM_GBR;           break;
+    case AVCOL_SPC_BT709:        param.sSpatialLayers[0].uiColorMatrix             = CM_BT709;         break;
+    case AVCOL_SPC_UNSPECIFIED:  param.sSpatialLayers[0].uiColorMatrix             = CM_UNDEF;         break;
+    case AVCOL_SPC_FCC:          param.sSpatialLayers[0].uiColorMatrix             = CM_FCC;           break;
+    case AVCOL_SPC_BT470BG:      param.sSpatialLayers[0].uiColorMatrix             = CM_BT470BG;       break;
+    case AVCOL_SPC_SMPTE170M:    param.sSpatialLayers[0].uiColorMatrix             = CM_SMPTE170M;     break;
+    case AVCOL_SPC_SMPTE240M:    param.sSpatialLayers[0].uiColorMatrix             = CM_SMPTE240M;     break;
+    case AVCOL_SPC_YCOCG:        param.sSpatialLayers[0].uiColorMatrix             = CM_YCGCO;         break;
+    case AVCOL_SPC_BT2020_NCL:   param.sSpatialLayers[0].uiColorMatrix             = CM_BT2020NC;      break;
+    case AVCOL_SPC_BT2020_CL:    param.sSpatialLayers[0].uiColorMatrix             = CM_BT2020C;       break;
+    default:                     param.sSpatialLayers[0].uiColorMatrix             = CM_UNDEF;
+        av_log(avctx, AV_LOG_WARNING, "Unsupported color matrix coefficients value %d was specified;" 
+            " color matrix coefficients value has been set to \"unspecified\"\n", avctx->colorspace);
+         break;
+    }
+#endif
+
     if ((*s->encoder)->InitializeExt(s->encoder, &param) != cmResultSuccess) {
         av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
         return AVERROR_UNKNOWN;