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.

1280 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 || op_offset - 12 > bytestream2_get_bytes_left(gb))
  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. if (op_size1 > max_op_size1)
  677. return AVERROR_INVALIDDATA;
  678. skip1 = dxv_decompress_opcodes(gb, op_data1, op_size1);
  679. if (skip1 < 0)
  680. return skip1;
  681. bytestream2_seek(gb, data_start, SEEK_SET);
  682. AV_WL32(dst, bytestream2_get_le32(gb));
  683. AV_WL32(dst + 4, bytestream2_get_le32(gb));
  684. AV_WL32(dst + 8, bytestream2_get_le32(gb));
  685. AV_WL32(dst + 12, bytestream2_get_le32(gb));
  686. tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
  687. tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
  688. tab2[0x9E3779B1 * AV_RL16(dst + 8) >> 24] = dst + 8;
  689. tab3[0x9E3779B1 * (AV_RL32(dst + 10) & 0xFFFFFF) >> 24] = dst + 10;
  690. dst += 16;
  691. while (dst + 10 < tex_data + tex_size) {
  692. ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data0, &oi0, op_size0,
  693. &dst, &state0, tab0, tab1, 8);
  694. if (ret < 0)
  695. return ret;
  696. ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data1, &oi1, op_size1,
  697. &dst, &state1, tab2, tab3, 8);
  698. if (ret < 0)
  699. return ret;
  700. }
  701. bytestream2_seek(gb, data_start - 12 + op_offset + skip0 + skip1, SEEK_SET);
  702. return 0;
  703. }
  704. static int dxv_decompress_yo(DXVContext *ctx, GetByteContext *gb,
  705. uint8_t *tex_data, int tex_size,
  706. uint8_t *op_data, int max_op_size)
  707. {
  708. int op_offset = bytestream2_get_le32(gb);
  709. unsigned op_size = bytestream2_get_le32(gb);
  710. int data_start = bytestream2_tell(gb);
  711. uint8_t *dst, *table0[256] = { 0 }, *table1[256] = { 0 };
  712. int ret, state = 0, skip, oi = 0, v, vv;
  713. if (op_offset < 8 || op_offset - 8 > bytestream2_get_bytes_left(gb))
  714. return AVERROR_INVALIDDATA;
  715. dst = tex_data;
  716. bytestream2_skip(gb, op_offset - 8);
  717. if (op_size > max_op_size)
  718. return AVERROR_INVALIDDATA;
  719. skip = dxv_decompress_opcodes(gb, op_data, op_size);
  720. if (skip < 0)
  721. return skip;
  722. bytestream2_seek(gb, data_start, SEEK_SET);
  723. v = bytestream2_get_le32(gb);
  724. AV_WL32(dst, v);
  725. vv = bytestream2_get_le32(gb);
  726. table0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
  727. AV_WL32(dst + 4, vv);
  728. table1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
  729. dst += 8;
  730. while (dst < tex_data + tex_size) {
  731. ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data, &oi, op_size,
  732. &dst, &state, table0, table1, 0);
  733. if (ret < 0)
  734. return ret;
  735. }
  736. bytestream2_seek(gb, data_start + op_offset + skip - 8, SEEK_SET);
  737. return 0;
  738. }
  739. static int dxv_decompress_ycg6(AVCodecContext *avctx)
  740. {
  741. DXVContext *ctx = avctx->priv_data;
  742. GetByteContext *gb = &ctx->gbc;
  743. int ret;
  744. ret = dxv_decompress_yo(ctx, gb, ctx->tex_data, ctx->tex_size,
  745. ctx->op_data[0], ctx->op_size[0]);
  746. if (ret < 0)
  747. return ret;
  748. return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
  749. ctx->op_data[1], ctx->op_data[2],
  750. ctx->op_size[1], ctx->op_size[2]);
  751. }
  752. static int dxv_decompress_yg10(AVCodecContext *avctx)
  753. {
  754. DXVContext *ctx = avctx->priv_data;
  755. GetByteContext *gb = &ctx->gbc;
  756. int ret;
  757. ret = dxv_decompress_cocg(ctx, gb, ctx->tex_data, ctx->tex_size,
  758. ctx->op_data[0], ctx->op_data[3],
  759. ctx->op_size[0], ctx->op_size[3]);
  760. if (ret < 0)
  761. return ret;
  762. return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
  763. ctx->op_data[1], ctx->op_data[2],
  764. ctx->op_size[1], ctx->op_size[2]);
  765. }
  766. static int dxv_decompress_dxt5(AVCodecContext *avctx)
  767. {
  768. DXVContext *ctx = avctx->priv_data;
  769. GetByteContext *gbc = &ctx->gbc;
  770. uint32_t value, op;
  771. int idx, prev, state = 0;
  772. int pos = 4;
  773. int run = 0;
  774. int probe, check;
  775. /* Copy the first four elements */
  776. AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
  777. AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
  778. AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
  779. AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
  780. /* Process input until the whole texture has been filled */
  781. while (pos + 2 <= ctx->tex_size / 4) {
  782. if (run) {
  783. run--;
  784. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  785. AV_WL32(ctx->tex_data + 4 * pos, prev);
  786. pos++;
  787. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  788. AV_WL32(ctx->tex_data + 4 * pos, prev);
  789. pos++;
  790. } else {
  791. if (bytestream2_get_bytes_left(gbc) < 1)
  792. return AVERROR_INVALIDDATA;
  793. if (state == 0) {
  794. value = bytestream2_get_le32(gbc);
  795. state = 16;
  796. }
  797. op = value & 0x3;
  798. value >>= 2;
  799. state--;
  800. switch (op) {
  801. case 0:
  802. /* Long copy */
  803. check = bytestream2_get_byte(gbc) + 1;
  804. if (check == 256) {
  805. do {
  806. probe = bytestream2_get_le16(gbc);
  807. check += probe;
  808. } while (probe == 0xFFFF);
  809. }
  810. while (check && pos + 4 <= ctx->tex_size / 4) {
  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. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  821. AV_WL32(ctx->tex_data + 4 * pos, prev);
  822. pos++;
  823. check--;
  824. }
  825. /* Restart (or exit) the loop */
  826. continue;
  827. break;
  828. case 1:
  829. /* Load new run value */
  830. run = bytestream2_get_byte(gbc);
  831. if (run == 255) {
  832. do {
  833. probe = bytestream2_get_le16(gbc);
  834. run += probe;
  835. } while (probe == 0xFFFF);
  836. }
  837. /* Copy two dwords from previous data */
  838. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  839. AV_WL32(ctx->tex_data + 4 * pos, prev);
  840. pos++;
  841. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  842. AV_WL32(ctx->tex_data + 4 * pos, prev);
  843. pos++;
  844. break;
  845. case 2:
  846. /* Copy two dwords from a previous index */
  847. idx = 8 + bytestream2_get_le16(gbc);
  848. if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
  849. return AVERROR_INVALIDDATA;
  850. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  851. AV_WL32(ctx->tex_data + 4 * pos, prev);
  852. pos++;
  853. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  854. AV_WL32(ctx->tex_data + 4 * pos, prev);
  855. pos++;
  856. break;
  857. case 3:
  858. /* Copy two dwords from input */
  859. prev = bytestream2_get_le32(gbc);
  860. AV_WL32(ctx->tex_data + 4 * pos, prev);
  861. pos++;
  862. prev = bytestream2_get_le32(gbc);
  863. AV_WL32(ctx->tex_data + 4 * pos, prev);
  864. pos++;
  865. break;
  866. }
  867. }
  868. CHECKPOINT(4);
  869. if (pos + 2 > ctx->tex_size / 4)
  870. return AVERROR_INVALIDDATA;
  871. /* Copy two elements from a previous offset or from the input buffer */
  872. if (op) {
  873. if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
  874. return AVERROR_INVALIDDATA;
  875. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  876. AV_WL32(ctx->tex_data + 4 * pos, prev);
  877. pos++;
  878. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  879. AV_WL32(ctx->tex_data + 4 * pos, prev);
  880. pos++;
  881. } else {
  882. CHECKPOINT(4);
  883. if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
  884. return AVERROR_INVALIDDATA;
  885. if (op)
  886. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  887. else
  888. prev = bytestream2_get_le32(gbc);
  889. AV_WL32(ctx->tex_data + 4 * pos, prev);
  890. pos++;
  891. CHECKPOINT(4);
  892. if (op)
  893. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  894. else
  895. prev = bytestream2_get_le32(gbc);
  896. AV_WL32(ctx->tex_data + 4 * pos, prev);
  897. pos++;
  898. }
  899. }
  900. return 0;
  901. }
  902. static int dxv_decompress_lzf(AVCodecContext *avctx)
  903. {
  904. DXVContext *ctx = avctx->priv_data;
  905. return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
  906. }
  907. static int dxv_decompress_raw(AVCodecContext *avctx)
  908. {
  909. DXVContext *ctx = avctx->priv_data;
  910. GetByteContext *gbc = &ctx->gbc;
  911. if (bytestream2_get_bytes_left(gbc) < ctx->tex_size)
  912. return AVERROR_INVALIDDATA;
  913. bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
  914. return 0;
  915. }
  916. static int dxv_decode(AVCodecContext *avctx, void *data,
  917. int *got_frame, AVPacket *avpkt)
  918. {
  919. DXVContext *ctx = avctx->priv_data;
  920. ThreadFrame tframe;
  921. GetByteContext *gbc = &ctx->gbc;
  922. int (*decompress_tex)(AVCodecContext *avctx);
  923. const char *msgcomp, *msgtext;
  924. uint32_t tag;
  925. int version_major, version_minor = 0;
  926. int size = 0, old_type = 0;
  927. int ret;
  928. bytestream2_init(gbc, avpkt->data, avpkt->size);
  929. ctx->texture_block_h = 4;
  930. ctx->texture_block_w = 4;
  931. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  932. avctx->colorspace = AVCOL_SPC_RGB;
  933. ctx->tex_funct = NULL;
  934. ctx->tex_funct_planar[0] = NULL;
  935. ctx->tex_funct_planar[1] = NULL;
  936. tag = bytestream2_get_le32(gbc);
  937. switch (tag) {
  938. case MKBETAG('D', 'X', 'T', '1'):
  939. decompress_tex = dxv_decompress_dxt1;
  940. ctx->tex_funct = ctx->texdsp.dxt1_block;
  941. ctx->tex_rat = 8;
  942. ctx->tex_step = 8;
  943. msgcomp = "DXTR1";
  944. msgtext = "DXT1";
  945. break;
  946. case MKBETAG('D', 'X', 'T', '5'):
  947. decompress_tex = dxv_decompress_dxt5;
  948. ctx->tex_funct = ctx->texdsp.dxt5_block;
  949. ctx->tex_rat = 4;
  950. ctx->tex_step = 16;
  951. msgcomp = "DXTR5";
  952. msgtext = "DXT5";
  953. break;
  954. case MKBETAG('Y', 'C', 'G', '6'):
  955. decompress_tex = dxv_decompress_ycg6;
  956. ctx->tex_funct_planar[0] = yo_block;
  957. ctx->tex_funct_planar[1] = cocg_block;
  958. ctx->tex_rat = 8;
  959. ctx->tex_step = 32;
  960. ctx->ctex_step = 16;
  961. msgcomp = "YOCOCG6";
  962. msgtext = "YCG6";
  963. ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
  964. ctx->texture_block_h = 4;
  965. ctx->texture_block_w = 16;
  966. ctx->ctexture_block_h = 4;
  967. ctx->ctexture_block_w = 4;
  968. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  969. avctx->colorspace = AVCOL_SPC_YCOCG;
  970. break;
  971. case MKBETAG('Y', 'G', '1', '0'):
  972. decompress_tex = dxv_decompress_yg10;
  973. ctx->tex_funct_planar[0] = yao_block;
  974. ctx->tex_funct_planar[1] = cocg_block;
  975. ctx->tex_rat = 4;
  976. ctx->tex_step = 64;
  977. ctx->ctex_step = 16;
  978. msgcomp = "YAOCOCG10";
  979. msgtext = "YG10";
  980. ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
  981. ctx->texture_block_h = 4;
  982. ctx->texture_block_w = 16;
  983. ctx->ctexture_block_h = 4;
  984. ctx->ctexture_block_w = 4;
  985. avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  986. avctx->colorspace = AVCOL_SPC_YCOCG;
  987. break;
  988. default:
  989. /* Old version does not have a real header, just size and type. */
  990. size = tag & 0x00FFFFFF;
  991. old_type = tag >> 24;
  992. version_major = (old_type & 0x0F) - 1;
  993. if (old_type & 0x80) {
  994. msgcomp = "RAW";
  995. decompress_tex = dxv_decompress_raw;
  996. } else {
  997. msgcomp = "LZF";
  998. decompress_tex = dxv_decompress_lzf;
  999. }
  1000. if (old_type & 0x40) {
  1001. msgtext = "DXT5";
  1002. ctx->tex_funct = ctx->texdsp.dxt5_block;
  1003. ctx->tex_step = 16;
  1004. } else if (old_type & 0x20 || version_major == 1) {
  1005. msgtext = "DXT1";
  1006. ctx->tex_funct = ctx->texdsp.dxt1_block;
  1007. ctx->tex_step = 8;
  1008. } else {
  1009. av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
  1010. return AVERROR_INVALIDDATA;
  1011. }
  1012. ctx->tex_rat = 1;
  1013. break;
  1014. }
  1015. ctx->slice_count = av_clip(avctx->thread_count, 1,
  1016. avctx->coded_height / FFMAX(ctx->texture_block_h,
  1017. ctx->ctexture_block_h));
  1018. /* New header is 12 bytes long. */
  1019. if (!old_type) {
  1020. version_major = bytestream2_get_byte(gbc) - 1;
  1021. version_minor = bytestream2_get_byte(gbc);
  1022. /* Encoder copies texture data when compression is not advantageous. */
  1023. if (bytestream2_get_byte(gbc)) {
  1024. msgcomp = "RAW";
  1025. ctx->tex_rat = 1;
  1026. decompress_tex = dxv_decompress_raw;
  1027. }
  1028. bytestream2_skip(gbc, 1); // unknown
  1029. size = bytestream2_get_le32(gbc);
  1030. }
  1031. av_log(avctx, AV_LOG_DEBUG,
  1032. "%s compression with %s texture (version %d.%d)\n",
  1033. msgcomp, msgtext, version_major, version_minor);
  1034. if (size != bytestream2_get_bytes_left(gbc)) {
  1035. av_log(avctx, AV_LOG_ERROR,
  1036. "Incomplete or invalid file (header %d, left %u).\n",
  1037. size, bytestream2_get_bytes_left(gbc));
  1038. return AVERROR_INVALIDDATA;
  1039. }
  1040. ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
  1041. ret = av_reallocp(&ctx->tex_data, ctx->tex_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1042. if (ret < 0)
  1043. return ret;
  1044. if (ctx->ctex_size) {
  1045. int i;
  1046. ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
  1047. ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
  1048. ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
  1049. ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
  1050. ret = av_reallocp(&ctx->ctex_data, ctx->ctex_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1051. if (ret < 0)
  1052. return ret;
  1053. for (i = 0; i < 4; i++) {
  1054. ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
  1055. if (ret < 0)
  1056. return ret;
  1057. }
  1058. }
  1059. /* Decompress texture out of the intermediate compression. */
  1060. ret = decompress_tex(avctx);
  1061. if (ret < 0)
  1062. return ret;
  1063. {
  1064. int w_block = avctx->coded_width / ctx->texture_block_w;
  1065. int h_block = avctx->coded_height / ctx->texture_block_h;
  1066. if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
  1067. return AVERROR_INVALIDDATA;
  1068. }
  1069. tframe.f = data;
  1070. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  1071. if (ret < 0)
  1072. return ret;
  1073. /* Now decompress the texture with the standard functions. */
  1074. avctx->execute2(avctx, decompress_texture_thread,
  1075. tframe.f, NULL, ctx->slice_count);
  1076. /* Frame is ready to be output. */
  1077. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  1078. tframe.f->key_frame = 1;
  1079. *got_frame = 1;
  1080. return avpkt->size;
  1081. }
  1082. static int dxv_init(AVCodecContext *avctx)
  1083. {
  1084. DXVContext *ctx = avctx->priv_data;
  1085. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  1086. if (ret < 0) {
  1087. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  1088. avctx->width, avctx->height);
  1089. return ret;
  1090. }
  1091. /* Codec requires 16x16 alignment. */
  1092. avctx->coded_width = FFALIGN(avctx->width, 16);
  1093. avctx->coded_height = FFALIGN(avctx->height, 16);
  1094. ff_texturedsp_init(&ctx->texdsp);
  1095. return 0;
  1096. }
  1097. static int dxv_close(AVCodecContext *avctx)
  1098. {
  1099. DXVContext *ctx = avctx->priv_data;
  1100. av_freep(&ctx->tex_data);
  1101. av_freep(&ctx->ctex_data);
  1102. av_freep(&ctx->op_data[0]);
  1103. av_freep(&ctx->op_data[1]);
  1104. av_freep(&ctx->op_data[2]);
  1105. av_freep(&ctx->op_data[3]);
  1106. return 0;
  1107. }
  1108. AVCodec ff_dxv_decoder = {
  1109. .name = "dxv",
  1110. .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
  1111. .type = AVMEDIA_TYPE_VIDEO,
  1112. .id = AV_CODEC_ID_DXV,
  1113. .init = dxv_init,
  1114. .decode = dxv_decode,
  1115. .close = dxv_close,
  1116. .priv_data_size = sizeof(DXVContext),
  1117. .capabilities = AV_CODEC_CAP_DR1 |
  1118. AV_CODEC_CAP_SLICE_THREADS |
  1119. AV_CODEC_CAP_FRAME_THREADS,
  1120. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  1121. FF_CODEC_CAP_INIT_CLEANUP,
  1122. };