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.

143 lines
4.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 "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. return ret;
  66. p->pict_type = AV_PICTURE_TYPE_I;
  67. ptr = p->data[0];
  68. stride = p->linesize[0];
  69. if (depth == 8) {
  70. pal = (uint32_t *) p->data[1];
  71. for (y = 0; y < 256; y++) {
  72. v = bytestream2_get_be32(&gb);
  73. pal[y] = (v >> 8) + (v << 24);
  74. }
  75. if (bytestream2_get_bytes_left(&gb) < w * h)
  76. return AVERROR_INVALIDDATA;
  77. bytestream2_skip(&gb, 4);
  78. for (y=0; y<h; y++) {
  79. bytestream2_get_buffer(&gb, ptr, w);
  80. ptr += stride;
  81. }
  82. } else if (depth == 16) {
  83. bytestream2_skip(&gb, 4);
  84. switch (d3d_format) {
  85. case 0:
  86. if (!(flags & 1))
  87. goto unsupported;
  88. case FF_S3TC_DXT1:
  89. if (bytestream2_get_bytes_left(&gb) < (w/4) * (h/4) * 8)
  90. return AVERROR_INVALIDDATA;
  91. ff_decode_dxt1(&gb, ptr, w, h, stride);
  92. break;
  93. case FF_S3TC_DXT3:
  94. if (bytestream2_get_bytes_left(&gb) < (w/4) * (h/4) * 16)
  95. return AVERROR_INVALIDDATA;
  96. ff_decode_dxt3(&gb, ptr, w, h, stride);
  97. break;
  98. default:
  99. goto unsupported;
  100. }
  101. } else if (depth == 32) {
  102. switch (d3d_format) {
  103. case 0x15:
  104. case 0x16:
  105. if (bytestream2_get_bytes_left(&gb) < h * w * 4)
  106. return AVERROR_INVALIDDATA;
  107. for (y=0; y<h; y++) {
  108. bytestream2_get_buffer(&gb, ptr, w * 4);
  109. ptr += stride;
  110. }
  111. break;
  112. default:
  113. goto unsupported;
  114. }
  115. }
  116. *got_frame = 1;
  117. return avpkt->size;
  118. unsupported:
  119. av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
  120. return AVERROR_PATCHWELCOME;
  121. }
  122. AVCodec ff_txd_decoder = {
  123. .name = "txd",
  124. .type = AVMEDIA_TYPE_VIDEO,
  125. .id = AV_CODEC_ID_TXD,
  126. .decode = txd_decode_frame,
  127. .capabilities = CODEC_CAP_DR1,
  128. .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
  129. };