2006-10-11 06:15:04 +02:00
|
|
|
/*
|
|
|
|
* Targa (.tga) image decoder
|
|
|
|
* Copyright (c) 2006 Konstantin Shishkov
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2009-01-11 23:19:48 +01:00
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-02-07 14:37:08 +01:00
|
|
|
#include "libavutil/imgutils.h"
|
2006-10-11 06:15:04 +02:00
|
|
|
#include "avcodec.h"
|
2010-10-13 12:39:45 +02:00
|
|
|
#include "targa.h"
|
2006-10-11 06:15:04 +02:00
|
|
|
|
|
|
|
typedef struct TargaContext {
|
|
|
|
AVFrame picture;
|
|
|
|
|
|
|
|
int width, height;
|
|
|
|
int bpp;
|
|
|
|
int color_type;
|
|
|
|
int compression_type;
|
|
|
|
} TargaContext;
|
|
|
|
|
2011-02-18 10:35:51 +01:00
|
|
|
#define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
|
2011-02-21 19:44:37 +01:00
|
|
|
if(needed > buf_end - buf){ \
|
2011-02-18 10:35:51 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
|
|
|
|
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)
|
2006-10-11 06:15:04 +02:00
|
|
|
{
|
|
|
|
int i, x, y;
|
|
|
|
int depth = (bpp + 1) >> 3;
|
|
|
|
int type, count;
|
|
|
|
int diff;
|
2011-02-18 10:35:51 +01:00
|
|
|
const uint8_t *src_end = src + src_size;
|
2006-10-11 06:15:04 +02:00
|
|
|
|
|
|
|
diff = stride - w * depth;
|
|
|
|
x = y = 0;
|
|
|
|
while(y < h){
|
2011-02-18 10:35:51 +01:00
|
|
|
CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
|
2006-10-11 06:15:04 +02:00
|
|
|
type = *src++;
|
|
|
|
count = (type & 0x7F) + 1;
|
|
|
|
type &= 0x80;
|
|
|
|
if((x + count > w) && (x + count + 1 > (h - y) * w)){
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
|
2011-02-18 10:35:51 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(type){
|
|
|
|
CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
|
|
|
|
}else{
|
|
|
|
CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
|
2006-10-11 06:15:04 +02:00
|
|
|
}
|
|
|
|
for(i = 0; i < count; i++){
|
|
|
|
switch(depth){
|
|
|
|
case 1:
|
|
|
|
*dst = *src;
|
|
|
|
break;
|
|
|
|
case 2:
|
2007-01-19 23:12:59 +01:00
|
|
|
*((uint16_t*)dst) = AV_RL16(src);
|
2006-10-11 06:15:04 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[1];
|
|
|
|
dst[2] = src[2];
|
|
|
|
break;
|
2006-11-03 14:13:08 +01:00
|
|
|
case 4:
|
2007-01-19 23:12:59 +01:00
|
|
|
*((uint32_t*)dst) = AV_RL32(src);
|
2006-11-03 14:13:08 +01:00
|
|
|
break;
|
2006-10-11 06:15:04 +02:00
|
|
|
}
|
|
|
|
dst += depth;
|
|
|
|
if(!type)
|
|
|
|
src += depth;
|
|
|
|
|
|
|
|
x++;
|
|
|
|
if(x == w){
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
dst += diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(type)
|
|
|
|
src += depth;
|
|
|
|
}
|
2011-02-18 10:35:51 +01:00
|
|
|
return src_size;
|
2006-10-11 06:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_frame(AVCodecContext *avctx,
|
|
|
|
void *data, int *data_size,
|
2009-04-07 17:59:50 +02:00
|
|
|
AVPacket *avpkt)
|
2006-10-11 06:15:04 +02:00
|
|
|
{
|
2009-04-07 17:59:50 +02:00
|
|
|
const uint8_t *buf = avpkt->data;
|
2011-02-18 10:35:51 +01:00
|
|
|
const uint8_t *buf_end = avpkt->data + avpkt->size;
|
2006-10-11 06:15:04 +02:00
|
|
|
TargaContext * const s = avctx->priv_data;
|
|
|
|
AVFrame *picture = data;
|
|
|
|
AVFrame * const p= (AVFrame*)&s->picture;
|
|
|
|
uint8_t *dst;
|
|
|
|
int stride;
|
|
|
|
int idlen, pal, compr, x, y, w, h, bpp, flags;
|
|
|
|
int first_clr, colors, csize;
|
|
|
|
|
|
|
|
/* parse image header */
|
2011-02-18 10:35:51 +01:00
|
|
|
CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
|
2006-10-11 06:15:04 +02:00
|
|
|
idlen = *buf++;
|
|
|
|
pal = *buf++;
|
|
|
|
compr = *buf++;
|
2007-01-19 23:12:59 +01:00
|
|
|
first_clr = AV_RL16(buf); buf += 2;
|
|
|
|
colors = AV_RL16(buf); buf += 2;
|
2006-10-11 06:15:04 +02:00
|
|
|
csize = *buf++;
|
2007-01-19 23:12:59 +01:00
|
|
|
x = AV_RL16(buf); buf += 2;
|
|
|
|
y = AV_RL16(buf); buf += 2;
|
|
|
|
w = AV_RL16(buf); buf += 2;
|
|
|
|
h = AV_RL16(buf); buf += 2;
|
2006-10-11 06:15:04 +02:00
|
|
|
bpp = *buf++;
|
|
|
|
flags = *buf++;
|
|
|
|
//skip identifier if any
|
2011-02-18 10:35:51 +01:00
|
|
|
CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
|
2006-10-11 06:15:04 +02:00
|
|
|
buf += idlen;
|
|
|
|
s->bpp = bpp;
|
|
|
|
s->width = w;
|
|
|
|
s->height = h;
|
|
|
|
switch(s->bpp){
|
|
|
|
case 8:
|
|
|
|
avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
avctx->pix_fmt = PIX_FMT_RGB555;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
avctx->pix_fmt = PIX_FMT_RGB555;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
avctx->pix_fmt = PIX_FMT_BGR24;
|
|
|
|
break;
|
2006-11-03 14:13:08 +01:00
|
|
|
case 32:
|
2007-02-07 02:48:09 +01:00
|
|
|
avctx->pix_fmt = PIX_FMT_RGB32;
|
2006-11-03 14:13:08 +01:00
|
|
|
break;
|
2006-10-11 06:15:04 +02:00
|
|
|
default:
|
2006-11-03 05:42:11 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
|
2006-10-11 06:15:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->picture.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->picture);
|
|
|
|
|
2010-09-07 21:15:29 +02:00
|
|
|
if(av_image_check_size(w, h, 0, avctx))
|
2006-10-11 06:15:04 +02:00
|
|
|
return -1;
|
|
|
|
if(w != avctx->width || h != avctx->height)
|
|
|
|
avcodec_set_dimensions(avctx, w, h);
|
|
|
|
if(avctx->get_buffer(avctx, p) < 0){
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(flags & 0x20){
|
|
|
|
dst = p->data[0];
|
|
|
|
stride = p->linesize[0];
|
|
|
|
}else{ //image is upside-down
|
|
|
|
dst = p->data[0] + p->linesize[0] * (h - 1);
|
|
|
|
stride = -p->linesize[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(colors){
|
2011-02-18 10:35:51 +01:00
|
|
|
size_t pal_size;
|
2006-10-11 06:15:04 +02:00
|
|
|
if((colors + first_clr) > 256){
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(csize != 24){
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-18 10:35:51 +01:00
|
|
|
pal_size = colors * ((csize + 1) >> 3);
|
|
|
|
CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
|
2006-10-11 06:15:04 +02:00
|
|
|
if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
|
2011-02-18 10:35:51 +01:00
|
|
|
buf += pal_size;
|
2006-10-11 06:15:04 +02:00
|
|
|
else{
|
|
|
|
int r, g, b, t;
|
|
|
|
int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
|
|
|
|
for(t = 0; t < colors; t++){
|
|
|
|
b = *buf++;
|
2011-04-27 21:49:45 +02:00
|
|
|
g = *buf++;
|
|
|
|
r = *buf++;
|
|
|
|
*pal++ = (0xff<<24) | (r << 16) | (g << 8) | b;
|
2006-10-11 06:15:04 +02:00
|
|
|
}
|
|
|
|
p->palette_has_changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((compr & (~TGA_RLE)) == TGA_NODATA)
|
|
|
|
memset(p->data[0], 0, p->linesize[0] * s->height);
|
|
|
|
else{
|
2011-02-18 10:35:51 +01:00
|
|
|
if(compr & TGA_RLE){
|
|
|
|
int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
|
|
|
|
if (res < 0)
|
|
|
|
return -1;
|
|
|
|
buf += res;
|
|
|
|
}else{
|
|
|
|
size_t img_size = s->width * ((s->bpp + 1) >> 3);
|
|
|
|
CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
|
2006-10-11 06:15:04 +02:00
|
|
|
for(y = 0; y < s->height; y++){
|
2009-07-26 14:20:04 +02:00
|
|
|
#if HAVE_BIGENDIAN
|
2006-10-11 06:15:04 +02:00
|
|
|
if((s->bpp + 1) >> 3 == 2){
|
|
|
|
uint16_t *dst16 = (uint16_t*)dst;
|
|
|
|
for(x = 0; x < s->width; x++)
|
2007-01-19 23:12:59 +01:00
|
|
|
dst16[x] = AV_RL16(buf + x * 2);
|
2006-11-03 14:13:08 +01:00
|
|
|
}else if((s->bpp + 1) >> 3 == 4){
|
|
|
|
uint32_t *dst32 = (uint32_t*)dst;
|
|
|
|
for(x = 0; x < s->width; x++)
|
2007-01-19 23:12:59 +01:00
|
|
|
dst32[x] = AV_RL32(buf + x * 4);
|
2006-10-11 06:15:04 +02:00
|
|
|
}else
|
|
|
|
#endif
|
2011-02-18 10:35:51 +01:00
|
|
|
memcpy(dst, buf, img_size);
|
2006-10-11 06:15:04 +02:00
|
|
|
|
|
|
|
dst += stride;
|
2011-02-18 10:35:51 +01:00
|
|
|
buf += img_size;
|
2006-10-11 06:15:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*picture= *(AVFrame*)&s->picture;
|
|
|
|
*data_size = sizeof(AVPicture);
|
|
|
|
|
2011-02-18 10:35:51 +01:00
|
|
|
return avpkt->size;
|
2006-10-11 06:15:04 +02:00
|
|
|
}
|
|
|
|
|
2008-03-21 04:11:20 +01:00
|
|
|
static av_cold int targa_init(AVCodecContext *avctx){
|
2006-10-11 06:15:04 +02:00
|
|
|
TargaContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
avcodec_get_frame_defaults((AVFrame*)&s->picture);
|
|
|
|
avctx->coded_frame= (AVFrame*)&s->picture;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 04:11:20 +01:00
|
|
|
static av_cold int targa_end(AVCodecContext *avctx){
|
2006-10-11 06:15:04 +02:00
|
|
|
TargaContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
if(s->picture.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->picture);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_targa_decoder = {
|
2006-10-11 06:15:04 +02:00
|
|
|
"targa",
|
2010-03-31 01:30:55 +02:00
|
|
|
AVMEDIA_TYPE_VIDEO,
|
2006-10-11 06:15:04 +02:00
|
|
|
CODEC_ID_TARGA,
|
|
|
|
sizeof(TargaContext),
|
|
|
|
targa_init,
|
|
|
|
NULL,
|
|
|
|
targa_end,
|
|
|
|
decode_frame,
|
2009-06-05 10:42:32 +02:00
|
|
|
CODEC_CAP_DR1,
|
2008-04-29 23:31:21 +02:00
|
|
|
NULL,
|
2008-06-12 23:50:13 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
|
2006-10-11 06:15:04 +02:00
|
|
|
};
|