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 24#include "libavutil/intreadwrite.h" 25#include "avcodec.h" 26#include "s3tc.h" 27 28typedef struct TXDContext { 29 AVFrame picture; 30} TXDContext; 31 32static av_cold int txd_init(AVCodecContext *avctx) { 33 TXDContext *s = avctx->priv_data; 34 35 avcodec_get_frame_defaults(&s->picture); 36 avctx->coded_frame = &s->picture; 37 38 return 0; 39} 40 41static int txd_decode_frame(AVCodecContext *avctx, void *data, int *data_size, 42 const uint8_t *buf, int buf_size) { 43 TXDContext * const s = avctx->priv_data; 44 AVFrame *picture = data; 45 AVFrame * const p = &s->picture; 46 unsigned int version, w, h, d3d_format, depth, stride, mipmap_count, flags; 47 unsigned int y, v; 48 uint8_t *ptr; 49 const uint8_t *cur = buf; 50 const uint32_t *palette = (const uint32_t *)(cur + 88); 51 uint32_t *pal; 52 53 version = AV_RL32(cur); 54 d3d_format = AV_RL32(cur+76); 55 w = AV_RL16(cur+80); 56 h = AV_RL16(cur+82); 57 depth = AV_RL8 (cur+84); 58 mipmap_count = AV_RL8 (cur+85); 59 flags = AV_RL8 (cur+87); 60 cur += 92; 61 62 if (version < 8 || version > 9) { 63 av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n", 64 version); 65 return -1; 66 } 67 68 if (depth == 8) { 69 avctx->pix_fmt = PIX_FMT_PAL8; 70 cur += 1024; 71 } else if (depth == 16 || depth == 32) 72 avctx->pix_fmt = PIX_FMT_RGB32; 73 else { 74 av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth); 75 return -1; 76 } 77 78 if (p->data[0]) 79 avctx->release_buffer(avctx, p); 80 81 if (avcodec_check_dimensions(avctx, w, h)) 82 return -1; 83 if (w != avctx->width || h != avctx->height) 84 avcodec_set_dimensions(avctx, w, h); 85 if (avctx->get_buffer(avctx, p) < 0) { 86 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 87 return -1; 88 } 89 90 p->pict_type = FF_I_TYPE; 91 92 ptr = p->data[0]; 93 stride = p->linesize[0]; 94 95 if (depth == 8) { 96 pal = (uint32_t *) p->data[1]; 97 for (y=0; y<256; y++) { 98 v = AV_RB32(palette+y); 99 pal[y] = (v>>8) + (v<<24); 100 } 101 for (y=0; y<h; y++) { 102 memcpy(ptr, cur, w); 103 ptr += stride; 104 cur += w; 105 } 106 } else if (depth == 16) { 107 switch (d3d_format) { 108 case 0: 109 if (!flags&1) goto unsupported; 110 case FF_S3TC_DXT1: 111 ff_decode_dxt1(cur, ptr, w, h, stride); 112 break; 113 case FF_S3TC_DXT3: 114 ff_decode_dxt3(cur, ptr, w, h, stride); 115 break; 116 default: 117 goto unsupported; 118 } 119 } else if (depth == 32) { 120 switch (d3d_format) { 121 case 0x15: 122 case 0x16: 123 for (y=0; y<h; y++) { 124 memcpy(ptr, cur, w*4); 125 ptr += stride; 126 cur += w*4; 127 } 128 break; 129 default: 130 goto unsupported; 131 } 132 } 133 134 for (; mipmap_count > 1; mipmap_count--) 135 cur += AV_RL32(cur) + 4; 136 137 *picture = s->picture; 138 *data_size = sizeof(AVPicture); 139 140 return cur - buf; 141 142unsupported: 143 av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format); 144 return -1; 145} 146 147static av_cold int txd_end(AVCodecContext *avctx) { 148 TXDContext *s = avctx->priv_data; 149 150 if (s->picture.data[0]) 151 avctx->release_buffer(avctx, &s->picture); 152 153 return 0; 154} 155 156AVCodec txd_decoder = { 157 "txd", 158 CODEC_TYPE_VIDEO, 159 CODEC_ID_TXD, 160 sizeof(TXDContext), 161 txd_init, 162 NULL, 163 txd_end, 164 txd_decode_frame, 165 0, 166 NULL, 167 .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"), 168}; 169