summaryrefslogtreecommitdiffstats
path: root/libhb/colorspace.c
blob: c535d222056178ddece9f88f1c3186856fd18d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* colorspace.c

   Copyright (c) 2003-2015 HandBrake Team
   This file is part of the HandBrake source code
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License v2.
   For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
 */

#include "handbrake/common.h"
#include "handbrake/avfilter_priv.h"

static int colorspace_init(hb_filter_object_t * filter,
                           hb_filter_init_t * init);

const char colorspace_template[] =
    "primaries=^"HB_ALL_REG"$:transfer=^"HB_ALL_REG"$:matrix=^"HB_ALL_REG"$:range=^"HB_ALL_REG"$:"
    "tonemap=^"HB_ALL_REG"$:param=^"HB_FLOAT_REG"$:desat=^"HB_FLOAT_REG"$";

hb_filter_object_t hb_filter_colorspace =
{
    .id                = HB_FILTER_COLORSPACE,
    .enforce_order     = 1,
    .skip              = 1,
    .name              = "Colorspace",
    .settings          = NULL,
    .init              = colorspace_init,
    .work              = hb_avfilter_null_work,
    .close             = hb_avfilter_alias_close,
    .settings_template = colorspace_template,
};

static const char * get_matrix_name(int colorspace)
{
    switch (colorspace) {
    case AVCOL_SPC_RGB:
        return "gbr";
    case AVCOL_SPC_BT709:
        return "bt709";
    case AVCOL_SPC_UNSPECIFIED:
        return "unknown";
    case AVCOL_SPC_FCC:
        return "fcc";
    case AVCOL_SPC_BT470BG:
        return "bt470bg";
    case AVCOL_SPC_SMPTE170M:
        return "smpte170m";
    case AVCOL_SPC_SMPTE240M:
        return "smpte2400m";
    case AVCOL_SPC_YCGCO:
        return "ycgco";
    case AVCOL_SPC_BT2020_NCL:
        return "bt2020nc";
    case AVCOL_SPC_BT2020_CL:
        return "bt2020c";
    case AVCOL_SPC_CHROMA_DERIVED_NCL:
        return "chroma-derived-nc";
    case AVCOL_SPC_CHROMA_DERIVED_CL:
        return "chroma-derived-c";
    case AVCOL_SPC_ICTCP:
        return "ictcp";
    }
    return "unspecified";
}

static const char * get_transfer_name(int color_trc)
{
    switch (color_trc) {
    case AVCOL_TRC_UNSPECIFIED:
        return "unspecified";
    case AVCOL_TRC_BT709:
        return "709";
    case AVCOL_TRC_GAMMA22:
        return "bt470m";
    case AVCOL_TRC_GAMMA28:
        return "bt470bg";
    case AVCOL_TRC_SMPTE170M:
        return "smpte170m";
    case AVCOL_TRC_SMPTE240M:
        return "240m"; // fixme
    case AVCOL_TRC_LINEAR:
        return "linear";
    case AVCOL_TRC_LOG:
        return "log100";
    case AVCOL_TRC_LOG_SQRT:
        return "log316";
    case AVCOL_TRC_IEC61966_2_4:
        return "iec61966-2-4";
    case AVCOL_TRC_BT2020_10:
        return "2020_10";
    case AVCOL_TRC_BT2020_12:
        return "2020_12";
    case AVCOL_TRC_SMPTE2084:
        return "smpte2084";
    case AVCOL_TRC_ARIB_STD_B67:
        return "arib-std-b67";
    case AVCOL_TRC_IEC61966_2_1:
        return "iec61966-2-1";
    }
    return "unspecified";
}

static const char * get_primaries_name(int color_primaries)
{
    switch (color_primaries) {
    case AVCOL_PRI_UNSPECIFIED:
        return "unspecified";
    case AVCOL_PRI_BT709:
        return "709";
    case AVCOL_PRI_BT470M:
        return "bt470m";
    case AVCOL_PRI_BT470BG:
        return "bt470bg";
    case AVCOL_PRI_SMPTE170M:
        return "smpte170m";
    case AVCOL_PRI_SMPTE240M:
        return "smpte240m";
    case AVCOL_PRI_FILM:
        return "film";
    case AVCOL_PRI_BT2020:
        return "bt2020";
    case AVCOL_PRI_SMPTE428:
        return "smpte428";
    case AVCOL_PRI_SMPTE431:
        return "smpte431";
    case AVCOL_PRI_SMPTE432:
        return "smpte432";
    case AVCOL_PRI_JEDEC_P22:
        return "jedec-p22";
    }
    return "unspecified";
}

static const char * get_range_name(int color_range)
{
    switch (color_range) {
    case AVCOL_RANGE_UNSPECIFIED:
    case AVCOL_RANGE_MPEG:
        return "limited";
    case AVCOL_RANGE_JPEG:
        return "full";
    }
    return "limited";
}

#define REFERENCE_WHITE 100.0f

