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