]> git.sesse.net Git - ffmpeg/blob - libavcodec/targa.c
fate: split off QuickTime codec FATE tests into their own file
[ffmpeg] / libavcodec / targa.c
1 /*
2  * Targa (.tga) image decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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, compr, 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     buf++; /* pal */
118     compr = *buf++;
119     first_clr = AV_RL16(buf); buf += 2;
120     colors = AV_RL16(buf); buf += 2;
121     csize = *buf++;
122     buf += 2; /* x */
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                 r = *buf++;
193                 g = *buf++;
194                 b = *buf++;
195                 *pal++ = (b << 16) | (g << 8) | r;
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                 int x;
214                 if((s->bpp + 1) >> 3 == 2){
215                     uint16_t *dst16 = (uint16_t*)dst;
216                     for(x = 0; x < s->width; x++)
217                         dst16[x] = AV_RL16(buf + x * 2);
218                 }else if((s->bpp + 1) >> 3 == 4){
219                     uint32_t *dst32 = (uint32_t*)dst;
220                     for(x = 0; x < s->width; x++)
221                         dst32[x] = AV_RL32(buf + x * 4);
222                 }else
223 #endif
224                     memcpy(dst, buf, img_size);
225
226                 dst += stride;
227                 buf += img_size;
228             }
229         }
230     }
231
232     *picture= *(AVFrame*)&s->picture;
233     *data_size = sizeof(AVPicture);
234
235     return avpkt->size;
236 }
237
238 static av_cold int targa_init(AVCodecContext *avctx){
239     TargaContext *s = avctx->priv_data;
240
241     avcodec_get_frame_defaults((AVFrame*)&s->picture);
242     avctx->coded_frame= (AVFrame*)&s->picture;
243
244     return 0;
245 }
246
247 static av_cold int targa_end(AVCodecContext *avctx){
248     TargaContext *s = avctx->priv_data;
249
250     if(s->picture.data[0])
251         avctx->release_buffer(avctx, &s->picture);
252
253     return 0;
254 }
255
256 AVCodec ff_targa_decoder = {
257     .name           = "targa",
258     .type           = AVMEDIA_TYPE_VIDEO,
259     .id             = CODEC_ID_TARGA,
260     .priv_data_size = sizeof(TargaContext),
261     .init           = targa_init,
262     .close          = targa_end,
263     .decode         = decode_frame,
264     .capabilities   = CODEC_CAP_DR1,
265     .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
266 };