You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-11-29 05:57:37 +02:00
avutil/{color_utils, csp}: merge color_utils into csp and expose API
libavutil/color_utils contains some avpriv_ symbols that map
enum AVTransferCharacteristic values to gamma-curve approximations and
to the actual transfer functions to invert them (i.e. -> linear).
There's two issues with this:
(1) avpriv is evil and should be avoided whenever possible
(2) libavutil/csp.h exposes a public API for handling color that
already handles primaries and matricies
I don't see any reason this API has to be private, so this commit takes
the functionality from avutil/color_utils and merges it into avutil/csp
with an exposed av_ API rather than the previous avpriv_ API.
Every reference to the previous API has been updated to point to the
new one. color_utils.h has been deleted as well. This should not break
any applications as it only contained avpriv_ symbols in the first
place, so nothing in that header could be referenced by other
applications.
Signed-off-by: Leo Izen <leo.izen@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
This commit is contained in:
172
libavutil/csp.c
172
libavutil/csp.c
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2015 Kevin Wheatley <kevin.j.wheatley@gmail.com>
|
||||
* Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
|
||||
* Copyright (c) 2023 Leo Izen <leo.izen@gmail.com>
|
||||
*
|
||||
* This file is part of FFmpeg.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
@@ -21,9 +24,11 @@
|
||||
* @file Colorspace functions for libavutil
|
||||
* @author Ronald S. Bultje <rsbultje@gmail.com>
|
||||
* @author Leo Izen <leo.izen@gmail.com>
|
||||
* @author Kevin Wheatley <kevin.j.wheatley@gmail.com>
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "attributes.h"
|
||||
#include "csp.h"
|
||||
@@ -126,3 +131,170 @@ enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *
|
||||
|
||||
return AVCOL_PRI_UNSPECIFIED;
|
||||
}
|
||||
|
||||
static const double approximate_gamma[AVCOL_TRC_NB] = {
|
||||
[AVCOL_TRC_BT709] = 1.961,
|
||||
[AVCOL_TRC_SMPTE170M] = 1.961,
|
||||
[AVCOL_TRC_SMPTE240M] = 1.961,
|
||||
[AVCOL_TRC_BT1361_ECG] = 1.961,
|
||||
[AVCOL_TRC_BT2020_10] = 1.961,
|
||||
[AVCOL_TRC_BT2020_12] = 1.961,
|
||||
[AVCOL_TRC_GAMMA22] = 2.2,
|
||||
[AVCOL_TRC_IEC61966_2_1] = 2.2,
|
||||
[AVCOL_TRC_GAMMA28] = 2.8,
|
||||
[AVCOL_TRC_LINEAR] = 1.0,
|
||||
[AVCOL_TRC_SMPTE428] = 2.6,
|
||||
};
|
||||
|
||||
double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
|
||||
{
|
||||
double gamma;
|
||||
if (trc >= AVCOL_TRC_NB)
|
||||
return 0.0;
|
||||
gamma = approximate_gamma[trc];
|
||||
if (gamma > 0)
|
||||
return gamma;
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
#define BT709_alpha 1.099296826809442
|
||||
#define BT709_beta 0.018053968510807
|
||||
|
||||
static double trc_bt709(double Lc)
|
||||
{
|
||||
const double a = BT709_alpha;
|
||||
const double b = BT709_beta;
|
||||
|
||||
return (0.0 > Lc) ? 0.0
|
||||
: ( b > Lc) ? 4.500 * Lc
|
||||
: a * pow(Lc, 0.45) - (a - 1.0);
|
||||
}
|
||||
|
||||
static double trc_gamma22(double Lc)
|
||||
{
|
||||
return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.2);
|
||||
}
|
||||
|
||||
static double trc_gamma28(double Lc)
|
||||
{
|
||||
return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.8);
|
||||
}
|
||||
|
||||
static double trc_smpte240M(double Lc)
|
||||
{
|
||||
const double a = 1.1115;
|
||||
const double b = 0.0228;
|
||||
|
||||
return (0.0 > Lc) ? 0.0
|
||||
: ( b > Lc) ? 4.000 * Lc
|
||||
: a * pow(Lc, 0.45) - (a - 1.0);
|
||||
}
|
||||
|
||||
static double trc_linear(double Lc)
|
||||
{
|
||||
return Lc;
|
||||
}
|
||||
|
||||
static double trc_log(double Lc)
|
||||
{
|
||||
return (0.01 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.0;
|
||||
}
|
||||
|
||||
static double trc_log_sqrt(double Lc)
|
||||
{
|
||||
// sqrt(10) / 1000
|
||||
return (0.00316227766 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.5;
|
||||
}
|
||||
|
||||
static double trc_iec61966_2_4(double Lc)
|
||||
{
|
||||
const double a = BT709_alpha;
|
||||
const double b = BT709_beta;
|
||||
|
||||
return (-b >= Lc) ? -a * pow(-Lc, 0.45) + (a - 1.0)
|
||||
: ( b > Lc) ? 4.500 * Lc
|
||||
: a * pow( Lc, 0.45) - (a - 1.0);
|
||||
}
|
||||
|
||||
static double trc_bt1361(double Lc)
|
||||
{
|
||||
const double a = BT709_alpha;
|
||||
const double b = BT709_beta;
|
||||
|
||||
return (-0.0045 >= Lc) ? -(a * pow(-4.0 * Lc, 0.45) + (a - 1.0)) / 4.0
|
||||
: ( b > Lc) ? 4.500 * Lc
|
||||
: a * pow( Lc, 0.45) - (a - 1.0);
|
||||
}
|
||||
|
||||
static double trc_iec61966_2_1(double Lc)
|
||||
{
|
||||
const double a = 1.055;
|
||||
const double b = 0.0031308;
|
||||
|
||||
return (0.0 > Lc) ? 0.0
|
||||
: ( b > Lc) ? 12.92 * Lc
|
||||
: a * pow(Lc, 1.0 / 2.4) - (a - 1.0);
|
||||
}
|
||||
|
||||
static double trc_smpte_st2084(double Lc)
|
||||
{
|
||||
const double c1 = 3424.0 / 4096.0; // c3-c2 + 1
|
||||
const double c2 = 32.0 * 2413.0 / 4096.0;
|
||||
const double c3 = 32.0 * 2392.0 / 4096.0;
|
||||
const double m = 128.0 * 2523.0 / 4096.0;
|
||||
const double n = 0.25 * 2610.0 / 4096.0;
|
||||
const double L = Lc / 10000.0;
|
||||
const double Ln = pow(L, n);
|
||||
|
||||
return (0.0 > Lc) ? 0.0
|
||||
: pow((c1 + c2 * Ln) / (1.0 + c3 * Ln), m);
|
||||
|
||||
}
|
||||
|
||||
static double trc_smpte_st428_1(double Lc)
|
||||
{
|
||||
return (0.0 > Lc) ? 0.0
|
||||
: pow(48.0 * Lc / 52.37, 1.0 / 2.6);
|
||||
}
|
||||
|
||||
|
||||
static double trc_arib_std_b67(double Lc) {
|
||||
// The function uses the definition from HEVC, which assumes that the peak
|
||||
// white is input level = 1. (this is equivalent to scaling E = Lc * 12 and
|
||||
// using the definition from the ARIB STD-B67 spec)
|
||||
const double a = 0.17883277;
|
||||
const double b = 0.28466892;
|
||||
const double c = 0.55991073;
|
||||
return (0.0 > Lc) ? 0.0 :
|
||||
(Lc <= 1.0 / 12.0 ? sqrt(3.0 * Lc) : a * log(12.0 * Lc - b) + c);
|
||||
}
|
||||
|
||||
static const av_csp_trc_function trc_funcs[AVCOL_TRC_NB] = {
|
||||
[AVCOL_TRC_BT709] = trc_bt709,
|
||||
[AVCOL_TRC_GAMMA22] = trc_gamma22,
|
||||
[AVCOL_TRC_GAMMA28] = trc_gamma28,
|
||||
[AVCOL_TRC_SMPTE170M] = trc_bt709,
|
||||
[AVCOL_TRC_SMPTE240M] = trc_smpte240M,
|
||||
[AVCOL_TRC_LINEAR] = trc_linear,
|
||||
[AVCOL_TRC_LOG] = trc_log,
|
||||
[AVCOL_TRC_LOG_SQRT] = trc_log_sqrt,
|
||||
[AVCOL_TRC_IEC61966_2_4] = trc_iec61966_2_4,
|
||||
[AVCOL_TRC_BT1361_ECG] = trc_bt1361,
|
||||
[AVCOL_TRC_IEC61966_2_1] = trc_iec61966_2_1,
|
||||
[AVCOL_TRC_BT2020_10] = trc_bt709,
|
||||
[AVCOL_TRC_BT2020_12] = trc_bt709,
|
||||
[AVCOL_TRC_SMPTE2084] = trc_smpte_st2084,
|
||||
[AVCOL_TRC_SMPTE428] = trc_smpte_st428_1,
|
||||
[AVCOL_TRC_ARIB_STD_B67] = trc_arib_std_b67,
|
||||
};
|
||||
|
||||
av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)
|
||||
{
|
||||
av_csp_trc_function func;
|
||||
if (trc >= AVCOL_TRC_NB)
|
||||
return NULL;
|
||||
func = trc_funcs[trc];
|
||||
if (!func)
|
||||
return NULL;
|
||||
return func;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user