1 /*
2 * Bink video decoder
3 * Copyright (c) 2009 Konstantin Shishkov
4 * Copyright (C) 2011 Peter Ross <pross@xvid.org>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
33
34 #define BITSTREAM_READER_LE
36
37 #define BINK_FLAG_ALPHA 0x00100000
38 #define BINK_FLAG_GRAY 0x00020000
39
41
42 /**
43 * IDs for different data types used in old version of Bink video codec
44 */
56
58 };
59
61 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
62 };
63
65 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
66 };
67
70
71 /**
72 * IDs for different data types used in Bink video codec
73 */
84
86 };
87
88 /**
89 * data needed to decode 4-bit Huffman-coded value
90 */
92 int vlc_num;
///< tree number (in bink_trees[])
95
96 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
97 bink_trees[(tree).vlc_num].bits, 1)]
98
99 /**
100 * data structure used for decoding single Bink data type
101 */
103 int len;
///< length of number of entries to decode (in bits)
110
111 /*
112 * Decoder context
113 */
124
126 Tree col_high[16];
///< trees for decoding high nibble in "colours" data type
127 int col_lastval;
///< value of last decoded high nibble in "colours" data type
129
130 /**
131 * Bink video block types
132 */
136 MOTION_BLOCK,
///< block is copied from previous frame with some offset
137 RUN_BLOCK,
///< block is composed from runs of colours with custom scan order
142 PATTERN_BLOCK,
///< block is filled with two colours following custom pattern
144 };
145
146 /**
147 * Initialize length in all bundles.
148 *
149 * @param c decoder context
150 * @param width plane width
151 * @param bw plane width in 8x8 blocks
152 */
154 {
156
158
160
162
167
169
171 }
172
173 /**
174 * Allocate memory for bundles.
175 *
176 * @param c decoder context
177 */
179 {
180 int bw, bh, blocks;
181 int i;
182
185 blocks = bw * bh;
186
192 }
193
194 return 0;
195 }
196
197 /**
198 * Free memory used by bundles.
199 *
200 * @param c decoder context
201 */
203 {
204 int i;
207 }
208
209 /**
210 * Merge two consequent lists of equal size depending on bits read.
211 *
212 * @param gb context for reading bits
213 * @param dst buffer where merged list will be written to
214 * @param src pointer to the head of the first list (the second lists starts at src+size)
215 * @param size input lists size
216 */
218 {
221
222 do {
224 *dst++ = *src++;
225 size--;
226 } else {
227 *dst++ = *src2++;
228 size2--;
229 }
230 } while (size && size2);
231
232 while (size--)
233 *dst++ = *src++;
234 while (size2--)
235 *dst++ = *src2++;
236 }
237
238 /**
239 * Read information about Huffman tree used to decode data.
240 *
241 * @param gb context for reading bits
242 * @param tree pointer for storing tree data
243 */
245 {
246 uint8_t tmp1[16] = { 0 }, tmp2[16], *
in = tmp1, *
out = tmp2;
248
251 for (i = 0; i < 16; i++)
253 return;
254 }
257 for (i = 0; i <=
len; i++) {
259 tmp1[tree->
syms[i]] = 1;
260 }
261 for (i = 0; i < 16 && len < 16 - 1; i++)
262 if (!tmp1[i])
264 } else {
266 for (i = 0; i < 16; i++)
267 in[i] = i;
268 for (i = 0; i <=
len; i++) {
270 for (t = 0; t < 16; t += size << 1)
271 merge(gb, out + t, in + t, size);
273 }
274 memcpy(tree->
syms, in, 16);
275 }
276 }
277
278 /**
279 * Prepare bundle for decoding data.
280 *
281 * @param gb context for reading bits
282 * @param c decoder context
283 * @param bundle_num number of the bundle to initialize
284 */
286 {
287 int i;
288
290 for (i = 0; i < 16; i++)
293 }
298 }
299
300 /**
301 * common check before starting decoding bundle data
302 *
303 * @param gb context for reading bits
304 * @param b bundle
305 * @param t variable where number of elements to decode will be stored
306 */
307 #define CHECK_READ_VAL(gb, b, t) \
308 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
309 return 0; \
310 t = get_bits(gb, b->len); \
311 if (!t) { \
312 b->cur_dec = NULL; \
313 return 0; \
314 } \
315
317 {
320
326 }
331 } else {
334 }
335 return 0;
336 }
337
339 {
342
348 }
351 if (v) {
353 v = (v ^ sign) - sign;
354 }
357 } else {
360 if (v) {
362 v = (v ^ sign) - sign;
363 }
365 }
366 }
367 return 0;
368 }
369
371
373 {
375 int last = 0;
377
383 }
388 } else {
391 if (v < 12) {
394 } else {
396
397 if (dec_end - b->
cur_dec < run)
401 }
402 }
403 }
404 return 0;
405 }
406
408 {
411
417 }
422 }
423
424 return 0;
425 }
426
428 {
431
437 }
443 sign = ((int8_t) v) >> 7;
444 v = ((v & 0x7F) ^ sign) - sign;
445 v += 0x80;
446 }
449 } else {
455 sign = ((int8_t) v) >> 7;
456 v = ((v & 0x7F) ^ sign) - sign;
457 v += 0x80;
458 }
460 }
461 }
462 return 0;
463 }
464
465 /** number of bits used to store first DC value in bundle */
466 #define DC_START_BITS 11
467
469 int start_bits, int has_sign)
470 {
471 int i, j,
len, len2, bsize, sign,
v, v2;
472 int16_t *dst = (int16_t*)b->
cur_dec;
473 int16_t *dst_end = (int16_t*)b->
data_end;
474
476 v =
get_bits(gb, start_bits - has_sign);
477 if (v && has_sign) {
479 v = (v ^ sign) - sign;
480 }
481 if (dst_end - dst < 1)
484 len--;
485 for (i = 0; i <
len; i += 8) {
486 len2 =
FFMIN(len - i, 8);
487 if (dst_end - dst < len2)
490 if (bsize) {
491 for (j = 0; j < len2; j++) {
493 if (v2) {
495 v2 = (v2 ^ sign) - sign;
496 }
497 v += v2;
499 if (v < -32768 || v > 32767) {
502 }
503 }
504 } else {
505 for (j = 0; j < len2; j++)
506 *dst++ = v;
507 }
508 }
509
511 return 0;
512 }
513
514 /**
515 * Retrieve next value from bundle.
516 *
517 * @param c decoder context
518 * @param bundle bundle number
519 */
521 {
523
530 return ret;
531 }
532
534 {
538 }
539
541 {
542 int i;
545 }
546
548 {
550 const int mask = 1 << (bits - 1);
554
558 if (bits <= 8) {
559 if (!issigned) {
560 for (i = 0; i <
len; i++)
562 } else {
563 for (i = 0; i <
len; i++)
565 }
566 } else {
567 int16_t *dst = (int16_t*)b->
cur_dec;
568
570 for (i = 0; i <
len; i++)
572 } else {
573 for (i = 0; i <
len; i++)
575 }
577 }
578 return 0;
579 }
580
582 {
585
586 if (bits <= 8) {
589 }
592 return ret;
593 }
594
595 /**
596 * Read 8x8 block of DCT coefficients.
597 *
598 * @param gb context for reading bits
599 * @param block place for storing coefficients
600 * @param scan scan order table
601 * @param quant_matrices quantization matrices
602 * @return 0 for success, negative value in other cases
603 */
605 const int32_t quant_matrices[16][64],
int q)
606 {
607 int coef_list[128];
608 int mode_list[128];
610 int list_start = 64, list_end = 64, list_pos;
611 int coef_count = 0;
612 int coef_idx[64];
613 int quant_idx;
615
616 coef_list[list_end] = 4; mode_list[list_end++] = 0;
617 coef_list[list_end] = 24; mode_list[list_end++] = 0;
618 coef_list[list_end] = 44; mode_list[list_end++] = 0;
619 coef_list[list_end] = 1; mode_list[list_end++] = 3;
620 coef_list[list_end] = 2; mode_list[list_end++] = 3;
621 coef_list[list_end] = 3; mode_list[list_end++] = 3;
622
623 for (bits =
get_bits(gb, 4) - 1; bits >= 0; bits--) {
624 list_pos = list_start;
625 while (list_pos < list_end) {
626 if (!(mode_list[list_pos] | coef_list[list_pos]) || !
get_bits1(gb)) {
627 list_pos++;
628 continue;
629 }
630 ccoef = coef_list[list_pos];
631 mode = mode_list[list_pos];
632 switch (mode) {
633 case 0:
634 coef_list[list_pos] = ccoef + 4;
635 mode_list[list_pos] = 1;
636 case 2:
637 if (mode == 2) {
638 coef_list[list_pos] = 0;
639 mode_list[list_pos++] = 0;
640 }
641 for (i = 0; i < 4; i++, ccoef++) {
643 coef_list[--list_start] = ccoef;
644 mode_list[ list_start] = 3;
645 } else {
646 if (!bits) {
648 } else {
651 t = (t ^ sign) - sign;
652 }
653 block[scan[ccoef]] = t;
654 coef_idx[coef_count++] = ccoef;
655 }
656 }
657 break;
658 case 1:
659 mode_list[list_pos] = 2;
660 for (i = 0; i < 3; i++) {
661 ccoef += 4;
662 coef_list[list_end] = ccoef;
663 mode_list[list_end++] = 2;
664 }
665 break;
666 case 3:
667 if (!bits) {
669 } else {
672 t = (t ^ sign) - sign;
673 }
674 block[scan[ccoef]] = t;
675 coef_idx[coef_count++] = ccoef;
676 coef_list[list_pos] = 0;
677 mode_list[list_pos++] = 0;
678 break;
679 }
680 }
681 }
682
683 if (q == -1) {
685 } else {
686 quant_idx = q;
687 if (quant_idx > 15
U) {
690 }
691 }
692
693 quant = quant_matrices[quant_idx];
694
695 block[0] = (block[0] * quant[0]) >> 11;
696 for (i = 0; i < coef_count; i++) {
697 int idx = coef_idx[i];
698 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
699 }
700
701 return 0;
702 }
703
704 /**
705 * Read 8x8 block with residue after motion compensation.
706 *
707 * @param gb context for reading bits
708 * @param block place to store read data
709 * @param masks_count number of masks to decode
710 * @return 0 on success, negative value in other cases
711 */
713 {
714 int coef_list[128];
715 int mode_list[128];
717 int list_start = 64, list_end = 64, list_pos;
718 int nz_coeff[64];
719 int nz_coeff_count = 0;
720
721 coef_list[list_end] = 4; mode_list[list_end++] = 0;
722 coef_list[list_end] = 24; mode_list[list_end++] = 0;
723 coef_list[list_end] = 44; mode_list[list_end++] = 0;
724 coef_list[list_end] = 0; mode_list[list_end++] = 2;
725
727 for (i = 0; i < nz_coeff_count; i++) {
729 continue;
730 if (block[nz_coeff[i]] < 0)
731 block[nz_coeff[i]] -=
mask;
732 else
733 block[nz_coeff[i]] +=
mask;
734 masks_count--;
735 if (masks_count < 0)
736 return 0;
737 }
738 list_pos = list_start;
739 while (list_pos < list_end) {
740 if (!(coef_list[list_pos] | mode_list[list_pos]) || !
get_bits1(gb)) {
741 list_pos++;
742 continue;
743 }
744 ccoef = coef_list[list_pos];
745 mode = mode_list[list_pos];
746 switch (mode) {
747 case 0:
748 coef_list[list_pos] = ccoef + 4;
749 mode_list[list_pos] = 1;
750 case 2:
751 if (mode == 2) {
752 coef_list[list_pos] = 0;
753 mode_list[list_pos++] = 0;
754 }
755 for (i = 0; i < 4; i++, ccoef++) {
757 coef_list[--list_start] = ccoef;
758 mode_list[ list_start] = 3;
759 } else {
760 nz_coeff[nz_coeff_count++] =
bink_scan[ccoef];
762 block[
bink_scan[ccoef]] = (mask ^ sign) - sign;
763 masks_count--;
764 if (masks_count < 0)
765 return 0;
766 }
767 }
768 break;
769 case 1:
770 mode_list[list_pos] = 2;
771 for (i = 0; i < 3; i++) {
772 ccoef += 4;
773 coef_list[list_end] = ccoef;
774 mode_list[list_end++] = 2;
775 }
776 break;
777 case 3:
778 nz_coeff[nz_coeff_count++] =
bink_scan[ccoef];
780 block[
bink_scan[ccoef]] = (mask ^ sign) - sign;
781 coef_list[list_pos] = 0;
782 mode_list[list_pos++] = 0;
783 masks_count--;
784 if (masks_count < 0)
785 return 0;
786 break;
787 }
788 }
789 }
790
791 return 0;
792 }
793
794 /**
795 * Copy 8x8 block from source to destination, where src and dst may be overlapped
796 */
798 {
800 int i;
801 for (i = 0; i < 8; i++)
802 memcpy(tmp + i*8, src + i*stride, 8);
803 for (i = 0; i < 8; i++)
804 memcpy(dst + i*stride, tmp + i*8, 8);
805 }
806
808 int plane_idx, int is_key, int is_chroma)
809 {
811 int i, j, bx, by;
812 uint8_t *dst, *ref, *ref_start, *ref_end;
815 int xoff, yoff;
818 int coordmap[64];
819 int ybias = is_key ? -15 : 0;
820 int qp;
821
825
827 ref_start = frame->
data[plane_idx];
828 ref_end = frame->
data[plane_idx] + (bh * frame->
linesize[plane_idx] + bw) * 8;
829
830 for (i = 0; i < 64; i++)
831 coordmap[i] = (i & 7) + (i >> 3) * stride;
832
833 for (by = 0; by < bh; by++) {
837 }
838
839 dst = frame->
data[plane_idx] + 8*by*stride;
840 for (bx = 0; bx < bw; bx++, dst += 8) {
842 switch (blk) {
843 case 0:
844 break;
845 case 1:
847 i = 0;
848 do {
850
853
855 if (i > 64) {
858 }
859 if (mode) {
861 for (j = 0; j <
run; j++)
862 dst[coordmap[*scan++]] = v;
863 } else {
864 for (j = 0; j <
run; j++)
866 }
867 } while (i < 63);
868 if (i == 63)
870 break;
871 case 2:
872 memset(dctblock, 0, sizeof(*dctblock) * 64);
877 break;
878 case 3:
881 ref = dst + xoff + yoff * stride;
882 if (ref < ref_start || ref + 8*stride > ref_end) {
886 } else {
888 }
893 break;
894 case 4:
897 ref = dst + xoff + yoff * stride;
898 if (ref < ref_start || ref + 8 * stride > ref_end) {
902 } else {
904 }
905 memset(dctblock, 0, sizeof(*dctblock) * 64);
910 break;
911 case 5:
914 break;
915 case 6:
916 for (i = 0; i < 2; i++)
918 for (i = 0; i < 8; i++) {
920 for (j = 0; j < 8; j++, v >>= 1)
921 dst[i*stride + j] = col[v & 1];
922 }
923 break;
924 case 7:
927 ref = dst + xoff + yoff * stride;
928 if (ref < ref_start || ref + 8 * stride > ref_end) {
932 } else {
934 }
935 break;
936 case 8:
937 for (i = 0; i < 8; i++)
940 break;
941 default:
944 }
945 }
946 }
947 if (
get_bits_count(gb) & 0x1F)
//next plane data starts at 32-bit boundary
949
950 return 0;
951 }
952
954 int plane_idx, int is_chroma)
955 {
957 int i, j, bx, by;
958 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
961 int xoff, yoff;
965 int coordmap[64];
966
971
975
977 : frame->
data[plane_idx];
978 ref_end = ref_start
979 + (bw - 1 + c->
last->
linesize[plane_idx] * (bh - 1)) * 8;
980
981 for (i = 0; i < 64; i++)
982 coordmap[i] = (i & 7) + (i >> 3) * stride;
983
984 for (by = 0; by < bh; by++) {
1003
1004 if (by == bh)
1005 break;
1006 dst = frame->
data[plane_idx] + 8*by*stride;
1008 : frame->
data[plane_idx]) + 8*by*stride;
1009 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1011 // 16x16 block type on odd line means part of the already decoded block, so skip it
1013 bx++;
1014 dst += 8;
1015 prev += 8;
1016 continue;
1017 }
1018 switch (blk) {
1021 break;
1024 switch (blk) {
1027 i = 0;
1028 do {
1030
1032 if (i > 64) {
1035 }
1038 for (j = 0; j <
run; j++)
1039 ublock[*scan++] = v;
1040 } else {
1041 for (j = 0; j <
run; j++)
1043 }
1044 } while (i < 63);
1045 if (i == 63)
1047 break;
1049 memset(dctblock, 0, sizeof(*dctblock) * 64);
1053 break;
1057 break;
1059 for (i = 0; i < 2; i++)
1061 for (j = 0; j < 8; j++) {
1063 for (i = 0; i < 8; i++, v >>= 1)
1064 ublock[i + j*8] = col[v & 1];
1065 }
1066 break;
1068 for (j = 0; j < 8; j++)
1069 for (i = 0; i < 8; i++)
1071 break;
1072 default:
1075 }
1078 bx++;
1079 dst += 8;
1080 prev += 8;
1081 break;
1085 ref = prev + xoff + yoff * stride;
1086 if (ref < ref_start || ref > ref_end) {
1088 bx*8 + xoff, by*8 + yoff);
1090 }
1092 break;
1095 i = 0;
1096 do {
1098
1100 if (i > 64) {
1103 }
1106 for (j = 0; j <
run; j++)
1107 dst[coordmap[*scan++]] = v;
1108 } else {
1109 for (j = 0; j <
run; j++)
1111 }
1112 } while (i < 63);
1113 if (i == 63)
1115 break;
1119 ref = prev + xoff + yoff * stride;
1120 if (ref < ref_start || ref > ref_end) {
1122 bx*8 + xoff, by*8 + yoff);
1124 }
1130 break;
1132 memset(dctblock, 0, sizeof(*dctblock) * 64);
1136 break;
1140 break;
1144 ref = prev + xoff + yoff * stride;
1145 if (ref < ref_start || ref > ref_end) {
1147 bx*8 + xoff, by*8 + yoff);
1148 return -1;
1149 }
1151 memset(dctblock, 0, sizeof(*dctblock) * 64);
1155 break;
1157 for (i = 0; i < 2; i++)
1159 for (i = 0; i < 8; i++) {
1161 for (j = 0; j < 8; j++, v >>= 1)
1162 dst[i*stride + j] = col[v & 1];
1163 }
1164 break;
1166 for (i = 0; i < 8; i++)
1169 break;
1170 default:
1173 }
1174 }
1175 }
1176 if (
get_bits_count(gb) & 0x1F)
//next plane data starts at 32-bit boundary
1178
1179 return 0;
1180 }
1181
1183 {
1187 int plane, plane_idx,
ret;
1188 int bits_count = pkt->
size << 3;
1189
1193 } else {
1198 }
1199
1206 }
1209
1211
1212 for (plane = 0; plane < 3; plane++) {
1213 plane_idx = (!plane || !c->
swap_planes) ? plane : (plane ^ 3);
1214
1217 return ret;
1218 } else {
1221 return ret;
1222 }
1224 break;
1225 }
1226 emms_c();
1227
1232 }
1233
1234 *got_frame = 1;
1235
1236 /* always report that the buffer was completely consumed */
1238 }
1239
1240 /**
1241 * Caclulate quantization tables for version b
1242 */
1244 {
1246 static const int s[64]={
1247 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1248 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1249 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1250 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1251 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1252 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1253 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1254 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1255 };
1256 int i, j;
1257 #define C (1LL<<30)
1258 for (i = 0; i < 64; i++)
1260
1261 for (j = 0; j < 16; j++) {
1262 for (i = 0; i < 64; i++) {
1263 int k = inv_bink_scan[i];
1268 }
1269 }
1270 }
1271
1273 {
1276 static int binkb_initialised = 0;
1279
1284 }
1288 if (!bink_trees[15].table) {
1289 for (i = 0; i < 16; i++) {
1291 bink_trees[i].
table = table + i*128;
1293 init_vlc(&bink_trees[i], maxbits, 16,
1296 }
1297 }
1299
1303
1306
1308
1312
1316 }
1317
1319 if (!binkb_initialised) {
1321 binkb_initialised = 1;
1322 }
1323 }
1324
1325 return 0;
1326 }
1327
1329 {
1331
1333
1335 return 0;
1336 }
1337
1339 {
1341
1343 }
1344
1346 .
name =
"binkvideo",
1356 };