23 #include "libavutil/imgutils.h"
32 #define BITSTREAM_READER_LE
35 #define BINK_FLAG_ALPHA 0x00100000
36 #define BINK_FLAG_GRAY 0x00020000
59 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
63 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
94 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
95 bink_trees[(tree).vlc_num].bits, 1)]
227 }
while (size && size2);
243 uint8_t tmp1[16] = { 0 }, tmp2[16], *
in = tmp1, *
out = tmp2;
248 for (i = 0; i < 16; i++)
254 for (i = 0; i <=
len; i++) {
256 tmp1[tree->
syms[i]] = 1;
258 for (i = 0; i < 16 && len < 16 - 1; i++)
263 for (i = 0; i < 16; i++)
265 for (i = 0; i <=
len; i++) {
267 for (t = 0; t < 16; t += size << 1)
268 merge(gb, out + t, in + t, size);
271 memcpy(tree->
syms, in, 16);
287 for (i = 0; i < 16; i++)
304 #define CHECK_READ_VAL(gb, b, t) \
305 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
307 t = get_bits(gb, b->len); \
350 v = (v ^ sign) - sign;
359 v = (v ^ sign) - sign;
394 if (dec_end - b->
cur_dec < run)
440 sign = ((int8_t) v) >> 7;
441 v = ((v & 0x7F) ^ sign) - sign;
452 sign = ((int8_t) v) >> 7;
453 v = ((v & 0x7F) ^ sign) - sign;
463 #define DC_START_BITS 11
466 int start_bits,
int has_sign)
468 int i, j,
len, len2, bsize, sign,
v, v2;
470 int16_t *dst_end = (int16_t*)b->
data_end;
473 v =
get_bits(gb, start_bits - has_sign);
476 v = (v ^ sign) - sign;
478 if (dst_end - dst < 1)
482 for (i = 0; i <
len; i += 8) {
483 len2 =
FFMIN(len - i, 8);
484 if (dst_end - dst < len2)
488 for (j = 0; j < len2; j++) {
492 v2 = (v2 ^ sign) - sign;
496 if (v < -32768 || v > 32767) {
502 for (j = 0; j < len2; j++)
547 const int mask = 1 << (bits - 1);
557 for (i = 0; i <
len; i++)
560 for (i = 0; i <
len; i++)
567 for (i = 0; i <
len; i++)
570 for (i = 0; i <
len; i++)
602 const int32_t quant_matrices[16][64],
int q)
607 int list_start = 64, list_end = 64, list_pos;
613 coef_list[list_end] = 4; mode_list[list_end++] = 0;
614 coef_list[list_end] = 24; mode_list[list_end++] = 0;
615 coef_list[list_end] = 44; mode_list[list_end++] = 0;
616 coef_list[list_end] = 1; mode_list[list_end++] = 3;
617 coef_list[list_end] = 2; mode_list[list_end++] = 3;
618 coef_list[list_end] = 3; mode_list[list_end++] = 3;
620 for (bits =
get_bits(gb, 4) - 1; bits >= 0; bits--) {
621 list_pos = list_start;
622 while (list_pos < list_end) {
623 if (!(mode_list[list_pos] | coef_list[list_pos]) || !
get_bits1(gb)) {
627 ccoef = coef_list[list_pos];
628 mode = mode_list[list_pos];
631 coef_list[list_pos] = ccoef + 4;
632 mode_list[list_pos] = 1;
635 coef_list[list_pos] = 0;
636 mode_list[list_pos++] = 0;
638 for (i = 0; i < 4; i++, ccoef++) {
640 coef_list[--list_start] = ccoef;
641 mode_list[ list_start] = 3;
648 t = (t ^ sign) - sign;
650 block[scan[ccoef]] =
t;
651 coef_idx[coef_count++] = ccoef;
656 mode_list[list_pos] = 2;
657 for (i = 0; i < 3; i++) {
659 coef_list[list_end] = ccoef;
660 mode_list[list_end++] = 2;
669 t = (t ^ sign) - sign;
671 block[scan[ccoef]] =
t;
672 coef_idx[coef_count++] = ccoef;
673 coef_list[list_pos] = 0;
674 mode_list[list_pos++] = 0;
684 if (quant_idx > 15
U) {
690 quant = quant_matrices[quant_idx];
692 block[0] = (block[0] * quant[0]) >> 11;
693 for (i = 0; i < coef_count; i++) {
694 int idx = coef_idx[i];
695 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
714 int list_start = 64, list_end = 64, list_pos;
716 int nz_coeff_count = 0;
718 coef_list[list_end] = 4; mode_list[list_end++] = 0;
719 coef_list[list_end] = 24; mode_list[list_end++] = 0;
720 coef_list[list_end] = 44; mode_list[list_end++] = 0;
721 coef_list[list_end] = 0; mode_list[list_end++] = 2;
724 for (i = 0; i < nz_coeff_count; i++) {
727 if (block[nz_coeff[i]] < 0)
728 block[nz_coeff[i]] -=
mask;
730 block[nz_coeff[i]] +=
mask;
735 list_pos = list_start;
736 while (list_pos < list_end) {
737 if (!(coef_list[list_pos] | mode_list[list_pos]) || !
get_bits1(gb)) {
741 ccoef = coef_list[list_pos];
742 mode = mode_list[list_pos];
745 coef_list[list_pos] = ccoef + 4;
746 mode_list[list_pos] = 1;
749 coef_list[list_pos] = 0;
750 mode_list[list_pos++] = 0;
752 for (i = 0; i < 4; i++, ccoef++) {
754 coef_list[--list_start] = ccoef;
755 mode_list[ list_start] = 3;
757 nz_coeff[nz_coeff_count++] =
bink_scan[ccoef];
759 block[
bink_scan[ccoef]] = (mask ^ sign) - sign;
767 mode_list[list_pos] = 2;
768 for (i = 0; i < 3; i++) {
770 coef_list[list_end] = ccoef;
771 mode_list[list_end++] = 2;
775 nz_coeff[nz_coeff_count++] =
bink_scan[ccoef];
777 block[
bink_scan[ccoef]] = (mask ^ sign) - sign;
778 coef_list[list_pos] = 0;
779 mode_list[list_pos++] = 0;
798 for (i = 0; i < 8; i++)
799 memcpy(tmp + i*8, src + i*stride, 8);
800 for (i = 0; i < 8; i++)
801 memcpy(dst + i*stride, tmp + i*8, 8);
805 int is_key,
int is_chroma)
816 int ybias = is_key ? -15 : 0;
824 ref_start = c->
pic->
data[plane_idx];
827 for (i = 0; i < 64; i++)
828 coordmap[i] = (i & 7) + (i >> 3) * stride;
830 for (by = 0; by < bh; by++) {
837 for (bx = 0; bx < bw; bx++, dst += 8) {
858 for (j = 0; j <
run; j++)
859 dst[coordmap[*scan++]] = v;
861 for (j = 0; j <
run; j++)
869 memset(dctblock, 0,
sizeof(*dctblock) * 64);
878 ref = dst + xoff + yoff *
stride;
879 if (ref < ref_start || ref + 8*stride > ref_end) {
881 }
else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
894 ref = dst + xoff + yoff *
stride;
895 if (ref < ref_start || ref + 8 * stride > ref_end) {
897 }
else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
902 memset(dctblock, 0,
sizeof(*dctblock) * 64);
913 for (i = 0; i < 2; i++)
915 for (i = 0; i < 8; i++) {
917 for (j = 0; j < 8; j++, v >>= 1)
918 dst[i*stride + j] = col[v & 1];
924 ref = dst + xoff + yoff *
stride;
925 if (ref < ref_start || ref + 8 * stride > ref_end) {
927 }
else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
934 for (i = 0; i < 8; i++)
955 uint8_t *
dst, *prev, *ref, *ref_start, *ref_end;
976 + (bw - 1 + c->
last->
linesize[plane_idx] * (bh - 1)) * 8;
978 for (i = 0; i < 64; i++)
979 coordmap[i] = (i & 7) + (i >> 3) * stride;
981 for (by = 0; by < bh; by++) {
1005 : c->
pic->
data[plane_idx]) + 8*by*stride;
1006 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1035 for (j = 0; j <
run; j++)
1036 ublock[*scan++] = v;
1038 for (j = 0; j <
run; j++)
1046 memset(dctblock, 0,
sizeof(*dctblock) * 64);
1056 for (i = 0; i < 2; i++)
1058 for (j = 0; j < 8; j++) {
1060 for (i = 0; i < 8; i++, v >>= 1)
1061 ublock[i + j*8] = col[v & 1];
1065 for (j = 0; j < 8; j++)
1066 for (i = 0; i < 8; i++)
1082 ref = prev + xoff + yoff *
stride;
1083 if (ref < ref_start || ref > ref_end) {
1085 bx*8 + xoff, by*8 + yoff);
1103 for (j = 0; j <
run; j++)
1104 dst[coordmap[*scan++]] = v;
1106 for (j = 0; j <
run; j++)
1116 ref = prev + xoff + yoff *
stride;
1117 if (ref < ref_start || ref > ref_end) {
1119 bx*8 + xoff, by*8 + yoff);
1129 memset(dctblock, 0,
sizeof(*dctblock) * 64);
1141 ref = prev + xoff + yoff *
stride;
1142 if (ref < ref_start || ref > ref_end) {
1144 bx*8 + xoff, by*8 + yoff);
1148 memset(dctblock, 0,
sizeof(*dctblock) * 64);
1154 for (i = 0; i < 2; i++)
1156 for (i = 0; i < 8; i++) {
1158 for (j = 0; j < 8; j++, v >>= 1)
1159 dst[i*stride + j] = col[v & 1];
1163 for (i = 0; i < 8; i++)
1183 int plane, plane_idx, ret;
1184 int bits_count = pkt->
size << 3;
1213 for (plane = 0; plane < 3; plane++) {
1214 plane_idx = (!plane || !c->
swap_planes) ? plane : (plane ^ 3);
1245 static const int s[64]={
1246 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1247 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1248 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1249 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1250 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1251 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1252 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1253 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1257 for (i = 0; i < 64; i++)
1260 for (j = 0; j < 16; j++) {
1261 for (i = 0; i < 64; i++) {
1262 int k = inv_bink_scan[i];
1275 static int binkb_initialised = 0;
1287 if (!bink_trees[15].table) {
1288 for (i = 0; i < 16; i++) {
1290 bink_trees[i].
table = table + i*128;
1292 init_vlc(&bink_trees[i], maxbits, 16,
1321 if (!binkb_initialised) {
1323 binkb_initialised = 1;
1353 .
name =
"binkvideo",