]> git.sesse.net Git - ffmpeg/blob - libavcodec/targa.c
Merge commit '594d4d5df3c70404168701dd5c90b7e6e5587793'
[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 "internal.h"
27 #include "targa.h"
28
29 typedef struct TargaContext {
30     AVFrame picture;
31     GetByteContext gb;
32 } TargaContext;
33
34 static uint8_t *advance_line(uint8_t *start, uint8_t *line,
35                              int stride, int *y, int h, int interleave)
36 {
37     *y += interleave;
38
39     if (*y < h) {
40         return line + interleave * stride;
41     } else {
42         *y = (*y + 1) & (interleave - 1);
43         if (*y) {
44             return start + *y * stride;
45         } else {
46             return NULL;
47         }
48     }
49 }
50
51 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
52                             uint8_t *start, int w, int h, int stride,
53                             int bpp, int interleave)
54 {
55     int x, y;
56     int depth = (bpp + 1) >> 3;
57     int type, count;
58     uint8_t *line = start;
59     uint8_t *dst  = line;
60
61     x = y = count = 0;
62     while (dst) {
63         if (bytestream2_get_bytes_left(&s->gb) <= 0) {
64             av_log(avctx, AV_LOG_ERROR,
65                    "Ran ouf of data before end-of-image\n");
66             return AVERROR_INVALIDDATA;
67         }
68         type  = bytestream2_get_byteu(&s->gb);
69         count = (type & 0x7F) + 1;
70         type &= 0x80;
71         if (!type) {
72             do {
73                 int n  = FFMIN(count, w - x);
74                 bytestream2_get_buffer(&s->gb, dst, n * depth);
75                 count -= n;
76                 dst   += n * depth;
77                 x     += n;
78                 if (x == w) {
79                     x    = 0;
80                     dst = line = advance_line(start, line, stride, &y, h, interleave);
81                 }
82             } while (dst && count > 0);
83         } else {
84             uint8_t tmp[4];
85             bytestream2_get_buffer(&s->gb, tmp, depth);
86             do {
87                 int n  = FFMIN(count, w - x);
88                 count -= n;
89                 x     += n;
90                 do {
91                     memcpy(dst, tmp, depth);
92                     dst += depth;
93                 } while (--n);
94                 if (x == w) {
95                     x    = 0;
96                     dst = line = advance_line(start, line, stride, &y, h, interleave);
97                 }
98             } while (dst && count > 0);
99         }
100     }
101
102     if (count) {
103         av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n");
104         return AVERROR_INVALIDDATA;
105     }
106
107     return 0;
108 }
109
110 static int decode_frame(AVCodecContext *avctx,
111                         void *data, int *data_size,
112                         AVPacket *avpkt)
113 {
114     TargaContext * const s = avctx->priv_data;
115     AVFrame *picture = data;
116     AVFrame * const p = &s->picture;
117     uint8_t *dst;
118     int stride;
119     int idlen, pal, compr, y, w, h, bpp, flags, ret;
120     int first_clr, colors, csize;
121     int interleave;
122
123     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
124
125     /* parse image header */
126     idlen     = bytestream2_get_byte(&s->gb);
127     pal       = bytestream2_get_byte(&s->gb);
128     compr     = bytestream2_get_byte(&s->gb);
129     first_clr = bytestream2_get_le16(&s->gb);
130     colors    = bytestream2_get_le16(&s->gb);
131     csize     = bytestream2_get_byte(&s->gb);
132     bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
133     w         = bytestream2_get_le16(&s->gb);
134     h         = bytestream2_get_le16(&s->gb);
135     bpp       = bytestream2_get_byte(&s->gb);
136
137     if (bytestream2_get_bytes_left(&s->gb) <= idlen) {
138         av_log(avctx, AV_LOG_ERROR,
139                 "Not enough data to read header\n");
140         return AVERROR_INVALIDDATA;
141     }
142
143     flags     = bytestream2_get_byte(&s->gb);
144
145     if (!pal && (first_clr || colors || csize)) {
146         av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
147         // specification says we should ignore those value in this case
148         first_clr = colors = csize = 0;
149     }
150
151     // skip identifier if any
152     bytestream2_skip(&s->gb, idlen);
153
154     switch (bpp) {
155     case 8:
156         avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
157         break;
158     case 15:
159     case 16:
160         avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
161         break;
162     case 24:
163         avctx->pix_fmt = AV_PIX_FMT_BGR24;
164         break;
165     case 32:
166         avctx->pix_fmt = AV_PIX_FMT_BGRA;
167         break;
168     default:
169         av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
170         return AVERROR_INVALIDDATA;
171     }
172
173     if (s->picture.data[0])
174         avctx->release_buffer(avctx, &s->picture);
175
176     if (colors && (colors + first_clr) > 256) {
177         av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
178         return AVERROR_INVALIDDATA;
179     }
180
181     if ((ret = av_image_check_size(w, h, 0, avctx)))
182         return ret;
183     if (w != avctx->width || h != avctx->height)
184         avcodec_set_dimensions(avctx, w, h);
185     if ((ret = ff_get_buffer(avctx, p)) < 0) {
186         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
187         return ret;
188     }
189
190     if (flags & TGA_TOPTOBOTTOM) {
191         dst = p->data[0];
192         stride = p->linesize[0];
193     } else { //image is upside-down
194         dst = p->data[0] + p->linesize[0] * (h - 1);
195         stride = -p->linesize[0];
196     }
197
198     interleave = flags & TGA_INTERLEAVE2 ? 2 :
199                  flags & TGA_INTERLEAVE4 ? 4 : 1;
200
201     if (colors) {
202         int pal_size, pal_sample_size;
203         switch (csize) {
204         case 32: pal_sample_size = 4; break;
205         case 24: pal_sample_size = 3; break;
206         case 16:
207         case 15: pal_sample_size = 2; break;
208         default:
209             av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
210             return AVERROR_INVALIDDATA;
211         }
212         pal_size = colors * pal_sample_size;
213         if (avctx->pix_fmt != AV_PIX_FMT_PAL8) //should not occur but skip palette anyway
214             bytestream2_skip(&s->gb, pal_size);
215         else {
216             int t;
217             uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
218
219             if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
220                 av_log(avctx, AV_LOG_ERROR,
221                        "Not enough data to read palette\n");
222                 return AVERROR_INVALIDDATA;
223             }
224             switch (pal_sample_size) {
225             case 4:
226                 for (t = 0; t < colors; t++)
227                     *pal++ = bytestream2_get_le32u(&s->gb);
228                 break;
229             case 3:
230                 /* RGB24 */
231                 for (t = 0; t < colors; t++)
232                     *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
233                 break;
234             case 2:
235                 /* RGB555 */
236                 for (t = 0; t < colors; t++) {
237                     uint32_t v = bytestream2_get_le16u(&s->gb);
238                     v = ((v & 0x7C00) <<  9) |
239                         ((v & 0x03E0) <<  6) |
240                         ((v & 0x001F) <<  3);
241                     /* left bit replication */
242                     v |= (v & 0xE0E0E0U) >> 5;
243                     *pal++ = (0xffU<<24) | v;
244                 }
245                 break;
246             }
247             p->palette_has_changed = 1;
248         }
249     }
250
251     if ((compr & (~TGA_RLE)) == TGA_NODATA) {
252         memset(p->data[0], 0, p->linesize[0] * h);
253     } else {
254         if (compr & TGA_RLE) {
255             int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave);
256             if (res < 0)
257                 return res;
258         } else {
259             size_t img_size = w * ((bpp + 1) >> 3);
260             uint8_t *line;
261             if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
262                 av_log(avctx, AV_LOG_ERROR,
263                        "Not enough data available for image\n");
264                 return AVERROR_INVALIDDATA;
265             }
266
267             line = dst;
268             y = 0;
269             do {
270                 bytestream2_get_bufferu(&s->gb, line, img_size);
271                 line = advance_line(dst, line, stride, &y, h, interleave);
272             } while (line);
273         }
274     }
275
276     if (flags & TGA_RIGHTTOLEFT) { // right-to-left, needs horizontal flip
277         int x;
278         for (y = 0; y < h; y++) {
279             void *line = &p->data[0][y * p->linesize[0]];
280             for (x = 0; x < w >> 1; x++) {
281                 switch (bpp) {
282                 case 32:
283                     FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
284                     break;
285                 case 24:
286                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x    ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
287                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
288                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
289                     break;
290                 case 16:
291                     FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
292                     break;
293                 case 8:
294                     FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
295                 }
296             }
297         }
298     }
299
300     *picture   = s->picture;
301     *data_size = sizeof(AVPicture);
302
303     return avpkt->size;
304 }
305
306 static av_cold int targa_init(AVCodecContext *avctx)
307 {
308     TargaContext *s = avctx->priv_data;
309
310     avcodec_get_frame_defaults(&s->picture);
311     avctx->coded_frame = &s->picture;
312
313     return 0;
314 }
315
316 static av_cold int targa_end(AVCodecContext *avctx)
317 {
318     TargaContext *s = avctx->priv_data;
319
320     if (s->picture.data[0])
321         avctx->release_buffer(avctx, &s->picture);
322
323     return 0;
324 }
325
326 AVCodec ff_targa_decoder = {
327     .name           = "targa",
328     .type           = AVMEDIA_TYPE_VIDEO,
329     .id             = AV_CODEC_ID_TARGA,
330     .priv_data_size = sizeof(TargaContext),
331     .init           = targa_init,
332     .close          = targa_end,
333     .decode         = decode_frame,
334     .capabilities   = CODEC_CAP_DR1,
335     .long_name      = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
336 };