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