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