static double determine_signal_peak(hb_filter_init_t * init)
{
    double peak = init->job->coll.max_cll / REFERENCE_WHITE;
    if (!peak && init->job->mastering.has_luminance)
    {
        peak = hb_q2d(init->job->mastering.max_luminance) / REFERENCE_WHITE;
    }
    if (!peak || peak < 1)
    {
        peak = init->color_transfer == HB_COLR_TRA_SMPTEST2084 ? 100.0f : 10.0f;
    }
    return peak;
}

static int colorspace_init(hb_filter_object_t * filter, hb_filter_init_t * init)
{
    hb_filter_private_t * pv = NULL;

    pv = calloc(1, sizeof(struct hb_filter_private_s));
    filter->private_data = pv;
    if (pv == NULL)
    {
        return 1;
    }
    pv->input = *init;

    hb_dict_t        * settings = filter->settings;

    char * range = NULL;
    char * primaries = NULL, * transfer = NULL, * matrix = NULL;
    char * tonemap = NULL;
    double param = 0, desat = 0;

    hb_dict_extract_string(&range, settings, "range");
    hb_dict_extract_string(&primaries, settings, "primaries");
    hb_dict_extract_string(&transfer, settings, "transfer");
    hb_dict_extract_string(&matrix, settings, "matrix");
    hb_dict_extract_string(&tonemap, settings, "tonemap");
    hb_dict_extract_double(&param, settings, "param");
    hb_dict_extract_double(&desat, settings, "desat");

    if (!(range || primaries || transfer || matrix))
    {
        return 0;
    }

    int color_prim, color_transfer, color_matrix, color_range;

    color_prim = init->color_prim;
    color_transfer = init->color_transfer;
    color_matrix = init->color_matrix;
    color_range = init->color_range;

    if (primaries)
    {
        color_prim = av_color_primaries_from_name(primaries);
    }
    if (transfer)
    {
        color_transfer = av_color_transfer_from_name(transfer);
    }
    if (matrix)
    {
        color_matrix = av_color_space_from_name(matrix);
    }
    if (range)
    {
        color_range = av_color_range_from_name(range);
    }

    if (color_prim == init->color_prim && color_transfer == init->color_transfer &&
        color_matrix == init->color_matrix && color_range == init->color_range)
    {
        return 0;
    }

    if (tonemap == NULL)
    {
        tonemap = "hable";
    }

    hb_value_array_t * avfilters = hb_value_array_init();
    hb_dict_t * avfilter   = NULL;
    hb_dict_t * avsettings = NULL;

    if (transfer && init->color_transfer != color_transfer &&
        (init->color_transfer == HB_COLR_TRA_SMPTEST2084 || init->color_transfer == HB_COLR_TRA_ARIB_STD_B67))
    {
        // Zscale
        avfilter   = hb_dict_init();
        avsettings = hb_dict_init();

        hb_dict_set_string(avsettings, "transfer", "linear");
        hb_dict_set_string(avsettings, "npl", "100");
        hb_dict_set(avfilter, "zscale", avsettings);

        hb_value_array_append(avfilters, avfilter);

        // Format
        avfilter   = hb_dict_init();
        avsettings = hb_dict_init();

        hb_dict_set_string(avsettings, "pix_fmts", "gbrpf32le");
        hb_dict_set(avfilter, "format", avsettings);

        hb_value_array_append(avfilters, avfilter);

        // Tonemap
        avfilter   = hb_dict_init();
        avsettings = hb_dict_init();

        hb_dict_set_string(avsettings, "tonemap", tonemap);
        if (strcmp(tonemap, "hable") && strcmp(tonemap, "none") && param != 0)
        {
            hb_dict_set_double(avsettings, "param", param);
        }
        hb_dict_set_double(avsettings, "desat", desat);
        // FIXME: this could be automated by passing through side data.
        double peak = determine_signal_peak(init);
        hb_dict_set_double(avsettings, "peak", peak);
        hb_dict_set(avfilter, "tonemap", avsettings);

        hb_value_array_append(avfilters, avfilter);
    }

    // Zscale
    avfilter   = hb_dict_init();
    avsettings = hb_dict_init();

    if (primaries)
    {
        hb_dict_set_string(avsettings, "primaries", get_primaries_name(color_prim));
    }
    if (transfer)
    {
        hb_dict_set_string(avsettings, "transfer", get_transfer_name(color_transfer));
    }
    if (matrix)
    {
        hb_dict_set_string(avsettings, "matrix", get_matrix_name(color_matrix));
    }
    if (range)
    {
        hb_dict_set_string(avsettings, "range", get_range_name(color_range));
    }

    hb_dict_set(avfilter, "zscale", avsettings);
    hb_value_array_append(avfilters, avfilter);

    // Format
    avfilter   = hb_dict_init();
    avsettings = hb_dict_init();

    hb_dict_set_string(avsettings, "pix_fmts", av_get_pix_fmt_name(init->pix_fmt));
    hb_dict_set(avfilter, "format", avsettings);

    hb_value_array_append(avfilters, avfilter);

    pv->avfilters = avfilters;

    init->color_prim = color_prim;
    init->color_transfer = color_transfer;
    init->color_matrix = color_matrix;
    init->color_range = color_range;

    pv->output = *init;

    return 0;
}