Commit a0a4a4b3 authored by Thomas Mundt's avatar Thomas Mundt Committed by Michael Niedermayer

avfilter/vf_colormatrix: add bt.2020 colorspace

Signed-off-by: 's avatarThomas Mundt <loudmax@yahoo.de>
Signed-off-by: 's avatarMichael Niedermayer <michael@niedermayer.cc>
parent 2e395bbc
......@@ -5025,6 +5025,9 @@ SMPTE-240M
@item fcc
FCC
@item bt2020
BT.2020
@end table
@end table
......
......@@ -40,11 +40,12 @@
#define NS(n) ((n) < 0 ? (int)((n)*65536.0-0.5+DBL_EPSILON) : (int)((n)*65536.0+0.5))
#define CB(n) av_clip_uint8(n)
static const double yuv_coeff_luma[4][3] = {
static const double yuv_coeff_luma[5][3] = {
{ +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
{ +0.5900, +0.1100, +0.3000 }, // FCC (1)
{ +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
{ +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
{ +0.6780, +0.0593, +0.2627 }, // Rec.2020 (4)
};
enum ColorMode {
......@@ -53,12 +54,13 @@ enum ColorMode {
COLOR_MODE_FCC,
COLOR_MODE_BT601,
COLOR_MODE_SMPTE240M,
COLOR_MODE_BT2020,
COLOR_MODE_COUNT
};
typedef struct {
const AVClass *class;
int yuv_convert[16][3][3];
int yuv_convert[25][3][3];
int interlaced;
int source, dest; ///< ColorMode
int mode;
......@@ -89,6 +91,7 @@ static const AVOption colormatrix_options[] = {
{ "bt470bg", "set BT.470 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601}, .flags=FLAGS, .unit="color_mode" },
{ "smpte170m", "set SMTPE-170M colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601}, .flags=FLAGS, .unit="color_mode" },
{ "smpte240m", "set SMPTE-240M colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_SMPTE240M}, .flags=FLAGS, .unit="color_mode" },
{ "bt2020", "set BT.2020 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT2020}, .flags=FLAGS, .unit="color_mode" },
{ NULL }
};
......@@ -140,13 +143,13 @@ static void solve_coefficients(double cm[3][3], double rgb[3][3], double yuv[3][
static void calc_coefficients(AVFilterContext *ctx)
{
ColorMatrixContext *color = ctx->priv;
double yuv_coeff[4][3][3];
double rgb_coeffd[4][3][3];
double yuv_convertd[16][3][3];
double yuv_coeff[5][3][3];
double rgb_coeffd[5][3][3];
double yuv_convertd[25][3][3];
double bscale, rscale;
int v = 0;
int i, j, k;
for (i = 0; i < 4; i++) {
for (i = 0; i < 5; i++) {
yuv_coeff[i][0][0] = yuv_coeff_luma[i][0];
yuv_coeff[i][0][1] = yuv_coeff_luma[i][1];
yuv_coeff[i][0][2] = yuv_coeff_luma[i][2];
......@@ -159,10 +162,10 @@ static void calc_coefficients(AVFilterContext *ctx)
yuv_coeff[i][2][1] = rscale * yuv_coeff[i][0][1];
yuv_coeff[i][2][2] = 0.5;
}
for (i = 0; i < 4; i++)
for (i = 0; i < 5; i++)
inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
for (k = 0; k < 3; k++) {
color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
......@@ -178,7 +181,7 @@ static void calc_coefficients(AVFilterContext *ctx)
}
}
static const char * const color_modes[] = {"bt709", "fcc", "bt601", "smpte240m"};
static const char * const color_modes[] = {"bt709", "fcc", "bt601", "smpte240m", "bt2020"};
static av_cold int init(AVFilterContext *ctx)
{
......@@ -441,20 +444,23 @@ static int filter_frame(AVFilterLink *link, AVFrame *in)
case AVCOL_SPC_SMPTE240M : source = COLOR_MODE_SMPTE240M ; break;
case AVCOL_SPC_BT470BG : source = COLOR_MODE_BT601 ; break;
case AVCOL_SPC_SMPTE170M : source = COLOR_MODE_BT601 ; break;
case AVCOL_SPC_BT2020_NCL: source = COLOR_MODE_BT2020 ; break;
case AVCOL_SPC_BT2020_CL : source = COLOR_MODE_BT2020 ; break;
default :
av_log(ctx, AV_LOG_ERROR, "Input frame does not specify a supported colorspace, and none has been specified as source either\n");
av_frame_free(&out);
return AVERROR(EINVAL);
}
color->mode = source * 4 + color->dest;
color->mode = source * 5 + color->dest;
} else
color->mode = color->source * 4 + color->dest;
color->mode = color->source * 5 + color->dest;
switch(color->dest) {
case COLOR_MODE_BT709 : av_frame_set_colorspace(out, AVCOL_SPC_BT709) ; break;
case COLOR_MODE_FCC : av_frame_set_colorspace(out, AVCOL_SPC_FCC) ; break;
case COLOR_MODE_SMPTE240M: av_frame_set_colorspace(out, AVCOL_SPC_SMPTE240M); break;
case COLOR_MODE_BT601 : av_frame_set_colorspace(out, AVCOL_SPC_BT470BG) ; break;
case COLOR_MODE_BT709 : av_frame_set_colorspace(out, AVCOL_SPC_BT709) ; break;
case COLOR_MODE_FCC : av_frame_set_colorspace(out, AVCOL_SPC_FCC) ; break;
case COLOR_MODE_SMPTE240M: av_frame_set_colorspace(out, AVCOL_SPC_SMPTE240M) ; break;
case COLOR_MODE_BT601 : av_frame_set_colorspace(out, AVCOL_SPC_BT470BG) ; break;
case COLOR_MODE_BT2020 : av_frame_set_colorspace(out, AVCOL_SPC_BT2020_NCL); break;
}
td.src = in;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment