]> git.sesse.net Git - ffmpeg/blob - libavcodec/targa.c
Merge remote-tracking branch 'qatar/master'
[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 "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "targa.h"
27
28 typedef struct TargaContext {
29     AVFrame picture;
30
31     int width, height;
32     int bpp;
33     int color_type;
34     int compression_type;
35 } TargaContext;
36
37 #define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
38     if(needed > buf_end - buf){ \
39         av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
40         return -1; \
41     } \
42
43 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)
44 {
45     int i, x, y;
46     int depth = (bpp + 1) >> 3;
47     int type, count;
48     int diff;
49     const uint8_t *src_end = src + src_size;
50
51     diff = stride - w * depth;
52     x = y = 0;
53     while(y < h){
54         CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
55         type = *src++;
56         count = (type & 0x7F) + 1;
57         type &= 0x80;
58         if(x + count > (h - y) * w){
59             av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
60             return -1;
61         }
62         if(type){
63             CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
64         }else{
65             CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
66         }
67         for(i = 0; i < count; i++){
68             switch(depth){
69             case 1:
70                 *dst = *src;
71                 break;
72             case 2:
73                 AV_WN16A(dst, AV_RN16A(src));
74                 break;
75             case 3:
76                 dst[0] = src[0];
77                 dst[1] = src[1];
78                 dst[2] = src[2];
79                 break;
80             case 4:
81                 AV_WN32A(dst, AV_RN32A(src));
82                 break;
83             }
84             dst += depth;
85             if(!type)
86                 src += depth;
87
88             x++;
89             if(x == w){
90                 x = 0;
91                 y++;
92                 dst += diff;
93             }
94         }
95         if(type)
96             src += depth;
97     }
98     return src_size;
99 }
100
101 static int decode_frame(AVCodecContext *avctx,
102                         void *data, int *data_size,
103                         AVPacket *avpkt)
104 {
105     const uint8_t *buf = avpkt->data;
106     const uint8_t *buf_end = avpkt->data + avpkt->size;
107     TargaContext * const s = avctx->priv_data;
108     AVFrame *picture = data;
109     AVFrame * const p = &s->picture;
110     uint8_t *dst;
111     int stride;
112     int idlen, pal, compr, y, w, h, bpp, flags;
113     int first_clr, colors, csize;
114
115     /* parse image header */
116     CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
117     idlen = *buf++;
118     pal = *buf++;
119     compr = *buf++;
120     first_clr = bytestream_get_le16(&buf);
121     colors = bytestream_get_le16(&buf);
122     csize = *buf++;
123     if (!pal && (first_clr || colors || csize)) {
124         av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
125         // specification says we should ignore those value in this case
126         first_clr = colors = csize = 0;
127     }
128     buf += 2; /* x */
129     y = bytestream_get_le16(&buf);
130     w = bytestream_get_le16(&buf);
131     h = bytestream_get_le16(&buf);
132     bpp = *buf++;
133     flags = *buf++;
134     //skip identifier if any
135     CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
136     buf += idlen;
137     s->bpp = bpp;
138     s->width = w;
139     s->height = h;
140     switch(s->bpp){
141     case 8:
142         avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
143         break;
144     case 15:
145     case 16:
146         avctx->pix_fmt = PIX_FMT_RGB555LE;
147         break;
148     case 24:
149         avctx->pix_fmt = PIX_FMT_BGR24;
150         break;
151     case 32:
152         avctx->pix_fmt = PIX_FMT_BGRA;
153         break;
154     default:
155         av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
156         return -1;
157     }
158
159     if(s->picture.data[0])
160         avctx->release_buffer(avctx, &s->picture);
161
162     if(av_image_check_size(w, h, 0, avctx))
163         return -1;
164     if(w != avctx->width || h != avctx->height)
165         avcodec_set_dimensions(avctx, w, h);
166     if(avctx->get_buffer(avctx, p) < 0){
167         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
168         return -1;
169     }
170     if(flags & 0x20){
171         dst = p->data[0];
172         stride = p->linesize[0];
173     }else{ //image is upside-down
174         dst = p->data[0] + p->linesize[0] * (h - 1);
175         stride = -p->linesize[0];
176     }
177
178     if(colors){
179         int pal_size, pal_sample_size;
180         if((colors + first_clr) > 256){
181             av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
182             return -1;
183         }
184         switch (csize) {
185         case 24: pal_sample_size = 3; break;
186         case 16:
187         case 15: pal_sample_size = 2; break;
188         default:
189             av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
190             return -1;
191         }
192         pal_size = colors * pal_sample_size;
193         CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
194         if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
195             buf += pal_size;
196         else{
197             int t;
198             uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
199
200             switch (pal_sample_size) {
201             case 3:
202                 /* RGB24 */
203                 for (t = 0; t < colors; t++)
204                     *pal++ = (0xffU<<24) | bytestream_get_le24(&buf);
205                 break;
206             case 2:
207                 /* RGB555 */
208                 for (t = 0; t < colors; t++) {
209                     uint32_t v = bytestream_get_le16(&buf);
210                     v = ((v & 0x7C00) <<  9) |
211                         ((v & 0x03E0) <<  6) |
212                         ((v & 0x001F) <<  3);
213                     /* left bit replication */
214                     v |= (v & 0xE0E0E0U) >> 5;
215                     *pal++ = (0xffU<<24) | v;
216                 }
217                 break;
218             }
219             p->palette_has_changed = 1;
220         }
221     }
222     if((compr & (~TGA_RLE)) == TGA_NODATA)
223         memset(p->data[0], 0, p->linesize[0] * s->height);
224     else{
225         if(compr & TGA_RLE){
226             int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
227             if (res < 0)
228                 return -1;
229             buf += res;
230         }else{
231             size_t img_size = s->width * ((s->bpp + 1) >> 3);
232             CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
233             for(y = 0; y < s->height; y++){
234                     memcpy(dst, buf, img_size);
235
236                 dst += stride;
237                 buf += img_size;
238             }
239         }
240     }
241     if(flags & 0x10){ // right-to-left, needs horizontal flip
242         int x;
243         for(y = 0; y < s->height; y++){
244             void *line = &p->data[0][y * p->linesize[0]];
245             for(x = 0; x < s->width >> 1; x++){
246                 switch(s->bpp){
247                 case 32:
248                     FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[s->width - x - 1]);
249                     break;
250                 case 24:
251                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x    ], ((uint8_t *)line)[3 * s->width - 3 * x - 3]);
252                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * s->width - 3 * x - 2]);
253                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * s->width - 3 * x - 1]);
254                     break;
255                 case 16:
256                     FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[s->width - x - 1]);
257                     break;
258                 case 8:
259                     FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[s->width - x - 1]);
260                 }
261             }
262         }
263     }
264
265     *picture   = s->picture;
266     *data_size = sizeof(AVPicture);
267
268     return avpkt->size;
269 }
270
271 static av_cold int targa_init(AVCodecContext *avctx){
272     TargaContext *s = avctx->priv_data;
273
274     avcodec_get_frame_defaults(&s->picture);
275     avctx->coded_frame = &s->picture;
276
277     return 0;
278 }
279
280 static av_cold int targa_end(AVCodecContext *avctx){
281     TargaContext *s = avctx->priv_data;
282
283     if(s->picture.data[0])
284         avctx->release_buffer(avctx, &s->picture);
285
286     return 0;
287 }
288
289 AVCodec ff_targa_decoder = {
290     .name           = "targa",
291     .type           = AVMEDIA_TYPE_VIDEO,
292     .id             = CODEC_ID_TARGA,
293     .priv_data_size = sizeof(TargaContext),
294     .init           = targa_init,
295     .close          = targa_end,
296     .decode         = decode_frame,
297     .capabilities   = CODEC_CAP_DR1,
298     .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
299 };