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.

508 lines
17KB

  1. /*
  2. * Resolume DXV decoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (idx > pos) { \
  96. av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
  97. return AVERROR_INVALIDDATA; \
  98. } \
  99. break; \
  100. case 3: \
  101. idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
  102. if (idx > pos) { \
  103. av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
  104. return AVERROR_INVALIDDATA; \
  105. } \
  106. break; \
  107. } \
  108. } while(0)
  109. static int dxv_decompress_dxt1(AVCodecContext *avctx)
  110. {
  111. DXVContext *ctx = avctx->priv_data;
  112. GetByteContext *gbc = &ctx->gbc;
  113. uint32_t value, prev, op;
  114. int idx = 0, state = 0;
  115. int pos = 2;
  116. /* Copy the first two elements */
  117. AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
  118. AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
  119. /* Process input until the whole texture has been filled */
  120. while (pos < ctx->tex_size / 4) {
  121. CHECKPOINT(2);
  122. /* Copy two elements from a previous offset or from the input buffer */
  123. if (op) {
  124. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  125. AV_WL32(ctx->tex_data + 4 * pos, prev);
  126. pos++;
  127. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  128. AV_WL32(ctx->tex_data + 4 * pos, prev);
  129. pos++;
  130. } else {
  131. CHECKPOINT(2);
  132. if (op)
  133. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  134. else
  135. prev = bytestream2_get_le32(gbc);
  136. AV_WL32(ctx->tex_data + 4 * pos, prev);
  137. pos++;
  138. CHECKPOINT(2);
  139. if (op)
  140. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  141. else
  142. prev = bytestream2_get_le32(gbc);
  143. AV_WL32(ctx->tex_data + 4 * pos, prev);
  144. pos++;
  145. }
  146. }
  147. return 0;
  148. }
  149. static int dxv_decompress_dxt5(AVCodecContext *avctx)
  150. {
  151. DXVContext *ctx = avctx->priv_data;
  152. GetByteContext *gbc = &ctx->gbc;
  153. uint32_t value, op;
  154. int idx, prev, state = 0;
  155. int pos = 4;
  156. int run = 0;
  157. int probe, check;
  158. /* Copy the first four elements */
  159. AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
  160. AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
  161. AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
  162. AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
  163. /* Process input until the whole texture has been filled */
  164. while (pos < ctx->tex_size / 4) {
  165. if (run) {
  166. run--;
  167. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  168. AV_WL32(ctx->tex_data + 4 * pos, prev);
  169. pos++;
  170. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  171. AV_WL32(ctx->tex_data + 4 * pos, prev);
  172. pos++;
  173. } else {
  174. if (state == 0) {
  175. value = bytestream2_get_le32(gbc);
  176. state = 16;
  177. }
  178. op = value & 0x3;
  179. value >>= 2;
  180. state--;
  181. switch (op) {
  182. case 0:
  183. /* Long copy */
  184. check = bytestream2_get_byte(gbc) + 1;
  185. if (check == 256) {
  186. do {
  187. probe = bytestream2_get_le16(gbc);
  188. check += probe;
  189. } while (probe == 0xFFFF);
  190. }
  191. while (check && pos < ctx->tex_size / 4) {
  192. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  193. AV_WL32(ctx->tex_data + 4 * pos, prev);
  194. pos++;
  195. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  196. AV_WL32(ctx->tex_data + 4 * pos, prev);
  197. pos++;
  198. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  199. AV_WL32(ctx->tex_data + 4 * pos, prev);
  200. pos++;
  201. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  202. AV_WL32(ctx->tex_data + 4 * pos, prev);
  203. pos++;
  204. check--;
  205. }
  206. /* Restart (or exit) the loop */
  207. continue;
  208. break;
  209. case 1:
  210. /* Load new run value */
  211. run = bytestream2_get_byte(gbc);
  212. if (run == 255) {
  213. do {
  214. probe = bytestream2_get_le16(gbc);
  215. run += probe;
  216. } while (probe == 0xFFFF);
  217. }
  218. /* Copy two dwords from previous data */
  219. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  220. AV_WL32(ctx->tex_data + 4 * pos, prev);
  221. pos++;
  222. prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
  223. AV_WL32(ctx->tex_data + 4 * pos, prev);
  224. pos++;
  225. break;
  226. case 2:
  227. /* Copy two dwords from a previous index */
  228. idx = 8 + bytestream2_get_le16(gbc);
  229. if (idx > pos) {
  230. av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos);
  231. return AVERROR_INVALIDDATA;
  232. }
  233. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  234. AV_WL32(ctx->tex_data + 4 * pos, prev);
  235. pos++;
  236. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  237. AV_WL32(ctx->tex_data + 4 * pos, prev);
  238. pos++;
  239. break;
  240. case 3:
  241. /* Copy two dwords from input */
  242. prev = bytestream2_get_le32(gbc);
  243. AV_WL32(ctx->tex_data + 4 * pos, prev);
  244. pos++;
  245. prev = bytestream2_get_le32(gbc);
  246. AV_WL32(ctx->tex_data + 4 * pos, prev);
  247. pos++;
  248. break;
  249. }
  250. }
  251. CHECKPOINT(4);
  252. /* Copy two elements from a previous offset or from the input buffer */
  253. if (op) {
  254. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  255. AV_WL32(ctx->tex_data + 4 * pos, prev);
  256. pos++;
  257. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  258. AV_WL32(ctx->tex_data + 4 * pos, prev);
  259. pos++;
  260. } else {
  261. CHECKPOINT(4);
  262. if (op)
  263. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  264. else
  265. prev = bytestream2_get_le32(gbc);
  266. AV_WL32(ctx->tex_data + 4 * pos, prev);
  267. pos++;
  268. CHECKPOINT(4);
  269. if (op)
  270. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  271. else
  272. prev = bytestream2_get_le32(gbc);
  273. AV_WL32(ctx->tex_data + 4 * pos, prev);
  274. pos++;
  275. }
  276. }
  277. return 0;
  278. }
  279. static int dxv_decompress_lzf(AVCodecContext *avctx)
  280. {
  281. DXVContext *ctx = avctx->priv_data;
  282. return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
  283. }
  284. static int dxv_decompress_raw(AVCodecContext *avctx)
  285. {
  286. DXVContext *ctx = avctx->priv_data;
  287. GetByteContext *gbc = &ctx->gbc;
  288. bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
  289. return 0;
  290. }
  291. static int dxv_decode(AVCodecContext *avctx, void *data,
  292. int *got_frame, AVPacket *avpkt)
  293. {
  294. DXVContext *ctx = avctx->priv_data;
  295. ThreadFrame tframe;
  296. GetByteContext *gbc = &ctx->gbc;
  297. int (*decompress_tex)(AVCodecContext *avctx);
  298. const char *msgcomp, *msgtext;
  299. uint32_t tag;
  300. int version_major, version_minor = 0;
  301. int size = 0, old_type = 0;
  302. int ret;
  303. bytestream2_init(gbc, avpkt->data, avpkt->size);
  304. tag = bytestream2_get_le32(gbc);
  305. switch (tag) {
  306. case MKBETAG('D', 'X', 'T', '1'):
  307. decompress_tex = dxv_decompress_dxt1;
  308. ctx->tex_funct = ctx->texdsp.dxt1_block;
  309. ctx->tex_rat = 8;
  310. ctx->tex_step = 8;
  311. msgcomp = "DXTR1";
  312. msgtext = "DXT1";
  313. break;
  314. case MKBETAG('D', 'X', 'T', '5'):
  315. decompress_tex = dxv_decompress_dxt5;
  316. ctx->tex_funct = ctx->texdsp.dxt5_block;
  317. ctx->tex_rat = 4;
  318. ctx->tex_step = 16;
  319. msgcomp = "DXTR5";
  320. msgtext = "DXT5";
  321. break;
  322. case MKBETAG('Y', 'C', 'G', '6'):
  323. case MKBETAG('Y', 'G', '1', '0'):
  324. avpriv_report_missing_feature(avctx, "Tag 0x%08X", tag);
  325. return AVERROR_PATCHWELCOME;
  326. default:
  327. /* Old version does not have a real header, just size and type. */
  328. size = tag & 0x00FFFFFF;
  329. old_type = tag >> 24;
  330. version_major = (old_type & 0x0F) - 1;
  331. if (old_type & 0x80) {
  332. msgcomp = "RAW";
  333. decompress_tex = dxv_decompress_raw;
  334. } else {
  335. msgcomp = "LZF";
  336. decompress_tex = dxv_decompress_lzf;
  337. }
  338. if (old_type & 0x40) {
  339. msgtext = "DXT5";
  340. ctx->tex_funct = ctx->texdsp.dxt5_block;
  341. ctx->tex_step = 16;
  342. } else if (old_type & 0x20 || version_major == 1) {
  343. msgtext = "DXT1";
  344. ctx->tex_funct = ctx->texdsp.dxt1_block;
  345. ctx->tex_step = 8;
  346. } else {
  347. av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08X)\n.", tag);
  348. return AVERROR_INVALIDDATA;
  349. }
  350. ctx->tex_rat = 1;
  351. break;
  352. }
  353. /* New header is 12 bytes long. */
  354. if (!old_type) {
  355. version_major = bytestream2_get_byte(gbc) - 1;
  356. version_minor = bytestream2_get_byte(gbc);
  357. /* Encoder copies texture data when compression is not advantageous. */
  358. if (bytestream2_get_byte(gbc)) {
  359. msgcomp = "RAW";
  360. ctx->tex_rat = 1;
  361. decompress_tex = dxv_decompress_raw;
  362. }
  363. bytestream2_skip(gbc, 1); // unknown
  364. size = bytestream2_get_le32(gbc);
  365. }
  366. av_log(avctx, AV_LOG_DEBUG,
  367. "%s compression with %s texture (version %d.%d)\n",
  368. msgcomp, msgtext, version_major, version_minor);
  369. if (size != bytestream2_get_bytes_left(gbc)) {
  370. av_log(avctx, AV_LOG_ERROR,
  371. "Incomplete or invalid file (header %d, left %d).\n",
  372. size, bytestream2_get_bytes_left(gbc));
  373. return AVERROR_INVALIDDATA;
  374. }
  375. ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
  376. ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
  377. if (ret < 0)
  378. return ret;
  379. /* Decompress texture out of the intermediate compression. */
  380. ret = decompress_tex(avctx);
  381. if (ret < 0)
  382. return ret;
  383. tframe.f = data;
  384. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  385. if (ret < 0)
  386. return ret;
  387. /* Now decompress the texture with the standard functions. */
  388. avctx->execute2(avctx, decompress_texture_thread,
  389. tframe.f, NULL, ctx->slice_count);
  390. /* Frame is ready to be output. */
  391. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  392. tframe.f->key_frame = 1;
  393. *got_frame = 1;
  394. return avpkt->size;
  395. }
  396. static int dxv_init(AVCodecContext *avctx)
  397. {
  398. DXVContext *ctx = avctx->priv_data;
  399. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  400. if (ret < 0) {
  401. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  402. avctx->width, avctx->height);
  403. return ret;
  404. }
  405. /* Codec requires 16x16 alignment. */
  406. avctx->coded_width = FFALIGN(avctx->width, 16);
  407. avctx->coded_height = FFALIGN(avctx->height, 16);
  408. ff_texturedsp_init(&ctx->texdsp);
  409. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  410. ctx->slice_count = av_clip(avctx->thread_count, 1,
  411. avctx->coded_height / TEXTURE_BLOCK_H);
  412. return 0;
  413. }
  414. static int dxv_close(AVCodecContext *avctx)
  415. {
  416. DXVContext *ctx = avctx->priv_data;
  417. av_freep(&ctx->tex_data);
  418. return 0;
  419. }
  420. AVCodec ff_dxv_decoder = {
  421. .name = "dxv",
  422. .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
  423. .type = AVMEDIA_TYPE_VIDEO,
  424. .id = AV_CODEC_ID_DXV,
  425. .init = dxv_init,
  426. .decode = dxv_decode,
  427. .close = dxv_close,
  428. .priv_data_size = sizeof(DXVContext),
  429. .capabilities = AV_CODEC_CAP_DR1 |
  430. AV_CODEC_CAP_SLICE_THREADS |
  431. AV_CODEC_CAP_FRAME_THREADS,
  432. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  433. FF_CODEC_CAP_INIT_CLEANUP,
  434. };