txd.c 4.6 KB

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