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.

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