j2k-transcode-metal 0.7.1

Metal acceleration for JPEG-to-HTJ2K coefficient-domain transcode stages
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// SPDX-License-Identifier: MIT OR Apache-2.0

#include <metal_stdlib>

using namespace metal;

struct Dct97ProjectionParams {
    uint width;
    uint height;
    uint block_cols;
    uint band_width;
    uint band_height;
};

struct Dct97BatchProjectionParams {
    uint width;
    uint height;
    uint block_cols;
    uint blocks_per_item;
    uint band_width;
    uint band_height;
    uint output_stride;
};

struct Dct97IdctRowLiftParams {
    uint width;
    uint height;
    uint block_cols;
    uint blocks_per_item;
    uint low_width;
    uint high_width;
};

struct Dct97ColumnLiftParams {
    uint height;
    uint low_width;
    uint high_width;
    uint low_height;
    uint high_height;
    uint row_low_stride;
    uint row_high_stride;
    uint ll_stride;
    uint hl_stride;
    uint lh_stride;
    uint hh_stride;
};

struct Dct97QuantizeCodeblocksParams {
    uint band_width;
    uint band_height;
    uint output_stride;
    uint code_block_width;
    uint code_block_height;
    float inv_delta;
};

struct Reversible53ProjectionParams {
    uint width;
    uint height;
    uint block_cols;
    uint blocks_per_item;
    uint band_width;
    uint band_height;
    uint output_stride;
    uint vertical_low;
    uint horizontal_low;
};

struct Dct97SparseRow {
    uint offset;
    uint count;
};

struct Dct97WeightTap {
    uint sample_idx;
    float weight;
};

#define DCT97_STAGED_MAX_AXIS 1024
#define DCT97_ROWS_PER_GROUP 2
#define DCT97_COLUMNS_PER_GROUP 4
#define DCT97_THREADS_PER_GROUP 256

static inline float dct97_idct_sample(
    device const float *blocks,
    device const float *idct_basis,
    constant Dct97IdctRowLiftParams &params,
    uint item_idx,
    uint x,
    uint y
) {
    const uint block_x = x / 8u;
    const uint block_y = y / 8u;
    const uint local_x = x % 8u;
    const uint local_y = y % 8u;
    const uint block_base =
        (item_idx * params.blocks_per_item + block_y * params.block_cols + block_x) * 64u;

    float sample = 0.0f;
    for (uint freq_y = 0; freq_y < 8u; ++freq_y) {
        const float y_basis = idct_basis[local_y * 8u + freq_y];
        for (uint freq_x = 0; freq_x < 8u; ++freq_x) {
            const float coefficient = blocks[block_base + freq_y * 8u + freq_x];
            sample += coefficient * y_basis * idct_basis[local_x * 8u + freq_x];
        }
    }
    return sample;
}

static inline void dct97_forward_lift_in_threadgroup(
    threadgroup float *data,
    uint n,
    uint thread_idx,
    uint threads_per_group
) {
    if (n < 2u) {
        return;
    }

    const uint last_even = ((n % 2u) == 0u) ? n - 2u : n - 1u;

    for (uint i = 1u + thread_idx * 2u; i < n; i += threads_per_group * 2u) {
        const float left = data[i - 1u];
        const float right = (i + 1u < n) ? data[i + 1u] : data[last_even];
        data[i] += CODEC_MATH_DWT97_ALPHA * (left + right);
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint i = thread_idx * 2u; i < n; i += threads_per_group * 2u) {
        const float left = (i > 0u) ? data[i - 1u] : data[1u];
        const float right = (i + 1u < n) ? data[i + 1u] : left;
        data[i] += CODEC_MATH_DWT97_BETA * (left + right);
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint i = 1u + thread_idx * 2u; i < n; i += threads_per_group * 2u) {
        const float left = data[i - 1u];
        const float right = (i + 1u < n) ? data[i + 1u] : data[last_even];
        data[i] += CODEC_MATH_DWT97_GAMMA * (left + right);
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint i = thread_idx * 2u; i < n; i += threads_per_group * 2u) {
        const float left = (i > 0u) ? data[i - 1u] : data[1u];
        const float right = (i + 1u < n) ? data[i + 1u] : left;
        data[i] += CODEC_MATH_DWT97_DELTA * (left + right);
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);
}

