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.

1346 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/imgutils.h"
  23. #include "libavutil/internal.h"
  24. #include "avcodec.h"
  25. #include "dsputil.h"
  26. #include "binkdata.h"
  27. #include "binkdsp.h"
  28. #include "hpeldsp.h"
  29. #include "internal.h"
  30. #include "mathops.h"
  31. #define BITSTREAM_READER_LE
  32. #include "get_bits.h"
  33. #define BINK_FLAG_ALPHA 0x00100000
  34. #define BINK_FLAG_GRAY 0x00020000
  35. static VLC bink_trees[16];
  36. /**
  37. * IDs for different data types used in old version of Bink video codec
  38. */
  39. enum OldSources {
  40. BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
  41. BINKB_SRC_COLORS, ///< pixel values used for different block types
  42. BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
  43. BINKB_SRC_X_OFF, ///< X components of motion value
  44. BINKB_SRC_Y_OFF, ///< Y components of motion value
  45. BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
  46. BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
  47. BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
  48. BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
  49. BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
  50. BINKB_NB_SRC
  51. };
  52. static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
  53. 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
  54. };
  55. static const int binkb_bundle_signed[BINKB_NB_SRC] = {
  56. 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
  57. };
  58. static int32_t binkb_intra_quant[16][64];
  59. static int32_t binkb_inter_quant[16][64];
  60. /**
  61. * IDs for different data types used in Bink video codec
  62. */
  63. enum Sources {
  64. BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
  65. BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
  66. BINK_SRC_COLORS, ///< pixel values used for different block types
  67. BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
  68. BINK_SRC_X_OFF, ///< X components of motion value
  69. BINK_SRC_Y_OFF, ///< Y components of motion value
  70. BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
  71. BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
  72. BINK_SRC_RUN, ///< run lengths for special fill block
  73. BINK_NB_SRC
  74. };
  75. /**
  76. * data needed to decode 4-bit Huffman-coded value
  77. */
  78. typedef struct Tree {
  79. int vlc_num; ///< tree number (in bink_trees[])
  80. uint8_t syms[16]; ///< leaf value to symbol mapping
  81. } Tree;
  82. #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
  83. bink_trees[(tree).vlc_num].bits, 1)]
  84. /**
  85. * data structure used for decoding single Bink data type
  86. */
  87. typedef struct Bundle {
  88. int len; ///< length of number of entries to decode (in bits)
  89. Tree tree; ///< Huffman tree-related data
  90. uint8_t *data; ///< buffer for decoded symbols
  91. uint8_t *data_end; ///< buffer end
  92. uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
  93. uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
  94. } Bundle;
  95. /*
  96. * Decoder context
  97. */
  98. typedef struct BinkContext {
  99. AVCodecContext *avctx;
  100. DSPContext dsp;
  101. HpelDSPContext hdsp;
  102. BinkDSPContext bdsp;
  103. AVFrame *last;
  104. int version; ///< internal Bink file version
  105. int has_alpha;
  106. int swap_planes;
  107. Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
  108. Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
  109. int col_lastval; ///< value of last decoded high nibble in "colours" data type
  110. } BinkContext;
  111. /**
  112. * Bink video block types
  113. */
  114. enum BlockTypes {
  115. SKIP_BLOCK = 0, ///< skipped block
  116. SCALED_BLOCK, ///< block has size 16x16
  117. MOTION_BLOCK, ///< block is copied from previous frame with some offset
  118. RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
  119. RESIDUE_BLOCK, ///< motion block with some difference added
  120. INTRA_BLOCK, ///< intra DCT block
  121. FILL_BLOCK, ///< block is filled with single colour
  122. INTER_BLOCK, ///< motion block with DCT applied to the difference
  123. PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
  124. RAW_BLOCK, ///< uncoded 8x8 block
  125. };
  126. /**
  127. * Initialize length length in all bundles.
  128. *
  129. * @param c decoder context
  130. * @param width plane width
  131. * @param bw plane width in 8x8 blocks
  132. */
  133. static void init_lengths(BinkContext *c, int width, int bw)
  134. {
  135. width = FFALIGN(width, 8);
  136. c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
  137. c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
  138. c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
  139. c->bundle[BINK_SRC_INTRA_DC].len =
  140. c->bundle[BINK_SRC_INTER_DC].len =
  141. c->bundle[BINK_SRC_X_OFF].len =
  142. c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
  143. c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
  144. c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
  145. }
  146. /**
  147. * Allocate memory for bundles.
  148. *
  149. * @param c decoder context
  150. */
  151. static av_cold void init_bundles(BinkContext *c)
  152. {
  153. int bw, bh, blocks;
  154. int i;
  155. bw = (c->avctx->width + 7) >> 3;
  156. bh = (c->avctx->height + 7) >> 3;
  157. blocks = bw * bh;
  158. for (i = 0; i < BINKB_NB_SRC; i++) {
  159. c->bundle[i].data = av_malloc(blocks * 64);
  160. c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
  161. }
  162. }
  163. /**
  164. * Free memory used by bundles.
  165. *
  166. * @param c decoder context
  167. */
  168. static av_cold void free_bundles(BinkContext *c)
  169. {
  170. int i;
  171. for (i = 0; i < BINKB_NB_SRC; i++)
  172. av_freep(&c->bundle[i].data);
  173. }
  174. /**
  175. * Merge two consequent lists of equal size depending on bits read.
  176. *
  177. * @param gb context for reading bits
  178. * @param dst buffer where merged list will be written to
  179. * @param src pointer to the head of the first list (the second lists starts at src+size)
  180. * @param size input lists size
  181. */
  182. static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
  183. {
  184. uint8_t *src2 = src + size;
  185. int size2 = size;
  186. do {
  187. if (!get_bits1(gb)) {
  188. *dst++ = *src++;
  189. size--;
  190. } else {
  191. *dst++ = *src2++;
  192. size2--;
  193. }
  194. } while (size && size2);
  195. while (size--)
  196. *dst++ = *src++;
  197. while (size2--)
  198. *dst++ = *src2++;
  199. }
  200. /**
  201. * Read information about Huffman tree used to decode data.
  202. *
  203. * @param gb context for reading bits
  204. * @param tree pointer for storing tree data
  205. */
  206. static void read_tree(GetBitContext *gb, Tree *tree)
  207. {
  208. uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
  209. int i, t, len;
  210. tree->vlc_num = get_bits(gb, 4);
  211. if (!tree->vlc_num) {
  212. for (i = 0; i < 16; i++)
  213. tree->syms[i] = i;
  214. return;
  215. }
  216. if (get_bits1(gb)) {
  217. len = get_bits(gb, 3);
  218. for (i = 0; i <= len; i++) {
  219. tree->syms[i] = get_bits(gb, 4);
  220. tmp1[tree->syms[i]] = 1;
  221. }
  222. for (i = 0; i < 16 && len < 16 - 1; i++)
  223. if (!tmp1[i])
  224. tree->syms[++len] = i;
  225. } else {
  226. len = get_bits(gb, 2);
  227. for (i = 0; i < 16; i++)
  228. in[i] = i;
  229. for (i = 0; i <= len; i++) {
  230. int size = 1 << i;
  231. for (t = 0; t < 16; t += size << 1)
  232. merge(gb, out + t, in + t, size);
  233. FFSWAP(uint8_t*, in, out);
  234. }
  235. memcpy(tree->syms, in, 16);
  236. }
  237. }
  238. /**
  239. * Prepare bundle for decoding data.
  240. *
  241. * @param gb context for reading bits
  242. * @param c decoder context
  243. * @param bundle_num number of the bundle to initialize
  244. */
  245. static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
  246. {
  247. int i;
  248. if (bundle_num == BINK_SRC_COLORS) {
  249. for (i = 0; i < 16; i++)
  250. read_tree(gb, &c->col_high[i]);
  251. c->col_lastval = 0;
  252. }
  253. if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
  254. read_tree(gb, &c->bundle[bundle_num].tree);
  255. c->bundle[bundle_num].cur_dec =
  256. c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
  257. }
  258. /**
  259. * common check before starting decoding bundle data
  260. *
  261. * @param gb context for reading bits
  262. * @param b bundle
  263. * @param t variable where number of elements to decode will be stored
  264. */
  265. #define CHECK_READ_VAL(gb, b, t) \
  266. if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
  267. return 0; \
  268. t = get_bits(gb, b->len); \
  269. if (!t) { \
  270. b->cur_dec = NULL; \
  271. return 0; \
  272. } \
  273. static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  274. {
  275. int t, v;
  276. const uint8_t *dec_end;
  277. CHECK_READ_VAL(gb, b, t);
  278. dec_end = b->cur_dec + t;
  279. if (dec_end > b->data_end) {
  280. av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
  281. return AVERROR_INVALIDDATA;
  282. }
  283. if (get_bits1(gb)) {
  284. v = get_bits(gb, 4);
  285. memset(b->cur_dec, v, t);
  286. b->cur_dec += t;
  287. } else {
  288. while (b->cur_dec < dec_end)
  289. *b->cur_dec++ = GET_HUFF(gb, b->tree);
  290. }
  291. return 0;
  292. }
  293. static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  294. {
  295. int t, sign, v;
  296. const uint8_t *dec_end;
  297. CHECK_READ_VAL(gb, b, t);
  298. dec_end = b->cur_dec + t;
  299. if (dec_end > b->data_end) {
  300. av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
  301. return AVERROR_INVALIDDATA;
  302. }
  303. if (get_bits1(gb)) {
  304. v = get_bits(gb, 4);
  305. if (v) {
  306. sign = -get_bits1(gb);
  307. v = (v ^ sign) - sign;
  308. }
  309. memset(b->cur_dec, v, t);
  310. b->cur_dec += t;
  311. } else {
  312. while (b->cur_dec < dec_end) {
  313. v = GET_HUFF(gb, b->tree);
  314. if (v) {
  315. sign = -get_bits1(gb);
  316. v = (v ^ sign) - sign;
  317. }
  318. *b->cur_dec++ = v;
  319. }
  320. }
  321. return 0;
  322. }
  323. static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
  324. static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  325. {
  326. int t, v;
  327. int last = 0;
  328. const uint8_t *dec_end;
  329. CHECK_READ_VAL(gb, b, t);
  330. dec_end = b->cur_dec + t;
  331. if (dec_end > b->data_end) {
  332. av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
  333. return AVERROR_INVALIDDATA;
  334. }
  335. if (get_bits1(gb)) {
  336. v = get_bits(gb, 4);
  337. memset(b->cur_dec, v, t);
  338. b->cur_dec += t;
  339. } else {
  340. while (b->cur_dec < dec_end) {
  341. v = GET_HUFF(gb, b->tree);
  342. if (v < 12) {
  343. last = v;
  344. *b->cur_dec++ = v;
  345. } else {
  346. int run = bink_rlelens[v - 12];
  347. if (dec_end - b->cur_dec < run)
  348. return AVERROR_INVALIDDATA;
  349. memset(b->cur_dec, last, run);
  350. b->cur_dec += run;
  351. }
  352. }
  353. }
  354. return 0;
  355. }
  356. static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  357. {
  358. int t, v;
  359. const uint8_t *dec_end;
  360. CHECK_READ_VAL(gb, b, t);
  361. dec_end = b->cur_dec + t;
  362. if (dec_end > b->data_end) {
  363. av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
  364. return AVERROR_INVALIDDATA;
  365. }
  366. while (b->cur_dec < dec_end) {
  367. v = GET_HUFF(gb, b->tree);
  368. v |= GET_HUFF(gb, b->tree) << 4;
  369. *b->cur_dec++ = v;
  370. }
  371. return 0;
  372. }
  373. static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
  374. {
  375. int t, sign, v;
  376. const uint8_t *dec_end;
  377. CHECK_READ_VAL(gb, b, t);
  378. dec_end = b->cur_dec + t;
  379. if (dec_end > b->data_end) {
  380. av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
  381. return AVERROR_INVALIDDATA;
  382. }
  383. if (get_bits1(gb)) {
  384. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  385. v = GET_HUFF(gb, b->tree);
  386. v = (c->col_lastval << 4) | v;
  387. if (c->version < 'i') {
  388. sign = ((int8_t) v) >> 7;
  389. v = ((v & 0x7F) ^ sign) - sign;
  390. v += 0x80;
  391. }
  392. memset(b->cur_dec, v, t);
  393. b->cur_dec += t;
  394. } else {
  395. while (b->cur_dec < dec_end) {
  396. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  397. v = GET_HUFF(gb, b->tree);
  398. v = (c->col_lastval << 4) | v;
  399. if (c->version < 'i') {
  400. sign = ((int8_t) v) >> 7;
  401. v = ((v & 0x7F) ^ sign) - sign;
  402. v += 0x80;
  403. }
  404. *b->cur_dec++ = v;
  405. }
  406. }
  407. return 0;
  408. }
  409. /** number of bits used to store first DC value in bundle */
  410. #define DC_START_BITS 11
  411. static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
  412. int start_bits, int has_sign)
  413. {
  414. int i, j, len, len2, bsize, sign, v, v2;
  415. int16_t *dst = (int16_t*)b->cur_dec;
  416. int16_t *dst_end = (int16_t*)b->data_end;
  417. CHECK_READ_VAL(gb, b, len);
  418. v = get_bits(gb, start_bits - has_sign);
  419. if (v && has_sign) {
  420. sign = -get_bits1(gb);
  421. v = (v ^ sign) - sign;
  422. }
  423. if (dst_end - dst < 1)
  424. return AVERROR_INVALIDDATA;
  425. *dst++ = v;
  426. len--;
  427. for (i = 0; i < len; i += 8) {
  428. len2 = FFMIN(len - i, 8);
  429. if (dst_end - dst < len2)
  430. return AVERROR_INVALIDDATA;
  431. bsize = get_bits(gb, 4);
  432. if (bsize) {
  433. for (j = 0; j < len2; j++) {
  434. v2 = get_bits(gb, bsize);
  435. if (v2) {
  436. sign = -get_bits1(gb);
  437. v2 = (v2 ^ sign) - sign;
  438. }
  439. v += v2;
  440. *dst++ = v;
  441. if (v < -32768 || v > 32767) {
  442. av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
  443. return AVERROR_INVALIDDATA;
  444. }
  445. }
  446. } else {
  447. for (j = 0; j < len2; j++)
  448. *dst++ = v;
  449. }
  450. }
  451. b->cur_dec = (uint8_t*)dst;
  452. return 0;
  453. }
  454. /**
  455. * Retrieve next value from bundle.
  456. *
  457. * @param c decoder context
  458. * @param bundle bundle number
  459. */
  460. static inline int get_value(BinkContext *c, int bundle)
  461. {
  462. int ret;
  463. if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
  464. return *c->bundle[bundle].cur_ptr++;
  465. if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
  466. return (int8_t)*c->bundle[bundle].cur_ptr++;
  467. ret = *(int16_t*)c->bundle[bundle].cur_ptr;
  468. c->bundle[bundle].cur_ptr += 2;
  469. return ret;
  470. }
  471. static void binkb_init_bundle(BinkContext *c, int bundle_num)
  472. {
  473. c->bundle[bundle_num].cur_dec =
  474. c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
  475. c->bundle[bundle_num].len = 13;
  476. }
  477. static void binkb_init_bundles(BinkContext *c)
  478. {
  479. int i;
  480. for (i = 0; i < BINKB_NB_SRC; i++)
  481. binkb_init_bundle(c, i);
  482. }
  483. static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
  484. {
  485. const int bits = binkb_bundle_sizes[bundle_num];
  486. const int mask = 1 << (bits - 1);
  487. const int issigned = binkb_bundle_signed[bundle_num];
  488. Bundle *b = &c->bundle[bundle_num];
  489. int i, len;
  490. CHECK_READ_VAL(gb, b, len);
  491. if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
  492. return AVERROR_INVALIDDATA;
  493. if (bits <= 8) {
  494. if (!issigned) {
  495. for (i = 0; i < len; i++)
  496. *b->cur_dec++ = get_bits(gb, bits);
  497. } else {
  498. for (i = 0; i < len; i++)
  499. *b->cur_dec++ = get_bits(gb, bits) - mask;
  500. }
  501. } else {
  502. int16_t *dst = (int16_t*)b->cur_dec;
  503. if (!issigned) {
  504. for (i = 0; i < len; i++)
  505. *dst++ = get_bits(gb, bits);
  506. } else {
  507. for (i = 0; i < len; i++)
  508. *dst++ = get_bits(gb, bits) - mask;
  509. }
  510. b->cur_dec = (uint8_t*)dst;
  511. }
  512. return 0;
  513. }
  514. static inline int binkb_get_value(BinkContext *c, int bundle_num)
  515. {
  516. int16_t ret;
  517. const int bits = binkb_bundle_sizes[bundle_num];
  518. if (bits <= 8) {
  519. int val = *c->bundle[bundle_num].cur_ptr++;
  520. return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
  521. }
  522. ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
  523. c->bundle[bundle_num].cur_ptr += 2;
  524. return ret;
  525. }
  526. /**
  527. * Read 8x8 block of DCT coefficients.
  528. *
  529. * @param gb context for reading bits
  530. * @param block place for storing coefficients
  531. * @param scan scan order table
  532. * @param quant_matrices quantization matrices
  533. * @return 0 for success, negative value in other cases
  534. */
  535. static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
  536. const int32_t quant_matrices[16][64], int q)
  537. {
  538. int coef_list[128];
  539. int mode_list[128];
  540. int i, t, bits, ccoef, mode, sign;
  541. int list_start = 64, list_end = 64, list_pos;
  542. int coef_count = 0;
  543. int coef_idx[64];
  544. int quant_idx;
  545. const int32_t *quant;
  546. coef_list[list_end] = 4; mode_list[list_end++] = 0;
  547. coef_list[list_end] = 24; mode_list[list_end++] = 0;
  548. coef_list[list_end] = 44; mode_list[list_end++] = 0;
  549. coef_list[list_end] = 1; mode_list[list_end++] = 3;
  550. coef_list[list_end] = 2; mode_list[list_end++] = 3;
  551. coef_list[list_end] = 3; mode_list[list_end++] = 3;
  552. for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
  553. list_pos = list_start;
  554. while (list_pos < list_end) {
  555. if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
  556. list_pos++;
  557. continue;
  558. }
  559. ccoef = coef_list[list_pos];
  560. mode = mode_list[list_pos];
  561. switch (mode) {
  562. case 0:
  563. coef_list[list_pos] = ccoef + 4;
  564. mode_list[list_pos] = 1;
  565. case 2:
  566. if (mode == 2) {
  567. coef_list[list_pos] = 0;
  568. mode_list[list_pos++] = 0;
  569. }
  570. for (i = 0; i < 4; i++, ccoef++) {
  571. if (get_bits1(gb)) {
  572. coef_list[--list_start] = ccoef;
  573. mode_list[ list_start] = 3;
  574. } else {
  575. if (!bits) {
  576. t = 1 - (get_bits1(gb) << 1);
  577. } else {
  578. t = get_bits(gb, bits) | 1 << bits;
  579. sign = -get_bits1(gb);
  580. t = (t ^ sign) - sign;
  581. }
  582. block[scan[ccoef]] = t;
  583. coef_idx[coef_count++] = ccoef;
  584. }
  585. }
  586. break;
  587. case 1:
  588. mode_list[list_pos] = 2;
  589. for (i = 0; i < 3; i++) {
  590. ccoef += 4;
  591. coef_list[list_end] = ccoef;
  592. mode_list[list_end++] = 2;
  593. }
  594. break;
  595. case 3:
  596. if (!bits) {
  597. t = 1 - (get_bits1(gb) << 1);
  598. } else {
  599. t = get_bits(gb, bits) | 1 << bits;
  600. sign = -get_bits1(gb);
  601. t = (t ^ sign) - sign;
  602. }
  603. block[scan[ccoef]] = t;
  604. coef_idx[coef_count++] = ccoef;
  605. coef_list[list_pos] = 0;
  606. mode_list[list_pos++] = 0;
  607. break;
  608. }
  609. }
  610. }
  611. if (q == -1) {
  612. quant_idx = get_bits(gb, 4);
  613. } else {
  614. quant_idx = q;
  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, int16_t 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, AVFrame *frame, GetBitContext *gb,
  723. int plane_idx, int is_key, int is_chroma)
  724. {
  725. int blk, ret;
  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(int16_t, 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 = frame->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 = frame->data[plane_idx];
  741. ref_end = frame->data[plane_idx] + (bh * frame->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 ((ret = binkb_read_bundle(c, gb, i)) < 0)
  747. return ret;
  748. }
  749. dst = frame->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 AVERROR_INVALIDDATA;
  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, 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->hdsp.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->hdsp.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, 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->hdsp.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 AVERROR_INVALIDDATA;
  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, AVFrame *frame, GetBitContext *gb,
  860. int plane_idx, int is_chroma)
  861. {
  862. int blk, ret;
  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(int16_t, 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 = frame->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. : frame->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 ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
  887. return ret;
  888. if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
  889. return ret;
  890. if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
  891. return ret;
  892. if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
  893. return ret;
  894. if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
  895. return ret;
  896. if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
  897. return ret;
  898. if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
  899. return ret;
  900. if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
  901. return ret;
  902. if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
  903. return ret;
  904. if (by == bh)
  905. break;
  906. dst = frame->data[plane_idx] + 8*by*stride;
  907. prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
  908. : frame->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->hdsp.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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  989. }
  990. c->hdsp.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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  1022. }
  1023. c->hdsp.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. c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  1044. memset(dctblock, 0, sizeof(*dctblock) * 64);
  1045. dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
  1046. read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
  1047. c->bdsp.idct_add(dst, stride, dctblock);
  1048. break;
  1049. case PATTERN_BLOCK:
  1050. for (i = 0; i < 2; i++)
  1051. col[i] = get_value(c, BINK_SRC_COLORS);
  1052. for (i = 0; i < 8; i++) {
  1053. v = get_value(c, BINK_SRC_PATTERN);
  1054. for (j = 0; j < 8; j++, v >>= 1)
  1055. dst[i*stride + j] = col[v & 1];
  1056. }
  1057. break;
  1058. case RAW_BLOCK:
  1059. for (i = 0; i < 8; i++)
  1060. memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
  1061. c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
  1062. break;
  1063. default:
  1064. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  1065. return AVERROR_INVALIDDATA;
  1066. }
  1067. }
  1068. }
  1069. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  1070. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  1071. return 0;
  1072. }
  1073. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
  1074. {
  1075. BinkContext * const c = avctx->priv_data;
  1076. AVFrame *frame = data;
  1077. GetBitContext gb;
  1078. int plane, plane_idx, ret;
  1079. int bits_count = pkt->size << 3;
  1080. if (c->version > 'b') {
  1081. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
  1082. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1083. return ret;
  1084. }
  1085. } else {
  1086. if ((ret = ff_reget_buffer(avctx, c->last)) < 0) {
  1087. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  1088. return ret;
  1089. }
  1090. if ((ret = av_frame_ref(frame, c->last)) < 0)
  1091. return ret;
  1092. }
  1093. init_get_bits(&gb, pkt->data, bits_count);
  1094. if (c->has_alpha) {
  1095. if (c->version >= 'i')
  1096. skip_bits_long(&gb, 32);
  1097. if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
  1098. return ret;
  1099. }
  1100. if (c->version >= 'i')
  1101. skip_bits_long(&gb, 32);
  1102. for (plane = 0; plane < 3; plane++) {
  1103. plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
  1104. if (c->version > 'b') {
  1105. if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
  1106. return ret;
  1107. } else {
  1108. if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
  1109. !avctx->frame_number, !!plane)) < 0)
  1110. return ret;
  1111. }
  1112. if (get_bits_count(&gb) >= bits_count)
  1113. break;
  1114. }
  1115. emms_c();
  1116. if (c->version > 'b') {
  1117. av_frame_unref(c->last);
  1118. if ((ret = av_frame_ref(c->last, frame)) < 0)
  1119. return ret;
  1120. }
  1121. *got_frame = 1;
  1122. /* always report that the buffer was completely consumed */
  1123. return pkt->size;
  1124. }
  1125. /**
  1126. * Caclulate quantization tables for version b
  1127. */
  1128. static av_cold void binkb_calc_quant(void)
  1129. {
  1130. uint8_t inv_bink_scan[64];
  1131. double s[64];
  1132. int i, j;
  1133. for (j = 0; j < 8; j++) {
  1134. for (i = 0; i < 8; i++) {
  1135. if (j && j != 4)
  1136. if (i && i != 4)
  1137. s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
  1138. else
  1139. s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
  1140. else
  1141. if (i && i != 4)
  1142. s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
  1143. else
  1144. s[j*8 + i] = 1.0;
  1145. }
  1146. }
  1147. for (i = 0; i < 64; i++)
  1148. inv_bink_scan[bink_scan[i]] = i;
  1149. for (j = 0; j < 16; j++) {
  1150. for (i = 0; i < 64; i++) {
  1151. int k = inv_bink_scan[i];
  1152. if (s[i] == 1.0) {
  1153. binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
  1154. binkb_num[j]/binkb_den[j];
  1155. binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
  1156. binkb_num[j]/binkb_den[j];
  1157. } else {
  1158. binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
  1159. binkb_num[j]/(double)binkb_den[j];
  1160. binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
  1161. binkb_num[j]/(double)binkb_den[j];
  1162. }
  1163. }
  1164. }
  1165. }
  1166. static av_cold int decode_init(AVCodecContext *avctx)
  1167. {
  1168. BinkContext * const c = avctx->priv_data;
  1169. static VLC_TYPE table[16 * 128][2];
  1170. static int binkb_initialised = 0;
  1171. int i, ret;
  1172. int flags;
  1173. c->version = avctx->codec_tag >> 24;
  1174. if (avctx->extradata_size < 4) {
  1175. av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
  1176. return AVERROR_INVALIDDATA;
  1177. }
  1178. flags = AV_RL32(avctx->extradata);
  1179. c->has_alpha = flags & BINK_FLAG_ALPHA;
  1180. c->swap_planes = c->version >= 'h';
  1181. if (!bink_trees[15].table) {
  1182. for (i = 0; i < 16; i++) {
  1183. const int maxbits = bink_tree_lens[i][15];
  1184. bink_trees[i].table = table + i*128;
  1185. bink_trees[i].table_allocated = 1 << maxbits;
  1186. init_vlc(&bink_trees[i], maxbits, 16,
  1187. bink_tree_lens[i], 1, 1,
  1188. bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  1189. }
  1190. }
  1191. c->avctx = avctx;
  1192. c->last = av_frame_alloc();
  1193. if (!c->last)
  1194. return AVERROR(ENOMEM);
  1195. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  1196. return ret;
  1197. avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
  1198. ff_dsputil_init(&c->dsp, avctx);
  1199. ff_hpeldsp_init(&c->hdsp, avctx->flags);
  1200. ff_binkdsp_init(&c->bdsp);
  1201. init_bundles(c);
  1202. if (c->version == 'b') {
  1203. if (!binkb_initialised) {
  1204. binkb_calc_quant();
  1205. binkb_initialised = 1;
  1206. }
  1207. }
  1208. return 0;
  1209. }
  1210. static av_cold int decode_end(AVCodecContext *avctx)
  1211. {
  1212. BinkContext * const c = avctx->priv_data;
  1213. av_frame_free(&c->last);
  1214. free_bundles(c);
  1215. return 0;
  1216. }
  1217. AVCodec ff_bink_decoder = {
  1218. .name = "binkvideo",
  1219. .type = AVMEDIA_TYPE_VIDEO,
  1220. .id = AV_CODEC_ID_BINKVIDEO,
  1221. .priv_data_size = sizeof(BinkContext),
  1222. .init = decode_init,
  1223. .close = decode_end,
  1224. .decode = decode_frame,
  1225. .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
  1226. .capabilities = CODEC_CAP_DR1,
  1227. };