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.

1357 lines
45KB

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