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.

170 lines
4.6KB

  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 FFmpeg.
  8. *
  9. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "avcodec.h"
  25. #include "s3tc.h"
  26. typedef struct TXDContext {
  27. AVFrame picture;
  28. } TXDContext;
  29. static av_cold int txd_init(AVCodecContext *avctx) {
  30. TXDContext *s = avctx->priv_data;
  31. avcodec_get_frame_defaults(&s->picture);
  32. avctx->coded_frame = &s->picture;
  33. return 0;
  34. }
  35. static int txd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  36. AVPacket *avpkt) {
  37. const uint8_t *buf = avpkt->data;
  38. TXDContext * const s = avctx->priv_data;
  39. AVFrame *picture = data;
  40. AVFrame * const p = &s->picture;
  41. unsigned int version, w, h, d3d_format, depth, stride, mipmap_count, flags;
  42. unsigned int y, v;
  43. uint8_t *ptr;
  44. const uint8_t *cur = buf;
  45. const uint32_t *palette = (const uint32_t *)(cur + 88);
  46. uint32_t *pal;
  47. version = AV_RL32(cur);
  48. d3d_format = AV_RL32(cur+76);
  49. w = AV_RL16(cur+80);
  50. h = AV_RL16(cur+82);
  51. depth = AV_RL8 (cur+84);
  52. mipmap_count = AV_RL8 (cur+85);
  53. flags = AV_RL8 (cur+87);
  54. cur += 92;
  55. if (version < 8 || version > 9) {
  56. av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n",
  57. version);
  58. return -1;
  59. }
  60. if (depth == 8) {
  61. avctx->pix_fmt = PIX_FMT_PAL8;
  62. cur += 1024;
  63. } else if (depth == 16 || depth == 32)
  64. avctx->pix_fmt = PIX_FMT_RGB32;
  65. else {
  66. av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth);
  67. return -1;
  68. }
  69. if (p->data[0])
  70. avctx->release_buffer(avctx, p);
  71. if (avcodec_check_dimensions(avctx, w, h))
  72. return -1;
  73. if (w != avctx->width || h != avctx->height)
  74. avcodec_set_dimensions(avctx, w, h);
  75. if (avctx->get_buffer(avctx, p) < 0) {
  76. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  77. return -1;
  78. }
  79. p->pict_type = FF_I_TYPE;
  80. ptr = p->data[0];
  81. stride = p->linesize[0];
  82. if (depth == 8) {
  83. pal = (uint32_t *) p->data[1];
  84. for (y=0; y<256; y++) {
  85. v = AV_RB32(palette+y);
  86. pal[y] = (v>>8) + (v<<24);
  87. }
  88. for (y=0; y<h; y++) {
  89. memcpy(ptr, cur, w);
  90. ptr += stride;
  91. cur += w;
  92. }
  93. } else if (depth == 16) {
  94. switch (d3d_format) {
  95. case 0:
  96. if (!flags&1) goto unsupported;
  97. case FF_S3TC_DXT1:
  98. ff_decode_dxt1(cur, ptr, w, h, stride);
  99. break;
  100. case FF_S3TC_DXT3:
  101. ff_decode_dxt3(cur, ptr, w, h, stride);
  102. break;
  103. default:
  104. goto unsupported;
  105. }
  106. } else if (depth == 32) {
  107. switch (d3d_format) {
  108. case 0x15:
  109. case 0x16:
  110. for (y=0; y<h; y++) {
  111. memcpy(ptr, cur, w*4);
  112. ptr += stride;
  113. cur += w*4;
  114. }
  115. break;
  116. default:
  117. goto unsupported;
  118. }
  119. }
  120. for (; mipmap_count > 1; mipmap_count--)
  121. cur += AV_RL32(cur) + 4;
  122. *picture = s->picture;
  123. *data_size = sizeof(AVPicture);
  124. return cur - buf;
  125. unsupported:
  126. av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
  127. return -1;
  128. }
  129. static av_cold int txd_end(AVCodecContext *avctx) {
  130. TXDContext *s = avctx->priv_data;
  131. if (s->picture.data[0])
  132. avctx->release_buffer(avctx, &s->picture);
  133. return 0;
  134. }
  135. AVCodec txd_decoder = {
  136. "txd",
  137. CODEC_TYPE_VIDEO,
  138. CODEC_ID_TXD,
  139. sizeof(TXDContext),
  140. txd_init,
  141. NULL,
  142. txd_end,
  143. txd_decode_frame,
  144. CODEC_CAP_DR1,
  145. NULL,
  146. .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
  147. };