targa.c 7.66 KB
Newer Older
Kostya Shishkov's avatar
Kostya Shishkov committed
1 2 3 4
/*
 * Targa (.tga) image decoder
 * Copyright (c) 2006 Konstantin Shishkov
 *
5
 * This file is part of Libav.
Kostya Shishkov's avatar
Kostya Shishkov committed
6
 *
7
 * Libav is free software; you can redistribute it and/or
Kostya Shishkov's avatar
Kostya Shishkov committed
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * 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,
Kostya Shishkov's avatar
Kostya Shishkov committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
Kostya Shishkov's avatar
Kostya Shishkov committed
19 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
21 22

#include "libavutil/intreadwrite.h"
23
#include "libavutil/imgutils.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
24
#include "avcodec.h"
25
#include "bytestream.h"
26
#include "internal.h"
27
#include "targa.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
28 29

typedef struct TargaContext {
30
    GetByteContext gb;
Kostya Shishkov's avatar
Kostya Shishkov committed
31 32 33 34 35

    int color_type;
    int compression_type;
} TargaContext;

36 37
static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
                            uint8_t *dst, int w, int h, int stride, int bpp)
Kostya Shishkov's avatar
Kostya Shishkov committed
38
{
39
    int x, y;
Kostya Shishkov's avatar
Kostya Shishkov committed
40 41 42 43 44 45
    int depth = (bpp + 1) >> 3;
    int type, count;
    int diff;

    diff = stride - w * depth;
    x = y = 0;
46 47 48 49 50 51 52
    while (y < h) {
        if (bytestream2_get_bytes_left(&s->gb) <= 0) {
            av_log(avctx, AV_LOG_ERROR,
                   "Ran ouf of data before end-of-image\n");
            return AVERROR_INVALIDDATA;
        }
        type  = bytestream2_get_byteu(&s->gb);
Kostya Shishkov's avatar
Kostya Shishkov committed
53 54
        count = (type & 0x7F) + 1;
        type &= 0x80;
55 56 57 58 59
        if (x + count > w && x + count + 1 > (h - y) * w) {
            av_log(avctx, AV_LOG_ERROR,
                   "Packet went out of bounds: position (%i,%i) size %i\n",
                   x, y, count);
            return AVERROR_INVALIDDATA;
60
        }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        if (!type) {
            do {
                int n  = FFMIN(count, w - x);
                bytestream2_get_buffer(&s->gb, dst, n * depth);
                count -= n;
                dst   += n * depth;
                x     += n;
                if (x == w) {
                    x    = 0;
                    y++;
                    dst += diff;
                }
            } while (count > 0);
        } else {
            uint8_t tmp[4];
            bytestream2_get_buffer(&s->gb, tmp, depth);
            do {
                int n  = FFMIN(count, w - x);
                count -= n;
                x     += n;
                do {
                    memcpy(dst, tmp, depth);
                    dst += depth;
                } while (--n);
                if (x == w) {
                    x    = 0;
                    y++;
                    dst += diff;
                }
            } while (count > 0);
Kostya Shishkov's avatar
Kostya Shishkov committed
91 92
        }
    }
93
    return 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
94 95 96
}

static int decode_frame(AVCodecContext *avctx,
97
                        void *data, int *got_frame,
98
                        AVPacket *avpkt)
Kostya Shishkov's avatar
Kostya Shishkov committed
99 100
{
    TargaContext * const s = avctx->priv_data;
101
    AVFrame * const p = data;
Kostya Shishkov's avatar
Kostya Shishkov committed
102 103
    uint8_t *dst;
    int stride;
104
    int idlen, compr, y, w, h, bpp, flags, ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
105 106
    int first_clr, colors, csize;

107 108
    bytestream2_init(&s->gb, avpkt->data, avpkt->size);

Kostya Shishkov's avatar
Kostya Shishkov committed
109
    /* parse image header */
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    idlen     = bytestream2_get_byte(&s->gb);
    bytestream2_skip(&s->gb, 1); /* pal */
    compr     = bytestream2_get_byte(&s->gb);
    first_clr = bytestream2_get_le16(&s->gb);
    colors    = bytestream2_get_le16(&s->gb);
    csize     = bytestream2_get_byte(&s->gb);
    bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
    w         = bytestream2_get_le16(&s->gb);
    h         = bytestream2_get_le16(&s->gb);
    bpp       = bytestream2_get_byte(&s->gb);
    flags     = bytestream2_get_byte(&s->gb);
    // skip identifier if any
    bytestream2_skip(&s->gb, idlen);

    switch(bpp){
Kostya Shishkov's avatar
Kostya Shishkov committed
125
    case 8:
126
        avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
Kostya Shishkov's avatar
Kostya Shishkov committed
127 128
        break;
    case 15:
129
        avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
Kostya Shishkov's avatar
Kostya Shishkov committed
130 131
        break;
    case 16:
132
        avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
Kostya Shishkov's avatar
Kostya Shishkov committed
133 134
        break;
    case 24:
135
        avctx->pix_fmt = AV_PIX_FMT_BGR24;
Kostya Shishkov's avatar
Kostya Shishkov committed
136
        break;
Kostya Shishkov's avatar
Kostya Shishkov committed
137
    case 32:
138
        avctx->pix_fmt = AV_PIX_FMT_BGRA;
Kostya Shishkov's avatar
Kostya Shishkov committed
139
        break;
Kostya Shishkov's avatar
Kostya Shishkov committed
140
    default:
141
        av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
142
        return AVERROR_INVALIDDATA;
Kostya Shishkov's avatar
Kostya Shishkov committed
143 144
    }

