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.

145 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 "libavutil/intreadwrite.h"
  24. #include "libavutil/imgutils.h"
  25. #include "bytestream.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. #include "s3tc.h"
  29. static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  30. AVPacket *avpkt) {
  31. GetByteContext gb;
  32. AVFrame * const p = data;
  33. unsigned int version, w, h, d3d_format, depth, stride, flags;
  34. unsigned int y, v;
  35. uint8_t *ptr;
  36. uint32_t *pal;
  37. int ret;
  38. bytestream2_init(&gb, avpkt->data, avpkt->size);
  39. version = bytestream2_get_le32(&gb);
  40. bytestream2_skip(&gb, 72);
  41. d3d_format = bytestream2_get_le32(&gb);
  42. w = bytestream2_get_le16(&gb);
  43. h = bytestream2_get_le16(&gb);
  44. depth = bytestream2_get_byte(&gb);
  45. bytestream2_skip(&gb, 2);
  46. flags = bytestream2_get_byte(&gb);
  47. if (version < 8 || version > 9) {
  48. av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n",
  49. version);
  50. return AVERROR_PATCHWELCOME;
  51. }
  52. if (depth == 8) {
  53. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  54. } else if (depth == 16 || depth == 32) {
  55. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  56. } else {
  57. av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth);
  58. return AVERROR_PATCHWELCOME;
  59. }
  60. if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
  61. return ret;
  62. if (w != avctx->width || h != avctx->height)
  63. avcodec_set_dimensions(avctx, w, h);
  64. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  65. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  66. return ret;
  67. }
  68. p->pict_type = AV_PICTURE_TYPE_I;
  69. ptr = p->data[0];
  70. stride = p->linesize[0];
  71. if (depth == 8) {
  72. pal = (uint32_t *) p->data[1];
  73. for (y = 0; y < 256; y++) {
  74. v = bytestream2_get_be32(&gb);
  75. pal[y] = (v >> 8) + (v << 24);
  76. }
  77. if (bytestream2_get_bytes_left(&gb) < w * h)
  78. return AVERROR_INVALIDDATA;
  79. bytestream2_skip(&gb, 4);
  80. for (y=0; y<h; y++) {
  81. bytestream2_get_buffer(&gb, ptr, w);
  82. ptr += stride;
  83. }
  84. } else if (depth == 16) {
  85. bytestream2_skip(&gb, 4);
  86. switch (d3d_format) {
  87. case 0:
  88. if (!(flags & 1))
  89. goto unsupported;
  90. case FF_S3TC_DXT1:
  91. if (bytestream2_get_bytes_left(&gb) < (w/4) * (h/4) * 8)
  92. return AVERROR_INVALIDDATA;
  93. ff_decode_dxt1(&gb, ptr, w, h, stride);
  94. break;
  95. case FF_S3TC_DXT3:
  96. if (bytestream2_get_bytes_left(&gb) < (w/4) * (h/4) * 16)
  97. return AVERROR_INVALIDDATA;
  98. ff_decode_dxt3(&gb, ptr, w, h, stride);
  99. break;
  100. default:
  101. goto unsupported;
  102. }
  103. } else if (depth == 32) {
  104. switch (d3d_format) {
  105. case 0x15:
  106. case 0x16:
  107. if (bytestream2_get_bytes_left(&gb) < h * w * 4)
  108. return AVERROR_INVALIDDATA;
  109. for (y=0; y<h; y++) {
  110. bytestream2_get_buffer(&gb, ptr, w * 4);
  111. ptr += stride;
  112. }
  113. break;
  114. default:
  115. goto unsupported;
  116. }
  117. }
  118. *got_frame = 1;
  119. return avpkt->size;
  120. unsupported:
  121. av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
  122. return AVERROR_PATCHWELCOME;
  123. }
  124. AVCodec ff_txd_decoder = {
  125. .name = "txd",
  126. .type = AVMEDIA_TYPE_VIDEO,
  127. .id = AV_CODEC_ID_TXD,
  128. .decode = txd_decode_frame,
  129. .capabilities = CODEC_CAP_DR1,
  130. .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
  131. };