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.

981 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. int width = avctx->width >> !!plane;
  640. init_lengths(c, FFMAX(width, 8), bw);
  641. for (i = 0; i < BINK_NB_SRC; i++)
  642. read_bundle(&gb, c, i);
  643. plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
  644. ref_start = c->last.data[plane_idx];
  645. ref_end = c->last.data[plane_idx]
  646. + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
  647. for (i = 0; i < 64; i++)
  648. coordmap[i] = (i & 7) + (i >> 3) * stride;
  649. for (by = 0; by < bh; by++) {
  650. if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
  651. return -1;
  652. if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
  653. return -1;
  654. if (read_colors(&gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
  655. return -1;
  656. if (read_patterns(avctx, &gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
  657. return -1;
  658. if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
  659. return -1;
  660. if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
  661. return -1;
  662. if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
  663. return -1;
  664. if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
  665. return -1;
  666. if (read_runs(avctx, &gb, &c->bundle[BINK_SRC_RUN]) < 0)
  667. return -1;
  668. if (by == bh)
  669. break;
  670. dst = c->pic.data[plane_idx] + 8*by*stride;
  671. prev = c->last.data[plane_idx] + 8*by*stride;
  672. for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
  673. blk = get_value(c, BINK_SRC_BLOCK_TYPES);
  674. // 16x16 block type on odd line means part of the already decoded block, so skip it
  675. if ((by & 1) && blk == SCALED_BLOCK) {
  676. bx++;
  677. dst += 8;
  678. prev += 8;
  679. continue;
  680. }
  681. switch (blk) {
  682. case SKIP_BLOCK:
  683. c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
  684. break;
  685. case SCALED_BLOCK:
  686. blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
  687. switch (blk) {
  688. case RUN_BLOCK:
  689. scan = bink_patterns[get_bits(&gb, 4)];
  690. i = 0;
  691. do {
  692. int run = get_value(c, BINK_SRC_RUN) + 1;
  693. i += run;
  694. if (i > 64) {
  695. av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  696. return -1;
  697. }
  698. if (get_bits1(&gb)) {
  699. v = get_value(c, BINK_SRC_COLORS);
  700. for (j = 0; j < run; j++)
  701. ublock[*scan++] = v;
  702. } else {
  703. for (j = 0; j < run; j++)
  704. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  705. }
  706. } while (i < 63);
  707. if (i == 63)
  708. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  709. break;
  710. case INTRA_BLOCK:
  711. c->dsp.clear_block(block);
  712. block[0] = get_value(c, BINK_SRC_INTRA_DC);
  713. read_dct_coeffs(&gb, block, c->scantable.permutated, 1);
  714. c->dsp.idct(block);
  715. c->dsp.put_pixels_nonclamped(block, ublock, 8);
  716. break;
  717. case FILL_BLOCK:
  718. v = get_value(c, BINK_SRC_COLORS);
  719. c->dsp.fill_block_tab[0](dst, v, stride, 16);
  720. break;
  721. case PATTERN_BLOCK:
  722. for (i = 0; i < 2; i++)
  723. col[i] = get_value(c, BINK_SRC_COLORS);
  724. for (j = 0; j < 8; j++) {
  725. v = get_value(c, BINK_SRC_PATTERN);
  726. for (i = 0; i < 8; i++, v >>= 1)
  727. ublock[i + j*8] = col[v & 1];
  728. }
  729. break;
  730. case RAW_BLOCK:
  731. for (j = 0; j < 8; j++)
  732. for (i = 0; i < 8; i++)
  733. ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
  734. break;
  735. default:
  736. av_log(avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
  737. return -1;
  738. }
  739. if (blk != FILL_BLOCK)
  740. c->dsp.scale_block(ublock, dst, stride);
  741. bx++;
  742. dst += 8;
  743. prev += 8;
  744. break;
  745. case MOTION_BLOCK:
  746. xoff = get_value(c, BINK_SRC_X_OFF);
  747. yoff = get_value(c, BINK_SRC_Y_OFF);
  748. ref = prev + xoff + yoff * stride;
  749. if (ref < ref_start || ref > ref_end) {
  750. av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  751. bx*8 + xoff, by*8 + yoff);
  752. return -1;
  753. }
  754. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  755. break;
  756. case RUN_BLOCK:
  757. scan = bink_patterns[get_bits(&gb, 4)];
  758. i = 0;
  759. do {
  760. int run = get_value(c, BINK_SRC_RUN) + 1;
  761. i += run;
  762. if (i > 64) {
  763. av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  764. return -1;
  765. }
  766. if (get_bits1(&gb)) {
  767. v = get_value(c, BINK_SRC_COLORS);
  768. for (j = 0; j < run; j++)
  769. dst[coordmap[*scan++]] = v;
  770. } else {
  771. for (j = 0; j < run; j++)
  772. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  773. }
  774. } while (i < 63);
  775. if (i == 63)
  776. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  777. break;
  778. case RESIDUE_BLOCK:
  779. xoff = get_value(c, BINK_SRC_X_OFF);
  780. yoff = get_value(c, BINK_SRC_Y_OFF);
  781. ref = prev + xoff + yoff * stride;
  782. if (ref < ref_start || ref > ref_end) {
  783. av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  784. bx*8 + xoff, by*8 + yoff);
  785. return -1;
  786. }
  787. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  788. c->dsp.clear_block(block);
  789. v = get_bits(&gb, 7);
  790. read_residue(&gb, block, v);
  791. c->dsp.add_pixels8(dst, block, stride);
  792. break;
  793. case INTRA_BLOCK:
  794. c->dsp.clear_block(block);
  795. block[0] = get_value(c, BINK_SRC_INTRA_DC);
  796. read_dct_coeffs(&gb, block, c->scantable.permutated, 1);
  797. c->dsp.idct_put(dst, stride, block);
  798. break;
  799. case FILL_BLOCK:
  800. v = get_value(c, BINK_SRC_COLORS);
  801. c->dsp.fill_block_tab[1](dst, v, stride, 8);
  802. break;
  803. case INTER_BLOCK:
  804. xoff = get_value(c, BINK_SRC_X_OFF);
  805. yoff = get_value(c, BINK_SRC_Y_OFF);
  806. ref = prev + xoff + yoff * stride;
  807. c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  808. c->dsp.clear_block(block);
  809. block[0] = get_value(c, BINK_SRC_INTER_DC);
  810. read_dct_coeffs(&gb, block, c->scantable.permutated, 0);
  811. c->dsp.idct_add(dst, stride, block);
  812. break;
  813. case PATTERN_BLOCK:
  814. for (i = 0; i < 2; i++)
  815. col[i] = get_value(c, BINK_SRC_COLORS);
  816. for (i = 0; i < 8; i++) {
  817. v = get_value(c, BINK_SRC_PATTERN);
  818. for (j = 0; j < 8; j++, v >>= 1)
  819. dst[i*stride + j] = col[v & 1];
  820. }
  821. break;
  822. case RAW_BLOCK:
  823. for (i = 0; i < 8; i++)
  824. memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
  825. c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
  826. break;
  827. default:
  828. av_log(avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  829. return -1;
  830. }
  831. }
  832. }
  833. if (get_bits_count(&gb) & 0x1F) //next plane data starts at 32-bit boundary
  834. skip_bits_long(&gb, 32 - (get_bits_count(&gb) & 0x1F));
  835. }
  836. emms_c();
  837. *data_size = sizeof(AVFrame);
  838. *(AVFrame*)data = c->pic;
  839. FFSWAP(AVFrame, c->pic, c->last);
  840. /* always report that the buffer was completely consumed */
  841. return pkt->size;
  842. }
  843. static av_cold int decode_init(AVCodecContext *avctx)
  844. {
  845. BinkContext * const c = avctx->priv_data;
  846. static VLC_TYPE table[16 * 128][2];
  847. int i;
  848. c->version = avctx->codec_tag >> 24;
  849. if (c->version < 'c') {
  850. av_log(avctx, AV_LOG_ERROR, "Too old version '%c'\n", c->version);
  851. return -1;
  852. }
  853. c->swap_planes = c->version >= 'i';
  854. if (!bink_trees[15].table) {
  855. for (i = 0; i < 16; i++) {
  856. const int maxbits = bink_tree_lens[i][15];
  857. bink_trees[i].table = table + i*128;
  858. bink_trees[i].table_allocated = 1 << maxbits;
  859. init_vlc(&bink_trees[i], maxbits, 16,
  860. bink_tree_lens[i], 1, 1,
  861. bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  862. }
  863. }
  864. c->avctx = avctx;
  865. c->pic.data[0] = NULL;
  866. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  867. return 1;
  868. }
  869. avctx->pix_fmt = PIX_FMT_YUV420P;
  870. avctx->idct_algo = FF_IDCT_BINK;
  871. dsputil_init(&c->dsp, avctx);
  872. ff_init_scantable(c->dsp.idct_permutation, &c->scantable, bink_scan);
  873. init_bundles(c);
  874. return 0;
  875. }
  876. static av_cold int decode_end(AVCodecContext *avctx)
  877. {
  878. BinkContext * const c = avctx->priv_data;
  879. if (c->pic.data[0])
  880. avctx->release_buffer(avctx, &c->pic);
  881. if (c->last.data[0])
  882. avctx->release_buffer(avctx, &c->last);
  883. free_bundles(c);
  884. return 0;
  885. }
  886. AVCodec bink_decoder = {
  887. "binkvideo",
  888. CODEC_TYPE_VIDEO,
  889. CODEC_ID_BINKVIDEO,
  890. sizeof(BinkContext),
  891. decode_init,
  892. NULL,
  893. decode_end,
  894. decode_frame,
  895. .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
  896. };