Statistics
| Branch: | Revision:

ffmpeg / libavcodec / targa.c @ 7782cb20

History | View | Annotate | Download (8.05 KB)

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 "targa.h"
26

    
27
typedef struct TargaContext {
28
    AVFrame picture;
29

    
30
    int width, height;
31
    int bpp;
32
    int color_type;
33
    int compression_type;
34
} TargaContext;
35

    
36
#define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
37
    if(buf + needed > buf_end){ \
38
        av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
39
        return -1; \
40
    } \
41

    
42
static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)
43
{
44
    int i, x, y;
45
    int depth = (bpp + 1) >> 3;
46
    int type, count;
47
    int diff;
48
    const uint8_t *src_end = src + src_size;
49

    
50
    diff = stride - w * depth;
51
    x = y = 0;
52
    while(y < h){
53
        CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
54
        type = *src++;
55
        count = (type & 0x7F) + 1;
56
        type &= 0x80;
57
        if((x + count > w) && (x + count + 1 > (h - y) * w)){
58
            av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
59
            return -1;
60
        }
61
        if(type){
62
            CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
63
        }else{
64
            CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
65
        }
66
        for(i = 0; i < count; i++){
67
            switch(depth){
68
            case 1:
69
                *dst = *src;
70
                break;
71
            case 2:
72
                *((uint16_t*)dst) = AV_RL16(src);
73
                break;
74
            case 3:
75
                dst[0] = src[0];
76
                dst[1] = src[1];
77
                dst[2] = src[2];
78
                break;
79
            case 4:
80
                *((uint32_t*)dst) = AV_RL32(src);
81
                break;
82
            }
83
            dst += depth;
84
            if(!type)
85
                src += depth;
86

    
87
            x++;
88
            if(x == w){
89
                x = 0;
90
                y++;
91
                dst += diff;
92
            }
93
        }
94
        if(type)
95
            src += depth;
96
    }
97
    return src_size;
98
}
99

    
100
static int decode_frame(AVCodecContext *avctx,
101
                        void *data, int *data_size,
102
                        AVPacket *avpkt)
103
{
104
    const uint8_t *buf = avpkt->data;
105
    const uint8_t *buf_end = avpkt->data + avpkt->size;
106
    TargaContext * const s = avctx->priv_data;
107
    AVFrame *picture = data;
108
    AVFrame * const p= (AVFrame*)&s->picture;
109
    uint8_t *dst;
110
    int stride;
111
    int idlen, pal, compr, x, y, w, h, bpp, flags;
112
    int first_clr, colors, csize;
113

    
114
    /* parse image header */
115
    CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
116
    idlen = *buf++;
117
    pal = *buf++;
118
    compr = *buf++;
119
    first_clr = AV_RL16(buf); buf += 2;
120
    colors = AV_RL16(buf); buf += 2;
121
    csize = *buf++;
122
    x = AV_RL16(buf); buf += 2;
123
    y = AV_RL16(buf); buf += 2;
124
    w = AV_RL16(buf); buf += 2;
125
    h = AV_RL16(buf); buf += 2;
126
    bpp = *buf++;
127
    flags = *buf++;
128
    //skip identifier if any
129
    CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
130
    buf += idlen;
131
    s->bpp = bpp;
132
    s->width = w;
133
    s->height = h;
134
    switch(s->bpp){
135
    case 8:
136
        avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
137
        break;
138
    case 15:
139
        avctx->pix_fmt = PIX_FMT_RGB555;
140
        break;
141
    case 16:
142
        avctx->pix_fmt = PIX_FMT_RGB555;
143
        break;
144
    case 24:
145
        avctx->pix_fmt = PIX_FMT_BGR24;
146
        break;
147
    case 32:
148
        avctx->pix_fmt = PIX_FMT_RGB32;
149
        break;
150
    default:
151
        av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
152
        return -1;
153
    }
154

    
155
    if(s->picture.data[0])
156
        avctx->release_buffer(avctx, &s->picture);
157

    
158
    if(av_image_check_size(w, h, 0, avctx))
159
        return -1;
160
    if(w != avctx->width || h != avctx->height)
161
        avcodec_set_dimensions(avctx, w, h);
162
    if(avctx->get_buffer(avctx, p) < 0){
163
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
164
        return -1;
165
    }
166
    if(flags & 0x20){
167
        dst = p->data[0];
168
        stride = p->linesize[0];
169
    }else{ //image is upside-down
170
        dst = p->data[0] + p->linesize[0] * (h - 1);
171
        stride = -p->linesize[0];
172
    }
173

    
174
    if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
175
        memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
176
        if(avctx->palctrl->palette_changed){
177
            p->palette_has_changed = 1;
178
            avctx->palctrl->palette_changed = 0;
179
        }
180
    }
