You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1346 lines
45KB

  1. /*
  2. * Bink video decoder
  3. * Copyright (c) 2009 Konstantin Shishkov
  4. * Copyright (C) 2011 Peter Ross <pross@xvid.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 int 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. if (!c->bundle[i].data)
  159. return AVERROR(ENOMEM);
  160. c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
  161. }
  162. return 0;
  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 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 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. if (quant_idx > 15U) {
  617. av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
  618. return AVERROR_INVALIDDATA;
  619. }
  620. }
  621. quant = quant_matrices[quant_idx];
  622. block[0] = (block[0] * quant[0]) >> 11;
  623. for (i = 0; i < coef_count; i++) {
  624. int idx = coef_idx[i];
  625. block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
  626. }
  627. return 0;
  628. }
  629. /**
  630. * Read 8x8 block with residue after motion compensation.
  631. *
  632. * @param gb context for reading bits
  633. * @param block place to store read data
  634. * @param masks_count number of masks to decode
  635. * @return 0 on success, negative value in other cases
  636. */
  637. static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
  638. {
  639. int coef_list[128];
  640. int mode_list[128];
  641. int i, sign, mask, ccoef, mode;
  642. int list_start = 64, list_end = 64, list_pos;
  643. int nz_coeff[64];
  644. int nz_coeff_count = 0;
  645. coef_list[list_end] = 4; mode_list[list_end++] = 0;
  646. coef_list[list_end] = 24; mode_list[list_end++] = 0;
  647. coef_list[list_end] = 44; mode_list[list_end++] = 0;
  648. coef_list[list_end] = 0; mode_list[list_end++] = 2;
  649. for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
  650. for (i = 0; i < nz_coeff_count; i++) {
  651. if (!get_bits1(gb))
  652. continue;
  653. if (block[nz_coeff[i]] < 0)
  654. block[nz_coeff[i]] -= mask;
  655. else
  656. block[nz_coeff[i]] += mask;
  657. masks_count--;
  658. if (masks_count < 0)
  659. return 0;
  660. }
  661. list_pos = list_start;
  662. while (list_pos < list_end) {
  663. if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
  664. list_pos++;
  665. continue;
  666. }
  667. ccoef = coef_list[list_pos];
  668. mode = mode_list[list_pos];
  669. switch (mode) {
  670. case 0:
  671. coef_list[list_pos] = ccoef + 4;
  672. mode_list[list_pos] = 1;
  673. case 2:
  674. if (mode == 2) {
  675. coef_list[list_pos] = 0;
  676. mode_list[list_pos++] = 0;
  677. }
  678. for (i = 0; i < 4; i++, ccoef++) {
  679. if (get_bits1(gb)) {
  680. coef_list[--list_start] = ccoef;
  681. mode_list[ list_start] = 3;
  682. } else {
  683. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  684. sign = -get_bits1(gb);
  685. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  686. masks_count--;
  687. if (masks_count < 0)
  688. return 0;
  689. }
  690. }
  691. break;
  692. case 1:
  693. mode_list[list_pos] = 2;
  694. for (i = 0; i < 3; i++) {
  695. ccoef += 4;
  696. coef_list[list_end] = ccoef;
  697. mode_list[list_end++] = 2;
  698. }
  699. break;
  700. case 3:
  701. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  702. sign = -get_bits1(gb);
  703. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  704. coef_list[list_pos] = 0;
  705. mode_list[list_pos++] = 0;
  706. masks_count--;
  707. if (masks_count < 0)
  708. return 0;
  709. break;
  710. }
  711. }
  712. }
  713. return 0;
  714. }
  715. /**
  716. * Copy 8x8 block from source to destination, where src and dst may be overlapped
  717. */
  718. static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
  719. {
  720. uint8_t tmp[64];
  721. int i;
  722. for (i = 0; i < 8; i++)
  723. memcpy(tmp + i*8, src + i*stride, 8);
  724. for (i = 0; i < 8; i++)
  725. memcpy(dst + i*stride, tmp + i*8, 8);
  726. }
  727. static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
  728. int plane_idx, int is_key, int is_chroma)
  729. {
  730. int blk, ret;
  731. int i, j, bx, by;
  732. uint8_t *dst, *ref, *ref_start, *ref_end;
  733. int v, col[2];
  734. const uint8_t *scan;
  735. int xoff, yoff;
  736. LOCAL_ALIGNED_16(int16_t, block, [64]);
  737. LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
  738. int coordmap[64];
  739. int ybias = is_key ? -15 : 0;
  740. int qp;
  741. const int stride = frame->linesize[plane_idx];
  742. int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
  743. int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
  744. binkb_init_bundles(c);
  745. ref_start = frame->data[plane_idx];
  746. ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
  747. for (i = 0; i < 64; i++)
  748. coordmap[i] = (i & 7) + (i >> 3) * stride;
  749. for (by = 0; by < bh; by++) {
  750. for (i = 0; i < BINKB_NB_SRC; i++) {
  751. if ((ret = binkb_read_bundle(c, gb, i)) < 0)
  752. return ret;
  753. }
  754. dst = frame->data[plane_idx] + 8*by*stride;
  755. for (bx = 0; bx < bw; bx++, dst += 8) {
  756. blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
  757. switch (blk) {
  758. case 0:
  759. break;
  760. case 1:
  761. scan = bink_patterns[get_bits(gb, 4)];
  762. i = 0;
  763. do {
  764. int mode, run;
  765. mode = get_bits1(gb);
  766. run = get_bits(gb, binkb_runbits[i]) + 1;
  767. i += run;
  768. if (i > 64) {
  769. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  770. return AVERROR_INVALIDDATA;
  771. }
  772. if (mode) {
  773. v = binkb_get_value(c, BINKB_SRC_COLORS);
  774. for (j = 0; j < run; j++)
  775. dst[coordmap[*scan++]] = v;
  776. } else {
  777. for (j = 0; j < run; j++)
  778. dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
  779. }
  780. } while (i < 63);
  781. if (i == 63)
  782. dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
  783. break;
  784. case 2:
  785. memset(dctblock, 0, sizeof(*dctblock) * 64);
  786. dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
  787. qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
  788. read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
  789. c->bdsp.idct_put(dst, stride, dctblock);
  790. break;
  791. case 3:
  792. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  793. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  794. ref = dst + xoff + yoff * stride;
  795. if (ref < ref_start || ref + 8*stride > ref_end) {
  796. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  797. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  798. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  799. } else {
  800. put_pixels8x8_overlapped(dst, ref, stride);
  801. }
  802. c->dsp.clear_block(block);
  803. v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
  804. read_residue(gb, block, v);
  805. c->dsp.add_pixels8(dst, block, stride);
  806. break;
  807. case 4:
  808. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  809. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  810. ref = dst + xoff + yoff * stride;
  811. if (ref < ref_start || ref + 8 * stride > ref_end) {
  812. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  813. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  814. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  815. } else {
  816. put_pixels8x8_overlapped(dst, ref, stride);
  817. }
  818. memset(dctblock, 0, sizeof(*dctblock) * 64);
  819. dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
  820. qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
  821. read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
  822. c->bdsp.idct_add(dst, stride, dctblock);
  823. break;
  824. case 5:
  825. v = binkb_get_value(c, BINKB_SRC_COLORS);
  826. c->dsp.fill_block_tab[1](dst, v, stride, 8);
  827. break;
  828. case 6:
  829. for (i = 0; i < 2; i++)
  830. col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
  831. for (i = 0; i < 8; i++) {
  832. v = binkb_get_value(c, BINKB_SRC_PATTERN);
  833. for (j = 0; j < 8; j++, v >>= 1)
  834. dst[i*stride + j] = col[v & 1];
  835. }
  836. break;
  837. case 7:
  838. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  839. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  840. ref = dst + xoff + yoff * stride;
  841. if (ref < ref_start || ref + 8 * stride > ref_end) {
  842. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  843. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  844. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  845. } else {
  846. put_pixels8x8_overlapped(dst, ref, stride);
  847. }
  848. break;
  849. case 8:
  850. for (i = 0; i < 8; i++)
  851. memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
  852. c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
  853. break;
  854. default:
  855. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  856. return AVERROR_INVALIDDATA;
  857. }
  858. }
  859. }
  860. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  861. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  862. return 0;
  863. }
  864. static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
  865. int plane_idx, int is_chroma)
  866. {
  867. int blk, ret;
  868. int i, j, bx, by;
  869. uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
  870. int v, col[2];
  871. const uint8_t *scan;
  872. int xoff, yoff;
  873. LOCAL_ALIGNED_16(int16_t, block, [64]);
  874. LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
  875. LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
  876. int coordmap[64];
  877. const int stride = frame->linesize[plane_idx];
  878. int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
  879. int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
  880. int width = c->avctx->width >> is_chroma;
  881. init_lengths(c, FFMAX(width, 8), bw);
  882. for (i = 0; i < BINK_NB_SRC; i++)
  883. read_bundle(gb, c, i);
  884. ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
  885. : frame->data[plane_idx];
  886. ref_end = ref_start
  887. + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
  888. for (i = 0; i < 64; i++)
  889. coordmap[i] = (i & 7) + (i >> 3) * stride;
  890. for (by = 0; by < bh; by++) {
  891. if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
  892. return ret;
  893. if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
  894. return ret;
  895. if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
  896. return ret;
  897. if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
  898. return ret;
  899. if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
  900. return ret;
  901. if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
  902. return ret;
  903. if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
  904. return ret;
  905. if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
  906. return ret;
  907. if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
  908. return ret;
  909. if (by == bh)
  910. break;
  911. dst = frame->data[plane_idx] + 8*by*stride;
  912. prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
  913. : frame->data[plane_idx]) + 8*by*stride;
  914. for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
  915. blk = get_value(c, BINK_SRC_BLOCK_TYPES);
  916. // 16x16 block type on odd line means part of the already decoded block, so skip it
  917. if ((by & 1) && blk == SCALED_BLOCK) {
  918. bx++;
  919. dst += 8;
  920. prev += 8;
  921. continue;
  922. }
  923. switch (blk) {
  924. case SKIP_BLOCK:
  925. c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
  926. break;
  927. case SCALED_BLOCK:
  928. blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
  929. switch (blk) {
  930. case RUN_BLOCK:
  931. scan = bink_patterns[get_bits(gb, 4)];
  932. i = 0;
  933. do {
  934. int run = get_value(c, BINK_SRC_RUN) + 1;
  935. i += run;
  936. if (i > 64) {
  937. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  938. return AVERROR_INVALIDDATA;
  939. }
  940. if (get_bits1(gb)) {
  941. v = get_value(c, BINK_SRC_COLORS);
  942. for (j = 0; j < run; j++)
  943. ublock[*scan++] = v;
  944. } else {
  945. for (j = 0; j < run; j++)
  946. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  947. }
  948. } while (i < 63);
  949. if (i == 63)
  950. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  951. break;
  952. case INTRA_BLOCK:
  953. memset(dctblock, 0, sizeof(*dctblock) * 64);
  954. dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
  955. read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
  956. c->bdsp.idct_put(ublock, 8, dctblock);
  957. break;
  958. case FILL_BLOCK:
  959. v = get_value(c, BINK_SRC_COLORS);
  960. c->dsp.fill_block_tab[0](dst, v, stride, 16);
  961. break;
  962. case PATTERN_BLOCK:
  963. for (i = 0; i < 2; i++)
  964. col[i] = get_value(c, BINK_SRC_COLORS);
  965. for (j = 0; j < 8; j++) {
  966. v = get_value(c, BINK_SRC_PATTERN);
  967. for (i = 0; i < 8; i++, v >>= 1)
  968. ublock[i + j*8] = col[v & 1];
  969. }
  970. break;
  971. case RAW_BLOCK:
  972. for (j = 0; j < 8; j++)
  973. for (i = 0; i < 8; i++)
  974. ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
  975. break;
  976. default:
  977. av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
  978. return AVERROR_INVALIDDATA;
  979. }
  980. if (blk != FILL_BLOCK)
  981. c->bdsp.scale_block(ublock, dst, stride);
  982. bx++;
  983. dst += 8;
  984. prev += 8;
  985. break;
  986. case MOTION_BLOCK:
  987. xoff = get_value(c, BINK_SRC_X_OFF);
  988. yoff = get_value(c, BINK_SRC_Y_OFF);
  989. ref = prev + xoff + yoff * stride;
  990. if (ref < ref_start || ref > ref_end) {
  991. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  992. bx*8 + xoff, by*8 + yoff);
  993. return AVERROR_INVALIDDATA;
  994. }
  995. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  996. break;
  997. case RUN_BLOCK:
  998. scan = bink_patterns[get_bits(gb, 4)];
  999. i = 0;
  1000. do {
  1001. int run = get_value(c, BINK_SRC_RUN) + 1;
  1002. i += run;
  1003. if (i > 64) {
  1004. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  1005. return AVERROR_INVALIDDATA;
  1006. }
  1007. if (get_bits1(gb)) {
  1008. v = get_value(c, BINK_SRC_COLORS);
  1009. for (j = 0; j < run; j++)
  1010. dst[coordmap[*scan++]] = v;
  1011. } else {
  1012. for (j = 0; j < run; j++)
  1013. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  1014. }
  1015. } while (i < 63);
  1016. if (i == 63)
  1017. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  1018. break;
  1019. case RESIDUE_BLOCK:
  1020. xoff = get_value(c, BINK_SRC_X_OFF);
  1021. yoff = get_value(c, BINK_SRC_Y_OFF);
  1022. ref = prev + xoff + yoff * stride;
  1023. if (ref < ref_start || ref > ref_end) {
  1024. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  1025. bx*8 + xoff, by*8 + yoff);
  1026. return AVERROR_INVALIDDATA;
  1027. }
  1028. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  1029. c->dsp.clear_block(block);
  1030. v = get_bits(gb, 7);
  1031. read_residue(gb, block, v);
  1032. c->dsp.add_pixels8(dst, block, stride);
  1033. break;
  1034. case INTRA_BLOCK:
  1035. memset(dctblock, 0, sizeof(*dctblock) * 64);
  1036. dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
  1037. read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
  1038. c->bdsp.idct_put(dst, stride, dctblock);
  1039. break;
  1040. case FILL_BLOCK:
  1041. v = get_value(c, BINK_SRC_COLORS);
  1042. c->dsp.fill_block_tab[1](dst, v, stride, 8);
  1043. break;
  1044. case INTER_BLOCK:
  1045. xoff = get_value(c, BINK_SRC_X_OFF);
  1046. yoff = get_value(c, BINK_SRC_Y_OFF);
  1047. ref = prev + xoff + yoff * stride;
  1048. if (ref < ref_start || ref > ref_end) {
  1049. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  1050. bx*8 + xoff, by*8 + yoff);
  1051. return -1;
  1052. }
  1053. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  1054. memset(dctblock, 0, sizeof(*dctblock) * 64);
  1055. dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
  1056. read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
  1057. c->bdsp.idct_add(dst, stride, dctblock);
  1058. break;
  1059. case PATTERN_BLOCK:
  1060. for (i = 0; i < 2; i++)
  1061. col[i] = get_value(c, BINK_SRC_COLORS);
  1062. for (i = 0; i < 8; i++) {
  1063. v = get_value(c, BINK_SRC_PATTERN);
  1064. for (j = 0; j < 8; j++, v >>= 1)
  1065. dst[i*stride + j] = col[v & 1];
  1066. }
  1067. break;
  1068. case RAW_BLOCK:
  1069. for (i = 0; i < 8; i++)
  1070. memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
  1071. c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
  1072. break;
  1073. default:
  1074. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  1075. return AVERROR_INVALIDDATA;
  1076. }
  1077. }
  1078. }
  1079. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  1080. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  1081. return 0;
  1082. }
  1083. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
  1084. {
  1085. BinkContext * const c = avctx->priv_data;
  1086. AVFrame *frame = data;
  1087. GetBitContext gb;
  1088. int plane, plane_idx, ret;
  1089. int bits_count = pkt->size << 3;
  1090. if (c->version > 'b') {
  1091. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
  1092. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1093. return ret;
  1094. }
  1095. } else {
  1096. if ((ret = ff_reget_buffer(avctx, c->last)) < 0) {
  1097. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  1098. return ret;
  1099. }
  1100. if ((ret = av_frame_ref(frame, c->last)) < 0)
  1101. return ret;
  1102. }
  1103. init_get_bits(&gb, pkt->data, bits_count);
  1104. if (c->has_alpha) {
  1105. if (c->version >= 'i')
  1106. skip_bits_long(&gb, 32);
  1107. if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
  1108. return ret;
  1109. }
  1110. if (c->version >= 'i')
  1111. skip_bits_long(&gb, 32);
  1112. for (plane = 0; plane < 3; plane++) {
  1113. plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
  1114. if (c->version > 'b') {
  1115. if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
  1116. return ret;
  1117. } else {
  1118. if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
  1119. !avctx->frame_number, !!plane)) < 0)
  1120. return ret;
  1121. }
  1122. if (get_bits_count(&gb) >= bits_count)
  1123. break;
  1124. }
  1125. emms_c();
  1126. if (c->version > 'b') {
  1127. av_frame_unref(c->last);
  1128. if ((ret = av_frame_ref(c->last, frame)) < 0)
  1129. return ret;
  1130. }
  1131. *got_frame = 1;
  1132. /* always report that the buffer was completely consumed */
  1133. return pkt->size;
  1134. }
  1135. /**
  1136. * Caclulate quantization tables for version b
  1137. */
  1138. static av_cold void binkb_calc_quant(void)
  1139. {
  1140. uint8_t inv_bink_scan[64];
  1141. static const int s[64]={
  1142. 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
  1143. 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
  1144. 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
  1145. 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
  1146. 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
  1147. 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
  1148. 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
  1149. 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
  1150. };
  1151. int i, j;
  1152. #define C (1LL<<30)
  1153. for (i = 0; i < 64; i++)
  1154. inv_bink_scan[bink_scan[i]] = i;
  1155. for (j = 0; j < 16; j++) {
  1156. for (i = 0; i < 64; i++) {
  1157. int k = inv_bink_scan[i];
  1158. binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
  1159. binkb_num[j]/(binkb_den[j] * (C>>12));
  1160. binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
  1161. binkb_num[j]/(binkb_den[j] * (C>>12));
  1162. }
  1163. }
  1164. }
  1165. static av_cold int decode_init(AVCodecContext *avctx)
  1166. {
  1167. BinkContext * const c = avctx->priv_data;
  1168. static VLC_TYPE table[16 * 128][2];
  1169. static int binkb_initialised = 0;
  1170. int i, ret;
  1171. int flags;
  1172. c->version = avctx->codec_tag >> 24;
  1173. if (avctx->extradata_size < 4) {
  1174. av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
  1175. return AVERROR_INVALIDDATA;
  1176. }
  1177. flags = AV_RL32(avctx->extradata);
  1178. c->has_alpha = flags & BINK_FLAG_ALPHA;
  1179. c->swap_planes = c->version >= 'h';
  1180. if (!bink_trees[15].table) {
  1181. for (i = 0; i < 16; i++) {
  1182. const int maxbits = bink_tree_lens[i][15];
  1183. bink_trees[i].table = table + i*128;
  1184. bink_trees[i].table_allocated = 1 << maxbits;
  1185. init_vlc(&bink_trees[i], maxbits, 16,
  1186. bink_tree_lens[i], 1, 1,
  1187. bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  1188. }
  1189. }
  1190. c->avctx = avctx;
  1191. c->last = av_frame_alloc();
  1192. if (!c->last)
  1193. return AVERROR(ENOMEM);
  1194. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  1195. return ret;
  1196. avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
  1197. ff_dsputil_init(&c->dsp, avctx);
  1198. ff_binkdsp_init(&c->bdsp);
  1199. if ((ret = init_bundles(c)) < 0) {
  1200. free_bundles(c);
  1201. return ret;
  1202. }
  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. };