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.

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