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.

137 lines
4.2KB

  1. /*
  2. * Renderware TeXture Dictionary (.txd) image decoder
  3. * Copyright (c) 2007 Ivo van Poorten
  4. *
  5. * See also: http://wiki.multimedia.cx/index.php?title=TXD
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. #include "s3tc.h"
  29. static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  30. AVPacket *avpkt) {
  31. GetByteContext gb;
  32. AVFrame * const p = data;
  33. unsigned int version, w, h, d3d_format, depth, stride, flags;
  34. unsigned int y, v;
  35. uint8_t *ptr;
  36. uint32_t *pal;
  37. int ret;
  38. bytestream2_init(&gb, avpkt->data, avpkt->size);
  39. version = bytestream2_get_le32(&gb);
  40. bytestream2_skip(&gb, 72);
  41. d3d_format = bytestream2_get_le32(&gb);
  42. w = bytestream2_get_le16(&gb);
  43. h = bytestream2_get_le16(&gb);
  44. depth = bytestream2_get_byte(&gb);
  45. bytestream2_skip(&gb, 2);
  46. flags = bytestream2_get_byte(&gb);
  47. if (version < 8 || version > 9) {
  48. av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n",
  49. version);
  50. return AVERROR_PATCHWELCOME;
  51. }
  52. if (depth == 8) {
  53. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  54. } else if (depth == 16 || depth == 32) {
  55. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  56. } else {
  57. av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth);
  58. return AVERROR_PATCHWELCOME;
  59. }
  60. if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
  61. return ret;
  62. if (w != avctx->width || h != avctx->height)
  63. avcodec_set_dimensions(avctx, w, h);
  64. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  65. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  66. return ret;
  67. }
  68. p->pict_type = AV_PICTURE_TYPE_I;
  69. ptr = p->data[0];
  70. stride = p->linesize[0];
  71. if (depth == 8) {
  72. pal = (uint32_t *) p->data[1];
  73. for (y = 0; y < 256; y++) {
  74. v = bytestream2_get_be32(&gb);
  75. pal[y] = (v >> 8) + (v << 24);
  76. }
  77. bytestream2_skip(&gb, 4);
  78. for (y=0; y<h; y++) {
  79. bytestream2_get_buffer(&gb, ptr, w);
  80. ptr += stride;
  81. }
  82. } else if (depth == 16) {
  83. bytestream2_skip(&gb, 4);
  84. switch (d3d_format) {
  85. case 0:
  86. if (!(flags & 1))
  87. goto unsupported;
  88. case FF_S3TC_DXT1:
  89. ff_decode_dxt1(&gb, ptr, w, h, stride);
  90. break;
  91. case FF_S3TC_DXT3:
  92. ff_decode_dxt3(&gb, ptr, w, h, stride);
  93. break;
  94. default:
  95. goto unsupported;
  96. }
  97. } else if (depth == 32) {
  98. switch (d3d_format) {
  99. case 0x15:
  100. case 0x16:
  101. for (y=0; y<h; y++) {
  102. bytestream2_get_buffer(&gb, ptr, w * 4);
  103. ptr += stride;
  104. }
  105. break;
  106. default:
  107. goto unsupported;
  108. }
  109. }
  110. *got_frame = 1;
  111. return avpkt->size;
  112. unsupported:
  113. av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
  114. return AVERROR_PATCHWELCOME;
  115. }
  116. AVCodec ff_txd_decoder = {
  117. .name = "txd",
  118. .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
  119. .type = AVMEDIA_TYPE_VIDEO,
  120. .id = AV_CODEC_ID_TXD,
  121. .decode = txd_decode_frame,
  122. .capabilities = CODEC_CAP_DR1,
  123. };