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