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.

165 lines
4.4KB

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