181
    if(colors){
182
        size_t pal_size;
183
        if((colors + first_clr) > 256){
184
            av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
185
            return -1;
186
        }
187
        if(csize != 24){
188
            av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
189
            return -1;
190
        }
191
        pal_size = colors * ((csize + 1) >> 3);
192
        CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
193
        if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
194
            buf += pal_size;
195
        else{
196
            int r, g, b, t;
197
            int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
198
            for(t = 0; t < colors; t++){
199
                r = *buf++;
200
                g = *buf++;
201
                b = *buf++;
202
                *pal++ = (b << 16) | (g << 8) | r;
203
            }
204
            p->palette_has_changed = 1;
205
        }
206
    }
207
    if((compr & (~TGA_RLE)) == TGA_NODATA)
208
        memset(p->data[0], 0, p->linesize[0] * s->height);
209
    else{
210
        if(compr & TGA_RLE){
211
            int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
212
            if (res < 0)
213
                return -1;
214
            buf += res;
215
        }else{
216
            size_t img_size = s->width * ((s->bpp + 1) >> 3);
217
            CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
218
            for(y = 0; y < s->height; y++){
219
#if HAVE_BIGENDIAN
220
                if((s->bpp + 1) >> 3 == 2){
221
                    uint16_t *dst16 = (uint16_t*)dst;
222
                    for(x = 0; x < s->width; x++)
223
                        dst16[x] = AV_RL16(buf + x * 2);
224
                }else if((s->bpp + 1) >> 3 == 4){
225
                    uint32_t *dst32 = (uint32_t*)dst;
226
                    for(x = 0; x < s->width; x++)
227
                        dst32[x] = AV_RL32(buf + x * 4);
228
                }else
229
#endif
230
                    memcpy(dst, buf, img_size);
231

    
232
                dst += stride;
233
                buf += img_size;
234
            }
235
        }
236
    }
237

    
238
    *picture= *(AVFrame*)&s->picture;
239
    *data_size = sizeof(AVPicture);
240

    
241
    return avpkt->size;
242
}
243

    
244
static av_cold int targa_init(AVCodecContext *avctx){
245
    TargaContext *s = avctx->priv_data;
246

    
247
    avcodec_get_frame_defaults((AVFrame*)&s->picture);
248
    avctx->coded_frame= (AVFrame*)&s->picture;
249

    
250
    return 0;
251
}
252

    
253
static av_cold int targa_end(AVCodecContext *avctx){
254
    TargaContext *s = avctx->priv_data;
255

    
256
    if(s->picture.data[0])
257
        avctx->release_buffer(avctx, &s->picture);
258

    
259
    return 0;
260
}
261

    
262
AVCodec ff_targa_decoder = {
263
    "targa",
264
    AVMEDIA_TYPE_VIDEO,
265
    CODEC_ID_TARGA,
266
    sizeof(TargaContext),
267
    targa_init,
268
    NULL,
269
    targa_end,
270
    decode_frame,
271
    CODEC_CAP_DR1,
272
    NULL,
273
    .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
274
};