Skip to content

Commit

Permalink
libhb: change preset key to VideoColorMatrixCodeOverride
Browse files Browse the repository at this point in the history
Reserve VideoColorMatrixCode for doing color conversion.
VideoColorMatrixCodeOverride sets color flags without transforming video
frame colors.
  • Loading branch information
jstebbins committed Apr 8, 2019
1 parent be58183 commit 219b254
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 190 deletions.
27 changes: 27 additions & 0 deletions libhb/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3791,6 +3791,9 @@ static void job_setup(hb_job_t * job, hb_title_t * title)
job->color_transfer = title->color_transfer;
job->color_matrix = title->color_matrix;
job->color_range = title->color_range;
job->color_prim_override = HB_COLR_PRI_UNDEF;
job->color_transfer_override = HB_COLR_TRA_UNDEF;
job->color_matrix_override = HB_COLR_MAT_UNDEF;

job->mux = HB_MUX_MP4;

Expand All @@ -3809,6 +3812,30 @@ static void job_setup(hb_job_t * job, hb_title_t * title)
#endif
}

int hb_output_color_prim(hb_job_t * job)
{
if (job->color_prim_override != HB_COLR_PRI_UNDEF)
return job->color_prim_override;
else
return job->color_prim;
}

int hb_output_color_transfer(hb_job_t * job)
{
if (job->color_transfer_override != HB_COLR_TRA_UNDEF)
return job->color_transfer_override;
else
return job->color_transfer;
}

int hb_output_color_matrix(hb_job_t * job)
{
if (job->color_matrix_override != HB_COLR_MAT_UNDEF)
return job->color_matrix_override;
else
return job->color_matrix;
}

