Libav
|
00001 /* 00002 * Renderware TeXture Dictionary (.txd) image decoder 00003 * Copyright (c) 2007 Ivo van Poorten 00004 * 00005 * See also: http://wiki.multimedia.cx/index.php?title=TXD 00006 * 00007 * This file is part of FFmpeg. 00008 * 00009 * FFmpeg is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * FFmpeg is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with FFmpeg; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00024 #include "libavutil/intreadwrite.h" 00025 #include "avcodec.h" 00026 #include "s3tc.h" 00027 00028 typedef struct TXDContext { 00029 AVFrame picture; 00030 } TXDContext; 00031 00032 static av_cold int txd_init(AVCodecContext *avctx) { 00033 TXDContext *s = avctx->priv_data; 00034 00035 avcodec_get_frame_defaults(&s->picture); 00036 avctx->coded_frame = &s->picture; 00037 00038 return 0; 00039 } 00040 00041 static int txd_decode_frame(AVCodecContext *avctx, void *data, int *data_size, 00042 AVPacket *avpkt) { 00043 const uint8_t *buf = avpkt->data; 00044 TXDContext * const s = avctx->priv_data; 00045 AVFrame *picture = data; 00046 AVFrame * const p = &s->picture; 00047 unsigned int version, w, h, d3d_format, depth, stride, mipmap_count, flags; 00048 unsigned int y, v; 00049 uint8_t *ptr; 00050 const uint8_t *cur = buf; 00051 const uint32_t *palette = (const uint32_t *)(cur + 88); 00052 uint32_t *pal; 00053 00054 version = AV_RL32(cur); 00055 d3d_format = AV_RL32(cur+76); 00056 w = AV_RL16(cur+80); 00057 h = AV_RL16(cur+82); 00058 depth = AV_RL8 (cur+84); 00059 mipmap_count = AV_RL8 (cur+85); 00060 flags = AV_RL8 (cur+87); 00061 cur += 92; 00062 00063 if (version < 8 || version > 9) { 00064 av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n", 00065 version); 00066 return -1; 00067 } 00068 00069 if (depth == 8) { 00070 avctx->pix_fmt = PIX_FMT_PAL8; 00071 cur += 1024; 00072 } else if (depth == 16 || depth == 32) 00073 avctx->pix_fmt = PIX_FMT_RGB32; 00074 else { 00075 av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth); 00076 return -1; 00077 } 00078 00079 if (p->data[0]) 00080 avctx->release_buffer(avctx, p); 00081 00082 if (avcodec_check_dimensions(avctx, w, h)) 00083 return -1; 00084 if (w != avctx->width || h != avctx->height) 00085 avcodec_set_dimensions(avctx, w, h); 00086 if (avctx->get_buffer(avctx, p) < 0) { 00087 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00088 return -1; 00089 } 00090 00091 p->pict_type = FF_I_TYPE; 00092 00093 ptr = p->data[0]; 00094 stride = p->linesize[0]; 00095 00096 if (depth == 8) { 00097 pal = (uint32_t *) p->data[1]; 00098 for (y=0; y<256; y++) { 00099 v = AV_RB32(palette+y); 00100 pal[y] = (v>>8) + (v<<24); 00101 } 00102 for (y=0; y<h; y++) { 00103 memcpy(ptr, cur, w); 00104 ptr += stride; 00105 cur += w; 00106 } 00107 } else if (depth == 16) { 00108 switch (d3d_format) { 00109 case 0: 00110 if (!flags&1) goto unsupported; 00111 case FF_S3TC_DXT1: 00112 ff_decode_dxt1(cur, ptr, w, h, stride); 00113 break; 00114 case FF_S3TC_DXT3: 00115 ff_decode_dxt3(cur, ptr, w, h, stride); 00116 break; 00117 default: 00118 goto unsupported; 00119 } 00120 } else if (depth == 32) { 00121 switch (d3d_format) { 00122 case 0x15: 00123 case 0x16: 00124 for (y=0; y<h; y++) { 00125 memcpy(ptr, cur, w*4); 00126 ptr += stride; 00127 cur += w*4; 00128 } 00129 break; 00130 default: 00131 goto unsupported; 00132 } 00133 } 00134 00135 for (; mipmap_count > 1; mipmap_count--) 00136 cur += AV_RL32(cur) + 4; 00137 00138 *picture = s->picture; 00139 *data_size = sizeof(AVPicture); 00140 00141 return cur - buf; 00142 00143 unsupported: 00144 av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format); 00145 return -1; 00146 } 00147 00148 static av_cold int txd_end(AVCodecContext *avctx) { 00149 TXDContext *s = avctx->priv_data; 00150 00151 if (s->picture.data[0]) 00152 avctx->release_buffer(avctx, &s->picture); 00153 00154 return 0; 00155 } 00156 00157 AVCodec txd_decoder = { 00158 "txd", 00159 AVMEDIA_TYPE_VIDEO, 00160 CODEC_ID_TXD, 00161 sizeof(TXDContext), 00162 txd_init, 00163 NULL, 00164 txd_end, 00165 txd_decode_frame, 00166 CODEC_CAP_DR1, 00167 NULL, 00168 .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"), 00169 };