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
22 #include "libavutil/intreadwrite.h"
23 #include "libavcore/imgutils.h"
27 typedef struct TargaContext {
36 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
39 int depth = (bpp + 1) >> 3;
43 diff = stride - w * depth;
47 count = (type & 0x7F) + 1;
49 if((x + count > w) && (x + count + 1 > (h - y) * w)){
50 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
53 for(i = 0; i < count; i++){
59 *((uint16_t*)dst) = AV_RL16(src);
67 *((uint32_t*)dst) = AV_RL32(src);
86 static int decode_frame(AVCodecContext *avctx,
87 void *data, int *data_size,
90 const uint8_t *buf = avpkt->data;
91 int buf_size = avpkt->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 = AV_RL16(buf); buf += 2;
105 colors = AV_RL16(buf); buf += 2;
107 x = AV_RL16(buf); buf += 2;
108 y = AV_RL16(buf); buf += 2;
109 w = AV_RL16(buf); buf += 2;
110 h = AV_RL16(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 avctx->pix_fmt = PIX_FMT_RGB32;
135 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
139 if(s->picture.data[0])
140 avctx->release_buffer(avctx, &s->picture);
142 if(av_image_check_size(w, h, 0, avctx))
144 if(w != avctx->width || h != avctx->height)
145 avcodec_set_dimensions(avctx, w, h);
146 if(avctx->get_buffer(avctx, p) < 0){
147 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
152 stride = p->linesize[0];
153 }else{ //image is upside-down
154 dst = p->data[0] + p->linesize[0] * (h - 1);
155 stride = -p->linesize[0];
158 if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
159 memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
160 if(avctx->palctrl->palette_changed){
161 p->palette_has_changed = 1;
162 avctx->palctrl->palette_changed = 0;
166 if((colors + first_clr) > 256){
167 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
171 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
174 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
175 buf += colors * ((csize + 1) >> 3);
178 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
179 for(t = 0; t < colors; t++){
183 *pal++ = (b << 16) | (g << 8) | r;
185 p->palette_has_changed = 1;
188 if((compr & (~TGA_RLE)) == TGA_NODATA)
189 memset(p->data[0], 0, p->linesize[0] * s->height);
192 targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
194 for(y = 0; y < s->height; y++){
196 if((s->bpp + 1) >> 3 == 2){
197 uint16_t *dst16 = (uint16_t*)dst;
198 for(x = 0; x < s->width; x++)
199 dst16[x] = AV_RL16(buf + x * 2);
200 }else if((s->bpp + 1) >> 3 == 4){
201 uint32_t *dst32 = (uint32_t*)dst;
202 for(x = 0; x < s->width; x++)
203 dst32[x] = AV_RL32(buf + x * 4);
206 memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
209 buf += s->width * ((s->bpp + 1) >> 3);
214 *picture= *(AVFrame*)&s->picture;
215 *data_size = sizeof(AVPicture);
220 static av_cold int targa_init(AVCodecContext *avctx){
221 TargaContext *s = avctx->priv_data;
223 avcodec_get_frame_defaults((AVFrame*)&s->picture);
224 avctx->coded_frame= (AVFrame*)&s->picture;
229 static av_cold int targa_end(AVCodecContext *avctx){
230 TargaContext *s = avctx->priv_data;
232 if(s->picture.data[0])
233 avctx->release_buffer(avctx, &s->picture);
238 AVCodec targa_decoder = {
242 sizeof(TargaContext),
249 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),