static void job_clean( hb_job_t * job )
{
if (job)
Expand Down
8 changes: 8 additions & 0 deletions libhb/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ struct hb_job_s
int color_transfer;
int color_matrix;
int color_range;

int color_prim_override;
int color_transfer_override;
int color_matrix_override;
// see https://developer.apple.com/library/content/technotes/tn2162/_index.html
// https://developer.apple.com/library/content/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html#//apple_ref/doc/uid/TP40000939-CH205-125526
// libav pixfmt.h
Expand Down Expand Up @@ -1432,6 +1436,10 @@ const char * hb_x264_encopt_name( const char * name );
const char * hb_x265_encopt_name( const char * name );
#endif

int hb_output_color_prim(hb_job_t * job);
int hb_output_color_transfer(hb_job_t * job);
int hb_output_color_matrix(hb_job_t * job);

#define HB_NEG_FLOAT_REG "(([-])?(([0-9]+([.,][0-9]+)?)|([.,][0-9]+))"
#define HB_FLOAT_REG "(([0-9]+([.,][0-9]+)?)|([.,][0-9]+))"
#define HB_NEG_INT_REG "(([-]?[0-9]+)"
Expand Down
44 changes: 42 additions & 2 deletions libhb/decavcodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1909,12 +1909,23 @@ static int get_color_prim(int color_primaries, hb_geometry_t geometry, hb_ration
{
case AVCOL_PRI_BT709:
return HB_COLR_PRI_BT709;
case AVCOL_PRI_BT470M:
return HB_COLR_PRI_BT470M;
case AVCOL_PRI_BT470BG:
return HB_COLR_PRI_EBUTECH;
case AVCOL_PRI_BT470M:
case AVCOL_PRI_SMPTE170M:
case AVCOL_PRI_SMPTE240M:
return HB_COLR_PRI_SMPTEC;
case AVCOL_PRI_FILM:
return HB_COLR_PRI_FILM;
case AVCOL_PRI_SMPTE428:
return HB_COLR_PRI_SMPTE428;
case AVCOL_PRI_SMPTE431:
return HB_COLR_PRI_SMPTE431;
case AVCOL_PRI_SMPTE432:
return HB_COLR_PRI_SMPTE432;
case AVCOL_PRI_JEDEC_P22:
return HB_COLR_PRI_JEDEC_P22;
case AVCOL_PRI_BT2020:
return HB_COLR_PRI_BT2020;
default:
Expand All @@ -1937,6 +1948,24 @@ static int get_color_transfer(int color_trc)
{
switch (color_trc)
{
case AVCOL_TRC_GAMMA22:
return HB_COLR_TRA_GAMMA22;
case AVCOL_TRC_GAMMA28:
return HB_COLR_TRA_GAMMA28;
case AVCOL_TRC_SMPTE170M:
return HB_COLR_TRA_SMPTE170M;
case AVCOL_TRC_LINEAR:
return HB_COLR_TRA_LINEAR;
case AVCOL_TRC_LOG:
return HB_COLR_TRA_LOG;
case AVCOL_TRC_LOG_SQRT:
return HB_COLR_TRA_LOG_SQRT;
case AVCOL_TRC_IEC61966_2_4:
return HB_COLR_TRA_IEC61966_2_4;
case AVCOL_TRC_BT1361_ECG:
return HB_COLR_TRA_BT1361_ECG;
case AVCOL_TRC_IEC61966_2_1:
return HB_COLR_TRA_IEC61966_2_1;
case AVCOL_TRC_SMPTE240M:
return HB_COLR_TRA_SMPTE240M;
case AVCOL_TRC_SMPTEST2084:
Expand All @@ -1957,19 +1986,30 @@ static int get_color_matrix(int colorspace, hb_geometry_t geometry)
{
switch (colorspace)
{
case AVCOL_SPC_RGB:
return HB_COLR_MAT_RGB;
case AVCOL_SPC_BT709:
return HB_COLR_MAT_BT709;
case AVCOL_SPC_FCC:
return HB_COLR_MAT_FCC;
case AVCOL_SPC_BT470BG:
return HB_COLR_MAT_BT470BG;
case AVCOL_SPC_SMPTE170M:
case AVCOL_SPC_RGB: // libswscale rgb2yuv
return HB_COLR_MAT_SMPTE170M;
case AVCOL_SPC_SMPTE240M:
return HB_COLR_MAT_SMPTE240M;
case AVCOL_SPC_YCGCO:
return HB_COLR_MAT_YCGCO;
case AVCOL_SPC_BT2020_NCL:
return HB_COLR_MAT_BT2020_NCL;
case AVCOL_SPC_BT2020_CL:
return HB_COLR_MAT_BT2020_CL;
case AVCOL_SPC_CHROMA_DERIVED_NCL:
return HB_COLR_MAT_CD_NCL;
case AVCOL_SPC_CHROMA_DERIVED_CL:
return HB_COLR_MAT_CD_CL;
case AVCOL_SPC_ICTCP:
return HB_COLR_MAT_ICTCP;
default:
{
if ((geometry.width >= 1280 || geometry.height >= 720)||
Expand Down
12 changes: 6 additions & 6 deletions libhb/enc_qsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ int encqsvInit(hb_work_object_t *w, hb_job_t *job)
pv->param.videoParam->AsyncDepth = job->qsv.async_depth;

// set and enable colorimetry (video signal information)
pv->param.videoSignalInfo.ColourPrimaries = job->color_prim;
pv->param.videoSignalInfo.TransferCharacteristics = job->color_transfer;
pv->param.videoSignalInfo.MatrixCoefficients = job->color_matrix;
pv->param.videoSignalInfo.ColourPrimaries = hb_output_color_prim(job);
pv->param.videoSignalInfo.TransferCharacteristics = hb_output_color_transfer(job);
pv->param.videoSignalInfo.MatrixCoefficients = hb_output_color_matrix(job);
pv->param.videoSignalInfo.ColourDescriptionPresent = 1;

// parse user-specified encoder options, if present
Expand Down Expand Up @@ -764,9 +764,9 @@ int encqsvInit(hb_work_object_t *w, hb_job_t *job)
// reload colorimetry in case values were set in encoder_options
if (pv->param.videoSignalInfo.ColourDescriptionPresent)
{
job->color_prim = pv->param.videoSignalInfo.ColourPrimaries;
job->color_transfer = pv->param.videoSignalInfo.TransferCharacteristics;
job->color_matrix = pv->param.videoSignalInfo.MatrixCoefficients;
job->color_prim_override = pv->param.videoSignalInfo.ColourPrimaries;
job->color_transfer_override = pv->param.videoSignalInfo.TransferCharacteristics;
job->color_matrix_override = pv->param.videoSignalInfo.MatrixCoefficients;
}

// sanitize values that may exceed the Media SDK variable size
Expand Down
14 changes: 11 additions & 3 deletions libhb/encavcodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,9 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
job->par.num, job->par.den );

// set colorimetry
context->color_primaries = job->color_prim;
context->color_trc = job->color_transfer;
context->colorspace = job->color_matrix;
context->color_primaries = hb_output_color_prim(job);
context->color_trc = hb_output_color_transfer(job);
context->colorspace = hb_output_color_matrix(job);

if (!job->inline_parameter_sets)
{
Expand Down Expand Up @@ -626,6 +626,14 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
goto done;
}

/*
* Reload colorimetry settings in case custom
* values were set in the encoder_options string.
*/
job->color_prim_override = context->color_primaries;
job->color_transfer_override = context->color_trc;
job->color_matrix_override = context->colorspace;

if (job->pass_id == HB_PASS_ENCODE_1ST &&
context->stats_out != NULL)
{
Expand Down
12 changes: 6 additions & 6 deletions libhb/encx264.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )

/* set up the VUI color model & gamma to match what the COLR atom
* set in muxmp4.c says. See libhb/muxmp4.c for notes. */
param.vui.i_colorprim = job->color_prim;
param.vui.i_transfer = job->color_transfer;
param.vui.i_colmatrix = job->color_matrix;
param.vui.i_colorprim = hb_output_color_prim(job);
param.vui.i_transfer = hb_output_color_transfer(job);
param.vui.i_colmatrix = hb_output_color_matrix(job);

/* place job->encoder_options in an hb_dict_t for convenience */
hb_dict_t * x264_opts = NULL;
Expand Down Expand Up @@ -425,9 +425,9 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )

/* Reload colorimetry settings in case custom values were set
* in the encoder_options string */
job->color_prim = param.vui.i_colorprim;
job->color_transfer = param.vui.i_transfer;
job->color_matrix = param.vui.i_colmatrix;
job->color_prim_override = param.vui.i_colorprim;
job->color_transfer_override = param.vui.i_transfer;
job->color_matrix_override = param.vui.i_colmatrix;

/* For 25 fps sources, HandBrake's explicit keyints will match the x264 defaults:
* min-keyint 25 (same as auto), keyint 250. */
Expand Down
12 changes: 6 additions & 6 deletions libhb/encx265.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ int encx265Init(hb_work_object_t *w, hb_job_t *job)
* flags, if any, should be set in the x265_param struct).
*/
char colorprim[11], transfer[11], colormatrix[11];
snprintf(colorprim, sizeof(colorprim), "%d", job->color_prim);
snprintf(transfer, sizeof(transfer), "%d", job->color_transfer);
snprintf(colormatrix, sizeof(colormatrix), "%d", job->color_matrix);
snprintf(colorprim, sizeof(colorprim), "%d", hb_output_color_prim(job));
snprintf(transfer, sizeof(transfer), "%d", hb_output_color_transfer(job));
snprintf(colormatrix, sizeof(colormatrix), "%d", hb_output_color_matrix(job));

if (param_parse(pv, param, "colorprim", colorprim) ||
param_parse(pv, param, "transfer", transfer) ||
Expand Down Expand Up @@ -190,9 +190,9 @@ int encx265Init(hb_work_object_t *w, hb_job_t *job)
* Reload colorimetry settings in case custom
* values were set in the encoder_options string.
*/
job->color_prim = param->vui.colorPrimaries;
job->color_transfer = param->vui.transferCharacteristics;
job->color_matrix = param->vui.matrixCoeffs;
job->color_prim_override = param->vui.colorPrimaries;
job->color_transfer_override = param->vui.transferCharacteristics;
job->color_matrix_override = param->vui.matrixCoeffs;

/*
* Settings which can't be overridden in the encodeer_options string
Expand Down
30 changes: 26 additions & 4 deletions libhb/hb_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,21 @@ hb_dict_t* hb_job_to_dict( const hb_job_t * job )
hb_value_int(job->color_transfer));
hb_dict_set(video_dict, "ColorMatrix",
hb_value_int(job->color_matrix));
if (job->color_prim_override != HB_COLR_PRI_UNDEF)
{
hb_dict_set(video_dict, "ColorPrimariesOverride",
hb_value_int(job->color_prim_override));
}
if (job->color_transfer_override != HB_COLR_TRA_UNDEF)
{
hb_dict_set(video_dict, "ColorTransferOverride",
hb_value_int(job->color_transfer_override));
}
if (job->color_matrix_override != HB_COLR_MAT_UNDEF)
{
hb_dict_set(video_dict, "ColorMatrixOverride",
hb_value_int(job->color_matrix_override));
}

if (job->vquality > HB_INVALID_VIDEO_QUALITY)
{
Expand Down Expand Up @@ -1010,12 +1025,16 @@ hb_job_t* hb_dict_to_job( hb_handle_t * h, hb_dict_t *dict )
// PAR {Num, Den}
"s?{s:i, s:i},"
// Video {Codec, Quality, Bitrate, Preset, Tune, Profile, Level, Options
// TwoPass, Turbo, ColorFormat, ColorRange, ColorPrimaries,
// ColorTransfer, ColorMatrix,
// QSV {Decode, AsyncDepth}}
// TwoPass, Turbo,
// ColorFormat, ColorRange,
// ColorPrimaries, ColorTransfer, ColorMatrix,
// ColorPrimariesOverride, ColorTransferOverride, ColorMatrixOverride,
// QSV {Decode, AsyncDepth}}
"s:{s:o, s?f, s?i, s?s, s?s, s?s, s?s, s?s,"
" s?b, s?b, s?i, s?i, s?i,"
" s?b, s?b,"
" s?i, s?i,"
" s?i, s?i, s?i,"
" s?i, s?i, s?i,"
" s?{s?b, s?i}},"
// Audio {CopyMask, FallbackEncoder, AudioList}
"s?{s?o, s?o, s?o},"
Expand Down Expand Up @@ -1064,6 +1083,9 @@ hb_job_t* hb_dict_to_job( hb_handle_t * h, hb_dict_t *dict )
"ColorPrimaries", unpack_i(&job->color_prim),
"ColorTransfer", unpack_i(&job->color_transfer),
"ColorMatrix", unpack_i(&job->color_matrix),
"ColorPrimariesOverride", unpack_i(&job->color_prim_override),
"ColorTransferOverride", unpack_i(&job->color_transfer_override),
"ColorMatrixOverride", unpack_i(&job->color_matrix_override),
"QSV",
"Decode", unpack_b(&job->qsv.decode),
"AsyncDepth", unpack_i(&job->qsv.async_depth),
Expand Down
11 changes: 7 additions & 4 deletions libhb/preset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ int hb_preset_apply_video(const hb_dict_t *preset, hb_dict_t *job_dict)
video_dict = hb_dict_get(job_dict, "Video");
hb_dict_set(video_dict, "Encoder", hb_value_string(encoder->short_name));

color_matrix_code = hb_value_get_int(hb_dict_get(preset, "VideoColorMatrixCode"));
color_matrix_code = hb_value_get_int(hb_dict_get(preset, "VideoColorMatrixCodeOveride"));
if (color_matrix_code != 0)
{
int color_prim, color_transfer, color_matrix;
Expand Down Expand Up @@ -1678,9 +1678,12 @@ int hb_preset_apply_video(const hb_dict_t *preset, hb_dict_t *job_dict)
break;
}

hb_dict_set(video_dict, "ColorPrimaries", hb_value_int(color_prim));
hb_dict_set(video_dict, "ColorTransfer", hb_value_int(color_transfer));
hb_dict_set(video_dict, "ColorMatrix", hb_value_int(color_matrix));
hb_dict_set(video_dict, "ColorPrimariesOverride",
hb_value_int(color_prim));
hb_dict_set(video_dict, "ColorTransferOverride",
hb_value_int(color_transfer));
hb_dict_set(video_dict, "ColorMatrixOverride",
hb_value_int(color_matrix));
}
hb_dict_set(video_dict, "Encoder", hb_value_dup(vcodec_value));

Expand Down
Loading

0 comments on commit 219b254

Please sign in to comment.