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