You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1337 lines
44KB

  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. #include "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "binkdata.h"
  26. #include "binkdsp.h"
  27. #include "mathops.h"
  28. #define BITSTREAM_READER_LE
  29. #include "get_bits.h"
  30. #define BINK_FLAG_ALPHA 0x00100000
  31. #define BINK_FLAG_GRAY 0x00020000
  32. static VLC bink_trees[16];
  33. /**
  34. * IDs for different data types used in old version of Bink video codec
  35. */
  36. enum OldSources {
  37. BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
  38. BINKB_SRC_COLORS, ///< pixel values used for different block types
  39. BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
  40. BINKB_SRC_X_OFF, ///< X components of motion value
  41. BINKB_SRC_Y_OFF, ///< Y components of motion value
  42. BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
  43. BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
  44. BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
  45. BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
  46. BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
  47. BINKB_NB_SRC
  48. };
  49. static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
  50. 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
  51. };
  52. static const int binkb_bundle_signed[BINKB_NB_SRC] = {
  53. 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
  54. };
  55. static int32_t binkb_intra_quant[16][64];
  56. static int32_t binkb_inter_quant[16][64];
  57. /**
  58. * IDs for different data types used in Bink video codec
  59. */
  60. enum Sources {
  61. BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
  62. BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
  63. BINK_SRC_COLORS, ///< pixel values used for different block types
  64. BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
  65. BINK_SRC_X_OFF, ///< X components of motion value
  66. BINK_SRC_Y_OFF, ///< Y components of motion value
  67. BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
  68. BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
  69. BINK_SRC_RUN, ///< run lengths for special fill block
  70. BINK_NB_SRC
  71. };
  72. /**
  73. * data needed to decode 4-bit Huffman-coded value
  74. */
  75. typedef struct Tree {
  76. int vlc_num; ///< tree number (in bink_trees[])
  77. uint8_t syms[16]; ///< leaf value to symbol mapping
  78. } Tree;
  79. #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
  80. bink_trees[(tree).vlc_num].bits, 1)]
  81. /**
  82. * data structure used for decoding single Bink data type
  83. */
  84. typedef struct Bundle {
  85. int len; ///< length of number of entries to decode (in bits)
  86. Tree tree; ///< Huffman tree-related data
  87. uint8_t *data; ///< buffer for decoded symbols
  88. uint8_t *data_end; ///< buffer end
  89. uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
  90. uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
  91. } Bundle;
  92. /*
  93. * Decoder context
  94. */
  95. typedef struct BinkContext {
  96. AVCodecContext *avctx;
  97. DSPContext dsp;
  98. BinkDSPContext bdsp;
  99. AVFrame pic, last;
  100. int version; ///< internal Bink file version
  101. int has_alpha;
  102. int swap_planes;
  103. Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
  104. Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
  105. int col_lastval; ///< value of last decoded high nibble in "colours" data type
  106. } BinkContext;
  107. /**
  108. * Bink video block types
  109. */
  110. enum BlockTypes {
  111. SKIP_BLOCK = 0, ///< skipped block
  112. SCALED_BLOCK, ///< block has size 16x16
  113. MOTION_BLOCK, ///< block is copied from previous frame with some offset
  114. RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
  115. RESIDUE_BLOCK, ///< motion block with some difference added
  116. INTRA_BLOCK, ///< intra DCT block
  117. FILL_BLOCK, ///< block is filled with single colour
  118. INTER_BLOCK, ///< motion block with DCT applied to the difference
  119. PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
  120. RAW_BLOCK, ///< uncoded 8x8 block
  121. };
  122. /**
  123. * Initialize length length in all bundles.
  124. *
  125. * @param c decoder context
  126. * @param width plane width
  127. * @param bw plane width in 8x8 blocks
  128. */
  129. static void init_lengths(BinkContext *c, int width, int bw)
  130. {
  131. width = FFALIGN(width, 8);
  132. c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
  133. c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
  134. c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
  135. c->bundle[BINK_SRC_INTRA_DC].len =
  136. c->bundle[BINK_SRC_INTER_DC].len =
  137. c->bundle[BINK_SRC_X_OFF].len =
  138. c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
  139. c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
  140. c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
  141. }
  142. /**
  143. * Allocate memory for bundles.
  144. *
  145. * @param c decoder context
  146. */
  147. static av_cold void init_bundles(BinkContext *c)
  148. {
  149. int bw, bh, blocks;
  150. int i;
  151. bw = (c->avctx->width + 7) >> 3;
  152. bh = (c->avctx->height + 7) >> 3;
  153. blocks = bw * bh;
  154. for (i = 0; i < BINKB_NB_SRC; i++) {
  155. c->bundle[i].data = av_malloc(blocks * 64);
  156. c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
  157. }
  158. }
  159. /**
  160. * Free memory used by bundles.
  161. *
  162. * @param c decoder context
  163. */
  164. static av_cold void free_bundles(BinkContext *c)
  165. {
  166. int i;
  167. for (i = 0; i < BINKB_NB_SRC; i++)
  168. av_freep(&c->bundle[i].data);
  169. }
  170. /**
  171. * Merge two consequent lists of equal size depending on bits read.
  172. *
  173. * @param gb context for reading bits
  174. * @param dst buffer where merged list will be written to
  175. * @param src pointer to the head of the first list (the second lists starts at src+size)
  176. * @param size input lists size
  177. */
  178. static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
  179. {
  180. uint8_t *src2 = src + size;
  181. int size2 = size;
  182. do {
  183. if (!get_bits1(gb)) {
  184. *dst++ = *src++;
  185. size--;
  186. } else {
  187. *dst++ = *src2++;
  188. size2--;
  189. }
  190. } while (size && size2);
  191. while (size--)
  192. *dst++ = *src++;
  193. while (size2--)
  194. *dst++ = *src2++;
  195. }
  196. /**
  197. * Read information about Huffman tree used to decode data.
  198. *
  199. * @param gb context for reading bits
  200. * @param tree pointer for storing tree data
  201. */
  202. static void read_tree(GetBitContext *gb, Tree *tree)
  203. {
  204. uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
  205. int i, t, len;
  206. tree->vlc_num = get_bits(gb, 4);
  207. if (!tree->vlc_num) {
  208. for (i = 0; i < 16; i++)
  209. tree->syms[i] = i;
  210. return;
  211. }
  212. if (get_bits1(gb)) {
  213. len = get_bits(gb, 3);
  214. for (i = 0; i <= len; i++) {
  215. tree->syms[i] = get_bits(gb, 4);
  216. tmp1[tree->syms[i]] = 1;
  217. }
  218. for (i = 0; i < 16 && len < 16 - 1; i++)
  219. if (!tmp1[i])
  220. tree->syms[++len] = i;
  221. } else {
  222. len = get_bits(gb, 2);
  223. for (i = 0; i < 16; i++)
  224. in[i] = i;
  225. for (i = 0; i <= len; i++) {
  226. int size = 1 << i;
  227. for (t = 0; t < 16; t += size << 1)
  228. merge(gb, out + t, in + t, size);
  229. FFSWAP(uint8_t*, in, out);
  230. }
  231. memcpy(tree->syms, in, 16);
  232. }
  233. }
  234. /**
  235. * Prepare bundle for decoding data.
  236. *
  237. * @param gb context for reading bits
  238. * @param c decoder context
  239. * @param bundle_num number of the bundle to initialize
  240. */
  241. static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
  242. {
  243. int i;
  244. if (bundle_num == BINK_SRC_COLORS) {
  245. for (i = 0; i < 16; i++)
  246. read_tree(gb, &c->col_high[i]);
  247. c->col_lastval = 0;
  248. }
  249. if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
  250. read_tree(gb, &c->bundle[bundle_num].tree);
  251. c->bundle[bundle_num].cur_dec =
  252. c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
  253. }
  254. /**
  255. * common check before starting decoding bundle data
  256. *
  257. * @param gb context for reading bits
  258. * @param b bundle
  259. * @param t variable where number of elements to decode will be stored
  260. */
  261. #define CHECK_READ_VAL(gb, b, t) \
  262. if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
  263. return 0; \
  264. t = get_bits(gb, b->len); \
  265. if (!t) { \
  266. b->cur_dec = NULL; \
  267. return 0; \
  268. } \
  269. static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  270. {
  271. int t, v;
  272. const uint8_t *dec_end;
  273. CHECK_READ_VAL(gb, b, t);
  274. dec_end = b->cur_dec + t;
  275. if (dec_end > b->data_end) {
  276. av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
  277. return -1;
  278. }
  279. if (get_bits1(gb)) {
  280. v = get_bits(gb, 4);
  281. memset(b->cur_dec, v, t);
  282. b->cur_dec += t;
  283. } else {
  284. while (b->cur_dec < dec_end)
  285. *b->cur_dec++ = GET_HUFF(gb, b->tree);
  286. }
  287. return 0;
  288. }
  289. static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  290. {
  291. int t, sign, v;
  292. const uint8_t *dec_end;
  293. CHECK_READ_VAL(gb, b, t);
  294. dec_end = b->cur_dec + t;
  295. if (dec_end > b->data_end) {
  296. av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
  297. return -1;
  298. }
  299. if (get_bits1(gb)) {
  300. v = get_bits(gb, 4);
  301. if (v) {
  302. sign = -get_bits1(gb);
  303. v = (v ^ sign) - sign;
  304. }
  305. memset(b->cur_dec, v, t);
  306. b->cur_dec += t;
  307. } else {
  308. while (b->cur_dec < dec_end) {
  309. v = GET_HUFF(gb, b->tree);
  310. if (v) {
  311. sign = -get_bits1(gb);
  312. v = (v ^ sign) - sign;
  313. }
  314. *b->cur_dec++ = v;
  315. }
  316. }
  317. return 0;
  318. }
  319. static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
  320. static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  321. {
  322. int t, v;
  323. int last = 0;
  324. const uint8_t *dec_end;
  325. CHECK_READ_VAL(gb, b, t);
  326. dec_end = b->cur_dec + t;
  327. if (dec_end > b->data_end) {
  328. av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
  329. return -1;
  330. }
  331. if (get_bits1(gb)) {
  332. v = get_bits(gb, 4);
  333. memset(b->cur_dec, v, t);
  334. b->cur_dec += t;
  335. } else {
  336. while (b->cur_dec < dec_end) {
  337. v = GET_HUFF(gb, b->tree);
  338. if (v < 12) {
  339. last = v;
  340. *b->cur_dec++ = v;
  341. } else {
  342. int run = bink_rlelens[v - 12];
  343. if (dec_end - b->cur_dec < run)
  344. return -1;
  345. memset(b->cur_dec, last, run);
  346. b->cur_dec += run;
  347. }
  348. }
  349. }
  350. return 0;
  351. }
  352. static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  353. {
  354. int t, v;
  355. const uint8_t *dec_end;
  356. CHECK_READ_VAL(gb, b, t);
  357. dec_end = b->cur_dec + t;
  358. if (dec_end > b->data_end) {
  359. av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
  360. return -1;
  361. }
  362. while (b->cur_dec < dec_end) {
  363. v = GET_HUFF(gb, b->tree);
  364. v |= GET_HUFF(gb, b->tree) << 4;
  365. *b->cur_dec++ = v;
  366. }
  367. return 0;
  368. }
  369. static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
  370. {
  371. int t, sign, v;
  372. const uint8_t *dec_end;
  373. CHECK_READ_VAL(gb, b, t);
  374. dec_end = b->cur_dec + t;
  375. if (dec_end > b->data_end) {
  376. av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
  377. return -1;
  378. }
  379. if (get_bits1(gb)) {
  380. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  381. v = GET_HUFF(gb, b->tree);
  382. v = (c->col_lastval << 4) | v;
  383. if (c->version < 'i') {
  384. sign = ((int8_t) v) >> 7;
  385. v = ((v & 0x7F) ^ sign) - sign;
  386. v += 0x80;
  387. }
  388. memset(b->cur_dec, v, t);
  389. b->cur_dec += t;
  390. } else {
  391. while (b->cur_dec < dec_end) {
  392. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  393. v = GET_HUFF(gb, b->tree);
  394. v = (c->col_lastval << 4) | v;
  395. if (c->version < 'i') {
  396. sign = ((int8_t) v) >> 7;
  397. v = ((v & 0x7F) ^ sign) - sign;
  398. v += 0x80;
  399. }
  400. *b->cur_dec++ = v;
  401. }
  402. }
  403. return 0;
  404. }
  405. /** number of bits used to store first DC value in bundle */
  406. #define DC_START_BITS 11
  407. static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
  408. int start_bits, int has_sign)
  409. {
  410. int i, j, len, len2, bsize, sign, v, v2;
  411. int16_t *dst = (int16_t*)b->cur_dec;
  412. int16_t *dst_end = (int16_t*)b->data_end;
  413. CHECK_READ_VAL(gb, b, len);
  414. v = get_bits(gb, start_bits - has_sign);
  415. if (v && has_sign) {
  416. sign = -get_bits1(gb);
  417. v = (v ^ sign) - sign;
  418. }
  419. if (dst_end - dst < 1)
  420. return -1;
  421. *dst++ = v;
  422. len--;
  423. for (i = 0; i < len; i += 8) {
  424. len2 = FFMIN(len - i, 8);
  425. if (dst_end - dst < len2)
  426. return -1;
  427. bsize = get_bits(gb, 4);
  428. if (bsize) {
  429. for (j = 0; j < len2; j++) {
  430. v2 = get_bits(gb, bsize);
  431. if (v2) {
  432. sign = -get_bits1(gb);
  433. v2 = (v2 ^ sign) - sign;
  434. }
  435. v += v2;
  436. *dst++ = v;
  437. if (v < -32768 || v > 32767) {
  438. av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
  439. return -1;
  440. }
  441. }
  442. } else {
  443. for (j = 0; j < len2; j++)
  444. *dst++ = v;
  445. }
  446. }
  447. b->cur_dec = (uint8_t*)dst;
  448. return 0;
  449. }
  450. /**
  451. * Retrieve next value from bundle.
  452. *
  453. * @param c decoder context
  454. * @param bundle bundle number
  455. */
  456. static inline int get_value(BinkContext *c, int bundle)
  457. {
  458. int ret;
  459. if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
  460. return *c->bundle[bundle].cur_ptr++;
  461. if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
  462. return (int8_t)*c->bundle[bundle].cur_ptr++;
  463. ret = *(int16_t*)c->bundle[bundle].cur_ptr;
  464. c->bundle[bundle].cur_ptr += 2;
  465. return ret;
  466. }
  467. static void binkb_init_bundle(BinkContext *c, int bundle_num)
  468. {
  469. c->bundle[bundle_num].cur_dec =
  470. c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
  471. c->bundle[bundle_num].len = 13;
  472. }
  473. static void binkb_init_bundles(BinkContext *c)
  474. {
  475. int i;
  476. for (i = 0; i < BINKB_NB_SRC; i++)
  477. binkb_init_bundle(c, i);
  478. }
  479. static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
  480. {
  481. const int bits = binkb_bundle_sizes[bundle_num];
  482. const int mask = 1 << (bits - 1);
  483. const int issigned = binkb_bundle_signed[bundle_num];
  484. Bundle *b = &c->bundle[bundle_num];
  485. int i, len;
  486. CHECK_READ_VAL(gb, b, len);
  487. if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
  488. return -1;
  489. if (bits <= 8) {
  490. if (!issigned) {
  491. for (i = 0; i < len; i++)
  492. *b->cur_dec++ = get_bits(gb, bits);
  493. } else {
  494. for (i = 0; i < len; i++)
  495. *b->cur_dec++ = get_bits(gb, bits) - mask;
  496. }
  497. } else {
  498. int16_t *dst = (int16_t*)b->cur_dec;
  499. if (!issigned) {
  500. for (i = 0; i < len; i++)
  501. *dst++ = get_bits(gb, bits);
  502. } else {
  503. for (i = 0; i < len; i++)
  504. *dst++ = get_bits(gb, bits) - mask;
  505. }
  506. b->cur_dec = (uint8_t*)dst;
  507. }
  508. return 0;
  509. }
  510. static inline int binkb_get_value(BinkContext *c, int bundle_num)
  511. {
  512. int16_t ret;
  513. const int bits = binkb_bundle_sizes[bundle_num];
  514. if (bits <= 8) {
  515. int val = *c->bundle[bundle_num].cur_ptr++;
  516. return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
  517. }
  518. ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
  519. c->bundle[bundle_num].cur_ptr += 2;
  520. return ret;
  521. }
  522. /**
  523. * Read 8x8 block of DCT coefficients.
  524. *
  525. * @param gb context for reading bits
  526. * @param block place for storing coefficients
  527. * @param scan scan order table
  528. * @param quant_matrices quantization matrices
  529. * @return 0 for success, negative value in other cases
  530. */
  531. static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
  532. const int32_t quant_matrices[16][64], int q)
  533. {
  534. int coef_list[128];
  535. int mode_list[128];
  536. int i, t, bits, ccoef, mode, sign;
  537. int list_start = 64, list_end = 64, list_pos;
  538. int coef_count = 0;
  539. int coef_idx[64];
  540. int quant_idx;
  541. const int32_t *quant;
  542. coef_list[list_end] = 4; mode_list[list_end++] = 0;
  543. coef_list[list_end] = 24; mode_list[list_end++] = 0;
  544. coef_list[list_end] = 44; mode_list[list_end++] = 0;
  545. coef_list[list_end] = 1; mode_list[list_end++] = 3;
  546. coef_list[list_end] = 2; mode_list[list_end++] = 3;
  547. coef_list[list_end] = 3; mode_list[list_end++] = 3;
  548. for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
  549. list_pos = list_start;
  550. while (list_pos < list_end) {
  551. if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
  552. list_pos++;
  553. continue;
  554. }
  555. ccoef = coef_list[list_pos];
  556. mode = mode_list[list_pos];
  557. switch (mode) {
  558. case 0:
  559. coef_list[list_pos] = ccoef + 4;
  560. mode_list[list_pos] = 1;
  561. case 2:
  562. if (mode == 2) {
  563. coef_list[list_pos] = 0;
  564. mode_list[list_pos++] = 0;
  565. }
  566. for (i = 0; i < 4; i++, ccoef++) {
  567. if (get_bits1(gb)) {
  568. coef_list[--list_start] = ccoef;
  569. mode_list[ list_start] = 3;
  570. } else {
  571. if (!bits) {
  572. t = 1 - (get_bits1(gb) << 1);
  573. } else {
  574. t = get_bits(gb, bits) | 1 << bits;
  575. sign = -get_bits1(gb);
  576. t = (t ^ sign) - sign;
  577. }
  578. block[scan[ccoef]] = t;
  579. coef_idx[coef_count++] = ccoef;
  580. }
  581. }
  582. break;
  583. case 1:
  584. mode_list[list_pos] = 2;
  585. for (i = 0; i < 3; i++) {
  586. ccoef += 4;
  587. coef_list[list_end] = ccoef;
  588. mode_list[list_end++] = 2;
  589. }
  590. break;
  591. case 3:
  592. if (!bits) {
  593. t = 1 - (get_bits1(gb) << 1);
  594. } else {
  595. t = get_bits(gb, bits) | 1 << bits;
  596. sign = -get_bits1(gb);
  597. t = (t ^ sign) - sign;
  598. }
  599. block[scan[ccoef]] = t;
  600. coef_idx[coef_count++] = ccoef;
  601. coef_list[list_pos] = 0;
  602. mode_list[list_pos++] = 0;
  603. break;
  604. }
  605. }
  606. }
  607. if (q == -1) {
  608. quant_idx = get_bits(gb, 4);
  609. } else {
  610. quant_idx = q;
  611. if (quant_idx > 15U) {
  612. av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
  613. return AVERROR_INVALIDDATA;
  614. }
  615. }
  616. quant = quant_matrices[quant_idx];
  617. block[0] = (block[0] * quant[0]) >> 11;
  618. for (i = 0; i < coef_count; i++) {
  619. int idx = coef_idx[i];
  620. block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
  621. }
  622. return 0;
  623. }
  624. /**
  625. * Read 8x8 block with residue after motion compensation.
  626. *
  627. * @param gb context for reading bits
  628. * @param block place to store read data
  629. * @param masks_count number of masks to decode
  630. * @return 0 on success, negative value in other cases
  631. */
  632. static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
  633. {
  634. int coef_list[128];
  635. int mode_list[128];
  636. int i, sign, mask, ccoef, mode;
  637. int list_start = 64, list_end = 64, list_pos;
  638. int nz_coeff[64];
  639. int nz_coeff_count = 0;
  640. coef_list[list_end] = 4; mode_list[list_end++] = 0;
  641. coef_list[list_end] = 24; mode_list[list_end++] = 0;
  642. coef_list[list_end] = 44; mode_list[list_end++] = 0;
  643. coef_list[list_end] = 0; mode_list[list_end++] = 2;
  644. for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
  645. for (i = 0; i < nz_coeff_count; i++) {
  646. if (!get_bits1(gb))
  647. continue;
  648. if (block[nz_coeff[i]] < 0)
  649. block[nz_coeff[i]] -= mask;
  650. else
  651. block[nz_coeff[i]] += mask;
  652. masks_count--;
  653. if (masks_count < 0)
  654. return 0;
  655. }
  656. list_pos = list_start;
  657. while (list_pos < list_end) {
  658. if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
  659. list_pos++;
  660. continue;
  661. }
  662. ccoef = coef_list[list_pos];
  663. mode = mode_list[list_pos];
  664. switch (mode) {
  665. case 0:
  666. coef_list[list_pos] = ccoef + 4;
  667. mode_list[list_pos] = 1;
  668. case 2:
  669. if (mode == 2) {
  670. coef_list[list_pos] = 0;
  671. mode_list[list_pos++] = 0;
  672. }
  673. for (i = 0; i < 4; i++, ccoef++) {
  674. if (get_bits1(gb)) {
  675. coef_list[--list_start] = ccoef;
  676. mode_list[ list_start] = 3;
  677. } else {
  678. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  679. sign = -get_bits1(gb);
  680. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  681. masks_count--;
  682. if (masks_count < 0)
  683. return 0;
  684. }
  685. }
  686. break;
  687. case 1:
  688. mode_list[list_pos] = 2;
  689. for (i = 0; i < 3; i++) {
  690. ccoef += 4;
  691. coef_list[list_end] = ccoef;
  692. mode_list[list_end++] = 2;
  693. }
  694. break;
  695. case 3:
  696. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  697. sign = -get_bits1(gb);
  698. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  699. coef_list[list_pos] = 0;
  700. mode_list[list_pos++] = 0;
  701. masks_count--;
  702. if (masks_count < 0)
  703. return 0;
  704. break;
  705. }
  706. }
  707. }
  708. return 0;
  709. }
  710. /**
  711. * Copy 8x8 block from source to destination, where src and dst may be overlapped
  712. */
  713. static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
  714. {
  715. uint8_t tmp[64];
  716. int i;
  717. for (i = 0; i < 8; i++)
  718. memcpy(tmp + i*8, src + i*stride, 8);
  719. for (i = 0; i < 8; i++)
  720. memcpy(dst + i*stride, tmp + i*8, 8);
  721. }
  722. static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
  723. int is_key, int is_chroma)
  724. {
  725. int blk;
  726. int i, j, bx, by;
  727. uint8_t *dst, *ref, *ref_start, *ref_end;
  728. int v, col[2];
  729. const uint8_t *scan;
  730. int xoff, yoff;
  731. LOCAL_ALIGNED_16(DCTELEM, block, [64]);
  732. LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
  733. int coordmap[64];
  734. int ybias = is_key ? -15 : 0;
  735. int qp;
  736. const int stride = c->pic.linesize[plane_idx];
  737. int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
  738. int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
  739. binkb_init_bundles(c);
  740. ref_start = c->pic.data[plane_idx];
  741. ref_end = c->pic.data[plane_idx] + (bh * c->pic.linesize[plane_idx] + bw) * 8;
  742. for (i = 0; i < 64; i++)
  743. coordmap[i] = (i & 7) + (i >> 3) * stride;
  744. for (by = 0; by < bh; by++) {
  745. for (i = 0; i < BINKB_NB_SRC; i++) {
  746. if (binkb_read_bundle(c, gb, i) < 0)
  747. return -1;
  748. }
  749. dst = c->pic.data[plane_idx] + 8*by*stride;
  750. for (bx = 0; bx < bw; bx++, dst += 8) {
  751. blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
  752. switch (blk) {
  753. case 0:
  754. break;
  755. case 1:
  756. scan = bink_patterns[get_bits(gb, 4)];
  757. i = 0;
  758. do {
  759. int mode, run;
  760. mode = get_bits1(gb);
  761. run = get_bits(gb, binkb_runbits[i]) + 1;
  762. i += run;
  763. if (i > 64) {
  764. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  765. return -1;
  766. }
  767. if (mode) {
  768. v = binkb_get_value(c, BINKB_SRC_COLORS);
  769. for (j = 0; j < run; j++)
  770. dst[coordmap[*scan++]] = v;
  771. } else {
  772. for (j = 0; j < run; j++)
  773. dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
  774. }
  775. } while (i < 63);
  776. if (i == 63)
  777. dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
  778. break;
  779. case 2:
  780. memset(dctblock, 0, sizeof(*dctblock) * 64);
  781. dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
  782. qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
  783. read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
  784. c->bdsp.idct_put(dst, stride, dctblock);
  785. break;
  786. case 3:
  787. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  788. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  789. ref = dst + xoff + yoff * stride;
  790. if (ref < ref_start || ref + 8*stride > ref_end) {
  791. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  792. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  793. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  794. } else {
  795. put_pixels8x8_overlapped(dst, ref, stride);
  796. }
  797. c->dsp.clear_block(block);
  798. v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
  799. read_residue(gb, block, v);
  800. c->dsp.add_pixels8(dst, block, stride);
  801. break;
  802. case 4:
  803. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  804. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  805. ref = dst + xoff + yoff * stride;
  806. if (ref < ref_start || ref + 8 * stride > ref_end) {
  807. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  808. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  809. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  810. } else {
  811. put_pixels8x8_overlapped(dst, ref, stride);
  812. }
  813. memset(dctblock, 0, sizeof(*dctblock) * 64);
  814. dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
  815. qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
  816. read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
  817. c->bdsp.idct_add(dst, stride, dctblock);
  818. break;
  819. case 5:
  820. v = binkb_get_value(c, BINKB_SRC_COLORS);
  821. c->dsp.fill_block_tab[1](dst, v, stride, 8);
  822. break;
  823. case 6:
  824. for (i = 0; i < 2; i++)
  825. col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
  826. for (i = 0; i < 8; i++) {
  827. v = binkb_get_value(c, BINKB_SRC_PATTERN);
  828. for (j = 0; j < 8; j++, v >>= 1)
  829. dst[i*stride + j] = col[v & 1];
  830. }
  831. break;
  832. case 7:
  833. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  834. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  835. ref = dst + xoff + yoff * stride;
  836. if (ref < ref_start || ref + 8 * stride > ref_end) {
  837. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  838. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  839. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  840. } else {
  841. put_pixels8x8_overlapped(dst, ref, stride);
  842. }
  843. break;
  844. case 8:
  845. for (i = 0; i < 8; i++)
  846. memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
  847. c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
  848. break;
  849. default:
  850. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  851. return -1;
  852. }
  853. }
  854. }
  855. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  856. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  857. return 0;
  858. }
  859. static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
  860. int is_chroma)
  861. {
  862. int blk;
  863. int i, j, bx, by;
  864. uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
  865. int v, col[2];
  866. const uint8_t *scan;
  867. int xoff, yoff;
  868. LOCAL_ALIGNED_16(DCTELEM, block, [64]);
  869. LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
  870. LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
  871. int coordmap[64];
  872. const int stride = c->pic.linesize[plane_idx];
  873. int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
  874. int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
  875. int width = c->avctx->width >> is_chroma;
  876. init_lengths(c, FFMAX(width, 8), bw);
  877. for (i = 0; i < BINK_NB_SRC; i++)
  878. read_bundle(gb, c, i);
  879. ref_start = c->last.data[plane_idx] ? c->last.data[plane_idx]
  880. : c->pic.data[plane_idx];
  881. ref_end = ref_start
  882. + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
  883. for (i = 0; i < 64; i++)
  884. coordmap[i] = (i & 7) + (i >> 3) * stride;
  885. for (by = 0; by < bh; by++) {
  886. if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
  887. return -1;
  888. if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
  889. return -1;
  890. if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
  891. return -1;
  892. if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
  893. return -1;
  894. if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
  895. return -1;
  896. if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
  897. return -1;
  898. if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
  899. return -1;
  900. if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
  901. return -1;
  902. if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0)
  903. return -1;
  904. if (by == bh)
  905. break;
  906. dst = c->pic.data[plane_idx] + 8*by*stride;
  907. prev = (c->last.data[plane_idx] ? c->last.data[plane_idx]
  908. : c->pic.data[plane_idx]) + 8*by*stride;
  909. for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
  910. blk = get_value(c, BINK_SRC_BLOCK_TYPES);
  911. // 16x16 block type on odd line means part of the already decoded block, so skip it
  912. if ((by & 1) && blk == SCALED_BLOCK) {
  913. bx++;
  914. dst += 8;
  915. prev += 8;
  916. continue;
  917. }
  918. switch (blk) {
  919. case SKIP_BLOCK:
  920. c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
  921. break;
  922. case SCALED_BLOCK:
  923. blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
  924. switch (blk) {
  925. case RUN_BLOCK:
  926. scan = bink_patterns[get_bits(gb, 4)];
  927. i = 0;
  928. do {
  929. int run = get_value(c, BINK_SRC_RUN) + 1;
  930. i += run;
  931. if (i > 64) {
  932. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  933. return -1;
  934. }
  935. if (get_bits1(gb)) {
  936. v = get_value(c, BINK_SRC_COLORS);
  937. for (j = 0; j < run; j++)
  938. ublock[*scan++] = v;
  939. } else {
  940. for (j = 0; j < run; j++)
  941. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  942. }
  943. } while (i < 63);
  944. if (i == 63)
  945. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  946. break;
  947. case INTRA_BLOCK:
  948. memset(dctblock, 0, sizeof(*dctblock) * 64);
  949. dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
  950. read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
  951. c->bdsp.idct_put(ublock, 8, dctblock);
  952. break;
  953. case FILL_BLOCK:
  954. v = get_value(c, BINK_SRC_COLORS);
  955. c->dsp.fill_block_tab[0](dst, v, stride, 16);
  956. break;
  957. case PATTERN_BLOCK:
  958. for (i = 0; i < 2; i++)
  959. col[i] = get_value(c, BINK_SRC_COLORS);
  960. for (j = 0; j < 8; j++) {
  961. v = get_value(c, BINK_SRC_PATTERN);
  962. for (i = 0; i < 8; i++, v >>= 1)
  963. ublock[i + j*8] = col[v & 1];
  964. }
  965. break;
  966. case RAW_BLOCK:
  967. for (j = 0; j < 8; j++)
  968. for (i = 0; i < 8; i++)
  969. ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
  970. break;
  971. default:
  972. av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
  973. return -1;
  974. }
  975. if (blk != FILL_BLOCK)
  976. c->bdsp.scale_block(ublock, dst, stride);
  977. bx++;
  978. dst += 8;
  979. prev += 8;
  980. break;
  981. case MOTION_BLOCK:
  982. xoff = get_value(c, BINK_SRC_X_OFF);
  983. yoff = get_value(c, BINK_SRC_Y_OFF);
  984. ref = prev + xoff + yoff * stride;
  985. if (ref < ref_start || ref > ref_end) {
  986. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  987. bx*8 + xoff, by*8 + yoff);
  988. return -1;
  989. }
  990. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  991. break;
  992. case RUN_BLOCK:
  993. scan = bink_patterns[get_bits(gb, 4)];
  994. i = 0;
  995. do {
  996. int run = get_value(c, BINK_SRC_RUN) + 1;
  997. i += run;
  998. if (i > 64) {
  999. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  1000. return -1;
  1001. }
  1002. if (get_bits1(gb)) {
  1003. v = get_value(c, BINK_SRC_COLORS);
  1004. for (j = 0; j < run; j++)
  1005. dst[coordmap[*scan++]] = v;
  1006. } else {
  1007. for (j = 0; j < run; j++)
  1008. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  1009. }
  1010. } while (i < 63);
  1011. if (i == 63)
  1012. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  1013. break;
  1014. case RESIDUE_BLOCK:
  1015. xoff = get_value(c, BINK_SRC_X_OFF);
  1016. yoff = get_value(c, BINK_SRC_Y_OFF);
  1017. ref = prev + xoff + yoff * stride;
  1018. if (ref < ref_start || ref > ref_end) {
  1019. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  1020. bx*8 + xoff, by*8 + yoff);
  1021. return -1;
  1022. }
  1023. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  1024. c->dsp.clear_block(block);
  1025. v = get_bits(gb, 7);
  1026. read_residue(gb, block, v);
  1027. c->dsp.add_pixels8(dst, block, stride);
  1028. break;
  1029. case INTRA_BLOCK:
  1030. memset(dctblock, 0, sizeof(*dctblock) * 64);
  1031. dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
  1032. read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
  1033. c->bdsp.idct_put(dst, stride, dctblock);
  1034. break;
  1035. case FILL_BLOCK:
  1036. v = get_value(c, BINK_SRC_COLORS);
  1037. c->dsp.fill_block_tab[1](dst, v, stride, 8);
  1038. break;
  1039. case INTER_BLOCK:
  1040. xoff = get_value(c, BINK_SRC_X_OFF);
  1041. yoff = get_value(c, BINK_SRC_Y_OFF);
  1042. ref = prev + xoff + yoff * stride;
  1043. if (ref < ref_start || ref > ref_end) {
  1044. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  1045. bx*8 + xoff, by*8 + yoff);
  1046. return -1;
  1047. }
  1048. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  1049. memset(dctblock, 0, sizeof(*dctblock) * 64);
  1050. dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
  1051. read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
  1052. c->bdsp.idct_add(dst, stride, dctblock);
  1053. break;
  1054. case PATTERN_BLOCK:
  1055. for (i = 0; i < 2; i++)
  1056. col[i] = get_value(c, BINK_SRC_COLORS);
  1057. for (i = 0; i < 8; i++) {
  1058. v = get_value(c, BINK_SRC_PATTERN);
  1059. for (j = 0; j < 8; j++, v >>= 1)
  1060. dst[i*stride + j] = col[v & 1];
  1061. }
  1062. break;
  1063. case RAW_BLOCK:
  1064. for (i = 0; i < 8; i++)
  1065. memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
  1066. c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
  1067. break;
  1068. default:
  1069. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  1070. return -1;
  1071. }
  1072. }
  1073. }
  1074. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  1075. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  1076. return 0;
  1077. }
  1078. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
  1079. {
  1080. BinkContext * const c = avctx->priv_data;
  1081. GetBitContext gb;
  1082. int plane, plane_idx;
  1083. int bits_count = pkt->size << 3;
  1084. if (c->version > 'b') {
  1085. if(c->pic.data[0])
  1086. avctx->release_buffer(avctx, &c->pic);
  1087. if(avctx->get_buffer(avctx, &c->pic) < 0){
  1088. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1089. return -1;
  1090. }
  1091. } else {
  1092. if(avctx->reget_buffer(avctx, &c->pic) < 0){
  1093. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  1094. return -1;
  1095. }
  1096. }
  1097. init_get_bits(&gb, pkt->data, bits_count);
  1098. if (c->has_alpha) {
  1099. if (c->version >= 'i')
  1100. skip_bits_long(&gb, 32);
  1101. if (bink_decode_plane(c, &gb, 3, 0) < 0)
  1102. return -1;
  1103. }
  1104. if (c->version >= 'i')
  1105. skip_bits_long(&gb, 32);
  1106. for (plane = 0; plane < 3; plane++) {
  1107. plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
  1108. if (c->version > 'b') {
  1109. if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
  1110. return -1;
  1111. } else {
  1112. if (binkb_decode_plane(c, &gb, plane_idx, !pkt->pts, !!plane) < 0)
  1113. return -1;
  1114. }
  1115. if (get_bits_count(&gb) >= bits_count)
  1116. break;
  1117. }
  1118. emms_c();
  1119. *data_size = sizeof(AVFrame);
  1120. *(AVFrame*)data = c->pic;
  1121. if (c->version > 'b')
  1122. FFSWAP(AVFrame, c->pic, c->last);
  1123. /* always report that the buffer was completely consumed */
  1124. return pkt->size;
  1125. }
  1126. /**
  1127. * Caclulate quantization tables for version b
  1128. */
  1129. static av_cold void binkb_calc_quant(void)
  1130. {
  1131. uint8_t inv_bink_scan[64];
  1132. static const int s[64]={
  1133. 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
  1134. 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
  1135. 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
  1136. 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
  1137. 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
  1138. 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
  1139. 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
  1140. 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
  1141. };
  1142. int i, j;
  1143. #define C (1LL<<30)
  1144. for (i = 0; i < 64; i++)
  1145. inv_bink_scan[bink_scan[i]] = i;
  1146. for (j = 0; j < 16; j++) {
  1147. for (i = 0; i < 64; i++) {
  1148. int k = inv_bink_scan[i];
  1149. binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
  1150. binkb_num[j]/(binkb_den[j] * (C>>12));
  1151. binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
  1152. binkb_num[j]/(binkb_den[j] * (C>>12));
  1153. }
  1154. }
  1155. }
  1156. static av_cold int decode_init(AVCodecContext *avctx)
  1157. {
  1158. BinkContext * const c = avctx->priv_data;
  1159. static VLC_TYPE table[16 * 128][2];
  1160. static int binkb_initialised = 0;
  1161. int i;
  1162. int flags;
  1163. c->version = avctx->codec_tag >> 24;
  1164. if (avctx->extradata_size < 4) {
  1165. av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
  1166. return -1;
  1167. }
  1168. flags = AV_RL32(avctx->extradata);
  1169. c->has_alpha = flags & BINK_FLAG_ALPHA;
  1170. c->swap_planes = c->version >= 'h';
  1171. if (!bink_trees[15].table) {
  1172. for (i = 0; i < 16; i++) {
  1173. const int maxbits = bink_tree_lens[i][15];
  1174. bink_trees[i].table = table + i*128;
  1175. bink_trees[i].table_allocated = 1 << maxbits;
  1176. init_vlc(&bink_trees[i], maxbits, 16,
  1177. bink_tree_lens[i], 1, 1,
  1178. bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  1179. }
  1180. }
  1181. c->avctx = avctx;
  1182. c->pic.data[0] = NULL;
  1183. if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
  1184. return 1;
  1185. }
  1186. avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
  1187. avctx->idct_algo = FF_IDCT_BINK;
  1188. ff_dsputil_init(&c->dsp, avctx);
  1189. ff_binkdsp_init(&c->bdsp);
  1190. init_bundles(c);
  1191. if (c->version == 'b') {
  1192. if (!binkb_initialised) {
  1193. binkb_calc_quant();
  1194. binkb_initialised = 1;
  1195. }
  1196. }
  1197. return 0;
  1198. }
  1199. static av_cold int decode_end(AVCodecContext *avctx)
  1200. {
  1201. BinkContext * const c = avctx->priv_data;
  1202. if (c->pic.data[0])
  1203. avctx->release_buffer(avctx, &c->pic);
  1204. if (c->last.data[0])
  1205. avctx->release_buffer(avctx, &c->last);
  1206. free_bundles(c);
  1207. return 0;
  1208. }
  1209. AVCodec ff_bink_decoder = {
  1210. .name = "binkvideo",
  1211. .type = AVMEDIA_TYPE_VIDEO,
  1212. .id = AV_CODEC_ID_BINKVIDEO,
  1213. .priv_data_size = sizeof(BinkContext),
  1214. .init = decode_init,
  1215. .close = decode_end,
  1216. .decode = decode_frame,
  1217. .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
  1218. .capabilities = CODEC_CAP_DR1,
  1219. };