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.

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