FFMPEG一些color属性处理

FFMPEG一些color属性

ffmpeg color有四个属性color space、transfer function、primaries、range,四个属性,定义主要见libavutils/pixfmt.h中,以AVCOL_前缀的几个enum类型。

1, color space: YUV是基于RGB的颜色空间,color space属性是决定了YUV2RGB/RGB2YUV的转换计算matrix
2, color primaries:基于的RGB空间对应的绝对颜色XYZ的变换,决定了最终三原色RGB分别是什么颜色
3, color transfer:定义了transfer funciton的gamma值,从RGB到最终显示的值需要进行gamma压暗,比如bt709的平均gamma为1.96
4,color range:pc range和tv range(又叫video range和full range),full range中YUV的取值[0-255],video range中Y[16-235],UV [16-240]

这4个值默认都是UnSpecified,这种情况下对于不同的播放器解释行为不太一致,要注意一下,可能会造成不同播放器下的颜色不一致,建议是已知的color属性在播放、转码的时候切记都要考虑!

shaders相关

移动端中在播放视频的时候需要YUV转RGB,直接拿SwsContext转太慢了,可以使用shader来完成,需要注意转换矩阵问题:

1,YUV2RGB
// Reference http://www.equasys.de/colorconversion.html
static const GLfloat kBt709VideoRangeYUV2RGBMatrix[] = {
  1.164,  1.164,  1.164,
  0.0,   -0.213,  2.112,
  1.793, -0.533,  0.0,
};

// not found bt709 full range matrix, identical with video range
static const GLfloat kBt709FullRangeYUV2RGBMatrix[] = {
  1.164,  1.164,  1.164,
  0.0,   -0.213,  2.112,
  1.793, -0.533,  0.0,
};

static const GLfloat kBt601VideoRangeYUV2RGBMatrix[] = {
  1.164,  1.164, 1.164,
  0.0,   -0.392, 2.017,
  1.596, -0.813, 0.0,
};

static const GLfloat kBt601FullRangeYUV2RGBMatrix[] = {
  1.0, 1.0, 1.0,
  0.0, -0.343, 1.765,
  1.4, -0.711, 0.0,
};

static const char* kGlvsYuv420pToRgb =
    "    // Vertex Shader                                                                \n"
    "    precision highp float;                                                           \n"
    "    attribute highp vec4 a_position;                                                      \n"
    "    attribute highp vec2 a_texCoord0;                                                     \n"
    "    varying highp vec2 v_texCoord0;                                                       \n"
    "    void main(void)                                                                 \n"
    "    {                                                                               \n"
    "        gl_Position = a_position;                                                   \n"
    "        v_texCoord0 = vec2(a_texCoord0.x,1.0-a_texCoord0.y);                        \n"
    "    }                                                                               \n";


static const char* kGlfsYuv420pToRgb =
    "    // Pixel Shader                                                                 \n"
    "    precision highp float;                                                           \n"
    "    uniform lowp sampler2D  ImageInput0    ;                                             \n"
    "    uniform lowp sampler2D  ImageInput1    ;                                             \n"
    "    uniform lowp sampler2D  ImageInput2    ;                                             \n"
    "    varying highp vec2 v_texCoord0;                                                       \n"
    "    uniform float y_offset;                                                         \n"
    "    uniform mat3 um3_ColorConversion;                            \n"
    "                                                                                    \n"
    "    void main(void)                                                                 \n"
    "    {                                                                               \n"
    "        mediump vec3 yuv;                                                           \n"
    "        lowp vec3 rgb;                                                              \n"
    "        yuv.x = texture2D(ImageInput0, v_texCoord0.xy).r - (y_offset / 255.0);      \n"
    "        yuv.y = texture2D(ImageInput1, v_texCoord0.xy).r - 0.5;                     \n"
    "        yuv.z = texture2D(ImageInput2, v_texCoord0.xy).r - 0.5;                     \n"
    "        rgb = um3_ColorConversion * yuv;                      \n"
    "                                                          \n"
    "        gl_FragColor = vec4(rgb, 1.0);                                             \n"
    "    }                                                                               \n";
}  // namespace

2,RGB2YUV
static const GLfloat kBt709VideoRangeRGB2YUVMatrix[] = {
  0.183,  -0.101, 0.439,
  0.614, -0.339, -0.399,
  0.062, 0.439, -0.040,
};

// Not found bt709 full range, identical as video range for now
static const GLfloat kBt709FullRangeRGB2YUVMatrix[] = {
  0.183,  -0.101, 0.439,
  0.614, -0.339, -0.399,
  0.062, 0.439, -0.040,
};

static const GLfloat kBt601VideoRangeRGB2YUVMatrix[] = {
  0.257, -0.148, 0.439,
  0.504, -0.291, -0.368,
  0.098, 0.439, -0.071,
};

