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.

187 lines
5.5KB

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