2 * Targa (.tga) image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 TGA_NODATA = 0, // no image data
26 TGA_PAL = 1, // palettized
27 TGA_RGB = 2, // true-color
28 TGA_BW = 3, // black & white or grayscale
29 TGA_RLE = 8, // flag pointing that data is RLE-coded
32 typedef struct TargaContext {
41 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
44 int depth = (bpp + 1) >> 3;
48 diff = stride - w * depth;
52 count = (type & 0x7F) + 1;
54 if((x + count > w) && (x + count + 1 > (h - y) * w)){
55 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
58 for(i = 0; i < count; i++){
64 *((uint16_t*)dst) = LE_16(src);
88 static int decode_frame(AVCodecContext *avctx,
89 void *data, int *data_size,
90 uint8_t *buf, int buf_size)
92 TargaContext * const s = avctx->priv_data;
93 AVFrame *picture = data;
94 AVFrame * const p= (AVFrame*)&s->picture;
97 int idlen, pal, compr, x, y, w, h, bpp, flags;
98 int first_clr, colors, csize;
100 /* parse image header */
104 first_clr = LE_16(buf); buf += 2;
105 colors = LE_16(buf); buf += 2;
107 x = LE_16(buf); buf += 2;
108 y = LE_16(buf); buf += 2;
109 w = LE_16(buf); buf += 2;
110 h = LE_16(buf); buf += 2;
113 //skip identifier if any
120 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
123 avctx->pix_fmt = PIX_FMT_RGB555;
126 avctx->pix_fmt = PIX_FMT_RGB555;
129 avctx->pix_fmt = PIX_FMT_BGR24;
132 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", avctx->bits_per_sample);
136 if(s->picture.data[0])
137 avctx->release_buffer(avctx, &s->picture);
139 if(avcodec_check_dimensions(avctx, w, h))
141 if(w != avctx->width || h != avctx->height)
142 avcodec_set_dimensions(avctx, w, h);
143 if(avctx->get_buffer(avctx, p) < 0){
144 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
149 stride = p->linesize[0];
150 }else{ //image is upside-down
151 dst = p->data[0] + p->linesize[0] * (h - 1);
152 stride = -p->linesize[0];
155 if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
156 memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
157 if(avctx->palctrl->palette_changed){
158 p->palette_has_changed = 1;
159 avctx->palctrl->palette_changed = 0;
163 if((colors + first_clr) > 256){
164 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
168 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
171 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
172 buf += colors * ((csize + 1) >> 3);
175 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
176 for(t = 0; t < colors; t++){
180 *pal++ = (b << 16) | (g << 8) | r;
182 p->palette_has_changed = 1;
183 avctx->palctrl->palette_changed = 0;
186 if((compr & (~TGA_RLE)) == TGA_NODATA)
187 memset(p->data[0], 0, p->linesize[0] * s->height);
190 targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
192 for(y = 0; y < s->height; y++){
193 #ifdef WORDS_BIGENDIAN
194 if((s->bpp + 1) >> 3 == 2){
195 uint16_t *dst16 = (uint16_t*)dst;
196 for(x = 0; x < s->width; x++)
197 dst16[x] = LE_16(buf + x * 2);
200 memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
203 buf += s->width * ((s->bpp + 1) >> 3);
208 *picture= *(AVFrame*)&s->picture;
209 *data_size = sizeof(AVPicture);
214 static int targa_init(AVCodecContext *avctx){
215 TargaContext *s = avctx->priv_data;
217 avcodec_get_frame_defaults((AVFrame*)&s->picture);
218 avctx->coded_frame= (AVFrame*)&s->picture;
219 s->picture.data[0] = NULL;
224 static int targa_end(AVCodecContext *avctx){
225 TargaContext *s = avctx->priv_data;
227 if(s->picture.data[0])
228 avctx->release_buffer(avctx, &s->picture);
233 AVCodec targa_decoder = {
237 sizeof(TargaContext),