00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022
00023 enum TargaCompr{
00024 TGA_NODATA = 0,
00025 TGA_PAL = 1,
00026 TGA_RGB = 2,
00027 TGA_BW = 3,
00028 TGA_RLE = 8,
00029 };
00030
00031 typedef struct TargaContext {
00032 AVFrame picture;
00033
00034 int width, height;
00035 int bpp;
00036 int color_type;
00037 int compression_type;
00038 } TargaContext;
00039
00040 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
00041 {
00042 int i, x, y;
00043 int depth = (bpp + 1) >> 3;
00044 int type, count;
00045 int diff;
00046
00047 diff = stride - w * depth;
00048 x = y = 0;
00049 while(y < h){
00050 type = *src++;
00051 count = (type & 0x7F) + 1;
00052 type &= 0x80;
00053 if((x + count > w) && (x + count + 1 > (h - y) * w)){
00054 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
00055 return;
00056 }
00057 for(i = 0; i < count; i++){
00058 switch(depth){
00059 case 1:
00060 *dst = *src;
00061 break;
00062 case 2:
00063 *((uint16_t*)dst) = AV_RL16(src);
00064 break;
00065 case 3:
00066 dst[0] = src[0];
00067 dst[1] = src[1];
00068 dst[2] = src[2];
00069 break;
00070 case 4:
00071 *((uint32_t*)dst) = AV_RL32(src);
00072 break;
00073 }
00074 dst += depth;
00075 if(!type)
00076 src += depth;
00077
00078 x++;
00079 if(x == w){
00080 x = 0;
00081 y++;
00082 dst += diff;
00083 }
00084 }
00085 if(type)
00086 src += depth;
00087 }
00088 }
00089
00090 static int decode_frame(AVCodecContext *avctx,
00091 void *data, int *data_size,
00092 const uint8_t *buf, int buf_size)
00093 {
00094 TargaContext * const s = avctx->priv_data;
00095 AVFrame *picture = data;
00096 AVFrame * const p= (AVFrame*)&s->picture;
00097 uint8_t *dst;
00098 int stride;
00099 int idlen, pal, compr, x, y, w, h, bpp, flags;
00100 int first_clr, colors, csize;
00101
00102
00103 idlen = *buf++;
00104 pal = *buf++;
00105 compr = *buf++;
00106 first_clr = AV_RL16(buf); buf += 2;
00107 colors = AV_RL16(buf); buf += 2;
00108 csize = *buf++;
00109 x = AV_RL16(buf); buf += 2;
00110 y = AV_RL16(buf); buf += 2;
00111 w = AV_RL16(buf); buf += 2;
00112 h = AV_RL16(buf); buf += 2;
00113 bpp = *buf++;
00114 flags = *buf++;
00115
00116 buf += idlen;
00117 s->bpp = bpp;
00118 s->width = w;
00119 s->height = h;
00120 switch(s->bpp){
00121 case 8:
00122 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
00123 break;
00124 case 15:
00125 avctx->pix_fmt = PIX_FMT_RGB555;
00126 break;
00127 case 16:
00128 avctx->pix_fmt = PIX_FMT_RGB555;
00129 break;
00130 case 24:
00131 avctx->pix_fmt = PIX_FMT_BGR24;
00132 break;
00133 case 32:
00134 avctx->pix_fmt = PIX_FMT_RGB32;
00135 break;
00136 default:
00137 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
00138 return -1;
00139 }
00140
00141 if(s->picture.data[0])
00142 avctx->release_buffer(avctx, &s->picture);
00143
00144 if(avcodec_check_dimensions(avctx, w, h))
00145 return -1;
00146 if(w != avctx->width || h != avctx->height)
00147 avcodec_set_dimensions(avctx, w, h);
00148 if(avctx->get_buffer(avctx, p) < 0){
00149 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00150 return -1;
00151 }
00152 if(flags & 0x20){
00153 dst = p->data[0];
00154 stride = p->linesize[0];
00155 }else{
00156 dst = p->data[0] + p->linesize[0] * (h - 1);
00157 stride = -p->linesize[0];
00158 }
00159
00160 if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
00161 memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
00162 if(avctx->palctrl->palette_changed){
00163 p->palette_has_changed = 1;
00164 avctx->palctrl->palette_changed = 0;
00165 }
00166 }
00167 if(colors){
00168 if((colors + first_clr) > 256){
00169 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
00170 return -1;
00171 }
00172 if(csize != 24){
00173 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
00174 return -1;
00175 }
00176 if(avctx->pix_fmt != PIX_FMT_PAL8)
00177 buf += colors * ((csize + 1) >> 3);
00178 else{
00179 int r, g, b, t;
00180 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
00181 for(t = 0; t < colors; t++){
00182 r = *buf++;
00183 g = *buf++;
00184 b = *buf++;
00185 *pal++ = (b << 16) | (g << 8) | r;
00186 }
00187 p->palette_has_changed = 1;
00188 avctx->palctrl->palette_changed = 0;
00189 }
00190 }
00191 if((compr & (~TGA_RLE)) == TGA_NODATA)
00192 memset(p->data[0], 0, p->linesize[0] * s->height);
00193 else{
00194 if(compr & TGA_RLE)
00195 targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
00196 else{
00197 for(y = 0; y < s->height; y++){
00198 #ifdef WORDS_BIGENDIAN
00199 if((s->bpp + 1) >> 3 == 2){
00200 uint16_t *dst16 = (uint16_t*)dst;
00201 for(x = 0; x < s->width; x++)
00202 dst16[x] = AV_RL16(buf + x * 2);
00203 }else if((s->bpp + 1) >> 3 == 4){
00204 uint32_t *dst32 = (uint32_t*)dst;
00205 for(x = 0; x < s->width; x++)
00206 dst32[x] = AV_RL32(buf + x * 4);
00207 }else
00208 #endif
00209 memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
00210
00211 dst += stride;
00212 buf += s->width * ((s->bpp + 1) >> 3);
00213 }
00214 }
00215 }
00216
00217 *picture= *(AVFrame*)&s->picture;
00218 *data_size = sizeof(AVPicture);
00219
00220 return buf_size;
00221 }
00222
00223 static int targa_init(AVCodecContext *avctx){
00224 TargaContext *s = avctx->priv_data;
00225
00226 avcodec_get_frame_defaults((AVFrame*)&s->picture);
00227 avctx->coded_frame= (AVFrame*)&s->picture;
00228 s->picture.data[0] = NULL;
00229
00230 return 0;
00231 }
00232
00233 static int targa_end(AVCodecContext *avctx){
00234 TargaContext *s = avctx->priv_data;
00235
00236 if(s->picture.data[0])
00237 avctx->release_buffer(avctx, &s->picture);
00238
00239 return 0;
00240 }
00241
00242 AVCodec targa_decoder = {
00243 "targa",
00244 CODEC_TYPE_VIDEO,
00245 CODEC_ID_TARGA,
00246 sizeof(TargaContext),
00247 targa_init,
00248 NULL,
00249 targa_end,
00250 decode_frame,
00251 0,
00252 NULL
00253 };