summaryrefslogtreecommitdiff
path: root/ext/cog/generate_tables.c
blob: d775bcc2168bad948868e25146bbbb85480f36aa (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

#include "config.h"

#include <glib.h>
#include <math.h>

#include "gstcms.h"

#define SCALE 256

static void
get_taps (double *taps, double x)
{
  taps[3] = x * x * (x - 1);
  taps[2] = x * (-x * x + x + 1);
  x = 1 - x;
  taps[1] = x * (-x * x + x + 1);
  taps[0] = x * x * (x - 1);
}

int
main (int argc, char *argv[])
{
  int i;

  g_print ("/* This file is autogenerated.  Do not edit.*/\n");
  g_print ("#include <glib.h>\n");
  g_print ("gint8 cog_resample_table_4tap[256][4] = {\n");
  for (i = 0; i < 256; i++) {
    double x = i / 256.0;
    double taps[4];
    int t[4];
    int sum;

    get_taps (taps, x);
    taps[0] *= SCALE;
    taps[1] *= SCALE;
    taps[2] *= SCALE;
    taps[3] *= SCALE;

    t[0] = floor (taps[0]);
    t[1] = floor (taps[1]);
    t[2] = floor (taps[2]);
    t[3] = floor (taps[3]);
    sum = t[0] + t[1] + t[2] + t[3];

    for (; sum < SCALE; sum++) {
      int i;
      double max = 0;
      int max_i = -1;
      for (i = 0; i < 4; i++) {
        if (max_i == -1 || (t[i] < taps[i] && (taps[i] - t[i]) > max)) {
          max_i = i;
          max = taps[i] - t[i];
        }
      }
      t[max_i]++;
    }
    sum = t[0] + t[1] + t[2] + t[3];

    g_print ("  { %d, %d, %d, %d }, /* %d %d */\n",
        t[0], t[1], t[2], t[3], t[2] + t[0], t[1] + t[3]);
#if 0
    g_print ("/* %.3f %.3f %.3f %.3f %d */\n",
        taps[0] - t[0], taps[1] - t[1], taps[2] - t[2], taps[3] - t[3], sum);
#endif
  }
  g_print ("};\n");
  g_print ("\n");


  {
    int cm, bits;

    for (cm = 0; cm < 2; cm++) {
      for (bits = 6; bits <= 8; bits += 2) {

        ColorMatrix matrix;

        /*
         * At this point, everything is in YCbCr
         * All components are in the range [0,255]
         */
        color_matrix_set_identity (&matrix);

        /* offset required to get input video black to (0.,0.,0.) */
        /* we don't do this because the code does it for us */
        color_matrix_offset_components (&matrix, -16, -128, -128);

        color_matrix_scale_components (&matrix, (1 / 219.0), (1 / 224.0),
            (1 / 224.0));

        /* colour matrix, YCbCr -> RGB */
        /* Requires Y in [0,1.0], Cb&Cr in [-0.5,0.5] */
        if (cm) {
          color_matrix_YCbCr_to_RGB (&matrix, 0.2126, 0.0722);  /* HD */
        } else {
          color_matrix_YCbCr_to_RGB (&matrix, 0.2990, 0.1140);  /* SD */
        }

        /*
         * We are now in RGB space
         */

        /* scale to output range. */
        color_matrix_scale_components (&matrix, 255.0, 255.0, 255.0);

        /* because we're doing 8-bit matrix coefficients */
        color_matrix_scale_components (&matrix, 1 << bits, 1 << bits,
            1 << bits);

        g_print ("static const int cog_ycbcr_to_rgb_matrix_%dbit_%s[] = {\n",
            bits, cm ? "hdtv" : "sdtv");
        g_print ("  %d, %d, %d, %d,\n",
            (int) rint (matrix.m[0][0]),
            (int) rint (matrix.m[0][1]),
            (int) rint (matrix.m[0][2]), (int) rint (matrix.m[0][3]));
        g_print ("  %d, %d, %d, %d,\n",
            (int) rint (matrix.m[1][0]),
            (int) rint (matrix.m[1][1]),
            (int) rint (matrix.m[1][2]), (int) rint (matrix.m[1][3]));
        g_print ("  %d, %d, %d, %d,\n",
            (int) rint (matrix.m[2][0]),
            (int) rint (matrix.m[2][1]),
            (int) rint (matrix.m[2][2]), (int) rint (matrix.m[2][3]));
        g_print ("};\n");
      }
    }
  }

  {
    int cm, bits;

    for (cm = 0; cm < 2; cm++) {
      for (bits = 8; bits <= 8; bits += 1) {

        ColorMatrix matrix;

        color_matrix_set_identity (&matrix);

        color_matrix_scale_components (&matrix, (1 / 255.0), (1 / 255.0),
            (1 / 255.0));

        /* colour matrix, RGB -> YCbCr */
        if (cm) {
          color_matrix_RGB_to_YCbCr (&matrix, 0.2126, 0.0722);  /* HD */
        } else {
          color_matrix_RGB_to_YCbCr (&matrix, 0.2990, 0.1140);  /* SD */
        }

        /*
         * We are now in YCbCr space
         */

        color_matrix_scale_components (&matrix, 219.0, 224.0, 224.0);

        color_matrix_offset_components (&matrix, 16, 128, 128);

        /* because we're doing 8-bit matrix coefficients */
        color_matrix_scale_components (&matrix, 1 << bits, 1 << bits,
            1 << bits);

        g_print ("static const int cog_rgb_to_ycbcr_matrix_%dbit_%s[] = {\n",
            bits, cm ? "hdtv" : "sdtv");
        g_print ("  %d, %d, %d, %d,\n",
            (int) rint (matrix.m[0][0]),
            (int) rint (matrix.m[0][1]),
            (int) rint (matrix.m[0][2]), (int) rint (matrix.m[0][3]));
        g_print ("  %d, %d, %d, %d,\n",
            (int) rint (matrix.m[1][0]),
            (int) rint (matrix.m[1][1]),
            (int) rint (matrix.m[1][2]), (int) rint (matrix.m[1][3]));
        g_print ("  %d, %d, %d, %d,\n",
            (int) rint (matrix.m[2][0]),
            (int) rint (matrix.m[2][1]),
            (int) rint (matrix.m[2][2]), (int) rint (matrix.m[2][3]));
        g_print ("};\n");
      }
    }
  }

  {
    int cm, bits;

    for (cm = 0; cm < 2; cm++) {
      for (bits = 8; bits <= 8; bits += 1) {

        ColorMatrix matrix;

        color_matrix_set_identity (&matrix);

        /* offset required to get input video black to (0.,0.,0.) */
        /* we don't do this because the code does it for us */
        color_matrix_offset_components (&matrix, -16, -128, -128);

        color_matrix_scale_components (&matrix, (1 / 219.0), (1 / 224.0),
            (1 / 224.0));

        /* colour matrix, RGB -> YCbCr */
        if (cm) {
          color_matrix_YCbCr_to_RGB (&matrix, 0.2126, 0.0722);  /* HD */
          color_matrix_RGB_to_YCbCr (&matrix, 0.2990, 0.1140);  /* SD */
        } else {
          color_matrix_YCbCr_to_RGB (&matrix, 0.2990, 0.1140);  /* SD */
          color_matrix_RGB_to_YCbCr (&matrix, 0.2126, 0.0722);  /* HD */
        }

        /*
         * We are now in YCbCr space
         */

        color_matrix_scale_components (&matrix, 219.0, 224.0, 224.0);

        color_matrix_offset_components (&matrix, 16, 128, 128);

        /* because we're doing 8-bit matrix coefficients */
        color_matrix_scale_components (&matrix, 1 << bits, 1 << bits,
            1 << bits);

        g_print
            ("static const int cog_ycbcr_%s_to_ycbcr_%s_matrix_%dbit[] = {\n",
            cm ? "hdtv" : "sdtv", cm ? "sdtv" : "hdtv", bits);
        g_print ("  %d, %d, %d, %d,\n", (int) rint (matrix.m[0][0]),
            (int) rint (matrix.m[0][1]), (int) rint (matrix.m[0][2]),
            (int) rint (matrix.m[0][3]));
        g_print ("  %d, %d, %d, %d,\n", (int) rint (matrix.m[1][0]),
            (int) rint (matrix.m[1][1]), (int) rint (matrix.m[1][2]),
            (int) rint (matrix.m[1][3]));
        g_print ("  %d, %d, %d, %d,\n", (int) rint (matrix.m[2][0]),
            (int) rint (matrix.m[2][1]), (int) rint (matrix.m[2][2]),
            (int) rint (matrix.m[2][3]));
        g_print ("};\n");
      }
    }
  }

  return 0;
}