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.

166 lines
4.8KB

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