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.

1278 lines
43KB

  1. /*
  2. * Resolume DXV decoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. * Copyright (C) 2018 Paul B Mahol
  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 <stdint.h>
  23. #include "libavutil/imgutils.h"
  24. #include "mathops.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. #include "lzf.h"
  29. #include "texturedsp.h"
  30. #include "thread.h"
  31. typedef struct DXVContext {
  32. TextureDSPContext texdsp;
  33. GetByteContext gbc;
  34. uint8_t *tex_data; // Compressed texture
  35. uint8_t *ctex_data; // Compressed texture
  36. int tex_rat; // Compression ratio
  37. int tex_step; // Distance between blocks
  38. int ctex_step; // Distance between blocks
  39. int64_t tex_size; // Texture size
  40. int64_t ctex_size; // Texture size
  41. /* Optimal number of slices for parallel decoding */
  42. int slice_count;
  43. uint8_t *op_data[4]; // Opcodes
  44. int64_t op_size[4]; // Opcodes size
  45. int texture_block_w;
  46. int texture_block_h;
  47. int ctexture_block_w;
  48. int ctexture_block_h;
  49. /* Pointer to the selected decompression function */
  50. int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
  51. int (*tex_funct_planar[2])(uint8_t *plane0, ptrdiff_t stride0,
  52. uint8_t *plane1, ptrdiff_t stride1,
  53. const uint8_t *block);
  54. } DXVContext;
  55. static void decompress_indices(uint8_t *dst, const uint8_t *src)
  56. {
  57. int block, i;
  58. for (block = 0; block < 2; block++) {
  59. int tmp = AV_RL24(src);
  60. /* Unpack 8x3 bit from last 3 byte block */
  61. for (i = 0; i < 8; i++)
  62. dst[i] = (tmp >> (i * 3)) & 0x7;
  63. src += 3;
  64. dst += 8;
  65. }
  66. }
  67. static int extract_component(int yo0, int yo1, int code)
  68. {
  69. int yo;
  70. if (yo0 == yo1) {
  71. yo = yo0;
  72. } else if (code == 0) {
  73. yo = yo0;
  74. } else if (code == 1) {
  75. yo = yo1;
  76. } else {
  77. if (yo0 > yo1) {
  78. yo = (uint8_t) (((8 - code) * yo0 +
  79. (code - 1) * yo1) / 7);
  80. } else {
  81. if (code == 6) {
  82. yo = 0;
  83. } else if (code == 7) {
  84. yo = 255;
  85. } else {
  86. yo = (uint8_t) (((6 - code) * yo0 +
  87. (code - 1) * yo1) / 5);
  88. }
  89. }
  90. }
  91. return yo;
  92. }
  93. static int cocg_block(uint8_t *plane0, ptrdiff_t stride0,
  94. uint8_t *plane1, ptrdiff_t stride1,
  95. const uint8_t *block)
  96. {
  97. uint8_t co_indices[16];
  98. uint8_t cg_indices[16];
  99. uint8_t co0 = *(block);
  100. uint8_t co1 = *(block + 1);
  101. uint8_t cg0 = *(block + 8);
  102. uint8_t cg1 = *(block + 9);
  103. int x, y;
  104. decompress_indices(co_indices, block + 2);
  105. decompress_indices(cg_indices, block + 10);
  106. for (y = 0; y < 4; y++) {
  107. for (x = 0; x < 4; x++) {
  108. int co_code = co_indices[x + y * 4];
  109. int cg_code = cg_indices[x + y * 4];
  110. plane0[x] = extract_component(cg0, cg1, cg_code);
  111. plane1[x] = extract_component(co0, co1, co_code);
  112. }
  113. plane0 += stride0;
  114. plane1 += stride1;
  115. }
  116. return 16;
  117. }
  118. static void yao_subblock(uint8_t *dst, uint8_t *yo_indices,
  119. ptrdiff_t stride, const uint8_t *block)
  120. {
  121. uint8_t yo0 = *(block);
  122. uint8_t yo1 = *(block + 1);
  123. int x, y;
  124. decompress_indices(yo_indices, block + 2);
  125. for (y = 0; y < 4; y++) {
  126. for (x = 0; x < 4; x++) {
  127. int yo_code = yo_indices[x + y * 4];
  128. dst[x] = extract_component(yo0, yo1, yo_code);
  129. }
  130. dst += stride;
  131. }
  132. }
  133. static int yo_block(uint8_t *dst, ptrdiff_t stride,
  134. uint8_t *unused0, ptrdiff_t unused1,
  135. const uint8_t *block)
  136. {
  137. uint8_t yo_indices[16];
  138. yao_subblock(dst, yo_indices, stride, block);
  139. yao_subblock(dst + 4, yo_indices, stride, block + 8);
  140. yao_subblock(dst + 8, yo_indices, stride, block + 16);
  141. yao_subblock(dst + 12, yo_indices, stride, block + 24);
  142. return 32;
  143. }
  144. static int yao_block(uint8_t *plane0, ptrdiff_t stride0,
  145. uint8_t *plane3, ptrdiff_t stride1,
  146. const uint8_t *block)
  147. {
  148. uint8_t yo_indices[16];
  149. uint8_t a_indices[16];
  150. yao_subblock(plane0, yo_indices, stride0, block);
  151. yao_subblock(plane3, a_indices, stride1, block + 8);
  152. yao_subblock(plane0 + 4, yo_indices, stride0, block + 16);
  153. yao_subblock(plane3 + 4, a_indices, stride1, block + 24);
  154. yao_subblock(plane0 + 8, yo_indices, stride0, block + 32);
  155. yao_subblock(plane3 + 8, a_indices, stride1, block + 40);
  156. yao_subblock(plane0 + 12, yo_indices, stride0, block + 48);
  157. yao_subblock(plane3 + 12, a_indices, stride1, block + 56);
  158. return 64;
  159. }
  160. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  161. int slice, int thread_nb)
  162. {
  163. DXVContext *ctx = avctx->priv_data;
  164. AVFrame *frame = arg;
  165. const uint8_t *d = ctx->tex_data;
  166. int w_block = avctx->coded_width / ctx->texture_block_w;
  167. int h_block = avctx->coded_height / ctx->texture_block_h;
  168. int x, y;
  169. int start_slice, end_slice;
  170. start_slice = h_block * slice / ctx->slice_count;
  171. end_slice = h_block * (slice + 1) / ctx->slice_count;
  172. if (ctx->tex_funct) {
  173. for (y = start_slice; y < end_slice; y++) {
  174. uint8_t *p = frame->data[0] + y * frame->linesize[0] * ctx->texture_block_h;
  175. int off = y * w_block;
  176. for (x = 0; x < w_block; x++) {
  177. ctx->tex_funct(p + x * 4 * ctx->texture_block_w, frame->linesize[0],
  178. d + (off + x) * ctx->tex_step);
  179. }
  180. }
  181. } else {
  182. const uint8_t *c = ctx->ctex_data;
  183. for (y = start_slice; y < end_slice; y++) {
  184. uint8_t *p0 = frame->data[0] + y * frame->linesize[0] * ctx->texture_block_h;
  185. uint8_t *p3 = ctx->tex_step != 64 ? NULL : frame->data[3] + y * frame->linesize[3] * ctx->texture_block_h;
  186. int off = y * w_block;
  187. for (x = 0; x < w_block; x++) {
  188. ctx->tex_funct_planar[0](p0 + x * ctx->texture_block_w, frame->linesize[0],
  189. p3 != NULL ? p3 + x * ctx->texture_block_w : NULL, frame->linesize[3],
  190. d + (off + x) * ctx->tex_step);
  191. }
  192. }
  193. w_block = (avctx->coded_width / 2) / ctx->ctexture_block_w;
  194. h_block = (avctx->coded_height / 2) / ctx->ctexture_block_h;
  195. start_slice = h_block * slice / ctx->slice_count;
  196. end_slice = h_block * (slice + 1) / ctx->slice_count;
  197. for (y = start_slice; y < end_slice; y++) {
  198. uint8_t *p0 = frame->data[1] + y * frame->linesize[1] * ctx->ctexture_block_h;
  199. uint8_t *p1 = frame->data[2] + y * frame->linesize[2] * ctx->ctexture_block_h;
  200. int off = y * w_block;
  201. for (x = 0; x < w_block; x++) {
  202. ctx->tex_funct_planar[1](p0 + x * ctx->ctexture_block_w, frame->linesize[1],
  203. p1 + x * ctx->ctexture_block_w, frame->linesize[2],
  204. c + (off + x) * ctx->ctex_step);
  205. }
  206. }
  207. }
  208. return 0;
  209. }
  210. /* This scheme addresses already decoded elements depending on 2-bit status:
  211. * 0 -> copy new element
  212. * 1 -> copy one element from position -x
  213. * 2 -> copy one element from position -(get_byte() + 2) * x
  214. * 3 -> copy one element from position -(get_16le() + 0x102) * x
  215. * x is always 2 for dxt1 and 4 for dxt5. */
  216. #define CHECKPOINT(x) \
  217. do { \
  218. if (state == 0) { \
  219. if (bytestream2_get_bytes_left(gbc) < 4) \
  220. return AVERROR_INVALIDDATA; \
  221. value = bytestream2_get_le32(gbc); \
  222. state = 16; \
  223. } \
  224. op = value & 0x3; \
  225. value >>= 2; \
  226. state--; \
  227. switch (op) { \
  228. case 1: \
  229. idx = x; \
  230. break; \
  231. case 2: \
  232. idx = (bytestream2_get_byte(gbc) + 2) * x; \
  233. if (idx > pos) { \
  234. av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
  235. return AVERROR_INVALIDDATA; \
  236. } \
  237. break; \
  238. case 3: \
  239. idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
  240. if (idx > pos) { \
  241. av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
  242. return AVERROR_INVALIDDATA; \
  243. } \
  244. break; \
  245. } \
  246. } while(0)
  247. static int dxv_decompress_dxt1(AVCodecContext *avctx)
  248. {
  249. DXVContext *ctx = avctx->priv_data;
  250. GetByteContext *gbc = &ctx->gbc;
  251. uint32_t value, prev, op;
  252. int idx = 0, state = 0;
  253. int pos = 2;
  254. /* Copy the first two elements */
  255. AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
  256. AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
  257. /* Process input until the whole texture has been filled */
  258. while (pos + 2 <= ctx->tex_size / 4) {
  259. CHECKPOINT(2);
  260. /* Copy two elements from a previous offset or from the input buffer */
  261. if (op) {
  262. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  263. AV_WL32(ctx->tex_data + 4 * pos, prev);
  264. pos++;
  265. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  266. AV_WL32(ctx->tex_data + 4 * pos, prev);
  267. pos++;
  268. } else {
  269. CHECKPOINT(2);
  270. if (op)
  271. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  272. else
  273. prev = bytestream2_get_le32(gbc);
  274. AV_WL32(ctx->tex_data + 4 * pos, prev);
  275. pos++;
  276. CHECKPOINT(2);
  277. if (op)
  278. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  279. else
  280. prev = bytestream2_get_le32(gbc);
  281. AV_WL32(ctx->tex_data + 4 * pos, prev);
  282. pos++;
  283. }
  284. }
  285. return 0;
  286. }
  287. typedef struct OpcodeTable {
  288. int16_t next;
  289. uint8_t val1;
  290. uint8_t val2;
  291. } OpcodeTable;
  292. static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
  293. {
  294. unsigned half = 512, bits = 1023, left = 1024, input, mask;
  295. int value, counter = 0, rshift = 10, lshift = 30;
  296. mask = bytestream2_get_le32(gb) >> 2;
  297. while (left) {
  298. if (counter >= 256)
  299. return AVERROR_INVALIDDATA;
  300. value = bits & mask;
  301. left -= bits & mask;
  302. mask >>= rshift;
  303. lshift -= rshift;
  304. table[counter++] = value;
  305. if (lshift < 16) {
  306. if (bytestream2_get_bytes_left(gb) <= 0)
  307. return AVERROR_INVALIDDATA;
  308. input = bytestream2_get_le16(gb);
  309. mask += input << lshift;
  310. lshift += 16;
  311. }
  312. if (left < half) {
  313. half >>= 1;
  314. bits >>= 1;
  315. rshift--;
  316. }
  317. }
  318. for (; !table[counter - 1]; counter--)
  319. if (counter <= 0)
  320. return AVERROR_INVALIDDATA;
  321. *nb_elements = counter;
  322. if (counter < 256)
  323. memset(&table[counter], 0, 4 * (256 - counter));
  324. if (lshift >= 16)
  325. bytestream2_seek(gb, -2, SEEK_CUR);
  326. return 0;
  327. }
  328. static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
  329. {
  330. unsigned table2[256] = { 0 };
  331. unsigned x = 0;
  332. int val0, val1, i, j = 2, k = 0;
  333. table2[0] = table0[0];
  334. for (i = 0; i < nb_elements - 1; i++, table2[i] = val0) {
  335. val0 = table0[i + 1] + table2[i];
  336. }
  337. if (!table2[0]) {
  338. do {
  339. k++;
  340. } while (!table2[k]);
  341. }
  342. j = 2;
  343. for (i = 1024; i > 0; i--) {
  344. for (table1[x].val1 = k; k < 256 && j > table2[k]; k++);
  345. x = (x - 383) & 0x3FF;
  346. j++;
  347. }
  348. if (nb_elements > 0)
  349. memcpy(&table2[0], table0, 4 * nb_elements);
  350. for (i = 0; i < 1024; i++) {
  351. val0 = table1[i].val1;
  352. val1 = table2[val0];
  353. table2[val0]++;
  354. x = 31 - ff_clz(val1);
  355. if (x > 10)
  356. return AVERROR_INVALIDDATA;
  357. table1[i].val2 = 10 - x;
  358. table1[i].next = (val1 << table1[i].val2) - 1024;
  359. }
  360. return 0;
  361. }
  362. static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
  363. {
  364. OpcodeTable optable[1024];
  365. int sum, x, val, lshift, rshift, ret, i, idx;
  366. int64_t size_in_bits;
  367. unsigned endoffset, newoffset, offset;
  368. unsigned next;
  369. uint8_t *src = (uint8_t *)gb->buffer;
  370. ret = fill_optable(table, optable, nb_elements);
  371. if (ret < 0)
  372. return ret;
  373. size_in_bits = bytestream2_get_le32(gb);
  374. endoffset = ((size_in_bits + 7) >> 3) - 4;
  375. if (endoffset <= 0 || bytestream2_get_bytes_left(gb) < endoffset)
  376. return AVERROR_INVALIDDATA;
  377. offset = endoffset;
  378. next = AV_RL32(src + endoffset);
  379. rshift = (((size_in_bits & 0xFF) - 1) & 7) + 15;
  380. lshift = 32 - rshift;
  381. idx = (next >> rshift) & 0x3FF;
  382. for (i = 0; i < op_size; i++) {
  383. dst[i] = optable[idx].val1;
  384. val = optable[idx].val2;
  385. sum = val + lshift;
  386. x = (next << lshift) >> 1 >> (31 - val);
  387. newoffset = offset - (sum >> 3);
  388. lshift = sum & 7;
  389. idx = x + optable[idx].next;
  390. offset = newoffset;
  391. if (offset > endoffset)
  392. return AVERROR_INVALIDDATA;
  393. next = AV_RL32(src + offset);
  394. }
  395. bytestream2_skip(gb, (size_in_bits + 7 >> 3) - 4);
  396. return 0;
  397. }
  398. static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
  399. {
  400. int pos = bytestream2_tell(gb);
  401. int flag = bytestream2_peek_byte(gb);
  402. if ((flag & 3) == 0) {
  403. bytestream2_skip(gb, 1);
  404. bytestream2_get_buffer(gb, dstp, op_size);
  405. } else if ((flag & 3) == 1) {
  406. bytestream2_skip(gb, 1);
  407. memset(dstp, bytestream2_get_byte(gb), op_size);
  408. } else {
  409. uint32_t table[256];
  410. int ret, elements = 0;
  411. ret = fill_ltable(gb, table, &elements);
  412. if (ret < 0)
  413. return ret;
  414. ret = get_opcodes(gb, table, dstp, op_size, elements);
  415. if (ret < 0)
  416. return ret;
  417. }
  418. return bytestream2_tell(gb) - pos;
  419. }
  420. static int dxv_decompress_cgo(DXVContext *ctx, GetByteContext *gb,
  421. uint8_t *tex_data, int tex_size,
  422. uint8_t *op_data, int *oindex,
  423. int op_size,
  424. uint8_t **dstp, int *statep,
  425. uint8_t **tab0, uint8_t **tab1,
  426. int offset)
  427. {
  428. uint8_t *dst = *dstp;
  429. uint8_t *tptr0, *tptr1, *tptr3;
  430. int oi = *oindex;
  431. int state = *statep;
  432. int opcode, v, vv;
  433. if (state <= 0) {
  434. if (oi >= op_size)
  435. return AVERROR_INVALIDDATA;
  436. opcode = op_data[oi++];
  437. if (!opcode) {
  438. v = bytestream2_get_byte(gb);
  439. if (v == 255) {
  440. do {
  441. if (bytestream2_get_bytes_left(gb) <= 0)
  442. return AVERROR_INVALIDDATA;
  443. opcode = bytestream2_get_le16(gb);
  444. v += opcode;
  445. } while (opcode == 0xFFFF);
  446. }
  447. AV_WL32(dst, AV_RL32(dst - (8 + offset)));
  448. AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
  449. state = v + 4;
  450. goto done;
  451. }
  452. switch (opcode) {
  453. case 1:
  454. AV_WL32(dst, AV_RL32(dst - (8 + offset)));
  455. AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
  456. break;
  457. case 2:
  458. vv = (8 + offset) * (bytestream2_get_le16(gb) + 1);
  459. if (vv < 0 || vv > dst - tex_data)
  460. return AVERROR_INVALIDDATA;
  461. tptr0 = dst - vv;
  462. v = AV_RL32(tptr0);
  463. AV_WL32(dst, AV_RL32(tptr0));
  464. AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
  465. tab0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
  466. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  467. break;
  468. case 3:
  469. AV_WL32(dst, bytestream2_get_le32(gb));
  470. AV_WL32(dst + 4, bytestream2_get_le32(gb));
  471. tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
  472. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  473. break;
  474. case 4:
  475. tptr3 = tab1[bytestream2_get_byte(gb)];
  476. if (!tptr3)
  477. return AVERROR_INVALIDDATA;
  478. AV_WL16(dst, bytestream2_get_le16(gb));
  479. AV_WL16(dst + 2, AV_RL16(tptr3));
  480. dst[4] = tptr3[2];
  481. AV_WL16(dst + 5, bytestream2_get_le16(gb));
  482. dst[7] = bytestream2_get_byte(gb);
  483. tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
  484. break;
  485. case 5:
  486. tptr3 = tab1[bytestream2_get_byte(gb)];
  487. if (!tptr3)
  488. return AVERROR_INVALIDDATA;
  489. AV_WL16(dst, bytestream2_get_le16(gb));
  490. AV_WL16(dst + 2, bytestream2_get_le16(gb));
  491. dst[4] = bytestream2_get_byte(gb);
  492. AV_WL16(dst + 5, AV_RL16(tptr3));
  493. dst[7] = tptr3[2];
  494. tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
  495. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  496. break;
  497. case 6:
  498. tptr0 = tab1[bytestream2_get_byte(gb)];
  499. if (!tptr0)
  500. return AVERROR_INVALIDDATA;
  501. tptr1 = tab1[bytestream2_get_byte(gb)];
  502. if (!tptr1)
  503. return AVERROR_INVALIDDATA;
  504. AV_WL16(dst, bytestream2_get_le16(gb));
  505. AV_WL16(dst + 2, AV_RL16(tptr0));
  506. dst[4] = tptr0[2];
  507. AV_WL16(dst + 5, AV_RL16(tptr1));
  508. dst[7] = tptr1[2];
  509. tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
  510. break;
  511. case 7:
  512. v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
  513. if (v < 0 || v > dst - tex_data)
  514. return AVERROR_INVALIDDATA;
  515. tptr0 = dst - v;
  516. AV_WL16(dst, bytestream2_get_le16(gb));
  517. AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
  518. AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
  519. tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
  520. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  521. break;
  522. case 8:
  523. tptr1 = tab0[bytestream2_get_byte(gb)];
  524. if (!tptr1)
  525. return AVERROR_INVALIDDATA;
  526. AV_WL16(dst, AV_RL16(tptr1));
  527. AV_WL16(dst + 2, bytestream2_get_le16(gb));
  528. AV_WL32(dst + 4, bytestream2_get_le32(gb));
  529. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  530. break;
  531. case 9:
  532. tptr1 = tab0[bytestream2_get_byte(gb)];
  533. if (!tptr1)
  534. return AVERROR_INVALIDDATA;
  535. tptr3 = tab1[bytestream2_get_byte(gb)];
  536. if (!tptr3)
  537. return AVERROR_INVALIDDATA;
  538. AV_WL16(dst, AV_RL16(tptr1));
  539. AV_WL16(dst + 2, AV_RL16(tptr3));
  540. dst[4] = tptr3[2];
  541. AV_WL16(dst + 5, bytestream2_get_le16(gb));
  542. dst[7] = bytestream2_get_byte(gb);
  543. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  544. break;
  545. case 10:
  546. tptr1 = tab0[bytestream2_get_byte(gb)];
  547. if (!tptr1)
  548. return AVERROR_INVALIDDATA;
  549. tptr3 = tab1[bytestream2_get_byte(gb)];
  550. if (!tptr3)
  551. return AVERROR_INVALIDDATA;
  552. AV_WL16(dst, AV_RL16(tptr1));
  553. AV_WL16(dst + 2, bytestream2_get_le16(gb));
  554. dst[4] = bytestream2_get_byte(gb);
  555. AV_WL16(dst + 5, AV_RL16(tptr3));
  556. dst[7] = tptr3[2];
  557. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  558. break;
  559. case 11:
  560. tptr0 = tab0[bytestream2_get_byte(gb)];
  561. if (!tptr0)
  562. return AVERROR_INVALIDDATA;
  563. tptr3 = tab1[bytestream2_get_byte(gb)];
  564. if (!tptr3)
  565. return AVERROR_INVALIDDATA;
  566. tptr1 = tab1[bytestream2_get_byte(gb)];
  567. if (!tptr1)
  568. return AVERROR_INVALIDDATA;
  569. AV_WL16(dst, AV_RL16(tptr0));
  570. AV_WL16(dst + 2, AV_RL16(tptr3));
  571. dst[4] = tptr3[2];
  572. AV_WL16(dst + 5, AV_RL16(tptr1));
  573. dst[7] = tptr1[2];
  574. break;
  575. case 12:
  576. tptr1 = tab0[bytestream2_get_byte(gb)];
  577. if (!tptr1)
  578. return AVERROR_INVALIDDATA;
  579. v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
  580. if (v < 0 || v > dst - tex_data)
  581. return AVERROR_INVALIDDATA;
  582. tptr0 = dst - v;
  583. AV_WL16(dst, AV_RL16(tptr1));
  584. AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
  585. AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
  586. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  587. break;
  588. case 13:
  589. AV_WL16(dst, AV_RL16(dst - (8 + offset)));
  590. AV_WL16(dst + 2, bytestream2_get_le16(gb));
  591. AV_WL32(dst + 4, bytestream2_get_le32(gb));
  592. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  593. break;
  594. case 14:
  595. tptr3 = tab1[bytestream2_get_byte(gb)];
  596. if (!tptr3)
  597. return AVERROR_INVALIDDATA;
  598. AV_WL16(dst, AV_RL16(dst - (8 + offset)));
  599. AV_WL16(dst + 2, AV_RL16(tptr3));
  600. dst[4] = tptr3[2];
  601. AV_WL16(dst + 5, bytestream2_get_le16(gb));
  602. dst[7] = bytestream2_get_byte(gb);
  603. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  604. break;
  605. case 15:
  606. tptr3 = tab1[bytestream2_get_byte(gb)];
  607. if (!tptr3)
  608. return AVERROR_INVALIDDATA;
  609. AV_WL16(dst, AV_RL16(dst - (8 + offset)));
  610. AV_WL16(dst + 2, bytestream2_get_le16(gb));
  611. dst[4] = bytestream2_get_byte(gb);
  612. AV_WL16(dst + 5, AV_RL16(tptr3));
  613. dst[7] = tptr3[2];
  614. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  615. break;
  616. case 16:
  617. tptr3 = tab1[bytestream2_get_byte(gb)];
  618. if (!tptr3)
  619. return AVERROR_INVALIDDATA;
  620. tptr1 = tab1[bytestream2_get_byte(gb)];
  621. if (!tptr1)
  622. return AVERROR_INVALIDDATA;
  623. AV_WL16(dst, AV_RL16(dst - (8 + offset)));
  624. AV_WL16(dst + 2, AV_RL16(tptr3));
  625. dst[4] = tptr3[2];
  626. AV_WL16(dst + 5, AV_RL16(tptr1));
  627. dst[7] = tptr1[2];
  628. break;
  629. case 17:
  630. v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
  631. if (v < 0 || v > dst - tex_data)
  632. return AVERROR_INVALIDDATA;
  633. AV_WL16(dst, AV_RL16(dst - (8 + offset)));
  634. AV_WL16(dst + 2, AV_RL16(&dst[-v + 2]));
  635. AV_WL32(dst + 4, AV_RL32(&dst[-v + 4]));
  636. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
  637. break;
  638. default:
  639. break;
  640. }
  641. } else {
  642. done:
  643. AV_WL32(dst, AV_RL32(dst - (8 + offset)));
  644. AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
  645. state--;
  646. }
  647. if (dst - tex_data + 8 > tex_size)
  648. return AVERROR_INVALIDDATA;
  649. dst += 8;
  650. *oindex = oi;
  651. *dstp = dst;
  652. *statep = state;
  653. return 0;
  654. }
  655. static int dxv_decompress_cocg(DXVContext *ctx, GetByteContext *gb,
  656. uint8_t *tex_data, int tex_size,
  657. uint8_t *op_data0, uint8_t *op_data1,
  658. int max_op_size0, int max_op_size1)
  659. {
  660. uint8_t *dst, *tab2[256] = { 0 }, *tab0[256] = { 0 }, *tab3[256] = { 0 }, *tab1[256] = { 0 };
  661. int op_offset = bytestream2_get_le32(gb);
  662. unsigned op_size0 = bytestream2_get_le32(gb);
  663. unsigned op_size1 = bytestream2_get_le32(gb);
  664. int data_start = bytestream2_tell(gb);
  665. int skip0, skip1, oi0 = 0, oi1 = 0;
  666. int ret, state0 = 0, state1 = 0;
  667. if (op_offset < 12)
  668. return AVERROR_INVALIDDATA;
  669. dst = tex_data;
  670. bytestream2_skip(gb, op_offset - 12);
  671. if (op_size0 > max_op_size0)
  672. return AVERROR_INVALIDDATA;
  673. skip0 = dxv_decompress_opcodes(gb, op_data0, op_size0);
  674. if (skip0 < 0)
  675. return skip0;
  676. bytestream2_seek(gb, data_start + op_offset + skip0 - 12, SEEK_SET);
  677. if (op_size1 > max_op_size1)
  678. return AVERROR_INVALIDDATA;
  679. skip1 = dxv_decompress_opcodes(gb, op_data1, op_size1);
  680. if (skip1 < 0)
  681. return skip1;
  682. bytestream2_seek(gb, data_start, SEEK_SET);
  683. AV_WL32(dst, bytestream2_get_le32(gb));
  684. AV_WL32(dst + 4, bytestream2_get_le32(gb));
  685. AV_WL32(dst + 8, bytestream2_get_le32(gb));
  686. AV_WL32(dst + 12, bytestream2_get_le32(gb));
  687. tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
  688. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
  689. tab2[0x9E3779B1 * AV_RL16(dst + 8) >> 24] = dst + 8;
  690. tab3[0x9E3779B1 * (AV_RL32(dst + 10) & 0xFFFFFF) >> 24] = dst + 10;
  691. dst += 16;
  692. while (dst + 10 < tex_data + tex_size) {
  693. ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data0, &oi0, op_size0,
  694. &dst, &state0, tab0, tab1, 8);
  695. if (ret < 0)
  696. return ret;
  697. ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data1, &oi1, op_size1,
  698. &dst, &state1, tab2, tab3, 8);
  699. if (ret < 0)
  700. return ret;
  701. }
  702. bytestream2_seek(gb, data_start + op_offset + skip0 + skip1 - 12, SEEK_SET);
  703. return 0;
  704. }
  705. static int dxv_decompress_yo(DXVContext *ctx, GetByteContext *gb,
  706. uint8_t *tex_data, int tex_size,
  707. uint8_t *op_data, int max_op_size)
  708. {
  709. int op_offset = bytestream2_get_le32(gb);
  710. unsigned op_size = bytestream2_get_le32(gb);
  711. int data_start = bytestream2_tell(gb);
  712. uint8_t *dst, *table0[256] = { 0 }, *table1[256] = { 0 };
  713. int ret, state = 0, skip, oi = 0, v, vv;
  714. dst = tex_data;
  715. bytestream2_skip(gb, op_offset - 8);
  716. if (op_size > max_op_size)
  717. return AVERROR_INVALIDDATA;
  718. skip = dxv_decompress_opcodes(gb, op_data, op_size);
  719. if (skip < 0)
  720. return skip;
  721. bytestream2_seek(gb, data_start, SEEK_SET);
  722. v = bytestream2_get_le32(gb);
  723. AV_WL32(dst, v);
  724. vv = bytestream2_get_le32(gb);
  725. table0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
  726. AV_WL32(dst + 4, vv);
  727. table1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
  728. dst += 8;
  729. while (dst < tex_data + tex_size) {
  730. ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data, &oi, op_size,
  731. &dst, &state, table0, table1, 0);
  732. if (ret < 0)
  733. return ret;
  734. }
  735. bytestream2_seek(gb, data_start + op_offset + skip - 8, SEEK_SET);
  736. return 0;
  737. }
  738. static int dxv_decompress_ycg6(AVCodecContext *avctx)
  739. {
  740. DXVContext *ctx = avctx->priv_data;
  741. GetByteContext *gb = &ctx->gbc;
  742. int ret;
  743. ret = dxv_decompress_yo(ctx, gb, ctx->tex_data, ctx->tex_size,
  744. ctx->op_data[0], ctx->op_size[0]);
  745. if (ret < 0)
  746. return ret;
  747. return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
  748. ctx->op_data[1], ctx->op_data[2],
  749. ctx->op_size[1], ctx->op_size[2]);
  750. }
  751. static int dxv_decompress_yg10(AVCodecContext *avctx)
  752. {
  753. DXVContext *ctx = avctx->priv_data;
  754. GetByteContext *gb = &ctx->gbc;
  755. int ret;
  756. ret = dxv_decompress_cocg(ctx, gb, ctx->tex_data, ctx->tex_size,
  757. ctx->op_data[0], ctx->op_data[3],
  758. ctx->op_size[0], ctx->op_size[3]);
  759. if (ret < 0)
  760. return ret;
  761. return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
  762. ctx->op_data[1], ctx->op_data[2],
  763. ctx->op_size[1], ctx->op_size[2]);
  764. }
  765. static int dxv_decompress_dxt5(AVCodecContext *avctx)
  766. {
  767. DXVContext *ctx = avctx->priv_data;
  768. GetByteContext *gbc = &ctx->gbc;
  769. uint32_t value, op;
  770. int idx, prev, state = 0;
  771. int pos = 4;
  772. int run = 0;
  773. int probe, check;
  774. /* Copy the first four elements */
  775. AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
  776. AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
  777. AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
  778. AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
  779. /* Process input until the whole texture has been filled */
  780. while (pos + 2 <= ctx->tex_size / 4) {
  781. if (run) {
  782. run--;
  783. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  784. AV_WL32(ctx->tex_data + 4 * pos, prev);
  785. pos++;
  786. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  787. AV_WL32(ctx->tex_data + 4 * pos, prev);
  788. pos++;
  789. } else {
  790. if (bytestream2_get_bytes_left(gbc) < 1)
  791. return AVERROR_INVALIDDATA;
  792. if (state == 0) {
  793. value = bytestream2_get_le32(gbc);
  794. state = 16;
  795. }
  796. op = value & 0x3;
  797. value >>= 2;
  798. state--;
  799. switch (op) {
  800. case 0:
  801. /* Long copy */
  802. check = bytestream2_get_byte(gbc) + 1;
  803. if (check == 256) {
  804. do {
  805. probe = bytestream2_get_le16(gbc);
  806. check += probe;
  807. } while (probe == 0xFFFF);
  808. }
  809. while (check && pos + 4 <= ctx->tex_size / 4) {
  810. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  811. AV_WL32(ctx->tex_data + 4 * pos, prev);
  812. pos++;
  813. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  814. AV_WL32(ctx->tex_data + 4 * pos, prev);
  815. pos++;
  816. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  817. AV_WL32(ctx->tex_data + 4 * pos, prev);
  818. pos++;
  819. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  820. AV_WL32(ctx->tex_data + 4 * pos, prev);
  821. pos++;
  822. check--;
  823. }
  824. /* Restart (or exit) the loop */
  825. continue;
  826. break;
  827. case 1:
  828. /* Load new run value */
  829. run = bytestream2_get_byte(gbc);
  830. if (run == 255) {
  831. do {
  832. probe = bytestream2_get_le16(gbc);
  833. run += probe;
  834. } while (probe == 0xFFFF);
  835. }
  836. /* Copy two dwords from previous data */
  837. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  838. AV_WL32(ctx->tex_data + 4 * pos, prev);
  839. pos++;
  840. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  841. AV_WL32(ctx->tex_data + 4 * pos, prev);
  842. pos++;
  843. break;
  844. case 2:
  845. /* Copy two dwords from a previous index */
  846. idx = 8 + bytestream2_get_le16(gbc);
  847. if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
  848. return AVERROR_INVALIDDATA;
  849. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  850. AV_WL32(ctx->tex_data + 4 * pos, prev);
  851. pos++;
  852. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  853. AV_WL32(ctx->tex_data + 4 * pos, prev);
  854. pos++;
  855. break;
  856. case 3:
  857. /* Copy two dwords from input */
  858. prev = bytestream2_get_le32(gbc);
  859. AV_WL32(ctx->tex_data + 4 * pos, prev);
  860. pos++;
  861. prev = bytestream2_get_le32(gbc);
  862. AV_WL32(ctx->tex_data + 4 * pos, prev);
  863. pos++;
  864. break;
  865. }
  866. }
  867. CHECKPOINT(4);
  868. if (pos + 2 > ctx->tex_size / 4)
  869. return AVERROR_INVALIDDATA;
  870. /* Copy two elements from a previous offset or from the input buffer */
  871. if (op) {
  872. if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
  873. return AVERROR_INVALIDDATA;
  874. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  875. AV_WL32(ctx->tex_data + 4 * pos, prev);
  876. pos++;
  877. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  878. AV_WL32(ctx->tex_data + 4 * pos, prev);
  879. pos++;
  880. } else {
  881. CHECKPOINT(4);
  882. if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
  883. return AVERROR_INVALIDDATA;
  884. if (op)
  885. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  886. else
  887. prev = bytestream2_get_le32(gbc);
  888. AV_WL32(ctx->tex_data + 4 * pos, prev);
  889. pos++;
  890. CHECKPOINT(4);
  891. if (op)
  892. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  893. else
  894. prev = bytestream2_get_le32(gbc);
  895. AV_WL32(ctx->tex_data + 4 * pos, prev);
  896. pos++;
  897. }
  898. }
  899. return 0;
  900. }
  901. static int dxv_decompress_lzf(AVCodecContext *avctx)
  902. {
  903. DXVContext *ctx = avctx->priv_data;
  904. return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
  905. }
  906. static int dxv_decompress_raw(AVCodecContext *avctx)
  907. {
  908. DXVContext *ctx = avctx->priv_data;
  909. GetByteContext *gbc = &ctx->gbc;
  910. if (bytestream2_get_bytes_left(gbc) < ctx->tex_size)
  911. return AVERROR_INVALIDDATA;
  912. bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
  913. return 0;
  914. }
  915. static int dxv_decode(AVCodecContext *avctx, void *data,
  916. int *got_frame, AVPacket *avpkt)
  917. {
  918. DXVContext *ctx = avctx->priv_data;
  919. ThreadFrame tframe;
  920. GetByteContext *gbc = &ctx->gbc;
  921. int (*decompress_tex)(AVCodecContext *avctx);
  922. const char *msgcomp, *msgtext;
  923. uint32_t tag;
  924. int version_major, version_minor = 0;
  925. int size = 0, old_type = 0;
  926. int ret;
  927. bytestream2_init(gbc, avpkt->data, avpkt->size);
  928. ctx->texture_block_h = 4;
  929. ctx->texture_block_w = 4;
  930. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  931. avctx->colorspace = AVCOL_SPC_RGB;
  932. ctx->tex_funct = NULL;
  933. ctx->tex_funct_planar[0] = NULL;
  934. ctx->tex_funct_planar[1] = NULL;
  935. tag = bytestream2_get_le32(gbc);
  936. switch (tag) {
  937. case MKBETAG('D', 'X', 'T', '1'):
  938. decompress_tex = dxv_decompress_dxt1;
  939. ctx->tex_funct = ctx->texdsp.dxt1_block;
  940. ctx->tex_rat = 8;
  941. ctx->tex_step = 8;
  942. msgcomp = "DXTR1";
  943. msgtext = "DXT1";
  944. break;
  945. case MKBETAG('D', 'X', 'T', '5'):
  946. decompress_tex = dxv_decompress_dxt5;
  947. ctx->tex_funct = ctx->texdsp.dxt5_block;
  948. ctx->tex_rat = 4;
  949. ctx->tex_step = 16;
  950. msgcomp = "DXTR5";
  951. msgtext = "DXT5";
  952. break;
  953. case MKBETAG('Y', 'C', 'G', '6'):
  954. decompress_tex = dxv_decompress_ycg6;
  955. ctx->tex_funct_planar[0] = yo_block;
  956. ctx->tex_funct_planar[1] = cocg_block;
  957. ctx->tex_rat = 8;
  958. ctx->tex_step = 32;
  959. ctx->ctex_step = 16;
  960. msgcomp = "YOCOCG6";
  961. msgtext = "YCG6";
  962. ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
  963. ctx->texture_block_h = 4;
  964. ctx->texture_block_w = 16;
  965. ctx->ctexture_block_h = 4;
  966. ctx->ctexture_block_w = 4;
  967. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  968. avctx->colorspace = AVCOL_SPC_YCOCG;
  969. break;
  970. case MKBETAG('Y', 'G', '1', '0'):
  971. decompress_tex = dxv_decompress_yg10;
  972. ctx->tex_funct_planar[0] = yao_block;
  973. ctx->tex_funct_planar[1] = cocg_block;
  974. ctx->tex_rat = 4;
  975. ctx->tex_step = 64;
  976. ctx->ctex_step = 16;
  977. msgcomp = "YAOCOCG10";
  978. msgtext = "YG10";
  979. ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
  980. ctx->texture_block_h = 4;
  981. ctx->texture_block_w = 16;
  982. ctx->ctexture_block_h = 4;
  983. ctx->ctexture_block_w = 4;
  984. avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  985. avctx->colorspace = AVCOL_SPC_YCOCG;
  986. break;
  987. default:
  988. /* Old version does not have a real header, just size and type. */
  989. size = tag & 0x00FFFFFF;
  990. old_type = tag >> 24;
  991. version_major = (old_type & 0x0F) - 1;
  992. if (old_type & 0x80) {
  993. msgcomp = "RAW";
  994. decompress_tex = dxv_decompress_raw;
  995. } else {
  996. msgcomp = "LZF";
  997. decompress_tex = dxv_decompress_lzf;
  998. }
  999. if (old_type & 0x40) {
  1000. msgtext = "DXT5";
  1001. ctx->tex_funct = ctx->texdsp.dxt5_block;
  1002. ctx->tex_step = 16;
  1003. } else if (old_type & 0x20 || version_major == 1) {
  1004. msgtext = "DXT1";
  1005. ctx->tex_funct = ctx->texdsp.dxt1_block;
  1006. ctx->tex_step = 8;
  1007. } else {
  1008. av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
  1009. return AVERROR_INVALIDDATA;
  1010. }
  1011. ctx->tex_rat = 1;
  1012. break;
  1013. }
  1014. ctx->slice_count = av_clip(avctx->thread_count, 1,
  1015. avctx->coded_height / FFMAX(ctx->texture_block_h,
  1016. ctx->ctexture_block_h));
  1017. /* New header is 12 bytes long. */
  1018. if (!old_type) {
  1019. version_major = bytestream2_get_byte(gbc) - 1;
  1020. version_minor = bytestream2_get_byte(gbc);
  1021. /* Encoder copies texture data when compression is not advantageous. */
  1022. if (bytestream2_get_byte(gbc)) {
  1023. msgcomp = "RAW";
  1024. ctx->tex_rat = 1;
  1025. decompress_tex = dxv_decompress_raw;
  1026. }
  1027. bytestream2_skip(gbc, 1); // unknown
  1028. size = bytestream2_get_le32(gbc);
  1029. }
  1030. av_log(avctx, AV_LOG_DEBUG,
  1031. "%s compression with %s texture (version %d.%d)\n",
  1032. msgcomp, msgtext, version_major, version_minor);
  1033. if (size != bytestream2_get_bytes_left(gbc)) {
  1034. av_log(avctx, AV_LOG_ERROR,
  1035. "Incomplete or invalid file (header %d, left %u).\n",
  1036. size, bytestream2_get_bytes_left(gbc));
  1037. return AVERROR_INVALIDDATA;
  1038. }
  1039. ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
  1040. ret = av_reallocp(&ctx->tex_data, ctx->tex_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1041. if (ret < 0)
  1042. return ret;
  1043. if (ctx->ctex_size) {
  1044. int i;
  1045. ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
  1046. ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
  1047. ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
  1048. ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
  1049. ret = av_reallocp(&ctx->ctex_data, ctx->ctex_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1050. if (ret < 0)
  1051. return ret;
  1052. for (i = 0; i < 4; i++) {
  1053. ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
  1054. if (ret < 0)
  1055. return ret;
  1056. }
  1057. }
  1058. /* Decompress texture out of the intermediate compression. */
  1059. ret = decompress_tex(avctx);
  1060. if (ret < 0)
  1061. return ret;
  1062. {
  1063. int w_block = avctx->coded_width / ctx->texture_block_w;
  1064. int h_block = avctx->coded_height / ctx->texture_block_h;
  1065. if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
  1066. return AVERROR_INVALIDDATA;
  1067. }
  1068. tframe.f = data;
  1069. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  1070. if (ret < 0)
  1071. return ret;
  1072. /* Now decompress the texture with the standard functions. */
  1073. avctx->execute2(avctx, decompress_texture_thread,
  1074. tframe.f, NULL, ctx->slice_count);
  1075. /* Frame is ready to be output. */
  1076. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  1077. tframe.f->key_frame = 1;
  1078. *got_frame = 1;
  1079. return avpkt->size;
  1080. }
  1081. static int dxv_init(AVCodecContext *avctx)
  1082. {
  1083. DXVContext *ctx = avctx->priv_data;
  1084. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  1085. if (ret < 0) {
  1086. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  1087. avctx->width, avctx->height);
  1088. return ret;
  1089. }
  1090. /* Codec requires 16x16 alignment. */
  1091. avctx->coded_width = FFALIGN(avctx->width, 16);
  1092. avctx->coded_height = FFALIGN(avctx->height, 16);
  1093. ff_texturedsp_init(&ctx->texdsp);
  1094. return 0;
  1095. }
  1096. static int dxv_close(AVCodecContext *avctx)
  1097. {
  1098. DXVContext *ctx = avctx->priv_data;
  1099. av_freep(&ctx->tex_data);
  1100. av_freep(&ctx->ctex_data);
  1101. av_freep(&ctx->op_data[0]);
  1102. av_freep(&ctx->op_data[1]);
  1103. av_freep(&ctx->op_data[2]);
  1104. av_freep(&ctx->op_data[3]);
  1105. return 0;
  1106. }
  1107. AVCodec ff_dxv_decoder = {
  1108. .name = "dxv",
  1109. .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
  1110. .type = AVMEDIA_TYPE_VIDEO,
  1111. .id = AV_CODEC_ID_DXV,
  1112. .init = dxv_init,
  1113. .decode = dxv_decode,
  1114. .close = dxv_close,
  1115. .priv_data_size = sizeof(DXVContext),
  1116. .capabilities = AV_CODEC_CAP_DR1 |
  1117. AV_CODEC_CAP_SLICE_THREADS |
  1118. AV_CODEC_CAP_FRAME_THREADS,
  1119. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  1120. FF_CODEC_CAP_INIT_CLEANUP,
  1121. };