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.

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