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
|
/*
* Copyright (c) 2024 Lynne <dev@lynne.ee>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it 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.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
void put_rac(inout RangeCoder c, uint64_t state, bool bit)
{
put_rac_norenorm(c, state, bit);
if (c.range < 0x100)
renorm_encoder(c);
}
/* Note - only handles signed values */
void put_symbol(inout RangeCoder c, uint64_t state, int v)
{
bool is_nil = (v == 0);
put_rac(c, state, is_nil);
if (is_nil)
return;
const int a = abs(v);
const int e = findMSB(a);
state += 1;
for (int i = 0; i < e; i++)
put_rac(c, state + min(i, 9), true);
put_rac(c, state + min(e, 9), false);
state += 21;
for (int i = e - 1; i >= 0; i--)
put_rac(c, state + min(i, 9), bool(bitfieldExtract(a, i, 1)));
put_rac(c, state - 11 + min(e, 10), v < 0);
}
void encode_line_pcm(inout SliceContext sc, int y, int p, int comp,
int bits)
{
ivec2 sp = sc.slice_pos;
int w = sc.slice_dim.x;
if (p > 0 && p < 3) {
w >>= chroma_shift.x;
sp >>= chroma_shift;
}
for (int x = 0; x < w; x++) {
uint v = imageLoad(src[p], (sp + ivec2(x, y)))[comp];
for (int i = (bits - 1); i >= 0; i--)
put_rac_equi(sc.c, bool(bitfieldExtract(v, i, 1)));
}
}
void encode_line(inout SliceContext sc, uint64_t state,
int y, int p, int comp, int bits, const int run_index)
{
ivec2 sp = sc.slice_pos;
int w = sc.slice_dim.x;
if (p > 0 && p < 3) {
w >>= chroma_shift.x;
sp >>= chroma_shift;
}
for (int x = 0; x < w; x++) {
const ivec2 d = get_diff(sp + ivec2(x, y), ivec2(x, y), p, comp, w, bits);
put_symbol(sc.c, state + CONTEXT_SIZE*d[0], d[1]);
}
}
|