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.

1271 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. 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. tag = bytestream2_get_le32(gbc);
  931. switch (tag) {
  932. case MKBETAG('D', 'X', 'T', '1'):
  933. decompress_tex = dxv_decompress_dxt1;
  934. ctx->tex_funct = ctx->texdsp.dxt1_block;
  935. ctx->tex_rat = 8;
  936. ctx->tex_step = 8;
  937. msgcomp = "DXTR1";
  938. msgtext = "DXT1";
  939. break;
  940. case MKBETAG('D', 'X', 'T', '5'):
  941. decompress_tex = dxv_decompress_dxt5;
  942. ctx->tex_funct = ctx->texdsp.dxt5_block;
  943. ctx->tex_rat = 4;
  944. ctx->tex_step = 16;
  945. msgcomp = "DXTR5";
  946. msgtext = "DXT5";
  947. break;
  948. case MKBETAG('Y', 'C', 'G', '6'):
  949. decompress_tex = dxv_decompress_ycg6;
  950. ctx->tex_funct_planar[0] = yo_block;
  951. ctx->tex_funct_planar[1] = cocg_block;
  952. ctx->tex_rat = 8;
  953. ctx->tex_step = 32;
  954. ctx->ctex_step = 16;
  955. msgcomp = "YOCOCG6";
  956. msgtext = "YCG6";
  957. ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
  958. ctx->texture_block_h = 4;
  959. ctx->texture_block_w = 16;
  960. ctx->ctexture_block_h = 4;
  961. ctx->ctexture_block_w = 4;
  962. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  963. avctx->colorspace = AVCOL_SPC_YCOCG;
  964. break;
  965. case MKBETAG('Y', 'G', '1', '0'):
  966. decompress_tex = dxv_decompress_yg10;
  967. ctx->tex_funct_planar[0] = yao_block;
  968. ctx->tex_funct_planar[1] = cocg_block;
  969. ctx->tex_rat = 4;
  970. ctx->tex_step = 64;
  971. ctx->ctex_step = 16;
  972. msgcomp = "YAOCOCG10";
  973. msgtext = "YG10";
  974. ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
  975. ctx->texture_block_h = 4;
  976. ctx->texture_block_w = 16;
  977. ctx->ctexture_block_h = 4;
  978. ctx->ctexture_block_w = 4;
  979. avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  980. avctx->colorspace = AVCOL_SPC_YCOCG;
  981. break;
  982. default:
  983. /* Old version does not have a real header, just size and type. */
  984. size = tag & 0x00FFFFFF;
  985. old_type = tag >> 24;
  986. version_major = (old_type & 0x0F) - 1;
  987. if (old_type & 0x80) {
  988. msgcomp = "RAW";
  989. decompress_tex = dxv_decompress_raw;
  990. } else {
  991. msgcomp = "LZF";
  992. decompress_tex = dxv_decompress_lzf;
  993. }
  994. if (old_type & 0x40) {
  995. msgtext = "DXT5";
  996. ctx->tex_funct = ctx->texdsp.dxt5_block;
  997. ctx->tex_step = 16;
  998. } else if (old_type & 0x20 || version_major == 1) {
  999. msgtext = "DXT1";
  1000. ctx->tex_funct = ctx->texdsp.dxt1_block;
  1001. ctx->tex_step = 8;
  1002. } else {
  1003. av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
  1004. return AVERROR_INVALIDDATA;
  1005. }
  1006. ctx->tex_rat = 1;
  1007. break;
  1008. }
  1009. ctx->slice_count = av_clip(avctx->thread_count, 1,
  1010. avctx->coded_height / FFMAX(ctx->texture_block_h,
  1011. ctx->ctexture_block_h));
  1012. /* New header is 12 bytes long. */
  1013. if (!old_type) {
  1014. version_major = bytestream2_get_byte(gbc) - 1;
  1015. version_minor = bytestream2_get_byte(gbc);
  1016. /* Encoder copies texture data when compression is not advantageous. */
  1017. if (bytestream2_get_byte(gbc)) {
  1018. msgcomp = "RAW";
  1019. ctx->tex_rat = 1;
  1020. decompress_tex = dxv_decompress_raw;
  1021. }
  1022. bytestream2_skip(gbc, 1); // unknown
  1023. size = bytestream2_get_le32(gbc);
  1024. }
  1025. av_log(avctx, AV_LOG_DEBUG,
  1026. "%s compression with %s texture (version %d.%d)\n",
  1027. msgcomp, msgtext, version_major, version_minor);
  1028. if (size != bytestream2_get_bytes_left(gbc)) {
  1029. av_log(avctx, AV_LOG_ERROR,
  1030. "Incomplete or invalid file (header %d, left %u).\n",
  1031. size, bytestream2_get_bytes_left(gbc));
  1032. return AVERROR_INVALIDDATA;
  1033. }
  1034. ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
  1035. ret = av_reallocp(&ctx->tex_data, ctx->tex_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1036. if (ret < 0)
  1037. return ret;
  1038. if (ctx->ctex_size) {
  1039. int i;
  1040. ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
  1041. ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
  1042. ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
  1043. ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
  1044. ret = av_reallocp(&ctx->ctex_data, ctx->ctex_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1045. if (ret < 0)
  1046. return ret;
  1047. for (i = 0; i < 4; i++) {
  1048. ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
  1049. if (ret < 0)
  1050. return ret;
  1051. }
  1052. }
  1053. /* Decompress texture out of the intermediate compression. */
  1054. ret = decompress_tex(avctx);
  1055. if (ret < 0)
  1056. return ret;
  1057. {
  1058. int w_block = avctx->coded_width / ctx->texture_block_w;
  1059. int h_block = avctx->coded_height / ctx->texture_block_h;
  1060. if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
  1061. return AVERROR_INVALIDDATA;
  1062. }
  1063. tframe.f = data;
  1064. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  1065. if (ret < 0)
  1066. return ret;
  1067. /* Now decompress the texture with the standard functions. */
  1068. avctx->execute2(avctx, decompress_texture_thread,
  1069. tframe.f, NULL, ctx->slice_count);
  1070. /* Frame is ready to be output. */
  1071. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  1072. tframe.f->key_frame = 1;
  1073. *got_frame = 1;
  1074. return avpkt->size;
  1075. }
  1076. static int dxv_init(AVCodecContext *avctx)
  1077. {
  1078. DXVContext *ctx = avctx->priv_data;
  1079. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  1080. if (ret < 0) {
  1081. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  1082. avctx->width, avctx->height);
  1083. return ret;
  1084. }
  1085. /* Codec requires 16x16 alignment. */
  1086. avctx->coded_width = FFALIGN(avctx->width, 16);
  1087. avctx->coded_height = FFALIGN(avctx->height, 16);
  1088. ff_texturedsp_init(&ctx->texdsp);
  1089. return 0;
  1090. }
  1091. static int dxv_close(AVCodecContext *avctx)
  1092. {
  1093. DXVContext *ctx = avctx->priv_data;
  1094. av_freep(&ctx->tex_data);
  1095. av_freep(&ctx->ctex_data);
  1096. av_freep(&ctx->op_data[0]);
  1097. av_freep(&ctx->op_data[1]);
  1098. av_freep(&ctx->op_data[2]);
  1099. av_freep(&ctx->op_data[3]);
  1100. return 0;
  1101. }
  1102. AVCodec ff_dxv_decoder = {
  1103. .name = "dxv",
  1104. .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
  1105. .type = AVMEDIA_TYPE_VIDEO,
  1106. .id = AV_CODEC_ID_DXV,
  1107. .init = dxv_init,
  1108. .decode = dxv_decode,
  1109. .close = dxv_close,
  1110. .priv_data_size = sizeof(DXVContext),
  1111. .capabilities = AV_CODEC_CAP_DR1 |
  1112. AV_CODEC_CAP_SLICE_THREADS |
  1113. AV_CODEC_CAP_FRAME_THREADS,
  1114. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  1115. FF_CODEC_CAP_INIT_CLEANUP,
  1116. };