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.

497 lines
16KB

  1. /*
  2. * Resolume DXV decoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. #include "lzf.h"
  27. #include "texturedsp.h"
  28. #include "thread.h"
  29. typedef struct DXVContext {
  30. TextureDSPContext texdsp;
  31. GetByteContext gbc;
  32. uint8_t *tex_data; // Compressed texture
  33. int tex_rat; // Compression ratio
  34. int tex_step; // Distance between blocks
  35. int64_t tex_size; // Texture size
  36. /* Optimal number of slices for parallel decoding */
  37. int slice_count;
  38. /* Pointer to the selected decompression function */
  39. int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
  40. } DXVContext;
  41. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  42. int slice, int thread_nb)
  43. {
  44. DXVContext *ctx = avctx->priv_data;
  45. AVFrame *frame = arg;
  46. const uint8_t *d = ctx->tex_data;
  47. int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
  48. int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
  49. int x, y;
  50. int start_slice, end_slice;
  51. int base_blocks_per_slice = h_block / ctx->slice_count;
  52. int remainder_blocks = h_block % ctx->slice_count;
  53. /* When the frame height (in blocks) doesn't divide evenly between the
  54. * number of slices, spread the remaining blocks evenly between the first
  55. * operations */
  56. start_slice = slice * base_blocks_per_slice;
  57. /* Add any extra blocks (one per slice) that have been added
  58. * before this slice */
  59. start_slice += FFMIN(slice, remainder_blocks);
  60. end_slice = start_slice + base_blocks_per_slice;
  61. /* Add an extra block if there are remainder blocks to be accounted for */
  62. if (slice < remainder_blocks)
  63. end_slice++;
  64. for (y = start_slice; y < end_slice; y++) {
  65. uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
  66. int off = y * w_block;
  67. for (x = 0; x < w_block; x++) {
  68. ctx->tex_funct(p + x * 16, frame->linesize[0],
  69. d + (off + x) * ctx->tex_step);
  70. }
  71. }
  72. return 0;
  73. }
  74. /* This scheme addresses already decoded elements depending on 2-bit status:
  75. * 0 -> copy new element
  76. * 1 -> copy one element from position -x
  77. * 2 -> copy one element from position -(get_byte() + 2) * x
  78. * 3 -> copy one element from position -(get_16le() + 0x102) * x
  79. * x is always 2 for dxt1 and 4 for dxt5. */
  80. #define CHECKPOINT(x) \
  81. do { \
  82. if (state == 0) { \
  83. value = bytestream2_get_le32(gbc); \
  84. state = 16; \
  85. } \
  86. op = value & 0x3; \
  87. value >>= 2; \
  88. state--; \
  89. switch (op) { \
  90. case 1: \
  91. idx = x; \
  92. break; \
  93. case 2: \
  94. idx = (bytestream2_get_byte(gbc) + 2) * x; \
  95. break; \
  96. case 3: \
  97. idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
  98. break; \
  99. } \
  100. } while(0)
  101. static int dxv_decompress_dxt1(AVCodecContext *avctx)
  102. {
  103. DXVContext *ctx = avctx->priv_data;
  104. GetByteContext *gbc = &ctx->gbc;
  105. uint32_t value, prev, op;
  106. int idx = 0, state = 0;
  107. int pos = 2;
  108. /* Copy the first two elements */
  109. AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
  110. AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
  111. /* Process input until the whole texture has been filled */
  112. while (pos < ctx->tex_size / 4) {
  113. CHECKPOINT(2);
  114. /* Copy two elements from a previous offset or from the input buffer */
  115. if (op) {
  116. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  117. AV_WL32(ctx->tex_data + 4 * pos, prev);
  118. pos++;
  119. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  120. AV_WL32(ctx->tex_data + 4 * pos, prev);
  121. pos++;
  122. } else {
  123. CHECKPOINT(2);
  124. if (op)
  125. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  126. else
  127. prev = bytestream2_get_le32(gbc);
  128. AV_WL32(ctx->tex_data + 4 * pos, prev);
  129. pos++;
  130. CHECKPOINT(2);
  131. if (op)
  132. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  133. else
  134. prev = bytestream2_get_le32(gbc);
  135. AV_WL32(ctx->tex_data + 4 * pos, prev);
  136. pos++;
  137. }
  138. }
  139. return 0;
  140. }
  141. static int dxv_decompress_dxt5(AVCodecContext *avctx)
  142. {
  143. DXVContext *ctx = avctx->priv_data;
  144. GetByteContext *gbc = &ctx->gbc;
  145. uint32_t value, op;
  146. int idx, prev, state = 0;
  147. int pos = 4;
  148. int run = 0;
  149. int probe, check;
  150. /* Copy the first four elements */
  151. AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
  152. AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
  153. AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
  154. AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
  155. /* Process input until the whole texture has been filled */
  156. while (pos < ctx->tex_size / 4) {
  157. if (run) {
  158. run--;
  159. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  160. AV_WL32(ctx->tex_data + 4 * pos, prev);
  161. pos++;
  162. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  163. AV_WL32(ctx->tex_data + 4 * pos, prev);
  164. pos++;
  165. } else {
  166. if (state == 0) {
  167. value = bytestream2_get_le32(gbc);
  168. state = 16;
  169. }
  170. op = value & 0x3;
  171. value >>= 2;
  172. state--;
  173. switch (op) {
  174. case 0:
  175. /* Long copy */
  176. check = bytestream2_get_byte(gbc) + 1;
  177. if (check == 256) {
  178. do {
  179. probe = bytestream2_get_le16(gbc);
  180. check += probe;
  181. } while (probe == 0xFFFF);
  182. }
  183. while (check && pos < ctx->tex_size / 4) {
  184. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  185. AV_WL32(ctx->tex_data + 4 * pos, prev);
  186. pos++;
  187. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  188. AV_WL32(ctx->tex_data + 4 * pos, prev);
  189. pos++;
  190. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  191. AV_WL32(ctx->tex_data + 4 * pos, prev);
  192. pos++;
  193. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  194. AV_WL32(ctx->tex_data + 4 * pos, prev);
  195. pos++;
  196. check--;
  197. }
  198. /* Restart (or exit) the loop */
  199. continue;
  200. break;
  201. case 1:
  202. /* Load new run value */
  203. run = bytestream2_get_byte(gbc);
  204. if (run == 255) {
  205. do {
  206. probe = bytestream2_get_le16(gbc);
  207. run += probe;
  208. } while (probe == 0xFFFF);
  209. }
  210. /* Copy two dwords from previous data */
  211. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  212. AV_WL32(ctx->tex_data + 4 * pos, prev);
  213. pos++;
  214. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  215. AV_WL32(ctx->tex_data + 4 * pos, prev);
  216. pos++;
  217. break;
  218. case 2:
  219. /* Copy two dwords from a previous index */
  220. idx = 8 + bytestream2_get_le16(gbc);
  221. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  222. AV_WL32(ctx->tex_data + 4 * pos, prev);
  223. pos++;
  224. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  225. AV_WL32(ctx->tex_data + 4 * pos, prev);
  226. pos++;
  227. break;
  228. case 3:
  229. /* Copy two dwords from input */
  230. prev = bytestream2_get_le32(gbc);
  231. AV_WL32(ctx->tex_data + 4 * pos, prev);
  232. pos++;
  233. prev = bytestream2_get_le32(gbc);
  234. AV_WL32(ctx->tex_data + 4 * pos, prev);
  235. pos++;
  236. break;
  237. }
  238. }
  239. CHECKPOINT(4);
  240. /* Copy two elements from a previous offset or from the input buffer */
  241. if (op) {
  242. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  243. AV_WL32(ctx->tex_data + 4 * pos, prev);
  244. pos++;
  245. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  246. AV_WL32(ctx->tex_data + 4 * pos, prev);
  247. pos++;
  248. } else {
  249. CHECKPOINT(4);
  250. if (op)
  251. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  252. else
  253. prev = bytestream2_get_le32(gbc);
  254. AV_WL32(ctx->tex_data + 4 * pos, prev);
  255. pos++;
  256. CHECKPOINT(4);
  257. if (op)
  258. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  259. else
  260. prev = bytestream2_get_le32(gbc);
  261. AV_WL32(ctx->tex_data + 4 * pos, prev);
  262. pos++;
  263. }
  264. }
  265. return 0;
  266. }
  267. static int dxv_decompress_lzf(AVCodecContext *avctx)
  268. {
  269. DXVContext *ctx = avctx->priv_data;
  270. return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
  271. }
  272. static int dxv_decompress_raw(AVCodecContext *avctx)
  273. {
  274. DXVContext *ctx = avctx->priv_data;
  275. GetByteContext *gbc = &ctx->gbc;
  276. bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
  277. return 0;
  278. }
  279. static int dxv_decode(AVCodecContext *avctx, void *data,
  280. int *got_frame, AVPacket *avpkt)
  281. {
  282. DXVContext *ctx = avctx->priv_data;
  283. ThreadFrame tframe;
  284. GetByteContext *gbc = &ctx->gbc;
  285. int (*decompress_tex)(AVCodecContext *avctx);
  286. const char *msgcomp, *msgtext;
  287. uint32_t tag;
  288. int version_major, version_minor = 0;
  289. int size = 0, old_type = 0;
  290. int ret;
  291. bytestream2_init(gbc, avpkt->data, avpkt->size);
  292. tag = bytestream2_get_le32(gbc);
  293. switch (tag) {
  294. case MKBETAG('D', 'X', 'T', '1'):
  295. decompress_tex = dxv_decompress_dxt1;
  296. ctx->tex_funct = ctx->texdsp.dxt1_block;
  297. ctx->tex_rat = 8;
  298. ctx->tex_step = 8;
  299. msgcomp = "DXTR1";
  300. msgtext = "DXT1";
  301. break;
  302. case MKBETAG('D', 'X', 'T', '5'):
  303. decompress_tex = dxv_decompress_dxt5;
  304. ctx->tex_funct = ctx->texdsp.dxt5_block;
  305. ctx->tex_rat = 4;
  306. ctx->tex_step = 16;
  307. msgcomp = "DXTR5";
  308. msgtext = "DXT5";
  309. break;
  310. case MKBETAG('Y', 'C', 'G', '6'):
  311. case MKBETAG('Y', 'G', '1', '0'):
  312. avpriv_report_missing_feature(avctx, "Tag 0x%08X", tag);
  313. return AVERROR_PATCHWELCOME;
  314. default:
  315. /* Old version does not have a real header, just size and type. */
  316. size = tag & 0x00FFFFFF;
  317. old_type = tag >> 24;
  318. version_major = (old_type & 0x0F) - 1;
  319. if (old_type & 0x80) {
  320. msgcomp = "RAW";
  321. decompress_tex = dxv_decompress_raw;
  322. } else {
  323. msgcomp = "LZF";
  324. decompress_tex = dxv_decompress_lzf;
  325. }
  326. if (old_type & 0x40) {
  327. msgtext = "DXT5";
  328. ctx->tex_funct = ctx->texdsp.dxt5_block;
  329. ctx->tex_step = 16;
  330. } else if (old_type & 0x20 || version_major == 1) {
  331. msgtext = "DXT1";
  332. ctx->tex_funct = ctx->texdsp.dxt1_block;
  333. ctx->tex_step = 8;
  334. } else {
  335. av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08X)\n.", tag);
  336. return AVERROR_INVALIDDATA;
  337. }
  338. ctx->tex_rat = 1;
  339. break;
  340. }
  341. /* New header is 12 bytes long. */
  342. if (!old_type) {
  343. version_major = bytestream2_get_byte(gbc) - 1;
  344. version_minor = bytestream2_get_byte(gbc);
  345. /* Encoder copies texture data when compression is not advantageous. */
  346. if (bytestream2_get_byte(gbc)) {
  347. msgcomp = "RAW";
  348. ctx->tex_rat = 1;
  349. decompress_tex = dxv_decompress_raw;
  350. }
  351. bytestream2_skip(gbc, 1); // unknown
  352. size = bytestream2_get_le32(gbc);
  353. }
  354. av_log(avctx, AV_LOG_DEBUG,
  355. "%s compression with %s texture (version %d.%d)\n",
  356. msgcomp, msgtext, version_major, version_minor);
  357. if (size != bytestream2_get_bytes_left(gbc)) {
  358. av_log(avctx, AV_LOG_ERROR,
  359. "Incomplete or invalid file (header %d, left %d).\n",
  360. size, bytestream2_get_bytes_left(gbc));
  361. return AVERROR_INVALIDDATA;
  362. }
  363. ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
  364. ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
  365. if (ret < 0)
  366. return ret;
  367. /* Decompress texture out of the intermediate compression. */
  368. ret = decompress_tex(avctx);
  369. if (ret < 0)
  370. return ret;
  371. tframe.f = data;
  372. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  373. if (ret < 0)
  374. return ret;
  375. ff_thread_finish_setup(avctx);
  376. /* Now decompress the texture with the standard functions. */
  377. avctx->execute2(avctx, decompress_texture_thread,
  378. tframe.f, NULL, ctx->slice_count);
  379. /* Frame is ready to be output. */
  380. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  381. tframe.f->key_frame = 1;
  382. *got_frame = 1;
  383. return avpkt->size;
  384. }
  385. static int dxv_init(AVCodecContext *avctx)
  386. {
  387. DXVContext *ctx = avctx->priv_data;
  388. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  389. if (ret < 0) {
  390. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  391. avctx->width, avctx->height);
  392. return ret;
  393. }
  394. /* Codec requires 16x16 alignment. */
  395. avctx->coded_width = FFALIGN(avctx->width, 16);
  396. avctx->coded_height = FFALIGN(avctx->height, 16);
  397. ff_texturedsp_init(&ctx->texdsp);
  398. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  399. ctx->slice_count = av_clip(avctx->thread_count, 1,
  400. avctx->coded_height / TEXTURE_BLOCK_H);
  401. return 0;
  402. }
  403. static int dxv_close(AVCodecContext *avctx)
  404. {
  405. DXVContext *ctx = avctx->priv_data;
  406. av_freep(&ctx->tex_data);
  407. return 0;
  408. }
  409. AVCodec ff_dxv_decoder = {
  410. .name = "dxv",
  411. .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
  412. .type = AVMEDIA_TYPE_VIDEO,
  413. .id = AV_CODEC_ID_DXV,
  414. .init = dxv_init,
  415. .decode = dxv_decode,
  416. .close = dxv_close,
  417. .priv_data_size = sizeof(DXVContext),
  418. .capabilities = AV_CODEC_CAP_DR1 |
  419. AV_CODEC_CAP_SLICE_THREADS |
  420. AV_CODEC_CAP_FRAME_THREADS,
  421. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  422. FF_CODEC_CAP_INIT_CLEANUP,
  423. };