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