j2k-metal 0.10.0

Metal decoder and encode-stage adapter for j2k
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
inline uint coeff_index(uint padded_width, uint index_x, uint index_y) {
    return index_x + index_y * padded_width;
}

inline void set_classic_status(device J2kClassicStatus *status, uint code, uint detail) {
    status->code = code;
    status->detail = detail;
    status->reserved0 = 0u;
    status->reserved1 = 0u;
}

inline uchar state_bit(thread const uchar *states, uint idx, uchar shift) {
    return (states[idx] >> shift) & uchar(1u);
}

inline void set_state_bit(thread uchar *states, uint idx, uchar shift, uchar value) {
    states[idx] = uchar((states[idx] & uchar(~(1u << shift))) | ((value & 1u) << shift));
}

inline uchar state_bit_dev(device const uchar *states, uint idx, uchar shift) {
    return (states[idx] >> shift) & uchar(1u);
}

inline void set_state_bit_dev(device uchar *states, uint idx, uchar shift, uchar value) {
    states[idx] = uchar((states[idx] & uchar(~(1u << shift))) | ((value & 1u) << shift));
}

inline uchar state_bit_tg(threadgroup const uchar *states, uint idx, uchar shift) {
    return (states[idx] >> shift) & uchar(1u);
}

inline void set_state_bit_tg(threadgroup uchar *states, uint idx, uchar shift, uchar value) {
    states[idx] = uchar((states[idx] & uchar(~(1u << shift))) | ((value & 1u) << shift));
}

inline uint coeff_sign(thread const uchar *states, uint idx) {
    return uint(state_bit(states, idx, J2K_SIGN_SHIFT));
}

inline uint coeff_sign_dev(device const uchar *states, uint idx) {
    return uint(state_bit_dev(states, idx, J2K_SIGN_SHIFT));
}

inline uint coeff_sign_tg(threadgroup const uchar *states, uint idx) {
    return uint(state_bit_tg(states, idx, J2K_SIGN_SHIFT));
}

inline void coeff_push_bit(device uint *coefficients, uint idx, uint bit, uint position) {
    coefficients[idx] |= (bit << position);
}

inline void coeff_set_sign_packed(device uint *coefficients, uint idx, uint sign) {
    if (sign != 0u) {
        coefficients[idx] |= 0x80000000u;
    } else {
        coefficients[idx] &= 0x7FFFFFFFu;
    }
}

inline void coeff_set_sign(thread uchar *states, uint idx, uint sign) {
    set_state_bit(states, idx, J2K_SIGN_SHIFT, uchar(sign));
}

inline void coeff_set_sign_dev(device uchar *states, uint idx, uint sign) {
    set_state_bit_dev(states, idx, J2K_SIGN_SHIFT, uchar(sign));
}

inline void coeff_set_sign_tg(threadgroup uchar *states, uint idx, uint sign) {
    set_state_bit_tg(states, idx, J2K_SIGN_SHIFT, uchar(sign));
}

inline uchar coeff_is_significant(thread const uchar *states, uint idx) {
    return state_bit(states, idx, J2K_SIG_SHIFT);
}

inline uchar coeff_is_significant_dev(device const uchar *states, uint idx) {
    return state_bit_dev(states, idx, J2K_SIG_SHIFT);
}

inline uchar coeff_is_significant_tg(threadgroup const uchar *states, uint idx) {
    return state_bit_tg(states, idx, J2K_SIG_SHIFT);
}

inline uchar coeff_zero_coded_marker(thread const uchar *states, uint idx) {
    return states[idx] & J2K_STATE_MARKER_MASK;
}

inline uchar coeff_zero_coded_marker_dev(device const uchar *states, uint idx) {
    return states[idx] & J2K_STATE_MARKER_MASK;
}

inline uchar coeff_zero_coded_marker_tg(threadgroup const uchar *states, uint idx) {
    return states[idx] & J2K_STATE_MARKER_MASK;
}

inline uchar coeff_is_zero_coded(thread const uchar *states, uint idx, uchar marker) {
    return uchar(marker != 0u && coeff_zero_coded_marker(states, idx) == marker);
}

