2 * Renderware TeXture Dictionary (.txd) image decoder
3 * Copyright (c) 2007 Ivo van Poorten
5 * See also: http://wiki.multimedia.cx/index.php?title=TXD
7 * This file is part of FFmpeg.
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.
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.
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
27 typedef struct TXDContext {
31 static av_cold int txd_init(AVCodecContext *avctx) {
32 TXDContext *s = avctx->priv_data;
34 avcodec_get_frame_defaults(&s->picture);
35 avctx->coded_frame = &s->picture;
40 static int txd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
41 const 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;
48 const uint8_t *cur = buf;
49 const uint32_t *palette = (const uint32_t *)(cur + 88);
52 version = AV_RL32(cur);
53 d3d_format = AV_RL32(cur+76);
56 depth = AV_RL8 (cur+84);
57 mipmap_count = AV_RL8 (cur+85);
58 flags = AV_RL8 (cur+87);
61 if (version < 8 || version > 9) {
62 av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n",
68 avctx->pix_fmt = PIX_FMT_PAL8;
70 } else if (depth == 16 || depth == 32)
71 avctx->pix_fmt = PIX_FMT_RGB32;
73 av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth);
78 avctx->release_buffer(avctx, p);
80 if (avcodec_check_dimensions(avctx, w, h))
82 if (w != avctx->width || h != avctx->height)
83 avcodec_set_dimensions(avctx, w, h);
84 if (avctx->get_buffer(avctx, p) < 0) {
85 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
89 p->pict_type = FF_I_TYPE;
92 stride = p->linesize[0];
95 pal = (uint32_t *) p->data[1];
96 for (y=0; y<256; y++) {
97 v = AV_RB32(palette+y);
98 pal[y] = (v>>8) + (v<<24);
100 for (y=0; y<h; y++) {
105 } else if (depth == 16) {
106 switch (d3d_format) {
108 if (!flags&1) goto unsupported;
110 ff_decode_dxt1(cur, ptr, w, h, stride);
113 ff_decode_dxt3(cur, ptr, w, h, stride);
118 } else if (depth == 32) {
119 switch (d3d_format) {
122 for (y=0; y<h; y++) {
123 memcpy(ptr, cur, w*4);
133 for (; mipmap_count > 1; mipmap_count--)
134 cur += AV_RL32(cur) + 4;
136 *picture = s->picture;
137 *data_size = sizeof(AVPicture);
142 av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
146 static av_cold int txd_end(AVCodecContext *avctx) {
147 TXDContext *s = avctx->priv_data;
149 if (s->picture.data[0])
150 avctx->release_buffer(avctx, &s->picture);
155 AVCodec txd_decoder = {
166 .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),