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.

994 lines
33KB

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