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.

175 lines
5.2KB

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