static const GLfloat kBt601FullRangeRGB2YUVMatrix[] = {
  0.299, -0.169, 0.500,
  0.587, -0.331, -0.419,
  0.114, 0.500, -0.081,
};

// Reference:   http://www.equasys.de/colorconversion.html
static const char* kGlfsRgbToYuv420p =
    "    // Pixel Shader                                                                 \n"
    "    precision highp float;                                                           \n"                                               
    "    uniform sampler2D  ImageSampler;                                             \n"
    "    varying vec2 v_texCoord0;                                                       \n"
    "    uniform   mat3 um3_ColorConversion;                                             \n"
    "    uniform   float y_offset;                                                        \n"
    "                                                                                    \n"
    "    void main(void)                                                                 \n"
    "    {                                                                               \n"
    "        lowp vec3 yuv;                                                               \n"
    "                                                                                   \n"
    "        vec4 rgba = texture2D(ImageSampler, v_texCoord0.xy);                         \n"
    "        yuv = um3_ColorConversion * rgba.rgb;                                              \n"
    "        yuv.r = yuv.r + (y_offset / 255.0);                                                  \n"
    "        yuv.g = yuv.g + 0.5;            \n"
    "        yuv.b = yuv.b + 0.5;             \n "
    "        gl_FragColor = vec4(yuv, 1.0);                                          \n"
    "    }                                                                               \n";

VideoToolBox相关

使用VT来硬解视频的时候支持直接输出RGB(kCVPixelFormatType_32BGRA等RGB空间的输出),也能够使用硬件加速,不过要注意color相关问题,否则会偏色。ffmpeg中和VT中对于color一些属性的name方式不一样。

 if (avctx->color_primaries == AVCOL_PRI_BT709) {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Primaries to BT709\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_ColorPrimaries,
                             kCMFormatDescriptionColorPrimaries_ITU_R_709_2);
    } else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M
               || avctx->color_primaries == AVCOL_PRI_SMPTE240M) {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Primaries to SMPTE_C\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_ColorPrimaries,
                             kCMFormatDescriptionColorPrimaries_SMPTE_C);
    } else if (avctx->color_primaries == AVCOL_SPC_BT470BG) {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Primaries to EBU_3213\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_ColorPrimaries,
                             kCMFormatDescriptionColorPrimaries_EBU_3213);
    } else {
        av_log(avctx, AV_LOG_INFO, "Did not set VideoToolbox Color Primaries for %d\n", avctx->color_primaries);
    }

    if (avctx->color_trc == AVCOL_TRC_BT709
        || avctx->color_trc == AVCOL_TRC_BT2020_10
        || avctx->color_trc == AVCOL_TRC_BT2020_12)
    {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Transfer Function to BT709\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_TransferFunction,
                             kCMFormatDescriptionTransferFunction_ITU_R_709_2);
    } else if (avctx->color_trc == AVCOL_TRC_SMPTE170M || avctx->color_trc == AVCOL_TRC_SMPTE240M) {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Transfer Function to SMPTE_240M_1995\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_TransferFunction,
                             kCMFormatDescriptionTransferFunction_SMPTE_240M_1995);
    } else {
        av_log(avctx, AV_LOG_INFO, "Did not set VideoToolbox Color Transfer Function for %d\n", avctx->color_trc);
    }


    if (avctx->colorspace == AVCOL_SPC_BT709) {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Space to BT709\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_YCbCrMatrix,
                             kCMFormatDescriptionYCbCrMatrix_ITU_R_709_2);
    } else if (avctx->colorspace == AVCOL_SPC_SMPTE170M || avctx->colorspace == AVCOL_SPC_BT470BG) {  
        // TODO: not sure for now whether AVCOL_SPC_BT470BG use 601_4
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Space to ITU_R_601_4\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_YCbCrMatrix,
                             kCMFormatDescriptionYCbCrMatrix_ITU_R_601_4);
    } else if (avctx->colorspace == AVCOL_SPC_SMPTE240M) {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Space to 240M_1995\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_YCbCrMatrix,
                             kCMFormatDescriptionYCbCrMatrix_SMPTE_240M_1995);
    } else {
        av_log(avctx, AV_LOG_INFO, "Did not set VideoToolbox Color Space for %d\n", avctx->colorspace);
    }

    if (avctx->color_range == AVCOL_RANGE_JPEG) {
        av_log(avctx, AV_LOG_INFO, "Setting VideoToolbox Color Range to full range\n");
        CFDictionarySetValue(config_info,
                             kCMFormatDescriptionExtension_FullRangeVideo,
                             kCFBooleanTrue);
    } else {
        av_log(avctx, AV_LOG_INFO, "Did not set VideoToolbox Color Range, will use video-range by default\n");
    }

你可能感兴趣的:(FFMPEG一些color属性处理)