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"
26 TGA_NODATA
= 0, // no image data
27 TGA_PAL
= 1, // palettized
28 TGA_RGB
= 2, // true-color
29 TGA_BW
= 3, // black & white or grayscale
30 TGA_RLE
= 8, // flag pointing that data is RLE-coded
33 typedef struct TargaContext
{
42 static void targa_decode_rle(AVCodecContext
*avctx
, TargaContext
*s
, const uint8_t *src
, uint8_t *dst
, int w
, int h
, int stride
, int bpp
)
45 int depth
= (bpp
+ 1) >> 3;
49 diff
= stride
- w
* depth
;
53 count
= (type
& 0x7F) + 1;
55 if((x
+ count
> w
) && (x
+ count
+ 1 > (h
- y
) * w
)){
56 av_log(avctx
, AV_LOG_ERROR
, "Packet went out of bounds: position (%i,%i) size %i\n", x
, y
, count
);
59 for(i
= 0; i
< count
; i
++){
65 *((uint16_t*)dst
) = AV_RL16(src
);
73 *((uint32_t*)dst
) = AV_RL32(src
);
92 static int decode_frame(AVCodecContext
*avctx
,
93 void *data
, int *data_size
,
94 const uint8_t *buf
, int buf_size
)
96 TargaContext
* const s
= avctx
->priv_data
;
97 AVFrame
*picture
= data
;
98 AVFrame
* const p
= (AVFrame
*)&s
->picture
;
101 int idlen
, pal
, compr
, x
, y
, w
, h
, bpp
, flags
;
102 int first_clr
, colors
, csize
;
104 /* parse image header */
108 first_clr
= AV_RL16(buf
); buf
+= 2;
109 colors
= AV_RL16(buf
); buf
+= 2;
111 x
= AV_RL16(buf
); buf
+= 2;
112 y
= AV_RL16(buf
); buf
+= 2;
113 w
= AV_RL16(buf
); buf
+= 2;
114 h
= AV_RL16(buf
); buf
+= 2;
117 //skip identifier if any
124 avctx
->pix_fmt
= ((compr
& (~TGA_RLE
)) == TGA_BW
) ? PIX_FMT_GRAY8
: PIX_FMT_PAL8
;
127 avctx
->pix_fmt
= PIX_FMT_RGB555
;
130 avctx
->pix_fmt
= PIX_FMT_RGB555
;
133 avctx
->pix_fmt
= PIX_FMT_BGR24
;
136 avctx
->pix_fmt
= PIX_FMT_RGB32
;
139 av_log(avctx
, AV_LOG_ERROR
, "Bit depth %i is not supported\n", s
->bpp
);
143 if(s
->picture
.data
[0])
144 avctx
->release_buffer(avctx
, &s
->picture
);
146 if(avcodec_check_dimensions(avctx
, w
, h
))
148 if(w
!= avctx
->width
|| h
!= avctx
->height
)
149 avcodec_set_dimensions(avctx
, w
, h
);
150 if(avctx
->get_buffer(avctx
, p
) < 0){
151 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
156 stride
= p
->linesize
[0];
157 }else{ //image is upside-down
158 dst
= p
->data
[0] + p
->linesize
[0] * (h
- 1);
159 stride
= -p
->linesize
[0];
162 if(avctx
->pix_fmt
== PIX_FMT_PAL8
&& avctx
->palctrl
){
163 memcpy(p
->data
[1], avctx
->palctrl
->palette
, AVPALETTE_SIZE
);
164 if(avctx
->palctrl
->palette_changed
){
165 p
->palette_has_changed
= 1;
166 avctx
->palctrl
->palette_changed
= 0;
170 if((colors
+ first_clr
) > 256){
171 av_log(avctx
, AV_LOG_ERROR
, "Incorrect palette: %i colors with offset %i\n", colors
, first_clr
);
175 av_log(avctx
, AV_LOG_ERROR
, "Palette entry size %i bits is not supported\n", csize
);
178 if(avctx
->pix_fmt
!= PIX_FMT_PAL8
)//should not occur but skip palette anyway
179 buf
+= colors
* ((csize
+ 1) >> 3);
182 int32_t *pal
= ((int32_t*)p
->data
[1]) + first_clr
;
183 for(t
= 0; t
< colors
; t
++){
187 *pal
++ = (b
<< 16) | (g
<< 8) | r
;
189 p
->palette_has_changed
= 1;
190 avctx
->palctrl
->palette_changed
= 0;
193 if((compr
& (~TGA_RLE
)) == TGA_NODATA
)
194 memset(p
->data
[0], 0, p
->linesize
[0] * s
->height
);
197 targa_decode_rle(avctx
, s
, buf
, dst
, avctx
->width
, avctx
->height
, stride
, bpp
);
199 for(y
= 0; y
< s
->height
; y
++){
200 #ifdef WORDS_BIGENDIAN
201 if((s
->bpp
+ 1) >> 3 == 2){
202 uint16_t *dst16
= (uint16_t*)dst
;
203 for(x
= 0; x
< s
->width
; x
++)
204 dst16
[x
] = AV_RL16(buf
+ x
* 2);
205 }else if((s
->bpp
+ 1) >> 3 == 4){
206 uint32_t *dst32
= (uint32_t*)dst
;
207 for(x
= 0; x
< s
->width
; x
++)
208 dst32
[x
] = AV_RL32(buf
+ x
* 4);
211 memcpy(dst
, buf
, s
->width
* ((s
->bpp
+ 1) >> 3));
214 buf
+= s
->width
* ((s
->bpp
+ 1) >> 3);
219 *picture
= *(AVFrame
*)&s
->picture
;
220 *data_size
= sizeof(AVPicture
);
225 static av_cold
int targa_init(AVCodecContext
*avctx
){
226 TargaContext
*s
= avctx
->priv_data
;
228 avcodec_get_frame_defaults((AVFrame
*)&s
->picture
);
229 avctx
->coded_frame
= (AVFrame
*)&s
->picture
;
230 s
->picture
.data
[0] = NULL
;
235 static av_cold
int targa_end(AVCodecContext
*avctx
){
236 TargaContext
*s
= avctx
->priv_data
;
238 if(s
->picture
.data
[0])
239 avctx
->release_buffer(avctx
, &s
->picture
);
244 AVCodec targa_decoder
= {
248 sizeof(TargaContext
),
255 .long_name
= NULL_IF_CONFIG_SMALL("Truevision Targa image"),