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.

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