2 * Targa (.tga) image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
29 typedef struct TargaContext {
37 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
38 uint8_t *dst, int w, int h, int stride, int bpp)
41 int depth = (bpp + 1) >> 3;
45 diff = stride - w * depth;
48 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
49 av_log(avctx, AV_LOG_ERROR,
50 "Ran ouf of data before end-of-image\n");
51 return AVERROR_INVALIDDATA;
53 type = bytestream2_get_byteu(&s->gb);
54 count = (type & 0x7F) + 1;
56 if (x + count > w && x + count + 1 > (h - y) * w) {
57 av_log(avctx, AV_LOG_ERROR,
58 "Packet went out of bounds: position (%i,%i) size %i\n",
60 return AVERROR_INVALIDDATA;
64 int n = FFMIN(count, w - x);
65 bytestream2_get_buffer(&s->gb, dst, n * depth);
77 bytestream2_get_buffer(&s->gb, tmp, depth);
79 int n = FFMIN(count, w - x);
83 memcpy(dst, tmp, depth);
97 static int decode_frame(AVCodecContext *avctx,
98 void *data, int *got_frame,
101 TargaContext * const s = avctx->priv_data;
102 AVFrame *picture = data;
103 AVFrame * const p = &s->picture;
106 int idlen, compr, y, w, h, bpp, flags, ret;
107 int first_clr, colors, csize;
109 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
111 /* parse image header */
112 idlen = bytestream2_get_byte(&s->gb);
113 bytestream2_skip(&s->gb, 1); /* pal */
114 compr = bytestream2_get_byte(&s->gb);
115 first_clr = bytestream2_get_le16(&s->gb);
116 colors = bytestream2_get_le16(&s->gb);
117 csize = bytestream2_get_byte(&s->gb);
118 bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
119 w = bytestream2_get_le16(&s->gb);
120 h = bytestream2_get_le16(&s->gb);
121 bpp = bytestream2_get_byte(&s->gb);
122 flags = bytestream2_get_byte(&s->gb);
123 // skip identifier if any
124 bytestream2_skip(&s->gb, idlen);
128 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
131 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
134 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
137 avctx->pix_fmt = AV_PIX_FMT_BGR24;
140 avctx->pix_fmt = AV_PIX_FMT_BGRA;
143 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
144 return AVERROR_INVALIDDATA;
147 if(s->picture.data[0])
148 avctx->release_buffer(avctx, &s->picture);
150 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
152 if(w != avctx->width || h != avctx->height)
153 avcodec_set_dimensions(avctx, w, h);
154 if ((ret = ff_get_buffer(avctx, p)) < 0){
155 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
160 stride = p->linesize[0];
161 }else{ //image is upside-down
162 dst = p->data[0] + p->linesize[0] * (h - 1);
163 stride = -p->linesize[0];
167 int pal_size, pal_sample_size;
168 if((colors + first_clr) > 256){
169 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
170 return AVERROR_INVALIDDATA;
173 case 24: pal_sample_size = 3; break;
175 case 15: pal_sample_size = 2; break;
177 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
178 return AVERROR_INVALIDDATA;
180 pal_size = colors * pal_sample_size;
181 if(avctx->pix_fmt != AV_PIX_FMT_PAL8)//should not occur but skip palette anyway
182 bytestream2_skip(&s->gb, pal_size);
185 uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
187 if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
188 av_log(avctx, AV_LOG_ERROR,
189 "Not enough data to read palette\n");
190 return AVERROR_INVALIDDATA;
192 switch (pal_sample_size) {
195 for (t = 0; t < colors; t++)
196 *pal++ = bytestream2_get_le24u(&s->gb);
200 for (t = 0; t < colors; t++) {
201 uint32_t v = bytestream2_get_le16u(&s->gb);
202 v = ((v & 0x7C00) << 9) |
203 ((v & 0x03E0) << 6) |
205 /* left bit replication */
206 v |= (v & 0xE0E0E0U) >> 5;
211 p->palette_has_changed = 1;
214 if ((compr & (~TGA_RLE)) == TGA_NODATA) {
215 memset(p->data[0], 0, p->linesize[0] * h);
218 int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
222 size_t img_size = w * ((bpp + 1) >> 3);
223 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
224 av_log(avctx, AV_LOG_ERROR,
225 "Not enough data available for image\n");
226 return AVERROR_INVALIDDATA;
228 for (y = 0; y < h; y++) {
229 bytestream2_get_bufferu(&s->gb, dst, img_size);
235 *picture = s->picture;
241 static av_cold int targa_init(AVCodecContext *avctx){
242 TargaContext *s = avctx->priv_data;
244 avcodec_get_frame_defaults(&s->picture);
245 avctx->coded_frame = &s->picture;
250 static av_cold int targa_end(AVCodecContext *avctx){
251 TargaContext *s = avctx->priv_data;
253 if(s->picture.data[0])
254 avctx->release_buffer(avctx, &s->picture);
259 AVCodec ff_targa_decoder = {
261 .type = AVMEDIA_TYPE_VIDEO,
262 .id = AV_CODEC_ID_TARGA,
263 .priv_data_size = sizeof(TargaContext),
266 .decode = decode_frame,
267 .capabilities = CODEC_CAP_DR1,
268 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),