00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 const uint8_t *buf, int buf_size) {
00043 TXDContext * const s = avctx->priv_data;
00044 AVFrame *picture = data;
00045 AVFrame * const p = &s->picture;
00046 unsigned int version, w, h, d3d_format, depth, stride, mipmap_count, flags;
00047 unsigned int y, v;
00048 uint8_t *ptr;
00049 const uint8_t *cur = buf;
00050 const uint32_t *palette = (const uint32_t *)(cur + 88);
00051 uint32_t *pal;
00052
00053 version = AV_RL32(cur);
00054 d3d_format = AV_RL32(cur+76);
00055 w = AV_RL16(cur+80);
00056 h = AV_RL16(cur+82);
00057 depth = AV_RL8 (cur+84);
00058 mipmap_count = AV_RL8 (cur+85);
00059 flags = AV_RL8 (cur+87);
00060 cur += 92;
00061
00062 if (version < 8 || version > 9) {
00063 av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n",
00064 version);
00065 return -1;
00066 }
00067
00068 if (depth == 8) {
00069 avctx->pix_fmt = PIX_FMT_PAL8;
00070 cur += 1024;
00071 } else if (depth == 16 || depth == 32)
00072 avctx->pix_fmt = PIX_FMT_RGB32;
00073 else {
00074 av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth);
00075 return -1;
00076 }
00077
00078 if (p->data[0])
00079 avctx->release_buffer(avctx, p);
00080
00081 if (avcodec_check_dimensions(avctx, w, h))
00082 return -1;
00083 if (w != avctx->width || h != avctx->height)
00084 avcodec_set_dimensions(avctx, w, h);
00085 if (avctx->get_buffer(avctx, p) < 0) {
00086 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00087 return -1;
00088 }
00089
00090 p->pict_type = FF_I_TYPE;
00091
00092 ptr = p->data[0];
00093 stride = p->linesize[0];
00094
00095 if (depth == 8) {
00096 pal = (uint32_t *) p->data[1];
00097 for (y=0; y<256; y++) {
00098 v = AV_RB32(palette+y);
00099 pal[y] = (v>>8) + (v<<24);
00100 }
00101 for (y=0; y<h; y++) {
00102 memcpy(ptr, cur, w);
00103 ptr += stride;
00104 cur += w;
00105 }
00106 } else if (depth == 16) {
00107 switch (d3d_format) {
00108 case 0:
00109 if (!flags&1) goto unsupported;
00110 case FF_S3TC_DXT1:
00111 ff_decode_dxt1(cur, ptr, w, h, stride);
00112 break;
00113 case FF_S3TC_DXT3:
00114 ff_decode_dxt3(cur, ptr, w, h, stride);
00115 break;
00116 default:
00117 goto unsupported;
00118 }
00119 } else if (depth == 32) {
00120 switch (d3d_format) {
00121 case 0x15:
00122 case 0x16:
00123 for (y=0; y<h; y++) {
00124 memcpy(ptr, cur, w*4);
00125 ptr += stride;
00126 cur += w*4;
00127 }
00128 break;
00129 default:
00130 goto unsupported;
00131 }
00132 }
00133
00134 for (; mipmap_count > 1; mipmap_count--)
00135 cur += AV_RL32(cur) + 4;
00136
00137 *picture = s->picture;
00138 *data_size = sizeof(AVPicture);
00139
00140 return cur - buf;
00141
00142 unsupported:
00143 av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
00144 return -1;
00145 }
00146
00147 static av_cold int txd_end(AVCodecContext *avctx) {
00148 TXDContext *s = avctx->priv_data;
00149
00150 if (s->picture.data[0])
00151 avctx->release_buffer(avctx, &s->picture);
00152
00153 return 0;
00154 }
00155
00156 AVCodec txd_decoder = {
00157 "txd",
00158 CODEC_TYPE_VIDEO,
00159 CODEC_ID_TXD,
00160 sizeof(TXDContext),
00161 txd_init,
00162 NULL,
00163 txd_end,
00164 txd_decode_frame,
00165 0,
00166 NULL,
00167 .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
00168 };