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.

1395 lines
47KB

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