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
44KB

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