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