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.

504 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. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  230. AV_WL32(ctx->tex_data + 4 * pos, prev);
  231. pos++;
  232. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  233. AV_WL32(ctx->tex_data + 4 * pos, prev);
  234. pos++;
  235. break;
  236. case 3:
  237. /* Copy two dwords from input */
  238. prev = bytestream2_get_le32(gbc);
  239. AV_WL32(ctx->tex_data + 4 * pos, prev);
  240. pos++;
  241. prev = bytestream2_get_le32(gbc);
  242. AV_WL32(ctx->tex_data + 4 * pos, prev);
  243. pos++;
  244. break;
  245. }
  246. }
  247. CHECKPOINT(4);
  248. /* Copy two elements from a previous offset or from the input buffer */
  249. if (op) {
  250. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  251. AV_WL32(ctx->tex_data + 4 * pos, prev);
  252. pos++;
  253. prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
  254. AV_WL32(ctx->tex_data + 4 * pos, prev);
  255. pos++;
  256. } else {
  257. CHECKPOINT(4);
  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%08X", 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, "Unsupported header (0x%08X)\n.", tag);
  344. return AVERROR_INVALIDDATA;
  345. }
  346. ctx->tex_rat = 1;
  347. break;
  348. }
  349. /* New header is 12 bytes long. */
  350. if (!old_type) {
  351. version_major = bytestream2_get_byte(gbc) - 1;
  352. version_minor = bytestream2_get_byte(gbc);
  353. /* Encoder copies texture data when compression is not advantageous. */
  354. if (bytestream2_get_byte(gbc)) {
  355. msgcomp = "RAW";
  356. ctx->tex_rat = 1;
  357. decompress_tex = dxv_decompress_raw;
  358. }
  359. bytestream2_skip(gbc, 1); // unknown
  360. size = bytestream2_get_le32(gbc);
  361. }
  362. av_log(avctx, AV_LOG_DEBUG,
  363. "%s compression with %s texture (version %d.%d)\n",
  364. msgcomp, msgtext, version_major, version_minor);
  365. if (size != bytestream2_get_bytes_left(gbc)) {
  366. av_log(avctx, AV_LOG_ERROR,
  367. "Incomplete or invalid file (header %d, left %d).\n",
  368. size, bytestream2_get_bytes_left(gbc));
  369. return AVERROR_INVALIDDATA;
  370. }
  371. ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
  372. ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
  373. if (ret < 0)
  374. return ret;
  375. /* Decompress texture out of the intermediate compression. */
  376. ret = decompress_tex(avctx);
  377. if (ret < 0)
  378. return ret;
  379. tframe.f = data;
  380. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  381. if (ret < 0)
  382. return ret;
  383. /* Now decompress the texture with the standard functions. */
  384. avctx->execute2(avctx, decompress_texture_thread,
  385. tframe.f, NULL, ctx->slice_count);
  386. /* Frame is ready to be output. */
  387. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  388. tframe.f->key_frame = 1;
  389. *got_frame = 1;
  390. return avpkt->size;
  391. }
  392. static int dxv_init(AVCodecContext *avctx)
  393. {
  394. DXVContext *ctx = avctx->priv_data;
  395. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  396. if (ret < 0) {
  397. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  398. avctx->width, avctx->height);
  399. return ret;
  400. }
  401. /* Codec requires 16x16 alignment. */
  402. avctx->coded_width = FFALIGN(avctx->width, 16);
  403. avctx->coded_height = FFALIGN(avctx->height, 16);
  404. ff_texturedsp_init(&ctx->texdsp);
  405. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  406. ctx->slice_count = av_clip(avctx->thread_count, 1,
  407. avctx->coded_height / TEXTURE_BLOCK_H);
  408. return 0;
  409. }
  410. static int dxv_close(AVCodecContext *avctx)
  411. {
  412. DXVContext *ctx = avctx->priv_data;
  413. av_freep(&ctx->tex_data);
  414. return 0;
  415. }
  416. AVCodec ff_dxv_decoder = {
  417. .name = "dxv",
  418. .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
  419. .type = AVMEDIA_TYPE_VIDEO,
  420. .id = AV_CODEC_ID_DXV,
  421. .init = dxv_init,
  422. .decode = dxv_decode,
  423. .close = dxv_close,
  424. .priv_data_size = sizeof(DXVContext),
  425. .capabilities = AV_CODEC_CAP_DR1 |
  426. AV_CODEC_CAP_SLICE_THREADS |
  427. AV_CODEC_CAP_FRAME_THREADS,
  428. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  429. FF_CODEC_CAP_INIT_CLEANUP,
  430. };