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.

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