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 "libavutil/imgutils.h"
25 #include "bytestream.h"
28 typedef struct TargaContext {
37 #define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
38 if(needed > buf_end - buf){ \
39 av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
43 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)
46 int depth = (bpp + 1) >> 3;
49 const uint8_t *src_end = src + src_size;
51 diff = stride - w * depth;
54 CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
56 count = (type & 0x7F) + 1;
58 if((x + count > w) && (x + count + 1 > (h - y) * w)){
59 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
63 CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
65 CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
67 for(i = 0; i < count; i++){
73 *((uint16_t*)dst) = AV_RL16(src);
81 *((uint32_t*)dst) = AV_RL32(src);
101 static int decode_frame(AVCodecContext *avctx,
102 void *data, int *data_size,
105 const uint8_t *buf = avpkt->data;
106 const uint8_t *buf_end = avpkt->data + avpkt->size;
107 TargaContext * const s = avctx->priv_data;
108 AVFrame *picture = data;
109 AVFrame * const p= (AVFrame*)&s->picture;
112 int idlen, pal, compr, y, w, h, bpp, flags;
113 int first_clr, colors, csize;
115 /* parse image header */
116 CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
120 first_clr = bytestream_get_le16(&buf);
121 colors = bytestream_get_le16(&buf);
123 if (!pal && (first_clr || colors || csize)) {
124 av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
125 // specification says we should ignore those value in this case
126 first_clr = colors = csize = 0;
129 y = bytestream_get_le16(&buf);
130 w = bytestream_get_le16(&buf);
131 h = bytestream_get_le16(&buf);
134 //skip identifier if any
135 CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
142 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
145 avctx->pix_fmt = PIX_FMT_RGB555;
148 avctx->pix_fmt = PIX_FMT_RGB555;
151 avctx->pix_fmt = PIX_FMT_BGR24;
154 avctx->pix_fmt = PIX_FMT_RGB32;
157 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
161 if(s->picture.data[0])
162 avctx->release_buffer(avctx, &s->picture);
164 if(av_image_check_size(w, h, 0, avctx))
166 if(w != avctx->width || h != avctx->height)
167 avcodec_set_dimensions(avctx, w, h);
168 if(avctx->get_buffer(avctx, p) < 0){
169 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
174 stride = p->linesize[0];
175 }else{ //image is upside-down
176 dst = p->data[0] + p->linesize[0] * (h - 1);
177 stride = -p->linesize[0];
182 if((colors + first_clr) > 256){
183 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
187 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
190 pal_size = colors * ((csize + 1) >> 3);
191 CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
192 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
196 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
197 for(t = 0; t < colors; t++){
198 *pal++ = (0xff<<24) | bytestream_get_le24(&buf);
200 p->palette_has_changed = 1;
203 if((compr & (~TGA_RLE)) == TGA_NODATA)
204 memset(p->data[0], 0, p->linesize[0] * s->height);
207 int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
212 size_t img_size = s->width * ((s->bpp + 1) >> 3);
213 CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
214 for(y = 0; y < s->height; y++){
217 if((s->bpp + 1) >> 3 == 2){
218 uint16_t *dst16 = (uint16_t*)dst;
219 for(x = 0; x < s->width; x++)
220 dst16[x] = AV_RL16(buf + x * 2);
221 }else if((s->bpp + 1) >> 3 == 4){
222 uint32_t *dst32 = (uint32_t*)dst;
223 for(x = 0; x < s->width; x++)
224 dst32[x] = AV_RL32(buf + x * 4);
227 memcpy(dst, buf, img_size);
234 if(flags & 0x10){ // right-to-left, needs horizontal flip
236 for(y = 0; y < s->height; y++){
237 void *line = &p->data[0][y * p->linesize[0]];
238 for(x = 0; x < s->width >> 1; x++){
241 FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[s->width - x - 1]);
244 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * s->width - 3 * x - 3]);
245 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * s->width - 3 * x - 2]);
246 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * s->width - 3 * x - 1]);
249 FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[s->width - x - 1]);
252 FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[s->width - x - 1]);
258 *picture= *(AVFrame*)&s->picture;
259 *data_size = sizeof(AVPicture);
264 static av_cold int targa_init(AVCodecContext *avctx){
265 TargaContext *s = avctx->priv_data;
267 avcodec_get_frame_defaults((AVFrame*)&s->picture);
268 avctx->coded_frame= (AVFrame*)&s->picture;
273 static av_cold int targa_end(AVCodecContext *avctx){
274 TargaContext *s = avctx->priv_data;
276 if(s->picture.data[0])
277 avctx->release_buffer(avctx, &s->picture);
282 AVCodec ff_targa_decoder = {
284 .type = AVMEDIA_TYPE_VIDEO,
285 .id = CODEC_ID_TARGA,
286 .priv_data_size = sizeof(TargaContext),
289 .decode = decode_frame,
290 .capabilities = CODEC_CAP_DR1,
291 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),