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.

1374 lines
46KB

  1. /*
  2. * Bink video decoder
  3. * Copyright (c) 2009 Konstantin Shishkov
  4. * Copyright (C) 2011 Peter Ross <pross@xvid.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/internal.h"
  25. #define BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "binkdata.h"
  28. #include "binkdsp.h"
  29. #include "blockdsp.h"
  30. #include "get_bits.h"
  31. #include "hpeldsp.h"
  32. #include "internal.h"
  33. #include "mathops.h"
  34. #define BINK_FLAG_ALPHA 0x00100000
  35. #define BINK_FLAG_GRAY 0x00020000
  36. static VLC bink_trees[16];
  37. /**
  38. * IDs for different data types used in old version of Bink video codec
  39. */
  40. enum OldSources {
  41. BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
  42. BINKB_SRC_COLORS, ///< pixel values used for different block types
  43. BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
  44. BINKB_SRC_X_OFF, ///< X components of motion value
  45. BINKB_SRC_Y_OFF, ///< Y components of motion value
  46. BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
  47. BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
  48. BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
  49. BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
  50. BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
  51. BINKB_NB_SRC
  52. };
  53. static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
  54. 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
  55. };
  56. static const int binkb_bundle_signed[BINKB_NB_SRC] = {
  57. 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
  58. };
  59. static int32_t binkb_intra_quant[16][64];
  60. static int32_t binkb_inter_quant[16][64];
  61. /**
  62. * IDs for different data types used in Bink video codec
  63. */
  64. enum Sources {
  65. BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
  66. BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
  67. BINK_SRC_COLORS, ///< pixel values used for different block types
  68. BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
  69. BINK_SRC_X_OFF, ///< X components of motion value
  70. BINK_SRC_Y_OFF, ///< Y components of motion value
  71. BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
  72. BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
  73. BINK_SRC_RUN, ///< run lengths for special fill block
  74. BINK_NB_SRC
  75. };
  76. /**
  77. * data needed to decode 4-bit Huffman-coded value
  78. */
  79. typedef struct Tree {
  80. int vlc_num; ///< tree number (in bink_trees[])
  81. uint8_t syms[16]; ///< leaf value to symbol mapping
  82. } Tree;
  83. #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
  84. bink_trees[(tree).vlc_num].bits, 1)]
  85. /**
  86. * data structure used for decoding single Bink data type
  87. */
  88. typedef struct Bundle {
  89. int len; ///< length of number of entries to decode (in bits)
  90. Tree tree; ///< Huffman tree-related data
  91. uint8_t *data; ///< buffer for decoded symbols
  92. uint8_t *data_end; ///< buffer end
  93. uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
  94. uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
  95. } Bundle;
  96. /*
  97. * Decoder context
  98. */
  99. typedef struct BinkContext {
  100. AVCodecContext *avctx;
  101. BlockDSPContext bdsp;
  102. HpelDSPContext hdsp;
  103. BinkDSPContext binkdsp;
  104. AVFrame *last;
  105. int version; ///< internal Bink file version
  106. int has_alpha;
  107. int swap_planes;
  108. unsigned frame_num;
  109. Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
  110. Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
  111. int col_lastval; ///< value of last decoded high nibble in "colours" data type
  112. } BinkContext;
  113. /**
  114. * Bink video block types
  115. */
  116. enum BlockTypes {
  117. SKIP_BLOCK = 0, ///< skipped block
  118. SCALED_BLOCK, ///< block has size 16x16
  119. MOTION_BLOCK, ///< block is copied from previous frame with some offset
  120. RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
  121. RESIDUE_BLOCK, ///< motion block with some difference added
  122. INTRA_BLOCK, ///< intra DCT block
  123. FILL_BLOCK, ///< block is filled with single colour
  124. INTER_BLOCK, ///< motion block with DCT applied to the difference
  125. PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
  126. RAW_BLOCK, ///< uncoded 8x8 block
  127. };
  128. /**
  129. * Initialize length in all bundles.
  130. *
  131. * @param c decoder context
  132. * @param width plane width
  133. * @param bw plane width in 8x8 blocks
  134. */
  135. static void init_lengths(BinkContext *c, int width, int bw)
  136. {
  137. width = FFALIGN(width, 8);
  138. c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
  139. c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
  140. c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
  141. c->bundle[BINK_SRC_INTRA_DC].len =
  142. c->bundle[BINK_SRC_INTER_DC].len =
  143. c->bundle[BINK_SRC_X_OFF].len =
  144. c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
  145. c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
  146. c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
  147. }
  148. /**
  149. * Allocate memory for bundles.
  150. *
  151. * @param c decoder context
  152. */
  153. static av_cold int init_bundles(BinkContext *c)
  154. {
  155. int bw, bh, blocks;
  156. int i;
  157. bw = (c->avctx->width + 7) >> 3;
  158. bh = (c->avctx->height + 7) >> 3;
  159. blocks = bw * bh;
  160. for (i = 0; i < BINKB_NB_SRC; i++) {
  161. c->bundle[i].data = av_mallocz(blocks * 64);
  162. if (!c->bundle[i].data)
  163. return AVERROR(ENOMEM);
  164. c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
  165. }
  166. return 0;
  167. }
  168. /**
  169. * Free memory used by bundles.
  170. *
  171. * @param c decoder context
  172. */
  173. static av_cold void free_bundles(BinkContext *c)
  174. {
  175. int i;
  176. for (i = 0; i < BINKB_NB_SRC; i++)
  177. av_freep(&c->bundle[i].data);
  178. }
  179. /**
  180. * Merge two consequent lists of equal size depending on bits read.
  181. *
  182. * @param gb context for reading bits
  183. * @param dst buffer where merged list will be written to
  184. * @param src pointer to the head of the first list (the second lists starts at src+size)
  185. * @param size input lists size
  186. */
  187. static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
  188. {
  189. uint8_t *src2 = src + size;
  190. int size2 = size;
  191. do {
  192. if (!get_bits1(gb)) {
  193. *dst++ = *src++;
  194. size--;
  195. } else {
  196. *dst++ = *src2++;
  197. size2--;
  198. }
  199. } while (size && size2);
  200. while (size--)
  201. *dst++ = *src++;
  202. while (size2--)
  203. *dst++ = *src2++;
  204. }
  205. /**
  206. * Read information about Huffman tree used to decode data.
  207. *
  208. * @param gb context for reading bits
  209. * @param tree pointer for storing tree data
  210. */
  211. static void read_tree(GetBitContext *gb, Tree *tree)
  212. {
  213. uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
  214. int i, t, len;
  215. tree->vlc_num = get_bits(gb, 4);
  216. if (!tree->vlc_num) {
  217. for (i = 0; i < 16; i++)
  218. tree->syms[i] = i;
  219. return;
  220. }
  221. if (get_bits1(gb)) {
  222. len = get_bits(gb, 3);
  223. for (i = 0; i <= len; i++) {
  224. tree->syms[i] = get_bits(gb, 4);
  225. tmp1[tree->syms[i]] = 1;
  226. }
  227. for (i = 0; i < 16 && len < 16 - 1; i++)
  228. if (!tmp1[i])
  229. tree->syms[++len] = i;
  230. } else {
  231. len = get_bits(gb, 2);
  232. for (i = 0; i < 16; i++)
  233. in[i] = i;
  234. for (i = 0; i <= len; i++) {
  235. int size = 1 << i;
  236. for (t = 0; t < 16; t += size << 1)
  237. merge(gb, out + t, in + t, size);
  238. FFSWAP(uint8_t*, in, out);
  239. }
  240. memcpy(tree->syms, in, 16);
  241. }
  242. }
  243. /**
  244. * Prepare bundle for decoding data.
  245. *
  246. * @param gb context for reading bits
  247. * @param c decoder context
  248. * @param bundle_num number of the bundle to initialize
  249. */
  250. static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
  251. {
  252. int i;
  253. if (bundle_num == BINK_SRC_COLORS) {
  254. for (i = 0; i < 16; i++)
  255. read_tree(gb, &c->col_high[i]);
  256. c->col_lastval = 0;
  257. }
  258. if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
  259. read_tree(gb, &c->bundle[bundle_num].tree);
  260. c->bundle[bundle_num].cur_dec =
  261. c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
  262. }
  263. /**
  264. * common check before starting decoding bundle data
  265. *
  266. * @param gb context for reading bits
  267. * @param b bundle
  268. * @param t variable where number of elements to decode will be stored
  269. */
  270. #define CHECK_READ_VAL(gb, b, t) \
  271. if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
  272. return 0; \
  273. t = get_bits(gb, b->len); \
  274. if (!t) { \
  275. b->cur_dec = NULL; \
  276. return 0; \
  277. } \
  278. static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  279. {
  280. int t, v;
  281. const uint8_t *dec_end;
  282. CHECK_READ_VAL(gb, b, t);
  283. dec_end = b->cur_dec + t;
  284. if (dec_end > b->data_end) {
  285. av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
  286. return AVERROR_INVALIDDATA;
  287. }
  288. if (get_bits1(gb)) {
  289. v = get_bits(gb, 4);
  290. memset(b->cur_dec, v, t);
  291. b->cur_dec += t;
  292. } else {
  293. while (b->cur_dec < dec_end)
  294. *b->cur_dec++ = GET_HUFF(gb, b->tree);
  295. }
  296. return 0;
  297. }
  298. static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  299. {
  300. int t, sign, v;
  301. const uint8_t *dec_end;
  302. CHECK_READ_VAL(gb, b, t);
  303. dec_end = b->cur_dec + t;
  304. if (dec_end > b->data_end) {
  305. av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
  306. return AVERROR_INVALIDDATA;
  307. }
  308. if (get_bits1(gb)) {
  309. v = get_bits(gb, 4);
  310. if (v) {
  311. sign = -get_bits1(gb);
  312. v = (v ^ sign) - sign;
  313. }
  314. memset(b->cur_dec, v, t);
  315. b->cur_dec += t;
  316. } else {
  317. while (b->cur_dec < dec_end) {
  318. v = GET_HUFF(gb, b->tree);
  319. if (v) {
  320. sign = -get_bits1(gb);
  321. v = (v ^ sign) - sign;
  322. }
  323. *b->cur_dec++ = v;
  324. }
  325. }
  326. return 0;
  327. }
  328. static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
  329. static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  330. {
  331. int t, v;
  332. int last = 0;
  333. const uint8_t *dec_end;
  334. CHECK_READ_VAL(gb, b, t);
  335. dec_end = b->cur_dec + t;
  336. if (dec_end > b->data_end) {
  337. av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
  338. return AVERROR_INVALIDDATA;
  339. }
  340. if (get_bits1(gb)) {
  341. v = get_bits(gb, 4);
  342. memset(b->cur_dec, v, t);
  343. b->cur_dec += t;
  344. } else {
  345. while (b->cur_dec < dec_end) {
  346. v = GET_HUFF(gb, b->tree);
  347. if (v < 12) {
  348. last = v;
  349. *b->cur_dec++ = v;
  350. } else {
  351. int run = bink_rlelens[v - 12];
  352. if (dec_end - b->cur_dec < run)
  353. return AVERROR_INVALIDDATA;
  354. memset(b->cur_dec, last, run);
  355. b->cur_dec += run;
  356. }
  357. }
  358. }
  359. return 0;
  360. }
  361. static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
  362. {
  363. int t, v;
  364. const uint8_t *dec_end;
  365. CHECK_READ_VAL(gb, b, t);
  366. dec_end = b->cur_dec + t;
  367. if (dec_end > b->data_end) {
  368. av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
  369. return AVERROR_INVALIDDATA;
  370. }
  371. while (b->cur_dec < dec_end) {
  372. v = GET_HUFF(gb, b->tree);
  373. v |= GET_HUFF(gb, b->tree) << 4;
  374. *b->cur_dec++ = v;
  375. }
  376. return 0;
  377. }
  378. static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
  379. {
  380. int t, sign, v;
  381. const uint8_t *dec_end;
  382. CHECK_READ_VAL(gb, b, t);
  383. dec_end = b->cur_dec + t;
  384. if (dec_end > b->data_end) {
  385. av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
  386. return AVERROR_INVALIDDATA;
  387. }
  388. if (get_bits1(gb)) {
  389. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  390. v = GET_HUFF(gb, b->tree);
  391. v = (c->col_lastval << 4) | v;
  392. if (c->version < 'i') {
  393. sign = ((int8_t) v) >> 7;
  394. v = ((v & 0x7F) ^ sign) - sign;
  395. v += 0x80;
  396. }
  397. memset(b->cur_dec, v, t);
  398. b->cur_dec += t;
  399. } else {
  400. while (b->cur_dec < dec_end) {
  401. c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
  402. v = GET_HUFF(gb, b->tree);
  403. v = (c->col_lastval << 4) | v;
  404. if (c->version < 'i') {
  405. sign = ((int8_t) v) >> 7;
  406. v = ((v & 0x7F) ^ sign) - sign;
  407. v += 0x80;
  408. }
  409. *b->cur_dec++ = v;
  410. }
  411. }
  412. return 0;
  413. }
  414. /** number of bits used to store first DC value in bundle */
  415. #define DC_START_BITS 11
  416. static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
  417. int start_bits, int has_sign)
  418. {
  419. int i, j, len, len2, bsize, sign, v, v2;
  420. int16_t *dst = (int16_t*)b->cur_dec;
  421. int16_t *dst_end = (int16_t*)b->data_end;
  422. CHECK_READ_VAL(gb, b, len);
  423. v = get_bits(gb, start_bits - has_sign);
  424. if (v && has_sign) {
  425. sign = -get_bits1(gb);
  426. v = (v ^ sign) - sign;
  427. }
  428. if (dst_end - dst < 1)
  429. return AVERROR_INVALIDDATA;
  430. *dst++ = v;
  431. len--;
  432. for (i = 0; i < len; i += 8) {
  433. len2 = FFMIN(len - i, 8);
  434. if (dst_end - dst < len2)
  435. return AVERROR_INVALIDDATA;
  436. bsize = get_bits(gb, 4);
  437. if (bsize) {
  438. for (j = 0; j < len2; j++) {
  439. v2 = get_bits(gb, bsize);
  440. if (v2) {
  441. sign = -get_bits1(gb);
  442. v2 = (v2 ^ sign) - sign;
  443. }
  444. v += v2;
  445. *dst++ = v;
  446. if (v < -32768 || v > 32767) {
  447. av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
  448. return AVERROR_INVALIDDATA;
  449. }
  450. }
  451. } else {
  452. for (j = 0; j < len2; j++)
  453. *dst++ = v;
  454. }
  455. }
  456. b->cur_dec = (uint8_t*)dst;
  457. return 0;
  458. }
  459. /**
  460. * Retrieve next value from bundle.
  461. *
  462. * @param c decoder context
  463. * @param bundle bundle number
  464. */
  465. static inline int get_value(BinkContext *c, int bundle)
  466. {
  467. int ret;
  468. if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
  469. return *c->bundle[bundle].cur_ptr++;
  470. if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
  471. return (int8_t)*c->bundle[bundle].cur_ptr++;
  472. ret = *(int16_t*)c->bundle[bundle].cur_ptr;
  473. c->bundle[bundle].cur_ptr += 2;
  474. return ret;
  475. }
  476. static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
  477. {
  478. c->bundle[bundle_num].cur_dec =
  479. c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
  480. c->bundle[bundle_num].len = 13;
  481. }
  482. static av_cold void binkb_init_bundles(BinkContext *c)
  483. {
  484. int i;
  485. for (i = 0; i < BINKB_NB_SRC; i++)
  486. binkb_init_bundle(c, i);
  487. }
  488. static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
  489. {
  490. const int bits = binkb_bundle_sizes[bundle_num];
  491. const int mask = 1 << (bits - 1);
  492. const int issigned = binkb_bundle_signed[bundle_num];
  493. Bundle *b = &c->bundle[bundle_num];
  494. int i, len;
  495. CHECK_READ_VAL(gb, b, len);
  496. if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
  497. return AVERROR_INVALIDDATA;
  498. if (bits <= 8) {
  499. if (!issigned) {
  500. for (i = 0; i < len; i++)
  501. *b->cur_dec++ = get_bits(gb, bits);
  502. } else {
  503. for (i = 0; i < len; i++)
  504. *b->cur_dec++ = get_bits(gb, bits) - mask;
  505. }
  506. } else {
  507. int16_t *dst = (int16_t*)b->cur_dec;
  508. if (!issigned) {
  509. for (i = 0; i < len; i++)
  510. *dst++ = get_bits(gb, bits);
  511. } else {
  512. for (i = 0; i < len; i++)
  513. *dst++ = get_bits(gb, bits) - mask;
  514. }
  515. b->cur_dec = (uint8_t*)dst;
  516. }
  517. return 0;
  518. }
  519. static inline int binkb_get_value(BinkContext *c, int bundle_num)
  520. {
  521. int16_t ret;
  522. const int bits = binkb_bundle_sizes[bundle_num];
  523. if (bits <= 8) {
  524. int val = *c->bundle[bundle_num].cur_ptr++;
  525. return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
  526. }
  527. ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
  528. c->bundle[bundle_num].cur_ptr += 2;
  529. return ret;
  530. }
  531. /**
  532. * Read 8x8 block of DCT coefficients.
  533. *
  534. * @param gb context for reading bits
  535. * @param block place for storing coefficients
  536. * @param scan scan order table
  537. * @param quant_matrices quantization matrices
  538. * @return 0 for success, negative value in other cases
  539. */
  540. static int read_dct_coeffs(GetBitContext *gb, int32_t block[64],
  541. const uint8_t *scan, int *coef_count_,
  542. int coef_idx[64], int q)
  543. {
  544. int coef_list[128];
  545. int mode_list[128];
  546. int i, t, bits, ccoef, mode, sign;
  547. int list_start = 64, list_end = 64, list_pos;
  548. int coef_count = 0;
  549. int quant_idx;
  550. coef_list[list_end] = 4; mode_list[list_end++] = 0;
  551. coef_list[list_end] = 24; mode_list[list_end++] = 0;
  552. coef_list[list_end] = 44; mode_list[list_end++] = 0;
  553. coef_list[list_end] = 1; mode_list[list_end++] = 3;
  554. coef_list[list_end] = 2; mode_list[list_end++] = 3;
  555. coef_list[list_end] = 3; mode_list[list_end++] = 3;
  556. for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
  557. list_pos = list_start;
  558. while (list_pos < list_end) {
  559. if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
  560. list_pos++;
  561. continue;
  562. }
  563. ccoef = coef_list[list_pos];
  564. mode = mode_list[list_pos];
  565. switch (mode) {
  566. case 0:
  567. coef_list[list_pos] = ccoef + 4;
  568. mode_list[list_pos] = 1;
  569. case 2:
  570. if (mode == 2) {
  571. coef_list[list_pos] = 0;
  572. mode_list[list_pos++] = 0;
  573. }
  574. for (i = 0; i < 4; i++, ccoef++) {
  575. if (get_bits1(gb)) {
  576. coef_list[--list_start] = ccoef;
  577. mode_list[ list_start] = 3;
  578. } else {
  579. if (!bits) {
  580. t = 1 - (get_bits1(gb) << 1);
  581. } else {
  582. t = get_bits(gb, bits) | 1 << bits;
  583. sign = -get_bits1(gb);
  584. t = (t ^ sign) - sign;
  585. }
  586. block[scan[ccoef]] = t;
  587. coef_idx[coef_count++] = ccoef;
  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. if (!bits) {
  601. t = 1 - (get_bits1(gb) << 1);
  602. } else {
  603. t = get_bits(gb, bits) | 1 << bits;
  604. sign = -get_bits1(gb);
  605. t = (t ^ sign) - sign;
  606. }
  607. block[scan[ccoef]] = t;
  608. coef_idx[coef_count++] = ccoef;
  609. coef_list[list_pos] = 0;
  610. mode_list[list_pos++] = 0;
  611. break;
  612. }
  613. }
  614. }
  615. if (q == -1) {
  616. quant_idx = get_bits(gb, 4);
  617. } else {
  618. quant_idx = q;
  619. if (quant_idx > 15U) {
  620. av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
  621. return AVERROR_INVALIDDATA;
  622. }
  623. }
  624. *coef_count_ = coef_count;
  625. return quant_idx;
  626. }
  627. static void unquantize_dct_coeffs(int32_t block[64], const uint32_t quant[64],
  628. int coef_count, int coef_idx[64],
  629. const uint8_t *scan)
  630. {
  631. int i;
  632. block[0] = (int)(block[0] * quant[0]) >> 11;
  633. for (i = 0; i < coef_count; i++) {
  634. int idx = coef_idx[i];
  635. block[scan[idx]] = (int)(block[scan[idx]] * quant[idx]) >> 11;
  636. }
  637. }
  638. /**
  639. * Read 8x8 block with residue after motion compensation.
  640. *
  641. * @param gb context for reading bits
  642. * @param block place to store read data
  643. * @param masks_count number of masks to decode
  644. * @return 0 on success, negative value in other cases
  645. */
  646. static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
  647. {
  648. int coef_list[128];
  649. int mode_list[128];
  650. int i, sign, mask, ccoef, mode;
  651. int list_start = 64, list_end = 64, list_pos;
  652. int nz_coeff[64];
  653. int nz_coeff_count = 0;
  654. coef_list[list_end] = 4; mode_list[list_end++] = 0;
  655. coef_list[list_end] = 24; mode_list[list_end++] = 0;
  656. coef_list[list_end] = 44; mode_list[list_end++] = 0;
  657. coef_list[list_end] = 0; mode_list[list_end++] = 2;
  658. for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
  659. for (i = 0; i < nz_coeff_count; i++) {
  660. if (!get_bits1(gb))
  661. continue;
  662. if (block[nz_coeff[i]] < 0)
  663. block[nz_coeff[i]] -= mask;
  664. else
  665. block[nz_coeff[i]] += mask;
  666. masks_count--;
  667. if (masks_count < 0)
  668. return 0;
  669. }
  670. list_pos = list_start;
  671. while (list_pos < list_end) {
  672. if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
  673. list_pos++;
  674. continue;
  675. }
  676. ccoef = coef_list[list_pos];
  677. mode = mode_list[list_pos];
  678. switch (mode) {
  679. case 0:
  680. coef_list[list_pos] = ccoef + 4;
  681. mode_list[list_pos] = 1;
  682. case 2:
  683. if (mode == 2) {
  684. coef_list[list_pos] = 0;
  685. mode_list[list_pos++] = 0;
  686. }
  687. for (i = 0; i < 4; i++, ccoef++) {
  688. if (get_bits1(gb)) {
  689. coef_list[--list_start] = ccoef;
  690. mode_list[ list_start] = 3;
  691. } else {
  692. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  693. sign = -get_bits1(gb);
  694. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  695. masks_count--;
  696. if (masks_count < 0)
  697. return 0;
  698. }
  699. }
  700. break;
  701. case 1:
  702. mode_list[list_pos] = 2;
  703. for (i = 0; i < 3; i++) {
  704. ccoef += 4;
  705. coef_list[list_end] = ccoef;
  706. mode_list[list_end++] = 2;
  707. }
  708. break;
  709. case 3:
  710. nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
  711. sign = -get_bits1(gb);
  712. block[bink_scan[ccoef]] = (mask ^ sign) - sign;
  713. coef_list[list_pos] = 0;
  714. mode_list[list_pos++] = 0;
  715. masks_count--;
  716. if (masks_count < 0)
  717. return 0;
  718. break;
  719. }
  720. }
  721. }
  722. return 0;
  723. }
  724. /**
  725. * Copy 8x8 block from source to destination, where src and dst may be overlapped
  726. */
  727. static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
  728. {
  729. uint8_t tmp[64];
  730. int i;
  731. for (i = 0; i < 8; i++)
  732. memcpy(tmp + i*8, src + i*stride, 8);
  733. for (i = 0; i < 8; i++)
  734. memcpy(dst + i*stride, tmp + i*8, 8);
  735. }
  736. static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
  737. int plane_idx, int is_key, int is_chroma)
  738. {
  739. int blk, ret;
  740. int i, j, bx, by;
  741. uint8_t *dst, *ref, *ref_start, *ref_end;
  742. int v, col[2];
  743. const uint8_t *scan;
  744. int xoff, yoff;
  745. LOCAL_ALIGNED_32(int16_t, block, [64]);
  746. LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
  747. int coordmap[64];
  748. int ybias = is_key ? -15 : 0;
  749. int qp, quant_idx, coef_count, coef_idx[64];
  750. const int stride = frame->linesize[plane_idx];
  751. int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
  752. int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
  753. binkb_init_bundles(c);
  754. ref_start = frame->data[plane_idx];
  755. ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
  756. for (i = 0; i < 64; i++)
  757. coordmap[i] = (i & 7) + (i >> 3) * stride;
  758. for (by = 0; by < bh; by++) {
  759. for (i = 0; i < BINKB_NB_SRC; i++) {
  760. if ((ret = binkb_read_bundle(c, gb, i)) < 0)
  761. return ret;
  762. }
  763. dst = frame->data[plane_idx] + 8*by*stride;
  764. for (bx = 0; bx < bw; bx++, dst += 8) {
  765. blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
  766. switch (blk) {
  767. case 0:
  768. break;
  769. case 1:
  770. scan = bink_patterns[get_bits(gb, 4)];
  771. i = 0;
  772. do {
  773. int mode, run;
  774. mode = get_bits1(gb);
  775. run = get_bits(gb, binkb_runbits[i]) + 1;
  776. i += run;
  777. if (i > 64) {
  778. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  779. return AVERROR_INVALIDDATA;
  780. }
  781. if (mode) {
  782. v = binkb_get_value(c, BINKB_SRC_COLORS);
  783. for (j = 0; j < run; j++)
  784. dst[coordmap[*scan++]] = v;
  785. } else {
  786. for (j = 0; j < run; j++)
  787. dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
  788. }
  789. } while (i < 63);
  790. if (i == 63)
  791. dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
  792. break;
  793. case 2:
  794. memset(dctblock, 0, sizeof(*dctblock) * 64);
  795. dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
  796. qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
  797. if ((quant_idx = read_dct_coeffs(gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
  798. return quant_idx;
  799. unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
  800. c->binkdsp.idct_put(dst, stride, dctblock);
  801. break;
  802. case 3:
  803. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  804. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  805. ref = dst + xoff + yoff * stride;
  806. if (ref < ref_start || ref + 8*stride > ref_end) {
  807. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  808. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  809. c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  810. } else {
  811. put_pixels8x8_overlapped(dst, ref, stride);
  812. }
  813. c->bdsp.clear_block(block);
  814. v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
  815. read_residue(gb, block, v);
  816. c->binkdsp.add_pixels8(dst, block, stride);
  817. break;
  818. case 4:
  819. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  820. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  821. ref = dst + xoff + yoff * stride;
  822. if (ref < ref_start || ref + 8 * stride > ref_end) {
  823. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  824. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  825. c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  826. } else {
  827. put_pixels8x8_overlapped(dst, ref, stride);
  828. }
  829. memset(dctblock, 0, sizeof(*dctblock) * 64);
  830. dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
  831. qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
  832. if ((quant_idx = read_dct_coeffs(gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
  833. return quant_idx;
  834. unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
  835. c->binkdsp.idct_add(dst, stride, dctblock);
  836. break;
  837. case 5:
  838. v = binkb_get_value(c, BINKB_SRC_COLORS);
  839. c->bdsp.fill_block_tab[1](dst, v, stride, 8);
  840. break;
  841. case 6:
  842. for (i = 0; i < 2; i++)
  843. col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
  844. for (i = 0; i < 8; i++) {
  845. v = binkb_get_value(c, BINKB_SRC_PATTERN);
  846. for (j = 0; j < 8; j++, v >>= 1)
  847. dst[i*stride + j] = col[v & 1];
  848. }
  849. break;
  850. case 7:
  851. xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
  852. yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
  853. ref = dst + xoff + yoff * stride;
  854. if (ref < ref_start || ref + 8 * stride > ref_end) {
  855. av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
  856. } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
  857. c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  858. } else {
  859. put_pixels8x8_overlapped(dst, ref, stride);
  860. }
  861. break;
  862. case 8:
  863. for (i = 0; i < 8; i++)
  864. memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
  865. c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
  866. break;
  867. default:
  868. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  869. return AVERROR_INVALIDDATA;
  870. }
  871. }
  872. }
  873. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  874. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  875. return 0;
  876. }
  877. static int bink_put_pixels(BinkContext *c,
  878. uint8_t *dst, uint8_t *prev, int stride,
  879. uint8_t *ref_start,
  880. uint8_t *ref_end)
  881. {
  882. int xoff = get_value(c, BINK_SRC_X_OFF);
  883. int yoff = get_value(c, BINK_SRC_Y_OFF);
  884. uint8_t *ref = prev + xoff + yoff * stride;
  885. if (ref < ref_start || ref > ref_end) {
  886. av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
  887. xoff, yoff);
  888. return AVERROR_INVALIDDATA;
  889. }
  890. c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
  891. return 0;
  892. }
  893. static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
  894. int plane_idx, int is_chroma)
  895. {
  896. int blk, ret;
  897. int i, j, bx, by;
  898. uint8_t *dst, *prev, *ref_start, *ref_end;
  899. int v, col[2];
  900. const uint8_t *scan;
  901. LOCAL_ALIGNED_32(int16_t, block, [64]);
  902. LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
  903. LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
  904. int coordmap[64], quant_idx, coef_count, coef_idx[64];
  905. const int stride = frame->linesize[plane_idx];
  906. int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
  907. int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
  908. int width = c->avctx->width >> is_chroma;
  909. init_lengths(c, FFMAX(width, 8), bw);
  910. for (i = 0; i < BINK_NB_SRC; i++)
  911. read_bundle(gb, c, i);
  912. ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
  913. : frame->data[plane_idx];
  914. ref_end = ref_start
  915. + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
  916. for (i = 0; i < 64; i++)
  917. coordmap[i] = (i & 7) + (i >> 3) * stride;
  918. for (by = 0; by < bh; by++) {
  919. if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
  920. return ret;
  921. if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
  922. return ret;
  923. if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
  924. return ret;
  925. if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
  926. return ret;
  927. if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
  928. return ret;
  929. if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
  930. return ret;
  931. if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
  932. return ret;
  933. if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
  934. return ret;
  935. if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
  936. return ret;
  937. if (by == bh)
  938. break;
  939. dst = frame->data[plane_idx] + 8*by*stride;
  940. prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
  941. : frame->data[plane_idx]) + 8*by*stride;
  942. for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
  943. blk = get_value(c, BINK_SRC_BLOCK_TYPES);
  944. // 16x16 block type on odd line means part of the already decoded block, so skip it
  945. if ((by & 1) && blk == SCALED_BLOCK) {
  946. bx++;
  947. dst += 8;
  948. prev += 8;
  949. continue;
  950. }
  951. switch (blk) {
  952. case SKIP_BLOCK:
  953. c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
  954. break;
  955. case SCALED_BLOCK:
  956. blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
  957. switch (blk) {
  958. case RUN_BLOCK:
  959. scan = bink_patterns[get_bits(gb, 4)];
  960. i = 0;
  961. do {
  962. int run = get_value(c, BINK_SRC_RUN) + 1;
  963. i += run;
  964. if (i > 64) {
  965. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  966. return AVERROR_INVALIDDATA;
  967. }
  968. if (get_bits1(gb)) {
  969. v = get_value(c, BINK_SRC_COLORS);
  970. for (j = 0; j < run; j++)
  971. ublock[*scan++] = v;
  972. } else {
  973. for (j = 0; j < run; j++)
  974. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  975. }
  976. } while (i < 63);
  977. if (i == 63)
  978. ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
  979. break;
  980. case INTRA_BLOCK:
  981. memset(dctblock, 0, sizeof(*dctblock) * 64);
  982. dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
  983. if ((quant_idx = read_dct_coeffs(gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
  984. return quant_idx;
  985. unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
  986. c->binkdsp.idct_put(ublock, 8, dctblock);
  987. break;
  988. case FILL_BLOCK:
  989. v = get_value(c, BINK_SRC_COLORS);
  990. c->bdsp.fill_block_tab[0](dst, v, stride, 16);
  991. break;
  992. case PATTERN_BLOCK:
  993. for (i = 0; i < 2; i++)
  994. col[i] = get_value(c, BINK_SRC_COLORS);
  995. for (j = 0; j < 8; j++) {
  996. v = get_value(c, BINK_SRC_PATTERN);
  997. for (i = 0; i < 8; i++, v >>= 1)
  998. ublock[i + j*8] = col[v & 1];
  999. }
  1000. break;
  1001. case RAW_BLOCK:
  1002. for (j = 0; j < 8; j++)
  1003. for (i = 0; i < 8; i++)
  1004. ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
  1005. break;
  1006. default:
  1007. av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
  1008. return AVERROR_INVALIDDATA;
  1009. }
  1010. if (blk != FILL_BLOCK)
  1011. c->binkdsp.scale_block(ublock, dst, stride);
  1012. bx++;
  1013. dst += 8;
  1014. prev += 8;
  1015. break;
  1016. case MOTION_BLOCK:
  1017. ret = bink_put_pixels(c, dst, prev, stride,
  1018. ref_start, ref_end);
  1019. if (ret < 0)
  1020. return ret;
  1021. break;
  1022. case RUN_BLOCK:
  1023. scan = bink_patterns[get_bits(gb, 4)];
  1024. i = 0;
  1025. do {
  1026. int run = get_value(c, BINK_SRC_RUN) + 1;
  1027. i += run;
  1028. if (i > 64) {
  1029. av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  1030. return AVERROR_INVALIDDATA;
  1031. }
  1032. if (get_bits1(gb)) {
  1033. v = get_value(c, BINK_SRC_COLORS);
  1034. for (j = 0; j < run; j++)
  1035. dst[coordmap[*scan++]] = v;
  1036. } else {
  1037. for (j = 0; j < run; j++)
  1038. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  1039. }
  1040. } while (i < 63);
  1041. if (i == 63)
  1042. dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
  1043. break;
  1044. case RESIDUE_BLOCK:
  1045. ret = bink_put_pixels(c, dst, prev, stride,
  1046. ref_start, ref_end);
  1047. if (ret < 0)
  1048. return ret;
  1049. c->bdsp.clear_block(block);
  1050. v = get_bits(gb, 7);
  1051. read_residue(gb, block, v);
  1052. c->binkdsp.add_pixels8(dst, block, stride);
  1053. break;
  1054. case INTRA_BLOCK:
  1055. memset(dctblock, 0, sizeof(*dctblock) * 64);
  1056. dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
  1057. if ((quant_idx = read_dct_coeffs(gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
  1058. return quant_idx;
  1059. unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
  1060. c->binkdsp.idct_put(dst, stride, dctblock);
  1061. break;
  1062. case FILL_BLOCK:
  1063. v = get_value(c, BINK_SRC_COLORS);
  1064. c->bdsp.fill_block_tab[1](dst, v, stride, 8);
  1065. break;
  1066. case INTER_BLOCK:
  1067. ret = bink_put_pixels(c, dst, prev, stride,
  1068. ref_start, ref_end);
  1069. if (ret < 0)
  1070. return ret;
  1071. memset(dctblock, 0, sizeof(*dctblock) * 64);
  1072. dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
  1073. if ((quant_idx = read_dct_coeffs(gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
  1074. return quant_idx;
  1075. unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
  1076. c->binkdsp.idct_add(dst, stride, dctblock);
  1077. break;
  1078. case PATTERN_BLOCK:
  1079. for (i = 0; i < 2; i++)
  1080. col[i] = get_value(c, BINK_SRC_COLORS);
  1081. for (i = 0; i < 8; i++) {
  1082. v = get_value(c, BINK_SRC_PATTERN);
  1083. for (j = 0; j < 8; j++, v >>= 1)
  1084. dst[i*stride + j] = col[v & 1];
  1085. }
  1086. break;
  1087. case RAW_BLOCK:
  1088. for (i = 0; i < 8; i++)
  1089. memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
  1090. c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
  1091. break;
  1092. default:
  1093. av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
  1094. return AVERROR_INVALIDDATA;
  1095. }
  1096. }
  1097. }
  1098. if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
  1099. skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
  1100. return 0;
  1101. }
  1102. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
  1103. {
  1104. BinkContext * const c = avctx->priv_data;
  1105. AVFrame *frame = data;
  1106. GetBitContext gb;
  1107. int plane, plane_idx, ret;
  1108. int bits_count = pkt->size << 3;
  1109. if (c->version > 'b') {
  1110. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  1111. return ret;
  1112. } else {
  1113. if ((ret = ff_reget_buffer(avctx, c->last)) < 0)
  1114. return ret;
  1115. if ((ret = av_frame_ref(frame, c->last)) < 0)
  1116. return ret;
  1117. }
  1118. init_get_bits(&gb, pkt->data, bits_count);
  1119. if (c->has_alpha) {
  1120. if (c->version >= 'i')
  1121. skip_bits_long(&gb, 32);
  1122. if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
  1123. return ret;
  1124. }
  1125. if (c->version >= 'i')
  1126. skip_bits_long(&gb, 32);
  1127. c->frame_num++;
  1128. for (plane = 0; plane < 3; plane++) {
  1129. plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
  1130. if (c->version > 'b') {
  1131. if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
  1132. return ret;
  1133. } else {
  1134. if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
  1135. c->frame_num == 1, !!plane)) < 0)
  1136. return ret;
  1137. }
  1138. if (get_bits_count(&gb) >= bits_count)
  1139. break;
  1140. }
  1141. emms_c();
  1142. if (c->version > 'b') {
  1143. av_frame_unref(c->last);
  1144. if ((ret = av_frame_ref(c->last, frame)) < 0)
  1145. return ret;
  1146. }
  1147. *got_frame = 1;
  1148. /* always report that the buffer was completely consumed */
  1149. return pkt->size;
  1150. }
  1151. /**
  1152. * Calculate quantization tables for version b
  1153. */
  1154. static av_cold void binkb_calc_quant(void)
  1155. {
  1156. uint8_t inv_bink_scan[64];
  1157. static const int s[64]={
  1158. 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
  1159. 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
  1160. 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
  1161. 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
  1162. 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
  1163. 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
  1164. 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
  1165. 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
  1166. };
  1167. int i, j;
  1168. #define C (1LL<<30)
  1169. for (i = 0; i < 64; i++)
  1170. inv_bink_scan[bink_scan[i]] = i;
  1171. for (j = 0; j < 16; j++) {
  1172. for (i = 0; i < 64; i++) {
  1173. int k = inv_bink_scan[i];
  1174. binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
  1175. binkb_num[j]/(binkb_den[j] * (C>>12));
  1176. binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
  1177. binkb_num[j]/(binkb_den[j] * (C>>12));
  1178. }
  1179. }
  1180. }
  1181. static av_cold int decode_init(AVCodecContext *avctx)
  1182. {
  1183. BinkContext * const c = avctx->priv_data;
  1184. static VLC_TYPE table[16 * 128][2];
  1185. static int binkb_initialised = 0;
  1186. int i, ret;
  1187. int flags;
  1188. c->version = avctx->codec_tag >> 24;
  1189. if (avctx->extradata_size < 4) {
  1190. av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
  1191. return AVERROR_INVALIDDATA;
  1192. }
  1193. flags = AV_RL32(avctx->extradata);
  1194. c->has_alpha = flags & BINK_FLAG_ALPHA;
  1195. c->swap_planes = c->version >= 'h';
  1196. if (!bink_trees[15].table) {
  1197. for (i = 0; i < 16; i++) {
  1198. const int maxbits = bink_tree_lens[i][15];
  1199. bink_trees[i].table = table + i*128;
  1200. bink_trees[i].table_allocated = 1 << maxbits;
  1201. init_vlc(&bink_trees[i], maxbits, 16,
  1202. bink_tree_lens[i], 1, 1,
  1203. bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  1204. }
  1205. }
  1206. c->avctx = avctx;
  1207. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  1208. return ret;
  1209. c->last = av_frame_alloc();
  1210. if (!c->last)
  1211. return AVERROR(ENOMEM);
  1212. avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
  1213. ff_blockdsp_init(&c->bdsp, avctx);
  1214. ff_hpeldsp_init(&c->hdsp, avctx->flags);
  1215. ff_binkdsp_init(&c->binkdsp);
  1216. if ((ret = init_bundles(c)) < 0) {
  1217. free_bundles(c);
  1218. return ret;
  1219. }
  1220. if (c->version == 'b') {
  1221. if (!binkb_initialised) {
  1222. binkb_calc_quant();
  1223. binkb_initialised = 1;
  1224. }
  1225. }
  1226. return 0;
  1227. }
  1228. static av_cold int decode_end(AVCodecContext *avctx)
  1229. {
  1230. BinkContext * const c = avctx->priv_data;
  1231. av_frame_free(&c->last);
  1232. free_bundles(c);
  1233. return 0;
  1234. }
  1235. static void flush(AVCodecContext *avctx)
  1236. {
  1237. BinkContext * const c = avctx->priv_data;
  1238. c->frame_num = 0;
  1239. }
  1240. AVCodec ff_bink_decoder = {
  1241. .name = "binkvideo",
  1242. .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
  1243. .type = AVMEDIA_TYPE_VIDEO,
  1244. .id = AV_CODEC_ID_BINKVIDEO,
  1245. .priv_data_size = sizeof(BinkContext),
  1246. .init = decode_init,
  1247. .close = decode_end,
  1248. .decode = decode_frame,
  1249. .flush = flush,
  1250. .capabilities = AV_CODEC_CAP_DR1,
  1251. };