145
    if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
146
        return ret;
147

148
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0){
Kostya Shishkov's avatar
Kostya Shishkov committed
149
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
150
        return ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
151 152 153 154 155 156 157 158 159 160
    }
    if(flags & 0x20){
        dst = p->data[0];
        stride = p->linesize[0];
    }else{ //image is upside-down
        dst = p->data[0] + p->linesize[0] * (h - 1);
        stride = -p->linesize[0];
    }

    if(colors){
161
        int pal_size, pal_sample_size;
Kostya Shishkov's avatar
Kostya Shishkov committed
162 163
        if((colors + first_clr) > 256){
            av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
164
            return AVERROR_INVALIDDATA;
Kostya Shishkov's avatar
Kostya Shishkov committed
165
        }
166 167 168 169 170
        switch (csize) {
        case 24: pal_sample_size = 3; break;
        case 16:
        case 15: pal_sample_size = 2; break;
        default:
Kostya Shishkov's avatar
Kostya Shishkov committed
171
            av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
172
            return AVERROR_INVALIDDATA;
Kostya Shishkov's avatar
Kostya Shishkov committed
173
        }
174
        pal_size = colors * pal_sample_size;
175
        if(avctx->pix_fmt != AV_PIX_FMT_PAL8)//should not occur but skip palette anyway
176
            bytestream2_skip(&s->gb, pal_size);
Kostya Shishkov's avatar
Kostya Shishkov committed
177
        else{
178 179 180
            int t;
            uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;

181 182 183 184 185
            if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
                av_log(avctx, AV_LOG_ERROR,
                       "Not enough data to read palette\n");
                return AVERROR_INVALIDDATA;
            }
186 187 188 189
            switch (pal_sample_size) {
            case 3:
                /* RGB24 */
                for (t = 0; t < colors; t++)
190
                    *pal++ = bytestream2_get_le24u(&s->gb);
191 192 193 194
                break;
            case 2:
                /* RGB555 */
                for (t = 0; t < colors; t++) {
195
                    uint32_t v = bytestream2_get_le16u(&s->gb);
196 197 198 199 200 201 202 203
                    v = ((v & 0x7C00) <<  9) |
                        ((v & 0x03E0) <<  6) |
                        ((v & 0x001F) <<  3);
                    /* left bit replication */
                    v |= (v & 0xE0E0E0U) >> 5;
                    *pal++ = v;
                }
                break;
Kostya Shishkov's avatar
Kostya Shishkov committed
204 205 206 207
            }
            p->palette_has_changed = 1;
        }
    }
208 209 210
    if ((compr & (~TGA_RLE)) == TGA_NODATA) {
        memset(p->data[0], 0, p->linesize[0] * h);
    } else {
211
        if(compr & TGA_RLE){
212
            int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
213
            if (res < 0)
214 215 216 217 218 219 220 221 222 223
                return res;
        } else {
            size_t img_size = w * ((bpp + 1) >> 3);
            if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
                av_log(avctx, AV_LOG_ERROR,
                       "Not enough data available for image\n");
                return AVERROR_INVALIDDATA;
            }
            for (y = 0; y < h; y++) {
                bytestream2_get_bufferu(&s->gb, dst, img_size);
Kostya Shishkov's avatar
Kostya Shishkov committed
224 225 226 227 228
                dst += stride;
            }
        }
    }

229
    *got_frame = 1;
Kostya Shishkov's avatar
Kostya Shishkov committed
230

231
    return avpkt->size;
Kostya Shishkov's avatar
Kostya Shishkov committed
232 233
}

234
AVCodec ff_targa_decoder = {
235
    .name           = "targa",
236
    .long_name      = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
237
    .type           = AVMEDIA_TYPE_VIDEO,
238
    .id             = AV_CODEC_ID_TARGA,
239 240
    .priv_data_size = sizeof(TargaContext),
    .decode         = decode_frame,
241
    .capabilities   = AV_CODEC_CAP_DR1,
Kostya Shishkov's avatar
Kostya Shishkov committed
242
};