inline uchar coeff_is_zero_coded_dev(device const uchar *states, uint idx, uchar marker) {
    return uchar(marker != 0u && coeff_zero_coded_marker_dev(states, idx) == marker);
}

inline uchar coeff_is_zero_coded_tg(threadgroup const uchar *states, uint idx, uchar marker) {
    return uchar(marker != 0u && coeff_zero_coded_marker_tg(states, idx) == marker);
}

inline void coeff_set_zero_coded_marker(thread uchar *states, uint idx, uchar marker) {
    states[idx] = uchar((states[idx] & uchar(0xE0u)) | (marker & J2K_STATE_MARKER_MASK));
}

inline void coeff_set_zero_coded_marker_dev(device uchar *states, uint idx, uchar marker) {
    states[idx] = uchar((states[idx] & uchar(0xE0u)) | (marker & J2K_STATE_MARKER_MASK));
}

inline void coeff_set_zero_coded_marker_tg(threadgroup uchar *states, uint idx, uchar marker) {
    states[idx] = uchar((states[idx] & uchar(0xE0u)) | (marker & J2K_STATE_MARKER_MASK));
}

inline uchar coeff_is_magnitude_refined(thread const uchar *states, uint idx) {
    return state_bit(states, idx, J2K_MAG_REF_SHIFT);
}

inline uchar coeff_is_magnitude_refined_dev(device const uchar *states, uint idx) {
    return state_bit_dev(states, idx, J2K_MAG_REF_SHIFT);
}

inline uchar coeff_is_magnitude_refined_tg(threadgroup const uchar *states, uint idx) {
    return state_bit_tg(states, idx, J2K_MAG_REF_SHIFT);
}

inline void coeff_set_magnitude_refined(thread uchar *states, uint idx) {
    set_state_bit(states, idx, J2K_MAG_REF_SHIFT, uchar(1u));
}

inline void coeff_set_magnitude_refined_dev(device uchar *states, uint idx) {
    set_state_bit_dev(states, idx, J2K_MAG_REF_SHIFT, uchar(1u));
}

inline void coeff_set_magnitude_refined_tg(threadgroup uchar *states, uint idx) {
    set_state_bit_tg(states, idx, J2K_MAG_REF_SHIFT, uchar(1u));
}

inline float reconstructed_classic_sample(
    uint coefficient,
    J2kClassicCleanupBatchJob job
) {
    const uint magnitude = coefficient & 0x7FFFFFFFu;
    const uint decoded_bitplanes =
        job.total_bitplanes + job.roi_shift - job.missing_msbs;
    float reconstructed;
    if (job.irreversible_midpoint != 0u && magnitude != 0u &&
        job.number_of_coding_passes != 0u) {
        const uint final_pass = job.number_of_coding_passes - 1u;
        const uint decoded_plane = (final_pass + 2u) / 3u;
        if (decoded_bitplanes > decoded_plane) {
            uint lowest_decoded_bit = decoded_bitplanes - decoded_plane - 1u;
            if (final_pass % 3u == 1u &&
                (magnitude & (1u << lowest_decoded_bit)) == 0u) {
                lowest_decoded_bit += 1u;
            }
            uint fixed_magnitude =
                (magnitude << 1u) | (1u << lowest_decoded_bit);
            if (job.roi_shift != 0u &&
                fixed_magnitude >= (1u << job.roi_shift)) {
                fixed_magnitude >>= job.roi_shift;
            }
            reconstructed = float(fixed_magnitude) * 0.5f;
        } else {
            reconstructed = float(magnitude);
        }
    } else {
        uint reconstructed_magnitude = magnitude;
        if (job.roi_shift != 0u &&
            reconstructed_magnitude >= (1u << job.roi_shift)) {
            reconstructed_magnitude >>= job.roi_shift;
        }
        reconstructed = float(reconstructed_magnitude);
    }
    return (coefficient & 0x80000000u) != 0u ? -reconstructed : reconstructed;
}

