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.

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