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 {
36 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
37 uint8_t *dst, int w, int h, int stride, int bpp)
40 int depth = (bpp + 1) >> 3;
44 diff = stride - w * depth;
47 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
48 av_log(avctx, AV_LOG_ERROR,
49 "Ran ouf of data before end-of-image\n");
50 return AVERROR_INVALIDDATA;
52 type = bytestream2_get_byteu(&s->gb);
53 count = (type & 0x7F) + 1;
55 if(x + count > (h - y) * w){
56 av_log(avctx, AV_LOG_ERROR,
57 "Packet went out of bounds: position (%i,%i) size %i\n",
59 return AVERROR_INVALIDDATA;
63 int n = FFMIN(count, w - x);
64 bytestream2_get_buffer(&s->gb, dst, n * depth);
76 bytestream2_get_buffer(&s->gb, tmp, depth);
78 int n = FFMIN(count, w - x);
82 memcpy(dst, tmp, depth);
96 static int decode_frame(AVCodecContext *avctx,
97 void *data, int *data_size,
100 TargaContext * const s = avctx->priv_data;
101 AVFrame *picture = data;
102 AVFrame * const p = &s->picture;
105 int idlen, pal, compr, y, w, h, bpp, flags;
106 int first_clr, colors, csize;
108 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
110 /* parse image header */
111 idlen = bytestream2_get_byte(&s->gb);
112 pal = bytestream2_get_byte(&s->gb);
113 compr = bytestream2_get_byte(&s->gb);
114 first_clr = bytestream2_get_le16(&s->gb);
115 colors = bytestream2_get_le16(&s->gb);
116 csize = bytestream2_get_byte(&s->gb);
117 bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
118 w = bytestream2_get_le16(&s->gb);
119 h = bytestream2_get_le16(&s->gb);
120 bpp = bytestream2_get_byte(&s->gb);
122 if (bytestream2_get_bytes_left(&s->gb) <= idlen) {
123 av_log(avctx, AV_LOG_ERROR,
124 "Not enough data to read header\n");
125 return AVERROR_INVALIDDATA;
128 flags = bytestream2_get_byte(&s->gb);
130 if (!pal && (first_clr || colors || csize)) {
131 av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
132 // specification says we should ignore those value in this case
133 first_clr = colors = csize = 0;
136 // skip identifier if any
137 bytestream2_skip(&s->gb, idlen);
141 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
145 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
148 avctx->pix_fmt = AV_PIX_FMT_BGR24;
151 avctx->pix_fmt = AV_PIX_FMT_BGRA;
154 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
158 if(s->picture.data[0])
159 avctx->release_buffer(avctx, &s->picture);
161 if(av_image_check_size(w, h, 0, avctx))
163 if(w != avctx->width || h != avctx->height)
164 avcodec_set_dimensions(avctx, w, h);
165 if(avctx->get_buffer(avctx, p) < 0){
166 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
171 stride = p->linesize[0];
172 }else{ //image is upside-down
173 dst = p->data[0] + p->linesize[0] * (h - 1);
174 stride = -p->linesize[0];
178 int pal_size, pal_sample_size;
179 if((colors + first_clr) > 256){
180 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
184 case 32: pal_sample_size = 4; break;
185 case 24: pal_sample_size = 3; break;
187 case 15: pal_sample_size = 2; break;
189 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
192 pal_size = colors * pal_sample_size;
193 if(avctx->pix_fmt != AV_PIX_FMT_PAL8)//should not occur but skip palette anyway
194 bytestream2_skip(&s->gb, pal_size);
197 uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
199 if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
200 av_log(avctx, AV_LOG_ERROR,
201 "Not enough data to read palette\n");
202 return AVERROR_INVALIDDATA;
204 switch (pal_sample_size) {
206 for (t = 0; t < colors; t++)
207 *pal++ = bytestream2_get_le32u(&s->gb);
211 for (t = 0; t < colors; t++)
212 *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
216 for (t = 0; t < colors; t++) {
217 uint32_t v = bytestream2_get_le16u(&s->gb);
218 v = ((v & 0x7C00) << 9) |
219 ((v & 0x03E0) << 6) |
221 /* left bit replication */
222 v |= (v & 0xE0E0E0U) >> 5;
223 *pal++ = (0xffU<<24) | v;
227 p->palette_has_changed = 1;
230 if ((compr & (~TGA_RLE)) == TGA_NODATA) {
231 memset(p->data[0], 0, p->linesize[0] * h);
234 int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
238 size_t img_size = w * ((bpp + 1) >> 3);
239 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
240 av_log(avctx, AV_LOG_ERROR,
241 "Not enough data available for image\n");
242 return AVERROR_INVALIDDATA;
244 for (y = 0; y < h; y++) {
245 bytestream2_get_bufferu(&s->gb, dst, img_size);
250 if(flags & 0x10){ // right-to-left, needs horizontal flip
252 for(y = 0; y < h; y++){
253 void *line = &p->data[0][y * p->linesize[0]];
254 for(x = 0; x < w >> 1; x++){
257 FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
260 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
261 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
262 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
265 FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
268 FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
274 *picture = s->picture;
275 *data_size = sizeof(AVPicture);
280 static av_cold int targa_init(AVCodecContext *avctx){
281 TargaContext *s = avctx->priv_data;
283 avcodec_get_frame_defaults(&s->picture);
284 avctx->coded_frame = &s->picture;
289 static av_cold int targa_end(AVCodecContext *avctx){
290 TargaContext *s = avctx->priv_data;
292 if(s->picture.data[0])
293 avctx->release_buffer(avctx, &s->picture);
298 AVCodec ff_targa_decoder = {
300 .type = AVMEDIA_TYPE_VIDEO,
301 .id = AV_CODEC_ID_TARGA,
302 .priv_data_size = sizeof(TargaContext),
305 .decode = decode_frame,
306 .capabilities = CODEC_CAP_DR1,
307 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),