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.

1013 lines
32KB

  1. /*
  2. * Bink video decoder
  3. * Copyright (c) 2009 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "dsputil.h"
  23. #include "binkdata.h"
  24. #include "mathops.h"
  25. #define ALT_BITSTREAM_READER_LE
  26. #include "get_bits.h"
  27. #define BINK_FLAG_ALPHA 0x00100000
  28. #define BINK_FLAG_GRAY 0x00020000
  29. static VLC bink_trees[16];
  30. /**
  31. * IDs for different data types used in Bink video codec
  32. */
  33. enum Sources {
  34. BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
  35. BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
  36. BINK_SRC_COLORS, ///< pixel values used for different block types
  37. BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
  38. BINK_SRC_X_OFF, ///< X components of motion value
  39. BINK_SRC_Y_OFF, ///< Y components of motion value
  40. BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
  41. BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
  42. BINK_SRC_RUN, ///< run lengths for special fill block
  43. BINK_NB_SRC
  44. };
  45. /**
  46. * data needed to decode 4-bit Huffman-coded value
  47. */
  48. typedef struct Tree {
  49. int vlc_num; ///< tree number (in bink_trees[])
  50. uint8_t syms[16]; ///< leaf value to symbol mapping
  51. } Tree;
  52. #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
  53. bink_trees[(tree).vlc_num].bits, 1)]
  54. /**
  55. * data structure used for decoding single Bink data type
  56. */
  57. typedef struct Bundle {
  58. int len; ///< length of number of entries to decode (in bits)
  59. Tree tree; ///< Huffman tree-related data
  60. uint8_t *data; ///< buffer for decoded symbols
  61. uint8_t *data_end; ///< buffer end
  62. uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
  63. uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
  64. } Bundle;
  65. /*
  66. * Decoder context
  67. */
  68. typedef struct BinkContext {
  69. AVCodecContext *avctx;
  70. DSPContext dsp;
  71. AVFrame pic, last;
  72. int version; ///< internal Bink file version
  73. int has_alpha;
  74. int swap_planes;
  75. ScanTable scantable; ///< permutated scantable for DCT coeffs decoding
  76. Bundle bundle[BINK_NB_SRC]; ///< bundles for decoding all data types
  77. Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
  78. int col_lastval; ///< value of last decoded high nibble in "colours" data type
  79. } BinkContext;
  80. /**
  81. * Bink video block types
  82. */
  83. enum BlockTypes {
  84. SKIP_BLOCK = 0, ///< skipped block
  85. SCALED_BLOCK, ///< block has size 16x16
  86. MOTION_BLOCK, ///< block is copied from previous frame with some offset
  87. RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
  88. RESIDUE_BLOCK, ///< motion block with some difference added
  89. INTRA_BLOCK, ///< intra DCT block
  90. FILL_BLOCK, ///< block is filled with single colour
  91. INTER_BLOCK, ///< motion block with DCT applied to the difference
  92. PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
  93. RAW_BLOCK, ///< uncoded 8x8 block
  94. };
  95. /**
  96. * Initialize length length in all bundles.
  97. *
  98. * @param c decoder context
  99. * @param width plane width
  100. * @param bw plane width in 8x8 blocks
  101. */
  102. static void init_lengths(BinkContext *c, int width, int bw)
  103. {
  104. c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
  105. c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
  106. c->bundle[BINK_SRC_COLORS].len = av_log2((width >> 3)*64 + 511) + 1;
  107. c->bundle[BINK_SRC_INTRA_DC].len =
  108. c->bundle[BINK_SRC_INTER_DC].len =
  109. c->bundle[BINK_SRC_X_OFF].len =
  110. c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
  111. c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
  112. c->bundle[BINK_SRC_RUN].len = av_log2((width >> 3)*48 + 511) + 1;
  113. }
  114. /**
  115. * Allocate memory for bundles.
  116. *
  117. * @param c decoder context
  118. */
  119. static av_cold void init_bundles(BinkContext *c)
  120. {
  121. int bw, bh, blocks;
  122. int i;
  123. bw = (c->avctx->width + 7) >> 3;
  124. bh = (c->avctx->height + 7) >> 3;
  125. blocks = bw * bh;
  126. for (i = 0; i < BINK_NB_SRC; i++) {
  127. c->bundle[i].data = av_malloc(blocks * 64);
  128. c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
  129. }
  130. }
  131. /**
  132. * Free memory used by bundles.
  133. *
  134. * @param c decoder context
  135. */
  136. static av_cold void free_bundles(BinkContext *c)
  137. {
  138. int i;
  139. for (i = 0; i < BINK_NB_SRC; i++)
  140. av_freep(&c->bundle[i].data);
  141. }
  142. /**
  143. * Merge two consequent lists of equal size depending on bits read.
  144. *
  145. * @param gb context for reading bits
  146. * @param dst buffer where merged list will be written to
  147. * @param src pointer to the head of the first list (the second lists starts at src+size)
  148. * @param size input lists size
  149. */
  150. static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
  151. {
  152. uint8_t *src2 = src + size;
  153. int size2 = size;
  154. do {
  155. if (!get_bits1(gb)) {
  156. *dst++ = *src++;
  157. size--;
  158. } else {
  159. *dst++ = *src2++;
  160. size2--;
  161. }
  162. } while (size && size2);
  163. while (size--)
  164. *dst++ = *src++;
  165. while (size2--)
  166. *dst++ = *src2++;
  167. }
  168. /**
  169. * Read information about Huffman tree used to decode data.
  170. *
  171. * @param gb context for reading bits
  172. * @param tree pointer for storing tree data
  173. */
  174. static void read_tree(GetBitContext *gb, Tree *tree)
  175. {
  176. uint8_t tmp1[16], tmp2[16], *in = tmp1, *out = tmp2;
  177. int i, t, len;
  178. tree->vlc_num = get_bits(gb, 4);
  179. if (!tree->vlc_num) {
  180. for (i = 0; i < 16; i++)
  181. tree->syms[i] = i;
  182. return;
  183. }
  184. if (get_bits1(gb)) {
  185. len = get_bits(gb, 3);
  186. memset(tmp1, 0, sizeof(tmp1));
  187. for (i = 0; i <= len; i++) {
  188. tree->syms[i] = get_bits(gb, 4);
  189. tmp1[tree->syms[i]] = 1;
  190. }
  191. for (i = 0; i < 16; i++)
  192. if (!tmp1[i])
  193. tree->syms[++len] = i;
  194. } else {
  195. len = get_bits(gb, 2);
  196. for (i = 0; i < 16; i++)
  197. in[i] = i;
  198. for (i = 0; i <= len; i++) {
  199. int size = 1 << i;
  200. for (t = 0; t < 16; t += size << 1)
  201. merge(gb, out + t, in + t, size);
  202. FFSWAP(uint8_t*, in, out);
  203. }
  204. memcpy(tree->syms, in, 16);
  205. }
  206. }
  207. /**
  208. * Prepare bundle for decoding data.
  209. *
  210. * @param gb context for reading bits
  211. * @param c decoder context
  212. * @param bundle_num number of the bundle to initialize
  213. */
  214. static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
  215. {
  216. int i;
  217. if (bundle_num == BINK_SRC_COLORS) {
  218. for (i = 0; i < 16; i++)
  219. read_tree(gb, &c->col_high[i]);
  220. c->col_lastval = 0;
  221. }
  222. if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
  223. read_tree(gb, &c->bundle[bundle_num].tree);
  224. c->bundle[bundle_num].cur_dec =
  225. c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
  226. }
  227. /**
  228. * common check before starting decoding bundle data
  229. *
  230. * @param gb context for reading bits
  231. * @param b bundle
  232. * @param t variable where number of elements to decode will be stored
  233. */
  234. #define CHECK_READ_VAL(gb, b, t) \
  235. if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
  236. return 0; \
  237. t = get_bits(gb, b->len); \
  238. if (!t) { \
  239. b->cur_dec = NULL; \
  240. return 0; \
  241. } \
  242. static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  243. {
  244. int t, v;
  245. const uint8_t *dec_end;
  246. CHECK_READ_VAL(gb, b, t);
  247. dec_end = b->cur_dec + t;
  248. if (dec_end > b->data_end) {
  249. av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
  250. return -1;
  251. }
  252. if (get_bits1(gb)) {
  253. v = get_bits(gb, 4);
  254. memset(b->cur_dec, v, t);
  255. b->cur_dec += t;
  256. } else {
  257. while (b->cur_dec < dec_end)
  258. *b->cur_dec++ = GET_HUFF(gb, b->tree);
  259. }
  260. return 0;
  261. }
  262. static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  263. {
  264. int t, sign, v;
  265. const uint8_t *dec_end;
  266. CHECK_READ_VAL(gb, b, t);
  267. dec_end = b->cur_dec + t;
  268. if (dec_end > b->data_end) {
  269. av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
  270. return -1;
  271. }
  272. if (get_bits1(gb)) {
  273. v = get_bits(gb, 4);
  274. if (v) {
  275. sign = -get_bits1(gb);
  276. v = (v ^ sign) - sign;
  277. }
  278. memset(b->cur_dec, v, t);
  279. b->cur_dec += t;
  280. } else {
  281. do {
  282. v = GET_HUFF(gb, b->tree);
  283. if (v) {
  284. sign = -get_bits1(gb);
  285. v = (v ^ sign) - sign;
  286. }
  287. *b->cur_dec++ = v;
  288. } while (b->cur_dec < dec_end);
  289. }
  290. return 0;
  291. }
  292. const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
  293. static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  294. {
  295. int t, v;
  296. int last = 0;
  297. const uint8_t *dec_end;
  298. CHECK_READ_VAL(gb, b, t);
  299. dec_end = b->cur_dec + t;
  300. if (dec_end > b->data_end) {
  301. av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
  302. return -1;
  303. }
  304. if (get_bits1(gb)) {
  305. v = get_bits(gb, 4);
  306. memset(b->cur_dec, v, t);
  307. b->cur_dec += t;
  308. } else {
  309. do {
  310. v = GET_HUFF(gb, b->tree);
  311. if (v < 12) {
  312. last = v;
  313. *b->cur_dec++ = v;
  314. } else {
  315. int run = bink_rlelens[v - 12];
  316. memset(b->cur_dec, last, run);
  317. b->cur_dec += run;
  318. }
  319. } while (b->cur_dec < dec_end);
  320. }
  321. return 0;
  322. }
  323. static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  324. {
  325. int t, v;
  326. const uint8_t *dec_end;
  327. CHECK_READ_VAL(gb, b, t);
  328. dec_end = b->cur_dec + t;
  329. if (dec_end > b->data_end) {
  330. av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
  331. return -1;
  332. }
  333. while (b->cur_dec < dec_end) {
  334. v = GET_HUFF(gb, b->tree);
  335. v |= GET_HUFF(gb, b->tree) << 4;
  336. *b->cur_dec++ = v;
  337. }
  338. return 0;
  339. }
  340. static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
  341. {
  342. int t, sign, v;
  343. const uint8_t *dec_end;
  344. CHECK_READ_VAL(gb, b, t);
  345. dec_end = b->cur_dec + t;
  346. if (dec_end > b->data_end) {
  347. av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
  348. return -1;
  349. }
  350. if (get_bits1(gb)) {
  351. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  352. v = GET_HUFF(gb, b->tree);
  353. v = (c->col_lastval << 4) | v;
  354. if (c->version < 'i') {
  355. sign = ((int8_t) v) >> 7;
  356. v = ((v & 0x7F) ^ sign) - sign;
  357. v += 0x80;
  358. }
  359. memset(b->cur_dec, v, t);
  360. b->cur_dec += t;
  361. } else {
  362. while (b->cur_dec < dec_end) {
  363. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  364. v = GET_HUFF(gb, b->tree);
  365. v = (c->col_lastval << 4) | v;
  366. if (c->version < 'i') {
  367. sign = ((int8_t) v) >> 7;
  368. v = ((v & 0x7F) ^ sign) - sign;
  369. v += 0x80;
  370. }
  371. *b->cur_dec++ = v;
  372. }
  373. }
  374. return 0;
  375. }
  376. /** number of bits used to store first DC value in bundle */
  377. #define DC_START_BITS 11
  378. static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
  379. int start_bits, int has_sign)
  380. {
  381. int i, j, len, len2, bsize, sign, v, v2;
  382. int16_t *dst = (int16_t*)b->cur_dec;
  383. CHECK_READ_VAL(gb, b, len);
  384. v = get_bits(gb, start_bits - has_sign);
  385. if (v && has_sign) {
  386. sign = -get_bits1(gb);
  387. v = (v ^ sign) - sign;
  388. }
  389. *dst++ = v;
  390. len--;
  391. for (i = 0; i < len; i += 8) {
  392. len2 = FFMIN(len - i, 8);
  393. bsize = get_bits(gb, 4);
  394. if (bsize) {
  395. for (j = 0; j < len2; j++) {
  396. v2 = get_bits(gb, bsize);
  397. if (v2) {
  398. sign = -get_bits1(gb);
  399. v2 = (v2 ^ sign) - sign;
  400. }
  401. v += v2;
  402. *dst++ = v;
  403. if (v < -32768 || v > 32767) {
  404. av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
  405. return -1;
  406. }
  407. }
  408. } else {
  409. for (j = 0; j < len2; j++)
  410. *dst++ = v;
  411. }
  412. }
  413. b->cur_dec = (uint8_t*)dst;
  414. return 0;
  415. }
  416. /**
  417. * Retrieve next value from bundle.
  418. *
  419. * @param c decoder context
  420. * @param bundle bundle number
  421. */
  422. static inline int get_value(BinkContext *c, int bundle)
  423. {
  424. int16_t ret;
  425. if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
  426. return *c->bundle[bundle].cur_ptr++;
  427. if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
  428. return (int8_t)*c->bundle[bundle].cur_ptr++;
  429. ret = *(int16_t*)c->bundle[bundle].cur_ptr;
  430. c->bundle[bundle].cur_ptr += 2;
  431. return ret;
  432. }
  433. /**
  434. * Read 8x8 block of DCT coefficients.
  435. *
  436. * @param gb context for reading bits
  437. * @param block place for storing coefficients
  438. * @param scan scan order table
  439. * @param is_intra tells what set of quantizer matrices to use
  440. * @return 0 for success, negative value in other cases
  441. */
  442. static int read_dct_coeffs(GetBitContext *gb, DCTELEM block[64], const uint8_t *scan,
  443. int is_intra)
  444. {
  445. int coef_list[128];
  446. int mode_list[128];
  447. int i, t, mask, bits, ccoef, mode, sign;
  448. int list_start = 64, list_end = 64, list_pos;
  449. int coef_count = 0;
  450. int coef_idx[64];
  451. int quant_idx;
  452. const uint32_t *quant;
  453. coef_list[list_end] = 4; mode_list[list_end++] = 0;
  454. coef_list[list_end] = 24; mode_list[list_end++] = 0;
  455. coef_list[list_end] = 44; mode_list[list_end++] = 0;
  456. coef_list[list_end] = 1; mode_list[list_end++] = 3;
  457. coef_list[list_end] = 2; mode_list[list_end++] = 3;
  458. coef_list[list_end] = 3; mode_list[list_end++] = 3;
  459. bits = get_bits(gb, 4) - 1;
  460. for (mask = 1 << bits; bits >= 0; mask >>= 1, bits--) {
  461. list_pos = list_start;
  462. while (list_pos < list_end) {
  463. if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
  464. list_pos++;
  465. continue;
  466. }
  467. ccoef = coef_list[list_pos];
  468. mode = mode_list[list_pos];
  469. switch (mode) {
  470. case 0:
  471. coef_list[list_pos] = ccoef + 4;
  472. mode_list[list_pos] = 1;
  473. case 2:
  474. if (mode == 2) {
  475. coef_list[list_pos] = 0;
  476. mode_list[list_pos++] = 0;
  477. }
  478. for (i = 0; i < 4; i++, ccoef++) {
  479. if (get_bits1(gb)) {
  480. coef_list[--list_start] = ccoef;
  481. mode_list[ list_start] = 3;
  482. } else {
  483. int t;
  484. if (!bits) {
  485. t = 1 - (get_bits1(gb) << 1);
  486. } else {
  487. t = get_bits(gb, bits) | mask;
  488. sign = -get_bits1(gb);
  489. t = (t ^ sign) - sign;
  490. }
  491. block[scan[ccoef]] = t;
  492. coef_idx[coef_count++] = ccoef;
  493. }
  494. }
  495. break;
  496. case 1:
  497. mode_list[list_pos] = 2;
  498. for (i = 0; i < 3; i++) {
  499. ccoef += 4;
  500. coef_list[list_end] = ccoef;
  501. mode_list[list_end++] = 2;
  502. }
  503. break;
  504. case 3:
  505. if (!bits) {
  506. t = 1 - (get_bits1(gb) << 1);
  507. } else {
  508. t = get_bits(gb, bits) | mask;
  509. sign = -get_bits1(gb);
  510. t = (t ^ sign) - sign;
  511. }
  512. block[scan[ccoef]] = t;
  513. coef_idx[coef_count++] = ccoef;
  514. coef_list[list_pos] = 0;
  515. mode_list[list_pos++] = 0;
  516. break;
  517. }
  518. }
  519. }
  520. quant_idx = get_bits(gb, 4);
  521. quant = is_intra ? bink_intra_quant[quant_idx]
  522. : bink_inter_quant[quant_idx];
  523. block[0] = (block[0] * quant[0]) >> 11;
  524. for (i = 0; i < coef_count; i++) {
  525. int idx = coef_idx[i];
  526. block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
  527. }
  528. return 0;
  529. }
  530. /**
  531. * Read 8x8 block with residue after motion compensation.
  532. *
  533. * @param gb context for reading bits
  534. * @param block place to store read data
  535. * @param masks_count number of masks to decode
  536. * @return 0 on success, negative value in other cases
  537. */
  538. static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
  539. {
  540. int coef_list[128];
  541. int mode_list[128];
  542. int i, sign, mask, ccoef, mode;
  543. int list_start = 64, list_end = 64, list_pos;
  544. int nz_coeff[64];
  545. int nz_coeff_count = 0;
  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] = 0; mode_list[list_end++] = 2;
  550. for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
  551. for (i = 0; i < nz_coeff_count; i++) {
  552. if (!get_bits1(gb))
  553. continue;
  554. if (block[nz_coeff[i]] < 0)
  555. block[nz_coeff[i]] -= mask;
  556. else
  557. block[nz_coeff[i]] += mask;
  558. masks_count--;
  559. if (masks_count < 0)
  560. return 0;
  561. }
  562. list_pos = list_start;
  563. while (list_pos < list_end) {
  564. if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
  565. list_pos++;
  566. continue;
  567. }
  568. ccoef = coef_list[list_pos];
  569. mode = mode_list[list_pos];
  570. switch (mode) {
  571. case 0:
  572. coef_list[list_pos] = ccoef + 4;
  573. mode_list[list_pos] = 1;
  574. case 2:
  575. if (mode == 2) {
  576. coef_list[list_pos] = 0;
  577. mode_list[list_pos++] = 0;
  578. }
  579. for (i = 0; i < 4; i++, ccoef++) {
  580. if (get_bits1(gb)) {
  581. coef_list[--list_start] = ccoef;
  582. mode_list[ list_start] = 3;
  583. } else {
  584. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  585. sign = -get_bits1(gb);
  586. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  587. masks_count--;
  588. if (masks_count < 0)
  589. return 0;
  590. }
  591. }
  592. break;
  593. case 1:
  594. mode_list[list_pos] = 2;
  595. for (i = 0; i < 3; i++) {
  596. ccoef += 4;
  597. coef_list[list_end] = ccoef;
  598. mode_list[list_end++] = 2;
  599. }
  600. break;
  601. case 3:
  602. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  603. sign = -get_bits1(gb);
  604. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  605. coef_list[list_pos] = 0;
  606. mode_list[list_pos++] = 0;
  607. masks_count--;
  608. if (masks_count < 0)
  609. return 0;
  610. break;
  611. }
  612. }
  613. }
  614. return 0;
  615. }
  616. static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
  617. int is_chroma)
  618. {
  619. int blk;
  620. int i, j, bx, by;
  621. uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
  622. int v, col[2];
  623. const uint8_t *scan;
  624. int xoff, yoff;
  625. LOCAL_ALIGNED_16(DCTELEM, block, [64]);
  626. LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
  627. int coordmap[64];
  628. const int stride = c->pic.linesize[plane_idx];
  629. int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
  630. int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
  631. int width = c->avctx->width >> is_chroma;
  632. init_lengths(c, FFMAX(width, 8), bw);
  633. for (i = 0; i < BINK_NB_SRC; i++)
  634. read_bundle(gb, c, i);
  635. ref_start = c->last.data[plane_idx];
  636. ref_end = c->last.data[plane_idx]
  637. + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
  638. for (i = 0; i < 64; i++)
  639. coordmap[i] = (i & 7) + (i >> 3) * stride;
  640. for (by = 0; by < bh; by++) {
  641. if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
  642. return -1;
  643. if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
  644. return -1;
  645. if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
  646. return -1;
  647. if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
  648. return -1;
  649. if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
  650. return -1;
  651. if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
  652. return -1;
  653. if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
  654. return -1;
  655. if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
  656. return -1;
  657. if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0)
  658. return -1;
  659. if (by == bh)
  660. break;
  661. dst = c->pic.data[plane_idx] + 8*by*stride;
  662. prev = c->last.data[plane_idx] + 8*by*stride;
  663. for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
  664. blk = get_value(c, BINK_SRC_BLOCK_TYPES);
  665. // 16x16 block type on odd line means part of the already decoded block, so skip it
  666. if ((by & 1) && blk == SCALED_BLOCK) {
  667. bx++;
  668. dst += 8;
  669. prev += 8;
  670. continue;
  671. }
  672. switch (blk) {
  673. case SKIP_BLOCK:
  674. c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
  675. break;
  676. case SCALED_BLOCK:
  677. blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
  678. switch (blk) {
  679. case RUN_BLOCK:
  680. scan = bink_patterns[get_bits(gb, 4)];
  681. i = 0;
  682. do {
  683. int run = get_value(c, BINK_SRC_RUN) + 1;
  684. i += run;
  685. if (i > 64) {
  686. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  687. return -1;
  688. }
  689. if (get_bits1(gb)) {
  690. v = get_value(c, BINK_SRC_COLORS);
  691. for (j = 0; j < run; j++)
  692. ublock[*scan++] = v;
  693. } else {
  694. for (j = 0; j < run; j++)
  695. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  696. }
  697. } while (i < 63);
  698. if (i == 63)
  699. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  700. break;
  701. case INTRA_BLOCK:
  702. c->dsp.clear_block(block);
  703. block[0] = get_value(c, BINK_SRC_INTRA_DC);
  704. read_dct_coeffs(gb, block, c->scantable.permutated, 1);
  705. c->dsp.idct(block);
  706. c->dsp.put_pixels_nonclamped(block, ublock, 8);
  707. break;
  708. case FILL_BLOCK:
  709. v = get_value(c, BINK_SRC_COLORS);
  710. c->dsp.fill_block_tab[0](dst, v, stride, 16);
  711. break;
  712. case PATTERN_BLOCK:
  713. for (i = 0; i < 2; i++)
  714. col[i] = get_value(c, BINK_SRC_COLORS);
  715. for (j = 0; j < 8; j++) {
  716. v = get_value(c, BINK_SRC_PATTERN);
  717. for (i = 0; i < 8; i++, v >>= 1)
  718. ublock[i + j*8] = col[v & 1];
  719. }
  720. break;
  721. case RAW_BLOCK:
  722. for (j = 0; j < 8; j++)
  723. for (i = 0; i < 8; i++)
  724. ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
  725. break;
  726. default:
  727. av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
  728. return -1;
  729. }
  730. if (blk != FILL_BLOCK)
  731. c->dsp.scale_block(ublock, dst, stride);
  732. bx++;
  733. dst += 8;
  734. prev += 8;
  735. break;
  736. case MOTION_BLOCK:
  737. xoff = get_value(c, BINK_SRC_X_OFF);
  738. yoff = get_value(c, BINK_SRC_Y_OFF);
  739. ref = prev + xoff + yoff * stride;
  740. if (ref < ref_start || ref > ref_end) {
  741. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  742. bx*8 + xoff, by*8 + yoff);
  743. return -1;
  744. }
  745. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  746. break;
  747. case RUN_BLOCK:
  748. scan = bink_patterns[get_bits(gb, 4)];
  749. i = 0;
  750. do {
  751. int run = get_value(c, BINK_SRC_RUN) + 1;
  752. i += run;
  753. if (i > 64) {
  754. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  755. return -1;
  756. }
  757. if (get_bits1(gb)) {
  758. v = get_value(c, BINK_SRC_COLORS);
  759. for (j = 0; j < run; j++)
  760. dst[coordmap[*scan++]] = v;
  761. } else {
  762. for (j = 0; j < run; j++)
  763. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  764. }
  765. } while (i < 63);
  766. if (i == 63)
  767. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  768. break;
  769. case RESIDUE_BLOCK:
  770. xoff = get_value(c, BINK_SRC_X_OFF);
  771. yoff = get_value(c, BINK_SRC_Y_OFF);
  772. ref = prev + xoff + yoff * stride;
  773. if (ref < ref_start || ref > ref_end) {
  774. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  775. bx*8 + xoff, by*8 + yoff);
  776. return -1;
  777. }
  778. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  779. c->dsp.clear_block(block);
  780. v = get_bits(gb, 7);
  781. read_residue(gb, block, v);
  782. c->dsp.add_pixels8(dst, block, stride);
  783. break;
  784. case INTRA_BLOCK:
  785. c->dsp.clear_block(block);
  786. block[0] = get_value(c, BINK_SRC_INTRA_DC);
  787. read_dct_coeffs(gb, block, c->scantable.permutated, 1);
  788. c->dsp.idct_put(dst, stride, block);
  789. break;
  790. case FILL_BLOCK:
  791. v = get_value(c, BINK_SRC_COLORS);
  792. c->dsp.fill_block_tab[1](dst, v, stride, 8);
  793. break;
  794. case INTER_BLOCK:
  795. xoff = get_value(c, BINK_SRC_X_OFF);
  796. yoff = get_value(c, BINK_SRC_Y_OFF);
  797. ref = prev + xoff + yoff * stride;
  798. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  799. c->dsp.clear_block(block);
  800. block[0] = get_value(c, BINK_SRC_INTER_DC);
  801. read_dct_coeffs(gb, block, c->scantable.permutated, 0);
  802. c->dsp.idct_add(dst, stride, block);
  803. break;
  804. case PATTERN_BLOCK:
  805. for (i = 0; i < 2; i++)
  806. col[i] = get_value(c, BINK_SRC_COLORS);
  807. for (i = 0; i < 8; i++) {
  808. v = get_value(c, BINK_SRC_PATTERN);
  809. for (j = 0; j < 8; j++, v >>= 1)
  810. dst[i*stride + j] = col[v & 1];
  811. }
  812. break;
  813. case RAW_BLOCK:
  814. for (i = 0; i < 8; i++)
  815. memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
  816. c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
  817. break;
  818. default:
  819. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  820. return -1;
  821. }
  822. }
  823. }
  824. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  825. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  826. return 0;
  827. }
  828. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
  829. {
  830. BinkContext * const c = avctx->priv_data;
  831. GetBitContext gb;
  832. int plane, plane_idx;
  833. int bits_count = pkt->size << 3;
  834. if(c->pic.data[0])
  835. avctx->release_buffer(avctx, &c->pic);
  836. if(avctx->get_buffer(avctx, &c->pic) < 0){
  837. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  838. return -1;
  839. }
  840. init_get_bits(&gb, pkt->data, bits_count);
  841. if (c->has_alpha) {
  842. if (c->version >= 'i')
  843. skip_bits_long(&gb, 32);
  844. if (bink_decode_plane(c, &gb, 3, 0) < 0)
  845. return -1;
  846. }
  847. if (c->version >= 'i')
  848. skip_bits_long(&gb, 32);
  849. for (plane = 0; plane < 3; plane++) {
  850. plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
  851. if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
  852. return -1;
  853. if (get_bits_count(&gb) >= bits_count)
  854. break;
  855. }
  856. emms_c();
  857. *data_size = sizeof(AVFrame);
  858. *(AVFrame*)data = c->pic;
  859. FFSWAP(AVFrame, c->pic, c->last);
  860. /* always report that the buffer was completely consumed */
  861. return pkt->size;
  862. }
  863. static av_cold int decode_init(AVCodecContext *avctx)
  864. {
  865. BinkContext * const c = avctx->priv_data;
  866. static VLC_TYPE table[16 * 128][2];
  867. int i;
  868. int flags;
  869. c->version = avctx->codec_tag >> 24;
  870. if (c->version < 'c') {
  871. av_log(avctx, AV_LOG_ERROR, "Too old version '%c'\n", c->version);
  872. return -1;
  873. }
  874. if (avctx->extradata_size < 4) {
  875. av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
  876. return -1;
  877. }
  878. flags = AV_RL32(avctx->extradata);
  879. c->has_alpha = flags & BINK_FLAG_ALPHA;
  880. c->swap_planes = c->version >= 'h';
  881. if (!bink_trees[15].table) {
  882. for (i = 0; i < 16; i++) {
  883. const int maxbits = bink_tree_lens[i][15];
  884. bink_trees[i].table = table + i*128;
  885. bink_trees[i].table_allocated = 1 << maxbits;
  886. init_vlc(&bink_trees[i], maxbits, 16,
  887. bink_tree_lens[i], 1, 1,
  888. bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  889. }
  890. }
  891. c->avctx = avctx;
  892. c->pic.data[0] = NULL;
  893. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  894. return 1;
  895. }
  896. avctx->pix_fmt = c->has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
  897. avctx->idct_algo = FF_IDCT_BINK;
  898. dsputil_init(&c->dsp, avctx);
  899. ff_init_scantable(c->dsp.idct_permutation, &c->scantable, bink_scan);
  900. init_bundles(c);
  901. return 0;
  902. }
  903. static av_cold int decode_end(AVCodecContext *avctx)
  904. {
  905. BinkContext * const c = avctx->priv_data;
  906. if (c->pic.data[0])
  907. avctx->release_buffer(avctx, &c->pic);
  908. if (c->last.data[0])
  909. avctx->release_buffer(avctx, &c->last);
  910. free_bundles(c);
  911. return 0;
  912. }
  913. AVCodec bink_decoder = {
  914. "binkvideo",
  915. AVMEDIA_TYPE_VIDEO,
  916. CODEC_ID_BINKVIDEO,
  917. sizeof(BinkContext),
  918. decode_init,
  919. NULL,
  920. decode_end,
  921. decode_frame,
  922. .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
  923. };