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.

1347 lines
45KB

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