inline bool classic_decoded_bitplanes(
    J2kClassicCleanupBatchJob job,
    thread uint &bitplanes
) {
    if (job.total_bitplanes == 0u || job.total_bitplanes > 31u ||
        job.roi_shift > 31u - job.total_bitplanes) {
        return false;
    }
    const uint coded_bitplanes = job.total_bitplanes + job.roi_shift;
    if (job.missing_msbs >= coded_bitplanes) {
        return false;
    }
    bitplanes = coded_bitplanes - job.missing_msbs;
    return true;
}

inline void reset_contexts(thread uchar *contexts) {
    for (uint idx = 0u; idx < 19u; ++idx) {
        contexts[idx] = uchar(0);
    }
    contexts[0] = uchar(4u);
    contexts[17] = uchar(3u);
    contexts[18] = uchar(46u);
}

inline uchar zero_context_label(uchar neighbors, uint sub_band_type) {
    if (sub_band_type == 1u) {
        return ZERO_CTX_HL_LOOKUP[neighbors];
    }
    if (sub_band_type == 3u) {
        return ZERO_CTX_HH_LOOKUP[neighbors];
    }
    return ZERO_CTX_LL_LH_LOOKUP[neighbors];
}

inline uchar neighborhood_states(thread const uchar *states, uint padded_width, uint index_x, uint index_y) {
    return uchar(
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x, index_y + 1u))) << 0u) |
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x + 1u, index_y + 1u))) << 1u) |
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x + 1u, index_y))) << 2u) |
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x - 1u, index_y + 1u))) << 3u) |
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x - 1u, index_y))) << 4u) |
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x + 1u, index_y - 1u))) << 5u) |
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x, index_y - 1u))) << 6u) |
        (uint(coeff_is_significant(states, coeff_index(padded_width, index_x - 1u, index_y - 1u))) << 7u)
    );
}

inline bool neighbor_in_next_stripe(uint index_y, uint height) {
    const uint real_y = index_y - J2K_CLASSIC_PADDING;
    return real_y + 1u < height && ((real_y + 1u) >> 2u) > (real_y >> 2u);
}

inline uchar effective_neighborhood_states(
    thread const uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y,
    uint height,
    uint style_flags
) {
    uchar states_mask = neighborhood_states(states, padded_width, index_x, index_y);
    if ((style_flags & J2K_CLASSIC_STYLE_VERTICALLY_CAUSAL_CONTEXT) != 0u &&
        neighbor_in_next_stripe(index_y, height)) {
        states_mask &= uchar(0b11110100);
    }
    return states_mask;
}

inline void set_significant(
    thread uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y
) {
    const uint idx = coeff_index(padded_width, index_x, index_y);
    set_state_bit(states, idx, J2K_SIG_SHIFT, uchar(1u));
}

inline uchar neighborhood_states_plain_dev(
    device const uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y
) {
    return uchar(
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x, index_y + 1u))) << 0u) |
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x + 1u, index_y + 1u))) << 1u) |
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x + 1u, index_y))) << 2u) |
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x - 1u, index_y + 1u))) << 3u) |
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x - 1u, index_y))) << 4u) |
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x + 1u, index_y - 1u))) << 5u) |
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x, index_y - 1u))) << 6u) |
        (uint(coeff_is_significant_dev(states, coeff_index(padded_width, index_x - 1u, index_y - 1u))) << 7u)
    );
}

inline uchar neighborhood_states_plain_tg(
    threadgroup const uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y
) {
    return uchar(
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x, index_y + 1u))) << 0u) |
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x + 1u, index_y + 1u))) << 1u) |
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x + 1u, index_y))) << 2u) |
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x - 1u, index_y + 1u))) << 3u) |
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x - 1u, index_y))) << 4u) |
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x + 1u, index_y - 1u))) << 5u) |
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x, index_y - 1u))) << 6u) |
        (uint(coeff_is_significant_tg(states, coeff_index(padded_width, index_x - 1u, index_y - 1u))) << 7u)
    );
}

inline void set_significant_plain_dev(
    device uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y
) {
    const uint idx = coeff_index(padded_width, index_x, index_y);
    set_state_bit_dev(states, idx, J2K_SIG_SHIFT, uchar(1u));
}

