]> git.sesse.net Git - ffmpeg/blob - libavcodec/targa.c
indention
[ffmpeg] / libavcodec / targa.c
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 "avcodec.h"
23
24 enum TargaCompr{
25     TGA_NODATA = 0, // no image data
26     TGA_PAL    = 1, // palettized
27     TGA_RGB    = 2, // true-color
28     TGA_BW     = 3, // black & white or grayscale
29     TGA_RLE    = 8, // flag pointing that data is RLE-coded
30 };
31
32 typedef struct TargaContext {
33     AVFrame picture;
34
35     int width, height;
36     int bpp;
37     int color_type;
38     int compression_type;
39 } TargaContext;
40
41 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
42 {
43     int i, x, y;
44     int depth = (bpp + 1) >> 3;
45     int type, count;
46     int diff;
47
48     diff = stride - w * depth;
49     x = y = 0;
50     while(y < h){
51         type = *src++;
52         count = (type & 0x7F) + 1;
53         type &= 0x80;
54         if((x + count > w) && (x + count + 1 > (h - y) * w)){
55             av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
56             return;
57         }
58         for(i = 0; i < count; i++){
59             switch(depth){
60             case 1:
61                 *dst = *src;
62                 break;
63             case 2:
64                 *((uint16_t*)dst) = LE_16(src);
65                 break;
66             case 3:
67                 dst[0] = src[0];
68                 dst[1] = src[1];
69                 dst[2] = src[2];
70                 break;
71             }
72             dst += depth;
73             if(!type)
74                 src += depth;
75
76             x++;
77             if(x == w){
78                 x = 0;
79                 y++;
80                 dst += diff;
81             }
82         }
83         if(type)
84             src += depth;
85     }
86 }
87
88 static int decode_frame(AVCodecContext *avctx,
89                         void *data, int *data_size,
90                         uint8_t *buf, int buf_size)
91 {
92     TargaContext * const s = avctx->priv_data;
93     AVFrame *picture = data;
94     AVFrame * const p= (AVFrame*)&s->picture;
95     uint8_t *dst;
96     int stride;
97     int idlen, pal, compr, x, y, w, h, bpp, flags;
98     int first_clr, colors, csize;
99
100     /* parse image header */
101     idlen = *buf++;
102     pal = *buf++;
103     compr = *buf++;
104     first_clr = LE_16(buf); buf += 2;
105     colors = LE_16(buf); buf += 2;
106     csize = *buf++;
107     x = LE_16(buf); buf += 2;
108     y = LE_16(buf); buf += 2;
109     w = LE_16(buf); buf += 2;
110     h = LE_16(buf); buf += 2;
111     bpp = *buf++;
112     flags = *buf++;
113     //skip identifier if any
114     buf += idlen;
115     s->bpp = bpp;
116     s->width = w;
117     s->height = h;
118     switch(s->bpp){
119     case 8:
120         avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
121         break;
122     case 15:
123         avctx->pix_fmt = PIX_FMT_RGB555;
124         break;
125     case 16:
126         avctx->pix_fmt = PIX_FMT_RGB555;
127         break;
128     case 24:
129         avctx->pix_fmt = PIX_FMT_BGR24;
130         break;
131     default:
132         av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", avctx->bits_per_sample);
133         return -1;
134     }
135
136     if(s->picture.data[0])
137         avctx->release_buffer(avctx, &s->picture);
138
139     if(avcodec_check_dimensions(avctx, w, h))
140         return -1;
141     if(w != avctx->width || h != avctx->height)
142         avcodec_set_dimensions(avctx, w, h);
143     if(avctx->get_buffer(avctx, p) < 0){
144         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
145         return -1;
146     }
147     if(flags & 0x20){
148         dst = p->data[0];
149         stride = p->linesize[0];
150     }else{ //image is upside-down
151         dst = p->data[0] + p->linesize[0] * (h - 1);
152         stride = -p->linesize[0];
153     }
154
155     if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
156         memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
157         if(avctx->palctrl->palette_changed){
158             p->palette_has_changed = 1;
159             avctx->palctrl->palette_changed = 0;
160         }
161     }
162     if(colors){
163         if((colors + first_clr) > 256){
164             av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
165             return -1;
166         }
167         if(csize != 24){
168             av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
169             return -1;
170         }
171         if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
172             buf += colors * ((csize + 1) >> 3);
173         else{
174             int r, g, b, t;
175             int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
176             for(t = 0; t < colors; t++){
177                 r = *buf++;
178                 g = *buf++;
179                 b = *buf++;
180                 *pal++ = (b << 16) | (g << 8) | r;
181             }
182             p->palette_has_changed = 1;
183             avctx->palctrl->palette_changed = 0;
184         }
185     }
186     if((compr & (~TGA_RLE)) == TGA_NODATA)
187         memset(p->data[0], 0, p->linesize[0] * s->height);
188     else{
189         if(compr & TGA_RLE)
190             targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
191         else{
192             for(y = 0; y < s->height; y++){
193 #ifdef WORDS_BIGENDIAN
194                 if((s->bpp + 1) >> 3 == 2){
195                     uint16_t *dst16 = (uint16_t*)dst;
196                     for(x = 0; x < s->width; x++)
197                         dst16[x] = LE_16(buf + x * 2);
198                 }else
199 #endif
200                     memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
201
202                 dst += stride;
203                 buf += s->width * ((s->bpp + 1) >> 3);
204             }
205         }
206     }
207
208     *picture= *(AVFrame*)&s->picture;
209     *data_size = sizeof(AVPicture);
210
211     return buf_size;
212 }
213
214 static int targa_init(AVCodecContext *avctx){
215     TargaContext *s = avctx->priv_data;
216
217     avcodec_get_frame_defaults((AVFrame*)&s->picture);
218     avctx->coded_frame= (AVFrame*)&s->picture;
219     s->picture.data[0] = NULL;
220
221     return 0;
222 }
223
224 static int targa_end(AVCodecContext *avctx){
225     TargaContext *s = avctx->priv_data;
226
227     if(s->picture.data[0])
228         avctx->release_buffer(avctx, &s->picture);
229
230     return 0;
231 }
232
233 AVCodec targa_decoder = {
234     "targa",
235     CODEC_TYPE_VIDEO,
236     CODEC_ID_TARGA,
237     sizeof(TargaContext),
238     targa_init,
239     NULL,
240     targa_end,
241     decode_frame,
242     0,
243     NULL
244 };