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.

167 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 "internal.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 *got_frame,
  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 = AV_PIX_FMT_PAL8;
  64. } else if (depth == 16 || depth == 32) {
  65. avctx->pix_fmt = AV_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 (ff_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. bytestream2_skip(&gb, 4);
  90. for (y=0; y<h; y++) {
  91. bytestream2_get_buffer(&gb, ptr, w);
  92. ptr += stride;
  93. }
  94. } else if (depth == 16) {
  95. bytestream2_skip(&gb, 4);
  96. switch (d3d_format) {
  97. case 0:
  98. if (!(flags & 1))
  99. goto unsupported;
  100. case FF_S3TC_DXT1:
  101. ff_decode_dxt1(&gb, ptr, w, h, stride);
  102. break;
  103. case FF_S3TC_DXT3:
  104. ff_decode_dxt3(&gb, ptr, w, h, stride);
  105. break;
  106. default:
  107. goto unsupported;
  108. }
  109. } else if (depth == 32) {
  110. switch (d3d_format) {
  111. case 0x15:
  112. case 0x16:
  113. for (y=0; y<h; y++) {
  114. bytestream2_get_buffer(&gb, ptr, w * 4);
  115. ptr += stride;
  116. }
  117. break;
  118. default:
  119. goto unsupported;
  120. }
  121. }
  122. *picture = s->picture;
  123. *got_frame = 1;
  124. return avpkt->size;
  125. unsupported:
  126. av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
  127. return -1;
  128. }
  129. static av_cold int txd_end(AVCodecContext *avctx) {
  130. TXDContext *s = avctx->priv_data;
  131. if (s->picture.data[0])
  132. avctx->release_buffer(avctx, &s->picture);
  133. return 0;
  134. }
  135. AVCodec ff_txd_decoder = {
  136. .name = "txd",
  137. .type = AVMEDIA_TYPE_VIDEO,
  138. .id = AV_CODEC_ID_TXD,
  139. .priv_data_size = sizeof(TXDContext),
  140. .init = txd_init,
  141. .close = txd_end,
  142. .decode = txd_decode_frame,
  143. .capabilities = CODEC_CAP_DR1,
  144. .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
  145. };