inline void set_significant_plain_tg(
    threadgroup uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y
) {
    const uint idx = coeff_index(padded_width, index_x, index_y);
    set_state_bit_tg(states, idx, J2K_SIG_SHIFT, uchar(1u));
}

inline uchar magnitude_refinement_context(
    thread const uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y,
    uint height,
    uint style_flags
) {
    const uint idx = coeff_index(padded_width, index_x, index_y);
    const uchar m1 = coeff_is_magnitude_refined(states, idx) * uchar(16u);
    const uchar m2 = uchar(14u + min(uint(effective_neighborhood_states(
        states,
        padded_width,
        index_x,
        index_y,
        height,
        style_flags
    )), 1u));
    return max(m1, m2);
}

inline uchar2 sign_context(
    thread const uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y,
    uint height,
    uint style_flags
) {
    const uchar significances =
        effective_neighborhood_states(
            states,
            padded_width,
            index_x,
            index_y,
            height,
            style_flags
        ) & uchar(0b01010101);
    const uint left_sign = coeff_sign(states, coeff_index(padded_width, index_x - 1u, index_y));
    const uint right_sign = coeff_sign(states, coeff_index(padded_width, index_x + 1u, index_y));
    const uint top_sign = coeff_sign(states, coeff_index(padded_width, index_x, index_y - 1u));
    const uint bottom_sign =
        ((style_flags & J2K_CLASSIC_STYLE_VERTICALLY_CAUSAL_CONTEXT) != 0u &&
            neighbor_in_next_stripe(index_y, height))
        ? 0u
        : coeff_sign(states, coeff_index(padded_width, index_x, index_y + 1u));
    const uchar signs = uchar((top_sign << 6u) | (left_sign << 4u) | (right_sign << 2u) | bottom_sign);
    const uchar negative = significances & signs;
    const uchar positive = significances & uchar(~signs);
    return SIGN_CONTEXT_LOOKUP[uchar((negative << 1u) | positive)];
}

inline uchar2 sign_context_plain_dev(
    device const uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y
) {
    const uchar significances =
        neighborhood_states_plain_dev(states, padded_width, index_x, index_y) & uchar(0b01010101);
    const uint left_sign = coeff_sign_dev(states, coeff_index(padded_width, index_x - 1u, index_y));
    const uint right_sign = coeff_sign_dev(states, coeff_index(padded_width, index_x + 1u, index_y));
    const uint top_sign = coeff_sign_dev(states, coeff_index(padded_width, index_x, index_y - 1u));
    const uint bottom_sign = coeff_sign_dev(states, coeff_index(padded_width, index_x, index_y + 1u));
    const uchar signs = uchar((top_sign << 6u) | (left_sign << 4u) | (right_sign << 2u) | bottom_sign);
    const uchar negative = significances & signs;
    const uchar positive = significances & uchar(~signs);
    return SIGN_CONTEXT_LOOKUP[uchar((negative << 1u) | positive)];
}

inline uchar2 sign_context_plain_tg(
    threadgroup const uchar *states,
    uint padded_width,
    uint index_x,
    uint index_y
) {
    const uchar significances =
        neighborhood_states_plain_tg(states, padded_width, index_x, index_y) & uchar(0b01010101);
    const uint left_sign = coeff_sign_tg(states, coeff_index(padded_width, index_x - 1u, index_y));
    const uint right_sign = coeff_sign_tg(states, coeff_index(padded_width, index_x + 1u, index_y));
    const uint top_sign = coeff_sign_tg(states, coeff_index(padded_width, index_x, index_y - 1u));
    const uint bottom_sign = coeff_sign_tg(states, coeff_index(padded_width, index_x, index_y + 1u));
    const uchar signs = uchar((top_sign << 6u) | (left_sign << 4u) | (right_sign << 2u) | bottom_sign);
    const uchar negative = significances & signs;
    const uchar positive = significances & uchar(~signs);
    return SIGN_CONTEXT_LOOKUP[uchar((negative << 1u) | positive)];
}