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.

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