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.

177 lines
5.3KB

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