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.

1431 lines
48KB

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