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 "libavutil/imgutils.h"
00024 #include "avcodec.h"
00025 #include "bytestream.h"
00026 #include "targa.h"
00027
00028 typedef struct TargaContext {
00029 AVFrame picture;
00030
00031 int width, height;
00032 int bpp;
00033 int color_type;
00034 int compression_type;
00035 } TargaContext;
00036
00037 #define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
00038 if(needed > buf_end - buf){ \
00039 av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
00040 return -1; \
00041 } \
00042
00043 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)
00044 {
00045 int i, x, y;
00046 int depth = (bpp + 1) >> 3;
00047 int type, count;
00048 int diff;
00049 const uint8_t *src_end = src + src_size;
00050
00051 diff = stride - w * depth;
00052 x = y = 0;
00053 while(y < h){
00054 CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
00055 type = *src++;
00056 count = (type & 0x7F) + 1;
00057 type &= 0x80;
00058 if((x + count > w) && (x + count + 1 > (h - y) * w)){
00059 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
00060 return -1;
00061 }
00062 if(type){
00063 CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
00064 }else{
00065 CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
00066 }
00067 for(i = 0; i < count; i++){
00068 switch(depth){
00069 case 1:
00070 *dst = *src;
00071 break;
00072 case 2:
00073 *((uint16_t*)dst) = AV_RL16(src);
00074 break;
00075 case 3:
00076 dst[0] = src[0];
00077 dst[1] = src[1];
00078 dst[2] = src[2];
00079 break;
00080 case 4:
00081 *((uint32_t*)dst) = AV_RL32(src);
00082 break;
00083 }
00084 dst += depth;
00085 if(!type)
00086 src += depth;
00087
00088 x++;
00089 if(x == w){
00090 x = 0;
00091 y++;
00092 dst += diff;
00093 }
00094 }
00095 if(type)
00096 src += depth;
00097 }
00098 return src_size;
00099 }
00100
00101 static int decode_frame(AVCodecContext *avctx,
00102 void *data, int *data_size,
00103 AVPacket *avpkt)
00104 {
00105 const uint8_t *buf = avpkt->data;
00106 const uint8_t *buf_end = avpkt->data + avpkt->size;
00107 TargaContext * const s = avctx->priv_data;
00108 AVFrame *picture = data;
00109 AVFrame * const p= (AVFrame*)&s->picture;
00110 uint8_t *dst;
00111 int stride;
00112 int idlen, compr, y, w, h, bpp, flags;
00113 int first_clr, colors, csize;
00114
00115
00116 CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
00117 idlen = *buf++;
00118 buf++;
00119 compr = *buf++;
00120 first_clr = AV_RL16(buf); buf += 2;
00121 colors = AV_RL16(buf); buf += 2;
00122 csize = *buf++;
00123 buf += 2;
00124 y = AV_RL16(buf); buf += 2;
00125 w = AV_RL16(buf); buf += 2;
00126 h = AV_RL16(buf); buf += 2;
00127 bpp = *buf++;
00128 flags = *buf++;
00129
00130 CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
00131 buf += idlen;
00132 s->bpp = bpp;
00133 s->width = w;
00134 s->height = h;
00135 switch(s->bpp){
00136 case 8:
00137 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
00138 break;
00139 case 15:
00140 avctx->pix_fmt = PIX_FMT_RGB555;
00141 break;
00142 case 16:
00143 avctx->pix_fmt = PIX_FMT_RGB555;
00144 break;
00145 case 24:
00146 avctx->pix_fmt = PIX_FMT_BGR24;
00147 break;
00148 case 32:
00149 avctx->pix_fmt = PIX_FMT_RGB32;
00150 break;
00151 default:
00152 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
00153 return -1;
00154 }
00155
00156 if(s->picture.data[0])
00157 avctx->release_buffer(avctx, &s->picture);
00158
00159 if(av_image_check_size(w, h, 0, avctx))
00160 return -1;
00161 if(w != avctx->width || h != avctx->height)
00162 avcodec_set_dimensions(avctx, w, h);
00163 if(avctx->get_buffer(avctx, p) < 0){
00164 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00165 return -1;
00166 }
00167 if(flags & 0x20){
00168 dst = p->data[0];
00169 stride = p->linesize[0];
00170 }else{
00171 dst = p->data[0] + p->linesize[0] * (h - 1);
00172 stride = -p->linesize[0];
00173 }
00174
00175 if(colors){
00176 int pal_size, pal_sample_size;
00177 if((colors + first_clr) > 256){
00178 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
00179 return -1;
00180 }
00181 switch (csize) {
00182 case 24: pal_sample_size = 3; break;
00183 case 16:
00184 case 15: pal_sample_size = 2; break;
00185 default:
00186 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
00187 return -1;
00188 }
00189 pal_size = colors * pal_sample_size;
00190 CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
00191 if(avctx->pix_fmt != PIX_FMT_PAL8)
00192 buf += pal_size;
00193 else{
00194 int t;
00195 uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
00196
00197 switch (pal_sample_size) {
00198 case 3:
00199
00200 for (t = 0; t < colors; t++)
00201 *pal++ = bytestream_get_le24(&buf);
00202 break;
00203 case 2:
00204
00205 for (t = 0; t < colors; t++) {
00206 uint32_t v = bytestream_get_le16(&buf);
00207 v = ((v & 0x7C00) << 9) |
00208 ((v & 0x03E0) << 6) |
00209 ((v & 0x001F) << 3);
00210
00211 v |= (v & 0xE0E0E0U) >> 5;
00212 *pal++ = v;
00213 }
00214 break;
00215 }
00216 p->palette_has_changed = 1;
00217 }
00218 }
00219 if((compr & (~TGA_RLE)) == TGA_NODATA)
00220 memset(p->data[0], 0, p->linesize[0] * s->height);
00221 else{
00222 if(compr & TGA_RLE){
00223 int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
00224 if (res < 0)
00225 return -1;
00226 buf += res;
00227 }else{
00228 size_t img_size = s->width * ((s->bpp + 1) >> 3);
00229 CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
00230 for(y = 0; y < s->height; y++){
00231 #if HAVE_BIGENDIAN
00232 int x;
00233 if((s->bpp + 1) >> 3 == 2){
00234 uint16_t *dst16 = (uint16_t*)dst;
00235 for(x = 0; x < s->width; x++)
00236 dst16[x] = AV_RL16(buf + x * 2);
00237 }else if((s->bpp + 1) >> 3 == 4){
00238 uint32_t *dst32 = (uint32_t*)dst;
00239 for(x = 0; x < s->width; x++)
00240 dst32[x] = AV_RL32(buf + x * 4);
00241 }else
00242 #endif
00243 memcpy(dst, buf, img_size);
00244
00245 dst += stride;
00246 buf += img_size;
00247 }
00248 }
00249 }
00250
00251 *picture= *(AVFrame*)&s->picture;
00252 *data_size = sizeof(AVPicture);
00253
00254 return avpkt->size;
00255 }
00256
00257 static av_cold int targa_init(AVCodecContext *avctx){
00258 TargaContext *s = avctx->priv_data;
00259
00260 avcodec_get_frame_defaults((AVFrame*)&s->picture);
00261 avctx->coded_frame= (AVFrame*)&s->picture;
00262
00263 return 0;
00264 }
00265
00266 static av_cold int targa_end(AVCodecContext *avctx){
00267 TargaContext *s = avctx->priv_data;
00268
00269 if(s->picture.data[0])
00270 avctx->release_buffer(avctx, &s->picture);
00271
00272 return 0;
00273 }
00274
00275 AVCodec ff_targa_decoder = {
00276 .name = "targa",
00277 .type = AVMEDIA_TYPE_VIDEO,
00278 .id = CODEC_ID_TARGA,
00279 .priv_data_size = sizeof(TargaContext),
00280 .init = targa_init,
00281 .close = targa_end,
00282 .decode = decode_frame,
00283 .capabilities = CODEC_CAP_DR1,
00284 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
00285 };