aboutsummaryrefslogtreecommitdiffhomepage
path: root/video/csputils.c
blob: 9c1b04c92866971c6672111bba53cf6e1b54711e (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * Common code related to colorspaces and conversion
 *
 * Copyleft (C) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
 *
 * mp_invert_yuv2rgb based on DarkPlaces engine, original code (GPL2 or later)
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * You can alternatively redistribute this file and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 */

#include <stdint.h>
#include <math.h>
#include <assert.h>
#include <libavutil/common.h>
#include <libavcodec/avcodec.h>

#include "csputils.h"

const char *const mp_csp_names[MP_CSP_COUNT] = {
    "Autoselect",
    "BT.601 (SD)",
    "BT.709 (HD)",
    "SMPTE-240M",
    "RGB",
    "XYZ",
    "YCgCo",
};

const char *const mp_csp_levels_names[MP_CSP_LEVELS_COUNT] = {
    "Autoselect",
    "TV",
    "PC",
};

const char *const mp_csp_equalizer_names[MP_CSP_EQ_COUNT] = {
    "brightness",
    "contrast",
    "hue",
    "saturation",
    "gamma",
};

const char *const mp_chroma_names[MP_CHROMA_COUNT] = {
    "unknown",
    "mpeg2/4/h264",
    "mpeg1/jpeg",
};

enum mp_csp avcol_spc_to_mp_csp(int avcolorspace)
{
    switch (avcolorspace) {
        case AVCOL_SPC_BT709:     return MP_CSP_BT_709;
        case AVCOL_SPC_BT470BG:   return MP_CSP_BT_601;
        case AVCOL_SPC_SMPTE170M: return MP_CSP_BT_601;
        case AVCOL_SPC_SMPTE240M: return MP_CSP_SMPTE_240M;
        case AVCOL_SPC_RGB:       return MP_CSP_RGB;
        case AVCOL_SPC_YCOCG:     return MP_CSP_YCGCO;
        default:                  return MP_CSP_AUTO;
    }
}

enum mp_csp_levels avcol_range_to_mp_csp_levels(int avrange)
{
    switch (avrange) {
        case AVCOL_RANGE_MPEG: return MP_CSP_LEVELS_TV;
        case AVCOL_RANGE_JPEG: return MP_CSP_LEVELS_PC;
        default:               return MP_CSP_LEVELS_AUTO;
    }
}

int mp_csp_to_avcol_spc(enum mp_csp colorspace)
{
    switch (colorspace) {
        case MP_CSP_BT_709:     return AVCOL_SPC_BT709;
        case MP_CSP_BT_601:     return AVCOL_SPC_BT470BG;
        case MP_CSP_SMPTE_240M: return AVCOL_SPC_SMPTE240M;
        case MP_CSP_RGB:        return AVCOL_SPC_RGB;
        case MP_CSP_YCGCO:      return AVCOL_SPC_YCOCG;
        default:                return AVCOL_SPC_UNSPECIFIED;
    }
}

int mp_csp_levels_to_avcol_range(enum mp_csp_levels range)
{
    switch (range) {
        case MP_CSP_LEVELS_TV: return AVCOL_RANGE_MPEG;
        case MP_CSP_LEVELS_PC: return AVCOL_RANGE_JPEG;
        default:               return AVCOL_RANGE_UNSPECIFIED;
    }
}

enum mp_csp mp_csp_guess_colorspace(int width, int height)
{
    return width >= 1280 || height > 576 ? MP_CSP_BT_709 : MP_CSP_BT_601;
}

enum mp_chroma_location avchroma_location_to_mp(int avloc)
{
    switch (avloc) {
    case AVCHROMA_LOC_LEFT:             return MP_CHROMA_LEFT;
    case AVCHROMA_LOC_CENTER:           return MP_CHROMA_CENTER;
    default:                            return MP_CHROMA_AUTO;
    }
}

int mp_chroma_location_to_av(enum mp_chroma_location mploc)
{
    switch (mploc) {
    case MP_CHROMA_LEFT:                return AVCHROMA_LOC_LEFT;
    case MP_CHROMA_CENTER:              return AVCHROMA_LOC_CENTER;
    default:                            return AVCHROMA_LOC_UNSPECIFIED;
    }
}

// Return location of chroma samples relative to luma samples. 0/0 means
// centered. Other possible values are -1 (top/left) and +1 (right/bottom).
void mp_get_chroma_location(enum mp_chroma_location loc, int *x, int *y)
{
    *x = 0;
    *y = 0;
    if (loc == MP_CHROMA_LEFT)
        *x = -1;
}

/**
 * \brief little helper function to create a lookup table for gamma
 * \param map buffer to create map into
 * \param size size of buffer
 * \param gamma gamma value
 */
void mp_gen_gamma_map(uint8_t *map, int size, float gamma)
{
    if (gamma == 1.0) {
        for (int i = 0; i < size; i++)
            map[i] = 255 * i / (size - 1);
        return;
    }
    gamma = 1.0 / gamma;
    for (int i = 0; i < size; i++) {
        float tmp = (float)i / (size - 1.0);
        tmp = pow(tmp, gamma);
        if (tmp > 1.0)
            tmp = 1.0;
        if (tmp < 0.0)
            tmp = 0.0;
        map[i] = 255 * tmp;
    }
}

/* Fill in the Y, U, V vectors of a yuv2rgb conversion matrix
 * based on the given luma weights of the R, G and B components (lr, lg, lb).
 * lr+lg+lb is assumed to equal 1.
 * This function is meant for colorspaces satisfying the following
 * conditions (which are true for common YUV colorspaces):
 * - The mapping from input [Y, U, V] to output [R, G, B] is linear.
 * - Y is the vector [1, 1, 1].  (meaning input Y component maps to 1R+1G+1B)
 * - U maps to a value with zero R and positive B ([0, x, y], y > 0;
 *   i.e. blue and green only).
 * - V maps to a value with zero B and positive R ([x, y, 0], x > 0;
 *   i.e. red and green only).
 * - U and V are orthogonal to the luma vector [lr, lg, lb].
 * - The magnitudes of the vectors U and V are the minimal ones for which
 *   the image of the set Y=[0...1],U=[-0.5...0.5],V=[-0.5...0.5] under the
 *   conversion function will cover the set R=[0...1],G=[0...1],B=[0...1]
 *   (the resulting matrix can be converted for other input/output ranges
 *   outside this function).
 * Under these conditions the given parameters lr, lg, lb uniquely
 * determine the mapping of Y, U, V to R, G, B.
 */
static void luma_coeffs(float m[3][4], float lr, float lg, float lb)
{
    assert(fabs(lr+lg+lb - 1) < 1e-6);
    m[0][0] = m[1][0] = m[2][0] = 1;
    m[0][1] = 0;
    m[1][1] = -2 * (1-lb) * lb/lg;
    m[2][1] = 2 * (1-lb);
    m[0][2] = 2 * (1-lr);
    m[1][2] = -2 * (1-lr) * lr/lg;
    m[2][2] = 0;
    // Constant coefficients (m[x][3]) not set here
}

/**
 * \brief get the coefficients of the yuv -> rgb conversion matrix
 * \param params struct specifying the properties of the conversion like
 *  brightness, ...
 * \param m array to store coefficients into
 */
void mp_get_yuv2rgb_coeffs(struct mp_csp_params *params, float m[3][4])
{
    int format = params->colorspace.format;
    if (format <= MP_CSP_AUTO || format >= MP_CSP_COUNT)
        format = MP_CSP_BT_601;
    int levels_in = params->colorspace.levels_in;
    if (levels_in <= MP_CSP_LEVELS_AUTO || levels_in >= MP_CSP_LEVELS_COUNT)
        levels_in = MP_CSP_LEVELS_TV;

    switch (format) {
    case MP_CSP_BT_601:     luma_coeffs(m, 0.299,  0.587,  0.114 ); break;
    case MP_CSP_BT_709:     luma_coeffs(m, 0.2126, 0.7152, 0.0722); break;
    case MP_CSP_SMPTE_240M: luma_coeffs(m, 0.2122, 0.7013, 0.0865); break;
    case MP_CSP_RGB: {
        static const float ident[3][4] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
        memcpy(m, ident, sizeof(ident));
        levels_in = -1;
        break;
    }
    case MP_CSP_XYZ: {
        static const float xyz_to_rgb[3][4] = {
            {3.2404542,  -1.5371385, -0.4985314},
            {-0.9692660,  1.8760108,  0.0415560},
            {0.0556434,  -0.2040259,  1.0572252},
        };
        memcpy(m, xyz_to_rgb, sizeof(xyz_to_rgb));
        levels_in = -1;
        break;
    }
    case MP_CSP_YCGCO: {
        static const float ycgco_to_rgb[3][4] = {
            {1,  -1,  1},
            {1,   1,  0},
            {1,  -1, -1},
        };
        memcpy(m, ycgco_to_rgb, sizeof(ycgco_to_rgb));
        break;
    }
    default:
        abort();
    };

    // Hue is equivalent to rotating input [U, V] subvector around the origin.
    // Saturation scales [U, V].
    float huecos = params->saturation * cos(params->hue);
    float huesin = params->saturation * sin(params->hue);
    for (int i = 0; i < 3; i++) {
        float u = m[i][COL_U];
        m[i][COL_U] = huecos * u - huesin * m[i][COL_V];
        m[i][COL_V] = huesin * u + huecos * m[i][COL_V];
    }

    assert(params->input_bits >= 8);
    assert(params->texture_bits >= params->input_bits);
    double s = (1 << (params->input_bits-8)) / ((1<<params->texture_bits)-1.);
    // The values below are written in 0-255 scale
    struct yuvlevels { double ymin, ymax, cmin, cmid; }
        yuvlim =  { 16*s, 235*s, 16*s, 128*s },
        yuvfull = {  0*s, 255*s,  1*s, 128*s },  // '1' for symmetry around 128
        anyfull = {  0*s, 255*s, -255*s/2, 0 },
        yuvlev;
    switch (levels_in) {
    case MP_CSP_LEVELS_TV: yuvlev = yuvlim; break;
    case MP_CSP_LEVELS_PC: yuvlev = yuvfull; break;
    case -1: yuvlev = anyfull; break;
    default:
        abort();
    }

    int levels_out = params->colorspace.levels_out;
    if (levels_out <= MP_CSP_LEVELS_AUTO || levels_out >= MP_CSP_LEVELS_COUNT)
        levels_out = MP_CSP_LEVELS_PC;
    struct rgblevels { double min, max; }
        rgblim =  { 16/255., 235/255. },
        rgbfull = {      0,        1  },
        rgblev;
    switch (levels_out) {
    case MP_CSP_LEVELS_TV: rgblev = rgblim; break;
    case MP_CSP_LEVELS_PC: rgblev = rgbfull; break;
    default:
        abort();
    }

    double ymul = (rgblev.max - rgblev.min) / (yuvlev.ymax - yuvlev.ymin);
    double cmul = (rgblev.max - rgblev.min) / (yuvlev.cmid - yuvlev.cmin) / 2;
    for (int i = 0; i < 3; i++) {
        m[i][COL_Y] *= ymul;
        m[i][COL_U] *= cmul;
        m[i][COL_V] *= cmul;
        // Set COL_C so that Y=umin,UV=cmid maps to RGB=min (black to black)
        m[i][COL_C] = rgblev.min - m[i][COL_Y] * yuvlev.ymin
                      -(m[i][COL_U] + m[i][COL_V]) * yuvlev.cmid;
    }

    // Brightness adds a constant to output R,G,B.
    // Contrast scales Y around 1/2 (not 0 in this implementation).
    for (int i = 0; i < 3; i++) {
        m[i][COL_C] += params->brightness;
        m[i][COL_Y] *= params->contrast;
        m[i][COL_C] += (rgblev.max-rgblev.min) * (1 - params->contrast)/2;
    }

    int in_bits = FFMAX(params->int_bits_in, 1);
    int out_bits = FFMAX(params->int_bits_out, 1);
    double in_scale = (1 << in_bits) - 1.0;
    double out_scale = (1 << out_bits) - 1.0;
    for (int i = 0; i < 3; i++) {
        m[i][COL_C] *= out_scale; // constant is 1.0
        for (int x = 0; x < 3; x++)
            m[i][x] *= out_scale / in_scale;
    }
}

//! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
#define GMAP_SIZE (1024)
/**
 * \brief generate a 3D YUV -> RGB map
 * \param params struct containing parameters like brightness, gamma, ...
 * \param map where to store map. Must provide space for (size + 2)^3 elements
 * \param size size of the map, excluding border
 */
void mp_gen_yuv2rgb_map(struct mp_csp_params *params, unsigned char *map, int size)
{
    int i, j, k, l;
    float step = 1.0 / size;
    float y, u, v;
    float yuv2rgb[3][4];
    unsigned char gmaps[3][GMAP_SIZE];
    mp_gen_gamma_map(gmaps[0], GMAP_SIZE, params->rgamma);
    mp_gen_gamma_map(gmaps[1], GMAP_SIZE, params->ggamma);
    mp_gen_gamma_map(gmaps[2], GMAP_SIZE, params->bgamma);
    mp_get_yuv2rgb_coeffs(params, yuv2rgb);
    for (i = 0; i < 3; i++)
        for (j = 0; j < 4; j++)
            yuv2rgb[i][j] *= GMAP_SIZE - 1;
    v = 0;
    for (i = -1; i <= size; i++) {
        u = 0;
        for (j = -1; j <= size; j++) {
            y = 0;
            for (k = -1; k <= size; k++) {
                for (l = 0; l < 3; l++) {
                    float rgb = yuv2rgb[l][COL_Y] * y + yuv2rgb[l][COL_U] * u +
                                yuv2rgb[l][COL_V] * v + yuv2rgb[l][COL_C];
                    *map++ = gmaps[l][av_clip(rgb, 0, GMAP_SIZE - 1)];
                }
                y += (k == -1 || k == size - 1) ? step / 2 : step;
            }
            u += (j == -1 || j == size - 1) ? step / 2 : step;
        }
        v += (i == -1 || i == size - 1) ? step / 2 : step;
    }
}

// Copy settings from eq into params.
void mp_csp_copy_equalizer_values(struct mp_csp_params *params,
                                  const struct mp_csp_equalizer *eq)
{
    params->brightness = eq->values[MP_CSP_EQ_BRIGHTNESS] / 100.0;
    params->contrast = (eq->values[MP_CSP_EQ_CONTRAST] + 100) / 100.0;
    params->hue = eq->values[MP_CSP_EQ_HUE] / 100.0 * 3.1415927;
    params->saturation = (eq->values[MP_CSP_EQ_SATURATION] + 100) / 100.0;
    float gamma = exp(log(8.0) * eq->values[MP_CSP_EQ_GAMMA] / 100.0);
    params->rgamma = gamma;
    params->ggamma = gamma;
    params->bgamma = gamma;
}

static int find_eq(int capabilities, const char *name)
{
    for (int i = 0; i < MP_CSP_EQ_COUNT; i++) {
        if (strcmp(name, mp_csp_equalizer_names[i]) == 0)
            return ((1 << i) & capabilities) ? i : -1;
    }
    return -1;
}

int mp_csp_equalizer_get(struct mp_csp_equalizer *eq, const char *property,
                         int *out_value)
{
    int index = find_eq(eq->capabilities, property);
    if (index < 0)
        return -1;

    *out_value = eq->values[index];

    return 0;
}

int mp_csp_equalizer_set(struct mp_csp_equalizer *eq, const char *property,
                         int value)
{
    int index = find_eq(eq->capabilities, property);
    if (index < 0)
        return 0;

    eq->values[index] = value;

    return 1;
}

void mp_invert_yuv2rgb(float out[3][4], float in[3][4])
{
    float m00 = in[0][0], m01 = in[0][1], m02 = in[0][2], m03 = in[0][3],
          m10 = in[1][0], m11 = in[1][1], m12 = in[1][2], m13 = in[1][3],
          m20 = in[2][0], m21 = in[2][1], m22 = in[2][2], m23 = in[2][3];

    // calculate the adjoint
    out[0][0] =  (m11 * m22 - m21 * m12);
    out[0][1] = -(m01 * m22 - m21 * m02);
    out[0][2] =  (m01 * m12 - m11 * m02);
    out[1][0] = -(m10 * m22 - m20 * m12);
    out[1][1] =  (m00 * m22 - m20 * m02);
    out[1][2] = -(m00 * m12 - m10 * m02);
    out[2][0] =  (m10 * m21 - m20 * m11);
    out[2][1] = -(m00 * m21 - m20 * m01);
    out[2][2] =  (m00 * m11 - m10 * m01);

    // calculate the determinant (as inverse == 1/det * adjoint,
    // adjoint * m == identity * det, so this calculates the det)
    float det = m00 * out[0][0] + m10 * out[0][1] + m20 * out[0][2];
    det = 1.0f / det;

    out[0][0] *= det;
    out[0][1] *= det;
    out[0][2] *= det;
    out[1][0] *= det;
    out[1][1] *= det;
    out[1][2] *= det;
    out[2][0] *= det;
    out[2][1] *= det;
    out[2][2] *= det;

    // fix the constant coefficient
    // rgb = M * yuv + C
    // M^-1 * rgb = yuv + M^-1 * C
    // yuv = M^-1 * rgb - M^-1 * C
    //                  ^^^^^^^^^^
    out[0][3] = -(out[0][0] * m03 + out[0][1] * m13 + out[0][2] * m23);
    out[1][3] = -(out[1][0] * m03 + out[1][1] * m13 + out[1][2] * m23);
    out[2][3] = -(out[2][0] * m03 + out[2][1] * m13 + out[2][2] * m23);
}

// Multiply the color in c with the given matrix.
// c is {R, G, B} or {Y, U, V} (depending on input/output and matrix).
// Output is clipped to the given number of bits.
void mp_map_int_color(float matrix[3][4], int clip_bits, int c[3])
{
    int in[3] = {c[0], c[1], c[2]};
    for (int i = 0; i < 3; i++) {
        double val = matrix[i][3];
        for (int x = 0; x < 3; x++)
            val += matrix[i][x] * in[x];
        int ival = lrint(val);
        c[i] = av_clip(ival, 0, (1 << clip_bits) - 1);
    }
}