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.

506 lines
17KB

  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 + 2 <= 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 + 2 <= 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 + 4 <= 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. if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
  222. return AVERROR_INVALIDDATA;
  223. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  224. AV_WL32(ctx->tex_data + 4 * pos, prev);
  225. pos++;
  226. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  227. AV_WL32(ctx->tex_data + 4 * pos, prev);
  228. pos++;
  229. break;
  230. case 3:
  231. /* Copy two dwords from input */
  232. prev = bytestream2_get_le32(gbc);
  233. AV_WL32(ctx->tex_data + 4 * pos, prev);
  234. pos++;
  235. prev = bytestream2_get_le32(gbc);
  236. AV_WL32(ctx->tex_data + 4 * pos, prev);
  237. pos++;
  238. break;
  239. }
  240. }
  241. CHECKPOINT(4);
  242. if (pos + 2 > ctx->tex_size / 4)
  243. return AVERROR_INVALIDDATA;
  244. /* Copy two elements from a previous offset or from the input buffer */
  245. if (op) {
  246. if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
  247. return AVERROR_INVALIDDATA;
  248. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  249. AV_WL32(ctx->tex_data + 4 * pos, prev);
  250. pos++;
  251. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  252. AV_WL32(ctx->tex_data + 4 * pos, prev);
  253. pos++;
  254. } else {
  255. CHECKPOINT(4);
  256. if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
  257. return AVERROR_INVALIDDATA;
  258. if (op)
  259. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  260. else
  261. prev = bytestream2_get_le32(gbc);
  262. AV_WL32(ctx->tex_data + 4 * pos, prev);
  263. pos++;
  264. CHECKPOINT(4);
  265. if (op)
  266. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  267. else
  268. prev = bytestream2_get_le32(gbc);
  269. AV_WL32(ctx->tex_data + 4 * pos, prev);
  270. pos++;
  271. }
  272. }
  273. return 0;
  274. }
  275. static int dxv_decompress_lzf(AVCodecContext *avctx)
  276. {
  277. DXVContext *ctx = avctx->priv_data;
  278. return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
  279. }
  280. static int dxv_decompress_raw(AVCodecContext *avctx)
  281. {
  282. DXVContext *ctx = avctx->priv_data;
  283. GetByteContext *gbc = &ctx->gbc;
  284. bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
  285. return 0;
  286. }
  287. static int dxv_decode(AVCodecContext *avctx, void *data,
  288. int *got_frame, AVPacket *avpkt)
  289. {
  290. DXVContext *ctx = avctx->priv_data;
  291. ThreadFrame tframe;
  292. GetByteContext *gbc = &ctx->gbc;
  293. int (*decompress_tex)(AVCodecContext *avctx);
  294. const char *msgcomp, *msgtext;
  295. uint32_t tag;
  296. int version_major, version_minor = 0;
  297. int size = 0, old_type = 0;
  298. int ret;
  299. bytestream2_init(gbc, avpkt->data, avpkt->size);
  300. tag = bytestream2_get_le32(gbc);
  301. switch (tag) {
  302. case MKBETAG('D', 'X', 'T', '1'):
  303. decompress_tex = dxv_decompress_dxt1;
  304. ctx->tex_funct = ctx->texdsp.dxt1_block;
  305. ctx->tex_rat = 8;
  306. ctx->tex_step = 8;
  307. msgcomp = "DXTR1";
  308. msgtext = "DXT1";
  309. break;
  310. case MKBETAG('D', 'X', 'T', '5'):
  311. decompress_tex = dxv_decompress_dxt5;
  312. ctx->tex_funct = ctx->texdsp.dxt5_block;
  313. ctx->tex_rat = 4;
  314. ctx->tex_step = 16;
  315. msgcomp = "DXTR5";
  316. msgtext = "DXT5";
  317. break;
  318. case MKBETAG('Y', 'C', 'G', '6'):
  319. case MKBETAG('Y', 'G', '1', '0'):
  320. avpriv_report_missing_feature(avctx, "Tag 0x%08"PRIX32"", tag);
  321. return AVERROR_PATCHWELCOME;
  322. default:
  323. /* Old version does not have a real header, just size and type. */
  324. size = tag & 0x00FFFFFF;
  325. old_type = tag >> 24;
  326. version_major = (old_type & 0x0F) - 1;
  327. if (old_type & 0x80) {
  328. msgcomp = "RAW";
  329. decompress_tex = dxv_decompress_raw;
  330. } else {
  331. msgcomp = "LZF";
  332. decompress_tex = dxv_decompress_lzf;
  333. }
  334. if (old_type & 0x40) {
  335. msgtext = "DXT5";
  336. ctx->tex_funct = ctx->texdsp.dxt5_block;
  337. ctx->tex_step = 16;
  338. } else if (old_type & 0x20 || version_major == 1) {
  339. msgtext = "DXT1";
  340. ctx->tex_funct = ctx->texdsp.dxt1_block;
  341. ctx->tex_step = 8;
  342. } else {
  343. av_log(avctx, AV_LOG_ERROR,
  344. "Unsupported header (0x%08"PRIX32")\n.", tag);
  345. return AVERROR_INVALIDDATA;
  346. }
  347. ctx->tex_rat = 1;
  348. break;
  349. }
  350. /* New header is 12 bytes long. */
  351. if (!old_type) {
  352. version_major = bytestream2_get_byte(gbc) - 1;
  353. version_minor = bytestream2_get_byte(gbc);
  354. /* Encoder copies texture data when compression is not advantageous. */
  355. if (bytestream2_get_byte(gbc)) {
  356. msgcomp = "RAW";
  357. ctx->tex_rat = 1;
  358. decompress_tex = dxv_decompress_raw;
  359. }
  360. bytestream2_skip(gbc, 1); // unknown
  361. size = bytestream2_get_le32(gbc);
  362. }
  363. av_log(avctx, AV_LOG_DEBUG,
  364. "%s compression with %s texture (version %d.%d)\n",
  365. msgcomp, msgtext, version_major, version_minor);
  366. if (size != bytestream2_get_bytes_left(gbc)) {
  367. av_log(avctx, AV_LOG_ERROR,
  368. "Incomplete or invalid file (header %d, left %u).\n",
  369. size, bytestream2_get_bytes_left(gbc));
  370. return AVERROR_INVALIDDATA;
  371. }
  372. ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
  373. ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
  374. if (ret < 0)
  375. return ret;
  376. /* Decompress texture out of the intermediate compression. */
  377. ret = decompress_tex(avctx);
  378. if (ret < 0)
  379. return ret;
  380. tframe.f = data;
  381. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  382. if (ret < 0)
  383. return ret;
  384. ff_thread_finish_setup(avctx);
  385. /* Now decompress the texture with the standard functions. */
  386. avctx->execute2(avctx, decompress_texture_thread,
  387. tframe.f, NULL, ctx->slice_count);
  388. /* Frame is ready to be output. */
  389. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  390. tframe.f->key_frame = 1;
  391. *got_frame = 1;
  392. return avpkt->size;
  393. }
  394. static int dxv_init(AVCodecContext *avctx)
  395. {
  396. DXVContext *ctx = avctx->priv_data;
  397. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  398. if (ret < 0) {
  399. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  400. avctx->width, avctx->height);
  401. return ret;
  402. }
  403. /* Codec requires 16x16 alignment. */
  404. avctx->coded_width = FFALIGN(avctx->width, 16);
  405. avctx->coded_height = FFALIGN(avctx->height, 16);
  406. ff_texturedsp_init(&ctx->texdsp);
  407. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  408. ctx->slice_count = av_clip(avctx->thread_count, 1,
  409. avctx->coded_height / TEXTURE_BLOCK_H);
  410. return 0;
  411. }
  412. static int dxv_close(AVCodecContext *avctx)
  413. {
  414. DXVContext *ctx = avctx->priv_data;
  415. av_freep(&ctx->tex_data);
  416. return 0;
  417. }
  418. AVCodec ff_dxv_decoder = {
  419. .name = "dxv",
  420. .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
  421. .type = AVMEDIA_TYPE_VIDEO,
  422. .id = AV_CODEC_ID_DXV,
  423. .init = dxv_init,
  424. .decode = dxv_decode,
  425. .close = dxv_close,
  426. .priv_data_size = sizeof(DXVContext),
  427. .capabilities = AV_CODEC_CAP_DR1 |
  428. AV_CODEC_CAP_SLICE_THREADS |
  429. AV_CODEC_CAP_FRAME_THREADS,
  430. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  431. FF_CODEC_CAP_INIT_CLEANUP,
  432. };