00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/imgutils.h"
00024 #include "avcodec.h"
00025 #include "bytestream.h"
00026 #include "internal.h"
00027 #include "targa.h"
00028
00029 typedef struct TargaContext {
00030 AVFrame picture;
00031 GetByteContext gb;
00032 } TargaContext;
00033
00034 static uint8_t *advance_line(uint8_t *start, uint8_t *line,
00035 int stride, int *y, int h, int interleave)
00036 {
00037 *y += interleave;
00038
00039 if (*y < h) {
00040 return line + interleave * stride;
00041 } else {
00042 *y = (*y + 1) & (interleave - 1);
00043 if (*y) {
00044 return start + *y * stride;
00045 } else {
00046 return NULL;
00047 }
00048 }
00049 }
00050
00051 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
00052 uint8_t *start, int w, int h, int stride,
00053 int bpp, int interleave)
00054 {
00055 int x, y;
00056 int depth = (bpp + 1) >> 3;
00057 int type, count;
00058 uint8_t *line = start;
00059 uint8_t *dst = line;
00060
00061 x = y = count = 0;
00062 while (dst) {
00063 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
00064 av_log(avctx, AV_LOG_ERROR,
00065 "Ran ouf of data before end-of-image\n");
00066 return AVERROR_INVALIDDATA;
00067 }
00068 type = bytestream2_get_byteu(&s->gb);
00069 count = (type & 0x7F) + 1;
00070 type &= 0x80;
00071 if (!type) {
00072 do {
00073 int n = FFMIN(count, w - x);
00074 bytestream2_get_buffer(&s->gb, dst, n * depth);
00075 count -= n;
00076 dst += n * depth;
00077 x += n;
00078 if (x == w) {
00079 x = 0;
00080 dst = line = advance_line(start, line, stride, &y, h, interleave);
00081 }
00082 } while (dst && count > 0);
00083 } else {
00084 uint8_t tmp[4];
00085 bytestream2_get_buffer(&s->gb, tmp, depth);
00086 do {
00087 int n = FFMIN(count, w - x);
00088 count -= n;
00089 x += n;
00090 do {
00091 memcpy(dst, tmp, depth);
00092 dst += depth;
00093 } while (--n);
00094 if (x == w) {
00095 x = 0;
00096 dst = line = advance_line(start, line, stride, &y, h, interleave);
00097 }
00098 } while (dst && count > 0);
00099 }
00100 }
00101
00102 if (count) {
00103 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n");
00104 return AVERROR_INVALIDDATA;
00105 }
00106
00107 return 0;
00108 }
00109
00110 static int decode_frame(AVCodecContext *avctx,
00111 void *data, int *got_frame,
00112 AVPacket *avpkt)
00113 {
00114 TargaContext * const s = avctx->priv_data;
00115 AVFrame *picture = data;
00116 AVFrame * const p = &s->picture;
00117 uint8_t *dst;
00118 int stride;
00119 int idlen, pal, compr, y, w, h, bpp, flags, ret;
00120 int first_clr, colors, csize;
00121 int interleave;
00122
00123 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
00124
00125
00126 idlen = bytestream2_get_byte(&s->gb);
00127 pal = bytestream2_get_byte(&s->gb);
00128 compr = bytestream2_get_byte(&s->gb);
00129 first_clr = bytestream2_get_le16(&s->gb);
00130 colors = bytestream2_get_le16(&s->gb);
00131 csize = bytestream2_get_byte(&s->gb);
00132 bytestream2_skip(&s->gb, 4);
00133 w = bytestream2_get_le16(&s->gb);
00134 h = bytestream2_get_le16(&s->gb);
00135 bpp = bytestream2_get_byte(&s->gb);
00136
00137 if (bytestream2_get_bytes_left(&s->gb) <= idlen) {
00138 av_log(avctx, AV_LOG_ERROR,
00139 "Not enough data to read header\n");
00140 return AVERROR_INVALIDDATA;
00141 }
00142
00143 flags = bytestream2_get_byte(&s->gb);
00144
00145 if (!pal && (first_clr || colors || csize)) {
00146 av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
00147
00148 first_clr = colors = csize = 0;
00149 }
00150
00151
00152 bytestream2_skip(&s->gb, idlen);
00153
00154 switch (bpp) {
00155 case 8:
00156 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
00157 break;
00158 case 15:
00159 case 16:
00160 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
00161 break;
00162 case 24:
00163 avctx->pix_fmt = AV_PIX_FMT_BGR24;
00164 break;
00165 case 32:
00166 avctx->pix_fmt = AV_PIX_FMT_BGRA;
00167 break;
00168 default:
00169 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
00170 return AVERROR_INVALIDDATA;
00171 }
00172
00173 if (s->picture.data[0])
00174 avctx->release_buffer(avctx, &s->picture);
00175
00176 if (colors && (colors + first_clr) > 256) {
00177 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
00178 return AVERROR_INVALIDDATA;
00179 }
00180
00181 if ((ret = av_image_check_size(w, h, 0, avctx)))
00182 return ret;
00183 if (w != avctx->width || h != avctx->height)
00184 avcodec_set_dimensions(avctx, w, h);
00185 if ((ret = ff_get_buffer(avctx, p)) < 0) {
00186 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00187 return ret;
00188 }
00189
00190 if (flags & TGA_TOPTOBOTTOM) {
00191 dst = p->data[0];
00192 stride = p->linesize[0];
00193 } else {
00194 dst = p->data[0] + p->linesize[0] * (h - 1);
00195 stride = -p->linesize[0];
00196 }
00197
00198 interleave = flags & TGA_INTERLEAVE2 ? 2 :
00199 flags & TGA_INTERLEAVE4 ? 4 : 1;
00200
00201 if (colors) {
00202 int pal_size, pal_sample_size;
00203 switch (csize) {
00204 case 32: pal_sample_size = 4; break;
00205 case 24: pal_sample_size = 3; break;
00206 case 16:
00207 case 15: pal_sample_size = 2; break;
00208 default:
00209 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
00210 return AVERROR_INVALIDDATA;
00211 }
00212 pal_size = colors * pal_sample_size;
00213 if (avctx->pix_fmt != AV_PIX_FMT_PAL8)
00214 bytestream2_skip(&s->gb, pal_size);
00215 else {
00216 int t;
00217 uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
00218
00219 if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
00220 av_log(avctx, AV_LOG_ERROR,
00221 "Not enough data to read palette\n");
00222 return AVERROR_INVALIDDATA;
00223 }
00224 switch (pal_sample_size) {
00225 case 4:
00226 for (t = 0; t < colors; t++)
00227 *pal++ = bytestream2_get_le32u(&s->gb);
00228 break;
00229 case 3:
00230
00231 for (t = 0; t < colors; t++)
00232 *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
00233 break;
00234 case 2:
00235
00236 for (t = 0; t < colors; t++) {
00237 uint32_t v = bytestream2_get_le16u(&s->gb);
00238 v = ((v & 0x7C00) << 9) |
00239 ((v & 0x03E0) << 6) |
00240 ((v & 0x001F) << 3);
00241
00242 v |= (v & 0xE0E0E0U) >> 5;
00243 *pal++ = (0xffU<<24) | v;
00244 }
00245 break;
00246 }
00247 p->palette_has_changed = 1;
00248 }
00249 }
00250
00251 if ((compr & (~TGA_RLE)) == TGA_NODATA) {
00252 memset(p->data[0], 0, p->linesize[0] * h);
00253 } else {
00254 if (compr & TGA_RLE) {
00255 int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave);
00256 if (res < 0)
00257 return res;
00258 } else {
00259 size_t img_size = w * ((bpp + 1) >> 3);
00260 uint8_t *line;
00261 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
00262 av_log(avctx, AV_LOG_ERROR,
00263 "Not enough data available for image\n");
00264 return AVERROR_INVALIDDATA;
00265 }
00266
00267 line = dst;
00268 y = 0;
00269 do {
00270 bytestream2_get_bufferu(&s->gb, line, img_size);
00271 line = advance_line(dst, line, stride, &y, h, interleave);
00272 } while (line);
00273 }
00274 }
00275
00276 if (flags & TGA_RIGHTTOLEFT) {
00277 int x;
00278 for (y = 0; y < h; y++) {
00279 void *line = &p->data[0][y * p->linesize[0]];
00280 for (x = 0; x < w >> 1; x++) {
00281 switch (bpp) {
00282 case 32:
00283 FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
00284 break;
00285 case 24:
00286 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
00287 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
00288 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
00289 break;
00290 case 16:
00291 FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
00292 break;
00293 case 8:
00294 FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
00295 }
00296 }
00297 }
00298 }
00299
00300 *picture = s->picture;
00301 *got_frame = 1;
00302
00303 return avpkt->size;
00304 }
00305
00306 static av_cold int targa_init(AVCodecContext *avctx)
00307 {
00308 TargaContext *s = avctx->priv_data;
00309
00310 avcodec_get_frame_defaults(&s->picture);
00311 avctx->coded_frame = &s->picture;
00312
00313 return 0;
00314 }
00315
00316 static av_cold int targa_end(AVCodecContext *avctx)
00317 {
00318 TargaContext *s = avctx->priv_data;
00319
00320 if (s->picture.data[0])
00321 avctx->release_buffer(avctx, &s->picture);
00322
00323 return 0;
00324 }
00325
00326 AVCodec ff_targa_decoder = {
00327 .name = "targa",
00328 .type = AVMEDIA_TYPE_VIDEO,
00329 .id = AV_CODEC_ID_TARGA,
00330 .priv_data_size = sizeof(TargaContext),
00331 .init = targa_init,
00332 .close = targa_end,
00333 .decode = decode_frame,
00334 .capabilities = CODEC_CAP_DR1,
00335 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
00336 };