kernel void dct97_idct_row_lift_batch(
    device const float *blocks [[buffer(0)]],
    device const float *idct_basis [[buffer(1)]],
    device float *row_low [[buffer(2)]],
    device float *row_high [[buffer(3)]],
    constant Dct97IdctRowLiftParams &params [[buffer(4)]],
    uint3 group_id [[threadgroup_position_in_grid]],
    uint thread_idx [[thread_index_in_threadgroup]]
) {
    threadgroup float rows[DCT97_ROWS_PER_GROUP][DCT97_STAGED_MAX_AXIS];

    const uint row_group = group_id.x;
    const uint item_idx = group_id.y;
    if (params.width > DCT97_STAGED_MAX_AXIS) {
        return;
    }

    for (uint row_offset = 0u; row_offset < DCT97_ROWS_PER_GROUP; ++row_offset) {
        const uint y = row_group * DCT97_ROWS_PER_GROUP + row_offset;
        if (y >= params.height) {
            continue;
        }
        for (uint x = thread_idx; x < params.width; x += DCT97_THREADS_PER_GROUP) {
            rows[row_offset][x] = dct97_idct_sample(blocks, idct_basis, params, item_idx, x, y);
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint row_offset = 0u; row_offset < DCT97_ROWS_PER_GROUP; ++row_offset) {
        const uint y = row_group * DCT97_ROWS_PER_GROUP + row_offset;
        if (y < params.height) {
            dct97_forward_lift_in_threadgroup(
                rows[row_offset], params.width, thread_idx, DCT97_THREADS_PER_GROUP);
        }
    }

    for (uint row_offset = 0u; row_offset < DCT97_ROWS_PER_GROUP; ++row_offset) {
        const uint y = row_group * DCT97_ROWS_PER_GROUP + row_offset;
        if (y >= params.height) {
            continue;
        }
        const uint low_base = item_idx * params.height * params.low_width + y * params.low_width;
        const uint high_base = item_idx * params.height * params.high_width + y * params.high_width;
        for (uint low_x = thread_idx; low_x < params.low_width; low_x += DCT97_THREADS_PER_GROUP) {
            row_low[low_base + low_x] =
                rows[row_offset][low_x * 2u] * CODEC_MATH_DWT97_INV_KAPPA;
        }
        for (uint high_x = thread_idx; high_x < params.high_width; high_x += DCT97_THREADS_PER_GROUP) {
            row_high[high_base + high_x] =
                rows[row_offset][high_x * 2u + 1u] * CODEC_MATH_DWT97_KAPPA;
        }
    }
}

kernel void dct97_column_lift_batch(
    device const float *row_low [[buffer(0)]],
    device const float *row_high [[buffer(1)]],
    device float *ll [[buffer(2)]],
    device float *hl [[buffer(3)]],
    device float *lh [[buffer(4)]],
    device float *hh [[buffer(5)]],
    constant Dct97ColumnLiftParams &params [[buffer(6)]],
    uint3 group_id [[threadgroup_position_in_grid]],
    uint thread_idx [[thread_index_in_threadgroup]]
) {
    threadgroup float columns[DCT97_COLUMNS_PER_GROUP][DCT97_STAGED_MAX_AXIS];

    const uint column_group = group_id.x;
    const uint item_idx = group_id.y;
    const bool horizontal_low = group_id.z == 0u;
    const uint band_width = horizontal_low ? params.low_width : params.high_width;
    if (params.height > DCT97_STAGED_MAX_AXIS) {
        return;
    }

    for (uint column_offset = 0u; column_offset < DCT97_COLUMNS_PER_GROUP; ++column_offset) {
        const uint x = column_group * DCT97_COLUMNS_PER_GROUP + column_offset;
        if (x >= band_width) {
            continue;
        }
        const uint source_stride = horizontal_low ? params.row_low_stride : params.row_high_stride;
        device const float *source = horizontal_low ? row_low : row_high;
        const uint source_width = band_width;
        const uint source_base = item_idx * source_stride + x;
        for (uint y = thread_idx; y < params.height; y += DCT97_THREADS_PER_GROUP) {
            columns[column_offset][y] = source[source_base + y * source_width];
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint column_offset = 0u; column_offset < DCT97_COLUMNS_PER_GROUP; ++column_offset) {
        const uint x = column_group * DCT97_COLUMNS_PER_GROUP + column_offset;
        if (x < band_width) {
            dct97_forward_lift_in_threadgroup(
                columns[column_offset], params.height, thread_idx, DCT97_THREADS_PER_GROUP);
        }
    }

    for (uint column_offset = 0u; column_offset < DCT97_COLUMNS_PER_GROUP; ++column_offset) {
        const uint x = column_group * DCT97_COLUMNS_PER_GROUP + column_offset;
        if (x >= band_width) {
            continue;
        }
        for (uint low_y = thread_idx; low_y < params.low_height; low_y += DCT97_THREADS_PER_GROUP) {
            const float value =
                columns[column_offset][low_y * 2u] * CODEC_MATH_DWT97_INV_KAPPA;
            if (horizontal_low) {
                ll[item_idx * params.ll_stride + low_y * params.low_width + x] = value;
            } else {
                hl[item_idx * params.hl_stride + low_y * params.high_width + x] = value;
            }
        }
        for (uint high_y = thread_idx; high_y < params.high_height; high_y += DCT97_THREADS_PER_GROUP) {
            const float value =
                columns[column_offset][high_y * 2u + 1u] * CODEC_MATH_DWT97_KAPPA;
            if (horizontal_low) {
                lh[item_idx * params.lh_stride + high_y * params.low_width + x] = value;
            } else {
                hh[item_idx * params.hh_stride + high_y * params.high_width + x] = value;
            }
        }
    }
}

kernel void dct97_quantize_codeblocks_batch(
    device const float *band [[buffer(0)]],
    device int *output [[buffer(1)]],
    constant Dct97QuantizeCodeblocksParams &params [[buffer(2)]],
    uint3 gid [[thread_position_in_grid]]
) {
    const uint x = gid.x;
    const uint y = gid.y;
    const uint item_idx = gid.z;
    if (x >= params.band_width || y >= params.band_height) {
        return;
    }

    const float value =
        band[item_idx * params.band_width * params.band_height + y * params.band_width + x];
    const int sign = value < 0.0f ? -1 : 1;
    const int magnitude = int(floor(fabs(value) * params.inv_delta));

    const uint cbx = x / params.code_block_width;
    const uint cby = y / params.code_block_height;
    const uint local_x = x - cbx * params.code_block_width;
    const uint local_y = y - cby * params.code_block_height;
    const uint block_x0 = cbx * params.code_block_width;
    const uint block_y0 = cby * params.code_block_height;
    const uint block_width = min(params.code_block_width, params.band_width - block_x0);
    const uint block_height = min(params.code_block_height, params.band_height - block_y0);
    const uint item_base = item_idx * params.output_stride;
    const uint codeblock_row_base = cby * params.code_block_height * params.band_width;
    const uint codeblock_base = codeblock_row_base + cbx * params.code_block_width * block_height;
    const uint block_offset = local_y * block_width + local_x;

    output[item_base + codeblock_base + block_offset] = sign * magnitude;
}

kernel void dct97_project_band(
    device const float *blocks [[buffer(0)]],
    device const Dct97SparseRow *x_rows [[buffer(1)]],
    device const Dct97WeightTap *x_taps [[buffer(2)]],
    device const Dct97SparseRow *y_rows [[buffer(3)]],
    device const Dct97WeightTap *y_taps [[buffer(4)]],
    device const float *idct_basis [[buffer(5)]],
    device float *output [[buffer(6)]],
    constant Dct97ProjectionParams &params [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    if (gid.x >= params.band_width || gid.y >= params.band_height) {
        return;
    }

    const Dct97SparseRow x_row = x_rows[gid.x];
    const Dct97SparseRow y_row = y_rows[gid.y];
    float value = 0.0f;
    for (uint y_tap_idx = 0; y_tap_idx < y_row.count; ++y_tap_idx) {
        const Dct97WeightTap y_tap = y_taps[y_row.offset + y_tap_idx];
        const uint sample_y = y_tap.sample_idx;
        const float y_weight = y_tap.weight;
        const uint block_y = sample_y / 8u;
        const uint local_y = sample_y % 8u;

        for (uint x_tap_idx = 0; x_tap_idx < x_row.count; ++x_tap_idx) {
            const Dct97WeightTap x_tap = x_taps[x_row.offset + x_tap_idx];
            const uint sample_x = x_tap.sample_idx;
            const float x_weight = x_tap.weight;
            const uint block_x = sample_x / 8u;
            const uint local_x = sample_x % 8u;
            const uint block_base = (block_y * params.block_cols + block_x) * 64u;
            const float sample_weight = y_weight * x_weight;

            for (uint freq_y = 0; freq_y < 8u; ++freq_y) {
                const float y_basis = idct_basis[local_y * 8u + freq_y];
                for (uint freq_x = 0; freq_x < 8u; ++freq_x) {
                    const float coefficient = blocks[block_base + freq_y * 8u + freq_x];
                    const float x_basis = idct_basis[local_x * 8u + freq_x];
                    value += sample_weight * y_basis * x_basis * coefficient;
                }
            }
        }
    }

    output[gid.y * params.band_width + gid.x] = value;
}

kernel void dct97_project_band_batch(
    device const float *blocks [[buffer(0)]],
    device const Dct97SparseRow *x_rows [[buffer(1)]],
    device const Dct97WeightTap *x_taps [[buffer(2)]],
    device const Dct97SparseRow *y_rows [[buffer(3)]],
    device const Dct97WeightTap *y_taps [[buffer(4)]],
    device const float *idct_basis [[buffer(5)]],
    device float *output [[buffer(6)]],
    constant Dct97BatchProjectionParams &params [[buffer(7)]],
    uint3 gid [[thread_position_in_grid]]
) {
    if (gid.x >= params.band_width || gid.y >= params.band_height) {
        return;
    }

    const Dct97SparseRow x_row = x_rows[gid.x];
    const Dct97SparseRow y_row = y_rows[gid.y];
    const uint item_base = gid.z * params.blocks_per_item;
    float value = 0.0f;
    for (uint y_tap_idx = 0; y_tap_idx < y_row.count; ++y_tap_idx) {
        const Dct97WeightTap y_tap = y_taps[y_row.offset + y_tap_idx];
        const uint sample_y = y_tap.sample_idx;
        const float y_weight = y_tap.weight;
        const uint block_y = sample_y / 8u;
        const uint local_y = sample_y % 8u;

        for (uint x_tap_idx = 0; x_tap_idx < x_row.count; ++x_tap_idx) {
            const Dct97WeightTap x_tap = x_taps[x_row.offset + x_tap_idx];
            const uint sample_x = x_tap.sample_idx;
            const float x_weight = x_tap.weight;
            const uint block_x = sample_x / 8u;
            const uint local_x = sample_x % 8u;
            const uint block_base = (item_base + block_y * params.block_cols + block_x) * 64u;
            const float sample_weight = y_weight * x_weight;

            for (uint freq_y = 0; freq_y < 8u; ++freq_y) {
                const float y_basis = idct_basis[local_y * 8u + freq_y];
                for (uint freq_x = 0; freq_x < 8u; ++freq_x) {
                    const float coefficient = blocks[block_base + freq_y * 8u + freq_x];
                    const float x_basis = idct_basis[local_x * 8u + freq_x];
                    value += sample_weight * y_basis * x_basis * coefficient;
                }
            }
        }
    }

    output[gid.z * params.output_stride + gid.y * params.band_width + gid.x] = value;
}

static inline int floor_div_i32(int numerator, int denominator) {
    const int quotient = numerator / denominator;
    const int remainder = numerator % denominator;
    return (remainder < 0) ? quotient - 1 : quotient;
}

static inline int reversible53_sample(
    device const int *blocks,
    uint block_cols,
    uint blocks_per_item,
    uint item_idx,
    uint x,
    uint y
) {
    const uint block_x = x / 8u;
    const uint block_y = y / 8u;
    const uint local_x = x % 8u;
    const uint local_y = y % 8u;
    const uint item_block_base = item_idx * blocks_per_item;
    const uint block_base = (item_block_base + block_y * block_cols + block_x) * 64u;
    return blocks[block_base + local_y * 8u + local_x];
}

static inline int reversible53_vertical_high(
    device const int *blocks,
    constant Reversible53ProjectionParams &params,
    uint item_idx,
    uint x,
    uint high_idx
) {
    const uint odd_idx = high_idx * 2u + 1u;
    const int current = reversible53_sample(
        blocks, params.block_cols, params.blocks_per_item, item_idx, x, odd_idx);
    const int left = reversible53_sample(
        blocks, params.block_cols, params.blocks_per_item, item_idx, x, odd_idx - 1u);
    if ((params.height % 2u) == 0u && odd_idx + 1u == params.height) {
        return current - left;
    }

    const uint right_idx = (odd_idx + 1u < params.height) ? odd_idx + 1u : params.height - 1u;
    const int right = reversible53_sample(
        blocks, params.block_cols, params.blocks_per_item, item_idx, x, right_idx);
    return current - floor_div_i32(left + right, 2);
}

static inline int reversible53_vertical_low(
    device const int *blocks,
    constant Reversible53ProjectionParams &params,
    uint item_idx,
    uint x,
    uint low_idx
) {
    const uint even_idx = low_idx * 2u;
    const int current = reversible53_sample(
        blocks, params.block_cols, params.blocks_per_item, item_idx, x, even_idx);
    if (params.height < 2u) {
        return current;
    }

    if ((params.height % 2u) == 0u) {
        const int right = reversible53_vertical_high(blocks, params, item_idx, x, low_idx);
        if (low_idx == 0u) {
            return current + floor_div_i32(right + 1, 2);
        }
        const int left = reversible53_vertical_high(blocks, params, item_idx, x, low_idx - 1u);
        return current + floor_div_i32(left + right + 2, 4);
    }

    const uint high_len = params.height / 2u;
    if (high_len == 0u) {
        return current;
    }
    const int left = low_idx > 0u
        ? reversible53_vertical_high(blocks, params, item_idx, x, low_idx - 1u)
        : reversible53_vertical_high(blocks, params, item_idx, x, 0u);
    const int right = low_idx < high_len
        ? reversible53_vertical_high(blocks, params, item_idx, x, low_idx)
        : left;
    return current + floor_div_i32(left + right + 2, 4);
}

static inline int reversible53_vertical_value(
    device const int *blocks,
    constant Reversible53ProjectionParams &params,
    uint item_idx,
    uint x,
    uint output_y
) {
    return params.vertical_low != 0u
        ? reversible53_vertical_low(blocks, params, item_idx, x, output_y)
        : reversible53_vertical_high(blocks, params, item_idx, x, output_y);
}

static inline int reversible53_horizontal_high(
    device const int *blocks,
    constant Reversible53ProjectionParams &params,
    uint item_idx,
    uint high_idx,
    uint output_y
) {
    const uint odd_idx = high_idx * 2u + 1u;
    const int current = reversible53_vertical_value(blocks, params, item_idx, odd_idx, output_y);
    const int left = reversible53_vertical_value(blocks, params, item_idx, odd_idx - 1u, output_y);
    if ((params.width % 2u) == 0u && odd_idx + 1u == params.width) {
        return current - left;
    }

    const uint right_idx = (odd_idx + 1u < params.width) ? odd_idx + 1u : params.width - 1u;
    const int right = reversible53_vertical_value(blocks, params, item_idx, right_idx, output_y);
    return current - floor_div_i32(left + right, 2);
}

static inline int reversible53_horizontal_low(
    device const int *blocks,
    constant Reversible53ProjectionParams &params,
    uint item_idx,
    uint low_idx,
    uint output_y
) {
    const uint even_idx = low_idx * 2u;
    const int current = reversible53_vertical_value(blocks, params, item_idx, even_idx, output_y);
    if (params.width < 2u) {
        return current;
    }

    if ((params.width % 2u) == 0u) {
        const int right = reversible53_horizontal_high(
            blocks, params, item_idx, low_idx, output_y);
        if (low_idx == 0u) {
            return current + floor_div_i32(right + 1, 2);
        }
        const int left = reversible53_horizontal_high(
            blocks, params, item_idx, low_idx - 1u, output_y);
        return current + floor_div_i32(left + right + 2, 4);
    }

    const uint high_len = params.width / 2u;
    if (high_len == 0u) {
        return current;
    }
    const int left = low_idx > 0u
        ? reversible53_horizontal_high(blocks, params, item_idx, low_idx - 1u, output_y)
        : reversible53_horizontal_high(blocks, params, item_idx, 0u, output_y);
    const int right = low_idx < high_len
        ? reversible53_horizontal_high(blocks, params, item_idx, low_idx, output_y)
        : left;
    return current + floor_div_i32(left + right + 2, 4);
}

kernel void reversible53_project_band(
    device const int *blocks [[buffer(0)]],
    device int *output [[buffer(1)]],
    constant Reversible53ProjectionParams &params [[buffer(2)]],
    uint3 gid [[thread_position_in_grid]]
) {
    if (gid.x >= params.band_width || gid.y >= params.band_height) {
        return;
    }

    const int value = params.horizontal_low != 0u
        ? reversible53_horizontal_low(blocks, params, gid.z, gid.x, gid.y)
        : reversible53_horizontal_high(blocks, params, gid.z, gid.x, gid.y);
    output[gid.z * params.output_stride + gid.y * params.band_width + gid.x] = value;
}