Statistics
| Branch: | Revision:

ffmpeg / libavcodec / targa.c @ dbfdb288

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

    
225
                dst += stride;
226
                buf += img_size;
227
            }
228
        }
229
    }
230

    
231
    *picture= *(AVFrame*)&s->picture;
232
    *data_size = sizeof(AVPicture);
233

    
234
    return avpkt->size;
235
}
236

    
237
static av_cold int targa_init(AVCodecContext *avctx){
238
    TargaContext *s = avctx->priv_data;
239

    
240
    avcodec_get_frame_defaults((AVFrame*)&s->picture);
241
    avctx->coded_frame= (AVFrame*)&s->picture;
242

    
243
    return 0;
244
}
245

    
246
static av_cold int targa_end(AVCodecContext *avctx){
247
    TargaContext *s = avctx->priv_data;
248

    
249
    if(s->picture.data[0])
250
        avctx->release_buffer(avctx, &s->picture);
251

    
252
    return 0;
253
}
254

    
255
AVCodec ff_targa_decoder = {
256
    "targa",
257
    AVMEDIA_TYPE_VIDEO,
258
    CODEC_ID_TARGA,
259
    sizeof(TargaContext),
260
    targa_init,
261
    NULL,
262
    targa_end,
263
    decode_frame,
264
    CODEC_CAP_DR1,
265
    NULL,
266
    .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
267
};