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.

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