1/* 2 * Targa (.tga) image decoder 3 * Copyright (c) 2006 Konstantin Shishkov 4 * 5 * This file is part of FFmpeg. 6 * 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. 11 * 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. 16 * 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 20 */ 21 22#include "libavutil/intreadwrite.h" 23#include "libavutil/imgutils.h" 24#include "avcodec.h" 25#include "bytestream.h" 26#include "codec_internal.h" 27#include "internal.h" 28#include "targa.h" 29 30typedef struct TargaContext { 31 GetByteContext gb; 32} TargaContext; 33 34static uint8_t *advance_line(uint8_t *start, uint8_t *line, 35 int stride, int *y, int h, int interleave) 36{ 37 *y += interleave; 38 39 if (*y < h) { 40 return line + interleave * stride; 41 } else { 42 *y = (*y + 1) & (interleave - 1); 43 if (*y && *y < h) { 44 return start + *y * stride; 45 } else { 46 return NULL; 47 } 48 } 49} 50 51static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, 52 uint8_t *start, int w, int h, int stride, 53 int bpp, int interleave) 54{ 55 int x, y; 56 int depth = (bpp + 1) >> 3; 57 int type, count; 58 uint8_t *line = start; 59 uint8_t *dst = line; 60 61 x = y = count = 0; 62 while (dst) { 63 if (bytestream2_get_bytes_left(&s->gb) <= 0) { 64 av_log(avctx, AV_LOG_ERROR, 65 "Ran ouf of data before end-of-image\n"); 66 return AVERROR_INVALIDDATA; 67 } 68 type = bytestream2_get_byteu(&s->gb); 69 count = (type & 0x7F) + 1; 70 type &= 0x80; 71 if (!type) { 72 do { 73 int n = FFMIN(count, w - x); 74 bytestream2_get_buffer(&s->gb, dst, n * depth); 75 count -= n; 76 dst += n * depth; 77 x += n; 78 if (x == w) { 79 x = 0; 80 dst = line = advance_line(start, line, stride, &y, h, interleave); 81 } 82 } while (dst && count > 0); 83 } else { 84 uint8_t tmp[4]; 85 bytestream2_get_buffer(&s->gb, tmp, depth); 86 do { 87 int n = FFMIN(count, w - x); 88 count -= n; 89 x += n; 90 do { 91 memcpy(dst, tmp, depth); 92 dst += depth; 93 } while (--n); 94 if (x == w) { 95 x = 0; 96 dst = line = advance_line(start, line, stride, &y, h, interleave); 97 } 98 } while (dst && count > 0); 99 } 100 } 101 102 if (count) { 103 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n"); 104 return AVERROR_INVALIDDATA; 105 } 106 107 return 0; 108} 109 110static int decode_frame(AVCodecContext *avctx, AVFrame *p, 111 int *got_frame, AVPacket *avpkt) 112{ 113 TargaContext * const s = avctx->priv_data; 114 uint8_t *dst; 115 int stride; 116 int idlen, pal, compr, y, w, h, bpp, flags, ret; 117 int first_clr, colors, csize; 118 int interleave; 119 size_t img_size; 120 121 bytestream2_init(&s->gb, avpkt->data, avpkt->size); 122 123 /* parse image header */ 124 idlen = bytestream2_get_byte(&s->gb); 125 pal = bytestream2_get_byte(&s->gb); 126 compr = bytestream2_get_byte(&s->gb); 127 first_clr = bytestream2_get_le16(&s->gb); 128 colors = bytestream2_get_le16(&s->gb); 129 csize = bytestream2_get_byte(&s->gb); 130 bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */ 131 w = bytestream2_get_le16(&s->gb); 132 h = bytestream2_get_le16(&s->gb); 133 bpp = bytestream2_get_byte(&s->gb); 134 135 flags = bytestream2_get_byte(&s->gb); 136 137 if (!pal && (first_clr || colors || csize)) { 138 av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n"); 139 // specification says we should ignore those value in this case 140 first_clr = colors = csize = 0; 141 } 142 143 if (bytestream2_get_bytes_left(&s->gb) < idlen + 2*colors) { 144 av_log(avctx, AV_LOG_ERROR, 145 "Not enough data to read header\n"); 146 return AVERROR_INVALIDDATA; 147 } 148 149 // skip identifier if any 150 bytestream2_skip(&s->gb, idlen); 151 152 switch (bpp) { 153 case 8: 154 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8; 155 break; 156 case 15: 157 case 16: 158 avctx->pix_fmt = AV_PIX_FMT_RGB555LE; 159 break; 160 case 24: 161 avctx->pix_fmt = AV_PIX_FMT_BGR24; 162 break; 163 case 32: 164 avctx->pix_fmt = AV_PIX_FMT_BGRA; 165 break; 166 default: 167 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp); 168 return AVERROR_INVALIDDATA; 169 } 170 171 if (colors && (colors + first_clr) > 256) { 172 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr); 173 return AVERROR_INVALIDDATA; 174 } 175 176 if ((ret = ff_set_dimensions(avctx, w, h)) < 0) 177 return ret; 178 179 if ((compr & (~TGA_RLE)) == TGA_NODATA) { 180 return avpkt->size; 181 } 182 183 if (!(compr & TGA_RLE)) { 184 img_size = w * ((bpp + 1) >> 3); 185 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) { 186 av_log(avctx, AV_LOG_ERROR, 187 "Not enough data available for image\n"); 188 return AVERROR_INVALIDDATA; 189 } 190 } 191 192 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) 193 return ret; 194 p->pict_type = AV_PICTURE_TYPE_I; 195 196 if (flags & TGA_TOPTOBOTTOM) { 197 dst = p->data[0]; 198 stride = p->linesize[0]; 199 } else { //image is upside-down 200 dst = p->data[0] + p->linesize[0] * (h - 1); 201 stride = -p->linesize[0]; 202 } 203 204 interleave = flags & TGA_INTERLEAVE2 ? 2 : 205 flags & TGA_INTERLEAVE4 ? 4 : 1; 206 207 if (colors) { 208 int pal_size, pal_sample_size; 209 210 switch (csize) { 211 case 32: pal_sample_size = 4; break; 212 case 24: pal_sample_size = 3; break; 213 case 16: 214 case 15: pal_sample_size = 2; break; 215 default: 216 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize); 217 return AVERROR_INVALIDDATA; 218 } 219 pal_size = colors * pal_sample_size; 220 if (avctx->pix_fmt != AV_PIX_FMT_PAL8) //should not occur but skip palette anyway 221 bytestream2_skip(&s->gb, pal_size); 222 else { 223 int t; 224 uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr; 225 226 if (bytestream2_get_bytes_left(&s->gb) < pal_size) { 227 av_log(avctx, AV_LOG_ERROR, 228 "Not enough data to read palette\n"); 229 return AVERROR_INVALIDDATA; 230 } 231 switch (pal_sample_size) { 232 case 4: 233 for (t = 0; t < colors; t++) 234 *pal++ = bytestream2_get_le32u(&s->gb); 235 break; 236 case 3: 237 /* RGB24 */ 238 for (t = 0; t < colors; t++) 239 *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb); 240 break; 241 case 2: 242 /* RGB555 */ 243 for (t = 0; t < colors; t++) { 244 uint32_t v = bytestream2_get_le16u(&s->gb); 245 v = ((v & 0x7C00) << 9) | 246 ((v & 0x03E0) << 6) | 247 ((v & 0x001F) << 3); 248 /* left bit replication */ 249 v |= (v & 0xE0E0E0U) >> 5; 250 *pal++ = (0xffU<<24) | v; 251 } 252 break; 253 } 254 p->palette_has_changed = 1; 255 } 256 } 257 258 if (compr & TGA_RLE) { 259 int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave); 260 if (res < 0) 261 return res; 262 } else { 263 uint8_t *line; 264 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) { 265 av_log(avctx, AV_LOG_ERROR, 266 "Not enough data available for image\n"); 267 return AVERROR_INVALIDDATA; 268 } 269 270 line = dst; 271 y = 0; 272 do { 273 bytestream2_get_buffer(&s->gb, line, img_size); 274 line = advance_line(dst, line, stride, &y, h, interleave); 275 } while (line); 276 } 277 278 if (flags & TGA_RIGHTTOLEFT) { // right-to-left, needs horizontal flip 279 int x; 280 for (y = 0; y < h; y++) { 281 void *line = &p->data[0][y * p->linesize[0]]; 282 for (x = 0; x < w >> 1; x++) { 283 switch (bpp) { 284 case 32: 285 FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]); 286 break; 287 case 24: 288 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]); 289 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]); 290 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]); 291 break; 292 case 16: 293 FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]); 294 break; 295 case 8: 296 FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]); 297 } 298 } 299 } 300 } 301 302 303 *got_frame = 1; 304 305 return avpkt->size; 306} 307 308const FFCodec ff_targa_decoder = { 309 .p.name = "targa", 310 .p.long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"), 311 .p.type = AVMEDIA_TYPE_VIDEO, 312 .p.id = AV_CODEC_ID_TARGA, 313 .p.capabilities = AV_CODEC_CAP_DR1, 314 .priv_data_size = sizeof(TargaContext), 315 FF_CODEC_DECODE_CB(decode_frame), 316}; 317