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.

1352 lines
45KB

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