• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/txd.c

Go to the documentation of this file.
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 Libav.
00008  *
00009  * Libav 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  * Libav 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 Libav; 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 "libavutil/imgutils.h"
00026 #include "avcodec.h"
00027 #include "s3tc.h"
00028 
00029 typedef struct TXDContext {
00030     AVFrame picture;
00031 } TXDContext;
00032 
00033 static av_cold int txd_init(AVCodecContext *avctx) {
00034     TXDContext *s = avctx->priv_data;
00035 
00036     avcodec_get_frame_defaults(&s->picture);
00037     avctx->coded_frame = &s->picture;
00038 
00039     return 0;
00040 }
00041 
00042 static int txd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00043                             AVPacket *avpkt) {
00044     const uint8_t *buf = avpkt->data;
00045     TXDContext * const s = avctx->priv_data;
00046     AVFrame *picture = data;
00047     AVFrame * const p = &s->picture;
00048     unsigned int version, w, h, d3d_format, depth, stride, mipmap_count, flags;
00049     unsigned int y, v;
00050     uint8_t *ptr;
00051     const uint8_t *cur = buf;
00052     const uint32_t *palette = (const uint32_t *)(cur + 88);
00053     uint32_t *pal;
00054 
00055     version         = AV_RL32(cur);
00056     d3d_format      = AV_RL32(cur+76);
00057     w               = AV_RL16(cur+80);
00058     h               = AV_RL16(cur+82);
00059     depth           = AV_RL8 (cur+84);
00060     mipmap_count    = AV_RL8 (cur+85);
00061     flags           = AV_RL8 (cur+87);
00062     cur            += 92;
00063 
00064     if (version < 8 || version > 9) {
00065         av_log(avctx, AV_LOG_ERROR, "texture data version %i is unsupported\n",
00066                                                                     version);
00067         return -1;
00068     }
00069 
00070     if (depth == 8) {
00071         avctx->pix_fmt = PIX_FMT_PAL8;
00072         cur += 1024;
00073     } else if (depth == 16 || depth == 32)
00074         avctx->pix_fmt = PIX_FMT_RGB32;
00075     else {
00076         av_log(avctx, AV_LOG_ERROR, "depth of %i is unsupported\n", depth);
00077         return -1;
00078     }
00079 
00080     if (p->data[0])
00081         avctx->release_buffer(avctx, p);
00082 
00083     if (av_image_check_size(w, h, 0, avctx))
00084         return -1;
00085     if (w != avctx->width || h != avctx->height)
00086         avcodec_set_dimensions(avctx, w, h);
00087     if (avctx->get_buffer(avctx, p) < 0) {
00088         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00089         return -1;
00090     }
00091 
00092     p->pict_type = AV_PICTURE_TYPE_I;
00093 
00094     ptr    = p->data[0];
00095     stride = p->linesize[0];
00096 
00097     if (depth == 8) {
00098         pal = (uint32_t *) p->data[1];
00099         for (y=0; y<256; y++) {
00100             v = AV_RB32(palette+y);
00101             pal[y] = (v>>8) + (v<<24);
00102         }
00103         for (y=0; y<h; y++) {
00104             memcpy(ptr, cur, w);
00105             ptr += stride;
00106             cur += w;
00107         }
00108     } else if (depth == 16) {
00109         switch (d3d_format) {
00110         case 0:
00111             if (!(flags & 1))
00112                 goto unsupported;
00113         case FF_S3TC_DXT1:
00114             ff_decode_dxt1(cur, ptr, w, h, stride);
00115             break;
00116         case FF_S3TC_DXT3:
00117             ff_decode_dxt3(cur, ptr, w, h, stride);
00118             break;
00119         default:
00120             goto unsupported;
00121         }
00122     } else if (depth == 32) {
00123         switch (d3d_format) {
00124         case 0x15:
00125         case 0x16:
00126             for (y=0; y<h; y++) {
00127                 memcpy(ptr, cur, w*4);
00128                 ptr += stride;
00129                 cur += w*4;
00130             }
00131             break;
00132         default:
00133             goto unsupported;
00134         }
00135     }
00136 
00137     for (; mipmap_count > 1; mipmap_count--)
00138         cur += AV_RL32(cur) + 4;
00139 
00140     *picture   = s->picture;
00141     *data_size = sizeof(AVPicture);
00142 
00143     return cur - buf;
00144 
00145 unsupported:
00146     av_log(avctx, AV_LOG_ERROR, "unsupported d3d format (%08x)\n", d3d_format);
00147     return -1;
00148 }
00149 
00150 static av_cold int txd_end(AVCodecContext *avctx) {
00151     TXDContext *s = avctx->priv_data;
00152 
00153     if (s->picture.data[0])
00154         avctx->release_buffer(avctx, &s->picture);
00155 
00156     return 0;
00157 }
00158 
00159 AVCodec ff_txd_decoder = {
00160     .name           = "txd",
00161     .type           = AVMEDIA_TYPE_VIDEO,
00162     .id             = CODEC_ID_TXD,
00163     .priv_data_size = sizeof(TXDContext),
00164     .init           = txd_init,
00165     .close          = txd_end,
00166     .decode         = txd_decode_frame,
00167     .capabilities   = CODEC_CAP_DR1,
00168     .long_name = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
00169 };
Generated on Sun Apr 22 2012 21:54:04 for Libav by doxygen 1.7.1