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.

517 lines
18KB

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