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.

169 lines
4.8KB

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