35 uint16_t *freqs, uint16_t *freqs1,
36 uint16_t *cnts,
uint8_t *dectab)
38 uint32_t
a = 0,
b = 4096 / nsym,
c =
b - (
b >> 1);
42 for (
int d = 0; d < nsym; d++) {
46 for (
int q = a + 128 - 1 >> 7,
f = (a +
b - 1 >> 7) + 1; q <
f; q++)
55 for (
int i = 0;
i < 3;
i++) {
56 for (
int j = 0; j < 4096; j++) {
62 for (
int i = 0;
i < 6;
i++) {
80 for (
int i = 0;
i < 4;
i++) {
86 for (
int i = 0;
i < 2;
i++) {
92 for (
int i = 0;
i < 6;
i++) {
101 uint32_t
code = a * (rc->
code >> 12) + (rc->
code & 0xFFF) -
b;
104 code = bytestream2_get_byteu(gb) | (code << 8);
115 for (
int b = 0;
b < m->
size;
b++) {
128 for (
int c = m->
size - 1;
c >= index;
c--) {
141 if (*totfr + 50 > 4096)
148 uint16_t *
a, uint16_t *
b, uint32_t *
c,
int max)
150 uint32_t q,
g, maxpos, d, e = *
c, totfr = *
c;
153 for (d = 0; e <= 2048; d++)
157 *c = m->
freqs[maxpos];
158 m->
freqs[maxpos] += 4096 - e >> d;
160 for (q = 0, g = 0, e = 0; q < m->
size; q++) {
162 uint32_t p = e + f -
g;
163 uint32_t k = m->
freqs[q];
166 *value = rccode - e +
g;
175 if (p + k > rccode) {
183 if ((q != maxpos) && (m->
freqs[q] > m->
freqs[maxpos]))
185 if (totfr + 50 > 4096)
196 *value = g + rccode - e;
208 uint16_t *freqs, *freqs1, *cnts;
217 for (i = 0; i <
length; i++) {
227 for (j = 0, e = 0; j < 256; j++) {
236 k = ((f + e - 1) >> 7) + 1;
239 for (i = 0; i < k - p; i++)
244 memcpy(m, &n,
sizeof(n));
256 for (
int c = 0;
c <
len;
c++)
263 uint16_t cnts[256] = {0};
264 uint16_t freqs[512] = {0};
268 for (a = 1 << (0 < m->
fshift ? m->
fshift - 1 : 0), b = 0; b < 256; b++)
271 for (a = 0, b = m->
size; a < b; a++)
274 for (b = a = 0; b < 256; b++) {
275 freqs[2 *
b] = cnts[
b];
276 freqs[2 * b + 1] =
a;
284 for (b = 0, c = m->
size; b < c; b++) {
288 g = freqs[2 * e + 1];
289 m->
freqs[2 *
b] = freqs[2 * e];
298 int c, d, e,
f,
g, k, q, p;
303 for (c = m->
size, d = 256 - c, e = 0; e < c; e++)
306 for (e = 0; d <= 2048; e++)
309 for (q = d = 0, g = q = 0; g <
c; g++) {
315 n.
freqs[2 * g + 1] = d << e;
316 n.
cnts[
g] = k - (k >> 1);
327 for (p = f = g = 0; p <
c; p++) {
329 if (k > d && k < value) {
332 f = n.
freqs[2 * p + 1];
335 d = 0 < g ? f + g + (value - d - 1 << n.
fshift) : value << n.
fshift;
338 n.
freqs[2 * c + 1] = d;
339 n.
cnts[
c] = e - (e >> 1);
345 if (n.
cnts[32] + e > 4096)
349 for (c = 0, e = n.
size - 1; c < e; c++) {
350 for (g = c + 1, f = n.
size; g < f; g++) {
351 if (q = n.
freqs[2 * g], k = n.
freqs[2 * c], q > k) {
352 int l = n.
freqs[2 * c + 1];
353 int h = n.
freqs[2 * g + 1];
357 n.
freqs[2 * g + 1] = l;
364 memcpy(m, &n,
sizeof(n));
388 m->
freqs[2 * size + 1] = f2;
403 if (a > 0 && m->
cnts[a] > m->
cnts[a - 1]) {
406 e = m->
freqs[2 * a + 1];
407 g = m->
freqs[2 * (a - 1) + 1];
412 m->
freqs[2 * g + 1] = e;
416 if (m->
cnts[len] + b > 4096)
421 uint16_t *
a, uint16_t *
b)
423 int c, d, e,
f,
g, q;
425 for (c = 0, d = 0, e = 0, f = 0, g = 0, q = m->
size; g < q; g++) {
426 uint32_t p = m->
freqs[2 * g + 1];
429 uint32_t k = m->
freqs[2 *
g];
451 f = code - (d +
c) >> m->
fshift;
453 f = d + c + (f << m->
fshift);
475 static int cmpbytes(
const void *p1,
const void *p2)
478 int right = *(
const uint8_t *)p2;
490 for (i = 0; i <
b; i++)
494 memcpy(m, &n,
sizeof(n));
507 for (i = 0; i < n.
size; i++) {
511 for (i = 0; i < n.
size; i++) {
520 memcpy(m, &n,
sizeof(n));
533 for (i = 0; i <
size; i++, a += freqs)
546 for (
int i = 0;
i <
size;
i++) {
566 int c, d, e,
f,
g, q;
574 d = 256 - c + (64 * c + 64);
575 for (e = 0; d <= 2048; e++) {
581 for (f = d = 0; f <
c; f++) {
595 n.
freqs[2 * p + 1] = g << e;
597 n.
cnts[p] = l - (l >> 1);
608 g = n.
freqs[2 * d + 1];
612 n.
freqs[2 * d + 1] = e;
617 memcpy(m, &n,
sizeof(n));
635 memcpy(m, &n,
sizeof(n));
645 for (
int i = 0;
i <
size;
i++) {
669 int c, d, e,
f,
g, q;
673 for (c = 0; c < 256; c++) {
679 for (c = m->
size, d = (4096 - (256 - c)) / (c + 1) | 0, e = d - (d >> 1), g = 0; g < c;) {
687 for (d = c = n.
cntsum = 0; 256 > d; d++) {
692 f = (c + g - 1 >> 7) + 1;
695 for (q = c + 128 - 1 >> 7; q <
f; q++) {
701 memcpy(m, &n,
sizeof(n));
710 for (
int i = 0;
i <
size;
i++) {
726 if (rc->
code1 == 0x20000) {
727 rc->
code = bytestream2_get_le32(gb);
733 uint16_t *freqs1, uint16_t *freqs2,
734 uint16_t *cnts,
uint8_t *dectable,
739 uint32_t
r, y,
a,
b, e,
g, q;
741 r = dectable[(rc->
code & 0xFFF
u) >> 7];
743 while (freqs2[r + 1] <= (rc->
code & 0xFFF)) {
756 if (*cntsum + 16 > 4096) {
758 for (
int c = 0,
i = 0;
i < max + 1;
i++) {
764 q = ((
c - 1) >> 7) + 1;
766 for (
int j = 0; j < q -
g; j++)
788 for (
int b = 0;
b < m->
size;
b++)
807 e += n.
freqs[g++] = 50;
808 for (; c < m->
size; g++, c++) {
818 memcpy(m, &n,
sizeof(n));
827 uint16_t
a = 0,
b = 0;
835 *value = bytestream2_get_byte(&s->
gb);
842 *value = bytestream2_get_byte(&s->
gb);
847 *value = bytestream2_get_byte(&s->
gb);
852 *value = bytestream2_get_byte(&s->
gb);
893 uint32_t *green, uint32_t *blue,
903 *cx1 = (*cx << 6) & 0xFC0;
910 *cx1 = (*cx << 6) & 0xFC0;
917 *cx1 = (*cx << 6) & 0xFC0;
925 rc->
code = bytestream2_get_le32(gb);
934 int cx = 0, cx1 = 0, k = 0;
935 int run, off, y = 0, x = 0,
ret;
936 uint32_t backstep = linesize - avctx->
width;
937 uint32_t clr = 0, lx, ly, ptype,
r,
g,
b;
943 while (k < avctx->
width + 1) {
957 clr = (b << 16) + (g << 8) +
r;
963 dst[y * linesize + x] = clr;
967 if (x >= avctx->
width) {
988 clr = (b << 16) + (g << 8) +
r;
1003 dst, linesize, &lx, &ly,
1004 backstep, off, &cx, &cx1);
1013 uint32_t *dst,
int linesize,
1014 uint32_t *prev,
int plinesize)
1019 int backstep = linesize - avctx->
width;
1020 int mvx = 0, mvy = 0;
1022 if (bytestream2_get_byte(gb) == 0)
1054 if (min > max || min >= s->
nbcount)
1059 while (min <= max) {
1077 while (min < s->nbcount && count-- > 0) {
1086 for (y = 0; y < s->
nby; y++) {
1087 for (x = 0; x < s->
nbx; x++) {
1088 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
1093 if (((s->
blocks[y * s->
nbx + x] + 1) & 1) > 0) {
1120 if (((s->
blocks[y * s->
nbx + x] + 3) & 2) > 0) {
1121 int i,
a,
b,
c, j, by = y * 16, bx = x * 16;
1133 code = a + ((s->
rc.
code >> 1) & 0xFFFFF800) -
b;
1135 code = bytestream2_get_byteu(gb) | (code << 8);
1158 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
1159 by + mvy + sy1 >= avctx->
height || bx + mvx + sx1 >= avctx->
width)
1162 for (i = 0; i < sy2 - sy1 && (by + sy1 +
i) < avctx->
height && (by + mvy + sy1 + i) < avctx->
height; i++) {
1163 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->
width && (bx + mvx + sx1 + j) < avctx->
width; j++) {
1164 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
1168 int run, bx = x * 16 + sx1, by = y * 16 + sy1;
1169 uint32_t clr, ptype = 0,
r,
g,
b;
1171 for (; by < y * 16 + sy2 && by < avctx->
height;) {
1184 clr = (b << 16) + (g << 8) +
r;
1199 dst, prev, linesize, plinesize, &bx, &by,
1200 backstep, sx1, sx2, &cx, &cx1);
static int update_model2_to_6(PixelModel3 *m, uint8_t value, int a4)
static int decode_run_i(AVCodecContext *avctx, uint32_t ptype, int run, int *px, int *py, uint32_t clr, uint32_t *dst, int linesize, uint32_t *plx, uint32_t *ply, uint32_t backstep, int off, int *cx, int *cx1)
static int add_symbol(PixelModel3 *m, int index, uint32_t symbol, int *totfr, int max)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void reinit_tables3(SCPRContext *s)
PixelModel3 pixel_model3[3][4096]
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static int decode_unit3(SCPRContext *s, PixelModel3 *m, uint32_t code, uint32_t *value)
#define FF_ARRAY_ELEMS(a)
static void sync_code3(GetByteContext *gb, RangeCoder *rc)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static int decompress_p3(AVCodecContext *avctx, uint32_t *dst, int linesize, uint32_t *prev, int plinesize)
static int update_model1_to_2(PixelModel3 *m, uint32_t val)
GLsizei GLboolean const GLfloat * value
#define u(width, name, range_min, range_max)
static int decode_run_p(AVCodecContext *avctx, uint32_t ptype, int run, int x, int y, uint32_t clr, uint32_t *dst, uint32_t *prev, int linesize, int plinesize, uint32_t *bx, uint32_t *by, uint32_t backstep, int sx1, int sx2, int *cx, int *cx1)
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
static int decode_adaptive45(PixelModel3 *m, int rccode, uint32_t *value, uint16_t *a, uint16_t *b, uint32_t *c, int max)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
static int decode3(GetByteContext *gb, RangeCoder *rc, uint32_t a, uint32_t b)
static void calc_sum5(PixelModel3 *m)
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
static void grow_dec(PixelModel3 *m)
#define FFDIFFSIGN(x, y)
Comparator.
int width
picture width / height.
static int update_model1_to_5(PixelModel3 *m, uint32_t val)
static void rescale(PixelModel3 *m, int *totfr)
static int update_model4_to_5(PixelModel3 *m, uint32_t value)
static int update_model5_to_6(PixelModel3 *m, uint8_t value)
static int add_dec(PixelModel3 *m, int sym, int f1, int f2)
Libavcodec external API header.
main external API structure.
static void init_rangecoder3(RangeCoder *rc, GetByteContext *gb)
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
static void incr_cntdec(PixelModel3 *m, int a)
static void renew_table3(uint32_t nsym, uint32_t *cntsum, uint16_t *freqs, uint16_t *freqs1, uint16_t *cnts, uint8_t *dectab)
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
static void rescale_dec(PixelModel3 *m)
static int decode_adaptive6(PixelModel3 *m, uint32_t code, uint32_t *value, uint16_t *a, uint16_t *b)
common internal api header.
static int update_model2_to_3(PixelModel3 *m, uint32_t val)
static void calc_sum(PixelModel3 *m)
static int decode_static2(PixelModel3 *m, uint32_t val)
static int update_model6_to_7(PixelModel3 *m)
static int decompress_i3(AVCodecContext *avctx, uint32_t *dst, int linesize)
static int update_model3_to_7(PixelModel3 *m, uint8_t value)
#define FFSWAP(type, a, b)
static int decode_units3(SCPRContext *s, uint32_t *red, uint32_t *green, uint32_t *blue, int *cx, int *cx1)
static int decode_static1(PixelModel3 *m, uint32_t val)
static int cmpbytes(const void *p1, const void *p2)
static double val(void *priv, double ch)
static int update_model1_to_4(PixelModel3 *m, uint32_t val)
static int decode_static3(PixelModel3 *m, uint32_t val)
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
static int decode_value3(SCPRContext *s, uint32_t max, uint32_t *cntsum, uint16_t *freqs1, uint16_t *freqs2, uint16_t *cnts, uint8_t *dectable, uint32_t *value)